From d14f56837031ebdab12871b148294c832fc09cf0 Mon Sep 17 00:00:00 2001 From: xwb Date: Thu, 8 Dec 2022 18:12:28 +0800 Subject: [PATCH] change included header in aes Signed-off-by: xwb --- frameworks/BUILD.gn | 1 - frameworks/crypto_operation/cipher.c | 1 - frameworks/frameworks.gni | 1 - .../aes => common}/inc/aes_openssl_common.h | 35 +++--- .../crypto_operation/aes/inc/aes_openssl.h | 18 --- test/unittest/src/crypto_aes_cipher_test.cpp | 117 ++++++++++++++++++ 6 files changed, 134 insertions(+), 39 deletions(-) rename plugin/openssl_plugin/{crypto_operation/aes => common}/inc/aes_openssl_common.h (64%) diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index ad2b803..a31f7ce 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 4a898f2..65674c1 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 bb71e27..af458e2 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 39dee96..be339aa 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 77d24f0..5fcb3da 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 0db75de..e7bf4ec 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 -- Gitee