hks_mbedtls_aes.c代码分析
一、背景知识
自己对代码的一些见解 这里主要是使用mbedtls库中aes对应的一些函数,该库中将功能基本封装好。鸿蒙这里进行了进一步的适配和封装。主要实现一些密钥的生成,加解密的实现。这里的加解密相比其他文件有一个特殊点:就是加解密都封装在一个函数内,在函数形参中传入逻辑变量来实现。相当于一个开关变量,开的时候实现加密,关闭的时候实现解密。真正的封装好各部分功能。
aes算法介绍链接:
- 队友的博客
- csdn好文分享
二、代码分析
1.密钥的生成 实现最基础的密钥生成(基于随机数的生成实现密钥生成,填写随机数据进key中data)
int32_t HksMbedtlsAesGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
{
const uint32_t keyByteLen = spec->keyLen / HKS_BITS_PER_BYTE;
uint8_t *outKey = (uint8_t *)HksMalloc(keyByteLen);
if (outKey == NULL) {
return HKS_ERROR_MALLOC_FAIL;
}
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctrDrbg;
int32_t ret = HksCtrDrbgSeed(&ctrDrbg, &entropy);
if (ret != HKS_SUCCESS) {
HKS_FREE_PTR(outKey);
return ret;
}
do {
ret = mbedtls_ctr_drbg_random(&ctrDrbg, outKey, keyByteLen);
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtls ctr drbg random failed! mbedtls ret = 0x%X", ret);
(void)memset_s(outKey, keyByteLen, 0, keyByteLen);
HKS_FREE_PTR(outKey);
break;
}
key->data = outKey;
key->size = keyByteLen;
} while (0);
mbedtls_ctr_drbg_free(&ctrDrbg);
mbedtls_entropy_free(&entropy);
return ret;
}
2.aes/cbc在nopadding模式下的加密 第一种加密模式,其中参数最重要的个人认为是这个bool变量 encrypt。他象个开关控制是加密还是解密。 参数介绍:
- key:加解密的密钥
- cipherParam:密文参数信息
- message:加密的结果会生成一个消息
- encrypt:逻辑变量控制是加密还是解密
- ciphetext:密文信息
static int32_t AesCbcNoPaddingCrypt(const struct HksBlob *key, const struct HksCipherParam *cipherParam,
const struct HksBlob *message, const bool encrypt, struct HksBlob *cipherText)
{
mbedtls_aes_context ctx;
mbedtls_aes_init(&ctx);
int32_t ret;
do {
if (encrypt) {
ret = mbedtls_aes_setkey_enc(&ctx, key->data, key->size * HKS_BITS_PER_BYTE);
} else {
ret = mbedtls_aes_setkey_dec(&ctx, key->data, key->size * HKS_BITS_PER_BYTE);
}
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtls aes set key failed! mbedtls ret = 0x%X", ret);
break;
}
uint8_t tmpIv[HKS_AES_CBC_NOPADDING_IV_SIZE];
if (memcpy_s(tmpIv, HKS_AES_CBC_NOPADDING_IV_SIZE, cipherParam->iv.data, cipherParam->iv.size) != EOK) {
HKS_LOG_E("Memcpy temp iv failed!");
break;
}
ret = mbedtls_aes_crypt_cbc(&ctx, (encrypt ? MBEDTLS_AES_ENCRYPT : MBEDTLS_AES_DECRYPT),
message->size, tmpIv, message->data, cipherText->data);
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtks aes cbc crypt failed! mbedtls ret = 0x%X", ret);
(void)memset_s(cipherText->data, cipherText->size, 0, cipherText->size);
break;
}
cipherText->size = message->size;
} while (0);
mbedtls_aes_free(&ctx);
return ret;
}
3.aes/cbc在pkcs7下的加密 该函数也是一种加解密的方式,上一种是nopadding,着一种是okcs7的方法。参数功能基本相似。
参数介绍:
- key:加解密的密钥
- cipherParam:密文参数信息
- message:加密的结果会生成一个消息
- encrypt:逻辑变量控制是加密还是解密
- ciphetext:密文信息
static int32_t AesCbcPkcs7Crypt(const struct HksBlob *key, const struct HksCipherParam *cipherParam,
const struct HksBlob *message, const bool encrypt, struct HksBlob *cipherText)
{
const uint32_t keyBitLen = key->size * HKS_BITS_PER_BYTE;
const mbedtls_cipher_info_t *info =
mbedtls_cipher_info_from_values(MBEDTLS_CIPHER_ID_AES, keyBitLen, MBEDTLS_MODE_CBC);
mbedtls_cipher_context_t ctx;
mbedtls_cipher_init(&ctx);
int32_t ret;
do {
ret = mbedtls_cipher_setup(&ctx, info);
if (ret != HKS_SUCCESS) {
HKS_LOG_E("Mbedtls cbc pkcs7 setup ctx failed! mbedtls ret = 0x%X", ret);
break;
}
ret = mbedtls_cipher_setkey(&ctx, key->data, keyBitLen, (encrypt ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT));
if (ret != HKS_SUCCESS) {
HKS_LOG_E("Mbedtls cbc pkcs7 set key failed! mbedtls ret = 0x%X", ret);
break;
}
ret = mbedtls_cipher_crypt(&ctx, cipherParam->iv.data, cipherParam->iv.size,
message->data, message->size, cipherText->data, (size_t *)&(cipherText->size));
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtls cbc pkcs7 crypt failed! mbedtls ret = 0x%X", ret);
(void)memset_s(cipherText->data, cipherText->size, 0, cipherText->size);
}
} while (0);
mbedtls_cipher_free(&ctx);
return ret;
}
4.aes/cbc的普通加密 该函数主要也是调用之前的加密方法,将算法写进usageSpec进行判断选用不同的模式进行加解密。
#if defined(HKS_SUPPORT_AES_CBC_NOPADDING) || defined(HKS_SUPPORT_AES_CBC_PKCS7)
static int32_t AesCbcCrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
const struct HksBlob *message, const bool encrypt, struct HksBlob *cipherText)
{
const struct HksCipherParam *cipherParam = (struct HksCipherParam *)(usageSpec->algParam);
switch (usageSpec->padding) {
#ifdef HKS_SUPPORT_AES_CBC_NOPADDING
case HKS_PADDING_NONE:
return AesCbcNoPaddingCrypt(key, cipherParam, message, encrypt, cipherText);
#endif
#ifdef HKS_SUPPORT_AES_CBC_PKCS7
case HKS_PADDING_PKCS7:
return AesCbcPkcs7Crypt(key, cipherParam, message, encrypt, cipherText);
#endif
default:
HKS_LOG_E("Unsupport padding! mode = 0x%X", usageSpec->padding);
return HKS_ERROR_INVALID_PADDING;
}
}
5.aes和gcm的加密 aes和gcm的加密模式。
static int32_t AesEncryptGcm(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
const struct HksBlob *message, struct HksBlob *cipherText, struct HksBlob *tagAead)
{
mbedtls_gcm_context ctx;
mbedtls_gcm_init(&ctx);
int32_t ret;
do {
ret = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key->data, key->size * HKS_BITS_PER_BYTE);
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtls aes gcm set key failed! mbedtls ret = 0x%X", ret);
break;
}
const struct HksAeadParam *aeadParam = (struct HksAeadParam *)(usageSpec->algParam);
ret = mbedtls_gcm_crypt_and_tag(&ctx, MBEDTLS_GCM_ENCRYPT, message->size,
aeadParam->nonce.data, aeadParam->nonce.size, aeadParam->aad.data, aeadParam->aad.size,
message->data, cipherText->data, tagAead->size, tagAead->data);
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtls aes gcm encryot failed! mbedtls ret = 0x%X", ret);
(void)memset_s(cipherText->data, cipherText->size, 0, cipherText->size);
(void)memset_s(tagAead->data, tagAead->size, 0, tagAead->size);
break;
}
cipherText->size = message->size;
} while (0);
mbedtls_gcm_free(&ctx);
return ret;
}
6.aes和gcm的解密 对应的解密函数,一般加密函数会对应一个唯一的解密函数。或者是将两个函数写在一起,用逻辑变量区分。
static int32_t AesDecryptGcm(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
const struct HksBlob *message, struct HksBlob *cipherText)
{
mbedtls_gcm_context ctx;
mbedtls_gcm_init(&ctx);
int32_t ret;
do {
ret = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key->data, key->size * HKS_BITS_PER_BYTE);
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtls aes gcm set key failed! mbedtls ret = 0x%X", ret);
break;
}
const struct HksAeadParam *aeadParam = (struct HksAeadParam *)(usageSpec->algParam);
ret = mbedtls_gcm_auth_decrypt(&ctx, message->size, aeadParam->nonce.data, aeadParam->nonce.size,
aeadParam->aad.data, aeadParam->aad.size, aeadParam->tagDec.data, aeadParam->tagDec.size,
message->data, cipherText->data);
if (ret != HKS_MBEDTLS_SUCCESS) {
HKS_LOG_E("Mbedtls aes gcm decrypt failed! mbedtls ret = 0x%X", ret);
(void)memset_s(cipherText->data, cipherText->size, 0, cipherText->size);
break;
}
cipherText->size = message->size;
} while (0);
mbedtls_gcm_free(&ctx);
return ret;
}
7.密钥的检查 检查密钥的位长度size是否位128、192、256等。
static int32_t CheckKeySize(const struct HksBlob *key)
{
if ((key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_128)) && (key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_192)) &&
(key->size != HKS_KEY_BYTES(HKS_AES_KEY_SIZE_256))) {
return HKS_ERROR_INVALID_KEY_SIZE;
}
return HKS_SUCCESS;
}
8.加密和解密功能的封装 总的函数功能封装,将会情况写进usagespec中,然后采用switch语句将不同函数封装进去,cbc将nopadding和pkcs7封装在了一起,整体叫做cbc加密,而相对应的另一种加解密方式就是gcm和aes的加解密模式,这里封装cbc和gcm的情况。同时实现了解密的封装。
int32_t HksMbedtlsAesEncrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
const struct HksBlob *message, struct HksBlob *cipherText, struct HksBlob *tagAead)
{
if (CheckKeySize(key) != HKS_SUCCESS) {
HKS_LOG_E("Invalid aes keySiz = 0x%X", key->size);
return HKS_ERROR_INVALID_KEY_SIZE;
}
switch (usageSpec->mode) {
#if defined(HKS_SUPPORT_AES_CBC_NOPADDING) || defined(HKS_SUPPORT_AES_CBC_PKCS7)
case HKS_MODE_CBC:
return AesCbcCrypt(key, usageSpec, message, true, cipherText);
#endif
#ifdef HKS_SUPPORT_AES_GCM
case HKS_MODE_GCM:
return AesEncryptGcm(key, usageSpec, message, cipherText, tagAead);
#endif
#ifdef HKS_SUPPORT_AES_CCM
case HKS_MODE_CCM:
return AesEncryptCcm(key, usageSpec, message, cipherText, tagAead);
#endif
default:
HKS_LOG_E("Unsupport key alg! mode = 0x%X", usageSpec->mode);
return HKS_ERROR_INVALID_ARGUMENT;
}
}
解密的封装
int32_t HksMbedtlsAesDecrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
const struct HksBlob *message, struct HksBlob *cipherText)
{
if (CheckKeySize(key) != HKS_SUCCESS) {
HKS_LOG_E("Invalid aes keySize = 0x%X", key->size);
return HKS_ERROR_INVALID_KEY_SIZE;
}
switch (usageSpec->mode) {
#if defined(HKS_SUPPORT_AES_CBC_NOPADDING) || defined(HKS_SUPPORT_AES_CBC_PKCS7)
case HKS_MODE_CBC:
return AesCbcCrypt(key, usageSpec, message, false, cipherText);
#endif
#ifdef HKS_SUPPORT_AES_GCM
case HKS_MODE_GCM:
return AesDecryptGcm(key, usageSpec, message, cipherText);
#endif
#ifdef HKS_SUPPORT_AES_CCM
case HKS_MODE_CCM:
return AesDecryptCcm(key, usageSpec, message, cipherText);
#endif
default:
HKS_LOG_E("Unsupport key alg! mode = 0x%X", usageSpec->mode);
return HKS_ERROR_INVALID_ARGUMENT;
}
}
|