1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
/**
* Copyright (C) 2019-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/platform/basic.h"
#include "mongo/crypto/aead_encryption.h"
#include "mongo/base/data_view.h"
#include "mongo/crypto/sha256_block.h"
#include "mongo/crypto/sha512_block.h"
#include "mongo/crypto/symmetric_crypto.h"
#include "mongo/db/matcher/schema/encrypt_schema_gen.h"
#include "mongo/util/secure_compare_memory.h"
namespace mongo {
namespace crypto {
namespace {
constexpr size_t kIVSize = 16;
// AssociatedData can be 2^24 bytes but since there needs to be room for the ciphertext in the
// object, a value of 1<<16 was decided to cap the maximum size of AssociatedData.
constexpr int kMaxAssociatedDataLength = 1 << 16;
size_t aesCBCCipherOutputLength(size_t plainTextLen) {
return aesBlockSize * (1 + plainTextLen / aesBlockSize);
}
std::pair<size_t, size_t> aesCBCExpectedPlaintextLen(size_t cipherTextLength) {
return {cipherTextLength - aesCBCIVSize - aesBlockSize, cipherTextLength - aesCBCIVSize};
}
void aeadGenerateIV(DataRange buffer) {
static_assert(aesCTRIVSize == aesCBCIVSize);
if (buffer.length() < aesCBCIVSize) {
fassert(51235, "IV buffer is too small for selected mode");
}
auto status = engineRandBytes(buffer.slice(aesCBCIVSize));
if (!status.isOK()) {
fassert(51236, status);
}
}
StatusWith<std::size_t> _aesEncrypt(const SymmetricKey& key,
aesMode mode,
ConstDataRange in,
DataRange outRange,
bool ivProvided) try {
static_assert(aesCTRIVSize == aesCBCIVSize);
if (!ivProvided) {
aeadGenerateIV(outRange);
}
DataRangeCursor out(outRange);
DataRange iv = out.sliceAndAdvance(aesCBCIVSize);
auto encryptor = uassertStatusOK(SymmetricEncryptor::create(key, mode, iv));
const auto updateLen = uassertStatusOK(encryptor->update(in, out));
out.advance(updateLen);
const auto finalLen = uassertStatusOK(encryptor->finalize(out));
out.advance(finalLen);
// Some cipher modes, such as GCM, will know in advance exactly how large their ciphertexts will
// be. Others, like CBC, will have an upper bound. When this is true, we must allocate enough
// memory to store the worst case. We must then set the actual size of the ciphertext so that
// the buffer it has been written to may be serialized.
const auto len = updateLen + finalLen;
std::size_t anticipatedLen;
if (mode == aesMode::cbc) {
anticipatedLen = aesCBCCipherOutputLength(in.length());
} else if (mode == aesMode::ctr) {
anticipatedLen = in.length();
} else {
return {ErrorCodes::BadValue, "Unsupported AES mode"};
}
// Check the returned length, including block size padding
if (len != anticipatedLen) {
return {ErrorCodes::BadValue,
str::stream() << "Encrypt error, expected cipher text of length " << anticipatedLen
<< " but found " << len};
}
return aesCBCIVSize + len;
} catch (const AssertionException& ex) {
return ex.toStatus();
}
StatusWith<std::size_t> _aesDecrypt(const SymmetricKey& key,
aesMode mode,
ConstDataRange ivAndCipherText,
DataRange outRange) try {
// Check the plaintext buffer can fit the product of decryption
size_t lowerBound = 0, upperBound = 0;
if (mode == aesMode::cbc) {
auto anticipatedLen = aesCBCExpectedPlaintextLen(ivAndCipherText.length());
lowerBound = anticipatedLen.first;
upperBound = anticipatedLen.second;
} else if (mode == aesMode::ctr) {
lowerBound = upperBound = ivAndCipherText.length() - aesCBCIVSize;
} else {
return {ErrorCodes::BadValue, "Unsupported AES mode"};
}
if (upperBound > outRange.length()) {
return {ErrorCodes::BadValue,
str::stream() << "Cleartext buffer of size " << outRange.length()
<< " too small for output which can be as large as " << upperBound
<< "]"};
}
ConstDataRangeCursor in(ivAndCipherText);
auto iv = in.sliceAndAdvance(aesCBCIVSize);
auto decryptor = uassertStatusOK(SymmetricDecryptor::create(key, mode, iv));
DataRangeCursor out(outRange);
const auto updateLen = uassertStatusOK(decryptor->update(in, out));
out.advance(updateLen);
const auto finalLen = uassertStatusOK(decryptor->finalize(out));
out.advance(finalLen);
auto outputLen = updateLen + finalLen;
// Check the returned length, excluding headers block padding
if ((outputLen < lowerBound) || (outputLen > upperBound)) {
return {ErrorCodes::BadValue,
str::stream() << "Decrypt error, expected clear text length in interval"
<< "[" << lowerBound << "," << upperBound << "]"
<< "but found " << outputLen};
}
if (mode == aesMode::cbc) {
/* Check that padding was removed.
*
* PKCS7 padding guarantees that the encrypted payload has
* between 1 and blocksize bytes of padding which should be
* removed during the decrypt process.
*
* If resultLen is the same as the payload len,
* that means no padding was removed.
*
* macOS CommonCrypto will return such payloads when either the
* key or ciphertext are corrupted and its unable to find any
* expected padding. It fails open by returning whatever it can.
*/
if (outputLen >= in.length()) {
return {ErrorCodes::BadValue,
"Decrypt error, plaintext is as large or larger than "
"the ciphertext. This usually indicates an invalid key."};
}
}
return outputLen;
} catch (const AssertionException& ex) {
return ex.toStatus();
}
} // namespace
size_t aeadCipherOutputLength(size_t plainTextLen) {
// To calculate the size of the byte, we divide by the byte size and add 2 for padding
// (1 for the attached IV, and 1 for the extra padding). The algorithm will add padding even
// if the len is a multiple of the byte size, so if the len divides cleanly it will be
// 32 bytes longer than the original, which is 16 bytes as padding and 16 bytes for the
// IV. For things that don't divide cleanly, the cast takes care of floor dividing so it will
// be 0 < x < 16 bytes added for padding and 16 bytes added for the IV.
size_t aesOutLen = aesBlockSize * (plainTextLen / aesBlockSize + 2);
return aesOutLen + kHmacOutSize;
}
size_t fle2AeadCipherOutputLength(size_t plainTextLen, aesMode mode) {
switch (mode) {
case aesMode::ctr:
return plainTextLen + aesCTRIVSize + kHmacOutSize;
case aesMode::cbc:
return aesCBCCipherOutputLength(plainTextLen) + aesCBCIVSize + kHmacOutSize;
default:
uasserted(ErrorCodes::BadValue, "Unsupported AES mode");
}
}
size_t fle2CipherOutputLength(size_t plainTextLen) {
return plainTextLen + aesCTRIVSize;
}
Status aeadEncryptLocalKMS(const SymmetricKey& key, ConstDataRange in, DataRange out) {
if (key.getKeySize() != kFieldLevelEncryptionKeySize) {
return Status(ErrorCodes::BadValue,
"AEAD encryption key is the incorrect length. "
"Must be 96 bytes.");
}
// According to the rfc on AES encryption, the associatedDataLength is defined as the
// number of bits in associatedData in BigEndian format. This is what the code segment
// below describes.
// RFC: (https://tools.ietf.org/html/draft-mcgrew-aead-aes-cbc-hmac-sha2-01#section-2.1)
std::array<uint8_t, sizeof(uint64_t)> dataLenBitsEncodedStorage;
DataRange dataLenBitsEncoded(dataLenBitsEncodedStorage);
dataLenBitsEncoded.write<BigEndian<uint64_t>>(static_cast<uint64_t>(0));
ConstDataRange keyCDR(key.getKey(), kAeadAesHmacKeySize);
ConstDataRange empty(nullptr, 0);
return aeadEncryptWithIV(keyCDR, in, empty, empty, dataLenBitsEncoded, out);
}
Status aeadEncryptDataFrame(FLEEncryptionFrame& dataframe) {
auto associatedData = dataframe.getAssociatedData();
if (associatedData.length() >= kMaxAssociatedDataLength) {
return Status(ErrorCodes::BadValue,
str::stream()
<< "AssociatedData for encryption is too large. Cannot be larger than "
<< kMaxAssociatedDataLength << " bytes.");
}
// According to the rfc on AES encryption, the associatedDataLength is defined as the
// number of bits in associatedData in BigEndian format. This is what the code segment
// below describes.
// RFC: (https://tools.ietf.org/html/draft-mcgrew-aead-aes-cbc-hmac-sha2-01#section-2.1)
std::array<uint8_t, sizeof(uint64_t)> dataLenBitsEncodedStorage;
DataRange dataLenBitsEncoded(dataLenBitsEncodedStorage);
dataLenBitsEncoded.write<BigEndian<uint64_t>>(static_cast<uint64_t>(associatedData.length()) *
8);
auto key = dataframe.getKey();
auto plaintext = dataframe.getPlaintext();
if (key->getKeySize() != kFieldLevelEncryptionKeySize) {
return Status(ErrorCodes::BadValue, "Invalid key size.");
}
if (plaintext.data() == nullptr) {
return Status(ErrorCodes::BadValue, "Invalid AEAD plaintext input.");
}
if (key->getAlgorithm() != aesAlgorithm) {
return Status(ErrorCodes::BadValue, "Invalid algorithm for key.");
}
ConstDataRange iv(nullptr, 0);
SHA512Block hmacOutput;
if (dataframe.getFLEAlgorithmType() == FleAlgorithmInt::kDeterministic) {
const uint8_t* ivKey = key->getKey() + kAeadAesHmacKeySize;
hmacOutput = SHA512Block::computeHmac(
ivKey, sym256KeySize, {associatedData, dataLenBitsEncoded, plaintext});
static_assert(SHA512Block::kHashLength >= kIVSize,
"Invalid AEAD parameters. Generated IV too short.");
iv = ConstDataRange(hmacOutput.data(), kIVSize);
}
ConstDataRange aeadKey(key->getKey(), kAeadAesHmacKeySize);
DataRange out(dataframe.getCiphertextMutable(), dataframe.getDataLength());
return aeadEncryptWithIV(aeadKey, plaintext, iv, associatedData, dataLenBitsEncoded, out);
}
Status aeadEncryptWithIV(ConstDataRange key,
ConstDataRange in,
ConstDataRange iv,
ConstDataRange associatedData,
ConstDataRange dataLenBitsEncoded,
DataRange out) {
if (key.length() != kAeadAesHmacKeySize) {
return Status(ErrorCodes::BadValue, "Invalid key size.");
}
if (!(in.length() && out.length())) {
return Status(ErrorCodes::BadValue, "Invalid AEAD parameters.");
}
if (out.length() != aeadCipherOutputLength(in.length())) {
return Status(ErrorCodes::BadValue, "Invalid output buffer size.");
}
if (associatedData.length() >= kMaxAssociatedDataLength) {
return Status(ErrorCodes::BadValue,
str::stream()
<< "AssociatedData for encryption is too large. Cannot be larger than "
<< kMaxAssociatedDataLength << " bytes.");
}
bool ivProvided = false;
if (iv.length() != 0) {
invariant(iv.length() == 16);
out.write(iv);
ivProvided = true;
}
const auto* macKey = key.data<uint8_t>();
const auto* encKey = key.data<uint8_t>() + sym256KeySize;
SymmetricKey symEncKey(encKey, sym256KeySize, aesAlgorithm, "aesKey", 1);
std::size_t aesOutLen = out.length() - kHmacOutSize;
auto swEncrypt = _aesEncrypt(symEncKey, aesMode::cbc, in, {out.data(), aesOutLen}, ivProvided);
if (!swEncrypt.isOK()) {
return swEncrypt.getStatus();
}
// Split `out` into two separate ranges.
// One for the just written ciphertext,
// and another for the HMAC signature on the end.
DataRangeCursor outCursor(out);
auto cipherTextRange = outCursor.sliceAndAdvance(swEncrypt.getValue());
SHA512Block hmacOutput = SHA512Block::computeHmac(
macKey, sym256KeySize, {associatedData, cipherTextRange, dataLenBitsEncoded});
// We intentionally only write the first 256 bits of the digest produced by SHA512.
ConstDataRange truncatedHash(hmacOutput.data(), kHmacOutSize);
outCursor.writeAndAdvance(truncatedHash);
return Status::OK();
}
Status fle2AeadEncrypt(ConstDataRange key,
ConstDataRange in,
ConstDataRange iv,
ConstDataRange associatedData,
DataRange out,
aesMode mode) {
if (mode != aesMode::cbc && mode != aesMode::ctr) {
return {ErrorCodes::BadValue, "Unsupported AES mode"};
}
if (key.length() != kFieldLevelEncryption2KeySize) {
return Status(ErrorCodes::BadValue, "Invalid key size.");
}
if (!(in.length() && out.length())) {
return Status(ErrorCodes::BadValue, "Invalid AEAD parameters.");
}
static_assert(aesCTRIVSize == aesCBCIVSize);
if (0 != iv.length() && aesCTRIVSize != iv.length()) {
return Status(ErrorCodes::BadValue, "Invalid IV length.");
}
if (out.length() != fle2AeadCipherOutputLength(in.length(), mode)) {
return Status(ErrorCodes::BadValue, "Invalid output buffer size.");
}
if (associatedData.length() >= kMaxAssociatedDataLength) {
return Status(ErrorCodes::BadValue,
str::stream()
<< "AssociatedData for encryption is too large. Cannot be larger than "
<< kMaxAssociatedDataLength << " bytes.");
}
bool ivProvided = false;
if (iv.length() != 0) {
out.write(iv);
ivProvided = true;
}
auto encKey = key.data<uint8_t>();
auto macKey = key.data<uint8_t>() + sym256KeySize;
SymmetricKey symEncKey(encKey, sym256KeySize, aesAlgorithm, "aesKey", 1);
std::size_t aesOutLen = out.length() - kHmacOutSize;
auto swEncrypt = _aesEncrypt(symEncKey, mode, in, {out.data(), aesOutLen}, ivProvided);
if (!swEncrypt.isOK()) {
return swEncrypt.getStatus();
}
// Split `out` into two separate ranges.
// One for the just written ciphertext,
// and another for the HMAC signature on the end.
DataRangeCursor outCursor(out);
auto cipherTextRange = outCursor.sliceAndAdvance(swEncrypt.getValue());
SHA256Block hmacOutput =
SHA256Block::computeHmac(macKey, sym256KeySize, {associatedData, cipherTextRange});
uassert(ErrorCodes::InternalError, "HMAC size mismatch", kHmacOutSize == hmacOutput.size());
outCursor.writeAndAdvance(hmacOutput);
return Status::OK();
}
Status fle2Encrypt(ConstDataRange key, ConstDataRange in, ConstDataRange iv, DataRange out) {
if (key.length() != sym256KeySize) {
return Status(ErrorCodes::BadValue, "Invalid key size.");
}
if (!in.length()) {
return Status(ErrorCodes::BadValue, "Invalid buffer length.");
}
if (0 != iv.length() && aesCTRIVSize != iv.length()) {
return Status(ErrorCodes::BadValue, "Invalid IV length.");
}
if (out.length() != fle2CipherOutputLength(in.length())) {
return Status(ErrorCodes::BadValue, "Invalid output buffer size.");
}
bool ivProvided = false;
if (iv.length() != 0) {
out.write(iv);
ivProvided = true;
}
auto encKey = key.data<uint8_t>();
SymmetricKey symEncKey(encKey, sym256KeySize, aesAlgorithm, "aesKey", 1);
auto swEncrypt = _aesEncrypt(symEncKey, aesMode::ctr, in, out, ivProvided);
if (!swEncrypt.isOK()) {
return swEncrypt.getStatus();
}
return Status::OK();
}
StatusWith<std::size_t> aeadDecrypt(const SymmetricKey& key,
ConstDataRange in,
ConstDataRange associatedData,
DataRange out) {
if (key.getKeySize() < kAeadAesHmacKeySize) {
return Status(ErrorCodes::BadValue, "Invalid key size.");
}
if (!out.length()) {
return Status(ErrorCodes::BadValue, "Invalid AEAD parameters.");
}
if (in.length() < kHmacOutSize) {
return Status(ErrorCodes::BadValue, "Ciphertext is not long enough.");
}
size_t expectedMaximumPlainTextSize =
uassertStatusOK(aeadGetMaximumPlainTextLength(in.length()));
if (out.length() != expectedMaximumPlainTextSize) {
return Status(ErrorCodes::BadValue, "Output buffer must be as long as the cipherText.");
}
if (associatedData.length() >= kMaxAssociatedDataLength) {
return Status(ErrorCodes::BadValue,
str::stream()
<< "AssociatedData for encryption is too large. Cannot be larger than "
<< kMaxAssociatedDataLength << " bytes.");
}
const uint8_t* macKey = key.getKey();
const uint8_t* encKey = key.getKey() + sym256KeySize;
// Split input into actual ciphertext, and the HMAC bit at the end.
auto [cipherText, hmacRange] = in.split(in.length() - kHmacOutSize);
// According to the rfc on AES encryption, the associatedDataLength is defined as the
// number of bits in associatedData in BigEndian format. This is what the code segment
// below describes.
std::array<uint8_t, sizeof(uint64_t)> dataLenBitsEncodedStorage;
DataRange dataLenBitsEncoded(dataLenBitsEncodedStorage);
dataLenBitsEncoded.write<BigEndian<uint64_t>>(associatedData.length() * 8);
SHA512Block hmacOutput = SHA512Block::computeHmac(
macKey, sym256KeySize, {associatedData, cipherText, dataLenBitsEncoded});
// Note that while we produce a 512bit digest with SHA512,
// we only store and validate the first 256 bits (32 bytes).
if (consttimeMemEqual(reinterpret_cast<const unsigned char*>(hmacOutput.data()),
hmacRange.data<unsigned char>(),
kHmacOutSize) == false) {
return Status(ErrorCodes::BadValue, "HMAC data authentication failed.");
}
SymmetricKey symEncKey(encKey, sym256KeySize, aesAlgorithm, key.getKeyId(), 1);
return _aesDecrypt(symEncKey, aesMode::cbc, cipherText, out);
}
StatusWith<std::size_t> fle2AeadDecrypt(ConstDataRange key,
ConstDataRange in,
ConstDataRange associatedData,
DataRange out,
aesMode mode) {
if (mode != aesMode::cbc && mode != aesMode::ctr) {
return {ErrorCodes::BadValue, "Unsupported AES mode"};
}
if (key.length() < kFieldLevelEncryption2KeySize) {
return Status(ErrorCodes::BadValue, "Invalid key size.");
}
if (!out.length()) {
return Status(ErrorCodes::BadValue, "Invalid AEAD parameters.");
}
static_assert(aesCTRIVSize == aesCBCIVSize);
if (in.length() < (aesCTRIVSize + kHmacOutSize)) {
return Status(ErrorCodes::BadValue, "Ciphertext is not long enough.");
}
size_t expectedPlainTextSize = uassertStatusOK(fle2AeadGetMaximumPlainTextLength(in.length()));
if (out.length() != expectedPlainTextSize) {
return Status(ErrorCodes::BadValue, "Output buffer must be as long as the cipherText.");
}
if (associatedData.length() >= kMaxAssociatedDataLength) {
return Status(ErrorCodes::BadValue,
str::stream()
<< "AssociatedData for encryption is too large. Cannot be larger than "
<< kMaxAssociatedDataLength << " bytes.");
}
auto encKey = key.data<uint8_t>();
auto macKey = key.data<uint8_t>() + sym256KeySize;
auto [ivAndCipherText, hmacRange] = in.split(in.length() - kHmacOutSize);
SHA256Block hmacOutput =
SHA256Block::computeHmac(macKey, sym256KeySize, {associatedData, ivAndCipherText});
if (consttimeMemEqual(reinterpret_cast<const unsigned char*>(hmacOutput.data()),
hmacRange.data<unsigned char>(),
kHmacOutSize) == false) {
return Status(ErrorCodes::BadValue, "HMAC data authentication failed.");
}
SymmetricKey symEncKey(encKey, sym256KeySize, aesAlgorithm, "aesKey", 1);
return _aesDecrypt(symEncKey, mode, ivAndCipherText, out);
}
StatusWith<std::size_t> fle2Decrypt(ConstDataRange key, ConstDataRange in, DataRange out) {
if (key.length() != sym256KeySize) {
return Status(ErrorCodes::BadValue, "Invalid key size.");
}
if (in.length() <= aesCTRIVSize) {
return Status(ErrorCodes::BadValue, "Ciphertext is not long enough.");
}
size_t expectedPlainTextSize = uassertStatusOK(fle2GetPlainTextLength(in.length()));
if (out.length() != expectedPlainTextSize) {
return Status(ErrorCodes::BadValue, "Output buffer must be as long as the cipherText.");
}
auto encKey = key.data<uint8_t>();
SymmetricKey symEncKey(encKey, sym256KeySize, aesAlgorithm, "aesKey", 1);
return _aesDecrypt(symEncKey, aesMode::ctr, in, out);
}
Status aeadDecryptDataFrame(FLEDecryptionFrame& dataframe) {
auto ciphertext = dataframe.getCiphertext();
auto associatedData = dataframe.getAssociatedData();
auto& plaintext = dataframe.getPlaintextMutable();
auto swPlainSize = aeadDecrypt(*dataframe.getKey(), ciphertext, associatedData, plaintext);
if (swPlainSize.isOK()) {
plaintext.resize(swPlainSize.getValue());
} else {
plaintext.resize(0);
}
return swPlainSize.getStatus();
}
StatusWith<std::size_t> aeadDecryptLocalKMS(const SymmetricKey& key,
ConstDataRange cipher,
DataRange out) {
return aeadDecrypt(key, cipher, {nullptr, 0}, out);
}
} // namespace crypto
} // namespace mongo
|