@@ -400,42 +400,12 @@ StatusWith<std::vector<uint8_t>> encryptDataWithAssociatedData(ConstDataRange ke
400
400
return {out};
401
401
}
402
402
403
- StatusWith<std::vector<uint8_t >> encryptData (ConstDataRange key, ConstDataRange plainText) {
404
- MongoCryptStatus status;
405
- auto * fle2alg = _mcFLE2Algorithm ();
406
- auto ciphertextLen = fle2alg->get_ciphertext_len (plainText.length (), status);
407
- if (!status.isOK ()) {
408
- return status.toStatus ();
409
- }
410
- MongoCryptBuffer out;
411
- out.resize (ciphertextLen);
412
-
413
- MongoCryptBuffer iv;
414
- iv.resize (MONGOCRYPT_IV_LEN);
415
-
416
- uint32_t written;
417
-
418
- if (!fle2alg->do_encrypt (getGlobalMongoCrypt ()->crypto ,
419
- iv.get () /* iv */ ,
420
- NULL /* aad */ ,
421
- MongoCryptBuffer::borrow (key).get (),
422
- MongoCryptBuffer::borrow (plainText).get (),
423
- out.get (),
424
- &written,
425
- status)) {
426
- return status.toStatus ();
427
- }
428
-
429
- auto cdr = out.toCDR ();
430
- return std::vector<uint8_t >(cdr.data (), cdr.data () + cdr.length ());
431
- }
432
-
433
403
StatusWith<std::vector<uint8_t >> encryptData (ConstDataRange key, uint64_t value) {
434
404
435
405
std::array<char , sizeof (uint64_t )> bufValue;
436
406
DataView (bufValue.data ()).write <LittleEndian<uint64_t >>(value);
437
407
438
- return encryptData (key, bufValue);
408
+ return FLEUtil:: encryptData (key, bufValue);
439
409
}
440
410
441
411
StatusWith<std::vector<uint8_t >> decryptDataWithAssociatedData (ConstDataRange key,
@@ -499,7 +469,7 @@ StatusWith<std::vector<uint8_t>> packAndEncrypt(std::tuple<T1, T2> tuple, const
499
469
}
500
470
501
471
dassert (builder.getCursor ().length () == (sizeof (T1) + sizeof (T2)));
502
- return encryptData (token.toCDR (), builder.getCursor ());
472
+ return FLEUtil:: encryptData (token.toCDR (), builder.getCursor ());
503
473
}
504
474
505
475
@@ -2226,10 +2196,10 @@ StateCollectionTokensV2::Encrypted StateCollectionTokensV2::encrypt(const ECOCTo
2226
2196
DataBuilder builder (sizeof (PrfBlock) + 1 );
2227
2197
uassertStatusOK (builder.writeAndAdvance (_esc.toCDR ()));
2228
2198
uassertStatusOK (builder.writeAndAdvance (*_isLeaf));
2229
- encryptedTokens = uassertStatusOK (encryptData (token.toCDR (), builder.getCursor ()));
2199
+ encryptedTokens = uassertStatusOK (FLEUtil:: encryptData (token.toCDR (), builder.getCursor ()));
2230
2200
} else {
2231
2201
// Equality
2232
- encryptedTokens = uassertStatusOK (encryptData (token.toCDR (), _esc.toCDR ()));
2202
+ encryptedTokens = uassertStatusOK (FLEUtil:: encryptData (token.toCDR (), _esc.toCDR ()));
2233
2203
}
2234
2204
2235
2205
return StateCollectionTokensV2::Encrypted (std::move (encryptedTokens));
@@ -3019,7 +2989,8 @@ StatusWith<std::vector<uint8_t>> FLE2TagAndEncryptedMetadataBlock::serialize(
3019
2989
return swEncryptedCount;
3020
2990
}
3021
2991
3022
- auto swEncryptedZeros = encryptData (zerosEncryptionToken.toCDR (), ConstDataRange (zeros));
2992
+ auto swEncryptedZeros =
2993
+ FLEUtil::encryptData (zerosEncryptionToken.toCDR (), ConstDataRange (zeros));
3023
2994
if (!swEncryptedZeros.isOK ()) {
3024
2995
return swEncryptedZeros;
3025
2996
}
@@ -3168,8 +3139,8 @@ FLE2IndexedEqualityEncryptedValueV2 FLE2IndexedEqualityEncryptedValueV2::fromUne
3168
3139
FLE2IndexedEqualityEncryptedValueV2 value;
3169
3140
mc_FLE2IndexedEncryptedValueV2_t* iev = value._value .get ();
3170
3141
3171
- auto swServerEncryptedValue =
3172
- encryptData (serverEncryptionToken. toCDR (), ConstDataRange (clientEncryptedValueParam));
3142
+ auto swServerEncryptedValue = FLEUtil::encryptData (serverEncryptionToken. toCDR (),
3143
+ ConstDataRange (clientEncryptedValueParam));
3173
3144
uassertStatusOK (swServerEncryptedValue);
3174
3145
3175
3146
auto swSerializedMetadata = metadataBlockParam.serialize (serverDataDerivedToken);
@@ -3511,7 +3482,7 @@ StatusWith<std::vector<uint8_t>> FLE2IndexedRangeEncryptedValueV2::serialize(
3511
3482
uint8_t edgeCount = static_cast <uint8_t >(metadataBlocks.size ());
3512
3483
3513
3484
auto swEncryptedData =
3514
- encryptData (serverEncryptionToken.toCDR (), ConstDataRange (clientEncryptedValue));
3485
+ FLEUtil:: encryptData (serverEncryptionToken.toCDR (), ConstDataRange (clientEncryptedValue));
3515
3486
if (!swEncryptedData.isOK ()) {
3516
3487
return swEncryptedData;
3517
3488
}
@@ -3642,7 +3613,7 @@ FLE2IndexedTextEncryptedValue FLE2IndexedTextEncryptedValue::fromUnencrypted(
3642
3613
3643
3614
auto keyId = payload.getIndexKeyId ().toCDR ();
3644
3615
3645
- auto serverEncryptedValue = uassertStatusOK (encryptData (
3616
+ auto serverEncryptedValue = uassertStatusOK (FLEUtil:: encryptData (
3646
3617
payload.getServerEncryptionToken ().toCDR (), ConstDataRange (clientEncryptedValue)));
3647
3618
3648
3619
if (!_mongocrypt_buffer_copy_from_data_and_size (
@@ -4835,6 +4806,41 @@ StatusWith<std::vector<uint8_t>> FLEUtil::decryptData(ConstDataRange key,
4835
4806
return {out};
4836
4807
}
4837
4808
4809
+ StatusWith<std::vector<uint8_t >> FLEUtil::encryptData (ConstDataRange key,
4810
+ ConstDataRange plainText) {
4811
+ MongoCryptStatus status;
4812
+ // AES-256-CTR
4813
+ auto * fle2alg = _mcFLE2Algorithm ();
4814
+ auto ciphertextLen = fle2alg->get_ciphertext_len (plainText.length (), status);
4815
+ if (!status.isOK ()) {
4816
+ return status.toStatus ();
4817
+ }
4818
+ MongoCryptBuffer out;
4819
+ out.resize (ciphertextLen);
4820
+
4821
+ MongoCryptBuffer iv;
4822
+ iv.resize (MONGOCRYPT_IV_LEN);
4823
+ auto * crypto = getGlobalMongoCrypt ()->crypto ;
4824
+ if (!_mongocrypt_random (crypto, iv.get (), MONGOCRYPT_IV_LEN, status)) {
4825
+ return status.toStatus ();
4826
+ }
4827
+
4828
+ uint32_t written;
4829
+ if (!fle2alg->do_encrypt (crypto,
4830
+ iv.get () /* iv */ ,
4831
+ NULL /* aad */ ,
4832
+ MongoCryptBuffer::borrow (key).get (),
4833
+ MongoCryptBuffer::borrow (plainText).get (),
4834
+ out.get (),
4835
+ &written,
4836
+ status)) {
4837
+ return status.toStatus ();
4838
+ }
4839
+
4840
+ auto cdr = out.toCDR ();
4841
+ return std::vector<uint8_t >(cdr.data (), cdr.data () + cdr.length ());
4842
+ }
4843
+
4838
4844
template class ESCCollectionCommon <ESCTwiceDerivedTagToken, ESCTwiceDerivedValueToken>;
4839
4845
template class ESCCollectionCommon <AnchorPaddingKeyToken, AnchorPaddingValueToken>;
4840
4846
} // namespace mongo
0 commit comments