diff --git a/test/unittest/include/aes_common.h b/test/unittest/include/aes_common.h index 7561988d3dee9588e00c5af1299ae76fb1e71a59..9d549afb1c30fd8d01357579fc449bd9834bb0a2 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, + HcfBlob *cipherText); +int32_t AesDecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + HcfBlob *cipherText); + // 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..94eb45f3af9e07935ac0aa8409ad1af94dda8c8c 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, + HcfBlob *cipherText); +int32_t Sm4DecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + HcfBlob *cipherText); + #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..c62fa20b9516cced0db6ca43b0ca2399c8f138a2 100644 --- a/test/unittest/src/aes_cipher/aes_common.cpp +++ b/test/unittest/src/aes_cipher/aes_common.cpp @@ -390,6 +390,96 @@ 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, + HcfBlob *cipherText) +{ + HcfBlob output = {}; + int32_t maxLen = cipherText->len; + 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; + } + cipherText->len = output.len; + if (output.data != nullptr) { + if (memcpy_s(cipherText->data, 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->data + cipherText->len, maxLen - cipherText->len, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherText->len += output.len; + HcfBlobDataFree(&output); + } + + PrintfHex("ciphertext", cipherText->data, cipherText->len); + return 0; +} + +int32_t AesDecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + HcfBlob *cipherText) +{ + HcfBlob input = {.data = reinterpret_cast(cipherText->data), .len = cipherText->len}; + HcfBlob output = {}; + int32_t maxLen = cipherText->len; + 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; + } + cipherText->len = output.len; + if (output.data != nullptr) { + if (memcpy_s(cipherText->data, 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->data + cipherText->len, maxLen - cipherText->len, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherText->len += output.len; + HcfBlobDataFree(&output); + } + + PrintfHex("plainText", cipherText->data, cipherText->len); + if (cipherText->len != plain->len) { + return -1; + } + return memcmp(cipherText->data, plain->data, cipherText->len); +} + 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..5bdaf0482d89c4668d7c1a39b371c8060e1d44b7 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,50 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesCbcCipherTest, CryptoAesCbcCipherTest010, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + 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), &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), &cipherText); + 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..056d873be14add0c757cc8b67e0ebdb260b937ad 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,59 @@ 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 cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + + 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), &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed!"); + goto CLEAR_UP; + } + + (void)memcpy_s(spec.tag.data, CCM_TAG_LEN, cipherText.data + cipherText.len - CCM_TAG_LEN, CCM_TAG_LEN); + PrintfHex("ccm tag", spec.tag.data, spec.tag.len); + cipherText.len -= CCM_TAG_LEN; + + ret = AesDecryptWithInputAndPar(cipher, key, &input, &(spec.base), &cipherText); + 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..f7bd090cc264ffde9d0bba99be9fae3ffad4b839 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,49 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoAesCfbCipherTest, CryptoAesCfbCipherTest037, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + + 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), &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), &cipherText); + 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..aca519e07aaa984ca1462d393f9ff9c1a18f4b3f 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,43 @@ CLEAR_UP: HcfObjDestroy(generator); EXPECT_NE(ret, 0); } +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest031, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + 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, &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, nullptr, &cipherText); + 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..682f287610a7dc73d7a9d8e50c8cf44a2a2f4a0b 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,49 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesCtrCipherTest, CryptoAesCtrCipherTest010, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[AES_IV_LEN] = { 0 }; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + + 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), &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, &(ivSpec.base), &cipherText); + 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..412302b1425abc0f1a45b0223eb69e77351d44b8 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,45 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoAesEcbCipherTest, CryptoAesEcbCipherTest030, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + + 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, &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, nullptr, &cipherText); + 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..c9df08d4061150c29974d410e6fedf5c588b25db 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,5 @@ CLEAR_UP: 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..7cf0ee6a07ffa5256aa1c5589a8276df0a5aef4a 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,58 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_EQ(ret, 0); } + +HWTEST_F(CryptoAesOfbCipherTest, CryptoAesOfbCipherTest010, TestSize.Level0) +{ + int ret = 0; + uint8_t iv[16] = {0}; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + 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, &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = AesDecryptWithInputAndPar(cipher, key, &input, (HcfParamsSpec *)&ivSpec, &cipherText); + 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..c5b79d478565e3cf992242ab779a0df979262275 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,125 @@ HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest035, TestSize.Level0) } EXPECT_NE(ret, 0); } + +static int32_t DesLongEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *input, HcfParamsSpec *params, + HcfBlob *cipherText) +{ + HcfBlob output = {}; + int32_t maxLen = cipherText->len; + 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; + } + cipherText->len = output.len; + ret = cipher->doFinal(cipher, nullptr, &output); + if (ret != 0) { + LOGE("doFinal failed!"); + return ret; + } + if (output.data != nullptr) { + if (memcpy_s(cipherText->data + cipherText->len, maxLen - cipherText->len, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherText->len += output.len; + HcfBlobDataFree(&output); + } + return 0; +} + +static int32_t DesLongDecrypt(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plainText, HcfParamsSpec *params, + HcfBlob *cipherText) +{ + HcfBlob input = {.data = (uint8_t *)cipherText->data, .len = cipherText->len}; + HcfBlob output = {}; + int32_t maxLen = cipherText->len; + 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; + } + cipherText->len = output.len; + ret = cipher->doFinal(cipher, nullptr, &output); + if (ret != 0) { + LOGE("doFinal failed!"); + return ret; + } + if (output.data != nullptr) { + if (memcpy_s(cipherText + cipherText->len, maxLen - cipherText->len, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherText->len += output.len; + HcfBlobDataFree(&output); + } + + if (cipherText->len != plainText->len) { + return -1; + } + return memcmp(cipherText, plainText->data, cipherText->len); +} + +HWTEST_F(Crypto3DesCipherTest, Crypto3DesCipherTest036, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + 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, &cipherText); + if (ret != 0) { + LOGE("DesEncrypt failed! %d", ret); + goto clearup; + } + + ret = DesLongDecrypt(cipher, key, &input, nullptr, &cipherText); + 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..2e2aec3ace809c24d69501bc7e177ad7032d0bbb 100644 --- a/test/unittest/src/crypto_sm4_cfb_cipher_test.cpp +++ b/test/unittest/src/crypto_sm4_cfb_cipher_test.cpp @@ -868,4 +868,44 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoSM4CfbCipherTest, CryptoSm4CipherTest084, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + 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, &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = Sm4DecryptWithInputAndPar(cipher, key, &input, nullptr, &cipherText); + 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..e969332562afb11a641805dd5d773d53e5a73fcf 100644 --- a/test/unittest/src/crypto_sm4_cipher_test.cpp +++ b/test/unittest/src/crypto_sm4_cipher_test.cpp @@ -1303,4 +1303,44 @@ CLEAR_UP: HcfObjDestroy(cipher); EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoSM4CipherTest, CryptoSm4CipherTest082, TestSize.Level0) +{ + int ret = 0; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + 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, &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = Sm4DecryptWithInputAndPar(cipher, key, &input, nullptr, &cipherText); + 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..28bf55d849a4f3baca8cb2a512d0e565be5cf24a 100644 --- a/test/unittest/src/crypto_sm4_ecb_cipher_test.cpp +++ b/test/unittest/src/crypto_sm4_ecb_cipher_test.cpp @@ -818,4 +818,43 @@ 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 }; + uint8_t cipherBuffer[CIPHER_TEXT_LEN] = { 0 }; + HcfBlob cipherText = { .data = NULL, .len = CIPHER_TEXT_LEN }; + cipherText.data = cipherBuffer; + 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, &cipherText); + if (ret != 0) { + LOGE("AesEncrypt failed! %d", ret); + goto CLEAR_UP; + } + + ret = Sm4DecryptWithInputAndPar(cipher, key, &input, nullptr, &cipherText); + 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..f59765a55fa77525b8d90a576e6f229524aabd2e 100644 --- a/test/unittest/src/sm4_common.cpp +++ b/test/unittest/src/sm4_common.cpp @@ -228,6 +228,93 @@ 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, + HcfBlob *cipherText) +{ + HcfBlob output = {}; + int32_t maxLen = cipherText->len; + 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; + } + cipherText->len = output.len; + if (output.data != nullptr) { + if (memcpy_s(cipherText->data, 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->data + cipherText->len, maxLen - cipherText->len, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherText->len += output.len; + HcfBlobDataFree(&output); + } + return 0; +} + +int32_t Sm4DecryptWithInputAndPar(HcfCipher *cipher, HcfSymKey *key, HcfBlob *plain, HcfParamsSpec *params, + HcfBlob *cipherText) +{ + HcfBlob input = {.data = reinterpret_cast(cipherText->data), .len = cipherText->len}; + HcfBlob output = {}; + int32_t maxLen = cipherText->len; + 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; + } + cipherText->len = output.len; + if (output.data != nullptr) { + if (memcpy_s(cipherText->data, 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->data + cipherText->len, maxLen - cipherText->len, output.data, output.len) != EOK) { + HcfBlobDataFree(&output); + return -1; + } + cipherText->len += output.len; + HcfBlobDataFree(&output); + } + + if (cipherText->len != plain->len) { + return -1; + } + return memcmp(cipherText->data, plain->data, cipherText->len); +} + int32_t Sm4NoUpdateEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params, uint8_t *cipherText, int *cipherTextLen) {