diff --git a/test/unittest/include/aes_common.h b/test/unittest/include/aes_common.h index 7561988d3dee9588e00c5af1299ae76fb1e71a59..83b6a66cc427762b9717f5c3761a11183f2696ec 100644 --- a/test/unittest/include/aes_common.h +++ b/test/unittest/include/aes_common.h @@ -64,6 +64,11 @@ int32_t AesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, int32_t AesNoUpdateEncWithInput(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, uint8_t *cipherText, int *cipherTextLen); +int32_t AesEncryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, HcfParamsSpec *params, + int *cipherTextLen); +int32_t AesDecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + int cipherTextLen); + // test encrypt and decrypt with null plain text int32_t AesDecryptEmptyMsg(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, uint8_t *cipherText, int cipherTextLen); diff --git a/test/unittest/include/sm4_common.h b/test/unittest/include/sm4_common.h index e3609bf1c9764ea1a39def88ed27cbd0fb308dca..e435305741e1da696dd33e940ab0be64bd76da26 100644 --- a/test/unittest/include/sm4_common.h +++ b/test/unittest/include/sm4_common.h @@ -49,6 +49,11 @@ int32_t Sm4NoUpdateDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *par uint8_t *cipherText, int cipherTextLen); const char *GetMockClass(void); +int32_t Sm4EncryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, HcfParamsSpec *params, + int *cipherTextLen); +int32_t Sm4DecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + int cipherTextLen); + #ifdef __cplusplus } #endif diff --git a/test/unittest/src/aes_cipher/aes_common.cpp b/test/unittest/src/aes_cipher/aes_common.cpp index 22dfc4f676e3b62a60be1e08690ed5fea2412b3e..81efd2800facd4f24b99b13914f7cfb4dc6be290 100644 --- a/test/unittest/src/aes_cipher/aes_common.cpp +++ b/test/unittest/src/aes_cipher/aes_common.cpp @@ -390,6 +390,98 @@ int32_t AesDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, return memcmp(cipherText, plainText, cipherTextLen); } +int32_t AesEncryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, HcfParamsSpec *params, + int *cipherTextLen) +{ + HcfBlob output = {}; + int32_t maxLen = *cipherTextLen; + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + int32_t ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast(key), params); + 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; +} + +int32_t AesDecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + int cipherTextLen) +{ + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob input = {.data = reinterpret_cast(cipherText), .len = cipherTextLen}; + HcfBlob output = {}; + int32_t maxLen = cipherTextLen; + int32_t ret = cipher->init(cipher, DECRYPT_MODE, reinterpret_cast(key), params); + 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("plainText", cipherText, cipherTextLen); + if (cipherTextLen != plain->len) { + return -1; + } + return memcmp(cipherText, plain->data, cipherTextLen); +} + int32_t AesNoUpdateEncWithInput(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, uint8_t *cipherText, int *cipherTextLen) { diff --git a/test/unittest/src/aes_cipher/crypto_aes_cbc_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_cbc_cipher_test.cpp index 084b004ccce8d3297a4209f4342cb7da610da9c6..602d8aa2d82c7f15dc418b24f2ace57d2c84068b 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_cbc_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_cbc_cipher_test.cpp @@ -483,4 +483,48 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesCbcCipherTest, CryptoAesCbcCipherTest010, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + + 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 CLEAR_UP; + } + + ret = HcfCipherCreate("AES256|CBC|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/aes_cipher/crypto_aes_ccm_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_ccm_cipher_test.cpp index b780128650d657e17ff6ca6b0beeb1dfb716fbaf..cc5807b71e1069f6e21f7e014516edc49eced4cc 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_ccm_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_ccm_cipher_test.cpp @@ -622,4 +622,58 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoAesCcmCipherTest, CryptoAesCcmCipherTest013, 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); + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + ret = GenerateSymKey("AES256", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("AES256|CCM|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, &(spec.base), &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + goto CLEAR_UP; + } + + (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 = AesDecryptWithInputAndPar(cipher, key, &input, &(spec.base), cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed!"); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/aes_cipher/crypto_aes_cfb_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_cfb_cipher_test.cpp index eed15032b5ace362ac7e4feaa86c5efc3183a024..1d91aa1f7919daff3b6d58efd1f1b5b84b31e729 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_cfb_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_cfb_cipher_test.cpp @@ -1720,4 +1720,47 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoAesCfbCipherTest, CryptoAesCfbCipherTest037, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_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; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("AES192|CFB128|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/aes_cipher/crypto_aes_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_cipher_test.cpp index 015b589dbf01fa2f5ea40564f2afb4f6f496f4f8..c8604dc55d1dea77f08647dd579cb9d50eaf1046 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_cipher_test.cpp @@ -870,4 +870,41 @@ CLEAR_UP: HcfObjDestroy(generator); EXPECT_NE(ret, 0); } +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest031, TestSize.Level0) +{ + int ret = 0; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + + ret = GenerateSymKey("AES128", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("AES128|NoPadding|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, nullptr, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, nullptr, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/aes_cipher/crypto_aes_ctr_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_ctr_cipher_test.cpp index c8f0d5b828e38814bb906d54913c2a3ee88cc93f..2b8b298596dfe7e6e772912beb63749d08df07c6 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_ctr_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_ctr_cipher_test.cpp @@ -482,4 +482,47 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesCtrCipherTest, CryptoAesCtrCipherTest010, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_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; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + ret = GenerateSymKey("AES192", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("AES192|CTR|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/aes_cipher/crypto_aes_ecb_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_ecb_cipher_test.cpp index 71d96b4a987df1f3df8e8eee7c401d33a6a769a0..f8f1fc2c7f926681f2a488cf110ab1bc9840be51 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_ecb_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_ecb_cipher_test.cpp @@ -1183,4 +1183,43 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest030, TestSize.Level0) +{ + int ret = 0; + int cipherTextLen = CIPHER_TEXT_LEN; + + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + ret = ConvertSymKey("AES128", &key); + if (ret != 0) { + LOGE("ConvertSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("AES128|ECB|PKCS7", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, nullptr, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, nullptr, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +CLEAR_UP: + HcfObjDestroy((HcfObjectBase *)key); + HcfObjDestroy((HcfObjectBase *)cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/aes_cipher/crypto_aes_gcm_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_gcm_cipher_test.cpp index b206b7ee692d28d58ba95bad3c8fe8129adf44b5..d480a9741754dd8560bfad79f7ced639ca575422 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_gcm_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_gcm_cipher_test.cpp @@ -938,4 +938,60 @@ CLEAR_UP: HcfObjDestroy((HcfObjectBase *)cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesGcmCipherTest, CryptoAesGcmCipherTest020, TestSize.Level0) +{ + int ret = 0; + uint8_t aad[8] = {0}; + uint8_t tag[16] = {0}; + uint8_t iv[12] = {0}; + uint8_t test[128] = {0}; + int cipherTextLen = 128; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + 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("AES128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("AES128|GCM|PKCS7", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, (HcfParamsSpec *)&spec, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed, ret:%d!", ret); + goto CLEAR_UP; + } + + (void)memcpy_s(spec.tag.data, 16, test + cipherTextLen - 16, 16); + PrintfHex("gcm tag", spec.tag.data, spec.tag.len); + cipherTextLen -= 16; + + ret = AesDecryptWithInputAndPar(cipher, key, &input, (HcfParamsSpec *)&spec, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed, ret:%d!", ret); + goto CLEAR_UP; + } + +CLEAR_UP: + HcfObjDestroy((HcfObjectBase *)key); + HcfObjDestroy((HcfObjectBase *)cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/aes_cipher/crypto_aes_ofb_cipher_test.cpp b/test/unittest/src/aes_cipher/crypto_aes_ofb_cipher_test.cpp index d9d60e47f2bd4aaa24e302b4bb18ac27517337f8..def51fbd8357c11102dc97fa9a0c07b4549f960e 100644 --- a/test/unittest/src/aes_cipher/crypto_aes_ofb_cipher_test.cpp +++ b/test/unittest/src/aes_cipher/crypto_aes_ofb_cipher_test.cpp @@ -480,4 +480,56 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesOfbCipherTest, CryptoAesOfbCipherTest010, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[16] = {0}; + int cipherTextLen = CIPHER_TEXT_LEN; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = { .data = plainText, .len = 47 }; + HcfIvParamsSpec ivSpec = {}; + HcfSymKeyGenerator *generator = nullptr; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + ivSpec.iv.data = iv; + ivSpec.iv.len = 16; + + ret = HcfSymKeyGeneratorCreate("AES128", &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorCreate failed!%d", ret); + goto CLEAR_UP; + } + + ret = generator->generateSymKey(generator, &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("AES128|OFB|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = AesEncryptWithInputAndPar(cipher, key, &input, (HcfParamsSpec *)&ivSpec, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, (HcfParamsSpec *)&ivSpec, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + goto CLEAR_UP; + } + +CLEAR_UP: + HcfObjDestroy((HcfObjectBase *)key); + HcfObjDestroy((HcfObjectBase *)cipher); + HcfObjDestroy((HcfObjectBase *)generator); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/crypto_3des_cipher_test.cpp b/test/unittest/src/crypto_3des_cipher_test.cpp index 4fca9f30be6187b9d8f577a67e09056d4b72083a..c066429c20026c43f555e01d2a402ef1139100bb 100644 --- a/test/unittest/src/crypto_3des_cipher_test.cpp +++ b/test/unittest/src/crypto_3des_cipher_test.cpp @@ -1747,7 +1747,7 @@ HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest033, TestSize.Level0) LOGE("HcfCipherCreate failed!"); goto clearup; } - + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); if (ret != 0) { LOGE("init failed! %d", ret); @@ -1787,7 +1787,7 @@ HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest034, TestSize.Level0) LOGE("HcfCipherCreate failed!"); goto clearup; } - + ret = cipher->init(cipher, ENCRYPT_MODE, &(key->key), nullptr); if (ret != 0) { LOGE("init failed! %d", ret); @@ -1815,4 +1815,141 @@ HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest035, TestSize.Level0) } EXPECT_NE(ret, 0); } + +static int32_t DesLongEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, HcfParamsSpec *params, + int *cipherTextLen) +{ + uint8_t cipherText[CIPHER_TEXT_LEN] = {0}; + HcfBlob output = {}; + int32_t maxLen = *cipherTextLen; + int32_t ret = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)key, params); + 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); + } + return 0; +} + +static int32_t DesLongDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plainText, HcfParamsSpec *params, + int cipherTextLen) +{ + uint8_t cipherText[CIPHER_TEXT_LEN] = {0}; + HcfBlob input = {.data = (uint8_t *)cipherText, .len = cipherTextLen}; + HcfBlob output = {}; + int32_t maxLen = cipherTextLen; + int32_t ret = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)key, params); + 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); + } + + if (cipherTextLen != plainText->len) { + return -1; + } + return memcmp(cipherText, plainText->data, cipherTextLen); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest036, TestSize.Level0) +{ + int ret = 0; + int cipherTextLen = 128; + uint8_t plainText[] = "this is test that its length is larger than 16.\0"; + HcfBlob input = {.data = (uint8_t *)plainText, .len = 41}; + 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|NoPadding", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto clearup; + } + + ret = DesLongEncrypt(cipher, key, &input, nullptr, &cipherTextLen); + if (ret != 0) { + LOGE("DesEncrypt failed! %d", ret); + goto clearup; + } + + ret = DesLongDecrypt(cipher, key, &input, nullptr, cipherTextLen); + if (ret != 0) { + LOGE("DesDecrypt failed! %d", ret); + goto clearup; + } + +clearup: + HcfObjDestroy((HcfObjectBase *)key); + HcfObjDestroy((HcfObjectBase *)cipher); + HcfObjDestroy((HcfObjectBase *)generator); + EXPECT_NE(ret, 0); +} + } diff --git a/test/unittest/src/crypto_rsa_cipher_test.cpp b/test/unittest/src/crypto_rsa_cipher_test.cpp index 200f29e9d82f75d9b647be4f47f14b8a9a502e57..86b2b60eb92ce59e56e518f09ee7cbd5c3e5b1e1 100644 --- a/test/unittest/src/crypto_rsa_cipher_test.cpp +++ b/test/unittest/src/crypto_rsa_cipher_test.cpp @@ -1898,4 +1898,59 @@ HWTEST_F(CryptoRsaCipherTest, CryptoRsaCipherTest016, TestSize.Level0) HcfObjDestroy(keyPair); HcfObjDestroy(generator); } + +HWTEST_F(CryptoRsaCipherTest, CryptoRsaCipherTest018, TestSize.Level0) +{ + uint8_t plan[] = "this is long rsa cipher test! this is long rsa cipher test! this is long rsa cipher test! \0"; + HcfRsaKeyPairParamsSpec rsaPairSpec = {}; + unsigned char dataN[RSA_2048_N_BYTE_SIZE] = {0}; + unsigned char dataE[RSA_2048_E_BYTE_SIZE] = {0}; + unsigned char dataD[RSA_2048_D_BYTE_SIZE] = {0}; + GenerateRsa2048CorrectKeyPairSpec(dataN, dataE, dataD, &rsaPairSpec); + + HcfAsyKeyGeneratorBySpec *generator = nullptr; + HcfResult res = HcfAsyKeyGeneratorBySpecCreate(reinterpret_cast(&rsaPairSpec), &generator); + EXPECT_EQ(res, HCF_SUCCESS); + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, &keyPair); + EXPECT_EQ(res, HCF_SUCCESS); + EXPECT_NE(keyPair, nullptr); + + HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)}; + HcfBlob encoutput = {.data = nullptr, .len = 0}; + HcfCipher *cipher = nullptr; + res = HcfCipherCreate("RSA|PKCS1_OAEP|SHA256|MGF1_SHA256", &cipher); + EXPECT_EQ(res, HCF_SUCCESS); + res = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)keyPair->pubKey, nullptr); + EXPECT_EQ(res, HCF_SUCCESS); + + HcfBlob pSource = { .data = nullptr, .len = 0 }; + // self == nullptr + res = cipher->setCipherSpecUint8Array(nullptr, OAEP_MGF1_PSRC_UINT8ARR, pSource); + EXPECT_NE(res, HCF_SUCCESS); + res = cipher->doFinal(cipher, &input, &encoutput); + EXPECT_EQ(res, HCF_SUCCESS); + HcfObjDestroy(cipher); + + // decrypt + HcfBlob decoutput = {.data = nullptr, .len = 0}; + cipher = nullptr; + res = HcfCipherCreate("RSA1024|PKCS1_OAEP|SHA256|MGF1_SHA256", &cipher); + EXPECT_EQ(res, HCF_SUCCESS); + res = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)keyPair->priKey, nullptr); + EXPECT_EQ(res, HCF_SUCCESS); + res = cipher->doFinal(cipher, &encoutput, &decoutput); + EXPECT_EQ(res, HCF_SUCCESS); + HcfObjDestroy(cipher); + + EXPECT_STREQ((char *)plan, (char *)decoutput.data); + + // free decrpyt spec + HcfFree(encoutput.data); + HcfFree(decoutput.data); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} + } diff --git a/test/unittest/src/crypto_sm2_cipher_test.cpp b/test/unittest/src/crypto_sm2_cipher_test.cpp index 836b0dd255cd7990e7941d920fe4f1e531090a4e..8f1c6d953cca03bc220556aec2fc5a926366fd6f 100644 --- a/test/unittest/src/crypto_sm2_cipher_test.cpp +++ b/test/unittest/src/crypto_sm2_cipher_test.cpp @@ -1216,4 +1216,56 @@ HWTEST_F(CryptoSm2CipherTest, CryptoSm2CipherTest058, TestSize.Level0) HcfObjDestroy(keyPair); HcfObjDestroy(generator); } + +HWTEST_F(CryptoSm2CipherTest, CryptoSm2CipherTest059, TestSize.Level0) +{ + HcfResult res = HCF_SUCCESS; + uint8_t plan[] = "this is a long length of sm2 cipher test! this is a long length of sm2 cipher test!\0"; + HcfAsyKeyGenerator *generator = nullptr; + res = HcfAsyKeyGeneratorCreate("SM2_256", &generator); + EXPECT_EQ(res, HCF_SUCCESS); + HcfKeyPair *keyPair = nullptr; + res = generator->generateKeyPair(generator, nullptr, &keyPair); + EXPECT_EQ(res, HCF_SUCCESS); + EXPECT_NE(keyPair, nullptr); + EXPECT_NE(keyPair->priKey, nullptr); + EXPECT_NE(keyPair->pubKey, nullptr); + + HcfBlob input = {.data = (uint8_t *)plan, .len = strlen((char *)plan)}; + HcfBlob encoutput = {.data = nullptr, .len = 0}; + HcfCipherGeneratorSpi *cipher = nullptr; + CipherAttr params = { + .algo = HCF_ALG_SM2, + .md = HCF_OPENSSL_DIGEST_SM3, + }; + res = HcfCipherSm2CipherSpiCreate(¶ms, &cipher); + EXPECT_EQ(res, HCF_SUCCESS); + + res = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)keyPair->pubKey, nullptr); + EXPECT_EQ(res, HCF_SUCCESS); + char *returnStr = nullptr; + res = cipher->getCipherSpecString(cipher, SM2_MD_NAME_STR, &returnStr); + EXPECT_EQ(res, HCF_SUCCESS); + res = cipher->doFinal(cipher, &input, &encoutput); + EXPECT_EQ(res, HCF_SUCCESS); + HcfFree(returnStr); + HcfObjDestroy(cipher); + + HcfBlob decoutput = {.data = nullptr, .len = 0}; + cipher = nullptr; + res = HcfCipherSm2CipherSpiCreate(¶ms, &cipher); + EXPECT_EQ(res, HCF_SUCCESS); + res = cipher->init(cipher, DECRYPT_MODE, (HcfKey *)keyPair->priKey, nullptr); + EXPECT_EQ(res, HCF_SUCCESS); + res = cipher->doFinal(cipher, &encoutput, &decoutput); + EXPECT_EQ(res, HCF_SUCCESS); + HcfObjDestroy(cipher); + EXPECT_STREQ((char *)plan, (char *)decoutput.data); + + HcfFree(encoutput.data); + HcfFree(decoutput.data); + + HcfObjDestroy(keyPair); + HcfObjDestroy(generator); +} } diff --git a/test/unittest/src/crypto_sm4_cfb_cipher_test.cpp b/test/unittest/src/crypto_sm4_cfb_cipher_test.cpp index 70018ea294fa7581cf383d9c1f7d757dc393edff..63cf45462b076e8198c403729cb11aae1899d612 100644 --- a/test/unittest/src/crypto_sm4_cfb_cipher_test.cpp +++ b/test/unittest/src/crypto_sm4_cfb_cipher_test.cpp @@ -868,4 +868,42 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoSM4CfbCipherTest, CryptoSm4CipherTest084, TestSize.Level0) +{ + int ret = 0; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is a long length of sm4 cipher test! this is a long length of sm4 cipher test!\0"; + HcfBlob input = { .data = plainText, .len = 0 }; + ret = GenerateSymKeyForSm4("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKey failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("SM4_128|CFB128|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = Sm4EncryptWithInputAndPar(cipher, key, &input, nullptr, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = Sm4DecryptWithInputAndPar(cipher, key, &input, nullptr, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/crypto_sm4_cipher_test.cpp b/test/unittest/src/crypto_sm4_cipher_test.cpp index 60d9c552580ac65b96f0256702b836e228877c60..116b7f5d3bb733d9f5b0a6966b72fb2fdee1a305 100644 --- a/test/unittest/src/crypto_sm4_cipher_test.cpp +++ b/test/unittest/src/crypto_sm4_cipher_test.cpp @@ -1303,4 +1303,42 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest082, TestSize.Level0) +{ + int ret = 0; + int cipherTextLen = CIPHER_TEXT_LEN; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is a long length of sm4 cipher test! this is a long length of sm4 cipher test!\0"; + HcfBlob input = { .data = plainText, .len = 0 }; + ret = GenerateSymKeyForSm4("AES256", &key); + if (ret != 0) { + LOGE("GenerateSymKeyForSm4 failed!"); + goto CLEAR_UP; + } + + ret = HcfCipherCreate("SM4_128|OFB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = Sm4EncryptWithInputAndPar(cipher, key, &input, nullptr, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = Sm4DecryptWithInputAndPar(cipher, key, &input, nullptr, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/crypto_sm4_ecb_cipher_test.cpp b/test/unittest/src/crypto_sm4_ecb_cipher_test.cpp index 6bee4647eaf00a9c334cb472917ed0e4daa0b8a1..4eaacc43e399c6e9459e719e4adfc3cab8f4d038 100644 --- a/test/unittest/src/crypto_sm4_ecb_cipher_test.cpp +++ b/test/unittest/src/crypto_sm4_ecb_cipher_test.cpp @@ -818,4 +818,42 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoSM4EcbCipherTest, CryptoAesCipherTest062, TestSize.Level0) +{ + int ret = 0; + HcfCipher *cipher = nullptr; + HcfSymKey *key = nullptr; + uint8_t plainText[] = "this is a long length of sm4 cipher test! this is a long length of sm4 cipher test!\0"; + HcfBlob input = { .data = plainText, .len = 0 }; + int cipherTextLen = CIPHER_TEXT_LEN; + + ret = GenerateSymKeyForSm4("SM4_128", &key); + if (ret != 0) { + LOGE("generateSymKey failed!"); + goto CLEAR_UP; + } + ret = HcfCipherCreate("SM4_128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + + ret = Sm4EncryptWithInputAndPar(cipher, key, &input, nullptr, &cipherTextLen); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = Sm4DecryptWithInputAndPar(cipher, key, &input, nullptr, cipherTextLen); + if (ret != 0) { + LOGE("AesDecrypt failed! %d", ret); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(cipher); + EXPECT_EQ(ret, 0); +} + } \ No newline at end of file diff --git a/test/unittest/src/sm4_common.cpp b/test/unittest/src/sm4_common.cpp index 94e418dcab8baf0e8ea1fcdb1668e4a85ecbacef..2053946f07d5c1962e1c2c7734e6d40eef3f04c6 100644 --- a/test/unittest/src/sm4_common.cpp +++ b/test/unittest/src/sm4_common.cpp @@ -228,6 +228,95 @@ int32_t Sm4Decrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, return memcmp(cipherText, plainText, cipherTextLen); } +int32_t Sm4EncryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, HcfParamsSpec *params, + int *cipherTextLen) +{ + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob output = {}; + int32_t maxLen = *cipherTextLen; + int32_t ret = cipher->init(cipher, ENCRYPT_MODE, reinterpret_cast(key), params); + if (ret != 0) { + LOGE("init failed! "); + 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); + } + return 0; +} + +int32_t Sm4DecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + int cipherTextLen) +{ + uint8_t cipherText[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob input = {.data = reinterpret_cast(cipherText), .len = cipherTextLen}; + HcfBlob output = {}; + int32_t maxLen = cipherTextLen; + int32_t ret = cipher->init(cipher, DECRYPT_MODE, reinterpret_cast(key), params); + if (ret != 0) { + LOGE("init failed! "); + 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); + } + + if (cipherTextLen != plain->len) { + return -1; + } + return memcmp(cipherText, plain->data, cipherTextLen); +} + int32_t Sm4NoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, uint8_t *cipherText, int *cipherTextLen) {