diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index ad2b8036212f0746cfe6b140b7a83b007ca445aa..a31f7ce8b2eb73a6ee63c56b05207bfbfa4cd46c 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -55,7 +55,6 @@ ohos_shared_library("crypto_framework_lib") { deps = [ "//base/security/crypto_framework:crypto_openssl_plugin_lib", "//base/security/crypto_framework/common:crypto_plugin_common", - "//third_party/openssl:libcrypto_shared", ] external_deps = [ diff --git a/frameworks/crypto_operation/cipher.c b/frameworks/crypto_operation/cipher.c index 4a898f2a10c376c535f13615fc51389436c73e68..65674c1a0b68b210a8d49342972412d1d132e1d9 100644 --- a/frameworks/crypto_operation/cipher.c +++ b/frameworks/crypto_operation/cipher.c @@ -16,7 +16,6 @@ #include "cipher.h" #include "aes_openssl.h" #include "config.h" -#include "aes_openssl_common.h" #include "securec.h" #include "result.h" #include "string.h" diff --git a/frameworks/frameworks.gni b/frameworks/frameworks.gni index bb71e2712629f3d2ea3b8cec6db3b50a7662671a..af458e2cdae018bf8c98cd47f9df3e55bd4e2d6c 100644 --- a/frameworks/frameworks.gni +++ b/frameworks/frameworks.gni @@ -23,7 +23,6 @@ framework_inc_path = [ "${base_path}/interfaces/innerkits/key", "${base_path}/interfaces/innerkits/rand", "${base_path}/common/inc", - "${plugin_path}/openssl_plugin/aes/inc", "${plugin_path}/openssl_plugin/certificate/inc", "${plugin_path}/openssl_plugin/crypto_operation/key_agreement/inc", "${plugin_path}/openssl_plugin/crypto_operation/signature/inc", diff --git a/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl_common.h b/plugin/openssl_plugin/common/inc/aes_openssl_common.h similarity index 64% rename from plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl_common.h rename to plugin/openssl_plugin/common/inc/aes_openssl_common.h index 39dee96111f21c1397343139cc2e39ac15dc3ed4..be339aa60a229dae89ee919f3165b03ce4d9671e 100644 --- a/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl_common.h +++ b/plugin/openssl_plugin/common/inc/aes_openssl_common.h @@ -13,31 +13,30 @@ * limitations under the License. */ -/* - * Copyright (C) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ #ifndef HCF_AES_OPENSSL_COMMON_H #define HCF_AES_OPENSSL_COMMON_H +#include +#include +#include "aes_openssl.h" #include "detailed_iv_params.h" #include "detailed_ccm_params.h" #include "detailed_gcm_params.h" - -#include "aes_openssl.h" - -#include +typedef struct { + EVP_CIPHER_CTX *ctx; + enum HcfCryptoMode enc; + /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE need AEAD */ + bool aead; + uint32_t updateLen; + unsigned char *iv; + uint32_t ivLen; + /* GCM, CCM only */ + unsigned char *aad; + uint32_t aadLen; + unsigned char *tag; + uint32_t tagLen; +} CipherData; #ifdef __cplusplus extern "C" { 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 77d24f0b90c2bdb55ca42cd2e16e7d1d133c59f7..5fcb3da563c8fd8fd14044ea1f419059145b7c22 100644 --- a/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl.h +++ b/plugin/openssl_plugin/crypto_operation/aes/inc/aes_openssl.h @@ -15,26 +15,8 @@ #ifndef HCF_AES_OPENSSL_H #define HCF_AES_OPENSSL_H -#include -#include -#include "params_parser.h" #include "cipher_factory_spi.h" -typedef struct { - EVP_CIPHER_CTX *ctx; - enum HcfCryptoMode enc; - /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE need AEAD */ - bool aead; - uint32_t updateLen; - unsigned char *iv; - uint32_t ivLen; - /* GCM, CCM only */ - unsigned char *aad; - uint32_t aadLen; - unsigned char *tag; - uint32_t tagLen; -} CipherData; - #ifdef __cplusplus extern "C" { #endif diff --git a/test/unittest/src/crypto_aes_cipher_test.cpp b/test/unittest/src/crypto_aes_cipher_test.cpp index 0db75de6f062ce41e019dc33cd149284cd52b3e9..e7bf4ec85f5c45bdb5c046e8ba0b9c48794e2af3 100644 --- a/test/unittest/src/crypto_aes_cipher_test.cpp +++ b/test/unittest/src/crypto_aes_cipher_test.cpp @@ -46,6 +46,7 @@ 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; +constexpr int32_t AES_KEY_SIZE = 128; class CryptoAesCipherTest : public testing::Test { public: @@ -6611,4 +6612,120 @@ HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest144, TestSize.Level0) } EXPECT_NE(ret, 0); } + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest145, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGeneratorSpi *generator = nullptr; + HcfSymKey *key = nullptr; + SymKeyAttr attr = { .algo = HCF_ALG_AES, .keySize = AES_KEY_SIZE }; + + ret = HcfSymKeyGeneratorSpiCreate(&attr, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorSpiCreate failed!%d", ret); + goto CLEAR_UP; + } + ret = generator->engineGenerateSymmKey(nullptr, &key); + if (ret != 0) { + LOGE("engineGenerateSymmKey failed!"); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest146, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGeneratorSpi *generator = nullptr; + HcfSymKey *key = nullptr; + HcfCipher *cipher = nullptr; + SymKeyAttr attr = { .algo = HCF_ALG_AES, .keySize = AES_KEY_SIZE }; + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + ret = HcfSymKeyGeneratorSpiCreate(&attr, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorSpiCreate failed!%d", ret); + goto CLEAR_UP; + } + ret = generator->engineGenerateSymmKey(reinterpret_cast(cipher), &key); + if (ret != 0) { + LOGE("engineGenerateSymmKey failed!"); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(generator); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest147, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGeneratorSpi *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 }; + SymKeyAttr attr = { .algo = HCF_ALG_AES, .keySize = AES_KEY_SIZE }; + + ret = HcfSymKeyGeneratorSpiCreate(&attr, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorSpiCreate failed!%d", ret); + goto CLEAR_UP; + } + ret = generator->engineConvertSymmKey(nullptr, &keyTmpBlob, &key); + if (ret != 0) { + LOGE("engineConvertSymmKey failed!"); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(generator); + EXPECT_NE(ret, 0); +} + +HWTEST_F(CryptoAesCipherTest, CryptoAesCipherTest148, TestSize.Level0) +{ + int ret = 0; + HcfSymKeyGeneratorSpi *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; + SymKeyAttr attr = { .algo = HCF_ALG_AES, .keySize = AES_KEY_SIZE }; + + ret = HcfCipherCreate("AES128|ECB|PKCS5", &cipher); + if (ret != 0) { + LOGE("HcfCipherCreate failed!"); + goto CLEAR_UP; + } + ret = HcfSymKeyGeneratorSpiCreate(&attr, &generator); + if (ret != 0) { + LOGE("HcfSymKeyGeneratorSpiCreate failed!%d", ret); + goto CLEAR_UP; + } + ret = generator->engineConvertSymmKey(reinterpret_cast(cipher), &keyTmpBlob, &key); + if (ret != 0) { + LOGE("engineConvertSymmKey failed!"); + } + +CLEAR_UP: + HcfObjDestroy(key); + HcfObjDestroy(generator); + HcfObjDestroy(cipher); + EXPECT_NE(ret, 0); +} } \ No newline at end of file