diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index 318b6b0bb1323dc80ed94d319b6e0f4c49eb68a5..32819374b2eafd629d55d09f01f353150e837318 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -30,11 +30,20 @@ ohos_shared_library("ohcrypto") { "-Wall", ] - include_dirs = [ "../../interfaces/kits/native/include" ] + include_dirs = [ + "../../interfaces/kits/native/include", + "include", + ] + include_dirs += framework_inc_path sources = [ + "src/asym_key.c", "src/crypto_common.c", "src/digest.c", + "src/native_common.c", + "src/signature.c", + "src/sym_cipher.c", + "src/sym_key.c", ] deps = [ "${framework_path}:crypto_framework_lib" ] @@ -42,6 +51,7 @@ ohos_shared_library("ohcrypto") { external_deps = [ "hilog:libhilog" ] subsystem_name = "security" + output_extension = "so" innerapi_tags = [ "ndk" ] part_name = "crypto_framework" -} \ No newline at end of file +} diff --git a/interfaces/kits/native/include/asym_key.h b/frameworks/native/include/native_common.h similarity index 70% rename from interfaces/kits/native/include/asym_key.h rename to frameworks/native/include/native_common.h index d3ad9410f4d134282b0f5352c812b90949e8f489..dc4bff0940294491c1fd9f8af5854ba0a7273ade 100644 --- a/interfaces/kits/native/include/asym_key.h +++ b/frameworks/native/include/native_common.h @@ -13,7 +13,20 @@ * limitations under the License. */ -#ifndef ASYM_KEY_H -#define ASYM_KEY_H +#ifndef NATIVE_COMMON_H +#define NATIVE_COMMON_H -#endif \ No newline at end of file +#include "result.h" +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +OH_Crypto_ErrCode GetOhCryptoErrCode(HcfResult errCode); + +#ifdef __cplusplus +} +#endif + +#endif /* NATIVE_COMMON_H */ \ No newline at end of file diff --git a/frameworks/native/src/asym_key.c b/frameworks/native/src/asym_key.c index 13e52f90406f31df4140aa4ef47fa3eca03be78e..53cda780e6d61bfbb9502ad3435eccfb7a7ee411 100644 --- a/frameworks/native/src/asym_key.c +++ b/frameworks/native/src/asym_key.c @@ -11,4 +11,211 @@ * 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. - */ \ No newline at end of file + */ + +#include "crypto_asym_key.h" +#include +#include +#include "key_pair.h" +#include "memory.h" +#include "pub_key.h" +#include "result.h" +#include "blob.h" +#include "object_base.h" +#include "native_common.h" +#include "big_integer.h" +#include "asy_key_generator.h" + + +typedef struct OH_CryptoAsymKeyGenerator { + HcfObjectBase base; + + HcfResult (*generateKeyPair)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, + HcfKeyPair **returnKeyPair); + + HcfResult (*convertKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob, + HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair); + + HcfResult (*convertPemKey)(HcfAsyKeyGenerator *self, HcfParamsSpec *params, const char *pubKeyStr, + const char *priKeyStr, HcfKeyPair **returnKeyPair); + + const char *(*getAlgoName)(HcfAsyKeyGenerator *self); +} OH_CryptoAsymKeyGenerator; + +typedef struct OH_CryptoKeyPair { + HcfObjectBase base; + + HcfPriKey *priKey; + + HcfPubKey *pubKey; +} OH_CryptoKeyPair; + +typedef struct OH_CryptoPubKey { + HcfKey base; + + HcfResult (*getAsyKeySpecBigInteger)(const HcfPubKey *self, const AsyKeySpecItem item, + HcfBigInteger *returnBigInteger); + + HcfResult (*getAsyKeySpecString)(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString); + + HcfResult (*getAsyKeySpecInt)(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt); + + HcfResult (*getEncodedDer)(const HcfPubKey *self, const char *format, HcfBlob *returnBlob); +} OH_CryptoPubKey; + +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_CryptoAsymKeyGenerator **ctx) +{ + if (ctx == NULL) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HcfAsyKeyGeneratorCreate(algoName, (HcfAsyKeyGenerator **)ctx); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Generate(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyCtx) +{ + if ((ctx == NULL) || (keyCtx == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->generateKeyPair((HcfAsyKeyGenerator *)ctx, NULL, (HcfKeyPair **)keyCtx); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_EncodingType type, + Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx) +{ + if ((ctx == NULL) || (pubKeyData == NULL && priKeyData == NULL) || (keyCtx == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HCF_SUCCESS; + const char *priKeyStr = (priKeyData == NULL)? NULL : (const char *)priKeyData->data; + const char *pubKeyStr = (pubKeyData == NULL)? NULL : (const char *)pubKeyData->data; + switch (type) { + case CRYPTO_PEM: + ret = ctx->convertPemKey((HcfAsyKeyGenerator *)ctx, NULL, pubKeyStr, priKeyStr, (HcfKeyPair **)keyCtx); + break; + case CRYPTO_DER: + ret = ctx->convertKey((HcfAsyKeyGenerator *)ctx, NULL, + (HcfBlob *)pubKeyData, (HcfBlob *)priKeyData, (HcfKeyPair **)keyCtx); + break; + default: + return CRYPTO_INVALID_PARAMS; + } + return GetOhCryptoErrCode(ret); +} + +const char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx) +{ + if (ctx == NULL) { + return NULL; + } + return ctx->getAlgoName((HcfAsyKeyGenerator *)ctx); +} + +void OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx) +{ + if (ctx == NULL) { + return; + } + ctx->base.destroy((HcfObjectBase *)ctx); +} + +void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx) +{ + if (keyCtx == NULL) { + return; + } + keyCtx->base.destroy((HcfObjectBase *)keyCtx); +} + +OH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx) +{ + if (keyCtx == NULL) { + return NULL; + } + return (OH_CryptoPubKey *)keyCtx->pubKey; +} + +OH_Crypto_ErrCode OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_EncodingType type, + const char *encodingStandard, Crypto_DataBlob *out) +{ + if ((key == NULL) || (out == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HCF_SUCCESS; + char *pemStr = NULL; + switch (type) { + case CRYPTO_PEM: + ret = key->base.getEncodedPem((HcfKey *)key, encodingStandard, &pemStr); + if (ret != HCF_SUCCESS) { + break; + } + out->data = (uint8_t *)pemStr; + out->len = strlen(pemStr); + break; + case CRYPTO_DER: + if (encodingStandard != NULL) { + ret = key->getEncodedDer((HcfPubKey *)key, encodingStandard, (HcfBlob *)out); + break; + } else { + ret = key->base.getEncoded((HcfKey *)key, (HcfBlob *)out); + break; + } + default: + return CRYPTO_INVALID_PARAMS; + } + + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value) +{ + if ((key == NULL) || (value == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HCF_SUCCESS; + int32_t *returnInt = NULL; + char *returnStr = NULL; + HcfBigInteger bigIntValue = {0}; + switch (item) { + case CRYPTO_ECC_H_INT: + case CRYPTO_ECC_FIELD_SIZE_INT: + returnInt = (int32_t *)HcfMalloc(sizeof(int32_t), 0); + if (returnInt == NULL) { + ret = HCF_ERR_MALLOC; + break; + } + ret = key->getAsyKeySpecInt((HcfPubKey *)key, (AsyKeySpecItem)item, returnInt); + if (ret != HCF_SUCCESS) { + HcfFree(returnInt); + break; + } + value->data = (uint8_t *)returnInt; + value->len = sizeof(int32_t); + case CRYPTO_ECC_FIELD_TYPE_STR: + case CRYPTO_ECC_CURVE_NAME_STR: + ret = key->getAsyKeySpecString((HcfPubKey *)key, (AsyKeySpecItem)item, &returnStr); + if (ret != HCF_SUCCESS) { + break; + } + value->data = (uint8_t *)returnStr; + value->len = strlen(returnStr); + case CRYPTO_DH_L_INT: + ret = key->getAsyKeySpecBigInteger((HcfPubKey *)key, + (AsyKeySpecItem)item, &bigIntValue); + if (ret != HCF_SUCCESS) { + break; + } + value->data = (uint8_t *)bigIntValue.data; + value->len = (size_t)bigIntValue.len; + default: + ret = key->getAsyKeySpecBigInteger((HcfPubKey *)key, + (AsyKeySpecItem)item, &bigIntValue); + if (ret != HCF_SUCCESS) { + break; + } + value->data = (uint8_t *)bigIntValue.data; + value->len = (size_t)bigIntValue.len; + } + return GetOhCryptoErrCode(ret); +} diff --git a/frameworks/native/src/crypto_common.c b/frameworks/native/src/crypto_common.c index 13e52f90406f31df4140aa4ef47fa3eca03be78e..b3d00a2811a1ff3bc8f548334530edff5cb2f3be 100644 --- a/frameworks/native/src/crypto_common.c +++ b/frameworks/native/src/crypto_common.c @@ -11,4 +11,16 @@ * 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. - */ \ No newline at end of file + */ + +#include "crypto_common.h" +#include "blob.h" + + +void OH_Crypto_FreeDataBlob(Crypto_DataBlob *dataBlob) +{ + if (dataBlob == NULL) { + return; + } + return HcfBlobDataFree((HcfBlob *)dataBlob); +} \ No newline at end of file diff --git a/frameworks/native/src/digest.c b/frameworks/native/src/digest.c index 14314f460ca418c0f2ce76d8cbfbb2f2b2ef48f4..b8d9ab2ff5e3a38d3ef96e5d96490b9057cbb2c0 100644 --- a/frameworks/native/src/digest.c +++ b/frameworks/native/src/digest.c @@ -13,10 +13,13 @@ * limitations under the License. */ -#include "digest.h" -#include "result.h" +#include "crypto_digest.h" #include "md.h" #include "crypto_common.h" +#include "blob.h" +#include "object_base.h" +#include "result.h" +#include "native_common.h" struct OH_CryptoDigest { HcfObjectBase base; @@ -30,32 +33,53 @@ struct OH_CryptoDigest { const char *(*getAlgoName)(HcfMd *self); }; -Crypto_Result OH_CryptoDigest_Create(const char *algoName, OH_CryptoDigest **md) +OH_Crypto_ErrCode OH_CryptoDigest_Create(const char *algoName, OH_CryptoDigest **ctx) { - return (Crypto_Result)HcfMdCreate(algoName, (HcfMd **)md); + if (ctx == NULL) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HcfMdCreate(algoName, (HcfMd **)ctx); + return GetOhCryptoErrCode(ret); } -Crypto_Result OH_CryptoDigest_Update(OH_CryptoDigest *ctx, Crypto_DataBlob *in) +OH_Crypto_ErrCode OH_CryptoDigest_Update(OH_CryptoDigest *ctx, Crypto_DataBlob *in) { - return (Crypto_Result)ctx->update((HcfMd *)ctx, (HcfBlob *)in); + if ((ctx == NULL) || (in == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->update((HcfMd *)ctx, (HcfBlob *)in); + return GetOhCryptoErrCode(ret); } -Crypto_Result OH_CryptoDigest_Final(OH_CryptoDigest *ctx, Crypto_DataBlob *out) +OH_Crypto_ErrCode OH_CryptoDigest_Final(OH_CryptoDigest *ctx, Crypto_DataBlob *out) { - return (Crypto_Result)ctx->doFinal((HcfMd *)ctx, (HcfBlob *)out); + if ((ctx == NULL) || (out == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->doFinal((HcfMd *)ctx, (HcfBlob *)out); + return GetOhCryptoErrCode(ret); } uint32_t OH_CryptoDigest_GetLength(OH_CryptoDigest *ctx) { + if (ctx == NULL) { + return CRYPTO_INVALID_PARAMS; + } return ctx->getMdLength((HcfMd *)ctx); } const char *OH_CryptoDigest_GetAlgoName(OH_CryptoDigest *ctx) { + if (ctx == NULL) { + return NULL; + } return ctx->getAlgoName((HcfMd *)ctx); } void OH_DigestCrypto_Destroy(OH_CryptoDigest *ctx) { - return ctx->base.destroy((HcfObjectBase *)ctx); + if (ctx == NULL) { + return; + } + ctx->base.destroy((HcfObjectBase *)ctx); } \ No newline at end of file diff --git a/interfaces/kits/native/include/sym_key.h b/frameworks/native/src/native_common.c similarity index 57% rename from interfaces/kits/native/include/sym_key.h rename to frameworks/native/src/native_common.c index d0f6ca3673a3484e1d183114cd44242bc2e7376b..65b23823e9b3cba427eee59fa44e820ac540b0f6 100644 --- a/interfaces/kits/native/include/sym_key.h +++ b/frameworks/native/src/native_common.c @@ -13,7 +13,20 @@ * limitations under the License. */ -#ifndef SYM_KEY_H -#define SYM_KEY_H +#include "native_common.h" -#endif \ No newline at end of file +OH_Crypto_ErrCode GetOhCryptoErrCode(HcfResult errCode) +{ + switch (errCode) { + case HCF_SUCCESS: + return CRYPTO_SUCCESS; + case HCF_INVALID_PARAMS: + return CRYPTO_INVALID_PARAMS; + case HCF_NOT_SUPPORT: + return CRYPTO_NOT_SUPPORTED; + case HCF_ERR_MALLOC: + return CRYPTO_MEMORY_ERROR; + default: + return CRYPTO_OPERTION_ERROR; + } +} \ No newline at end of file diff --git a/frameworks/native/src/signature.c b/frameworks/native/src/signature.c index 13e52f90406f31df4140aa4ef47fa3eca03be78e..c2a779720eadedc6f52ae65f24a70c55eb14ccaa 100644 --- a/frameworks/native/src/signature.c +++ b/frameworks/native/src/signature.c @@ -11,4 +11,171 @@ * 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. - */ \ No newline at end of file + */ + +#include "crypto_signature.h" +#include +#include +#include "signature.h" +#include "memory.h" +#include "crypto_common.h" +#include "blob.h" +#include "object_base.h" +#include "result.h" +#include "native_common.h" + +struct OH_CryptoVerify { + HcfObjectBase base; + + HcfResult (*init)(HcfVerify *self, HcfParamsSpec *params, HcfPubKey *publicKey); + + HcfResult (*update)(HcfVerify *self, HcfBlob *data); + + bool (*verify)(HcfVerify *self, HcfBlob *data, HcfBlob *signatureData); + + HcfResult (*recover)(HcfVerify *self, HcfBlob *signatureData, HcfBlob *rawSignatureData); + + const char *(*getAlgoName)(HcfVerify *self); + + HcfResult (*setVerifySpecInt)(HcfVerify *self, SignSpecItem item, int32_t saltLen); + + HcfResult (*getVerifySpecString)(HcfVerify *self, SignSpecItem item, char **returnString); + + HcfResult (*getVerifySpecInt)(HcfVerify *self, SignSpecItem item, int32_t *returnInt); + + HcfResult (*setVerifySpecUint8Array)(HcfVerify *self, SignSpecItem item, HcfBlob blob); +}; + +OH_Crypto_ErrCode OH_CryptoVerify_Create(const char *algoName, OH_CryptoVerify **ctx) +{ + if (ctx == NULL) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HcfVerifyCreate(algoName, (HcfVerify **)ctx); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoVerify_Init(OH_CryptoVerify *ctx, OH_CryptoPubKey *pubKey) +{ + if ((ctx == NULL) || (pubKey == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->init((HcfVerify *)ctx, NULL, (HcfPubKey *)pubKey); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoVerify_Update(OH_CryptoVerify *ctx, Crypto_DataBlob *in) +{ + if ((ctx == NULL) || (in == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->update((HcfVerify *)ctx, (HcfBlob *)in); + return GetOhCryptoErrCode(ret); +} + +bool OH_CryptoVerify_Final(OH_CryptoVerify *ctx, Crypto_DataBlob *in, Crypto_DataBlob *signData) +{ + if ((ctx == NULL) || (signData == NULL)) { + return false; + } + bool ret = ctx->verify((HcfVerify *)ctx, (HcfBlob *)in, (HcfBlob *)signData); + if (ret != true) { + return false; + } + + return ret; +} + +OH_Crypto_ErrCode OH_CryptoVerify_Recover(OH_CryptoVerify *ctx, Crypto_DataBlob *signData, + Crypto_DataBlob *rawSignData) +{ + if ((ctx == NULL) || (signData == NULL) || (rawSignData == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->recover((HcfVerify *)ctx, (HcfBlob *)signData, (HcfBlob *)rawSignData); + return GetOhCryptoErrCode(ret); +} + +const char *OH_CryptoVerify_GetAlgoName(OH_CryptoVerify *ctx) +{ + if (ctx == NULL) { + return NULL; + } + return ctx->getAlgoName((HcfVerify *)ctx); +} + +OH_Crypto_ErrCode OH_CryptoVerify_SetParam(OH_CryptoVerify *ctx, CryptoSignature_ParamType type, + Crypto_DataBlob *value) +{ + if ((ctx == NULL) || (value == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HCF_INVALID_PARAMS; + switch (type) { + case CRYPTO_PSS_SALT_LEN_INT: + case CRYPTO_PSS_TRAILER_FIELD_INT: + if (value->len != sizeof(int32_t)) { + ret = HCF_INVALID_PARAMS; + break; + } + ret = ctx->setVerifySpecInt((HcfVerify *)ctx, (SignSpecItem)type, *((int32_t *)value->data)); + break; + case CRYPTO_SM2_USER_ID_DATABLOB: + case CRYPTO_PSS_MGF1_NAME_STR: + case CRYPTO_PSS_MGF_NAME_STR: + case CRYPTO_PSS_MD_NAME_STR: + ret = ctx->setVerifySpecUint8Array((HcfVerify *)ctx, (SignSpecItem)type, *((HcfBlob *)value)); + break; + default: + return CRYPTO_INVALID_PARAMS; + } + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoVerify_GetParam(OH_CryptoVerify *ctx, CryptoSignature_ParamType type, + Crypto_DataBlob *value) +{ + if ((ctx == NULL) || (value == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + int32_t *returnInt = NULL; + char *returnStr = NULL; + HcfResult ret = HCF_INVALID_PARAMS; + switch (type) { + case CRYPTO_PSS_SALT_LEN_INT: + case CRYPTO_PSS_TRAILER_FIELD_INT: + case CRYPTO_SM2_USER_ID_DATABLOB: + returnInt = (int32_t *)HcfMalloc(sizeof(int32_t), 0); + if (returnInt == NULL) { + return CRYPTO_MEMORY_ERROR; + } + ret = ctx->getVerifySpecInt((HcfVerify *)ctx, (SignSpecItem)type, returnInt); + if (ret != HCF_SUCCESS) { + HcfFree(returnInt); + break; + } + value->data = (uint8_t *)returnInt; + value->len = sizeof(int32_t); + case CRYPTO_PSS_MD_NAME_STR: + case CRYPTO_PSS_MGF_NAME_STR: + case CRYPTO_PSS_MGF1_NAME_STR: + ret = ctx->getVerifySpecString((HcfVerify *)ctx, (SignSpecItem)type, &returnStr); + if (ret != HCF_SUCCESS) { + break; + } + value->data = (uint8_t *)returnStr; + value->len = strlen(returnStr); + default: + return CRYPTO_INVALID_PARAMS; + } + return GetOhCryptoErrCode(ret); +} + + +void OH_CryptoVerify_Destroy(OH_CryptoVerify *ctx) +{ + if (ctx == NULL) { + return; + } + ctx->base.destroy((HcfObjectBase *)ctx); +} diff --git a/frameworks/native/src/sym_cipher.c b/frameworks/native/src/sym_cipher.c index 13e52f90406f31df4140aa4ef47fa3eca03be78e..282907c3c294753fec0f84af245d99f4716181f6 100644 --- a/frameworks/native/src/sym_cipher.c +++ b/frameworks/native/src/sym_cipher.c @@ -11,4 +11,145 @@ * 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. - */ \ No newline at end of file + */ + +#include "crypto_sym_cipher.h" +#include "memory.h" +#include +#include "sym_key_generator.h" +#include "crypto_common.h" +#include "cipher.h" +#include "blob.h" +#include "object_base.h" +#include "result.h" +#include "native_common.h" + +struct OH_CryptoSymCipher { + HcfObjectBase base; + + HcfResult (*init)(HcfCipher *self, enum HcfCryptoMode opMode, + HcfKey *key, HcfParamsSpec *params); + + HcfResult (*update)(HcfCipher *self, HcfBlob *input, HcfBlob *output); + + HcfResult (*doFinal)(HcfCipher *self, HcfBlob *input, HcfBlob *output); + + const char *(*getAlgorithm)(HcfCipher *self); + + HcfResult (*setCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob blob); + + HcfResult (*getCipherSpecString)(HcfCipher *self, CipherSpecItem item, char **returnString); + + HcfResult (*getCipherSpecUint8Array)(HcfCipher *self, CipherSpecItem item, HcfBlob *returnUint8Array); +}; + +struct OH_CryptoSymCipherParams { + HcfParamsSpec base; + HcfBlob iv; + HcfBlob aad; + HcfBlob tag; +}; + +struct OH_CryptoSymKey { + HcfKey key; + + void (*clearMem)(HcfSymKey *self); +}; + +OH_Crypto_ErrCode OH_CryptoSymCipherParams_Create(OH_CryptoSymCipherParams **params) +{ + if (params == NULL) { + return CRYPTO_INVALID_PARAMS; + } + *params = (OH_CryptoSymCipherParams *)HcfMalloc(sizeof(OH_CryptoSymCipherParams), 0); + if (*params == NULL) { + return CRYPTO_MEMORY_ERROR; + } + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode OH_CryptoSymCipherParams_SetParam(OH_CryptoSymCipherParams *params, + CryptoSymCipher_ParamsType paramsType, Crypto_DataBlob *value) +{ + if ((params == NULL) || (value == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + switch (paramsType) { + case CRYPTO_IV_DATABLOB: + params->iv.data = value->data; + params->iv.len = value->len; + break; + case CRYPTO_AAD_DATABLOB: + params->aad.data = value->data; + params->aad.len = value->len; + break; + case CRYPTO_TAG_DATABLOB: + params->tag.data = value->data; + params->tag.len = value->len; + break; + default: + return CRYPTO_INVALID_PARAMS; + } + return CRYPTO_SUCCESS; +} + +void OH_CryptoSymCipherParams_Destroy(OH_CryptoSymCipherParams *params) +{ + if (params == NULL) { + return; + } + HcfFree(params); +} + +OH_Crypto_ErrCode OH_CryptoSymCipher_Create(const char *algoName, OH_CryptoSymCipher **ctx) +{ + if (ctx == NULL) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HcfCipherCreate(algoName, (HcfCipher **)ctx); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoSymCipher_Init(OH_CryptoSymCipher *ctx, Crypto_CipherMode mod, + OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params) +{ + if ((ctx == NULL) || (key == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->init((HcfCipher *)ctx, (enum HcfCryptoMode)mod, (HcfKey *)key, (HcfParamsSpec *)params); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoSymCipher_Update(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out) +{ + if ((ctx == NULL) || (in == NULL) || (out == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->update((HcfCipher *)ctx, (HcfBlob*)in, (HcfBlob *)out); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoSymCipher_Final(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out) +{ + if ((ctx == NULL) || (out == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->doFinal((HcfCipher *)ctx, (HcfBlob* )in, (HcfBlob* )out); + return GetOhCryptoErrCode(ret); +} + +const char *OH_CryptoSymCipher_GetAlgoName(OH_CryptoSymCipher *ctx) +{ + if (ctx == NULL) { + return NULL; + } + return ctx->getAlgorithm((HcfCipher *)ctx); +} + +void OH_CryptoSymCipher_Destroy(OH_CryptoSymCipher *ctx) +{ + if (ctx == NULL) { + return; + } + ctx->base.destroy((HcfObjectBase *)ctx); +} diff --git a/frameworks/native/src/sym_key.c b/frameworks/native/src/sym_key.c index 13e52f90406f31df4140aa4ef47fa3eca03be78e..df250d8658ceb3fae34ac1bf6b268ec9f50a7f01 100644 --- a/frameworks/native/src/sym_key.c +++ b/frameworks/native/src/sym_key.c @@ -11,4 +11,102 @@ * 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. - */ \ No newline at end of file + */ + + +#include "crypto_sym_key.h" +#include "crypto_common.h" +#include "sym_key_generator.h" +#include "result.h" +#include "blob.h" +#include "object_base.h" +#include "native_common.h" + + +struct OH_CryptoSymKeyGenerator { + HcfObjectBase base; + + /** Generate symmetric key object */ + HcfResult (*generateSymKey)(HcfSymKeyGenerator *self, HcfSymKey **symKey); + + /** Convert byte data to symmetric key object */ + HcfResult (*convertSymKey)(HcfSymKeyGenerator *self, const HcfBlob *key, HcfSymKey **symKey); + + /** Get the algorithm name of the current these key generator objects */ + const char *(*getAlgoName)(HcfSymKeyGenerator *self); +}; + +struct OH_CryptoSymKey { + HcfKey key; + + void (*clearMem)(HcfSymKey *self); +}; + +OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Create(const char *algoName, OH_CryptoSymKeyGenerator **ctx) +{ + if (ctx == NULL) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = HcfSymKeyGeneratorCreate(algoName, (HcfSymKeyGenerator **)ctx); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Generate(OH_CryptoSymKeyGenerator *ctx, OH_CryptoSymKey **keyCtx) +{ + if ((ctx == NULL) || (keyCtx == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->generateSymKey((HcfSymKeyGenerator *)ctx, (HcfSymKey **)keyCtx); + return GetOhCryptoErrCode(ret); +} + +OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Convert(OH_CryptoSymKeyGenerator *ctx, + const Crypto_DataBlob *keyData, OH_CryptoSymKey **keyCtx) +{ + if ((ctx == NULL) || (keyData == NULL) || (keyCtx == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = ctx->convertSymKey((HcfSymKeyGenerator *)ctx, (HcfBlob *)keyData, (HcfSymKey **)keyCtx); + return GetOhCryptoErrCode(ret); +} + +const char *OH_CryptoSymKeyGenerator_GetAlgoName(OH_CryptoSymKeyGenerator *ctx) +{ + if (ctx == NULL) { + return NULL; + } + return ctx->getAlgoName((HcfSymKeyGenerator *)ctx); +} + +void OH_CryptoSymKeyGenerator_Destroy(OH_CryptoSymKeyGenerator *ctx) +{ + if (ctx == NULL) { + return; + } + ctx->base.destroy((HcfObjectBase *)ctx); +} + +const char *OH_CryptoSymKey_GetAlgoName(OH_CryptoSymKey *keyCtx) +{ + if (keyCtx == NULL) { + return NULL; + } + return keyCtx->key.getAlgorithm((HcfKey *)keyCtx); +} + +OH_Crypto_ErrCode OH_CryptoSymKey_GetKeyData(OH_CryptoSymKey *keyCtx, Crypto_DataBlob *out) +{ + if ((keyCtx == NULL) || (out == NULL)) { + return CRYPTO_INVALID_PARAMS; + } + HcfResult ret = keyCtx->key.getEncoded((HcfKey *)keyCtx, (HcfBlob *)out); + return GetOhCryptoErrCode(ret); +} + +void OH_CryptoSymKey_Destroy(OH_CryptoSymKey *keyCtx) +{ + if (keyCtx == NULL) { + return; + } + keyCtx->key.base.destroy((HcfObjectBase *)keyCtx); +} diff --git a/interfaces/kits/native/include/crypto_asym_key.h b/interfaces/kits/native/include/crypto_asym_key.h new file mode 100644 index 0000000000000000000000000000000000000000..a110ffd6bd3a425a5598e5c1ab2bc06bc9dfee2a --- /dev/null +++ b/interfaces/kits/native/include/crypto_asym_key.h @@ -0,0 +1,267 @@ +/* + * Copyright (C) 2024 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 CRYPTO_ASYM_KEY_H +#define CRYPTO_ASYM_KEY_H + +/** + * @addtogroup CryptoAsymKeyApi + * @{ + * + * @brief Describe the features provided by the openHarmony asymmetric key related interface for applications. + * + * @since 12 + */ + +/** + * @file crypto_asym_key.h + * + * @brief Defines the AsymKey APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + */ + +#include "crypto_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the key pair structure. + * + * @since 12 + */ +typedef struct OH_CryptoKeyPair OH_CryptoKeyPair; + +/** + * @brief Define the public Key structure. + * + * @since 12 + */ +typedef struct OH_CryptoPubKey OH_CryptoPubKey; + +/** + * @brief Define the asymmetric key parameter types. + * + * @since 12 + */ +typedef enum { + /** Indicates the DSA prime p. */ + CRYPTO_DSA_P_DATABLOB = 101, + /** Indicates the DSA sub-prime q. */ + CRYPTO_DSA_Q_DATABLOB = 102, + /** Indicates the DSA base g. */ + CRYPTO_DSA_G_DATABLOB = 103, + /** Indicates the DSA private key. */ + CRYPTO_DSA_SK_DATABLOB = 104, + /** Indicates the DSA public key. */ + CRYPTO_DSA_PK_DATABLOB = 105, + + /** Indicates the prime p of an elliptic curve (EC) prime finite field. */ + CRYPTO_ECC_FP_P_DATABLOB = 201, + /** Indicates the first coefficient a of this elliptic curve. */ + CRYPTO_ECC_A_DATABLOB = 202, + /** Indicates the second coefficient b of this elliptic curve. */ + CRYPTO_ECC_B_DATABLOB = 203, + /** Indicates the affine x-coordinate of base point g. */ + CRYPTO_ECC_G_X_DATABLOB = 204, + /** Indicates the affine y-coordinate of base point g. */ + CRYPTO_ECC_G_Y_DATABLOB = 205, + /** Indicates the order of the base point g. */ + CRYPTO_ECC_N_DATABLOB = 206, + /** Indicates the cofactor of the elliptic curve. */ + CRYPTO_ECC_H_INT = 207, + /** Indicates the private value of the ECC private key. */ + CRYPTO_ECC_SK_DATABLOB = 208, + /** Indicates the affine x-coordinate of a point, which is the public point of an ECC public key. */ + CRYPTO_ECC_PK_X_DATABLOB = 209, + /** Indicates the affine y-coordinate of a point, which is the public point of an ECC public key. */ + CRYPTO_ECC_PK_Y_DATABLOB = 210, + /** Indicates an elliptic curve finite field type. */ + CRYPTO_ECC_FIELD_TYPE_STR = 211, + /** Indicates the field size in bits. */ + CRYPTO_ECC_FIELD_SIZE_INT = 212, + /** Indicates the curve name according to SECG (Standards for Efficient Cryptography Group). */ + CRYPTO_ECC_CURVE_NAME_STR = 213, + + /** Indicates the modulus n of RSA algorithm. */ + CRYPTO_RSA_N_DATABLOB = 301, + /** Indicates the private exponent d of RSA algorithm. */ + CRYPTO_RSA_D_DATABLOB = 302, + /** Indicates the public exponent e of RSA algorithm. */ + CRYPTO_RSA_E_DATABLOB = 303, + + /** Indicates the prime p of DH algorithm. */ + CRYPTO_DH_P_DATABLOB = 401, + /** Indicates the generator g of DH algorithm. */ + CRYPTO_DH_G_DATABLOB = 402, + /** Indicates the number of bits of the private key length used in the DH algorithm. */ + CRYPTO_DH_L_INT = 403, + /** Indicates the private value of the DH private key. */ + CRYPTO_DH_SK_DATABLOB = 404, + /** Indicates the public value of the DH public key. */ + CRYPTO_DH_PK_DATABLOB = 405, + + /** Indicates the private value of the ED25519 private key. */ + CRYPTO_ED25519_SK_DATABLOB = 501, + /** Indicates the public value of the ED25519 public key. */ + CRYPTO_ED25519_PK_DATABLOB = 502, + /** Indicates the private value of the X25519 private key. */ + CRYPTO_X25519_SK_DATABLOB = 601, + /** Indicates the public value of the X25519 public key. */ + CRYPTO_X25519_PK_DATABLOB = 602, +} CryptoAsymKey_ParamType; + +/** + * @brief Define the encoding type. + * + * @since 12 + */ +typedef enum { + /** PEM format */ + CRYPTO_PEM = 0, + /** DER format */ + CRYPTO_DER = 1, +} Crypto_EncodingType; + +/** + * @brief Define the asymmetric key generator structure. + * + * @since 12 + */ +typedef struct OH_CryptoAsymKeyGenerator OH_CryptoAsymKeyGenerator; + +/** + * @brief Create an asymmetric key generator according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the generator. Example RSA1024|PRIMES_2. + * @param ctx Indicates the pointer to asymmetric key generator context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create(const char *algoName, OH_CryptoAsymKeyGenerator **ctx); + +/** + * @brief Generate an asymmetric key(a key pair). + * + * @param ctx Indicates the asymmetric key generator context. + * @param keyCtx Indicates the pointer to the asyKey context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Generate(OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyCtx); + +/** + * @brief Convert the asymmetric key data to a key pair. + * + * @param ctx Indicates the asymmetric key generator context. + * @param type Indicates the encryption encoding type. + * @param pubKeyData Indicates the public key data. + * @param priKeyData Indicates the private key data. + * @param keyCtx Indicates the pointer to the keyPair instance. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert(OH_CryptoAsymKeyGenerator *ctx, Crypto_EncodingType type, + Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx); + +/** + * @brief Get the algorithm name of the asymmetric key generator. + * + * @param ctx Indicates the asymmetric key generator context. + * @return Returns the asymmetric key algorithm name. + * @since 12 + */ +const char *OH_CryptoAsymKeyGenerator_GetAlgoName(OH_CryptoAsymKeyGenerator *ctx); + +/** + * @brief Destroy the asymmetric key generator. + * + * @param ctx Indicates the asymmetric key generator context. + * @since 12 + */ +void OH_CryptoAsymKeyGenerator_Destroy(OH_CryptoAsymKeyGenerator *ctx); + +/** + * @brief Destroy the key pair. + * + * @param keyCtx Indicates the keyPair context. + * @since 12 + */ +void OH_CryptoKeyPair_Destroy(OH_CryptoKeyPair *keyCtx); + +/** + * @brief Get the public key of the key pair. + * + * @param keyCtx Indicates the keyPair context. + * @return Return the public key context from the key pair. + * @since 12 + */ +OH_CryptoPubKey *OH_CryptoKeyPair_GetPubKey(OH_CryptoKeyPair *keyCtx); + +/** + * @brief Encode the public key. + * + * @param key Indicates the public key. + * @param type Indicates the pubkey type. + * @param encodingStandard Indicates the encoding standard . + * @param out Indicates the encoded result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoPubKey_Encode(OH_CryptoPubKey *key, Crypto_EncodingType type, + const char *encodingStandard, Crypto_DataBlob *out); + +/** + * @brief Get the specified param of the public key. + * + * @param key Indicates the public key. + * @param item Indicates the asymmetric key param type. + * @param value Indicates the output data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoPubKey_GetParam(OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* CRYPTO_ASYM_KEY_H */ diff --git a/interfaces/kits/native/include/crypto_common.h b/interfaces/kits/native/include/crypto_common.h index c7f33e84119efcaac4326da3088b9baf6677a2a8..f3d13a787c1067a67c31651f8ab5a98559a8c199 100644 --- a/interfaces/kits/native/include/crypto_common.h +++ b/interfaces/kits/native/include/crypto_common.h @@ -20,46 +20,82 @@ * @addtogroup CryptoCommonApi * @{ * - * @brief Describe OpenHarmony common interfaces Provide for applications. + * @brief Describe openHarmony crypto common interfaces provide for applications. * - * @syscap SystemCapability.Security.CryptoFramework * @since 12 - * @version 1.0 */ /** * @file crypto_common.h * - * @brief Defines the CryptoCommon APIs. + * @brief Defines the crypto common APIs. * + * @library libohcrypto.so * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework * @since 12 - * @version 1.0 */ #include #include +/** + * @brief Crypto data struct. + * + * @since 12 + */ typedef struct Crypto_DataBlob { + /** Data buffer. */ uint8_t *data; + /** Data length. */ size_t len; } Crypto_DataBlob; +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates the error codes. + * + * @since 12 + */ typedef enum { + /** Indicates that crypto operation success. */ CRYPTO_SUCCESS = 0, - CRYPTO_INVALID_PARAMS = -10001, - CRYPTO_NOT_SUPPORT = -10002, - CRYPTO_ERR_MALLOC = -20001, - CRYPTO_CRYPTO_OPERTION = -30001, -} Crypto_Result; + /** Indicates that input parameters is invalid. */ + CRYPTO_INVALID_PARAMS = 401, + /** Indicates that function or algorithm is not supported. */ + CRYPTO_NOT_SUPPORTED = 801, + /** Indicates the memory error. */ + CRYPTO_MEMORY_ERROR = 17620001, + /** Indicates that crypto operation error. */ + CRYPTO_OPERTION_ERROR = 17630001, +} OH_Crypto_ErrCode; +/** + * @brief Define crypto cipher mode. + * + * @since 12 + */ typedef enum { + /** Indicates encryption operation. */ CRYPTO_ENCRYPT_MODE = 0, + /** Indicates decryption operation. */ CRYPTO_DECRYPT_MODE = 1, } Crypto_CipherMode; +/** + * @brief Free the data of dataBlob. + * + * @param dataBlob Indicates the data blob. + * @since 12 + */ +void OH_Crypto_FreeDataBlob(Crypto_DataBlob *dataBlob); + #ifdef __cplusplus } #endif +/** @} */ #endif /* CRYPTO_COMMON_H */ \ No newline at end of file diff --git a/interfaces/kits/native/include/digest.h b/interfaces/kits/native/include/crypto_digest.h similarity index 39% rename from interfaces/kits/native/include/digest.h rename to interfaces/kits/native/include/crypto_digest.h index 674716cd75c3c4f1c56469343b6f05a5831d2766..0fa2b98ee28d910eb3ecab81545ba37797971a32 100644 --- a/interfaces/kits/native/include/digest.h +++ b/interfaces/kits/native/include/crypto_digest.h @@ -13,30 +13,27 @@ * limitations under the License. */ -#ifndef DIGEST_H -#define DIGEST_H +#ifndef CRYPTO_DIGEST_H +#define CRYPTO_DIGEST_H /** * @addtogroup CryptoDigestApi * @{ * - * @brief Describe OpenHarmony encryption features, including key generation, - * encryption and decryption, signature verification, and digest interfaces - * Provide for applications. + * @brief Describe openHarmony digest interfaces provide for applications. * - * @syscap SystemCapability.Security.CryptoFramework * @since 12 - * @version 1.0 */ /** - * @file digest.h + * @file crypto_digest.h * - * @brief Defines the Digest APIs. + * @brief Defines the digest apis. * + * @library libohcrypto.so * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework * @since 12 - * @version 1.0 */ #include "crypto_common.h" @@ -45,92 +42,89 @@ extern "C" { #endif +/** + * @brief Define the digest structure. + * + * @since 12 + */ typedef struct OH_CryptoDigest OH_CryptoDigest; /** - * @brief Create the Digest generater. + * @brief Create a digest context according to the given algorithm name. * - * @param algoName Indicates the algorithm name for generating the generator. - * @param md Indicates the pointer to the md instance. + * @param algoName Indicates the algorithm name for generating the digest context. Example SHA256. + * @param ctx Indicates the pointer to the md context. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. - * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. - * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. - * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. - * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 401 - If crypto opertion failed. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 12 - * @version 1.0 */ -Crypto_Result OH_CryptoDigest_Create(const char *algoName, OH_CryptoDigest **md); +OH_Crypto_ErrCode OH_CryptoDigest_Create(const char *algoName, OH_CryptoDigest **ctx); /** - * @brief Update md with DataBlob. + * @brief Update digest with dataBlob. * - * @param md Indicates the pointer to the md instance. - * @param in Indicates the DataBlob. + * @param ctx Indicates the digest context. + * @param in Indicates the dataBlob. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. - * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. - * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. - * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. - * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 401 - If crypto opertion failed. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoDigest_Final * @since 12 - * @version 1.0 */ -Crypto_Result OH_CryptoDigest_Update(OH_CryptoDigest *ctx, Crypto_DataBlob *in); +OH_Crypto_ErrCode OH_CryptoDigest_Update(OH_CryptoDigest *ctx, Crypto_DataBlob *in); /** - * @brief Update md with DataBlob. + * @brief Final digest with dataBlob. * - * @param md Indicates the pointer to the md instance. - * @param out Return the result as DataBlob. + * @param ctx Indicates the digest context. + * @param out Indicates the result as dataBlob. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. - * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. - * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. - * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. - * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 401 - If crypto opertion failed. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoDigest_Update * @since 12 - * @version 1.0 */ -Crypto_Result OH_CryptoDigest_Final(OH_CryptoDigest *ctx, Crypto_DataBlob *out); +OH_Crypto_ErrCode OH_CryptoDigest_Final(OH_CryptoDigest *ctx, Crypto_DataBlob *out); /** - * @brief Get digest length. + * @brief Get the digest length of the digest context. * - * @param md Indicates the pointer to the md instance. + * @param ctx Indicates the digest context. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. - * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. - * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. - * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. - * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 401 - If crypto opertion failed. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 12 - * @version 1.0 */ uint32_t OH_CryptoDigest_GetLength(OH_CryptoDigest *ctx); /** - * @brief Get digest algoName. + * @brief Get the algorithm name of the digest context. * - * @param md Indicates the pointer to the md instance. - * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. - * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. - * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. - * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. - * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 401 - If crypto opertion failed. + * @param ctx Indicates the digest context. + * @return Return md algorithm name. * @since 12 - * @version 1.0 */ const char *OH_CryptoDigest_GetAlgoName(OH_CryptoDigest *ctx); /** - * @brief Destroy digest pointer. + * @brief Destroy the digest context. * - * @param md Indicates the pointer to the md instance. + * @param ctx Indicates the digest context. * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. - * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If paramSet is invalid. - * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORT} 801 - If algorithm name not support. - * {@link OH_Crypto_ErrCode#CRYPTO_ERR_MALLOC} 17620001 - If malloc failed. - * {@link OH_Crypto_ErrCode#CRYPTO_CRYPTO_OPERTION} 401 - If crypto opertion failed. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. * @since 12 - * @version 1.0 */ void OH_DigestCrypto_Destroy(OH_CryptoDigest *ctx); @@ -139,4 +133,4 @@ void OH_DigestCrypto_Destroy(OH_CryptoDigest *ctx); #endif /** @} */ -#endif /* DIGEST_H */ \ No newline at end of file +#endif /* CRYPTO_DIGEST_H */ \ No newline at end of file diff --git a/interfaces/kits/native/include/crypto_signature.h b/interfaces/kits/native/include/crypto_signature.h new file mode 100644 index 0000000000000000000000000000000000000000..f1683cd978591e381af77acd9e5fb44908123adb --- /dev/null +++ b/interfaces/kits/native/include/crypto_signature.h @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2024 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 CRYPTO_SIGNATURE_H +#define CRYPTO_SIGNATURE_H + +/** + * @addtogroup CryptoSignatureApi + * @{ + * + * @brief Describe openHarmony signature verification interfaces provide for applications. + * + * @since 12 + */ + +/** + * @file crypto_signature.h + * + * @brief Defines the Signature APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + */ + +#include "crypto_common.h" +#include "crypto_asym_key.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the signature param type. + * + * @since 12 + */ +typedef enum { + /** Indicates the algorithm name of the message digest function.*/ + CRYPTO_PSS_MD_NAME_STR = 100, + /** Indicates the algorithm name for the mask generation function. */ + CRYPTO_PSS_MGF_NAME_STR = 101, + /** Indicates the message digest parameter for the MGF1 mask generation function. */ + CRYPTO_PSS_MGF1_NAME_STR = 102, + /** Indicates the salt length in bits. */ + CRYPTO_PSS_SALT_LEN_INT = 103, + /** Indicates the value for the trailer field. */ + CRYPTO_PSS_TRAILER_FIELD_INT = 104, + /** Indicates the value for user id. */ + CRYPTO_SM2_USER_ID_DATABLOB = 105, +} CryptoSignature_ParamType; + +/** + * @brief Define the verify structure. + * + * @since 12 + */ +typedef struct OH_CryptoVerify OH_CryptoVerify; + +/** + * @brief Create a verify context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name for generating the verify context. Example RSA1024|PKCS1|SHA256. + * @param ctx Indicates the pointer to the verify context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoVerify_Create(const char *algoName, OH_CryptoVerify **verify); + +/** + * @brief Init verify context with given public Key. + * + * @param ctx Indicates the verify context. + * @param pubKey indicates the public Key + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoVerify_Update + * @see OH_CryptoVerify_Final + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoVerify_Init(OH_CryptoVerify *ctx, OH_CryptoPubKey *pubKey); + +/** + * @brief Used to append the message that needs to be verified. + * + * @param ctx Indicates the verify context. + * @param in Indicates the data need to be verified. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoVerify_Init + * @see OH_CryptoVerify_Final + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoVerify_Update(OH_CryptoVerify *ctx, Crypto_DataBlob *in); + +/** + * @brief Used to verify the message. + * + * @param ctx Indicates the verify context. + * @param in Indicates the data need to be verified. + * @param signData Indicates the signature data. + * @return Return result use bool value. + * @see OH_CryptoVerify_Init + * @see OH_CryptoVerify_Update + * @since 12 + */ +bool OH_CryptoVerify_Final(OH_CryptoVerify *ctx, Crypto_DataBlob *in, Crypto_DataBlob *signData); + +/** + * @brief Used to recover signed data. + * + * @param ctx Indicates the verify context. + * @param signData Indicates the signature data. + * @param rawSignData Indicates the raw sign data. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoVerify_Recover(OH_CryptoVerify *ctx, Crypto_DataBlob *signData, + Crypto_DataBlob *rawSignData); + +/** + * @brief Get the algorithm name of the verify context. + * + * @param ctx Indicates the verify context. + * @return Return verify algorithm name. + * @since 12 + */ +const char *OH_CryptoVerify_GetAlgoName(OH_CryptoVerify *ctx); + +/** + * @brief Set the specified parameter to the verify context. + * + * @param ctx Indicates the verify context. + * @param type Indicates the verify signature_paramType. + * @param value Indicates the verify result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoVerify_SetParam(OH_CryptoVerify *ctx, CryptoSignature_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Get the specified parameter from the verify context. + * + * @param ctx Indicates the verify context. + * @param type Indicates the verify signature_paramType. + * @param value Indicates the verify result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoVerify_GetParam(OH_CryptoVerify *ctx, CryptoSignature_ParamType type, + Crypto_DataBlob *value); + +/** + * @brief Destroy the verify context. + * + * @param ctx Indicates the verify context. + * @since 12 + */ +void OH_CryptoVerify_Destroy(OH_CryptoVerify *ctx); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* CRYPTO_SIGNATURE_H */ diff --git a/interfaces/kits/native/include/crypto_sym_cipher.h b/interfaces/kits/native/include/crypto_sym_cipher.h new file mode 100644 index 0000000000000000000000000000000000000000..43df73baeecdfd1b7d1cb3480f05da717b0e2be4 --- /dev/null +++ b/interfaces/kits/native/include/crypto_sym_cipher.h @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2024 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 CRYPTO_SYM_CIPHER_H +#define CRYPTO_SYM_CIPHER_H + +/** + * @addtogroup CryptoSymCipherApi + * @{ + * + * @brief Describe the functions provided by the openHarmony symmetric key encryption + * and decryption interface for applications. + * + * @since 12 + */ + +/** + * @file crypto_sym_cipher.h + * + * @brief Defines the symmetric key cipher APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + */ + +#include "crypto_common.h" +#include "crypto_sym_key.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the cipher param type. + * + * @since 12 + */ +typedef enum { + /** Indicates the parameters such as iv. */ + CRYPTO_IV_DATABLOB = 100, + /** Indicates the additional Authenticated Data in GCM mode. */ + CRYPTO_AAD_DATABLOB = 101, + /** Indicates the output tag from the encryption operation. The tag is used for integrity check. */ + CRYPTO_TAG_DATABLOB = 102, +} CryptoSymCipher_ParamsType; + +/** + * @brief Define the symmetric key cipher structure. + * + * @since 12 + */ +typedef struct OH_CryptoSymCipher OH_CryptoSymCipher; + +/** + * @brief Define the symmetric key cipher params structure. + * + * @since 12 + */ +typedef struct OH_CryptoSymCipherParams OH_CryptoSymCipherParams; + +/** + * @brief Create a symmetric key cipher params. + * + * @param params Indicates the pointer to the cipher params context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymCipherParams_Create(OH_CryptoSymCipherParams **params); + +/** + * @brief Set a parameter to the cipher params context. + * + * @param params Indicates the parameters context. + * @param paramsType Set cipher parameters. + * @param value Indicates the setParam result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymCipherParams_SetParam(OH_CryptoSymCipherParams *params, + CryptoSymCipher_ParamsType paramsType, Crypto_DataBlob *value); + +/** + * @brief Destroy the cipher params context. + * + * @param params Indicates the parameters context. + * @since 12 + */ +void OH_CryptoSymCipherParams_Destroy(OH_CryptoSymCipherParams *params); + +/** + * @brief Create a symmetric key cipher context according to the given algorithm name. + * + * @param algoName Indicates the algorithm name used to generate the symmetric key cipher context. + * Example AES128|GCM|PKCS7. + * @param ctx Indicates the pointer to the symmetric key cipher context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymCipher_Create(const char *algoName, OH_CryptoSymCipher **ctx); + +/** + * @brief Init the crypto operation with the given crypto mode, key and parameters. + * + * @param ctx Indicates the symmetric key cipher context. + * @param mod Indicates the crypto mode is encryption or decryption. + * @param key Indicates the symmetric key or the asymmetric key. + * @param params Indicates the algorithm parameters such as IV. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoSymCipher_Update + * @see OH_CryptoSymCipher_Final + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymCipher_Init(OH_CryptoSymCipher *ctx, Crypto_CipherMode mod, + OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params); + +/** + * @brief Update the crypto operation with the input data, and feed back the encrypted or decrypted data. + * + * @param ctx Indicates the symmetric key cipher context. + * @param in Indicates the data to be encrypted or decrypted. + * @param out Indicates the data to be update encrypted or decrypted. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoSymCipher_Init + * @see OH_CryptoSymCipher_Final + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymCipher_Update(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out); + +/** + * @brief Finish the crypto operation, encrypt or decrypt the input data, and then feed back the output data. + * + * @param ctx Indicates the symmetric key cipher context. + * @param in Indicates the data to be encrypted or decrypted. + * @param out Indicates the data to be finally encrypted or decrypted. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @see OH_CryptoSymCipher_Init + * @see OH_CryptoSymCipher_Update + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymCipher_Final(OH_CryptoSymCipher *ctx, Crypto_DataBlob *in, Crypto_DataBlob *out); + +/** + * @brief Get the algorithm name of the symmetric key cipher context. + * + * @param ctx Indicates the symmetric key context. + * @return Return symmetric key cipher algorithm name. + * @since 12 + */ +const char *OH_CryptoSymCipher_GetAlgoName(OH_CryptoSymCipher *ctx); + +/** + * @brief Destroy the symmetric key cipher context. + * + * @param ctx Indicates the symmetric key context. + * @since 12 + */ +void OH_CryptoSymCipher_Destroy(OH_CryptoSymCipher *ctx); + + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* CRYPTO_SYM_CIPHER_H */ diff --git a/interfaces/kits/native/include/crypto_sym_key.h b/interfaces/kits/native/include/crypto_sym_key.h new file mode 100644 index 0000000000000000000000000000000000000000..9bca2fd0fddf0de7d97ccbb156671b8132123374 --- /dev/null +++ b/interfaces/kits/native/include/crypto_sym_key.h @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2024 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 CRYPTO_SYM_KEY_H +#define CRYPTO_SYM_KEY_H + +#include "crypto_common.h" + +/** + * @addtogroup CryptoSymKeyApi + * @{ + * + * @brief Describe openHarmony symmetric key related features interfaces provide for applications. + * + * @since 12 + */ + +/** + * @file crypto_sym_key.h + * + * @brief Defines the symmetric key APIs. + * + * @library libohcrypto.so + * @kit Crypto Architecture Kit + * @syscap SystemCapability.Security.CryptoFramework + * @since 12 + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Define the symmetric key structure. + * + * @since 12 + */ +typedef struct OH_CryptoSymKey OH_CryptoSymKey; + +/** + * @brief Define the symmetric key generator structure. + * + * @since 12 + */ +typedef struct OH_CryptoSymKeyGenerator OH_CryptoSymKeyGenerator; + +/** + * @brief Create a symmetric key generator according to the given algorithm name. Example AES256. + * + * @param algoName Indicates the algorithm name for generating the generator. + * @param ctx Indicates the pointer to the symmetric key generator context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Create(const char *algoName, OH_CryptoSymKeyGenerator **ctx); + +/** + * @brief Generate a symmetric key. + * + * @param ctx Indicates the Symmetric key generator context. + * @param keyCtx Indicates the pointer to the symmetric key context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Generate(OH_CryptoSymKeyGenerator *ctx, OH_CryptoSymKey **keyCtx); + +/** + * @brief Convert the symmetric key data to a key. + * + * @param ctx Indicates the symmetric key generator context. + * @param keyData Indicates the data to generate the Symkey. + * @param keyCtx Indicates the pointer to the symmetric key context. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymKeyGenerator_Convert(OH_CryptoSymKeyGenerator *ctx, + const Crypto_DataBlob *keyData, OH_CryptoSymKey **keyCtx); + +/** + * @brief Get the algorithm name of the symmetric key generator. + * + * @param ctx Indicates the symmetric key generator context. + * @return Return symmetric key algorithm name. + * @since 12 + */ +const char *OH_CryptoSymKeyGenerator_GetAlgoName(OH_CryptoSymKeyGenerator *ctx); + +/** + * @brief Destroy the symmetric key generator. + * + * @param ctx Indicates the symmetric key generator context. + * @since 12 + */ +void OH_CryptoSymKeyGenerator_Destroy(OH_CryptoSymKeyGenerator *ctx); + +/** + * @brief Get the symmetric key algorithm name from a symmetric key. + * + * @param keyCtx Indicates the symmetric key context. + * @return Return algorithm name. + * @since 12 + */ +const char *OH_CryptoSymKey_GetAlgoName(OH_CryptoSymKey *keyCtx); + +/** + * @brief Get the symmetric key data from a symmetric key. + * + * @param keyCtx Indicates the symmetric key context. + * @param out Indicate to obtain the result. + * @return {@link OH_Crypto_ErrCode#CRYPTO_SUCCESS} 0 - If the operation is successful. + * {@link OH_Crypto_ErrCode#CRYPTO_INVALID_PARAMS} 401 - If parameter is invalid. + * {@link OH_Crypto_ErrCode#CRYPTO_NOT_SUPPORTED} 801 - If the operation is not supported. + * {@link OH_Crypto_ErrCode#CRYPTO_MEMORY_ERROR} 17620001 - If memory operation failed. + * {@link OH_Crypto_ErrCode#CRYPTO_OPERTION_ERROR} 17630001 - If crypto opertion failed. + * @since 12 + */ +OH_Crypto_ErrCode OH_CryptoSymKey_GetKeyData(OH_CryptoSymKey *keyCtx, Crypto_DataBlob *out); + +/** + * @brief Destroy the symmetric key. + * + * @param keyCtx Indicates the symmetric key context. + * @since 12 + */ +void OH_CryptoSymKey_Destroy(OH_CryptoSymKey *keyCtx); + +#ifdef __cplusplus +} +#endif + +/** @} */ +#endif /* CRYPTO_SYM_KEY_H */ diff --git a/interfaces/kits/native/include/signature.h b/interfaces/kits/native/include/signature.h deleted file mode 100644 index 7c0153312d95c01cfb0a598db3ad7b593543980d..0000000000000000000000000000000000000000 --- a/interfaces/kits/native/include/signature.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2024 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 SIGNATURE_H -#define SIGNATURE_H - -#endif \ No newline at end of file diff --git a/interfaces/kits/native/include/sym_cipher.h b/interfaces/kits/native/include/sym_cipher.h deleted file mode 100644 index e12130f36cbf1178ab248b697b541453d62e80fd..0000000000000000000000000000000000000000 --- a/interfaces/kits/native/include/sym_cipher.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (C) 2024 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 SYM_CIPHER_H -#define SYM_CIPHER_H - -#endif \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index a22b7bbd9c13cb346a6fc08303658dd814b4f5c3..9322da55e4bf682168b28f6c4fd0909852ece94c 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -27,6 +27,7 @@ ohos_unittest("crypto_framework_test") { "../../plugin/openssl_plugin/key/asy_key_generator/src", "../../plugin/openssl_plugin/crypto_operation/signature/src", "../../interfaces/innerkits/key/", + "../../interfaces/kits/native/include/" ] include_dirs += framework_inc_path + plugin_inc_path + crypto_framwork_common_inc_path @@ -118,6 +119,11 @@ ohos_unittest("crypto_framework_test") { "src/sm2/crypto_sm2_asy_key_generator_by_spec_sub_test.cpp", "src/sm2/crypto_sm2_asy_key_generator_by_spec_test.cpp", "src/sm2/crypto_sm2_util_test.cpp", + "src/native/native_asym_key_test.cpp", + "src/native/native_sym_cipher_test.cpp", + "src/native/native_sym_key_test.cpp", + "src/native/native_signature_test.cpp", + "src/native/native_digest_test.cpp" ] sources += framework_files + plugin_files @@ -147,7 +153,10 @@ ohos_unittest("crypto_framework_test") { cflags += [ "-DBINDER_IPC_32BIT" ] } - deps = [ "../../plugin:crypto_openssl_plugin_lib" ] + deps = [ + "../../plugin:crypto_openssl_plugin_lib", + "../../frameworks/native:ohcrypto", + ] defines = [ "HILOG_ENABLE", diff --git a/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp b/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp index ad1da2ff4ed7c4c8c90382a7e551c0bc9a4d0c5f..aa7e1bcf722bfa0e50d8b450d0886806e977f533 100644 --- a/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp +++ b/test/unittest/src/crypto_brainpool_no_length_verify_test.cpp @@ -31,7 +31,6 @@ #include "memory_mock.h" #include "openssl_adapter_mock.h" #include "openssl_common.h" -#include "signature.h" #include "pub_key.h" using namespace std; diff --git a/test/unittest/src/native/native_asym_key_test.cpp b/test/unittest/src/native/native_asym_key_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e4f0a6351364e427ba08dc06c0b933bad535c45 --- /dev/null +++ b/test/unittest/src/native/native_asym_key_test.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2024 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. + */ + +#include +#include "crypto_common.h" +#include "crypto_asym_key.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +static string g_testPubkeyX509Str512 = "-----BEGIN PUBLIC KEY-----\n" +"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKG0KN3tjZM8dCNfCg9bcmZM3Bhv/mRr\n" +"Mxuvua2Ru8Kr1NL+/wyeEMnIARFr+Alf1Tyfjy0PWwFnf8jHWRsz0vkCAwEAAQ==\n" +"-----END PUBLIC KEY-----\n"; + +static string g_testPubkeyPkcs1Str512 = "-----BEGIN RSA PUBLIC KEY-----\n" +"MEgCQQChtCjd7Y2TPHQjXwoPW3JmTNwYb/5kazMbr7mtkbvCq9TS/v8MnhDJyAER\n" +"a/gJX9U8n48tD1sBZ3/Ix1kbM9L5AgMBAAE=\n" +"-----END RSA PUBLIC KEY-----\n"; + +const int ECC224_PUB_KEY_LEN = 80; +const int ECC224_PRI_KEY_LEN = 44; +uint8_t g_mockEcc224PubKeyBlobData[ECC224_PUB_KEY_LEN] = { 48, 78, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, + 6, 5, 43, 129, 4, 0, 33, 3, 58, 0, 4, 252, 171, 11, 115, 79, 252, 109, 120, 46, 97, 131, 145, 207, 141, 146, + 235, 133, 37, 218, 180, 8, 149, 47, 244, 137, 238, 207, 95, 153, 65, 250, 32, 77, 184, 249, 181, 172, 192, 2, + 99, 194, 170, 25, 44, 255, 87, 246, 42, 133, 83, 66, 197, 97, 95, 12, 84 }; + +uint8_t g_mockEcc224PriKeyBlobData[ECC224_PRI_KEY_LEN] = { 48, 42, 2, 1, 1, 4, 28, 250, 86, 6, 147, 222, 43, + 252, 139, 90, 139, 5, 33, 184, 230, 26, 68, 94, 57, 145, 229, 146, 49, 221, 119, 206, 32, 198, 19, 160, 7, 6, + 5, 43, 129, 4, 0, 33 }; + +namespace { +class NativeAsymKeyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeAsymKeyTest::SetUpTestCase() {} +void NativeAsymKeyTest::TearDownTestCase() {} + +void NativeAsymKeyTest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeAsymKeyTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest001, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *generator = nullptr; + OH_CryptoKeyPair *keyCtx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("DSA2048", &generator); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeyGenerator_Generate(generator, &keyCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + const char *algoName = OH_CryptoAsymKeyGenerator_GetAlgoName(generator); + ASSERT_NE(algoName, nullptr); + + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); + + Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 }; + ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_DSA_Q_DATABLOB, &dataBlob); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(dataBlob.data, nullptr); + ASSERT_NE(dataBlob.len, 0); + HcfFree(dataBlob.data); + OH_CryptoKeyPair_Destroy(keyCtx); + OH_CryptoAsymKeyGenerator_Destroy(generator); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest002, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *generator = nullptr; + OH_Crypto_ErrCode res = OH_CryptoAsymKeyGenerator_Create("RSA512", &generator); + EXPECT_EQ(res, CRYPTO_SUCCESS); + EXPECT_NE(generator, nullptr); + + OH_CryptoKeyPair *dupKeyPair = nullptr; + Crypto_DataBlob pubKeyX509Str = {}; + pubKeyX509Str.data = reinterpret_cast(const_cast(g_testPubkeyX509Str512.c_str())); + pubKeyX509Str.len = strlen(g_testPubkeyX509Str512.c_str()); + + Crypto_DataBlob pubKeyPkcs1Str = {}; + pubKeyPkcs1Str.data = reinterpret_cast(const_cast(g_testPubkeyPkcs1Str512.c_str())); + pubKeyPkcs1Str.len = strlen(g_testPubkeyPkcs1Str512.c_str()); + res = OH_CryptoAsymKeyGenerator_Convert(generator, CRYPTO_PEM, &pubKeyX509Str, nullptr, &dupKeyPair); + EXPECT_EQ(res, CRYPTO_SUCCESS); + + OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(dupKeyPair); + Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; + res = OH_CryptoPubKey_Encode(pubkey, CRYPTO_PEM, "PKCS1", &retBlob); + EXPECT_EQ(res, CRYPTO_SUCCESS); + int32_t cmpRes = strcmp(reinterpret_cast(retBlob.data), + reinterpret_cast(pubKeyPkcs1Str.data)); + EXPECT_EQ(cmpRes, CRYPTO_SUCCESS); + + Crypto_DataBlob retBlobX509 = { .data = nullptr, .len = 0 }; + res = OH_CryptoPubKey_Encode(pubkey, CRYPTO_PEM, "X509", &retBlobX509); + EXPECT_EQ(res, CRYPTO_SUCCESS); + cmpRes = strcmp(reinterpret_cast(retBlobX509.data), reinterpret_cast(pubKeyX509Str.data)); + EXPECT_EQ(cmpRes, CRYPTO_SUCCESS); + + + OH_Crypto_FreeDataBlob(&retBlob); + OH_Crypto_FreeDataBlob(&retBlobX509); + OH_CryptoKeyPair_Destroy(dupKeyPair); + OH_CryptoAsymKeyGenerator_Destroy(generator); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest003, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *generator = nullptr; + OH_CryptoKeyPair *keyCtx = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoAsymKeyGenerator_Create("RSA768", &generator); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoAsymKeyGenerator_Generate(generator, &keyCtx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyCtx); + Crypto_DataBlob dataBlobE = { .data = nullptr, .len = 0 }; + ret = OH_CryptoPubKey_GetParam(pubKey, CRYPTO_RSA_E_DATABLOB, &dataBlobE); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(dataBlobE.data, nullptr); + ASSERT_NE(dataBlobE.len, 0); + HcfFree(dataBlobE.data); + OH_CryptoKeyPair_Destroy(keyCtx); + OH_CryptoAsymKeyGenerator_Destroy(generator); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest004, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *ctx = nullptr; + OH_CryptoKeyPair *keyPair = nullptr; + OH_Crypto_ErrCode ret; + + ret = OH_CryptoAsymKeyGenerator_Create("RSA512|PRIMES_2", &ctx); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + const char *algoName = OH_CryptoAsymKeyGenerator_GetAlgoName(ctx); + EXPECT_NE(algoName, nullptr); + + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; + ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_PEM, "PKCS1", &retBlob); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *dupKeyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_PEM, &retBlob, nullptr, &dupKeyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 }; + + ret = OH_CryptoPubKey_GetParam(pubKey1, CRYPTO_RSA_N_DATABLOB, &dataBlob); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(dataBlob.data, nullptr); + ASSERT_NE(dataBlob.len, 0); + OH_Crypto_FreeDataBlob(&dataBlob); + + OH_CryptoAsymKeyGenerator_Destroy(ctx); + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoKeyPair_Destroy(dupKeyPair); +} + +HWTEST_F(NativeAsymKeyTest, NativeAsymKeyTest005, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *ctx = nullptr; + OH_CryptoKeyPair *keyPair = nullptr; + OH_Crypto_ErrCode ret; + + ret = OH_CryptoAsymKeyGenerator_Create("Ed25519", &ctx); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoAsymKeyGenerator_Generate(ctx, &keyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + const char *algoName = OH_CryptoAsymKeyGenerator_GetAlgoName(ctx); + EXPECT_NE(algoName, nullptr); + + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + Crypto_DataBlob retBlob = { .data = nullptr, .len = 0 }; + ret = OH_CryptoPubKey_Encode(pubKey, CRYPTO_DER, nullptr, &retBlob); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoKeyPair *dupKeyPair = nullptr; + ret = OH_CryptoAsymKeyGenerator_Convert(ctx, CRYPTO_DER, &retBlob, nullptr, &dupKeyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoPubKey *pubKey1 = OH_CryptoKeyPair_GetPubKey(dupKeyPair); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 }; + + ret = OH_CryptoPubKey_GetParam(pubKey1, CRYPTO_ED25519_PK_DATABLOB, &dataBlob); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(dataBlob.data, nullptr); + ASSERT_NE(dataBlob.len, 0); + OH_Crypto_FreeDataBlob(&dataBlob); + + OH_CryptoAsymKeyGenerator_Destroy(ctx); + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoKeyPair_Destroy(dupKeyPair); +} +} \ No newline at end of file diff --git a/test/unittest/src/native/native_digest_test.cpp b/test/unittest/src/native/native_digest_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3c69cef39946591cc43f7662f11e3b2c37ebb24 --- /dev/null +++ b/test/unittest/src/native/native_digest_test.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2024 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. + */ + +#include +#include "crypto_common.h" +#include "crypto_digest.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +constexpr uint32_t SHA1_LEN = 20; + +namespace { +class NativeDigestTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeDigestTest::SetUpTestCase() {} +void NativeDigestTest::TearDownTestCase() {} + +void NativeDigestTest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeDigestTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +HWTEST_F(NativeDigestTest, NativeDigestTest001, TestSize.Level0) +{ + OH_CryptoDigest *mdObj = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoDigest_Create("SHA1", &mdObj); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + // set input and output buf + uint8_t testData[] = "My test data"; + // define input and output data in blob form + Crypto_DataBlob inBlob = {.data = reinterpret_cast(testData), .len = sizeof(testData)}; + Crypto_DataBlob outBlob = { .data = nullptr, .len = 0 }; + // test api functions + ret = OH_CryptoDigest_Update(mdObj, &inBlob); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoDigest_Final(mdObj, &outBlob); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + // destroy the API obj and blob data + OH_Crypto_FreeDataBlob(&outBlob); + OH_DigestCrypto_Destroy(mdObj); +} + +HWTEST_F(NativeDigestTest, NativeDigestTest002, TestSize.Level0) +{ + // create a API obj with SHA1 + OH_CryptoDigest *mdObj = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoDigest_Create("SHA1", &mdObj); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + // test api functions + uint32_t len = OH_CryptoDigest_GetLength(mdObj); + EXPECT_EQ(len, SHA1_LEN); + OH_DigestCrypto_Destroy(mdObj); +} + +HWTEST_F(NativeDigestTest, NativeDigestTest003, TestSize.Level0) +{ + // create a SHA1 obj + OH_CryptoDigest *mdObj = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoDigest_Create("SHA1", &mdObj); + ASSERT_EQ(ret, CRYPTO_SUCCESS); + ASSERT_NE(mdObj, nullptr); + // test api functions + const char *algoName = OH_CryptoDigest_GetAlgoName(mdObj); + int32_t cmpRes = strcmp(algoName, "SHA1"); + EXPECT_EQ(cmpRes, CRYPTO_SUCCESS); + OH_DigestCrypto_Destroy(mdObj); +} + +} \ No newline at end of file diff --git a/test/unittest/src/native/native_signature_test.cpp b/test/unittest/src/native/native_signature_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9443c127c17de119406c01acd64dab2d86f45fd7 --- /dev/null +++ b/test/unittest/src/native/native_signature_test.cpp @@ -0,0 +1,145 @@ +/* + * Copyright (C) 2024 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. + */ + +#include +#include "crypto_signature.h" +#include "crypto_common.h" +#include "crypto_asym_key.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class NativeSignatureTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeSignatureTest::SetUpTestCase() {} +void NativeSignatureTest::TearDownTestCase() {} + +void NativeSignatureTest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeSignatureTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +HWTEST_F(NativeSignatureTest, NativeSignatureTest001, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *generator = nullptr; + OH_Crypto_ErrCode res = OH_CryptoAsymKeyGenerator_Create("RSA2048|PRIMES_2", &generator); + + OH_CryptoKeyPair *keyPair = nullptr; + res = OH_CryptoAsymKeyGenerator_Generate(generator, &keyPair); + EXPECT_EQ(res, CRYPTO_SUCCESS); + + OH_CryptoPubKey *pubkey = OH_CryptoKeyPair_GetPubKey(keyPair); + OH_CryptoVerify *verify = nullptr; + res = OH_CryptoVerify_Create("RSA1024|PSS|SHA256|MGF1_SHA512", &verify); + EXPECT_EQ(res, CRYPTO_SUCCESS); + + const char *algoName = OH_CryptoVerify_GetAlgoName(verify); + ASSERT_NE(algoName, nullptr); + + int32_t buf[] = {32}; + Crypto_DataBlob value = { .data = reinterpret_cast(buf), .len = sizeof(buf) }; + res = OH_CryptoVerify_SetParam(verify, CRYPTO_PSS_SALT_LEN_INT, &value); + EXPECT_EQ(res, CRYPTO_SUCCESS); + res = OH_CryptoVerify_Init(verify, pubkey); + EXPECT_EQ(res, CRYPTO_SUCCESS); + res = OH_CryptoVerify_Update(verify, nullptr); + EXPECT_NE(res, 1); + + OH_CryptoVerify_Destroy(verify); + OH_CryptoKeyPair_Destroy(keyPair); + OH_CryptoAsymKeyGenerator_Destroy(generator); +} + +HWTEST_F(NativeSignatureTest, NativeSignatureTest002, TestSize.Level0) +{ + OH_Crypto_ErrCode res = CRYPTO_SUCCESS; + OH_CryptoVerify *verify = nullptr; + res = OH_CryptoVerify_Create("RSA512|NoPadding|NoHash|Recover", &verify); + EXPECT_EQ(res, CRYPTO_SUCCESS); + EXPECT_NE(verify, nullptr); + EXPECT_NE(OH_CryptoVerify_Destroy, nullptr); + EXPECT_NE(OH_CryptoVerify_Init, nullptr); + EXPECT_NE(OH_CryptoVerify_Update, nullptr); + EXPECT_NE(OH_CryptoVerify_Final, nullptr); + EXPECT_NE(OH_CryptoVerify_Recover, nullptr); + OH_CryptoVerify_Destroy(verify); +} + +HWTEST_F(NativeSignatureTest, NativeSignatureTest003, TestSize.Level0) +{ + OH_CryptoAsymKeyGenerator *keyCtx = nullptr; + OH_CryptoKeyPair *keyPair = nullptr; + OH_CryptoVerify *verify = nullptr; + + uint8_t plainText[] = { + 0xe4, 0x2b, 0xcc, 0x08, 0x11, 0x79, 0x16, 0x1b, 0x35, 0x7f, 0xb3, 0xaf, 0x40, 0x3b, 0x3f, 0x7c + }; + Crypto_DataBlob msgBlob = { + .data = reinterpret_cast(plainText), + .len = sizeof(plainText) + }; + + uint8_t pubKeyText[] = { + 0x30, 0x39, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, + 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x22, 0x00, 0x03, 0x4d, 0xe4, 0xbb, 0x11, 0x10, + 0x1a, 0xd2, 0x05, 0x74, 0xf1, 0x0b, 0xb4, 0x75, 0x57, 0xf4, 0x3e, 0x55, 0x14, 0x17, 0x05, 0x4a, + 0xb2, 0xfb, 0x8c, 0x84, 0x64, 0x38, 0x02, 0xa0, 0x2a, 0xa6, 0xf0 + }; + + Crypto_DataBlob keyBlob = { + .data = reinterpret_cast(pubKeyText), + .len = sizeof(pubKeyText) + }; + + uint8_t signText[] = { + 0x30, 0x44, 0x02, 0x20, 0x21, 0x89, 0x99, 0xb1, 0x56, 0x4e, 0x3a, 0x2c, 0x16, 0x08, 0xb5, 0x8a, + 0x06, 0x6f, 0x67, 0x47, 0x1b, 0x04, 0x18, 0x7d, 0x53, 0x2d, 0xba, 0x00, 0x38, 0xd9, 0xe3, 0xe7, + 0x8c, 0xcf, 0x76, 0x83, 0x02, 0x20, 0x13, 0x54, 0x84, 0x9d, 0x73, 0x40, 0xc3, 0x92, 0x66, 0xdc, + 0x3e, 0xc9, 0xf1, 0x4c, 0x33, 0x84, 0x2a, 0x76, 0xaf, 0xc6, 0x61, 0x84, 0x5c, 0xae, 0x4b, 0x0d, + 0x3c, 0xb0, 0xc8, 0x04, 0x89, 0x71 + }; + + Crypto_DataBlob signBlob = { + .data = reinterpret_cast(signText), + .len = sizeof(signText) + }; + + // keypair + ASSERT_EQ(OH_CryptoAsymKeyGenerator_Create((const char *)"ECC256", &keyCtx), CRYPTO_SUCCESS); + ASSERT_EQ(OH_CryptoAsymKeyGenerator_Convert(keyCtx, CRYPTO_DER, &keyBlob, nullptr, &keyPair), CRYPTO_SUCCESS); + OH_CryptoPubKey *pubKey = OH_CryptoKeyPair_GetPubKey(keyPair); + // verify + ASSERT_EQ(OH_CryptoVerify_Create((const char *)"ECC|SHA256", &verify), CRYPTO_SUCCESS); + ASSERT_EQ(OH_CryptoVerify_Init(verify, pubKey), CRYPTO_SUCCESS); + ASSERT_TRUE(OH_CryptoVerify_Final(verify, &msgBlob, &signBlob)); + + OH_CryptoVerify_Destroy(verify); + OH_CryptoAsymKeyGenerator_Destroy(keyCtx); + OH_CryptoKeyPair_Destroy(keyPair); +} +} \ No newline at end of file diff --git a/test/unittest/src/native/native_sym_cipher_test.cpp b/test/unittest/src/native/native_sym_cipher_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..607652bf1c91d132bd103cfc82ab1d2bcf034f7c --- /dev/null +++ b/test/unittest/src/native/native_sym_cipher_test.cpp @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2024 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. + */ + +#include +#include +#include +#include "crypto_common.h" +#include "crypto_sym_cipher.h" +#include "sym_key.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" +#include "securec.h" + +using namespace std; +using namespace testing::ext; + +static constexpr int32_t GCM_TAG_LEN = 16; + +namespace { +class NativeSymCipherTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeSymCipherTest::SetUpTestCase() {} +void NativeSymCipherTest::TearDownTestCase() {} + +void NativeSymCipherTest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeSymCipherTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +OH_Crypto_ErrCode AesEncrypt(OH_CryptoSymCipher *cipher, OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params, + uint8_t *cipherText, int *cipherTextLen) +{ + uint8_t plainText[] = "this is test!"; + Crypto_DataBlob input = {.data = reinterpret_cast(plainText), .len = 13}; + Crypto_DataBlob output = {}; + int32_t maxLen = *cipherTextLen; + OH_Crypto_ErrCode ret = OH_CryptoSymCipher_Init(cipher, CRYPTO_ENCRYPT_MODE, key, params); + if (ret != CRYPTO_SUCCESS) { + LOGE("init failed! %d", ret); + return ret; + } + + ret = OH_CryptoSymCipher_Update(cipher, &input, &output); + if (ret != CRYPTO_SUCCESS) { + LOGE("update failed!"); + return ret; + } + *cipherTextLen = output.len; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + OH_Crypto_FreeDataBlob(&output); + return CRYPTO_INVALID_PARAMS; + } + OH_Crypto_FreeDataBlob(&output); + } + + ret = OH_CryptoSymCipher_Final(cipher, nullptr, &output); + if (ret != CRYPTO_SUCCESS) { + LOGE("doFinal failed!"); + return ret; + } + if (output.data != nullptr) { + if (memcpy_s(cipherText + *cipherTextLen, maxLen - *cipherTextLen, output.data, output.len) != EOK) { + OH_Crypto_FreeDataBlob(&output); + return CRYPTO_INVALID_PARAMS; + } + *cipherTextLen += output.len; + OH_Crypto_FreeDataBlob(&output); + } + + return CRYPTO_SUCCESS; +} + +OH_Crypto_ErrCode AesDecrypt(OH_CryptoSymCipher *cipher, OH_CryptoSymKey *key, OH_CryptoSymCipherParams *params, + uint8_t *cipherText, int cipherTextLen) +{ + uint8_t plainText[] = "this is test!"; + Crypto_DataBlob input = {.data = reinterpret_cast(cipherText), .len = cipherTextLen}; + Crypto_DataBlob output = {}; + int32_t maxLen = cipherTextLen; + OH_Crypto_ErrCode ret = OH_CryptoSymCipher_Init(cipher, CRYPTO_DECRYPT_MODE, key, params); + if (ret != CRYPTO_SUCCESS) { + LOGE("init failed! %d", ret); + return ret; + } + + ret = OH_CryptoSymCipher_Update(cipher, &input, &output); + if (ret != CRYPTO_SUCCESS) { + LOGE("update failed!"); + return ret; + } + cipherTextLen = output.len; + if (output.data != nullptr) { + if (memcpy_s(cipherText, maxLen, output.data, output.len) != EOK) { + OH_Crypto_FreeDataBlob(&output); + return CRYPTO_INVALID_PARAMS; + } + OH_Crypto_FreeDataBlob(&output); + } + + ret = OH_CryptoSymCipher_Final(cipher, nullptr, &output); + if (ret != CRYPTO_SUCCESS) { + LOGE("doFinal failed!"); + return ret; + } + if (output.data != nullptr) { + if (memcpy_s(cipherText + cipherTextLen, maxLen - cipherTextLen, output.data, output.len) != EOK) { + OH_Crypto_FreeDataBlob(&output); + return CRYPTO_INVALID_PARAMS; + } + cipherTextLen += output.len; + OH_Crypto_FreeDataBlob(&output); + } + + if (cipherTextLen != sizeof(plainText) - 1) { + return CRYPTO_INVALID_PARAMS; + } + if (memcmp(cipherText, plainText, cipherTextLen) != 0) { + return CRYPTO_INVALID_PARAMS; + } + return CRYPTO_SUCCESS; +} + +HWTEST_F(NativeSymCipherTest, NativeSymCipherTest001, TestSize.Level0) +{ + uint8_t aad[8] = {0}; + uint8_t tag[16] = {0}; + uint8_t iv[12] = {0}; + uint8_t cipherText[128] = {0}; + int cipherTextLen = 128; + Crypto_DataBlob ivData = { .data = iv, .len = sizeof(iv) }; + Crypto_DataBlob aadData = { .data = aad, .len = sizeof(aad) }; + Crypto_DataBlob tagData = { .data = tag, .len = sizeof(tag) }; + OH_CryptoSymCipherParams *params = nullptr; + OH_Crypto_ErrCode ret = OH_CryptoSymCipherParams_Create(¶ms); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_IV_DATABLOB, &ivData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_AAD_DATABLOB, &aadData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoSymKeyGenerator *ctx = nullptr; + OH_CryptoSymKey *symKey = nullptr; + ret = OH_CryptoSymKeyGenerator_Create("AES128", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSymKeyGenerator_Generate(ctx, &symKey); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoSymCipher *cipher = nullptr; + ret = OH_CryptoSymCipher_Create("AES128|GCM|PKCS7", &cipher); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + ret = AesEncrypt(cipher, symKey, params, cipherText, &cipherTextLen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + (void)memcpy_s(tagData.data, GCM_TAG_LEN, cipherText + cipherTextLen - GCM_TAG_LEN, GCM_TAG_LEN); + cipherTextLen -= GCM_TAG_LEN; + ret = OH_CryptoSymCipherParams_SetParam(params, CRYPTO_TAG_DATABLOB, &tagData); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = AesDecrypt(cipher, symKey, params, cipherText, cipherTextLen); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + + OH_CryptoSymCipherParams_Destroy(params); + OH_CryptoSymCipher_Destroy(cipher); + OH_CryptoSymKey_Destroy(symKey); + OH_CryptoSymKeyGenerator_Destroy(ctx); +} +} \ No newline at end of file diff --git a/test/unittest/src/native/native_sym_key_test.cpp b/test/unittest/src/native/native_sym_key_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79bc8619ecfad0f290ec7d49bfb25d9094c76e36 --- /dev/null +++ b/test/unittest/src/native/native_sym_key_test.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2024 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. + */ + +#include +#include "crypto_common.h" +#include "crypto_sym_key.h" +#include "log.h" +#include "memory.h" +#include "memory_mock.h" + +using namespace std; +using namespace testing::ext; + +namespace { +class NativeSymKeyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void NativeSymKeyTest::SetUpTestCase() {} +void NativeSymKeyTest::TearDownTestCase() {} + +void NativeSymKeyTest::SetUp() // add init here, this will be called before test. +{ +} + +void NativeSymKeyTest::TearDown() // add destroy here, this will be called when test case done. +{ +} + +HWTEST_F(NativeSymKeyTest, NativeSymKeyTest001, TestSize.Level0) +{ + OH_CryptoSymKeyGenerator *ctx = nullptr; + OH_CryptoSymKey *convertKey = nullptr; + uint8_t testKey[] = "abcdefghijklmnop"; + uint32_t testKeyLen = 16; + Crypto_DataBlob keyMaterialBlob = {.data = reinterpret_cast(testKey), .len = testKeyLen}; + + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSymKeyGenerator_Convert(ctx, &keyMaterialBlob, &convertKey); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + const char *algoName = OH_CryptoSymKeyGenerator_GetAlgoName(ctx); + ASSERT_NE(algoName, nullptr); + + OH_CryptoSymKey_Destroy(convertKey); + OH_CryptoSymKeyGenerator_Destroy(ctx); +} + +HWTEST_F(NativeSymKeyTest, NativeSymKeyTest002, TestSize.Level0) +{ + OH_CryptoSymKeyGenerator *ctx = nullptr; + OH_CryptoSymKey *symKey = nullptr; + + OH_Crypto_ErrCode ret = OH_CryptoSymKeyGenerator_Create("AES128", &ctx); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + ret = OH_CryptoSymKeyGenerator_Generate(ctx, &symKey); + EXPECT_EQ(ret, CRYPTO_SUCCESS); + Crypto_DataBlob dataBlob = { .data = nullptr, .len = 0 }; + ret = OH_CryptoSymKey_GetKeyData(symKey, &dataBlob); + + EXPECT_EQ(ret, CRYPTO_SUCCESS); + const char *algoName = OH_CryptoSymKeyGenerator_GetAlgoName(ctx); + ASSERT_NE(algoName, nullptr); + + OH_CryptoSymKey_Destroy(symKey); + OH_CryptoSymKeyGenerator_Destroy(ctx); +} +} \ No newline at end of file