72void CCMEncrypt(
const std::string &aes_key,
const std::string &iv,
73 const std::string &auth_data,
const std::string &plain, std::string *cipher,
76 ASSERT_EQ(32u, aes_key.size());
77 ASSERT_EQ(11u, iv.size());
80 ctx = EVP_CIPHER_CTX_new();
83 EVP_EncryptInit_ex(ctx, EVP_aes_256_ccm(),
nullptr,
nullptr,
nullptr));
85 constexpr int taglen = 16;
86 constexpr int Lvalue = 4;
87 constexpr int ivlen = 15 - Lvalue;
88 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, taglen,
nullptr);
89 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, ivlen, NULL);
90 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_L, Lvalue, NULL);
92 int key_length = EVP_CIPHER_CTX_key_length(ctx);
93 ASSERT_EQ(32, key_length);
95 1, EVP_EncryptInit_ex(ctx,
nullptr,
nullptr,
96 (
const uint8_t *)aes_key.data(), (
const uint8_t *)iv.data()));
100 1, EVP_EncryptUpdate(ctx,
nullptr, &outlen,
nullptr, plain.size()));
102 ASSERT_EQ(1, EVP_EncryptUpdate(ctx,
nullptr, &outlen,
103 (
const uint8_t *)auth_data.data(), auth_data.size()));
104 cipher->resize(plain.size() + 16);
105 ASSERT_EQ(1, EVP_EncryptUpdate(ctx, (uint8_t *)&((*cipher)[0]), &outlen,
106 (
const uint8_t *)plain.data(), plain.size()));
107 HASSERT(outlen <= (
int)cipher->size());
108 LOG(
INFO,
"encupdate: in=%d, ret=%d", (
int)plain.size(), outlen);
109 cipher->resize(outlen);
111 ASSERT_EQ((
int)plain.size(), outlen);
113 ASSERT_EQ(1, EVP_EncryptFinal_ex(ctx, (uint8_t *)&((*cipher)[0]), &ret));
118 1, EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, 16, &((*tag)[0])));
120 EVP_CIPHER_CTX_free(ctx);
void CCMEncrypt(const std::string &aes_key, const std::string &iv, const std::string &auth_data, const std::string &plain, std::string *cipher, std::string *tag)
Performs authenticated encryption using AES-CCM.