MCPcopy Create free account
hub / github.com/Snapchat/Valdi / encryptWithAd

Method encryptWithAd

libs/utils/src/utils/crypto/AesEncryptor.cpp:44–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42}
43
44std::optional<std::vector<uint8_t>> AesEncryptor::encryptWithAd(const uint8_t* plainText,
45 size_t len,
46 const uint8_t* additionalData,
47 size_t adLen) {
48 SC_ASSERT_NOTNULL(plainText);
49 SC_ASSERT(len > 0);
50 EVP_AEAD_CTX ctx;
51 if (EVP_AEAD_CTX_init(&ctx, _cipher, _key.data(), _key.size(), EVP_AEAD_max_tag_len(_cipher), nullptr) == 0) {
52 SCLOGE(kTag, "Failed to initialize encryption key");
53 logCryptoErrors();
54 SC_ASSERT(false, "Bad key");
55 return {};
56 }
57 std::vector<uint8_t> cipherText(len + EVP_AEAD_max_overhead(_cipher), 0);
58 size_t outLen;
59 if (EVP_AEAD_CTX_seal(&ctx,
60 cipherText.data(),
61 &outLen,
62 cipherText.capacity(),
63 _iv.data(),
64 _iv.size(),
65 plainText,
66 len,
67 additionalData,
68 adLen) == 0) {
69 SCLOGE(kTag, "Failed encryption");
70 logCryptoErrors();
71 return {};
72 }
73 SC_ASSERT(outLen == cipherText.size());
74 EVP_AEAD_CTX_cleanup(&ctx);
75 return {cipherText};
76}
77
78std::optional<std::vector<uint8_t>> AesEncryptor::decryptWithAd(const uint8_t* cipherText,
79 size_t len,

Callers

nothing calls this directly

Calls 4

logCryptoErrorsFunction · 0.85
dataMethod · 0.45
sizeMethod · 0.45
capacityMethod · 0.45

Tested by

no test coverage detected