diff --git a/common/src/utils.c b/common/src/utils.c index e4f10b291757e871687a450bd9d440e5bb062863..a66cbcec37214fd074620e18d284a3423ab9a0d3 100644 --- a/common/src/utils.c +++ b/common/src/utils.c @@ -45,7 +45,7 @@ bool IsClassMatch(const HcfObjectBase *obj, const char *className) if (strcmp(obj->getClass(), className) == 0) { return true; } else { - LOGE("class is not match. except class: %s, input class: %s", className, obj->getClass()); + LOGE("class is not match. expect class: %s, input class: %s", className, obj->getClass()); return false; } } diff --git a/frameworks/crypto_operation/cipher.c b/frameworks/crypto_operation/cipher.c index f44075d4547a21db7042479794c84bb4580a1459..4a898f2a10c376c535f13615fc51389436c73e68 100644 --- a/frameworks/crypto_operation/cipher.c +++ b/frameworks/crypto_operation/cipher.c @@ -25,11 +25,11 @@ #include "cipher_rsa_openssl.h" #include "utils.h" -typedef HcfResult (*HcfCipherGeneratorSpiCreateFunc)(CipherAttr *, OH_HCF_CipherGeneratorSpi **); +typedef HcfResult (*HcfCipherGeneratorSpiCreateFunc)(CipherAttr *, HcfCipherGeneratorSpi **); typedef struct { HcfCipher super; - OH_HCF_CipherGeneratorSpi *spiObj; + HcfCipherGeneratorSpi *spiObj; char algoName[HCF_MAX_ALGO_NAME_LEN]; } CipherGenImpl; @@ -133,7 +133,7 @@ static HcfResult OnSetParameter(const HcfParaConfig *config, void *cipher) static const char *GetCipherGeneratorClass(void) { - return "OH_HCF_CipherGenerator"; + return "HcfCipherGenerator"; } const char *GetAlogrithm(HcfCipher *self) @@ -206,7 +206,7 @@ static HcfResult CipherFinal(HcfCipher *self, HcfBlob *input, HcfBlob *output) return impl->spiObj->doFinal(impl->spiObj, input, output); } -static void InitCipher(OH_HCF_CipherGeneratorSpi *spiObj, CipherGenImpl *cipher) +static void InitCipher(HcfCipherGeneratorSpi *spiObj, CipherGenImpl *cipher) { cipher->super.init = CipherInit; cipher->super.update = CipherUpdate; @@ -222,7 +222,7 @@ static const HcfCipherGenFuncSet *FindAbility(CipherAttr *attr) return NULL; } for (uint32_t i = 0; i < sizeof(CIPHER_ABILITY_SET) / sizeof(HcfCipherGenAbility); i++) { - if (CIPHER_ABILITY_SET[i].algo == attr->algo) { + if (CIPHER_ABILITY_SET[i].algo == attr->algo) { return &(CIPHER_ABILITY_SET[i].funcSet); } } @@ -230,10 +230,10 @@ static const HcfCipherGenFuncSet *FindAbility(CipherAttr *attr) return NULL; } -HcfResult HcfCipherCreate(const char *transformation, HcfCipher **cipher) +HcfResult HcfCipherCreate(const char *transformation, HcfCipher **returnObj) { CipherAttr attr = {0}; - if (!IsStrValid(transformation, HCF_MAX_ALGO_NAME_LEN) || (cipher == NULL)) { + if (!IsStrValid(transformation, HCF_MAX_ALGO_NAME_LEN) || (returnObj == NULL)) { LOGE("Invalid input params while creating cipher!"); return HCF_INVALID_PARAMS; } @@ -257,7 +257,7 @@ HcfResult HcfCipherCreate(const char *transformation, HcfCipher **cipher) HcfFree(returnGenerator); return HCF_ERR_COPY; } - OH_HCF_CipherGeneratorSpi *spiObj = NULL; + HcfCipherGeneratorSpi *spiObj = NULL; HcfResult res = funcSet->createFunc(&attr, &spiObj); if (res != HCF_SUCCESS) { LOGE("Failed to create spi object!"); @@ -267,6 +267,6 @@ HcfResult HcfCipherCreate(const char *transformation, HcfCipher **cipher) returnGenerator->spiObj = spiObj; InitCipher(spiObj, returnGenerator); - *cipher = (HcfCipher *)returnGenerator; + *returnObj = (HcfCipher *)returnGenerator; return res; } diff --git a/frameworks/key/sym_key_generator.c b/frameworks/key/sym_key_generator.c index fb34edd579ed7d6092d418d1f124d7e47d4a5296..fce9ebcb6a99e4ef0c2d45089470fdddf5a627e0 100644 --- a/frameworks/key/sym_key_generator.c +++ b/frameworks/key/sym_key_generator.c @@ -30,7 +30,7 @@ #define AES_KEY_SIZE_256 256 #define DES_KEY_SIZE_192 192 -typedef HcfResult (*SymKeyGeneratorSpiCreateFunc)(SymKeyAttr *, OH_HCF_SymKeyGeneratorSpi **); +typedef HcfResult (*SymKeyGeneratorSpiCreateFunc)(SymKeyAttr *, HcfSymKeyGeneratorSpi **); typedef struct { SymKeyGeneratorSpiCreateFunc createFunc; @@ -43,7 +43,7 @@ typedef struct { typedef struct { HcfSymKeyGenerator base; - OH_HCF_SymKeyGeneratorSpi *spiObj; + HcfSymKeyGeneratorSpi *spiObj; char algoName[HCF_MAX_ALGO_NAME_LEN]; } HcfSymmKeyGeneratorImpl; @@ -58,7 +58,7 @@ static const SymKeyGenFuncSet *FindAbility(SymKeyAttr *attr) return NULL; } for (uint32_t i = 0; i < sizeof(SYMKEY_ABILITY_SET); i++) { - if (SYMKEY_ABILITY_SET[i].algo == attr->algo) { + if (SYMKEY_ABILITY_SET[i].algo == attr->algo) { return &(SYMKEY_ABILITY_SET[i].funcSet); } } @@ -173,9 +173,9 @@ static HcfResult ConvertSymmKey(HcfSymKeyGenerator *self, const HcfBlob *key, Hc return impl->spiObj->engineConvertSymmKey(impl->spiObj, key, symmKey); } -HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **generator) +HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **returnObj) { - if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (generator == NULL)) { + if (!IsStrValid(algoName, HCF_MAX_ALGO_NAME_LEN) || (returnObj == NULL)) { LOGE("Invalid input params while creating symkey!"); return HCF_INVALID_PARAMS; } @@ -201,7 +201,7 @@ HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **ge HcfFree(returnGenerator); return HCF_ERR_COPY; } - OH_HCF_SymKeyGeneratorSpi *spiObj = NULL; + HcfSymKeyGeneratorSpi *spiObj = NULL; int32_t res = funcSet->createFunc(&attr, &spiObj); if (res != HCF_SUCCESS) { LOGE("Failed to create spi object!"); @@ -215,6 +215,6 @@ HcfResult HcfSymKeyGeneratorCreate(const char *algoName, HcfSymKeyGenerator **ge returnGenerator->base.getAlgoName = GetAlgoName; returnGenerator->spiObj = spiObj; - *generator = (HcfSymKeyGenerator *)returnGenerator; + *returnObj = (HcfSymKeyGenerator *)returnGenerator; return HCF_SUCCESS; } diff --git a/frameworks/spi/cipher_factory_spi.h b/frameworks/spi/cipher_factory_spi.h index d53bb8a0bb196bb64150b26023c59adb52fa2aa2..1806bd955963d639ab7366db35d61bd60175c080 100644 --- a/frameworks/spi/cipher_factory_spi.h +++ b/frameworks/spi/cipher_factory_spi.h @@ -24,17 +24,17 @@ #include "result.h" #include "params_parser.h" -typedef struct OH_HCF_CipherGeneratorSpi OH_HCF_CipherGeneratorSpi; +typedef struct HcfCipherGeneratorSpi HcfCipherGeneratorSpi; -struct OH_HCF_CipherGeneratorSpi { +struct HcfCipherGeneratorSpi { HcfObjectBase base; - HcfResult (*init)(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoMode opMode, + HcfResult (*init)(HcfCipherGeneratorSpi *self, enum HcfCryptoMode opMode, HcfKey *key, HcfParamsSpec *params); - HcfResult (*update)(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output); + HcfResult (*update)(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output); - HcfResult (*doFinal)(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output); + HcfResult (*doFinal)(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output); }; #endif diff --git a/frameworks/spi/sym_key_factory_spi.h b/frameworks/spi/sym_key_factory_spi.h index c8ff9dc3bca0080508c6ba440803e7c2d306dfa1..5fbf4cc54055dcc3d86a98f34d138e13d6c2d301 100644 --- a/frameworks/spi/sym_key_factory_spi.h +++ b/frameworks/spi/sym_key_factory_spi.h @@ -20,12 +20,12 @@ #include "result.h" #include "sym_key.h" -typedef struct OH_HCF_SymKeyGeneratorSpi OH_HCF_SymKeyGeneratorSpi; +typedef struct HcfSymKeyGeneratorSpi HcfSymKeyGeneratorSpi; -struct OH_HCF_SymKeyGeneratorSpi { +struct HcfSymKeyGeneratorSpi { HcfObjectBase base; - HcfResult (*engineGenerateSymmKey)(OH_HCF_SymKeyGeneratorSpi *self, HcfSymKey **symmKey); - HcfResult (*engineConvertSymmKey)(OH_HCF_SymKeyGeneratorSpi *self, const HcfBlob *key, HcfSymKey **symmKey); + HcfResult (*engineGenerateSymmKey)(HcfSymKeyGeneratorSpi *self, HcfSymKey **symmKey); + HcfResult (*engineConvertSymmKey)(HcfSymKeyGeneratorSpi *self, const HcfBlob *key, HcfSymKey **symmKey); }; #define OPENSSL_SYM_GENERATOR_CLASS "OPENSSL.SYM.KEYGENERATOR" #define OPENSSL_SYM_KEY_CLASS "OPENSSL.SYM.KEY" diff --git a/interfaces/innerkits/crypto_operation/cipher.h b/interfaces/innerkits/crypto_operation/cipher.h index 0454c3d734e6790179e9daec5e7a6a3dad961b08..885b51e43494cbdb76adc473e564dc25a1bf1ba2 100644 --- a/interfaces/innerkits/crypto_operation/cipher.h +++ b/interfaces/innerkits/crypto_operation/cipher.h @@ -64,13 +64,13 @@ extern "C" { /** * @brief Generate a corresponding cryptographic operation cipher object according to the algorithm name. * - * @param algoName Specifies the type of generated cipher object. + * @param transformation Specifies the type of generated cipher object. * @param returnObj The address of the pointer to the generated cipher object. * @return Returns the status code of the execution. * @since 9 * @version 1.0 */ -HcfResult HcfCipherCreate(const char *algoName, HcfCipher **returnObj); +HcfResult HcfCipherCreate(const char *transformation, HcfCipher **returnObj); #ifdef __cplusplus } diff --git a/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl.h b/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl.h index 173a09bdc0af6232434bfa36d2a47c6d6c18602d..77d24f0b90c2bdb55ca42cd2e16e7d1d133c59f7 100644 --- a/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl.h +++ b/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl.h @@ -39,9 +39,9 @@ typedef struct { extern "C" { #endif -HcfResult HcfCipherDesGeneratorSpiCreate(CipherAttr *attr, OH_HCF_CipherGeneratorSpi **generator); +HcfResult HcfCipherDesGeneratorSpiCreate(CipherAttr *attr, HcfCipherGeneratorSpi **generator); -HcfResult HcfCipherAesGeneratorSpiCreate(CipherAttr *attr, OH_HCF_CipherGeneratorSpi **generator); +HcfResult HcfCipherAesGeneratorSpiCreate(CipherAttr *attr, HcfCipherGeneratorSpi **generator); #ifdef __cplusplus } diff --git a/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl_common.h b/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl_common.h index e64407ec2cfb76c0995e673a4a858e08e013bd04..39dee96111f21c1397343139cc2e39ac15dc3ed4 100644 --- a/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl_common.h +++ b/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl_common.h @@ -44,18 +44,14 @@ extern "C" { #endif const unsigned char *GetIv(HcfParamsSpec *params); -int32_t GetIvLen(HcfParamsSpec *params); - -int32_t GetGcmTagLen(HcfParamsSpec *params); - int32_t GetCcmTagLen(HcfParamsSpec *params); -void *GetGcmTag(HcfParamsSpec *params); - void *GetCcmTag(HcfParamsSpec *params); void FreeCipherData(CipherData **data); +void FreeRedundantOutput(HcfBlob *blob); + #ifdef __cplusplus } #endif diff --git a/plugin/openssl_plugin/crypto_operation/aes/src/cipher_3des_openssl.c b/plugin/openssl_plugin/crypto_operation/aes/src/cipher_3des_openssl.c index c8fed98d3c79077623d163cb25280344e3c0ab3b..6a1cc19a561017ff6e3aed49aa338d1b37de22c4 100644 --- a/plugin/openssl_plugin/crypto_operation/aes/src/cipher_3des_openssl.c +++ b/plugin/openssl_plugin/crypto_operation/aes/src/cipher_3des_openssl.c @@ -26,9 +26,10 @@ #include "openssl_class.h" #define DES_BLOCK_SIZE 8 +#define DES_SIZE_192 24 typedef struct { - OH_HCF_CipherGeneratorSpi base; + HcfCipherGeneratorSpi base; CipherAttr attr; CipherData *cipherData; } HcfCipherDesGeneratorSpiOpensslImpl; @@ -90,7 +91,7 @@ clearup: return ret; } -static HcfResult EngineCipherInit(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoMode opMode, +static HcfResult EngineCipherInit(HcfCipherGeneratorSpi *self, enum HcfCryptoMode opMode, HcfKey *key, HcfParamsSpec *params) { if ((self == NULL) || (key == NULL)) { /* params maybe is null */ @@ -108,9 +109,12 @@ static HcfResult EngineCipherInit(OH_HCF_CipherGeneratorSpi *self, enum HcfCrypt HcfCipherDesGeneratorSpiOpensslImpl *cipherImpl = (HcfCipherDesGeneratorSpiOpensslImpl *)self; SymKeyImpl *keyImpl = (SymKeyImpl *)key; - int32_t enc = (opMode == ENCRYPT_MODE) ? 1 : 0; + if (keyImpl->keyMaterial.len < DES_SIZE_192) { + LOGE("Init failed, the input key size is smaller than keySize specified in cipher."); + return HCF_INVALID_PARAMS; + } if (InitCipherData(opMode, &(cipherImpl->cipherData)) != HCF_SUCCESS) { LOGE("InitCipherData failed"); return HCF_INVALID_PARAMS; @@ -141,15 +145,10 @@ clearup: static HcfResult AllocateOutput(HcfBlob *input, HcfBlob *output) { - uint32_t outLen = 0; + uint32_t outLen = DES_BLOCK_SIZE; if (IsBlobValid(input)) { outLen += input->len; } - outLen += DES_BLOCK_SIZE; - if (outLen == 0) { - LOGE("output size is invaild!"); - return HCF_INVALID_PARAMS; - } output->data = (uint8_t *)HcfMalloc(outLen, 0); if (output->data == NULL) { LOGE("malloc output failed!"); @@ -159,7 +158,7 @@ static HcfResult AllocateOutput(HcfBlob *input, HcfBlob *output) return HCF_SUCCESS; } -static HcfResult EngineUpdate(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) +static HcfResult EngineUpdate(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) { if ((self == NULL) || (input == NULL) || (output == NULL)) { LOGE("Invalid input parameter!"); @@ -195,6 +194,8 @@ clearup: if (res != HCF_SUCCESS) { HcfBlobDataFree(output); FreeCipherData(&(cipherImpl->cipherData)); + } else { + FreeRedundantOutput(output); } return res; } @@ -224,7 +225,7 @@ static HcfResult DesDoFinal(CipherData *data, HcfBlob *input, HcfBlob *output) return HCF_SUCCESS; } -static HcfResult EngineDoFinal(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) +static HcfResult EngineDoFinal(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) { if ((self == NULL) || (output == NULL)) { /* input maybe is null */ LOGE("Invalid input parameter!"); @@ -253,6 +254,8 @@ static HcfResult EngineDoFinal(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, clearup: if (res != HCF_SUCCESS) { HcfBlobDataFree(output); + } else { + FreeRedundantOutput(output); } FreeCipherData(&(cipherImpl->cipherData)); return res; @@ -272,7 +275,7 @@ static void EngineDesGeneratorDestroy(HcfObjectBase *self) HcfFree(impl); } -HcfResult HcfCipherDesGeneratorSpiCreate(CipherAttr *attr, OH_HCF_CipherGeneratorSpi **generator) +HcfResult HcfCipherDesGeneratorSpiCreate(CipherAttr *attr, HcfCipherGeneratorSpi **generator) { if ((attr == NULL) || (generator == NULL)) { LOGE("Invalid input parameter!"); @@ -291,6 +294,6 @@ HcfResult HcfCipherDesGeneratorSpiCreate(CipherAttr *attr, OH_HCF_CipherGenerato returnImpl->base.base.destroy = EngineDesGeneratorDestroy; returnImpl->base.base.getClass = GetDesGeneratorClass; - *generator = (OH_HCF_CipherGeneratorSpi *)returnImpl; + *generator = (HcfCipherGeneratorSpi *)returnImpl; return HCF_SUCCESS; } diff --git a/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_common.c b/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_common.c index e416e1c75446ca4ecfa4d508c2c428385c56b4bd..933e3073cb4ed216054541a578f17e60013e788d 100644 --- a/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_common.c +++ b/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_common.c @@ -29,26 +29,6 @@ const unsigned char *GetIv(HcfParamsSpec *params) return (const unsigned char *)iv; } -int32_t GetIvLen(HcfParamsSpec *params) -{ - if (params == NULL) { - return 0; - } - HcfIvParamsSpec *spec = (HcfIvParamsSpec *)params; - size_t ivLen = spec->iv.len; - return (int)ivLen; -} - -int32_t GetGcmTagLen(HcfParamsSpec *params) -{ - if (params == NULL) { - return 0; - } - HcfGcmParamsSpec *spec = (HcfGcmParamsSpec *)params; - size_t tagLen = spec->tag.len; - return (int)tagLen; -} - int32_t GetCcmTagLen(HcfParamsSpec *params) { if (params == NULL) { @@ -59,16 +39,6 @@ int32_t GetCcmTagLen(HcfParamsSpec *params) return (int)tagLen; } -void *GetGcmTag(HcfParamsSpec *params) -{ - if (params == NULL) { - return NULL; - } - HcfGcmParamsSpec *spec = (HcfGcmParamsSpec *)params; - uint8_t *tag = spec->tag.data; - return (void *)tag; -} - void *GetCcmTag(HcfParamsSpec *params) { if (params == NULL) { @@ -103,3 +73,15 @@ void FreeCipherData(CipherData **data) HcfFree(*data); *data = NULL; } + +void FreeRedundantOutput(HcfBlob *blob) +{ + if (blob == NULL) { + return; + } + // when decrypt result is empty plaintext, out blob data maybe not null (malloc by hcf before decryption) + if ((blob->len == 0) && (blob->data != NULL)) { + HcfFree(blob->data); + blob->data = NULL; + } +} \ No newline at end of file diff --git a/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_openssl.c b/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_openssl.c index a9b2b8d5d0a33211f774166086fd82617cca0091..fc9cc0df858c46cb68c121e7bf74140a97e7a508 100644 --- a/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_openssl.c +++ b/plugin/openssl_plugin/crypto_operation/aes/src/cipher_aes_openssl.c @@ -31,9 +31,12 @@ #define AES_BLOCK_SIZE 16 #define GCM_TAG_SIZE 16 #define CCM_TAG_SIZE 12 +#define AES_SIZE_128 16 +#define AES_SIZE_192 24 +#define AES_SIZE_256 32 typedef struct { - OH_HCF_CipherGeneratorSpi base; + HcfCipherGeneratorSpi base; CipherAttr attr; CipherData *cipherData; } HcfCipherAesGeneratorSpiOpensslImpl; @@ -43,11 +46,6 @@ static const char *GetAesGeneratorClass(void) return OPENSSL_AES_CIPHER_CLASS; } -static const EVP_CIPHER *DefautCiherType() -{ - return EVP_aes_128_ecb(); -} - static const EVP_CIPHER *CipherEcbType(HCF_ALG_PARA_VALUE value) { switch (value) { @@ -199,6 +197,11 @@ static const EVP_CIPHER *CipherGcmType(HCF_ALG_PARA_VALUE value) return EVP_aes_128_gcm(); } +static const EVP_CIPHER *DefaultCiherType(HCF_ALG_PARA_VALUE value) +{ + return CipherEcbType(value); +} + static const EVP_CIPHER *GetCipherType(HcfCipherAesGeneratorSpiOpensslImpl *impl) { switch (impl->attr.mode) { @@ -225,7 +228,7 @@ static const EVP_CIPHER *GetCipherType(HcfCipherAesGeneratorSpiOpensslImpl *impl default: break; } - return DefautCiherType(); + return DefaultCiherType(impl->attr.keySize); } static bool IsGcmParamsValid(HcfGcmParamsSpec *params) @@ -330,7 +333,7 @@ static HcfResult InitAadAndTagFromCcmParams(enum HcfCryptoMode opMode, HcfCcmPar return HCF_SUCCESS; } -static HcfResult InitCipherData(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoMode opMode, +static HcfResult InitCipherData(HcfCipherGeneratorSpi *self, enum HcfCryptoMode opMode, HcfParamsSpec *params, CipherData **cipherData) { HcfResult ret = HCF_ERR_MALLOC; @@ -346,7 +349,7 @@ static HcfResult InitCipherData(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoM (*cipherData)->ctx = EVP_CIPHER_CTX_new(); if ((*cipherData)->ctx == NULL) { HcfPrintOpensslError(); - LOGE("Failed to allocate ctx memroy!"); + LOGE("Failed to allocate ctx memory!"); goto clearup; } @@ -366,7 +369,23 @@ clearup: return ret; } -static HcfResult EngineCipherInit(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoMode opMode, +static HcfResult IsKeySizeMatchCipher(SymKeyImpl *keyImpl, HcfCipherAesGeneratorSpiOpensslImpl *cipherImpl) +{ + size_t keySize = keyImpl->keyMaterial.len; + HCF_ALG_PARA_VALUE cipherValue = cipherImpl->attr.keySize; + switch (cipherValue) { + case HCF_ALG_AES_128: + return (keySize < AES_SIZE_128) ? HCF_INVALID_PARAMS : HCF_SUCCESS; + case HCF_ALG_AES_192: + return (keySize < AES_SIZE_192) ? HCF_INVALID_PARAMS : HCF_SUCCESS; + case HCF_ALG_AES_256: + return (keySize < AES_SIZE_256) ? HCF_INVALID_PARAMS : HCF_SUCCESS; + default: + return HCF_INVALID_PARAMS; + } +} + +static HcfResult EngineCipherInit(HcfCipherGeneratorSpi *self, enum HcfCryptoMode opMode, HcfKey *key, HcfParamsSpec *params) { if ((self == NULL) || (key == NULL)) { /* params maybe is null */ @@ -384,11 +403,15 @@ static HcfResult EngineCipherInit(OH_HCF_CipherGeneratorSpi *self, enum HcfCrypt HcfCipherAesGeneratorSpiOpensslImpl *cipherImpl = (HcfCipherAesGeneratorSpiOpensslImpl *)self; SymKeyImpl *keyImpl = (SymKeyImpl *)key; int enc = (opMode == ENCRYPT_MODE) ? 1 : 0; - + if (IsKeySizeMatchCipher(keyImpl, cipherImpl) != HCF_SUCCESS) { + LOGE("Init failed, key size is smaller than cipher size."); + return HCF_INVALID_PARAMS; + } if (InitCipherData(self, opMode, params, &(cipherImpl->cipherData)) != HCF_SUCCESS) { LOGE("InitCipherData failed!"); return HCF_INVALID_PARAMS; } + CipherData *data = cipherImpl->cipherData; HcfResult ret = HCF_ERR_CRYPTO_OPERATION; if (EVP_CipherInit(data->ctx, GetCipherType(cipherImpl), keyImpl->keyMaterial.data, GetIv(params), enc) != @@ -460,16 +483,11 @@ static HcfResult AeadUpdate(CipherData *data, HCF_ALG_PARA_VALUE mode, HcfBlob * static HcfResult AllocateOutput(HcfBlob *input, HcfBlob *output, bool *isUpdateInput) { - uint32_t outLen = 0; + uint32_t outLen = AES_BLOCK_SIZE; if (IsBlobValid(input)) { outLen += input->len; *isUpdateInput = true; } - outLen += AES_BLOCK_SIZE; - if (outLen == 0) { - LOGE("output size is invaild!"); - return HCF_INVALID_PARAMS; - } output->data = (uint8_t *)HcfMalloc(outLen, 0); if (output->data == NULL) { LOGE("malloc output failed!"); @@ -479,7 +497,7 @@ static HcfResult AllocateOutput(HcfBlob *input, HcfBlob *output, bool *isUpdateI return HCF_SUCCESS; } -static HcfResult EngineUpdate(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) +static HcfResult EngineUpdate(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) { if ((self == NULL) || (input == NULL) || (output == NULL)) { LOGE("Invalid input parameter!"); @@ -513,6 +531,7 @@ static HcfResult EngineUpdate(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, H FreeCipherData(&(cipherImpl->cipherData)); } data->aead = false; + FreeRedundantOutput(output); return ret; } @@ -706,7 +725,7 @@ static HcfResult GcmDoFinal(CipherData *data, HcfBlob *input, HcfBlob *output) } } -static HcfResult EngineDoFinal(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) +static HcfResult EngineDoFinal(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) { if ((self == NULL) || (output == NULL)) { /* input maybe is null */ LOGE("Invalid input parameter!"); @@ -737,6 +756,7 @@ static HcfResult EngineDoFinal(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, if (ret != HCF_SUCCESS) { HcfBlobDataFree(output); } + FreeRedundantOutput(output); return ret; } @@ -755,7 +775,7 @@ static void EngineAesGeneratorDestroy(HcfObjectBase *self) HcfFree(impl); } -HcfResult HcfCipherAesGeneratorSpiCreate(CipherAttr *attr, OH_HCF_CipherGeneratorSpi **generator) +HcfResult HcfCipherAesGeneratorSpiCreate(CipherAttr *attr, HcfCipherGeneratorSpi **generator) { if ((attr == NULL) || (generator == NULL)) { LOGE("Invalid input parameter."); @@ -774,6 +794,6 @@ HcfResult HcfCipherAesGeneratorSpiCreate(CipherAttr *attr, OH_HCF_CipherGenerato returnImpl->base.base.destroy = EngineAesGeneratorDestroy; returnImpl->base.base.getClass = GetAesGeneratorClass; - *generator = (OH_HCF_CipherGeneratorSpi *)returnImpl; + *generator = (HcfCipherGeneratorSpi *)returnImpl; return HCF_SUCCESS; } diff --git a/plugin/openssl_plugin/crypto_operation/rsa/inc/cipher_rsa_openssl.h b/plugin/openssl_plugin/crypto_operation/rsa/inc/cipher_rsa_openssl.h index 5684edc52675b579d875f18d563c9d6f26cddce1..27b60cd9983f3b2518a32f386bbc3883d4426ae5 100644 --- a/plugin/openssl_plugin/crypto_operation/rsa/inc/cipher_rsa_openssl.h +++ b/plugin/openssl_plugin/crypto_operation/rsa/inc/cipher_rsa_openssl.h @@ -22,7 +22,7 @@ extern "C" { #endif -HcfResult HcfCipherRsaCipherSpiCreate(CipherAttr *params, OH_HCF_CipherGeneratorSpi **generator); +HcfResult HcfCipherRsaCipherSpiCreate(CipherAttr *params, HcfCipherGeneratorSpi **generator); #ifdef __cplusplus } diff --git a/plugin/openssl_plugin/crypto_operation/rsa/src/cipher_rsa_openssl.c b/plugin/openssl_plugin/crypto_operation/rsa/src/cipher_rsa_openssl.c index 10f23080368e116a9c01e925d7e799fd0e3ad92a..63261646d5b66cf04976c1b687472aaeaecdecba 100644 --- a/plugin/openssl_plugin/crypto_operation/rsa/src/cipher_rsa_openssl.c +++ b/plugin/openssl_plugin/crypto_operation/rsa/src/cipher_rsa_openssl.c @@ -28,7 +28,7 @@ static const char *EngineGetClass(void); typedef struct { - OH_HCF_CipherGeneratorSpi super; + HcfCipherGeneratorSpi super; CipherAttr attr; @@ -157,7 +157,7 @@ static HcfResult SetDetailParams(HcfCipherRsaGeneratorSpiImpl *impl) return HCF_SUCCESS; } -static HcfResult EngineInit(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoMode opMode, +static HcfResult EngineInit(HcfCipherGeneratorSpi *self, enum HcfCryptoMode opMode, HcfKey *key, HcfParamsSpec *params) { LOGI("EngineInit start"); @@ -192,7 +192,7 @@ static HcfResult EngineInit(OH_HCF_CipherGeneratorSpi *self, enum HcfCryptoMode return HCF_SUCCESS; } -static HcfResult EngineUpdata(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) +static HcfResult EngineUpdata(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) { LOGE("Openssl don't support update"); (void)self; @@ -219,7 +219,7 @@ static HcfResult DoRsaCrypt(EVP_PKEY_CTX *ctx, HcfBlob *input, HcfBlob *output, return HCF_SUCCESS; } -static HcfResult EngineDoFinal(OH_HCF_CipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) +static HcfResult EngineDoFinal(HcfCipherGeneratorSpi *self, HcfBlob *input, HcfBlob *output) { LOGI("EngineDoFinal start"); if (self == NULL || input == NULL || input->data == NULL) { @@ -305,7 +305,7 @@ static HcfResult CheckRsaCipherParams(CipherAttr *params) return HCF_SUCCESS; } -HcfResult HcfCipherRsaCipherSpiCreate(CipherAttr *params, OH_HCF_CipherGeneratorSpi **generator) +HcfResult HcfCipherRsaCipherSpiCreate(CipherAttr *params, HcfCipherGeneratorSpi **generator) { LOGI("Start create rsa cipher spiObj."); if (generator == NULL || params == NULL) { @@ -332,7 +332,7 @@ HcfResult HcfCipherRsaCipherSpiCreate(CipherAttr *params, OH_HCF_CipherGenerator returnImpl->super.base.destroy = EngineDestroySpiImpl; returnImpl->super.base.getClass = EngineGetClass; returnImpl->initFlag = UNINITIALIZED; - *generator = (OH_HCF_CipherGeneratorSpi *)returnImpl; + *generator = (HcfCipherGeneratorSpi *)returnImpl; LOGI("Rsa Cipher create success."); return HCF_SUCCESS; } diff --git a/plugin/openssl_plugin/key/sym_key_generator/inc/sym_common_defines.h b/plugin/openssl_plugin/key/sym_key_generator/inc/sym_common_defines.h index 71cd093a314b1b5c50d0367c6a4b9595169b3e3c..b9f626f6643903213c0d9d68411053d4106d5664 100644 --- a/plugin/openssl_plugin/key/sym_key_generator/inc/sym_common_defines.h +++ b/plugin/openssl_plugin/key/sym_key_generator/inc/sym_common_defines.h @@ -35,7 +35,7 @@ typedef struct { extern "C" { #endif -HcfResult HcfSymKeyGeneratorSpiCreate(SymKeyAttr *attr, OH_HCF_SymKeyGeneratorSpi **genertor); +HcfResult HcfSymKeyGeneratorSpiCreate(SymKeyAttr *attr, HcfSymKeyGeneratorSpi **genertor); #ifdef __cplusplus } diff --git a/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c b/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c index 5773ce7730c10cb0bb8b25368f17bb1989d718ba..958059022a4c379fee70f4e0b6c775e0eaecd9ce 100644 --- a/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c +++ b/plugin/openssl_plugin/key/sym_key_generator/src/sym_key_openssl.c @@ -29,7 +29,7 @@ #define DES_ALG_NAME "3DES" typedef struct { - OH_HCF_SymKeyGeneratorSpi base; + HcfSymKeyGeneratorSpi base; SymKeyAttr attr; } HcfSymKeyGeneratorSpiOpensslImpl; @@ -119,7 +119,7 @@ static HcfResult RandomSymmKey(int32_t keyLen, HcfBlob *symmKey) LOGE("keyMaterial malloc failed!"); return HCF_ERR_MALLOC; } - int ret = RAND_bytes(keyMaterial, keyLen); + int ret = RAND_priv_bytes(keyMaterial, keyLen); if (ret != HCF_OPENSSL_SUCCESS) { LOGE("RAND_bytes failed!"); HcfPrintOpensslError(); @@ -243,7 +243,7 @@ static HcfResult CopySymmKey(const HcfBlob *srcKey, HcfBlob *dstKey) return HCF_SUCCESS; } -static HcfResult GenerateSymmKey(OH_HCF_SymKeyGeneratorSpi *self, HcfSymKey **symmKey) +static HcfResult GenerateSymmKey(HcfSymKeyGeneratorSpi *self, HcfSymKey **symmKey) { if ((self == NULL) || (symmKey == NULL)) { LOGE("Invalid input parameter!"); @@ -275,7 +275,7 @@ static HcfResult GenerateSymmKey(OH_HCF_SymKeyGeneratorSpi *self, HcfSymKey **sy return HCF_SUCCESS; } -static HcfResult ConvertSymmKey(OH_HCF_SymKeyGeneratorSpi *self, const HcfBlob *key, HcfSymKey **symmKey) +static HcfResult ConvertSymmKey(HcfSymKeyGeneratorSpi *self, const HcfBlob *key, HcfSymKey **symmKey) { if ((self == NULL) || (symmKey == NULL) || !IsBlobValid(key)) { LOGE("Invalid input parameter."); @@ -313,7 +313,7 @@ static HcfResult ConvertSymmKey(OH_HCF_SymKeyGeneratorSpi *self, const HcfBlob * return HCF_SUCCESS; } -HcfResult HcfSymKeyGeneratorSpiCreate(SymKeyAttr *attr, OH_HCF_SymKeyGeneratorSpi **generator) +HcfResult HcfSymKeyGeneratorSpiCreate(SymKeyAttr *attr, HcfSymKeyGeneratorSpi **generator) { if ((attr == NULL) || (generator == NULL)) { LOGE("Invalid input parameter."); @@ -330,7 +330,7 @@ HcfResult HcfSymKeyGeneratorSpiCreate(SymKeyAttr *attr, OH_HCF_SymKeyGeneratorSp returnGenerator->base.engineConvertSymmKey = ConvertSymmKey; returnGenerator->base.base.destroy = DestroySymKeyGeneratorSpi; returnGenerator->base.base.getClass = GetSymKeyGeneratorClass; - *generator = (OH_HCF_SymKeyGeneratorSpi *)returnGenerator; + *generator = (HcfSymKeyGeneratorSpi *)returnGenerator; return HCF_SUCCESS; } diff --git a/test/unittest/src/crypto_3des_cipher_test.cpp b/test/unittest/src/crypto_3des_cipher_test.cpp index 675470f75e1065ca6074b0796b1b243473744b74..fd730808ac67c69564a33bf4bf516dadbb789779 100644 --- a/test/unittest/src/crypto_3des_cipher_test.cpp +++ b/test/unittest/src/crypto_3des_cipher_test.cpp @@ -23,12 +23,16 @@ #include "detailed_iv_params.h" #include "detailed_gcm_params.h" #include "detailed_ccm_params.h" - +#include "aes_openssl.h" using namespace std; using namespace testing::ext; namespace { +constexpr int32_t CIPHER_TEXT_LEN = 128; +constexpr int32_t DES_IV_LEN = 8; +constexpr int32_t PLAINTEXT_LEN = 13; + class Crypto3DesCipherTest : public testing::Test { public: static void SetUpTestCase(); @@ -48,6 +52,24 @@ void Crypto3DesCipherTest::TearDown() // add destroy here, this will be called w { } +static int32_t GenerateDesSymKey(HcfSymKey **key) +{ + HcfSymKeyGenerator *generator = nullptr; + + int32_t ret = HcfSymKeyGeneratorCreate("3DES192", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!"); + return ret; + } + + ret = generator->generateSymKey(generator, key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + } + HcfObjDestroy(generator); + return ret; +} + static int32_t DesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, uint8_t *cipherText, int *cipherTextLen) { @@ -67,25 +89,26 @@ static int32_t DesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *para return ret; } *cipherTextLen = output.len; - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); - } - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + HcfBlobDataFree(&output); } + ret = cipher->doFinal(cipher, NULL, &output); if (ret != 0) { LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len); - } - *cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; + if (output.data != nullptr) { + if (memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + *cipherTextLen += output.len; + HcfBlobDataFree(&output); } return 0; } @@ -108,33 +131,33 @@ static int32_t DesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *para LOGE("update failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); - } cipherTextLen = output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; - output.len = 0; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + HcfBlobDataFree(&output); } + ret = cipher->doFinal(cipher, NULL, &output); if (ret != 0) { LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len); - } - cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; - output.len = 0; + if (output.data != nullptr) { + if (memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherTextLen += output.len; + HcfBlobDataFree(&output); } - ret = memcmp(cipherText, plainText, cipherTextLen); - ret = ret || (sizeof(plainText) - 1 == cipherTextLen) ? 0 : 1; - return ret; + if (cipherTextLen != sizeof(plainText) - 1) { + return -1; + } + return memcmp(cipherText, plainText, cipherTextLen); } static int32_t DesNoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, @@ -156,13 +179,13 @@ static int32_t DesNoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSp LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); - } - *cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + *cipherTextLen += output.len; + HcfBlobDataFree(&output); } return 0; } @@ -186,19 +209,19 @@ static int32_t DesNoUpdateDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSp LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); - } - cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; - output.len = 0; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherTextLen += output.len; + HcfBlobDataFree(&output); } - ret = memcmp(cipherText, plainText, cipherTextLen); - ret = ret || (sizeof(plainText) - 1 == cipherTextLen) ? 0 : 1; - return ret; + if (cipherTextLen != sizeof(plainText) - 1) { + return -1; + } + return memcmp(cipherText, plainText, cipherTextLen); } HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest001, TestSize.Level0) @@ -1405,4 +1428,391 @@ clearup: HcfObjDestroy((HcfObjectBase *)generator); EXPECT_EQ(ret, 0); } -} \ No newline at end of file + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest025, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t iv[DES_IV_LEN] = { 0 }; + HcfIvParamsSpec ivSpec = {}; + ivSpec.iv.data = iv; + ivSpec.iv.len = DES_IV_LEN; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|CFB1|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = DesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("DesEncrypt failed! %d", ret); + goto clearup; + } + + ret = DesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("DesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest026, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t iv[DES_IV_LEN] = { 0 }; + HcfIvParamsSpec ivSpec = {}; + ivSpec.iv.data = iv; + ivSpec.iv.len = DES_IV_LEN; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|CFB8|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = DesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("DesEncrypt failed! %d", ret); + goto clearup; + } + + ret = DesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("DesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest027, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t iv[DES_IV_LEN] = { 0 }; + HcfIvParamsSpec ivSpec = {}; + ivSpec.iv.data = iv; + ivSpec.iv.len = DES_IV_LEN; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = DesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("DesEncrypt failed! %d", ret); + goto clearup; + } + + ret = DesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("DesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest028, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(nullptr, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest029, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = HcfSymKeyGeneratorCreate("3DES192", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!"); + goto clearup; + } + + ret = generator->generateSymKey(generator, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(reinterpret_cast(generator), ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest030, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast(cipher), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest031, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test!"; + HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; + HcfBlob output = { .data = nullptr, .len = 0 }; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + goto clearup; + } + ret = cipher->update(nullptr, &input, &output); + if (ret != 0) { + LOGE("update failed!"); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + if (output.data != nullptr) { + HcfFree(output.data); + output.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest032, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test!"; + HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; + HcfBlob output = { .data = nullptr, .len = 0 }; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + goto clearup; + } + ret = cipher->update(reinterpret_cast(key), &input, &output); + if (ret != 0) { + LOGE("update failed!"); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + if (output.data != nullptr) { + HcfFree(output.data); + output.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest033, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test!"; + HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; + HcfBlob output = { .data = nullptr, .len = 0 }; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + goto clearup; + } + ret = cipher->doFinal(nullptr, &input, &output); + if (ret != 0) { + LOGE("doFinal failed!"); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + if (output.data != nullptr) { + HcfFree(output.data); + output.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest034, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test!"; + HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; + HcfBlob output = { .data = nullptr, .len = 0 }; + + ret = GenerateDesSymKey(&key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("3DES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + goto clearup; + } + ret = cipher->doFinal(reinterpret_cast(key), &input, &output); + if (ret != 0) { + LOGE("doFinal failed!"); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + if (output.data != nullptr) { + HcfFree(output.data); + output.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest035, TestSize.Level0) +{ + int ret = HcfCipherDesGeneratorSpiCreate(nullptr, nullptr); + if (ret != 0) { + LOGE("HcfCipherDesGeneratorSpiCreate failed!"); + } + EXPECT_NE(ret, 0); +} +} diff --git a/test/unittest/src/crypto_aes_cipher_test.cpp b/test/unittest/src/crypto_aes_cipher_test.cpp index 96c79506d4c4d6d341dd4bfa47d57c200e1ee5fd..4078e8dd05500b1a353dde503649ede584c06e7f 100644 --- a/test/unittest/src/crypto_aes_cipher_test.cpp +++ b/test/unittest/src/crypto_aes_cipher_test.cpp @@ -18,14 +18,16 @@ #include #include "securec.h" -#include "sym_key_generator.h" +#include "aes_openssl.h" +#include "blob.h" #include "cipher.h" -#include "log.h" -#include "memory.h" #include "detailed_iv_params.h" #include "detailed_gcm_params.h" #include "detailed_ccm_params.h" - +#include "log.h" +#include "memory.h" +#include "sym_common_defines.h" +#include "sym_key_generator.h" using namespace std; using namespace testing::ext; @@ -34,6 +36,16 @@ namespace { const int32_t FILE_BLOCK_SIZE = 1024; const int32_t RAND_MAX_NUM = 100; const bool IS_DEBUG = false; +constexpr int32_t CIPHER_TEXT_LEN = 128; +constexpr int32_t KEY_MATERIAL_LEN = 16; +constexpr int32_t AES_IV_LEN = 16; // iv for CBC|CTR|OFB|CFB mode +constexpr int32_t GCM_IV_LEN = 12; // GCM +constexpr int32_t GCM_AAD_LEN = 8; +constexpr int32_t GCM_TAG_LEN = 16; +constexpr int32_t CCM_IV_LEN = 7; // CCM +constexpr int32_t CCM_AAD_LEN = 8; +constexpr int32_t CCM_TAG_LEN = 12; +constexpr int32_t PLAINTEXT_LEN = 13; class CryptoAesCipherTest : public testing::Test { public: @@ -276,6 +288,50 @@ clearup: return ret; } +// use ECB, test abnormal input +static int32_t AesEncryptWithInput(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, + uint8_t *cipherText, int *cipherTextLen) +{ + HcfBlob output = { .data = nullptr, .len = 0 }; + int32_t maxLen = *cipherTextLen; + int32_t ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + return ret; + } + + ret = cipher->update(cipher, input, &output); + if (ret != 0) { + LOGE("update failed!"); + return ret; + } + *cipherTextLen = output.len; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + HcfBlobDataFree(&output); + } + + ret = cipher->doFinal(cipher, nullptr, &output); + if (ret != 0) { + LOGE("doFinal failed!"); + return ret; + } + if (output.data != nullptr) { + if (memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + *cipherTextLen += output.len; + HcfBlobDataFree(&output); + } + + PrintfHex("ciphertext", cipherText, *cipherTextLen); + return 0; +} + static int32_t AesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, uint8_t *cipherText, int *cipherTextLen) { @@ -295,26 +351,28 @@ static int32_t AesEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *para return ret; } *cipherTextLen = output.len; - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); - } - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + HcfBlobDataFree(&output); } + ret = cipher->doFinal(cipher, NULL, &output); if (ret != 0) { LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len); - } - *cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; + if (output.data != nullptr) { + if (memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + *cipherTextLen += output.len; + HcfBlobDataFree(&output); } + PrintfHex("ciphertext", cipherText, *cipherTextLen); return 0; } @@ -337,32 +395,89 @@ static int32_t AesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *para LOGE("update failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); - } cipherTextLen = output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; - output.len = 0; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + HcfBlobDataFree(&output); } + ret = cipher->doFinal(cipher, NULL, &output); if (ret != 0) { LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len); + if (output.data != nullptr) { + if (memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherTextLen += output.len; + HcfBlobDataFree(&output); } - cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; - output.len = 0; + + PrintfHex("plainText", cipherText, cipherTextLen); + if (cipherTextLen != sizeof(plainText) - 1) { + return -1; + } + return memcmp(cipherText, plainText, cipherTextLen); +} + +static int32_t AesNoUpdateEncWithInput(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, + uint8_t *cipherText, int *cipherTextLen) +{ + HcfBlob output = { .data = nullptr, .len = 0 }; + int32_t maxLen = *cipherTextLen; + int32_t ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! %d", ret); + return ret; + } + + *cipherTextLen = 0; + ret = cipher->doFinal(cipher, input, &output); + if (ret != 0) { + LOGE("doFinal failed!"); + return ret; + } + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + *cipherTextLen += output.len; + HcfBlobDataFree(&output); + } + + PrintfHex("ciphertext", cipherText, *cipherTextLen); + return 0; +} + +// test encrypt and decrypt with null plain text +static int32_t AesDecryptEmptyMsg(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, + uint8_t *cipherText, int cipherTextLen) +{ + HcfBlob input = { .data = cipherText, .len = cipherTextLen }; + HcfBlob output = { .data = nullptr, .len = 0 }; + int32_t ret = cipher->init(cipher, DECRYPT_MODE, &(key->key), params); + if (ret != 0) { + LOGE("init failed! %d", ret); + return ret; + } + + ret = cipher->doFinal(cipher, &input, &output); + if (ret != 0) { + LOGE("doFinal failed!"); + return ret; + } + if (output.len == 0 && output.data == nullptr) { + ret = 0; + } else { + ret = -1; } - PrintfHex("planText", cipherText, cipherTextLen); - ret = memcmp(cipherText, plainText, cipherTextLen); - ret = ret || (cipherTextLen == sizeof(plainText) - 1) ? 0 : 1; + HcfBlobDataFree(&output); return ret; } @@ -385,14 +500,15 @@ static int32_t AesNoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSp LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); - } - *cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + *cipherTextLen += output.len; + HcfBlobDataFree(&output); } + PrintfHex("ciphertext", cipherText, *cipherTextLen); return 0; } @@ -416,19 +532,20 @@ static int32_t AesNoUpdateDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSp LOGE("doFinal failed!"); return ret; } - if (output.len > 0 && output.data != NULL) { - (void)memcpy_s(cipherText, maxLen, output.data, output.len); + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherTextLen += output.len; + HcfBlobDataFree(&output); } - cipherTextLen += output.len; - if (output.data != NULL) { - HcfFree(output.data); - output.data = NULL; - output.len = 0; + + PrintfHex("plainText", cipherText, cipherTextLen); + if (cipherTextLen != sizeof(plainText) - 1) { + return -1; } - PrintfHex("planText", cipherText, cipherTextLen); - ret = memcmp(cipherText, plainText, cipherTextLen); - ret = ret || (cipherTextLen == sizeof(plainText) - 1) ? 0 : 1; - return ret; + return memcmp(cipherText, plainText, cipherTextLen); } /** @@ -1912,7 +2029,6 @@ clearup: EXPECT_EQ(ret, 0); } - HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest030, TestSize.Level0) { int ret = 0; @@ -1999,7 +2115,7 @@ HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest031, TestSize.Level0) ret = memcmp(cipherText, codeCipherText, cipherTextLen); if (ret != 0) { - LOGE("cipherText cpmpare failed!"); + LOGE("cipherText compare failed!"); goto clearup; } @@ -2127,7 +2243,7 @@ HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest034, TestSize.Level0) goto clearup; } - ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher); if (ret != 0) { LOGE("HcfCipherCreate failed!"); goto clearup; @@ -3567,7 +3683,7 @@ HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest062, TestSize.Level0) ret = memcmp(cipherText, codeCipherText, cipherTextLen); if (ret != 0) { - LOGE("cipherText cpmpare failed!"); + LOGE("cipherText compare failed!"); goto clearup; } @@ -3745,7 +3861,7 @@ HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest066, TestSize.Level0) ret = GenerateSymKey("AES128", &key); if (ret != 0) { - LOGE("ConvertSymKey failed!"); + LOGE("GenerateSymKey failed!"); goto clearup; } @@ -3796,7 +3912,7 @@ HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest067, TestSize.Level0) ret = GenerateSymKey("AES128", &key); if (ret != 0) { - LOGE("ConvertSymKey failed!"); + LOGE("GenerateSymKey failed!"); goto clearup; } @@ -3833,4 +3949,2666 @@ clearup: HcfObjDestroy((HcfObjectBase *)cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest068, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|ECB|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest069, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + key->clearMem(key); +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest070, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|ECB|PKCS7", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + key->clearMem(key); +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest071, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|CBC|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest072, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|CBC|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest073, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|CTR|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest074, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|CTR|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest075, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|OFB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest076, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|OFB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest077, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|CFB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest078, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|CFB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest079, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|CFB1|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest080, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|CFB1|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest081, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|CFB8|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest082, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|CFB8|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest083, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|CFB128|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest084, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfIvParamsSpec ivSpec = {}; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = AES_IV_LEN; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|CFB128|PKCS5", &cipher); // CFB1/CFB8/CFB128 bit + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(ivSpec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, &(ivSpec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest085, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[GCM_AAD_LEN] = { 0 }; + uint8_t tag[GCM_TAG_LEN] = { 0 }; + uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + HcfGcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|GCM|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed, ret:%d!", ret); + goto clearup; + } + + (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN); + PrintfHex("gcm tag", spec.tag.data, spec.tag.len); + cipherTextLen -= GCM_TAG_LEN; + + ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed, ret:%d!", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest086, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[GCM_AAD_LEN] = { 0 }; + uint8_t tag[GCM_TAG_LEN] = { 0 }; + uint8_t iv[GCM_IV_LEN] = { 0 }; // openssl only support nonce 12 bytes, tag 16 bytes + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + HcfGcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|GCM|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed, ret:%d!", ret); + goto clearup; + } + + (void)memcpy_s(spec.tag.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN); + PrintfHex("gcm tag", spec.tag.data, spec.tag.len); + cipherTextLen -= GCM_TAG_LEN; + + ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed, ret:%d!", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest087, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[CCM_AAD_LEN] = { 0 }; + uint8_t tag[CCM_TAG_LEN] = { 0 }; + uint8_t iv[CCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfCcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES192|CCM|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + goto clearup; + } + + (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN); + PrintfHex("ccm tag", spec.tag.data, spec.tag.len); + cipherTextLen -= CCM_TAG_LEN; + + ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest088, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[CCM_AAD_LEN] = { 0 }; + uint8_t tag[CCM_TAG_LEN] = { 0 }; + uint8_t iv[CCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfCcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|CCM|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + goto clearup; + } + + (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText + cipherTextLen - CCM_TAG_LEN, CCM_TAG_LEN); + PrintfHex("ccm tag", spec.tag.data, spec.tag.len); + cipherTextLen -= CCM_TAG_LEN; + + ret = AesDecrypt(cipher, key, &(spec.base), cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest089, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + const char *cipherName = "AES128|CFB|NoPadding"; + const char *retAlgo = nullptr; + ret = HcfCipherCreate(cipherName, &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + retAlgo = cipher->getAlgorithm(cipher); + if (retAlgo == nullptr) { + LOGE("cipher getAlgorithm failed!"); + ret = HCF_ERR_CRYPTO_OPERATION; + goto clearup; + } + + ret = strcmp(retAlgo, cipherName); + if (ret != 0) { + LOGE("cipher getAlgorithm failed!"); + } +clearup: + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest090, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + const char *cipherName = "AES128|CFB|NoPadding"; + const char *retAlgo = nullptr; + ret = HcfCipherCreate(cipherName, &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + retAlgo = cipher->getAlgorithm(nullptr); + if (retAlgo == nullptr) { + LOGE("cipher getAlgorithm failed!"); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest091, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKeyGenerator *generator = nullptr; + const char *cipherName = "AES128|CFB|NoPadding"; + const char *retAlgo = nullptr; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + + ret = HcfCipherCreate(cipherName, &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + retAlgo = cipher->getAlgorithm(reinterpret_cast(generator)); + if (retAlgo == nullptr) { + LOGE("cipher getAlgorithm failed!"); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(generator); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest092, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + const char *inputAlgoName = "AES128"; + const char *generatorAlgoName = nullptr; + + ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->generateSymKey(generator, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + // generator getAlgoName + generatorAlgoName = generator->getAlgoName(generator); + if (generatorAlgoName == nullptr) { + LOGE("generator getAlgoName returns nullptr."); + ret = HCF_ERR_CRYPTO_OPERATION; + goto clearup; + } + + ret = strcmp(generatorAlgoName, inputAlgoName); + if (ret != 0) { + LOGE("generator getAlgoName failed!"); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest093, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + const char *generatorAlgoName = nullptr; + const char *inputAlgoName = "AES128"; + + ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + + // generator getAlgoName + generatorAlgoName = generator->getAlgoName(nullptr); + if (generatorAlgoName == nullptr) { + LOGE("generator getAlgoName failed!"); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest094, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + const char *generatorAlgoName = nullptr; + const char *inputAlgoName = "AES128"; + + ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->generateSymKey(generator, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + // generator getAlgoName + generatorAlgoName = generator->getAlgoName(reinterpret_cast(key)); + if (generatorAlgoName == nullptr) { + LOGE("generator getAlgoName failed!"); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest095, TestSize.Level0) +{ + int ret = 0; + HcfSymKey *key = nullptr; + const char *inputAlgoName = "AES128"; + const char *keyAlgoName = nullptr; + + ret = GenerateSymKey(inputAlgoName, &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // key getAlgorithm + keyAlgoName = key->key.getAlgorithm(&(key->key)); + if (keyAlgoName == nullptr) { + LOGE("key getAlgorithm returns nullptr."); + ret = HCF_ERR_CRYPTO_OPERATION; + goto clearup; + } + + ret = strcmp(keyAlgoName, inputAlgoName); + if (ret != 0) { + LOGE("key getAlgorithm failed!"); + } +clearup: + HcfObjDestroy(key); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest096, TestSize.Level0) +{ + int ret = 0; + HcfSymKey *key = nullptr; + const char *inputAlgoName = "AES128"; + const char *keyAlgoName = nullptr; + + ret = GenerateSymKey(inputAlgoName, &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // key getAlgorithm + keyAlgoName = key->key.getAlgorithm(nullptr); + if (keyAlgoName == nullptr) { + LOGE("key getAlgorithm returns nullptr."); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(key); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest097, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + const char *inputAlgoName = "AES128"; + const char *keyAlgoName = nullptr; + + ret = HcfSymKeyGeneratorCreate(inputAlgoName, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->generateSymKey(generator, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + // key getAlgorithm + keyAlgoName = key->key.getAlgorithm(reinterpret_cast(generator)); + if (keyAlgoName == nullptr) { + LOGE("key getAlgorithm returns nullptr."); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest098, TestSize.Level0) +{ + int ret = 0; + HcfSymKey *key = nullptr; + const char *keyFormat = "PKCS#8"; + const char *retFormat = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // key GetFormat + retFormat = key->key.getFormat(&(key->key)); + if (retFormat == nullptr) { + LOGE("key GetFormat returns nullptr."); + ret = HCF_ERR_CRYPTO_OPERATION; + goto clearup; + } + + ret = strcmp(retFormat, keyFormat); + if (ret != 0) { + LOGE("key GetFormat failed!"); + } + +clearup: + HcfObjDestroy(key); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest099, TestSize.Level0) +{ + int ret = 0; + HcfSymKey *key = nullptr; + const char *retFormat = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // key getFormat + retFormat = key->key.getFormat(nullptr); + if (retFormat == nullptr) { + LOGE("key GetFormat returns nullptr."); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(key); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest100, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + const char *retFormat = nullptr; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->generateSymKey(generator, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + // key getFormat + retFormat = key->key.getFormat(reinterpret_cast(generator)); + if (retFormat == nullptr) { + LOGE("key GetFormat returns nullptr."); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest101, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + HcfBlob encodedBlob = { 0 }; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN }; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->convertSymKey(generator, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + // key getEncoded + ret = key->key.getEncoded(&(key->key), &encodedBlob); + if (ret != 0) { + LOGE("key GetEncoded failed."); + goto clearup; + } + + if (encodedBlob.len != keyTmpBlob.len) { + LOGE("key GetEncoded failed!"); + ret = HCF_ERR_CRYPTO_OPERATION; + goto clearup; + } + ret = memcmp(encodedBlob.data, keyTmpBlob.data, keyTmpBlob.len); + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + if (encodedBlob.data != nullptr) { + HcfFree(encodedBlob.data); + encodedBlob.data = nullptr; + } + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest102, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + HcfBlob encodedBlob = { 0 }; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN }; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->convertSymKey(generator, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + // key getEncoded + ret = key->key.getEncoded(nullptr, &encodedBlob); + if (ret != 0) { + LOGE("key GetEncoded failed."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + if (encodedBlob.data != nullptr) { + HcfFree(encodedBlob.data); + encodedBlob.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest103, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + HcfBlob encodedBlob = { 0 }; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN }; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->convertSymKey(generator, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + // key getEncoded + ret = key->key.getEncoded(reinterpret_cast(generator), &encodedBlob); + if (ret != 0) { + LOGE("key GetEncoded failed."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + if (encodedBlob.data != nullptr) { + HcfFree(encodedBlob.data); + encodedBlob.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest104, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + HcfBlob encodedBlob = { 0 }; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN }; + SymKeyImpl *impl = nullptr; + size_t tmpLen = 0; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->convertSymKey(generator, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + impl = reinterpret_cast(key); + tmpLen = impl->keyMaterial.len; + impl->keyMaterial.len = 0; + + // key getEncoded + ret = key->key.getEncoded(&(key->key), &encodedBlob); + impl->keyMaterial.len = tmpLen; + if (ret != 0) { + LOGE("key GetEncoded failed."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + if (encodedBlob.data != nullptr) { + HcfFree(encodedBlob.data); + encodedBlob.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest105, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + HcfBlob encodedBlob = { 0 }; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN }; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->convertSymKey(generator, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + key->clearMem(nullptr); + + ret = key->key.getEncoded(&(key->key), &encodedBlob); + if (ret != 0) { + LOGE("key GetEncoded failed."); + goto clearup; + } + if ((encodedBlob.data != nullptr) && (encodedBlob.data[0] != '\0')) { + LOGE("clearMem failed!"); + ret = HCF_ERR_CRYPTO_OPERATION; + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + if (encodedBlob.data != nullptr) { + HcfFree(encodedBlob.data); + encodedBlob.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest106, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + + ret = HcfSymKeyGeneratorCreate("RSA128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed! Should not select RSA for symKey generator."); + } + + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest107, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + + ret = HcfSymKeyGeneratorCreate("RSA512", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed! Should not select RSA for symKey generator."); + } + + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest108, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + + ret = HcfSymKeyGeneratorCreate("", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed! Should not select empty string for symKey generator."); + } + + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest109, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + + ret = HcfSymKeyGeneratorCreate(nullptr, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed! Should not select nullptr for symKey generator."); + } + + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest110, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + + ret = HcfSymKeyGeneratorSpiCreate(nullptr, nullptr); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorSpiCreate failed!"); + } + + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest111, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->generateSymKey(nullptr, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest112, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + HcfCipher *cipher = nullptr; + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->generateSymKey(reinterpret_cast(cipher), &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest113, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN }; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + + ret = generator->convertSymKey(nullptr, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest114, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = KEY_MATERIAL_LEN }; + HcfCipher *cipher = nullptr; + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + + ret = generator->convertSymKey(reinterpret_cast(cipher), &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest115, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + uint8_t keyMaterial[] = { + 0xba, 0x3b, 0xc2, 0x71, 0x21, 0x1e, 0x30, 0x56, + 0xad, 0x47, 0xfc, 0x5a, 0x46, 0x39, 0xee, 0x7c + }; + HcfBlob keyTmpBlob = { .data = keyMaterial, .len = 0 }; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + + ret = generator->convertSymKey(generator, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest116, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + + ret = HcfCipherCreate("RSA128|GCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed! Should not select RSA for GCM generator."); + } + + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest117, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // allow input without encryption mode. It will pick the last PKCS5, and use default aes128ecb. + ret = HcfCipherCreate("AES128|NoPadding|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest118, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + + // not allow '|' without content, because findAbility will fail for "" input + ret = HcfCipherCreate("AES128|GCM|", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed! Should select padding mode for AES generator."); + } + + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest119, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // allow input without encryption mode. It will use default aes128ecb. + ret = HcfCipherCreate("AES128|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest120, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // allow input without encryption mode. It will use default aes128ecb. + ret = HcfCipherCreate("AES128", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest121, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + + ret = HcfCipherCreate("", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + } + + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest122, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + + ret = HcfCipherCreate(nullptr, &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + } + + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest123, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // allow input with more than one padding mode. It will pick the last PKCS5. + ret = HcfCipherCreate("AES128|ECB|NoPadding|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest124, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES256|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + // It is not allowed that AES128 in key is smaller AES256 in cipher. + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest125, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest126, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + // CBC, CTR, OFB, CFB enc/dec success, + // GCM, CCM enc/dec failed with params set to nullptr. + ret = HcfCipherCreate("AES128|GCM|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecrypt(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest127, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(nullptr, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! Should input cipher when init."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest128, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfCipher *cipher = nullptr; + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, nullptr, nullptr); + if (ret != 0) { + LOGE("init failed! Should input key when init."); + } + +clearup: + HcfObjDestroy(cipher); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest129, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGenerator *generator = nullptr; + HcfSymKey *key = nullptr; + HcfCipher *cipher = nullptr; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto clearup; + } + ret = generator->generateSymKey(generator, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(reinterpret_cast(generator), ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed! Should input key when init."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest130, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[GCM_AAD_LEN] = { 0 }; + uint8_t tag[GCM_TAG_LEN] = { 0 }; + uint8_t iv[GCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + HcfGcmParamsSpec spec = {}; + spec.aad.data = nullptr; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed, ret:%d!", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest131, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[GCM_AAD_LEN] = { 0 }; + uint8_t tag[GCM_TAG_LEN] = { 0 }; + uint8_t iv[GCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + HcfGcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = nullptr; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed, ret:%d!", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest132, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[GCM_AAD_LEN] = { 0 }; + uint8_t tag[GCM_TAG_LEN] = { 0 }; + uint8_t iv[GCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + HcfGcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = nullptr; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|GCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed, ret:%d!", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest133, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest134, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[CCM_AAD_LEN] = { 0 }; + uint8_t tag[CCM_TAG_LEN] = { 0 }; + uint8_t iv[CCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfCcmParamsSpec spec = {}; + spec.aad.data = nullptr; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest135, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[CCM_AAD_LEN] = { 0 }; + uint8_t tag[CCM_TAG_LEN] = { 0 }; + uint8_t iv[CCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfCcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = tag; + spec.tag.len = sizeof(tag); + spec.iv.data = nullptr; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest136, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[CCM_AAD_LEN] = { 0 }; + uint8_t tag[CCM_TAG_LEN] = { 0 }; + uint8_t iv[CCM_IV_LEN] = { 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfCcmParamsSpec spec = {}; + spec.aad.data = aad; + spec.aad.len = sizeof(aad); + spec.tag.data = nullptr; + spec.tag.len = sizeof(tag); + spec.iv.data = iv; + spec.iv.len = sizeof(iv); + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|CCM|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncrypt(cipher, key, &(spec.base), cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest137, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test!"; + HcfBlob input = { .data = plainText, .len = 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncryptWithInput(cipher, key, &input, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest138, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfBlob input = { .data = nullptr, .len = 0 }; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesEncryptWithInput(cipher, key, &input, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest139, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfBlob input = { .data = nullptr, .len = PLAINTEXT_LEN }; + HcfBlob output = { .data = nullptr, .len = 0 }; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed!"); + goto clearup; + } + + ret = cipher->update(nullptr, &input, &output); + if (ret != 0) { + LOGE("update failed! Blob data should not be nullptr."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + if (output.data != nullptr) { + HcfFree(output.data); + output.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest140, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + HcfBlob input = { .data = nullptr, .len = PLAINTEXT_LEN }; + HcfBlob output = { .data = nullptr, .len = 0 }; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed!"); + goto clearup; + } + + ret = cipher->update(reinterpret_cast(key), &input, &output); + if (ret != 0) { + LOGE("update failed! Blob data should not be nullptr."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + if (output.data != nullptr) { + HcfFree(output.data); + output.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest141, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = AesNoUpdateEncWithInput(cipher, key, nullptr, cipherText, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto clearup; + } + + ret = AesDecryptEmptyMsg(cipher, key, nullptr, cipherText, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest142, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test!"; + HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed!"); + goto clearup; + } + + ret = cipher->doFinal(cipher, &input, nullptr); + if (ret != 0) { + LOGE("update failed! Blob data should not be nullptr."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest143, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test!"; + HcfBlob input = { .data = plainText, .len = PLAINTEXT_LEN }; + HcfBlob output = { .data = nullptr, .len = 0 }; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto clearup; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); + if (ret != 0) { + LOGE("init failed!"); + goto clearup; + } + + ret = cipher->doFinal(reinterpret_cast(key), &input, &output); + if (ret != 0) { + LOGE("update failed! Blob data should not be nullptr."); + } + +clearup: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + if (output.data != nullptr) { + HcfFree(output.data); + output.data = nullptr; + } + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest144, TestSize.Level0) +{ + int ret = HcfCipherAesGeneratorSpiCreate(nullptr, nullptr); + if (ret != 0) { + LOGE("HcfCipherAesGeneratorSpiCreate failed!"); + } + EXPECT_NE(ret, 0); +} } \ No newline at end of file