code
stringlengths
1
1.05M
repo_name
stringlengths
6
83
path
stringlengths
3
242
language
stringclasses
222 values
license
stringclasses
20 values
size
int64
1
1.05M
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP #include <stdlib.h> #include <stddef.h> #include <string.h> #include "crypt_cmvp.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_eal_implprovider.h" #include "crypt_eal_cmvp.h" #ifdef HITLS_CRYPTO_PROVIDER static int32_t CRYPT_EAL_SetCmvpSelftestMethod(CRYPT_SelftestCtx *ctx, const CRYPT_EAL_Func *funcs) { int32_t index = 0; EAL_CmvpSelftestMethod *method = BSL_SAL_Calloc(1, sizeof(EAL_CmvpSelftestMethod)); if (method == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } while (funcs[index].id != 0) { switch (funcs[index].id) { case CRYPT_EAL_IMPLSELFTEST_NEWCTX: method->provNewCtx = funcs[index].func; break; case CRYPT_EAL_IMPLSELFTEST_GETVERSION: method->getVersion = funcs[index].func; break; case CRYPT_EAL_IMPLSELFTEST_SELFTEST: method->selftest = funcs[index].func; break; case CRYPT_EAL_IMPLSELFTEST_FREECTX: method->freeCtx = funcs[index].func; break; default: BSL_SAL_FREE(method); BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_ERR_UNEXPECTED_IMPL); return CRYPT_PROVIDER_ERR_UNEXPECTED_IMPL; } index++; } ctx->method = method; return CRYPT_SUCCESS; } static CRYPT_SelftestCtx *CRYPT_CMVP_SelftestNewCtxInner(CRYPT_EAL_LibCtx *libCtx, const char *attrName) { const CRYPT_EAL_Func *funcs = NULL; void *provCtx = NULL; int32_t algId = CRYPT_CMVP_PROVIDER_SELFTEST; int32_t ret = CRYPT_EAL_ProviderGetFuncs(libCtx, CRYPT_EAL_OPERAID_SELFTEST, algId, attrName, &funcs, &provCtx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } CRYPT_SelftestCtx *ctx = BSL_SAL_Calloc(1u, sizeof(CRYPT_SelftestCtx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } ret = CRYPT_EAL_SetCmvpSelftestMethod(ctx, funcs); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_FREE(ctx); return NULL; } if (ctx->method->provNewCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_ERR_IMPL_NULL); BSL_SAL_FREE(ctx->method); BSL_SAL_FREE(ctx); return NULL; } ctx->data = ctx->method->provNewCtx(provCtx); if (ctx->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_FREE(ctx->method); BSL_SAL_FREE(ctx); return NULL; } ctx->id = algId; ctx->isProvider = true; return ctx; } #endif // HITLS_CRYPTO_PROVIDER CRYPT_SelftestCtx *CRYPT_CMVP_SelftestNewCtx(CRYPT_EAL_LibCtx *libCtx, const char *attrName) { #ifdef HITLS_CRYPTO_PROVIDER return CRYPT_CMVP_SelftestNewCtxInner(libCtx, attrName); #else (void)libCtx; (void)attrName; return NULL; #endif } const char *CRYPT_CMVP_GetVersion(CRYPT_SelftestCtx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } if (ctx->method == NULL || ctx->method->getVersion == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_NOT_SUPPORT); return NULL; } return ctx->method->getVersion(ctx->data); } int32_t CRYPT_CMVP_Selftest(CRYPT_SelftestCtx *ctx, const BSL_Param *param) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->method == NULL || ctx->method->selftest == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_NOT_SUPPORT); return CRYPT_EAL_ALG_NOT_SUPPORT; } return ctx->method->selftest(ctx->data, param); } void CRYPT_CMVP_SelftestFreeCtx(CRYPT_SelftestCtx *ctx) { if (ctx == NULL) { return; } if (ctx->method != NULL && ctx->method->freeCtx != NULL) { ctx->method->freeCtx(ctx->data); } BSL_SAL_FREE(ctx->method); BSL_SAL_FREE(ctx); } #endif /* HITLS_CRYPTO_CMVP */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp.c
C
unknown
4,640
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #define _GNU_SOURCE #include <stdio.h> #include <string.h> #include <dlfcn.h> #include <syslog.h> #include <stdarg.h> #include "securec.h" #include "crypt_cmvp.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_err.h" #include "crypt_errno.h" #include "bsl_sal.h" #include "crypt_cmvp_selftest.h" #include "cmvp_integrity_hmac.h" #include "cmvp_common.h" #define BSL_PARAM_MAX_NUMBER 1000 uint8_t *CMVP_StringsToBins(const char *in, uint32_t *outLen) { if (in == NULL) { return NULL; } uint32_t inLen = (uint32_t)strlen(in); uint8_t *out = NULL; if (inLen == 0) { return NULL; } // The length of a hexadecimal string must be a multiple of 2. if (inLen % 2 != 0) { return NULL; } // Length of the hexadecimal string / 2 = Length of the byte stream inLen = inLen / 2; out = BSL_SAL_Malloc(inLen); if (out == NULL) { return NULL; } *outLen = inLen; // A group of 2 bytes for (uint32_t i = 0; i < 2 * inLen; i += 2) { // Formula for converting hex to int: (Hex% 32 + 9)% 25 = int, hexadecimal, 16: high 4 bits. out[i / 2] = ((uint8_t)in[i] % 32 + 9) % 25 * 16 + ((uint8_t)in[i + 1] % 32 + 9) % 25; } return out; } void CMVP_WriteSyslog(const char *ident, int32_t priority, const char *format, ...) { va_list vargs; va_start(vargs, format); openlog(ident, LOG_PID | LOG_ODELAY, LOG_USER); vsyslog(priority, format, vargs); closelog(); va_end(vargs); } char *CMVP_ReadFile(const char *path, const char *mode, uint32_t *bufLen) { int64_t len; int64_t readLen; FILE *fp = NULL; char *buf = NULL; fp = fopen(path, mode); if (fp == NULL) { return false; } GOTO_ERR_IF_TRUE(fseek(fp, 0, SEEK_END) != 0, CRYPT_CMVP_COMMON_ERR); len = ftell(fp); GOTO_ERR_IF_TRUE(len == -1, CRYPT_CMVP_COMMON_ERR); buf = BSL_SAL_Malloc((uint32_t)len + 1); GOTO_ERR_IF_TRUE(buf == NULL, CRYPT_MEM_ALLOC_FAIL); buf[len] = '\0'; GOTO_ERR_IF_TRUE(fseek(fp, 0, SEEK_SET) != 0, CRYPT_CMVP_COMMON_ERR); readLen = (int64_t)fread(buf, sizeof(uint8_t), (uint64_t)len, fp); GOTO_ERR_IF_TRUE(readLen != len && feof(fp) == 0, CRYPT_CMVP_COMMON_ERR); *bufLen = (uint32_t)readLen; (void)fclose(fp); return buf; ERR: BSL_SAL_Free(buf); (void)fclose(fp); return NULL; } static char *CMVP_GetLibPath(void *func) { Dl_info info; char *path = NULL; GOTO_ERR_IF_TRUE(dladdr(func, &info) == 0, CRYPT_CMVP_COMMON_ERR); path = BSL_SAL_Malloc((uint32_t)strlen(info.dli_fname) + 1); GOTO_ERR_IF_TRUE(path == NULL, CRYPT_MEM_ALLOC_FAIL); (void)memcpy_s(path, strlen(info.dli_fname), info.dli_fname, strlen(info.dli_fname)); path[strlen(info.dli_fname)] = '\0'; return path; ERR: BSL_SAL_Free(path); return NULL; } int32_t CMVP_CheckIntegrity(void *libCtx, const char *attrName, CRYPT_MAC_AlgId macId) { int32_t ret = CRYPT_CMVP_ERR_INTEGRITY; char *libCryptoPath = NULL; char *libBslPath = NULL; if (CRYPT_CMVP_SelftestProviderMac(libCtx, attrName, macId) != true) { return CRYPT_CMVP_ERR_ALGO_SELFTEST; } libCryptoPath = CMVP_GetLibPath(CMVP_IntegrityHmac); GOTO_ERR_IF_TRUE(libCryptoPath == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CMVP_IntegrityHmac(libCtx, attrName, libCryptoPath, macId) == false, CRYPT_CMVP_ERR_INTEGRITY); libBslPath = CMVP_GetLibPath(BSL_SAL_Malloc); GOTO_ERR_IF_TRUE(libBslPath == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CMVP_IntegrityHmac(libCtx, attrName, libBslPath, macId) == false, CRYPT_CMVP_ERR_INTEGRITY); ret = CRYPT_SUCCESS; ERR: BSL_SAL_Free(libCryptoPath); BSL_SAL_Free(libBslPath); return ret; } static int32_t CopyParam(BSL_Param *param, int32_t *selfTestFlag, BSL_Param **newParam) { int32_t index = 0; if (param != NULL) { while (param[index].key != 0 && index < BSL_PARAM_MAX_NUMBER) { index++; } if (index >= BSL_PARAM_MAX_NUMBER) { BSL_ERR_PUSH_ERROR(CRYPT_CMVP_COMMON_ERR); return CRYPT_CMVP_COMMON_ERR; } } int32_t count = index + 2; BSL_Param *tmpParam = (BSL_Param *)BSL_SAL_Calloc(count, sizeof(BSL_Param)); if (tmpParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (param != NULL) { (void)memcpy_s(tmpParam, count * sizeof(BSL_Param), param, index * sizeof(BSL_Param)); } int32_t ret = BSL_PARAM_InitValue(&tmpParam[index], CRYPT_PARAM_CMVP_INTERNAL_LIBCTX_FLAG, BSL_PARAM_TYPE_INT32, selfTestFlag, sizeof(int32_t)); if (ret != BSL_SUCCESS) { BSL_SAL_FREE(tmpParam); return ret; } *newParam = tmpParam; return CRYPT_SUCCESS; } int32_t CMVP_CreateInternalLibCtx(BSL_Param *param, CRYPT_EAL_LibCtx **libCtx, void *func) { int32_t selfTestFlag = 1; int32_t ret = CRYPT_SUCCESS; char *libPath = NULL; BSL_Param *newParam = NULL; CRYPT_EAL_LibCtx *ctx = NULL; do { ret = CopyParam(param, &selfTestFlag, &newParam); if (ret != CRYPT_SUCCESS) { break; } ctx = CRYPT_EAL_LibCtxNew(); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; break; } libPath = CMVP_GetLibPath(func); if (libPath == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_CMVP_COMMON_ERR); ret = CRYPT_CMVP_COMMON_ERR; break; } ret = CRYPT_EAL_ProviderLoad(ctx, 0, libPath, newParam, NULL); if (ret != CRYPT_SUCCESS) { break; } *libCtx = ctx; } while (0); if (ret != CRYPT_SUCCESS) { CRYPT_EAL_LibCtxFree(ctx); } BSL_SAL_Free(libPath); BSL_SAL_Free(newParam); return ret; } bool CMVP_CheckIsInternalLibCtx(BSL_Param *param) { if (param == NULL) { return false; } BSL_Param *temp = BSL_PARAM_FindParam(param, CRYPT_PARAM_CMVP_INTERNAL_LIBCTX_FLAG); if (temp != NULL && temp->valueType == BSL_PARAM_TYPE_INT32) { return true; } return false; } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_common.c
C
unknown
7,001
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CMVP_COMMON_H #define CMVP_COMMON_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <stdint.h> #include <stdbool.h> #include <syslog.h> #include "crypt_cmvp.h" #include "crypt_types.h" #include "crypt_algid.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus uint8_t *CMVP_StringsToBins(const char *in, uint32_t *outLen); // Converting a hexadecimal string to a buf array void CMVP_WriteSyslog(const char *ident, int32_t priority, const char *format, ...) __attribute__((format(printf, 3, 4))); // Write syslog char *CMVP_ReadFile(const char *path, const char *mode, uint32_t *bufLen); // Read file int32_t CMVP_CheckIntegrity(void *libCtx, const char *attrName, CRYPT_MAC_AlgId macId); int32_t CMVP_CreateInternalLibCtx(BSL_Param *param, CRYPT_EAL_LibCtx **libCtx, void *func); bool CMVP_CheckIsInternalLibCtx(BSL_Param *param); #ifdef __cplusplus } #endif // __cplusplus #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */ #endif // CMVP_COMMON_H
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_common.h
C
unknown
1,652
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "cmvp_common.h" #include "crypt_eal_mac.h" #include "crypt_errno.h" #include "bsl_err.h" #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" #include "cmvp_integrity_hmac.h" #ifndef CMVP_INTEGRITYKEY #define CMVP_INTEGRITYKEY "" #endif const char *GetIntegrityKey(void) { return CMVP_INTEGRITYKEY; } static uint8_t *GetLibHmac(void *libCtx, const char *attrName, CRYPT_MAC_AlgId id, const char *libPath, uint32_t *hmacLen) { char *buf = NULL; uint8_t *hmac = NULL; uint32_t bufLen; CRYPT_EAL_MacCtx *ctx = NULL; buf = CMVP_ReadFile(libPath, "rb", &bufLen); GOTO_ERR_IF_TRUE(buf == NULL, CRYPT_CMVP_COMMON_ERR); ctx = CRYPT_EAL_ProviderMacNewCtx(libCtx, id, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); *hmacLen = CRYPT_EAL_GetMacLen(ctx); hmac = BSL_SAL_Malloc(*hmacLen); GOTO_ERR_IF_TRUE(hmac == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_MacInit(ctx, (const uint8_t *)GetIntegrityKey(), (uint32_t)strlen(GetIntegrityKey())) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_INTEGRITY); GOTO_ERR_IF_TRUE(CRYPT_EAL_MacUpdate(ctx, (uint8_t *)buf, bufLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_INTEGRITY); GOTO_ERR_IF_TRUE(CRYPT_EAL_MacFinal(ctx, hmac, hmacLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_INTEGRITY); BSL_SAL_Free(buf); CRYPT_EAL_MacFreeCtx(ctx); return hmac; ERR: BSL_SAL_Free(buf); BSL_SAL_Free(hmac); CRYPT_EAL_MacFreeCtx(ctx); return NULL; } static uint8_t *GetExpectHmac(const char *hmacPath, uint32_t *hmacLen) { uint8_t *hmac = NULL; char *buf = NULL; uint32_t bufLen; char seps[] = " \n"; char *tmp = NULL; char *nextTmp = NULL; buf = CMVP_ReadFile(hmacPath, "r", &bufLen); GOTO_ERR_IF_TRUE(buf == NULL, CRYPT_CMVP_COMMON_ERR); // HMAC-SHA256(libhitls_crypto.so)= 76a90d73cb68585837a2ebdf009e9e485acba4fd718bae898bdc354537f8a72a\n // The format of the generated .hmac file is as shown in the preceding figure. // The content between spaces and newline characters is truncated. tmp = strtok_s(buf, seps, &nextTmp); GOTO_ERR_IF_TRUE(tmp == NULL, CRYPT_CMVP_COMMON_ERR); tmp = strtok_s(NULL, seps, &nextTmp); GOTO_ERR_IF_TRUE(tmp == NULL, CRYPT_CMVP_COMMON_ERR); hmac = CMVP_StringsToBins(tmp, hmacLen); GOTO_ERR_IF_TRUE(hmac == NULL, CRYPT_CMVP_COMMON_ERR); BSL_SAL_Free(buf); return hmac; ERR: BSL_SAL_Free(buf); BSL_SAL_Free(hmac); return NULL; } bool CMVP_IntegrityHmac(void *libCtx, const char *attrName, const char *libPath, CRYPT_MAC_AlgId id) { bool ret = false; char *hmacPath = NULL; uint8_t *hmac = NULL; uint8_t *expectHmac = NULL; uint32_t hmacLen, expectHmacLen; hmacPath = BSL_SAL_Malloc((uint32_t)strlen(libPath) + (uint32_t)strlen(".hmac") + 1); GOTO_ERR_IF_TRUE(hmacPath == NULL, CRYPT_MEM_ALLOC_FAIL); (void)sprintf_s(hmacPath, strlen(libPath) + strlen(".hmac") + 1, "%s%s", libPath, ".hmac"); hmacPath[strlen(libPath) + strlen(".hmac")] = '\0'; hmac = GetLibHmac(libCtx, attrName, id, libPath, &hmacLen); GOTO_ERR_IF_TRUE(hmac == NULL, CRYPT_CMVP_ERR_INTEGRITY); expectHmac = GetExpectHmac(hmacPath, &expectHmacLen); GOTO_ERR_IF_TRUE(expectHmac == NULL, CRYPT_CMVP_ERR_INTEGRITY); GOTO_ERR_IF_TRUE(hmacLen != expectHmacLen, CRYPT_CMVP_ERR_INTEGRITY); GOTO_ERR_IF_TRUE(memcmp(expectHmac, hmac, hmacLen) != 0, CRYPT_CMVP_ERR_INTEGRITY); ret = true; ERR: BSL_SAL_Free(hmac); BSL_SAL_Free(expectHmac); BSL_SAL_Free(hmacPath); return ret; } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_integrity_hmac.c
C
unknown
4,406
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CMVP_INTEGRITY_HMAC_H #define CMVP_INTEGRITY_HMAC_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <stdbool.h> #include "crypt_algid.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus // When the HMAC is used to perform integrity verification, a failure message is returned, // and the module does not enter the error state. bool CMVP_IntegrityHmac(void *libCtx, const char *attrName, const char *libPath, CRYPT_MAC_AlgId id); #ifdef __cplusplus } #endif // __cplusplus #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */ #endif /* CMVP_INTEGRITY_HMAC_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_integrity_hmac.h
C
unknown
1,253
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <math.h> #include <float.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_log_internal.h" #include "bsl_binlog_id.h" #include "crypt_errno.h" #include "crypt_cmvp.h" #define ALPHA (0.01) #define MAXITERTIMES 1e5 #define BITSPERBYTE 8 static uint8_t *Byte2Bits(const uint8_t *data, uint32_t len) { if (data == NULL || len == 0) { return NULL; } uint8_t *bits = BSL_SAL_Malloc(sizeof(uint8_t) * (len * BITSPERBYTE)); if (bits == NULL) { return NULL; } for (uint32_t i = 0; i < len; i++) { uint32_t j = i * 8; bits[j + 7] = data[i] & 0x01; // bit0 offset 7 bits[j + 6] = (data[i] >> 1) & 0x01; // bit1 offset 6 bits[j + 5] = (data[i] >> 2) & 0x01; // bit2 offset 5 bits[j + 4] = (data[i] >> 3) & 0x01; // bit3 offset 4 bits[j + 3] = (data[i] >> 4) & 0x01; // bit4 offset 3 bits[j + 2] = (data[i] >> 5) & 0x01; // bit5 offset 2 bits[j + 1] = (data[i] >> 6) & 0x01; // bit6 offset 1 bits[j] = (data[i] >> 7) & 0x01; // bit7 offset 0 } return bits; } static double IgammaFraction(double a, double x); static double IgammaSeries(double a, double x); // Upper Incomplete Gamma Function static double Igamc(double a, double x) { if (a <= 0 || x <= 0) { return 1.0; } if (x < a + 1.0) { return 1.0 - IgammaSeries(a, x); } else { return IgammaFraction(a, x); } } // Evaluate upper igamma by continued fraction // use Lentz's algorithm to calculate the continued fraction // where ak = k(a - k), bk = (x - a + 2k + 1) // define // C_n = b_n + a_n / C_(n-1) // D_n = 1 / (b_n + a_n * D_(n - 1)) // then f_n = C_n * D_n * f_n-1 converges to Igamc // see https://en.wikipedia.org/wiki/Lentz%27s_algorithm for detailed information. static double IgammaFraction(double a, double x) { double an, bn = x + 1.0 - a; double factor = a * log(x) - x - lgamma(a); if (factor >= DBL_MAX_EXP) { return 1.0; // float underflow } factor = exp(factor); double c = 1 / DBL_MIN; double d = 1.0 / bn; double prod = d; for (uint32_t k = 1; k < MAXITERTIMES; k++) { an = ((double)k) * (a - (double)k); // ak = k(a - k) bn += 2.0; // bk = (x - a + 2.0 * k + 1) c = bn + an / c; d = bn + an * d; if (fabs(c) < DBL_MIN) { break; // float underflow } if (fabs(d) < DBL_MIN) { break; // float underflow } d = 1 / d; prod *= (d * c); } return factor * prod; } // Evaluate lower incomplete gamma function by series representation static double IgammaSeries(double a, double x) { double sum = 0, bn = 1, factor, ak = a; factor = a * log(x) - x - lgamma(a); if (factor >= DBL_MAX_EXP) { return 0.0; // float underflow } factor = exp(factor); for (uint32_t k = 1; k < MAXITERTIMES; k++) { sum += bn; ak += 1; bn *= x / ak; } return (sum / a) * factor; } static uint8_t DataToIndex(const uint8_t* data, const int32_t blocklen) { uint8_t s = 0; for (int32_t i = 0; i < blocklen; i++) { s += (data[i] << (blocklen - i - 1)); } return s; } static int32_t PokerTest(const uint8_t *data, uint32_t len, int32_t blocklen) { uint32_t N = len / blocklen; uint32_t maxComb = (uint32_t)pow(2.0, (double)blocklen); uint32_t *dict = BSL_SAL_Malloc(maxComb * sizeof(uint32_t)); if (dict == NULL) { return CRYPT_CMVP_RANDOMNESS_ERR; } memset_s(dict, maxComb * sizeof(uint32_t), 0, maxComb * sizeof(uint32_t)); for (uint32_t i = 0; (uint32_t)(i + blocklen) <= len; i += (uint32_t)blocklen) { dict[DataToIndex(data + i, blocklen)]++; } double s = 0.0; for (uint32_t i = 0; i < maxComb; i++) { s += pow(dict[i], 2); // 2: square each dict value } double v = (pow(2.0, (double)blocklen) / (double) N) * s - N; double pValue = Igamc(((double)maxComb - 1.0) / 2.0, v / 2.0); // p_value = igamc((2^m - 1) / 2, v / 2) BSL_SAL_FREE(dict); return pValue >= ALPHA ? CRYPT_SUCCESS : CRYPT_CMVP_RANDOMNESS_ERR; } int32_t CRYPT_CMVP_RandomnessTest(const uint8_t *data, const uint32_t len) { int32_t ret = CRYPT_SUCCESS; if (len > UINT32_MAX / BITSPERBYTE) { return CRYPT_CMVP_RANDOMNESS_ERR; } uint8_t *bits = Byte2Bits(data, len); if (bits == NULL) { return CRYPT_CMVP_RANDOMNESS_ERR; } // GM/T 0062-2018 Table 8: perform Poker test with parameter m = 2 ret = PokerTest(bits, len * BITSPERBYTE, 2); BSL_SAL_FREE(bits); return ret; } #endif /* HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_randomness.c
C
unknown
5,359
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_cipher.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { const char *key; const char *iv; const char *aad; const char *plaintext; const char *ciphertext; const char *tag; } CMVP_CHACHA20POLY1305_VECTOR; // https://datatracker.ietf.org/doc/html/rfc7539.html#page-22 static const CMVP_CHACHA20POLY1305_VECTOR CHACHA20POLY1305_VECTOR = { .key = "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f", .iv = "070000004041424344454647", .aad = "50515253c0c1c2c3c4c5c6c7", .plaintext = "4c616469657320616e642047656e746c656d656e206f662074686520636c617373206f6620273939" "3a204966204920636f756c64206f6666657220796f75206f6e6c79206f6e652074697020666f7220" "746865206675747572652c2073756e73637265656e20776f756c642062652069742e", .ciphertext = "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca96712" "82fafb69da92728b1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58" "fab324e4fad675945585808b4831d7bc3ff4def08e4b7a9de576d26586cec64b6116", .tag = "1ae10b594f09e26a7e902ecbd0600691" }; static bool GetData(CRYPT_Data *key, CRYPT_Data *iv, CRYPT_Data *aad, CRYPT_Data *data, CRYPT_Data *cipher) { key->data = CMVP_StringsToBins(CHACHA20POLY1305_VECTOR.key, &(key->len)); GOTO_ERR_IF_TRUE(key->data == NULL, CRYPT_CMVP_COMMON_ERR); iv->data = CMVP_StringsToBins(CHACHA20POLY1305_VECTOR.iv, &(iv->len)); GOTO_ERR_IF_TRUE(iv->data == NULL, CRYPT_CMVP_COMMON_ERR); aad->data = CMVP_StringsToBins(CHACHA20POLY1305_VECTOR.aad, &(aad->len)); GOTO_ERR_IF_TRUE(aad->data == NULL, CRYPT_CMVP_COMMON_ERR); data->data = CMVP_StringsToBins(CHACHA20POLY1305_VECTOR.plaintext, &(data->len)); GOTO_ERR_IF_TRUE(data->data == NULL, CRYPT_CMVP_COMMON_ERR); cipher->data = CMVP_StringsToBins(CHACHA20POLY1305_VECTOR.ciphertext, &(cipher->len)); GOTO_ERR_IF_TRUE(cipher->data == NULL, CRYPT_CMVP_COMMON_ERR); return true; ERR: return false; } static void FreeData(CRYPT_Data key, CRYPT_Data iv, CRYPT_Data aad, CRYPT_Data data, CRYPT_Data cipher) { BSL_SAL_Free(key.data); BSL_SAL_Free(iv.data); BSL_SAL_Free(aad.data); BSL_SAL_Free(data.data); BSL_SAL_Free(cipher.data); } static bool CRYPT_CMVP_SelftestChacha20poly1305Internal(void *libCtx, const char *attrName) { bool ret = false; CRYPT_Data key = { NULL, 0 }; CRYPT_Data iv = { NULL, 0 }; CRYPT_Data aad = { NULL, 0 }; CRYPT_Data data = { NULL, 0 }; CRYPT_Data cipher = { NULL, 0 }; CRYPT_Data tag = { NULL, 0 }; uint8_t *out = NULL; uint8_t *outTag = NULL; uint32_t outLen; uint32_t first; int32_t err = CRYPT_CMVP_ERR_ALGO_SELFTEST; CRYPT_EAL_CipherCtx *ctx = NULL; GOTO_ERR_IF_TRUE(!GetData(&key, &iv, &aad, &data, &cipher), err); tag.data = CMVP_StringsToBins(CHACHA20POLY1305_VECTOR.tag, &(tag.len)); GOTO_ERR_IF_TRUE(tag.data == NULL, CRYPT_CMVP_COMMON_ERR); outTag = BSL_SAL_Malloc(tag.len); outLen = cipher.len; out = BSL_SAL_Malloc(outLen); GOTO_ERR_IF_TRUE(outTag == NULL || out == NULL, CRYPT_MEM_ALLOC_FAIL); first = data.len / 2; // The length of the data in the first operation is 1/2 of the data. ctx = CRYPT_EAL_ProviderCipherNewCtx(libCtx, CRYPT_CIPHER_CHACHA20_POLY1305, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherInit(ctx, key.data, key.len, iv.data, iv.len, true) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, aad.data, aad.len) != CRYPT_SUCCESS, err); outLen = first; GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherUpdate(ctx, data.data, first, out, &outLen) != CRYPT_SUCCESS, err); outLen = data.len - first; GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherUpdate(ctx, data.data + first, data.len - first, out + first, &outLen) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, outTag, (uint32_t)tag.len) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(memcmp(out, cipher.data, cipher.len) != 0, err); GOTO_ERR_IF_TRUE(memcmp(outTag, tag.data, tag.len) != 0, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherInit(ctx, key.data, key.len, iv.data, iv.len, false) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, aad.data, aad.len) != CRYPT_SUCCESS, err); outLen = cipher.len; GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherUpdate(ctx, cipher.data, cipher.len, out, &outLen) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(memcmp(out, data.data, data.len) != 0, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, outTag, (uint32_t)tag.len) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(memcmp(outTag, tag.data, tag.len) != 0, err); ret = true; ERR: FreeData(key, iv, aad, data, cipher); BSL_SAL_Free(tag.data); BSL_SAL_Free(outTag); BSL_SAL_Free(out); CRYPT_EAL_CipherFreeCtx(ctx); return ret; } bool CRYPT_CMVP_SelftestChacha20poly1305(void) { return CRYPT_CMVP_SelftestChacha20poly1305Internal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderChacha20poly1305(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestChacha20poly1305Internal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_chacha20poly1305.c
C
unknown
6,126
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <stdio.h> #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_cipher.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_local_types.h" #include "bsl_sal.h" #include <securec.h> typedef struct { uint32_t id; const char *key; const char *aad; const char *iv; const char *plaintext; const char *ciphertext; const char *tag; uint32_t mode; } CMVP_CIPHER_VECTOR; // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf static const CMVP_CIPHER_VECTOR CIPHER_VECTOR[] = { // CRYPT_CIPHER_AES128_CBC { .id = CRYPT_CIPHER_AES128_CBC, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "7649abac8119b246cee98e9b12e9197d", .tag = NULL, .mode = CRYPT_MODE_CBC }, // CRYPT_CIPHER_AES192_CBC { .id = CRYPT_CIPHER_AES192_CBC, .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "4f021db243bc633d7178183a9fa071e8", .tag = NULL, .mode = CRYPT_MODE_CBC }, // CRYPT_CIPHER_AES256_CBC { .id = CRYPT_CIPHER_AES256_CBC, .key = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "f58c4c04d6e5f1ba779eabfb5f7bfbd6", .tag = NULL, .mode = CRYPT_MODE_CBC }, // CRYPT_CIPHER_AES128_CTR { .id = CRYPT_CIPHER_AES128_CTR, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "874d6191b620e3261bef6864990db6ce", .tag = NULL, .mode = CRYPT_MODE_CTR }, // CRYPT_CIPHER_AES192_CTR { .id = CRYPT_CIPHER_AES192_CTR, .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", .aad = NULL, .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "1abc932417521ca24f2b0459fe7e6e0b", .tag = NULL, .mode = CRYPT_MODE_CTR }, // CRYPT_CIPHER_AES256_CTR { .id = CRYPT_CIPHER_AES256_CTR, .key = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", .aad = NULL, .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "601ec313775789a5b7a7f504bbf3d228", .tag = NULL, .mode = CRYPT_MODE_CTR }, // CRYPT_CIPHER_AES128_ECB // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/ // aes/XTSTestVectors.zip { .id = CRYPT_CIPHER_AES128_ECB, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = NULL, .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "3ad77bb40d7a3660a89ecaf32466ef97", .tag = NULL, .mode = CRYPT_MODE_ECB }, // CRYPT_CIPHER_AES192_ECB { .id = CRYPT_CIPHER_AES192_ECB, .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", .aad = NULL, .iv = NULL, .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "bd334f1d6e45f25ff712a214571fa5cc", .tag = NULL, .mode = CRYPT_MODE_ECB }, // CRYPT_CIPHER_AES256_ECB { .id = CRYPT_CIPHER_AES256_ECB, .key = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", .aad = NULL, .iv = NULL, .plaintext = "6bc1bee22e409f96e93d7e117393172a", .ciphertext = "f3eed1bdb5d2a03c064b5a7e3db181f8", .tag = NULL, .mode = CRYPT_MODE_ECB }, // CRYPT_CIPHER_AES128_XTS { .id = CRYPT_CIPHER_AES128_XTS, .key = "a1b90cba3f06ac353b2c343876081762090923026e91771815f29dab01932f2f", .aad = NULL, .iv = "4faef7117cda59c66e4b92013e768ad5", .plaintext = "ebabce95b14d3c8d6fb350390790311c", .ciphertext = "778ae8b43cb98d5a825081d5be471c63", .tag = NULL, .mode = CRYPT_MODE_XTS }, // CRYPT_CIPHER_AES256_XTS { .id = CRYPT_CIPHER_AES256_XTS, .key = "e149be00177d76b7c1d85bcbb6b5054ee10b9f51cd73f59e0840628b9e7d854e2e1c0a" "b0537186a2a7c314bbc5eb23b6876a26bcdbf9e6b758d1cae053c2f278", .aad = NULL, .iv = "0ea18818fab95289b1caab4e61349501", .plaintext = "f5f101d8e3a7681b1ddb21bd2826b24e32990bca49b39291b5369a9bca277d75", .ciphertext = "5bf2479393cc673306fbb15e72600598e33d4d8a470727ce098730fd80afa959", .tag = NULL, .mode = CRYPT_MODE_XTS }, // CRYPT_CIPHER_AES128_CCM // http://csrc.nist.gov/groups/STM/cavp/documents/mac/ccmtestvectors.zip { .id = CRYPT_CIPHER_AES128_CCM, .key = "f149e41d848f59276cfddd743bafa9a9", .aad = "f5827e", .iv = "14b756d66fc51134e203d1c6f9", .plaintext = "9759e6f21f5a588010f57e6d6eae178d8b20ab59cda66f42", .ciphertext = "f634bf00f1f9f1f93f41049d7f3797b05e805f0b14850f4e78e2a23411147a6187da6818506232ee", .tag = NULL, .mode = CRYPT_MODE_CCM }, // CRYPT_CIPHER_AES192_CCM { .id = CRYPT_CIPHER_AES192_CCM, .key = "393dcac5a28d77297946d7ab471ae03bd303ba3499e2ce26", .aad = "1c8b", .iv = "fe7329f343f6e726a90b11ae37", .plaintext = "262f4ac988812500cb437f52f0c182148e85a0bec67a2736", .ciphertext = "e6d43f822ad168aa9c2e29c07f4592d7bbeb0203f418f3020ecdbc200be353112faf20e2be711908", .tag = NULL, .mode = CRYPT_MODE_CCM }, // CRYPT_CIPHER_AES256_CCM { .id = CRYPT_CIPHER_AES256_CCM, .key = "c5a850167a5bfdf56636ce9e56e2952855504e35cc4f5d24ee5e168853be82d8", .aad = "4759557e9bab", .iv = "c45b165477e8bfa9ca3a1cd3ca", .plaintext = "e758796d7db73bccb1697c42df691ac57974b40ca9186a43", .ciphertext = "93ad58bd5f4f77ac4f92b0ae16c62489e4074c7f152e2ed8a88179e0d32f4928eff13b4ce2873338", .tag = NULL, .mode = CRYPT_MODE_CCM }, // CRYPT_CIPHER_AES128_GCM // http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip { .id = CRYPT_CIPHER_AES128_GCM, .key = "07a6be880a58f572dbc2ad74a56db8b6", .aad = "de4269feea1a439d6e8990fd6f9f9d5bc67935294425255ea89b6f6772d680fd656b06" "581a5d8bc5c017ab532b4a9b83a55fde58cdfb3d2a8fef3aa426bc59d3e32f09d3cc20" "b1ceb9a9e349d1068a0aa3d39617fae0582ccef0", .iv = "95fc6654e6dc3a8adf5e7a69", .plaintext = "7680b48b5d28f38cdeab2d5851769394a3e141b990ec4bdf79a33e5315ac0338", .ciphertext = "095635c7e0eac0fc1059e67e1a936b6f72671121f96699fed520e5f8aff777f0", .tag = "b2235f6d4bdd7b9c0901711048859d47", .mode = CRYPT_MODE_GCM }, // CRYPT_CIPHER_AES192_GCM { .id = CRYPT_CIPHER_AES192_GCM, .key = "4e2d3d59e95884dc3aab32afdb96938cc6e9016d7f21e95f", .aad = "bd0dbced527c4df9e76c67405cfd0536ef45d6a392b789370356d71a12ee0cacbca6d8a8caa96d4c89923ddb6ba96622", .iv = "48fd791bf49a798a54fcdc60", .plaintext = "8e36651ba5bf9a6f903e01080083feeb", .ciphertext = "f96e2cc58714fd512b1fdbeba770b460", .tag = "63dfe1bbd756237a43150c82341486", .mode = CRYPT_MODE_GCM }, // CRYPT_CIPHER_AES256_GCM { .id = CRYPT_CIPHER_AES256_GCM, .key = "4c8cacccd8a55ec4222f3ec3996b23c4e86f9ab9c1312d53a7eb9b8891085ad9", .aad = "7dc38ba88808f3e0c7c08111e3f305b7f26ebb0f8915ab1d06b6eaa09ec9258fef04f4" "0c174d7cf4161653582058e611d667077""cbf0a974b632ed8d486dd807e2d9e8d8ef3" "749d2b2105e2a3161fe0b42b09fae30db42958aa94", .iv = "d9e80f9ab45c186c846b3605", .plaintext = "24ed8a0023a9e11d127488234c285956", .ciphertext = "4df94bd82b1b284e2dda6dccbbe5076f", .tag = "241e6c864aabc4a99e344a5d", .mode = CRYPT_MODE_GCM }, // CRYPT_CIPHER_CHACHA20_POLY1305 { .id = CRYPT_CIPHER_CHACHA20_POLY1305, .key = NULL, .aad = NULL, .iv = NULL, .plaintext = NULL, .ciphertext = NULL, .tag = NULL, .mode = CRYPT_MODE_CHACHA20_POLY1305 }, // CRYPT_CIPHER_SM4_CBC // http://c.gb688.cn/bzgk/gb/showGb?type=online&hcno=4F89D833626340B1F71068D25EAC737D // GB/T 17964-2021 { .id = CRYPT_CIPHER_SM4_CBC, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "000102030405060708090A0b0C0D0E0F", .plaintext = "6BC1BEE22E409F96E93D7E117393172A", .ciphertext = "AC529AF989A62FCE9CDDC5FFB84125CA", .tag = NULL, .mode = CRYPT_MODE_CBC }, // CRYPT_CIPHER_SM4_XTS // GB/T 17964-2021 { .id = CRYPT_CIPHER_SM4_XTS, .key = "2b7e151628aed2a6abf7158809cf4f3c000102030405060708090a0b0c0d0e0f", .aad = NULL, .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", .plaintext = "6BC1BEE22E409F96E93D7E117393172AAE2D8A571E03AC9C9EB76FAC45AF8E5130C81C46A35CE411E5FBC1191A0A52EFF69F2445DF4F9B17", .ciphertext = "E9538251C71D7B80BBE4483FEF497BD12C5C581BD6242FC51E08964FB4F60FDB0BA42F63499279213D318D2C11F6886E903BE7F93A1B3479", .tag = NULL, .mode = CRYPT_MODE_XTS }, // CRYPT_CIPHER_SM4_ECB // GB/T 17964-2021 { .id = CRYPT_CIPHER_SM4_ECB, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = NULL, .plaintext = "6BC1BEE22E409F96E93D7E117393172A", .ciphertext = "A51411ff04a711443891fce7ab842a29", .tag = NULL, .mode = CRYPT_MODE_ECB }, // CRYPT_CIPHER_SM4_CTR // GB/T 17964-2021 { .id = CRYPT_CIPHER_SM4_CTR, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff", .plaintext = "6BC1BEE22E409F96E93D7E117393172A", .ciphertext = "14AE4A72B97A93CE1216CCD998E371C1", .tag = NULL, .mode = CRYPT_MODE_CTR }, // CRYPT_CIPHER_SM4_GCM // https://www.rfc-editor.org/rfc/rfc8998.html { .id = CRYPT_CIPHER_SM4_GCM, .key = "0123456789ABCDEFFEDCBA9876543210", .aad = "FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2", .iv = "00001234567800000000ABCD", .plaintext = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDEEEEEEEEEEEEEEEEFFFFFFFFFFFFFFFFEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAA", .ciphertext = "17F399F08C67D5EE19D0DC9969C4BB7D5FD46FD3756489069157B282BB200735D82710CA5C22F0CCFA7CBF93D496AC15A56834CBCF98C397B4024A2691233B8D", .tag = "83DE3541E4C2B58177E065A9BF7B62EC", .mode = CRYPT_MODE_GCM }, // CRYPT_CIPHER_SM4_CFB // GB/T 17964-2021 { .id = CRYPT_CIPHER_SM4_CFB, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "000102030405060708090A0B0C0D0E0F", .plaintext = "6BC1BEE22E409F96E93D7E117393172A", .ciphertext = "bc710d762d070b26361da82b54565e46", .tag = NULL, .mode = CRYPT_MODE_CFB }, // CRYPT_CIPHER_SM4_OFB // GB/T 17964-2021 { .id = CRYPT_CIPHER_SM4_OFB, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "000102030405060708090A0B0C0D0E0F", .plaintext = "6BC1BEE22E409F96E93D7E117393172A", .ciphertext = "BC710D762D070B26361DA82B54565E46", .tag = NULL, .mode = CRYPT_MODE_OFB }, // https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf // CRYPT_CIPHER_AES128_CFB { .id = CRYPT_CIPHER_AES128_CFB, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c8" "1c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", .ciphertext = "3b3fd92eb72dad20333449f8e83cfb4ac8a64537a0b3a93fcde3cdad9f1ce58b2675" "1f67a3cbb140b1808cf187a4f4dfc04b05357c5d1c0eeac4c66f9ff7f2e6", .tag = NULL, .mode = CRYPT_MODE_CFB }, // CRYPT_CIPHER_AES192_CFB { .id = CRYPT_CIPHER_AES192_CFB, .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c8" "1c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", .ciphertext = "cdc80d6fddf18cab34c25909c99a417467ce7f7f81173621961a2b70171d3d7a2e1e" "8a1dd59b88b1c8e60fed1efac4c9c05f9f9ca9834fa042ae8fba584b09ff", .tag = NULL, .mode = CRYPT_MODE_CFB }, // CRYPT_CIPHER_AES256_CFB { .id = CRYPT_CIPHER_AES256_CFB, .key = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c8" "1c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", .ciphertext = "dc7e84bfda79164b7ecd8486985d386039ffed143b28b1c832113c6331e5407bdf10" "132415e54b92a13ed0a8267ae2f975a385741ab9cef82031623d55b1e471", .tag = NULL, .mode = CRYPT_MODE_CFB }, // CRYPT_CIPHER_AES128_OFB { .id = CRYPT_CIPHER_AES128_OFB, .key = "2b7e151628aed2a6abf7158809cf4f3c", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c8" "1c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", .ciphertext = "3b3fd92eb72dad20333449f8e83cfb4a7789508d16918f03f53c52dac54ed8259740" "051e9c5fecf64344f7a82260edcc304c6528f659c77866a510d9c1d6ae5e", .tag = NULL, .mode = CRYPT_MODE_OFB }, // CRYPT_CIPHER_AES192_OFB { .id = CRYPT_CIPHER_AES192_OFB, .key = "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c8" "1c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", .ciphertext = "cdc80d6fddf18cab34c25909c99a4174fcc28b8d4c63837c09e81700c11004018d9a" "9aeac0f6596f559c6d4daf59a5f26d9f200857ca6c3e9cac524bd9acc92a", .tag = NULL, .mode = CRYPT_MODE_OFB }, // CRYPT_CIPHER_AES256_OFB { .id = CRYPT_CIPHER_AES256_OFB, .key = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", .aad = NULL, .iv = "000102030405060708090a0b0c0d0e0f", .plaintext = "6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c8" "1c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710", .ciphertext = "dc7e84bfda79164b7ecd8486985d38604febdc6740d20b3ac88f6ad82a4fb08d71ab" "47a086e86eedf39d1c5bba97c4080126141d67f37be8538f5a8be740e484", .tag = NULL, .mode = CRYPT_MODE_OFB } }; typedef struct { CRYPT_Data key; CRYPT_Data iv; CRYPT_Data aad; CRYPT_Data plainText; CRYPT_Data cipherText; CRYPT_Data tag; } CIPHER_SELFTEST_DATA; bool CipherEnc(void *libCtx, const char *attrName, CRYPT_CIPHER_AlgId id, CIPHER_SELFTEST_DATA data) { bool ret = false; CRYPT_EAL_CipherCtx *ctx = NULL; uint32_t finLen; uint32_t len = data.cipherText.len; uint8_t *out = BSL_SAL_Malloc(len); GOTO_ERR_IF_TRUE(out == NULL, CRYPT_MEM_ALLOC_FAIL); memset_s(out, len, 0, len); ctx = CRYPT_EAL_ProviderCipherNewCtx(libCtx, id, attrName); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherInit(ctx, data.key.data, data.key.len, data.iv.data, data.iv.len, true) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherUpdate(ctx, data.plainText.data, data.plainText.len, out, &len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(data.cipherText.len < len, CRYPT_CMVP_ERR_ALGO_SELFTEST); finLen = data.cipherText.len - len; CRYPT_EAL_CipherFinal(ctx, out + len, &finLen); GOTO_ERR_IF_TRUE(memcmp(out, data.cipherText.data, data.cipherText.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(out); CRYPT_EAL_CipherFreeCtx(ctx); return ret; } bool CipherDec(void *libCtx, const char *attrName, CRYPT_CIPHER_AlgId id, CIPHER_SELFTEST_DATA data) { bool ret = false; CRYPT_EAL_CipherCtx *ctx = NULL; uint32_t finLen; uint32_t len = data.plainText.len; uint8_t *out = BSL_SAL_Malloc(len); GOTO_ERR_IF_TRUE(out == NULL, CRYPT_MEM_ALLOC_FAIL); ctx = CRYPT_EAL_ProviderCipherNewCtx(libCtx, id, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherInit(ctx, data.key.data, data.key.len, data.iv.data, data.iv.len, false) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherUpdate(ctx, data.cipherText.data, data.cipherText.len, out, &len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(data.plainText.len < len, CRYPT_CMVP_ERR_ALGO_SELFTEST); finLen = data.plainText.len - len; CRYPT_EAL_CipherFinal(ctx, out + len, &finLen); GOTO_ERR_IF_TRUE(memcmp(out, data.plainText.data, data.plainText.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(out); CRYPT_EAL_CipherFreeCtx(ctx); return ret; } bool AesAeadEnc(void *libCtx, const char *attrName, const CMVP_CIPHER_VECTOR *cipherVec, CIPHER_SELFTEST_DATA data) { bool ret = false; uint64_t msgLen = data.plainText.len; uint32_t cipherLen = data.cipherText.len; uint8_t *cipher = NULL; uint32_t tagLen; uint8_t *tag = NULL; uint32_t finLen; CRYPT_EAL_CipherCtx *ctx = NULL; cipher = BSL_SAL_Malloc(cipherLen); GOTO_ERR_IF_TRUE(cipher == NULL, CRYPT_MEM_ALLOC_FAIL); if (cipherVec->mode == CRYPT_MODE_GCM) { tagLen = data.tag.len; tag = BSL_SAL_Malloc(tagLen); GOTO_ERR_IF_TRUE(tag == NULL, CRYPT_ERR_ALGID); } else { tagLen = data.cipherText.len - data.plainText.len; } ctx = CRYPT_EAL_ProviderCipherNewCtx(libCtx, cipherVec->id, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherInit(ctx, data.key.data, data.key.len, data.iv.data, data.iv.len, true) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (cipherVec->mode == CRYPT_MODE_CCM) { GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_MSGLEN, &msgLen, sizeof(msgLen)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, data.aad.data, data.aad.len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); cipherLen = data.plainText.len; GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherUpdate(ctx, data.plainText.data, data.plainText.len, cipher, &cipherLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(data.cipherText.len < cipherLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); finLen = data.cipherText.len - cipherLen; if (cipherVec->mode != CRYPT_MODE_CCM && cipherVec->mode != CRYPT_MODE_GCM) { GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherFinal(ctx, cipher + cipherLen, &finLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } if (cipherVec->mode == CRYPT_MODE_CCM) { GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, cipher + msgLen, tagLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(cipher, data.cipherText.data, data.cipherText.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); } else { GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, tag, tagLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(cipher, data.cipherText.data, data.cipherText.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(tag, data.tag.data, data.tag.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); } ret = true; ERR: BSL_SAL_Free(tag); BSL_SAL_Free(cipher); CRYPT_EAL_CipherFreeCtx(ctx); return ret; } bool AesAeadDec(void *libCtx, const char *attrName, const CMVP_CIPHER_VECTOR *cipherVec, CIPHER_SELFTEST_DATA data) { bool ret = false; uint32_t tagLen; uint8_t *tag = NULL; uint64_t msgLen = data.plainText.len; uint32_t plainLen = data.plainText.len; uint8_t *plain = NULL; CRYPT_EAL_CipherCtx *ctx = NULL; plain = BSL_SAL_Malloc(plainLen); GOTO_ERR_IF_TRUE(plain == NULL, CRYPT_MEM_ALLOC_FAIL); if (cipherVec->mode == CRYPT_MODE_CCM) { tagLen = data.cipherText.len - data.plainText.len; } else { tagLen = data.tag.len; } tag = BSL_SAL_Malloc(tagLen); GOTO_ERR_IF_TRUE(tag == NULL, CRYPT_MEM_ALLOC_FAIL); ctx = CRYPT_EAL_ProviderCipherNewCtx(libCtx, cipherVec->id, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherInit(ctx, data.key.data, data.key.len, data.iv.data, data.iv.len, false) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &tagLen, sizeof(tagLen)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (cipherVec->mode == CRYPT_MODE_CCM) { GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_MSGLEN, &msgLen, sizeof(msgLen)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_SET_AAD, data.aad.data, data.aad.len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherUpdate(ctx, data.cipherText.data, data.plainText.len, plain, &plainLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(data.plainText.len != plainLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(plain, data.plainText.data, data.plainText.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_CipherCtrl(ctx, CRYPT_CTRL_GET_TAG, tag, tagLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (cipherVec->mode == CRYPT_MODE_CCM) { GOTO_ERR_IF_TRUE(memcmp(tag, data.cipherText.data + msgLen, tagLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); } else { GOTO_ERR_IF_TRUE(memcmp(tag, data.tag.data, tagLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); } ret = true; ERR: BSL_SAL_Free(tag); BSL_SAL_Free(plain); CRYPT_EAL_CipherFreeCtx(ctx); return ret; } const CMVP_CIPHER_VECTOR *FindCipherVectorById(CRYPT_CIPHER_AlgId id) { uint32_t num = sizeof(CIPHER_VECTOR) / sizeof(CIPHER_VECTOR[0]); const CMVP_CIPHER_VECTOR *cipherVec = NULL; for (uint32_t i = 0; i < num; i++) { if (CIPHER_VECTOR[i].id == id) { cipherVec = &CIPHER_VECTOR[i]; return cipherVec; } } return NULL; } static bool CRYPT_CMVP_SelftestCipherInternal(void *libCtx, const char *attrName, CRYPT_CIPHER_AlgId id) { const CMVP_CIPHER_VECTOR *cipherVec = FindCipherVectorById(id); if (cipherVec == NULL || cipherVec->key == NULL) { return false; } bool ret = false; CIPHER_SELFTEST_DATA data = { { NULL, 0 }, { NULL, 0 }, { NULL, 0 }, { NULL, 0 }, { NULL, 0 }, { NULL, 0 }, }; data.key.data = CMVP_StringsToBins(cipherVec->key, &(data.key.len)); GOTO_ERR_IF_TRUE(data.key.data == NULL, CRYPT_CMVP_COMMON_ERR); if (cipherVec->iv != NULL) { data.iv.data = CMVP_StringsToBins(cipherVec->iv, &(data.iv.len)); GOTO_ERR_IF_TRUE(data.iv.data == NULL, CRYPT_CMVP_COMMON_ERR); } data.plainText.data = CMVP_StringsToBins(cipherVec->plaintext, &(data.plainText.len)); GOTO_ERR_IF_TRUE(data.plainText.data == NULL, CRYPT_CMVP_COMMON_ERR); data.cipherText.data = CMVP_StringsToBins(cipherVec->ciphertext, &(data.cipherText.len)); GOTO_ERR_IF_TRUE(data.cipherText.data == NULL, CRYPT_CMVP_COMMON_ERR); if (cipherVec->aad != NULL) { data.aad.data = CMVP_StringsToBins(cipherVec->aad, &(data.aad.len)); GOTO_ERR_IF_TRUE(data.aad.data == NULL, CRYPT_CMVP_COMMON_ERR); } if (cipherVec->tag != NULL) { data.tag.data = CMVP_StringsToBins(cipherVec->tag, &(data.tag.len)); GOTO_ERR_IF_TRUE(data.tag.data == NULL, CRYPT_CMVP_COMMON_ERR); } if (cipherVec->mode == CRYPT_MODE_CCM || cipherVec->mode == CRYPT_MODE_GCM) { GOTO_ERR_IF_TRUE(AesAeadEnc(libCtx, attrName, cipherVec, data) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(AesAeadDec(libCtx, attrName, cipherVec, data) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); } else { GOTO_ERR_IF_TRUE(CipherEnc(libCtx, attrName, id, data) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CipherDec(libCtx, attrName, id, data) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); } ret = true; ERR: BSL_SAL_Free(data.key.data); BSL_SAL_Free(data.iv.data); BSL_SAL_Free(data.cipherText.data); BSL_SAL_Free(data.plainText.data); BSL_SAL_Free(data.aad.data); BSL_SAL_Free(data.tag.data); return ret; } bool CRYPT_CMVP_SelftestCipher(CRYPT_CIPHER_AlgId id) { return CRYPT_CMVP_SelftestCipherInternal(NULL, NULL, id); } bool CRYPT_CMVP_SelftestProviderCipher(void *libCtx, const char *attrName, CRYPT_CIPHER_AlgId id) { return CRYPT_CMVP_SelftestCipherInternal(libCtx, attrName, id); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_cipher.c
C
unknown
27,208
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { CRYPT_PKEY_ParaId paraId; const char *p; const char *q; const char *g; const char *xa; const char *ya; const char *xb; const char *yb; const char *z; } CMVP_DH_VECTOR; // https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/key-management static const CMVP_DH_VECTOR DH_VECTOR_MODP2048 = { .paraId = CRYPT_DH_RFC3526_2048, .p = "f528aa2762df76d7802fea087005d76feb69b7afd9e7fb363715a1b44f09dbda5d06a6" "3e3d512abe9c6810abba2bfd48cf554513712df25f786265a75f4e1dacaacbbe9e528a" "6346a8bf38015ecbc523d4e78d738b8852bcd66600dc434286c78f6ccae992568b258b" "b0d3b503e37e7a956ec1616978c1e7989229e39914ea7c1bea3902e669dfc62368da13" "7448350c1def54bf3ee2142c065601dfae6bca07e93cc463393cbc483b2c7272553788" "2ed5b96074f519fc738f5033b87b5edca0ec4a34dc0f84d2fa2c365f691a8fc7dc64bc" "0839a5813fef9af385c68e952af566f79b0802be9900cf0838032ed249afea05572154" "0c8961e6300ad5dec651c7", .q = "fa472bf5133f45b4c7ab14078e1281b487fc4fde1422ba4cafa2a64ea1aad989", .g = "085c0a0512c241b583d41703edfbfea2a3e863deac68855097707967e097186ef89d05" "c65227f56b12de6123bac86c3c13d680994bffaf24b2a1ed7f01c108b06593f22f74b4" "af222b46e5fb89482fbd96f5451c9f45393136ec037aa81a81245459ec018024518394" "f4d936596dc53c3d8a9f732903719796c045b62fadea9dd1b2fabec1560ddb3b780d96" "46ad0dd3168c07cc994f79ee804cae07573912511de050d05a0d58b819ec41e7c1205d" "c7199fc65a6a1a4ffcb4df38d9b6757269003401d84c732385a55174f27d4b493cb710" "980c3af98be7bcff9467e81792f2d2f9a2f8d5d0ddc9a229790192a194da5b2032f2f0" "cc23ffbcb2cc166f2128ee", .xa = "e62d8fdb14fcdc8e4b0c254216eb584a8e79115a294119806ac660cc9d0e3222", .ya = "815e1c8e64be6786b6c2194506b661199a56c807c6340a83697d3bc75304fbeb91bb15" "ac7981babba260a653e666b0da8012042a088547f0fbd1ef56d1d1f65ed809ca854513" "9f57d3ebda3e0f13e7ef19bded4a8dfa204f2457a3d392839dd70bec538a29d7887470" "9f905ec28740be157696aa862b57ab51ef20033601ff83ba3fb0e80255674e0e6e7269" "cd85ca10379d418b737d3cdeb5f31fb06f4a141145936a9eec0551b4ba59c59d2b3edd" "c19ea18978279cf11f21a65a9d2a104cfe6af93fa8a1ccd18b03017a8ab31943993192" "f794d87bbc273ef9160af954480783cfb6a37ba085503ba258169a8daba71d4c52de7c" "4e65472223c202ad277e93", .xb = "561c948be9d0d968cdfc6d27cbfe52cda342f544f56d57f118866326f76e1f70", .yb = "3c24973b3296269759ce38e2e525c0095d4b5c34be5d13b45b222b8e489c27fb4442f3" "2cc764665e28a06655c71fb37fd875a921d179551a1e4f2a9054a76cae2a61d3cbec55" "c3be19853a5409d9ff914b93bc78b8aa1525b908f32419a88d7726ead76a3f8895d630" "71a9b0a63fe4728d19518d1d08088141b8269f0b0cd77112d476af9efcc7f590af8fc1" "f9dc5e4c00cd5dfa64a33b2df4db9d8594d87489bea6f6f37958bfb598e5692b81bf11" "6b60227b6252a6438f049c5c449bab027740f8551bf1ffe25084f231ff646388d009ba" "22193262029ba19af2643dd679f283212a2d26ad917efe9642c748fceb33fb0a6c132f" "378dada2cccede086d8a31", .z = "ec2309a7d238aeb06714c27c1bbbb1b1c5aa3cdddd76a419b1f3704dd3437cd1f2c884" "3f350d67872ee325973f4e5ace7d406be5de75d9a9120af67e32f0291e77e7a3976249" "29d63dc0c42ef8f442ce89a39b192fee386ce68301c4b828ea9189798346b60f615dd9" "639105ffea6ec61ee5e4a7d68ce72bbf1281d6864e30181ce419952a3ef83a9ad7b26f" "c7292ad745bfb543e5c2ea310a4159e9d660279d12c1e03850e837c01c542a0f59ad61" "d0731005e8009a3d8406691abb22f5f2e96ae345783c403e59b9e948addbea8ac7d770" "821044e03f15ae6fc367ddc85ba62a26b4d94d7705f5ecad8aa21b619d0e09f124bee8" "658a2187f7029107105dbf" }; // Test vectors sourced from NIST ACVP-Server. static const CMVP_DH_VECTOR DH_VECTOR_FFDHE2048 = { .paraId = CRYPT_DH_RFC7919_2048, .p = NULL, .q = NULL, .g = NULL, .xa = "39983F0CD77A92B44E304AF18C811DB1DB2B820055F1A6CC8B78BFFFA57EB08299664B" "2CF76C3D5564FEC48C2F2F56AA63ACED5D11AB0B3390C008A94BD4351416DC978F5750" "410A2B618105EE9847DC9AB12AB39C971522EF97919E57C4DE706614FE20037738F871" "7895D5BC41458C437DA33F89745D6D742CC29632C0AF0A36CE0B99E0231D4C27833E71" "60F2687801462D5AA038B89A802B5E1ADF42A1D97C142D5851103E9034AE33B7916397" "D23BA16B9FF481FC1FD45FE35AA85E27B0E6A58A60A8E37D2A4D237793C4FA4E1E04ED" "F6090A4331237C9F8C2C19076367762D92491024A5F6B3BA510496154E05B43CE1A722" "9F793ABD2458095B3D9C78", .ya = "5FA0CB4D5A976B11442FFDA4569FFF10058E41988C78D0AB6E39B2E226EEFB7C13B9FD" "65720940118134762CD1ADE1EA83AF64548D8C8BFFA564C8747860FF816AE9C6F13797" "9FA7C2A4D3E12A3DE68986B43F14CC808E61FAEBAF702FF523795459CFCD19F83563F2" "5EFC0E57277512FAA8FA23CEF3C8F1D517ABED113FECEE21D926D96BA52A716B5FCDF1" "87D1ACD999A9CFA951311A042C55C693B20B0DDD98C2F2BBEF67E77FD9E18F0D52045E" "0B424ED3ECBBD2F34E008FD5C7B482B99CED2A7963DFCA54C5EF9E1FB56F4450B7312C" "F389D66A696479A6BFEE031D2ECED27969A4061E73331C63B6F21BD7D6E8358FF052F8" "71DE595DFD57752016D1CA", .xb = "78502F2AF8969B51D50D2756BAA5EF936FA735B2B2F8423E00D347FF6FB078F14BFA8B" "2A01A4CBF700C4861A7C65C7EFA026B6AAA7A60B944B09364D29AC3A9C10F6066AA0BB" "0115BA15E3934A6C9E981B5732E03A32FC23B3B320089F4EA6CFB31C6C7107E99C3063" "751238D21352BE40B602F94131D9C2D26E3444437D58ED0793D48ACC50749235F26B66" "837923750E5801F91ED3584A585A0FB213142312A438BE35A309D87009C30F40D49D2E" "D554F9D6BCBE84F54AB6382E7425FCADAED9D11909FA4624647B963605BC68896A34DA" "5C7F20447AD19C3381F87F40DC63969855A50E92D4448E432189697F7CAB4191D2B175" "6BB01FF62D4CA1661F6A33", .yb = "8CDD3810AA88A5410A5E416A68E9CD31A4542EEC0073A67D12300D3DC1EE5890AFE2DA" "CA654BFC29C63E1D011C7A52E3DE8D89A3771F43F6921521BB137D25459C90E46F090B" "FD53B58901109A28F587B96A35828C3464ACF6F80D9646494CF4389FD82BFE4E7AE2BB" "E5CA2694D7967F56A0E92D43E8C119BD81B7A50516BFA8B39BBEA538AE56556808F4B5" "C3BAD84A76A5EB373D596DBEF75AB90A49C902615C7386334B70BAE9AC0C4E953DB03B" "883B64E7E3C3FDD547C6BC3C004D032A57BA40E06DCB0E87016DDADED991FF34FD6ECA" "06635041DD98C0C4C1DEF942C751DB3F5218BC7C2F6039AF2323F473D3775ED27936FD" "2E209541C9FAEAE2A75874", .z = "024E9786943F8AA48E0FC7B2862D620A6DDA720F7CEFB53A38D8DEDC8D8E29737BA62A" "2E4D04B7E64B2B23396497FBDE5FE803437199C1703C6DA3ED9867FCB358EFD5B98B8B" "077E608F39DFBF574A89AA5521F78856E04D05E02B525928C437DF2E2AD5D45B0E6E3C" "5D656CE2434D4D8BB4DCD11496C30615EB1970CA3DE40723518D79E8ED1A07BE59C486" "134E5075367A74CF1F9DCBFB85BA36E643D4915C481B3129BD6988CB2E7BE441DA257C" "924C34B431CE048F8F7BC21B601F51D1EA009C77D3E10F3A59196ED40EE2D80699970F" "76EEC65142617CF427F1386566FDCE92FBA5FE734071B504EED601797589BF7C7F9EAD" "508A7B5B4915DD477009E1", }; static bool GetPara(const CMVP_DH_VECTOR *vector, CRYPT_EAL_PkeyPara *para) { para->id = CRYPT_PKEY_DH; para->para.dhPara.p = CMVP_StringsToBins(vector->p, &(para->para.dhPara.pLen)); GOTO_ERR_IF_TRUE(para->para.dhPara.p == NULL, CRYPT_CMVP_COMMON_ERR); para->para.dhPara.q = CMVP_StringsToBins(vector->q, &(para->para.dhPara.qLen)); GOTO_ERR_IF_TRUE(para->para.dhPara.q == NULL, CRYPT_CMVP_COMMON_ERR); para->para.dhPara.g = CMVP_StringsToBins(vector->g, &(para->para.dhPara.gLen)); GOTO_ERR_IF_TRUE(para->para.dhPara.g == NULL, CRYPT_CMVP_COMMON_ERR); return true; ERR: return false; } static bool GetKey(const CMVP_DH_VECTOR *vector, CRYPT_EAL_PkeyPrv *prv1, CRYPT_EAL_PkeyPub *pub1, CRYPT_EAL_PkeyPrv *prv2, CRYPT_EAL_PkeyPub *pub2) { prv1->id = CRYPT_PKEY_DH; prv1->key.dhPrv.data = CMVP_StringsToBins(vector->xa, &(prv1->key.dhPrv.len)); GOTO_ERR_IF_TRUE(prv1->key.dhPrv.data == NULL, CRYPT_CMVP_COMMON_ERR); pub1->id = CRYPT_PKEY_DH; pub1->key.dhPub.data = CMVP_StringsToBins(vector->ya, &(pub1->key.dhPub.len)); GOTO_ERR_IF_TRUE(pub1->key.dhPub.data == NULL, CRYPT_CMVP_COMMON_ERR); prv2->id = CRYPT_PKEY_DH; prv2->key.dhPrv.data = CMVP_StringsToBins(vector->xb, &(prv2->key.dhPrv.len)); GOTO_ERR_IF_TRUE(prv2->key.dhPrv.data == NULL, CRYPT_CMVP_COMMON_ERR); pub2->id = CRYPT_PKEY_DH; pub2->key.dhPub.data = CMVP_StringsToBins(vector->yb, &(pub2->key.dhPub.len)); GOTO_ERR_IF_TRUE(pub2->key.dhPub.data == NULL, CRYPT_CMVP_COMMON_ERR); return true; ERR: return false; } static bool ComputeShareKey(const CMVP_DH_VECTOR *vector, CRYPT_EAL_PkeyCtx *prv, CRYPT_EAL_PkeyCtx *pub) { bool ret = false; uint8_t *expShare = NULL; uint8_t *share = NULL; uint32_t expShareLen; uint32_t shareLen; expShare = CMVP_StringsToBins(vector->z, &expShareLen); GOTO_ERR_IF_TRUE(expShare == NULL, CRYPT_CMVP_COMMON_ERR); shareLen = expShareLen; share = BSL_SAL_Malloc(shareLen); GOTO_ERR_IF_TRUE(share == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyComputeShareKey(prv, pub, share, &shareLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(shareLen != expShareLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(share, expShare, expShareLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(expShare); BSL_SAL_Free(share); return ret; } static void FreeData(CRYPT_EAL_PkeyPara para, CRYPT_EAL_PkeyPrv prv1, CRYPT_EAL_PkeyPub pub1, CRYPT_EAL_PkeyPrv prv2, CRYPT_EAL_PkeyPub pub2) { BSL_SAL_Free(para.para.dhPara.p); BSL_SAL_Free(para.para.dhPara.q); BSL_SAL_Free(para.para.dhPara.g); BSL_SAL_Free(prv1.key.dhPrv.data); BSL_SAL_Free(pub1.key.dhPub.data); BSL_SAL_Free(prv2.key.dhPrv.data); BSL_SAL_Free(pub2.key.dhPub.data); } static bool CRYPT_CMVP_SelftestDhInternal(const CMVP_DH_VECTOR *vector, void *libCtx, const char *attrName) { bool ret = false; CRYPT_EAL_PkeyPara para; para.para.dhPara.p = NULL; para.para.dhPara.q = NULL; para.para.dhPara.g = NULL; CRYPT_EAL_PkeyPrv prv1 = {0}; prv1.key.dhPrv.data = NULL; CRYPT_EAL_PkeyPub pub1; pub1.key.dhPub.data = NULL; CRYPT_EAL_PkeyPrv prv2 = {0}; prv2.key.dhPrv.data = NULL; CRYPT_EAL_PkeyPub pub2; pub2.key.dhPub.data = NULL; CRYPT_EAL_PkeyCtx *pkeyPrv = NULL; CRYPT_EAL_PkeyCtx *pkeyPub = NULL; if (vector->paraId == CRYPT_DH_RFC3526_2048) { GOTO_ERR_IF_TRUE(GetPara(vector, &para) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(GetKey(vector, &prv1, &pub1, &prv2, &pub2) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_DH, 0, attrName); pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_DH, 0, attrName); GOTO_ERR_IF_TRUE(pkeyPrv == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(pkeyPub == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (vector->paraId == CRYPT_DH_RFC3526_2048) { GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPara(pkeyPrv, &para) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } else { GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetParaById(pkeyPrv, vector->paraId) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(pkeyPrv, &prv1) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(pkeyPub, &pub2) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(ComputeShareKey(vector, pkeyPrv, pkeyPub) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(pkeyPrv, &prv2) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(pkeyPub, &pub1) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(ComputeShareKey(vector, pkeyPrv, pkeyPub) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: FreeData(para, prv1, pub1, prv2, pub2); CRYPT_EAL_PkeyFreeCtx(pkeyPrv); CRYPT_EAL_PkeyFreeCtx(pkeyPub); return ret; } bool CRYPT_CMVP_SelftestDh(void) { return CRYPT_CMVP_SelftestDhInternal(&DH_VECTOR_MODP2048, NULL, NULL) && CRYPT_CMVP_SelftestDhInternal(&DH_VECTOR_FFDHE2048, NULL, NULL); } bool CRYPT_CMVP_SelftestProviderDh(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestDhInternal(&DH_VECTOR_MODP2048, libCtx, attrName) && CRYPT_CMVP_SelftestDhInternal(&DH_VECTOR_FFDHE2048, libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_dh.c
C
unknown
13,410
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_rand.h" #include "crypt_eal_cmvp.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" #define DRBG_PARAM_COUNT 6 typedef enum { CMVP_DRBG_INSTANTIATE, CMVP_DRBG_SEED, CMVP_DRBG_MAX, } CMVP_DrbgState; typedef struct { uint32_t id; const char *entropy; // EntropyInput const char *nonce; // Nonce const char *pers; // PersonalizationString const char *entropySeed; // EntropyInputReseed const char *adinSeed; // AdditionalInputReseed const char *adin1; // AdditionalInput const char *adin2; // AdditionalInput const char *retBits; // ReturnedBits } CMVP_DRBG_VECTOR; typedef struct { CMVP_DrbgState state; CRYPT_RAND_AlgId id; } CMVP_DRBG_SEEDCTX; const CMVP_DRBG_VECTOR *g_currentTestVector = NULL; /** * Test Vector Execution Order: * 1. Instantiate * 2. Reseed * 3. Generate Random Bits * 4. Generate Random Bits * 5. Uninstantiate * Data Source: * https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Random-Number-Generators * NON PR */ static const CMVP_DRBG_VECTOR DRBG_VECTOR[] = { // CRYPT_RAND_SHA1 { .id = CRYPT_RAND_SHA1, .entropy = "48a1a97ccc49d7ccf6e378a2f16b0fcd", .nonce = "b091d2ec12a839fe", .pers = "3dc16c1add9cac4ebbb0b889e43b9e12", .entropySeed = "ba5da6791237243fea6050f5b99ecdf5", .adinSeed = "d123e38e4c97e82994a9717ac6f17c08", .adin1 = "800bed9729cfade6680dfe53ba0c1e28", .adin2 = "251e66b9e385ac1c17fb771b5dc76cf2", .retBits = "a1b2ee86a0f1dab79383133a62279908953a1c9a987760121119cc78b8512bd537a19db973ca397a" "dd9233786d5d41fffae98059048521e25284bc6fdb97f34e6a127acd410f50682846be569e9a6bc8" }, // CRYPT_RAND_SHA224 { .id = CRYPT_RAND_SHA224, .entropy = "184cbf7f1c462f27fc640ccf2aac1b26174ee41e42dcceaa", .nonce = "09f9d8acd06aba74b9f849f7", .pers = "5a5afe330e898ca94fad05b0e6b3f8146f46c90379a0b1eb", .entropySeed = "b5eb44d3515c74d2cbd28c4ac5edb5fb95846e74e8398ce5", .adinSeed = "a793fefe0f2ab3e9a0d1ddbc058d78369b03597f44099a81", .adin1 = "930ef8531a344fef957660cbb401583afa0f016b7023a9db", .adin2 = "2ee03b7314fb00e1e2616799c144cd58f051cde370588d70", .retBits = "22b856603db40f1b6d439d5b88fbe4734f7fdee15f4df47dfd418b362f23e48fef0f48f03d1a7b7b" "0de607c2a8288b1aaa01bc84646c322a88b2351855d7fa1b66b0b12baccbaa5ad6cc71833998f899" "8712bddf54ab8af329c55791b7576cf36ade4b921009ffe32a8d22ecf4747571" }, // CRYPT_RAND_SHA256 { .id = CRYPT_RAND_SHA256, .entropy = "6c623aea73bc8a59e28c6cd9c7c7ec8ca2e75190bd5dcae5978cf0c199c23f4f", .nonce = "e55db067a0ed537e66886b7cda02f772", .pers = "1e59d798810083d1ff848e90b25c9927e3dfb55a0888b0339566a9f9ca7542dc", .entropySeed = "9ab40164744c7d00c78b4196f6f917ec33d70030a0812cd4606c5a25387568a9", .adinSeed = "4e8bead7cbba7a7bc9ae1e1617222c4139661347599950e7225d1e2faa5d57f5", .adin1 = "dcb22a5d9f149858636f3ede2253e419816fb7b1103194451ed6a573a8fe6271", .adin2 = "8f9d5c78cdabc32e71ac3b3c49239caddf96053250f4fd92056efbd0be487d36", .retBits = "6e98a3b1f686f6ffa79355c9d8a5ab7f93312159d52659a2298315f10007c71adabc0b5ccb4164c0" "949fbdb221b43acdb62bed3099596f2d7bd5d0048173dd2360a543b234ab61a441ddb9299af84ca4" "5c6e618fd521366dbf509d4ec06174da924361d642b107e5564ac1b32340dd2f3158bf4c00bcb4dc" "f12c6d67af4b74ee" }, // CRYPT_RAND_SHA384 { .id = CRYPT_RAND_SHA384, .entropy = "f411e1feeccf01c0d4bde61ca2384a2640b41e383a055b374e0acfa8170c2f28", .nonce = "7cf75b960dcd0a0a9d2a4e7e8d5e47d3", .pers = "25d6dfee3e74d3b6a9f459094203fc76e0e589fa879cc445008c80e3736fc0a9", .entropySeed = "d222df563773906b875d55dc1aef90337ff59fc3ca5ed0af5e46d306d630c7e3", .adinSeed = "07a576624662253737789e543734d7c35ded8d74a3b53919b1c28c21a2b5ebc5", .adin1 = "2561c8591281f0682d3811387d0cdc16c137edfcc9527134212701f73550c572", .adin2 = "870441d9435f2cbf16f1168f50e32d9b8811be7adc10a5070c5eb993372c5732", .retBits = "9107af002a8bc3e0f0394eb0db3a801ca73844db0600873d1d576ccfbdd88dfc3eaa101e52e4c4ad" "9958d9d0e5f1eb555cd0d93ad2745a1302dfead60c42ef28e7211740b1dc694fdf72dd066d1d66a5" "8aceeb9a8c6a9c67a75326f97b742b85e7abdc853b01bd799bb9f3e8e6b5f2a41919543b17c0da4e" "4e25f04e1c2859a56466689ab85c46cb9f593abff0f058f7d26f2c09e379e5e0b6e123f24fb9bcfb" "a9a468dcb38a9577d63251d20f09b8d2b4dad74fb52e1e8dbdde6e0436563d66" }, // CRYPT_RAND_SHA512 { .id = CRYPT_RAND_SHA512, .entropy = "4b23595b0a3640cfabb0ec34df6a613308b0448488a5d9ff99da4278e072eb34", .nonce = "8e696bffd9ca3a71d2e2f05e600c8364", .pers = "010ba93ea68a3d4a200e5145859e299c5b5349b7645fb5bbcad687aba7d67313", .entropySeed = "04de4babdbe143bde99aa4452f9aa43b0a164eb927555c0496aa0fc9328a521c", .adinSeed = "2b0c7c3efb36b71b917a44086d168313675b426b17c5ab3d0eb6af753f6040e0", .adin1 = "d0b7d1d12ab15d3bba8f4eba07fee0974838962b247be480683b8e3d4a91033a", .adin2 = "66c78ca12e45bdca003b49cb6440b977dd85b167e7c803890ed1a73666eaa869", .retBits = "4008cbd8281dc82fd6c368f650ef2609bb771e80c63d478a77fa938248dcbb8b79e54ead0265f6ff" "1ebfafe4e387c6e27df9f03e4a5225e86a4436e56ebf03b3be2cfbcb49c89c92ec1dfa5ee445dd4f" "6f64e02a2423a0b18ebd02eec52f5cc21bc3565e796b3ded6552f1b5a574a201c3b11018222806f9" "618d23d77fd02db879cf87fe24ed7ba11b3b108b559633db1f95c5121b28011aa4dd20399bd4978e" "1f8b8880c333a47ff1750679bf28d329347b26d347aae90ee562ae8029579cbe0336e066d6b8ba5e" "0169fec804c30189a4434c1bf8a5b0a249951d3d89554da38ff0751b8b1fef9ae18a0aa2bc477736" "d199a06f61d400039a4cc03869bb10ca" }, // CRYPT_RAND_SM3 { .id = CRYPT_RAND_SM3, .entropy = "8c6368232f5cc9da92e5877fd368c5769ecf1f4eaf011a89e11686af8e379895", .nonce = "8dd1cfbcbd615a47e1298a94ca12f248", .pers = "c325c1db2ddaa54616b2b804cca6f1a8", .entropySeed = "5c638a5bc1ffd99fa58b0e2482347f9d5c638a5bc1ffd99fa58b0e2482347f9d", .adinSeed = "b05a4b1751cd5fb3e583966cf888d44d", .adin1 = "81b5bbc2ec9e7ae9c2f999ff58d28f2b", .adin2 = "7af5ca6867e0211baad5b24c6229d6a5", .retBits = "05a2637f235e86be101ec21b1e75ae26" }, // CRYPT_RAND_HMAC_SHA1 { .id = CRYPT_RAND_HMAC_SHA1, .entropy = "03e7b41c95818eb0b667bfa8a175a824", .nonce = "66a1e417a9b6b92f", .pers = "126dded5eb0bc81be37c10bcd9d5f793", .entropySeed = "d17e98c2e50ee0db00d25c3364451e95", .adinSeed = "dc596d188e2343802240bc7f5cc60516", .adin1 = "14c8ec10f5bdde6b9e75898d7f9f03d0", .adin2 = "31aa842afcc1daa94098241a87d6ddfc", .retBits = "4739b1bcf87404a2290829bd7a61f0b391a794c71c055c7cc513b28dcb5fdc88645bc9cb490f41fa" "b134c6b33ce9336571762754343961de671b02a47960b4b4e23c5bfb87dcc19b260b3bcb921ae325" }, // CRYPT_RAND_HMAC_SHA224 { .id = CRYPT_RAND_HMAC_SHA224, .entropy = "96ae702af50c50c7c38818a5133938bd7ce51197fc78e218", .nonce = "15b6c5a7ff9c0395d764159f", .pers = "e96554644097e9932585b7f4bb14d101f24c8b0376f38c05", .entropySeed = "707d5813e5bf47c1b8232b44a007bf7decfef499d758ed53", .adinSeed = "3f698a5f6f4fe67ef2ddf23bd5a67c1a2df4f3b19425fb85", .adin1 = "fe1f6a90fc0ed396bca21c0d40a1bb583eb63df78c98adac", .adin2 = "5942b56148f27dd5388f00caa47ffd4925e854237fe14454", .retBits = "150b9260ce9aa419fe1860332ae7c9f42d9ada1649679b53f46bc9d20de3431186a54afb5df7b626" "9cdc05540a93fdd50a2cd3a862372d862841768df02846b057993dd6aa32f874b7220a5a1fd9cb57" "3d720a54af5715cedfc16f0d9a467735e253b2b1a6e97421fcee1f2d670dec1a" }, // CRYPT_RAND_HMAC_SHA256 { .id = CRYPT_RAND_HMAC_SHA256, .entropy = "cdb0d9117cc6dbc9ef9dcb06a97579841d72dc18b2d46a1cb61e314012bdf416", .nonce = "d0c0d01d156016d0eb6b7e9c7c3c8da8", .pers = "6f0fb9eab3f9ea7ab0a719bfa879bf0aaed683307fda0c6d73ce018b6e34faaa", .entropySeed = "8ec6f7d5a8e2e88f43986f70b86e050d07c84b931bcf18e601c5a3eee3064c82", .adinSeed = "1ab4ca9014fa98a55938316de8ba5a68c629b0741bdd058c4d70c91cda5099b3", .adin1 = "16e2d0721b58d839a122852abd3bf2c942a31c84d82fca74211871880d7162ff", .adin2 = "53686f042a7b087d5d2eca0d2a96de131f275ed7151189f7ca52deaa78b79fb2", .retBits = "dda04a2ca7b8147af1548f5d086591ca4fd951a345ce52b3cd49d47e84aa31a183e31fbc42a1ff1d" "95afec7143c8008c97bc2a9c091df0a763848391f68cb4a366ad89857ac725a53b303ddea767be8d" "c5f605b1b95f6d24c9f06be65a973a089320b3cc42569dcfd4b92b62a993785b0301b3fc45244565" "6fce22664827b88f" }, // CRYPT_RAND_HMAC_SHA384 { .id = CRYPT_RAND_HMAC_SHA384, .entropy = "c4868db5c46fde0a10008838b5be62c349209fded42fab461b01e11723c8242a", .nonce = "618faba54acba1e0afd4b27cbd731ed9", .pers = "135132cf2b8a57554bdc13c68e90dc434353e4f65a4d5ca07c3e0a13c62e7265", .entropySeed = "d30016b5827dc2bfe4034c6654d69775fe98432b19e3da373213d939d391f54a", .adinSeed = "a0bbd02f6aa71a06d1642ca2cc7cdc5e8857e431b176bcf1ecd20f041467bd2d", .adin1 = "93ee30a9e7a0e244aa91da62f2215c7233bdfc415740d2770780cbbad61b9ba2", .adin2 = "36d922cacca00ae89db8f0c1cae5a47d2de8e61ae09357ca431c28a07907fce1", .retBits = "2aac4cebed080c68ef0dcff348506eca568180f7370c020deda1a4c9050ce94d4db90fd827165846" "d6dd6cb2031eec1634b0e7f3e0e89504e34d248e23a8fb31cd32ff39a486946b2940f54c968f96cf" "c508cd871c84e68458ca7dccabc6dcfb1e9fbef9a47caae14c5239c28686e0fc0942b0c847c9d8d9" "87970c1c5f5f06eaa8385575dacb1e925c0ed85e13edbb9922083f9bbbb79405411ff5dfe7061568" "5df1f1e49867d0b6ed69afe8ac5e76ffab6ff3d71b4dae998faf8c7d5bc6ae4d" }, // CRYPT_RAND_HMAC_SHA512 { .id = CRYPT_RAND_HMAC_SHA512, .entropy = "da740cbc36057a8e282ae717fe7dfbb245e9e5d49908a0119c5dbcf0a1f2d5ab", .nonce = "46561ff612217ba3ff91baa06d4b5440", .pers = "fc227293523ecb5b1e28c87863626627d958acc558a672b148ce19e2abd2dde4", .entropySeed = "1d61d4d8a41c3254b92104fd555adae0569d1835bb52657ec7fbba0fe03579c5", .adinSeed = "b9ed8e35ad018a375b61189c8d365b00507cb1b4510d21cac212356b5bbaa8b2", .adin1 = "b7998998eaf9e5d34e64ff7f03de765b31f407899d20535573e670c1b402c26a", .adin2 = "2089d49d63e0c4df58879d0cb1ba998e5b3d1a7786b785e7cf13ca5ea5e33cfd", .retBits = "5b70f3e4da95264233efbab155b828d4e231b67cc92757feca407cc9615a660871cb07ad1a2e9a99" "412feda8ee34dc9c57fa08d3f8225b30d29887d20907d12330fffd14d1697ba0756d37491b0a8814" "106e46c8677d49d9157109c402ad0c247a2f50cd5d99e538c850b906937a05dbb8888d984bc77f6c" "a00b0e3bc97b16d6d25814a54aa12143afddd8b2263690565d545f4137e593bb3ca88a37b0aadf79" "726b95c61906257e6dc47acd5b6b7e4b534243b13c16ad5a0a1163c0099fce43f428cd27c3e6463c" "f5e9a9621f4b3d0b3d4654316f4707675df39278d5783823049477dcce8c57fdbd576711c91301e9" "bd6bb0d3e72dc46d480ed8f61fd63811" }, // CRYPT_RAND_AES128_CTR { .id = CRYPT_RAND_AES128_CTR, .entropy = "289e5c8283cbd7dbe707255cb3cf2907d8a5ce5b347314966f9b2bebb1a1e200", .nonce = NULL, .pers = "7f7b59f23510b976fe155d047525c94e2dacb30d77ac8b09281544dd815d5293", .entropySeed = "98c522028f36fc6b85a8f3c003efd4b130dd90180ec81cf7c67d4c53d10f0022", .adinSeed = "f7a0378328d939f0f8521e39409d7175d87319c7597a9050414f7adc392a328d", .adin1 = "19c286f5b36194d1cc62c0188140bc9d61d2a9c5d88bb5aebc224bfb04dfca83", .adin2 = "820650c3201d347f5b20d3d25d1c8c7bef4d9f66a5a04c7dd9d669e95182a0c4", .retBits = "79a79d44edada58e3fc12a4e36ae900eeace290265f01262f40f2958a70dcbd4d4185f708c088ede" "7ff8c8375f44f4012f2512d38328a5df171a17029d90f185" }, // CRYPT_RAND_AES192_CTR { .id = CRYPT_RAND_AES192_CTR, .entropy = "4b58271b116237eedd4e9ff9360382a59f3e2a173d860f2bbd8b2bace142b2395c67cf5a513f06f3", .nonce = NULL, .pers = "cf76c16cd5d270707ea9acc39744db69bfac63e566256fd6917bf9819679840f3fea2aa535d8df01", .entropySeed = "1867f371a345eef98b2d70fc1960397892645b7b29a4ead252e8835e0b600618a9bd6ff99785d890", .adinSeed = "6d44839aff8b7165deebd489ad088ecb7dcec11c32b1e747dba8f0e8a0b89f74a84ea8a05586fe9e", .adin1 = "42248fce0994e0e63504209d629a6943eb3e2ad512f03f79cbd5102928392bce1cacbba056ac6ca9", .adin2 = "bd529b600273329423a58d6f8a12be0f17989a02e73e347bc7d49d9169337a6cff7c07e8a807a80a", .retBits = "02486d32cd55954f406ba55705f1460d384439592dede81a84fda221fd45c0d651d67ec4a81a8b40" "4151a643f331ad051cb004352289de37bca71e8cc0a6aeab" }, // CRYPT_RAND_AES256_CTR { .id = CRYPT_RAND_AES256_CTR, .entropy = "ae7ebe062971f5eb32e5b21444750785de816595ad2cbe80a209c8f8ab04b5468166de8c6ae522d8" "f10b56386a3b424f", .nonce = NULL, .pers = "55860dae57fcac297087c137efb796878a75868f6e7681114e9b73ed0c67e3c62bfc9f5d77e8caa5" "9bcdb223f4ffd247", .entropySeed = "a42407931bfeca70e6ee5dd197021a129525051c07468e8b25587c5ad50abe9204e882fe847b8fd4" "7cf7b4360e5aa034", .adinSeed = "ee4c88d1eb05f4853663eada501d2fc4b4984b283a88db579af2113031e03d9bc570de943dd16891" "8f3ba8065581fea7", .adin1 = "4b4b03ef19b0f259dca2b3ee3ae4cd86c3895a784b3d8eee043a2003c08289f8fffdad141e6b1ab2" "174d8d5d79c1e581", .adin2 = "3062b33f116b46e20fe3c354726ae9b2a3a4c51922c8107863cb86f1f0bdad7554075659d91c371e" "2b11b1e8106a1ed5", .retBits = "0d270518baeafac160ff1cb28c11ef68712c764c0c01674e6c9ca2cc9c7e0e8accfd3c753635ee07" "0081eee7628af6187fbc2854b3c204461a796cf3f3fcb092" }, // CRYPT_RAND_AES128_CTR_DF { .id = CRYPT_RAND_AES128_CTR_DF, .entropy = "e14ed7064a97814dd326b9a05bc44543", .nonce = "876240c1f7de3dba", .pers = "26ccf56848a048721d0aad87d6fc65f0", .entropySeed = "7ec4ac660fa0bbfa66ac3802e511901f", .adinSeed = "8835d28e7f85a4e95087bdd1bb7ad57e", .adin1 = "2a9bd50bbb20fefe24649f5f80eede66", .adin2 = "f7ce3d5c6c381e56b25410c6909c1074", .retBits = "d2f3130d309bed1da65545b9d793e035fd2564303d1fdcfb6c7fee019500d9f5d434fab2d3c8d15e" "39a25f965aaa804c7141407e90c4a86a6c8d303ce83bfb34" }, // CRYPT_RAND_AES192_CTR_DF { .id = CRYPT_RAND_AES192_CTR_DF, .entropy = "c4b1e6a99587eacd7ec8517f40f9433ca432cea8686433f0", .nonce = "d03a29e548e58ca7cbf0ac707b1464e3", .pers = "0daaead21779b2a428d2b7fb12d9ab8316899edbe26b5460de1549c99e4781c9", .entropySeed = "2229144c1b4efb79ab5fe079cda26bc33acbb2a0a87f642c", .adinSeed = "f116a683ca485fda846a598b8d9b079e78c2828286ad530bf01f693cc8af9f84", .adin1 = "7c89de353298935bd26aa18517355313df0630da5f45ea0240e809179363080b", .adin2 = "e978b8fe56afc908bed129a46d57a8698d66034d4dbcc7aba3a33d5796fb7559", .retBits = "8ce7e9589c2975fd6989a450aa65da9114e515777c97351da037ccb72d4987eb69c680411724ed60" "2e6ac76cd2d085725616c92777a4664d43a59c3ae9946134" }, // CRYPT_RAND_AES256_CTR_DF { .id = CRYPT_RAND_AES256_CTR_DF, .entropy = "174b46250051a9e3d80c56ae7163dafe7e54481a56cafd3b8625f99bbb29c442", .nonce = "98ffd99c466e0e94a45da7e0e82dbc6b", .pers = "7095268e99938b3e042734b9176c9aa051f00a5f8d2a89ada214b89beef18ebf", .entropySeed = "e88be1967c5503f65d23867bbc891bd679db03b4878663f6c877592df25f0d9a", .adinSeed = "cdf6ad549e45b6aa5cd67d024931c33cd133d52d5ae500c3015020beb30da063", .adin1 = "c7228e90c62f896a09e11684530102f926ec90a3255f6c21b857883c75800143", .adin2 = "76a94f224178fe4cbf9e2b8acc53c9dc3e50bb613aac8936601453cda3293b17", .retBits = "1a6d8dbd642076d13916e5e23038b60b26061f13dd4e006277e0268698ffb2c87e453bae1251631a" "c90c701a9849d933995e8b0221fe9aca1985c546c2079027" }, // CRYPT_RAND_SM4_CTR_DF { .id = CRYPT_RAND_SM4_CTR_DF, .entropy = "8c6368232f5cc9da92e5877fd368c5769ecf1f4eaf011a89e11686af8e379895", .nonce = "8dd1cfbcbd615a47e1298a94ca12f248", .pers = "c325c1db2ddaa54616b2b804cca6f1a8", .entropySeed = "5c638a5bc1ffd99fa58b0e2482347f9d5c638a5bc1ffd99fa58b0e2482347f9d", .adinSeed = "b05a4b1751cd5fb3e583966cf888d44d", .adin1 = "81b5bbc2ec9e7ae9c2f999ff58d28f2b", .adin2 = "7af5ca6867e0211baad5b24c6229d6a5", .retBits = "5e9a44ce51ee802f2fc49335d8b4588b" }, }; static int32_t CMVP_DrbgGetEntropy(void *ctx, CRYPT_Data *entropy, uint32_t strength, CRYPT_Range *lenRange) { CMVP_DRBG_SEEDCTX *seedCtx = (CMVP_DRBG_SEEDCTX *)ctx; const CMVP_DRBG_VECTOR *vector = g_currentTestVector; GOTO_ERR_IF_TRUE(vector == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(vector->entropy == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (seedCtx->state == CMVP_DRBG_INSTANTIATE) { entropy->data = CMVP_StringsToBins(vector->entropy, &(entropy->len)); GOTO_ERR_IF_TRUE(entropy->data == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); seedCtx->state = CMVP_DRBG_SEED; } else if (seedCtx->state == CMVP_DRBG_SEED) { entropy->data = CMVP_StringsToBins(vector->entropySeed, &(entropy->len)); GOTO_ERR_IF_TRUE(entropy->data == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); seedCtx->state = CMVP_DRBG_MAX; } (void)strength; (void)lenRange; return CRYPT_SUCCESS; ERR: return CRYPT_CMVP_ERR_ALGO_SELFTEST; } static void CMVP_DrbgCleanData(void *ctx, CRYPT_Data *data) { BSL_SAL_Free(data->data); data->data = NULL; data->len = 0; (void)ctx; } static int32_t CMVP_DrbgGetNonce(void *ctx, CRYPT_Data *nonce, uint32_t strength, CRYPT_Range *lenRange) { (void)ctx; uint8_t *data = NULL; uint32_t dataLen; const CMVP_DRBG_VECTOR *vector = g_currentTestVector; GOTO_ERR_IF_TRUE(vector == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(vector->nonce == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); data = CMVP_StringsToBins(vector->nonce, &dataLen); GOTO_ERR_IF_TRUE(data == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); nonce->data = data; nonce->len = dataLen; (void)strength; (void)lenRange; return CRYPT_SUCCESS; ERR: BSL_SAL_Free(data); return CRYPT_CMVP_ERR_ALGO_SELFTEST; } static CRYPT_EAL_RndCtx *CMVP_DrbgInit(void *libCtx, const char *attrName, const CMVP_DRBG_VECTOR *drbgVec, CMVP_DRBG_SEEDCTX *seedCtx) { CRYPT_EAL_RndCtx *ctx = NULL; uint8_t *pers = NULL; uint32_t persLen; CRYPT_RandSeedMethod method; const CMVP_DRBG_VECTOR *vector = drbgVec; GOTO_ERR_IF_TRUE(vector->pers == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); pers = CMVP_StringsToBins(vector->pers, &persLen); GOTO_ERR_IF_TRUE(pers == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); method.getEntropy = CMVP_DrbgGetEntropy; method.cleanEntropy = CMVP_DrbgCleanData; method.getNonce = CMVP_DrbgGetNonce; method.cleanNonce = CMVP_DrbgCleanData; int32_t index = 0; BSL_Param param[DRBG_PARAM_COUNT] = {0}; (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, seedCtx, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR, method.getEntropy, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR, method.cleanEntropy, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, method.getNonce, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, method.cleanNonce, 0); ctx = CRYPT_EAL_ProviderDrbgNewCtx(libCtx, drbgVec->id, attrName, param); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); #ifdef HITLS_CRYPTO_CMVP_SM int32_t selfTest = 0; GOTO_ERR_IF_TRUE(CRYPT_EAL_DrbgCtrl(ctx, CRYPT_CTRL_SET_SELFTEST_FLAG, &selfTest, sizeof(int32_t)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); #endif GOTO_ERR_IF_TRUE(CRYPT_EAL_DrbgInstantiate(ctx, pers, persLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); BSL_SAL_Free(pers); return ctx; ERR: CRYPT_EAL_DrbgDeinit(ctx); BSL_SAL_Free(pers); return NULL; } static void FreeData(uint8_t *rand, uint8_t *expectRand, uint8_t *adinSeed, uint8_t *adin1, uint8_t *adin2) { BSL_SAL_Free(rand); BSL_SAL_Free(expectRand); BSL_SAL_Free(adinSeed); BSL_SAL_Free(adin1); BSL_SAL_Free(adin2); } static uint8_t *ExecDrbg(CRYPT_EAL_RndCtx *ctx, uint32_t randLen, uint8_t *adin1, uint32_t adin1Len, uint8_t *adin2, uint32_t adin2Len) { uint8_t *rand = NULL; rand = BSL_SAL_Malloc(randLen); GOTO_ERR_IF_TRUE(rand == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_DrbgbytesWithAdin(ctx, rand, randLen, adin1, adin1Len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_DrbgbytesWithAdin(ctx, rand, randLen, adin2, adin2Len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); return rand; ERR: BSL_SAL_Free(rand); return NULL; } static bool GetData(const CMVP_DRBG_VECTOR *drbgVec, CRYPT_Data *expectRand, CRYPT_Data *adinSeed, CRYPT_Data *adin1, CRYPT_Data *adin2) { expectRand->data = CMVP_StringsToBins(drbgVec->retBits, &(expectRand->len)); GOTO_ERR_IF_TRUE(expectRand->data == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); adin1->data = CMVP_StringsToBins(drbgVec->adin1, &(adin1->len)); GOTO_ERR_IF_TRUE(adin1->data == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); adin2->data = CMVP_StringsToBins(drbgVec->adin2, &(adin2->len)); GOTO_ERR_IF_TRUE(adin2->data == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); adinSeed->data = CMVP_StringsToBins(drbgVec->adinSeed, &(adinSeed->len)); GOTO_ERR_IF_TRUE(adinSeed->data == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); return true; ERR: return false; } const CMVP_DRBG_VECTOR *FindDrbgVectorById(CRYPT_RAND_AlgId id) { uint32_t num = sizeof(DRBG_VECTOR) / sizeof(DRBG_VECTOR[0]); const CMVP_DRBG_VECTOR *drbgVec = NULL; for (uint32_t i = 0; i < num; i++) { if (DRBG_VECTOR[i].id == id) { drbgVec = &DRBG_VECTOR[i]; return drbgVec; } } return NULL; } static bool CRYPT_CMVP_SelftestDrbgInternal(void *libCtx, const char *attrName, CRYPT_RAND_AlgId id) { bool ret = false; CRYPT_EAL_RndCtx *ctx = NULL; uint8_t *rand = NULL; CRYPT_Data expectRand = { NULL, 0 }; CRYPT_Data adinSeed = { NULL, 0 }; CRYPT_Data adin1 = { NULL, 0 }; CRYPT_Data adin2 = { NULL, 0 }; CMVP_DRBG_SEEDCTX seedCtx = { CMVP_DRBG_INSTANTIATE, id }; const CMVP_DRBG_VECTOR *drbgVec = FindDrbgVectorById(id); if (drbgVec == NULL || drbgVec->entropy == NULL) { return false; } g_currentTestVector = drbgVec; GOTO_ERR_IF_TRUE(!GetData(drbgVec, &expectRand, &adinSeed, &adin1, &adin2), CRYPT_CMVP_ERR_ALGO_SELFTEST); ctx = CMVP_DrbgInit(libCtx, attrName, drbgVec, &seedCtx); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_DrbgSeedWithAdin(ctx, adinSeed.data, adinSeed.len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); // 2: One byte and two characters rand = ExecDrbg(ctx, (uint32_t)strlen(drbgVec->retBits) / 2, adin1.data, adin1.len, adin2.data, adin2.len); GOTO_ERR_IF_TRUE(rand == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(rand, expectRand.data, expectRand.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: CRYPT_EAL_DrbgDeinit(ctx); FreeData(rand, expectRand.data, adinSeed.data, adin1.data, adin2.data); return ret; } bool CRYPT_CMVP_SelftestDrbg(CRYPT_RAND_AlgId id) { return CRYPT_CMVP_SelftestDrbgInternal(NULL, NULL, id); } bool CRYPT_CMVP_SelftestProviderDrbg(void *libCtx, const char *attrName, CRYPT_RAND_AlgId id) { return CRYPT_CMVP_SelftestDrbgInternal(libCtx, attrName, id); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_drbg.c
C
unknown
25,548
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_bn.h" #include "crypt_encode_internal.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "crypt_eal_rand.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_util_rand.h" #include "bsl_sal.h" #define BITS_OF_BYTE 8 typedef struct { const char *p; const char *q; const char *g; const char *msg; const char *x; // The private key. const char *y; const char *k; const char *r; const char *s; CRYPT_MD_AlgId mdId; } CMVP_DSA_VECTOR; // https://www.rfc-editor.org/rfc/rfc6979#page-27 static const CMVP_DSA_VECTOR DSA_VECTOR = { .p = "a8adb6c0b4cf9588012e5deff1a871d383e0e2a85b5e8e03d814fe13a059705e" "663230a377bf7323a8fa117100200bfd5adf857393b0bbd67906c081e585410e" "38480ead51684dac3a38f7b64c9eb109f19739a4517cd7d5d6291e8af20a3fbf" "17336c7bf80ee718ee087e322ee41047dabefbcc34d10b66b644ddb3160a28c0" "639563d71993a26543eadb7718f317bf5d9577a6156561b082a10029cd44012b" "18de6844509fe058ba87980792285f2750969fe89c2cd6498db3545638d5379d" "125dccf64e06c1af33a6190841d223da1513333a7c9d78462abaab31b9f96d5f" "34445ceb6309f2f6d2c8dde06441e87980d303ef9a1ff007e8be2f0be06cc15f", .q = "e71f8567447f42e75f5ef85ca20fe557ab0343d37ed09edc3f6e68604d6b9dfb", .g = "5ba24de9607b8998e66ce6c4f812a314c6935842f7ab54cd82b19fa104abfb5d" "84579a623b2574b37d22ccae9b3e415e48f5c0f9bcbdff8071d63b9bb956e547" "af3a8df99e5d3061979652ff96b765cb3ee493643544c75dbe5bb39834531952" "a0fb4b0378b3fcbb4c8b5800a5330392a2a04e700bb6ed7e0b85795ea38b1b96" "2741b3f33b9dde2f4ec1354f09e2eb78e95f037a5804b6171659f88715ce1a9b" "0cc90c27f35ef2f10ff0c7c7a2bb0154d9b8ebe76a3d764aa879af372f4240de" "8347937e5a90cec9f41ff2f26b8da9a94a225d1a913717d73f10397d2183f1ba" "3b7b45a68f1ff1893caf69a827802f7b6a48d51da6fbefb64fd9a6c5b75c4561", .msg = "4e3a28bcf90d1d2e75f075d9fbe55b36c5529b17bc3a9ccaba6935c9e2054825" "5b3dfae0f91db030c12f2c344b3a29c4151c5b209f5e319fdf1c23b190f64f1f" "e5b330cb7c8fa952f9d90f13aff1cb11d63181da9efc6f7e15bfed4862d1a62c" "7dcf3ba8bf1ff304b102b1ec3f1497dddf09712cf323f5610a9d10c3d9132659", .x = "446969025446247f84fdea74d02d7dd13672b2deb7c085be11111441955a377b", .y = "5a55dceddd1134ee5f11ed85deb4d634a3643f5f36dc3a70689256469a0b651a" "d22880f14ab85719434f9c0e407e60ea420e2a0cd29422c4899c416359dbb1e5" "92456f2b3cce233259c117542fd05f31ea25b015d9121c890b90e0bad033be13" "68d229985aac7226d1c8c2eab325ef3b2cd59d3b9f7de7dbc94af1a9339eb430" "ca36c26c46ecfa6c5481711496f624e188ad7540ef5df26f8efacb820bd17a1f" "618acb50c9bc197d4cb7ccac45d824a3bf795c234b556b06aeb9291734532520" "84003f69fe98045fe74002ba658f93475622f76791d9b2623d1b5fff2cc16844" "746efd2d30a6a8134bfc4c8cc80a46107901fb973c28fc553130f3286c1489da", .k = "117a529e3fdfc79843a5a4c07539036b865214e014b4928c2a31f47bf62a4fdb", .r = "633055e055f237c38999d81c397848c38cce80a55b649d9e7905c298e2a51447", .s = "2bbf68317660ec1e4b154915027b0bc00ee19cfc0bf75d01930504f2ce10a8b0", .mdId = CRYPT_MD_SHA256 }; static bool GetPkey(void *libCtx, const char *attrName, CRYPT_EAL_PkeyCtx **pkeyPrv, CRYPT_EAL_PkeyCtx **pkeyPub, CRYPT_EAL_PkeyPara *para, CRYPT_EAL_PkeyPub *pub, CRYPT_EAL_PkeyPrv *prv) { *pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_DSA, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPrv == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); *pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_DSA, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPub == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); para->para.dsaPara.p = CMVP_StringsToBins(DSA_VECTOR.p, &(para->para.dsaPara.pLen)); GOTO_ERR_IF_TRUE(para->para.dsaPara.p == NULL, CRYPT_CMVP_COMMON_ERR); para->para.dsaPara.q = CMVP_StringsToBins(DSA_VECTOR.q, &(para->para.dsaPara.qLen)); GOTO_ERR_IF_TRUE(para->para.dsaPara.q == NULL, CRYPT_CMVP_COMMON_ERR); para->para.dsaPara.g = CMVP_StringsToBins(DSA_VECTOR.g, &(para->para.dsaPara.gLen)); GOTO_ERR_IF_TRUE(para->para.dsaPara.g == NULL, CRYPT_CMVP_COMMON_ERR); prv->key.dsaPrv.data = CMVP_StringsToBins(DSA_VECTOR.x, &(prv->key.dsaPrv.len)); GOTO_ERR_IF_TRUE(prv->key.dsaPrv.data == NULL, CRYPT_CMVP_COMMON_ERR); pub->key.dsaPub.data = CMVP_StringsToBins(DSA_VECTOR.y, &(pub->key.dsaPub.len)); GOTO_ERR_IF_TRUE(pub->key.dsaPub.data == NULL, CRYPT_CMVP_COMMON_ERR); para->id = CRYPT_PKEY_DSA; pub->id = CRYPT_PKEY_DSA; prv->id = CRYPT_PKEY_DSA; GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPara(*pkeyPrv, para) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPara(*pkeyPub, para) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(*pkeyPrv, prv) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(*pkeyPub, pub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); return true; ERR: return false; } static int32_t TestVectorRandom(uint8_t *r, uint32_t rLen) { uint8_t *rand = NULL; uint32_t randLen; rand = CMVP_StringsToBins(DSA_VECTOR.k, &randLen); if (rand == NULL) { return CRYPT_MEM_ALLOC_FAIL; } if (randLen < rLen) { BSL_SAL_Free(rand); return CRYPT_CMVP_ERR_ALGO_SELFTEST; } for (uint32_t i = 0; i < randLen; i++) { r[i] = rand[i]; } BSL_SAL_Free(rand); return 0; } static int32_t SignEncode(const char *signR, const char *signS, uint8_t *vectorSign, uint32_t *vectorSignLen) { int ret = CRYPT_CMVP_ERR_ALGO_SELFTEST; BN_BigNum *bnR = NULL; BN_BigNum *bnS = NULL; uint8_t *r = NULL; uint8_t *s = NULL; uint32_t rLen, sLen; r = CMVP_StringsToBins(signR, &rLen); GOTO_ERR_IF_TRUE(r == NULL, CRYPT_CMVP_COMMON_ERR); s = CMVP_StringsToBins(signS, &sLen); GOTO_ERR_IF_TRUE(s == NULL, CRYPT_CMVP_COMMON_ERR); bnR = BN_Create(rLen * BITS_OF_BYTE); bnS = BN_Create(sLen * BITS_OF_BYTE); GOTO_ERR_IF_TRUE(BN_Bin2Bn(bnR, r, rLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(BN_Bin2Bn(bnS, s, sLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_EAL_EncodeSign(bnR, bnS, vectorSign, vectorSignLen); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_SUCCESS; ERR: BSL_SAL_Free(r); BSL_SAL_Free(s); BN_Destroy(bnR); BN_Destroy(bnS); return ret; } static void FreeData(CRYPT_EAL_PkeyPara para, CRYPT_EAL_PkeyPub pub, CRYPT_EAL_PkeyPrv prv, uint8_t *msg, uint8_t *sign) { BSL_SAL_Free(para.para.dsaPara.p); BSL_SAL_Free(para.para.dsaPara.q); BSL_SAL_Free(para.para.dsaPara.g); BSL_SAL_Free(msg); BSL_SAL_Free(pub.key.dsaPub.data); BSL_SAL_Free(prv.key.dsaPrv.data); BSL_SAL_Free(sign); } static bool CRYPT_CMVP_SelftestDsaInternal(void *libCtx, const char *attrName) { bool ret = false; uint8_t *msg = NULL; uint8_t *sign = NULL; uint8_t *signVec = NULL; uint32_t msgLen, signLen; uint32_t signVecLen = 0; CRYPT_EAL_PkeyCtx *pkeyPrv = NULL; CRYPT_EAL_PkeyCtx *pkeyPub = NULL; CRYPT_EAL_PkeyPara para = { 0 }; CRYPT_EAL_PkeyPub pub = { 0 }; CRYPT_EAL_PkeyPrv prv = { 0 }; CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); msg = CMVP_StringsToBins(DSA_VECTOR.msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(!GetPkey(libCtx, attrName, &pkeyPrv, &pkeyPub, &para, &pub, &prv), CRYPT_CMVP_ERR_ALGO_SELFTEST); signLen = CRYPT_EAL_PkeyGetSignLen(pkeyPrv); sign = BSL_SAL_Malloc(signLen); signVecLen = signLen; GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); signVec = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(signVec == NULL, CRYPT_MEM_ALLOC_FAIL); // regist rand function CRYPT_RandRegist(TestVectorRandom); // sign GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(pkeyPrv, DSA_VECTOR.mdId, msg, msgLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); // compare the signature GOTO_ERR_IF_TRUE(SignEncode(DSA_VECTOR.r, DSA_VECTOR.s, signVec, &signVecLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(signLen != signVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(signVec, sign, signLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); // verify GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyVerify(pkeyPub, DSA_VECTOR.mdId, msg, msgLen, sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: FreeData(para, pub, prv, msg, sign); BSL_SAL_Free(signVec); CRYPT_EAL_PkeyFreeCtx(pkeyPrv); CRYPT_EAL_PkeyFreeCtx(pkeyPub); CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); return ret; } bool CRYPT_CMVP_SelftestDsa(void) { return CRYPT_CMVP_SelftestDsaInternal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderDsa(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestDsaInternal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_dsa.c
C
unknown
9,983
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "securec.h" #include "bsl_sal.h" typedef struct { const char *bobD; const char *bobX; const char *bobY; const char *aliceD; const char *aliceX; const char *aliceY; const char *shareKey; int32_t curveId; CRYPT_MD_AlgId mdId; } CMVP_EcdhVector; // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/keymgmt/KASTestVectorsECC2016.zip static const CMVP_EcdhVector ECDH_VECTOR = { .bobD = "d58751997b3a551ccdc6f507bf6ab87e2be7f8267067a455a5815a54", .bobX = "c7893ced15c3009b93cb0abeaea2ede308b67fbbd902c6c5d94c24a4", .bobY = "858afc2edd61fcefef3469dd5a004e74a3c727d16498a9408a8e3224", .aliceD = "e935434303d842605d07112b3b7789ccbe4c6d987db5fa15ea1cdadb", .aliceX = "784028a2246950401ec81f8e4e03fd3765c4da4d45eba652ed5ba2b5", .aliceY = "d071c32634bcf058f072493b6943453a0bd117f5ee5c5866f037f6ab", .shareKey = "dfd0570d4ae5c9f690c757aead04f7e14758fdc4ee05d8f0d089b91a", .curveId = CRYPT_ECC_NISTP224, .mdId = CRYPT_MD_SHA224 }; static bool GetPkey(void *libCtx, const char *attrName, bool isBob, CRYPT_EAL_PkeyCtx **pkeyPrv, CRYPT_EAL_PkeyCtx **pkeyPub, CRYPT_EAL_PkeyPub *pub, CRYPT_EAL_PkeyPrv *prv) { bool ret = false; uint8_t *x = NULL; uint8_t *y = NULL; uint32_t xLen, yLen; *pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_ECDH, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPrv == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); *pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_ECDH, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPub == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); prv->id = CRYPT_PKEY_ECDH; if (isBob == true) { prv->key.eccPrv.data = CMVP_StringsToBins(ECDH_VECTOR.bobD, &(prv->key.eccPrv.len)); } else { prv->key.eccPrv.data = CMVP_StringsToBins(ECDH_VECTOR.aliceD, &(prv->key.eccPrv.len)); } GOTO_ERR_IF_TRUE(prv->key.eccPrv.data == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetParaById(*pkeyPrv, ECDH_VECTOR.curveId) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(*pkeyPrv, prv) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); pub->id = CRYPT_PKEY_ECDH; if (isBob == true) { x = CMVP_StringsToBins(ECDH_VECTOR.bobX, &xLen); y = CMVP_StringsToBins(ECDH_VECTOR.bobY, &yLen); } else { x = CMVP_StringsToBins(ECDH_VECTOR.aliceX, &xLen); y = CMVP_StringsToBins(ECDH_VECTOR.aliceY, &yLen); } GOTO_ERR_IF_TRUE(x == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(y == NULL, CRYPT_CMVP_COMMON_ERR); pub->key.eccPub.len = xLen + yLen + 1; pub->key.eccPub.data = BSL_SAL_Malloc(pub->key.eccPub.len); GOTO_ERR_IF_TRUE(pub->key.eccPub.data == NULL, CRYPT_MEM_ALLOC_FAIL); pub->key.eccPub.data[0] = 0x04; // CRYPT_POINT_UNCOMPRESSED标记头 GOTO_ERR_IF_TRUE(memcpy_s(pub->key.eccPub.data + 1, pub->key.eccPub.len, x, xLen) != EOK, CRYPT_SECUREC_FAIL); GOTO_ERR_IF_TRUE( memcpy_s(pub->key.eccPub.data + 1 + xLen, pub->key.eccPub.len, y, yLen) != EOK, CRYPT_SECUREC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetParaById(*pkeyPub, ECDH_VECTOR.curveId) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(*pkeyPub, pub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(x); BSL_SAL_FREE(y); return ret; } static bool CRYPT_CMVP_SelftestEcdhInternal(void *libCtx, const char *attrName) { bool ret = false; CRYPT_EAL_PkeyCtx *bobPrvPkey = NULL; CRYPT_EAL_PkeyCtx *bobPubPkey = NULL; CRYPT_EAL_PkeyPub bobPub = { 0 }; CRYPT_EAL_PkeyPrv bobPrv = { 0 }; CRYPT_EAL_PkeyCtx *alicePrvPkey = NULL; CRYPT_EAL_PkeyCtx *alicePubPkey = NULL; CRYPT_EAL_PkeyPub alicePub = { 0 }; CRYPT_EAL_PkeyPrv alicePrv = { 0 }; uint8_t *shareKey = NULL; uint32_t shareKeyLen; uint8_t *expShareKey = NULL; uint32_t expShareKeyLen; expShareKey = CMVP_StringsToBins(ECDH_VECTOR.shareKey, &expShareKeyLen); GOTO_ERR_IF_TRUE(expShareKey == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(GetPkey(libCtx, attrName, true, &bobPrvPkey, &bobPubPkey, &bobPub, &bobPrv) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(GetPkey(libCtx, attrName, false, &alicePrvPkey, &alicePubPkey, &alicePub, &alicePrv) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); shareKeyLen = CRYPT_EAL_PkeyGetKeyLen(bobPrvPkey); shareKey = BSL_SAL_Malloc(shareKeyLen); GOTO_ERR_IF_TRUE(shareKey == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyComputeShareKey(bobPrvPkey, alicePubPkey, shareKey, &shareKeyLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(shareKeyLen != expShareKeyLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(expShareKey, shareKey, shareKeyLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); shareKeyLen = CRYPT_EAL_PkeyGetKeyLen(bobPrvPkey); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyComputeShareKey(alicePrvPkey, bobPubPkey, shareKey, &shareKeyLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(shareKeyLen != expShareKeyLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(expShareKey, shareKey, shareKeyLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(bobPub.key.eccPub.data); BSL_SAL_Free(bobPrv.key.eccPrv.data); CRYPT_EAL_PkeyFreeCtx(bobPrvPkey); CRYPT_EAL_PkeyFreeCtx(bobPubPkey); BSL_SAL_Free(alicePub.key.eccPub.data); BSL_SAL_Free(alicePrv.key.eccPrv.data); CRYPT_EAL_PkeyFreeCtx(alicePrvPkey); CRYPT_EAL_PkeyFreeCtx(alicePubPkey); BSL_SAL_Free(shareKey); BSL_SAL_Free(expShareKey); return ret; } bool CRYPT_CMVP_SelftestEcdh(void) { return CRYPT_CMVP_SelftestEcdhInternal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderEcdh(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestEcdhInternal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_ecdh.c
C
unknown
6,935
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_bn.h" #include "crypt_encode_internal.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "crypt_eal_rand.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_util_rand.h" #include "securec.h" #include "bsl_sal.h" #define BITS_OF_BYTE 8 typedef struct { const char *msg; const char *d; const char *qX; const char *qY; const char *k; const char *signR; const char *signS; int32_t curveId; CRYPT_MD_AlgId mdId; } CMVP_EcdsaVector; // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/dss/186-4ecdsatestvectors.zip static const CMVP_EcdsaVector ECDSA_VECTOR = { .msg = "ff624d0ba02c7b6370c1622eec3fa2186ea681d1659e0a845448e777b75a8e77" "a77bb26e5733179d58ef9bc8a4e8b6971aef2539f77ab0963a3415bbd6258339" "bd1bf55de65db520c63f5b8eab3d55debd05e9494212170f5d65b3286b8b6687" "05b1e2b2b5568610617abb51d2dd0cb450ef59df4b907da90cfa7b268de8c4c2", .d = "708309a7449e156b0db70e5b52e606c7e094ed676ce8953bf6c14757c826f590", .qX = "29578c7ab6ce0d11493c95d5ea05d299d536801ca9cbd50e9924e43b733b83ab", .qY = "08c8049879c6278b2273348474158515accaa38344106ef96803c5a05adc4800", .k = "58f741771620bdc428e91a32d86d230873e9140336fcfb1e122892ee1d501bdc", .signR = "4a19274429e40522234b8785dc25fc524f179dcc95ff09b3c9770fc71f54ca0d", .signS = "58982b79a65b7320f5b92d13bdaecdd1259e760f0f718ba933fd098f6f75d4b7", .curveId = CRYPT_ECC_NISTP256, .mdId = CRYPT_MD_SHA224 }; static bool GetPkey(void *libCtx, const char *attrName, CRYPT_EAL_PkeyCtx **pkeyPrv, CRYPT_EAL_PkeyCtx **pkeyPub, CRYPT_EAL_PkeyPub *pub, CRYPT_EAL_PkeyPrv *prv) { bool ret = false; uint8_t *x = NULL; uint8_t *y = NULL; uint32_t xLen, yLen; *pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_ECDSA, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPrv == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); *pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_ECDSA, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPub == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); prv->id = CRYPT_PKEY_ECDSA; prv->key.eccPrv.data = CMVP_StringsToBins(ECDSA_VECTOR.d, &(prv->key.eccPrv.len)); GOTO_ERR_IF_TRUE(prv->key.eccPrv.data == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetParaById(*pkeyPrv, ECDSA_VECTOR.curveId) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(*pkeyPrv, prv) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); pub->id = CRYPT_PKEY_ECDSA; x = CMVP_StringsToBins(ECDSA_VECTOR.qX, &xLen); GOTO_ERR_IF_TRUE(x == NULL, CRYPT_CMVP_COMMON_ERR); y = CMVP_StringsToBins(ECDSA_VECTOR.qY, &yLen); GOTO_ERR_IF_TRUE(y == NULL, CRYPT_CMVP_COMMON_ERR); pub->key.eccPub.len = xLen + yLen + 1; pub->key.eccPub.data = BSL_SAL_Malloc(pub->key.eccPub.len); GOTO_ERR_IF_TRUE(pub->key.eccPub.data == NULL, CRYPT_MEM_ALLOC_FAIL); pub->key.eccPub.data[0] = 0x04; // CRYPT_POINT_UNCOMPRESSED标记头 GOTO_ERR_IF_TRUE(memcpy_s(pub->key.eccPub.data + 1, pub->key.eccPub.len, x, xLen) != EOK, CRYPT_SECUREC_FAIL); GOTO_ERR_IF_TRUE( memcpy_s(pub->key.eccPub.data + 1 + xLen, pub->key.eccPub.len, y, yLen) != EOK, CRYPT_SECUREC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetParaById(*pkeyPub, ECDSA_VECTOR.curveId) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(*pkeyPub, pub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(x); BSL_SAL_Free(y); return ret; } static int32_t TestVectorRandom(uint8_t *r, uint32_t rLen) { uint8_t *rand = NULL; uint32_t randLen; rand = CMVP_StringsToBins(ECDSA_VECTOR.k, &randLen); if (rand == NULL) { return CRYPT_MEM_ALLOC_FAIL; } if (randLen < rLen) { BSL_SAL_Free(rand); return CRYPT_CMVP_ERR_ALGO_SELFTEST; } for (uint32_t i = 0; i < randLen; i++) { r[i] = rand[i]; } BSL_SAL_Free(rand); return 0; } static int SignEncode(const char *signR, const char *signS, uint8_t *vectorSign, uint32_t *vectorSignLen) { int ret = CRYPT_CMVP_ERR_ALGO_SELFTEST; BN_BigNum *bnR = NULL; BN_BigNum *bnS = NULL; uint8_t *r = NULL; uint8_t *s = NULL; uint32_t rLen, sLen; r = CMVP_StringsToBins(signR, &rLen); GOTO_ERR_IF_TRUE(r == NULL, CRYPT_CMVP_COMMON_ERR); s = CMVP_StringsToBins(signS, &sLen); GOTO_ERR_IF_TRUE(s == NULL, CRYPT_CMVP_COMMON_ERR); bnR = BN_Create(rLen * BITS_OF_BYTE); bnS = BN_Create(sLen * BITS_OF_BYTE); GOTO_ERR_IF_TRUE(BN_Bin2Bn(bnR, r, rLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(BN_Bin2Bn(bnS, s, sLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_EAL_EncodeSign(bnR, bnS, vectorSign, vectorSignLen); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_SUCCESS; ERR: BSL_SAL_Free(r); BSL_SAL_Free(s); BN_Destroy(bnR); BN_Destroy(bnS); return ret; } static bool CRYPT_CMVP_SelftestEcdsaInternal(void *libCtx, const char *attrName) { bool ret = false; uint8_t *sign = NULL; uint8_t *signVec = NULL; uint32_t signLen; uint32_t signVecLen = 0; uint8_t *msg = NULL; uint32_t msgLen; CRYPT_EAL_PkeyCtx *pkeyPrv = NULL; CRYPT_EAL_PkeyCtx *pkeyPub = NULL; CRYPT_EAL_PkeyPub pub = { 0 }; CRYPT_EAL_PkeyPrv prv = { 0 }; CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); msg = CMVP_StringsToBins(ECDSA_VECTOR.msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(GetPkey(libCtx, attrName, &pkeyPrv, &pkeyPub, &pub, &prv) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); signLen = CRYPT_EAL_PkeyGetSignLen(pkeyPrv); sign = BSL_SAL_Malloc(signLen); signVecLen = signLen; GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); signVec = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(signVec == NULL, CRYPT_MEM_ALLOC_FAIL); // regist rand function CRYPT_RandRegist(TestVectorRandom); // sign GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(pkeyPrv, ECDSA_VECTOR.mdId, msg, msgLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); // compare the signature GOTO_ERR_IF_TRUE(SignEncode(ECDSA_VECTOR.signR, ECDSA_VECTOR.signS, signVec, &signVecLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(signLen != signVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(signVec, sign, signLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); // verify GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyVerify(pkeyPub, ECDSA_VECTOR.mdId, msg, msgLen, sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(pub.key.eccPub.data); BSL_SAL_Free(prv.key.eccPrv.data); BSL_SAL_Free(sign); BSL_SAL_Free(signVec); BSL_SAL_Free(msg); CRYPT_EAL_PkeyFreeCtx(pkeyPrv); CRYPT_EAL_PkeyFreeCtx(pkeyPub); CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); return ret; } bool CRYPT_CMVP_SelftestEcdsa(void) { return CRYPT_CMVP_SelftestEcdsaInternal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderEcdsa(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestEcdsaInternal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_ecdsa.c
C
unknown
8,267
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { const char *prv; const char *pub; const char *msg; const char *sign; CRYPT_MD_AlgId mdId; } CMVP_ED25519_VECTOR; // https://datatracker.ietf.org/doc/html/rfc8032.html#page-24 static const CMVP_ED25519_VECTOR ED25519_VECTOR = { .prv = "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb", .pub = "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c", .msg = "72", .sign = "92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e" "458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00", .mdId = CRYPT_MD_SHA512 }; static void FreeData(uint8_t *prv, uint8_t *pub, uint8_t *msg, uint8_t *expSign, uint8_t *sign) { BSL_SAL_Free(prv); BSL_SAL_Free(pub); BSL_SAL_Free(msg); BSL_SAL_Free(expSign); BSL_SAL_Free(sign); } static bool CRYPT_CMVP_SelftestEd25519Internal(void *libCtx, const char *attrName) { bool ret = false; uint8_t *prv = NULL; uint8_t *pub = NULL; uint8_t *msg = NULL; uint8_t *expSign = NULL; uint8_t *sign = NULL; uint32_t prvLen, pubLen, msgLen, expSignLen, signLen; CRYPT_EAL_PkeyCtx *prvCtx = NULL; CRYPT_EAL_PkeyCtx *pubCtx = NULL; CRYPT_EAL_PkeyPrv prvKey = {0}; CRYPT_EAL_PkeyPub pubKey; prv = CMVP_StringsToBins(ED25519_VECTOR.prv, &prvLen); GOTO_ERR_IF_TRUE(prv == NULL, CRYPT_CMVP_COMMON_ERR); pub = CMVP_StringsToBins(ED25519_VECTOR.pub, &pubLen); GOTO_ERR_IF_TRUE(pub == NULL, CRYPT_CMVP_COMMON_ERR); msg = CMVP_StringsToBins(ED25519_VECTOR.msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); expSign = CMVP_StringsToBins(ED25519_VECTOR.sign, &expSignLen); GOTO_ERR_IF_TRUE(expSign == NULL, CRYPT_CMVP_COMMON_ERR); signLen = expSignLen; sign = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); prvKey.id = CRYPT_PKEY_ED25519; prvKey.key.curve25519Prv.data = prv; prvKey.key.curve25519Prv.len = prvLen; prvCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_ED25519, 0, attrName); GOTO_ERR_IF_TRUE(prvCtx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(prvCtx, &prvKey) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(prvCtx, ED25519_VECTOR.mdId, msg, msgLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(signLen != expSignLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(expSign, sign, signLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); pubKey.id = CRYPT_PKEY_ED25519; pubKey.key.curve25519Pub.data = pub; pubKey.key.curve25519Pub.len = pubLen; pubCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_ED25519, 0, attrName); GOTO_ERR_IF_TRUE(pubCtx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(pubCtx, &pubKey) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyVerify(pubCtx, ED25519_VECTOR.mdId, msg, msgLen, sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: FreeData(prv, pub, msg, expSign, sign); CRYPT_EAL_PkeyFreeCtx(prvCtx); CRYPT_EAL_PkeyFreeCtx(pubCtx); return ret; } bool CRYPT_CMVP_SelftestEd25519(void) { return CRYPT_CMVP_SelftestEd25519Internal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderEd25519(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestEd25519Internal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_ed25519.c
C
unknown
4,464
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "crypt_params_key.h" #include "crypt_errno.h" #include "crypt_eal_kdf.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { const char *ikm; const char *salt; const char *info; const char *okm; CRYPT_MAC_AlgId id; } CMVP_HKDF_VECTOR; // https://datatracker.ietf.org/doc/html/rfc5869.html#appendix-A static const CMVP_HKDF_VECTOR HKDF_VECTOR = { .ikm = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", .salt = "000102030405060708090a0b0c", .info = "f0f1f2f3f4f5f6f7f8f9", .okm = "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865", .id = CRYPT_MAC_HMAC_SHA256 }; static bool CRYPT_CMVP_SelftestHkdfInternal(void *libCtx, const char *attrName) { bool ret = false; uint8_t *key = NULL; uint8_t *salt = NULL; uint8_t *info = NULL; uint8_t *expOut = NULL; uint8_t *out = NULL; uint32_t keyLen, saltLen, infoLen, expOutLen; CRYPT_EAL_KdfCTX *ctx = NULL; CRYPT_HKDF_MODE mode = CRYPT_KDF_HKDF_MODE_FULL; CRYPT_MAC_AlgId id = CRYPT_MAC_HMAC_SHA256; key = CMVP_StringsToBins(HKDF_VECTOR.ikm, &keyLen); GOTO_ERR_IF_TRUE(key == NULL, CRYPT_CMVP_COMMON_ERR); salt = CMVP_StringsToBins(HKDF_VECTOR.salt, &saltLen); GOTO_ERR_IF_TRUE(salt == NULL, CRYPT_CMVP_COMMON_ERR); info = CMVP_StringsToBins(HKDF_VECTOR.info, &infoLen); GOTO_ERR_IF_TRUE(info == NULL, CRYPT_CMVP_COMMON_ERR); expOut = CMVP_StringsToBins(HKDF_VECTOR.okm, &expOutLen); GOTO_ERR_IF_TRUE(expOut == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(expOut == NULL, CRYPT_CMVP_COMMON_ERR); out = BSL_SAL_Malloc(expOutLen); GOTO_ERR_IF_TRUE(out == NULL, CRYPT_MEM_ALLOC_FAIL); ctx = CRYPT_EAL_ProviderKdfNewCtx(libCtx, CRYPT_KDF_HKDF, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); BSL_Param param[6] = { {CRYPT_PARAM_KDF_MAC_ID, BSL_PARAM_TYPE_UINT32, &id, sizeof(id), 0}, {CRYPT_PARAM_KDF_MODE, BSL_PARAM_TYPE_UINT32, &mode, sizeof(mode), 0}, {CRYPT_PARAM_KDF_KEY, BSL_PARAM_TYPE_OCTETS, key, keyLen, 0}, {CRYPT_PARAM_KDF_SALT, BSL_PARAM_TYPE_OCTETS, salt, saltLen, 0}, {CRYPT_PARAM_KDF_INFO, BSL_PARAM_TYPE_OCTETS, info, infoLen, 0}, BSL_PARAM_END }; GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfSetParam(ctx, param) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfDerive(ctx, out, expOutLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(out, expOut, expOutLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(key); BSL_SAL_Free(salt); BSL_SAL_Free(info); BSL_SAL_Free(expOut); BSL_SAL_Free(out); CRYPT_EAL_KdfFreeCtx(ctx); return ret; } bool CRYPT_CMVP_SelftestHkdf(void) { return CRYPT_CMVP_SelftestHkdfInternal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderHkdf(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestHkdfInternal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_hkdf.c
C
unknown
3,825
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_kdf.h" #include "crypt_params_key.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { const char *key; const char *label; const char *seed; const char *dk; CRYPT_MAC_AlgId id; } CMVP_KDFTLS12_VECTOR; // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/components/800-135testvectors/tls.zip static const CMVP_KDFTLS12_VECTOR KDF_TLS12_VECTOR = { .key = "202c88c00f84a17a20027079604787461176455539e705be730890602c289a5001e34eeb3a043e5d52a65e66125188bf", .label = "6b657920657870616e73696f6eae6c806f8ad4d80784549dff28a4b58fd837681a51d928c3e30ee5ff14f3986862e1fd91", .seed = "f23f558a605f28478c58cf72637b89784d959df7e946d3f07bd1b616", .dk = "d06139889fffac1e3a71865f504aa5d0d2a2e89506c6f2279b670c3e1b74f531016a25" "30c51a3a0f7e1d6590d0f0566b2f387f8d11fd4f731cdd572d2eae927f6f2f81410b25" "e6960be68985add6c38445ad9f8c64bf8068bf9a6679485d966f1ad6f68b43495b10a6" "83755ea2b858d70ccac7ec8b053c6bd41ca299d4e51928", .id = CRYPT_MAC_HMAC_SHA256 }; static bool CRYPT_CMVP_SelftestKdfTls12Internal(void *libCtx, const char *attrName) { bool ret = false; uint8_t *key = NULL; uint8_t *label = NULL; uint8_t *seed = NULL; uint8_t *expDk = NULL; uint8_t *dk = NULL; uint32_t keyLen, labelLen, seedLen, expDkLen; CRYPT_EAL_KdfCTX *ctx = NULL; CRYPT_MAC_AlgId id = CRYPT_MAC_HMAC_SHA256; key = CMVP_StringsToBins(KDF_TLS12_VECTOR.key, &keyLen); GOTO_ERR_IF_TRUE(key == NULL, CRYPT_CMVP_COMMON_ERR); label = CMVP_StringsToBins(KDF_TLS12_VECTOR.label, &labelLen); GOTO_ERR_IF_TRUE(label == NULL, CRYPT_CMVP_COMMON_ERR); seed = CMVP_StringsToBins(KDF_TLS12_VECTOR.seed, &seedLen); GOTO_ERR_IF_TRUE(seed == NULL, CRYPT_CMVP_COMMON_ERR); expDk = CMVP_StringsToBins(KDF_TLS12_VECTOR.dk, &expDkLen); GOTO_ERR_IF_TRUE(expDk == NULL, CRYPT_CMVP_COMMON_ERR); dk = BSL_SAL_Malloc(expDkLen); GOTO_ERR_IF_TRUE(dk == NULL, CRYPT_MEM_ALLOC_FAIL); ctx = CRYPT_EAL_ProviderKdfNewCtx(libCtx, CRYPT_KDF_KDFTLS12, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); BSL_Param param[5] = { {CRYPT_PARAM_KDF_MAC_ID, BSL_PARAM_TYPE_UINT32, &id, sizeof(id), 0}, {CRYPT_PARAM_KDF_KEY, BSL_PARAM_TYPE_OCTETS, key, keyLen, 0}, {CRYPT_PARAM_KDF_LABEL, BSL_PARAM_TYPE_OCTETS, label, labelLen, 0}, {CRYPT_PARAM_KDF_SEED, BSL_PARAM_TYPE_OCTETS, seed, seedLen, 0}, BSL_PARAM_END }; GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfSetParam(ctx, param) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfDerive(ctx, dk, expDkLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(dk, expDk, expDkLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(key); BSL_SAL_Free(label); BSL_SAL_Free(seed); BSL_SAL_Free(expDk); BSL_SAL_Free(dk); CRYPT_EAL_KdfFreeCtx(ctx); return ret; } bool CRYPT_CMVP_SelftestKdfTls12(void) { return CRYPT_CMVP_SelftestKdfTls12Internal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderKdfTls12(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestKdfTls12Internal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_kdftls12.c
C
unknown
4,134
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_mac.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" #include "crypt_local_types.h" #define MAC_TYPE_HMAC_CMAC 1 #define MAC_TYPE_GMAC 2 #define MAC_TYPE_CBC_MAC 3 typedef struct { uint32_t id; const char *key; const char *msg; const char *mac; const char *iv; uint32_t type; } CMVP_MAC_VECTOR; static const CMVP_MAC_VECTOR MAC_VECTOR[] = { // CRYPT_MAC_HMAC_MD5 // https://datatracker.ietf.org/doc/html/rfc2202 { .id = CRYPT_MAC_HMAC_MD5, .key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", .msg = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD", .mac = "56be34521d144c88dbb8c733f0e8b3f6", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_HMAC_SHA1 // https://datatracker.ietf.org/doc/html/rfc2202 { .id = CRYPT_MAC_HMAC_SHA1, .key = "0102030405060708090a0b0c0d0e0f10111213141516171819", .msg = "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd", .mac = "4c9007f4026250c6bc8414f9bf50c86c2d7235da", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_HMAC_SHA224 // https://datatracker.ietf.org/doc/html/rfc4231.html#page-4 { .id = CRYPT_MAC_HMAC_SHA224, .key = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", .msg = "4869205468657265", .mac = "896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_HMAC_SHA256 // https://datatracker.ietf.org/doc/html/rfc4231.html#page-4 { .id = CRYPT_MAC_HMAC_SHA256, .key = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", .msg = "4869205468657265", .mac = "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_HMAC_SHA384 // https://datatracker.ietf.org/doc/html/rfc4231.html#page-4 { .id = CRYPT_MAC_HMAC_SHA384, .key = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", .msg = "4869205468657265", .mac = "afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_HMAC_SHA512 // https://datatracker.ietf.org/doc/html/rfc4231.html#page-4 { .id = CRYPT_MAC_HMAC_SHA512, .key = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", .msg = "4869205468657265", .mac = "87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833" "b7d6b8a702038b274eaea3f4e4be9d914""eeb61f1702e696c203a126854", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_HMAC_SM3 { .id = CRYPT_MAC_HMAC_SM3, .key = "00112233445566778899aabbccddeeff", .msg = "6162636465666768696A6B6C6D6E6F707172737475767778797A", // abcdefghijklmnopqrstuvwxyz .mac = "a51ce58c52ae29edd66a53e6aaf0745bf4fedbde899973b2d817290e646df87e", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_CMAC_AES128 // https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/cavp-testing-block-cipher-modes#CMAC { .id = CRYPT_MAC_CMAC_AES128, .key = "5fcad38ae778394912f8f1b8413cf773", .msg = "07185502bf6d275c84e3ac4f5f77c3d4", .mac = "fd44fbc0dd9719e8b569ff10421df4", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_CMAC_AES192 // https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/cavp-testing-block-cipher-modes#CMAC { .id = CRYPT_MAC_CMAC_AES192, .key = "7fea563a866571822472dade8a0bec4b98202d47a3443129", .msg = "a206a1eb70a9d24bb5e72f314e7d91de074f59055653bdd24aab5f2bbe112436", .mac = "3bfe96f05e9cf96a98bd", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_CMAC_AES256 // https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/cavp-testing-block-cipher-modes#CMAC { .id = CRYPT_MAC_CMAC_AES256, .key = "7bef8d35616108922aab78936967204980b8a4945b31602f5ef2feec9b144841", .msg = "40affd355416200191ba64edec8d7d27ead235a7b2e01a12662273deb36379b8a748c422c31e046152d6f196f94e852b", .mac = "b2d078071e318ec88de9", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_CMAC_SM4 // GB/T 15852.1-2020 B.6 { .id = CRYPT_MAC_CMAC_SM4, .key = "0123456789ABCDEFFEDCBA9876543210", .msg = "54686973206973207468652074657374206d65737361676520666f72206d6163", // "This is the test message for mac" .mac = "692c437100f3b5ee2b8abcef373d990c", .iv = NULL, .type = MAC_TYPE_HMAC_CMAC }, // CRYPT_MAC_GMAC_AES128 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/gcmtestvectors.zip { .id = CRYPT_MAC_GMAC_AES128, .key = "bea48ae4980d27f357611014d4486625", .msg = "8a50b0b8c7654bced884f7f3afda2ead", .mac = "8e0f6d8bf05ffebe6f500eb1", .iv = "32bddb5c3aa998a08556454c", .type = MAC_TYPE_GMAC }, // CRYPT_MAC_GMAC_AES192 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/gcmtestvectors.zip { .id = CRYPT_MAC_GMAC_AES192, .key = "41c5da8667ef725220ffe39ae0ac590ac9fca729ab60ada0", .msg = "8b5c124bef6e2f0fe4d8c95cd5fa4cf1", .mac = "204bdb1bd62154bf08922aaa54eed705", .iv = "05ad13a5e2c2ab667e1a6fbc", .type = MAC_TYPE_GMAC }, // CRYPT_MAC_GMAC_AES256 // https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/mac/gcmtestvectors.zip { .id = CRYPT_MAC_GMAC_AES256, .key = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223", .msg = "b96baa8c1c75a671bfb2d08d06be5f36", .mac = "3e5d486aa2e30b22e040b85723a06e76", .iv = "d79cf22d504cc793c3fb6c8a", .type = MAC_TYPE_GMAC }, // CRYPT_MAC_CBC_MAC_SM4 // GB/T 15852.1-2020 B.2 { .id = CRYPT_MAC_CBC_MAC_SM4, .key = "0123456789ABCDEFFEDCBA9876543210", .msg = "54686973206973207468652074657374206d65737361676520666f72206d6163", // "This is the test message for mac" .mac = "16e02904efb765b706459c9edabdb519", .iv = NULL, .type = MAC_TYPE_CBC_MAC }, { .id = CRYPT_MAC_MAX, .key = NULL, .msg = NULL, .mac = NULL, .iv = NULL, .type = 0 } }; static void FreeData(uint8_t *key, uint8_t *msg, uint8_t *mac, uint8_t *expectMac, uint8_t *iv) { BSL_SAL_Free(key); BSL_SAL_Free(msg); BSL_SAL_Free(mac); BSL_SAL_Free(expectMac); BSL_SAL_Free(iv); } static bool GetData(const CMVP_MAC_VECTOR *macVec, CRYPT_Data *key, CRYPT_Data *msg, CRYPT_Data *expectMac, CRYPT_Data *iv) { key->data = CMVP_StringsToBins(macVec->key, &(key->len)); GOTO_ERR_IF_TRUE(key->data == NULL, CRYPT_CMVP_COMMON_ERR); msg->data = CMVP_StringsToBins(macVec->msg, &(msg->len)); GOTO_ERR_IF_TRUE(msg->data == NULL, CRYPT_CMVP_COMMON_ERR); expectMac->data = CMVP_StringsToBins(macVec->mac, &(expectMac->len)); GOTO_ERR_IF_TRUE(expectMac->data == NULL, CRYPT_CMVP_COMMON_ERR); if (macVec->iv != NULL) { iv->data = CMVP_StringsToBins(macVec->iv, &(iv->len)); GOTO_ERR_IF_TRUE(iv->data == NULL, CRYPT_CMVP_COMMON_ERR); } return true; ERR: return false; } const CMVP_MAC_VECTOR *FindMacVectorById(CRYPT_MAC_AlgId id) { uint32_t num = sizeof(MAC_VECTOR) / sizeof(MAC_VECTOR[0]); const CMVP_MAC_VECTOR *macVec = NULL; for (uint32_t i = 0; i < num; i++) { if (MAC_VECTOR[i].id == id) { macVec = &MAC_VECTOR[i]; return macVec; } } return NULL; } static bool CRYPT_CMVP_SelftestMacInternal(void *libCtx, const char *attrName, CRYPT_MAC_AlgId id) { bool ret = false; CRYPT_EAL_MacCtx *ctx = NULL; CRYPT_Data key = { NULL, 0 }; CRYPT_Data msg = { NULL, 0 }; CRYPT_Data iv = { NULL, 0 }; uint8_t *mac = NULL; uint32_t macLen = 0; CRYPT_Data expectMac = { NULL, 0 }; const CMVP_MAC_VECTOR *macVec = FindMacVectorById(id); // HMAC-MD5 is a non-approved algorithm and does not provide self-test. if (macVec == NULL || macVec->msg == NULL || macVec->id == CRYPT_MAC_HMAC_MD5) { return false; } GOTO_ERR_IF_TRUE(!GetData(macVec, &key, &msg, &expectMac, &iv), CRYPT_CMVP_ERR_ALGO_SELFTEST); ctx = CRYPT_EAL_ProviderMacNewCtx(libCtx, id, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (macVec->type == MAC_TYPE_GMAC) { macLen = expectMac.len; } else { macLen = CRYPT_EAL_GetMacLen(ctx); } mac = BSL_SAL_Malloc(macLen); GOTO_ERR_IF_TRUE(mac == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_MacInit(ctx, key.data, key.len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (macVec->type == MAC_TYPE_GMAC) { GOTO_ERR_IF_TRUE(CRYPT_EAL_MacCtrl(ctx, CRYPT_CTRL_SET_IV, iv.data, iv.len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_MacCtrl(ctx, CRYPT_CTRL_SET_TAGLEN, &macLen, sizeof(uint32_t)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } if (macVec->type == MAC_TYPE_CBC_MAC) { CRYPT_PaddingType padType = CRYPT_PADDING_ZEROS; GOTO_ERR_IF_TRUE(CRYPT_EAL_MacCtrl(ctx, CRYPT_CTRL_SET_CBC_MAC_PADDING, &padType, sizeof(CRYPT_PaddingType)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(CRYPT_EAL_MacUpdate(ctx, msg.data, msg.len) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_MacFinal(ctx, mac, &macLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(mac, expectMac.data, expectMac.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: FreeData(key.data, msg.data, mac, expectMac.data, iv.data); CRYPT_EAL_MacDeinit(ctx); CRYPT_EAL_MacFreeCtx(ctx); return ret; } bool CRYPT_CMVP_SelftestMac(CRYPT_MAC_AlgId id) { return CRYPT_CMVP_SelftestMacInternal(NULL, NULL, id); } bool CRYPT_CMVP_SelftestProviderMac(void *libCtx, const char *attrName, CRYPT_MAC_AlgId id) { return CRYPT_CMVP_SelftestMacInternal(libCtx, attrName, id); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_mac.c
C
unknown
11,545
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_md.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { uint32_t id; const char *msg; const char *md; } CMVP_HASH_VECTOR; static const CMVP_HASH_VECTOR HASH_VECTOR[] = { // CRYPT_MD_MD5 { .id = CRYPT_MD_MD5, .msg = NULL, .md = NULL }, // CRYPT_MD_SHA1 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA1, .msg = "7e3d7b3eada98866", .md = "24a2c34b976305277ce58c2f42d5092031572520" }, // CRYPT_MD_SHA224 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA224, .msg = "5f77b3664823c33e", .md = "bdf21ff325f754157ccf417f4855360a72e8fd117d28c8fe7da3ea38" }, // CRYPT_MD_SHA256 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA256, .msg = "5738c929c4f4ccb6", .md = "963bb88f27f512777aab6c8b1a02c70ec0ad651d428f870036e1917120fb48bf" }, // CRYPT_MD_SHA384 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA384, .msg = "de60275bdafce4b1", .md = "a3d861d866c1362423eb21c6bec8e44b74ce993c55baa2b6640567560ebecdaeda07183dbbbd95e0" "f522caee5ddbdaf0" }, // CRYPT_MD_SHA512 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA512, .msg = "6f8d58b7cab1888c", .md = "a3941def2803c8dfc08f20c06ba7e9a332ae0c67e47ae57365c243ef40059b11be22c91da6a80c2c" "ff0742a8f4bcd941bdee0b861ec872b215433ce8dcf3c031" }, // CRYPT_MD_SHA3_224 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA3_224, .msg = "d85e470a7c6988", .md = "8a134c33c7abd673cd3d0c33956700760de980c5aee74c96e6ba08b2" }, // CRYPT_MD_SHA3_256 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA3_256, .msg = "8bca931c8a132d2f", .md = "dbb8be5dec1d715bd117b24566dc3f24f2cc0c799795d0638d9537481ef1e03e" }, // CRYPT_MD_SHA3_384 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA3_384, .msg = "c44a2c58c84c393a", .md = "60ad40f964d0edcf19281e415f7389968275ff613199a069c916a0ff7ef65503b740683162a622b913d43a46559e913c" }, // CRYPT_MD_SHA3_512 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHA3_512, .msg = "af53fa3ff8a3cfb2", .md = "03c2ac02de1765497a0a6af466fb64758e3283ed83d02c0edb3904fd3cf296442e790018d4bf4ce55bc869" "cebb4aa1a799afc9d987e776fef5dfe6628e24de97" }, // CRYPT_MD_SHAKE128 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHAKE128, .msg = "7bf2fef375bcaff3", .md = "5ef5578b89c50532131b7843de7329a3" }, // CRYPT_MD_SHAKE256 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Secure-Hashing#shavs { .id = CRYPT_MD_SHAKE256, .msg = "587cb398fe82ffda", .md = "54f5dddb85f62dba7dc4727d502bdee959fb665bd482bd0ce31cbdd1a042e4b5" }, // CRYPT_MD_SM3 { .id = CRYPT_MD_SM3, .msg = "616263", .md = "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0" }, { .id = CRYPT_MD_MAX, .msg = NULL, .md = NULL } }; static void FreeData(uint8_t *msg, uint8_t *md, uint8_t *expectMd) { BSL_SAL_Free(msg); BSL_SAL_Free(md); BSL_SAL_Free(expectMd); } const CMVP_HASH_VECTOR *FindVectorById(CRYPT_MD_AlgId id) { const CMVP_HASH_VECTOR *pHashVec = NULL; uint32_t num = sizeof(HASH_VECTOR) / sizeof(HASH_VECTOR[0]); for (uint32_t i = 0; i < num; i++) { if (HASH_VECTOR[i].id == id) { pHashVec = &HASH_VECTOR[i]; return pHashVec; } } return NULL; } static bool CRYPT_CMVP_SelftestMdInternal(void *libCtx, const char *attrName, CRYPT_MD_AlgId id) { bool ret = false; uint8_t *msg = NULL; uint8_t *md = NULL; uint8_t *expectMd = NULL; uint32_t msgLen, mdLen, expectMdLen; CRYPT_EAL_MdCTX *ctx = NULL; const CMVP_HASH_VECTOR *hashVec = FindVectorById(id); if (hashVec == NULL || hashVec->msg == NULL) { return false; } msg = CMVP_StringsToBins(hashVec->msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); expectMd = CMVP_StringsToBins(hashVec->md, &expectMdLen); GOTO_ERR_IF_TRUE(expectMd == NULL, CRYPT_CMVP_COMMON_ERR); ctx = CRYPT_EAL_ProviderMdNewCtx(libCtx, id, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (id == CRYPT_MD_SHAKE128 || id == CRYPT_MD_SHAKE256) { mdLen = expectMdLen; } else { mdLen = CRYPT_EAL_MdGetDigestSize(id); } md = BSL_SAL_Malloc(mdLen); GOTO_ERR_IF_TRUE(md == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_MdInit(ctx) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_MdUpdate(ctx, msg, msgLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_MdFinal(ctx, md, &mdLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(mdLen != expectMdLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(expectMd, md, mdLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: FreeData(msg, md, expectMd); CRYPT_EAL_MdFreeCtx(ctx); return ret; } bool CRYPT_CMVP_SelftestMd(CRYPT_MD_AlgId id) { return CRYPT_CMVP_SelftestMdInternal(NULL, NULL, id); } bool CRYPT_CMVP_SelftestProviderMd(void *libCtx, const char *attrName, CRYPT_MD_AlgId id) { return CRYPT_CMVP_SelftestMdInternal(libCtx, attrName, id); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_md.c
C
unknown
7,164
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "crypt_eal_rand.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_util_rand.h" #include "crypt_algid.h" #include "securec.h" #include "bsl_sal.h" #define BITS_OF_BYTE 8 typedef struct { const char *rnd; const char *sk; const char *pk; const char *msg; const char *sig; int32_t algId; int32_t type; } CMVP_MldsaVector; static const CMVP_MldsaVector MLDSA_VECTOR[] = { { // https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files/ML-DSA-sigGen-FIPS204 .rnd = "1D073BE3C59909102F6C26537E61E56CDB37FE1167E092DC3B73067E1B43B2A2", .sk = "C5459B7F833D8862B24945270793A9FA036D1C1CFB9AF2048FD6A53A5194ADB1FC6055E75441771C6E34EDF641C5B92DC33D5987ED44059728D873D6A3156BE51F5DCFBBADE70683E03D0605F365199AAF9A045564760FCDE95BF7C65868606D281BBF74E23A8739B39E2E34E0C3B6C254C3B6D3FC26F3F5D3A618070E7A54439B2845E4280593A22C88C42842C65009A369023806441621982262430065A1B8484194909BC06193164182A0848B9401114466C1468E6004801A45712433861A4605C14291D28841C2B22C53A0419842854B844159449150C610521861E088115C484252C869020330C9006484162901C1289930280880255C480AA3A6919B188460089104884D0949309CC26540122810470C0446494BA2901B028402472900B044190842DBC841A0A66583C46D93046950461023B88800157089064C2288110218891CC8454C30728AC20DC80049144205084230E2865123002062140DCCB08083A86C11239283A80488C291A0068A2024115B0828D1C4215380702384648394904B229122C5700A1592E08085184865E3206A4112300A268C0B354E14944404401010260280B801CA4650C1C80420C20180122290A420D31244C3863113047209978CA1C64113B12DDC362642289183B46810170CD922904B104C63068294866CD31285091240C1209199124CD8464043B88184103202098EDC28291C405023C99118056C62B031CBA2688B286112450610248E04444151101114178CC004112230214C802C8AB4904C28119038264BC46409264C1015500416491A8708942406C4A220011792E030702033295194515AB08589120624C304088165D43470C8404A203252A0948C9B142112476564B88962321284388C11891091144193982CE300729990500AA4606080044B346C01070648A48144000EA4364D03142C1817720A190013868414841040800102A22CD23232C4C41018C93149C02162B064DB002D04A421C190694A384C1385211A2988A0228E8CC4314A044C92907091B8680B29259930200CB7651A238A0241420B158CA484249B420114C585A304922228220A0389DC269192128C840601D036302404288BB88409112EC9121223120954284410090224965001878492B45040B649A486290C9610A41269132648A0366E9080690B02489C2049C4384918280D0429651A4964E23649C08485D0845160460C8CB04C1A9690E1340EC9220513494A51A845180168C4A0898812024CC0883324F1662FFA6CA7BDA4C113CCBD9B4D696EBA9A7F1B12DCCAD143D2FB35669A47E89432B51B4AACC086720CAD29D5AAF2A9290F5D3E4980B92BB6B82A0C07D46791FE0931CDDAF181DE924F4430DC0E02E901C73A0E4FD6539529DC7E4F98D8AEDCF68E3D1EFC0411A55EC05464C64F098199F1EF0846800DB2B7B4F116137237D12C214FA0B9C344ACAC5B684F76C7962CA1097DBE39F01ADEA74228A14D67B2FD891F41A010EB34CCA36A4AFE1F7A7BF23FC6CDD333161ABB3317D23E4863580883572647F80739748A6DCF4677CDAB621B0085F3C5A965A1BAA2881E9966651DFC6F9EF23BF08F0E7A9291A924B2BEBEA44D3ED6D9A5442A3897457C4A24C52DDE62CF03A413BA4F1CFFF5D7AD6F118C3749414D1ACDEEAFB14793F1609CE885B9856E10076F18A6106430610797802E1617AB1F066520A5487AB980AA928382EC2831A1FC873D9630BD21336540CC6AD23BE49BD18515A8568993C9B4525C42290AD4607E17DAAE37BAC65B82CF3524B22B3F7A2DAD9B42AA7E45EC27857F92C3288DF7734C4A1C8B2CB6990DF2D8EA92BF75F9E68797742240F5322FA26F0069675CA96F0DA7983960E71E74F285CE7FCEAB33F9FBBF52636771E537AB608EAC188F0112A6E579AAC2E519440D89FD43D313E4EAE4432E89587FCCFC487AB43B33F66F0885B7CCEDB2BD65EA23A4EA0ED266A3B506268DFA363DD792CF358FAC0BD80F77271F73FC3057ADA74FF4BDFABAC429D29846E3F1F2C64FF79B3FDC2D1B36684ADE97D5465494DE92B7657B58FD8F51731F30447A54C0309607798A4213735B61453D77B3C38F76182253172BCF1076D974D25F1A006A743DB61B4B34375E04A54FAF059A7F95FE3E7569A5BDCCBE09C1E31B17F92C47D1A5254B3BA346F0FDB42D2DC70B377D4E1347ACB30556C9BD24CAB617143503F1316C58B3A6B19B39342BCA9A0F9BA7D0A835216603F43212865A97DA9F495E59F273BCCED6E0A3BE990A6D5E13769F1AC4FC1FDE3F6865CE3B554679DF2C59121DC3F6CF1EB538AC0CC1EB66AAB155007D8B5369AFBAC0108B1F2F7B922C87E7C4D3E47B689F8A43F8CA8D7FC8A4C5BC037F6CCF6F3C878CFCA557ABE13019AC989155C7F5B73C045C1EB2C117BD3FFD7EA606B78DFFF56491BB9BD27499D00A539CBD5466F421D4BD27C8E669803E75297519EE509C1B52FDADBE141727FC55ECA7ACA14A52E9956F06F738FB9BC806D7EFA5BFF974AFF0F75CCE5F5FE7C998F883C8BEF882F8885D5243ACF262AF6CE9BFE1CEB390DB3C2DB268BEE42143B8FE8613EAE0A0D8E2AA8C137E46A9AA9C89371998A668CE58308720917B955BABA608177AAA17233412A1782D86BD12488A026B7F9FD44AE385C521B20144646319730B7699BAF8BF7246587EC2E33A7E5FD4E0A220C619B0A7B52527EC54C7BA7C40060C18C82EAA6F27CBFF30FB5DD7C9F4A2077078C16AD3DB0216AA965D250F7195B0BF338323E7EBCA4E18D1C4F80AD6FE81E38B256262D6E29885137205C148B15DFBEDEFCC4B58EF8EA017F5704F3E822DF785C4A33898F78A9F6D07FE1A02807500A646984A1EB1DA480D7AF4607F316E9459CB57EBA7F5E829F9EAE74C1A4A1AB1C9A0C9569208F5549B64391A7D9829715EA1E4A325C552A100D38DCC105EA11BF9B0BD8709D6EA67592D1DBAA4E2A541E0B6A1667CCD3610BFE73E477DA88050B0F037FE2F74B23D2342DDD2ABABF358748FA191255ACF82E863AC2234BC4549EA38E780FB65153CD83C2837B982FAC27B775DBD67953077DD4A219707A2BBAE8E2949FA74E1E41E44AF82E30471E656BD20358C96275ABAF7B547B52AA57FAB9245B6BD1CBDC5036BC53D75E3A09DAC5F16C4B321DD943B61954BFFB346ECD222E904D478D22D3BF886B680DA368FE5F74930B7B76B3FAB5EBA3FCE1A95F344D218AD8CC5236EB5F497799C2C99942007868CE8AC366477744BC5C1621569512EDCAACC1C9E282DA11098511A4191D8796D033F395E460E0E6213C7908077E71DCD8E2906A3D8C6583566077732DA25C8115B7260E1E2E936247622648CE8E0634852349CEBDF495C729434317633786399B18499CA013DA06FAB275EBEB8F78A58ED8F65B72E5E2E212D4D63595E0108976E8792C18A046A058B986E29BAC259D921E4743F4CF53BF3FF9FFD7B928F19EFB44F4657A8E93B1FAC23C0CF64A1630478D11C313F0CAEEE51787D30AEE34531E4C6A90D62C34EB7D5FE31C451C12879C97636F65C4AE47D05AC8109DA2589D5402402A5B09EDA9B3BA07723E38CBB4B5ED847601F348CE4B3A8D11916E617389160E98C997255D4CAB8DC", .pk = "C5459B7F833D8862B24945270793A9FA036D1C1CFB9AF2048FD6A53A5194ADB1B691D76050141493236DF39767EC9E2CDBAAF50613C38541E39F77E1415FF9BA5AD22EEDCFA2C69514ACC1FB9C8A3BD2D55E20E7624256E06A589D1C38EA2641723E9FA8A45A546050A266073F6977B5801DF8C4CF2DCD6965BD032C5D07269AD534CDD25495E574800E36733EAB8CB473163608097C271F8A75E9C4CBCE5170EA05CF4FD41288C4A57A6FB6685CC9B05FAEE90E674053270A8D700EE4313705DF5C14DA0A8D05CFD3807F3137C2F272D2A2DC18E7EC8A8E155E52A3974F413ACA3C3914C552CBD4CC36CD3F71FE8DEE522CD65FE4BFF2B51CE01DBAE4890D8B298995D159F60289277F77E9A8EA9DC1793E92730F4CA3514A9E2D0094123863F050393AF9932954A7AB58872EE333E522D87CDCF560C39DA2FF5750E0D85A6A81BCA8D42C6F3A08BA4A6D9B76385059030B1A131FDFF04F749D5AF87433FC05E6D9D580C832FF75F409BF9022EEAF8168A8B2ACCCA33BBC5FE1BDB44268EE77A39475439DADEFE6368CA1D1578EDCB5D3E5B862FAE157ED07584D1BDFADB71E31344FE51CF65A1D21D9037E876CC887FC30498CA6A54ADF7A4BCE24AE42C4C56AC29691A332108DBD00C4C368653B109D998C4B05BCDEB7C86E9CAEE0D1D55F2EE46ACAEA41AF1BFF43A479D9C928DEFDF99B009CE3B08AE4CA9CADAA407138B82F80766D9E648440D012404056B187DF2B19D5C263B0767C7A1FAB17D0E94F2FB342A9D3C98F6BA4D5EBE75264ADFDC4F167B9DA5D36FA3FD419007D0BC8CD1DBB7E3E897D9D03F6D2F674B3E4C7370ABCA10D91C7FCB13866866E254D991AAE86E917C78EFFADB02E88CD73F5CF47D54C18171828FDE585FBA2608B803D2623F466F3802A46201C67EACB811B139CAA240873D3E28CC7A9E71E3B3E3CCE1B98BD31E8DB83E72F7ECC2E4F4D93004DE855E6AD89E9A0C72279B67EF351E944CBA0BACC6FE31778209B13D9ED4D2AACFD054BB50BD8E379CF86678399991B31B3C179AADAC600DEA359787CB0A16F5EE911E5653E8E0CCF75FFEC9B1240928E1AD95F9630639AA20A529D27A5ACCCDA87FB2ECE63D42D50FBE70C69140CF3E6623E185EAEB219DEB1B6F0BBDC45587B46F7908B07E363A8EFDB4C0FE7881628A7F885054E43A1854DBFFD574A92C7318F1467441F554644BEC8F2C4F090678C0CF79B7CAC3800BAD6AC459FD8A87C2E410762DB26EC6C202CA996F3DF1B718B8E2C3DCDF90F61F9B97C9998B0E13B84D15D6C2ECA3BBBDF47E0CE53971A952B66915EA0FFA1D47B22F826FF51E1AD6E47C8FFAC640A8BCFD8B1E8E81A1D3AD4275552CDFE495464E32182E6FC707F41846325F6B53DCB2EAC2A5B3E5B84D6449681E9220642382FDE5F1479953E0C4DBB52B84258EB5E5847DFC527799BE27052BC19F3D769B48F8CDA7A3BB5414FC1232B66C8585F6AA2DD15978F1D0CDA59D03E13D8DBA6EBE1BD7E05D7AB0E429A156425E99DD8CC548C1B855810D3CBE0F599DC2FDFCF50C19FEBBCD6C2CD3BB98315C3016FAECC27FF0C7FE41F369511EC69BAAAFB1306B962A10A1A4BEDE822C86912A9453DAA30C7C1BD05038BE97E382651CB67C33B76CCFD33E1DBC3AE5149C485DFE2115779183DE801FEA99ECBBC069720D10E48C7437D9152030D48193898BAB2CCBCD363ED68FE5C74DA50D301CE7E4327BD387982C6A9C295F9EBD7F5764F009400F56BEDB8FAC01B3E4A33EB85AEA353906BB6B2CBC3EEA650EE428F40B969A193F99E7F2A40F71347066D16D29F33D364F8499F8EA96A1BA68E6CDB30F7909E889E4DB804A201B516EBB2", .msg = "73E28480E96EB5203E1E6DA81B3E433D07A4D5E1BDE100A7B97381EEEF61B73B736F55370B7457AFBFF669607C954C101DF1D8E4AB7D72C91A90F4401D7516A87FE93E684E602FE088805E30AA1A37AEB49F6306093625B1D7A4983B54DF301D26137BB44301B081F7E21AD02F4A6DB2CC48204E933DBCFA9D8169FEE227C2A3518D7B17CD03F7D4EDF644C530C33341A27A62178382ABA22B48B83AD59D783BA84B94628B102D2E60A3D03EE0E74255BC3F9C5A636BEEE5F00727FA325777BE3524AD7319F0803FD5C58272679BA95343EA924D5CAD6056BFAD520C4F03F4E31538D9893B85404E8CF908C3C1AE92421377FE273A8DF60FAEE8F9593E0C38DE894420FADEBDCA4112CCDA53BC0E3EB806911888E17E273533B84705C7521A664CC5D2006699D42CFFE0A561C22AC7F5EEC0AC597384A35B448694B2E9CA21B72E9D7D84042D78B3EA8B0D85D96BBCC76A30D0E90A4903C949D62DDE4DBEB70C657FCD7EF23225BEA9B7A2774EEC317165E271A1D78736FB1B6FB629EB216B8C035B720BDC21AADCD1A77041441F39FFFF1800B84BBA68D78AA9A09C0EF66619F8E69688EC3189669ABC5C3F5FC1A5BB0BC420513B637399E45F3C668744982DE6CF02E9DB45C4206A3C1CBE544C37C71027E0A7234CE9FBCF72CDB151F4A2B67BB08C9688B5E3EC6C449E255F430CB1118E5B2AFB14DE72668B9EBE31306409627B0260FE48566B51DCBB01BF4F876E31FFE2B6FD04327D118ADEDF2135C827465E75E7B23DB1F344582F2E91EFFD446ABFF3FA58AA67D763B49EAF3C9AC4810A4CF1BD94F22E25FA3A6065DC9631B0391A7AE5B6D374371B09D9E162AB2EA5BE0BC4C3A0365FE4A4AACDCCE283E13C95005A6A40828CCE1091F3F3D7D3CFC70742E9BDD2C7B09485BA6742A284C90FFCB0B4317336E8A7C9978B50CAE49F0CEC6F51AB93DDED4A32CDA1282955669BE2BED776DFBC3758EE7C9BC011E36226E390980820ACCED42794EDAB8ED62364579D342C0A829C8577715761B215DF20782FFF4CBABD3817EBE2F97C41AFCA46DE4DD5F103DB56E2DD53C559C3BAB720112F5A591D02B22D8E3807616479D83DCB1B1B6935F04FD3891C4F6CEAC74FF3445DF51AED9661FAB70BE153AEDFFF2CC0B02012ADFB7E064A48EF7FFC6A386F8BB64164A599EEEC2DC965B4F7991C799C4BD72336DAAA85C29BB3DC828FC31DBFD8EDCA599B7797D457ED588140A2FE24720FC087F84878819AFF1D30C2431B21E7F4999740EBAB0B5E1C7F532B20C1B9E49FC6AAE504B472850B444683F6F0D64084A1025BE2C338BAA58BADBAFE8CDE46A026B31A5BA588F7F57A032465B1EAC93951B8FF08590B52E1068B8DAB68FF0F68AC34E0B910923CA10AB5ED151AC131195923FD9C47F3D5076D2AEEE22902AB68193E26E203225C4F5446FB2F12BF0CF56DB7AD146728A6D316E4DC6DE291E706E400D526B69FD0063765C4EDC6EBEC4998D07411089068EF64DD81D42994B51934C21AFF09A4C5E2C8B6A4D0180AB098B8050197AB1BC8FFFFDECA7C8198BDDB36DDF87784A54E99681F2CD19068CE2E59026CE66FEBF72C399FA4573E2E01F5873438E6390A9B25EB7083A248019FA33608AA838E44106FC432811FCA3A0AEBC274D32F0B05370486FB2B805ABB5EBABE6E914819C36A5A6444EB710E212016F1DE5DA7BC5D12C851C347F89F75B3B0A39041EB51FBC91F1C908D1DCD88C8CDF3CAF50E6B024BFF5AE344E3E386EAF162B9FAD6EE14E1E6ECC9A9085D6CAB95A72B7E896615699E672F0032E2B6E8C598EB8A791E6FF872545C23D4A0B22F5A162699CC4C82DCEC321946E78636420F9D4ABA8DC7C2BC40F7CB0C43029CB7AD17866924C586105278523BDBAFF03D619F2571586046E17F4B5E535DD5553F6EF4F5D7C497E2350EA31AD4A941089F4A28E298410AABA3EE4B0007FA95B36D4CFB7CE34E3A654338667B0303352C7F5259E81143A13AEBD7441229DA1A9B8AA61E09D5C05AF83339263A7BF582E80074DB2296205A8C56D13D3D476E9E6973E27186EB36A15A75F0096CD0F70C28158C80C8448E7EFC2D0A3F771BD0E93DA5F632871E9774813D20B0151D88969548590E31AB445FA56A901E5FB7DE7A250526F4678D548223BD2C0CA433E88C4B6AF75596F5EF63995DC0DCA0BA31AE49C4E4FEAAD3018C9EF003200FDDBD664730D99165283F0904D0C7681AF37226DE3BEA62992FBD24FB5D7DD6FEF33361C5DAD012E10FE933267E2E67F8FCE91CC60214433F83401743BAA14F5B66873238C065040F3828F37E29E5DA4701DF9DACD93C86256C212BDFE04B0A03685C0C587569D79A96F603883AAF60AE20F78FC4A4B8B872E0A7A297FF9B063CB12D2E8994FB3E91A3416592AF66CD0EB92A7AE688BC3097E6A053B3FB7BDCFC88B3EF07BD1AD99ADD1881D54F9F13345B9D12DB978EEFA135ECA46D961BA92516B531C29B28ABCC061174785D66838A0DEACFA4A0F2B1B9758B3069E6DFAC5290DF3BD32A201E2A41B3CE4848652C65558A4D436E390452885BE3B953ED7FD55ABCDD8ACBD9155B2D3DDEA44539155944835665B5070F6D6A07AD3F7C4B681F5813815DBE71E9ACEF024A7DA76E59359A5F05BB0CD7E8DC1A9BFCB586045D77BB85139E6FB0B84A3F633C9EE3290966BC3344D9C223A56BB1F10E76BC29102CA02DA7B0D51568DF39C8DFB2429ACD2812B8DE9631FEA46236E8B3D88FF2121F167F38B0521C1F9AC975D86785DC5365C99D68CE4826ABD9DEF9CDF5AF3254122F658E960096C67B8726BCDDD8835B03DD630AE4044284A41539471998A7C18653B0EBE1E030C9D4826C10CDCBC1132613659C70B9A7C21B476EC6C96A48E898F3879EEBAA6E6979D4C38CC65DCB13DC02B262803FB40EF19C955049AA9D970E336E99DE9D15D975E6DC25C7995EB097B8716602571C068C663C0183E34F56FA8B26250E63CB4D69B5B99D4641752E9D18349900972334597CFCAAEB7F8E2E8323673CC8FCC9CC5172FE258B7583891127811112D37961C7EE22D72F1DC068DB4513CACA86551E2A466C79F8EA237B532CC8ABE7697397ED31C21D1AF312BEECF710AEF1D7B5D9D2697A856A66938A0C3DCA42DCAC5FEBF304A180602887A4CCD7E08B752FB81E6EA4574755679B8EB22246A298DE96CC8286DEAAF5247E2AB0D0EC06F445AD52DF62217278E171D2AF96A4DE533775B8DED4D0228298819017D028C7394B25560199DA0D9774A153A8604277B496FBD9972651199BAEECBA70B9E619351A32F10D58744E100CDE2FEE6CD16E52F149FEEA5EE26B3D33AF76E8EB1A76A48FC07ACE9568FD94E53779918E4C4B25D4A58A2F8AA9F595018776560933488670BE3BB8F643F87B79899454776DC4A9057F1B33A0E762CA7871128DE4761E798BF0FE53F651A4F0D92A17ED746E0613145BCD27ADD3766F1131099B33D1A6907708C27D80D904D3A7C907175674FC810C465E8B0795814D6E7F2CFA244CB9E0416D6270EF135973E6D4BC2F21E5EE3E2C2DE8EDB1E8FED979B190DA4CEAA764F829FC45B229AFF4331073056EFBFFFF5978E5CCB0B1093EB3159A9F078D8359D92B2A20E448B712FA999FBB27B1EE13509867D0448190224AE18E6A3BAA8DD3CB9AB65C0F5097C1482A4D40D4531F417FD54995604FF4908839717C09003C48DA740F46F36DE89A2EC81F3911185CBB5D976A78F26747DBE1A9CAF8EAC68968635F59310F7DD9E94D3BFDA72AD63A52315B429268A0A733A51EC4CC5736D52C704A0F4C6B8E179F2AD3189A9BECFAA576212EC0F2F9B373ABC4BF261C03BC65D59994257CB074601C348B1B1453F76545E1D0139CABC5046F8CFDDFEE33D4B6EF2CEE93513F864735CA6CB7FD22FFA8A3AE59F1BAAC92C42CE66403F7A92A88EC893E13FC9B139ED28AC02E6FF1EAD38FF58819BBC0402D40EA2E64DD59D1F5F9DBA8E8F68F9D2CC714B1FAD9D2AE5A4B415CC99F2464947235A39AC7DAB384849A011E4B3615ECBE273D1F55B263E485F53F91DD568FFAFAF41F0C7C5C53053407AD3DB7344F8BD33A1F2E1E5E8891411FF71BE0E78B31D7B8529BC1E826DE565C7DDE44A4037C111F78688866B34C8FE0F5A23AAA61B2E5F1E2BAB060AB4CA2A895D2AB187237C231C541997023B97303FC6C34C986EB2E29F6B6DCC6AED66112D8AB2026508C4C2A6CC1968FDBDBA7BE815E370C5E7B53B5F9FBA4BF612FF35BD1744BCA77F56133799345D9A127249B753DF5A13232872476971CB2C0C6BEB29B37B7FF761DFB7700693FE6A0C94D15AD64851FE18E938874C2E8736C629DB87AE6A3EF3CD1FFFA5CD630C5BBFCE75E0B88145D7E38983E26F4D17618584E302FFBE28828589DC1C2035779CDB97D", .sig = "CCD638E04CED882E7B1A9E2427F6029EAC98ECC9D5DB55F4812B4C416E770F5321A36F9429D39AE4122DEA3A1C3EDBE8F096AB23D48BD11D0D0B447BF7FD5D75F7561B6D21AE48154C7939AAB8C97BBEF789115CF20FF1CC6CEAEB7902CDB13D66DE16545BA190C2A71456341095C8B6F8118ADF196A63EFDB15609C085634EE9476F8086C89553D5D69EA9E7B592351CEE371DB250EBB8B547A909902C1C629836E58FAFF2996B0D32FC80381200483B1850983EA9FEA864ED24B42898E8C18F0B9AFDFBE48634CD3C8F33E6121CCEC08272F776F39E2AA3FA0BE1AED0D3B8F200B20E50F85284A649EC7563C1A919F092CD6BA519B0A38B85F6822B9A2B9378B63E4869BD745391928CA3812C846FEE057D27D79567F5CA36B4B3DEFCACB4E9356B78A2BAC246C4950804AA5F51CBF54F6C9E1043612B9C689B197D73F2F65BED76B69EF68D59B164FD0D374229C86433ABD3E9227C30BDF01E7B3072DEB1A45B7201C722DAC04D52F6A4045233266ADDA5B6883F0F270C1B1F4A5FFBF6517957E1DE46D1BE58532DF6EFF6C96B2205695ABEF9DD318AA81EE9224723C618BCED0C579812CEC6AE018E7E44F8A709AC01B136C5ECBE892701AF0FCD3A5ABD1AF15B10EBE19CF61087EC5FFE588412A25C44505D4B8C5E594FF0F77FFF2F5EFD7E36E7707BBE4FB335A00838BF693C05D23E979CE2379A11525254F2AE308CB7407BA2A4FBF073D1082262AF43B0D90C4B1CD3152502667C88A664BE6A71DA574F37095339916D3220A7F351D638403CDE9EF0085E714BC5F5E423E6D36E82BD2322FC2A02D8BE337F2A38BDA4BC8C196F208EB917B4573E2B167672ECFE0C354958F456D85A95E656DAA83785612945A91BA155D8996600C7210136F7F74CE5761C121024FE542C44898BCD79D5B22A232DE8EB4BF4AD03B3B1AEA83C1AA56E9F4558BA00A73B1EA61E7AE991D7453A67C5D06D822BD838ED7E0CEC504EF9CB42833A52DD426786BFD8E308A87CBDA7C15BD7A1022EDE96239EAB2B13BDF632788B67F712BCE3FD431DEF039D88A423E17EC4AECD774282C7257A6BC16E8838858F5FFF558B1870362D3DBA87F4D3142D7E8D2AF8A84AFF38A5C1BA928461C7A8D7C068F975A832383F4A82B0060ED3E74040B7E09479830DE26B3B45C59884F87E8E598C172DE7AF298E0952B4BA444B42F0B3C6140ECAC00429F4662FE577A22471A7CED89856205F8F0F2EEE8D33EFC945D4DFF37670C6B739C2031C864463B865FF2FB29A155B2735609254C4E1924D97514955049608A95282F0EF4B37DD15825B3FF66D6DF663571692A4548C13AE8512ECB4C807194A9870A638BD7FF59085AD867BAF5CC6198E32C3080E1514EBB12D6562AF3E6CACA8078633573FA4F0A713371672554EF5CE7C353CA27CD11B74E8E105EACF8FA92AF656083D9D18597846A799B857FF2923372CD4A15538319C12BCA83FFA5F658A5CFC48E5FF0ED1CD1E7A6E6D2876B4AAE80115469AB743304DAA324FD185B479DC5239D17E75A8BEA32277731B8164C033113728FBA5D01E95F72AF1C9CBAA3C666F0892894F3E4F14E2449DA8178250176C74634BA1405ABB0333D20E44F5CAA24C858C43099C3D402797F8B6081165AA8EA879B35907A4CDAFB9E478BF4C09581092958BAE72415305F0407E39F7D73FE6395763D0EE9639C7906E584833BBE4456218BCEF1F823E9D842EDC23DF77A11F2ADCE13A748AA1E0057A6DDC9B1497173BA48DAB5908609CD043FDDB44B397DF730606B2D7D27E221E6F1E7D4130EF4656D689FE24CF1F8EFAE96361426DAB83C24A9CCB081D5946B4F5D7E0089C7B39395C9B198A39E3D462290E90C657EB0B8702FBFF1CA4F3E27C960ED9F574F45C2497063D94617A06C0FBAFF55B195BE2A2D3CC32AED3A6FA081F8B58173EC5DA6E4E1128E09525CB3AF2BDCE75EA6FBEC31E3C4CA9274DFF3BA1528EC41ABC50C273C0398A46148DB495E802F955A60959053B174C5348C23EBFA0BE0F7CB7A8AB0C59B3CC90DBCE3D0A989DC64F152D2DA306ACE8203989DA7B73C53156E806D1F57B871CCD4A7FCFD3946EB9A8A73635E4F524AD323B517DFF38AD92C44D0F44FF2FE3B81CEB32A34E4E20A41ABB880D625EEEB6AA190AFAF06A5250CEC5D8B432D4B68E2BC2CE81E7D015A957465604CD4383686026777B0DE078E5F0E87D8DE00F46D7CF187EDF2A64095A3A377C4BA65A57FBC9EDEA140E79ABBCF294382A9C6BBAE4330186351BD810D1AED2D01FB0A816D31E284695D224C16AC5473F65607B135EAF79783394393C3C5865CA0CAD8DBF8FF2164FC69C03FE6DE178BDE02255BAB62E63ED672210D6761C3BA7B8625A567D548E85E6B6BD1DEC599A96E519CB99986AA927272F725953B555E9E5E04615DFC44FEFD3ADDC6D48B5145939A220F478468C706C6EA346BB3844A13DAC2F23B5F28A7ED60F5ED536BAE9FA7A07ABF811E4FD050C0831358B30B2EB64B7608076F620AA35867BFA8D7A463FF1C0563218A1A614FA1F2CD670F72AFAFCD687A79D85250DE77DBF7BB3FD38B44F118AD6FCB176FE50E01694C7C78C4D10A95621A99606F88DFE6E72E46326812AC6014D1091647219B51693F0FA23A2173446FACAA370FB0C967779295F8CD9BD2E3C1FB00242304EC21B6E1B0B15FB9A42761B1B63736A003FF2A8EDA22BD22640D2B327EA44F395098D4B64A878B26ED91472807772B7E639EF30741E7C824111DE5B0D3D0002B664849BCCBFB9985922929BBBEC0EDB7DF06F158E4A38BF91615F91935FCB54A60BF56D10012F18FDA39782221FA0646956D392E4174CFBA36E30658B58CAD707A52F26C6ACE72C05A8A6EEE5A4E2F69850361F8927D6A5B4084F85B9B4A225CF50BF048EB938E18FFB37631095DCB39C764BA40303A97EB33C9E63A2D1F6DF630DFC387FC1B10A524D7E638AFDF983BFFDCA14A3D24D5E2D807B27FF136D30C60FFBF2711D24FE10829657065500F5B8DFC2538AB434F030981ABAA6E4ABB10C0AB9D0BC9DB8B172AE93323237BB8521835EEB681A0DC2EE4E4A325404142014FE252C9821A5BEFC924892112116C21492C0F54074F4D615D54CD754E6AFF49D1D78EFE531E0C7FE7440FCF80752BE38EDBBE946586E94E89E76E7B7B6D032F69898B54BDC1B583338A0BCF60CE60ED407AA3C782FA677E92EA9D5565573D9CE551D10B6208480565E99FE0E72D997F4B96497346BCDFCABBDC494B968132DD632D111D24D230168D45FD8DDBEA851F80882AF700D809126A241618212D5052568E96989BB0C3C6CF202227363A3D6984B1B8C0F510172529474B57626E7B8391BDCDEC16282F3E3F545574757E85A7AEBFC4C9D00000000000000000000000000000000000000000000F1B2A3B", .algId = CRYPT_PKEY_ML_DSA, .type = CRYPT_MLDSA_TYPE_MLDSA_44, } }; static const char *MLDSA_SEED_VECTOR = NULL; static int32_t GetPkey(void *libCtx, const char *attrName, const CMVP_MldsaVector *vector, CRYPT_EAL_PkeyCtx **pkeyPrv, CRYPT_EAL_PkeyCtx **pkeyPub) { int32_t ret = CRYPT_CMVP_ERR_ALGO_SELFTEST; CRYPT_EAL_PkeyPrv prvKey = { 0 }; CRYPT_EAL_PkeyPub pubKey = { 0 }; *pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, vector->algId, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPrv == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); *pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, vector->algId, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPub == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_EAL_PkeySetParaById(*pkeyPrv, vector->type); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_PkeySetParaById(*pkeyPub, vector->type); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); int32_t val = 0; ret = CRYPT_EAL_PkeyCtrl(*pkeyPrv, CRYPT_CTRL_SET_MLDSA_ENCODE_FLAG, &val, sizeof(val)); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_PkeyCtrl(*pkeyPub, CRYPT_CTRL_SET_MLDSA_ENCODE_FLAG, &val, sizeof(val)); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); prvKey.id = vector->algId; prvKey.key.mldsaPrv.data = CMVP_StringsToBins(vector->sk, &(prvKey.key.mldsaPrv.len)); ret = CRYPT_EAL_PkeySetPrv(*pkeyPrv, &prvKey); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); pubKey.id = vector->algId; pubKey.key.mldsaPub.data = CMVP_StringsToBins(vector->pk, &(pubKey.key.mldsaPub.len)); ret = CRYPT_EAL_PkeySetPub(*pkeyPub, &pubKey); ERR: BSL_SAL_Free(prvKey.key.mldsaPrv.data); BSL_SAL_Free(pubKey.key.mldsaPub.data); return ret; } static int32_t TestVectorRandom(uint8_t *r, uint32_t rLen) { uint8_t *rand = NULL; uint32_t randLen; rand = CMVP_StringsToBins(MLDSA_SEED_VECTOR, &randLen); if (rand == NULL) { return CRYPT_MEM_ALLOC_FAIL; } if (randLen < rLen) { BSL_SAL_Free(rand); return CRYPT_CMVP_ERR_ALGO_SELFTEST; } for (uint32_t i = 0; i < randLen; i++) { r[i] = rand[i]; } BSL_SAL_Free(rand); return 0; } static bool TestMldsaSignVerify(void *libCtx, const char *attrName, const CMVP_MldsaVector *vector) { bool ret = false; uint8_t *sign = NULL; uint8_t *signVec = NULL; uint32_t signLen; uint32_t signVecLen = 0; uint8_t *msg = NULL; uint32_t msgLen; CRYPT_EAL_PkeyCtx *pkeyPrv = NULL; CRYPT_EAL_PkeyCtx *pkeyPub = NULL; CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); GOTO_ERR_IF_TRUE( GetPkey(libCtx, attrName, vector, &pkeyPrv, &pkeyPub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); signLen = CRYPT_EAL_PkeyGetSignLen(pkeyPrv); sign = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); msg = CMVP_StringsToBins(vector->msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); signVec = CMVP_StringsToBins(vector->sig, &signVecLen); GOTO_ERR_IF_TRUE(signVec == NULL, CRYPT_CMVP_COMMON_ERR); // regist rand function MLDSA_SEED_VECTOR = vector->rnd; CRYPT_RandRegist(TestVectorRandom); // sign GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(pkeyPrv, CRYPT_MD_MAX, msg, msgLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); // compare the signature GOTO_ERR_IF_TRUE(signLen != signVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(signVec, sign, signLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); // verify GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyVerify(pkeyPub, CRYPT_MD_MAX, msg, msgLen, sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(sign); BSL_SAL_Free(signVec); BSL_SAL_Free(msg); CRYPT_EAL_PkeyFreeCtx(pkeyPrv); CRYPT_EAL_PkeyFreeCtx(pkeyPub); CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); return ret; } bool CRYPT_CMVP_SelftestMldsaSignVerify(void) { bool ret = TestMldsaSignVerify(NULL, NULL, &MLDSA_VECTOR[0]); return ret; } bool CRYPT_CMVP_SelftestProviderMldsaSignVerify(void *libCtx, const char *attrName) { return TestMldsaSignVerify(libCtx, attrName, &MLDSA_VECTOR[0]); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_mldsa.c
C
unknown
24,761
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "crypt_eal_rand.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_util_rand.h" #include "securec.h" #include "bsl_sal.h" #define BITS_OF_BYTE 8 typedef struct { const char *ek; const char *dk; const char *c; const char *k; const char *m; int32_t algId; int32_t type; } CMVP_MlkemVector; static const CMVP_MlkemVector MLKEM_VECTOR[] = { { // https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files/ML-KEM-encapDecap-FIPS203 .ek = "EDDBA9CB830474EC0111408243A97335494DDDFA8D5C6A344B0C9ED1061A7D4A840891C24E140BE6D7BDEE01571A1312D4000591F2224B850105878FD3966F537AC348D042B95785B8171F18647619072780C131BDAA462D34A039014012BBB2197A4328F89CC5B750CAD9495BE607D5017DFA105D87C830432795B42C5E89D8B536C087592829ED934822768A1824696A278952627CA2EA73CDA4A306F972B8521B6A7B28DD0AC44E6A8C46BA7291D48574F8356065AFBB073DF9B84B520673F6644D3959BAD576497E0BC846F560CE697C35493A995597F66315639A7B20B84105EAA48F3B4593A0BF12D64AD4CB043B6435D44A2F5E136F21621B88704DF8855C4F1689761A239DFA6874455D48251EB087865AC3825C275A1018B112F01E82D8698F684C8EE127D9D25FB92B180677A36930793DAB0306AAC8BC80A79885B0737C6B319600BB859966AB9F082490E31B669008CD055921CB2360667180611485E5D70E774401739B16F54601D4A600608119E4B3112AAC92D1C094E40318D4259044D16D3AD62B35170B54D16F0F850C593B2062CC3A769B4D28B6A776F981D4BC835612349292B748E30098D29C668A80F68718BF40B7E785CE41C094A7326F3E6992D99B80E64350C5395823B1315160582EC10B01A4337C52C355E98B857B2DF0006A0E8AB49F5BA23B8C7E329C20F91386852A8948751C0F301465D713E7857A7BD8B6076C059BE378F74BB257750F544A31874B79ABEA78F8647E77001506919AA467019E924B7BE53705A5C9411A8B513C8C51300849136E77919CF83CBA9EA1CD6004AA7178C8E7D062E5E13544E060B34984F61C2EB6B56580C45F67DA8982A97BD5BBA9D98462161A55357079C15A2A33992D4B66093DB03676D0C385DCC6DD3C18EC8138738AA0FD6058C9F721C5E3131D7828C9675048949D0CE32E92A3363EC0527F54C89B5140590822D9C96CB1BAA43A8CACCC085EA6FB602B871128AB2C8A2A1644623A1D7175CF4A23F4B497B7BB4D0B652EA4272D7A99A9A5F4C54EAA6482D0BFB4E46EE482B892895C78DCBAE4414543E0BF8AC4C82889BC6DDC385624AC7805CCC644BB430588AA19723AEE5999EF2AE2565C3BB3", .dk = "502B723FE238EDE6A7867C66B49BAA99280584DB1318281150128D65F8A2D54381444994AD938805A3065271BCB390B9D100454A5644C5E858C1F223BF83654627AB98F433AED20E26D8597CD41D802C6323B18446487E51D3660458156DF1571E092763133B87F35BDC2CBC39422A65FB36488B523123B36E3AAF7A8C4DFFAA3719460ACEEC6F5F6957619B776925854A7A49E976592B042A7D3C49F3089F3FAC6F29185BF5DCAF77E6AB69F7ABB926A360385F5A79B7CB8CCC63BC38E0495A89A70E461CBDDBE34AA2F6CE7BF3C1533569B61A88DC1038A5B27C42D024EAF60041D0C771833D3C3213572CBC1C356CB649128A767A27150849D3684DF92159969A15261ABE78A6DCC549AA027216545F862959B45A22AEA7C01D64630CE498E788BF93C8A2FF4B3DA52387F5B171AF979001917716C2B887FBB0D7D243DBA5337BFA8DA97453236B36FFA278E36A28743A10035B34546514EFFB2CB4CC92F7C614EAE9BCE501B90CD94BB1A1A4C9FAA922BAB2DF8C9BCB8B76CE0B035C8312A990C180F23BD1018D44870021345DB3B39DD55A79229A69B1D79802735E40990CB6092FE614327BDC1FA7B158C16327AAE85456B48D8E64398EF90B24D9C8FBCB9DBC72422126644A83AE4A17069A5CBBCAFBA71473256F1568CB18CC4612A0379BA1E4330A782137282B82ECD25492475F50B30CC7A31521CCB1151CB096944C4E5C21255C1C89C130BE9070DA1696FD1C2901E975B7805C2737BBBAA2965AD10DC43727556A6608E70CD7E59B8FA3CBCD4CB2FE48912B2B72C2D592E0C1AF7702387AF167964685B1D40349F20CA6808B74F87DE67C255145A738F7B3DB8B89F6A71FCF2BB7ED11C6EB2154A37B4CF7063F8BA12433B457F4E3CC4B05370C0A76896A7EE39A863AE49945FAA497766DE3201E34AB7678663E33F85B12C9B570184E0DA3092598AC5B961D7D48C9B30624F829AB66F63B8D34478D35664FB84BECD1951375183B6A2357587F8513C6C5431C0198316AF2CA3987B9F5E219B36116108C8C8E767FE7C690AF248CE0839C13EC4A4CD800FC719BC191B59C5A17EDDBA9CB830474EC0111408243A97335494DDDFA8D5C6A344B0C9ED1061A7D4A840891C24E140BE6D7BDEE01571A1312D4000591F2224B850105878FD3966F537AC348D042B95785B8171F18647619072780C131BDAA462D34A039014012BBB2197A4328F89CC5B750CAD9495BE607D5017DFA105D87C830432795B42C5E89D8B536C087592829ED934822768A1824696A278952627CA2EA73CDA4A306F972B8521B6A7B28DD0AC44E6A8C46BA7291D48574F8356065AFBB073DF9B84B520673F6644D3959BAD576497E0BC846F560CE697C35493A995597F66315639A7B20B84105EAA48F3B4593A0BF12D64AD4CB043B6435D44A2F5E136F21621B88704DF8855C4F1689761A239DFA6874455D48251EB087865AC3825C275A1018B112F01E82D8698F684C8EE127D9D25FB92B180677A36930793DAB0306AAC8BC80A79885B0737C6B319600BB859966AB9F082490E31B669008CD055921CB2360667180611485E5D70E774401739B16F54601D4A600608119E4B3112AAC92D1C094E40318D4259044D16D3AD62B35170B54D16F0F850C593B2062CC3A769B4D28B6A776F981D4BC835612349292B748E30098D29C668A80F68718BF40B7E785CE41C094A7326F3E6992D99B80E64350C5395823B1315160582EC10B01A4337C52C355E98B857B2DF0006A0E8AB49F5BA23B8C7E329C20F91386852A8948751C0F301465D713E7857A7BD8B6076C059BE378F74BB257750F544A31874B79ABEA78F8647E77001506919AA467019E924B7BE53705A5C9411A8B513C8C51300849136E77919CF83CBA9EA1CD6004AA7178C8E7D062E5E13544E060B34984F61C2EB6B56580C45F67DA8982A97BD5BBA9D98462161A55357079C15A2A33992D4B66093DB03676D0C385DCC6DD3C18EC8138738AA0FD6058C9F721C5E3131D7828C9675048949D0CE32E92A3363EC0527F54C89B5140590822D9C96CB1BAA43A8CACCC085EA6FB602B871128AB2C8A2A1644623A1D7175CF4A23F4B497B7BB4D0B652EA4272D7A99A9A5F4C54EAA6482D0BFB4E46EE482B892895C78DCBAE4414543E0BF8AC4C82889BC6DDC385624AC7805CCC644BB430588AA19723AEE5999EF2AE2565C3BB3519E879596C07C24B516C80882627308F4244B98164FDA55AC298B3955EB4C9A35956CA9712BCA0ED46C82CF3506460EB702105E3A4DF7410C9FFEA032F26C02", .c = "387F23816CA3AAE900520559080374E6BFF44117D50D78EC4CB4A5C08A955DBBDFCBDE0803301BA3E40A9C33CAAE6DFA71EED311D9ECB84B497D3259B614DA30C71803288904D0E1B9EA8FB5750878602148E6FCE794B91E2778B07D87706E620B727162290CA7CE27DDEC5110770614175CE61A837CE46DA95D6AE7FBD9D4709636807EB67CC5992A643EABA2C253050C7B63CA6CA83617BCAF453AC3E24B9C3617038AA3A2FDC52A32EE7B2EC2B67E5F442BFB8D5429F079096FA7D5B9FBD81B096A75F2C9FAAD8E97DDC003EB88F55F482775BA7BD661BFCFC27D3A7C1071C5A7B020E80F6950BF315A040F4CF025A74B42DC008F4C084690F43E8502BFB740538BF3DF05336DDD6E33A3A4679A33C8A724125A8EC897A593741CA9C934CEEC2F20C82CCF439011C5066937377183C8CC96046985B92F6D014E0223EA542A8BA0A789478EB4B4F04AA98F7E6A4D4AC8852E8670B5A4097F7584FA136A159DAF5005D9FEF08868BFBFBE15D320AEAFFB3DE18C8F39A22A876D750AAD4F85546A88B0D0B498535B590B191894F43763BD13379BBD2CDEF4C98A571103E9409B2227315AB892990984FD6C9F375164959E3035B16C650CE71AFB62F7E3BFC624FACC973B104B44F6A2BCEEE1AACF0C3D8CE9ADDCEB26E3E786C116B70A3CF2C63524F472A2F15DBAB3FF8FDE34F015DD8DCFCBB17793A552E805AD5541ADFC6890B5E3A4DDBBC1CDB9B36D12DC49CA22CBB91F5BB599EF86DC4B5820B8E3D965839DFAA9392D29A878315BEA9A6885994C0D07843A4DC2FB87D0C8507CB17C549C3E60564D263221635F831C9F1F6539F07248E5BA8571541CF69151630161C0DBA57DA3A3C6296FEB02C19E2080F57C657CA74656B88B708450DA5DED44594B12614054D643FEB674E18FD3B863DE526EFB6D2426A58AE363658E8318578029DA3388D5A3C81E53E22F2802CBFD52760AF128EDAE7D1FC3379642FD8B9A791B289D7ECFC546158FBDCFBE1643B03B6874F3BA918849D93E5C0172E684C303D8D58CC5DE703A35FDB5796B81CD415B35DAEB60FCC043827401542FE35AD72375", .k = "9A4E2BCC0E5E40528B4C2879D8BE4BF7AA266C16762480054B475814789EABCA", .m = "381C547CE06B687A7A17C1A99D11A7A4C35B82685AF51CE452099C60136FD84A", .algId = CRYPT_PKEY_ML_KEM, .type = CRYPT_KEM_TYPE_MLKEM_512, } }; static const char *MLKEM_SEED_VECTOR = NULL; static int32_t GetPkey(void *libCtx, const char *attrName, const CMVP_MlkemVector *vector, CRYPT_EAL_PkeyCtx **pkeyPrv, CRYPT_EAL_PkeyCtx **pkeyPub) { int32_t ret = CRYPT_CMVP_ERR_ALGO_SELFTEST; CRYPT_EAL_PkeyPrv prvKey = { 0 }; CRYPT_EAL_PkeyPub pubKey = { 0 }; *pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, vector->algId, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPrv == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); *pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, vector->algId, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPub == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_EAL_PkeySetParaById(*pkeyPrv, vector->type); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_PkeySetParaById(*pkeyPub, vector->type); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); prvKey.id = vector->algId; prvKey.key.mldsaPrv.data = CMVP_StringsToBins(vector->dk, &(prvKey.key.kemDk.len)); ret = CRYPT_EAL_PkeySetPrv(*pkeyPrv, &prvKey); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); pubKey.id = vector->algId; pubKey.key.mldsaPub.data = CMVP_StringsToBins(vector->ek, &(pubKey.key.kemEk.len)); ret = CRYPT_EAL_PkeySetPub(*pkeyPub, &pubKey); ERR: BSL_SAL_Free(prvKey.key.kemDk.data); BSL_SAL_Free(pubKey.key.kemEk.data); return ret; } static int32_t TestVectorRandom(uint8_t *r, uint32_t rLen) { uint8_t *rand = NULL; uint32_t randLen; rand = CMVP_StringsToBins(MLKEM_SEED_VECTOR, &randLen); if (rand == NULL) { return CRYPT_MEM_ALLOC_FAIL; } if (randLen < rLen) { BSL_SAL_Free(rand); return CRYPT_CMVP_ERR_ALGO_SELFTEST; } for (uint32_t i = 0; i < randLen; i++) { r[i] = rand[i]; } BSL_SAL_Free(rand); return 0; } static bool TestMlkemEncapsDecaps(void *libCtx, const char *attrName, const CMVP_MlkemVector *vector) { bool ret = false; uint8_t *cipher = NULL; uint8_t *cipherVec = NULL; uint32_t cipherLen = 0; uint32_t cipherVecLen = 0; CRYPT_EAL_PkeyCtx *pkeyPrv = NULL; CRYPT_EAL_PkeyCtx *pkeyPub = NULL; uint8_t sharedKey[32] = { 0 }; uint32_t sharedLen = sizeof(sharedKey); uint8_t *sharedKeyVec = NULL; uint32_t sharedKeyVecLen = 0; CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); GOTO_ERR_IF_TRUE( GetPkey(libCtx, attrName, vector, &pkeyPrv, &pkeyPub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(pkeyPrv, CRYPT_CTRL_GET_CIPHERTEXT_LEN, &cipherLen, sizeof(cipherLen)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); cipher = BSL_SAL_Malloc(cipherLen); GOTO_ERR_IF_TRUE(cipher == NULL, CRYPT_MEM_ALLOC_FAIL); cipherVec = CMVP_StringsToBins(vector->c, &cipherVecLen); GOTO_ERR_IF_TRUE(cipherVec == NULL, CRYPT_CMVP_COMMON_ERR); sharedKeyVec = CMVP_StringsToBins(vector->k, &sharedKeyVecLen); GOTO_ERR_IF_TRUE(sharedKeyVec == NULL, CRYPT_CMVP_COMMON_ERR); // regist rand function MLKEM_SEED_VECTOR = vector->m; CRYPT_RandRegist(TestVectorRandom); // encaps GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyEncaps(pkeyPub, cipher, &cipherLen, sharedKey, &sharedLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(cipherLen != cipherVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(cipher, cipherVec, cipherLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(sharedLen != sharedKeyVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(sharedKey, sharedKeyVec, sharedLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); // decaps GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyDecaps(pkeyPrv, cipherVec, cipherVecLen, sharedKey, &sharedLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(sharedLen != sharedKeyVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(sharedKey, sharedKeyVec, sharedLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(cipher); BSL_SAL_Free(cipherVec); BSL_SAL_Free(sharedKeyVec); CRYPT_EAL_PkeyFreeCtx(pkeyPrv); CRYPT_EAL_PkeyFreeCtx(pkeyPub); CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); return ret; } bool CRYPT_CMVP_SelftestMlkemEncapsDecaps(void) { bool ret = TestMlkemEncapsDecaps(NULL, NULL, &MLKEM_VECTOR[0]); return ret; } bool CRYPT_CMVP_SelftestProviderMlkemEncapsDecaps(void *libCtx, const char *attrName) { return TestMlkemEncapsDecaps(libCtx, attrName, &MLKEM_VECTOR[0]); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_mlkem.c
C
unknown
12,690
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_kdf.h" #include "bsl_err_internal.h" #include "crypt_params_key.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { const char *pw; const char *salt; uint32_t iter; const char *key; CRYPT_MAC_AlgId id; } CMVP_PBKDF2_VECTOR; // https://www.ietf.org/rfc/rfc6070.txt static const CMVP_PBKDF2_VECTOR PBKDF2_VECTOR[] = { { .pw = "passwordPASSWORDpassword", .salt = "saltSALTsaltSALTsaltSALTsaltSALTsalt", .iter = 4096, .key = "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038", .id = CRYPT_MAC_HMAC_SHA1 }, { .pw = "passwordPASSWORDpassword", .salt = "saltSALTsaltSALTsaltSALTsaltSALTsalt", .iter = 1024, .key = "1e0d8ab1b32bb96dff58ff89ab171e643a425fd87f26261b56f9d38c992f2593a713f9c1772f8bf2", .id = CRYPT_MAC_HMAC_SM3 }, }; static const CMVP_PBKDF2_VECTOR *FindVectorById(CRYPT_MAC_AlgId id) { const CMVP_PBKDF2_VECTOR *pPbkdf2Vec = NULL; uint32_t num = sizeof(PBKDF2_VECTOR) / sizeof(PBKDF2_VECTOR[0]); for (uint32_t i = 0; i < num; i++) { if (PBKDF2_VECTOR[i].id == id) { pPbkdf2Vec = &PBKDF2_VECTOR[i]; return pPbkdf2Vec; } } return NULL; } static bool CRYPT_CMVP_SelftestPbkdf2Internal(void *libCtx, const char *attrName, CRYPT_MAC_AlgId id) { (void)id; bool ret = false; const char *pw = NULL; const char *salt = NULL; uint8_t *expOut = NULL; uint8_t *out = NULL; uint32_t expOutLen, pwLen, saltLen, iter; CRYPT_EAL_KdfCTX *ctx = NULL; const CMVP_PBKDF2_VECTOR *pbkdf2Vec = FindVectorById(id); if (pbkdf2Vec == NULL || pbkdf2Vec->pw == NULL) { return false; } iter = pbkdf2Vec->iter; pw = pbkdf2Vec->pw; pwLen = (uint32_t)strlen(pbkdf2Vec->pw); salt = pbkdf2Vec->salt; saltLen = (uint32_t)strlen(pbkdf2Vec->salt); expOut = CMVP_StringsToBins(pbkdf2Vec->key, &expOutLen); GOTO_ERR_IF_TRUE(expOut == NULL, CRYPT_CMVP_COMMON_ERR); out = BSL_SAL_Malloc(expOutLen); GOTO_ERR_IF_TRUE(out == NULL, CRYPT_MEM_ALLOC_FAIL); ctx = CRYPT_EAL_ProviderKdfNewCtx(libCtx, CRYPT_KDF_PBKDF2, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); BSL_Param param[5] = { {CRYPT_PARAM_KDF_MAC_ID, BSL_PARAM_TYPE_UINT32, &id, sizeof(id), 0}, {CRYPT_PARAM_KDF_PASSWORD, BSL_PARAM_TYPE_OCTETS, (void *)(uintptr_t)pw, pwLen, 0}, {CRYPT_PARAM_KDF_SALT, BSL_PARAM_TYPE_OCTETS, (void *)(uintptr_t)salt, saltLen, 0}, {CRYPT_PARAM_KDF_ITER, BSL_PARAM_TYPE_UINT32, &iter, sizeof(uint32_t), 0}, BSL_PARAM_END }; GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfSetParam(ctx, param) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfDerive(ctx, out, expOutLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(out, expOut, expOutLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(expOut); BSL_SAL_Free(out); CRYPT_EAL_KdfFreeCtx(ctx); return ret; } bool CRYPT_CMVP_SelftestPbkdf2(CRYPT_MAC_AlgId id) { return CRYPT_CMVP_SelftestPbkdf2Internal(NULL, NULL, id); } bool CRYPT_CMVP_SelftestProviderPbkdf2(void *libCtx, const char *attrName, CRYPT_MAC_AlgId id) { return CRYPT_CMVP_SelftestPbkdf2Internal(libCtx, attrName, id); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_pbkdf2.c
C
unknown
4,260
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "crypt_utils.h" #include "crypt_dsa.h" #include "crypt_ecdsa.h" #include "crypt_curve25519.h" #include "crypt_rsa.h" #include "crypt_sm2.h" #include "crypt_mldsa.h" #include "crypt_mlkem.h" #include "crypt_eal_implprovider.h" #include "crypt_slh_dsa.h" #ifdef HITLS_CRYPTO_MLKEM static bool CMVP_MlkemPct(void *ctx) { bool ret = false; uint32_t cipherLen = 0; uint8_t *ciphertext = NULL; uint8_t sharedKey[32] = {0}; uint32_t sharedLen = sizeof(sharedKey); uint8_t sharedKey2[32] = {0}; uint32_t sharedLen2 = sizeof(sharedKey2); GOTO_ERR_IF_TRUE(CRYPT_ML_KEM_Ctrl(ctx, CRYPT_CTRL_GET_CIPHERTEXT_LEN, &cipherLen, sizeof(cipherLen)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ciphertext = BSL_SAL_Malloc(cipherLen); GOTO_ERR_IF_TRUE(ciphertext == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_ML_KEM_Encaps(ctx, ciphertext, &cipherLen, sharedKey, &sharedLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_ML_KEM_Decaps(ctx, ciphertext, cipherLen, sharedKey2, &sharedLen2) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(sharedLen != sharedLen2 || memcmp(sharedKey, sharedKey2, sharedLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(ciphertext); return ret; } #endif typedef struct { int32_t id; CRYPT_EAL_ImplPkeySign sign; CRYPT_EAL_ImplPkeyVerify verify; CRYPT_EAL_ImplPkeyMgmtCtrl ctrl; } PkeyMethodMap; static const PkeyMethodMap pkey_map[] = { #ifdef HITLS_CRYPTO_DSA {CRYPT_PKEY_DSA, (CRYPT_EAL_ImplPkeySign)CRYPT_DSA_Sign, (CRYPT_EAL_ImplPkeyVerify)CRYPT_DSA_Verify, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_DSA_Ctrl}, #endif #ifdef HITLS_CRYPTO_ED25519 {CRYPT_PKEY_ED25519, (CRYPT_EAL_ImplPkeySign)CRYPT_CURVE25519_Sign, (CRYPT_EAL_ImplPkeyVerify)CRYPT_CURVE25519_Verify, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_CURVE25519_Ctrl}, #endif #ifdef HITLS_CRYPTO_RSA {CRYPT_PKEY_RSA, (CRYPT_EAL_ImplPkeySign)CRYPT_RSA_Sign, (CRYPT_EAL_ImplPkeyVerify)CRYPT_RSA_Verify, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_RSA_Ctrl}, #endif #ifdef HITLS_CRYPTO_ECDSA {CRYPT_PKEY_ECDSA, (CRYPT_EAL_ImplPkeySign)CRYPT_ECDSA_Sign, (CRYPT_EAL_ImplPkeyVerify)CRYPT_ECDSA_Verify, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ECDSA_Ctrl}, #endif #ifdef HITLS_CRYPTO_SM2 {CRYPT_PKEY_SM2, (CRYPT_EAL_ImplPkeySign)CRYPT_SM2_Sign, (CRYPT_EAL_ImplPkeyVerify)CRYPT_SM2_Verify, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_SM2_Ctrl}, #endif #ifdef HITLS_CRYPTO_SLH_DSA {CRYPT_PKEY_SLH_DSA, (CRYPT_EAL_ImplPkeySign)CRYPT_SLH_DSA_Sign, (CRYPT_EAL_ImplPkeyVerify)CRYPT_SLH_DSA_Verify, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_SLH_DSA_Ctrl}, #endif #ifdef HITLS_CRYPTO_MLDSA {CRYPT_PKEY_ML_DSA, (CRYPT_EAL_ImplPkeySign)CRYPT_ML_DSA_Sign, (CRYPT_EAL_ImplPkeyVerify)CRYPT_ML_DSA_Verify, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ML_DSA_Ctrl}, #endif {CRYPT_PKEY_MAX, NULL, NULL, NULL} }; static bool CMVP_SignVerifyPct(void *ctx, int32_t algId) { bool ret = false; uint8_t *sign = NULL; uint32_t signLen = 0; const uint8_t msg[] = {0x01, 0x02, 0x03, 0x04}; uint32_t mdId = CRYPT_MD_SHA512; const PkeyMethodMap *map = NULL; for (uint32_t i = 0; i < sizeof(pkey_map) / sizeof(pkey_map[0]); i++) { if (algId == pkey_map[i].id) { map = &pkey_map[i]; break; } } GOTO_ERR_IF_TRUE(map == NULL, CRYPT_EAL_ERR_ALGID); GOTO_ERR_IF_TRUE(map->ctrl(ctx, CRYPT_CTRL_GET_SIGNLEN, &signLen, sizeof(signLen)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); sign = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); if (algId == CRYPT_PKEY_RSA) { GOTO_ERR_IF_TRUE(map->ctrl(ctx, CRYPT_CTRL_SET_RSA_EMSA_PKCSV15, &mdId, sizeof(mdId)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(map->sign(ctx, algId == CRYPT_PKEY_SM2 ? CRYPT_MD_SM3 : CRYPT_MD_SHA512, msg, sizeof(msg), sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(map->verify(ctx, algId == CRYPT_PKEY_SM2 ? CRYPT_MD_SM3 : CRYPT_MD_SHA512, msg, sizeof(msg), sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(sign); return ret; } bool CRYPT_CMVP_SelftestPkeyPct(void *ctx, int32_t algId) { if (algId == CRYPT_PKEY_DH || algId == CRYPT_PKEY_ECDH || algId == CRYPT_PKEY_X25519) { return true; } #ifdef HITLS_CRYPTO_MLKEM if (algId == CRYPT_PKEY_ML_KEM) { return CMVP_MlkemPct(ctx); } #endif return CMVP_SignVerifyPct(ctx, algId); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_pct.c
C
unknown
5,574
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "bsl_err_internal.h" #include "crypt_params_key.h" #include "crypt_utils.h" #include "crypt_eal_rand.h" #include "crypt_util_rand.h" #include "securec.h" #include "bsl_sal.h" #define PKCSV15_PAD 0 #define PSS_PAD 1 #define OAEP_PAD 2 #define MAX_CIPHER_TEXT_LEN 512 typedef struct { const char *n; const char *e; const char *d; const char *salt; const char *msg; const char *sign; CRYPT_MD_AlgId mdId; } CMVP_RSA_VECTOR; // 与CRYPT_EAL_PkeyPadId顺序一致 static const CMVP_RSA_VECTOR RSA_VECTOR[] = { // RSA-2048bits-SHA224 PKCS#1 Ver 1.5 // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Digital-Signatures#rsa2vs { .n = "e0b14b99cd61cd3db9c2076668841324fa3174f33ce66ffd514394d34178d29a49493276b6777233" "e7d46a3e68bc7ca7e899e901d54f6dee0749c3e48ddf68685867ee2ae66df88eb563f6db137a9f6b" "175a112e0eda8368e88e45efe1ce14bc6016d52639627066af1872c72f60b9161c1d237eeb34b0f8" "41b3f0896f9fe0e16b0f74352d101292cc464a7e7861bbeb86f6df6151cb265417c66c565ed8974b" "d8fc984d5ddfd4eb91a3d5234ce1b5467f3ade375f802ec07293f1236efa3068bc91b158551c875c" "5dc0a9d6fa321bf9421f08deac910e35c1c28549ee8eed8330cf70595ff70b94b49907e27698a9d9" "11f7ac0706afcb1a4a39feb38b0a8049", .e = "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000010001", .d = "1dbca92e4245c2d57bfba76210cc06029b502753b7c821a32b799fbd33c98b49db10226b1eac0143" "c8574ef652833b96374d034ef84daa5559c693f3f028d49716b82e87a3f682f25424563bd9409dcf" "9d08110500f73f74076f28e75e0199b1f29fa2f70b9a31190dec54e872a740e7a1b1e38c3d11bca8" "267deb842cef4262237ac875725068f32563b478aca8d6a99f34cb8876b97145b2e8529ec8adea83" "ead4ec63e3ff2d17a2ffefb05c902ca7a92168378c89f75c928fc4f0707e43487a4f47df70cae87e" "24272c136d3e98cf59066d41a3d038857d073d8b4d2c27b8f0ea6bfa50d263091a4a18c63f446bc9" "a61e8c4a688347b2435ec8e72eddaea7", .salt = NULL, .msg = "79bcffbfd6bcf638934b38e47a1b821dc97cafe1da757f820313989ebc01ca52ff5997abf5baf35d" "ce9b48b8f0debdd755a8b81b2e71a1d8cd57ea4dc1b84cda43ff536dd1be1c3e18fe5ebc17d3a7c6" "8233e81f6407341c0983c5a01bb3404a0b5739edb2f1fa41391c80d8361fc75317c248d5c461bfb8" "803e317f101b2e0c", .sign = "5cbc1d2c696e7c5c0a538db35a793959008564c43d9aa8ed20816b66ef77124eca7584631308d0fd" "7383be62eaf799b5e67e8874cc9d88d507e1bd4fb9fd7517adebe5d583b075040ce3db2affcf77ee" "0162be2e575413f455841cb6ea4a30595daee45e3042b0b9d8f9ee700df3f1898219777c21ef3695" "af95628ae64260dd2cb7ee6270fb06f52ea1aea72e1a26a26f2e7cee560ae0cb8be323113c3f19c9" "7cb5a3e61b998a68432aa2d1f8c8c00ac92b0f35344710ae1d6d79f379fbb3dba41b46b9c814eb3a" "25ca64a3ff86af613d163f941a897676652e7c3f6769fd964b862dc58cc2e652d0a404e94853fb83" "937c862c1df2df9fd297f058bf660d15", .mdId = CRYPT_MD_SHA224 }, // RSA-2048bits-SHA224 PKCS#1 RSASSA-PSS // https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Digital-Signatures#rsa2vs { .n = "d95b71c9dfee453ba1b1a7de2c1f0b0a67579ee91d1d3ad97e481829b86edac750c48e12a8cdb026" "c82f273dafc222009f0db3b08b2db10a69c4b2dddaaeceac1b0c862682eef294e579f55aab871bc0" "a7eeabc923c9e80dddc22ec0a27002aee6a5ba66397f412bbaf5fb4eaf66a1a0f82eaf6827198caf" "49b347258b1283e8cbb10da2837f6ecc3490c728fe927f44455a6f194f3776bf79151d9ad7e2daf7" "70b37d12627cc0c5fb62484f46258d9ce2c11b26256d09cb412f8d8f8f1fe91bb94ac27de6d26a83" "a8439e51b35dbee46b3b8ff991d667bb53eeee85ff1652c8981f141d47c8205791cef5b32d718ddc" "082ed0dd542826416b2271064ef437a9", .e = "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000000000000000000000000000000000000000000000000000000000" "00000000000000000000000000010001", .d = "2f21b01be94dde7f5ec18a3817f3274ebb37f9c26cc8c0d1169c05794e7fe33ae31dabfd09d38845" "f094a0fab458f14c9730be6d22d0e699ee7373a1bde0b7fa03e784536782eee1309d708197be355b" "624ed3bb4ae2664a5372def67082bf6233ab6e2eea7ad8a3e5e79ef5e1fcec415e6fa923798f05bd" "a0ca9a3bdedb45f4d781ef1a4f5075cd9bb399635da3e9a6880ed021a750bc9806af81fbffcd4ace" "af804ec76808ae186715c772caa961a862991c67ca8bffef6b34087b44db5b59abce09317747fc75" "252f1705260b13dd62ccbc745091f3c1b64f59031d340c7362a0e1066ab0554d466f209a3cf51bc6" "4b3c70c3ce52f413d81b228fa31d9efd", .salt = "6f2841166a64471d4f0b8ed0dbb7db32161da13b", .msg = "e2b81456c355c3f80a363a85cbf245e85a5ff2435e5548d627b5362242aaca4e4a2fa4c900d2a931" "9eb7fc7469df2a3586aaa4710e9b7362655c27a3c70210962391b1032dc37201af05951a1fc36baa" "77e5c888419ab4e8f1546380781468ea16e7254a70b08630e229efc016257210d61846d11ed87432" "76a5d4017e683813", .sign = "cd1fe0acb89969ae139c178bfef1cc982993521b3a020ec847c89c0cc6c869d970f43f018d495b9e" "991457e7501a344c33c376fd2efcf05ad6eb2bd0b3c0e7cc3c88a4124398ca16585490a0817a3614" "9cc82cdc01b20e9026261215dd06f9db4e13613c6a569c2187a0e00bc63c281149433ac7f061bd21" "8e79f8eca9dd9c93ebc3cc013bf27aa0bf286e124593e76d3c7012f97ae1d0c4bf5823cf17fe76d5" "05a54cef174add58ae616f47de825049e9916bf2ab7de4d443745763b0c314cfae3a6e57ad475cc5" "fae47cddcad7b526c2154a15f9ee8eab02f4c36f7a41d7a19b23c5996b627270ceb2c0dbed1a6b6d" "d2ff94868e073cb7b1a1fa3429e487ae", .mdId = CRYPT_MD_SHA224 }, }; typedef struct { const char *n; const char *e; const char *d; const char *seed; const char *msg; const char *cipher; CRYPT_MD_AlgId mdId; } CMVP_RSA_ENC_DEC_VECTOR; // Test vectors sourced from the pyca/cryptography project. static const CMVP_RSA_ENC_DEC_VECTOR RSA_ENC_DEC_VECTOR = { .n = "ae45ed5601cec6b8cc05f803935c674ddbe0d75c4c09fd7951fc6b0caec313a8" "df39970c518bffba5ed68f3f0d7f22a4029d413f1ae07e4ebe9e4177ce23e7f5" "404b569e4ee1bdcf3c1fb03ef113802d4f855eb9b5134b5a7c8085adcae6fa2f" "a1417ec3763be171b0c62b760ede23c12ad92b980884c641f5a8fac26bdad4a0" "3381a22fe1b754885094c82506d4019a535a286afeb271bb9ba592de18dcf600" "c2aeeae56e02f7cf79fc14cf3bdc7cd84febbbf950ca90304b2219a7aa063aef" "a2c3c1980e560cd64afe779585b6107657b957857efde6010988ab7de417fc88" "d8f384c4e6e72c3f943e0c31c0c4a5cc36f879d8a3ac9d7d59860eaada6b83bb", .e = "010001", .d = "056b04216fe5f354ac77250a4b6b0c8525a85c59b0bd80c56450a22d5f438e59" "6a333aa875e291dd43f48cb88b9d5fc0d499f9fcd1c397f9afc070cd9e398c8d" "19e61db7c7410a6b2675dfbf5d345b804d201add502d5ce2dfcb091ce9997bbe" "be57306f383e4d588103f036f7e85d1934d152a323e4a8db451d6f4a5b1b0f10" "2cc150e02feee2b88dea4ad4c1baccb24d84072d14e1d24a6771f7408ee30564" "fb86d4393a34bcf0b788501d193303f13a2284b001f0f649eaf79328d4ac5c43" "0ab4414920a9460ed1b7bc40ec653e876d09abc509ae45b525190116a0c26101" "848298509c1c3bf3a483e7274054e15e97075036e989f60932807b5257751e79", .msg = "8bba6bf82a6c0f86d5f1756e97956870b08953b06b4eb205bc1694ee", .seed = "47e1ab7119fee56c95ee5eaad86f40d0aa63bd33", .cipher = "53ea5dc08cd260fb3b858567287fa91552c30b2febfba213f0ae87702d068d19" "bab07fe574523dfb42139d68c3c5afeee0bfe4cb7969cbf382b804d6e6139614" "4e2d0e60741f8993c3014b58b9b1957a8babcd23af854f4c356fb1662aa72bfc" "c7e586559dc4280d160c126785a723ebeebeff71f11594440aaef87d10793a87" "74a239d4a04c87fe1467b9daf85208ec6c7255794a96cc29142f9a8bd418e3c1" "fd67344b0cd0829df3b2bec60253196293c6b34d3f75d32f213dd45c6273d505" "adf4cced1057cb758fc26aeefa441255ed4e64c199ee075e7f16646182fdb464" "739b68ab5daff0e63e9552016824f054bf4d3c8c90a97bb6b6553284eb429fcc", .mdId = CRYPT_MD_SHA1, }; static bool GetPrvKey(const char *n, const char *d, CRYPT_EAL_PkeyPrv *prv) { (void)memset_s(&prv->key.rsaPrv, sizeof(prv->key.rsaPrv), 0, sizeof(prv->key.rsaPrv)); prv->key.rsaPrv.n = CMVP_StringsToBins(n, &(prv->key.rsaPrv.nLen)); GOTO_ERR_IF_TRUE(prv->key.rsaPrv.n == NULL, CRYPT_CMVP_COMMON_ERR); prv->key.rsaPrv.d = CMVP_StringsToBins(d, &(prv->key.rsaPrv.dLen)); GOTO_ERR_IF_TRUE(prv->key.rsaPrv.d == NULL, CRYPT_CMVP_COMMON_ERR); prv->id = CRYPT_PKEY_RSA; return true; ERR: return false; } static bool GetPubKey(const char *n, const char *e, CRYPT_EAL_PkeyPub *pub) { pub->key.rsaPub.n = CMVP_StringsToBins(n, &(pub->key.rsaPub.nLen)); GOTO_ERR_IF_TRUE(pub->key.rsaPub.n == NULL, CRYPT_CMVP_COMMON_ERR); pub->key.rsaPub.e = CMVP_StringsToBins(e, &(pub->key.rsaPub.eLen)); GOTO_ERR_IF_TRUE(pub->key.rsaPub.e == NULL, CRYPT_CMVP_COMMON_ERR); pub->id = CRYPT_PKEY_RSA; return true; ERR: return false; } static bool SetPkcsv15Pad(CRYPT_EAL_PkeyCtx *pkey, uint32_t *hashId) { *hashId = RSA_VECTOR[PKCSV15_PAD].mdId; GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_SET_RSA_EMSA_PKCSV15, hashId, sizeof(uint32_t)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); return true; ERR: return false; } static bool SetPssPad(CRYPT_EAL_PkeyCtx *pkey, uint32_t saltLen) { uint32_t mdId = RSA_VECTOR[PSS_PAD].mdId; uint32_t mgfId = RSA_VECTOR[PSS_PAD].mdId; BSL_Param pss[4] = { {CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&mdId, sizeof(mdId), 0}, {CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&mgfId, sizeof(mgfId), 0}, {CRYPT_PARAM_RSA_SALTLEN, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&saltLen, sizeof(saltLen), 0}, BSL_PARAM_END }; GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_SET_RSA_EMSA_PSS, pss, 0) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); return true; ERR: return false; } static bool RsaSelftestSign(void *libCtx, const char *attrName, int32_t id) { bool ret = false; uint8_t *salt = NULL; uint32_t pkcsv15; CRYPT_EAL_PkeyPrv prv = { 0 }; CRYPT_EAL_PkeyCtx *pkey = NULL; uint8_t *msg = NULL; uint8_t *expectSign = NULL; uint8_t *sign = NULL; uint32_t msgLen, expectSignLen, signLen, saltLen; msg = CMVP_StringsToBins(RSA_VECTOR[id].msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); expectSign = CMVP_StringsToBins(RSA_VECTOR[id].sign, &expectSignLen); GOTO_ERR_IF_TRUE(expectSign == NULL, CRYPT_CMVP_COMMON_ERR); pkey = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_RSA, 0, attrName); GOTO_ERR_IF_TRUE(pkey == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(GetPrvKey(RSA_VECTOR[id].n, RSA_VECTOR[id].d, &prv) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(pkey, &prv) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); signLen = CRYPT_EAL_PkeyGetSignLen(pkey); sign = BSL_SAL_Malloc(sizeof(uint32_t) * signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); if (id == PKCSV15_PAD) { GOTO_ERR_IF_TRUE(!SetPkcsv15Pad(pkey, &pkcsv15), CRYPT_CMVP_ERR_ALGO_SELFTEST); } else { salt = CMVP_StringsToBins(RSA_VECTOR[PSS_PAD].salt, &(saltLen)); GOTO_ERR_IF_TRUE(salt == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(!SetPssPad(pkey, saltLen), CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_SET_RSA_SALT, salt, saltLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(pkey, RSA_VECTOR[id].mdId, msg, msgLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(signLen != expectSignLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(expectSign, sign, signLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(salt); BSL_SAL_Free(msg); BSL_SAL_Free(sign); BSL_SAL_Free(expectSign); BSL_SAL_Free(prv.key.rsaPrv.n); BSL_SAL_Free(prv.key.rsaPrv.d); CRYPT_EAL_PkeyFreeCtx(pkey); return ret; } static bool RsaSelftestVerify(void *libCtx, const char *attrName, int32_t id) { bool ret = false; uint8_t *salt = NULL; uint32_t mdId; CRYPT_EAL_PkeyPub pub = { 0 }; uint8_t *msg = NULL; uint8_t *sign = NULL; uint32_t signLen, msgLen, saltLen; CRYPT_EAL_PkeyCtx *pkey = NULL; msg = CMVP_StringsToBins(RSA_VECTOR[id].msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); sign = CMVP_StringsToBins(RSA_VECTOR[id].sign, &signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_CMVP_COMMON_ERR); pkey = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_RSA, 0, attrName); GOTO_ERR_IF_TRUE(pkey == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(GetPubKey(RSA_VECTOR[id].n, RSA_VECTOR[id].e, &pub) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(pkey, &pub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); if (id == PKCSV15_PAD) { GOTO_ERR_IF_TRUE(!SetPkcsv15Pad(pkey, &mdId), CRYPT_CMVP_ERR_ALGO_SELFTEST); } else { salt = CMVP_StringsToBins(RSA_VECTOR[PSS_PAD].salt, &(saltLen)); GOTO_ERR_IF_TRUE(salt == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(!SetPssPad(pkey, saltLen), CRYPT_CMVP_ERR_ALGO_SELFTEST); } GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyVerify(pkey, RSA_VECTOR[id].mdId, msg, msgLen, sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(salt); BSL_SAL_Free(msg); BSL_SAL_Free(sign); BSL_SAL_Free(pub.key.rsaPub.n); BSL_SAL_Free(pub.key.rsaPub.e); CRYPT_EAL_PkeyFreeCtx(pkey); return ret; } static int32_t SetRandomVector(const char *vector, uint8_t *r, uint32_t rLen) { uint8_t *rand = NULL; uint32_t randLen; rand = CMVP_StringsToBins(vector, &randLen); if (rand == NULL) { return CRYPT_MEM_ALLOC_FAIL; } if (randLen < rLen) { BSL_SAL_FREE(rand); return CRYPT_CMVP_ERR_ALGO_SELFTEST; } (void)memcpy_s(r, rLen, rand, rLen); BSL_SAL_FREE(rand); return CRYPT_SUCCESS; } static int32_t TestVectorRandom(uint8_t *r, uint32_t rLen) { return SetRandomVector(RSA_ENC_DEC_VECTOR.seed, r, rLen); } static bool RsaSelftestEncrypt(void *libCtx, const char *attrName, const uint8_t *plain, const uint32_t plainLen, const uint8_t *cipher, const uint32_t cipherLen) { bool ret = false; CRYPT_EAL_PkeyCtx *pubCtx = NULL; CRYPT_EAL_PkeyPub pub = { 0 }; uint8_t cipherText[MAX_CIPHER_TEXT_LEN] = {0}; uint32_t cipherTextLen = sizeof(cipherText); int32_t err = CRYPT_CMVP_ERR_ALGO_SELFTEST; uint32_t mdId = RSA_ENC_DEC_VECTOR.mdId; BSL_Param oaep[3] = {{CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, &mdId, sizeof(mdId), 0}, {CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, &mdId, sizeof(mdId), 0}, BSL_PARAM_END }; CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); CRYPT_RandRegist(TestVectorRandom); // encrypt pubCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_RSA, 0, attrName); GOTO_ERR_IF_TRUE(pubCtx == NULL, err); GOTO_ERR_IF_TRUE(GetPubKey(RSA_ENC_DEC_VECTOR.n, RSA_ENC_DEC_VECTOR.e, &pub) != true, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(pubCtx, &pub) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(pubCtx, CRYPT_CTRL_SET_RSA_RSAES_OAEP, oaep, 0) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyEncrypt(pubCtx, plain, plainLen, cipherText, &cipherTextLen) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(cipherTextLen != cipherLen, err); GOTO_ERR_IF_TRUE(memcmp(cipher, cipherText, cipherTextLen) != 0, err); ret = true; ERR: CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); CRYPT_EAL_PkeyFreeCtx(pubCtx); BSL_SAL_Free(pub.key.rsaPub.n); BSL_SAL_Free(pub.key.rsaPub.e); return ret; } static bool RsaSelftestDecrypt(void *libCtx, const char *attrName, const uint8_t *cipher, const uint32_t cipherLen, const uint8_t *plain, const uint32_t plainLen) { bool ret = false; CRYPT_EAL_PkeyCtx *prvCtx = NULL; CRYPT_EAL_PkeyPrv prv = { 0 }; uint8_t plainText[MAX_CIPHER_TEXT_LEN] = {0}; uint32_t plainTextLen = sizeof(plainText); int32_t err = CRYPT_CMVP_ERR_ALGO_SELFTEST; uint32_t mdId = RSA_ENC_DEC_VECTOR.mdId; BSL_Param oaep[3] = {{CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, &mdId, sizeof(mdId), 0}, {CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, &mdId, sizeof(mdId), 0}, BSL_PARAM_END }; // decrypt prvCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_RSA, 0, attrName); GOTO_ERR_IF_TRUE(prvCtx == NULL, err); GOTO_ERR_IF_TRUE(GetPrvKey(RSA_ENC_DEC_VECTOR.n, RSA_ENC_DEC_VECTOR.d, &prv) != true, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(prvCtx, &prv) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(prvCtx, CRYPT_CTRL_SET_RSA_RSAES_OAEP, oaep, 0) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE( CRYPT_EAL_PkeyDecrypt(prvCtx, cipher, cipherLen, plainText, &plainTextLen) != CRYPT_SUCCESS, err); GOTO_ERR_IF_TRUE(plainTextLen != plainLen, err); GOTO_ERR_IF_TRUE(memcmp(plain, plainText, plainTextLen) != 0, err); ret = true; ERR: CRYPT_EAL_PkeyFreeCtx(prvCtx); BSL_SAL_Free(prv.key.rsaPrv.n); BSL_SAL_Free(prv.key.rsaPrv.d); return ret; } static bool RsaSelftestEncryptDecrypt(void *libCtx, const char *attrName) { bool ret = false; uint8_t *plain = NULL; uint32_t plainLen; uint8_t *cipher = NULL; uint32_t cipherLen; plain = CMVP_StringsToBins(RSA_ENC_DEC_VECTOR.msg, &plainLen); GOTO_ERR_IF_TRUE(plain == NULL, CRYPT_CMVP_COMMON_ERR); cipher = CMVP_StringsToBins(RSA_ENC_DEC_VECTOR.cipher, &cipherLen); GOTO_ERR_IF_TRUE(cipher == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE( RsaSelftestEncrypt(libCtx, attrName, plain, plainLen, cipher, cipherLen) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE( RsaSelftestDecrypt(libCtx, attrName, cipher, cipherLen, plain, plainLen) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(plain); BSL_SAL_Free(cipher); return ret; } bool CRYPT_CMVP_SelftestProviderRsa(void *libCtx, const char *attrName) { GOTO_ERR_IF_TRUE(RsaSelftestSign(libCtx, attrName, PKCSV15_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestVerify(libCtx, attrName, PKCSV15_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestSign(libCtx, attrName, PSS_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestVerify(libCtx, attrName, PSS_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestEncryptDecrypt(libCtx, attrName) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); return true; ERR: return false; } bool CRYPT_CMVP_SelftestRsa(void) { GOTO_ERR_IF_TRUE(RsaSelftestSign(NULL, NULL, PKCSV15_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestVerify(NULL, NULL, PKCSV15_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestSign(NULL, NULL, PSS_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestVerify(NULL, NULL, PSS_PAD) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(RsaSelftestEncryptDecrypt(NULL, NULL) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); return true; ERR: return false; } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_rsa.c
C
unknown
21,532
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_kdf.h" #include "crypt_params_key.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { const char *pw; const char *salt; uint32_t n; uint32_t r; uint32_t p; const char *key; } CMVP_SCRYPT_VECTOR; // https://datatracker.ietf.org/doc/html/rfc7914#page-13 static const CMVP_SCRYPT_VECTOR SCRYPT_VECTOR = { .pw = "70617373776f7264", .salt = "4e61436c", .n = 1024, .r = 8, .p = 16, .key = "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a388" "6ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640" }; static bool CRYPT_CMVP_SelftestScryptInternal(void *libCtx, const char *attrName) { bool ret = false; uint8_t *pw = NULL; uint8_t *salt = NULL; uint8_t *key = NULL; uint8_t *expkey = NULL; uint32_t pwLen, saltLen, expkeyLen; uint32_t n = SCRYPT_VECTOR.n; uint32_t r = SCRYPT_VECTOR.r; uint32_t p = SCRYPT_VECTOR.p; CRYPT_EAL_KdfCTX *ctx = NULL; pw = CMVP_StringsToBins(SCRYPT_VECTOR.pw, &pwLen); GOTO_ERR_IF_TRUE(pw == NULL, CRYPT_CMVP_COMMON_ERR); salt = CMVP_StringsToBins(SCRYPT_VECTOR.salt, &saltLen); GOTO_ERR_IF_TRUE(salt == NULL, CRYPT_CMVP_COMMON_ERR); expkey = CMVP_StringsToBins(SCRYPT_VECTOR.key, &expkeyLen); GOTO_ERR_IF_TRUE(expkey == NULL, CRYPT_CMVP_COMMON_ERR); key = BSL_SAL_Malloc(expkeyLen); GOTO_ERR_IF_TRUE(key == NULL, CRYPT_MEM_ALLOC_FAIL); ctx = CRYPT_EAL_ProviderKdfNewCtx(libCtx, CRYPT_KDF_SCRYPT, attrName); GOTO_ERR_IF_TRUE(ctx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); BSL_Param param[6] = { {CRYPT_PARAM_KDF_PASSWORD, BSL_PARAM_TYPE_OCTETS, pw, pwLen, 0}, {CRYPT_PARAM_KDF_SALT, BSL_PARAM_TYPE_OCTETS, salt, saltLen, 0}, {CRYPT_PARAM_KDF_N, BSL_PARAM_TYPE_UINT32, &n, sizeof(uint32_t), 0}, {CRYPT_PARAM_KDF_R, BSL_PARAM_TYPE_UINT32, &r, sizeof(uint32_t), 0}, {CRYPT_PARAM_KDF_P, BSL_PARAM_TYPE_UINT32, &p, sizeof(uint32_t), 0}, BSL_PARAM_END }; GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfSetParam(ctx, param) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_KdfDerive(ctx, key, expkeyLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(key, expkey, expkeyLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(pw); BSL_SAL_Free(salt); BSL_SAL_Free(expkey); BSL_SAL_Free(key); CRYPT_EAL_KdfFreeCtx(ctx); return ret; } bool CRYPT_CMVP_SelftestScrypt(void) { return CRYPT_CMVP_SelftestScryptInternal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderScrypt(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestScryptInternal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_scrypt.c
C
unknown
3,597
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_algid.h" #include "crypt_eal_pkey.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "securec.h" #include "bsl_sal.h" #define BITS_OF_BYTE 8 typedef struct { const char *rnd; const char *sk; const char *pk; const char *context; const char *msg; const char *sig; int32_t preHashId; int32_t algId; int32_t type; } CMVP_SlhdsaSignVector; static const CMVP_SlhdsaSignVector SLHDSA_VECTOR[] = { { // https://github.com/usnistgov/ACVP-Server/tree/master/gen-val/json-files/SLH-DSA-sigGen-FIPS205 .rnd = "FD3FE9ADC298FAD0BF5A08D804C690B1", .sk = "F9FFBDE3D5FB47C2669823076777A941B810BE27FEF66DD4353B4A21DC972842EA5EE0F3E9B301B5CC32814C8AC6CDBD8BE5D429B0104F2F0A929099F687A74C", .pk = "EA5EE0F3E9B301B5CC32814C8AC6CDBD8BE5D429B0104F2F0A929099F687A74C", .context = "F29200E32E0DCCC92F029503DF3C878E340569C54169A31F7704A16EEBDF5667D0A5DB865F486F67052B1765B772D461C9F4B75B2D6056CD185F2C9D74D1F446674D75B49D5082EC6097269837BC7FA3A2163FE026BEB4455A79D7E135D0FF2BC44E213715767808D4DD1AE2A6B147F041616D74FDB89589848B2B155957CA7B79CC497EACC1050F8A3C98BB4839D1858F", .msg = "3039FF4B78F80DCDCEF91B8602456D99621B7736D6207A60B9EDF5C662587DA6B2189FC463DE94448055CD3C65C877325F1EA0B77737A8CFFD63290D3D8E229B35804F7A78CDB703D2EE6E7EF078C3E2FA325585F3B326019317911BE16633EFD4BDB429AB7265B4A4936A9E393BE78F7E0B74707DF758EAAAFAECDD815C973C8DA8BAD70842EE6EA3D27D4C9DD1FC7FF5137779A3C0978E95BD6B99E0088721DF086F35BBDCA3F68417301585EEA71594CDE5097D7744FCDE75B4AC387386D4E8023A45E6904DDA1CF4F0E7BC75034969A30C67DC6F231A4D56C608B7BFEB75FFED8B893811B9A134C178A1919252A47E6B6B23E885C1738C7257A031829C9596827A0F1A81B5D5C7F5EC9EE3B08CA5F6F88A545581F779FDA50BEDE7819621605C511E37489D08BFC1A4E0FFBF1997603E21C8A3FCF1E7A0B158B78A75976CD5EB5DFC47EDE317C02E514768982B555D96637EED6B0DBD58990B1B605D3711080D782A14762DC4BC0F33B1BA4A80ACCDB915C2869F7A4E16717375B1A027F4DF0B92E944F80E7258EA1E68B9232FCC0EBCBB8851171C9AF7BD2323E986AEDA77EDD4E9178F68884167D9A33A710B39955DFC607B9EE2B4F5A978AD814FDA328D59D9C55DC4290B1D362633CB11446F6AA473C3B93C3350B231AE409A85BF679BA5B47FE175DD0820B69DAFEF94E6A3EA133340B775D842D8BB3A3BCD4AC1BAE516C8B2B52E839DE52B39927F0C0CED6D7C8B5C3FA57D645769AD6413C3335A481CBBFB82531F756F73E635D930AC851F4EFE01A40AD6A1114B05D4A3F210CB6013C22B922B3C6B7D306616468FAF5D3E5156248E946D0B474D27BFBE9DA5D5892CC5898DEAD776728C9612151EEE380AC47249C79AF8259C501669A2AEF714427B3435C8BA21B09A1EBE86BA0A5B5EB7C96DB6B1E750BE628D44AF9B72953012501C717CA602787A243018585AB3E100B1DC37DF0A645EECB0DBCA339CD4CB3716E06DEB5C0F93014DC116463D6E3E58784C703ECD47F3BE07CD8C3A03C63B3A7756C6980B594C221A0427FE2366BA3C15CEF9D80F428DDD797945A932AE0674BF3AACF7AB5426722480815A256D374D76BEE80A3AFB75234D72ED32CB71A29B5F4C6B5BB330FBFED834D38466DA5067AB111AD54A65C02B966028A652FFA6F805E73575D4E35E9194326FFB260B83751940317269A88395C1403E11C28F31482FF98FE95069A7DC83847DCDFA826BD571EA368347D6F194D186DE7CBFE11511006A7D92E0F5604FC378485B3C551BA0BCFEF3D8A131B2AD8B70DFB1B96B55CCDFBA23162228D365C3D7B5FA837D05B13797E715AEF5CC2380A0BAB45B23277973CCD942065237A314A1CE6D55334D46243AD5E0E4C898B8FC29695ACCE80791440B2DD1C429EB0C77CCDE47AEC84CE9FA879C62B56B85A09E9D6BF25FAF70F2E97C35A21C48CDC09D8A4C69693755681BC8093CDAF00E7AAD4A1991EA8996C336477E043068B204C37753C3E632EBB72F557BF673A6396781660ACF4C9137F14D329C54EF84BE96BE7DE214588FE1B8196523227AE4F6D39C6D89EEFE7181A68BAB52C7EB051D5278F5DA6979D6D40A550940CD38C39CA0F9885A0CD7368C99E65CEA2C9936C259335CE51784F05BF67A60248B0B680376D8ECFEAE5F51AE5D0C5F59D4CD4F6B7A944B363B39758CF836B95E869399F94030B87C04B157A56A971F8C10E892B0D9E7B82404A8342E54F1AF0504BB467D997041667D285233B950DF6F68CA7DA73BBD1ACD11326A829735A457E410F5490787568E8FCD6DF3B4CA576E2BCB63F9489316F9054E05F0FED134EA1B07FD11BF0A3BCBC3C63B51B71F1B16236853CC20C05FE221B45343531954CA1F2513D0E07AD77E9883B9A87537AE5891590A539C54102FD8DE8B9689D34F7C6EB1896578AD6F2DBCF02030EF37C0AD4BDC16A396C27BA98B8B7E50A7936FB8B4A8DF36E5F935A8FCA01518FE9BBA562F332AF88C04254DE76F5FAD566CCDD1081FCCA98DEFE076D4A3A7BB106319EA38D4BD756577D880526C26C56F0A4CA45C267B1785F32AECA8906F648477BA0D35CEB7A21029F88436519DB7DC880644082C33CA714EB8F077CE9E5EFB0ED2EF24024E764E54D08FF0ED653EE3A1BA47EE6B735667DE10844405F7E1B9E8719C90805996D0F5AF7B829BA981D2FDF66EAF58466CAA62707E991F6BFED0532B996E383CD9106A9F35D81DB9D85CA9A3B004C735E482DFD4CC5A6901702B88993C73062B76AEFA36E43DC84C7F2ECBE89A8F9686698F9F9F99AEEAD4464FDDFE259CE8A4444433478A31C483217982B62FB0504D29AD3624174B6FA9F58567A9C1EAC601E5FA98EBDDC0339FB4B7EA0957DF59AEF99EE9EB9ECD9EDD301007BED0793B0C82DA95885E4AEC1298D4F2B628A0B9FD433823FCF749DC7B83F636C9D2CC74A66D13A910DC9DE61C9BE0347A4822D466D9DA8A451FA81F2CEA7226456687F2CC4F3BD8FC0692D7CE7D42A51675C6EDA93B9C50C598C3E1460FB0A3B46B645F40F7A94136CB1003299F230801EF2FDC8E2692A9C5C7E9769071A40C0ABC77F1864494465255143CE56218AF0F5453EA1CBA5F2B5CEAA3D53DFB964A07F565414F325C55DA4B97ED51EF94699C277227BCAC026FFEC623E3996E91E942662B79CBB4D6817B0F971E2A5DF180CE3D43CEE5D9B0336F30F625B3D8657169C14B436F62A6E0F558006DDF71A0F4EBD7BCFEE0FBAB8EA99ADB7295CCD97C7D768FCA52DD578A3C24C991675ACD4AA38BF18D55E2E8F98B9D20C7ECE204BFE968C7ADBC4380DD6411E0C8F4372CCA61C855998BC2E544244B0DD2BABC9C1DECBEC893242F13F8257C503A270C459970DB8388FFF17DB1FCECDCCF3BA65AB54C08D539D19CA5E551558B0874E131001B49781471E825C2F79930CA14644DB085C51C580C60E2E9F0F57125B3744B2DB6F0348856DF7A6960E19AB48695DAC27A3E7DC00FE7F04FD2CFF3EB5286A3914DB760EE26C7EE02852C40FDB0B5B9E4E1C3AB570699E416DF5B5045E1FF95A1DE4A693F7ABB8625958359F3FECFAA2214D5EBE82782DBB6F7DAAAADED8486DE203ECD5AD7CEB9CCABD2D9E24FDCF18A083DC967DEB5DFACA5400E338831F76F65A9D0EF5AB260DE3DCA9D062E80F7050E33DEE5F3EC88F02E270D37ACF79749D9086347E1FFBEE622BD5466A60ED3B378BA2B207BD178C9F1B2EF1899829B91314315023370C039535AB66BB579A21E3FA7BA4889C88DEE0B97B8A2391140D82909E0482C125A3303B4E808C08360A954282ADA5BC59B118C42158EA4C4EE3C6276EF1523F6476982160D24DA193120255C66274EA34F181E033F747CDD4911D8C222F1A38605BB50787EABDDB915CBB6916CEDD5C7FF8568929B29B298E201C1EBDDEFE5E8B292493CEE4D28B8ABBA2CF10C5EDF0BFDB6B207DA55184FF73FC6A764897341BCC3F04BF8D37DB18C43E8B478A9D7A8A825745130A2ABA2DE420F6CFFB5FB946EA59BE40787307108E2AA86F8F69207ACC55C5F1DE1E657A7F8D32944859436C9E1AA0BB1846E0585923317FA539AB16B54F293ECC57619B04C68758CB5033BFAA4D993A7FD5B9F59482593FA97A30F6D5FDF48C18FDBA27552090C23C74EAA252EAF9E6208114A9DB422C767EB6BB9588B3DDB602969A9423C9CB848F8537C108D7F6651FC9AAA3A3F722B391DDE110B510576435E37A594009AFDB34B8A0FDF5BE546FD770B5E01813997CFFEAADD394F04D20832D8F6D37CDD5C5B9C2B74D74129E3FB186010240EA81B540B22725DF699DEC06151083FE15F5E872F6CA99C8EA3F17107D3D32EC5335A3A17D733DB648BC845C8037A339AEACFEB8FD55769E3332C3FFDD7E9BEDFA7C4DB85A32AB04BED5C87D68CD0BAE2EE48F091B2D5890FDFDD41877FC8C1139ED298BE5C21E3DB619E645F6749C074B3BEC599FC5CB1408238A6860511EA52BCEF1F4D456EA0B4E1ECEE64160C4886644698691A7E02BD20DE2CFDD5C4C84F027A6BB0CF25BB4C27E017FF1AD11EAF367C4ACD61ECCC289FEE7200BAC894F9EC083634BB7F02AF14FA55E7A6A2AC92DFE7C56ABEB45838AF272FD2D4D0467DC7BEA1C664FD96FCE60982B9257C50CFC146D101455BB78DCF9A32527BE9B896B47B63F6726DD9B0E90BC5754228D3E27AABBB8C67FAF82BB414343EAE8007AC7EC4E1F85ECA7B7DD238FC3E876CA103D8E0D28395365B9574C2C1EDEAACE9E5494DE2E42CACC24D2E51C3C2906DED76011FEA976551F960A80DA42117A9CE95FB8DAB75E2A322C37738D48F9703A318E3D897B942E472F5C2C4874E6F3A8502E15AC63485D14F99D82D7CD746BB25305C145B9DACAFF0C2C13146313CFBF0F4101A4ABD33002D3F76446933A7342609F6C44A602E951D2C17FA35B311C5DD4160230B6DC41BF4A7748A33CADC08BCF6E458F6193D51171C880FD828A7A1CAA90E453B632A0B61EDDD87B145B2A53BF022DD55DB6EEB879C8736782608D22E26D0E57017F9DFD439336505C4ED9E530035E1FAD6C89A8E7E5D3543FE459552E34C9FE405A671D21CB41D75FC69E4D1CA35BEBB5BB57BC5B5EA1689CBFC8857A9626EA9BE6988BEF13CAF528D4F7894E6003F879C3FB452F55E4E8D65630FCC06AAC575FECBD20ABC5AFB0F07702303777BEDDAC88E016FD76D7491734519ED4CB6C3B052F010D3DB08169EAE8CBF69EFC1D37E505B83C95B17650E9FAE13D3F9A99BC6DA12C54BF93D364C585D863D7C133F86181297E87D9EC3173B091112546FFC35C93B7D2E48EC90E74401681E2FA118B0C06742F5DF650258410E378A257A4DC07CB6DF94C4FFEE5A07764D80F604257394378DA23078EBA8D0412E3747985E91815E225515ADE84D09726DCB0DD39A6F457F23840CCE1F9F0BF441AC4DF8CD75425A5390736BF3B8BC99704F17F79D12EE7C30140EA1ECD5EF96C00865A4E77AC184C2AAE588EB76933125F8A40F35CBFDA75134738FF9050539195FE186EAE191155F4DC39AB93FB1A3A308A6F20E08F6A28F4F86789021D185459FC89C1A373ACBEAB3182B6E8DF7D7AD03B0CB8B4BD8E1FBFCBE24AD7CC877783128B1175EA753D3313DE7EAF19E4E7434F558984AB76D50DFFBC18B0EFD279B021D0CDB6B0CA35C481642B929C0F7BE4932ABA46379963E031C2302039CE973F611AAE95C97A6DF99BC11E1F5716D03FDC527F8701EACAC07697312EB3C1859F7372B97C3F583702A1BC210269EA7762F0F3E1BF6E1AB21A86EDEE008D1FB5CCC377F9DE89D123CE11DD91C40551BE6497BE4E798E49176D51334D631648E033703654184F1F213F6D60087578035DD440F4496D5335F3178038FDAB10912E1A4309D753360F58E11B91AF7160A7964EEE5DF44463234D57898A1798003CD11CE1692D1E523C324538BC90A08D7CA1A7D0FDE7E1AE1F837018299F6844919DAC422CFE125F673CC64FA253E6A3657118F26C399DE91339DF8B7C759F0281E79C9334060E95A4140A8BC104E7EF0EEC9DB891EA3C2E30D0482B83B66371C897548A32F5800339BADCA867DEC96B656331DFF75799D671CF98B409384070BC726A8A704D4240BC85102BB0BDFDC9945163D4371FE42311C4D785363C9849E69D5665985DCCC94778EFDB8ABB9B4954CEF0A182371E12D7F2E83D1699B9AC609AE75C07529884F2E61B5522CC2609F1FE1EFFD8F8E4CDF96DB830E2BFBF52CE1297A0B59D4773BEC99A670507709033CAFCD5EC77BF084BACE94ED6575E9D5D105CA35C59D4EEC40B679801206AD2AC8E88415D0C632AB5F40A9A7C1B7F2661AB977FEA2D1ECC0C49E24F9274FC4637655A457DB038F48046C07B15E138B64B0A0D0D76A23806D405F195F0F361766F207BB4C1CA206EA91E122223F2C20DEFE34303EB6B76A31A2C1FAE2EE202A268E46DE7EF18999E0DE868FD455222E3B420780395CA6ACD186ACD5B582E57120B677456E6863E67E6D8CE46288F0DE78767DC75F41E09DD4B446E2D708066DC3E917C5339BA5B80A67C119FA7A61176884DEBCF0A95A387BAAE013206D4D4086875FC3A0596256326D71BA795B48B4C8674F25488DF423ADB71E96F1D8267B223B75422F297FB7AA2749E5CC07EF9A40D2EC46E86F7BFCCA8EED1F100D7D7C523DC268F68987B4AE3795C489BEB29AD62FA36E5478B9E4A46BCBB6DD2DEF40EBD2A37EA2C6C9E44140BFA1541F679545CA4C004D694FF6BC642FBF6DC8F56ED9E9E4F154F765232FF1D8A36F7649D32A397ED29BC929D0C4F87607E9F8D519E42F189DF392CEC49DE8C9773913EF336C18B843E1DA9DF187D78323BC020BF86812B65EAAB5ECE27C60AC39BBB1A8471C330AF5F13E2B9B8EC252DF754B254A228428E25A0B67760EA9810D752BB4DAB6F86FE14C2D7D90F41169112CFA8CFD6E22656F8B5F408B1457236ECE20ADF8D37FD90149936C87F3B3419B967CCD3218502E2323875990FAB874B9B5714435AC008DFA2CA44E9C4589F6E5EF3B438910D8E013565AF8607906A6972FB210EA9830D6DA05DE0AF863847D1CE8D396A150A46CC65CDD6683DA93D9379DB121C3A5DFB886DC932E7D9F3CF1BCDDD2F4523D43495075B6CA51B6B36D345CAA3455415AEEDC25D4E599F5E17C262E1D3648C735E1D107621E94BCDE5AC22D235F1C6320DA141ABB871F1550B3934A862F11759B57BBC697751B6F0998ADF3FBEAA875C7AD5D2EACE31FF0A7697C7E8D9D548B4EBD17CCD4E3107E08C1FEDB78C77ECC3121E8DBBA5F33B32490A9A8F32430077325BB79B7DA97AF89188EE097438051116FA2BDA5C8B10937CFA66C93999696A1951FF819903CD4DF8DA46C696A82CAFC3D428ABBD19D931ED40AF3AE763D2D85F74B190E7C8F53F95E8F425BCFDE42C255E2D0C71C8EECAF2D75A8E436326FD0003C47AC4520DE0D09A649BE4CC0937DEF29E1E50D2DC13FF85F2F5B9C5D9FA84E8F1C635B31EC36C7DDF5DDCDE40E5CD04542D6DABD5F1C9FA3DE3256A6EBFF8317A25B1D64C231FFBBA26C4A61E2D6918501885D23BFF0EA3AC71D62ECF16A4A403EB0BAA0E2D2CD9FF99AAE4A5767C1401E4811363FC2C85DFB003690D0F95550B7A1C8A316B0CE12BF9CE6884EE63AFDAE32E647BB01E20754D050B0E48D3BE022FC3091646F720590CC3821CA1C2E4CB11B1D0CDA784CE7332D1289B55EDFF7BEB1A57C32EB18CB3EC0C008E91635675A060FC484FA91C878637D7D3AB1C550D52137FF848852C0DFA1066B40C54BB1AB4D3A7BAFB419F6B0290485ABF86D442C01CD34C7E0B49482BE799EF042F17CE82975A7F6F251A256FFDF381CD47E5840F6AE324134DB21F5306D005770F59062205A2A50AD3BDA340F68A301A9C5EFBED00EE6D2549D4353C65531CC85B2BFA2E1E2E1C43D294EDC8187DE772FE84A613A860930987245021E677AFB175BBBEEE0D2E9F4272945DA3E7E4599A062BB4151482841A0A075CBD708EE83038C5BC6217FD9EA299BAA62C8A9D00D3D1C0B0DEA7D887321DC6DBABD1390D18C0D840E8C83B6AC6FA6BFD9BB491D0D5A32DE67DA7A90879BE849D72B4991E87566ACE5B72B401C83E6887B05245B6BB398034EFB053838F33B39CD9F3992F821C4FE17B5D1A65E80DA6D12B384C3D5B5E810BD41B2AC747D61B75E89C1F6F0F93B00C8FA1054C4564501A879B59AAB99C38E962A8B34218DF8270996FCA48282E6C84E2F5D99447E3B4975D43A6CA56B4D5A2E4379D7C1892F269354D2FF9FBBD6E2E65B05F8F4F1D20F024DBE51B38C7CDC969C605865CD245328518B168E3D973426EEF82247E3F68E9CDFA4E6DD58771E6B613D36EFFDD1DE7E4DDCEB806EEEA6339BA92BAAE83DD3D34F5536ACA2A0043D0B857DE4CACFD019A30A85B6CADEF21651887E5CB52AF0DE2101BFCDEA8288D292B55E6004F7BC1906E0D5D81CF4D40F23212E2A7297F26AE581DC87E9D7FA79E2A1AB9EED15377FB3F0F4A794D560FD88A8A5E5FB6B733E544C763C776F0AFB886AAE7CF25DB8C8072A95E6D546B65BBE53F6F4D282E3901D9DFCA6DD3EA949D7CAEE4B311E517F9D7768DFFC91DB51F2537B21B8E2C576B5DF9A43DC553F6F099277B197A8AF3975AED697F99970B8E0D9FB1DADB3DB4F7B86F4E158C62D730F61786D49BC2AED7129D5933141FCA9C1F30A0AC84FD082FFC16EC29621624A4107D9F54332ABEAE4C46C7E35DA10EA488A02BB116F7AE856BB9FF5A70EB1DEA70A6E412B4534C3AE1B794337273E9EB615D148FF0954AF49043A78F559065D07A4A57379C7FF1AD302C4651FC4EC9134954FAD6CB9824C7EE7092B5F07C57381980824B8723406C33E028256D297522F9A80BBCD87F60FC85EE5ADCDC359D5841A38B6171C2CB4501C3463C94BAA6394D6EEB8DD4A8EB94F1F40E5268422F8A1CBCE00362C291E535D055B40452F09430B32F0BBFAA1521B6FE2403A8011567775A8F855EE6FC7C6AAB37E9DA92931607A6637DB996E4AC423EEA5F3F0C807DCA3046A768F84AA430D89294466760A15D5310288EDF97CDF1F88D998B2D550D3F8E413B33E07917DEE25D5154D694DFC8658C8419CFFC0B6B8A87D20019869E39F77AEB372906E9D729F88AEB034889DA9DA301BB8F04BA6988644ACA4041C76D4854AD606F6D7B7748031E7A6593DFC5522251E0F3D69EA4DB5A94A48E31EA4862E07ED30DDC0CEF93F5921DA995C41D08C8FC3A8A3A5DC737BB3C7C4D0F7CEA10C80039A0E89E4E6CDC1B419DC65007C6E1A3FB1C0B10732A2FBBAC2A69CBAF472FD4099D8B0EBCB41835AABD77E6E35756D608BEC409F4D1ADB4BF9552569276C8C984B9A76E9CFA62BF260FE220EEDF50FE70FC327810A49DB0FC27F91F733E358CDE56C64445D7C24B26D4BE8282A9ECA8B4C594D80397204AFDFD366F358983D48274CACB8815B415C4461F30FF8BAE32973EA90702C7F1A9224E5A09ED96813605AE1E30DC73D75DCC97900540429947F71405C6BF9982A0A5A74F5F7380AD644B5F7388D3A5469CF568908568A74061ADCD8A9AFADC858936E1D62310DCC65D2D08AC80B4370571AF00A34512B9827BCA502C39DDDA6CF7D037CFE916F59D98ADA34EFB130AC1D9F7586567D465B0F6CAFD18AE6564006BF3CF8B76110CDA146877BAC3B04D78D30E91ADDE4650C76BADD0E5F10A06006876FF488BEDC06B9536ABC89BD15ED7A0FE22B475DC776B3C5B79467C9210C031038C2297FE6705B3077A45B19D12E4F45D7811CA3713E4999736F2E383730F8C6D55AEB64335290FFCEB89B100F2634E6CC9FFA1AD2D86B23275DD2766F2AE913B56A4006141BE5D29BEF7BDC6270F646E59E622143AE301561FF8019242CEE4A94FD81056D1A842FC13EC803EDCC22A876F7A25D747E113670AD20984BA61064F269A884186C3D88FCD69F55E5A1C28298CCBF3590E1B33D829A72AAA380066093ADA5B04EBA74362C333F28BB94D1E357C3185107A3129719F63F8DA269BC3BFF5BE9B6841946B01EC7D8A279CD34C22714145FBB05BB7C642ACA946B92FD05D6B8F6EF57F8F86B23D457C21F76C2E48BAB1F9F6F92F4F54A40AF7D8569857589F8193BB1BF6E47B483F1AFFB06BC13A74FFDF10887FEEFF4A8AEC540ADEF148453C8DC896933889D27276D81E89DF905EF2A747D0AC852D5A2A4E5470C6FDCF0739799FFC202D1C47BFBD0DAD391988EF6D1AF89E0881CECC89E8B3AF80499F8014013802BEFD79EB3550626B51A742EDFDE4CA567DD318618C5EF9AA0841A812185F447BF776C42771666110F34B1B7392CBE1286BA7EF687D3F898324EB51967155A66D1AD5A9DF1D260E6D3B95A105D21E9569D81977CECC6F41A155D15D7FC39CA69450FD822825C931D376ABC7BEE0DF54726B253327EA075AACFDDFA84EF97509034481E53690F08F0B3151C6271C3F810E2046F4DC9FFB1636242318B4D473254CC2E3FE61DD84C95FF1C313003D8ED006848CC22EC1DBE742F3C2BB6A4080DE546203A5FC46158AE275B6B8874E09F8EF9D5935B2C7975078E0448768773F16BA5A611A367E6D6FCA3F89B61254D7877B3B32A9573B38B57C3A2469A5DFCA7C2BD90D5ED585241236A070EB8D139A9D49FA5732E36AEFC7B9369F6A9094103B2B06D9BCF21181C26E16044702E1FC1BA4B4FDC61AF57305860F70574792283AE773DD792F3F5247B07228666075C7299DE6FCC8DF07BF088008EC44593244967C74BCCA1B89F2917DDCCDC9FE030E7243C9899C28A4BFA44DE8F321D0C0147E4C26697EE81AF31773256D8EABFCE1833ABD047DB080CCD5E3A4E50EA175C6DB6B69B6EF86392F58374A2CE57AA34922E7A4A7979B8FE80315EFB817632A51A01AD821F51FD927E244AFFD9883D2B5ABBC48290325D1B39D5ED6C8AE1B73F834D2B0C8304F057B10A7CD695F1D46D9DE8DD38B6B5F3450D31870FA8AF9CFBFADFF0AEF6DB1437F37B3884776A4CC0E5951B27DDB11F73FC21E7973BE3D2EA8EF440E1B2B903B90FBAEAD629F066F4C8C2F37B992BEF3BC1A268F99424F1DE526274DE5E1483D811B552CCEDE116641B7BBB2DC2254D02D29FF63591146E6286D7B84054827083EFB5A0E72EC2DAB8D8FA0D038678B50AA3C960CF98738DB19C818EC79AEF5E287E42A57730F0CB336E8FA81FC60BE3159E532FB3ABEFC6952CD0D7FAF549488119D30E6A8D545ED37C0BBE5E08BA82747B8310E27078FC8DAC32CC8EC3483E2DEEB1D9487F5E99AF35A02BA77C72C7F95F9B4233763632F21C8DC926049373A8B33BD1D0B3E10FBDB78619A26B191A7659D02236F1F168BF818C2A8E1C50364F4397C197E820C981D23325DEF6B37F471CE5773AE3B63F74D2BAF17C63E1F1DD650F56EA60375BD00A05ED30663A7A72098E6F0D2E9B5F998121CE44F1D487BF648B19702689D58CF3A5D54250C195A1FFE251EAA055697E807C7435C34F76E0483EF2FCDC79555B7810639719DF16F657929367B5CFFD5687F5EA7BE44D9C563CEF2267B7ED33934A06BA9EACF1736AF7588360A5B3D06BA5AAE90F0C9F6132AF0F888D4A0E4EDAF2391A853AA2007104F71A46804C65C57D6D7506C726CC658CF441AD627862129E03AC0A784DD81AB0B036D905ED27F0BCEA05AD21EDDE9F4393D313BDB1213BD01F34A73C21AF821A96F4A6C510F85F07FE5E84B33232D3ABE92C27D0F3B1A557FBE9FD4D2FEF1A6EB9DF53C0898870BBA4066F567837B91D82D62C3C1E3BFAEAB52F4E80B3030E6EB5FDEAEC5055821569460CED9C5A864969B1513D5DCF6BA3CBA3160C9DD901B0F30BB1FF8123CED314890C8ADFC58ECB3CEB5AB5B4027966FD6B0F1E29979E75D0EBB32A25C335C466063FD735680FDBC23FA33C4FD29CA635D6C3B6E835DA546142F3EEA1500ADF27AA8750A8AF3CC022F75403EB00B8C4DA547A30A25D6EEE56E887722931D316428F3C089676FBB00765135623303DE8B51C2074674D6E64B21EC6A4604426354E773429E96DE0FAC9E27D11C1200C1D8CE7B74F67228D76B825757771C4928577D4772F4EC9D06C6B8797F4C3", .sig = "38F090E96F70C349B2F7F5222ECCED39FBC011D899838B6385C547C489DB07F7E16C67FF2A72B3AD6ACD4FE29B265930C2917608DE48576D9D2B06BB314CB1978FED83802C953949192E91809F10231643F29AF9F24B0919C2BAE2A1B05B51EE4106E0C640211A4A0B2D7FF26ED68A7C03D8F2EBA9F6A815B81DBDAE108829E8C8BBBAEB5F9C54CE944EDA9F37EEA8D77A8EA5C5932E19F0ED53DCFC17E0D4C9A09F7AF37BDE11311DD7BE9834DBDD3319E1F2DD4F402A6A18318353066F0EDFDD2CD1C6A068BEB99E58E5A974435B598F71DACD3C5060E840FE0245D4C1B4B216EDB1A182CB4D8EE4B87D46556766CD189A6FE5D0F4000EEE20C5017BD568AE4C280084F785EA40E4BB64EB6DECDDEDB795C79E36D0C4E032E0314DD92155593F3175A49B3A4D66FD6380AECE85EC06512B6A3D28C8A132B0C9571BD8477468CB0D1F5F2D30076F1FEE8E02D859275F935D06AFE92956603D173D9022D2F1C9FD66E58EB712676BBB9585395EFE1E70BB62CB0515F25639B60183518AF6C80B0B96061E07D2B8F5EB9F88225258F82CC1D6A165CBADEB7C1DD015B9D4A09E0352AF9934002247CA179F5629805D736F531D4453BD9F629EDC5CD7905FE3C010B39B572D52938E62F1341A34A9EE7BDF348B84FEE3C24EC1D13E755377CB6404A32492787967D8DB5C05E42D07843412D1CB9CCBCB6A36DEFDC69DAE7F0012DD1ABC4345566AFACCDD6DAA7E5C764E87F14B636E7292F5A61D33D9F175F31828A8A0374B16C9214CFE9E4D543BC1E036C05FDA1E99F595E3E67E1EC9D6126618F2CA820381B9E3A73D85A2400A226E52508E6D8C9D2433CC30A482F84F6CF13345387693B15DCC704A9917348E0BA630F7268B819572DAFAFC0E81EE012E44F304D7EF11CA27B14E32DCD4628BB1AFB620E48538AED6296FC0FFBF7ACBA790A7559B3F232CC69F506201A85C5740D4106477496028663F215ACDB13AEDC7A20CDBD25788C3E9234A17CA60A04EF044877C66F2CA58432FE115C83A8430C9CD7D28FAA551A8A7971F6CF2EBE686260D08537ACDCD93621F883AEA5E020221CC6493BADEAFEA26B2AE3185707D9764FAEE3487D2F9DF9AD5446661A0A46B4F7257FF8CB3DAF7C86482278678388AACF10EE5E3DAC609BC1952F8264CFB013A129A522696E62D3466934F05CB1F0EBB14270AC48E0019E98AE58119A384C81E1BED12C3F49EA650029D4A3C04E093F9617346DABE1F9A71D629BFE2A67812D6441557BA9E055878F81E26136A0A475443D03DE5FE34D5027D380339FDBAF69A0F78A112C8E2C085C37101E424E35E4DA645D03671BF030253384364BCF99F27135671071ACA1BD7D402FF513BE24407E400495C2D3635E320BA747026EF4510B3BBE22B3266CFFCC42A587BB8DAF9325EDED5CBFF9213F5616D935C2C81D65BE85662D47A63BC71A97FECC21DA89EFACC389944F0CA94604A6E102E57B7D2C567EE185B1EA07F2752EB85D2B2B90056FCCE7D0FA40DB9D189C88449C50CE78884594081C0E0FB835EB919CD9995FEBCDB7177A84213809CF107DEA6EC7769F24AC4C7047EF7BC5E365A3DA7A074B8CFB5FED85539AE397CF7EBC4B55A99A9A3AF14926C59B487F6CB66170800B9A5FAF484B536B608F4C1AE5B7B6CD7AD09003956300F36D040A7CCE76F2D50F63ED9CB7158FC058ECBECEAAD7A02FDB6EE5703D451E4C832A02972934FC34DA1C1F6680DA3B15B1877B4CB25E05DCD32EDB00269042384EEA16C758FE1B32121CE692C5E327C2B93E64AF2770C2FE2B88DACC96525FB46A7EDE1D43F2ED9CC0122934514B95C08F1C3937FB8DAF833A7C2E32A26600F6C2A69667F60A7B4B81115FD32AE3577900E8B11F17DA57CD4FDDF19DC174427D05DE53D276F755AA8EDEBE83AB4A82D58349C6963F052DCE606A8A06075D91BEF2F16CA0178899FE4F3E7DCCC7B0854A31D95E1AB4019B834EB3004C4B7A63DF73C9F7CB0CE2E9F15C4DBCCF206F93837A20211EDCA3DCF5794B77579FB943C9584F19B48C14790384856C4F8A6AB8783C9FEC7FDD53EBD536DDF2EC5090F160CE1A72E346303CF647D1BCD8B8928EFDA579EB69CAFF51B58A77F48345D4BBAD29163DC8D4B9B566C2744DDB4C771BD38CEBA46DFD494655AB66D075F12636515A2688F23B39E92B63219A599CD1D15E66354C218BFC3EADA673653812BF3B2E5D760647BFB2C65EF64363331FA54609C3291F3285C3E76A5EA81E02080B2055BCD4687ED8DF984E1FF9859B1B85085732C376DC9060B1787C3A778E75B60DF98D86691D626BA71E50B37AB9D4BF96C4195A6866859B6EAB710A2F28E62908B0EDCE20F3EC5487B30BEBDA898A7BA70878332940FB4DB2E6CD8D248D6E0BAFA91FE51CD6C9057FED8785E7E5631424487A8E8CFDD5EE78A3C8C8FC882DA1E27EFE647EB0529553EE371B3E3AF6E5135CAB410F1444A1153B87BC8AE91782115CE446C4E742D539710D2547C8701AA5F865D095B0299B2AFCC22A35CBE23FC813175EBB281CEED1233CAB85005A7DED484E5AA95C72DCAF3BDA6C0AA3FE82725ED770B82DBB5F79B66A17CA41CC7B07D9AC34BA7C8717035F40F3CB3580B1DE12312B149EAA52240EF945A6EA788DC3BA82ED2881710F1FDCEBB1D6A50CC0C28018A2837DDBDB19D893305BC2DC5DBD2E1B72DD2D372F3884F794B49330855351468243EBCD524406A04258961A0046B97F2FA0A4E575F46C823D92368C63D80B3976D6DD8A109CDB54316477116D54C3FEEF3B19E4E934D684BB18DDE1B396FD056733364B24A2822B7F1A49DDB2AE19213E4E22B4D00CEEE93DB74FB1B2230CEF71A2BE44868B0BB0B905DB49A0B19B55942B2603E46E3B7D34AA3E5877E64A5D4EC487A56C3DAFF772176267241CDC520EFDB08919595D67C619121F99113CCF5C26E000DEF5D4202261FAD2481CC8A5FD73889AB3A565839B925883BCF44B404A2C6EA39327AAB30A0E9407BF7DF38538B931C16589ECB928623C44191EC1CC8CFD4069C6726424F2DBC11C0D81632C23CA7676256909917CA01F33070489842CD4FA05BBDCD3CE84223918119D3075BD69D9867A790076A3A31E536A712A75542DB482CB877B9DB87F04901C0B5BDBC65BCAC6C5CD7C1394AA497AC6845F850BCF6192F0060F46E2FA343A57B2CF025B834C9D1B0406FF538B2A2153AF183F15D19ECDF1BC9B77973F5C28900D0AE94BD0C1F178D2D63F4662D2DECF1CDADA12237C0465F502E4066689B3617F48BD9D15974430E173153E3FC701B3FFD7AB15301E7D4BC0CAC3ED9B14C188DA2EED61F9551EA3E92D3E0F9D501BEF5E6A9F5B128A49F0C61E596DDF1EBBC500FF011BFE2FD9EF86ECCA41EF3C0FA68EDE6A13BA13C37B96725C68B053ABD5C33B650BFB15C6FE1EDB015CB652269453E4182F8E1B973FDA6CB5471A092F5A30D4AA27787E7016936743FB561A0EF97480885EB9D83845E709A1B21E2A68AD5EA5F9575AA6E47D0E256A2AE0FDC35F0F85BAD7E8B79A82870D8664FEE656EB77818D94B89FD040963304A72885052A2A1E26DF98A8D2B3A9B51EBB126136331033C4D1E4D90593C113D8D0F373F8E1ADC38A6CBBD13C19432A975DA09FB8BEF48B7E765DCA9ACB68B4AEB56654DEA51127482166D0FEA00DDACC02ABEB9CE08BA2E88563EF6B77FF1C1D2CA73B3741C24C4B17EDF628E7A0F345352DEDBF68D0CAEB1483F199D625F111C47583506E2B5D2B7B1616C4C8828DF9EAA9C29C51FB6E9C9143927080C7209D4A77F153780F32EE812F2C48D6E02BE9CAA31A0F41465B2FE31D465461D22B5640765E3942CC8571A1F181F1989ECCA3697119E1D4EA7AC3FEE3EAF8F08675625FE32F0A15B848D54940B4E5DBBBC51082F425B927CAD8975E28FC953EAB8874060BFFD1E85D81521549447E50A981AF8FEE3ED288D51DC352ACA1CFBE33466472FE9F8E99B25D134F5880E0BA6695DB585608F3BBD322649FFAB54BEF5BC5757B2B6CAAADD6FF0BB303089FF8844CD5646F4B48A796AC80E908AC3D6467281DF56A5FD5748059F9162B1299AC64F9EF2F3BC528692E38EF62D7693E1DE24E8C5FE44CC592261C4A2366E5330090296FBDCCE7CE37D4A4091A3E4BC309B7ED5C3D5A41DF55B8D6C40F170C78E1C31929116388E75B116588CCBAABD7DBC42B2CC926964DA61DC83FDAB47913D781437FA8556BDC2C5A366699158C72F7C1E48EF4F0267729CD079D1C58F10C119D455F701E115470B1CFA37E70699BCD5D9755D9C6E0190B8E7D08277153D83CECA5D6231843D7A87C8F58A08C379401D521DABB22A0DB46CA58D45B742C2DDC0260577F2A9AE4C30FAE2C40A899D8307A7E608939A56CC69BC8F7557C8B397EA6141369B157160FAAE6988D6EC508A73E454CE33D614DFC3AFE8A374F6E2B5C6DA95CD1FDA7D594940F3C03377CD330DE5A9AB9EF9410010278D896E52912BD4D9E1D949F95087D0F0D204E5EEFD41F392FC1E6ACE54F869B28A872FC0245F67196E316406257F338D7A21603A49BC6FA3C544C1CCC8CBFB53E62C95E48F8327576CB7E17EEFF44C4EFCE4D86EAF16FF181ABB3E5CCEE978094083BB66AC257B12C6A01036856A5720291DD4415EEB4EAD88D861C444A89D0801BDD9D67C30A7163AE2B606C7C9A66BE27ADA79278B045CC33612C3B8D321AA5F3DDACA21084BC8E9B301965247B67A052C89939A614664AAE0AC40C98CD49A1D50C2D479675265CD4E2603A5C61BF9A5D1CE2F50BEE66CE5C243A97F24A6FF442F5EC1F01EDC0078D3009A474559B2DC576F82AEC6C078C25ED96987702A2AB3F8790620E50CA5CE5A99447F4AA5B681E79485A1A0B31EB8E4931167BE532F83402347075AF716B0C3D62814FDF48194A0A5D698127C7763CDCA9E566855CC7DB4D53877BE297A999F3E9D1FFC5825F0223A5F4733BF3E190F33BC19DB713F5B6FA71D06668415A041D8D716E1C33F127BD41BD63C0E70DEEC1F0EBBE578D60C96511EEC10250DD6E92834EB4F07DA57EB985F61473A8E2426355595C938B6F11A5C18FBC9EB14BD34B4BC965138171C81589F565AC4EDAB623E24B9C5219385D8D2FC3D7D7BFFE830E48BE5FA8A7846A19668FA7C2F92856ABCA3110DA718252E00F6BA7723BCE9B45C89B9123E5B1A85085D44E7587918F769CA156D92E068CFBF1F572DF15EF49098888592D10FE33A242E70E899959E9C52AA18F2214E0628D44C4DDF32B22EDA8CC88A4D5A5B68422926B67473E5516BC36959A230FD9CBC50E80C4C06FE5443F9DD4FC8F9B2D7EE2CF93D5C6285685DA5FD28D4A124145A63869902CF9E255286B5CC1B5AB3CEDD0B9B7B16663919737AA9D4F98F50AF37CD0B7529566BD84DF1F26A077364B654E9E1EB0B83DB186FDA8A19CBB90A79E32F329F58646C66B63BA28D505CEC735A098C784B113155C924818ABB449B0DE2F97DAB80F266402AEAA921AD626538FA40A0B4F8D6C021DAD976FE94326AF49A1476626BAAB11BB166173837A88AAF292FD5A4AB9EDC971A3133FBB6BC3ADF9725B38E8072F9D42D92EAA847E514ED12C003CFFC1E52F47380A66694A6DB3B5DA4F05A128B4100C6E5E8948402165CBD2C363C440FE225DC7321CCE2FCA9E4D7FA1667D3DCC16BBB2C37058E261B98EE6BA8830B0F9E8617729ADCD2CB13F2DCB6C600A5B17B28492041A6F2A2E4F1767F5B18458C2BACF45C1AF60C7215D8EA012B8350A1F93D2F49564C1B354358998253099C10BDC489DEE8456A66D1E0DD7589275984E0D2A5001C88760B70B4DE152DC6A3EBEA18A73009E99A5653A5FA8501C8A473D49AAF3E00C63227FFC315B31C4617B8B052F4806301C78177CD888058C0DF08D60F09F90FD54F09B453C3F7217A8F7F7928B669DBD8363BE56AA18547E54007B4D30F5AA122555EE5318EEAA96BBD9A5ECE5E35EBC5693D8ABFE7085A07E375F3D05273B8D3897F08CF722BADB78D09CCFB10787219BEE2B93795DEBC7CE1F9DA5098C0F0675BC57A851C99F49F3E1BDADF6C547CBF265591D171EC5BAA0C290ADC2DFA44C89FABA4A39BF47AA16B06BD66C2FE9958249E10AC7FEBB4E554131E948DEED24587062FBBA41E6CF0E1E991324BFECDBECE7808919024800D36682FB3A549EFF42954F79222F35D26B8AB6FB7B48D1591DE082F16EC06237C7963B4458A32F312AEBB2956B163BB9E3D049887D426690D6EE668F061BEF185AF0967A031D0084F919D4C837716545C50932145A412AF384FF08A88132F298CBF58C4661D235D87D3FF8469492A1A0B99F44609E62540F2EA3392843BDFFCDD68AE61BE06415F926E34C969686748DC005117F87286926DBE146A4218DC18512702C05BCDFC964470565396180305A72E3902C746F123FE5579D4E30DCA4EA9D18E7584F92DC50653D7E9328306C5C9190139EDF51723FCBD1ABFED644846F3D156B9DB3BC3A796843167DC06DA16F7DA4153C160D71C56ACA3612DA2FE9F4FF4C6CB00993D8FB2D63ACE78B86496AE111F8F32F1111FBA49B9BB04E1C8324E204780581D219D7554E54E5A1AF6CB1CD3F5A0950151DE8F12F35ABD7B3C633F412BC99233680AE1CF27AB56B6B30A45941711BED75980471EDE1C507EE35C710F04987CC22ABACAE9CF316F0D8F2486FAEE98EBC522EDDDF636D06C7EF1CD315A56CCC68A31ACE6E426E021DB64CE9FA529A70ACD9F1F7A0BFCDE17B53ACFDC587B7671A93A76CD4BED047BC6354B760526C3146759D3BF9928E331ADDABF3E9FE344698662E05DC279C17260EC19BBC5B5291405E2396BB303BD990E1F012A8DCB1BA80E828CE3C3E2359BFF84A864A2F919AB16275CCC47F1977C22BDC7B43195DA0AAF0F0571ECA573FE8342DC711365BF75313946D9E5FC4B66E47AC8BE8D4389B19A95DC74AD61A076F4AF4338922D2A65809B3C0A1C9A50F850C7D28BC8C786128A5AC2E7E8302BEEDE6896C3B30D56E9F5774D19953858A2A437BF07273FE086DFFC120D48D9F7B522F046EFEA62AAA78EB1CD6E1A4D5C54815DD3404A8560890DCF2A2D272F9C5973E3E3D04969DC415B9AFD1E908BB135135146FD93C906E2AC05A67AA4732ECD49AB449F87F8D5B84A5C2F2F6B19B20D1681DB75AA4E7C327D9075490D72C4582451281D164C5C0392701AA6940CF26B81FDE21C0FC6B560A047B46641858B08A3934188D08605FF27D201022BFCDA5E5CE5A8265566DA0641245B86B5D2BD412733ED3B906005935DD0A8A6C6142F5502A6BF5432FC663486356CF221B34C22850A4ED6D3BD649E1A5DEBD314709656CBC4659FF3E095B589F73D84767E3D3AD2FF112DD0BBA217A46A52178312DA2B0B125D74C0C75C481AB819482835227A1EB2E7E720DA94C790355A1DD4A10467401A126D57618B2C6E9EDD785C3E37D218C5D52CFC50DC13977A8083BC6E529AF2DD49700420DD22C99E388D1DD70848443FB6818035D417FF815988765FCE05EEE33D6DE0D56155BE38A6E9AF293D3E17E2132477ED4C16E34B3BAB672C667F8E75F3AB6D85B1F2E3D791CA2782F473079474E7F58E23A163B73CB78964868FB7EB43D775B10FA071575D7CFF205E1999BA7433F7CE330553C03320425CE9EE8D7668928ABCB4E63991D966BD478C6DAB53202E912E50D1822DA47323CD51DC85A916D3E548A823ED4DE8BB911D3E0F0E59D74B7756054C91F10603B88F3DBE4A1456ABDAB3586F4315C1746F7736513CA70B18CCC3A7F157D66340B3A2140BE038DF5CFB90DCA603B3D151FACE585BF321D47B0FC4D73286F90703FD01C91B06D7C55F10B9901ADA34074F2935885FD304A651988DCE2BC3143BEC56C5D2F4937A10ADF5545FE9B9B5FA354EA8599ADE5FFD86BB4B27F6F20C02693576690AF2EA18091811DFCD31669B0013257FAC58ABBBC677F93A2CD69D1EF68D3088322FB37D4BD2989E4EA467A9119E75B423999E93B461324FBBF260C0E352F5E2AD8FA344C23015B0AEF5D436313BC6A0C2A3556D5C9458605AA3D5A23AA45B8EB75996D1631187AC8E8F9D8C7C32459C5EDB95258BCFFBA4BE45002FF28D627CEDD9D7318160BD96F6D43D544C859D51AC224D17C1E9D3EF72EA914ED718189BC90BC051CF09A4D9D5AF393D3EB6EA4F5644211913CEF6E935B8088DFB918EA76CD5A5BD4118B63B2B815AF5C2114C61177F59074E620F352C1D6BA882564B7CD607CFB6EF1078E7AAF0B58B581D7C3CCB9A4ED61F098ADF54B9118768FEF4470C8EA98D3A0353BC507B66558F7FFFCB7F29DD262055F233548C6DCD88B74D2A0662F20800AF1ED2DC58ACFDDE5A0B52C1CDB07FD97084CD9DBCA9298BCBA7A1F8911E1A3F1DD3BB27AA07F7B2C845191459809F0BD41159AF8EEE0011456923FC582C59A0AB292F6D602836A547ED643604434729FEA2B3885C637137B72AD0ACF4BB7E840BF046979146294CB7A32085D98813622F54C315B66D81F39F40985E5C2C62D608323385A55B97A1A92543893DF3FB3BE9853222372BB6F8A58122778970745FF9F00A8BA1721E16A02B744C39720E228A220ED7A20FF3E43577F84E6821069CA4454219C15A4E18CE30FF150EAB6D1BD3F0C3321D9AD6901DF7DF78A3F04EBEB72A779E8739B482E0EBF19EE92A77D4F1F7B60E667639ED50BF66800C70932400641AC6D9266A050CEEEFD896C133FCE307CA5D893D5F58A459661DE15DE42D2183ACC8CAF8C063876A1773EEBA178FF389640777E21BB8113A2F883C335631F69D4B0A9BD9D23CA021D01515A266E11B282649F7FBA0B4D86431E269189997CF518A7B223E4D367E843B175B10D459292CFCA91BFE239B3458DADA6F695D16F92D1815D5E80233F0C134EECBD66FE39303568D87F87BB421A7B1701FF6C60194E63B021FD368E3D2219A528C68E956BBACF423BCF5F22A827EEC3CB9D14C4CD415460812B84A91A37919AF0F9C45670081394D209374B461EAC561EABEF8524C2905A245F35D771DFF96DB98981D2CF1ADEAF51B15C01FCD213CE8C06AF68BD195994CFB69B459B7FF09A4F46991FBD2CB6037ACAF62AE57CDDA816CCEEE91F953E3409745D7AA57C03958B36E2ACF029ED359E9AE338D4A3FE7888DC1E84BB0A1EF8B1FC0C4CDAD8B176AA11B181142A8D6C55B2890908932C786086E42FF00B3AEBB7B6385E7510723CBB07F5E808B8EDACFD33009770DDD2A6B3A96DA93B36FAE37B4AC56BF47344CB2A709222E9879E59CE355B8E1B3113C2DDAB6B23386316F4E48C8024AE5FA155BBB93FDAA73240743B6A08A32D17B77E45B0DA43B38CA47AA1F56F6722546640A44F207F249BE9F9C7BADF501307D377E5DF7002A4C8487112FA552C08BBC2BBEC4500BC9B98AF1A417BAD389F82680C40750311A25EE015D33A3B3A44B2C60D531A9AF8844181B32A24FAAE10A4534E7CBD21A706AC8600C2B3D872F0CE46573EA16B0EAD4197B3D6B8E1449880163F31BB4A6826E0AE1A42806583FA8FA7D8BCA379F850B4F2A34348C248889E9AE8901228B4E11938A56BB3CDB41265BF03EA8E9883896A4E07B565EDF69221B3661AC2D41DE055DFDCC7C48CF7E1105E5FE59D5E4561E3A225C9BCE4E7D1B4CA91E3D00E6DB85ADB842975B1197F3C22DC7AE98AEB9B505C78EEB526B29473100CF01925B2A561EDB10209186171112C48F02D6D4E9945A93401B7DDEB06930D565F7AE3D30AA284E9562B858A3A5AEDCAD7740ECD19EBFFE4801C419928B74406C232CAE9B8FF1ED1E9950E956A901C26BCAD903E02245352B95DD586414DCF692CA33D302A37744001AF0465853FC76564E9884D13B60047102F4087D447FD7A59834B66ABAC991A0E4C5B697865DDC98E27DD6E2A2C8456B5D480186668A1B8435AC2E75E35B137F80D37A8E8BABDE0D121F453BE1F4B46F4D3647AE37EAF7B37AD34BCF74118C449250F9269066223CF995C9F1773619217B5BBADDFCA61F5ABBCA86A669F9D5229F941B5E55B216470F8B75EB34320AC929DBB5F6B4BE38F367F7CB090AE85966FCB6202EA50164B75C592056BCB50879ED2F68A9B8D859DC1A297932AA861454D6EE3C0C522FA095980ED53A8D766AE0A0AB4168DC552A5F7F48C08DE36FE01E443E697AAE13F3010060523E037AE20468D50C003BD87BCEF48D2A4A4F1DB6C92D170F67B544C9A0624FFA63FE49ED6E37418F927C004BFB4BC1CBA39437BC0699C1228CD7F16670D10C74649F5E26C126CC819B65D7FEE2917BEB6ED160455E4F47E71D0E8477A2746CE981AF889628D0268D5B3ED4C9D632C0A42D8C38AE428AC6B715785755B90C4816E1325FBD862A3EF322FE8AA93507C13935CA8BBF068D135989A11FAE9E98A4494154E3D1AE8CB6A97BEF3D179B43C815A513D96CE4BC9B0F47C5C55BF0D6573D2A36040B6C66DB9BD4BEBB6226363944C8CF0B8F989BC56A67C43FB700B850BF4ED52857E216A4604B957048DB6C79E6BF199592D3167100925E94051399F2DB38639C05BFD3EEF8C5B985F975A8FABE32909537239421E81BCD4198D1E51062355BE0DD16C5025DE43944CE46E528801753474624155BD98C29451E2A0BF51E2CC099D80F76284A8A0445CADFA39B9AB8D4201AD00AA424B8B6EE8CF2A815CCE0BF234F7F42E07BB754331D7E192D132960326CD57BBD56FC5A9EA2BD5E7C87ADA9F061DC9EA7E9EB88F40E899E44D3558FB9B16C7F0DF9A25BB1E5EE0C6C3651D5861C94769EB7D57B84217DD7ABE8A4386890487E70ADEDD11CB102689BE77A6F73BF11287E65076000935EDC758E191D6DDE89296675746AA7CA36F87FFFFEAC5CB3F9081DEDA46C0F04EFB15F980FCC2C3D787F81DF668AE9A10FCA5016598D24771FA93AFEF4284E4364A0D54FDF5AB780E85CDEE0554F26C870B91B00E945AC32415B23EA3EF30D6E5E48EB871E83B16C9BDC06B141B53E4C09554DF988170CEF09D3CDA9E5A6014C1DD5B74CB95CDB94CC24B71297E8CCEF720F1B0EA62AA5A53C730D3DBC8FCFE29F706C7E70AE7FC258D6CE7F9A3722DC473DD3A41F807C4D6E99AB2FEDB28039A09319EDD077042075AB4E37E03DEBA5E875DC2384E5AA5C32F68C2D385E9B4A2B46DE6BFFA5B92E105F44CC41FD28D7080540B342649710EC859F9E8D06C70A1DC7C28D162089C2321A899320C1CB75A0110759E1323197738C775C7B23B8CF52F24872ABF4AA6D37CBC9723E9C32996C2DB34346A3CA44924ECECA05916C1BF1A64D817EB1C6CEB1B831BFE6779DFDCDD3E5E04328FCF3AADCFDB7EA1A70E1D461FF3EF600856D4F13DEC927809C9B060605FEA3A1C36477C833EE58A3ADB47CD339C23163ED28F65C407D98D2EB6B93B17EBA8B0C80E789090FCBBA28E9CC9257764CB505E071DEB6F6E1BA54D049E68BA58C12526964C7028AA1C0D428451A6E2B9539B83F21A161FC955247441D5E2F5301D588D8EE489C6ED536E20E155E1AA4558F3C9C04E285335DCBB314FE9F5EA95984A1C4CCA1A6776FBB09F54216647701984F8FC385E61B36E22BD3D247D21E0AA0C4A87024BC80D0732D48A153473765A8D27D251DFA48CFC087B494E0B62885146CFC6BC972915B2038EB699CA389367479EFB41E4AB283238BF18EE953D1511F0BAE0F0D81630E56A727CD9AAC85FA266369C62A1C2E39383A1306091274F77C0F120AAB529965778FE143457A0D0B7DBD4AC56D1AAB570E1D4B74D3773BD6BBD7CE8AF97B8E892F222290AC5BBB83AFC2F4557F2EFB844C9F460CFAD1CDE9E152EC52F422A6905F280CBAED45BDE910DFB2950C39C29F80EB6872D14DDD5F88FFBEDF79A35D16DFF7944A292BDF1A1D104C59B4B2DB55CE817F2AF539F0D5E14B3F7F45E8812F519BEE3A24BED6FF6C6F42306BBEB0FBE70F9F8E74EA3B32B1EBFCD9C2BBE30F4A87A4A202975DD0BFA6BDE4196AEAE10DACA585CCAB4F4BE966C1EC2180892A05706721B2886DF16E22121FA7FDC833379B0EB33E3711A6940C1D345F19EB1DDE0923EA8AD12ADA44F31BF6B29B4A84C3651408DB9E2504C0A6DE81730AFDF959A149C113923F4F419ECEFA20CB79EC52210B8DB66C126A100AD2803E09ABAD29DEC3346A9BA364657257928A905225547501957DC73004B7196ED6BCD4EFC981EE8A2799A53D2B921DA98E9924BA7BB2186883FEA2AF3C06954236C540FCCBBBB470F73D219714C7B811233E153711496BC1278B1200927B79B11982504E5FC53B85E23ECF435570F645D5E9353E9718D493FB7C97CAB597ECA391DAB8FD534CA24B85891EBDF4ED9810E5EE38D7BF26AF7ADF72C5E35277E51D3088B4B8FC689237D12B5A3EF5644DA9FD7026BE2CF9569716E758208B1A5735DCE516DD14C05EA145D5C803F1C6195C22A088994418CF951292750CC276C819F5B1056B1D09DB11E8B37C79BB06DC2E2597CD5B9C97CACFF32F8F9626BC5D3E279E036BE74A07224E6EA8CC3926ED645C065E70D7610DD1405C0D1137CE799AF66EC05EFDE745293F28185B82BF835FB3746EE6FE92ED0A73E6B1B3BD5797E489B1C6E21BA624A45F82CDE96CA8976052BCB06E065A9C73AC9F8200DFAA8E754F5A4145E2257198C4123B88796CF77CC9B655741C9D0CC7C1FAF1CF4AD364F63EC10AF2C83AE5AF873D7FF97D51E0138E56EE9307E4559529DB5B322F5B3AA59202BBCAE8A2EBB01681E90D59EDA37BA4FE66735AB50C33EA54B3B29DA44E12D3380CDE5C7DEDC302BE49ED7C3441C5E370A43D1270905C978245B9598367CA1B65D72D757D494DD14004C224823A1658A745F12D009AF70137712560A5FD178DFCE01A4007B774333A8452D8F89749DB2880B98C572EA5B3BCD5E8BC75C74352010AC4DFC5F461326BD1CE92C87E54AB7214F7C8ACA886181E9F002E324C79D84938C539A0B5ABCEB44E5BBD3C6502666C10922357096C2DECD51CF76540838EBB60BF3A3872AC4FC2F2539FB690FB5D655295C772FD529CE9856CF0D4C22FF1B82345294209A12F4B9EEBFDFA07945C181C86E883908C0A662BE08B4B863287B9E9E05F51BFABB1EB3A342979E644FB60E84547FB954385B8BB2156DB032C9DBB5C3F0EF14E84E30A884049639861738D0A8E45FBE326048A98ACBD1398B525CF6435B73F1EA9B7DF96176EEA260A0000B5C9E3FA3FFF764CD1ED3A06ABE2AC20A7ACDEC2F7B8C4969401E6360A0347AF0C489C4A6773AF4235C39ED4637B29028EB73C0EF93C4E919ED283D59A14D941AED8B9FC64F4A2154C9C98118E5F8913454C0033672D7D32AA2015D28AE47B7DCE093CB83068675001C932F8EC4A7D65DC583C5BF53F180F11EF422A5D6CCC5911F042CAB7FBE1507F3DBA86537E71935753514EAF4846ECB33E82B4EBD24E8F3241E92757A9ED4A75DF774EB8A5507E90AB9FF53A344F08C31E2F57F85B5AFD271DA594E23A67C4175AB6941AAD38889A4C573D5A3C93DC70F244A73DC7DECEDC15C260D3901A3C05BE5F75E536AE4606773B0C43897146155C71D1C00DA2A526F8677C32CE4A7D76879DECAF36067E4DB2CCF1661B44070C00D01C29C9344C243D43470840F11C370F642EE44E72C693DD5C0FA17C3B069F378776B73044AF8D483F14C8FAF5FCD7E279F9CD18A45D700820958F3F98F79F843FC0599BEC3039CDA8CB33F2A0539B5477C84EEA9289651760D2501DB24B3C661BF018EFF817203310DEDD0C2CAC6F3F6457235D84A8C626CD2565F588ABD70433BF419E2CE25E0F96DFD1FB32C6307A1997B7BD275C3E717C9681D23620259964EDA5A7C582B5C616D71D0C73C9D1E4E21FAD548DB6923D1BFC5D74523C4581EAD84D37C0F0A8466760A7439522C3291805FE11CDDA13C0DBA94210C6A8D7B24D66F2D79D771DB1C32E4A06598A1585D4C79CC34354AF2ECEF5995A610C289F37C5F4AE7B550ECD60E90CEB69D21CCD366A29D70395F0DE991A33B4D8800EC0E444222D8C48A6BBD958583C6BCD68D27F618F6FED62ADFFC7B2817CF397D17E411F09817E1D7B2CE4A965571D7C8F466707AF0AE244422F8B303DFB36AC7F828B0ACC732C9C3BDA6E0677390BC9676FEAB51E631D9C53811F002731EDCA721FD098527DA5F1FE6541DAFA880627BA365235D33BCAAC78B82C13587EFC7A0B446EEB3E52464F38E3F94A21E4E24F8658F156D957D1FE636DB6D2B72A14FE84C427883C346A0C641EBC5655AB78852E9FCCCDA6A38E85E4D30F62836A69118D339722E33CED531EB8B3735F5D84D95AD1D6A16ED4CB070C4823F94A3E59856C71DC1B0A4C7D979D5FE84DA3AF3FBB83C6E5A21E86E4D137615DFF6ED022B478A79BD6801CFC6F62F299C0D1CAECE6F98E99E677A7658F239BDD6F94071BE816267FA850BF4B42F9A9D90A7CB7177CF368A8379ACC76DDD4229039CEEDDE89560E9054349E77B17ADE0F45CF28F8DEFA425153347F78A858A1EB832AFEA3CEB72DC3907A399FA9EDCDBCA5B6062E4AF5BD2028B4A638C03E1D8B5D45E5B11D1FC50B4E2CA2AFAC3C5BF9783B7848E1090B9991686F412B5CE2B7C8DCA04310D1B1A0BA2E3BC0F0B86E974A8114A37C35C72359AA63D500BBC50DC511FC0B3A54627D708D8D2FABC297185DB7B279D1337B08E1FCEB30BCB69DA8A006527E519992274465E50DC2E1627F2756FEFAF46A150ACE7F1FFE8501112E4CA916482F98051D84E3F0D0B9196B7A1982A3B8C66E6EABF58B583ED704FF3584ACF7FEB5DD62E9542C6FDA2361CDE4BDA77C547D34223C9A7A4986B24FFDAD6F23E804724782008FD804B70D291E53FF44E8861D9126E9FE2B377A48CE0F26EA453E877064E2038943C138E0F3F2A550AAA60CFAD81B0E1A0CD580EAA15D48883305547D535767FED2441C8EFE1E5067B01959276B809E989299FB0E5C05FE9726A1781414009A1304D7AAB4C832735F26B7C22214077629F9769C365DADAB7518F547B1264B823F23A16271142D29BEE48AD5CF51FE699AD77BF1CBBB2F1CB25D80D7CFD40EA0E4D06529D720969A1DFB52201B411F4560E03B05B4EA7A8CEF5D4A3F7D31ABF367210103B0032E77A3EEC1D97E178D038EC58A58B5EF879BBBAB53FE1348A15DA6507BE2CAC71D15357529CC9602E7633530FB0ED98AA70843D86783AA3C21B33515F19EC0DC0C6BA5ACA96BF38E04163B7B459983038DB0BFD90583540DF5E2199C65BAB582C631806B134AF5D6595598C1D415F79E7A485AE6F78447E29FF81D404888D37170E42DD13F4BE90C16C8CB2BDB2EC8358CD9051AC0DA078D057DEFCFE3EDDA783DA92CC208F3B86DF3CF72C0F3C859A693562383EA754EB254630CB5089585030033C66759E87116CF9D681DF365EEE989226C2956A0F78E9928E435172F6E00737418344EEC208BCBB159D8123CB8912F6A337CBEA5B03CE98D3ADC33356C96CD4264CDE5CCDE1EB27F6002C46691D4827BFED9972A6BAC4987C6432BB411C8F9F52A774ADDFF2C7F53E49EDA739B1CF2EF13C75D62C3053189CC0B5C3722ED1C1F3415CCC977BBDA6B2B2F2944EE735D13B8C31955B9CC0DE77D541C9F765B01521970DBDFCF96F80705378524743FB2FE1356C9CFEE33D90B75B3CD0FCDD5920DC5A19C6C369F774EEFFF8F37D4BAC8758DC77E6A4644146EB1557529D35FF1B4B1778814BD65E4C735908E402190C8E1C80FF96D0A626DF599FD6F14EE8E595FFDB3C97C29AA36E14B249FD79AD8D812077F72EE39EDB740C00825404DA06CB27526F49CEB9DF4BFEE3E0622D8B59ED11D6E52DD011825C94A866D064B77D504344997F0A0CFB610E3FA10535127AEC1C67F87751DE7173BFE53E3597177128106CD1F241FC8F27C847E67BEF419D2B77F6B665700BB1C96B5F4775C994DC7341D7F8525C94FC41830C89CEA1B69DD68838113015F509D3A81BFC0C2F4D3145FFB5BA4F852782E43056D1A65D9F4B9F770ED4356785C8A36E33BE3D5AEF7069AC3F4696B4B65C154CA46D2A89BBDD7107416D05FD1649D82B8755B7B80E6824E19DE730F895F6B60D6E63E64D65310B59C2B4453DB2AC03A33BD045E304AA22D87DB448521685475469A25EC0811FC745A2AD52618E8DD9807674F9D26067F7B846BCBBE83A7B3FFCFD13587276F44E4CF8BA0BF1FAFED3DB1FD26801F4C66180EACE5C5B9EEEAA028A994B6F062CEFA91EF118F0B4BA1F99289D7AF83861BA4C736706A90354261EF1ABE9611D4F06996F6243F30CBA6CB2A00571A91D48FC84F66C466B660DF77822CCE184D1CB313ED78AD95EE904CC0DE27B9DA8CE49DDD6615289B2C567B71108045ED5534CCCA6CF46A2DCAF273215D2930D7590BCC975E8A44C726B737219D4CACE47D302F451EF7653A63ED90772FA4691847CCAE68F3581AFCFF08E0E1174769380DF40BF25A1F3FD2D85A88DE3BB702F8147DE60A8CB509970FAB172F2E1682DE3282F03D2E6B8AC6C7B98B98F85F0027CCD47D90F2BABC55AAAA4724A08B365ECE8AA7D8373C76141CC56D64F43379984277BA03B2BC954DE3CF06812FBA6C462E583652049BD37276B0897522E3B0B6BCDF4D8D032E0F6DFA7E6E388A2DBBEFAD7B58E5487321CFE6AFF39C3E7C0FBFD6ED848557C2952CF5FDB03D99E0C2F112077502841D1DD4905B5056E4B202D94B95CDD2D798FDA38CAE3A51E8C14809C7E7CA70316CEBECB25F250B62D54646FA5A62339B2BE43F273488DBD350D334B34AEACBAED01BE424D02C1244C52245B746DA0A6BD092170EF7E4C6B4AA5C449AB11B4879215134E57D06DCFC980F1589ACA45818B186614B0432E698354C4EAA6E5FD8D1CD5C11F0315982EE1522CC6F22D8557CC3DBE64397352EDB9AB247E6F0819E548502A50BB379B5B8432F9B04FDCD7A5EE670694386DAE4DF491EE807D3783E86221408B51B8D3EB8FDD0878E9797F830D95697B9E270422698A77914F59B58E739A171F4BF7BB012572B05CFB66051E32EC69239EEE0248FEEC2AF7B13977B1DC7070043FB099E04C4B2B610EA199922E7D29B766881E8391DD8FD9E26305DBDF963D8929D470157CA6A0216D6E2DA0022796F62C944E98498DF04A252889A93EE6AA55300C1DE6AE5E1C2D499F9EE6C080C5B5A77F4BA851491FECEC268D407299A502F7672CB60E685D50DCB92E309ED85F3088B03E774E964571E846C78CBC75D48BA46598E70D142529D3A04E395E72B7292E961E2BAF0101EA4E54318292CCBE1A499354A8AC656F72AED89B31B54ABA8E149CEC378ACFC6E1D98552323BB7EF439259C0A28AE4EFF217DD09F91C777618917C644B0A265CB6D85001DF9193F89854C1E10C62143BE9ECB3A1EB29E368B6141824C0216EA7DC15F51AE424CCFB6FED85CEB23FD67B338D0D24A9FD70C1610715919F2AC45C6B5D441815555D5D73BE70815AFCC8B0A4C1147D82746DC7E7D866379FE60CB2B444B80E568E0361EF5DD55523D0193BD11C0D90615E5B504F56507499BAE78D117578AEE8EE86BF103D51BABE09B777B9A23F05C0018CFCCF2907874038846A658F3E71356776BB598D1DE55FC9885E49985E12CC7FC8F7BED04C24E48CFE2F671322610B435C09907016E3EBC95EFF5ECD1F4FA8253A1AD5487F1A3663A27958670E5ECB971CDE9B3272ED49C13DEA51161168FA1A9459F97CDB568DD3676DC232C0BDAF07CEBEDFD92E9A152E669B2EFE0029AC28B7B64B792F4EB6FB173BD7F7FE750D78E18FAA1F21D4A87BF888D09D36EAA274D499556BE00361EB83AA870B2BC534575633F7FE93C4BE97A20E58011BA1B39E086C78D54DB1DA94404548472C7E9BE9E86E1273C129E5A909E4D01FBC8A336065CFFA60A233DE79B15EE863FE80F3365E35802C7DC2D1B1C3E3194BA7185F8EC46230F5893BF802A83D045F2E131546B2D096A4D66F144E26A8FA8EB494EEEDFF899F4D4930F0CA5A006FC0C2CF90375DF12B56DA8F7A5A346ECBCFD519DFD01A1BE4FE893B66F099456D7A03B92FAC57C348D4B75C34F341937F1E665354057AD180C1A1A69EA24A410ADEAAD1E3E0F633D6D7A56BB2153D2D29EB029B42C7DE777284DB524F2711A51F57D1BDA7BA587C2EF745EA867CF4BD12F05436F929C77970EEABC849F15AB717EAA5A614D67F7C86E8F5D331CA9EB7DDA110E5852AFA86091C61DC2CE98A232DB9DE4051C92BB23C9B0FB6B46B3995F66562F44E541950E0E9C5EBC86D565C4DCCDAD128F7E7432E9C9B9D38E31309B26F1FED6BC2680190BE12D1C257AD50E41BFF1B3F30DC16A4A870A4D48098102B50F08DF015BD0CE9051B55107774B78B20E2CBE264671662C84CB9F27C43A8BCE6FC06B73316A21FD83D4397F01F600B72A0C0DDA6BC77532FE3744E5563EEF8CB2525DFF2E1A303DDDA6C15B74B534C95FC8C9257930ECC7EF28248E5C8660BC34A87DDA051155302BB1CFA1AEA72830B9982B2527FA33A23F068816CE4C5D04523FC22BF727EA25280AA7A4AFDD48102E78D758EEB3C56F9A17F6C022ACC369E359EC9E4C9280C6AC09D0C73AB1C0BB6C96F11E6074346290FF0452D9A9311733CF5FC698DAD10D2DFF3187C293BA4EEC05F981AA5461A5E098C8EC25C1EC7DF15D2BFF23591E89EB9E49BE297754E2F4130FFACDF00570C617AED7A32EC8C9FF5120B26A8079F1E565FBCC7E695029214CF12417733E2CB1C3FA09174DFBAE59F2C8A2585A68EB9629087B57FEF0B289C9564CF904DFEB9E17F2A6B9AB52AA9CB61913A4E74E4B34E3A8343CCC83379CBCA28F70209BD3110A0EEBB9027109562C9C184B93377191E72FB868C07F0503E5EE2FA8E139F4E3D29B19E3510525131F0C6C25C49BE4F1DFA7AF5FB7B43E49D663A17CF37B826E4CC3104610A4624E802CB80A1D1225738D2324C4B27D3736D92E5698B96B38FA502EA146EB147B4A90DEC15BB7F734A0DBA7BB5D3BC2EF2679214572F795085E83007C90323942914A0139113DD675E9F1CD871D45C92B1A4D8B6A0C0FE4DECFCB4D8EC81206B0F1A23814E2371D4D3F0FC81DEE289DE8322917ACA096DA1BB10CDCF912CEDA633BA0DD4EDF7D30642944A34A51A449936BD0C3A8DC99399E63748E3C4E37F3DE37CE1C41776B6C8B12C644CBC45F3714926DA240DD5A2960F9E93173E25FFAC713AE07827DA98C1C0F1A558C6E26E42C327623EB74A1700F9AF633339AB28889F3D0E624652E008919B4A97D161E5865FCBF2972BE191D1C86C878E88BC3D50D7D0AE9CF37424146306488D6472EA949A0EB5515C1146EDFFC6E4B4C5CD750481B44EB600952BD45F097DBC713E88ACDB1338677E19351CBC4D52F24FE44048EAFA7C70F0128E5F115B5516D04EB34719857BC7CC33F0EB26BD325F62D14412B37C72BC21266E54211D47BEAAB01A87056E9B9AD88CDB8305AB52FD6191666F6D136C273C75C77ACB5FE119DD21C70F4FFAB7B4CEA7B316E2FFCC4C6EB2BD3DF1044A16D3D609BF6E19B01C0CE035175148BA780C6E509E8458329B1AEDF9F02E25BA03E8DE3A5CB1E120453D55A2696A46F5FEAAC49025473ED01478BCBFB0BFAFC969EB37550B022A58B4D13D2F8BA58532DA9BC1186666AB017A7B9246B3FBA75926A4C72B97F8AF4B27946FEDC5A07554CBD1A792D5DBB0591F04B53343CEC496AA418A00A5300421A90394553CD2C3EB70A51B1DCE6C0CC0BFF99DCB8F2771597DC270A1911A23C606DF36A7EFECBE0A00B9FD7FBF6586BA9D770A3C3D6DEAB357E307973E38582CE2A88B868CD291986B97C3040AF48477C586E6AE0BD76A209AA263824344C861B56CED6D4026C9A1DAD1BB9FDB71386B2751255BD6BFF9C02B59DEFADBAF2CB214DB79B6972692DF3A157243431F2BF7FC62048A935D3D392B9A8D52904E66095227C82D625DA9D86E3A9A9F5D881021E8E70187B60040E89200BA35EFEE309F06C8A91C341C46FC3F145B8E125D265EC24AF88EEACCFAB3D227FE9B3EF04B2C661A21FB7721A2CC43DB7BAB904B01BF72DF0602E4C1192AE5BE3EABC5347E2B861D0700AAD018338FF893566CCA1B2BFDCF486F5FF5D54D9DC6B89FCD555AD387A38D175F77AA055A02A6C130885E8F666D9D7145B4D396BC1C2AB9204608C78AEB66DA17EDF8DD395C59DE1FF3F81B5D64663F9322365FBE553E8C1AFFCE2B41C7AA3EF9798E783B80D231CF97F0CB7B5616844D8D0E934180815A8050D697E062566D5AB39B62CF2BA528C302734F945C776473D53E8CBEE5D903E1295D28DE187ABAC4808C748401F61C6D4512B605A646E2E6502FF245DF7A88525F6D9527084E87AE7738BBEBC9CEE78D3FA217BB13F0CAFEF786B448E6C8A28AA6B871ED827D9569E20F60D4475A750BE65A4706598118004200E9151FDE421AAA5C2A3DAE8CAF96E98A8DDCCABDD2176B8DD2BF59B3D0FBA841DDFC74BD7E26B381AD02621D7487040A042B049532196D5EB778A254DD782F87831C81D94A81E909FB74B48680D1C803FAAF96A0CA260A24F21FEDEA3099AC9FCC83E679CEAB2A22AE1F5B0C6850AB0BE7894E886E5422234D05767CDB2A22BBC226A243EBB8C937E4F8212F674A7469FD8022E1F245C1FDD23569A8CA34D8BB5346B95E683A16F9494801957A5166373B9375EDCFE44C1A0947355600F22434C1B9DBE01847D3DDCD9232A56670F00574234BC1643BE0F4D9BFC0950D81C1A31C6B348B2268743076380B1AF99CA3AAEA618A955CBA32E2B97F41EEBA2928EA568DFA5F783B2BE02CBE12FD147A1382E9ABE2F7173D570393C06C0C2F8A4ADF7B7953152D3EBBA7EDEFB50C08FC8D3AE64698619C14F5626AF218301953A69FE005AB930539ED693B091C3D130B29B98CDE85C32649634E457801BE427B712BC7B9E5D4584EBE3E8C8CA800569735B6EDE1EB7B3AF8A033BE5F7B02E0BD70870F766581551B59447EC13618F94CB9C862D924FA688B1C6DEEBEA08802AC66FF7B986BD832C5A8C80E44F56727652AFB0F95FA93DE0F754470046C123AA1CD7BABC5870EA715C82A55E9C4F6E49B56497E4D696B84360348AF11A25DFEC2550705D519FEC9D14428FC5392F7416E8A3441C7E84B676BCC6736FC5092F752461B1461D444141AF8AB986BB23ED75220CD70BBB71E3086F95177943D0299DF3F2E97FE8C2F4F5C19156CF377A9A72BFAFC7AC2CFB4951A93D94B090B2AEFB3E9F6C8F4DE04802D2E13E613C9BC0DF40DF5FA53DD5831B9E4F6B2C997E6BA6FBAE79BA434E371FED374555E80DCEEAC4B1B2CD3A859D99D7F29B8C3DC5799F8AF6F3F1FDD1DE9A5F137D731E3A70CFBA4126AF74FD3D5DAF7A2EDD7E201BC4FEFEACB537AAE25A0B6E165A51FE621F9F4FCAC11B40510CABA0DA82F2BA8EA4B1EE639E3A85B479A40192767CEC3FB686B81EC9F8FC71A320EEA51CD7C2808A7230D8A67BCB29405BFEA4E3E2261525282D3CF17D992D4B162580D5D9672668E941448263D67A7341BE50FCE58E9D0E814C967714553639AD883210F246D1F3CE615297D9D6EF66C814BFB1887AA1DAB86E3CA6CC22269B1102E0AFE85043E77D095D7DA4A8B838710D11190EF49E63459F7E57A9A01E105D99F606E89973B90DB3FE68B710D098682B00420F3D445E300C82A84482B6A3661EE02DC1F0FA65EF3A34BFF686FD65CB001D3F1E76D503CBA6A2A2E8D1D30F11B37212EACFEA0FF20D9CBA11F19D4F37ECCAC792858D620D146C2533DCDF2ED6B7EFF9319CE1C5560158C5797068055A0876A5D080C52EC1E87AE656A82D4A836E39A61F93CF2F72F1C20BF5D48B9878E80D09BAB74E250A6CDC09742DE6F6579E5FD73C4CDDD25A95EBC1719BE024BC8FE7E1AC12E0FAFF46785D1A93811A81F08E8FBB61BA2999C49B3E97A99EB0787B9466BC24B2CC7F1DEBA162A0484E70CAA64AFDC9D3FC4CBA6AAD3009367BBA2831E821057FC8E43C3A4016D93E1115BE137A8A6712DAFD012ADEA1385369676C58B0E219F85235138E74FA73D9393F618733CE4BA8D588B68D7879AA071BE80DFE27996F5957A736833CC24A6F87338C69F3D9F3863A3D7355FF965AE7E9FFD4F349C4CC4F2140F28435FBDE89FD759720BCC1BB5AD3C7E166FB41DBC6998929DD53C1FB3841CC8CA38610E11AAB4654938C890DC9F00BA3C1DC59A9457F13E750933A4258059B7FB4BAA89B0FBB1CB4E84B4593CCC250E5C23D2F6EC66F1B974901B6D75A73D2B067B7BAE3466EBB3F3FB21B589D2EC8ADE45CDD07C8974B6D84186E4117A56BA8C73D3B98838D338D22D169A903759FBFDC653D54DDFB1BFF7FFD3634B802A4036C4FFC819AC29B33F9920F8CD34F8ED11E1D21C624CC826003E2DFAD4624CE79DBE1081BDC01ED527E731240829108CC0D0EF7A20CC528E5EFDD7258B2222B97F31C1E9E4BFC19381B70CE1D9D9AB2D79DB5D6C96F1B91C92BDEAC9A4633655A6C4BB0508EFC53D665828321CB82782C7B0C1B48F5FDA2AF2A83F30ADDFE23E648234B9BCA4AD3D4CD1D6CEBAC39C3B87C4D5C4DC8AAA48E4BC3168D00CC8B6F04F87FA9957A4E2D894346BA9FD4D931C14774FCF7D2610EBFF687F4EE4441C1B8FE84A2C5EAA9CAC071A8061C29B1C48273DC35475BD4C82A72DB9853DBECFB0E7CAF4C2C0654255EB5B994235CAE8E908C671BCF4F2E33CC7A08C7814B8A92D6260E444EAC0B0C983A4241F7D4AF78DEC16C7B078D20F57A48FE100DFC64EFB9AB50BC2ED379B455865EEEE109AFE5B23D563FFDE07858601C8FCAF5FDF2B102E70D830CC74672D92065BF026631DBDD67DAC4380BE6B33D117DBB74FCFC899EC614709D83CF9C821E3E1571EBA0FCFF396E3108F508544CC9022455C79D4D3D72FC6958BFA3F67ECBC4A2C961E4AC882F9B73EFDAF92DE61BD9A829A37012A3ADB642E890807865F8D14A51EAA07AF561F16AA44DE655C4DC87E4B1EF95F3B09F45B43823611A14E345D739FB83136339DA50EE3AA53EBB6B9625BB6248DBF7075F52345D597E7304FB3B9A56115D160B99DE111C78664533E249C4BB7B7335DE6F6BA94A7E30C5A2EF3DC7E4D402F704C6D81D7071BC1D299DDE6109C4458100EC83B3E2517280C8C0B72E43A842ECB54894151882ED95DADFBEFDC45FDCF92017D3271920ACB5165590CDF9F269BCB2C997541C06F3C5BF29F0BBC1E21B6D51FB091CD53C3855EC5A41348F21D4947D876795848DDEAB4240735935AE5333CA0E0B07F1A9BBE5A3B139FA6FE1D0D03EE410917FDA57251FAE218F3E7FFC2C85D15D494F9EC4BF8E4E38A9D0B23EA2D115B2B3BB16CF1A18AC411F38966A20E5332C0A36EB86D5C9761EDEAF6D162C307EA17C3FBBB99D2806DFE161F2D45969E9C680C8FA023EDC41394AA21D787897016F5903E063D99FB70E7620A6BDB0BFF1FAC7D72EC631102F86AFAA6E17817A4B64FC3496B1E5858B96D8150A32915A6EC286296D3DD6D92417FE9DD11B9B685DFBAEB2A4CE9F0797DA68525D56E511DAF946A70E0AE72FEA2757E1DA0DD93E88D911F526760474D61188BE1140E69A281465A88FA82FB14EF92F8EB69C64EE3FA49BBD5C038CF5057A0FE83E46EDFB5C93E04B682159144FFF0C8BDB0B636403B705ED815B4990D2045C40E8A01CDAB2693A966567B1ACDFAFD353A94F7F468E2CBF64026E661E4AA3DBA0F1437E5F865A74F207EE8D8178459BE18FC6D8CDACC857847DA05B9514CAC0BC6C6341A45779F0D349BCB096CCBECAE4859ACA4E15A8D2C3056860434C5601F5D40E1B670E812835B086C5474BC4755D5484E45B98F5DB8EB26FDCBB346A014021C47B04FA38DBD1AE78B413CC6E10940541F1E8B869CD7B64989319E39A9FA5B69FC7356393224FE229ED08518730B2BC435482731A9E10DF998B1C32EBC902B3000E87260FA41792A15BBF635256821F32FCDC7E74400F97FE2224968190218739663D8F3024F16CF8F222B7E170ECA64FF6B743A9B7E425FAAB21C0A8936E50FAEFF402E8D41CCEC1B74904A3AE233DE49EDDCA509291104A5E011E993F0AEB2EF356A3940A6E8D9D17AC7FF4339A8F92142094891C236348695D2565FDFCECD7CDAB3DE13B9B73C8F97608470ED4182A14BD2D8B3673ACA9ACBB37F9D91067F795503B6512D6741F093C5D887D8844F7E1894013E3DCF444A7C8ED79165BEA99D6CB05EC", .algId = CRYPT_PKEY_SLH_DSA, .preHashId = CRYPT_MD_SHA256, .type = CRYPT_SLH_DSA_SHA2_128F, } }; static int32_t GetPkey(void *libCtx, const char *attrName, const CMVP_SlhdsaSignVector *vector, CRYPT_EAL_PkeyCtx **pkeyPrv, CRYPT_EAL_PkeyCtx **pkeyPub) { int32_t ret = CRYPT_CMVP_ERR_ALGO_SELFTEST; CRYPT_EAL_PkeyPrv prvKey = { 0 }; uint8_t *rand = NULL; uint8_t *vectorKey = NULL; uint8_t *context = NULL; *pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, vector->algId, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPrv == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); *pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, vector->algId, 0, attrName); GOTO_ERR_IF_TRUE(*pkeyPub == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_EAL_PkeySetParaById(*pkeyPrv, vector->type); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_PkeySetParaById(*pkeyPub, vector->type); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); uint32_t keyLen = 0; ret = CRYPT_EAL_PkeyCtrl(*pkeyPrv, CRYPT_CTRL_GET_SLH_DSA_KEY_LEN, (void *)&keyLen, sizeof(keyLen)); uint32_t randLen = 0; rand = CMVP_StringsToBins(vector->rnd, &randLen); ret = CRYPT_EAL_PkeyCtrl(*pkeyPrv, CRYPT_CTRL_SET_SLH_DSA_ADDRAND, (void *)rand, randLen); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); uint32_t vectorKeyLen = 0; vectorKey = CMVP_StringsToBins(vector->sk, &vectorKeyLen); prvKey.id = CRYPT_PKEY_SLH_DSA; prvKey.key.slhDsaPrv.seed = vectorKey; prvKey.key.slhDsaPrv.prf = vectorKey + keyLen; prvKey.key.slhDsaPrv.pub.seed = vectorKey + keyLen * 2; // 2: pub.seed offset prvKey.key.slhDsaPrv.pub.root = vectorKey + keyLen * 3; // 3: pub.root offset prvKey.key.slhDsaPrv.pub.len = keyLen; ret = CRYPT_EAL_PkeySetPrv(*pkeyPrv, &prvKey); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); uint32_t contextLen = 0; context = CMVP_StringsToBins(vector->context, &contextLen); ret = CRYPT_EAL_PkeyCtrl(*pkeyPrv, CRYPT_CTRL_SET_CTX_INFO, context, contextLen); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_PkeyCtrl(*pkeyPub, CRYPT_CTRL_SET_SLH_DSA_ADDRAND, (void *)rand, randLen); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_PkeySetPrv(*pkeyPub, &prvKey); // The prvKey contains the public key, public key is used here. GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_PkeyCtrl(*pkeyPub, CRYPT_CTRL_SET_CTX_INFO, context, contextLen); ERR: BSL_SAL_Free(rand); BSL_SAL_Free(vectorKey); BSL_SAL_Free(context); return ret; } static bool TestSlhdsaSignVerify(void *libCtx, const char *attrName, const CMVP_SlhdsaSignVector *vector) { bool ret = false; uint8_t *sign = NULL; uint8_t *signVec = NULL; uint32_t signLen; uint32_t signVecLen = 0; uint8_t *msg = NULL; uint32_t msgLen; CRYPT_EAL_PkeyCtx *pkeyPrv = NULL; CRYPT_EAL_PkeyCtx *pkeyPub = NULL; GOTO_ERR_IF_TRUE( GetPkey(libCtx, attrName, vector, &pkeyPrv, &pkeyPub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); msg = CMVP_StringsToBins(vector->msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); signVec = CMVP_StringsToBins(vector->sig, &signVecLen); GOTO_ERR_IF_TRUE(signVec == NULL, CRYPT_CMVP_COMMON_ERR); signLen = signVecLen; sign = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); // sign GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(pkeyPrv, vector->preHashId, msg, msgLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); // compare the signature GOTO_ERR_IF_TRUE(signLen != signVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(signVec, sign, signLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); // verify GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyVerify(pkeyPub, vector->preHashId, msg, msgLen, sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(sign); BSL_SAL_Free(signVec); BSL_SAL_Free(msg); CRYPT_EAL_PkeyFreeCtx(pkeyPrv); CRYPT_EAL_PkeyFreeCtx(pkeyPub); return ret; } bool CRYPT_CMVP_SelftestSlhdsaSignVerify(void) { bool ret = TestSlhdsaSignVerify(NULL, NULL, &SLHDSA_VECTOR[0]); return ret; } bool CRYPT_CMVP_SelftestProviderSlhdsaSignVerify(void *libCtx, const char *attrName) { return TestSlhdsaSignVerify(libCtx, attrName, &SLHDSA_VECTOR[0]); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_slhdsa.c
C
unknown
57,044
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_SM) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "securec.h" #include "crypt_bn.h" #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "crypt_bn.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" #include "crypt_eal_rand.h" #include "crypt_encode_internal.h" #include "crypt_util_rand.h" #define BITS_OF_BYTE 8 #define MAX_PLAIN_TEXT_LEN 19 #define CIPHER_TEXT_EXTRA_LEN 108 #define SM3_MD_SIZE 32 #define SM2_POINT_SINGLE_COORDINATE_LEN 32 #define SM2_POINT_COORDINATE_LEN 65 const char *consistestdata = "01020304050607080910"; typedef struct { const char *d; const char *qX; const char *qY; } CMVP_SM2_KEYS; static const CMVP_SM2_KEYS SM2_TEST_KEYS = { .d = "3945208f7b2144b13f36e38ac6d39f95889393692860b51a42fb81ef4df7c5b8", .qX = "09f9df311e5421a150dd7d161e4bc5c672179fad1833fc076bb08ff356f35020", .qY = "ccea490ce26775a52dc6ea718cc1aa600aed05fbf35e084a6632f6072da9ad13", }; typedef struct { const char *plain; const char *cipher; const char *k; int32_t curveId; CRYPT_MD_AlgId mdId; } CMVP_SM2CryptVector; static const CMVP_SM2CryptVector SM2_CRYPT_TEST_VECTOR = { .plain = "656e6372797074696f6e207374616e64617264", .k = "59276e27d506861a16680f3ad9c02dccef3cc1fa3cdbe4ce6d54b80deac1bc21", .cipher = "0404ebfc718e8d1798620432268e77feb6415e2ede0e073c0f4f640ecd2e149a73e858f9d81e5430a57b36daab8f950a3c64e6ee6a63094d99283aff767e124df059983c18f809e262923c53aec295d30383b54e39d609d160afcb1908d0bd876621886ca989ca9c7d58087307ca93092d651efa", }; typedef struct { const char *msg; const char *k; const char *signR; const char *signS; const char *userid; int32_t curveId; CRYPT_MD_AlgId mdId; } CMVP_SM2SignVector; static const CMVP_SM2SignVector SM2DSA_VECTOR = { .msg = "6d65737361676520646967657374", .k = "59276e27d506861a16680f3ad9c02dccef3cc1fa3cdbe4ce6d54b80deac1bc21", .signR = "f5a03b0648d2c4630eeac513e1bb81a15944da3827d5b74143ac7eaceee720b3", .signS = "b1b6aa29df212fd8763182bc0d421ca1bb9038fd1f7f42d4840b69c485bbc1aa", .userid = "31323334353637383132333435363738", .curveId = CRYPT_ECC_SM2, .mdId = CRYPT_MD_SM3 }; typedef struct { const char *self_d; const char *self_x; const char *self_y; const char *peer_d; const char *peer_x; const char *peer_y; const char *r; const char *R; const char *sharekey; const char *userid1; const char *userid2; int32_t server; } CMVP_SM2ExchangeVector; static const CMVP_SM2ExchangeVector SM2Exchange_VECTOR = { .self_d = "81eb26e941bb5af16df116495f90695272ae2cd63d6c4ae1678418be48230029", .self_x = "160e12897df4edb61dd812feb96748fbd3ccf4ffe26aa6f6db9540af49c94232", .self_y = "4a7dad08bb9a459531694beb20aa489d6649975e1bfcf8c4741b78b4b223007f", .peer_d = "785129917d45a9ea5437a59356b82338eaadda6ceb199088f14ae10defa229b5", .peer_x = "6ae848c57c53c7b1b5fa99eb2286af078ba64c64591b8b566f7357d576f16dfb", .peer_y = "ee489d771621a27b36c5c7992062e9cd09a9264386f3fbea54dff69305621c4d", .r = "d4de15474db74d06491c440d305e012400990f3e390c7e87153c12db2ea60bb3", .R = "04acc27688a6f7b706098bc91ff3ad1bff7dc2802cdb14ccccdb0a90471f9bd7072fedac0494b2ffc4d6853876c79b8f301c6573ad0aa50f39fc87181e1a1b46fe", .sharekey = "6c89347354de2484c60b4ab1fde4c6e5", .userid1 = "31323334353637383132333435363738", .userid2 = "31323334353637383132333435363738", }; static int32_t SetRandomVector(const char *vector, uint8_t *r, uint32_t rLen) { uint8_t *rand = NULL; uint32_t randLen; rand = CMVP_StringsToBins(vector, &randLen); if (rand == NULL) { return CRYPT_MEM_ALLOC_FAIL; } if (randLen < rLen) { BSL_SAL_FREE(rand); return CRYPT_CMVP_ERR_ALGO_SELFTEST; } (void)memcpy_s(r, rLen, rand, rLen); BSL_SAL_FREE(rand); return CRYPT_SUCCESS; } static int32_t TestVectorRandom(uint8_t *r, uint32_t rLen) { return SetRandomVector(SM2DSA_VECTOR.k, r, rLen); } static bool SetPrvPkey(CRYPT_EAL_PkeyCtx **pkeyPrv, const char qd[]) { bool ret = false; uint8_t *d = NULL; uint32_t dLen; CRYPT_EAL_PkeyPrv prv = {0}; prv.id = CRYPT_PKEY_SM2; d = CMVP_StringsToBins(qd, &dLen); GOTO_ERR_IF_TRUE(d == NULL, CRYPT_CMVP_COMMON_ERR); prv.key.eccPrv.len = dLen; prv.key.eccPrv.data = BSL_SAL_Malloc(prv.key.eccPrv.len); GOTO_ERR_IF_TRUE(prv.key.eccPrv.data == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_TRUE(memcpy_s(prv.key.eccPrv.data, prv.key.eccPrv.len, d, dLen) != EOK, CRYPT_SECUREC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(*pkeyPrv, &prv) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(prv.key.eccPrv.data); BSL_SAL_FREE(d); return ret; } static bool SetPubPkey(CRYPT_EAL_PkeyCtx **pkeyPub, const char qX[], const char qY[]) { bool ret = false; CRYPT_EAL_PkeyPub pub = {0}; uint8_t *x = NULL; uint8_t *y = NULL; uint32_t xLen, yLen; pub.id = CRYPT_PKEY_SM2; x = CMVP_StringsToBins(qX, &xLen); GOTO_ERR_IF_TRUE(x == NULL, CRYPT_CMVP_COMMON_ERR); y = CMVP_StringsToBins(qY, &yLen); GOTO_ERR_IF_TRUE(y == NULL, CRYPT_CMVP_COMMON_ERR); pub.key.eccPub.len = xLen + yLen + 1; pub.key.eccPub.data = BSL_SAL_Malloc(pub.key.eccPub.len); GOTO_ERR_IF_TRUE(pub.key.eccPub.data == NULL, CRYPT_MEM_ALLOC_FAIL); pub.key.eccPub.data[0] = 0x04; GOTO_ERR_IF_TRUE(memcpy_s(pub.key.eccPub.data + 1, pub.key.eccPub.len, x, xLen) != EOK, CRYPT_SECUREC_FAIL); GOTO_ERR_IF_TRUE(memcpy_s(pub.key.eccPub.data + 1 + xLen, pub.key.eccPub.len, y, yLen) != EOK, CRYPT_SECUREC_FAIL); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(*pkeyPub, &pub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(pub.key.eccPub.data); BSL_SAL_FREE(x); BSL_SAL_FREE(y); return ret; } bool CRYPT_CMVP_SelftestSM2Crypt(void *libCtx, const char *attrName) { bool ret = false; CRYPT_EAL_PkeyCtx *pubCtx = NULL; CRYPT_EAL_PkeyCtx *prvCtx = NULL; uint8_t *plain = NULL; uint32_t plainLen; uint8_t *cipher = NULL; uint32_t cipherLen; uint8_t cipherText[MAX_PLAIN_TEXT_LEN + CIPHER_TEXT_EXTRA_LEN] = {0}; uint32_t cipherTextLen = sizeof(cipherText); uint8_t decodeText[MAX_PLAIN_TEXT_LEN + CIPHER_TEXT_EXTRA_LEN] = {0}; uint32_t decodeoutLen = sizeof(decodeText); uint8_t plainText[MAX_PLAIN_TEXT_LEN] = {0}; uint32_t plainTextLen = sizeof(plainText); uint8_t encodeText[MAX_PLAIN_TEXT_LEN + CIPHER_TEXT_EXTRA_LEN] = {0}; uint32_t encodeTextLen = sizeof(encodeText); CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); pubCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_SM2, 0, attrName); GOTO_ERR_IF_TRUE(pubCtx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); prvCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_SM2, 0, attrName); GOTO_ERR_IF_TRUE(prvCtx == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); SetPrvPkey(&prvCtx, SM2_TEST_KEYS.d); SetPubPkey(&pubCtx, SM2_TEST_KEYS.qX, SM2_TEST_KEYS.qY); plain = CMVP_StringsToBins(SM2_CRYPT_TEST_VECTOR.plain, &plainLen); GOTO_ERR_IF_TRUE(plain == NULL, CRYPT_CMVP_COMMON_ERR); cipher = CMVP_StringsToBins(SM2_CRYPT_TEST_VECTOR.cipher, &cipherLen); GOTO_ERR_IF_TRUE(cipher == NULL, CRYPT_CMVP_COMMON_ERR); CRYPT_RandRegist(TestVectorRandom); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyEncrypt(pubCtx, plain, plainLen, cipherText, &cipherTextLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); CRYPT_SM2_EncryptData data = { .x = decodeText + 1, .xLen = SM2_POINT_SINGLE_COORDINATE_LEN, .y = decodeText + SM2_POINT_SINGLE_COORDINATE_LEN + 1, .yLen = SM2_POINT_SINGLE_COORDINATE_LEN, .hash = decodeText + SM2_POINT_COORDINATE_LEN, .hashLen = SM3_MD_SIZE, .cipher = decodeText + SM2_POINT_COORDINATE_LEN + SM3_MD_SIZE, .cipherLen = decodeoutLen - SM2_POINT_COORDINATE_LEN - SM3_MD_SIZE }; GOTO_ERR_IF_TRUE(CRYPT_EAL_DecodeSm2EncryptData(cipherText, cipherTextLen, &data) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); decodeText[0] = 0x04; decodeoutLen = SM2_POINT_SINGLE_COORDINATE_LEN + SM2_POINT_SINGLE_COORDINATE_LEN + SM3_MD_SIZE + data.cipherLen; GOTO_ERR_IF_TRUE(decodeoutLen + 1 != cipherLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(decodeText, cipher, cipherLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_EncodeSm2EncryptData(&data, encodeText, &encodeTextLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE( CRYPT_EAL_PkeyDecrypt(prvCtx, encodeText, encodeTextLen, plainText, &plainTextLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(plainTextLen != plainLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(plainText, plain, plainLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(plain); BSL_SAL_FREE(cipher); CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); CRYPT_EAL_PkeyFreeCtx(pubCtx); CRYPT_EAL_PkeyFreeCtx(prvCtx); return ret; } static int32_t SignEncode(const char *signR, const char *signS, uint8_t *vectorSign, uint32_t *vectorSignLen) { int ret = CRYPT_CMVP_ERR_ALGO_SELFTEST; BN_BigNum *bnR = NULL; BN_BigNum *bnS = NULL; uint8_t *r = NULL; uint8_t *s = NULL; uint32_t rLen, sLen; r = CMVP_StringsToBins(signR, &rLen); GOTO_ERR_IF_TRUE(r == NULL, CRYPT_CMVP_COMMON_ERR); s = CMVP_StringsToBins(signS, &sLen); GOTO_ERR_IF_TRUE(s == NULL, CRYPT_CMVP_COMMON_ERR); bnR = BN_Create(rLen * BITS_OF_BYTE); bnS = BN_Create(sLen * BITS_OF_BYTE); GOTO_ERR_IF_TRUE(BN_Bin2Bn(bnR, r, rLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(BN_Bin2Bn(bnS, s, sLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = CRYPT_EAL_EncodeSign(bnR, bnS, vectorSign, vectorSignLen); ERR: BSL_SAL_FREE(r); BSL_SAL_FREE(s); BN_Destroy(bnR); BN_Destroy(bnS); return ret; } static bool SetUserId(CRYPT_EAL_PkeyCtx *pkey, const char id[]) { bool ret = false; uint8_t *userId = NULL; uint32_t userIdLen; userId = CMVP_StringsToBins(id, &(userIdLen)); GOTO_ERR_IF_TRUE(userId == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(pkey, CRYPT_CTRL_SET_SM2_USER_ID, userId, userIdLen) != CRYPT_SUCCESS, CRYPT_CMVP_COMMON_ERR); ret = true; ERR: BSL_SAL_FREE(userId); return ret; } bool CRYPT_CMVP_SelftestSM2Sign(void *libCtx, const char *attrName) { bool ret = false; uint8_t *sign = NULL; uint8_t *signVec = NULL; uint32_t signLen; uint32_t signVecLen = 0; uint8_t *msg = NULL; uint32_t msgLen; CRYPT_EAL_PkeyCtx *pkeyPrv = NULL; CRYPT_EAL_PkeyCtx *pkeyPub = NULL; CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); msg = CMVP_StringsToBins(SM2DSA_VECTOR.msg, &msgLen); GOTO_ERR_IF_TRUE(msg == NULL, CRYPT_CMVP_COMMON_ERR); pkeyPrv = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_SM2, 0, attrName); GOTO_ERR_IF_TRUE(pkeyPrv == NULL, CRYPT_CMVP_COMMON_ERR); pkeyPub = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_SM2, 0, attrName); GOTO_ERR_IF_TRUE(pkeyPub == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(SetUserId(pkeyPub, SM2DSA_VECTOR.userid) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetUserId(pkeyPrv, SM2DSA_VECTOR.userid) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetPrvPkey(&pkeyPrv, SM2_TEST_KEYS.d) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetPubPkey(&pkeyPub, SM2_TEST_KEYS.qX, SM2_TEST_KEYS.qY) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); signLen = CRYPT_EAL_PkeyGetSignLen(pkeyPrv); sign = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_MEM_ALLOC_FAIL); // regist rand function CRYPT_RandRegist(TestVectorRandom); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(pkeyPrv, SM2DSA_VECTOR.mdId, msg, msgLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); // compare the signature signVecLen = CRYPT_EAL_PkeyGetSignLen(pkeyPrv); signVec = (uint8_t *)BSL_SAL_Malloc(signVecLen); GOTO_ERR_IF_TRUE(signVec == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(SignEncode(SM2DSA_VECTOR.signR, SM2DSA_VECTOR.signS, signVec, &signVecLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(signLen != signVecLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(signVec, sign, signLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE( CRYPT_EAL_PkeyVerify(pkeyPub, SM2DSA_VECTOR.mdId, msg, msgLen, signVec, signVecLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(sign); BSL_SAL_FREE(signVec); BSL_SAL_FREE(msg); CRYPT_EAL_PkeyFreeCtx(pkeyPrv); CRYPT_EAL_PkeyFreeCtx(pkeyPub); CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); return ret; } static int32_t TestExchangeVectorRandom(uint8_t *r, uint32_t rLen) { return SetRandomVector(SM2Exchange_VECTOR.r, r, rLen); } bool CRYPT_CMVP_SelftestSM2Exchange(void *libCtx, const char *attrName) { bool ret = false; uint8_t *R = NULL; uint8_t *out = NULL; uint8_t *sharekey = NULL; uint8_t localR[65]; int32_t server = 1; CRYPT_EAL_PkeyCtx *selfCtx = NULL; CRYPT_EAL_PkeyCtx *peerCtx = NULL; CRYPT_EAL_RandFunc func = CRYPT_RandRegistGet(); CRYPT_EAL_RandFuncEx funcEx = CRYPT_RandRegistExGet(); CRYPT_RandRegistEx(NULL); selfCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_SM2, 0, attrName); GOTO_ERR_IF_TRUE(selfCtx == NULL, CRYPT_CMVP_COMMON_ERR); peerCtx = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_SM2, 0, attrName); GOTO_ERR_IF_TRUE(peerCtx == NULL, CRYPT_CMVP_COMMON_ERR); uint32_t RLen; R = CMVP_StringsToBins(SM2Exchange_VECTOR.R, &RLen); GOTO_ERR_IF_TRUE(R == NULL, CRYPT_CMVP_COMMON_ERR); uint32_t sharekeyLen; sharekey = CMVP_StringsToBins(SM2Exchange_VECTOR.sharekey, &sharekeyLen); GOTO_ERR_IF_TRUE(sharekey == NULL, CRYPT_CMVP_COMMON_ERR); uint32_t outLen = sharekeyLen; out = BSL_SAL_Malloc(outLen); GOTO_ERR_IF_TRUE(out == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(SetUserId(selfCtx, SM2Exchange_VECTOR.userid1) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(selfCtx, CRYPT_CTRL_SET_SM2_SERVER, &server, sizeof(int32_t)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); CRYPT_RandRegist(TestExchangeVectorRandom); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(selfCtx, CRYPT_CTRL_GENE_SM2_R, localR, sizeof(localR)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetUserId(peerCtx, SM2Exchange_VECTOR.userid2) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyCtrl(peerCtx, CRYPT_CTRL_SET_SM2_R, R, RLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetPrvPkey(&selfCtx, SM2Exchange_VECTOR.self_d) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetPrvPkey(&peerCtx, SM2Exchange_VECTOR.peer_d) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetPubPkey(&selfCtx, SM2Exchange_VECTOR.self_x, SM2Exchange_VECTOR.self_y) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetPubPkey(&peerCtx, SM2Exchange_VECTOR.peer_x, SM2Exchange_VECTOR.peer_y) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyComputeShareKey(selfCtx, peerCtx, out, &outLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(outLen != sharekeyLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(out, sharekey, sharekeyLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(sharekey); BSL_SAL_FREE(out); BSL_SAL_FREE(R); CRYPT_EAL_PkeyFreeCtx(peerCtx); CRYPT_EAL_PkeyFreeCtx(selfCtx); CRYPT_RandRegist(func); CRYPT_RandRegistEx(funcEx); return ret; } static bool SM2_Consistency_Sign(void) { bool ret = false; CRYPT_EAL_PkeyCtx *pkey = NULL; uint8_t *sign = NULL; uint32_t signLen; uint8_t *data = NULL; uint32_t dataLen; pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2); GOTO_ERR_IF_TRUE(pkey == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(SetUserId(pkey, SM2DSA_VECTOR.userid) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); signLen = CRYPT_EAL_PkeyGetSignLen(pkey); sign = BSL_SAL_Malloc(signLen); GOTO_ERR_IF_TRUE(sign == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyGen(pkey) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); data = CMVP_StringsToBins(consistestdata, &dataLen); GOTO_ERR_IF_TRUE(data == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySign(pkey, CRYPT_MD_SM3, data, dataLen, sign, &signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyVerify(pkey, CRYPT_MD_SM3, data, dataLen, sign, signLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(sign); BSL_SAL_FREE(data); CRYPT_EAL_PkeyFreeCtx(pkey); return ret; } static bool SM2_Consistency_Crypt(void) { bool ret = false; uint8_t *plain = NULL; uint32_t plainLen; uint8_t cipherText[MAX_PLAIN_TEXT_LEN + CIPHER_TEXT_EXTRA_LEN] = {0}; uint32_t cipherTextLen = sizeof(cipherText); uint8_t decodeText[MAX_PLAIN_TEXT_LEN + CIPHER_TEXT_EXTRA_LEN] = {0}; uint32_t decodeTextLen = sizeof(decodeText); uint8_t plainText[MAX_PLAIN_TEXT_LEN] = {0}; uint32_t plainTextLen = sizeof(plainText); uint8_t encodeText[MAX_PLAIN_TEXT_LEN + CIPHER_TEXT_EXTRA_LEN] = {0}; uint32_t encodeTextLen = sizeof(encodeText); CRYPT_EAL_PkeyCtx *pkey = NULL; pkey = CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_SM2); GOTO_ERR_IF_TRUE(pkey == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyGen(pkey) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); plain = CMVP_StringsToBins(SM2_CRYPT_TEST_VECTOR.plain, &plainLen); GOTO_ERR_IF_TRUE(plain == NULL, CRYPT_CMVP_COMMON_ERR); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyEncrypt(pkey, plain, plainLen, cipherText, &cipherTextLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); CRYPT_SM2_EncryptData data = { .x = decodeText+ 1, .xLen = SM2_POINT_SINGLE_COORDINATE_LEN, .y = decodeText + SM2_POINT_SINGLE_COORDINATE_LEN + 1, .yLen = SM2_POINT_SINGLE_COORDINATE_LEN, .hash = decodeText + SM2_POINT_COORDINATE_LEN, .hashLen = SM3_MD_SIZE, .cipher = decodeText + SM2_POINT_COORDINATE_LEN + SM3_MD_SIZE, .cipherLen = decodeTextLen - SM2_POINT_COORDINATE_LEN - SM3_MD_SIZE }; GOTO_ERR_IF_TRUE(CRYPT_EAL_DecodeSm2EncryptData(cipherText, cipherTextLen, &data) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); decodeText[0] = 0x04; GOTO_ERR_IF_TRUE(memcmp(decodeText, plain, plainLen) == 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_EncodeSm2EncryptData(&data, encodeText, &encodeTextLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyDecrypt(pkey, encodeText, encodeTextLen, plainText, &plainTextLen) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(plainTextLen != plainLen, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(plainText, plain, plainLen) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_FREE(plain); CRYPT_EAL_PkeyFreeCtx(pkey); return ret; } bool CRYPT_CMVP_SelftestSM2Consistency(void) { return SM2_Consistency_Sign() && SM2_Consistency_Crypt(); } bool CRYPT_CMVP_SelftestSM2(void) { return CRYPT_CMVP_SelftestSM2Sign(NULL, NULL) && CRYPT_CMVP_SelftestSM2Crypt(NULL, NULL) && CRYPT_CMVP_SelftestSM2Exchange(NULL, NULL) && CRYPT_CMVP_SelftestSM2Consistency(); } bool CRYPT_CMVP_SelftestProviderSM2(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestSM2Sign(libCtx, attrName) && CRYPT_CMVP_SelftestSM2Crypt(libCtx, attrName) && CRYPT_CMVP_SelftestSM2Exchange(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_SM || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_sm2.c
C
unknown
21,294
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CMVP_ISO19790) || defined(HITLS_CRYPTO_CMVP_FIPS) #include <string.h> #include "crypt_cmvp_selftest.h" #include "cmvp_common.h" #include "err.h" #include "crypt_errno.h" #include "crypt_eal_pkey.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "bsl_sal.h" typedef struct { const char *alicePri; const char *alicePub; const char *bobPri; const char *bobPub; const char *share1; } CMVP_X25519_VECTOR; // https://datatracker.ietf.org/doc/html/rfc7748.html#page-14 static const CMVP_X25519_VECTOR X25519_VECTOR = { .alicePri = "77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a", .alicePub = "8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a", .bobPri = "5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb", .bobPub = "de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f", .share1 = "4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742" }; static bool GetData(CRYPT_Data *expShare, CRYPT_Data *share1, CRYPT_Data *share2) { expShare->data = CMVP_StringsToBins(X25519_VECTOR.share1, &(expShare->len)); GOTO_ERR_IF_TRUE(expShare->data == NULL, CRYPT_CMVP_COMMON_ERR); share1->len = expShare->len; share1->data = BSL_SAL_Malloc(share1->len); GOTO_ERR_IF_TRUE(share1->data == NULL, CRYPT_MEM_ALLOC_FAIL); share2->len = expShare->len; share2->data = BSL_SAL_Malloc(share2->len); GOTO_ERR_IF_TRUE(share2->data == NULL, CRYPT_MEM_ALLOC_FAIL); return true; ERR: return false; } static bool GetKey(CRYPT_EAL_PkeyPrv *alicePri, CRYPT_EAL_PkeyPub *alicePub, CRYPT_EAL_PkeyPrv *bobPri, CRYPT_EAL_PkeyPub *bobPub) { alicePri->id = CRYPT_PKEY_X25519; alicePri->key.curve25519Prv.data = CMVP_StringsToBins(X25519_VECTOR.alicePri, &(alicePri->key.curve25519Prv.len)); GOTO_ERR_IF_TRUE(alicePri->key.curve25519Prv.data == NULL, CRYPT_CMVP_COMMON_ERR); alicePub->id = CRYPT_PKEY_X25519; alicePub->key.curve25519Pub.data = CMVP_StringsToBins(X25519_VECTOR.alicePub, &(alicePub->key.curve25519Pub.len)); GOTO_ERR_IF_TRUE(alicePub->key.curve25519Pub.data == NULL, CRYPT_CMVP_COMMON_ERR); bobPri->id = CRYPT_PKEY_X25519; bobPri->key.curve25519Prv.data = CMVP_StringsToBins(X25519_VECTOR.bobPri, &(bobPri->key.curve25519Prv.len)); GOTO_ERR_IF_TRUE(bobPri->key.curve25519Prv.data == NULL, CRYPT_CMVP_COMMON_ERR); bobPub->id = CRYPT_PKEY_X25519; bobPub->key.curve25519Pub.data = CMVP_StringsToBins(X25519_VECTOR.bobPub, &(bobPub->key.curve25519Pub.len)); GOTO_ERR_IF_TRUE(bobPub->key.curve25519Pub.data == NULL, CRYPT_CMVP_COMMON_ERR); return true; ERR: return false; } static bool CRYPT_CMVP_SelftestX25519Internal(void *libCtx, const char *attrName) { bool ret = false; CRYPT_Data expShare = { NULL, 0 }; CRYPT_Data share1 = { NULL, 0 }; CRYPT_Data share2 = { NULL, 0 }; CRYPT_EAL_PkeyCtx *alice = NULL; CRYPT_EAL_PkeyCtx *bob = NULL; CRYPT_EAL_PkeyPrv alicePri = {0}; alicePri.key.curve25519Prv.data = NULL; CRYPT_EAL_PkeyPub alicePub; alicePub.key.curve25519Pub.data = NULL; CRYPT_EAL_PkeyPrv bobPri = {0}; bobPri.key.curve25519Prv.data = NULL; CRYPT_EAL_PkeyPub bobPub; bobPub.key.curve25519Pub.data = NULL; alice = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_X25519, 0, attrName); GOTO_ERR_IF_TRUE(alice == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); bob = CRYPT_EAL_ProviderPkeyNewCtx(libCtx, CRYPT_PKEY_X25519, 0, attrName); GOTO_ERR_IF_TRUE(bob == NULL, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(GetKey(&alicePri, &alicePub, &bobPri, &bobPub) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(GetData(&expShare, &share1, &share2) != true, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(alice, &alicePri) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(bob, &bobPub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyComputeShareKey(alice, bob, share1.data, &(share1.len)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(share1.len != expShare.len, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(share1.data, expShare.data, expShare.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPrv(bob, &bobPri) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeySetPub(alice, &alicePub) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(CRYPT_EAL_PkeyComputeShareKey(bob, alice, share2.data, &(share2.len)) != CRYPT_SUCCESS, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(share2.len != expShare.len, CRYPT_CMVP_ERR_ALGO_SELFTEST); GOTO_ERR_IF_TRUE(memcmp(share2.data, expShare.data, expShare.len) != 0, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = true; ERR: BSL_SAL_Free(alicePri.key.curve25519Prv.data); BSL_SAL_Free(alicePub.key.curve25519Pub.data); BSL_SAL_Free(bobPri.key.curve25519Prv.data); BSL_SAL_Free(bobPub.key.curve25519Pub.data); BSL_SAL_Free(expShare.data); BSL_SAL_Free(share1.data); BSL_SAL_Free(share2.data); CRYPT_EAL_PkeyFreeCtx(alice); CRYPT_EAL_PkeyFreeCtx(bob); return ret; } bool CRYPT_CMVP_SelftestX25519(void) { return CRYPT_CMVP_SelftestX25519Internal(NULL, NULL); } bool CRYPT_CMVP_SelftestProviderX25519(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestX25519Internal(libCtx, attrName); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 || HITLS_CRYPTO_CMVP_FIPS */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/cmvp_selftest_x25519.c
C
unknown
6,185
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_CMVP_H #define CRYPT_CMVP_H #include "hitls_build.h" #include <stdint.h> #include "bsl_params.h" #include "crypt_types.h" #include "crypt_eal_pkey.h" #include "crypt_eal_rand.h" #include "crypt_eal_md.h" #include "crypt_eal_mac.h" #include "crypt_eal_cipher.h" #include "crypt_eal_kdf.h" #include "crypt_cmvp.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ typedef void* (*CmvpProvNewCtx)(void *provCtx); typedef const char* (*CmvpGetVersion)(void *ctx); typedef int32_t (*CmvpSelftest)(void *ctx, const BSL_Param *param); typedef void (*CmvpFreeCtx)(void *ctx); typedef struct { CmvpProvNewCtx provNewCtx; CmvpGetVersion getVersion; CmvpSelftest selftest; CmvpFreeCtx freeCtx; } EAL_CmvpSelftestMethod; struct EAL_SelftestCtx { bool isProvider; EAL_CmvpSelftestMethod *method; void *data; uint32_t state; int32_t id; }; typedef struct { CRYPT_MAC_AlgId macId; /**< MAC algorithm ID */ uint32_t saltLen; /**< Salt length in bytes */ uint32_t iter; uint32_t dkeyLen; /**< Derived key length in bytes */ } CRYPT_EAL_Pbkdf2Param; typedef struct { CRYPT_MAC_AlgId macId; /**< MAC algorithm ID */ uint32_t keyLen; /**< Derived key length in bytes */ } CRYPT_EAL_HkdfParam; typedef struct { CRYPT_EAL_Pbkdf2Param *pbkdf2; CRYPT_EAL_HkdfParam *hkdf; } CRYPT_EAL_KdfC2Data; typedef struct { const CRYPT_EAL_PkeyPara *para; const CRYPT_EAL_PkeyPub *pub; const CRYPT_EAL_PkeyPrv *prv; CRYPT_MD_AlgId mdId; /**< MD algorithm ID */ CRYPT_PKEY_ParaId paraId; /**< PKEY parameter ID */ CRYPT_EVENT_TYPE oper; const CRYPT_RSA_PkcsV15Para *pkcsv15; BSL_Param *pss; BSL_Param *oaep; } CRYPT_EAL_PkeyC2Data; #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* CRYPT_CMVP_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/cmvp_utils/crypt_cmvp.h
C
unknown
2,346
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_FIPS #endif
2301_79861745/bench_create
crypto/provider/src/cmvp/fips_prov/cmvp_fips.c
C
unknown
631
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CMVP_FIPS_H #define CMVP_FIPS_H #endif
2301_79861745/bench_create
crypto/provider/src/cmvp/fips_prov/cmvp_fips.h
C
unknown
591
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "cmvp_iso19790.h" #include "cmvp_common.h" #include "crypt_errno.h" #include "crypt_cmvp_selftest.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "cmvp_integrity_hmac.h" #include "crypt_params_key.h" #include "securec.h" #include "bsl_sal.h" void CMVP_Iso19790EventProcess(CRYPT_EVENT_TYPE oper, CRYPT_ALGO_TYPE type, int32_t id, int32_t err) { if (oper == CRYPT_EVENT_RANDGEN) { CMVP_WriteSyslog("openHiTLS", err == CRYPT_SUCCESS ? LOG_INFO : LOG_ERR, "Excute - entropy collection, result: 0x%x", err); return; } if (oper == CRYPT_EVENT_INTEGRITY_TEST) { if (err == CRYPT_SUCCESS) { CMVP_WriteSyslog("openHiTLS", LOG_INFO, "Integrity test begin."); } else { CMVP_WriteSyslog("openHiTLS", LOG_ERR, "Integrity test failed, errcode: 0x%x", err); } return; } // ISO/IEC 19790:2012 AS09.33 // The module shall provide an output status indication when zeroing is complete if (oper == CRYPT_EVENT_ZERO && err == CRYPT_SUCCESS) { CMVP_WriteSyslog("openHiTLS", LOG_INFO, "SSP already zeroisation - algorithm type: %d, id: %d", type, id); } /* ISO/IEC 19790:2012 AS06.26 The following events of the cryptographic module should be recorded by the OS audit mechanism: ● Attempted to provide invalid input for the cryptographic officer function; */ if (err != CRYPT_SUCCESS) { CMVP_WriteSyslog("openHiTLS", LOG_ERR, "Occur error - algorithm type: %d, id: %d, operate: %d, errcode: 0x%x", type, id, oper, err); } /* ISO/IEC 19790:2012 AS06.26 The following events of the cryptographic module should be recorded by the OS audit mechanism: ● Modify, access, delete, and add encrypted data and SSPs; ● Use security-related encryption features ISO/IEC 19790:2012 AS02.24 When a service uses approved encryption algorithms, security functions or processes, and specified services or processes in an approved manner, the service shall provide corresponding status indications. */ CMVP_WriteSyslog("openHiTLS", LOG_INFO, "Excute - algorithm type: %d, id: %d, operate: %d", type, id, oper); } typedef struct { uint32_t algId; uint32_t mdId; bool signValid; bool verifyValid; } ASYM_MD_MAP; static const ASYM_MD_MAP ASYM_MD_LIST[] = { { CRYPT_PKEY_DSA, CRYPT_MD_SHA1, false, true }, { CRYPT_PKEY_DSA, CRYPT_MD_SHA224, true, true }, { CRYPT_PKEY_DSA, CRYPT_MD_SHA256, true, true }, { CRYPT_PKEY_DSA, CRYPT_MD_SHA384, true, true }, { CRYPT_PKEY_DSA, CRYPT_MD_SHA512, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA1, false, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA224, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA256, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA384, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA512, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA3_224, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA3_256, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA3_384, true, true }, { CRYPT_PKEY_ECDSA, CRYPT_MD_SHA3_512, true, true }, { CRYPT_PKEY_RSA, CRYPT_MD_SHA1, false, true }, { CRYPT_PKEY_RSA, CRYPT_MD_SHA224, true, true }, { CRYPT_PKEY_RSA, CRYPT_MD_SHA256, true, true }, { CRYPT_PKEY_RSA, CRYPT_MD_SHA384, true, true }, { CRYPT_PKEY_RSA, CRYPT_MD_SHA512, true, true }, { CRYPT_PKEY_SM2, CRYPT_MD_SM3, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA224, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA256, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA384, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA512, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA3_224, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA3_256, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA3_384, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHA3_512, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHAKE128, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_SHAKE256, true, true }, { CRYPT_PKEY_SLH_DSA, CRYPT_MD_MAX, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA224, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA256, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA384, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA512, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA3_224, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA3_256, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA3_384, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHA3_512, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHAKE128, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_SHAKE256, true, true }, { CRYPT_PKEY_ML_DSA, CRYPT_MD_MAX, true, true }, }; static bool GetVaildFlag(uint32_t algId, uint32_t mdId, bool isSign) { if (algId == CRYPT_PKEY_ED25519) { return true; } for (uint32_t i = 0; i < sizeof(ASYM_MD_LIST) / sizeof(ASYM_MD_LIST[0]); i++) { if (isSign == true && algId == ASYM_MD_LIST[i].algId && mdId == ASYM_MD_LIST[i].mdId) { return ASYM_MD_LIST[i].signValid; } else if (isSign == false && algId == ASYM_MD_LIST[i].algId && mdId == ASYM_MD_LIST[i].mdId) { return ASYM_MD_LIST[i].verifyValid; } } return false; } // Check whether the RSA parameter is approved. static bool RsaParamCheck(const CRYPT_EAL_PkeyC2Data *data) { if (data->para != NULL) { CRYPT_RsaPara para = data->para->para.rsaPara; // The length of the RSA key must be at least 2048 bits. GOTO_ERR_IF_TRUE(para.bits < 2048, CRYPT_CMVP_ERR_PARAM_CHECK); return true; } if (data->pub != NULL) { CRYPT_RsaPub pub = data->pub->key.rsaPub; // The length of the RSA key must be at least 2048 bits. 8 bits are 1 byte. GOTO_ERR_IF_TRUE(pub.nLen < (2048 / 8), CRYPT_CMVP_ERR_PARAM_CHECK); return true; } if (data->prv != NULL) { CRYPT_RsaPrv prv = data->prv->key.rsaPrv; // The length of the RSA key must be at least 2048 bits. 8 bits are 1 byte. GOTO_ERR_IF_TRUE(prv.nLen < (2048 / 8), CRYPT_CMVP_ERR_PARAM_CHECK); return true; } if (data->pkcsv15 != NULL) { GOTO_ERR_IF_TRUE( (GetVaildFlag(CRYPT_PKEY_RSA, data->pkcsv15->mdId, false) == false), CRYPT_CMVP_ERR_PARAM_CHECK); return true; } if (data->pss != NULL) { BSL_Param *mdParam = BSL_PARAM_FindParam(data->pss, CRYPT_PARAM_RSA_MD_ID); GOTO_ERR_IF_TRUE(mdParam == NULL, CRYPT_CMVP_ERR_PARAM_CHECK); BSL_Param *mgfParam = BSL_PARAM_FindParam(data->pss, CRYPT_PARAM_RSA_MGF1_ID); GOTO_ERR_IF_TRUE(mgfParam == NULL, CRYPT_CMVP_ERR_PARAM_CHECK); GOTO_ERR_IF_TRUE((GetVaildFlag(CRYPT_PKEY_RSA, *(uint32_t *)(mdParam->value), false) == false), CRYPT_CMVP_ERR_PARAM_CHECK); GOTO_ERR_IF_TRUE((GetVaildFlag(CRYPT_PKEY_RSA, *(uint32_t *)(mgfParam->value), false) == false), CRYPT_CMVP_ERR_PARAM_CHECK); return true; } if (data->oaep != NULL) { BSL_Param *mdParam = BSL_PARAM_FindParam(data->oaep, CRYPT_PARAM_RSA_MD_ID); GOTO_ERR_IF_TRUE(mdParam == NULL, CRYPT_CMVP_ERR_PARAM_CHECK); BSL_Param *mgfParam = BSL_PARAM_FindParam(data->oaep, CRYPT_PARAM_RSA_MGF1_ID); GOTO_ERR_IF_TRUE(mgfParam == NULL, CRYPT_CMVP_ERR_PARAM_CHECK); GOTO_ERR_IF_TRUE((GetVaildFlag(CRYPT_PKEY_RSA, *(uint32_t *)(mdParam->value), false) == false), CRYPT_CMVP_ERR_PARAM_CHECK); GOTO_ERR_IF_TRUE((GetVaildFlag(CRYPT_PKEY_RSA, *(uint32_t *)(mgfParam->value), false) == false), CRYPT_CMVP_ERR_PARAM_CHECK); return true; } return true; ERR: return false; } // Check whether the DSA parameter is approved static bool DsaParamCheck(const CRYPT_EAL_PkeyC2Data *data) { if (data->para == NULL) { return true; } uint32_t pLen = data->para->para.dsaPara.pLen; uint32_t qLen = data->para->para.dsaPara.qLen; // https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf Chapter 3 // (L, N) = (2048, 224),(2048, 256), 8 bits: 1 byte if ((pLen != 2048 / 8 || qLen != 224 / 8) && (pLen != 2048 / 8 || qLen != 256 / 8) && // (L, N) = (3072, 256), 8 bits: 1 byte (pLen != 3072 / 8 || qLen != 256 / 8)) { return false; } return true; } // Check whether the dh parameter is approved static bool DhParamCheck(const CRYPT_EAL_PkeyC2Data *data) { static const uint32_t list[] = { CRYPT_DH_RFC2409_768, CRYPT_DH_RFC2409_1024, CRYPT_DH_RFC3526_1536, }; if (data->para != NULL) { uint32_t pLen = data->para->para.dhPara.pLen; uint32_t qLen = data->para->para.dhPara.qLen; // https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf Chapter 5 // (len(p), len(q)) = (2048, 224) // The length of p must be at least 2048 bits, and the length of q must be at least 224 bits. GOTO_ERR_IF_TRUE(((pLen != 2048 / 8 || qLen != 224 / 8) && // (len(p), len(q)) = (2048, 256) (pLen != 2048 / 8 || qLen != 256 / 8)), CRYPT_CMVP_ERR_PARAM_CHECK); } if (data->paraId != CRYPT_PKEY_PARAID_MAX) { // The length of p must be at least 2048 bits, and the length of q must be at least 224 bits. for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { GOTO_ERR_IF_TRUE((data->paraId == list[i]), CRYPT_CMVP_ERR_PARAM_CHECK); } } return true; ERR: return false; } static bool EcdhParamCheck(const CRYPT_EAL_PkeyC2Data *data) { // https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf Chapters 3 and 5 // Requires curve specified using SP 800-56A static const uint32_t list[] = { CRYPT_ECC_NISTP224, CRYPT_ECC_NISTP256, CRYPT_ECC_NISTP384, CRYPT_ECC_NISTP521, CRYPT_ECC_SM2, CRYPT_PKEY_PARAID_MAX, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (data->paraId == list[i]) { return true; } } return false; } static bool EcdsaParamCheck(const CRYPT_EAL_PkeyC2Data *data) { // https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf Chapters 3 and 5 // Requires curve specified using SP 800-56A static const uint32_t list[] = { CRYPT_ECC_NISTP224, CRYPT_ECC_NISTP256, CRYPT_ECC_NISTP384, CRYPT_ECC_NISTP521, CRYPT_ECC_BRAINPOOLP256R1, CRYPT_ECC_BRAINPOOLP384R1, CRYPT_ECC_BRAINPOOLP512R1, CRYPT_PKEY_PARAID_MAX, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (data->paraId == list[i]) { return true; } } return false; } static bool ISO19790_AsymParamCheck(CRYPT_PKEY_AlgId id, const CRYPT_EAL_PkeyC2Data *data) { // If the value is NULL, the interface does not need to check the algorithm parameters // and directly returns a success message. if (data == NULL) { return true; } switch (id) { case CRYPT_PKEY_DSA: GOTO_ERR_IF_TRUE(DsaParamCheck(data) != true, CRYPT_CMVP_ERR_PARAM_CHECK); break; case CRYPT_PKEY_RSA: GOTO_ERR_IF_TRUE(RsaParamCheck(data) != true, CRYPT_CMVP_ERR_PARAM_CHECK); break; case CRYPT_PKEY_DH: GOTO_ERR_IF_TRUE(DhParamCheck(data) != true, CRYPT_CMVP_ERR_PARAM_CHECK); break; case CRYPT_PKEY_ECDH: GOTO_ERR_IF_TRUE(EcdhParamCheck(data) != true, CRYPT_CMVP_ERR_PARAM_CHECK); break; case CRYPT_PKEY_ECDSA: GOTO_ERR_IF_TRUE(EcdsaParamCheck(data) != true, CRYPT_CMVP_ERR_PARAM_CHECK); break; default: break; } if (data->oper == CRYPT_EVENT_SIGN) { GOTO_ERR_IF_TRUE((GetVaildFlag(id, data->mdId, true) == false), CRYPT_CMVP_ERR_PARAM_CHECK); return true; } if (data->oper == CRYPT_EVENT_VERIFY) { GOTO_ERR_IF_TRUE((GetVaildFlag(id, data->mdId, false) == false), CRYPT_CMVP_ERR_PARAM_CHECK); return true; } return true; ERR: return false; } static bool ISO19790_MacParamCheck(CRYPT_MAC_AlgId id, uint32_t keyLen) { (void)id; // https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf Chapter 10 // Key lengths ≥ 112 bits, 8 bits: 1 byte if (keyLen >= (112 / 8)) { return true; } return false; } static bool ISO19790_KdfTls12ParamCheck(CRYPT_MAC_AlgId id, uint32_t keyLen) { static const uint32_t list[] = { CRYPT_MAC_HMAC_SHA256, CRYPT_MAC_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { // https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf Chapter 8 // Key lengths ≥ 112 bits, 8 bits: 1 byte if (id == list[i] && (keyLen >= (112 / 8))) { return true; } } return false; } static bool ISO19790_HkdfParamCheck(CRYPT_MAC_AlgId id, uint32_t keyLen) { static const uint32_t list[] = { CRYPT_MAC_HMAC_SHA1, CRYPT_MAC_HMAC_SHA224, CRYPT_MAC_HMAC_SHA256, CRYPT_MAC_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512 }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { // https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf Chapter 8 // Key lengths ≥ 112 bits, 8 bits: 1 byte if (id == list[i] && (keyLen >= (112 / 8))) { return true; } } return false; } static bool ISO19790_PbkdfParamCheck(const CRYPT_EAL_Pbkdf2Param *param) { static const uint32_t list[] = { CRYPT_MAC_HMAC_SHA1, CRYPT_MAC_HMAC_SHA224, CRYPT_MAC_HMAC_SHA256, CRYPT_MAC_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512, CRYPT_MAC_HMAC_SM3, CRYPT_MAC_HMAC_SHA3_224, CRYPT_MAC_HMAC_SHA3_256, CRYPT_MAC_HMAC_SHA3_384, CRYPT_MAC_HMAC_SHA3_512 }; bool ret = false; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (param->macId == list[i]) { ret = true; break; } } if (!ret) { BSL_ERR_PUSH_ERROR(CRYPT_ERR_ALGID); return false; } if (param->saltLen < 16) { // FIPS SP800-132 section 5,The salt value must contain at least 16 bytes. BSL_ERR_PUSH_ERROR(CRYPT_ERR_ALGID); return false; } if (param->iter < 1000) { // FIPS SP800-132 section 5,The number of iterations must be at least 1000. BSL_ERR_PUSH_ERROR(CRYPT_ERR_ALGID); return false; } if (param->dkeyLen < 14) { // FIPS SP800-132 section 5,The length of the derived key must be at least 14 bytes. BSL_ERR_PUSH_ERROR(CRYPT_ERR_ALGID); return false; } return true; } static bool ISO19790_CipherKat(void *libCtx, const char *attrName) { static const uint32_t list[] = { CRYPT_CIPHER_AES128_ECB, CRYPT_CIPHER_AES192_ECB, CRYPT_CIPHER_AES256_ECB, CRYPT_CIPHER_AES128_CBC, CRYPT_CIPHER_AES192_CBC, CRYPT_CIPHER_AES256_CBC, CRYPT_CIPHER_AES128_CTR, CRYPT_CIPHER_AES192_CTR, CRYPT_CIPHER_AES256_CTR, CRYPT_CIPHER_AES128_CCM, CRYPT_CIPHER_AES192_CCM, CRYPT_CIPHER_AES256_CCM, CRYPT_CIPHER_AES128_GCM, CRYPT_CIPHER_AES192_GCM, CRYPT_CIPHER_AES256_GCM, CRYPT_CIPHER_AES128_XTS, CRYPT_CIPHER_AES256_XTS, CRYPT_CIPHER_AES128_OFB, CRYPT_CIPHER_AES192_OFB, CRYPT_CIPHER_AES256_OFB, CRYPT_CIPHER_AES128_CFB, CRYPT_CIPHER_AES192_CFB, CRYPT_CIPHER_AES256_CFB, CRYPT_CIPHER_CHACHA20_POLY1305, CRYPT_CIPHER_SM4_XTS, CRYPT_CIPHER_SM4_CBC, CRYPT_CIPHER_SM4_ECB, CRYPT_CIPHER_SM4_CTR, CRYPT_CIPHER_SM4_GCM, CRYPT_CIPHER_SM4_CFB, CRYPT_CIPHER_SM4_OFB, }; bool ret = false; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (list[i] == CRYPT_CIPHER_CHACHA20_POLY1305) { ret = CRYPT_CMVP_SelftestProviderChacha20poly1305(libCtx, attrName); } else { ret = CRYPT_CMVP_SelftestProviderCipher(libCtx, attrName, list[i]); } if (!ret) { return false; } } return true; } static bool ISO19790_MdKat(void *libCtx, const char *attrName) { static const uint32_t list[] = { CRYPT_MD_SHA1, CRYPT_MD_SHA224, CRYPT_MD_SHA256, CRYPT_MD_SHA384, CRYPT_MD_SHA512, CRYPT_MD_SHA3_224, CRYPT_MD_SHA3_256, CRYPT_MD_SHA3_384, CRYPT_MD_SHA3_512, CRYPT_MD_SHAKE128, CRYPT_MD_SHAKE256, CRYPT_MD_SM3, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (!CRYPT_CMVP_SelftestProviderMd(libCtx, attrName, list[i])) { return false; } } return true; } static bool ISO19790_MacKat(void *libCtx, const char *attrName) { static const uint32_t list[] = { CRYPT_MAC_CMAC_AES128, CRYPT_MAC_CMAC_AES192, CRYPT_MAC_CMAC_AES256, CRYPT_MAC_GMAC_AES128, CRYPT_MAC_GMAC_AES192, CRYPT_MAC_GMAC_AES256, CRYPT_MAC_HMAC_SHA1, CRYPT_MAC_HMAC_SHA224, CRYPT_MAC_HMAC_SHA256, CRYPT_MAC_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512, CRYPT_MAC_HMAC_SM3, CRYPT_MAC_CMAC_SM4, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (!CRYPT_CMVP_SelftestProviderMac(libCtx, attrName, list[i])) { return false; } } return true; } static bool ISO19790_DrbgKat(void *libCtx, const char *attrName) { static const uint32_t list[] = { CRYPT_RAND_AES128_CTR, CRYPT_RAND_AES192_CTR, CRYPT_RAND_AES256_CTR, CRYPT_RAND_AES128_CTR_DF, CRYPT_RAND_AES192_CTR_DF, CRYPT_RAND_AES256_CTR_DF, CRYPT_RAND_HMAC_SHA1, CRYPT_RAND_HMAC_SHA224, CRYPT_RAND_HMAC_SHA256, CRYPT_RAND_HMAC_SHA384, CRYPT_RAND_HMAC_SHA512, CRYPT_RAND_SHA1, CRYPT_RAND_SHA224, CRYPT_RAND_SHA256, CRYPT_RAND_SHA384, CRYPT_RAND_SHA512, CRYPT_RAND_SM4_CTR_DF, CRYPT_RAND_SM3, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (!CRYPT_CMVP_SelftestProviderDrbg(libCtx, attrName, list[i])) { return false; } } return true; } static bool ISO19790_KdfKat(void *libCtx, const char *attrName) { if (!CRYPT_CMVP_SelftestProviderKdfTls12(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderHkdf(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderScrypt(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderPbkdf2(libCtx, attrName, CRYPT_MAC_HMAC_SHA1)) { return false; } if (!CRYPT_CMVP_SelftestProviderPbkdf2(libCtx, attrName, CRYPT_MAC_HMAC_SM3)) { return false; } return true; } static bool ISO19790_PkeyKat(void *libCtx, const char *attrName) { if (!CRYPT_CMVP_SelftestProviderDsa(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderEcdsa(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderRsa(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderEd25519(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderSM2(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderEcdh(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderDh(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderX25519(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderMlkemEncapsDecaps(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderMldsaSignVerify(libCtx, attrName)) { return false; } if (!CRYPT_CMVP_SelftestProviderSlhdsaSignVerify(libCtx, attrName)) { return false; } return true; } bool CMVP_Iso19790PkeyPct(CRYPT_Iso_Pkey_Ctx *ctx) { return CRYPT_CMVP_SelftestPkeyPct(ctx->ctx, ctx->algId); } bool CMVP_Iso19790PkeyC2(CRYPT_PKEY_AlgId id, const CRYPT_EAL_PkeyC2Data *data) { return ISO19790_AsymParamCheck(id, data); } bool CMVP_Iso19790MacC2(CRYPT_MAC_AlgId id, uint32_t keyLen) { return ISO19790_MacParamCheck(id, keyLen); } bool CMVP_Iso19790KdfC2(CRYPT_KDF_AlgId id, const CRYPT_EAL_KdfC2Data *data) { switch (id) { case CRYPT_KDF_SCRYPT: return false; case CRYPT_KDF_PBKDF2: return ISO19790_PbkdfParamCheck(data->pbkdf2); case CRYPT_KDF_KDFTLS12: return ISO19790_KdfTls12ParamCheck(data->hkdf->macId, data->hkdf->keyLen); case CRYPT_KDF_HKDF: return ISO19790_HkdfParamCheck(data->hkdf->macId, data->hkdf->keyLen); default: return false; } } int32_t CMVP_Iso19790Kat(void *libCtx, const char *attrName) { bool ret = false; ret = ISO19790_CipherKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = ISO19790_MdKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = ISO19790_MacKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = ISO19790_DrbgKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = ISO19790_KdfKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = ISO19790_PkeyKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); return CRYPT_SUCCESS; } int32_t CMVP_Iso19790CheckIntegrity(void *libCtx, const char *attrName) { return CMVP_CheckIntegrity(libCtx, attrName, CRYPT_MAC_HMAC_SHA256); } #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/cmvp_iso19790.c
C
unknown
22,558
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CMVP_ISO19790_H #define CMVP_ISO19790_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include <stdint.h> #include <stdbool.h> #include "crypt_cmvp.h" #include "crypt_algid.h" #include "crypt_iso_provderimpl.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ void CMVP_Iso19790EventProcess(CRYPT_EVENT_TYPE oper, CRYPT_ALGO_TYPE type, int32_t id, int32_t err); bool CMVP_Iso19790PkeyC2(CRYPT_PKEY_AlgId id, const CRYPT_EAL_PkeyC2Data *data); bool CMVP_Iso19790MacC2(CRYPT_MAC_AlgId id, uint32_t keyLen); bool CMVP_Iso19790KdfC2(CRYPT_KDF_AlgId id, const CRYPT_EAL_KdfC2Data *data); int32_t CMVP_Iso19790Kat(void *libCtx, const char *attrName); int32_t CMVP_Iso19790CheckIntegrity(void *libCtx, const char *attrName); bool CMVP_Iso19790PkeyPct(CRYPT_Iso_Pkey_Ctx *ctx); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_ISO19790 */ #endif /* CMVP_ISO19790_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/cmvp_iso19790.h
C
unknown
1,470
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "crypt_modes_cbc.h" #include "crypt_modes_ccm.h" #include "crypt_modes_chacha20poly1305.h" #include "crypt_modes_ctr.h" #include "crypt_modes_ecb.h" #include "crypt_modes_gcm.h" #include "crypt_modes_ofb.h" #include "crypt_modes_cfb.h" #include "crypt_modes_xts.h" #include "crypt_local_types.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provider.h" typedef struct { int32_t algId; void *ctx; void *provCtx; } IsoCipherCtx; static int32_t CRYPT_ASMCAP_CipherCheck(int32_t algId) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Cipher(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return CRYPT_EAL_ALG_ASM_NOT_SUPPORT; } #else (void)algId; #endif return CRYPT_SUCCESS; } #define CIPHER_NewCtx_FUNC(name) \ static void *MODES_##name##_NewCtxWrapper(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId) \ { \ if (CRYPT_ASMCAP_CipherCheck(algId) != CRYPT_SUCCESS) { \ return NULL; \ } \ if (provCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return NULL; \ } \ void *cipherCtx = MODES_##name##_NewCtx(algId); \ if (cipherCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ IsoCipherCtx *ctx = BSL_SAL_Calloc(1, sizeof(IsoCipherCtx)); \ if (ctx == NULL) { \ MODES_##name##_FreeCtx(cipherCtx); \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ ctx->algId = algId; \ ctx->ctx = cipherCtx; \ ctx->provCtx = provCtx; \ return ctx; \ } \ \ static int32_t MODES_##name##_CtrlWrapper(IsoCipherCtx *ctx, int32_t cmd, void *val, uint32_t valLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return MODES_##name##_Ctrl(ctx->ctx, cmd, val, valLen); \ } \ \ static void MODES_##name##_FreeCtxWrapper(IsoCipherCtx *ctx) \ { \ if (ctx == NULL) { \ return; \ } \ (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_CIPHER, ctx->algId); \ if (ctx->ctx != NULL) { \ MODES_##name##_FreeCtx(ctx->ctx); \ } \ BSL_SAL_Free(ctx); \ } #define CIPHER_INIT_FUNC(initFunc, updateFunc, finalFunc, deinitFunc) \ static int32_t initFunc##Wrapper(IsoCipherCtx *ctx, const uint8_t *key, uint32_t keyLen, \ const uint8_t *iv, uint32_t ivLen, void *param, bool enc) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t event = enc ? CRYPT_EVENT_ENC : CRYPT_EVENT_DEC; \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, event, CRYPT_ALGO_CIPHER, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_CIPHER, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return (initFunc)(ctx->ctx, key, keyLen, iv, ivLen, param, enc); \ } \ \ static int32_t updateFunc##Wrapper(IsoCipherCtx *ctx, const uint8_t *in, uint32_t inLen, \ uint8_t *out, uint32_t *outLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return (updateFunc)(ctx->ctx, in, inLen, out, outLen); \ } \ \ static int32_t finalFunc##Wrapper(IsoCipherCtx *ctx, uint8_t *out, uint32_t *outLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return (finalFunc)(ctx->ctx, out, outLen); \ } \ \ static int32_t deinitFunc##Wrapper(IsoCipherCtx *ctx) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_CIPHER, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return (deinitFunc)(ctx->ctx); \ } \ CIPHER_NewCtx_FUNC(CBC) CIPHER_NewCtx_FUNC(CCM) CIPHER_NewCtx_FUNC(CFB) CIPHER_NewCtx_FUNC(CTR) CIPHER_NewCtx_FUNC(ECB) CIPHER_NewCtx_FUNC(GCM) CIPHER_NewCtx_FUNC(OFB) CIPHER_NewCtx_FUNC(XTS) CIPHER_NewCtx_FUNC(CHACHA20POLY1305) CIPHER_INIT_FUNC(MODES_CBC_InitCtxEx, MODES_CBC_UpdateEx, MODES_CBC_FinalEx, MODES_CBC_DeInitCtx) CIPHER_INIT_FUNC(MODES_CCM_InitCtx, MODES_CCM_UpdateEx, MODES_CCM_Final, MODES_CCM_DeInitCtx) CIPHER_INIT_FUNC(MODES_CFB_InitCtxEx, MODES_CFB_UpdateEx, MODES_CFB_Final, MODES_CFB_DeInitCtx) CIPHER_INIT_FUNC(MODES_CTR_InitCtxEx, MODES_CTR_UpdateEx, MODES_CTR_Final, MODES_CTR_DeInitCtx) CIPHER_INIT_FUNC(MODES_ECB_InitCtxEx, MODES_ECB_UpdateEx, MODES_ECB_Final, MODES_ECB_DeinitCtx) CIPHER_INIT_FUNC(MODES_GCM_InitCtxEx, MODES_GCM_UpdateEx, MODES_GCM_Final, MODES_GCM_DeInitCtx) CIPHER_INIT_FUNC(MODES_OFB_InitCtxEx, MODES_OFB_UpdateEx, MODES_OFB_Final, MODES_OFB_DeInitCtx) CIPHER_INIT_FUNC(MODES_XTS_InitCtxEx, MODES_XTS_UpdateEx, MODES_XTS_Final, MODES_XTS_DeInitCtx) CIPHER_INIT_FUNC(MODES_CHACHA20POLY1305_InitCtx, MODES_CHACHA20POLY1305_Update, MODES_CHACHA20POLY1305_Final, MODES_CHACHA20POLY1305_DeInitCtx) const CRYPT_EAL_Func g_isoCbc[] = { #ifdef HITLS_CRYPTO_CBC {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_CBC_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CBC_InitCtxExWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CBC_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CBC_FinalExWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CBC_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CBC_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CBC_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoCcm[] = { #ifdef HITLS_CRYPTO_CCM {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_CCM_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CCM_InitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CCM_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CCM_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CCM_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CCM_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CCM_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoCfb[] = { #ifdef HITLS_CRYPTO_CFB {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_CFB_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CFB_InitCtxExWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CFB_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CFB_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CFB_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CFB_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CFB_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoChaCha[] = { #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_CHACHA20POLY1305_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CHACHA20POLY1305_InitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CHACHA20POLY1305_UpdateWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CHACHA20POLY1305_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CHACHA20POLY1305_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CHACHA20POLY1305_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CHACHA20POLY1305_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoCtr[] = { #ifdef HITLS_CRYPTO_CTR {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_CTR_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CTR_InitCtxExWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CTR_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CTR_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CTR_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CTR_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CTR_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoEcb[] = { #ifdef HITLS_CRYPTO_ECB {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_ECB_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_ECB_InitCtxExWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_ECB_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_ECB_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_ECB_DeinitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_ECB_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_ECB_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoGcm[] = { #ifdef HITLS_CRYPTO_GCM {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_GCM_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_GCM_InitCtxExWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_GCM_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_GCM_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_GCM_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_GCM_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_GCM_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoOfb[] = { #ifdef HITLS_CRYPTO_OFB {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_OFB_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_OFB_InitCtxExWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_OFB_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_OFB_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_OFB_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_OFB_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_OFB_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoXts[] = { #ifdef HITLS_CRYPTO_XTS {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)MODES_XTS_NewCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_XTS_InitCtxExWrapper}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_XTS_UpdateExWrapper}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_XTS_FinalWrapper}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_XTS_DeInitCtxWrapper}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_XTS_CtrlWrapper}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_XTS_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_cipher.c
C
unknown
20,295
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_params_key.h" #include "crypt_cmvp_selftest.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provider.h" #define ISO_19790_PROVIDER_VERSION "openHiTLS ISO 19790 Provider Version : V0.3.0" typedef struct { void *ctx; void *provCtx; void *libCtx; } IsoSelftestCtx; static IsoSelftestCtx *CRYPT_Selftest_NewCtx(CRYPT_EAL_IsoProvCtx *provCtx) { if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } IsoSelftestCtx *ctx = (IsoSelftestCtx *)BSL_SAL_Calloc(1, sizeof(IsoSelftestCtx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } ctx->provCtx = provCtx; ctx->libCtx = provCtx->libCtx; return ctx; } static const char *CRYPT_Selftest_GetVersion(IsoSelftestCtx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_GET_VERSION, 0, 0); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } return ISO_19790_PROVIDER_VERSION; } static int32_t CRYPT_Selftest_Selftest(IsoSelftestCtx *ctx, const BSL_Param *param) { int32_t type = 0; uint32_t len = sizeof(type); if (ctx == NULL || param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } const BSL_Param *temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_CMVP_SELFTEST_TYPE); if (temp == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_CMVP_SELFTEST_TYPE, BSL_PARAM_TYPE_INT32, &type, &len); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } int32_t event = 0; switch (type) { case CRYPT_CMVP_INTEGRITY_TEST: event = CRYPT_EVENT_INTEGRITY_TEST; break; case CRYPT_CMVP_KAT_TEST: event = CRYPT_EVENT_KAT_TEST; break; default: BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } BSL_Param tmpParams[3] = {{0}, {0}, BSL_PARAM_END}; (void)BSL_PARAM_InitValue(&tmpParams[0], CRYPT_PARAM_EVENT, BSL_PARAM_TYPE_INT32, &event, sizeof(event)); (void)BSL_PARAM_InitValue(&tmpParams[1], CRYPT_PARAM_LIB_CTX, BSL_PARAM_TYPE_CTX_PTR, ctx->libCtx, 0); return CRYPT_Iso_EventOperation(ctx->provCtx, tmpParams); } static void CRYPT_Selftest_FreeCtx(IsoSelftestCtx *ctx) { if (ctx == NULL) { return; } BSL_SAL_Free(ctx); } const CRYPT_EAL_Func g_isoSelftest[] = { {CRYPT_EAL_IMPLSELFTEST_NEWCTX, (CRYPT_EAL_ImplSelftestNewCtx)CRYPT_Selftest_NewCtx}, {CRYPT_EAL_IMPLSELFTEST_GETVERSION, (CRYPT_EAL_ImplSelftestGetVersion)CRYPT_Selftest_GetVersion}, {CRYPT_EAL_IMPLSELFTEST_SELFTEST, (CRYPT_EAL_ImplSelftestSelftest)CRYPT_Selftest_Selftest}, {CRYPT_EAL_IMPLSELFTEST_FREECTX, (CRYPT_EAL_ImplSelftestFreeCtx)CRYPT_Selftest_FreeCtx}, CRYPT_EAL_FUNC_END }; #endif // HITLS_CRYPTO_CMVP_ISO19790
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_cmvp.c
C
unknown
3,801
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "crypt_pbkdf2.h" #include "crypt_kdf_tls12.h" #include "crypt_hkdf.h" #include "crypt_scrypt.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_cmvp.h" #include "cmvp_iso19790.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provider.h" typedef struct { int32_t algId; void *ctx; void *provCtx; } IsoKdfCtx; /* Constants for parameter validation */ #define KDF_DEF_MAC_ALGID CRYPT_MAC_HMAC_SHA256 #define KDF_DEF_SALT_LEN 16 #define KDF_DEF_PBKDF2_ITER 1024 #define KDF_DEF_KEY_LEN 16 static int32_t GetMacId(const BSL_Param *param, CRYPT_MAC_AlgId *macId) { int32_t id; uint32_t len = sizeof(id); const BSL_Param *temp = NULL; if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_MAC_ID)) == NULL) { return CRYPT_SUCCESS; } int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_MAC_ID, BSL_PARAM_TYPE_UINT32, &id, &len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *macId = (CRYPT_MAC_AlgId)id; return CRYPT_SUCCESS; } static int32_t GetPbkdf2Params(const BSL_Param *param, CRYPT_EAL_Pbkdf2Param *pbkdf2Param) { uint32_t iter = 0; uint32_t len = 0; const BSL_Param *temp = NULL; if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_SALT)) != NULL) { pbkdf2Param->saltLen = temp->valueLen; } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_ITER)) != NULL) { len = sizeof(iter); int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_ITER, BSL_PARAM_TYPE_UINT32, &iter, &len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } pbkdf2Param->iter = iter; } return GetMacId(param, &pbkdf2Param->macId); } static int32_t GetHkdfAndTlskdfParam(const BSL_Param *param, CRYPT_EAL_HkdfParam *hkdf) { const BSL_Param *temp = NULL; if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_KEY)) != NULL) { hkdf->keyLen = temp->valueLen; } return GetMacId(param, &hkdf->macId); } static int32_t CheckKdfParam(IsoKdfCtx *ctx, const BSL_Param *param) { int32_t ret = CRYPT_SUCCESS; CRYPT_EAL_Pbkdf2Param pbkdf2 = {KDF_DEF_MAC_ALGID, KDF_DEF_SALT_LEN, KDF_DEF_PBKDF2_ITER, KDF_DEF_KEY_LEN}; CRYPT_EAL_HkdfParam hkdf = {KDF_DEF_MAC_ALGID, KDF_DEF_KEY_LEN}; CRYPT_EAL_KdfC2Data data = {&pbkdf2, &hkdf}; switch (ctx->algId) { case CRYPT_KDF_HKDF: case CRYPT_KDF_KDFTLS12: ret = GetHkdfAndTlskdfParam(param, &hkdf); break; case CRYPT_KDF_PBKDF2: ret = GetPbkdf2Params(param, &pbkdf2); break; default: BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ret != CRYPT_SUCCESS) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_KDF, ctx->algId); return ret; } if (!CMVP_Iso19790KdfC2(ctx->algId, &data)) { BSL_ERR_PUSH_ERROR(CRYPT_CMVP_ERR_PARAM_CHECK); (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_KDF, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return ret; } /* Algorithm-specific parameter check functions */ static int32_t SSPLog(IsoKdfCtx *ctx, const BSL_Param *param, const int32_t *sspParam, uint32_t paramCount) { for (uint32_t i = 0; i < paramCount; i++) { if (BSL_PARAM_FindConstParam(param, sspParam[i]) != NULL) { return CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_KDF, ctx->algId); } } return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_SCRYPT static int32_t CheckSCRYPTParamAndLog(IsoKdfCtx *ctx, const BSL_Param *param) { int32_t sspParam[] = {CRYPT_PARAM_KDF_PASSWORD}; return SSPLog(ctx, param, sspParam, sizeof(sspParam)/sizeof(sspParam[0])); } #endif #ifdef HITLS_CRYPTO_KDFTLS12 static int32_t CheckKDFTLS12ParamAndLog(IsoKdfCtx *ctx, const BSL_Param *param) { int32_t ret = CheckKdfParam(ctx, param); if (ret != CRYPT_SUCCESS) { return ret; } int32_t sspParam[] = {CRYPT_PARAM_KDF_KEY, CRYPT_PARAM_KDF_SEED}; return SSPLog(ctx, param, sspParam, sizeof(sspParam)/sizeof(sspParam[0])); } #endif #ifdef HITLS_CRYPTO_HKDF static int32_t CheckHKDFParamAndLog(IsoKdfCtx *ctx, const BSL_Param *param) { int32_t ret = CheckKdfParam(ctx, param); if (ret != CRYPT_SUCCESS) { return ret; } int32_t sspParam[] = { CRYPT_PARAM_KDF_KEY, CRYPT_PARAM_KDF_PRK, CRYPT_PARAM_KDF_INFO }; return SSPLog(ctx, param, sspParam, sizeof(sspParam)/sizeof(sspParam[0])); } #endif static int32_t CheckPBKDF2ParamAndLog(IsoKdfCtx *ctx, const BSL_Param *param) { int32_t ret = CheckKdfParam(ctx, param); if (ret != CRYPT_SUCCESS) { return ret; } int32_t sspParam[] = {CRYPT_PARAM_KDF_PASSWORD}; return SSPLog(ctx, param, sspParam, sizeof(sspParam)/sizeof(sspParam[0])); } static int32_t CheckDeriveKeyLen(IsoKdfCtx *ctx, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->algId != CRYPT_KDF_PBKDF2) { return CRYPT_SUCCESS; } CRYPT_EAL_Pbkdf2Param pbkdf2Param = {KDF_DEF_MAC_ALGID, KDF_DEF_SALT_LEN, KDF_DEF_PBKDF2_ITER, len}; CRYPT_EAL_KdfC2Data data = {&pbkdf2Param, NULL}; if (!CMVP_Iso19790KdfC2(CRYPT_KDF_PBKDF2, &data)) { BSL_ERR_PUSH_ERROR(CRYPT_CMVP_ERR_PARAM_CHECK); (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_KDF, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } #define KDF_METHOD_FUNC(name) \ static void *CRYPT_##name##_NewCtxExWrapper(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId) \ { \ if (provCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return NULL; \ } \ void *kdfCtx = CRYPT_##name##_NewCtxEx(provCtx->libCtx); \ if (kdfCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ IsoKdfCtx *ctx = BSL_SAL_Calloc(1, sizeof(IsoKdfCtx)); \ if (ctx == NULL) { \ CRYPT_##name##_FreeCtx(kdfCtx); \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ ctx->algId = algId; \ ctx->ctx = kdfCtx; \ ctx->provCtx = provCtx; \ return ctx; \ } \ \ static int32_t CRYPT_##name##_SetParamWrapper(IsoKdfCtx *ctx, const BSL_Param *param) \ { \ if (ctx == NULL || param == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = Check##name##ParamAndLog(ctx, param); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_SetParam(ctx->ctx, param); \ } \ \ static int32_t CRYPT_##name##_DeriveWrapper(IsoKdfCtx *ctx, uint8_t *out, uint32_t len) \ { \ int32_t ret = CheckDeriveKeyLen(ctx, len); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_KDF, CRYPT_ALGO_KDF, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Derive(ctx->ctx, out, len); \ } \ \ static int32_t CRYPT_##name##_DeinitWrapper(IsoKdfCtx *ctx) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_KDF, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Deinit(ctx->ctx); \ } \ \ static void CRYPT_##name##_FreeCtxWrapper(IsoKdfCtx *ctx) \ { \ if (ctx == NULL) { \ return; \ } \ (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_KDF, ctx->algId); \ if (ctx->ctx != NULL) { \ CRYPT_##name##_FreeCtx(ctx->ctx); \ } \ BSL_SAL_Free(ctx); \ } #ifdef HITLS_CRYPTO_SCRYPT KDF_METHOD_FUNC(SCRYPT); #endif #ifdef HITLS_CRYPTO_PBKDF2 KDF_METHOD_FUNC(PBKDF2); #endif #ifdef HITLS_CRYPTO_KDFTLS12 KDF_METHOD_FUNC(KDFTLS12); #endif #ifdef HITLS_CRYPTO_HKDF KDF_METHOD_FUNC(HKDF); #endif const CRYPT_EAL_Func g_isoKdfScrypt[] = { #ifdef HITLS_CRYPTO_SCRYPT {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_SCRYPT_NewCtxExWrapper}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_SCRYPT_SetParamWrapper}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_SCRYPT_DeriveWrapper}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_SCRYPT_DeinitWrapper}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_SCRYPT_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKdfPBKdf2[] = { #ifdef HITLS_CRYPTO_PBKDF2 {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_PBKDF2_NewCtxExWrapper}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_PBKDF2_SetParamWrapper}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_PBKDF2_DeriveWrapper}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_PBKDF2_DeinitWrapper}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_PBKDF2_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKdfKdfTLS12[] = { #ifdef HITLS_CRYPTO_KDFTLS12 {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_KDFTLS12_NewCtxExWrapper}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_KDFTLS12_SetParamWrapper}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_KDFTLS12_DeriveWrapper}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_KDFTLS12_DeinitWrapper}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_KDFTLS12_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKdfHkdf[] = { #ifdef HITLS_CRYPTO_HKDF {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_HKDF_NewCtxExWrapper}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_HKDF_SetParamWrapper}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_HKDF_DeriveWrapper}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_HKDF_DeinitWrapper}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_HKDF_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_kdf.c
C
unknown
17,063
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #ifdef HITLS_CRYPTO_MLKEM #include "crypt_mlkem.h" #endif #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provderimpl.h" #ifdef HITLS_CRYPTO_MLKEM static int32_t CRYPT_ML_KEM_EncapsWrapper(const CRYPT_Iso_Pkey_Ctx *ctx, uint8_t *cipher, uint32_t *cipherLen, uint8_t *share, uint32_t *shareLen) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ENCAPS, CRYPT_ALGO_PKEY, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return CRYPT_ML_KEM_Encaps(ctx->ctx, cipher, cipherLen, share, shareLen); } static int32_t CRYPT_ML_KEM_DecapsWrapper(const CRYPT_Iso_Pkey_Ctx *ctx, uint8_t *cipher, uint32_t cipherLen, uint8_t *share, uint32_t *shareLen) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_DECAPS, CRYPT_ALGO_PKEY, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return CRYPT_ML_KEM_Decaps(ctx->ctx, cipher, cipherLen, share, shareLen); } #endif const CRYPT_EAL_Func g_isoMlKem[] = { #ifdef HITLS_CRYPTO_MLKEM {CRYPT_EAL_IMPLPKEYKEM_ENCAPSULATE, (CRYPT_EAL_ImplPkeyKemEncapsulate)CRYPT_ML_KEM_EncapsWrapper}, {CRYPT_EAL_IMPLPKEYKEM_DECAPSULATE, (CRYPT_EAL_ImplPkeyKemDecapsulate)CRYPT_ML_KEM_DecapsWrapper}, #endif CRYPT_EAL_FUNC_END }; #endif // HITLS_CRYPTO_CMVP_ISO19790
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_kem.c
C
unknown
2,186
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "crypt_curve25519.h" #include "crypt_dh.h" #include "crypt_ecdh.h" #include "crypt_sm2.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provderimpl.h" #define KEY_EXCH_FUNC(name) \ static int32_t name##Wrapper(const CRYPT_Iso_Pkey_Ctx *prvKey, const CRYPT_Iso_Pkey_Ctx *pubKey, \ uint8_t *sharedKey, uint32_t *shareKeyLen) \ { \ if (prvKey == NULL || pubKey == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(prvKey->provCtx, CRYPT_EVENT_KEYAGGREMENT, CRYPT_ALGO_PKEY, prvKey->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return (name)(prvKey->ctx, pubKey->ctx, sharedKey, shareKeyLen); \ } #ifdef HITLS_CRYPTO_X25519 KEY_EXCH_FUNC(CRYPT_CURVE25519_ComputeSharedKey) #endif #ifdef HITLS_CRYPTO_DH KEY_EXCH_FUNC(CRYPT_DH_ComputeShareKey) #endif #ifdef HITLS_CRYPTO_ECDH KEY_EXCH_FUNC(CRYPT_ECDH_ComputeShareKey) #endif #ifdef HITLS_CRYPTO_SM2_EXCH KEY_EXCH_FUNC(CRYPT_SM2_KapComputeKey) #endif const CRYPT_EAL_Func g_isoExchX25519[] = { #ifdef HITLS_CRYPTO_X25519 {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_CURVE25519_ComputeSharedKeyWrapper}, #endif CRYPT_EAL_FUNC_END }; const CRYPT_EAL_Func g_isoExchDh[] = { #ifdef HITLS_CRYPTO_DH {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_DH_ComputeShareKeyWrapper}, #endif CRYPT_EAL_FUNC_END }; const CRYPT_EAL_Func g_isoExchEcdh[] = { #ifdef HITLS_CRYPTO_ECDH {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_ECDH_ComputeShareKeyWrapper}, #endif CRYPT_EAL_FUNC_END }; const CRYPT_EAL_Func g_isoExchSm2[] = { #if defined(HITLS_CRYPTO_SM2_EXCH) {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_SM2_KapComputeKeyWrapper}, #endif CRYPT_EAL_FUNC_END }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_keyexch.c
C
unknown
3,481
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #ifdef HITLS_CRYPTO_DSA #include "crypt_dsa.h" #endif #ifdef HITLS_CRYPTO_CURVE25519 #include "crypt_curve25519.h" #endif #ifdef HITLS_CRYPTO_RSA #include "crypt_rsa.h" #endif #ifdef HITLS_CRYPTO_DH #include "crypt_dh.h" #endif #ifdef HITLS_CRYPTO_ECDSA #include "crypt_ecdsa.h" #endif #ifdef HITLS_CRYPTO_ECDH #include "crypt_ecdh.h" #endif #ifdef HITLS_CRYPTO_SM2 #include "crypt_sm2.h" #endif #ifdef HITLS_CRYPTO_SLH_DSA #include "crypt_slh_dsa.h" #endif #ifdef HITLS_CRYPTO_MLKEM #include "crypt_mlkem.h" #endif #ifdef HITLS_CRYPTO_MLDSA #include "crypt_mldsa.h" #endif #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "crypt_iso_provider.h" #include "crypt_eal_pkey.h" #include "crypt_cmvp.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provderimpl.h" #include "cmvp_iso19790.h" #define PKEY_PCT_PARAM_COUNT 4 static int32_t ParaCheckAndLog(const CRYPT_Iso_Pkey_Ctx *ctx, const CRYPT_EAL_PkeyPara *para) { CRYPT_EAL_PkeyC2Data data = {para, NULL, NULL, CRYPT_MD_MAX, CRYPT_PKEY_PARAID_MAX, CRYPT_EVENT_MAX, NULL, NULL, NULL}; if (!CMVP_Iso19790PkeyC2(ctx->algId, &data)) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_PKEY, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } static int32_t GetParamValue(const BSL_Param *params, int32_t paramId, uint8_t **value, uint32_t *valueLen) { const BSL_Param *param = BSL_PARAM_FindConstParam(params, paramId); if (param == NULL || (param->value == NULL && param->valueLen != 0)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *value = param->value; *valueLen = param->valueLen; return CRYPT_SUCCESS; } static int32_t CheckDsaPara(const CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { CRYPT_EAL_PkeyPara para = {0}; int32_t ret = GetParamValue(params, CRYPT_PARAM_DSA_P, &para.para.dsaPara.p, &para.para.dsaPara.pLen); if (ret != CRYPT_SUCCESS) { return ret; } ret = GetParamValue(params, CRYPT_PARAM_DSA_Q, &para.para.dsaPara.q, &para.para.dsaPara.qLen); if (ret != CRYPT_SUCCESS) { return ret; } ret = GetParamValue(params, CRYPT_PARAM_DSA_G, &para.para.dsaPara.g, &para.para.dsaPara.gLen); if (ret != CRYPT_SUCCESS) { return ret; } return ParaCheckAndLog(ctx, &para); } static int32_t GetRsaBits(const BSL_Param *params, uint32_t *bits) { uint32_t bitsLen = sizeof(*bits); const BSL_Param *temp = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_BITS); if (temp == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_BITS, BSL_PARAM_TYPE_UINT32, bits, &bitsLen); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } return CRYPT_SUCCESS; } static int32_t CheckRsaPara(const CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { uint32_t bits = 0; int32_t ret = GetRsaBits(params, &bits); if (ret != CRYPT_SUCCESS) { return ret; } CRYPT_EAL_PkeyPara para = {0}; para.para.rsaPara.bits = bits; return ParaCheckAndLog(ctx, &para); } static int32_t CheckDhPara(const CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { CRYPT_EAL_PkeyPara para = {0}; int32_t ret = GetParamValue(params, CRYPT_PARAM_DH_P, &para.para.dhPara.p, &para.para.dhPara.pLen); if (ret != CRYPT_SUCCESS) { return ret; } ret = GetParamValue(params, CRYPT_PARAM_DH_Q, &para.para.dhPara.q, &para.para.dhPara.qLen); if (ret != CRYPT_SUCCESS) { return ret; } return ParaCheckAndLog(ctx, &para); } static int32_t CheckPkeyParam(const CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { switch (ctx->algId) { case CRYPT_PKEY_DH: return CheckDhPara(ctx, params); case CRYPT_PKEY_DSA: return CheckDsaPara(ctx, params); case CRYPT_PKEY_RSA: return CheckRsaPara(ctx, params); default: return CRYPT_SUCCESS; } } static int32_t CheckSetRsaPrvKey(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { CRYPT_EAL_PkeyPrv prv = {0}; int32_t ret = GetParamValue(params, CRYPT_PARAM_RSA_N, &prv.key.rsaPrv.n, &prv.key.rsaPrv.nLen); if (ret != CRYPT_SUCCESS) { return ret; } ret = GetParamValue(params, CRYPT_PARAM_RSA_D, &prv.key.rsaPrv.d, &prv.key.rsaPrv.dLen); if (ret != CRYPT_SUCCESS) { return ret; } CRYPT_EAL_PkeyC2Data data = {NULL, NULL, &prv, CRYPT_MD_MAX, CRYPT_PKEY_PARAID_MAX, CRYPT_EVENT_MAX, NULL, NULL, NULL}; if (!CMVP_Iso19790PkeyC2(ctx->algId, &data)) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_PKEY, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } static int32_t CheckSetPrvKey(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { if (ctx->algId == CRYPT_PKEY_RSA) { return CheckSetRsaPrvKey(ctx, params); } return CRYPT_SUCCESS; } static int32_t CheckSetRsaPubKey(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { CRYPT_EAL_PkeyPub pub = {0}; int32_t ret = GetParamValue(params, CRYPT_PARAM_RSA_N, &pub.key.rsaPub.n, &pub.key.rsaPub.nLen); if (ret != CRYPT_SUCCESS) { return ret; } CRYPT_EAL_PkeyC2Data data = {NULL, &pub, NULL, CRYPT_MD_MAX, CRYPT_PKEY_PARAID_MAX, CRYPT_EVENT_MAX, NULL, NULL, NULL}; if (!CMVP_Iso19790PkeyC2(ctx->algId, &data)) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_PKEY, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } static int32_t CheckSetPubKey(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) { if (ctx->algId == CRYPT_PKEY_RSA) { return CheckSetRsaPubKey(ctx, params); } return CRYPT_SUCCESS; } static int32_t CheckParaId(CRYPT_Iso_Pkey_Ctx *ctx, int32_t opt, void *val, uint32_t len) { if (opt != CRYPT_CTRL_SET_PARA_BY_ID) { return CRYPT_SUCCESS; } if (val == NULL || len != sizeof(CRYPT_PKEY_ParaId)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } CRYPT_EAL_PkeyC2Data data = {NULL, NULL, NULL, CRYPT_MD_MAX, *(CRYPT_PKEY_ParaId *)val, CRYPT_EVENT_MAX, NULL, NULL, NULL}; if (!CMVP_Iso19790PkeyC2(ctx->algId, &data)) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_PKEY, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } static int32_t CheckRsaPadding(CRYPT_Iso_Pkey_Ctx *ctx, int32_t opt, void *val, uint32_t len) { if (ctx->algId != CRYPT_PKEY_RSA) { return CRYPT_SUCCESS; } int32_t ret = CRYPT_SUCCESS; do { if (opt == CRYPT_CTRL_SET_RSA_RSAES_PKCSV15 || opt == CRYPT_CTRL_SET_RSA_RSAES_PKCSV15_TLS || opt == CRYPT_CTRL_SET_NO_PADDING) { ret = CRYPT_CMVP_ERR_PARAM_CHECK; break; } if (opt == CRYPT_CTRL_SET_RSA_PADDING) { if (val == NULL || len != sizeof(int32_t)) { ret = CRYPT_NULL_INPUT; break; } int32_t padType = *(int32_t *)val; if (padType != CRYPT_EMSA_PKCSV15 && padType != CRYPT_EMSA_PSS && padType != CRYPT_RSAES_OAEP) { ret = CRYPT_CMVP_ERR_PARAM_CHECK; break; } } } while (0); if (ret != CRYPT_SUCCESS) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_PKEY, ctx->algId); BSL_ERR_PUSH_ERROR(ret); } return ret; } static int32_t CheckRsaMdId(CRYPT_Iso_Pkey_Ctx *ctx, int32_t opt, void *val, uint32_t len) { CRYPT_RSA_PkcsV15Para pkcsv15 = {0}; CRYPT_EAL_PkeyC2Data data = {NULL, NULL, NULL, CRYPT_MD_MAX, CRYPT_PKEY_PARAID_MAX, CRYPT_EVENT_MAX, NULL, NULL, NULL}; switch (opt) { case CRYPT_CTRL_SET_RSA_EMSA_PKCSV15: if (val == NULL || len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } pkcsv15.mdId = *(int32_t *)val; data.pkcsv15 = &pkcsv15; break; case CRYPT_CTRL_SET_RSA_EMSA_PSS: data.pss = (BSL_Param *)val; break; case CRYPT_CTRL_SET_RSA_RSAES_OAEP: data.oaep = (BSL_Param *)val; break; default: return CRYPT_SUCCESS; } if (!CMVP_Iso19790PkeyC2(ctx->algId, &data)) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_PKEY, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } static int32_t PkeyCtrlCheck(CRYPT_Iso_Pkey_Ctx *ctx, int32_t opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret = CheckParaId(ctx, opt, val, len); if (ret != CRYPT_SUCCESS) { return ret; } ret = CheckRsaPadding(ctx, opt, val, len); if (ret != CRYPT_SUCCESS) { return ret; } return CheckRsaMdId(ctx, opt, val, len); } static int32_t CRYPT_ASMCAP_PkeyCheck(int32_t algId) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Pkey(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return CRYPT_EAL_ALG_ASM_NOT_SUPPORT; } #else (void)algId; #endif return CRYPT_SUCCESS; } #define PKEY_NEW_Ctx_FUNC(name) \ static void *CRYPT_##name##_NewCtxExWrapper(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId) \ { \ if (CRYPT_ASMCAP_PkeyCheck(algId) != CRYPT_SUCCESS) { \ return NULL; \ } \ if (provCtx == NULL || provCtx->libCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return NULL; \ } \ CRYPT_Iso_Pkey_Ctx *ctx = (CRYPT_Iso_Pkey_Ctx *)BSL_SAL_Calloc(1, sizeof(CRYPT_Iso_Pkey_Ctx)); \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ void *pkeyCtx = CRYPT_##name##_NewCtxEx(provCtx->libCtx); \ if (pkeyCtx == NULL) { \ BSL_SAL_Free(ctx); \ return NULL; \ } \ ctx->algId = algId; \ ctx->ctx = pkeyCtx; \ ctx->provCtx = provCtx; \ return ctx; \ } #define PKEY_SET_PARA_FUNC(name) \ static int32_t CRYPT_##name##_SetParaWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *param) \ { \ if (ctx == NULL || param == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CheckPkeyParam(ctx, param); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_SetParaEx(ctx->ctx, param); \ } #define PKEY_GET_PARA_FUNC(name) \ static int32_t CRYPT_##name##_GetParaWrapper(const CRYPT_Iso_Pkey_Ctx *ctx, BSL_Param *param) \ { \ if (ctx == NULL || param == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CheckPkeyParam(ctx, param); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_GetParaEx(ctx->ctx, param); \ } #define PKEY_GEN_KEY_FUNC(name) \ static int32_t name##Wrapper(CRYPT_Iso_Pkey_Ctx *ctx) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_GEN, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = (name)(ctx->ctx); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PCT_TEST, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ if (!CMVP_Iso19790PkeyPct(ctx)) { \ return CRYPT_CMVP_ERR_PAIRWISETEST; \ } \ return CRYPT_SUCCESS; \ } #define PKEY_SET_KEY_FUNC(setPrvFunc, setPubFunc) \ static int32_t setPrvFunc##Wrapper(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *para) \ { \ if (ctx == NULL || para == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CheckSetPrvKey(ctx, para); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return (setPrvFunc)(ctx->ctx, para); \ } \ \ static int32_t setPubFunc##Wrapper(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *para) \ { \ if (ctx == NULL || para == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CheckSetPubKey(ctx, para); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return (setPubFunc)(ctx->ctx, para); \ } #define PKEY_GET_KEY_FUNC(getPrvFunc, getPubFunc) \ static int32_t getPrvFunc##Wrapper(const CRYPT_Iso_Pkey_Ctx *ctx, BSL_Param *para) \ { \ if (ctx == NULL || para == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_GETSSP, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return (getPrvFunc)(ctx->ctx, para); \ } \ \ static int32_t getPubFunc##Wrapper(const CRYPT_Iso_Pkey_Ctx *ctx, BSL_Param *para) \ { \ if (ctx == NULL || para == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_GETSSP, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return (getPubFunc)(ctx->ctx, para); \ } #define PKEY_DUP_CTX_FUNC(name) \ static CRYPT_Iso_Pkey_Ctx *CRYPT_##name##_DupCtxWrapper(CRYPT_Iso_Pkey_Ctx *ctx) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return NULL; \ } \ CRYPT_Iso_Pkey_Ctx *newCtx = (CRYPT_Iso_Pkey_Ctx *)BSL_SAL_Calloc(1, sizeof(CRYPT_Iso_Pkey_Ctx)); \ if (newCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ void *pkeyCtx = CRYPT_##name##_DupCtx(ctx->ctx); \ if (pkeyCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ BSL_SAL_Free(newCtx); \ return NULL; \ } \ newCtx->algId = ctx->algId; \ newCtx->ctx = pkeyCtx; \ newCtx->provCtx = ctx->provCtx; \ return newCtx; \ } #define PKEY_CMP_FUNC(name) \ static int32_t CRYPT_##name##_CmpWrapper(const CRYPT_Iso_Pkey_Ctx *a, const CRYPT_Iso_Pkey_Ctx *b) \ { \ if (a == NULL || b == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Cmp(a->ctx, b->ctx); \ } #define PKEY_CTRL_FUNC(name) \ static int32_t CRYPT_##name##_CtrlWrapper(CRYPT_Iso_Pkey_Ctx *ctx, int32_t opt, void *val, uint32_t len) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = PkeyCtrlCheck(ctx, opt, val, len); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Ctrl(ctx->ctx, opt, val, len); \ } #define PKEY_FREE_CTX_FUNC(name) \ static void CRYPT_##name##_FreeCtxWrapper(CRYPT_Iso_Pkey_Ctx *ctx) \ { \ if (ctx == NULL) { \ return; \ } \ (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_PKEY, ctx->algId); \ if (ctx->algId == CRYPT_PKEY_SLH_DSA || ctx->algId == CRYPT_PKEY_ML_DSA || \ ctx->algId == CRYPT_PKEY_ML_KEM) { \ CRYPT_##name##_Ctrl(ctx->ctx, CRYPT_CTRL_CLEAN_PUB_KEY, NULL, 0); \ } \ if (ctx->ctx != NULL) { \ CRYPT_##name##_FreeCtx(ctx->ctx); \ } \ BSL_SAL_Free(ctx); \ } #define PKEY_IMPORT_EXPORT_FUNC(name) \ static int32_t CRYPT_##name##_ImportWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const BSL_Param *params) \ { \ if (ctx == NULL || params == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Import(ctx->ctx, params); \ } \ \ static int32_t CRYPT_##name##_ExportWrapper(const CRYPT_Iso_Pkey_Ctx *ctx, BSL_Param *params) \ { \ if (ctx == NULL || params == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_GETSSP, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Export(ctx->ctx, params); \ } #define PKEY_CHECK_FUNC(name) \ static int32_t CRYPT_##name##_CheckWrapper(uint32_t checkType, const CRYPT_Iso_Pkey_Ctx *ctx1, \ const CRYPT_Iso_Pkey_Ctx *ctx2) \ { \ return CRYPT_##name##_Check(checkType, ctx1 != NULL ? ctx1->ctx : NULL, \ ctx2 != NULL ? ctx2->ctx : NULL); \ } PKEY_NEW_Ctx_FUNC(DSA) PKEY_NEW_Ctx_FUNC(ED25519) PKEY_NEW_Ctx_FUNC(X25519) PKEY_NEW_Ctx_FUNC(RSA) PKEY_NEW_Ctx_FUNC(DH) PKEY_NEW_Ctx_FUNC(ECDSA) PKEY_NEW_Ctx_FUNC(ECDH) PKEY_NEW_Ctx_FUNC(SM2) PKEY_NEW_Ctx_FUNC(SLH_DSA) PKEY_NEW_Ctx_FUNC(ML_KEM) PKEY_NEW_Ctx_FUNC(ML_DSA) PKEY_SET_PARA_FUNC(DSA) PKEY_SET_PARA_FUNC(RSA) PKEY_SET_PARA_FUNC(DH) PKEY_SET_PARA_FUNC(ECDSA) PKEY_SET_PARA_FUNC(ECDH) PKEY_GET_PARA_FUNC(DSA) PKEY_GET_PARA_FUNC(DH) PKEY_GET_PARA_FUNC(ECDSA) PKEY_GET_PARA_FUNC(ECDH) PKEY_GEN_KEY_FUNC(CRYPT_DSA_Gen) PKEY_GEN_KEY_FUNC(CRYPT_ED25519_GenKey) PKEY_GEN_KEY_FUNC(CRYPT_X25519_GenKey) PKEY_GEN_KEY_FUNC(CRYPT_RSA_Gen) PKEY_GEN_KEY_FUNC(CRYPT_DH_Gen) PKEY_GEN_KEY_FUNC(CRYPT_ECDSA_Gen) PKEY_GEN_KEY_FUNC(CRYPT_ECDH_Gen) PKEY_GEN_KEY_FUNC(CRYPT_SM2_Gen) PKEY_GEN_KEY_FUNC(CRYPT_SLH_DSA_Gen) PKEY_GEN_KEY_FUNC(CRYPT_ML_KEM_GenKey) PKEY_GEN_KEY_FUNC(CRYPT_ML_DSA_GenKey) PKEY_SET_KEY_FUNC(CRYPT_DSA_SetPrvKeyEx, CRYPT_DSA_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_CURVE25519_SetPrvKeyEx, CRYPT_CURVE25519_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_RSA_SetPrvKeyEx, CRYPT_RSA_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_DH_SetPrvKeyEx, CRYPT_DH_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_ECDSA_SetPrvKeyEx, CRYPT_ECDSA_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_ECDH_SetPrvKeyEx, CRYPT_ECDH_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_SM2_SetPrvKeyEx, CRYPT_SM2_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_ML_KEM_SetDecapsKeyEx, CRYPT_ML_KEM_SetEncapsKeyEx) PKEY_SET_KEY_FUNC(CRYPT_ML_DSA_SetPrvKeyEx, CRYPT_ML_DSA_SetPubKeyEx) PKEY_SET_KEY_FUNC(CRYPT_SLH_DSA_SetPrvKeyEx, CRYPT_SLH_DSA_SetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_DSA_GetPrvKeyEx, CRYPT_DSA_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_CURVE25519_GetPrvKeyEx, CRYPT_CURVE25519_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_RSA_GetPrvKeyEx, CRYPT_RSA_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_DH_GetPrvKeyEx, CRYPT_DH_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_ECDSA_GetPrvKeyEx, CRYPT_ECDSA_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_ECDH_GetPrvKeyEx, CRYPT_ECDH_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_SM2_GetPrvKeyEx, CRYPT_SM2_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_ML_KEM_GetDecapsKeyEx, CRYPT_ML_KEM_GetEncapsKeyEx) PKEY_GET_KEY_FUNC(CRYPT_ML_DSA_GetPrvKeyEx, CRYPT_ML_DSA_GetPubKeyEx) PKEY_GET_KEY_FUNC(CRYPT_SLH_DSA_GetPrvKeyEx, CRYPT_SLH_DSA_GetPubKeyEx) PKEY_DUP_CTX_FUNC(DSA) PKEY_DUP_CTX_FUNC(CURVE25519) PKEY_DUP_CTX_FUNC(RSA) PKEY_DUP_CTX_FUNC(DH) PKEY_DUP_CTX_FUNC(ECDSA) PKEY_DUP_CTX_FUNC(ECDH) PKEY_DUP_CTX_FUNC(SM2) PKEY_DUP_CTX_FUNC(ML_KEM) PKEY_DUP_CTX_FUNC(ML_DSA) PKEY_CMP_FUNC(DSA) PKEY_CMP_FUNC(CURVE25519) PKEY_CMP_FUNC(RSA) PKEY_CMP_FUNC(DH) PKEY_CMP_FUNC(ECDSA) PKEY_CMP_FUNC(ECDH) PKEY_CMP_FUNC(SM2) PKEY_CMP_FUNC(ML_KEM) PKEY_CMP_FUNC(ML_DSA) PKEY_CTRL_FUNC(DSA) PKEY_CTRL_FUNC(CURVE25519) PKEY_CTRL_FUNC(RSA) PKEY_CTRL_FUNC(DH) PKEY_CTRL_FUNC(ECDSA) PKEY_CTRL_FUNC(ECDH) PKEY_CTRL_FUNC(SM2) PKEY_CTRL_FUNC(ML_KEM) PKEY_CTRL_FUNC(ML_DSA) PKEY_CTRL_FUNC(SLH_DSA) PKEY_FREE_CTX_FUNC(DSA) PKEY_FREE_CTX_FUNC(CURVE25519) PKEY_FREE_CTX_FUNC(RSA) PKEY_FREE_CTX_FUNC(DH) PKEY_FREE_CTX_FUNC(ECDSA) PKEY_FREE_CTX_FUNC(ECDH) PKEY_FREE_CTX_FUNC(SM2) PKEY_FREE_CTX_FUNC(ML_KEM) PKEY_FREE_CTX_FUNC(ML_DSA) PKEY_FREE_CTX_FUNC(SLH_DSA) PKEY_IMPORT_EXPORT_FUNC(CURVE25519) PKEY_IMPORT_EXPORT_FUNC(RSA) PKEY_IMPORT_EXPORT_FUNC(ECDSA) PKEY_IMPORT_EXPORT_FUNC(SM2) PKEY_CHECK_FUNC(DSA) PKEY_CHECK_FUNC(ED25519) PKEY_CHECK_FUNC(X25519) PKEY_CHECK_FUNC(RSA) PKEY_CHECK_FUNC(DH) PKEY_CHECK_FUNC(ECDSA) PKEY_CHECK_FUNC(ECDH) PKEY_CHECK_FUNC(SM2) PKEY_CHECK_FUNC(ML_KEM) PKEY_CHECK_FUNC(ML_DSA) PKEY_CHECK_FUNC(SLH_DSA) const CRYPT_EAL_Func g_isoKeyMgmtDsa[] = { #ifdef HITLS_CRYPTO_DSA {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_DSA_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_DSA_SetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_DSA_GetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_DSA_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_DSA_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_DSA_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_DSA_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_DSA_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_DSA_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_DSA_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_DSA_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_DSA_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_DSA_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtEd25519[] = { #ifdef HITLS_CRYPTO_ED25519 {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_ED25519_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ED25519_GenKeyWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_CURVE25519_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_CURVE25519_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_CURVE25519_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_CURVE25519_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_CURVE25519_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ED25519_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_CURVE25519_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_CURVE25519_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_CURVE25519_FreeCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_CURVE25519_ImportWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_CURVE25519_ExportWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtX25519[] = { #ifdef HITLS_CRYPTO_X25519 {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_X25519_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_X25519_GenKeyWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_CURVE25519_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_CURVE25519_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_CURVE25519_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_CURVE25519_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_CURVE25519_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_X25519_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_CURVE25519_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_CURVE25519_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_CURVE25519_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtRsa[] = { #ifdef HITLS_CRYPTO_RSA {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_RSA_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_RSA_SetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_RSA_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_RSA_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_RSA_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_RSA_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_RSA_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_RSA_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_RSA_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_RSA_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_RSA_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_RSA_FreeCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_RSA_ImportWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_RSA_ExportWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtDh[] = { #ifdef HITLS_CRYPTO_DH {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_DH_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_DH_SetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_DH_GetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_DH_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_DH_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_DH_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_DH_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_DH_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_DH_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_DH_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_DH_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_DH_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_DH_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtEcdsa[] = { #ifdef HITLS_CRYPTO_ECDSA {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_ECDSA_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_ECDSA_SetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_ECDSA_GetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ECDSA_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ECDSA_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ECDSA_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ECDSA_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ECDSA_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ECDSA_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ECDSA_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ECDSA_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ECDSA_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ECDSA_FreeCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_ECDSA_ImportWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_ECDSA_ExportWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtEcdh[] = { #ifdef HITLS_CRYPTO_ECDH {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_ECDH_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_ECDH_SetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_ECDH_GetParaWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ECDH_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ECDH_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ECDH_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ECDH_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ECDH_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ECDH_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ECDH_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ECDH_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ECDH_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ECDH_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtSm2[] = { #ifdef HITLS_CRYPTO_SM2 {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_SM2_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_SM2_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_SM2_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_SM2_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_SM2_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_SM2_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_SM2_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_SM2_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_SM2_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_SM2_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_SM2_FreeCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_SM2_ImportWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_SM2_ExportWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtMlKem[] = { #ifdef HITLS_CRYPTO_MLKEM {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_ML_KEM_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ML_KEM_GenKeyWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ML_KEM_SetDecapsKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ML_KEM_SetEncapsKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ML_KEM_GetDecapsKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ML_KEM_GetEncapsKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ML_KEM_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ML_KEM_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ML_KEM_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ML_KEM_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ML_KEM_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtMlDsa[] = { #ifdef HITLS_CRYPTO_MLDSA {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_ML_DSA_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ML_DSA_GenKeyWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ML_DSA_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ML_DSA_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ML_DSA_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ML_DSA_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ML_DSA_DupCtxWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ML_DSA_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ML_DSA_CmpWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ML_DSA_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ML_DSA_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoKeyMgmtSlhDsa[] = { #ifdef HITLS_CRYPTO_SLH_DSA {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_SLH_DSA_NewCtxExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_SLH_DSA_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_SLH_DSA_SetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_SLH_DSA_SetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_SLH_DSA_GetPrvKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_SLH_DSA_GetPubKeyExWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_SLH_DSA_CheckWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_SLH_DSA_CtrlWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_SLH_DSA_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_keymgmt.c
C
unknown
52,129
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "crypt_hmac.h" #include "crypt_cmac.h" #include "crypt_cbc_mac.h" #include "crypt_gmac.h" #include "crypt_siphash.h" #include "crypt_ealinit.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "cmvp_iso19790.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provider.h" #define MAC_KEY_LEN_MIN 14 typedef struct { int32_t algId; void *ctx; void *provCtx; } IsoMacCtx; static int32_t CRYPT_ASMCAP_MacCheck(int32_t algId) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Mac(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return CRYPT_EAL_ALG_ASM_NOT_SUPPORT; } #else (void)algId; #endif return CRYPT_SUCCESS; } static int32_t CheckMacKeyLen(IsoMacCtx *ctx, uint32_t keyLen) { if (!CMVP_Iso19790MacC2(ctx->algId, keyLen)) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_MAC, ctx->algId); BSL_ERR_PUSH_ERROR(CRYPT_CMVP_ERR_PARAM_CHECK); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } #define MAC_NewCtx_FUNC(name) \ static void *CRYPT_##name##_NewCtxExWrapper(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId) \ { \ if (CRYPT_ASMCAP_MacCheck(algId) != CRYPT_SUCCESS) { \ return NULL; \ } \ if (provCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return NULL; \ } \ void *macCtx = CRYPT_##name##_NewCtxEx(provCtx->libCtx, algId); \ if (macCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ IsoMacCtx *ctx = BSL_SAL_Calloc(1, sizeof(IsoMacCtx)); \ if (ctx == NULL) { \ CRYPT_##name##_FreeCtx(macCtx); \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ ctx->algId = algId; \ ctx->ctx = macCtx; \ ctx->provCtx = provCtx; \ return ctx; \ } #define MAC_INIT_FUNC(name) \ static int32_t CRYPT_##name##_InitWrapper(IsoMacCtx *ctx, const uint8_t *key, uint32_t len, void *param) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_MAC, CRYPT_ALGO_MAC, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CheckMacKeyLen(ctx, len); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_MAC, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Init(ctx->ctx, key, len, param); \ } #define MAC_Update_FUNC(name) \ static int32_t CRYPT_##name##_UpdateWrapper(IsoMacCtx *ctx, const uint8_t *in, uint32_t len) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Update(ctx->ctx, in, len); \ } #define MAC_Final_FUNC(name) \ static int32_t CRYPT_##name##_FinalWrapper(IsoMacCtx *ctx, uint8_t *out, uint32_t *len) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Final(ctx->ctx, out, len); \ } #define MAC_Ctrl_FUNC(name) \ static int32_t CRYPT_##name##_CtrlWrapper(IsoMacCtx *ctx, CRYPT_MacCtrl opt, void *val, uint32_t len) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Ctrl(ctx->ctx, opt, val, len); \ } #define MAC_FreeCtx_FUNC(name) \ static void CRYPT_##name##_FreeCtxWrapper(IsoMacCtx *ctx) \ { \ if (ctx == NULL) { \ return; \ } \ (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_MAC, ctx->algId); \ if (ctx->ctx != NULL) { \ CRYPT_##name##_FreeCtx(ctx->ctx); \ } \ BSL_SAL_Free(ctx); \ } #define MAC_DEINIT_FUNC(name) \ static int32_t CRYPT_##name##_DeinitWrapper(IsoMacCtx *ctx) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_MAC, ctx->algId); \ return CRYPT_##name##_Deinit(ctx->ctx); \ } #define MAC_REINIT_FUNC(name) \ static int32_t CRYPT_##name##_ReinitWrapper(IsoMacCtx *ctx) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Reinit(ctx->ctx); \ } #define MAC_SET_PARAM_FUNC(name) \ static int32_t CRYPT_##name##_SetParamWrapper(IsoMacCtx *ctx, const BSL_Param *param) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_SetParam(ctx->ctx, param); \ } #define MAC_FUNCS(name) \ MAC_NewCtx_FUNC(name) \ MAC_INIT_FUNC(name) \ MAC_Update_FUNC(name) \ MAC_Final_FUNC(name) \ MAC_DEINIT_FUNC(name) \ MAC_Ctrl_FUNC(name) \ MAC_FreeCtx_FUNC(name) #ifdef HITLS_CRYPTO_HMAC MAC_FUNCS(HMAC) MAC_REINIT_FUNC(HMAC) MAC_SET_PARAM_FUNC(HMAC) #endif #ifdef HITLS_CRYPTO_CMAC MAC_FUNCS(CMAC) MAC_REINIT_FUNC(CMAC) #endif #ifdef HITLS_CRYPTO_GMAC MAC_FUNCS(GMAC) #endif const CRYPT_EAL_Func g_isoMacHmac[] = { #ifdef HITLS_CRYPTO_HMAC {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_HMAC_NewCtxExWrapper}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_HMAC_InitWrapper}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_HMAC_UpdateWrapper}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_HMAC_FinalWrapper}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_HMAC_DeinitWrapper}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_HMAC_ReinitWrapper}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_HMAC_CtrlWrapper}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_HMAC_FreeCtxWrapper}, {CRYPT_EAL_IMPLMAC_SETPARAM, (CRYPT_EAL_ImplMacSetParam)CRYPT_HMAC_SetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMacCmac[] = { #ifdef HITLS_CRYPTO_CMAC {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_CMAC_NewCtxExWrapper}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_CMAC_InitWrapper}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_CMAC_UpdateWrapper}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_CMAC_FinalWrapper}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_CMAC_DeinitWrapper}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_CMAC_ReinitWrapper}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_CMAC_CtrlWrapper}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_CMAC_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMacGmac[] = { #ifdef HITLS_CRYPTO_GMAC {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_GMAC_NewCtxExWrapper}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_GMAC_InitWrapper}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_GMAC_UpdateWrapper}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_GMAC_FinalWrapper}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_GMAC_DeinitWrapper}, {CRYPT_EAL_IMPLMAC_REINITCTX, NULL}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_GMAC_CtrlWrapper}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_GMAC_FreeCtxWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_mac.c
C
unknown
16,515
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "crypt_md5.h" #include "crypt_sha1.h" #include "crypt_sha2.h" #include "crypt_sha3.h" #include "crypt_sm3.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provider.h" typedef struct { int32_t algId; void *ctx; void *provCtx; } IsoMdCtx; static uint32_t CRYPT_ASMCAP_Test(int32_t algId) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Md(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return CRYPT_EAL_ALG_ASM_NOT_SUPPORT; } #else (void)algId; #endif return CRYPT_SUCCESS; } #define MD_METHOD_FUNC(name) \ static IsoMdCtx *CRYPT_##name##_NewCtxExWrapper(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId) \ { \ int32_t ret = CRYPT_ASMCAP_Test(algId); \ if (ret != CRYPT_SUCCESS) { \ return NULL; \ } \ if (provCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return NULL; \ } \ void *mdCtx = CRYPT_##name##_NewCtxEx(provCtx->libCtx, algId); \ if (mdCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ IsoMdCtx *ctx = BSL_SAL_Calloc(1, sizeof(IsoMdCtx)); \ if (ctx == NULL) { \ CRYPT_##name##_FreeCtx(mdCtx); \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ ctx->algId = algId; \ ctx->ctx = mdCtx; \ ctx->provCtx = provCtx; \ return ctx; \ } \ \ static int32_t CRYPT_##name##_InitWrapper(IsoMdCtx *ctx, BSL_Param *param) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_MD, CRYPT_ALGO_MD, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Init(ctx->ctx, param); \ } \ \ static int32_t CRYPT_##name##_UpdateWrapper(IsoMdCtx *ctx, const uint8_t *data, uint32_t nbytes) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Update(ctx->ctx, data, nbytes); \ } \ \ static int32_t CRYPT_##name##_FinalWrapper(IsoMdCtx *ctx, uint8_t *digest, uint32_t *len) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Final(ctx->ctx, digest, len); \ } \ \ static int32_t CRYPT_##name##_DeinitWrapper(IsoMdCtx *ctx) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Deinit(ctx->ctx); \ } \ \ static IsoMdCtx *CRYPT_##name##_DupCtxWrapper(const IsoMdCtx *src) \ { \ if (src == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return NULL; \ } \ void *mdCtx = CRYPT_##name##_DupCtx(src->ctx); \ if (mdCtx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ IsoMdCtx *dupCtx = BSL_SAL_Calloc(1, sizeof(IsoMdCtx)); \ if (dupCtx == NULL) { \ CRYPT_##name##_FreeCtx(mdCtx); \ BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); \ return NULL; \ } \ dupCtx->algId = src->algId; \ dupCtx->ctx = mdCtx; \ dupCtx->provCtx = src->provCtx; \ return dupCtx; \ } \ \ static void CRYPT_##name##_FreeCtxWrapper(IsoMdCtx *ctx) \ { \ if (ctx == NULL) { \ return; \ } \ if (ctx->ctx != NULL) { \ CRYPT_##name##_FreeCtx(ctx->ctx); \ } \ BSL_SAL_Free(ctx); \ } \ \ static int32_t CRYPT_##name##_GetParamWrapper(IsoMdCtx *ctx, BSL_Param *param) \ { \ (void)ctx; \ return CRYPT_##name##_GetParam(NULL, param); \ } \ \ static int32_t CRYPT_##name##_CopyCtxWrapper(IsoMdCtx *dst, const IsoMdCtx *src) \ { \ if (dst == NULL || src == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ dst->algId = src->algId; \ dst->provCtx = src->provCtx; \ return CRYPT_##name##_CopyCtx(dst->ctx, src->ctx); \ } MD_METHOD_FUNC(SHA1) MD_METHOD_FUNC(SHA2_224) MD_METHOD_FUNC(SHA2_256) MD_METHOD_FUNC(SHA2_384) MD_METHOD_FUNC(SHA2_512) MD_METHOD_FUNC(SHA3_224) MD_METHOD_FUNC(SHA3_256) MD_METHOD_FUNC(SHA3_384) MD_METHOD_FUNC(SHA3_512) MD_METHOD_FUNC(SHAKE128) MD_METHOD_FUNC(SHAKE256) MD_METHOD_FUNC(SM3) #define MD_SQUEEZE_FUNC(name) \ static int32_t CRYPT_##name##_SqueezeWrapper(IsoMdCtx *ctx, uint8_t *out, uint32_t len) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ return CRYPT_##name##_Squeeze(ctx->ctx, out, len); \ } MD_SQUEEZE_FUNC(SHAKE128) MD_SQUEEZE_FUNC(SHAKE256) const CRYPT_EAL_Func g_isoMdSha1[] = { #ifdef HITLS_CRYPTO_SHA1 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA1_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA1_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA1_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA1_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA1_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA1_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA1_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA1_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA1_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha224[] = { #ifdef HITLS_CRYPTO_SHA224 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA2_224_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_224_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_224_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_224_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_224_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_224_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_224_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_224_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_224_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha256[] = { #ifdef HITLS_CRYPTO_SHA256 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA2_256_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_256_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_256_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_256_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_256_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_256_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_256_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_256_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_256_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha384[] = { #ifdef HITLS_CRYPTO_SHA384 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA2_384_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_384_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_384_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_384_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_384_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_384_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_384_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_384_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_384_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha512[] = { #ifdef HITLS_CRYPTO_SHA512 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA2_512_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_512_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_512_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_512_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_512_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_512_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_512_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_512_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_512_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha3224[] = { #ifdef HITLS_CRYPTO_SHA3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA3_224_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_224_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_224_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_224_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_224_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_224_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_224_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_224_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_224_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha3256[] = { #ifdef HITLS_CRYPTO_SHA3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA3_256_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_256_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_256_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_256_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_256_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_256_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_256_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_256_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_256_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha3384[] = { #ifdef HITLS_CRYPTO_SHA3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA3_384_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_384_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_384_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_384_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_384_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_384_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_384_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_384_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_384_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSha3512[] = { #ifdef HITLS_CRYPTO_SHA3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHA3_512_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_512_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_512_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_512_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_512_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_512_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_512_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_512_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_512_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdShake128[] = { #ifdef HITLS_CRYPTO_SHA3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHAKE128_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHAKE128_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHAKE128_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHAKE128_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHAKE128_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHAKE128_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHAKE128_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_SQUEEZE, (CRYPT_EAL_ImplMdSqueeze)CRYPT_SHAKE128_SqueezeWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHAKE128_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHAKE128_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdShake256[] = { #ifdef HITLS_CRYPTO_SHA3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SHAKE256_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHAKE256_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHAKE256_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHAKE256_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHAKE256_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHAKE256_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHAKE256_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_SQUEEZE, (CRYPT_EAL_ImplMdSqueeze)CRYPT_SHAKE256_SqueezeWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHAKE256_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHAKE256_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoMdSm3[] = { #ifdef HITLS_CRYPTO_SM3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_SM3_NewCtxExWrapper}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SM3_InitWrapper}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SM3_UpdateWrapper}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SM3_FinalWrapper}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SM3_DeinitWrapper}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SM3_DupCtxWrapper}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SM3_FreeCtxWrapper}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SM3_CopyCtxWrapper}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SM3_GetParamWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_md.c
C
unknown
25,596
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "crypt_rsa.h" #include "crypt_sm2.h" #include "crypt_paillier.h" #include "crypt_elgamal.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provderimpl.h" #define PKEY_CIPHER_FUNC(name) \ static int32_t CRYPT_##name##_EncryptWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const uint8_t *data, uint32_t dataLen, \ uint8_t *out, uint32_t *outLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ENC, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Encrypt(ctx->ctx, data, dataLen, out, outLen); \ } \ \ static int32_t CRYPT_##name##_DecryptWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const uint8_t *data, uint32_t dataLen, \ uint8_t *out, uint32_t *outLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_DEC, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Decrypt(ctx->ctx, data, dataLen, out, outLen); \ } PKEY_CIPHER_FUNC(RSA) PKEY_CIPHER_FUNC(SM2) const CRYPT_EAL_Func g_isoAsymCipherRsa[] = { #ifdef HITLS_CRYPTO_RSA_ENCRYPT {CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT, (CRYPT_EAL_ImplPkeyEncrypt)CRYPT_RSA_EncryptWrapper}, #endif #ifdef HITLS_CRYPTO_RSA_DECRYPT {CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT, (CRYPT_EAL_ImplPkeyDecrypt)CRYPT_RSA_DecryptWrapper}, #endif CRYPT_EAL_FUNC_END }; const CRYPT_EAL_Func g_isoAsymCipherSm2[] = { #ifdef HITLS_CRYPTO_SM2_CRYPT {CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT, (CRYPT_EAL_ImplPkeyEncrypt)CRYPT_SM2_EncryptWrapper}, {CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT, (CRYPT_EAL_ImplPkeyDecrypt)CRYPT_SM2_DecryptWrapper}, #endif CRYPT_EAL_FUNC_END }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_pkeycipher.c
C
unknown
4,774
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief iso19790 provider impl */ #ifndef CRYPT_EAL_ISO_PROVIDERIMPL_H #define CRYPT_EAL_ISO_PROVIDERIMPL_H #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ typedef struct { int32_t algId; void *ctx; void *provCtx; } CRYPT_Iso_Pkey_Ctx; extern const CRYPT_EAL_Func g_isoMdSha1[]; extern const CRYPT_EAL_Func g_isoMdSha224[]; extern const CRYPT_EAL_Func g_isoMdSha256[]; extern const CRYPT_EAL_Func g_isoMdSha384[]; extern const CRYPT_EAL_Func g_isoMdSha512[]; extern const CRYPT_EAL_Func g_isoMdSha3224[]; extern const CRYPT_EAL_Func g_isoMdSha3256[]; extern const CRYPT_EAL_Func g_isoMdSha3384[]; extern const CRYPT_EAL_Func g_isoMdSha3512[]; extern const CRYPT_EAL_Func g_isoMdShake512[]; extern const CRYPT_EAL_Func g_isoMdShake128[]; extern const CRYPT_EAL_Func g_isoMdShake256[]; extern const CRYPT_EAL_Func g_isoMdSm3[]; extern const CRYPT_EAL_Func g_isoKdfScrypt[]; extern const CRYPT_EAL_Func g_isoKdfPBKdf2[]; extern const CRYPT_EAL_Func g_isoKdfKdfTLS12[]; extern const CRYPT_EAL_Func g_isoKdfHkdf[]; extern const CRYPT_EAL_Func g_isoKeyMgmtDsa[]; extern const CRYPT_EAL_Func g_isoKeyMgmtEd25519[]; extern const CRYPT_EAL_Func g_isoKeyMgmtX25519[]; extern const CRYPT_EAL_Func g_isoKeyMgmtRsa[]; extern const CRYPT_EAL_Func g_isoKeyMgmtDh[]; extern const CRYPT_EAL_Func g_isoKeyMgmtEcdsa[]; extern const CRYPT_EAL_Func g_isoKeyMgmtEcdh[]; extern const CRYPT_EAL_Func g_isoKeyMgmtSm2[]; extern const CRYPT_EAL_Func g_isoKeyMgmtSlhDsa[]; extern const CRYPT_EAL_Func g_isoKeyMgmtMlKem[]; extern const CRYPT_EAL_Func g_isoKeyMgmtMlDsa[]; extern const CRYPT_EAL_Func g_isoExchX25519[]; extern const CRYPT_EAL_Func g_isoExchDh[]; extern const CRYPT_EAL_Func g_isoExchEcdh[]; extern const CRYPT_EAL_Func g_isoExchSm2[]; extern const CRYPT_EAL_Func g_isoAsymCipherRsa[]; extern const CRYPT_EAL_Func g_isoAsymCipherSm2[]; extern const CRYPT_EAL_Func g_isoSignDsa[]; extern const CRYPT_EAL_Func g_isoSignEd25519[]; extern const CRYPT_EAL_Func g_isoSignRsa[]; extern const CRYPT_EAL_Func g_isoSignEcdsa[]; extern const CRYPT_EAL_Func g_isoSignSm2[]; extern const CRYPT_EAL_Func g_isoSignMlDsa[]; extern const CRYPT_EAL_Func g_isoMacHmac[]; extern const CRYPT_EAL_Func g_isoSignSlhDsa[]; extern const CRYPT_EAL_Func g_isoMacCmac[]; extern const CRYPT_EAL_Func g_isoMacGmac[]; extern const CRYPT_EAL_Func g_isoRand[]; extern const CRYPT_EAL_Func g_isoCbc[]; extern const CRYPT_EAL_Func g_isoCcm[]; extern const CRYPT_EAL_Func g_isoCfb[]; extern const CRYPT_EAL_Func g_isoChaCha[]; extern const CRYPT_EAL_Func g_isoCtr[]; extern const CRYPT_EAL_Func g_isoEcb[]; extern const CRYPT_EAL_Func g_isoGcm[]; extern const CRYPT_EAL_Func g_isoOfb[]; extern const CRYPT_EAL_Func g_isoXts[]; extern const CRYPT_EAL_Func g_isoMlKem[]; extern const CRYPT_EAL_Func g_isoSelftest[]; #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_ISO19790 */ #endif /* CRYPT_EAL_ISO_PROVIDERIMPL_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_provderimpl.h
C
unknown
3,600
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include <stdint.h> #include <string.h> #include "bsl_sal.h" #include "bsl_obj.h" #include "bsl_errno.h" #include "bsl_params.h" #include "bsl_err_internal.h" #include "crypt_algid.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "cmvp_iso19790.h" #include "crypt_eal_entropy.h" #include "crypt_eal_implprovider.h" #include "crypt_eal_provider.h" #include "crypt_iso_provderimpl.h" #include "crypt_iso_provider.h" #include "crypt_iso_selftest.h" #include "crypt_params_key.h" #include "crypt_params_key.h" #include "hitls_crypt_type.h" #include "hitls_cert_type.h" #include "crypt_eal_rand.h" #include "hitls_type.h" #define CRYPT_ENTROPY_SOURCE_ENTROPY 8 #define CRYPT_ENTROPY_SEED_POOL_SIZE 4096 static const CRYPT_EAL_AlgInfo g_isoMds[] = { {CRYPT_MD_SHA1, g_isoMdSha1, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA224, g_isoMdSha224, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA256, g_isoMdSha256, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA384, g_isoMdSha384, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA512, g_isoMdSha512, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA3_224, g_isoMdSha3224, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA3_256, g_isoMdSha3256, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA3_384, g_isoMdSha3384, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHA3_512, g_isoMdSha3512, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHAKE128, g_isoMdShake128, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SHAKE256, g_isoMdShake256, CRYPT_EAL_ISO_ATTR}, {CRYPT_MD_SM3, g_isoMdSm3, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoKdfs[] = { {CRYPT_KDF_SCRYPT, g_isoKdfScrypt, CRYPT_EAL_ISO_ATTR}, {CRYPT_KDF_PBKDF2, g_isoKdfPBKdf2, CRYPT_EAL_ISO_ATTR}, {CRYPT_KDF_KDFTLS12, g_isoKdfKdfTLS12, CRYPT_EAL_ISO_ATTR}, {CRYPT_KDF_HKDF, g_isoKdfHkdf, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoKeyMgmt[] = { {CRYPT_PKEY_DSA, g_isoKeyMgmtDsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ED25519, g_isoKeyMgmtEd25519, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_X25519, g_isoKeyMgmtX25519, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_RSA, g_isoKeyMgmtRsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_DH, g_isoKeyMgmtDh, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ECDSA, g_isoKeyMgmtEcdsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ECDH, g_isoKeyMgmtEcdh, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_SM2, g_isoKeyMgmtSm2, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_SLH_DSA, g_isoKeyMgmtSlhDsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ML_KEM, g_isoKeyMgmtMlKem, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ML_DSA, g_isoKeyMgmtMlDsa, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoAsymCiphers[] = { {CRYPT_PKEY_RSA, g_isoAsymCipherRsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_SM2, g_isoAsymCipherSm2, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoKeyExch[] = { {CRYPT_PKEY_X25519, g_isoExchX25519, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_DH, g_isoExchDh, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ECDH, g_isoExchEcdh, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_SM2, g_isoExchSm2, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoSigns[] = { {CRYPT_PKEY_DSA, g_isoSignDsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ED25519, g_isoSignEd25519, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_RSA, g_isoSignRsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ECDSA, g_isoSignEcdsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_SM2, g_isoSignSm2, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_SLH_DSA, g_isoSignSlhDsa, CRYPT_EAL_ISO_ATTR}, {CRYPT_PKEY_ML_DSA, g_isoSignMlDsa, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoMacs[] = { {CRYPT_MAC_HMAC_SHA1, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA224, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA256, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA384, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA512, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA3_224, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA3_256, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA3_384, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SHA3_512, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_HMAC_SM3, g_isoMacHmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_CMAC_AES128, g_isoMacCmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_CMAC_AES192, g_isoMacCmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_CMAC_AES256, g_isoMacCmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_CMAC_SM4, g_isoMacCmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_GMAC_AES128, g_isoMacGmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_GMAC_AES192, g_isoMacGmac, CRYPT_EAL_ISO_ATTR}, {CRYPT_MAC_GMAC_AES256, g_isoMacGmac, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoRands[] = { {CRYPT_RAND_SHA1, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_SHA224, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_SHA256, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_SHA384, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_SHA512, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_SM3, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_HMAC_SHA1, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_HMAC_SHA224, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_HMAC_SHA256, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_HMAC_SHA384, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_HMAC_SHA512, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_AES128_CTR, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_AES192_CTR, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_AES256_CTR, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_AES128_CTR_DF, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_AES192_CTR_DF, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_AES256_CTR_DF, g_isoRand, CRYPT_EAL_ISO_ATTR}, {CRYPT_RAND_SM4_CTR_DF, g_isoRand, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoCiphers[] = { {CRYPT_CIPHER_AES128_CBC, g_isoCbc, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES192_CBC, g_isoCbc, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_CBC, g_isoCbc, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES128_CTR, g_isoCtr, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES192_CTR, g_isoCtr, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_CTR, g_isoCtr, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES128_ECB, g_isoEcb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES192_ECB, g_isoEcb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_ECB, g_isoEcb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES128_CCM, g_isoCcm, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES192_CCM, g_isoCcm, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_CCM, g_isoCcm, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES128_GCM, g_isoGcm, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES192_GCM, g_isoGcm, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_GCM, g_isoGcm, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES128_XTS, g_isoXts, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_XTS, g_isoXts, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_CHACHA20_POLY1305, g_isoChaCha, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_SM4_XTS, g_isoXts, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_SM4_CBC, g_isoCbc, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_SM4_ECB, g_isoEcb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_SM4_CTR, g_isoCtr, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_SM4_GCM, g_isoGcm, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_SM4_CFB, g_isoCfb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_SM4_OFB, g_isoOfb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES128_CFB, g_isoCfb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES192_CFB, g_isoCfb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_CFB, g_isoCfb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES128_OFB, g_isoOfb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES192_OFB, g_isoOfb, CRYPT_EAL_ISO_ATTR}, {CRYPT_CIPHER_AES256_OFB, g_isoOfb, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoKems[] = { {CRYPT_PKEY_ML_KEM, g_isoMlKem, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_isoSelftests[] = { {CRYPT_CMVP_PROVIDER_SELFTEST, g_isoSelftest, CRYPT_EAL_ISO_ATTR}, CRYPT_EAL_ALGINFO_END }; static int32_t CRYPT_EAL_IsoProvQuery(void *provCtx, int32_t operaId, const CRYPT_EAL_AlgInfo **algInfos) { (void)provCtx; int32_t ret = CRYPT_SUCCESS; switch (operaId) { case CRYPT_EAL_OPERAID_SYMMCIPHER: *algInfos = g_isoCiphers; break; case CRYPT_EAL_OPERAID_KEYMGMT: *algInfos = g_isoKeyMgmt; break; case CRYPT_EAL_OPERAID_SIGN: *algInfos = g_isoSigns; break; case CRYPT_EAL_OPERAID_ASYMCIPHER: *algInfos = g_isoAsymCiphers; break; case CRYPT_EAL_OPERAID_KEYEXCH: *algInfos = g_isoKeyExch; break; case CRYPT_EAL_OPERAID_KEM: *algInfos = g_isoKems; break; case CRYPT_EAL_OPERAID_HASH: *algInfos = g_isoMds; break; case CRYPT_EAL_OPERAID_MAC: *algInfos = g_isoMacs; break; case CRYPT_EAL_OPERAID_KDF: *algInfos = g_isoKdfs; break; case CRYPT_EAL_OPERAID_RAND: *algInfos = g_isoRands; break; case CRYPT_EAL_OPERAID_SELFTEST: *algInfos = g_isoSelftests; break; default: ret = CRYPT_NOT_SUPPORT; break; } return ret; } static void CRYPT_EAL_IsoProvFree(void *provCtx) { if (provCtx == NULL) { return; } CRYPT_EAL_IsoProvCtx *temp = (CRYPT_EAL_IsoProvCtx *)provCtx; CRYPT_EAL_SeedPoolFree(temp->pool); CRYPT_EAL_EsFree(temp->es); BSL_SAL_Free(provCtx); } static CRYPT_EAL_Func g_isoProvOutFuncs[] = { {CRYPT_EAL_PROVCB_QUERY, CRYPT_EAL_IsoProvQuery}, {CRYPT_EAL_PROVCB_FREE, CRYPT_EAL_IsoProvFree}, {CRYPT_EAL_PROVCB_CTRL, NULL}, {CRYPT_EAL_PROVCB_GETCAPS, NULL}, CRYPT_EAL_FUNC_END }; static void EntropyRunLogCb(int32_t ret) { CMVP_Iso19790EventProcess(CRYPT_EVENT_ES_HEALTH_TEST, 0, 0, ret); } static int32_t CreateIsoEs(CRYPT_EAL_Es **es) { int32_t ret = CRYPT_SUCCESS; CRYPT_EAL_Es *esTemp = CRYPT_EAL_EsNew(); RETURN_RET_IF(esTemp == NULL, CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_SET_CF, "sha256_df", (uint32_t)strlen("sha256_df")); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_REMOVE_NS, "timestamp", (uint32_t)strlen("timestamp")); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_SET_LOG_CALLBACK, EntropyRunLogCb, 0); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); bool healthTest = true; ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_ENABLE_TEST, &healthTest, sizeof(healthTest)); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); uint32_t size = CRYPT_ENTROPY_SEED_POOL_SIZE; ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_SET_POOL_SIZE, &size, sizeof(size)); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_EsInit(esTemp); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); *es = esTemp; return ret; ERR: CRYPT_EAL_EsFree(esTemp); return ret; } static int32_t CreateSeedPool(CRYPT_EAL_SeedPoolCtx **seedPool, CRYPT_EAL_Es **es) { CRYPT_EAL_SeedPoolCtx *poolTemp = NULL; CRYPT_EAL_Es *esTemp = NULL; int32_t ret = CreateIsoEs(&esTemp); if (ret != CRYPT_SUCCESS) { return ret; } poolTemp = CRYPT_EAL_SeedPoolNew(true); if (poolTemp == NULL) { CRYPT_EAL_EsFree(esTemp); BSL_ERR_PUSH_ERROR(CRYPT_SEED_POOL_NEW_ERROR); return CRYPT_SEED_POOL_NEW_ERROR; } CRYPT_EAL_EsPara para = {false, CRYPT_ENTROPY_SOURCE_ENTROPY, esTemp, (CRYPT_EAL_EntropyGet)CRYPT_EAL_EsEntropyGet}; ret = CRYPT_EAL_SeedPoolAddEs(poolTemp, &para); if (ret != CRYPT_SUCCESS) { CRYPT_EAL_SeedPoolFree(poolTemp); CRYPT_EAL_EsFree(esTemp); return ret; } *seedPool = poolTemp; *es = esTemp; return CRYPT_SUCCESS; } static int32_t IsoCreateProvCtx(void *libCtx, CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, void **provCtx) { CRYPT_EAL_IsoProvCtx *temp = BSL_SAL_Calloc(1, sizeof(CRYPT_EAL_IsoProvCtx)); if (temp == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } int32_t ret = CRYPT_Iso_GetLogFunc(param, &temp->runLog); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(temp); return ret; } CRYPT_EAL_SetRandCallBackEx((CRYPT_EAL_RandFuncEx)CRYPT_EAL_RandbytesEx); ret = CreateSeedPool(&temp->pool, &temp->es); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(temp); return ret; } temp->libCtx = libCtx; temp->mgrCtx = mgrCtx; *provCtx = temp; return CRYPT_SUCCESS; } int32_t CRYPT_EAL_ProviderInit(CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, CRYPT_EAL_Func *capFuncs, CRYPT_EAL_Func **outFuncs, void **provCtx) { void *libCtx = NULL; CRYPT_EAL_ProvMgrCtrlCb mgrCtrl = NULL; int32_t index = 0; int32_t ret; while (capFuncs[index].id != 0) { switch (capFuncs[index].id) { case CRYPT_EAL_CAP_MGRCTXCTRL: mgrCtrl = capFuncs[index].func; break; default: break; } index++; } if (mgrCtrl == NULL) { return CRYPT_PROVIDER_NOT_SUPPORT; } ret = mgrCtrl(mgrCtx, CRYPT_EAL_MGR_GETLIBCTX, &libCtx, 0); if (ret != CRYPT_SUCCESS) { return ret; } ret = CRYPT_Iso_Selftest(param); if (ret != CRYPT_SUCCESS) { return ret; } ret = IsoCreateProvCtx(libCtx, mgrCtx, param, provCtx); if (ret != CRYPT_SUCCESS) { return ret; } *outFuncs = g_isoProvOutFuncs; return CRYPT_SUCCESS; } #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_provider.c
C
unknown
14,518
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief iso19790 provider header */ #ifndef CRYPT_EAL_ISO_PROVIDER_H #define CRYPT_EAL_ISO_PROVIDER_H #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include <stdint.h> #include "crypt_eal_entropy.h" #include "crypt_eal_implprovider.h" #include "crypt_eal_cmvp.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #define CRYPT_PARAM_EVENT 1 #define CRYPT_PARAM_ALGID 2 #define CRYPT_PARAM_ALGO_TYPE 3 #define CRYPT_PARAM_LIB_CTX 4 #define CRYPT_EAL_ISO_ATTR "provider=iso" typedef struct EalIsoProvCtx { void *libCtx; void *mgrCtx; CRYPT_EAL_Es *es; CRYPT_EAL_SeedPoolCtx *pool; CRYPT_EAL_CMVP_LogFunc runLog; } CRYPT_EAL_IsoProvCtx; int32_t CRYPT_EAL_ProviderInit(CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, CRYPT_EAL_Func *capFuncs, CRYPT_EAL_Func **outFuncs, void **provCtx); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_ISO19790 */ #endif /* CRYPT_EAL_ISO_PROVIDER_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_provider.h
C
unknown
1,566
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include <string.h> #include "crypt_eal_implprovider.h" #include "crypt_drbg.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "bsl_params.h" #include "eal_entropy.h" #include "cmvp_iso19790.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provider.h" #if defined(HITLS_CRYPTO_DRBG) #define CRYPT_DRBG_PARAM_NUM 6 typedef struct { int32_t algId; void *ctx; void *provCtx; } IsoRandCtx; static void *DefaultDrbgNew(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId) { void *randCtx = NULL; CRYPT_RandSeedMethod seedMethond = {0}; int32_t ret = EAL_SetDefaultEntropyMeth(&seedMethond); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } int32_t index = 0; BSL_Param randParam[CRYPT_DRBG_PARAM_NUM] = {{0}, {0}, {0}, {0}, {0}, BSL_PARAM_END}; (void)BSL_PARAM_InitValue(&randParam[index++], CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMethond.getEntropy, 0); (void)BSL_PARAM_InitValue(&randParam[index++], CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMethond.cleanEntropy, 0); (void)BSL_PARAM_InitValue(&randParam[index++], CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMethond.getNonce, 0); (void)BSL_PARAM_InitValue(&randParam[index++], CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMethond.cleanNonce, 0); (void)BSL_PARAM_InitValue(&randParam[index++], CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, provCtx->pool, 0); randCtx = DRBG_New(provCtx->libCtx, algId, randParam); if (randCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } return randCtx; } static void *CRYPT_EAL_IsoRandNewCtx(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId, BSL_Param *param) { void *randCtx = NULL; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Drbg(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif BSL_Param *getEnt = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_GETENTROPY); if (param == NULL || getEnt == NULL) { return DefaultDrbgNew(provCtx, algId); } randCtx = DRBG_New(provCtx->libCtx, algId, param); if (randCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } return randCtx; } static void *CRYPT_EAL_IsoRandNewCtxWrapper(CRYPT_EAL_IsoProvCtx *provCtx, int32_t algId, BSL_Param *param) { if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } IsoRandCtx *ctx = BSL_SAL_Calloc(1, sizeof(IsoRandCtx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } void *randCtx = CRYPT_EAL_IsoRandNewCtx(provCtx, algId, param); if (randCtx == NULL) { BSL_SAL_FREE(ctx); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } ctx->algId = algId; ctx->ctx = randCtx; ctx->provCtx = provCtx; return ctx; } static int32_t DRBG_InstantiateWrapper(IsoRandCtx *ctx, const uint8_t *person, uint32_t persLen, BSL_Param *param) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_RAND, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return DRBG_Instantiate(ctx->ctx, person, persLen, param); } static int32_t DRBG_UninstantiateWrapper(IsoRandCtx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_RAND, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return DRBG_Uninstantiate(ctx->ctx); } static int32_t DRBG_GenerateBytesWrapper(IsoRandCtx *ctx, uint8_t *out, uint32_t outLen, const uint8_t *adin, uint32_t adinLen, BSL_Param *param) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_RANDGEN, CRYPT_ALGO_RAND, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return DRBG_GenerateBytes(ctx->ctx, out, outLen, adin, adinLen, param); } static int32_t DRBG_ReseedWrapper(IsoRandCtx *ctx, const uint8_t *adin, uint32_t adinLen, BSL_Param *param) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SETSSP, CRYPT_ALGO_RAND, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return DRBG_Reseed(ctx->ctx, adin, adinLen, param); } static int32_t DRBG_CtrlWrapper(IsoRandCtx *ctx, int32_t opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return DRBG_Ctrl(ctx->ctx, opt, val, len); } static void DRBG_FreeWrapper(IsoRandCtx *ctx) { if (ctx == NULL) { return; } (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_ZERO, CRYPT_ALGO_RAND, ctx->algId); if (ctx->ctx != NULL) { DRBG_Free(ctx->ctx); } BSL_SAL_Free(ctx); } #endif const CRYPT_EAL_Func g_isoRand[] = { #if defined(HITLS_CRYPTO_DRBG) {CRYPT_EAL_IMPLRAND_DRBGNEWCTX, (CRYPT_EAL_ImplRandDrbgNewCtx)CRYPT_EAL_IsoRandNewCtxWrapper}, {CRYPT_EAL_IMPLRAND_DRBGINST, (CRYPT_EAL_ImplRandDrbgInst)DRBG_InstantiateWrapper}, {CRYPT_EAL_IMPLRAND_DRBGUNINST, (CRYPT_EAL_ImplRandDrbgUnInst)DRBG_UninstantiateWrapper}, {CRYPT_EAL_IMPLRAND_DRBGGEN, (CRYPT_EAL_ImplRandDrbgGen)DRBG_GenerateBytesWrapper}, {CRYPT_EAL_IMPLRAND_DRBGRESEED, (CRYPT_EAL_ImplRandDrbgReSeed)DRBG_ReseedWrapper}, {CRYPT_EAL_IMPLRAND_DRBGCTRL, (CRYPT_EAL_ImplRandDrbgCtrl)DRBG_CtrlWrapper}, {CRYPT_EAL_IMPLRAND_DRBGFREECTX, (CRYPT_EAL_ImplRandDrbgFreeCtx)DRBG_FreeWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_rand.c
C
unknown
6,755
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "securec.h" #include "crypt_types.h" #include "crypt_errno.h" #include "crypt_iso_provider.h" #include "bsl_params.h" #include "bsl_err_internal.h" #include "cmvp_iso19790.h" #include "crypt_cmvp_selftest.h" #include "crypt_params_key.h" #include "cmvp_common.h" #include "crypt_iso_selftest.h" #define BSL_PARAM_COUNT 4 int32_t CRYPT_Iso_Log(void *provCtx, CRYPT_EVENT_TYPE event, CRYPT_ALGO_TYPE type, int32_t id) { int32_t algId = id; int32_t algType = type; int index = 0; BSL_Param param[BSL_PARAM_COUNT] = {{0}, {0}, {0}, BSL_PARAM_END}; (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_EVENT, BSL_PARAM_TYPE_INT32, &event, sizeof(event)); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_ALGID, BSL_PARAM_TYPE_INT32, &algId, sizeof(algId)); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_ALGO_TYPE, BSL_PARAM_TYPE_INT32, &algType, sizeof(algType)); return CRYPT_Iso_EventOperation(provCtx, param); } static void IsoRunLog(void *provCtx, CRYPT_EVENT_TYPE oper, CRYPT_ALGO_TYPE type, int32_t id, int32_t err) { if (provCtx == NULL || ((CRYPT_EAL_IsoProvCtx *)provCtx)->runLog == NULL) { return; } ((CRYPT_EAL_IsoProvCtx *)provCtx)->runLog(oper, type, id, err); } static int32_t IsoLogEvent(CRYPT_EVENT_TYPE event, void *provCtx, BSL_Param *param, int32_t ret) { BSL_Param *temp = BSL_PARAM_FindParam(param, CRYPT_PARAM_ALGID); if (temp == NULL || temp->valueType != BSL_PARAM_TYPE_INT32) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t id = *(int32_t *)(uintptr_t)temp->value; temp = BSL_PARAM_FindParam(param, CRYPT_PARAM_ALGO_TYPE); if (temp == NULL || temp->valueType != BSL_PARAM_TYPE_INT32) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t type = *(int32_t *)(uintptr_t)temp->value; IsoRunLog(provCtx, event, type, id, ret); return CRYPT_SUCCESS; } static int32_t IsoIntegrityTest(void *provCtx, BSL_Param *param) { void *libCtx = NULL; BSL_Param *temp = BSL_PARAM_FindParam(param, CRYPT_PARAM_LIB_CTX); int32_t ret = BSL_PARAM_GetPtrValue(temp, CRYPT_PARAM_LIB_CTX, BSL_PARAM_TYPE_CTX_PTR, &libCtx, NULL); if (ret != BSL_SUCCESS || libCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } IsoRunLog(provCtx, CRYPT_EVENT_INTEGRITY_TEST, 0, 0, CRYPT_SUCCESS); ret = CMVP_Iso19790CheckIntegrity(libCtx, CRYPT_EAL_ISO_ATTR); if (ret != CRYPT_SUCCESS) { IsoRunLog(provCtx, CRYPT_EVENT_INTEGRITY_TEST, 0, 0, ret); return ret; } return CRYPT_SUCCESS; } static int32_t IsoKatTest(void *provCtx, BSL_Param *param) { void *libCtx = NULL; BSL_Param *temp = BSL_PARAM_FindParam(param, CRYPT_PARAM_LIB_CTX); int32_t ret = BSL_PARAM_GetPtrValue(temp, CRYPT_PARAM_LIB_CTX, BSL_PARAM_TYPE_CTX_PTR, &libCtx, NULL); if (ret != BSL_SUCCESS || libCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } IsoRunLog(provCtx, CRYPT_EVENT_KAT_TEST, 0, 0, CRYPT_SUCCESS); ret = CMVP_Iso19790Kat(libCtx, CRYPT_EAL_ISO_ATTR); if (ret != CRYPT_SUCCESS) { IsoRunLog(provCtx, CRYPT_EVENT_KAT_TEST, 0, 0, ret); return ret; } return CRYPT_SUCCESS; } int32_t CRYPT_Iso_EventOperation(void *provCtx, BSL_Param *param) { BSL_Param *temp = BSL_PARAM_FindParam(param, CRYPT_PARAM_EVENT); if (temp == NULL || temp->valueType != BSL_PARAM_TYPE_INT32) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t event = *(int32_t *)(uintptr_t)temp->value; switch (event) { case CRYPT_EVENT_KAT_TEST: return IsoKatTest(provCtx, param); case CRYPT_EVENT_INTEGRITY_TEST: return IsoIntegrityTest(provCtx, param); case CRYPT_EVENT_PARAM_CHECK: return IsoLogEvent(event, provCtx, param, CRYPT_CMVP_ERR_PARAM_CHECK); case CRYPT_EVENT_ENC: case CRYPT_EVENT_DEC: case CRYPT_EVENT_GEN: case CRYPT_EVENT_SIGN: case CRYPT_EVENT_VERIFY: case CRYPT_EVENT_MD: case CRYPT_EVENT_MAC: case CRYPT_EVENT_KDF: case CRYPT_EVENT_KEYAGGREMENT: case CRYPT_EVENT_RANDGEN: case CRYPT_EVENT_ZERO: case CRYPT_EVENT_SETSSP: case CRYPT_EVENT_GETSSP: case CRYPT_EVENT_ENCAPS: case CRYPT_EVENT_DECAPS: case CRYPT_EVENT_BLIND: case CRYPT_EVENT_UNBLIND: case CRYPT_EVENT_PCT_TEST: case CRYPT_EVENT_GET_VERSION: return IsoLogEvent(event, provCtx, param, CRYPT_SUCCESS); default: break; } return CRYPT_NOT_SUPPORT; } int32_t CRYPT_Iso_GetLogFunc(BSL_Param *param, CRYPT_EAL_CMVP_LogFunc *logFunc) { int32_t ret = CRYPT_SUCCESS; BSL_Param *temp = BSL_PARAM_FindParam(param, CRYPT_PARAM_CMVP_LOG_FUNC); if (temp == NULL) { *logFunc = CMVP_Iso19790EventProcess; return CRYPT_SUCCESS; } ret = BSL_PARAM_GetPtrValue(temp, CRYPT_PARAM_CMVP_LOG_FUNC, BSL_PARAM_TYPE_FUNC_PTR, (void **)logFunc, NULL); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (*logFunc == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SUCCESS; } int32_t CRYPT_Iso_Selftest(BSL_Param *param) { CRYPT_EAL_LibCtx *libCtx = NULL; if (CMVP_CheckIsInternalLibCtx(param)) { return CRYPT_SUCCESS; } int32_t ret = CMVP_CreateInternalLibCtx(param, &libCtx, CRYPT_Iso_Selftest); if (ret != CRYPT_SUCCESS) { return ret; } CRYPT_EAL_CMVP_LogFunc runLog = NULL; ret = CRYPT_Iso_GetLogFunc(param, &runLog); if (ret != CRYPT_SUCCESS) { CRYPT_EAL_LibCtxFree(libCtx); return ret; } runLog(CRYPT_EVENT_INTEGRITY_TEST, 0, 0, CRYPT_SUCCESS); ret = CMVP_Iso19790CheckIntegrity(libCtx, CRYPT_EAL_ISO_ATTR); if (ret != CRYPT_SUCCESS) { runLog(CRYPT_EVENT_INTEGRITY_TEST, 0, 0, ret); CRYPT_EAL_LibCtxFree(libCtx); return ret; } runLog(CRYPT_EVENT_KAT_TEST, 0, 0, CRYPT_SUCCESS); ret = CMVP_Iso19790Kat(libCtx, CRYPT_EAL_ISO_ATTR); CRYPT_EAL_LibCtxFree(libCtx); if (ret != CRYPT_SUCCESS) { runLog(CRYPT_EVENT_KAT_TEST, 0, 0, ret); return ret; } return CRYPT_SUCCESS; } #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_selftest.c
C
unknown
7,110
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief iso19790 provider header */ #ifndef CRYPT_EAL_ISO_SELFTEST_H #define CRYPT_EAL_ISO_SELFTEST_H #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include <stdint.h> #include "crypt_iso_provider.h" #include "crypt_eal_provider.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ int32_t CRYPT_Iso_Selftest(BSL_Param *param); int32_t CRYPT_Iso_Log(void *provCtx, CRYPT_EVENT_TYPE event, CRYPT_ALGO_TYPE type, int32_t id); int32_t CRYPT_Iso_EventOperation(void *provCtx, BSL_Param *param); int32_t CRYPT_Iso_GetLogFunc(BSL_Param *param, CRYPT_EAL_CMVP_LogFunc *logFunc); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_ISO19790 */ #endif /* CRYPT_EAL_ISO_SELFTEST_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_selftest.h
C
unknown
1,301
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_ISO19790 #include "crypt_eal_implprovider.h" #include "crypt_dsa.h" #include "crypt_rsa.h" #include "crypt_ecdsa.h" #include "crypt_sm2.h" #include "crypt_curve25519.h" #include "crypt_slh_dsa.h" #include "crypt_mldsa.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_cmvp.h" #include "cmvp_iso19790.h" #include "crypt_iso_selftest.h" #include "crypt_iso_provderimpl.h" static int32_t CheckSignVerifyMdAlgId(CRYPT_Iso_Pkey_Ctx *ctx, int32_t algId, bool isSign) { CRYPT_EVENT_TYPE event = isSign ? CRYPT_EVENT_SIGN : CRYPT_EVENT_VERIFY; CRYPT_EAL_PkeyC2Data data = {NULL, NULL, NULL, algId, CRYPT_PKEY_PARAID_MAX, event, NULL, NULL, NULL}; if (!CMVP_Iso19790PkeyC2(ctx->algId, &data)) { (void)CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_PARAM_CHECK, CRYPT_ALGO_PKEY, ctx->algId); return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SUCCESS; } #define PKEY_SIGN_FUNC(name) \ static int32_t CRYPT_##name##_SignWrapper(CRYPT_Iso_Pkey_Ctx *ctx, int32_t algId, const uint8_t *data, \ uint32_t dataLen, uint8_t *sign, uint32_t *signLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SIGN, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CheckSignVerifyMdAlgId(ctx, algId, true); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Sign(ctx->ctx, algId, data, dataLen, sign, signLen); \ } \ \ static int32_t CRYPT_##name##_VerifyWrapper(CRYPT_Iso_Pkey_Ctx *ctx, int32_t algId, const uint8_t *msg, \ uint32_t msgLen, uint8_t *sign, uint32_t signLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_VERIFY, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ ret = CheckSignVerifyMdAlgId(ctx, algId, false); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_Verify(ctx->ctx, algId, msg, msgLen, sign, signLen); \ } #define PKEY_SIGN_DATA_FUNC(name) \ static int32_t CRYPT_##name##_SignDataWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const uint8_t *data, uint32_t dataLen, \ uint8_t *sign, uint32_t *signLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_SIGN, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_SignData(ctx->ctx, data, dataLen, sign, signLen); \ } \ \ static int32_t CRYPT_##name##_VerifyDataWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const uint8_t *data, uint32_t dataLen, \ uint8_t *sign, uint32_t signLen) \ { \ if (ctx == NULL) { \ BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); \ return CRYPT_NULL_INPUT; \ } \ int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_VERIFY, CRYPT_ALGO_PKEY, ctx->algId); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ return CRYPT_##name##_VerifyData(ctx->ctx, data, dataLen, sign, signLen); \ } static int32_t CRYPT_RSA_RecoverWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_RSA_Recover(ctx->ctx, data, dataLen, out, outLen); } static int32_t CRYPT_RSA_BlindWrapper(CRYPT_Iso_Pkey_Ctx *ctx, int32_t algId, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_BLIND, CRYPT_ALGO_PKEY, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return CRYPT_RSA_Blind(ctx->ctx, algId, input, inputLen, out, outLen); } static int32_t CRYPT_RSA_UnBlindWrapper(CRYPT_Iso_Pkey_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_Iso_Log(ctx->provCtx, CRYPT_EVENT_UNBLIND, CRYPT_ALGO_PKEY, ctx->algId); if (ret != CRYPT_SUCCESS) { return ret; } return CRYPT_RSA_UnBlind(ctx->ctx, input, inputLen, out, outLen); } PKEY_SIGN_FUNC(DSA) PKEY_SIGN_FUNC(CURVE25519) PKEY_SIGN_FUNC(RSA) PKEY_SIGN_FUNC(ECDSA) PKEY_SIGN_FUNC(SM2) PKEY_SIGN_FUNC(SLH_DSA) PKEY_SIGN_FUNC(ML_DSA) PKEY_SIGN_DATA_FUNC(DSA) PKEY_SIGN_DATA_FUNC(RSA) PKEY_SIGN_DATA_FUNC(ECDSA) const CRYPT_EAL_Func g_isoSignDsa[] = { #ifdef HITLS_CRYPTO_DSA {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_DSA_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_SIGNDATA, (CRYPT_EAL_ImplPkeySignData)CRYPT_DSA_SignDataWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_DSA_VerifyWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFYDATA, (CRYPT_EAL_ImplPkeyVerifyData)CRYPT_DSA_VerifyDataWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoSignEd25519[] = { #ifdef HITLS_CRYPTO_ED25519 {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_CURVE25519_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_CURVE25519_VerifyWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoSignRsa[] = { #ifdef HITLS_CRYPTO_RSA_SIGN {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_RSA_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_SIGNDATA, (CRYPT_EAL_ImplPkeySignData)CRYPT_RSA_SignDataWrapper}, #endif #ifdef HITLS_CRYPTO_RSA_VERIFY {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_RSA_VerifyWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFYDATA, (CRYPT_EAL_ImplPkeyVerifyData)CRYPT_RSA_VerifyDataWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_RECOVER, (CRYPT_EAL_ImplPkeyRecover)CRYPT_RSA_RecoverWrapper}, #endif #ifdef HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_SIGN {CRYPT_EAL_IMPLPKEYSIGN_BLIND, (CRYPT_EAL_ImplPkeyBlind)CRYPT_RSA_BlindWrapper}, #endif #ifdef HITLS_CRYPTO_RSA_VERIFY {CRYPT_EAL_IMPLPKEYSIGN_UNBLIND, (CRYPT_EAL_ImplPkeyUnBlind)CRYPT_RSA_UnBlindWrapper}, #endif #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoSignEcdsa[] = { #ifdef HITLS_CRYPTO_ECDSA {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_ECDSA_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_SIGNDATA, (CRYPT_EAL_ImplPkeySignData)CRYPT_ECDSA_SignDataWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_ECDSA_VerifyWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFYDATA, (CRYPT_EAL_ImplPkeyVerifyData)CRYPT_ECDSA_VerifyDataWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoSignSm2[] = { #ifdef HITLS_CRYPTO_SM2_SIGN {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_SM2_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_SM2_VerifyWrapper}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_isoSignSlhDsa[] = { #ifdef HITLS_CRYPTO_SLH_DSA {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_SLH_DSA_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_SLH_DSA_VerifyWrapper}, #endif CRYPT_EAL_FUNC_END }; const CRYPT_EAL_Func g_isoSignMlDsa[] = { #ifdef HITLS_CRYPTO_MLDSA {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_ML_DSA_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_ML_DSA_VerifyWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_ISO19790 */
2301_79861745/bench_create
crypto/provider/src/cmvp/iso_prov/crypt_iso_sign.c
C
unknown
13,342
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "cmvp_sm.h" #include "cmvp_common.h" #include "crypt_errno.h" #include "crypt_cmvp_selftest.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "cmvp_integrity_hmac.h" #include "crypt_params_key.h" #include "securec.h" #include "bsl_sal.h" bool CMVP_SmPkeyPct(void *ctx, int32_t algId) { return CRYPT_CMVP_SelftestPkeyPct(ctx, algId); } bool CMVP_SmPkeyC2(int32_t algId) { if (algId != CRYPT_MD_SM3) { return false; } return true; } bool CMVP_SmKdfC2(const CRYPT_EAL_KdfC2Data *data) { // According to GM/T 0091-2020 A1.1 and A1.2, min saltLen is 8, min iter is 1024. if (data->pbkdf2->macId != CRYPT_MAC_HMAC_SM3 || data->pbkdf2->saltLen < 8 || data->pbkdf2->iter < 1024) { return false; } return true; } static bool CipherKat(void *libCtx, const char *attrName) { static const uint32_t list[] = { CRYPT_CIPHER_SM4_XTS, CRYPT_CIPHER_SM4_CBC, CRYPT_CIPHER_SM4_ECB, CRYPT_CIPHER_SM4_CTR, CRYPT_CIPHER_SM4_GCM, CRYPT_CIPHER_SM4_CFB, CRYPT_CIPHER_SM4_OFB, }; bool ret = false; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { ret = CRYPT_CMVP_SelftestProviderCipher(libCtx, attrName, list[i]); if (!ret) { return false; } } return true; } static bool MdKat(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestProviderMd(libCtx, attrName, CRYPT_MD_SM3); } static bool MacKat(void *libCtx, const char *attrName) { static const uint32_t list[] = { CRYPT_MAC_HMAC_SM3, CRYPT_MAC_CBC_MAC_SM4, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (!CRYPT_CMVP_SelftestProviderMac(libCtx, attrName, list[i])) { return false; } } return true; } static bool DrbgKat(void *libCtx, const char *attrName) { static const uint32_t list[] = { CRYPT_RAND_SM4_CTR_DF, CRYPT_RAND_SM3, }; for (uint32_t i = 0; i < sizeof(list) / sizeof(list[0]); i++) { if (!CRYPT_CMVP_SelftestProviderDrbg(libCtx, attrName, list[i])) { return false; } } return true; } static bool KdfKat(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestProviderPbkdf2(libCtx, attrName, CRYPT_MAC_HMAC_SM3); } static bool PkeyKat(void *libCtx, const char *attrName) { return CRYPT_CMVP_SelftestProviderSM2(libCtx, attrName); } int32_t CMVP_SmKat(void *libCtx, const char *attrName) { bool ret = CipherKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = MdKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = MacKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = DrbgKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = KdfKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); ret = PkeyKat(libCtx, attrName); RETURN_RET_IF(ret == false, CRYPT_CMVP_ERR_ALGO_SELFTEST); return CRYPT_SUCCESS; } int32_t CMVP_SmCheckIntegrity(void *libCtx, const char *attrName) { return CMVP_CheckIntegrity(libCtx, attrName, CRYPT_MAC_HMAC_SM3); } static int32_t RandomSelftest(CRYPT_EAL_RndCtx *randCtx) { // GM/T 0062-2018: // Random number power-on self-test parameters: // a) Test quantity: Collect 20 * 10^4 bits of random numbers, divided into 20 groups, each with 10^4 bits. uint32_t groups = 20; uint32_t bitsPerGroup = 10000; // b) Test item: Poker test, parameter m=2. // c) Test criteria: If 2 or more groups fail the test criteria, an alert is triggered indicating test failure. // One retry of random number collection and testing is allowed. If the repeated test still fails, // the product's random number generator is deemed to have failed. uint32_t retry = 2; uint32_t threshold = 2; const uint32_t bytesPerGroup = (bitsPerGroup + 7) >> 3; const uint32_t totalLen = groups * bytesPerGroup; uint8_t *data = BSL_SAL_Malloc(totalLen); if (data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } bool isSuccess = false; for (uint32_t attempt = 0; attempt < retry; attempt++) { int32_t ret = CRYPT_EAL_Drbgbytes(randCtx, data, totalLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_Free(data); return ret; } uint32_t failCnt = 0; for (uint32_t i = 0; i < groups; i++) { ret = CRYPT_CMVP_RandomnessTest(data + i * bytesPerGroup, bytesPerGroup); if (ret == CRYPT_SUCCESS) { continue; } failCnt++; if (failCnt >= threshold) { break; } } if (failCnt < threshold) { isSuccess = true; break; } } BSL_SAL_Free(data); return isSuccess ? CRYPT_SUCCESS : CRYPT_CMVP_RANDOMNESS_ERR; } static int32_t RandomStartupSelftest(void *libCtx, const char *attrName, int32_t algId) { int32_t ret = CRYPT_SUCCESS; CRYPT_EAL_RndCtx *randCtx = CRYPT_EAL_ProviderDrbgNewCtx(libCtx, algId, attrName, NULL); if (randCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t selfTest = 0; ret = CRYPT_EAL_DrbgCtrl(randCtx, CRYPT_CTRL_SET_SELFTEST_FLAG, &selfTest, sizeof(int32_t)); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); CRYPT_EAL_DrbgDeinit(randCtx); return ret; } ret = CRYPT_EAL_DrbgInstantiate(randCtx, NULL, 0); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); CRYPT_EAL_DrbgDeinit(randCtx); return ret; } ret = RandomSelftest(randCtx); CRYPT_EAL_DrbgDeinit(randCtx); return ret; } int32_t CMVP_SmRandomStartupSelftest(void *libCtx, const char *attrName) { int32_t ret = RandomStartupSelftest(libCtx, attrName, CRYPT_RAND_SM3); if (ret != CRYPT_SUCCESS) { return ret; } return RandomStartupSelftest(libCtx, attrName, CRYPT_RAND_SM4_CTR_DF); } #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/cmvp_sm.c
C
unknown
6,877
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CMVP_SM_H #define CMVP_SM_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include <stdint.h> #include <stdbool.h> #include "crypt_cmvp.h" #include "crypt_algid.h" #include "crypt_sm_provider.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ bool CMVP_SmPkeyC2(int32_t algId); bool CMVP_SmKdfC2(const CRYPT_EAL_KdfC2Data *data); int32_t CMVP_SmKat(void *libCtx, const char *attrName); int32_t CMVP_SmCheckIntegrity(void *libCtx, const char *attrName); bool CMVP_SmPkeyPct(void *ctx, int32_t algId); int32_t CMVP_SmRandomStartupSelftest(void *libCtx, const char *attrName); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_SM */ #endif /* CMVP_SM_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/cmvp_sm.h
C
unknown
1,256
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "crypt_modes_cbc.h" #include "crypt_modes_ctr.h" #include "crypt_modes_ecb.h" #include "crypt_modes_gcm.h" #include "crypt_modes_ofb.h" #include "crypt_modes_cfb.h" #include "crypt_modes_xts.h" #include "crypt_local_types.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" static void *CRYPT_EAL_SmCipherNewCtx(void *provCtx, int32_t algId) { (void) provCtx; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Cipher(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif CRYPT_EAL_Func cipherNewCtxFunc[] = { {CRYPT_CIPHER_SM4_XTS, MODES_XTS_NewCtx}, {CRYPT_CIPHER_SM4_CBC, MODES_CBC_NewCtx}, {CRYPT_CIPHER_SM4_ECB, MODES_ECB_NewCtx}, {CRYPT_CIPHER_SM4_CTR, MODES_CTR_NewCtx}, {CRYPT_CIPHER_SM4_GCM, MODES_GCM_NewCtx}, {CRYPT_CIPHER_SM4_CFB, MODES_CFB_NewCtx}, {CRYPT_CIPHER_SM4_OFB, MODES_OFB_NewCtx}, }; for (size_t i = 0; i < sizeof(cipherNewCtxFunc)/sizeof(cipherNewCtxFunc[0]); i++) { if (cipherNewCtxFunc[i].id == algId) { return ((CipherNewCtx)cipherNewCtxFunc[i].func)(algId); } } BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } const CRYPT_EAL_Func g_smCbc[] = { #ifdef HITLS_CRYPTO_CBC {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_SmCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CBC_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CBC_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CBC_FinalEx}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CBC_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CBC_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CBC_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smCfb[] = { #ifdef HITLS_CRYPTO_CFB {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_SmCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CFB_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CFB_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CFB_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CFB_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CFB_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CFB_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smCtr[] = { #ifdef HITLS_CRYPTO_CTR {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_SmCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CTR_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CTR_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CTR_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CTR_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CTR_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CTR_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smEcb[] = { #ifdef HITLS_CRYPTO_ECB {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_SmCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_ECB_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_ECB_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_ECB_FinalEx}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_ECB_DeinitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_ECB_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_ECB_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smGcm[] = { #ifdef HITLS_CRYPTO_GCM {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_SmCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_GCM_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_GCM_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_GCM_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_GCM_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_GCM_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_GCM_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smOfb[] = { #ifdef HITLS_CRYPTO_OFB {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_SmCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_OFB_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_OFB_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_OFB_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_OFB_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_OFB_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_OFB_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smXts[] = { #ifdef HITLS_CRYPTO_XTS {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_SmCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_XTS_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_XTS_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_XTS_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_XTS_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_XTS_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_XTS_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_cipher.c
C
unknown
6,701
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_cmvp_selftest.h" #include "cmvp_sm.h" #include "crypt_params_key.h" #include "crypt_sm_provider.h" #define SM_PROVIDER_VERSION "openHiTLS SM Provider Version : V0.3.0" typedef struct { void *provCtx; void *libCtx; } SmSelftestCtx; static SmSelftestCtx *CRYPT_Selftest_NewCtx(CRYPT_EAL_SmProvCtx *provCtx) { if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } SmSelftestCtx *ctx = (SmSelftestCtx *)BSL_SAL_Calloc(1, sizeof(SmSelftestCtx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } ctx->provCtx = provCtx; ctx->libCtx = provCtx->libCtx; return ctx; } static const char *CRYPT_Selftest_GetVersion(SmSelftestCtx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } return SM_PROVIDER_VERSION; } static int32_t CRYPT_Selftest_Selftest(SmSelftestCtx *ctx, const BSL_Param *param) { int32_t type = 0; uint32_t len = sizeof(type); if (ctx == NULL || param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } const BSL_Param *temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_CMVP_SELFTEST_TYPE); if (temp == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_CMVP_SELFTEST_TYPE, BSL_PARAM_TYPE_INT32, &type, &len); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } switch (type) { case CRYPT_CMVP_INTEGRITY_TEST: ret = CMVP_SmCheckIntegrity(ctx->libCtx, CRYPT_EAL_SM_ATTR); break; case CRYPT_CMVP_KAT_TEST: ret = CMVP_SmKat(ctx->libCtx, CRYPT_EAL_SM_ATTR); break; case CRYPT_CMVP_RANDOMNESS_TEST: if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_CMVP_RANDOM)) == NULL || temp->valueType != BSL_PARAM_TYPE_OCTETS || temp->value == NULL || temp->valueLen == 0) { ret = CRYPT_INVALID_ARG; break; } ret = CRYPT_CMVP_RandomnessTest(temp->value, temp->valueLen); break; default: BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static void CRYPT_Selftest_FreeCtx(SmSelftestCtx *ctx) { if (ctx == NULL) { return; } BSL_SAL_Free(ctx); } const CRYPT_EAL_Func g_smSelftest[] = { {CRYPT_EAL_IMPLSELFTEST_NEWCTX, (CRYPT_EAL_ImplSelftestNewCtx)CRYPT_Selftest_NewCtx}, {CRYPT_EAL_IMPLSELFTEST_GETVERSION, (CRYPT_EAL_ImplSelftestGetVersion)CRYPT_Selftest_GetVersion}, {CRYPT_EAL_IMPLSELFTEST_SELFTEST, (CRYPT_EAL_ImplSelftestSelftest)CRYPT_Selftest_Selftest}, {CRYPT_EAL_IMPLSELFTEST_FREECTX, (CRYPT_EAL_ImplSelftestFreeCtx)CRYPT_Selftest_FreeCtx}, CRYPT_EAL_FUNC_END }; #endif // HITLS_CRYPTO_CMVP_SM
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_cmvp.c
C
unknown
3,745
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #if defined(HITLS_CRYPTO_CODECSKEY) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_eal_pkey.h" #include "crypt_decode_key_impl.h" const CRYPT_EAL_Func g_smEalPrvP8Enc2P8[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_EPKI2PKI_NewCtx}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_EPKI2PKI_SetParam}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_EPKI2PKI_GetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_EPKI2PKI_Decode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_EPKI2PKI_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_EPKI2PKI_FreeCtx}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smEalPem2Der[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Pem2DerNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_Pem2DerGetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_Pem2DerSetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Pem2DerDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_Pem2DerFreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_Pem2DerFreeCtx}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smEalSm2PrvDer2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2PrvKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smEalP8Der2Sm2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2Pkcs8Der2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smEalSubPubKeyDer2Sm2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2SubPubKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smEalSubPubKeyWithoutSeqDer2Sm2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2SubPubKeyWithOutSeqDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smEalLowKeyObject2PkeyObject[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_LowKeyObject2PkeyObjectNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_LowKeyObject2PkeyObjectGetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_LowKeyObject2PkeyObjectSetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_LowKeyObject2PkeyObjectDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_LowKeyObject2PkeyObjectFreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_LowKeyObject2PkeyObjectFreeCtx}, CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_PROVIDER */ #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_decode.c
C
unknown
5,274
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "crypt_pbkdf2.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "crypt_cmvp.h" #include "cmvp_sm.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" /* Constants for parameter validation */ #define KDF_DEF_MAC_ALGID CRYPT_MAC_HMAC_SM3 #define KDF_DEF_SALT_LEN 16 #define KDF_DEF_PBKDF2_ITER 1024 void *CRYPT_EAL_SmKdfNewCtxEx(CRYPT_EAL_SmProvCtx *provCtx, int32_t algId) { if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } switch (algId) { #ifdef HITLS_CRYPTO_PBKDF2 case CRYPT_KDF_PBKDF2: return CRYPT_PBKDF2_NewCtxEx(provCtx->libCtx); #endif default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } } static int32_t GetPbkdf2Params(const BSL_Param *param, CRYPT_EAL_Pbkdf2Param *pbkdf2Param) { int32_t id; uint32_t iter; uint32_t len; const BSL_Param *temp = NULL; if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_SALT)) != NULL) { pbkdf2Param->saltLen = temp->valueLen; } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_ITER)) != NULL) { len = sizeof(iter); int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_ITER, BSL_PARAM_TYPE_UINT32, &iter, &len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } pbkdf2Param->iter = iter; } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_MAC_ID)) != NULL) { len = sizeof(id); int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_MAC_ID, BSL_PARAM_TYPE_UINT32, &id, &len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } pbkdf2Param->macId = (CRYPT_MAC_AlgId)id; } return CRYPT_SUCCESS; } static int32_t CheckKdfParam(const BSL_Param *param) { int32_t ret = CRYPT_SUCCESS; CRYPT_EAL_Pbkdf2Param pbkdf2 = {KDF_DEF_MAC_ALGID, KDF_DEF_SALT_LEN, KDF_DEF_PBKDF2_ITER, 0}; CRYPT_EAL_KdfC2Data data = {&pbkdf2, NULL}; ret = GetPbkdf2Params(param, &pbkdf2); if (ret != CRYPT_SUCCESS) { return ret; } if (!CMVP_SmKdfC2(&data)) { BSL_ERR_PUSH_ERROR(CRYPT_CMVP_ERR_PARAM_CHECK); return CRYPT_CMVP_ERR_PARAM_CHECK; } return ret; } static int32_t CRYPT_PBKDF2_SetParamWrapper(CRYPT_PBKDF2_Ctx *ctx, const BSL_Param *param) { if (ctx == NULL || param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CheckKdfParam(param); if (ret != CRYPT_SUCCESS) { return ret; } return CRYPT_PBKDF2_SetParam(ctx, param); } const CRYPT_EAL_Func g_smKdfPBKdf2[] = { #ifdef HITLS_CRYPTO_PBKDF2 {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_EAL_SmKdfNewCtxEx}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_PBKDF2_SetParamWrapper}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_PBKDF2_Derive}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_PBKDF2_Deinit}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_PBKDF2_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_kdf.c
C
unknown
3,897
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "crypt_sm2.h" const CRYPT_EAL_Func g_smExchSm2[] = { #if defined(HITLS_CRYPTO_SM2_EXCH) {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_SM2_KapComputeKey}, #endif CRYPT_EAL_FUNC_END }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_keyexch.c
C
unknown
883
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #ifdef HITLS_CRYPTO_SM2 #include "crypt_sm2.h" #endif #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "cmvp_sm.h" #include "crypt_sm_provider.h" void *CRYPT_EAL_SmPkeyMgmtNewCtx(CRYPT_EAL_SmProvCtx *provCtx, int32_t algId) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Pkey(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } switch (algId) { #ifdef HITLS_CRYPTO_SM2 case CRYPT_PKEY_SM2: return CRYPT_SM2_NewCtxEx(provCtx->libCtx); #endif default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } }; static int32_t CRYPT_SM2_GenWrapper(CRYPT_SM2_Ctx *ctx) { int32_t ret = CRYPT_SM2_Gen(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (!CMVP_SmPkeyPct(ctx, CRYPT_PKEY_SM2)) { return CRYPT_CMVP_ERR_PAIRWISETEST; } return CRYPT_SUCCESS; } const CRYPT_EAL_Func g_smKeyMgmtSm2[] = { #ifdef HITLS_CRYPTO_SM2 {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_SmPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_SM2_GenWrapper}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_SM2_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_SM2_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_SM2_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_SM2_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_SM2_DupCtx}, {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_SM2_Check}, {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_SM2_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_SM2_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_SM2_FreeCtx}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_SM2_Import}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_SM2_Export}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_keymgmt.c
C
unknown
3,040
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "crypt_sm_provider.h" #include "crypt_hmac.h" #include "crypt_cbc_mac.h" #include "crypt_ealinit.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" void *CRYPT_EAL_SmMacNewCtxEx(CRYPT_EAL_SmProvCtx *provCtx, int32_t algId) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Mac(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } switch (algId) { #ifdef HITLS_CRYPTO_HMAC case CRYPT_MAC_HMAC_SM3: return CRYPT_HMAC_NewCtxEx(provCtx->libCtx, algId); #endif #ifdef HITLS_CRYPTO_CBC_MAC case CRYPT_MAC_CBC_MAC_SM4: return CRYPT_CBC_MAC_NewCtxEx(provCtx->libCtx, algId); #endif default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } } const CRYPT_EAL_Func g_smMacHmac[] = { #ifdef HITLS_CRYPTO_HMAC {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_EAL_SmMacNewCtxEx}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_HMAC_Init}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_HMAC_Update}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_HMAC_Final}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_HMAC_Deinit}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_HMAC_Reinit}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_HMAC_Ctrl}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_HMAC_FreeCtx}, {CRYPT_EAL_IMPLMAC_SETPARAM, (CRYPT_EAL_ImplMacSetParam)CRYPT_HMAC_SetParam}, #endif CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_smMacCbcMac[] = { #ifdef HITLS_CRYPTO_CBC_MAC {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_EAL_SmMacNewCtxEx}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_CBC_MAC_Init}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_CBC_MAC_Update}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_CBC_MAC_Final}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_CBC_MAC_Deinit}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_CBC_MAC_Reinit}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_CBC_MAC_Ctrl}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_CBC_MAC_FreeCtx}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_mac.c
C
unknown
3,170
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "crypt_sm_provider.h" #include "crypt_sm3.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" static void *CRYPT_EAL_SmMdNewCtxEx(CRYPT_EAL_SmProvCtx *provCtx, int32_t algId) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Md(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } switch (algId) { #ifdef HITLS_CRYPTO_SM3 case CRYPT_MD_SM3: return CRYPT_SM3_NewCtxEx(provCtx->libCtx, algId); #endif default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } } const CRYPT_EAL_Func g_smMdSm3[] = { #ifdef HITLS_CRYPTO_SM3 {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_SmMdNewCtxEx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SM3_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SM3_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SM3_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SM3_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SM3_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SM3_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SM3_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SM3_GetParam}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_md.c
C
unknown
2,240
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "crypt_sm2.h" const CRYPT_EAL_Func g_smAsymCipherSm2[] = { #ifdef HITLS_CRYPTO_SM2_CRYPT {CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT, (CRYPT_EAL_ImplPkeyEncrypt)CRYPT_SM2_Encrypt}, {CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT, (CRYPT_EAL_ImplPkeyDecrypt)CRYPT_SM2_Decrypt}, #endif CRYPT_EAL_FUNC_END }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_pkeycipher.c
C
unknown
972
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief sm provider impl */ #ifndef CRYPT_EAL_SM_PROVIDERIMPL_H #define CRYPT_EAL_SM_PROVIDERIMPL_H #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ typedef struct { int32_t algId; void *ctx; void *provCtx; } CRYPT_Sm_Pkey_Ctx; extern const CRYPT_EAL_Func g_smMdSm3[]; extern const CRYPT_EAL_Func g_smKdfPBKdf2[]; extern const CRYPT_EAL_Func g_smKeyMgmtSm2[]; extern const CRYPT_EAL_Func g_smExchSm2[]; extern const CRYPT_EAL_Func g_smAsymCipherSm2[]; extern const CRYPT_EAL_Func g_smSignSm2[]; extern const CRYPT_EAL_Func g_smMacHmac[]; extern const CRYPT_EAL_Func g_smMacCbcMac[]; extern const CRYPT_EAL_Func g_smRand[]; extern const CRYPT_EAL_Func g_smCbc[]; extern const CRYPT_EAL_Func g_smCfb[]; extern const CRYPT_EAL_Func g_smCtr[]; extern const CRYPT_EAL_Func g_smEcb[]; extern const CRYPT_EAL_Func g_smGcm[]; extern const CRYPT_EAL_Func g_smOfb[]; extern const CRYPT_EAL_Func g_smXts[]; extern const CRYPT_EAL_Func g_smSelftest[]; #ifdef HITLS_CRYPTO_CODECSKEY extern const CRYPT_EAL_Func g_smEalPrvP8Enc2P8[]; extern const CRYPT_EAL_Func g_smEalPem2Der[]; extern const CRYPT_EAL_Func g_smEalSm2PrvDer2Key[]; extern const CRYPT_EAL_Func g_smEalP8Der2Sm2Key[]; extern const CRYPT_EAL_Func g_smEalSubPubKeyDer2Sm2Key[]; extern const CRYPT_EAL_Func g_smEalSubPubKeyWithoutSeqDer2Sm2Key[]; extern const CRYPT_EAL_Func g_smEalLowKeyObject2PkeyObject[]; #endif // HITLS_CRYPTO_CODECSKEY #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_SM */ #endif /* CRYPT_EAL_SM_PROVIDERIMPL_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_provderimpl.h
C
unknown
2,214
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include <stdint.h> #include <string.h> #include "bsl_errno.h" #include "bsl_params.h" #include "crypt_algid.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "crypt_eal_rand.h" #include "entropy_seed_pool.h" #include "crypt_sm_selftest.h" #include "crypt_sm_provderimpl.h" #include "crypt_sm_provider.h" #define CRYPT_ENTROPY_SOURCE_ENTROPY 8 #define CRYPT_ENTROPY_SEED_POOL_SIZE 4096 static const CRYPT_EAL_AlgInfo g_smMds[] = { {CRYPT_MD_SM3, g_smMdSm3, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smKdfs[] = { {CRYPT_KDF_PBKDF2, g_smKdfPBKdf2, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smKeyMgmt[] = { {CRYPT_PKEY_SM2, g_smKeyMgmtSm2, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smAsymCiphers[] = { {CRYPT_PKEY_SM2, g_smAsymCipherSm2, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smKeyExch[] = { {CRYPT_PKEY_SM2, g_smExchSm2, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smSigns[] = { {CRYPT_PKEY_SM2, g_smSignSm2, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smMacs[] = { {CRYPT_MAC_HMAC_SM3, g_smMacHmac, CRYPT_EAL_SM_ATTR}, {CRYPT_MAC_CBC_MAC_SM4, g_smMacCbcMac, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smRands[] = { {CRYPT_RAND_SM3, g_smRand, CRYPT_EAL_SM_ATTR}, {CRYPT_RAND_SM4_CTR_DF, g_smRand, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smCiphers[] = { {CRYPT_CIPHER_SM4_XTS, g_smXts, CRYPT_EAL_SM_ATTR}, {CRYPT_CIPHER_SM4_CBC, g_smCbc, CRYPT_EAL_SM_ATTR}, {CRYPT_CIPHER_SM4_ECB, g_smEcb, CRYPT_EAL_SM_ATTR}, {CRYPT_CIPHER_SM4_CTR, g_smCtr, CRYPT_EAL_SM_ATTR}, {CRYPT_CIPHER_SM4_GCM, g_smGcm, CRYPT_EAL_SM_ATTR}, {CRYPT_CIPHER_SM4_CFB, g_smCfb, CRYPT_EAL_SM_ATTR}, {CRYPT_CIPHER_SM4_OFB, g_smOfb, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; static const CRYPT_EAL_AlgInfo g_smSelftests[] = { {CRYPT_CMVP_PROVIDER_SELFTEST, g_smSelftest, CRYPT_EAL_SM_ATTR}, CRYPT_EAL_ALGINFO_END }; #ifdef HITLS_CRYPTO_CODECSKEY static const CRYPT_EAL_AlgInfo g_smEalDecoders[] = { {BSL_CID_DECODE_UNKNOWN, g_smEalPem2Der, "provider=sm, inFormat=PEM, outFormat=ASN1"}, {BSL_CID_DECODE_UNKNOWN, g_smEalPrvP8Enc2P8, "provider=sm, inFormat=ASN1, inType=PRIKEY_PKCS8_ENCRYPT, outFormat=ASN1, outType=PRIKEY_PKCS8_UNENCRYPT"}, {CRYPT_PKEY_SM2, g_smEalSm2PrvDer2Key, "provider=sm, inFormat=ASN1, inType=PRIKEY_ECC, outFormat=OBJECT, outType=LOW_KEY"}, {CRYPT_PKEY_SM2, g_smEalP8Der2Sm2Key, "provider=sm, inFormat=ASN1, inType=PRIKEY_PKCS8_UNENCRYPT, outFormat=OBJECT, outType=LOW_KEY"}, {CRYPT_PKEY_SM2, g_smEalSubPubKeyDer2Sm2Key, "provider=sm, inFormat=ASN1, inType=PUBKEY_SUBKEY, outFormat=OBJECT, outType=LOW_KEY"}, {CRYPT_PKEY_SM2, g_smEalSubPubKeyWithoutSeqDer2Sm2Key, "provider=sm, inFormat=ASN1, inType=PUBKEY_SUBKEY_WITHOUT_SEQ, outFormat=OBJECT, outType=LOW_KEY"}, {BSL_CID_DECODE_UNKNOWN, g_smEalLowKeyObject2PkeyObject, "provider=sm, inFormat=OBJECT, inType=LOW_KEY, outFormat=OBJECT, outType=HIGH_KEY"}, CRYPT_EAL_ALGINFO_END }; #endif // HITLS_CRYPTO_CODECSKEY static int32_t CRYPT_EAL_SmProvQuery(void *provCtx, int32_t operaId, const CRYPT_EAL_AlgInfo **algInfos) { (void)provCtx; int32_t ret = CRYPT_SUCCESS; switch (operaId) { case CRYPT_EAL_OPERAID_SYMMCIPHER: *algInfos = g_smCiphers; break; case CRYPT_EAL_OPERAID_KEYMGMT: *algInfos = g_smKeyMgmt; break; case CRYPT_EAL_OPERAID_SIGN: *algInfos = g_smSigns; break; case CRYPT_EAL_OPERAID_ASYMCIPHER: *algInfos = g_smAsymCiphers; break; case CRYPT_EAL_OPERAID_KEYEXCH: *algInfos = g_smKeyExch; break; case CRYPT_EAL_OPERAID_HASH: *algInfos = g_smMds; break; case CRYPT_EAL_OPERAID_MAC: *algInfos = g_smMacs; break; case CRYPT_EAL_OPERAID_KDF: *algInfos = g_smKdfs; break; case CRYPT_EAL_OPERAID_RAND: *algInfos = g_smRands; break; case CRYPT_EAL_OPERAID_SELFTEST: *algInfos = g_smSelftests; break; #ifdef HITLS_CRYPTO_CODECSKEY case CRYPT_EAL_OPERAID_DECODER: *algInfos = g_smEalDecoders; break; #endif default: *algInfos = NULL; ret = CRYPT_NOT_SUPPORT; break; } return ret; } static void CRYPT_EAL_SmProvFree(void *provCtx) { if (provCtx == NULL) { return; } CRYPT_EAL_SmProvCtx *temp = (CRYPT_EAL_SmProvCtx *)provCtx; CRYPT_EAL_SeedPoolFree(temp->pool); CRYPT_EAL_EsFree(temp->es); BSL_SAL_Free(provCtx); } static CRYPT_EAL_Func g_smProvOutFuncs[] = { {CRYPT_EAL_PROVCB_QUERY, CRYPT_EAL_SmProvQuery}, {CRYPT_EAL_PROVCB_FREE, CRYPT_EAL_SmProvFree}, {CRYPT_EAL_PROVCB_CTRL, NULL}, {CRYPT_EAL_PROVCB_GETCAPS, NULL}, CRYPT_EAL_FUNC_END }; static int32_t ReadDevRandom(void *ctx, uint32_t timeout, uint8_t *buf, uint32_t bufLen) { (void)ctx; (void)timeout; if (ENTROPY_SysEntropyGet(NULL, buf, bufLen) != bufLen) { return CRYPT_DRBG_FAIL_GET_ENTROPY; } return CRYPT_SUCCESS; } static int32_t CreateSmEs(CRYPT_EAL_Es **es) { int32_t ret = CRYPT_SUCCESS; CRYPT_EAL_Es *esTemp = CRYPT_EAL_EsNew(); RETURN_RET_IF(esTemp == NULL, CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_SET_CF, "sm3_df", (uint32_t)strlen("sm3_df")); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); CRYPT_EAL_NsPara para = { "dev-random", false, 5, {NULL, NULL, ReadDevRandom, NULL}, {5, 39, 512}, }; ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_ADD_NS, (void *)&para, sizeof(CRYPT_EAL_NsPara)); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); bool healthTest = true; ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_ENABLE_TEST, &healthTest, sizeof(healthTest)); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); uint32_t size = CRYPT_ENTROPY_SEED_POOL_SIZE; ret = CRYPT_EAL_EsCtrl(esTemp, CRYPT_ENTROPY_SET_POOL_SIZE, &size, sizeof(size)); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); ret = CRYPT_EAL_EsInit(esTemp); GOTO_ERR_IF_TRUE(ret != CRYPT_SUCCESS, ret); *es = esTemp; return ret; ERR: CRYPT_EAL_EsFree(esTemp); return ret; } static int32_t CreateSeedPool(CRYPT_EAL_SeedPoolCtx **seedPool, CRYPT_EAL_Es **es) { CRYPT_EAL_SeedPoolCtx *poolTemp = NULL; CRYPT_EAL_Es *esTemp = NULL; int32_t ret = CreateSmEs(&esTemp); if (ret != CRYPT_SUCCESS) { return ret; } poolTemp = CRYPT_EAL_SeedPoolNew(true); if (poolTemp == NULL) { CRYPT_EAL_EsFree(esTemp); BSL_ERR_PUSH_ERROR(CRYPT_SEED_POOL_NEW_ERROR); return CRYPT_SEED_POOL_NEW_ERROR; } CRYPT_EAL_EsPara para = {false, CRYPT_ENTROPY_SOURCE_ENTROPY, esTemp, (CRYPT_EAL_EntropyGet)CRYPT_EAL_EsEntropyGet}; ret = CRYPT_EAL_SeedPoolAddEs(poolTemp, &para); if (ret != CRYPT_SUCCESS) { CRYPT_EAL_SeedPoolFree(poolTemp); CRYPT_EAL_EsFree(esTemp); return ret; } *seedPool = poolTemp; *es = esTemp; return CRYPT_SUCCESS; } static int32_t CreateProvCtx(void *libCtx, CRYPT_EAL_ProvMgrCtx *mgrCtx, void **provCtx) { CRYPT_EAL_SmProvCtx *temp = BSL_SAL_Calloc(1, sizeof(CRYPT_EAL_SmProvCtx)); if (temp == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } CRYPT_EAL_SetRandCallBackEx((CRYPT_EAL_RandFuncEx)CRYPT_EAL_RandbytesEx); int32_t ret = CreateSeedPool(&temp->pool, &temp->es); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(temp); return ret; } temp->libCtx = libCtx; temp->mgrCtx = mgrCtx; *provCtx = temp; return CRYPT_SUCCESS; } int32_t CRYPT_EAL_ProviderInit(CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, CRYPT_EAL_Func *capFuncs, CRYPT_EAL_Func **outFuncs, void **provCtx) { void *libCtx = NULL; CRYPT_EAL_ProvMgrCtrlCb mgrCtrl = NULL; int32_t index = 0; int32_t ret; while (capFuncs[index].id != 0) { switch (capFuncs[index].id) { case CRYPT_EAL_CAP_MGRCTXCTRL: mgrCtrl = capFuncs[index].func; break; default: break; } index++; } if (mgrCtrl == NULL) { return CRYPT_PROVIDER_NOT_SUPPORT; } ret = mgrCtrl(mgrCtx, CRYPT_EAL_MGR_GETLIBCTX, &libCtx, 0); if (ret != CRYPT_SUCCESS) { return ret; } ret = CRYPT_SM_Selftest(param); if (ret != CRYPT_SUCCESS) { return ret; } ret = CreateProvCtx(libCtx, mgrCtx, provCtx); if (ret != CRYPT_SUCCESS) { return ret; } *outFuncs = g_smProvOutFuncs; return CRYPT_SUCCESS; } #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_provider.c
C
unknown
9,780
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief sm provider header */ #ifndef CRYPT_EAL_SM_PROVIDER_H #define CRYPT_EAL_SM_PROVIDER_H #ifdef HITLS_CRYPTO_CMVP_SM #include <stdint.h> #include "crypt_eal_entropy.h" #include "crypt_eal_implprovider.h" #include "crypt_eal_cmvp.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #define CRYPT_EAL_SM_ATTR "provider=sm" typedef struct EalSmProvCtx { void *libCtx; void *mgrCtx; CRYPT_EAL_Es *es; CRYPT_EAL_SeedPoolCtx *pool; } CRYPT_EAL_SmProvCtx; int32_t CRYPT_EAL_ProviderInit(CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, CRYPT_EAL_Func *capFuncs, CRYPT_EAL_Func **outFuncs, void **provCtx); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_SM */ #endif /* CRYPT_EAL_SM_PROVIDER_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_provider.h
C
unknown
1,357
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "securec.h" #include "crypt_eal_implprovider.h" #include "crypt_drbg.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "crypt_cmvp_selftest.h" #include "bsl_params.h" #include "eal_entropy.h" #include "crypt_sm_provider.h" #ifdef HITLS_CRYPTO_ENTROPY #define SM_RANDOM_MIN_LEN 32 typedef struct { void *ctx; int32_t isSelfTest; } SmRandCtx; static int32_t GetDefaultSeed(CRYPT_EAL_SmProvCtx *provCtx, BSL_Param *param) { CRYPT_RandSeedMethod seedMethod = {0}; int32_t ret = EAL_SetDefaultEntropyMeth(&seedMethod); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } int32_t index = 0; (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, provCtx->pool, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMethod.getEntropy, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR, seedMethod.cleanEntropy, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMethod.getNonce, 0); (void)BSL_PARAM_InitValue(&param[index++], CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, seedMethod.cleanNonce, 0); return CRYPT_SUCCESS; } #endif static void *CRYPT_EAL_SmRandNewCtx(CRYPT_EAL_SmProvCtx *provCtx, int32_t algId, BSL_Param *param) { void *randCtx = NULL; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Drbg(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif BSL_Param *getEnt = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_GETENTROPY); BSL_Param *cleanEnt = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_CLEANENTROPY); BSL_Param *getNonce = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_GETNONCE); BSL_Param *cleanNonce = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_CLEANNONCE); BSL_Param *ctx = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEEDCTX); /** * If you use a registered entropy source, the getEntropy callback cannot be NULL, * and if getEntropy is NULL, cleanEntropy, getNonce, cleanNonce, etc. must be NULL */ if (getEnt == NULL && ((cleanEnt != NULL && cleanEnt->value != NULL) || (getNonce != NULL && getNonce->value != NULL) || (cleanNonce != NULL && cleanNonce->value != NULL) || (ctx != NULL && ctx->value != NULL))) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } if (param == NULL || getEnt == NULL) { #ifdef HITLS_CRYPTO_ENTROPY BSL_Param defaultParam[6] = {BSL_PARAM_END}; if (GetDefaultSeed(provCtx, defaultParam) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } return DRBG_New(provCtx->libCtx, algId, defaultParam); #else BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; #endif } randCtx = DRBG_New(provCtx->libCtx, algId, param); if (randCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } return randCtx; } void *CRYPT_EAL_SmRandNewCtxWrapper(CRYPT_EAL_SmProvCtx *provCtx, int32_t algId, BSL_Param *param) { if (provCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } SmRandCtx *ctx = BSL_SAL_Calloc(1, sizeof(SmRandCtx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } void *randCtx = CRYPT_EAL_SmRandNewCtx(provCtx, algId, param); if (randCtx == NULL) { BSL_SAL_FREE(ctx); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } ctx->ctx = randCtx; ctx->isSelfTest = 1; return ctx; } static int32_t DRBG_InstantiateWrapper(SmRandCtx *ctx, const uint8_t *person, uint32_t persLen, BSL_Param *param) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return DRBG_Instantiate(ctx->ctx, person, persLen, param); } static int32_t DRBG_UninstantiateWrapper(SmRandCtx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return DRBG_Uninstantiate(ctx->ctx); } static int32_t GenerateBytesAndTest(void *ctx, uint8_t *out, uint32_t outLen, const uint8_t *adin, uint32_t adinLen, BSL_Param *param) { // 2: GM/T 0062-2018 Table 8, retry 2 times for (uint32_t attempt = 0; attempt < 2; attempt++) { int32_t ret = DRBG_GenerateBytes(ctx, out, outLen, adin, adinLen, param); if (ret != CRYPT_SUCCESS) { return ret; } ret = CRYPT_CMVP_RandomnessTest(out, outLen); if (ret != CRYPT_SUCCESS) { continue; } return CRYPT_SUCCESS; } return CRYPT_CMVP_RANDOMNESS_ERR; } static int32_t DRBG_GenerateBytesWrapper(SmRandCtx *ctx, uint8_t *out, uint32_t outLen, const uint8_t *adin, uint32_t adinLen, BSL_Param *param) { if (ctx == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->isSelfTest == 0) { return DRBG_GenerateBytes(ctx->ctx, out, outLen, adin, adinLen, param); } /* * To satisfy the minimum 256-bit test length requirement: * - If outLen < 256 bits, internally generate and test 256 bits (SM_RANDOM_MIN_LEN), * then copy only the requested length back to the caller upon success. * - If outLen >= 256 bits, test directly on the target output buffer. */ uint8_t randomData[SM_RANDOM_MIN_LEN] = {0}; uint32_t dataLen = outLen < SM_RANDOM_MIN_LEN ? SM_RANDOM_MIN_LEN : outLen; uint8_t *data = outLen < SM_RANDOM_MIN_LEN ? randomData : out; int32_t ret = GenerateBytesAndTest(ctx->ctx, data, dataLen, adin, adinLen, param); if (ret != CRYPT_SUCCESS) { return ret; } if (outLen < SM_RANDOM_MIN_LEN) { (void)memcpy_s(out, outLen, randomData, outLen); } return CRYPT_SUCCESS; } static int32_t DRBG_ReseedWrapper(SmRandCtx *ctx, const uint8_t *adin, uint32_t adinLen, BSL_Param *param) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return DRBG_Reseed(ctx->ctx, adin, adinLen, param); } static int32_t DRBG_CtrlWrapper(SmRandCtx *ctx, int32_t opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (opt == CRYPT_CTRL_SET_SELFTEST_FLAG) { if (val == NULL || len != sizeof(int32_t) || (*(int32_t*)val != 0 && *(int32_t*)val != 1)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->isSelfTest = *(int32_t*)val; return CRYPT_SUCCESS; } return DRBG_Ctrl(ctx->ctx, opt, val, len); } static void DRBG_FreeWrapper(SmRandCtx *ctx) { if (ctx == NULL) { return; } DRBG_Free(ctx->ctx); BSL_SAL_FREE(ctx); } const CRYPT_EAL_Func g_smRand[] = { #if defined(HITLS_CRYPTO_DRBG) {CRYPT_EAL_IMPLRAND_DRBGNEWCTX, (CRYPT_EAL_ImplRandDrbgNewCtx)CRYPT_EAL_SmRandNewCtxWrapper}, {CRYPT_EAL_IMPLRAND_DRBGINST, (CRYPT_EAL_ImplRandDrbgInst)DRBG_InstantiateWrapper}, {CRYPT_EAL_IMPLRAND_DRBGUNINST, (CRYPT_EAL_ImplRandDrbgUnInst)DRBG_UninstantiateWrapper}, {CRYPT_EAL_IMPLRAND_DRBGGEN, (CRYPT_EAL_ImplRandDrbgGen)DRBG_GenerateBytesWrapper}, {CRYPT_EAL_IMPLRAND_DRBGRESEED, (CRYPT_EAL_ImplRandDrbgReSeed)DRBG_ReseedWrapper}, {CRYPT_EAL_IMPLRAND_DRBGCTRL, (CRYPT_EAL_ImplRandDrbgCtrl)DRBG_CtrlWrapper}, {CRYPT_EAL_IMPLRAND_DRBGFREECTX, (CRYPT_EAL_ImplRandDrbgFreeCtx)DRBG_FreeWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_rand.c
C
unknown
8,579
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "securec.h" #include "crypt_types.h" #include "crypt_errno.h" #include "bsl_params.h" #include "cmvp_sm.h" #include "cmvp_common.h" #include "crypt_sm_selftest.h" int32_t CRYPT_SM_Selftest(BSL_Param *param) { CRYPT_EAL_LibCtx *libCtx = NULL; if (CMVP_CheckIsInternalLibCtx(param)) { return CRYPT_SUCCESS; } int32_t ret = CMVP_CreateInternalLibCtx(param, &libCtx, CRYPT_SM_Selftest); if (ret != CRYPT_SUCCESS) { return ret; } ret = CMVP_SmCheckIntegrity(libCtx, CRYPT_EAL_SM_ATTR); if (ret != CRYPT_SUCCESS) { CRYPT_EAL_LibCtxFree(libCtx); return ret; } ret = CMVP_SmKat(libCtx, CRYPT_EAL_SM_ATTR); if (ret != CRYPT_SUCCESS) { CRYPT_EAL_LibCtxFree(libCtx); return ret; } ret = CMVP_SmRandomStartupSelftest(libCtx, CRYPT_EAL_SM_ATTR); CRYPT_EAL_LibCtxFree(libCtx); if (ret != CRYPT_SUCCESS) { return ret; } return CRYPT_SUCCESS; } #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_selftest.c
C
unknown
1,599
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief sm provider header */ #ifndef CRYPT_EAL_SM_SELFTEST_H #define CRYPT_EAL_SM_SELFTEST_H #ifdef HITLS_CRYPTO_CMVP_SM #include <stdint.h> #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ int32_t CRYPT_SM_Selftest(BSL_Param *param); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* HITLS_CRYPTO_CMVP_SM */ #endif /* CRYPT_EAL_SM_SELFTEST_H */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_selftest.h
C
unknown
992
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_CMVP_SM #include "crypt_eal_implprovider.h" #include "crypt_sm2.h" #include "crypt_errno.h" #include "cmvp_sm.h" #include "crypt_cmvp.h" static int32_t CRYPT_SM2_SignWrapper(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { if (!CMVP_SmPkeyC2(algId)) { return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SM2_Sign(ctx, algId, data, dataLen, sign, signLen); } static int32_t CRYPT_SM2_VerifyWrapper(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen) { if (!CMVP_SmPkeyC2(algId)) { return CRYPT_CMVP_ERR_PARAM_CHECK; } return CRYPT_SM2_Verify(ctx, algId, data, dataLen, sign, signLen); } const CRYPT_EAL_Func g_smSignSm2[] = { #ifdef HITLS_CRYPTO_SM2_SIGN {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_SM2_SignWrapper}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_SM2_VerifyWrapper}, #endif CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CMVP_SM */
2301_79861745/bench_create
crypto/provider/src/cmvp/sm_prov/crypt_sm_sign.c
C
unknown
1,665
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_AES) || defined(HITLS_CRYPTO_SM4) || defined(HITLS_CRYPTO_CHACHA20) || \ defined(HITLS_CRYPTO_CHACHA20POLY1305) || defined(HITLS_CRYPTO_XTS) || defined(HITLS_CRYPTO_CBC) || \ defined(HITLS_CRYPTO_CCM) || defined(HITLS_CRYPTO_CFB) || defined(HITLS_CRYPTO_ECB) || \ defined(HITLS_CRYPTO_GCM) || defined(HITLS_CRYPTO_OFB) || defined(HITLS_CRYPTO_CTR)) && \ defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_modes_cbc.h" #include "crypt_modes_ccm.h" #include "crypt_modes_chacha20poly1305.h" #include "crypt_modes_ctr.h" #include "crypt_modes_ecb.h" #include "crypt_modes_gcm.h" #include "crypt_modes_ofb.h" #include "crypt_modes_cfb.h" #include "crypt_modes_xts.h" #include "crypt_local_types.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" static void *CRYPT_EAL_DefCipherNewCtx(void *provCtx, int32_t algId) { (void) provCtx; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Cipher(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif CRYPT_EAL_Func cipherNewCtxFunc[] = { {CRYPT_CIPHER_AES128_CBC, MODES_CBC_NewCtx}, {CRYPT_CIPHER_AES192_CBC, MODES_CBC_NewCtx}, {CRYPT_CIPHER_AES256_CBC, MODES_CBC_NewCtx}, {CRYPT_CIPHER_AES128_CTR, MODES_CTR_NewCtx}, {CRYPT_CIPHER_AES192_CTR, MODES_CTR_NewCtx}, {CRYPT_CIPHER_AES256_CTR, MODES_CTR_NewCtx}, {CRYPT_CIPHER_AES128_ECB, MODES_ECB_NewCtx}, {CRYPT_CIPHER_AES192_ECB, MODES_ECB_NewCtx}, {CRYPT_CIPHER_AES256_ECB, MODES_ECB_NewCtx}, #ifdef HITLS_CRYPTO_CCM {CRYPT_CIPHER_AES128_CCM, MODES_CCM_NewCtx}, {CRYPT_CIPHER_AES192_CCM, MODES_CCM_NewCtx}, {CRYPT_CIPHER_AES256_CCM, MODES_CCM_NewCtx}, #endif {CRYPT_CIPHER_AES128_GCM, MODES_GCM_NewCtx}, {CRYPT_CIPHER_AES192_GCM, MODES_GCM_NewCtx}, {CRYPT_CIPHER_AES256_GCM, MODES_GCM_NewCtx}, {CRYPT_CIPHER_AES128_CFB, MODES_CFB_NewCtx}, {CRYPT_CIPHER_AES192_CFB, MODES_CFB_NewCtx}, {CRYPT_CIPHER_AES256_CFB, MODES_CFB_NewCtx}, {CRYPT_CIPHER_AES128_OFB, MODES_OFB_NewCtx}, {CRYPT_CIPHER_AES192_OFB, MODES_OFB_NewCtx}, {CRYPT_CIPHER_AES256_OFB, MODES_OFB_NewCtx}, {CRYPT_CIPHER_AES128_XTS, MODES_XTS_NewCtx}, {CRYPT_CIPHER_AES256_XTS, MODES_XTS_NewCtx}, #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) {CRYPT_CIPHER_CHACHA20_POLY1305, MODES_CHACHA20POLY1305_NewCtx}, #endif {CRYPT_CIPHER_SM4_XTS, MODES_XTS_NewCtx}, {CRYPT_CIPHER_SM4_CBC, MODES_CBC_NewCtx}, {CRYPT_CIPHER_SM4_ECB, MODES_ECB_NewCtx}, {CRYPT_CIPHER_SM4_CTR, MODES_CTR_NewCtx}, {CRYPT_CIPHER_SM4_GCM, MODES_GCM_NewCtx}, {CRYPT_CIPHER_SM4_CFB, MODES_CFB_NewCtx}, {CRYPT_CIPHER_SM4_OFB, MODES_OFB_NewCtx}, }; for (size_t i = 0; i < sizeof(cipherNewCtxFunc)/sizeof(cipherNewCtxFunc[0]); i++) { if (cipherNewCtxFunc[i].id == algId) { return ((CipherNewCtx)cipherNewCtxFunc[i].func)(algId); } } return NULL; } #ifdef HITLS_CRYPTO_CBC const CRYPT_EAL_Func g_defEalCbc[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CBC_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CBC_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CBC_FinalEx}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CBC_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CBC_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CBC_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_CCM const CRYPT_EAL_Func g_defEalCcm[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CCM_InitCtx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CCM_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CCM_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CCM_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CCM_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CCM_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_CFB const CRYPT_EAL_Func g_defEalCfb[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CFB_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CFB_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CFB_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CFB_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CFB_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CFB_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) const CRYPT_EAL_Func g_defEalChaCha[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CHACHA20POLY1305_InitCtx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CHACHA20POLY1305_Update}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CHACHA20POLY1305_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CHACHA20POLY1305_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CHACHA20POLY1305_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CHACHA20POLY1305_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_CTR const CRYPT_EAL_Func g_defEalCtr[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_CTR_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_CTR_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_CTR_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_CTR_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_CTR_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_CTR_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECB const CRYPT_EAL_Func g_defEalEcb[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_ECB_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_ECB_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_ECB_FinalEx}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_ECB_DeinitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_ECB_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_ECB_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_GCM const CRYPT_EAL_Func g_defEalGcm[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_GCM_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_GCM_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_GCM_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_GCM_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_GCM_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_GCM_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_OFB const CRYPT_EAL_Func g_defEalOfb[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_OFB_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_OFB_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_OFB_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_OFB_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_OFB_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_OFB_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_XTS const CRYPT_EAL_Func g_defEalXts[] = { {CRYPT_EAL_IMPLCIPHER_NEWCTX, (CRYPT_EAL_ImplCipherNewCtx)CRYPT_EAL_DefCipherNewCtx}, {CRYPT_EAL_IMPLCIPHER_INITCTX, (CRYPT_EAL_ImplCipherInitCtx)MODES_XTS_InitCtxEx}, {CRYPT_EAL_IMPLCIPHER_UPDATE, (CRYPT_EAL_ImplCipherUpdate)MODES_XTS_UpdateEx}, {CRYPT_EAL_IMPLCIPHER_FINAL, (CRYPT_EAL_ImplCipherFinal)MODES_XTS_Final}, {CRYPT_EAL_IMPLCIPHER_DEINITCTX, (CRYPT_EAL_ImplCipherDeinitCtx)MODES_XTS_DeInitCtx}, {CRYPT_EAL_IMPLCIPHER_CTRL, (CRYPT_EAL_ImplCipherCtrl)MODES_XTS_Ctrl}, {CRYPT_EAL_IMPLCIPHER_FREECTX, (CRYPT_EAL_ImplCipherFreeCtx)MODES_XTS_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_cipher.c
C
unknown
10,031
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CODECSKEY) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_eal_pkey.h" #include "crypt_decode_key_impl.h" #ifdef HITLS_CRYPTO_KEY_EPKI const CRYPT_EAL_Func g_defEalPrvP8Enc2P8[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_EPKI2PKI_NewCtx}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_EPKI2PKI_SetParam}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_EPKI2PKI_GetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_EPKI2PKI_Decode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_EPKI2PKI_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_EPKI2PKI_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_BSL_PEM const CRYPT_EAL_Func g_defEalPem2Der[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Pem2DerNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_Pem2DerGetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_Pem2DerSetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Pem2DerDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_Pem2DerFreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_Pem2DerFreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_RSA const CRYPT_EAL_Func g_defEalRsaPrvDer2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_RsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_RsaPrvKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_RSA const CRYPT_EAL_Func g_defEalRsaPubDer2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_RsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_RsaPubKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECDSA const CRYPT_EAL_Func g_defEalEcdsaPrvDer2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_EcdsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_EcdsaPrvKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SM2 const CRYPT_EAL_Func g_defEalSm2PrvDer2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2PrvKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_RSA const CRYPT_EAL_Func g_defEalP8Der2RsaKey[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_RsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_RsaPkcs8Der2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECDSA const CRYPT_EAL_Func g_defEalP8Der2EcdsaKey[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_EcdsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_EcdsaPkcs8Der2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SM2 const CRYPT_EAL_Func g_defEalP8Der2Sm2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2Pkcs8Der2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ED25519 const CRYPT_EAL_Func g_defEalP8Der2Ed25519Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Ed25519Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Ed25519Pkcs8Der2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_RSA const CRYPT_EAL_Func g_defEalSubPubKeyDer2RsaKey[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_RsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_RsaSubPubKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECDSA const CRYPT_EAL_Func g_defEalSubPubKeyDer2EcdsaKey[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_EcdsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_EcdsaSubPubKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SM2 const CRYPT_EAL_Func g_defEalSubPubKeyDer2Sm2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2SubPubKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ED25519 const CRYPT_EAL_Func g_defEalSubPubKeyDer2Ed25519Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Ed25519Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Ed25519SubPubKeyDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_RSA const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2RsaKey[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_RsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_RsaSubPubKeyWithOutSeqDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECDSA const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2EcdsaKey[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_EcdsaDer2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_EcdsaSubPubKeyWithOutSeqDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SM2 const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2Sm2Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Sm2Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Sm2SubPubKeyWithOutSeqDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ED25519 const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2Ed25519Key[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_Ed25519Der2KeyNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_DER2KEY_GetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_DER2KEY_SetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_Ed25519SubPubKeyWithOutSeqDer2KeyDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_DER2KEY_FreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_DER2KEY_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif const CRYPT_EAL_Func g_defEalLowKeyObject2PkeyObject[] = { {CRYPT_DECODER_IMPL_NEWCTX, (CRYPT_DECODER_IMPL_NewCtx)DECODER_LowKeyObject2PkeyObjectNewCtx}, {CRYPT_DECODER_IMPL_GETPARAM, (CRYPT_DECODER_IMPL_GetParam)DECODER_LowKeyObject2PkeyObjectGetParam}, {CRYPT_DECODER_IMPL_SETPARAM, (CRYPT_DECODER_IMPL_SetParam)DECODER_LowKeyObject2PkeyObjectSetParam}, {CRYPT_DECODER_IMPL_DECODE, (CRYPT_DECODER_IMPL_Decode)DECODER_LowKeyObject2PkeyObjectDecode}, {CRYPT_DECODER_IMPL_FREEOUTDATA, (CRYPT_DECODER_IMPL_FreeOutData)DECODER_LowKeyObject2PkeyObjectFreeOutData}, {CRYPT_DECODER_IMPL_FREECTX, (CRYPT_DECODER_IMPL_FreeCtx)DECODER_LowKeyObject2PkeyObjectFreeCtx}, CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_CODECSKEY && HITLS_CRYPTO_PROVIDER */
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_decode.c
C
unknown
13,446
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_SCRYPT) || defined(HITLS_CRYPTO_PBKDF2) || defined(HITLS_CRYPTO_KDFTLS12) || \ defined(HITLS_CRYPTO_HKDF)) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_pbkdf2.h" #include "crypt_kdf_tls12.h" #include "crypt_hkdf.h" #include "crypt_scrypt.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_default_provider.h" void *CRYPT_EAL_DefKdfNewCtx(CRYPT_EAL_DefProvCtx *provCtx, int32_t algId) { void *libCtx = provCtx == NULL ? NULL : provCtx->libCtx; switch (algId) { #ifdef HITLS_CRYPTO_SCRYPT case CRYPT_KDF_SCRYPT: return CRYPT_SCRYPT_NewCtxEx(libCtx); #endif #ifdef HITLS_CRYPTO_PBKDF2 case CRYPT_KDF_PBKDF2: return CRYPT_PBKDF2_NewCtxEx(libCtx); #endif #ifdef HITLS_CRYPTO_KDFTLS12 case CRYPT_KDF_KDFTLS12: return CRYPT_KDFTLS12_NewCtxEx(libCtx); #endif #ifdef HITLS_CRYPTO_HKDF case CRYPT_KDF_HKDF: return CRYPT_HKDF_NewCtxEx(libCtx); #endif default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } } #ifdef HITLS_CRYPTO_SCRYPT const CRYPT_EAL_Func g_defEalKdfScrypt[] = { {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_EAL_DefKdfNewCtx}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_SCRYPT_SetParam}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_SCRYPT_Derive}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_SCRYPT_Deinit}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_SCRYPT_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_PBKDF2 const CRYPT_EAL_Func g_defEalKdfPBKdf2[] = { {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_EAL_DefKdfNewCtx}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_PBKDF2_SetParam}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_PBKDF2_Derive}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_PBKDF2_Deinit}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_PBKDF2_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_KDFTLS12 const CRYPT_EAL_Func g_defEalKdfKdfTLS12[] = { {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_EAL_DefKdfNewCtx}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_KDFTLS12_SetParam}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_KDFTLS12_Derive}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_KDFTLS12_Deinit}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_KDFTLS12_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_HKDF const CRYPT_EAL_Func g_defEalKdfHkdf[] = { {CRYPT_EAL_IMPLKDF_NEWCTX, (CRYPT_EAL_ImplKdfNewCtx)CRYPT_EAL_DefKdfNewCtx}, {CRYPT_EAL_IMPLKDF_SETPARAM, (CRYPT_EAL_ImplKdfSetParam)CRYPT_HKDF_SetParam}, {CRYPT_EAL_IMPLKDF_DERIVE, (CRYPT_EAL_ImplKdfDerive)CRYPT_HKDF_Derive}, {CRYPT_EAL_IMPLKDF_DEINITCTX, (CRYPT_EAL_ImplKdfDeInitCtx)CRYPT_HKDF_Deinit}, {CRYPT_EAL_IMPLKDF_FREECTX, (CRYPT_EAL_ImplKdfFreeCtx)CRYPT_HKDF_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_kdf.c
C
unknown
3,823
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_MLKEM) || defined(HITLS_CRYPTO_HYBRIDKEM)) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #ifdef HITLS_CRYPTO_MLKEM #include "crypt_mlkem.h" #endif #ifdef HITLS_CRYPTO_HYBRIDKEM #include "crypt_hybridkem.h" #endif #ifdef HITLS_CRYPTO_MLKEM const CRYPT_EAL_Func g_defEalMlKem[] = { {CRYPT_EAL_IMPLPKEYKEM_ENCAPSULATE, (CRYPT_EAL_ImplPkeyKemEncapsulate)CRYPT_ML_KEM_Encaps}, {CRYPT_EAL_IMPLPKEYKEM_DECAPSULATE, (CRYPT_EAL_ImplPkeyKemDecapsulate)CRYPT_ML_KEM_Decaps}, CRYPT_EAL_FUNC_END }; #endif #ifdef HITLS_CRYPTO_HYBRIDKEM const CRYPT_EAL_Func g_defEalHybridKeyKem[] = { {CRYPT_EAL_IMPLPKEYKEM_ENCAPSULATE, (CRYPT_EAL_ImplPkeyKemEncapsulate)CRYPT_HYBRID_KEM_Encaps}, {CRYPT_EAL_IMPLPKEYKEM_DECAPSULATE, (CRYPT_EAL_ImplPkeyKemDecapsulate)CRYPT_HYBRID_KEM_Decaps}, CRYPT_EAL_FUNC_END }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_kem.c
C
unknown
1,445
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_X25519) || defined(HITLS_CRYPTO_DH) || defined(HITLS_CRYPTO_ECDH) || \ defined(HITLS_CRYPTO_SM2_EXCH)) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_curve25519.h" #include "crypt_dh.h" #include "crypt_ecdh.h" #include "crypt_sm2.h" typedef struct { void *pkeyCtx; int32_t algId; int32_t index; } CRYPT_EAL_DefPkeyCtx; #ifdef HITLS_CRYPTO_X25519 const CRYPT_EAL_Func g_defEalExchX25519[] = { {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_CURVE25519_ComputeSharedKey}, CRYPT_EAL_FUNC_END }; #endif #ifdef HITLS_CRYPTO_DH const CRYPT_EAL_Func g_defEalExchDh[] = { {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_DH_ComputeShareKey}, CRYPT_EAL_FUNC_END }; #endif #ifdef HITLS_CRYPTO_ECDH const CRYPT_EAL_Func g_defEalExchEcdh[] = { {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_ECDH_ComputeShareKey}, CRYPT_EAL_FUNC_END }; #endif #ifdef HITLS_CRYPTO_SM2_EXCH const CRYPT_EAL_Func g_defEalExchSm2[] = { {CRYPT_EAL_IMPLPKEYEXCH_EXCH, (CRYPT_EAL_ImplPkeyExch)CRYPT_SM2_KapComputeKey}, CRYPT_EAL_FUNC_END }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_keyexch.c
C
unknown
1,743
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_DSA) || defined(HITLS_CRYPTO_ED25519) || defined(HITLS_CRYPTO_X25519) || \ defined(HITLS_CRYPTO_RSA) || defined(HITLS_CRYPTO_DH) || defined(HITLS_CRYPTO_ECDSA) || \ defined(HITLS_CRYPTO_ECDH) || defined(HITLS_CRYPTO_SM2) || defined(HITLS_CRYPTO_PAILLIER) || \ defined(HITLS_CRYPTO_ELGAMAL) || defined(HITLS_CRYPTO_SLH_DSA) || defined(HITLS_CRYPTO_MLKEM) || \ defined(HITLS_CRYPTO_MLDSA) || defined(HITLS_CRYPTO_HYBRIDKEM)) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #ifdef HITLS_CRYPTO_DSA #include "crypt_dsa.h" #endif #ifdef HITLS_CRYPTO_CURVE25519 #include "crypt_curve25519.h" #endif #ifdef HITLS_CRYPTO_RSA #include "crypt_rsa.h" #endif #ifdef HITLS_CRYPTO_DH #include "crypt_dh.h" #endif #ifdef HITLS_CRYPTO_ECDSA #include "crypt_ecdsa.h" #endif #ifdef HITLS_CRYPTO_ECDH #include "crypt_ecdh.h" #endif #ifdef HITLS_CRYPTO_SM2 #include "crypt_sm2.h" #endif #ifdef HITLS_CRYPTO_PAILLIER #include "crypt_paillier.h" #endif #ifdef HITLS_CRYPTO_ELGAMAL #include "crypt_elgamal.h" #endif #ifdef HITLS_CRYPTO_SLH_DSA #include "crypt_slh_dsa.h" #endif #ifdef HITLS_CRYPTO_MLKEM #include "crypt_mlkem.h" #endif #ifdef HITLS_CRYPTO_MLDSA #include "crypt_mldsa.h" #endif #ifdef HITLS_CRYPTO_HYBRIDKEM #include "crypt_hybridkem.h" #endif #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "crypt_default_provider.h" void *CRYPT_EAL_DefPkeyMgmtNewCtx(CRYPT_EAL_DefProvCtx *provCtx, int32_t algId) { (void)provCtx; void *pkeyCtx = NULL; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Pkey(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif switch (algId) { #ifdef HITLS_CRYPTO_DSA case CRYPT_PKEY_DSA: return CRYPT_DSA_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_ED25519 case CRYPT_PKEY_ED25519: pkeyCtx = CRYPT_ED25519_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_X25519 case CRYPT_PKEY_X25519: pkeyCtx = CRYPT_X25519_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_RSA case CRYPT_PKEY_RSA: pkeyCtx = CRYPT_RSA_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_DH case CRYPT_PKEY_DH: pkeyCtx = CRYPT_DH_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_ECDSA case CRYPT_PKEY_ECDSA: pkeyCtx = CRYPT_ECDSA_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_ECDH case CRYPT_PKEY_ECDH: pkeyCtx = CRYPT_ECDH_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_SM2 case CRYPT_PKEY_SM2: pkeyCtx = CRYPT_SM2_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_PAILLIER case CRYPT_PKEY_PAILLIER: pkeyCtx = CRYPT_PAILLIER_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_ELGAMAL case CRYPT_PKEY_ELGAMAL: pkeyCtx = CRYPT_ELGAMAL_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_SLH_DSA case CRYPT_PKEY_SLH_DSA: pkeyCtx = CRYPT_SLH_DSA_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_MLKEM case CRYPT_PKEY_ML_KEM: pkeyCtx = CRYPT_ML_KEM_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_MLDSA case CRYPT_PKEY_ML_DSA: pkeyCtx = CRYPT_ML_DSA_NewCtxEx(provCtx->libCtx); break; #endif #ifdef HITLS_CRYPTO_HYBRIDKEM case CRYPT_PKEY_HYBRID_KEM: pkeyCtx = CRYPT_HYBRID_KEM_NewCtx(); break; #endif default: pkeyCtx = NULL; } if (pkeyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } return pkeyCtx; }; #ifdef HITLS_CRYPTO_DSA const CRYPT_EAL_Func g_defEalKeyMgmtDsa[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_DSA_SetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_DSA_GetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_DSA_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_DSA_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_DSA_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_DSA_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_DSA_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_DSA_DupCtx}, #ifdef HITLS_CRYPTO_DSA_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_DSA_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_DSA_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_DSA_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_DSA_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ED25519 const CRYPT_EAL_Func g_defEalKeyMgmtEd25519[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ED25519_GenKey}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_CURVE25519_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_CURVE25519_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_CURVE25519_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_CURVE25519_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_CURVE25519_DupCtx}, #ifdef HITLS_CRYPTO_ED25519_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ED25519_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_CURVE25519_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_CURVE25519_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_CURVE25519_FreeCtx}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_CURVE25519_Import}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_CURVE25519_Export}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_X25519 const CRYPT_EAL_Func g_defEalKeyMgmtX25519[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_X25519_GenKey}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_CURVE25519_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_CURVE25519_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_CURVE25519_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_CURVE25519_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_CURVE25519_DupCtx}, #ifdef HITLS_CRYPTO_X25519_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_X25519_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_CURVE25519_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_CURVE25519_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_CURVE25519_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_RSA const CRYPT_EAL_Func g_defEalKeyMgmtRsa[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_RSA_SetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_RSA_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_RSA_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_RSA_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_RSA_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_RSA_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_RSA_DupCtx}, #ifdef HITLS_CRYPTO_RSA_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_RSA_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_RSA_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_RSA_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_RSA_FreeCtx}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_RSA_Import}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_RSA_Export}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_DH const CRYPT_EAL_Func g_defEalKeyMgmtDh[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_DH_SetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_DH_GetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_DH_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_DH_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_DH_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_DH_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_DH_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_DH_DupCtx}, #ifdef HITLS_CRYPTO_DH_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_DH_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_DH_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_DH_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_DH_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECDSA const CRYPT_EAL_Func g_defEalKeyMgmtEcdsa[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_ECDSA_SetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_ECDSA_GetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ECDSA_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ECDSA_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ECDSA_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ECDSA_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ECDSA_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ECDSA_DupCtx}, #ifdef HITLS_CRYPTO_ECDSA_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ECDSA_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ECDSA_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ECDSA_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ECDSA_FreeCtx}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_ECDSA_Import}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_ECDSA_Export}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECDH const CRYPT_EAL_Func g_defEalKeyMgmtEcdh[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_ECDH_SetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPARAM, (CRYPT_EAL_ImplPkeyMgmtGetParam)CRYPT_ECDH_GetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ECDH_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ECDH_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ECDH_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ECDH_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ECDH_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ECDH_DupCtx}, #ifdef HITLS_CRYPTO_ECDH_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ECDH_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ECDH_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ECDH_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ECDH_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SM2 const CRYPT_EAL_Func g_defEalKeyMgmtSm2[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_SM2_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_SM2_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_SM2_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_SM2_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_SM2_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_SM2_DupCtx}, #ifdef HITLS_CRYPTO_SM2_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_SM2_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_SM2_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_SM2_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_SM2_FreeCtx}, {CRYPT_EAL_IMPLPKEYMGMT_IMPORT, (CRYPT_EAL_ImplPkeyMgmtImport)CRYPT_SM2_Import}, {CRYPT_EAL_IMPLPKEYMGMT_EXPORT, (CRYPT_EAL_ImplPkeyMgmtExport)CRYPT_SM2_Export}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_PAILLIER const CRYPT_EAL_Func g_defEalKeyMgmtPaillier[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_PAILLIER_SetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_PAILLIER_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_PAILLIER_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_PAILLIER_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_PAILLIER_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_PAILLIER_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_PAILLIER_DupCtx}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_PAILLIER_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_PAILLIER_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ELGAMAL const CRYPT_EAL_Func g_defEalKeyMgmtElGamal[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPARAM, (CRYPT_EAL_ImplPkeyMgmtSetParam)CRYPT_ELGAMAL_SetParaEx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ELGAMAL_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ELGAMAL_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ELGAMAL_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ELGAMAL_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ELGAMAL_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ELGAMAL_DupCtx}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ELGAMAL_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ELGAMAL_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_MLKEM const CRYPT_EAL_Func g_defEalKeyMgmtMlKem[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ML_KEM_GenKey}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ML_KEM_SetDecapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ML_KEM_SetEncapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ML_KEM_GetDecapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ML_KEM_GetEncapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ML_KEM_DupCtx}, #ifdef HITLS_CRYPTO_MLKEM_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ML_KEM_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ML_KEM_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ML_KEM_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ML_KEM_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_MLDSA const CRYPT_EAL_Func g_defEalKeyMgmtMlDsa[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_ML_DSA_GenKey}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_ML_DSA_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_ML_DSA_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_ML_DSA_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_ML_DSA_GetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_DUPCTX, (CRYPT_EAL_ImplPkeyMgmtDupCtx)CRYPT_ML_DSA_DupCtx}, #ifdef HITLS_CRYPTO_MLDSA_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_ML_DSA_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_COMPARE, (CRYPT_EAL_ImplPkeyMgmtCompare)CRYPT_ML_DSA_Cmp}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_ML_DSA_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_ML_DSA_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SLH_DSA const CRYPT_EAL_Func g_defEalKeyMgmtSlhDsa[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_SLH_DSA_Gen}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_SLH_DSA_SetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_SLH_DSA_SetPubKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_SLH_DSA_GetPrvKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_SLH_DSA_GetPubKeyEx}, #ifdef HITLS_CRYPTO_SLH_DSA_CHECK {CRYPT_EAL_IMPLPKEYMGMT_CHECK, (CRYPT_EAL_ImplPkeyMgmtCheck)CRYPT_SLH_DSA_Check}, #endif {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_SLH_DSA_Ctrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_SLH_DSA_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_HYBRIDKEM const CRYPT_EAL_Func g_defEalKeyMgmtHybridKem[] = { {CRYPT_EAL_IMPLPKEYMGMT_NEWCTX, (CRYPT_EAL_ImplPkeyMgmtNewCtx)CRYPT_EAL_DefPkeyMgmtNewCtx}, {CRYPT_EAL_IMPLPKEYMGMT_GENKEY, (CRYPT_EAL_ImplPkeyMgmtGenKey)CRYPT_HYBRID_KEM_GenKey}, {CRYPT_EAL_IMPLPKEYMGMT_SETPRV, (CRYPT_EAL_ImplPkeyMgmtSetPrv)CRYPT_HYBRID_KEM_SetDecapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_SETPUB, (CRYPT_EAL_ImplPkeyMgmtSetPub)CRYPT_HYBRID_KEM_SetEncapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPRV, (CRYPT_EAL_ImplPkeyMgmtGetPrv)CRYPT_HYBRID_KEM_GetDecapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_GETPUB, (CRYPT_EAL_ImplPkeyMgmtGetPub)CRYPT_HYBRID_KEM_GetEncapsKeyEx}, {CRYPT_EAL_IMPLPKEYMGMT_CTRL, (CRYPT_EAL_ImplPkeyMgmtCtrl)CRYPT_HYBRID_KEM_KeyCtrl}, {CRYPT_EAL_IMPLPKEYMGMT_FREECTX, (CRYPT_EAL_ImplPkeyMgmtFreeCtx)CRYPT_HYBRID_KEM_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_keymgmt.c
C
unknown
21,261
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_HMAC) || defined(HITLS_CRYPTO_CMAC) || defined(HITLS_CRYPTO_CBC_MAC) || \ defined(HITLS_CRYPTO_SIPHASH) || defined(HITLS_CRYPTO_GMAC)) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_hmac.h" #include "crypt_cmac.h" #include "crypt_cbc_mac.h" #include "crypt_gmac.h" #include "crypt_siphash.h" #include "crypt_ealinit.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_default_provider.h" void *CRYPT_EAL_DefMacNewCtx(CRYPT_EAL_DefProvCtx *provCtx, int32_t algId) { void *libCtx = provCtx == NULL ? NULL : provCtx->libCtx; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Mac(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif switch (algId) { #ifdef HITLS_CRYPTO_HMAC case CRYPT_MAC_HMAC_MD5: case CRYPT_MAC_HMAC_SHA1: case CRYPT_MAC_HMAC_SHA224: case CRYPT_MAC_HMAC_SHA256: case CRYPT_MAC_HMAC_SHA384: case CRYPT_MAC_HMAC_SHA512: case CRYPT_MAC_HMAC_SHA3_224: case CRYPT_MAC_HMAC_SHA3_256: case CRYPT_MAC_HMAC_SHA3_384: case CRYPT_MAC_HMAC_SHA3_512: case CRYPT_MAC_HMAC_SM3: return CRYPT_HMAC_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_CMAC case CRYPT_MAC_CMAC_AES128: case CRYPT_MAC_CMAC_AES192: case CRYPT_MAC_CMAC_AES256: case CRYPT_MAC_CMAC_SM4: return CRYPT_CMAC_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_CBC_MAC case CRYPT_MAC_CBC_MAC_SM4: return CRYPT_CBC_MAC_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SIPHASH case CRYPT_MAC_SIPHASH64: case CRYPT_MAC_SIPHASH128: return CRYPT_SIPHASH_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_GMAC case CRYPT_MAC_GMAC_AES128: case CRYPT_MAC_GMAC_AES192: case CRYPT_MAC_GMAC_AES256: return CRYPT_GMAC_NewCtxEx(libCtx, algId); #endif default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } } #ifdef HITLS_CRYPTO_HMAC const CRYPT_EAL_Func g_defEalMacHmac[] = { {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_EAL_DefMacNewCtx}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_HMAC_Init}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_HMAC_Update}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_HMAC_Final}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_HMAC_Deinit}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_HMAC_Reinit}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_HMAC_Ctrl}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_HMAC_FreeCtx}, {CRYPT_EAL_IMPLMAC_SETPARAM, (CRYPT_EAL_ImplMacSetParam)CRYPT_HMAC_SetParam}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_CMAC const CRYPT_EAL_Func g_defEalMacCmac[] = { {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_EAL_DefMacNewCtx}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_CMAC_Init}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_CMAC_Update}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_CMAC_Final}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_CMAC_Deinit}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_CMAC_Reinit}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_CMAC_Ctrl}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_CMAC_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_CBC_MAC const CRYPT_EAL_Func g_defEalMacCbcMac[] = { {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_EAL_DefMacNewCtx}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_CBC_MAC_Init}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_CBC_MAC_Update}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_CBC_MAC_Final}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_CBC_MAC_Deinit}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_CBC_MAC_Reinit}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_CBC_MAC_Ctrl}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_CBC_MAC_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_GMAC const CRYPT_EAL_Func g_defEalMacGmac[] = { {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_EAL_DefMacNewCtx}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_GMAC_Init}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_GMAC_Update}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_GMAC_Final}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_GMAC_Deinit}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_GMAC_Ctrl}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_GMAC_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SIPHASH const CRYPT_EAL_Func g_defEalMacSiphash[] = { {CRYPT_EAL_IMPLMAC_NEWCTX, (CRYPT_EAL_ImplMacNewCtx)CRYPT_EAL_DefMacNewCtx}, {CRYPT_EAL_IMPLMAC_INIT, (CRYPT_EAL_ImplMacInit)CRYPT_SIPHASH_Init}, {CRYPT_EAL_IMPLMAC_UPDATE, (CRYPT_EAL_ImplMacUpdate)CRYPT_SIPHASH_Update}, {CRYPT_EAL_IMPLMAC_FINAL, (CRYPT_EAL_ImplMacFinal)CRYPT_SIPHASH_Final}, {CRYPT_EAL_IMPLMAC_DEINITCTX, (CRYPT_EAL_ImplMacDeInitCtx)CRYPT_SIPHASH_Deinit}, {CRYPT_EAL_IMPLMAC_REINITCTX, (CRYPT_EAL_ImplMacReInitCtx)CRYPT_SIPHASH_Reinit}, {CRYPT_EAL_IMPLMAC_CTRL, (CRYPT_EAL_ImplMacCtrl)CRYPT_SIPHASH_Ctrl}, {CRYPT_EAL_IMPLMAC_FREECTX, (CRYPT_EAL_ImplMacFreeCtx)CRYPT_SIPHASH_FreeCtx}, CRYPT_EAL_FUNC_END, }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_mac.c
C
unknown
6,383
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_MD5) || defined(HITLS_CRYPTO_SHA1) || defined(HITLS_CRYPTO_SHA256) || \ defined(HITLS_CRYPTO_SHA224) || defined(HITLS_CRYPTO_SHA384) || defined(HITLS_CRYPTO_SHA512) || \ defined(HITLS_CRYPTO_SHA3) || defined(HITLS_CRYPTO_SM3)) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_md5.h" #include "crypt_sha1.h" #include "crypt_sha2.h" #include "crypt_sha3.h" #include "crypt_sm3.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "crypt_default_provider.h" static void *CRYPT_EAL_DefMdNewCtx(CRYPT_EAL_DefProvCtx *provCtx, int32_t algId) { void *libCtx = provCtx == NULL ? NULL : provCtx->libCtx; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Md(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif switch (algId) { #ifdef HITLS_CRYPTO_MD5 case CRYPT_MD_MD5: return CRYPT_MD5_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SHA1 case CRYPT_MD_SHA1: return CRYPT_SHA1_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SHA224 case CRYPT_MD_SHA224: return CRYPT_SHA2_224_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SHA256 case CRYPT_MD_SHA256: return CRYPT_SHA2_256_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SHA384 case CRYPT_MD_SHA384: return CRYPT_SHA2_384_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SHA512 case CRYPT_MD_SHA512: return CRYPT_SHA2_512_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SHA3 case CRYPT_MD_SHA3_224: case CRYPT_MD_SHA3_256: case CRYPT_MD_SHA3_384: case CRYPT_MD_SHA3_512: return CRYPT_SHA3_256_NewCtxEx(libCtx, algId); case CRYPT_MD_SHAKE128: case CRYPT_MD_SHAKE256: return CRYPT_SHAKE256_NewCtxEx(libCtx, algId); #endif #ifdef HITLS_CRYPTO_SM3 case CRYPT_MD_SM3: return CRYPT_SM3_NewCtxEx(libCtx, algId); #endif default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } } #ifdef HITLS_CRYPTO_MD5 const CRYPT_EAL_Func g_defEalMdMd5[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_MD5_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_MD5_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_MD5_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_MD5_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_MD5_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_MD5_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_MD5_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_MD5_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_MD5 #ifdef HITLS_CRYPTO_SHA1 const CRYPT_EAL_Func g_defEalMdSha1[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA1_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA1_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA1_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA1_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA1_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA1_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA1_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA1_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_SHA1 #ifdef HITLS_CRYPTO_SHA224 const CRYPT_EAL_Func g_defEalMdSha224[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_224_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_224_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_224_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_224_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_224_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_224_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_224_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_224_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_SHA224 #ifdef HITLS_CRYPTO_SHA256 const CRYPT_EAL_Func g_defEalMdSha256[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_256_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_256_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_256_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_256_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_256_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_256_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_256_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_256_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_SHA256 #ifdef HITLS_CRYPTO_SHA384 const CRYPT_EAL_Func g_defEalMdSha384[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_384_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_384_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_384_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_384_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_384_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_384_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_384_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_384_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_SHA384 #ifdef HITLS_CRYPTO_SHA512 const CRYPT_EAL_Func g_defEalMdSha512[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA2_512_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA2_512_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA2_512_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA2_512_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA2_512_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA2_512_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA2_512_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA2_512_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_SHA512 #ifdef HITLS_CRYPTO_SHA3 const CRYPT_EAL_Func g_defEalMdSha3224[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_224_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_224_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_224_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_224_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_224_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_224_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_224_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_224_GetParam}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_defEalMdSha3256[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_256_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_256_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_256_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_256_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_256_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_256_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_256_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_256_GetParam}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_defEalMdSha3384[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_384_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_384_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_384_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_384_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_384_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_384_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_384_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_384_GetParam}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_defEalMdSha3512[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHA3_512_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHA3_512_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHA3_512_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHA3_512_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHA3_512_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHA3_512_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHA3_512_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHA3_512_GetParam}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_defEalMdShake128[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHAKE128_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHAKE128_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHAKE128_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHAKE128_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHAKE128_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHAKE128_FreeCtx}, {CRYPT_EAL_IMPLMD_SQUEEZE, (CRYPT_EAL_ImplMdSqueeze)CRYPT_SHAKE128_Squeeze}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHAKE128_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHAKE128_GetParam}, CRYPT_EAL_FUNC_END, }; const CRYPT_EAL_Func g_defEalMdShake256[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SHAKE256_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SHAKE256_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SHAKE256_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SHAKE256_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SHAKE256_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SHAKE256_FreeCtx}, {CRYPT_EAL_IMPLMD_SQUEEZE, (CRYPT_EAL_ImplMdSqueeze)CRYPT_SHAKE256_Squeeze}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SHAKE256_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SHAKE256_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_SHA3 #ifdef HITLS_CRYPTO_SM3 const CRYPT_EAL_Func g_defEalMdSm3[] = { {CRYPT_EAL_IMPLMD_NEWCTX, (CRYPT_EAL_ImplMdNewCtx)CRYPT_EAL_DefMdNewCtx}, {CRYPT_EAL_IMPLMD_INITCTX, (CRYPT_EAL_ImplMdInitCtx)CRYPT_SM3_Init}, {CRYPT_EAL_IMPLMD_UPDATE, (CRYPT_EAL_ImplMdUpdate)CRYPT_SM3_Update}, {CRYPT_EAL_IMPLMD_FINAL, (CRYPT_EAL_ImplMdFinal)CRYPT_SM3_Final}, {CRYPT_EAL_IMPLMD_DEINITCTX, (CRYPT_EAL_ImplMdDeInitCtx)CRYPT_SM3_Deinit}, {CRYPT_EAL_IMPLMD_DUPCTX, (CRYPT_EAL_ImplMdDupCtx)CRYPT_SM3_DupCtx}, {CRYPT_EAL_IMPLMD_FREECTX, (CRYPT_EAL_ImplMdFreeCtx)CRYPT_SM3_FreeCtx}, {CRYPT_EAL_IMPLMD_COPYCTX, (CRYPT_EAL_ImplMdCopyCtx)CRYPT_SM3_CopyCtx}, {CRYPT_EAL_IMPLMD_GETPARAM, (CRYPT_EAL_ImplMdGetParam)CRYPT_SM3_GetParam}, CRYPT_EAL_FUNC_END, }; #endif // HITLS_CRYPTO_SM3 #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_md.c
C
unknown
13,561
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) || \ defined(HITLS_CRYPTO_SM2_CRYPT) || defined(HITLS_CRYPTO_PAILLIER) || defined(HITLS_CRYPTO_ELGAMAL)) && \ defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_rsa.h" #include "crypt_sm2.h" #include "crypt_paillier.h" #include "crypt_elgamal.h" #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) const CRYPT_EAL_Func g_defEalAsymCipherRsa[] = { #ifdef HITLS_CRYPTO_RSA_ENCRYPT {CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT, (CRYPT_EAL_ImplPkeyEncrypt)CRYPT_RSA_Encrypt}, #endif #ifdef HITLS_CRYPTO_RSA_DECRYPT {CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT, (CRYPT_EAL_ImplPkeyDecrypt)CRYPT_RSA_Decrypt}, #endif CRYPT_EAL_FUNC_END }; #endif #ifdef HITLS_CRYPTO_SM2_CRYPT const CRYPT_EAL_Func g_defEalAsymCipherSm2[] = { {CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT, (CRYPT_EAL_ImplPkeyEncrypt)CRYPT_SM2_Encrypt}, {CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT, (CRYPT_EAL_ImplPkeyDecrypt)CRYPT_SM2_Decrypt}, CRYPT_EAL_FUNC_END }; #endif #ifdef HITLS_CRYPTO_PAILLIER const CRYPT_EAL_Func g_defEalAsymCipherPaillier[] = { {CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT, (CRYPT_EAL_ImplPkeyEncrypt)CRYPT_PAILLIER_Encrypt}, {CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT, (CRYPT_EAL_ImplPkeyDecrypt)CRYPT_PAILLIER_Decrypt}, {CRYPT_EAL_IMPLPKEYCIPHER_HEADD, (CRYPT_EAL_ImplPkeyHEAdd)CRYPT_PAILLIER_Add}, CRYPT_EAL_FUNC_END }; #endif #ifdef HITLS_CRYPTO_ELGAMAL const CRYPT_EAL_Func g_defEalAsymCipherElGamal[] = { {CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT, CRYPT_ELGAMAL_Encrypt}, {CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT, CRYPT_ELGAMAL_Decrypt}, CRYPT_EAL_FUNC_END }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_pkeycipher.c
C
unknown
2,255
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_PROVIDER #include <stdint.h> #include <string.h> #include "bsl_sal.h" #include "bsl_obj.h" #include "bsl_errno.h" #include "bsl_params.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_algid.h" #include "crypt_errno.h" #include "crypt_params_key.h" #ifdef HITLS_TLS_FEATURE_PROVIDER #include "hitls_crypt_type.h" #include "hitls_cert_type.h" #include "hitls_type.h" #endif #include "crypt_eal_implprovider.h" #include "crypt_eal_provider.h" #include "crypt_provider.h" #include "crypt_default_provderimpl.h" #include "crypt_default_provider.h" #define CRYPT_EAL_DEFAULT_ATTR "provider=default" #ifdef HITLS_CRYPTO_MD static const CRYPT_EAL_AlgInfo g_defEalMds[] = { #ifdef HITLS_CRYPTO_MD5 {CRYPT_MD_MD5, g_defEalMdMd5, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_MD5 #ifdef HITLS_CRYPTO_SHA1 {CRYPT_MD_SHA1, g_defEalMdSha1, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_SHA1 #ifdef HITLS_CRYPTO_SHA224 {CRYPT_MD_SHA224, g_defEalMdSha224, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_SHA224 #ifdef HITLS_CRYPTO_SHA256 {CRYPT_MD_SHA256, g_defEalMdSha256, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_SHA256 #ifdef HITLS_CRYPTO_SHA384 {CRYPT_MD_SHA384, g_defEalMdSha384, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_SHA384 #ifdef HITLS_CRYPTO_SHA512 {CRYPT_MD_SHA512, g_defEalMdSha512, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_SHA512 #ifdef HITLS_CRYPTO_SHA3 {CRYPT_MD_SHA3_224, g_defEalMdSha3224, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MD_SHA3_256, g_defEalMdSha3256, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MD_SHA3_384, g_defEalMdSha3384, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MD_SHA3_512, g_defEalMdSha3512, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MD_SHAKE128, g_defEalMdShake128, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MD_SHAKE256, g_defEalMdShake256, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_SHA3 #ifdef HITLS_CRYPTO_SM3 {CRYPT_MD_SM3, g_defEalMdSm3, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_SM3 CRYPT_EAL_ALGINFO_END }; #endif // HITLS_CRYPTO_MD #ifdef HITLS_CRYPTO_MAC static const CRYPT_EAL_AlgInfo g_defEalMacs[] = { #ifdef HITLS_CRYPTO_HMAC // The underlying hash algorithm in HMAC can be offloaded to other providers, // so no need to add hash-related macro guards here {CRYPT_MAC_HMAC_MD5, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA1, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA224, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA256, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA384, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA512, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA3_224, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA3_256, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA3_384, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SHA3_512, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_HMAC_SM3, g_defEalMacHmac, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_CMAC #ifdef HITLS_CRYPTO_AES {CRYPT_MAC_CMAC_AES128, g_defEalMacCmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_CMAC_AES192, g_defEalMacCmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_CMAC_AES256, g_defEalMacCmac, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SM4 {CRYPT_MAC_CMAC_SM4, g_defEalMacCmac, CRYPT_EAL_DEFAULT_ATTR}, #endif #endif #ifdef HITLS_CRYPTO_CBC_MAC {CRYPT_MAC_CBC_MAC_SM4, g_defEalMacCbcMac, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SIPHASH {CRYPT_MAC_SIPHASH64, g_defEalMacSiphash, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_SIPHASH128, g_defEalMacSiphash, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_GMAC {CRYPT_MAC_GMAC_AES128, g_defEalMacGmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_GMAC_AES192, g_defEalMacGmac, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_MAC_GMAC_AES256, g_defEalMacGmac, CRYPT_EAL_DEFAULT_ATTR}, #endif CRYPT_EAL_ALGINFO_END }; #endif #ifdef HITLS_CRYPTO_KDF static const CRYPT_EAL_AlgInfo g_defEalKdfs[] = { #ifdef HITLS_CRYPTO_SCRYPT {CRYPT_KDF_SCRYPT, g_defEalKdfScrypt, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_PBKDF2 {CRYPT_KDF_PBKDF2, g_defEalKdfPBKdf2, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_KDFTLS12 {CRYPT_KDF_KDFTLS12, g_defEalKdfKdfTLS12, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_HKDF {CRYPT_KDF_HKDF, g_defEalKdfHkdf, CRYPT_EAL_DEFAULT_ATTR}, #endif CRYPT_EAL_ALGINFO_END }; #endif #ifdef HITLS_CRYPTO_CIPHER static const CRYPT_EAL_AlgInfo g_defEalCiphers[] = { #ifdef HITLS_CRYPTO_AES #ifdef HITLS_CRYPTO_CBC {CRYPT_CIPHER_AES128_CBC, g_defEalCbc, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES192_CBC, g_defEalCbc, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_CBC, g_defEalCbc, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_CTR {CRYPT_CIPHER_AES128_CTR, g_defEalCtr, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES192_CTR, g_defEalCtr, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_CTR, g_defEalCtr, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ECB {CRYPT_CIPHER_AES128_ECB, g_defEalEcb, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES192_ECB, g_defEalEcb, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_ECB, g_defEalEcb, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_CCM {CRYPT_CIPHER_AES128_CCM, g_defEalCcm, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES192_CCM, g_defEalCcm, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_CCM, g_defEalCcm, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_GCM {CRYPT_CIPHER_AES128_GCM, g_defEalGcm, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES192_GCM, g_defEalGcm, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_GCM, g_defEalGcm, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_XTS {CRYPT_CIPHER_AES128_XTS, g_defEalXts, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_XTS, g_defEalXts, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_CFB {CRYPT_CIPHER_AES128_CFB, g_defEalCfb, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES192_CFB, g_defEalCfb, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_CFB, g_defEalCfb, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_OFB {CRYPT_CIPHER_AES128_OFB, g_defEalOfb, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES192_OFB, g_defEalOfb, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_CIPHER_AES256_OFB, g_defEalOfb, CRYPT_EAL_DEFAULT_ATTR}, #endif #endif // HITLS_CRYPTO_AES #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) {CRYPT_CIPHER_CHACHA20_POLY1305, g_defEalChaCha, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SM4 #ifdef HITLS_CRYPTO_XTS {CRYPT_CIPHER_SM4_XTS, g_defEalXts, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_CBC {CRYPT_CIPHER_SM4_CBC, g_defEalCbc, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ECB {CRYPT_CIPHER_SM4_ECB, g_defEalEcb, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_CTR {CRYPT_CIPHER_SM4_CTR, g_defEalCtr, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_GCM {CRYPT_CIPHER_SM4_GCM, g_defEalGcm, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_CFB {CRYPT_CIPHER_SM4_CFB, g_defEalCfb, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_OFB {CRYPT_CIPHER_SM4_OFB, g_defEalOfb, CRYPT_EAL_DEFAULT_ATTR}, #endif #endif // HITLS_CRYPTO_SM4 CRYPT_EAL_ALGINFO_END }; #endif #ifdef HITLS_CRYPTO_DRBG static const CRYPT_EAL_AlgInfo g_defEalRands[] = { #ifdef HITLS_CRYPTO_DRBG_HASH {CRYPT_RAND_SHA1, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_SHA224, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_SHA256, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_SHA384, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_SHA512, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_SM3, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_DRBG_HASH #ifdef HITLS_CRYPTO_DRBG_HMAC {CRYPT_RAND_HMAC_SHA1, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_HMAC_SHA224, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_HMAC_SHA256, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_HMAC_SHA384, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_HMAC_SHA512, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, #endif // HITLS_CRYPTO_DRBG_HMAC #ifdef HITLS_CRYPTO_DRBG_CTR #ifdef HITLS_CRYPTO_AES {CRYPT_RAND_AES128_CTR, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_AES192_CTR, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_AES256_CTR, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_AES128_CTR_DF, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_AES192_CTR_DF, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, {CRYPT_RAND_AES256_CTR_DF, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SM4 {CRYPT_RAND_SM4_CTR_DF, g_defEalRand, CRYPT_EAL_DEFAULT_ATTR}, #endif #endif // HITLS_CRYPTO_DRBG_CTR CRYPT_EAL_ALGINFO_END }; #endif #ifdef HITLS_CRYPTO_PKEY static const CRYPT_EAL_AlgInfo g_defEalKeyMgmt[] = { #ifdef HITLS_CRYPTO_DSA {CRYPT_PKEY_DSA, g_defEalKeyMgmtDsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ED25519 {CRYPT_PKEY_ED25519, g_defEalKeyMgmtEd25519, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_X25519 {CRYPT_PKEY_X25519, g_defEalKeyMgmtX25519, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_RSA {CRYPT_PKEY_RSA, g_defEalKeyMgmtRsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_DH {CRYPT_PKEY_DH, g_defEalKeyMgmtDh, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ECDSA {CRYPT_PKEY_ECDSA, g_defEalKeyMgmtEcdsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ECDH {CRYPT_PKEY_ECDH, g_defEalKeyMgmtEcdh, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SM2 {CRYPT_PKEY_SM2, g_defEalKeyMgmtSm2, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_PAILLIER {CRYPT_PKEY_PAILLIER, g_defEalKeyMgmtPaillier, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ELGAMAL {CRYPT_PKEY_ELGAMAL, g_defEalKeyMgmtElGamal, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SLH_DSA {CRYPT_PKEY_SLH_DSA, g_defEalKeyMgmtSlhDsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_MLKEM {CRYPT_PKEY_ML_KEM, g_defEalKeyMgmtMlKem, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_MLDSA {CRYPT_PKEY_ML_DSA, g_defEalKeyMgmtMlDsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_HYBRIDKEM {CRYPT_PKEY_HYBRID_KEM, g_defEalKeyMgmtHybridKem, CRYPT_EAL_DEFAULT_ATTR}, #endif CRYPT_EAL_ALGINFO_END }; #if (defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) || \ defined(HITLS_CRYPTO_SM2_CRYPT) || defined(HITLS_CRYPTO_PAILLIER) || defined(HITLS_CRYPTO_ELGAMAL)) && \ defined(HITLS_CRYPTO_PROVIDER) static const CRYPT_EAL_AlgInfo g_defEalAsymCiphers[] = { #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) {CRYPT_PKEY_RSA, g_defEalAsymCipherRsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SM2_CRYPT {CRYPT_PKEY_SM2, g_defEalAsymCipherSm2, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_PAILLIER {CRYPT_PKEY_PAILLIER, g_defEalAsymCipherPaillier, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ELGAMAL {CRYPT_PKEY_ELGAMAL, g_defEalAsymCipherElGamal, CRYPT_EAL_DEFAULT_ATTR}, #endif CRYPT_EAL_ALGINFO_END }; #endif #if (defined(HITLS_CRYPTO_X25519) || defined(HITLS_CRYPTO_DH) || defined(HITLS_CRYPTO_ECDH) || \ defined(HITLS_CRYPTO_SM2_EXCH)) && defined(HITLS_CRYPTO_PROVIDER) static const CRYPT_EAL_AlgInfo g_defEalKeyExch[] = { #ifdef HITLS_CRYPTO_X25519 {CRYPT_PKEY_X25519, g_defEalExchX25519, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_DH {CRYPT_PKEY_DH, g_defEalExchDh, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ECDH {CRYPT_PKEY_ECDH, g_defEalExchEcdh, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SM2_EXCH {CRYPT_PKEY_SM2, g_defEalExchSm2, CRYPT_EAL_DEFAULT_ATTR}, #endif CRYPT_EAL_ALGINFO_END }; #endif #if (defined(HITLS_CRYPTO_DSA) || defined(HITLS_CRYPTO_ED25519) || defined(HITLS_CRYPTO_RSA_SIGN) || \ defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_ECDSA) || defined(HITLS_CRYPTO_SM2_SIGN) || \ defined(HITLS_CRYPTO_SLH_DSA) || defined(HITLS_CRYPTO_MLDSA)) && defined(HITLS_CRYPTO_PROVIDER) static const CRYPT_EAL_AlgInfo g_defEalSigns[] = { #ifdef HITLS_CRYPTO_DSA {CRYPT_PKEY_DSA, g_defEalSignDsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ED25519 {CRYPT_PKEY_ED25519, g_defEalSignEd25519, CRYPT_EAL_DEFAULT_ATTR}, #endif #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) {CRYPT_PKEY_RSA, g_defEalSignRsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_ECDSA {CRYPT_PKEY_ECDSA, g_defEalSignEcdsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SM2_SIGN {CRYPT_PKEY_SM2, g_defEalSignSm2, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_SLH_DSA {CRYPT_PKEY_SLH_DSA, g_defEalSignSlhDsa, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_MLDSA {CRYPT_PKEY_ML_DSA, g_defEalSignMlDsa, CRYPT_EAL_DEFAULT_ATTR}, #endif CRYPT_EAL_ALGINFO_END }; #endif #if defined(HITLS_CRYPTO_MLKEM) || defined(HITLS_CRYPTO_HYBRIDKEM) static const CRYPT_EAL_AlgInfo g_defEalKems[] = { #ifdef HITLS_CRYPTO_MLKEM {CRYPT_PKEY_ML_KEM, g_defEalMlKem, CRYPT_EAL_DEFAULT_ATTR}, #endif #ifdef HITLS_CRYPTO_HYBRIDKEM {CRYPT_PKEY_HYBRID_KEM, g_defEalHybridKeyKem, CRYPT_EAL_DEFAULT_ATTR}, #endif CRYPT_EAL_ALGINFO_END }; #endif #endif #ifdef HITLS_CRYPTO_CODECSKEY static const CRYPT_EAL_AlgInfo g_defEalDecoders[] = { #ifdef HITLS_BSL_PEM {BSL_CID_DECODE_UNKNOWN, g_defEalPem2Der, "provider=default, inFormat=PEM, outFormat=ASN1"}, #endif #ifdef HITLS_CRYPTO_KEY_EPKI {BSL_CID_DECODE_UNKNOWN, g_defEalPrvP8Enc2P8, "provider=default, inFormat=ASN1, inType=PRIKEY_PKCS8_ENCRYPT, outFormat=ASN1, outType=PRIKEY_PKCS8_UNENCRYPT"}, #endif #ifdef HITLS_CRYPTO_RSA {CRYPT_PKEY_RSA, g_defEalRsaPrvDer2Key, "provider=default, inFormat=ASN1, inType=PRIKEY_RSA, outFormat=OBJECT, outType=LOW_KEY"}, {CRYPT_PKEY_RSA, g_defEalRsaPubDer2Key, "provider=default, inFormat=ASN1, inType=PUBKEY_RSA, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_ECDSA {CRYPT_PKEY_ECDSA, g_defEalEcdsaPrvDer2Key, "provider=default, inFormat=ASN1, inType=PRIKEY_ECC, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_SM2 {CRYPT_PKEY_SM2, g_defEalSm2PrvDer2Key, "provider=default, inFormat=ASN1, inType=PRIKEY_ECC, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_RSA {CRYPT_PKEY_RSA, g_defEalP8Der2RsaKey, "provider=default, inFormat=ASN1, inType=PRIKEY_PKCS8_UNENCRYPT, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_ECDSA {CRYPT_PKEY_ECDSA, g_defEalP8Der2EcdsaKey, "provider=default, inFormat=ASN1, inType=PRIKEY_PKCS8_UNENCRYPT, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_SM2 {CRYPT_PKEY_SM2, g_defEalP8Der2Sm2Key, "provider=default, inFormat=ASN1, inType=PRIKEY_PKCS8_UNENCRYPT, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_ED25519 {CRYPT_PKEY_ED25519, g_defEalP8Der2Ed25519Key, "provider=default, inFormat=ASN1, inType=PRIKEY_PKCS8_UNENCRYPT, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_RSA {CRYPT_PKEY_RSA, g_defEalSubPubKeyDer2RsaKey, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_ECDSA {CRYPT_PKEY_ECDSA, g_defEalSubPubKeyDer2EcdsaKey, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_SM2 {CRYPT_PKEY_SM2, g_defEalSubPubKeyDer2Sm2Key, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_ED25519 {CRYPT_PKEY_ED25519, g_defEalSubPubKeyDer2Ed25519Key, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_RSA {CRYPT_PKEY_RSA, g_defEalSubPubKeyWithoutSeqDer2RsaKey, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY_WITHOUT_SEQ, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_ECDSA {CRYPT_PKEY_ECDSA, g_defEalSubPubKeyWithoutSeqDer2EcdsaKey, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY_WITHOUT_SEQ, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_SM2 {CRYPT_PKEY_SM2, g_defEalSubPubKeyWithoutSeqDer2Sm2Key, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY_WITHOUT_SEQ, outFormat=OBJECT, outType=LOW_KEY"}, #endif #ifdef HITLS_CRYPTO_ED25519 {CRYPT_PKEY_ED25519, g_defEalSubPubKeyWithoutSeqDer2Ed25519Key, "provider=default, inFormat=ASN1, inType=PUBKEY_SUBKEY_WITHOUT_SEQ, outFormat=OBJECT, outType=LOW_KEY"}, #endif {BSL_CID_DECODE_UNKNOWN, g_defEalLowKeyObject2PkeyObject, "provider=default, inFormat=OBJECT, inType=LOW_KEY, outFormat=OBJECT, outType=HIGH_KEY"}, CRYPT_EAL_ALGINFO_END }; #endif static int32_t CRYPT_EAL_DefaultProvQuery(void *provCtx, int32_t operaId, const CRYPT_EAL_AlgInfo **algInfos) { (void)provCtx; int32_t ret = CRYPT_SUCCESS; switch (operaId) { #ifdef HITLS_CRYPTO_CIPHER case CRYPT_EAL_OPERAID_SYMMCIPHER: *algInfos = g_defEalCiphers; break; #endif #ifdef HITLS_CRYPTO_PKEY case CRYPT_EAL_OPERAID_KEYMGMT: *algInfos = g_defEalKeyMgmt; break; #if (defined(HITLS_CRYPTO_DSA) || defined(HITLS_CRYPTO_ED25519) || defined(HITLS_CRYPTO_RSA_SIGN) || \ defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_ECDSA) || defined(HITLS_CRYPTO_SM2_SIGN) || \ defined(HITLS_CRYPTO_SLH_DSA) || defined(HITLS_CRYPTO_MLDSA)) && defined(HITLS_CRYPTO_PROVIDER) case CRYPT_EAL_OPERAID_SIGN: *algInfos = g_defEalSigns; break; #endif #if (defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) || \ defined(HITLS_CRYPTO_SM2_CRYPT) || defined(HITLS_CRYPTO_PAILLIER) || defined(HITLS_CRYPTO_ELGAMAL)) && \ defined(HITLS_CRYPTO_PROVIDER) case CRYPT_EAL_OPERAID_ASYMCIPHER: *algInfos = g_defEalAsymCiphers; break; #endif #if (defined(HITLS_CRYPTO_X25519) || defined(HITLS_CRYPTO_DH) || defined(HITLS_CRYPTO_ECDH) || \ defined(HITLS_CRYPTO_SM2_EXCH)) && defined(HITLS_CRYPTO_PROVIDER) case CRYPT_EAL_OPERAID_KEYEXCH: *algInfos = g_defEalKeyExch; break; #endif #if defined(HITLS_CRYPTO_MLKEM) || defined(HITLS_CRYPTO_HYBRIDKEM) case CRYPT_EAL_OPERAID_KEM: *algInfos = g_defEalKems; break; #endif #endif #ifdef HITLS_CRYPTO_MD case CRYPT_EAL_OPERAID_HASH: *algInfos = g_defEalMds; break; #endif #ifdef HITLS_CRYPTO_MAC case CRYPT_EAL_OPERAID_MAC: *algInfos = g_defEalMacs; break; #endif #ifdef HITLS_CRYPTO_KDF case CRYPT_EAL_OPERAID_KDF: *algInfos = g_defEalKdfs; break; #endif #ifdef HITLS_CRYPTO_DRBG case CRYPT_EAL_OPERAID_RAND: *algInfos = g_defEalRands; break; #endif #ifdef HITLS_CRYPTO_CODECSKEY case CRYPT_EAL_OPERAID_DECODER: *algInfos = g_defEalDecoders; break; #endif default: ret = CRYPT_NOT_SUPPORT; break; } return ret; } static void CRYPT_EAL_DefaultProvFree(void *provCtx) { BSL_SAL_Free(provCtx); } #ifdef HITLS_TLS_FEATURE_PROVIDER #define TLS_GROUP_PARAM_COUNT 11 #define TLS_SIGN_SCHEME_PARAM_COUNT 18 typedef struct { const char *name; // group name int32_t paraId; // parameter id CRYPT_PKEY_ParaId int32_t algId; // algorithm id CRYPT_PKEY_AlgId int32_t secBits; // security bits uint16_t groupId; // iana group id, HITLS_NamedGroup uint32_t pubkeyLen; // public key length(CH keyshare / SH keyshare) uint32_t sharedkeyLen; // shared key length uint32_t ciphertextLen; // ciphertext length(SH keyshare) uint32_t versionBits; // TLS_VERSION_MASK bool isKem; // true: KEM, false: KEX } TLS_GroupInfo; static const TLS_GroupInfo g_tlsGroupInfo[] = { { "x25519", CRYPT_PKEY_PARAID_MAX, CRYPT_PKEY_X25519, 128, // secBits HITLS_EC_GROUP_CURVE25519, // groupId 32, 32, 0, // pubkeyLen=32, sharedkeyLen=32 (256 bits) TLS_VERSION_MASK | DTLS_VERSION_MASK, // versionBits false, }, #ifdef HITLS_TLS_FEATURE_KEM { "X25519MLKEM768", CRYPT_HYBRID_X25519_MLKEM768, CRYPT_PKEY_HYBRID_KEM, 192, // secBits HITLS_HYBRID_X25519_MLKEM768, // groupId 1184 + 32, 32 + 32, 1088 + 32, // pubkeyLen=1216, sharedkeyLen=64, ciphertextLen=1120 TLS13_VERSION_BIT, // versionBits true, }, { "SecP256r1MLKEM768", CRYPT_HYBRID_ECDH_NISTP256_MLKEM768, CRYPT_PKEY_HYBRID_KEM, 192, // secBits HITLS_HYBRID_ECDH_NISTP256_MLKEM768, // groupId 1184 + 65, 32 + 32, 1088 + 65, // pubkeyLen=1249, sharedkeyLen=64, ciphertextLen=1153 TLS13_VERSION_BIT, // versionBits true, }, { "SecP384r1MLKEM1024", CRYPT_HYBRID_ECDH_NISTP384_MLKEM1024, CRYPT_PKEY_HYBRID_KEM, 256, // secBits HITLS_HYBRID_ECDH_NISTP384_MLKEM1024, // groupId 1568 + 97, 32 + 48, 1568 + 97, // pubkeyLen=1665, sharedkeyLen=80, ciphertextLen=1665 TLS13_VERSION_BIT, // versionBits true, }, #endif /* HITLS_TLS_FEATURE_KEM */ { "secp256r1", CRYPT_ECC_NISTP256, // CRYPT_ECC_NISTP256 CRYPT_PKEY_ECDH, // CRYPT_PKEY_ECDH 128, // secBits HITLS_EC_GROUP_SECP256R1, // groupId 65, 32, 0, // pubkeyLen=65, sharedkeyLen=32 (256 bits) TLS_VERSION_MASK | DTLS_VERSION_MASK, // versionBits false, }, { "secp384r1", CRYPT_ECC_NISTP384, // CRYPT_ECC_NISTP384 CRYPT_PKEY_ECDH, // CRYPT_PKEY_ECDH 192, // secBits HITLS_EC_GROUP_SECP384R1, // groupId 97, 48, 0, // pubkeyLen=97, sharedkeyLen=48 (384 bits) TLS_VERSION_MASK | DTLS_VERSION_MASK, // versionBits false, }, { "secp521r1", CRYPT_ECC_NISTP521, // CRYPT_ECC_NISTP521 CRYPT_PKEY_ECDH, // CRYPT_PKEY_ECDH 256, // secBits HITLS_EC_GROUP_SECP521R1, // groupId 133, 66, 0, // pubkeyLen=133, sharedkeyLen=66 (521 bits) TLS_VERSION_MASK | DTLS_VERSION_MASK, // versionBits false, }, { "brainpoolP256r1", CRYPT_ECC_BRAINPOOLP256R1, // CRYPT_ECC_BRAINPOOLP256R1 CRYPT_PKEY_ECDH, // CRYPT_PKEY_ECDH 128, // secBits HITLS_EC_GROUP_BRAINPOOLP256R1, // groupId 65, 32, 0, // pubkeyLen=65, sharedkeyLen=32 (256 bits) TLS10_VERSION_BIT | TLS11_VERSION_BIT | TLS12_VERSION_BIT | DTLS_VERSION_MASK, // versionBits false, }, { "brainpoolP384r1", CRYPT_ECC_BRAINPOOLP384R1, // CRYPT_ECC_BRAINPOOLP384R1 CRYPT_PKEY_ECDH, // CRYPT_PKEY_ECDH 192, // secBits HITLS_EC_GROUP_BRAINPOOLP384R1, // groupId 97, 48, 0, // pubkeyLen=97, sharedkeyLen=48 (384 bits) TLS10_VERSION_BIT | TLS11_VERSION_BIT | TLS12_VERSION_BIT | DTLS_VERSION_MASK, // versionBits false, }, { "brainpoolP512r1", CRYPT_ECC_BRAINPOOLP512R1, // CRYPT_ECC_BRAINPOOLP512R1 CRYPT_PKEY_ECDH, // CRYPT_PKEY_ECDH 256, // secBits HITLS_EC_GROUP_BRAINPOOLP512R1, // groupId 129, 64, 0, // pubkeyLen=129, sharedkeyLen=64 (512 bits) TLS10_VERSION_BIT | TLS11_VERSION_BIT | TLS12_VERSION_BIT | DTLS_VERSION_MASK, // versionBits false, }, { "sm2", CRYPT_PKEY_PARAID_MAX, // CRYPT_PKEY_PARAID_MAX CRYPT_PKEY_SM2, // CRYPT_PKEY_SM2 128, // secBits HITLS_EC_GROUP_SM2, // groupId 65, 32, 0, // pubkeyLen=65, sharedkeyLen=32 (256 bits) TLCP11_VERSION_BIT | DTLCP11_VERSION_BIT, // versionBits false, }, { "ffdhe8192", CRYPT_DH_RFC7919_8192, // CRYPT_DH_8192 CRYPT_PKEY_DH, // CRYPT_PKEY_DH 192, // secBits HITLS_FF_DHE_8192, // groupId 1024, 1024, 0, // pubkeyLen=1024, sharedkeyLen=1024 (8192 bits) TLS13_VERSION_BIT, // versionBits false, }, { "ffdhe6144", CRYPT_DH_RFC7919_6144, // CRYPT_DH_6144 CRYPT_PKEY_DH, // CRYPT_PKEY_DH 128, // secBits HITLS_FF_DHE_6144, // groupId 768, 768, 0, // pubkeyLen=768, sharedkeyLen=768 (6144 bits) TLS13_VERSION_BIT, // versionBits false, }, { "ffdhe4096", CRYPT_DH_RFC7919_4096, // CRYPT_DH_4096 CRYPT_PKEY_DH, // CRYPT_PKEY_DH 128, // secBits HITLS_FF_DHE_4096, // groupId 512, 512, 0, // pubkeyLen=512, sharedkeyLen=512 (4096 bits) TLS13_VERSION_BIT, // versionBits false, }, { "ffdhe3072", CRYPT_DH_RFC7919_3072, // Fixed constant name CRYPT_PKEY_DH, 128, HITLS_FF_DHE_3072, 384, 384, 0, // pubkeyLen=384, sharedkeyLen=384 (3072 bits) TLS13_VERSION_BIT, false, }, { "ffdhe2048", CRYPT_DH_RFC7919_2048, // CRYPT_DH_2048 CRYPT_PKEY_DH, // CRYPT_PKEY_DH 112, // secBits HITLS_FF_DHE_2048, // groupId 256, 256, 0, // pubkeyLen=256, sharedkeyLen=256 (2048 bits) TLS13_VERSION_BIT, // versionBits false, } }; static int32_t BuildTlsGroupParam(const TLS_GroupInfo *groupInfo, BSL_Param *param) { int32_t ret = 0; RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[0], CRYPT_PARAM_CAP_TLS_GROUP_IANA_GROUP_NAME, BSL_PARAM_TYPE_OCTETS_PTR, (void *)(uintptr_t)groupInfo->name, (uint32_t)strlen(groupInfo->name)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[1], CRYPT_PARAM_CAP_TLS_GROUP_IANA_GROUP_ID, BSL_PARAM_TYPE_UINT16, (void *)(uintptr_t)&(groupInfo->groupId), sizeof(groupInfo->groupId)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[2], CRYPT_PARAM_CAP_TLS_GROUP_PARA_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(groupInfo->paraId), sizeof(groupInfo->paraId)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[3], CRYPT_PARAM_CAP_TLS_GROUP_ALG_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(groupInfo->algId), sizeof(groupInfo->algId)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[4], CRYPT_PARAM_CAP_TLS_GROUP_SEC_BITS, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(groupInfo->secBits), sizeof(groupInfo->secBits)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[5], CRYPT_PARAM_CAP_TLS_GROUP_VERSION_BITS, BSL_PARAM_TYPE_UINT32, (void *)(uintptr_t)&(groupInfo->versionBits), sizeof(groupInfo->versionBits)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[6], CRYPT_PARAM_CAP_TLS_GROUP_IS_KEM, BSL_PARAM_TYPE_BOOL, (void *)(uintptr_t)&(groupInfo->isKem), sizeof(groupInfo->isKem)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[7], CRYPT_PARAM_CAP_TLS_GROUP_PUBKEY_LEN, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(groupInfo->pubkeyLen), sizeof(groupInfo->pubkeyLen)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[8], CRYPT_PARAM_CAP_TLS_GROUP_SHAREDKEY_LEN, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(groupInfo->sharedkeyLen), sizeof(groupInfo->sharedkeyLen)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[9], CRYPT_PARAM_CAP_TLS_GROUP_CIPHERTEXT_LEN, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(groupInfo->ciphertextLen), sizeof(groupInfo->ciphertextLen)), ret); return ret; } static int32_t CryptGetGroupCaps(CRYPT_EAL_ProcessFuncCb cb, void *args) { for (size_t i = 0; i < sizeof(g_tlsGroupInfo) / sizeof(g_tlsGroupInfo[0]); i++) { BSL_Param param[TLS_GROUP_PARAM_COUNT] = {0}; int32_t ret = BuildTlsGroupParam(&g_tlsGroupInfo[i], param); if (ret != BSL_SUCCESS) { return ret; } ret = cb(param, args); if (ret != CRYPT_SUCCESS) { return ret; } } return CRYPT_SUCCESS; } typedef struct { const char *name; // name uint16_t signatureScheme; // HITLS_SignHashAlgo, IANA specified int32_t keyType; // HITLS_CERT_KeyType int32_t paraId; // CRYPT_PKEY_ParaId int32_t signHashAlgId; // combined sign hash algorithm id int32_t signAlgId; // CRYPT_PKEY_AlgId int32_t hashAlgId; // CRYPT_MD_AlgId int32_t secBits; // security bits uint32_t certVersionBits; // TLS_VERSION_MASK uint32_t chainVersionBits; // TLS_VERSION_MASK } TLS_SigSchemeInfo; static const TLS_SigSchemeInfo g_signSchemeInfo[] = { { "ecdsa_secp521r1_sha512", CERT_SIG_SCHEME_ECDSA_SECP521R1_SHA512, TLS_CERT_KEY_TYPE_ECDSA, CRYPT_ECC_NISTP521, BSL_CID_ECDSAWITHSHA512, HITLS_SIGN_ECDSA, HITLS_HASH_SHA_512, 256, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "ecdsa_secp384r1_sha384", CERT_SIG_SCHEME_ECDSA_SECP384R1_SHA384, TLS_CERT_KEY_TYPE_ECDSA, CRYPT_ECC_NISTP384, BSL_CID_ECDSAWITHSHA384, HITLS_SIGN_ECDSA, HITLS_HASH_SHA_384, 192, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "ed25519", CERT_SIG_SCHEME_ED25519, TLS_CERT_KEY_TYPE_ED25519, CRYPT_PKEY_PARAID_MAX, BSL_CID_ED25519, HITLS_SIGN_ED25519, HITLS_HASH_SHA_512, 128, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "ecdsa_secp256r1_sha256", CERT_SIG_SCHEME_ECDSA_SECP256R1_SHA256, TLS_CERT_KEY_TYPE_ECDSA, CRYPT_ECC_NISTP256, BSL_CID_ECDSAWITHSHA256, HITLS_SIGN_ECDSA, HITLS_HASH_SHA_256, 128, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "sm2_sm3", CERT_SIG_SCHEME_SM2_SM3, TLS_CERT_KEY_TYPE_SM2, CRYPT_PKEY_PARAID_MAX, BSL_CID_SM2DSAWITHSM3, HITLS_SIGN_SM2, HITLS_HASH_SM3, 128, TLCP11_VERSION_BIT | DTLCP11_VERSION_BIT, TLCP11_VERSION_BIT | DTLCP11_VERSION_BIT, }, { "rsa_pss_pss_sha512", CERT_SIG_SCHEME_RSA_PSS_PSS_SHA512, TLS_CERT_KEY_TYPE_RSA_PSS, CRYPT_PKEY_PARAID_MAX, BSL_CID_RSASSAPSS, HITLS_SIGN_RSA_PSS, HITLS_HASH_SHA_512, 256, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "rsa_pss_pss_sha384", CERT_SIG_SCHEME_RSA_PSS_PSS_SHA384, TLS_CERT_KEY_TYPE_RSA_PSS, CRYPT_PKEY_PARAID_MAX, BSL_CID_RSASSAPSS, HITLS_SIGN_RSA_PSS, HITLS_HASH_SHA_384, 192, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "rsa_pss_pss_sha256", CERT_SIG_SCHEME_RSA_PSS_PSS_SHA256, TLS_CERT_KEY_TYPE_RSA_PSS, CRYPT_PKEY_PARAID_MAX, BSL_CID_RSASSAPSS, HITLS_SIGN_RSA_PSS, HITLS_HASH_SHA_256, 128, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "rsa_pss_rsae_sha512", CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA512, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_RSASSAPSS, HITLS_SIGN_RSA_PSS, HITLS_HASH_SHA_512, 256, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "rsa_pss_rsae_sha384", CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA384, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_RSASSAPSS, HITLS_SIGN_RSA_PSS, HITLS_HASH_SHA_384, 192, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "rsa_pss_rsae_sha256", CERT_SIG_SCHEME_RSA_PSS_RSAE_SHA256, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_RSASSAPSS, HITLS_SIGN_RSA_PSS, HITLS_HASH_SHA_256, 128, TLS_VERSION_MASK | DTLS_VERSION_MASK, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "rsa_pkcs1_sha512", CERT_SIG_SCHEME_RSA_PKCS1_SHA512, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_SHA512WITHRSAENCRYPTION, HITLS_SIGN_RSA_PKCS1_V15, HITLS_HASH_SHA_512, 256, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "dsa_sha512", CERT_SIG_SCHEME_DSA_SHA512, TLS_CERT_KEY_TYPE_DSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_DSAWITHSHA512, HITLS_SIGN_DSA, HITLS_HASH_SHA_512, 256, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "rsa_pkcs1_sha384", CERT_SIG_SCHEME_RSA_PKCS1_SHA384, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_SHA384WITHRSAENCRYPTION, HITLS_SIGN_RSA_PKCS1_V15, HITLS_HASH_SHA_384, 192, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "dsa_sha384", CERT_SIG_SCHEME_DSA_SHA384, TLS_CERT_KEY_TYPE_DSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_DSAWITHSHA384, HITLS_SIGN_DSA, HITLS_HASH_SHA_384, 192, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "rsa_pkcs1_sha256", CERT_SIG_SCHEME_RSA_PKCS1_SHA256, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_SHA256WITHRSAENCRYPTION, HITLS_SIGN_RSA_PKCS1_V15, HITLS_HASH_SHA_256, 128, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS_VERSION_MASK | DTLS_VERSION_MASK, }, { "dsa_sha256", CERT_SIG_SCHEME_DSA_SHA256, TLS_CERT_KEY_TYPE_DSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_DSAWITHSHA256, HITLS_SIGN_DSA, HITLS_HASH_SHA_256, 128, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "ecdsa_sha224", CERT_SIG_SCHEME_ECDSA_SHA224, TLS_CERT_KEY_TYPE_ECDSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_ECDSAWITHSHA224, HITLS_SIGN_ECDSA, HITLS_HASH_SHA_224, 112, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "rsa_pkcs1_sha224", CERT_SIG_SCHEME_RSA_PKCS1_SHA224, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_SHA224WITHRSAENCRYPTION, HITLS_SIGN_RSA_PKCS1_V15, HITLS_HASH_SHA_224, 112, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "dsa_sha224", CERT_SIG_SCHEME_DSA_SHA224, TLS_CERT_KEY_TYPE_DSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_DSAWITHSHA224, HITLS_SIGN_DSA, HITLS_HASH_SHA_224, 112, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "ecdsa_sha1", CERT_SIG_SCHEME_ECDSA_SHA1, TLS_CERT_KEY_TYPE_ECDSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_ECDSAWITHSHA1, HITLS_SIGN_ECDSA, HITLS_HASH_SHA1, -1, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "rsa_pkcs1_sha1", CERT_SIG_SCHEME_RSA_PKCS1_SHA1, TLS_CERT_KEY_TYPE_RSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_SHA1WITHRSA, HITLS_SIGN_RSA_PKCS1_V15, HITLS_HASH_SHA1, -1, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, { "dsa_sha1", CERT_SIG_SCHEME_DSA_SHA1, TLS_CERT_KEY_TYPE_DSA, CRYPT_PKEY_PARAID_MAX, BSL_CID_DSAWITHSHA1, HITLS_SIGN_DSA, HITLS_HASH_SHA1, -1, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, TLS12_VERSION_BIT | DTLS12_VERSION_BIT, }, }; static int32_t BuildTlsSigAlgParam(const TLS_SigSchemeInfo *sigSchemeInfo, BSL_Param *param) { int32_t ret = 0; RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[0], CRYPT_PARAM_CAP_TLS_SIGNALG_IANA_SIGN_NAME, BSL_PARAM_TYPE_OCTETS_PTR, (void *)(uintptr_t)sigSchemeInfo->name, (uint32_t)strlen(sigSchemeInfo->name)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[1], CRYPT_PARAM_CAP_TLS_SIGNALG_IANA_SIGN_ID, BSL_PARAM_TYPE_UINT16, (void *)(uintptr_t)&(sigSchemeInfo->signatureScheme), sizeof(sigSchemeInfo->signatureScheme)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[2], CRYPT_PARAM_CAP_TLS_SIGNALG_KEY_TYPE, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(sigSchemeInfo->keyType), sizeof(sigSchemeInfo->keyType)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[3], CRYPT_PARAM_CAP_TLS_SIGNALG_PARA_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(sigSchemeInfo->paraId), sizeof(sigSchemeInfo->paraId)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[4], CRYPT_PARAM_CAP_TLS_SIGNALG_SIGNWITHMD_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(sigSchemeInfo->signHashAlgId), sizeof(sigSchemeInfo->signHashAlgId)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[5], CRYPT_PARAM_CAP_TLS_SIGNALG_SIGN_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(sigSchemeInfo->signAlgId), sizeof(sigSchemeInfo->signAlgId)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[6], CRYPT_PARAM_CAP_TLS_SIGNALG_MD_ID, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(sigSchemeInfo->hashAlgId), sizeof(sigSchemeInfo->hashAlgId)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[7], CRYPT_PARAM_CAP_TLS_SIGNALG_SEC_BITS, BSL_PARAM_TYPE_INT32, (void *)(uintptr_t)&(sigSchemeInfo->secBits), sizeof(sigSchemeInfo->secBits)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[8], CRYPT_PARAM_CAP_TLS_SIGNALG_CERT_VERSION_BITS, BSL_PARAM_TYPE_UINT32, (void *)(uintptr_t)&(sigSchemeInfo->certVersionBits), sizeof(sigSchemeInfo->certVersionBits)), ret); RETURN_RET_IF_ERR_EX(BSL_PARAM_InitValue(&param[9], CRYPT_PARAM_CAP_TLS_SIGNALG_CHAIN_VERSION_BITS, BSL_PARAM_TYPE_UINT32, (void *)(uintptr_t)&(sigSchemeInfo->chainVersionBits), sizeof(sigSchemeInfo->chainVersionBits)), ret); return ret; } static int32_t CryptGetSignAlgCaps(CRYPT_EAL_ProcessFuncCb cb, void *args) { for (size_t i = 0; i < sizeof(g_signSchemeInfo) / sizeof(g_signSchemeInfo[0]); i++) { BSL_Param param[TLS_SIGN_SCHEME_PARAM_COUNT] = {0}; int32_t ret = BuildTlsSigAlgParam(&g_signSchemeInfo[i], param); if (ret != BSL_SUCCESS) { return ret; } ret = cb(param, args); if (ret != CRYPT_SUCCESS) { return ret; } } return CRYPT_SUCCESS; } static int32_t CRYPT_EAL_DefaultProvGetCaps(void *provCtx, int32_t cmd, CRYPT_EAL_ProcessFuncCb cb, void *args) { (void)provCtx; if (cb == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (cmd) { case CRYPT_EAL_GET_GROUP_CAP: return CryptGetGroupCaps(cb, args); case CRYPT_EAL_GET_SIGALG_CAP: return CryptGetSignAlgCaps(cb, args); default: BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } } #endif static CRYPT_EAL_Func g_defEalProvOutFuncs[] = { {CRYPT_EAL_PROVCB_QUERY, CRYPT_EAL_DefaultProvQuery}, {CRYPT_EAL_PROVCB_FREE, CRYPT_EAL_DefaultProvFree}, {CRYPT_EAL_PROVCB_CTRL, NULL}, #ifdef HITLS_TLS_FEATURE_PROVIDER {CRYPT_EAL_PROVCB_GETCAPS, CRYPT_EAL_DefaultProvGetCaps}, #endif CRYPT_EAL_FUNC_END }; #ifdef HITLS_CRYPTO_ENTROPY static void *g_providerSeedCtx = NULL; static CRYPT_RandSeedMethod g_providerSeedMethod = {0}; int32_t CRYPT_EAL_ProviderGetSeed(CRYPT_RandSeedMethod **method, void **seedCtx) { if (method == NULL || seedCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } *method = &g_providerSeedMethod; *seedCtx = g_providerSeedCtx; return CRYPT_SUCCESS; } #endif int32_t CRYPT_EAL_DefaultProvInit(CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, CRYPT_EAL_Func *capFuncs, CRYPT_EAL_Func **outFuncs, void **provCtx) { (void)param; void *libCtx = NULL; CRYPT_EAL_ProvMgrCtrlCb mgrCtrl = NULL; int32_t index = 0; int32_t ret; while (capFuncs[index].id != 0) { switch (capFuncs[index].id) { #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT case CRYPT_EAL_CAP_GETENTROPY: g_providerSeedMethod.getEntropy = capFuncs[index].func; break; case CRYPT_EAL_CAP_CLEANENTROPY: g_providerSeedMethod.cleanEntropy = capFuncs[index].func; break; case CRYPT_EAL_CAP_GETNONCE: g_providerSeedMethod.getNonce = capFuncs[index].func; break; case CRYPT_EAL_CAP_CLEANNONCE: g_providerSeedMethod.cleanNonce = capFuncs[index].func; break; #endif case CRYPT_EAL_CAP_MGRCTXCTRL: mgrCtrl = capFuncs[index].func; break; default: break; } index++; } if (mgrCtrl == NULL) { return CRYPT_PROVIDER_NOT_SUPPORT; } #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT RETURN_RET_IF_ERR_EX(mgrCtrl(mgrCtx, CRYPT_EAL_MGR_GETSEEDCTX, &g_providerSeedCtx, 0), ret); #endif RETURN_RET_IF_ERR_EX(mgrCtrl(mgrCtx, CRYPT_EAL_MGR_GETLIBCTX, &libCtx, 0), ret); CRYPT_EAL_DefProvCtx *temp = BSL_SAL_Malloc(sizeof(CRYPT_EAL_DefProvCtx)); if (temp == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } temp->libCtx = libCtx; *provCtx = temp; *outFuncs = g_defEalProvOutFuncs; return CRYPT_SUCCESS; } #endif /* HITLS_CRYPTO_PROVIDER */
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_provider.c
C
unknown
43,723
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief default provider header */ #ifndef CRYPT_EAL_DEFAULT_PROVIDER_H #define CRYPT_EAL_DEFAULT_PROVIDER_H #ifdef HITLS_CRYPTO_PROVIDER #include <stdint.h> #include "crypt_eal_implprovider.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct EalDefProvCtx { void *libCtx; } CRYPT_EAL_DefProvCtx; int32_t CRYPT_EAL_ProviderGetSeed(CRYPT_RandSeedMethod **method, void **seedCtx); #ifdef __cplusplus } #endif // __cplusplus #endif /* HITLS_CRYPTO_PROVIDER */ #endif // CRYPT_EAL_DEFAULT_PROVIDER_H
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_provider.h
C
unknown
1,128
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_DRBG) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_drbg.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "bsl_log_internal.h" #include "bsl_err_internal.h" #include "crypt_ealinit.h" #include "bsl_params.h" #include "crypt_default_provider.h" #ifdef HITLS_CRYPTO_ENTROPY static int32_t GetDefaultSeed(BSL_Param *param) { void *defaultSeedCtx = NULL; CRYPT_RandSeedMethod *defaultSeedMethod = NULL; int32_t ret = CRYPT_EAL_ProviderGetSeed(&defaultSeedMethod, &defaultSeedCtx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)BSL_PARAM_InitValue(&param[0], CRYPT_PARAM_RAND_SEEDCTX, BSL_PARAM_TYPE_CTX_PTR, defaultSeedCtx, 0); (void)BSL_PARAM_InitValue(&param[1], CRYPT_PARAM_RAND_SEED_GETENTROPY, BSL_PARAM_TYPE_FUNC_PTR, defaultSeedMethod->getEntropy, 0); (void)BSL_PARAM_InitValue(&param[2], CRYPT_PARAM_RAND_SEED_CLEANENTROPY, BSL_PARAM_TYPE_FUNC_PTR, defaultSeedMethod->cleanEntropy, 0); (void)BSL_PARAM_InitValue(&param[3], CRYPT_PARAM_RAND_SEED_GETNONCE, BSL_PARAM_TYPE_FUNC_PTR, defaultSeedMethod->getNonce, 0); (void)BSL_PARAM_InitValue(&param[4], CRYPT_PARAM_RAND_SEED_CLEANNONCE, BSL_PARAM_TYPE_FUNC_PTR, defaultSeedMethod->cleanNonce, 0); return CRYPT_SUCCESS; } #endif void *CRYPT_EAL_DefRandNewCtx(CRYPT_EAL_DefProvCtx *provCtx, int32_t algId, BSL_Param *param) { void *libCtx = provCtx == NULL ? NULL : provCtx->libCtx; void *randCtx = NULL; #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Drbg(algId) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return NULL; } #endif BSL_Param *getEnt = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_GETENTROPY); BSL_Param *cleanEnt = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_CLEANENTROPY); BSL_Param *getNonce = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_GETNONCE); BSL_Param *cleanNonce = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEED_CLEANNONCE); BSL_Param *ctx = BSL_PARAM_FindParam(param, CRYPT_PARAM_RAND_SEEDCTX); /** * If you use a registered entropy source, the getEntropy callback cannot be NULL, * and if getEntropy is NULL, cleanEntropy, getNonce, cleanNonce, etc. must be NULL */ if (getEnt == NULL && ((cleanEnt != NULL && cleanEnt->value != NULL) || (getNonce != NULL && getNonce->value != NULL) || (cleanNonce != NULL && cleanNonce->value != NULL) || (ctx != NULL && ctx->value != NULL))) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } if (param == NULL || getEnt == NULL) { #ifdef HITLS_CRYPTO_ENTROPY BSL_Param defaultParam[6] = {BSL_PARAM_END}; if (GetDefaultSeed(defaultParam) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } return DRBG_New(libCtx, algId, defaultParam); #else BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; #endif } randCtx = DRBG_New(libCtx, algId, param); if (randCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return NULL; } return randCtx; } const CRYPT_EAL_Func g_defEalRand[] = { {CRYPT_EAL_IMPLRAND_DRBGNEWCTX, (CRYPT_EAL_ImplRandDrbgNewCtx)CRYPT_EAL_DefRandNewCtx}, {CRYPT_EAL_IMPLRAND_DRBGINST, (CRYPT_EAL_ImplRandDrbgInst)DRBG_Instantiate}, {CRYPT_EAL_IMPLRAND_DRBGUNINST, (CRYPT_EAL_ImplRandDrbgUnInst)DRBG_Uninstantiate}, {CRYPT_EAL_IMPLRAND_DRBGGEN, (CRYPT_EAL_ImplRandDrbgGen)DRBG_GenerateBytes}, {CRYPT_EAL_IMPLRAND_DRBGRESEED, (CRYPT_EAL_ImplRandDrbgReSeed)DRBG_Reseed}, {CRYPT_EAL_IMPLRAND_DRBGCTRL, (CRYPT_EAL_ImplRandDrbgCtrl)DRBG_Ctrl}, {CRYPT_EAL_IMPLRAND_DRBGFREECTX, (CRYPT_EAL_ImplRandDrbgFreeCtx)DRBG_Free}, CRYPT_EAL_FUNC_END, }; #endif /* HITLS_CRYPTO_DRBG && HITLS_CRYPTO_PROVIDER */
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_rand.c
C
unknown
4,519
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if (defined(HITLS_CRYPTO_DSA) || defined(HITLS_CRYPTO_ED25519) || defined(HITLS_CRYPTO_RSA_SIGN) || \ defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_ECDSA) || defined(HITLS_CRYPTO_SM2_SIGN) || \ defined(HITLS_CRYPTO_SLH_DSA) || defined(HITLS_CRYPTO_MLDSA)) && defined(HITLS_CRYPTO_PROVIDER) #include "crypt_eal_implprovider.h" #include "crypt_dsa.h" #include "crypt_rsa.h" #include "crypt_ecdsa.h" #include "crypt_sm2.h" #include "crypt_curve25519.h" #include "crypt_slh_dsa.h" #include "crypt_mldsa.h" #ifdef HITLS_CRYPTO_DSA const CRYPT_EAL_Func g_defEalSignDsa[] = { {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_DSA_Sign}, {CRYPT_EAL_IMPLPKEYSIGN_SIGNDATA, (CRYPT_EAL_ImplPkeySignData)CRYPT_DSA_SignData}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_DSA_Verify}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFYDATA, (CRYPT_EAL_ImplPkeyVerifyData)CRYPT_DSA_VerifyData}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ED25519 const CRYPT_EAL_Func g_defEalSignEd25519[] = { {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_CURVE25519_Sign}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_CURVE25519_Verify}, CRYPT_EAL_FUNC_END, }; #endif #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) const CRYPT_EAL_Func g_defEalSignRsa[] = { #ifdef HITLS_CRYPTO_RSA_SIGN {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_RSA_Sign}, {CRYPT_EAL_IMPLPKEYSIGN_SIGNDATA, (CRYPT_EAL_ImplPkeySignData)CRYPT_RSA_SignData}, #endif #ifdef HITLS_CRYPTO_RSA_VERIFY {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_RSA_Verify}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFYDATA, (CRYPT_EAL_ImplPkeyVerifyData)CRYPT_RSA_VerifyData}, {CRYPT_EAL_IMPLPKEYSIGN_RECOVER, (CRYPT_EAL_ImplPkeyRecover)CRYPT_RSA_Recover}, #endif #ifdef HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_SIGN {CRYPT_EAL_IMPLPKEYSIGN_BLIND, (CRYPT_EAL_ImplPkeyBlind)CRYPT_RSA_Blind}, #endif #ifdef HITLS_CRYPTO_RSA_VERIFY {CRYPT_EAL_IMPLPKEYSIGN_UNBLIND, (CRYPT_EAL_ImplPkeyUnBlind)CRYPT_RSA_UnBlind}, #endif #endif CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_ECDSA const CRYPT_EAL_Func g_defEalSignEcdsa[] = { {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_ECDSA_Sign}, {CRYPT_EAL_IMPLPKEYSIGN_SIGNDATA, (CRYPT_EAL_ImplPkeySignData)CRYPT_ECDSA_SignData}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_ECDSA_Verify}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFYDATA, (CRYPT_EAL_ImplPkeyVerifyData)CRYPT_ECDSA_VerifyData}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SM2_SIGN const CRYPT_EAL_Func g_defEalSignSm2[] = { {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_SM2_Sign}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_SM2_Verify}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_SLH_DSA const CRYPT_EAL_Func g_defEalSignSlhDsa[] = { {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_SLH_DSA_Sign}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_SLH_DSA_Verify}, CRYPT_EAL_FUNC_END, }; #endif #ifdef HITLS_CRYPTO_MLDSA const CRYPT_EAL_Func g_defEalSignMlDsa[] = { {CRYPT_EAL_IMPLPKEYSIGN_SIGN, (CRYPT_EAL_ImplPkeySign)CRYPT_ML_DSA_Sign}, {CRYPT_EAL_IMPLPKEYSIGN_VERIFY, (CRYPT_EAL_ImplPkeyVerify)CRYPT_ML_DSA_Verify}, CRYPT_EAL_FUNC_END, }; #endif #endif
2301_79861745/bench_create
crypto/provider/src/default/crypt_default_sign.c
C
unknown
3,986
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_PROVIDER #include <stdlib.h> #include "securec.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_eal_provider.h" #include "crypt_eal_implprovider.h" #include "crypt_provider_local.h" #include "crypt_provider.h" #include "crypt_drbg_local.h" // Name of the dl initialization function #define PROVIDER_INIT_FUNC "CRYPT_EAL_ProviderInit" // Maximum length of search path static uint32_t g_threadRunOnce = 0; CRYPT_EAL_LibCtx *CRYPT_EAL_LibCtxNew(void) { return CRYPT_EAL_LibCtxNewInternal(); } // Free EAL_LibCtx context void CRYPT_EAL_LibCtxFree(CRYPT_EAL_LibCtx *libCtx) { if (libCtx == NULL) { return; } #ifdef HITLS_CRYPTO_DRBG if (libCtx->drbg != NULL) { EAL_RandDeinit(libCtx->drbg); libCtx->drbg = NULL; } #endif if (libCtx->providers != NULL) { BSL_LIST_FREE(libCtx->providers, (BSL_LIST_PFUNC_FREE)CRYPT_EAL_ProviderMgrCtxFree); } if (libCtx->lock != NULL) { BSL_SAL_ThreadLockFree(libCtx->lock); } BSL_SAL_FREE(libCtx->searchProviderPath); BSL_SAL_Free(libCtx); } static void InitPreDefinedProviders(void) { CRYPT_EAL_LibCtx *globalCtx = CRYPT_EAL_GetGlobalLibCtx(); if (globalCtx == NULL) { int32_t ret = CRYPT_EAL_InitPreDefinedProviders(); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } } } static CRYPT_EAL_LibCtx *GetCurrentProviderLibCtx(CRYPT_EAL_LibCtx *libCtx) { CRYPT_EAL_LibCtx *curLibCtx = libCtx; if (curLibCtx == NULL) { int32_t ret = BSL_SAL_ThreadRunOnce(&g_threadRunOnce, InitPreDefinedProviders); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } curLibCtx = CRYPT_EAL_GetGlobalLibCtx(); } return curLibCtx; } // Write a function to search for providers according to BSL_LIST_Search requirements, // comparing the input providerName with the providerName in EAL_ProviderMgrCtx for an exact match static int32_t ListCompareProvider(const void *a, const void *b) { const CRYPT_EAL_ProvMgrCtx *ctx = (const CRYPT_EAL_ProvMgrCtx *)a; const char *providerName = (const char *)b; return (strcmp(ctx->providerName, providerName) == 0) ? 0 : 1; } static int32_t CheckProviderLoaded(CRYPT_EAL_LibCtx *libCtx, const char *providerName, bool increaseRef, CRYPT_EAL_ProvMgrCtx **providerMgr) { int32_t ret = BSL_SAL_ThreadReadLock(libCtx->lock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } CRYPT_EAL_ProvMgrCtx *tempProviderMgr = (CRYPT_EAL_ProvMgrCtx *)BSL_LIST_Search(libCtx->providers, providerName, ListCompareProvider, NULL); if (tempProviderMgr != NULL && increaseRef) { // Provider is already loaded, increase the reference count int32_t tempCount = 0; ret = BSL_SAL_AtomicUpReferences(&tempProviderMgr->ref, &tempCount); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); (void)BSL_SAL_ThreadUnlock(libCtx->lock); return ret; } } (void)BSL_SAL_ThreadUnlock(libCtx->lock); *providerMgr = tempProviderMgr; return CRYPT_SUCCESS; } static bool IsEalPreDefinedProvider(const char *providerName) { const char *preProvider[] = {CRYPT_EAL_DEFAULT_PROVIDER}; for (size_t i = 0; i < sizeof(preProvider) / sizeof(preProvider[0]); i++) { if (strcmp(preProvider[i], providerName) == 0) { return true; } } return false; } static int32_t FindProviderInitFunc(CRYPT_EAL_LibCtx *libCtx, char *providerName, void **initFunc) { uint32_t pathLen = BSL_SAL_Strnlen(providerName, DEFAULT_PROVIDER_NAME_LEN_MAX) + BSL_SAL_Strnlen(libCtx->searchProviderPath, DEFAULT_PROVIDER_PATH_LEN_MAX) + 1; if (pathLen > DEFAULT_PROVIDER_PATH_LEN_MAX) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } char *providerPath = providerName; int32_t ret; // Construct the full path of the provider if (libCtx->searchProviderPath != NULL) { providerPath = (char *)BSL_SAL_Calloc(pathLen + 1, sizeof(char)); if (providerPath == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = snprintf_s(providerPath, pathLen + 1, pathLen, "%s/%s", libCtx->searchProviderPath, providerName); if (ret < 0) { BSL_SAL_Free(providerPath); BSL_ERR_PUSH_ERROR(ret); return ret; } } // Attempt to load the dynamic library void *handle = NULL; ret = BSL_SAL_LoadLib(providerPath, &handle); if (libCtx->searchProviderPath != NULL) { BSL_SAL_Free(providerPath); } if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // Attempt to get the initialization function ret = BSL_SAL_GetFuncAddress(handle, PROVIDER_INIT_FUNC, initFunc); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } // Load provider dynamic library int32_t CRYPT_EAL_ProviderLoad(CRYPT_EAL_LibCtx *libCtx, BSL_SAL_LibFmtCmd cmd, const char *providerName, BSL_Param *param, CRYPT_EAL_ProvMgrCtx **mgrCtx) { if (providerName == NULL || (mgrCtx != NULL && *mgrCtx != NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_EAL_LibCtx *localCtx = GetCurrentProviderLibCtx(libCtx); if (localCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_INVALID_LIB_CTX); return CRYPT_PROVIDER_INVALID_LIB_CTX; } CRYPT_EAL_ProvMgrCtx *providerMgr = NULL; char *providerFullName = NULL; int32_t ret = BSL_SAL_LibNameFormat(cmd, providerName, &providerFullName); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // Check if the provider is already loaded ret = CheckProviderLoaded(localCtx, providerFullName, true, &providerMgr); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(providerFullName); return ret; } if (providerMgr != NULL) { BSL_SAL_Free(providerFullName); if (mgrCtx != NULL) { *mgrCtx = providerMgr; } return CRYPT_SUCCESS; } CRYPT_EAL_ImplProviderInit initFunc = NULL; if (IsEalPreDefinedProvider(providerFullName)) { initFunc = CRYPT_EAL_DefaultProvInit; } else { ret = FindProviderInitFunc(localCtx, providerFullName, (void **)&initFunc); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(providerFullName); return ret; } } ret = CRYPT_EAL_AddNewProvMgrCtx(localCtx, providerFullName, localCtx->searchProviderPath, initFunc, param, mgrCtx); BSL_SAL_Free(providerFullName); return ret; } int32_t CRYPT_EAL_ProviderRegister(CRYPT_EAL_LibCtx *libCtx, const char *providerName, CRYPT_EAL_ImplProviderInit init, BSL_Param *param, CRYPT_EAL_ProvMgrCtx **mgrCtx) { if (providerName == NULL || (mgrCtx != NULL && *mgrCtx != NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } uint32_t providerNameLen = BSL_SAL_Strnlen(providerName, DEFAULT_PROVIDER_NAME_LEN_MAX + 1); if (providerNameLen == DEFAULT_PROVIDER_NAME_LEN_MAX + 1 || providerNameLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } CRYPT_EAL_ProvMgrCtx *providerMgr = NULL; CRYPT_EAL_ImplProviderInit initFunc = init; CRYPT_EAL_LibCtx *localCtx = GetCurrentProviderLibCtx(libCtx); if (localCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_INVALID_LIB_CTX); return CRYPT_PROVIDER_INVALID_LIB_CTX; } int32_t ret = CheckProviderLoaded(localCtx, providerName, true, &providerMgr); if (ret != CRYPT_SUCCESS) { return ret; } if (providerMgr != NULL) { if (mgrCtx != NULL) { *mgrCtx = providerMgr; } return CRYPT_SUCCESS; } if (IsEalPreDefinedProvider(providerName)) { initFunc = CRYPT_EAL_DefaultProvInit; } else { if (initFunc == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } } return CRYPT_EAL_AddNewProvMgrCtx(localCtx, providerName, NULL, initFunc, param, mgrCtx); } int32_t CRYPT_EAL_ProviderIsLoaded(CRYPT_EAL_LibCtx *libCtx, BSL_SAL_LibFmtCmd cmd, const char *providerName, bool *isLoaded) { if (providerName == NULL || isLoaded == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_EAL_LibCtx *localCtx = GetCurrentProviderLibCtx(libCtx); if (localCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_INVALID_LIB_CTX); return CRYPT_PROVIDER_INVALID_LIB_CTX; } char *providerFullName = NULL; int32_t ret = BSL_SAL_LibNameFormat(cmd, providerName, &providerFullName); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // Check if the provider is already loaded CRYPT_EAL_ProvMgrCtx *providerMgr = NULL; ret = CheckProviderLoaded(localCtx, providerFullName, false, &providerMgr); BSL_SAL_Free(providerFullName); if (ret != CRYPT_SUCCESS) { return ret; } *isLoaded = providerMgr != NULL ? true : false; return CRYPT_SUCCESS; } // Remove provider from the list static void RemoveAndFreeProvider(BslList *providers, CRYPT_EAL_ProvMgrCtx *providerMgr) { BslListNode *node = BSL_LIST_FirstNode(providers); while (node != NULL) { if (BSL_LIST_GetData(node) == providerMgr) { BSL_LIST_DetachNode(providers, &node); break; } node = BSL_LIST_GetNextNode(providers, node); } CRYPT_EAL_ProviderMgrCtxFree(providerMgr); } // Unload provider int32_t CRYPT_EAL_ProviderUnload(CRYPT_EAL_LibCtx *libCtx, BSL_SAL_LibFmtCmd cmd, const char *providerName) { if (providerName == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_EAL_LibCtx *localCtx = libCtx; if (localCtx == NULL) { localCtx = CRYPT_EAL_GetGlobalLibCtx(); } if (localCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_INVALID_LIB_CTX); return CRYPT_PROVIDER_INVALID_LIB_CTX; } char *providerFullName = NULL; int32_t ret = BSL_SAL_LibNameFormat(cmd, providerName, &providerFullName); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // Search for the specified provider ret = BSL_SAL_ThreadReadLock(localCtx->lock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_FREE(providerFullName); return ret; } CRYPT_EAL_ProvMgrCtx *providerMgr = (CRYPT_EAL_ProvMgrCtx *)BSL_LIST_Search(localCtx->providers, providerFullName, ListCompareProvider, NULL); BSL_SAL_FREE(providerFullName); if (providerMgr == NULL) { (void)BSL_SAL_ThreadUnlock(localCtx->lock); return CRYPT_SUCCESS; } // Decrease reference count int refCount = 0; ret = BSL_SAL_AtomicDownReferences(&providerMgr->ref, &refCount); if (ret != BSL_SUCCESS) { (void)BSL_SAL_ThreadUnlock(localCtx->lock); BSL_ERR_PUSH_ERROR(ret); return ret; } if (refCount <= 0) { RemoveAndFreeProvider(localCtx->providers, providerMgr); } (void)BSL_SAL_ThreadUnlock(localCtx->lock); return CRYPT_SUCCESS; } // Set the path for loading providers int32_t CRYPT_EAL_ProviderSetLoadPath(CRYPT_EAL_LibCtx *libCtx, const char *searchPath) { if (BSL_SAL_Strnlen(searchPath, DEFAULT_PROVIDER_PATH_LEN_MAX) >= DEFAULT_PROVIDER_PATH_LEN_MAX) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } CRYPT_EAL_LibCtx *localCtx = GetCurrentProviderLibCtx(libCtx); if (localCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_INVALID_LIB_CTX); return CRYPT_PROVIDER_INVALID_LIB_CTX; } char *tempPath = NULL; if (searchPath != NULL) { tempPath = BSL_SAL_Dump(searchPath, BSL_SAL_Strnlen(searchPath, DEFAULT_PROVIDER_PATH_LEN_MAX) + 1); if (tempPath == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } BSL_SAL_FREE(localCtx->searchProviderPath); localCtx->searchProviderPath = tempPath; return CRYPT_SUCCESS; } static int32_t GetProviderUserCtx(CRYPT_EAL_ProvMgrCtx *ctx, void **val) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } *val = ctx->provCtx; return CRYPT_SUCCESS; } int32_t CRYPT_EAL_ProviderCtrl(CRYPT_EAL_ProvMgrCtx *ctx, int32_t cmd, void *val, uint32_t valLen) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (cmd == CRYPT_PROVIDER_GET_USER_CTX) { return GetProviderUserCtx(ctx, val); } if (ctx->provCtrlCb == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return CRYPT_PROVIDER_NOT_SUPPORT; } return ctx->provCtrlCb(ctx->provCtx, cmd, val, valLen); } int32_t CRYPT_EAL_ProviderGetCaps(CRYPT_EAL_ProvMgrCtx *ctx, int32_t cmd, CRYPT_EAL_ProcessFuncCb cb, void *args) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->provGetCap == NULL) { return CRYPT_SUCCESS; } return ctx->provGetCap(ctx->provCtx, cmd, cb, args); } int32_t CRYPT_EAL_ProviderProcessAll(CRYPT_EAL_LibCtx *ctx, CRYPT_EAL_ProviderProcessCb cb, void *args) { if (cb == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_EAL_LibCtx *localCtx = ctx; if (localCtx == NULL) { localCtx = CRYPT_EAL_GetGlobalLibCtx(); } if (localCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_INVALID_LIB_CTX); return CRYPT_PROVIDER_INVALID_LIB_CTX; } BslListNode *node = BSL_LIST_FirstNode(localCtx->providers); while (node != NULL) { CRYPT_EAL_ProvMgrCtx *providerMgr = (CRYPT_EAL_ProvMgrCtx *)BSL_LIST_GetData(node); int32_t ret = cb(providerMgr, args); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } node = BSL_LIST_GetNextNode(localCtx->providers, node); } return CRYPT_SUCCESS; } int32_t CRYPT_EAL_ProviderQuery(CRYPT_EAL_ProvMgrCtx *ctx, int32_t operaId, CRYPT_EAL_AlgInfo **algInfos) { if (ctx == NULL || algInfos == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->provQueryCb == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_SUPPORT); return CRYPT_PROVIDER_NOT_SUPPORT; } return ctx->provQueryCb(ctx->provCtx, operaId, algInfos); } #endif // HITLS_CRYPTO_PROVIDER
2301_79861745/bench_create
crypto/provider/src/mgr/crypt_provider.c
C
unknown
15,597
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifdef HITLS_CRYPTO_PROVIDER #include "securec.h" #include "bsl_list.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "eal_entropy.h" #include "crypt_eal_entropy.h" #include "crypt_drbg_local.h" #include "crypt_drbg.h" #include "crypt_provider_local.h" #include "crypt_provider.h" static CRYPT_EAL_LibCtx *g_libCtx = NULL; CRYPT_EAL_LibCtx *CRYPT_EAL_GetGlobalLibCtx(void) { return g_libCtx; } int32_t CRYPT_EAL_ProviderGetFuncs(CRYPT_EAL_LibCtx *libCtx, int32_t operaId, int32_t algId, const char *attribute, const CRYPT_EAL_Func **funcs, void **provCtx) { CRYPT_EAL_ProvMgrCtx *mgrCtx = NULL; int32_t ret = CRYPT_EAL_ProviderGetFuncsAndMgrCtx(libCtx, operaId, algId, attribute, funcs, &mgrCtx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (mgrCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_FOUND); return CRYPT_PROVIDER_NOT_FOUND; } if (provCtx != NULL) { *provCtx = mgrCtx->provCtx; } return CRYPT_SUCCESS; } int32_t CRYPT_EAL_ProviderGetFuncsAndMgrCtx(CRYPT_EAL_LibCtx *libCtx, int32_t operaId, int32_t algId, const char *attribute, const CRYPT_EAL_Func **funcs, CRYPT_EAL_ProvMgrCtx **mgrCtx) { if (funcs == NULL || mgrCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_EAL_LibCtx *localCtx = libCtx; if (localCtx == NULL) { localCtx = g_libCtx; } if (localCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (attribute != NULL && strlen(attribute) > (INT32_MAX >> 1)) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_ERR_ATTRIBUTE); return CRYPT_PROVIDER_ERR_ATTRIBUTE; } return CRYPT_EAL_CompareAlgAndAttr(localCtx, operaId, algId, attribute, funcs, mgrCtx); } int32_t CRYPT_EAL_ProvMgrCtrl(CRYPT_EAL_ProvMgrCtx *ctx, int32_t cmd, void *val, uint32_t valLen) { (void)valLen; if (ctx == NULL || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } switch (cmd) { #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT case CRYPT_EAL_MGR_GETSEEDCTX: *(void **)val = ctx->providerSeed.seed; return CRYPT_SUCCESS; #endif case CRYPT_EAL_MGR_GETLIBCTX: *(void **)val = ctx->libCtx; return CRYPT_SUCCESS; default: BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_NOT_FOUND); return CRYPT_PROVIDER_NOT_FOUND; } } static void MountMgrMethod(CRYPT_EAL_Func *funcs, CRYPT_EAL_ProvMgrCtx *ctx) { // Mount function addresses to corresponding positions in mgr according to method definition for (uint32_t i = 0; funcs[i].id != 0; i++) { switch (funcs[i].id) { case CRYPT_EAL_PROVCB_FREE: ctx->provFreeCb = (CRYPT_EAL_ProvFreeCb)funcs[i].func; break; case CRYPT_EAL_PROVCB_QUERY: ctx->provQueryCb = (CRYPT_EAL_ProvQueryCb)funcs[i].func; break; case CRYPT_EAL_PROVCB_CTRL: ctx->provCtrlCb = (CRYPT_EAL_ProvCtrlCb)funcs[i].func; break; case CRYPT_EAL_PROVCB_GETCAPS: ctx->provGetCap = (CRYPT_EAL_ProvGetCapsCb)funcs[i].func; break; default: break; } } } #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT static void ProviderSeedDeinit(EAL_SeedDrbg *seedDrbg) { if (seedDrbg == NULL) { return; } if (seedDrbg->seed != NULL) { EAL_SeedDrbgRandDeinit(seedDrbg->seed); seedDrbg->seed = NULL; CRYPT_EAL_SeedPoolFree(seedDrbg->seedCtx); seedDrbg->seedCtx = NULL; BSL_SAL_ReferencesFree(&(seedDrbg->references)); (void)memset_s(seedDrbg, sizeof(EAL_SeedDrbg), 0, sizeof(EAL_SeedDrbg)); } } #endif // Function to get provider methods int32_t CRYPT_EAL_InitProviderMethod(CRYPT_EAL_ProvMgrCtx *ctx, BSL_Param *param, CRYPT_EAL_ImplProviderInit providerInit) { int32_t ret; #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT CRYPT_RandSeedMethod meth = {0}; // The implementer of provider may not use the default entropy source (void)EAL_SeedDrbgEntropyMeth(&meth); ctx->providerSeed.id = HITLS_SEED_DRBG_INIT_RAND_ALG; ret = EAL_SeedDrbgInit(&(ctx->providerSeed)); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } #endif // Construct input method structure array CRYPT_EAL_Func capFuncs[] = { #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT {CRYPT_EAL_CAP_GETENTROPY, (CRYPT_EAL_GetEntropyCb)meth.getEntropy}, {CRYPT_EAL_CAP_CLEANENTROPY, (CRYPT_EAL_CleanEntropyCb)meth.cleanEntropy}, {CRYPT_EAL_CAP_GETNONCE, (CRYPT_EAL_GetNonceCb)meth.getNonce}, {CRYPT_EAL_CAP_CLEANNONCE, (CRYPT_EAL_CleanNonceCb)meth.cleanNonce}, #endif {CRYPT_EAL_CAP_MGRCTXCTRL, (CRYPT_EAL_ProvMgrCtrlCb)CRYPT_EAL_ProvMgrCtrl}, CRYPT_EAL_FUNC_END // End marker }; CRYPT_EAL_Func *outFuncs = NULL; // Call CRYPT_EAL_ImplProviderInit to get methods ret = providerInit(ctx, param, capFuncs, &outFuncs, &ctx->provCtx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } if (outFuncs == NULL) { ret = CRYPT_PROVIDER_ERR_UNEXPECTED_IMPL; BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_ERR_UNEXPECTED_IMPL); goto ERR; } MountMgrMethod(outFuncs, ctx); if (ctx->provQueryCb == NULL) { if (ctx->provFreeCb != NULL) { ctx->provFreeCb(ctx->provCtx); ctx->provCtx = NULL; } ret = CRYPT_PROVIDER_ERR_IMPL_NULL; BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_ERR_IMPL_NULL); goto ERR; } return CRYPT_SUCCESS; ERR: #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT ProviderSeedDeinit(&(ctx->providerSeed)); #endif return ret; } CRYPT_EAL_LibCtx *CRYPT_EAL_LibCtxNewInternal(void) { CRYPT_EAL_LibCtx *libCtx = (CRYPT_EAL_LibCtx *)BSL_SAL_Calloc(1, sizeof(CRYPT_EAL_LibCtx)); if (libCtx == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return NULL; } // Initialize providers list libCtx->providers = BSL_LIST_New(sizeof(struct EAL_ProviderMgrCtx *)); if (libCtx->providers == NULL) { goto ERR; } // Initialize thread lock if (BSL_SAL_ThreadLockNew(&libCtx->lock) != BSL_SUCCESS) { BSL_LIST_FREE(libCtx->providers, NULL); goto ERR; } return libCtx; ERR: BSL_SAL_Free(libCtx); libCtx = NULL; return NULL; } void CRYPT_EAL_ProviderMgrCtxFree(CRYPT_EAL_ProvMgrCtx *ctx) { if (ctx == NULL) { return; } if (ctx->provFreeCb != NULL) { ctx->provFreeCb(ctx->provCtx); ctx->provCtx = NULL; } BSL_SAL_FREE(ctx->providerName); BSL_SAL_FREE(ctx->providerPath); BSL_SAL_ReferencesFree(&(ctx->ref)); if (ctx->handle != NULL) { BSL_SAL_UnLoadLib(ctx->handle); ctx->handle = NULL; } #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT ProviderSeedDeinit(&(ctx->providerSeed)); #endif BSL_SAL_Free(ctx); } // Add provider to the list static int32_t AddProviderToList(CRYPT_EAL_LibCtx *libCtx, CRYPT_EAL_ProvMgrCtx *providerMgr) { int32_t ret = BSL_SAL_ThreadWriteLock(libCtx->lock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BSL_LIST_AddElement(libCtx->providers, providerMgr, BSL_LIST_POS_END); (void)BSL_SAL_ThreadUnlock(libCtx->lock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t CRYPT_EAL_AddNewProvMgrCtx(CRYPT_EAL_LibCtx *libCtx, const char *providerName, const char *providerPath, CRYPT_EAL_ImplProviderInit init, BSL_Param *param, CRYPT_EAL_ProvMgrCtx **ctx) { CRYPT_EAL_ProvMgrCtx *mgrCtx = (CRYPT_EAL_ProvMgrCtx *)BSL_SAL_Calloc(1, sizeof(CRYPT_EAL_ProvMgrCtx)); if (mgrCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } mgrCtx->libCtx = libCtx; mgrCtx->providerName = BSL_SAL_Dump(providerName, BSL_SAL_Strnlen(providerName, DEFAULT_PROVIDER_NAME_LEN_MAX) + 1); if (mgrCtx->providerName == NULL) { BSL_SAL_Free(mgrCtx); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (providerPath != NULL) { mgrCtx->providerPath = BSL_SAL_Dump(providerPath, BSL_SAL_Strnlen(providerPath, DEFAULT_PROVIDER_PATH_LEN_MAX) + 1); if (mgrCtx->providerPath == NULL) { BSL_SAL_Free(mgrCtx->providerName); BSL_SAL_Free(mgrCtx); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } int32_t ret = BSL_SAL_ReferencesInit(&mgrCtx->ref); if (ret != BSL_SUCCESS) { CRYPT_EAL_ProviderMgrCtxFree(mgrCtx); BSL_ERR_PUSH_ERROR(ret); return ret; } ret = AddProviderToList(libCtx, mgrCtx); if (ret != CRYPT_SUCCESS) { CRYPT_EAL_ProviderMgrCtxFree(mgrCtx); return ret; } ret = CRYPT_EAL_InitProviderMethod(mgrCtx, param, init); if (ret != BSL_SUCCESS) { BSL_LIST_DeleteCurrent(libCtx->providers, (BSL_LIST_PFUNC_FREE)CRYPT_EAL_ProviderMgrCtxFree); return ret; } if (ctx != NULL) { *ctx = mgrCtx; } return ret; } int32_t CRYPT_EAL_InitPreDefinedProviders(void) { CRYPT_EAL_LibCtx *libCtx = CRYPT_EAL_LibCtxNewInternal(); if (libCtx == NULL) { BSL_ERR_PUSH_ERROR(BSL_MALLOC_FAIL); return BSL_MALLOC_FAIL; } g_libCtx = libCtx; int32_t ret = CRYPT_EAL_AddNewProvMgrCtx(libCtx, CRYPT_EAL_DEFAULT_PROVIDER, NULL, CRYPT_EAL_DefaultProvInit, NULL, NULL); if (ret != CRYPT_SUCCESS) { BSL_LIST_FREE(libCtx->providers, NULL); BSL_SAL_ThreadLockFree(libCtx->lock); BSL_SAL_FREE(g_libCtx); return ret; } return ret; } void CRYPT_EAL_FreePreDefinedProviders(void) { CRYPT_EAL_LibCtx *libCtx = g_libCtx; if (libCtx == NULL) { return; } #ifdef HITLS_CRYPTO_DRBG if (libCtx->drbg != NULL) { EAL_RandDeinit(libCtx->drbg); libCtx->drbg = NULL; } #endif // Free the providers list and each EAL_ProviderMgrCtx in it if (libCtx->providers != NULL) { BSL_LIST_FREE(libCtx->providers, (BSL_LIST_PFUNC_FREE)CRYPT_EAL_ProviderMgrCtxFree); } BSL_SAL_FREE(libCtx->searchProviderPath); // Free thread lock if (libCtx->lock != NULL) { BSL_SAL_ThreadLockFree(libCtx->lock); libCtx->lock = NULL; } // Free the libctx structure itself BSL_SAL_Free(libCtx); g_libCtx = NULL; } #endif /* HITLS_CRYPTO_PROVIDER */
2301_79861745/bench_create
crypto/provider/src/mgr/crypt_provider_common.c
C
unknown
11,323
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_PROVIDER #include <string.h> #include "securec.h" #include "bsl_list.h" #include "bsl_err_internal.h" #include "bsl_hash.h" #include "list_base.h" #include "crypt_errno.h" #include "crypt_provider.h" #include "crypt_provider_local.h" #include "crypt_eal_provider.h" #include "crypt_eal_implprovider.h" #define HISH_SIZE 8 #define NOT_EQUAL_SIZE 2 // Store the information of the input attribute string typedef struct { const char *attribute; // Attribute string BSL_HASH_Hash *hash; // Hash table uint32_t attributeNum; // Number of attributes uint32_t mustAttributeNum; // Number of mandatory attributes bool repeatFlag; // Repeat search flag } InputAttributeStrInfo; // Define the data structure for values typedef struct { char *judgeStr; // Judge string char *valueStr; // Value string } AttributeValue; // Define a function to free AttributeValue resources static void AttributeValueFree(void *ptr) { if (ptr == NULL) { return; } AttributeValue *value = (AttributeValue *)ptr; BSL_SAL_FREE(value->judgeStr); BSL_SAL_FREE(value->valueStr); BSL_SAL_Free(value); } // Define a function to free the value list static void FreeValueList(void *ptr) { if (ptr == NULL) { return; } BslList *valueList = (BslList *)ptr; BSL_LIST_DeleteAll(valueList, AttributeValueFree); BSL_SAL_Free(valueList); } // Value copy callback function, essentially a function to create a new value list static void *ValueDupFunc(void *ptr, size_t size) { if (ptr == NULL || size == 0) { return NULL; } int32_t ret; AttributeValue *srcValue = (AttributeValue *)ptr; BslList *newList = BSL_LIST_New(sizeof(AttributeValue)); if (newList == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } AttributeValue *newValue = BSL_SAL_Calloc(1, sizeof(AttributeValue)); if (newValue == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } newValue->judgeStr = BSL_SAL_Dump(srcValue->judgeStr, BSL_SAL_Strnlen(srcValue->judgeStr, UINT32_MAX) + 1); newValue->valueStr = BSL_SAL_Dump(srcValue->valueStr, BSL_SAL_Strnlen(srcValue->valueStr, UINT32_MAX) + 1); if (newValue->judgeStr == NULL || newValue->valueStr == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } ret = BSL_LIST_AddElement(newList, newValue, BSL_LIST_POS_END); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } return newList; ERR: AttributeValueFree(newValue); BSL_SAL_Free(newList); return NULL; } // Key copy callback function static void *KeyDupFunc(void *ptr, size_t size) { if (ptr == NULL || size == 0) { return NULL; } return BSL_SAL_Dump(ptr, size); } // Key release callback function static void KeyFreeFunc(void *ptr) { BSL_SAL_FREE(ptr); } // Implement the BSL_HASH_UpdateNodeFunc callback function for BSL_HASH_Put to call static int32_t UpdateAttributeValueNode(BSL_HASH_Hash *hash, BSL_HASH_Iterator node, uintptr_t value, uint32_t valueSize) { if (hash == NULL || valueSize == 0) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } AttributeValue *srcValue = (AttributeValue *)value; AttributeValue *newValue = BSL_SAL_Calloc(1, sizeof(AttributeValue)); if (newValue == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } newValue->judgeStr = BSL_SAL_Dump(srcValue->judgeStr, BSL_SAL_Strnlen(srcValue->judgeStr, UINT32_MAX) + 1); newValue->valueStr = BSL_SAL_Dump(srcValue->valueStr, BSL_SAL_Strnlen(srcValue->valueStr, UINT32_MAX) + 1); if (newValue->judgeStr == NULL || newValue->valueStr == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); AttributeValueFree(newValue); return CRYPT_MEM_ALLOC_FAIL; } BslList *valueList = (BslList *)BSL_HASH_IterValue(hash, node); int32_t ret = BSL_LIST_AddElement(valueList, (AttributeValue *)newValue, BSL_LIST_POS_END); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); AttributeValueFree(newValue); } return ret; } // Get key-value pair positions static int32_t GetAttributePositions(const char *attribute, int32_t start, int32_t *keyStart, int32_t *keyEnd, int32_t *judgeStart, int32_t *judgeEnd, int32_t *valueStart, int32_t *valueEnd) { int32_t temp = start; *keyStart = *keyEnd = *judgeStart = *judgeEnd = *valueStart = *valueEnd = temp; // Find key while (attribute[temp] != '\0' && attribute[temp] != '=' && attribute[temp] != '?' && attribute[temp] != '!') { temp++; } *keyEnd = temp; if (*keyEnd <= *keyStart) { return CRYPT_PROVIDER_ERR_ATTRIBUTE; } // Find judge string *judgeStart = temp; if (attribute[temp] == '!' && attribute[temp + 1] == '=') { temp = temp + NOT_EQUAL_SIZE; } else if (attribute[temp] == '=' || attribute[temp] == '?') { temp++; } else { return CRYPT_PROVIDER_ERR_ATTRIBUTE; } *judgeEnd = temp; if (*judgeEnd <= *judgeStart) { return CRYPT_PROVIDER_ERR_ATTRIBUTE; } // Find value *valueStart = temp; while (attribute[temp] != '\0' && attribute[temp] != ',') { temp++; } *valueEnd = temp; if (*valueEnd <= *valueStart) { return CRYPT_PROVIDER_ERR_ATTRIBUTE; } return CRYPT_SUCCESS; } static int32_t ParseAttributeValue(const char *attribute, int32_t *startPos, char **key, AttributeValue **value) { int32_t start = *startPos; char *tempKey = NULL; AttributeValue *tempValue = NULL; // Call the sub-function to get key-value pair positions int32_t keyStart, keyEnd, judgeStart, judgeEnd, valueStart, valueEnd; int32_t ret = GetAttributePositions(attribute, start, &keyStart, &keyEnd, &judgeStart, &judgeEnd, &valueStart, &valueEnd); if (ret != CRYPT_SUCCESS) { return ret; } int32_t keyLen = keyEnd - keyStart; int32_t judgeLen = judgeEnd - judgeStart; int32_t valueLen = valueEnd - valueStart; // Allocate space for keys and values tempValue = BSL_SAL_Calloc(1, sizeof(AttributeValue)); if (tempValue == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } tempKey = BSL_SAL_Calloc(1, keyLen + 1); tempValue->judgeStr = BSL_SAL_Calloc(1, judgeLen + 1); tempValue->valueStr = BSL_SAL_Calloc(1, valueLen + 1); if (tempKey == NULL || tempValue->judgeStr == NULL || tempValue->valueStr == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } // Copy the string corresponding to the key and value if (memcpy_s(tempKey, keyLen + 1, attribute + keyStart, keyLen) != EOK || memcpy_s(tempValue->judgeStr, judgeLen + 1, attribute + judgeStart, judgeLen) != EOK || memcpy_s(tempValue->valueStr, valueLen + 1, attribute + valueStart, valueLen) != EOK) { ret = CRYPT_SECUREC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } *startPos = attribute[valueEnd] == '\0' ? valueEnd : valueEnd + 1; *key = tempKey; *value = tempValue; return CRYPT_SUCCESS; ERR: BSL_SAL_FREE(tempKey); AttributeValueFree(tempValue); return ret; } static int32_t ParseAttributeString(InputAttributeStrInfo *attrInfo) { int32_t ret; const char *attribute = attrInfo->attribute; BSL_HASH_Hash *hash = NULL; int32_t startPos = 0; char *key = NULL; AttributeValue *value = NULL; uint32_t tempMustAttributeNum = 0, tempAttributeNum = 0; ListDupFreeFuncPair keyFunc = {KeyDupFunc, KeyFreeFunc}; ListDupFreeFuncPair valueFunc = {ValueDupFunc, FreeValueList}; // Create a hash table with a bucket size of 8 hash = BSL_HASH_Create(HISH_SIZE, BSL_HASH_CodeCalcStr, BSL_HASH_MatchStr, &keyFunc, &valueFunc); if (hash == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } while (attribute[startPos] != '\0') { // Extract a key-value pair ret = ParseAttributeValue(attribute, &startPos, &key, &value); // An unknown error occurred during the lookup if (ret != CRYPT_SUCCESS) { BSL_HASH_Destory(hash); return ret; } tempAttributeNum++; if (value->judgeStr[0] == '=' || value->judgeStr[0] == '!') { tempMustAttributeNum++; } ret = BSL_HASH_Put(hash, (uintptr_t)key, BSL_SAL_Strnlen(key, UINT32_MAX) + 1, (uintptr_t)value, sizeof(AttributeValue), UpdateAttributeValueNode); BSL_SAL_FREE(key); AttributeValueFree(value); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_HASH_Destory(hash); return ret; } } if (tempAttributeNum == 0) { BSL_ERR_PUSH_ERROR(CRYPT_PROVIDER_ERR_ATTRIBUTE); BSL_HASH_Destory(hash); return CRYPT_PROVIDER_ERR_ATTRIBUTE; } attrInfo->attributeNum = tempAttributeNum; attrInfo->mustAttributeNum = tempMustAttributeNum; attrInfo->hash = hash; return CRYPT_SUCCESS; } static int32_t CompareAttributeValue(AttributeValue *value, AttributeValue *hashValue, uint32_t *comparedCount, uint32_t *satisfiedMustCount, int32_t *totalScore) { if (hashValue->judgeStr[0] == '=') { if (strcmp(value->valueStr, hashValue->valueStr) == 0) { (*comparedCount)++; (*satisfiedMustCount)++; } else { return -1; } } else if (hashValue->judgeStr[0] == '!' && hashValue->judgeStr[1] == '=') { if (strcmp(value->valueStr, hashValue->valueStr) != 0) { (*comparedCount)++; (*satisfiedMustCount)++; } else { return -1; } } else if (hashValue->judgeStr[0] == '?') { (*comparedCount)++; if (strcmp(value->valueStr, hashValue->valueStr) == 0) { (*totalScore)++; } } return 0; } static int32_t CompareAttribute(BSL_HASH_Hash *hash, const char *attribute, uint32_t mustAttributeNum, uint32_t attributeNum) { int32_t ret; int32_t startPos = 0; char *key = NULL; AttributeValue *value = NULL; uint32_t comparedCount = 0; uint32_t satisfiedMustCount = 0; int32_t totalScore = 0; while (attribute[startPos] != '\0') { ret = ParseAttributeValue(attribute, &startPos, &key, &value); if (ret != CRYPT_SUCCESS) { break; } BSL_HASH_Iterator it = BSL_HASH_Find(hash, (uintptr_t)key); if (it == BSL_HASH_IterEnd(hash)) { BSL_SAL_Free(key); AttributeValueFree(value); continue; } BslList *valueList = (BslList *)BSL_HASH_IterValue(hash, it); for (void *listValue = BSL_LIST_GET_FIRST(valueList); listValue != NULL; listValue = BSL_LIST_GET_NEXT(valueList)) { AttributeValue *hashValue = (AttributeValue *)listValue; ret = CompareAttributeValue(value, hashValue, &comparedCount, &satisfiedMustCount, &totalScore); if (ret == -1) { BSL_SAL_Free(key); AttributeValueFree(value); return -1; } if (comparedCount == attributeNum) { BSL_SAL_Free(key); AttributeValueFree(value); return (satisfiedMustCount < mustAttributeNum) ? -1 : totalScore; } } BSL_SAL_Free(key); AttributeValueFree(value); } if (satisfiedMustCount < mustAttributeNum) { return -1; } return totalScore; } static void FindHighestScoreFunc(CRYPT_EAL_LibCtx *localCtx, int32_t operaId, int32_t algId, InputAttributeStrInfo attrInfo, const CRYPT_EAL_Func **implFunc, CRYPT_EAL_ProvMgrCtx **mgrCtx) { int32_t ret; int32_t totalScore = -1; int32_t index = 0; const char *attribute = attrInfo.attribute; BSL_HASH_Hash *hash = attrInfo.hash; uint32_t attributeNum = attrInfo.attributeNum; uint32_t mustAttributeNum = attrInfo.mustAttributeNum; uint32_t repeatFlag = attrInfo.repeatFlag; CRYPT_EAL_ProvMgrCtx *node = BSL_LIST_GET_FIRST(localCtx->providers); for (; node!= NULL; node = BSL_LIST_GET_NEXT(localCtx->providers)) { CRYPT_EAL_AlgInfo *algInfos = NULL; ret = node->provQueryCb(node->provCtx, operaId, &algInfos); if (ret != CRYPT_SUCCESS) { continue; } for (index = 0; algInfos != NULL && algInfos[index].algId != 0; index++) { if (algInfos[index].algId != algId) { continue; } if (attribute == NULL) { *implFunc = algInfos[index].implFunc; *mgrCtx = node; return; } int32_t tempScore; tempScore = CompareAttribute(hash, algInfos[index].attr, mustAttributeNum, attributeNum); if (tempScore <= totalScore) { continue; } totalScore = tempScore; *implFunc = algInfos[index].implFunc; *mgrCtx = node; if (repeatFlag) { continue; } return; } } } int32_t CRYPT_EAL_CompareAlgAndAttr(CRYPT_EAL_LibCtx *localCtx, int32_t operaId, int32_t algId, const char *attribute, const CRYPT_EAL_Func **funcs, CRYPT_EAL_ProvMgrCtx **mgrCtx) { int32_t ret; const CRYPT_EAL_Func *implFunc = NULL; CRYPT_EAL_ProvMgrCtx *ctx = NULL; InputAttributeStrInfo attrInfo = {0}; if (attribute != NULL) { attrInfo.attribute = attribute; ret = ParseAttributeString(&attrInfo); if (ret != BSL_SUCCESS) { return ret; } attrInfo.repeatFlag = (attrInfo.attributeNum != attrInfo.mustAttributeNum) ? true : false; } ret = BSL_SAL_ThreadWriteLock(localCtx->lock); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_HASH_Destory(attrInfo.hash); return ret; } FindHighestScoreFunc(localCtx, operaId, algId, attrInfo, &implFunc, &ctx); BSL_SAL_ThreadUnlock(localCtx->lock); BSL_HASH_Destory(attrInfo.hash); if (implFunc == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } *funcs = implFunc; if (mgrCtx != NULL) { *mgrCtx = ctx; } return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_PROVIDER
2301_79861745/bench_create
crypto/provider/src/mgr/crypt_provider_compare.c
C
unknown
15,323
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief Internal use of provider */ #ifndef CRYPT_EAL_PROVIDER_LOCAL_H #define CRYPT_EAL_PROVIDER_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_PROVIDER #include <stdint.h> #include "sal_atomic.h" #include "crypt_eal_implprovider.h" #include "bsl_list.h" #include "crypt_drbg_local.h" #include "crypt_provider.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define DEFAULT_PROVIDER_PATH_LEN_MAX 4095 struct EAL_ProviderMgrCtx { void *handle; // so handle void *provCtx; BSL_SAL_RefCount ref; char *providerName; char *providerPath; #ifdef HITLS_CRYPTO_ENTROPY_DEFAULT EAL_SeedDrbg providerSeed; // entropy ctx #endif struct EAL_LibCtx *libCtx; CRYPT_EAL_ImplProviderInit provInitFunc; // out funcs CRYPT_EAL_ProvFreeCb provFreeCb; CRYPT_EAL_ProvQueryCb provQueryCb; CRYPT_EAL_ProvCtrlCb provCtrlCb; CRYPT_EAL_ProvGetCapsCb provGetCap; }; int32_t CRYPT_EAL_InitProviderMethod(CRYPT_EAL_ProvMgrCtx *ctx, BSL_Param *param, CRYPT_EAL_ImplProviderInit providerInit); CRYPT_EAL_LibCtx *CRYPT_EAL_LibCtxNewInternal(void); int32_t CRYPT_EAL_CompareAlgAndAttr(CRYPT_EAL_LibCtx *localCtx, int32_t operaId, int32_t algId, const char *attribute, const CRYPT_EAL_Func **funcs, CRYPT_EAL_ProvMgrCtx **mgrCtx); void CRYPT_EAL_ProviderMgrCtxFree(CRYPT_EAL_ProvMgrCtx *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif /* HITLS_CRYPTO_PROVIDER */ #endif // CRYPT_EAL_PROVIDER_LOCAL_H
2301_79861745/bench_create
crypto/provider/src/mgr/crypt_provider_local.h
C
unknown
2,060
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_RSA_H #define CRYPT_RSA_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include <stdlib.h> #include <stdint.h> #include "crypt_local_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define RSA_MIN_MODULUS_BITS 1024 #define RSA_MAX_MODULUS_BITS 16384 #define RSA_SMALL_MODULUS_BYTES (3072 / 8) #define RSA_MAX_PUBEXP_BYTES (64 / 8) #define RSA_MIN_MODULUS_LEN (RSA_MIN_MODULUS_BITS / 8) #define RSA_MAX_MODULUS_LEN (RSA_MAX_MODULUS_BITS / 8) /* RSA */ typedef struct RSA_Ctx CRYPT_RSA_Ctx; typedef struct RSA_Para CRYPT_RSA_Para; /* RSA method */ /** * @ingroup rsa * @brief Allocate rsa context memory space. * * @retval (CRYPT_RSA_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_RSA_Ctx *CRYPT_RSA_NewCtx(void); // create key structure /** * @ingroup rsa * @brief Allocate rsa context memory space. * * @param libCtx [IN] Library context * * @retval (CRYPT_RSA_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_RSA_Ctx *CRYPT_RSA_NewCtxEx(void *libCtx); /** * @ingroup rsa * @brief Copy the RSA context. After the duplication is complete, call the CRYPT_RSA_FreeCtx to release the memory. * * @param ctx [IN] RSA context * * @return CRYPT_RSA_Ctx Rsa context pointer * If the operation fails, a null value is returned. */ CRYPT_RSA_Ctx *CRYPT_RSA_DupCtx(CRYPT_RSA_Ctx *keyCtx); /** * @ingroup rsa * @brief Create rsa key parameter structure * * @param para [IN] RSA External parameter * * @retval (CRYPT_RSA_Para *) Pointer to the allocated memory space of the structure * @retval NULL Invalid null pointer. */ CRYPT_RSA_Para *CRYPT_RSA_NewParaEx(const BSL_Param *para); /** * @ingroup rsa * @brief Release rsa key parameter structure * * @param para [IN] Storage pointer in the parameter structure to be released. The parameter is set NULL by the invoker. */ void CRYPT_RSA_FreePara(CRYPT_RSA_Para *para); /** * @ingroup rsa * @brief release rsa key context structure * * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker. */ void CRYPT_RSA_FreeCtx(CRYPT_RSA_Ctx *ctx); /** * @ingroup rsa * @brief Set the data of the key parameter structure to the key structure. * * @param ctx [OUT] Key structure for which related parameters need to be set * @param para [IN] Key parameter structure * * @retval CRYPT_NULL_INPUT Invalid null pointer input. * @retval CRYPT_RSA_ERR_KEY_BITS The expected key length does not meet the requirements. * @retval CRYPT_RSA_ERR_E_VALUE The expected value of e does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL internal memory allocation error * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_RSA_SetPara(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPara *para); /** * @ingroup rsa * @brief Obtain the valid length of the key. * * @param ctx [IN] Structure from which the key length is expected to be obtained * * @retval 0: The input is incorrect or the corresponding key structure does not have a valid key length. * @retval uint32_t: Valid key length */ uint32_t CRYPT_RSA_GetBits(const CRYPT_RSA_Ctx *ctx); #ifdef HITLS_CRYPTO_RSA_GEN /** * @ingroup rsa * @brief Generate the RSA key pair. * * @param ctx [IN/OUT] rsa context structure * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The value of e in the context structure does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The key pair is successfully generated. */ int32_t CRYPT_RSA_Gen(CRYPT_RSA_Ctx *ctx); #endif #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_RSA_SIGN) /** * @ingroup rsa * @brief RSA public key encryption * * @param ctx [IN] RSA context structure * @param input [IN] Information to be encrypted * @param inputLen [IN] Length of the information to be encrypted * @param out [OUT] Pointer to the encrypted information output. * @param outLen [IN/OUT] Pointer to the length of the encrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS encryption succeeded. */ int32_t CRYPT_RSA_PubEnc(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); #endif /** * @ingroup rsa * @brief RSA private key decryption * * @param ctx [IN] RSA context structure * @param input [IN] Information to be decrypted * @param inputLen [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the decrypted information output. * @param outLen [IN/OUT] Pointer to the length of the decrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_ERR_DEC_BITS Incorrect length of the encrypted private key. * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS Decrypted Successfully */ int32_t CRYPT_RSA_PrvDec(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); /** * @ingroup rsa * @brief RSA Set the private key information. * * @param ctx [OUT] rsa context structure * @param prv [IN] Private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is successfully set. */ int32_t CRYPT_RSA_SetPrvKey(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPrv *prv); /** * @ingroup rsa * @brief RSA Set the public key information. * * @param ctx [OUT] RSA context structure * @param pub [IN] Public key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is successfully set. */ int32_t CRYPT_RSA_SetPubKey(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPub *pub); /** * @ingroup rsa * @brief RSA Obtain the private key information. * * @param ctx [IN] RSA context structure * @param prv [OUT] Private key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is obtained successfully. */ int32_t CRYPT_RSA_GetPrvKey(const CRYPT_RSA_Ctx *ctx, CRYPT_RsaPrv *prv); /** * @ingroup rsa * @brief RSA Obtain the public key information. * * @param ctx [IN] RSA context structure * @param pub [OUT] Public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is obtained successfully. */ int32_t CRYPT_RSA_GetPubKey(const CRYPT_RSA_Ctx *ctx, CRYPT_RsaPub *pub); #ifdef HITLS_BSL_PARAMS /** * @ingroup rsa * @brief RSA Set the private key information. * * @param ctx [OUT] rsa context structure * @param para [IN] Private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is successfully set. */ int32_t CRYPT_RSA_SetPrvKeyEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); /** * @ingroup rsa * @brief RSA Set the public key information. * * @param ctx [OUT] RSA context structure * @param para [IN] Public key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is successfully set. */ int32_t CRYPT_RSA_SetPubKeyEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); /** * @ingroup rsa * @brief RSA Obtain the private key information. * * @param ctx [IN] RSA context structure * @param para [OUT] Private key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is obtained successfully. */ int32_t CRYPT_RSA_GetPrvKeyEx(const CRYPT_RSA_Ctx *ctx, BSL_Param *para); /** * @ingroup rsa * @brief RSA Obtain the public key information. * * @param ctx [IN] RSA context structure * @param para [OUT] Public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is obtained successfully. */ int32_t CRYPT_RSA_GetPubKeyEx(const CRYPT_RSA_Ctx *ctx, BSL_Param *para); /** * @ingroup rsa * @brief Set the data of the key parameter structure to the key structure. * * @param ctx [OUT] Key structure for which related parameters need to be set * @param para [IN] Key parameter structure * * @retval CRYPT_NULL_INPUT Invalid null pointer input. * @retval CRYPT_RSA_ERR_KEY_BITS The expected key length does not meet the requirements. * @retval CRYPT_RSA_ERR_E_VALUE The expected value of e does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL internal memory allocation error * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_RSA_SetParaEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); #endif int32_t CRYPT_RSA_Ctrl(CRYPT_RSA_Ctx *ctx, int32_t opt, void *val, uint32_t len); #ifdef HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_SIGN /** * @ingroup RSA * @brief RSA blind operation for blind signature * * @param ctx [IN] RSA Context structure * @param algId [IN] hash Id for input * @param input [IN] Message to be blinded * @param inputLen [IN] Length of input message * @param out [OUT] Blinded message * @param outLen [OUT] Length of blinded message * * @retval CRYPT_SUCCESS on success * For other error codes, see crypt_errno.h. */ int32_t CRYPT_RSA_Blind(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup RSA * @brief RSA unblind operation for blind signature * * @param ctx [IN] RSA Context structure * @param input [IN] Blind signature to be unblinded * @param inputLen [IN] Length of blind signature * @param out [OUT] Final unblinded signature * @param outLen [OUT] Length of unblinded signature * * @retval CRYPT_SUCCESS on success * For other error codes, see crypt_errno.h. */ int32_t CRYPT_RSA_UnBlind(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); #endif #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_BSSA) /** * @ingroup rsa * @brief Set the PSS for the original data. * * @param ctx [IN] CRYPT_RSA_Ctx * @param hashMethod [IN] pss Required Hash Method * @param mgfMethod [IN] pss Internal hash method required by the mgf. * @param saltLen [IN] Length of the input salt. * @param data [IN] Original data * @param dataLen [IN] Length of the original data * @param pad [OUT] pss Output buffer * @param padLen [OUT] Maximum length of the array output by the PSS. * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_PSS_SALT_DATA The salt value does not meet the requirements. * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_RSA_ERR_PSS_SALT_LEN The salt length does not meet the requirements. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length of the reserved buffer is insufficient. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SUCCESS Succeeded in setting the PSS. */ int32_t CRYPT_RSA_SetPss(CRYPT_RSA_Ctx *ctx, const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t saltLen, const uint8_t *data, uint32_t dataLen, uint8_t *pad, uint32_t padLen); #endif // HITLS_CRYPTO_RSA_SIGN || HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup rsa * @brief Compare the original data from the PSS. * * @param ctx [IN] CRYPT_RSA_Ctx * @param hashMethod [IN] pss Required the hash method * @param mgfMethod [IN] pss Internal hash method required by the mgf. * @param saltLen [IN] Salt value length * @param data [IN] Original data * @param dataLen [IN] Length of the original data * @param pad [IN] Data after PSS is set. * @param padLen [IN] Data length after PSS is set. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_ERR_PSS_SALT_DATA The salt value does not meet the requirements. * @retval CRYPT_RSA_ERR_PSS_SALT_LEN The salt length does not meet the requirements. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required for padding does not match the input parameter. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SUCCESS pss comparison succeeded. */ int32_t CRYPT_RSA_VerifyPss(CRYPT_RSA_Ctx *ctx, const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t saltLen, const uint8_t *data, uint32_t dataLen, const uint8_t *pad, uint32_t padLen); #endif // HITLS_CRYPTO_RSA_VERIFY #endif // HITLS_CRYPTO_RSA_EMSA_PSS #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 /** * @ingroup rsa * @brief Set pkcsv1.5 padding. * * @param hashId [IN] the hash method required by pkcsv1.5 setting. * @param data [IN] Original data * @param dataLen [IN] Length of the original data * @param pad [OUT] Pointer to the array for receiving the padding. * @param padLen [IN] Array length for receiving padding. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO The key information is insufficient. * @retval CRYPT_SECUREC_FAIL The security function fails. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required by the padding does not match the input parameter. * @retval CRYPT_RSA_ERR_INPUT_VALUE The hash algorithm ID is not supported. * @retval CRYPT_SUCCESS The pkcsv1.5 padding is successfully set. */ int32_t CRYPT_RSA_SetPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *data, uint32_t dataLen, uint8_t *pad, uint32_t padLen); #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup rsa * @brief Verify pkcsv1.5 padding. * * @param hashId [IN] the hash method corresponding to pkcsv1.5 verification. * @param pad [IN] Data after padding * @param padLen [IN] Data length after padding * @param data [IN] Original data * @param dataLen [IN] Length of the original data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_ERR_PKCSV15_SALT_DATA Incorrect padding value. * @retval CRYPT_SECUREC_FAIL Security Function Failure * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required for padding does not match the input parameter. * @retval CRYPT_RSA_ERR_INPUT_VALUE The hash algorithm ID is not supported. * @retval CRYPT_SUCCESS Verify pkcsv1.5 is padded successfully. */ int32_t CRYPT_RSA_VerifyPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *pad, uint32_t padLen, const uint8_t *data, uint32_t dataLen); #endif // HITLS_CRYPTO_RSA_VERIFY #endif // HITLS_CRYPTO_RSA_EMSA_PKCSV15 #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) /** * @ingroup rsa * @brief Obtain the maximum length of RSA signature data. * * @param ctx [IN] Maximum length of the RSA signature data that is expected to be obtained * * @retval 0 The input is incorrect or the corresponding key structure does not contain valid key information. * @retval uint32_t Maximum length of the signature data */ uint32_t CRYPT_RSA_GetSignLen(const CRYPT_RSA_Ctx *ctx); #endif #ifdef HITLS_CRYPTO_RSA_VERIFY int32_t CRYPT_RSA_VerifyData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); int32_t CRYPT_RSA_Verify(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); #endif #ifdef HITLS_CRYPTO_RSA_SIGN int32_t CRYPT_RSA_SignData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); int32_t CRYPT_RSA_Sign(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); #endif #ifdef HITLS_CRYPTO_RSA_ENCRYPT /** * @ingroup rsa * @brief RSA public key encryption * * @param ctx [IN] RSA context structure * @param data [IN] Information to be encrypted * @param dataLen [IN] Length of the information to be encrypted * @param out [OUT] Pointer to the encrypted information output. * @param outLen [OUT] Pointer to the length of the encrypted information * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A safe function error occurs. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the encryption method. * @retval CRYPT_SUCCESS encryption succeeded. */ int32_t CRYPT_RSA_Encrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSA_DECRYPT /** * @ingroup rsa * @brief RSA private key decryption * * @param ctx [IN] RSA context structure * @param data [IN] Information to be decrypted * @param dataLen [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the output information after decryption. * @param outLen [OUT] Pointer to the length of the decrypted information * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the decryption method. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS Decryption succeeded. */ int32_t CRYPT_RSA_Decrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSA_VERIFY /** * @ingroup rsa * @brief RSA public key decryption * * @param ctx [IN] RSA context structure * @param data [IN] Information to be decrypted * @param dataLen [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the output information after decryption. * @param outLen [IN/OUT] Pointer to the length of the decrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. * @retval CRYPT_RSA_PAD_NO_SET_ERROR The padding type is not set. * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The space is insufficient after decryption. * @retval CRYPT_RSA_ERR_INPUT_VALUE The input parameter does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval Other error codes, for example, the CRYPT_RSA_UnPackPkcsV15Type1 de-padding function. * @retval CRYPT_SUCCESS Decrypted Successfully */ int32_t CRYPT_RSA_Recover(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); #endif /** * @ingroup rsa * @brief RSA compare the public key * * @param a [IN] RSA context structure * @param b [IN] RSA context structure * * @retval CRYPT_SUCCESS is the same * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_RSA_NO_KEY_INFO No public key * @retval CRYPT_RSA_PUBKEY_NOT_EQUAL Public Keys are not equal */ int32_t CRYPT_RSA_Cmp(const CRYPT_RSA_Ctx *a, const CRYPT_RSA_Ctx *b); #ifdef HITLS_CRYPTO_RSAES_OAEP #ifdef HITLS_CRYPTO_RSA_ENCRYPT /** * @ingroup rsa * @brief oaep padding * * @param hashMethod [IN] Hash method. Only sha1, sha244, sha256, sha384, and sha512 are supported. * @param mgfMethod [IN] Hash method required by mgf * @param in [IN] Original data * @param inLen [IN] Original data length * @param param [IN] oaep parameter, which can be null * @param paramLen [IN] oaep Parameter length * @param pad [IN] Data after padding * @param padLen [IN] Data length after padding * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * */ int32_t CRYPT_RSA_SetPkcs1Oaep(CRYPT_RSA_Ctx *ctx, const uint8_t *in, uint32_t inLen, uint8_t *pad, uint32_t padLen); #endif // HITLS_CRYPTO_RSA_ENCRYPT #ifdef HITLS_CRYPTO_RSA_DECRYPT /** * @ingroup rsa * @brief Verify the oaep padding. * * @param pad [IN] oaep parameter, which can be null * @param in [IN] Data after padding * @param inLen [IN] Data length after padding * @param param [IN] oaep parameter, which can be null * @param paramLen [IN] oaep Parameter length * @param msg [IN] Data after the de-padding * @param msgLen [IN/OUT] The input parameter is the length of the msg buffer, * and the output parameter is the length of the msg after the de-padding. * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * */ int32_t CRYPT_RSA_VerifyPkcs1Oaep(RSA_PadingPara *pad, const uint8_t *in, uint32_t inLen, const uint8_t *param, uint32_t paramLen, uint8_t *msg, uint32_t *msgLen); #endif // HITLS_CRYPTO_RSA_DECRYPT #endif // HITLS_CRYPTO_RSAES_OAEP #if defined(HITLS_CRYPTO_RSA_ENCRYPT) && \ (defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) || defined(HITLS_CRYPTO_RSAES_PKCSV15)) int32_t CRYPT_RSA_SetPkcsV15Type2(void *libCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t outLen); #endif #ifdef HITLS_CRYPTO_RSA_DECRYPT #ifdef HITLS_CRYPTO_RSAES_PKCSV15 int32_t CRYPT_RSA_VerifyPkcsV15Type2(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS int32_t CRYPT_RSA_VerifyPkcsV15Type2TLS(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #endif #endif // HITLS_CRYPTO_RSA_DECRYPT /** * @ingroup rsa * @brief rsa get security bits * * @param ctx [IN] rsa Context structure * * @retval security bits */ int32_t CRYPT_RSA_GetSecBits(const CRYPT_RSA_Ctx *ctx); #ifdef HITLS_CRYPTO_RSA_CHECK /** * @ingroup rsa * @brief check the key pair consistency * * @param checkType [IN] check type * @param pkey1 [IN] rsa key context structure * @param pkey2 [IN] rsa key context structure * * @retval CRYPT_SUCCESS check success. * Others. For details, see error code in errno. */ int32_t CRYPT_RSA_Check(uint32_t checkType, const CRYPT_RSA_Ctx *pkey1, const CRYPT_RSA_Ctx *pkey2); #endif // HITLS_CRYPTO_RSA_CHECK #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup RSA * @brief RSA import key * * @param ctx [IN/OUT] RSA context structure * @param params [IN] parameters */ int32_t CRYPT_RSA_Import(CRYPT_RSA_Ctx *ctx, const BSL_Param *params); /** * @ingroup RSA * @brief RSA export key * * @param ctx [IN] RSA context structure * @param params [IN/OUT] key parameters */ int32_t CRYPT_RSA_Export(const CRYPT_RSA_Ctx *ctx, BSL_Param *params); #endif // HITLS_CRYPTO_PROVIDER #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_RSA #endif // CRYPT_RSA_H
2301_79861745/bench_create
crypto/rsa/include/crypt_rsa.h
C
unknown
27,743
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_RSA_BLINDING) || defined(HITLS_CRYPTO_RSA_BSSA) #include "crypt_utils.h" #include "crypt_rsa.h" #include "rsa_local.h" #include "crypt_errno.h" #include "bsl_sal.h" #include "securec.h" #include "bsl_err_internal.h" RSA_Blind *RSA_BlindNewCtx(void) { RSA_Blind *ret = BSL_SAL_Malloc(sizeof(RSA_Blind)); if (ret == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(ret, sizeof(RSA_Blind), 0, sizeof(RSA_Blind)); return ret; } void RSA_BlindFreeCtx(RSA_Blind *b) { if (b == NULL) { return; } BN_Destroy(b->r); BN_Destroy(b->rInv); BSL_SAL_FREE(b); } static int32_t BlindUpdate(RSA_Blind *b, BN_BigNum *n, BN_Optimizer *opt) { int32_t ret = BN_ModMul(b->r, b->r, b->r, n, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_ModMul(b->rInv, b->rInv, b->rInv, n, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t RSA_BlindCovert(RSA_Blind *b, BN_BigNum *data, BN_BigNum *n, BN_Optimizer *opt) { int32_t ret; ret = BlindUpdate(b, n, opt); if (ret != CRYPT_SUCCESS) { return ret; } // 8. z = m * x mod n ret = BN_ModMul(data, data, b->r, n, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t RSA_BlindInvert(RSA_Blind *b, BN_BigNum *data, BN_BigNum *n, BN_Optimizer *opt) { int32_t ret; ret = BN_ModMul(data, data, b->rInv, n, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t RSA_CreateBlind(RSA_Blind *b, uint32_t bits) { // create a BigNum b->r = BN_Create(bits); if (b->r == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } b->rInv = BN_Create(bits); if (b->rInv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } return CRYPT_SUCCESS; } /* * Initializes blind signature parameters for an RSA key. * Ref. https://www.rfc-editor.org/rfc/rfc9474.html#name-blind * * As RFC-9474 Section 2.1, we need to do this. * 1. Generates a random blinding factor r * 2. Computes r^(-1) mod n (modular inverse) * 3. Computes r^e mod n (where e is the public exponent) */ int32_t RSA_BlindCreateParam(void *libCtx, RSA_Blind *b, BN_BigNum *e, BN_BigNum *n, uint32_t bits, BN_Optimizer *opt) { int32_t ret; if (b == NULL || e == NULL || n == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BN_Destroy(b->r); BN_Destroy(b->rInv); b->r = NULL; b->rInv = NULL; ret = RSA_CreateBlind(b, bits); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto END; } // b->r = random_integer_uniform(1, n) ret = BN_RandRangeEx(libCtx, b->r, n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto END; } // b->rInv = inverse_mod(r, n) ret = BN_ModInv(b->rInv, b->r, n, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto END; } // b->r = RSAVP1(pk, r) ret = BN_ModExp(b->r, b->r, e, n, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto END; } return ret; END: BN_Destroy(b->r); BN_Destroy(b->rInv); b->r = NULL; b->rInv = NULL; return ret; } #endif /* HITLS_CRYPTO_RSA_BLINDING || HITLS_CRYPTO_RSA_BSSA */
2301_79861745/bench_create
crypto/rsa/src/rsa_blinding.c
C
unknown
4,123
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include "crypt_utils.h" #include "rsa_local.h" #include "crypt_errno.h" #include "securec.h" #include "eal_md_local.h" #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 static int32_t SetEmsaPkcsV15(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR); return CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR; } static const uint32_t SIGN_MD_ID_LIST[] = { CRYPT_MD_SHA224, CRYPT_MD_SHA256, CRYPT_MD_SHA384, CRYPT_MD_SHA512, CRYPT_MD_SM3, CRYPT_MD_SHA1, CRYPT_MD_MD5 }; int32_t mdId = *(int32_t *)val; if (ParamIdIsValid(mdId, SIGN_MD_ID_LIST, sizeof(SIGN_MD_ID_LIST) / sizeof(SIGN_MD_ID_LIST[0])) == false) { // This hash algorithm is not supported. BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_MD_ALGID); return CRYPT_RSA_ERR_MD_ALGID; } (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad)); ctx->pad.type = EMSA_PKCSV15; ctx->pad.para.pkcsv15.mdId = mdId; return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS static int32_t SetEmsaPss(CRYPT_RSA_Ctx *ctx, RSA_PadingPara *pad, void *mdProvCtx, void *mgfProvCtx) { uint32_t bits = CRYPT_RSA_GetBits(ctx); if (bits == 0) { // The valid key information does not exist. BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } if (pad->saltLen < CRYPT_RSA_SALTLEN_TYPE_AUTOLEN) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_SALT_LEN); return CRYPT_RSA_ERR_SALT_LEN; } uint32_t saltLen = (uint32_t)pad->saltLen; if (pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_HASHLEN) { saltLen = pad->mdMeth.mdSize; } uint32_t bytes = BN_BITS_TO_BYTES(bits); // The minimum specification supported by RSA is 1K, // and the maximum hash length supported by the hash algorithm is 64 bytes. // Therefore, specifying the salt length as the maximum available length is satisfied. if (pad->saltLen != CRYPT_RSA_SALTLEN_TYPE_MAXLEN && pad->saltLen != CRYPT_RSA_SALTLEN_TYPE_AUTOLEN && saltLen > bytes - pad->mdMeth.mdSize - 2) { // maximum length of the salt is padLen-mdMethod->GetDigestSize-2 // The configured salt length does not meet the specification. BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PSS_SALT_LEN); return CRYPT_RSA_ERR_PSS_SALT_LEN; } (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad)); (void)memcpy_s(&(ctx->pad.para.pss), sizeof(RSA_PadingPara), pad, sizeof(RSA_PadingPara)); ctx->pad.type = EMSA_PSS; ctx->pad.para.pss.mdId = pad->mdId; ctx->pad.para.pss.mgfId = pad->mgfId; ctx->pad.para.pss.mdProvCtx = mdProvCtx; ctx->pad.para.pss.mgfProvCtx = mgfProvCtx; return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_RSA_EMSA_PSS #ifdef HITLS_CRYPTO_RSAES_OAEP void SetOaep(CRYPT_RSA_Ctx *ctx, const RSA_PadingPara *val) { (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad)); (void)memcpy_s(&(ctx->pad.para.oaep), sizeof(RSA_PadingPara), val, sizeof(RSA_PadingPara)); ctx->pad.type = RSAES_OAEP; return; } static int32_t SetOaepLabel(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { uint8_t *data = NULL; // val can be NULL if ((val == NULL && len != 0) || (len == 0 && val != NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len == 0 && val == NULL) { BSL_SAL_FREE(ctx->label.data); ctx->label.data = NULL; ctx->label.len = 0; return CRYPT_SUCCESS; } data = (uint8_t *)BSL_SAL_Malloc(len); if (data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } BSL_SAL_FREE(ctx->label.data); ctx->label.data = data; ctx->label.len = len; (void)memcpy_s(ctx->label.data, ctx->label.len, val, len); return CRYPT_SUCCESS; } #endif #if defined(HITLS_CRYPTO_RSAES_PKCSV15) || defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) static int32_t SetRsaesPkcsV15(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR); return CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR; } (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad)); ctx->pad.para.pkcsv15.mdId = *(const int32_t *)val; ctx->pad.type = RSAES_PKCSV15; return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS static int32_t SetRsaesPkcsV15Tls(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { int32_t ret = SetRsaesPkcsV15(ctx, val, len); if (ret != CRYPT_SUCCESS) { return ret; } ctx->pad.type = RSAES_PKCSV15_TLS; return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS static int32_t SetSalt(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len) { if (val == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } if (ctx->pad.type != EMSA_PSS) { // In non-PSS mode, salt information cannot be set. BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_SALT_NOT_PSS_ERROR); return CRYPT_RSA_SET_SALT_NOT_PSS_ERROR; } RSA_PadingPara *pad = &(ctx->pad.para.pss); if (pad->mdMeth.id == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t bytes = BN_BITS_TO_BYTES(CRYPT_RSA_GetBits(ctx)); // The maximum salt length is padLen - mdMethod->GetDigestSize - 2 if (len > bytes - pad->mdMeth.mdSize - 2) { // The configured salt length does not meet the specification. BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_SALT_LEN); return CRYPT_RSA_ERR_SALT_LEN; } ctx->pad.salt.data = val; ctx->pad.salt.len = len; return CRYPT_SUCCESS; } static int32_t GetSaltLen(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len) { if (val == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_GET_SALT_LEN_ERROR); return CRYPT_RSA_GET_SALT_LEN_ERROR; } if (ctx->prvKey == NULL && ctx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } if (ctx->pad.type != EMSA_PSS) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_GET_SALT_NOT_PSS_ERROR); return CRYPT_RSA_GET_SALT_NOT_PSS_ERROR; } int32_t *ret = val; int32_t valTmp; RSA_PadingPara *pad = &(ctx->pad.para.pss); if (pad->mdMeth.id == 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PSS_PARAMS); return CRYPT_RSA_ERR_PSS_PARAMS; } uint32_t bytes = BN_BITS_TO_BYTES(CRYPT_RSA_GetBits(ctx)); if (pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_HASHLEN) { // saltLen is -1 valTmp = (int32_t)pad->mdMeth.mdSize; } else if (pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_MAXLEN || pad->saltLen == CRYPT_RSA_SALTLEN_TYPE_AUTOLEN) { // RFC 8017: Max(salt length) = ceil(bits/8) - mdSize - 2 valTmp = (int32_t)(bytes - pad->mdMeth.mdSize - 2); } else { valTmp = (int32_t)pad->saltLen; } if (valTmp < 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_SALT_LEN); return CRYPT_RSA_ERR_SALT_LEN; } *ret = valTmp; return CRYPT_SUCCESS; } #endif static uint32_t RSAGetKeyLen(CRYPT_RSA_Ctx *ctx) { return BN_BITS_TO_BYTES(CRYPT_RSA_GetBits(ctx)); } static int32_t GetPadding(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len) { RSA_PadType *valTmp = val; if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *valTmp = ctx->pad.type; return CRYPT_SUCCESS; } static int32_t GetMd(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len) { CRYPT_MD_AlgId *valTmp = val; if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ctx->pad.type == EMSA_PKCSV15) { *valTmp = ctx->pad.para.pkcsv15.mdId; return CRYPT_SUCCESS; } *valTmp = ctx->pad.para.pss.mdId; return CRYPT_SUCCESS; } static int32_t GetMgf(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len) { CRYPT_MD_AlgId *valTmp = val; if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ctx->pad.type == EMSA_PKCSV15) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_ALGID); return CRYPT_RSA_ERR_ALGID; } *valTmp = ctx->pad.para.pss.mgfId; return CRYPT_SUCCESS; } static int32_t SetFlag(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_SET_FLAG_LEN_ERROR); return CRYPT_RSA_SET_FLAG_LEN_ERROR; } uint32_t flag = *(const uint32_t *)val; if (flag == 0 || flag >= CRYPT_RSA_MAXFLAG) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR); return CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR; } ctx->flags |= flag; return CRYPT_SUCCESS; } static int32_t ClearFlag(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } uint32_t flag = *(const uint32_t *)val; if (flag == 0 || flag >= CRYPT_RSA_MAXFLAG) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR); return CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR; } ctx->flags &= ~flag; return CRYPT_SUCCESS; } static int32_t RsaUpReferences(CRYPT_RSA_Ctx *ctx, void *val, uint32_t len) { if (val != NULL && len == (uint32_t)sizeof(int)) { return BSL_SAL_AtomicUpReferences(&(ctx->references), (int *)val); } BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } static int32_t SetRsaPad(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t pad = *(const int32_t *)val; if (pad < EMSA_PKCSV15 || pad > RSA_NO_PAD) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->pad.type = pad; return CRYPT_SUCCESS; } #if defined(HITLS_CRYPTO_RSAES_OAEP) || defined(HITLS_CRYPTO_RSA_EMSA_PSS) static int32_t MdIdCheckSha1Sha2(CRYPT_MD_AlgId id) { if (id < CRYPT_MD_MD5 || id > CRYPT_MD_SHA512) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID); return CRYPT_EAL_ERR_ALGID; } return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_RSAES_OAEP static int32_t RsaSetOaep(CRYPT_RSA_Ctx *ctx, BSL_Param *param) { int32_t ret; uint32_t len = 0; RSA_PadingPara padPara = {0}; const BSL_Param *temp = NULL; if (param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MD_ID)) != NULL) { len = sizeof(padPara.mdId); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, &padPara.mdId, &len), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MGF1_ID)) != NULL) { len = sizeof(padPara.mgfId); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, &padPara.mgfId, &len), ret); } ret = MdIdCheckSha1Sha2(padPara.mdId); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = MdIdCheckSha1Sha2(padPara.mgfId); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } void *mdProvCtx = NULL; void *mgfProvCtx = NULL; EAL_MdMethod *mdMeth = EAL_MdFindMethodEx(padPara.mdId, LIBCTX_FROM_RSA_CTX(ctx), MDATTR_FROM_RSA_CTX(ctx), &padPara.mdMeth, &mdProvCtx); EAL_MdMethod *mgfMeth = EAL_MdFindMethodEx(padPara.mgfId, LIBCTX_FROM_RSA_CTX(ctx), MDATTR_FROM_RSA_CTX(ctx), &padPara.mgfMeth, &mgfProvCtx); if (mdMeth == NULL || mgfMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID); return CRYPT_EAL_ERR_ALGID; } padPara.mdProvCtx = mdProvCtx; padPara.mgfProvCtx = mgfProvCtx; SetOaep(ctx, &padPara); ERR: return ret; } #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS static int32_t RsaSetPss(CRYPT_RSA_Ctx *ctx, BSL_Param *param) { int32_t ret; uint32_t len = 0; RSA_PadingPara padPara = {0}; const BSL_Param *temp = NULL; if (param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MD_ID)) != NULL) { len = sizeof(padPara.mdId); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_INT32, &padPara.mdId, &len), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_MGF1_ID)) != NULL) { len = sizeof(padPara.mgfId); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_INT32, &padPara.mgfId, &len), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_RSA_SALTLEN)) != NULL) { len = sizeof(padPara.saltLen); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_SALTLEN, BSL_PARAM_TYPE_INT32, &padPara.saltLen, &len), ret); } ret = MdIdCheckSha1Sha2(padPara.mdId); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = MdIdCheckSha1Sha2(padPara.mgfId); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } void *mdProvCtx = NULL; void *mgfProvCtx = NULL; EAL_MdMethod *mdMeth = EAL_MdFindMethodEx(padPara.mdId, LIBCTX_FROM_RSA_CTX(ctx), MDATTR_FROM_RSA_CTX(ctx), &padPara.mdMeth, &mdProvCtx); EAL_MdMethod *mgfMeth = EAL_MdFindMethodEx(padPara.mgfId, LIBCTX_FROM_RSA_CTX(ctx), MDATTR_FROM_RSA_CTX(ctx), &padPara.mgfMeth, &mgfProvCtx); if (mdMeth == NULL || mgfMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID); return CRYPT_EAL_ERR_ALGID; } ret = SetEmsaPss(ctx, &padPara, mdProvCtx, mgfProvCtx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } ERR: return ret; } #endif static int32_t RsaCommonCtrl(CRYPT_RSA_Ctx *ctx, int32_t opt, void *val, uint32_t len) { switch (opt) { case CRYPT_CTRL_UP_REFERENCES: return RsaUpReferences(ctx, val, len); case CRYPT_CTRL_GET_BITS: return GetUintCtrl(ctx, val, len, (GetUintCallBack)CRYPT_RSA_GetBits); case CRYPT_CTRL_GET_SECBITS: return GetUintCtrl(ctx, val, len, (GetUintCallBack)CRYPT_RSA_GetSecBits); case CRYPT_CTRL_SET_RSA_FLAG: return SetFlag(ctx, val, len); case CRYPT_CTRL_CLR_RSA_FLAG: return ClearFlag(ctx, val, len); case CRYPT_CTRL_GET_PUBKEY_LEN: case CRYPT_CTRL_GET_PRVKEY_LEN: return GetUintCtrl(ctx, val, len, (GetUintCallBack)RSAGetKeyLen); default: BSL_ERR_PUSH_ERROR(CRYPT_RSA_CTRL_NOT_SUPPORT_ERROR); return CRYPT_RSA_CTRL_NOT_SUPPORT_ERROR; } } #ifdef HITLS_CRYPTO_RSA_BSSA static int32_t SetBssaParamCheck(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_PUBKEY_INFO); return CRYPT_RSA_ERR_NO_PUBKEY_INFO; } return CRYPT_SUCCESS; } static int32_t RsaSetBssa(CRYPT_RSA_Ctx *ctx, const void *val, uint32_t len) { int32_t ret = SetBssaParamCheck(ctx, val, len); if (ret != CRYPT_SUCCESS) { return ret; } const uint8_t *r = (const uint8_t *)val; BN_Optimizer *opt = BN_OptimizerCreate(); if (opt == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } RSA_Blind *blind = NULL; RSA_BlindParam *param = ctx->blindParam; if (param == NULL) { param = BSL_SAL_Calloc(1u, sizeof(RSA_BlindParam)); if (param == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } param->type = RSABSSA; } if (param->para.bssa != NULL) { RSA_BlindFreeCtx(param->para.bssa); param->para.bssa = NULL; } param->para.bssa = RSA_BlindNewCtx(); if (param->para.bssa == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; goto ERR; } blind = param->para.bssa; GOTO_ERR_IF(RSA_CreateBlind(blind, 0), ret); GOTO_ERR_IF(BN_Bin2Bn(blind->r, r, len), ret); if (BN_IsZero(blind->r) || (BN_Cmp(blind->r, ctx->pubKey->n) >= 0)) { // 1 <= r < n ret = CRYPT_RSA_ERR_BSSA_PARAM; BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_BSSA_PARAM); goto ERR; } GOTO_ERR_IF(BN_ModInv(blind->rInv, blind->r, ctx->pubKey->n, opt), ret); GOTO_ERR_IF(BN_ModExp(blind->r, blind->r, ctx->pubKey->e, ctx->pubKey->n, opt), ret); ctx->blindParam = param; ERR: if (ret != CRYPT_SUCCESS && ctx->blindParam == NULL && param != NULL) { RSA_BlindFreeCtx(param->para.bssa); BSL_SAL_FREE(param); } BN_OptimizerDestroy(opt); return ret; } #endif int32_t CRYPT_RSA_Ctrl(CRYPT_RSA_Ctx *ctx, int32_t opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (opt) { #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 case CRYPT_CTRL_SET_RSA_EMSA_PKCSV15: return SetEmsaPkcsV15(ctx, val, len); #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS case CRYPT_CTRL_SET_RSA_EMSA_PSS: return RsaSetPss(ctx, val); case CRYPT_CTRL_SET_RSA_SALT: return SetSalt(ctx, val, len); case CRYPT_CTRL_GET_RSA_SALTLEN: return GetSaltLen(ctx, val, len); #endif case CRYPT_CTRL_GET_RSA_PADDING: return GetPadding(ctx, val, len); case CRYPT_CTRL_GET_RSA_MD: return GetMd(ctx, val, len); case CRYPT_CTRL_GET_RSA_MGF: return GetMgf(ctx, val, len); #ifdef HITLS_CRYPTO_RSAES_OAEP case CRYPT_CTRL_SET_RSA_RSAES_OAEP: return RsaSetOaep(ctx, val); case CRYPT_CTRL_SET_RSA_OAEP_LABEL: return SetOaepLabel(ctx, val, len); #endif #ifdef HITLS_CRYPTO_RSAES_PKCSV15 case CRYPT_CTRL_SET_RSA_RSAES_PKCSV15: return SetRsaesPkcsV15(ctx, val, len); #endif #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS case CRYPT_CTRL_SET_RSA_RSAES_PKCSV15_TLS: return SetRsaesPkcsV15Tls(ctx, val, len); #endif #ifdef HITLS_CRYPTO_RSA_NO_PAD case CRYPT_CTRL_SET_NO_PADDING: ctx->pad.type = RSA_NO_PAD; return CRYPT_SUCCESS; #endif case CRYPT_CTRL_SET_RSA_PADDING: return SetRsaPad(ctx, val, len); #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) case CRYPT_CTRL_GET_SIGNLEN: return GetUintCtrl(ctx, val, len, (GetUintCallBack)CRYPT_RSA_GetSignLen); #endif #ifdef HITLS_CRYPTO_RSA_BSSA case CRYPT_CTRL_SET_RSA_BSSA_FACTOR_R: return RsaSetBssa(ctx, val, len); #endif default: return RsaCommonCtrl(ctx, opt, val, len); } } #endif /* HITLS_CRYPTO_RSA */
2301_79861745/bench_create
crypto/rsa/src/rsa_ctrl.c
C
unknown
21,570
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include "crypt_utils.h" #include "crypt_rsa.h" #include "rsa_local.h" #include "crypt_errno.h" #include "crypt_eal_md.h" #include "bsl_sal.h" #include "securec.h" #include "bsl_err_internal.h" #include "eal_pkey_local.h" #include "eal_md_local.h" #include "bsl_params.h" #include "crypt_params_key.h" // rsa-decrypt Calculation used by Chinese Remainder Theorem(CRT). intermediate variables: typedef struct { BN_BigNum *cP; BN_BigNum *cQ; BN_BigNum *mP; BN_BigNum *mQ; BN_Mont *montP; BN_Mont *montQ; } RsaDecProcedurePara; static int32_t InputRangeCheck(const BN_BigNum *input, const BN_BigNum *n, uint32_t bits) { // The value range defined in RFC is [0, n - 1]. Because the operation result of 0, 1, n - 1 is relatively fixed, // it is considered invalid here. The actual valid value range is [2, n - 2]. int32_t ret; BN_BigNum *nMinusOne = NULL; if (BN_IsZero(input) == true || BN_IsOne(input) == true) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } /* Allocate 8 extra bits to prevent calculation errors due to the feature of BigNum calculation. */ nMinusOne = BN_Create(bits); if (nMinusOne == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = BN_SubLimb(nMinusOne, n, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BN_Destroy(nMinusOne); return ret; } if (BN_Cmp(input, nMinusOne) >= 0) { ret = CRYPT_RSA_ERR_INPUT_VALUE; BSL_ERR_PUSH_ERROR(ret); } BN_Destroy(nMinusOne); return ret; } static int32_t AddZero(uint32_t bits, uint8_t *out, uint32_t *outLen) { uint32_t i; uint32_t zeros = 0; uint32_t needBytes = BN_BITS_TO_BYTES(bits); /* Divide bits by 8 to obtain the byte length. If it is smaller than the key length, pad it with 0. */ if ((*outLen) < needBytes) { /* Divide bits by 8 to obtain the byte length. If it is smaller than the key length, pad it with 0. */ zeros = needBytes - (*outLen); if (memmove_s(out + zeros, needBytes - zeros, out, (*outLen)) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } for (i = 0; i < zeros; i++) { out[i] = 0x0; } } *outLen = needBytes; return CRYPT_SUCCESS; } static int32_t ResultToOut(uint32_t bits, const BN_BigNum *result, uint8_t *out, uint32_t *outLen) { int32_t ret = BN_Bn2Bin(result, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return AddZero(bits, out, outLen); } static int32_t AllocResultAndInputBN(uint32_t bits, BN_BigNum **result, BN_BigNum **inputBN, const uint8_t *input, uint32_t inputLen) { if (inputLen > BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } *result = BN_Create(bits + 1); *inputBN = BN_Create(bits); if (*result == NULL || *inputBN == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } return BN_Bin2Bn(*inputBN, input, inputLen); } static int32_t CalcMontExp(CRYPT_RSA_PrvKey *prvKey, BN_BigNum *result, const BN_BigNum *input, BN_Optimizer *opt, bool consttime) { int32_t ret; BN_Mont *mont = NULL; if (BN_IsZero(prvKey->n) || BN_IsZero(prvKey->d)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } mont = BN_MontCreate(prvKey->n); if (mont == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (consttime) { ret = BN_MontExpConsttime(result, input, prvKey->d, mont, opt); } else { ret = BN_MontExp(result, input, prvKey->d, mont, opt); } BN_MontDestroy(mont); return ret; } #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_RSA_SIGN) int32_t CRYPT_RSA_PubEnc(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { int32_t ret; BN_BigNum *inputBN = NULL; BN_BigNum *result = NULL; if (ctx == NULL || input == NULL || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_RSA_PubKey *pubKey = ctx->pubKey; if (pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } uint32_t bits = CRYPT_RSA_GetBits(ctx); if ((*outLen) < BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } BN_Optimizer *optimizer = BN_OptimizerCreate(); if (optimizer == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF_EX(AllocResultAndInputBN(bits, &result, &inputBN, input, inputLen), ret); GOTO_ERR_IF_EX(InputRangeCheck(inputBN, pubKey->n, bits), ret); // pubKey->mont: Ensure that this value is not empty when the public key is set or generated. GOTO_ERR_IF(BN_MontExp(result, inputBN, pubKey->e, pubKey->mont, optimizer), ret); ret = ResultToOut(bits, result, out, outLen); ERR: BN_Destroy(result); BN_Destroy(inputBN); BN_OptimizerDestroy(optimizer); return ret; } #endif /* Release intermediate variables. */ static void RsaDecProcedureFree(RsaDecProcedurePara *para) { if (para == NULL) { return; } BN_Destroy(para->cP); BN_Destroy(para->cQ); BN_Destroy(para->mP); BN_Destroy(para->mQ); BN_MontDestroy(para->montP); BN_MontDestroy(para->montQ); } /* Apply for intermediate variables. */ static int32_t RsaDecProcedureAlloc(RsaDecProcedurePara *para, uint32_t bits, const CRYPT_RSA_PrvKey *priKey) { para->cP = BN_Create(bits); para->cQ = BN_Create(bits); para->mP = BN_Create(bits); para->mQ = BN_Create(bits); para->montP = BN_MontCreate(priKey->p); para->montQ = BN_MontCreate(priKey->q); if (para->cP == NULL || para->cQ == NULL || para->mP == NULL || para->mQ == NULL || para->montP == NULL || para->montQ == NULL) { RsaDecProcedureFree(para); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } return CRYPT_SUCCESS; } /* rsa decryption calculation by CRT. Message is the BigNum converted from the original input ciphertext. */ static int32_t NormalDecProcedure(const CRYPT_RSA_Ctx *ctx, const BN_BigNum *message, BN_BigNum *result, BN_Optimizer *opt) { CRYPT_RSA_PrvKey *priKey = ctx->prvKey; uint32_t bits = CRYPT_RSA_GetBits(ctx); RsaDecProcedurePara procedure = {0}; // Temporary variable /* Apply for temporary variable */ int32_t ret = RsaDecProcedureAlloc(&procedure, bits, priKey); if (ret != CRYPT_SUCCESS) { return ret; } /* cP = M mod P where inp = M = Message */ ret = BN_Mod(procedure.cP, message, priKey->p, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* cQ = M mod Q where inp = M = Message */ ret = BN_Mod(procedure.cQ, message, priKey->q, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* mP = cP^dP mod p */ ret = BN_MontExpConsttime(procedure.mP, procedure.cP, priKey->dP, procedure.montP, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* mQ = cQ^dQ mod q */ ret = BN_MontExpConsttime(procedure.mQ, procedure.cQ, priKey->dQ, procedure.montQ, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* result = (mP - mQ) mod p */ ret = BN_ModSub(result, procedure.mP, procedure.mQ, priKey->p, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* result = result * qInv mod p */ ret = MontMulCore(result, result, priKey->qInv, procedure.montP, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* result = result * q */ ret = BN_Mul(result, result, priKey->q, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* result = result + mQ */ ret = BN_Add(result, result, procedure.mQ); EXIT: RsaDecProcedureFree(&procedure); return ret; } #ifdef HITLS_CRYPTO_RSA_BLINDING static int32_t RSA_GetSub(const BN_BigNum *p, const BN_BigNum *q, BN_BigNum *r1, BN_BigNum *r2) { int32_t ret = BN_SubLimb(r1, p, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_SubLimb(r2, q, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static int32_t RSA_GetL(BN_BigNum *l, BN_BigNum *u, BN_BigNum *r1, BN_BigNum *r2, BN_Optimizer *opt) { int32_t ret = BN_Mul(l, r1, r2, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_Gcd(u, r1, r2, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_Div(l, NULL, l, u, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static BN_BigNum *RSA_GetPublicExp(const BN_BigNum *d, const BN_BigNum *p, const BN_BigNum *q, uint32_t bits, BN_Optimizer *opt) { int32_t ret; /* Apply for the temporary space of the BN object */ BN_BigNum *l = BN_Create(bits); BN_BigNum *r1 = BN_Create(bits >> 1); BN_BigNum *r2 = BN_Create(bits >> 1); BN_BigNum *u = BN_Create(bits + 1); BN_BigNum *e = BN_Create(bits); if (l == NULL || r1 == NULL || r2 == NULL || u == NULL || e == NULL) { ret = CRYPT_NULL_INPUT; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = RSA_GetSub(p, q, r1, r2); // The push error in GetSub can be used to locate the fault. Therefore, it is not added here. if (ret != CRYPT_SUCCESS) { goto EXIT; } ret = RSA_GetL(l, u, r1, r2, opt); // The push error in GetL can be used to locate the fault. Therefore, it is not added here. if (ret != CRYPT_SUCCESS) { goto EXIT; } ret = BN_ModInv(e, d, l, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: BN_Destroy(r1); BN_Destroy(r2); BN_Destroy(l); BN_Destroy(u); if (ret != CRYPT_SUCCESS) { BN_Destroy(e); e = NULL; } return e; } static int32_t RSA_InitBlind(CRYPT_RSA_Ctx *ctx, BN_Optimizer *opt) { uint32_t bits = BN_Bits(ctx->prvKey->n); bool needDestoryE = false; BN_BigNum *e = ctx->prvKey->e; if (e == NULL || BN_IsZero(e)) { e = RSA_GetPublicExp(ctx->prvKey->d, ctx->prvKey->p, ctx->prvKey->q, bits, opt); if (e == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_E_VALUE); return CRYPT_RSA_ERR_E_VALUE; } needDestoryE = true; } ctx->scBlind = RSA_BlindNewCtx(); int32_t ret = RSA_BlindCreateParam(LIBCTX_FROM_RSA_CTX(ctx), ctx->scBlind, e, ctx->prvKey->n, bits, opt); if (needDestoryE) { BN_Destroy(e); } return ret; } static int32_t RSA_BlindProcess(CRYPT_RSA_Ctx *ctx, BN_BigNum *message, BN_Optimizer *opt) { int32_t ret; if (ctx->scBlind == NULL) { ret = RSA_InitBlind(ctx, opt); if (ret != CRYPT_SUCCESS) { return ret; } } return RSA_BlindCovert(ctx->scBlind, message, ctx->prvKey->n, opt); } #endif static int32_t RSA_AllocAndCheck(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, BN_BigNum **result, BN_BigNum **message) { int32_t ret; if (ctx->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } uint32_t bits = CRYPT_RSA_GetBits(ctx); ret = AllocResultAndInputBN(bits, result, message, input, inputLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = InputRangeCheck(*message, ctx->prvKey->n, bits); if (ret != CRYPT_SUCCESS) { goto ERR; } return ret; ERR: BN_Destroy(*result); BN_Destroy(*message); return ret; } static int32_t RSA_PrvProcess(const CRYPT_RSA_Ctx *ctx, BN_BigNum *message, BN_BigNum *result, BN_Optimizer *opt) { #ifndef HITLS_CRYPTO_RSA_BLINDING (void)opt; #endif int32_t ret; #ifdef HITLS_CRYPTO_RSA_BLINDING // blinding if ((ctx->flags & CRYPT_RSA_BLINDING) != 0) { ret = RSA_BlindProcess((CRYPT_RSA_Ctx *)(uintptr_t)ctx, message, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } #endif /* If ctx->prvKey->p is set to 0, the standard mode is used for RSA decryption. Otherwise, the CRT mode is used for RSA decryption. */ if (BN_IsZero(ctx->prvKey->p)) { ret = CalcMontExp(ctx->prvKey, result, message, opt, true); } else { ret = NormalDecProcedure(ctx, message, result, opt); } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } #ifdef HITLS_CRYPTO_RSA_BLINDING // unblinding if ((ctx->flags & CRYPT_RSA_BLINDING) != 0) { ret = RSA_BlindInvert(ctx->scBlind, result, ctx->prvKey->n, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } } #endif return ret; } int32_t CRYPT_RSA_PrvDec(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { int32_t ret; uint32_t bits; BN_BigNum *result = NULL; BN_BigNum *message = NULL; BN_Optimizer *opt = NULL; if (ctx == NULL || input == NULL || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } bits = CRYPT_RSA_GetBits(ctx); if ((*outLen) < BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } opt = BN_OptimizerCreate(); if (opt == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = RSA_AllocAndCheck(ctx, input, inputLen, &result, &message); if (ret != CRYPT_SUCCESS) { BN_OptimizerDestroy(opt); return ret; } (void)OptimizerStart(opt); ret = RSA_PrvProcess(ctx, message, result, opt); if (ret != CRYPT_SUCCESS) { goto EXIT; } ret = ResultToOut(bits, result, out, outLen); EXIT: OptimizerEnd(opt); BN_OptimizerDestroy(opt); BN_Destroy(result); BN_Destroy(message); return ret; } #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) static uint32_t GetHashLen(const CRYPT_RSA_Ctx *ctx) { if (ctx->pad.type == EMSA_PKCSV15) { return CRYPT_GetMdSizeById(ctx->pad.para.pkcsv15.mdId); } return (uint32_t)(ctx->pad.para.pss.mdMeth.mdSize); } static int32_t CheckHashLen(uint32_t inputLen) { if (inputLen > 64) { // 64 is the maximum of the hash length. BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_ALGID); return CRYPT_RSA_ERR_ALGID; } // Inconsistent length BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_ALGID); return CRYPT_RSA_ERR_ALGID; } #endif #if defined(HITLS_CRYPTO_RSA_EMSA_PSS) && defined(HITLS_CRYPTO_RSA_SIGN) static int32_t PssPad(CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t outLen) { uint32_t saltLen = 0; bool kat = false; // mark if (ctx->pad.salt.data != NULL) { // If the salt contains data, that is the kat test. kat = true; } if (kat == true) { saltLen = ctx->pad.salt.len; } else if (ctx->pad.para.pss.saltLen != 0) { saltLen = ctx->pad.para.pss.saltLen; } int32_t ret = CRYPT_RSA_SetPss(ctx, &ctx->pad.para.pss.mdMeth, &ctx->pad.para.pss.mgfMeth, saltLen, input, inputLen, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } #endif #ifdef HITLS_CRYPTO_RSA_BSSA static int32_t BlindInputCheck(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, const uint8_t *out, const uint32_t *outLen) { if (ctx == NULL || input == NULL || inputLen == 0 || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->pubKey == NULL) { // Check whether the private key information exists. BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_PUBKEY_INFO); return CRYPT_RSA_ERR_NO_PUBKEY_INFO; } uint32_t bits = CRYPT_RSA_GetBits(ctx); if ((*outLen) < BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } if (ctx->pad.type != EMSA_PSS) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_PADDING_NOT_SUPPORTED); return CRYPT_RSA_PADDING_NOT_SUPPORTED; } return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_RSA_SIGN static RSA_BlindParam *BssaParamNew(void) { RSA_BlindParam *param = BSL_SAL_Calloc(1u, sizeof(RSA_BlindParam)); if (param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } param->para.bssa = RSA_BlindNewCtx(); if (param->para.bssa == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_FREE(param); return NULL; } param->type = RSABSSA; return param; } static int32_t BssaBlind(CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { int32_t ret; uint32_t bits = CRYPT_RSA_GetBits(ctx); uint32_t padLen = BN_BITS_TO_BYTES(bits); RSA_BlindParam *param = NULL; BN_BigNum *e = ctx->pubKey->e; BN_BigNum *n = ctx->pubKey->n; RSA_Blind *blind = NULL; uint8_t *pad = BSL_SAL_Malloc(padLen); BN_Optimizer *opt = BN_OptimizerCreate(); BN_BigNum *enMsg = BN_Create(bits); BN_BigNum *gcd = BN_Create(bits); if (pad == NULL || opt == NULL || enMsg == NULL || gcd == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto ERR; } // encoded_msg = EMSA-PSS-ENCODE(msg, bit_len(n)) GOTO_ERR_IF(PssPad(ctx, input, inputLen, pad, padLen), ret); GOTO_ERR_IF(BN_Bin2Bn(enMsg, pad, padLen), ret); // Check if bigNumOut and n are coprime using GCD GOTO_ERR_IF(BN_Gcd(gcd, enMsg, n, opt), ret); // Check if gcd is 1 if (!BN_IsOne(gcd)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); ret = CRYPT_INVALID_ARG; goto ERR; } param = ctx->blindParam; if (param == NULL) { param = BssaParamNew(); if (param == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; goto ERR; } GOTO_ERR_IF(RSA_BlindCreateParam(LIBCTX_FROM_RSA_CTX(ctx), param->para.bssa, e, n, bits, opt), ret); } blind = param->para.bssa; GOTO_ERR_IF(BN_ModMul(enMsg, enMsg, blind->r, n, opt), ret); GOTO_ERR_IF(ResultToOut(bits, enMsg, out, outLen), ret); ctx->blindParam = param; ERR: if (ret != CRYPT_SUCCESS && ctx->blindParam == NULL && param != NULL) { RSA_BlindFreeCtx(param->para.bssa); BSL_SAL_Free(param); } BN_Destroy(enMsg); BN_Destroy(gcd); BSL_SAL_FREE(pad); BN_OptimizerDestroy(opt); return ret; } static int32_t BlindSign(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { int32_t ret; uint32_t bits = CRYPT_RSA_GetBits(ctx); uint32_t sLen = BN_BITS_TO_BYTES(bits); uint32_t mLen = BN_BITS_TO_BYTES(bits); uint8_t *s = BSL_SAL_Malloc(sLen); uint8_t *m = BSL_SAL_Malloc(mLen); if (s == NULL || m == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto EXIT; } /* Step 1: Compute blind signature using RSA private key operation */ ret = CRYPT_RSA_PrvDec(ctx, data, dataLen, s, &sLen); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* Step 2: Verify the signature by applying the public key operation * This step ensures the signature is valid under the RSA key pair */ ret = CRYPT_RSA_PubEnc(ctx, s, sLen, m, &mLen); if (ret != CRYPT_SUCCESS) { goto EXIT; } /* Step 3: Verify that the result matches the input blinded message * This ensures the signature operation was performed correctly */ if (dataLen != mLen || memcmp(data, m, mLen) != 0) { ret = CRYPT_RSA_NOR_VERIFY_FAIL; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } /* Copy the blind signature to output buffer */ (void)memcpy_s(sign, *signLen, s, sLen); *signLen = sLen; EXIT: BSL_SAL_FREE(m); BSL_SAL_FREE(s); return ret; } int32_t CRYPT_RSA_Blind(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { int32_t ret = BlindInputCheck(ctx, input, inputLen, out, outLen); if (ret != CRYPT_SUCCESS) { return ret; } if ((int32_t)ctx->pad.para.pss.mdId != algId) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_MD_ALGID); return CRYPT_RSA_ERR_MD_ALGID; } uint8_t hash[64]; // 64 is max hash len uint32_t hashLen = sizeof(hash); ret = EAL_Md(algId, LIBCTX_FROM_RSA_CTX(ctx), MDATTR_FROM_RSA_CTX(ctx), input, inputLen, hash, &hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if ((ctx->flags & CRYPT_RSA_BSSA) != 0) { ret = BssaBlind(ctx, hash, hashLen, out, outLen); } else { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_BLIND_TYPE); ret = CRYPT_RSA_ERR_BLIND_TYPE; } return ret; } #endif // HITLS_CRYPTO_RSA_SIGN #ifdef HITLS_CRYPTO_RSA_VERIFY static int32_t BssaUnBlind(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { uint32_t bits = CRYPT_RSA_GetBits(ctx); uint32_t sigLen = BN_BITS_TO_BYTES(bits); if (inputLen != sigLen) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret; RSA_Blind *blind = NULL; BN_BigNum *n = ctx->pubKey->n; BN_Optimizer *opt = BN_OptimizerCreate(); BN_BigNum *z = BN_Create(bits); BN_BigNum *s = BN_Create(bits); if (opt == NULL || z == NULL || s == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto ERR; } if (ctx->blindParam == NULL || ctx->blindParam->para.bssa == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_BLIND_INFO); ret = CRYPT_RSA_ERR_NO_BLIND_INFO; goto ERR; } if (ctx->blindParam->type != RSABSSA) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_BLIND_TYPE); ret = CRYPT_RSA_ERR_BLIND_TYPE; goto ERR; } blind = ctx->blindParam->para.bssa; GOTO_ERR_IF(BN_Bin2Bn(z, input, inputLen), ret); GOTO_ERR_IF(BN_ModMul(s, z, blind->rInv, n, opt), ret); GOTO_ERR_IF(ResultToOut(bits, s, out, outLen), ret); ERR: BN_Destroy(z); BN_Destroy(s); BN_OptimizerDestroy(opt); return ret; } int32_t CRYPT_RSA_UnBlind(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { int32_t ret; ret = BlindInputCheck(ctx, input, inputLen, out, outLen); if (ret != CRYPT_SUCCESS) { return ret; } if ((ctx->flags & CRYPT_RSA_BSSA) != 0) { ret = BssaUnBlind(ctx, input, inputLen, out, outLen); } else { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_BLIND_TYPE); ret = CRYPT_RSA_ERR_BLIND_TYPE; } return ret; } #endif // HITLS_CRYPTO_RSA_VERIFY #endif // HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_SIGN static int32_t SignInputCheck(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, const uint8_t *out, const uint32_t *outLen) { if (ctx == NULL || input == NULL || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->prvKey == NULL) { // Check whether the private key information exists. BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } // Check whether the length of the out is sufficient to place the signature information. uint32_t bits = CRYPT_RSA_GetBits(ctx); if ((*outLen) < BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } if (ctx->pad.type != EMSA_PKCSV15 && ctx->pad.type != EMSA_PSS) { // No padding type is set. BSL_ERR_PUSH_ERROR(CRYPT_RSA_PAD_NO_SET_ERROR); return CRYPT_RSA_PAD_NO_SET_ERROR; } #ifdef HITLS_CRYPTO_RSA_BSSA if ((ctx->flags & CRYPT_RSA_BSSA) != 0) { if (BN_BITS_TO_BYTES(bits) != inputLen) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } return CRYPT_SUCCESS; } #endif if (GetHashLen(ctx) != inputLen) { return CheckHashLen(inputLen); } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_SignData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { int32_t ret = SignInputCheck(ctx, data, dataLen, sign, signLen); if (ret != CRYPT_SUCCESS) { return ret; } #ifdef HITLS_CRYPTO_RSA_BSSA if ((ctx->flags & CRYPT_RSA_BSSA) != 0) { return BlindSign(ctx, data, dataLen, sign, signLen); } #endif uint32_t bits = CRYPT_RSA_GetBits(ctx); uint32_t padLen = BN_BITS_TO_BYTES(bits); uint8_t *pad = BSL_SAL_Malloc(padLen); if (pad == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } switch (ctx->pad.type) { #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 case EMSA_PKCSV15: ret = CRYPT_RSA_SetPkcsV15Type1(ctx->pad.para.pkcsv15.mdId, data, dataLen, pad, padLen); break; #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS case EMSA_PSS: ret = PssPad(ctx, data, dataLen, pad, padLen); break; #endif default: // This branch cannot be entered because it's been verified before. ret = CRYPT_RSA_PAD_NO_SET_ERROR; break; } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = CRYPT_RSA_PrvDec(ctx, pad, padLen, sign, signLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: (void)memset_s(pad, padLen, 0, padLen); BSL_SAL_FREE(pad); return ret; } int32_t CRYPT_RSA_Sign(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { uint8_t hash[64]; // 64 is max hash len uint32_t hashLen = sizeof(hash) / sizeof(hash[0]); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = EAL_Md(algId, LIBCTX_FROM_RSA_CTX(ctx), MDATTR_FROM_RSA_CTX(ctx), data, dataLen, hash, &hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_RSA_SignData(ctx, hash, hashLen, sign, signLen); } #endif // HITLS_CRYPTO_RSA_SIGN #ifdef HITLS_CRYPTO_RSA_VERIFY static int32_t VerifyInputCheck(const CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, const uint8_t *sign) { if (ctx == NULL || data == NULL || sign == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->pubKey == NULL) { // Check whether the private key information exists. BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } if (ctx->pad.type != EMSA_PKCSV15 && ctx->pad.type != EMSA_PSS) { // No padding type is set. BSL_ERR_PUSH_ERROR(CRYPT_RSA_PAD_NO_SET_ERROR); return CRYPT_RSA_PAD_NO_SET_ERROR; } if (GetHashLen(ctx) != dataLen) { return CheckHashLen(dataLen); } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_VerifyData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen) { uint8_t *pad = NULL; #ifdef HITLS_CRYPTO_RSA_EMSA_PSS uint32_t saltLen = 0; #endif int32_t ret = VerifyInputCheck(ctx, data, dataLen, sign); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t bits = CRYPT_RSA_GetBits(ctx); uint32_t padLen = BN_BITS_TO_BYTES(bits); pad = BSL_SAL_Malloc(padLen); if (pad == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = CRYPT_RSA_PubEnc(ctx, sign, signLen, pad, &padLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } switch (ctx->pad.type) { #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 case EMSA_PKCSV15: ret = CRYPT_RSA_VerifyPkcsV15Type1(ctx->pad.para.pkcsv15.mdId, pad, padLen, data, dataLen); break; #endif #ifdef HITLS_CRYPTO_RSA_EMSA_PSS case EMSA_PSS: saltLen = (uint32_t)ctx->pad.para.pss.saltLen; if (ctx->pad.para.pss.mdMeth.id == 0) { ret = CRYPT_NULL_INPUT; goto EXIT; } ret = CRYPT_RSA_VerifyPss(ctx, &ctx->pad.para.pss.mdMeth, &ctx->pad.para.pss.mgfMeth, saltLen, data, dataLen, pad, padLen); break; #endif default: // This branch cannot be entered because it's been verified before. ret = CRYPT_RSA_PAD_NO_SET_ERROR; BSL_ERR_PUSH_ERROR(ret); } EXIT: (void)memset_s(pad, padLen, 0, padLen); BSL_SAL_FREE(pad); return ret; } int32_t CRYPT_RSA_Verify(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen) { uint8_t hash[64]; // 64 is max hash len uint32_t hashLen = sizeof(hash) / sizeof(hash[0]); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = EAL_Md(algId, LIBCTX_FROM_RSA_CTX(ctx), MDATTR_FROM_RSA_CTX(ctx), data, dataLen, hash, &hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_RSA_VerifyData(ctx, hash, hashLen, sign, signLen); } #endif // HITLS_CRYPTO_RSA_VERIFY #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_VERIFY) static int32_t EncryptInputCheck(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, const uint8_t *out, const uint32_t *outLen) { if (ctx == NULL || (input == NULL && inputLen != 0) || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->pubKey == NULL) { // Check whether the public key information exists. BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } // Check whether the length of the out is sufficient to place the encryption information. uint32_t bits = CRYPT_RSA_GetBits(ctx); if ((*outLen) < BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } if (inputLen > BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_ENC_BITS); return CRYPT_RSA_ERR_ENC_BITS; } return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_RSA_ENCRYPT int32_t CRYPT_RSA_Encrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen) { uint32_t bits, padLen; uint8_t *pad = NULL; int32_t ret = EncryptInputCheck(ctx, data, dataLen, out, outLen); // The static function has pushed an error. The push error is not repeated here. if (ret != CRYPT_SUCCESS) { return ret; } bits = CRYPT_RSA_GetBits(ctx); padLen = BN_BITS_TO_BYTES(bits); pad = BSL_SAL_Malloc(padLen); if (pad == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } switch (ctx->pad.type) { #if defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) || defined(HITLS_CRYPTO_RSAES_PKCSV15) case RSAES_PKCSV15_TLS: case RSAES_PKCSV15: ret = CRYPT_RSA_SetPkcsV15Type2(LIBCTX_FROM_RSA_CTX(ctx), data, dataLen, pad, padLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } break; #endif #ifdef HITLS_CRYPTO_RSAES_OAEP case RSAES_OAEP: ret = CRYPT_RSA_SetPkcs1Oaep(ctx, data, dataLen, pad, padLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } break; #endif #ifdef HITLS_CRYPTO_RSA_NO_PAD case RSA_NO_PAD: if (dataLen != padLen) { ret = CRYPT_RSA_ERR_ENC_INPUT_NOT_ENOUGH; BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_ENC_INPUT_NOT_ENOUGH); goto EXIT; } (void)memcpy_s(pad, padLen, data, dataLen); break; #endif default: ret = CRYPT_RSA_PAD_NO_SET_ERROR; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = CRYPT_RSA_PubEnc(ctx, pad, padLen, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: (void)memset_s(pad, padLen, 0, padLen); BSL_SAL_FREE(pad); return ret; } #endif // HITLS_CRYPTO_RSA_ENCRYPT #ifdef HITLS_CRYPTO_RSA_DECRYPT static int32_t DecryptInputCheck(const CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, const uint8_t *out, const uint32_t *outLen) { if (ctx == NULL || data == NULL || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->prvKey == NULL) { // Check whether the private key information exists. BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } uint32_t bits = CRYPT_RSA_GetBits(ctx); if (dataLen != BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_DEC_BITS); return CRYPT_RSA_ERR_DEC_BITS; } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_Decrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen) { uint8_t *pad = NULL; int32_t ret = DecryptInputCheck(ctx, data, dataLen, out, outLen); // The static function has pushed an error. The push error is not repeated here. if (ret != CRYPT_SUCCESS) { return ret; } uint32_t bits = CRYPT_RSA_GetBits(ctx); uint32_t padLen = BN_BITS_TO_BYTES(bits); pad = BSL_SAL_Malloc(padLen); if (pad == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = CRYPT_RSA_PrvDec(ctx, data, dataLen, pad, &padLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } switch (ctx->pad.type) { #ifdef HITLS_CRYPTO_RSAES_OAEP case RSAES_OAEP: ret = CRYPT_RSA_VerifyPkcs1Oaep(&ctx->pad.para.oaep, pad, padLen, ctx->label.data, ctx->label.len, out, outLen); break; #endif #ifdef HITLS_CRYPTO_RSAES_PKCSV15 case RSAES_PKCSV15: ret = CRYPT_RSA_VerifyPkcsV15Type2(pad, padLen, out, outLen); break; #endif #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS case RSAES_PKCSV15_TLS: ret = CRYPT_RSA_VerifyPkcsV15Type2TLS(pad, padLen, out, outLen); break; #endif #ifdef HITLS_CRYPTO_RSA_NO_PAD case RSA_NO_PAD: if (memcpy_s(out, *outLen, pad, padLen) != EOK) { ret = CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); goto EXIT; } *outLen = padLen; break; #endif default: ret = CRYPT_RSA_PAD_NO_SET_ERROR; BSL_ERR_PUSH_ERROR(ret); break; } EXIT: BSL_SAL_CleanseData(pad, padLen); BSL_SAL_FREE(pad); return ret; } #endif // HITLS_CRYPTO_RSA_DECRYPT #ifdef HITLS_CRYPTO_RSA_VERIFY int32_t CRYPT_RSA_Recover(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen) { int32_t ret = EncryptInputCheck(ctx, data, dataLen, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint8_t *emMsg = NULL; uint32_t bits = CRYPT_RSA_GetBits(ctx); uint32_t emLen = BN_BITS_TO_BYTES(bits); emMsg = BSL_SAL_Malloc(emLen); if (emMsg == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = CRYPT_RSA_PubEnc(ctx, data, dataLen, emMsg, &emLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } switch (ctx->pad.type) { // Remove padding based on the padding type to obtain the plaintext. #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 case RSAES_PKCSV15: ret = CRYPT_RSA_UnPackPkcsV15Type1(emMsg, emLen, out, outLen); break; #endif #ifdef HITLS_CRYPTO_RSA_NO_PAD case RSA_NO_PAD: if (memcpy_s(out, *outLen, emMsg, emLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); ret = CRYPT_SECUREC_FAIL; goto ERR; } *outLen = emLen; break; #endif default: ret = CRYPT_RSA_PAD_NO_SET_ERROR; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ERR: (void)memset_s(emMsg, emLen, 0, emLen); BSL_SAL_FREE(emMsg); return ret; } #endif // HITLS_CRYPTO_RSA_VERIFY #endif /* HITLS_CRYPTO_RSA */
2301_79861745/bench_create
crypto/rsa/src/rsa_encdec.c
C
unknown
38,586
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include "crypt_rsa.h" #include "rsa_local.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" CRYPT_RSA_Ctx *CRYPT_RSA_NewCtx(void) { CRYPT_RSA_Ctx *keyCtx = NULL; keyCtx = BSL_SAL_Malloc(sizeof(CRYPT_RSA_Ctx)); if (keyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(keyCtx, sizeof(CRYPT_RSA_Ctx), 0, sizeof(CRYPT_RSA_Ctx)); BSL_SAL_ReferencesInit(&(keyCtx->references)); return keyCtx; } CRYPT_RSA_Ctx *CRYPT_RSA_NewCtxEx(void *libCtx) { CRYPT_RSA_Ctx *keyCtx = CRYPT_RSA_NewCtx(); if (keyCtx == NULL) { return NULL; } keyCtx->libCtx = libCtx; return keyCtx; } static CRYPT_RSA_PubKey *RSAPubKeyDupCtx(CRYPT_RSA_PubKey *pubKey) { CRYPT_RSA_PubKey *newPubKey = BSL_SAL_Malloc(sizeof(CRYPT_RSA_PubKey)); if (newPubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newPubKey, sizeof(CRYPT_RSA_PubKey), 0, sizeof(CRYPT_RSA_PubKey)); GOTO_ERR_IF_SRC_NOT_NULL(newPubKey->e, pubKey->e, BN_Dup(pubKey->e), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPubKey->n, pubKey->n, BN_Dup(pubKey->n), CRYPT_MEM_ALLOC_FAIL); newPubKey->mont = BN_MontCreate(pubKey->n); if (newPubKey->mont == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } return newPubKey; ERR: RSA_FREE_PUB_KEY(newPubKey); return NULL; } static CRYPT_RSA_PrvKey *RSAPriKeyDupCtx(CRYPT_RSA_PrvKey *prvKey) { CRYPT_RSA_PrvKey *newPriKey = BSL_SAL_Malloc(sizeof(CRYPT_RSA_PrvKey)); if (newPriKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newPriKey, sizeof(CRYPT_RSA_PrvKey), 0, sizeof(CRYPT_RSA_PrvKey)); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->n, prvKey->n, BN_Dup(prvKey->n), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->d, prvKey->d, BN_Dup(prvKey->d), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->p, prvKey->p, BN_Dup(prvKey->p), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->q, prvKey->q, BN_Dup(prvKey->q), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->dP, prvKey->dP, BN_Dup(prvKey->dP), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->dQ, prvKey->dQ, BN_Dup(prvKey->dQ), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->qInv, prvKey->qInv, BN_Dup(prvKey->qInv), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPriKey->e, prvKey->e, BN_Dup(prvKey->e), CRYPT_MEM_ALLOC_FAIL); return newPriKey; ERR: RSA_FREE_PRV_KEY(newPriKey); return NULL; } static CRYPT_RSA_Para *RSAParaDupCtx(CRYPT_RSA_Para *para) { CRYPT_RSA_Para *newPara = BSL_SAL_Malloc(sizeof(CRYPT_RSA_Para)); if (newPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newPara, sizeof(CRYPT_RSA_Para), 0, sizeof(CRYPT_RSA_Para)); newPara->bits = para->bits; GOTO_ERR_IF_SRC_NOT_NULL(newPara->e, para->e, BN_Dup(para->e), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPara->p, para->p, BN_Dup(para->p), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPara->q, para->q, BN_Dup(para->q), CRYPT_MEM_ALLOC_FAIL); return newPara; ERR: RSA_FREE_PARA(newPara); return NULL; } #if defined(HITLS_CRYPTO_RSA_BLINDING) || defined(HITLS_CRYPTO_RSA_BSSA) static RSA_Blind *RSABlindDupCtx(RSA_Blind *blind) { RSA_Blind *newBlind = BSL_SAL_Malloc(sizeof(RSA_Blind)); if (newBlind == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newBlind, sizeof(RSA_Blind), 0, sizeof(RSA_Blind)); GOTO_ERR_IF_SRC_NOT_NULL(newBlind->r, blind->r, BN_Dup(blind->r), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newBlind->rInv, blind->rInv, BN_Dup(blind->rInv), CRYPT_MEM_ALLOC_FAIL); return newBlind; ERR: RSA_BlindFreeCtx(newBlind); return NULL; } #endif #ifdef HITLS_CRYPTO_RSA_BSSA static RSA_BlindParam *RSABssADupCtx(RSA_BlindParam *blind) { RSA_BlindParam *newBlind = BSL_SAL_Calloc(1u, sizeof(RSA_BlindParam)); if (newBlind == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } if (blind->type == RSABSSA) { GOTO_ERR_IF_SRC_NOT_NULL(newBlind->para.bssa, blind->para.bssa, RSABlindDupCtx(blind->para.bssa), CRYPT_MEM_ALLOC_FAIL); newBlind->type = RSABSSA; return newBlind; } ERR: BSL_SAL_FREE(newBlind); return NULL; } #endif CRYPT_RSA_Ctx *CRYPT_RSA_DupCtx(CRYPT_RSA_Ctx *keyCtx) { if (keyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_RSA_Ctx *newKeyCtx = NULL; newKeyCtx = BSL_SAL_Malloc(sizeof(CRYPT_RSA_Ctx)); if (newKeyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newKeyCtx, sizeof(CRYPT_RSA_Ctx), 0, sizeof(CRYPT_RSA_Ctx)); newKeyCtx->flags = keyCtx->flags; (void)memcpy_s(&(newKeyCtx->pad), sizeof(RSAPad), &(keyCtx->pad), sizeof(RSAPad)); GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->prvKey, keyCtx->prvKey, RSAPriKeyDupCtx(keyCtx->prvKey), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->pubKey, keyCtx->pubKey, RSAPubKeyDupCtx(keyCtx->pubKey), CRYPT_MEM_ALLOC_FAIL); #ifdef HITLS_CRYPTO_RSA_BLINDING GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->scBlind, keyCtx->scBlind, RSABlindDupCtx(keyCtx->scBlind), CRYPT_MEM_ALLOC_FAIL); #endif #ifdef HITLS_CRYPTO_RSA_BSSA if (keyCtx->blindParam != NULL) { GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->blindParam, keyCtx->blindParam, RSABssADupCtx(keyCtx->blindParam), CRYPT_MEM_ALLOC_FAIL); } #endif GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->para, keyCtx->para, RSAParaDupCtx(keyCtx->para), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->mdAttr, keyCtx->mdAttr, BSL_SAL_Dump(keyCtx->mdAttr, strlen(keyCtx->mdAttr) + 1), CRYPT_MEM_ALLOC_FAIL); newKeyCtx->libCtx = keyCtx->libCtx; BSL_SAL_ReferencesInit(&(newKeyCtx->references)); return newKeyCtx; ERR: CRYPT_RSA_FreeCtx(newKeyCtx); return NULL; } static int32_t GetRsaParam(const BSL_Param *params, int32_t type, const uint8_t **value, uint32_t *valueLen) { const BSL_Param *temp = BSL_PARAM_FindConstParam(params, type); if (temp == NULL || temp->valueLen == 0 || temp->value == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *value = temp->value; *valueLen = temp->valueLen; return CRYPT_SUCCESS; } static int32_t GetRsaBits(const BSL_Param *params, uint32_t *bits) { uint32_t bitsLen = sizeof(*bits); const BSL_Param *temp = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_BITS); if (temp == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret = BSL_PARAM_GetValue(temp, CRYPT_PARAM_RSA_BITS, BSL_PARAM_TYPE_UINT32, bits, &bitsLen); if (ret != BSL_SUCCESS || *bits < RSA_MIN_MODULUS_BITS || *bits > RSA_MAX_MODULUS_BITS) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } return CRYPT_SUCCESS; } static int32_t ValidateRsaParams(uint32_t eLen, uint32_t bits) { /* the length of e cannot be greater than bits */ if (eLen > BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_ACVP_TESTS static int32_t ProcessRsaPrimeSeeds(const BSL_Param *para, CRYPT_RSA_Para *retPara, uint32_t bits) { const BSL_Param *sp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_RSA_XP); if (sp != NULL && sp->valueLen > 0) { if ((retPara->acvpTests.primeSeed.fipsPrimeSeeds.xp = BN_Create(bits)) == NULL || (BN_Bin2Bn(retPara->acvpTests.primeSeed.fipsPrimeSeeds.xp, sp->value, sp->valueLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } sp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_RSA_XP1); if (sp != NULL && sp->valueLen > 0) { if ((retPara->acvpTests.primeSeed.fipsPrimeSeeds.xp1 = BN_Create(bits)) == NULL || (BN_Bin2Bn(retPara->acvpTests.primeSeed.fipsPrimeSeeds.xp1, sp->value, sp->valueLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } sp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_RSA_XP2); if (sp != NULL && sp->valueLen > 0) { if ((retPara->acvpTests.primeSeed.fipsPrimeSeeds.xp2 = BN_Create(bits)) == NULL || (BN_Bin2Bn(retPara->acvpTests.primeSeed.fipsPrimeSeeds.xp2, sp->value, sp->valueLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } sp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_RSA_XQ); if (sp != NULL && sp->valueLen > 0) { if ((retPara->acvpTests.primeSeed.fipsPrimeSeeds.xq = BN_Create(bits)) == NULL || (BN_Bin2Bn(retPara->acvpTests.primeSeed.fipsPrimeSeeds.xq, sp->value, sp->valueLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } sp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_RSA_XQ1); if (sp != NULL && sp->valueLen > 0) { if ((retPara->acvpTests.primeSeed.fipsPrimeSeeds.xq1 = BN_Create(bits)) == NULL || (BN_Bin2Bn(retPara->acvpTests.primeSeed.fipsPrimeSeeds.xq1, sp->value, sp->valueLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } sp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_RSA_XQ2); if (sp != NULL && sp->valueLen > 0) { if ((retPara->acvpTests.primeSeed.fipsPrimeSeeds.xq2 = BN_Create(bits)) == NULL || (BN_Bin2Bn(retPara->acvpTests.primeSeed.fipsPrimeSeeds.xq2, sp->value, sp->valueLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } } return CRYPT_SUCCESS; } #endif static int32_t RsaNewParaBasicCheck(const CRYPT_RsaPara *para) { if (para == NULL || para->e == NULL || para->eLen == 0 || para->bits > RSA_MAX_MODULUS_BITS || para->bits < RSA_MIN_MODULUS_BITS) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } /* the length of e cannot be greater than bits */ if (para->eLen > BN_BITS_TO_BYTES(para->bits)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } return CRYPT_SUCCESS; } CRYPT_RSA_Para *CRYPT_RSA_NewPara(const CRYPT_RsaPara *para) { if (RsaNewParaBasicCheck(para) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } CRYPT_RSA_Para *retPara = BSL_SAL_Calloc(1, sizeof(CRYPT_RSA_Para)); if (retPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } retPara->bits = para->bits; retPara->e = BN_Create(para->bits); retPara->p = BN_Create(para->bits); retPara->q = BN_Create(para->bits); if (retPara->e == NULL || retPara->p == NULL || retPara->q == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } int32_t ret; ret = BN_Bin2Bn(retPara->e, para->e, para->eLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } if (BN_BITS_TO_BYTES(para->bits) > RSA_SMALL_MODULUS_BYTES && BN_Bytes(retPara->e) > RSA_MAX_PUBEXP_BYTES) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); goto ERR; } return retPara; ERR: CRYPT_RSA_FreePara(retPara); return NULL; } void CRYPT_RSA_FreePara(CRYPT_RSA_Para *para) { if (para == NULL) { return; } #ifdef HITLS_CRYPTO_ACVP_TESTS BN_Destroy(para->acvpTests.primeSeed.fipsPrimeSeeds.xp); BN_Destroy(para->acvpTests.primeSeed.fipsPrimeSeeds.xp1); BN_Destroy(para->acvpTests.primeSeed.fipsPrimeSeeds.xp2); BN_Destroy(para->acvpTests.primeSeed.fipsPrimeSeeds.xq); BN_Destroy(para->acvpTests.primeSeed.fipsPrimeSeeds.xq1); BN_Destroy(para->acvpTests.primeSeed.fipsPrimeSeeds.xq2); #endif BN_Destroy(para->e); BN_Destroy(para->p); BN_Destroy(para->q); BSL_SAL_FREE(para); } void RSA_FreePrvKey(CRYPT_RSA_PrvKey *prvKey) { if (prvKey == NULL) { return; } BN_Destroy(prvKey->n); BN_Destroy(prvKey->d); BN_Destroy(prvKey->p); BN_Destroy(prvKey->q); BN_Destroy(prvKey->e); BN_Destroy(prvKey->dP); BN_Destroy(prvKey->dQ); BN_Destroy(prvKey->qInv); BSL_SAL_FREE(prvKey); } void RSA_FreePubKey(CRYPT_RSA_PubKey *pubKey) { if (pubKey == NULL) { return; } BN_Destroy(pubKey->n); BN_Destroy(pubKey->e); BN_MontDestroy(pubKey->mont); BSL_SAL_FREE(pubKey); } void CRYPT_RSA_FreeCtx(CRYPT_RSA_Ctx *ctx) { if (ctx == NULL) { return; } int i = 0; BSL_SAL_AtomicDownReferences(&(ctx->references), &i); if (i > 0) { return; } BSL_SAL_ReferencesFree(&(ctx->references)); RSA_FREE_PARA(ctx->para); RSA_FREE_PRV_KEY(ctx->prvKey); RSA_FREE_PUB_KEY(ctx->pubKey); #ifdef HITLS_CRYPTO_RSA_BLINDING RSA_BlindFreeCtx(ctx->scBlind); ctx->scBlind = NULL; #endif #ifdef HITLS_CRYPTO_RSA_BSSA if (ctx->blindParam != NULL) { if (ctx->blindParam->type == RSABSSA) { RSA_BlindFreeCtx(ctx->blindParam->para.bssa); } BSL_SAL_FREE(ctx->blindParam); } #endif BSL_SAL_CleanseData((void *)(&(ctx->pad)), sizeof(RSAPad)); BSL_SAL_FREE(ctx->label.data); BSL_SAL_FREE(ctx->mdAttr); BSL_SAL_FREE(ctx); } static int32_t IsRSASetParamValid(const CRYPT_RSA_Ctx *ctx, const CRYPT_RSA_Para *para) { if (ctx == NULL || para == NULL || para->e == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (para->bits > RSA_MAX_MODULUS_BITS || para->bits < RSA_MIN_MODULUS_BITS) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } if (BN_GetBit(para->e, 0) != true || BN_IsLimb(para->e, 1) == true) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_E_VALUE); return CRYPT_RSA_ERR_E_VALUE; } return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_ACVP_TESTS static int32_t DupRsaPrimeSeeds(const CRYPT_RSA_Para *para, CRYPT_RSA_Para *paraCopy) { if (para->acvpTests.primeSeed.fipsPrimeSeeds.xp != NULL && (paraCopy->acvpTests.primeSeed.fipsPrimeSeeds.xp = BN_Dup(para->acvpTests.primeSeed.fipsPrimeSeeds.xp)) == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (para->acvpTests.primeSeed.fipsPrimeSeeds.xp1 != NULL && (paraCopy->acvpTests.primeSeed.fipsPrimeSeeds.xp1 = BN_Dup(para->acvpTests.primeSeed.fipsPrimeSeeds.xp1)) == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (para->acvpTests.primeSeed.fipsPrimeSeeds.xp2 != NULL && (paraCopy->acvpTests.primeSeed.fipsPrimeSeeds.xp2 = BN_Dup(para->acvpTests.primeSeed.fipsPrimeSeeds.xp2)) == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (para->acvpTests.primeSeed.fipsPrimeSeeds.xq != NULL && (paraCopy->acvpTests.primeSeed.fipsPrimeSeeds.xq = BN_Dup(para->acvpTests.primeSeed.fipsPrimeSeeds.xq)) == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (para->acvpTests.primeSeed.fipsPrimeSeeds.xq1 != NULL && (paraCopy->acvpTests.primeSeed.fipsPrimeSeeds.xq1 = BN_Dup(para->acvpTests.primeSeed.fipsPrimeSeeds.xq1)) == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (para->acvpTests.primeSeed.fipsPrimeSeeds.xq2 != NULL && (paraCopy->acvpTests.primeSeed.fipsPrimeSeeds.xq2 = BN_Dup(para->acvpTests.primeSeed.fipsPrimeSeeds.xq2)) == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } return CRYPT_SUCCESS; } #endif CRYPT_RSA_Para *CRYPT_RSA_DupPara(const CRYPT_RSA_Para *para) { CRYPT_RSA_Para *paraCopy = BSL_SAL_Calloc(1, sizeof(CRYPT_RSA_Para)); if (paraCopy == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } paraCopy->bits = para->bits; paraCopy->e = BN_Dup(para->e); paraCopy->p = BN_Dup(para->p); paraCopy->q = BN_Dup(para->q); if (paraCopy->e == NULL || paraCopy->p == NULL || paraCopy->q == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } #ifdef HITLS_CRYPTO_ACVP_TESTS int32_t ret = DupRsaPrimeSeeds(para, paraCopy); if (ret != CRYPT_SUCCESS) { goto ERR; } #endif return paraCopy; ERR: RSA_FREE_PARA(paraCopy); return NULL; } int32_t CRYPT_RSA_SetPara(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPara *para) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_RSA_Para *rsaPara = CRYPT_RSA_NewPara(para); if (rsaPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_NEW_PARA_FAIL); return CRYPT_EAL_ERR_NEW_PARA_FAIL; } int32_t ret = IsRSASetParamValid(ctx, rsaPara); if (ret != CRYPT_SUCCESS) { RSA_FREE_PARA(rsaPara); return ret; } (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad)); RSA_FREE_PARA(ctx->para); RSA_FREE_PRV_KEY(ctx->prvKey); RSA_FREE_PUB_KEY(ctx->pubKey); ctx->para = rsaPara; return CRYPT_SUCCESS; } #ifdef HITLS_BSL_PARAMS CRYPT_RSA_Para *CRYPT_RSA_NewParaEx(const BSL_Param *para) { const uint8_t *e = NULL; uint32_t eLen = 0; int32_t ret = GetRsaParam(para, CRYPT_PARAM_RSA_E, &e, &eLen); if (ret != CRYPT_SUCCESS) { return NULL; } uint32_t bits = 0; ret = GetRsaBits(para, &bits); if (ret != CRYPT_SUCCESS) { return NULL; } ret = ValidateRsaParams(eLen, bits); if (ret != CRYPT_SUCCESS) { return NULL; } CRYPT_RSA_Para *retPara = BSL_SAL_Calloc(1, sizeof(CRYPT_RSA_Para)); if (retPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } retPara->bits = bits; retPara->e = BN_Create(bits); retPara->p = BN_Create(bits); retPara->q = BN_Create(bits); if (retPara->e == NULL || retPara->p == NULL || retPara->q == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } ret = BN_Bin2Bn(retPara->e, e, eLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } if (BN_BITS_TO_BYTES(bits) > RSA_SMALL_MODULUS_BYTES && BN_Bytes(retPara->e) > RSA_MAX_PUBEXP_BYTES) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); goto ERR; } #ifdef HITLS_CRYPTO_ACVP_TESTS ret = ProcessRsaPrimeSeeds(para, retPara, bits); if (ret != CRYPT_SUCCESS) { goto ERR; } #endif return retPara; ERR: CRYPT_RSA_FreePara(retPara); return NULL; } static int32_t IsRSASetParamValidEx(const CRYPT_RSA_Para *para) { if (para == NULL || para->e == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (para->bits > RSA_MAX_MODULUS_BITS || para->bits < RSA_MIN_MODULUS_BITS) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } if (BN_GetBit(para->e, 0) != true || BN_IsLimb(para->e, 1) == true) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_E_VALUE); return CRYPT_RSA_ERR_E_VALUE; } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_SetParaEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para) { if (ctx == NULL || para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; #ifdef HITLS_CRYPTO_PROVIDER const BSL_Param *temp = NULL; if ((temp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_MD_ATTR)) != NULL) { ret = CRYPT_PkeySetMdAttr((const char *)(temp->value), temp->valueLen, &(ctx->mdAttr)); if (ret != CRYPT_SUCCESS) { return ret; } } #endif if (BSL_PARAM_FindConstParam(para, CRYPT_PARAM_RSA_E) == NULL) { return CRYPT_SUCCESS; } CRYPT_RSA_Para *rsaPara = CRYPT_RSA_NewParaEx(para); if (rsaPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_NEW_PARA_FAIL); return CRYPT_EAL_ERR_NEW_PARA_FAIL; } ret = IsRSASetParamValidEx(rsaPara); if (ret != CRYPT_SUCCESS) { RSA_FREE_PARA(rsaPara); return ret; } (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad)); RSA_FREE_PARA(ctx->para); RSA_FREE_PRV_KEY(ctx->prvKey); RSA_FREE_PUB_KEY(ctx->pubKey); ctx->para = rsaPara; return CRYPT_SUCCESS; } #endif CRYPT_RSA_PrvKey *RSA_NewPrvKey(uint32_t bits) { CRYPT_RSA_PrvKey *priKey = BSL_SAL_Malloc(sizeof(CRYPT_RSA_PrvKey)); if (priKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } priKey->n = BN_Create(bits); priKey->d = BN_Create(bits); priKey->p = BN_Create(bits >> 1); priKey->q = BN_Create(bits >> 1); priKey->e = BN_Create(bits >> 1); priKey->dP = BN_Create(bits >> 1); priKey->dQ = BN_Create(bits >> 1); priKey->qInv = BN_Create(bits >> 1); bool creatFailed = (priKey->n == NULL || priKey->d == NULL || priKey->e == NULL || priKey->p == NULL || priKey->q == NULL || priKey->dP == NULL || priKey->dQ == NULL || priKey->qInv == NULL); if (creatFailed) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); RSA_FREE_PRV_KEY(priKey); } return priKey; } CRYPT_RSA_PubKey *RSA_NewPubKey(uint32_t bits) { CRYPT_RSA_PubKey *pubKey = BSL_SAL_Malloc(sizeof(CRYPT_RSA_PubKey)); if (pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } pubKey->n = BN_Create(bits); pubKey->e = BN_Create(bits); pubKey->mont = NULL; if (pubKey->n == NULL || pubKey->e == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); RSA_FREE_PUB_KEY(pubKey); } return pubKey; } uint32_t CRYPT_RSA_GetBits(const CRYPT_RSA_Ctx *ctx) { if (ctx == NULL) { return 0; } if (ctx->para != NULL) { return ctx->para->bits; } if (ctx->prvKey != NULL) { return BN_Bits(ctx->prvKey->n); } if (ctx->pubKey != NULL) { return BN_Bits(ctx->pubKey->n); } return 0; } #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) uint32_t CRYPT_RSA_GetSignLen(const CRYPT_RSA_Ctx *ctx) { return BN_BITS_TO_BYTES(CRYPT_RSA_GetBits(ctx)); } #endif #ifdef HITLS_CRYPTO_RSA_GEN static int32_t GetRandomX(void *libCtx, BN_BigNum *X, uint32_t nlen, bool isP) { /* * The FIPS 185-5 Appendix B.9 required √2(2 ^(nlen/2 - 1)) <= x <= ((2 ^(nlen/2) - 1)) * hence we can limit it as follows: * √2 ~= 1.41421 < 1.5 --> * √2(2 ^(nlen/2 - 1)) < 1.5 * (2 ^(nlen/2 - 1)) * next, we need to prove 1.5 * (2 ^(nlen/2 - 1)) <= ((2 ^(nlen/2) - 1)) * --> let x = 2 ^(nlen/2), 1.5 * (x/2) ≤ x - 1 * --> (3/4) x ≤ x - 1 * --> x >= 4, obviously correct. * And, 1.5 * 2 ^(nlen/2 - 1) = 2 ^ (nlen/2 - 1) + 2 ^ (nlen/2 - 2); * If we follow these steps to construct the bigNum: * i. Randomly generate a random number, the most significant bit is (nlen / 2). * ii. Set the (nlen/2 - 1) bits. * We can obtain the x, satisfied [ 1.5 * 2 ^(nlen/2 - 1), ((2 ^(nlen/2) - 1) ]. */ if ((nlen % 2) == 0) { return BN_RandEx(libCtx, X, nlen >> 1, BN_RAND_TOP_TWOBIT, BN_RAND_BOTTOM_NOBIT); } /* * Meanwhile, if nlen is odd, We need to consider p, q separately. */ if (isP) { /* * left : √2(2 ^(nlen/2 - 1)) < 2 ^ ⌊ (nlen / 2) ⌋ * right: if nlen is odd, 2 ^ (nlen/2) - 1 == 2 ^ ( ⌊ (nlen)/2 ⌋ + 1/2) - 1 == √2 * 2 ^ (⌊ (nlen)/2 ⌋) - 1 * if we want left <= right: * 2 ^ ⌊ (nlen / 2) ⌋ < √2 * 2 ^ (⌊ (nlen)/2 ⌋) - 1 * --> 2 ^ ⌊ (nlen / 2) ⌋ < 1.4 * 2 ^ (⌊ (nlen)/2 ⌋) - 1 * --> 1 < 0.4 * 2 ^ (⌊ (nlen)/2 ⌋) * --> nlen >= 3, obviously correct. * hence, We can obtain the x, set the (nlen)/2 + 1 bits. */ return BN_RandEx(libCtx, X, (nlen + 1) >> 1, BN_RAND_TOP_ONEBIT, BN_RAND_BOTTOM_NOBIT); } return BN_RandEx(libCtx, X, nlen >> 1, BN_RAND_TOP_TWOBIT, BN_RAND_BOTTOM_NOBIT); } /* * Ref: FIPS 186-5: Table A.1 * Get the maximum lengths of p1, p2, q1, and q2. */ static uint32_t GetAuxiliaryPrimeBitLen(uint32_t nlen) { if (nlen <= 3071) { return 141; } else if (nlen <= 4095) { return 171; } else { return 201; } } /* * Ref: FIPS 186-5: Table A.1 * Get the maximum lengths of p, q. */ static uint32_t GetProbableNoLimitedBitLen(uint32_t nlen) { if (nlen <= 3071) { return 1007; } else if (nlen <= 4095) { return 1518; } else { return 2030; } } /* * Ref: FIPS 186-5: Table B.1 * Get minimum number of rounds of M-R testing when generating auxiliary primes. */ static uint32_t GetAuxPrimeMillerCheckTimes(uint32_t auxBits) { if (auxBits <= 170) { return 38; // Error probability = 2 ^ (-112) } else if (auxBits <= 200) { return 41; // Error probability = 2 ^ (-128) } else { return 44; // Error probability = 2 ^ (-144) } } /* * Ref: FIPS 186-5: Table B.1 * Get minimum number of rounds of M-R testing when generating probable primes. */ static uint32_t GetProbPrimeMillerCheckTimes(uint32_t proBits) { if (proBits < 1536) { return 5; } return 4; } static int32_t GenAuxPrime(BN_BigNum *Xp, uint32_t auxBits, BN_Optimizer *opt, bool isSeed) { int32_t ret = CRYPT_SUCCESS; if (!isSeed) { ret = BN_RandEx(BN_OptimizerGetLibCtx(opt), Xp, auxBits, BN_RAND_TOP_ONEBIT, BN_RAND_BOTTOM_ONEBIT); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } uint32_t auxPrimeCheck = GetAuxPrimeMillerCheckTimes(auxBits); do { ret = BN_PrimeCheck(Xp, auxPrimeCheck, opt, NULL); if (ret == CRYPT_SUCCESS) { return ret; } if (ret != CRYPT_BN_NOR_CHECK_PRIME) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_AddLimb(Xp, Xp, 2); // Try with odd numbers every time. if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } while (true); } /* * Ref: FIPS 186-5 B.9 Compute a Probable Prime Factor Based on Auxiliary Primes. * The standard specifies that the length of two small primes should meet * len(r1) + len(r2) ≤ (nlen/2) – log2(nlen/2) – 7 * If nlen = 1024, r1, r2 is obtained by search from 141 bits data, the above inequality is still satisfied. * Hence, it's a only performance consideration for us to use this standard for 1024-bit rsa key-Gen. */ static int32_t GenPrimeWithAuxiliaryPrime(uint32_t auxBits, uint32_t proBits, BN_BigNum *Xp, BN_BigNum *Xp0, BN_BigNum *Xp1, BN_BigNum *Xp2, BN_BigNum *p, const CRYPT_RSA_Para *para, bool isP, BN_Optimizer *opt) { BN_BigNum *r1; BN_BigNum *r2; uint32_t auxRoom = BITS_TO_BN_UNIT(auxBits); int32_t ret = OptimizerStart(opt); // use the optimizer if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t probPrimeCheck = GetProbPrimeMillerCheckTimes(proBits); r1 = (Xp1 != NULL) ? Xp1 : OptimizerGetBn(opt, auxRoom); r2 = (Xp2 != NULL) ? Xp2 : OptimizerGetBn(opt, auxRoom); BN_BigNum *r1Double = OptimizerGetBn(opt, auxRoom); BN_BigNum *primeCheck = OptimizerGetBn(opt, auxRoom); BN_BigNum *r2Inv = OptimizerGetBn(opt, auxRoom); BN_BigNum *r1DoubleInv = OptimizerGetBn(opt, auxRoom); BN_BigNum *R = OptimizerGetBn(opt, auxRoom); BN_BigNum *pMinusOne = OptimizerGetBn(opt, BITS_TO_BN_UNIT(proBits)); uint32_t bits = isP ? (para->bits + 1) >> 1 : (para->bits >> 1); // Avoid the bit is odd. uint32_t iterRound = 20 * bits; // Step 9 specifies that the iteration round is 20 * (nlen/2); if (r1 == NULL || r2 == NULL || r1Double == NULL || primeCheck == NULL || r2Inv == NULL || r1DoubleInv == NULL || R == NULL || pMinusOne == NULL) { ret = CRYPT_BN_OPTIMIZER_GET_FAIL; OptimizerEnd(opt); return ret; } // Choose auxiliary prime r1, either from seed or generate randomly ret = GenAuxPrime(r1, auxBits, opt, (Xp1 != NULL)); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); OptimizerEnd(opt); return ret; } GOTO_ERR_IF(GenAuxPrime(r2, auxBits, opt, (Xp2 != NULL)), ret); GOTO_ERR_IF(BN_Lshift(r1Double, r1, 1), ret); // Step 1: check 2r1, r2 are coprime. GOTO_ERR_IF(BN_Gcd(primeCheck, r1Double, r2, opt), ret); if (!BN_IsOne(primeCheck)) { ret = CRYPT_RSA_NOR_KEYGEN_FAIL; BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_KEYGEN_FAIL); goto ERR; } // Step 2: cal R = (r2^-1 mod 2r1) * r2 - ((2 * r1)^-1 mod r2) * (2 * r1) GOTO_ERR_IF(BN_ModInv(r2Inv, r2, r1Double, opt), ret); // (r2^-1 mod 2r1) * r2 GOTO_ERR_IF(BN_Mul(r2Inv, r2, r2Inv, opt), ret); // ((2 * r1)^-1 mod r2) * (2 * r1) GOTO_ERR_IF(BN_ModInv(r1DoubleInv, r1Double, r2, opt), ret); GOTO_ERR_IF(BN_Mul(r1DoubleInv, r1Double, r1DoubleInv, opt), ret); // get R. GOTO_ERR_IF(BN_Sub(R, r2Inv, r1DoubleInv), ret); do { // Step 3: get x via seed xp/xq or random if (Xp0 == NULL) { GOTO_ERR_IF(GetRandomX(BN_OptimizerGetLibCtx(opt), Xp, para->bits, isP), ret); } // Step 4: Y = X + ((R – X) mod 2r1r2 GOTO_ERR_IF(BN_Mul(r1, r1Double, r2, opt), ret); // 2r1r2 GOTO_ERR_IF(BN_ModSub(R, R, Xp, r1, opt), ret); GOTO_ERR_IF(BN_Add(p, Xp, R), ret); uint32_t i = 0; for (; i < iterRound; i++) { // Step 6: Check p ≥ 2 ^ (nlen/2) if (BN_Bits(p) > bits) { break; } // Step 7: Check the p - 1 and e are corprime. GOTO_ERR_IF(BN_SubLimb(pMinusOne, p, 1), ret); GOTO_ERR_IF(BN_Gcd(pMinusOne, pMinusOne, para->e, opt), ret); if (BN_IsOne(pMinusOne)) { // Step 7.1: Check the primality of p. ret = BN_PrimeCheck(p, probPrimeCheck, opt, NULL); if (ret == CRYPT_SUCCESS) { // We find a primes successfully. goto ERR; } if (ret != CRYPT_BN_NOR_CHECK_PRIME) { // Another exception has occurred. BSL_ERR_PUSH_ERROR(ret); goto ERR; } } // Step 10: Update p. GOTO_ERR_IF(BN_Add(p, p, r1), ret); } // Step 9: check i ≥ 20 * (nlen/2). if (i == iterRound) { ret = CRYPT_RSA_NOR_KEYGEN_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } } while (true); ERR: if (Xp1 == NULL) { BN_Zeroize(r1); } if (Xp2 == NULL) { BN_Zeroize(r2); } OptimizerEnd(opt); return ret; } // ref: FIPS 186-5, A.1.6 & B.9 static int32_t GenPQBasedOnProbPrimes(const CRYPT_RSA_Para *para, CRYPT_RSA_PrvKey *priKey, BN_Optimizer *opt) { BN_BigNum *Xp = NULL, *Xq = NULL, *Xp0 = NULL, *Xp1 = NULL, *Xp2 = NULL, *Xq0 = NULL, *Xq1 = NULL, *Xq2 = NULL; uint32_t proBits = GetProbableNoLimitedBitLen(para->bits); uint32_t auxBits = GetAuxiliaryPrimeBitLen(para->bits); // Used in check |Xp – Xq| ≤ 2^(nlen/2) – 100 or |p – q| ≤ 2^(nlen/2) – 100. uint32_t secBits = ((para->bits + 1) >> 1) - 100; uint32_t proRoom = BITS_TO_BN_UNIT(proBits); int32_t ret = OptimizerStart(opt); // use the optimizer if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } #ifdef HITLS_CRYPTO_ACVP_TESTS Xp0 = para->acvpTests.primeSeed.fipsPrimeSeeds.xp; Xp1 = para->acvpTests.primeSeed.fipsPrimeSeeds.xp1; Xp2 = para->acvpTests.primeSeed.fipsPrimeSeeds.xp2; Xq0 = para->acvpTests.primeSeed.fipsPrimeSeeds.xq; Xq1 = para->acvpTests.primeSeed.fipsPrimeSeeds.xq1; Xq2 = para->acvpTests.primeSeed.fipsPrimeSeeds.xq2; #endif Xp = (Xp0 != NULL) ? Xp0 : OptimizerGetBn(opt, proRoom); Xq = (Xq0 != NULL) ? Xq0 : OptimizerGetBn(opt, proRoom); if (Xp == NULL || Xq == NULL) { ret = CRYPT_BN_OPTIMIZER_GET_FAIL; BSL_ERR_PUSH_ERROR(ret); OptimizerEnd(opt); return ret; } // Step 4: get p ret = GenPrimeWithAuxiliaryPrime(auxBits, proBits, Xp, Xp0, Xp1, Xp2, priKey->p, para, true, opt); if (ret != CRYPT_SUCCESS) { BN_Zeroize(Xp); BSL_ERR_PUSH_ERROR(ret); OptimizerEnd(opt); return ret; } /* * If |Xp – Xq| ≤ 2 ^ (2nlen/2 – 100) or |p – q| ≤ 2 ^ (2nlen/2 – 100), need to try again. * We think there can ever be repeated many times here unless the 'random' is stuck. * For example, nlen = 2048 and |Xp – Xq| ≤ 2 ^ (1024 – 100), it means that the most significant * 99 bits of our Xq and Xp randomly generated are all identical. It's a low-probability event. */ do { // Step 5: get q ret = GenPrimeWithAuxiliaryPrime(auxBits, proBits, Xq, Xq0, Xq1, Xq2, priKey->q, para, false, opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } // Step 6: Check (|Xp – Xq| ≤ 2^(nlen/2) – 100) and (|p – q| ≤ 2^(nlen/2) – 100) ret = BN_Sub(Xq, Xp, Xq); // Xq dont needs anymore, but Xp may be used. if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } // |Xp – Xq| ≤ 2 ^ (2nlen/2 – 100) -> BN_Bits(Xp) <= secBits + 1 -> BN_Bits(Xp) < secBits if (BN_Bits(Xq) < secBits) { if (Xq0 != NULL && Xq1 != NULL && Xq2 != NULL) { ret = CRYPT_RSA_NOR_KEYGEN_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } continue; } ret = BN_Sub(Xq, priKey->p, priKey->q); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } // |p – q| ≤ 2 ^ (2nlen/2 – 100) if (BN_Bits(Xq) < secBits) { if (Xq0 != NULL && Xq1 != NULL && Xq2 != NULL) { ret = CRYPT_RSA_NOR_KEYGEN_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } continue; } break; } while (true); ERR: if (Xp0 == NULL) { BN_Zeroize(Xp); } if (Xq0 == NULL) { BN_Zeroize(Xq); } OptimizerEnd(opt); return ret; } #endif static int32_t RsaPrvKeyCalcND( const CRYPT_RSA_Para *para, CRYPT_RSA_Ctx *ctx, BN_BigNum *pMinusOne, BN_BigNum *qMinusOne, BN_Optimizer *optimizer) { int32_t ret; if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = OptimizerStart(optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } CRYPT_RSA_PrvKey *prvKey = ctx->prvKey; BN_BigNum *l = OptimizerGetBn(optimizer, BITS_TO_BN_UNIT(para->bits)); BN_BigNum *u = OptimizerGetBn(optimizer, BITS_TO_BN_UNIT(para->bits)); if (l == NULL || u == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_BN_OPTIMIZER_GET_FAIL); ret = CRYPT_BN_OPTIMIZER_GET_FAIL; goto EXIT; } ret = BN_Mul(prvKey->n, prvKey->p, prvKey->q, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Mul(l, pMinusOne, qMinusOne, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Gcd(u, pMinusOne, qMinusOne, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Div(l, NULL, l, u, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_ModInv(prvKey->d, para->e, l, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: OptimizerEnd(optimizer); return ret; } // p, q [ => n, d] => dP dQ qInv // ctx->para may be NULL when setting key int32_t RSA_CalcPrvKey(const CRYPT_RSA_Para *para, CRYPT_RSA_Ctx *ctx, BN_Optimizer *optimizer) { int32_t ret; CRYPT_RSA_PrvKey *prvKey = ctx->prvKey; uint32_t needRoom = BITS_TO_BN_UNIT(BN_Bits(prvKey->p)); ret = OptimizerStart(optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } BN_BigNum *pMinusOne = OptimizerGetBn(optimizer, needRoom); BN_BigNum *qMinusOne = OptimizerGetBn(optimizer, needRoom); if (pMinusOne == NULL || qMinusOne == NULL) { ret = CRYPT_BN_OPTIMIZER_GET_FAIL; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_SubLimb(pMinusOne, prvKey->p, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_SubLimb(qMinusOne, prvKey->q, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if (BN_IsZero(prvKey->n)) { // when generating key ret = RsaPrvKeyCalcND(para, ctx, pMinusOne, qMinusOne, optimizer); if (ret != CRYPT_SUCCESS) { goto EXIT; } } ret = BN_ModInv(prvKey->qInv, prvKey->q, prvKey->p, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Div(NULL, prvKey->dP, prvKey->d, pMinusOne, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Div(NULL, prvKey->dQ, prvKey->d, qMinusOne, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: OptimizerEnd(optimizer); return ret; } #ifdef HITLS_CRYPTO_RSA_GEN /* * In NIST SP 800-56B, Section 6.4.1.1, requiring we should perform a successful key-pair validation * while generating the key pair. */ static int32_t RSA_KeyValidationCheck(CRYPT_RSA_Ctx *ctx, uint32_t bits) { int32_t ret; BN_BigNum *val = BN_Create(1); BN_BigNum *expect = BN_Create(bits); if (val == NULL || expect == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } // for performance reasons, we choose test num = 2. (void)BN_SetLimb(val, 2); // val is not null, and the val-memory must be sufficient. GOTO_ERR_IF(BN_MontExp(expect, val, ctx->prvKey->e, ctx->pubKey->mont, NULL), ret); GOTO_ERR_IF(BN_MontExpConsttime(expect, expect, ctx->prvKey->d, ctx->pubKey->mont, NULL), ret); if (BN_Cmp(val, expect) != 0) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE); goto ERR; } ERR: BN_Destroy(val); BN_Destroy(expect); return ret; } int32_t CRYPT_RSA_Gen(CRYPT_RSA_Ctx *ctx) { if (ctx == NULL || ctx->para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_MEM_ALLOC_FAIL; BN_Optimizer *optimizer = NULL; CRYPT_RSA_Ctx *newCtx = CRYPT_RSA_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(ret); return ret; } newCtx->prvKey = RSA_NewPrvKey(ctx->para->bits); newCtx->pubKey = RSA_NewPubKey(ctx->para->bits); optimizer = BN_OptimizerCreate(); if (optimizer == NULL || newCtx->prvKey == NULL || newCtx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } /* * Currently, although the FIPS 186-5 standard does not support key generation of 1024 bits * due to its low security, our interface does not lift this restriction. * Meanwhile, the check of e is not added to ensure compatibility. */ BN_OptimizerSetLibCtx(ctx->libCtx, optimizer); ret = GenPQBasedOnProbPrimes(ctx->para, newCtx->prvKey, optimizer); if (ret != CRYPT_SUCCESS) { BN_OptimizerDestroy(optimizer); BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = RSA_CalcPrvKey(ctx->para, newCtx, optimizer); BN_OptimizerDestroy(optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(BN_Copy(newCtx->pubKey->n, newCtx->prvKey->n), ret); GOTO_ERR_IF(BN_Copy(newCtx->pubKey->e, ctx->para->e), ret); GOTO_ERR_IF(BN_Copy(newCtx->prvKey->e, ctx->para->e), ret); if ((newCtx->pubKey->mont = BN_MontCreate(newCtx->pubKey->n)) == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = RSA_KeyValidationCheck(newCtx, ctx->para->bits); if (ret != CRYPT_SUCCESS) { goto ERR; // dont't push the stack repeatedly. } ShallowCopyCtx(ctx, newCtx); BSL_SAL_FREE(newCtx); return ret; ERR: CRYPT_RSA_FreeCtx(newCtx); return ret; } void ShallowCopyCtx(CRYPT_RSA_Ctx *ctx, CRYPT_RSA_Ctx *newCtx) { RSA_FREE_PRV_KEY(ctx->prvKey); RSA_FREE_PUB_KEY(ctx->pubKey); #ifdef HITLS_CRYPTO_RSA_BLINDING RSA_BlindFreeCtx(ctx->scBlind); #endif BSL_SAL_ReferencesFree(&(newCtx->references)); ctx->prvKey = newCtx->prvKey; ctx->pubKey = newCtx->pubKey; #ifdef HITLS_CRYPTO_RSA_BLINDING ctx->scBlind = newCtx->scBlind; #endif ctx->pad = newCtx->pad; ctx->flags = newCtx->flags; } #endif // HITLS_CRYPTO_RSA_GEN #ifdef HITLS_CRYPTO_PROVIDER static bool IsExistPrvKeyParams(const BSL_Param *params) { const BSL_Param *d = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_D); const BSL_Param *n = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_N); const BSL_Param *p = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_P); const BSL_Param *q = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_Q); const BSL_Param *dp = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_DP); const BSL_Param *dq = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_DQ); const BSL_Param *qInv = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_QINV); return n != NULL && d != NULL && (PARAMISNULL(p) == PARAMISNULL(q)) && (PARAMISNULL(dp) == PARAMISNULL(dq)) && PARAMISNULL(dq) == PARAMISNULL(qInv); } static bool IsExistPubKeyParams(const BSL_Param *params) { const BSL_Param *e = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_E); const BSL_Param *n = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_N); return e != NULL && n != NULL; } static bool IsExistRsaParam(const BSL_Param *params) { const BSL_Param *bits = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_BITS); const BSL_Param *e = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_E); return bits != NULL && e != NULL; } int32_t CRYPT_RSA_Import(CRYPT_RSA_Ctx *ctx, const BSL_Param *params) { int32_t ret = CRYPT_SUCCESS; if (IsExistPrvKeyParams(params)) { ret = CRYPT_RSA_SetPrvKeyEx(ctx, params); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } if (IsExistPubKeyParams(params)) { ret = CRYPT_RSA_SetPubKeyEx(ctx, params); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } if (IsExistRsaParam(params)) { ret = CRYPT_RSA_SetParaEx(ctx, params); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } const BSL_Param *mdIdParam = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_MD_ID); const BSL_Param *mgf1IdParam = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_MGF1_ID); const BSL_Param *saltLenParam = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_RSA_SALTLEN); if (mdIdParam != NULL && mgf1IdParam != NULL && saltLenParam != NULL) { ret = CRYPT_RSA_Ctrl(ctx, CRYPT_CTRL_SET_RSA_EMSA_PSS, (void *)(uintptr_t)params, 0); } else if (mdIdParam != NULL && mdIdParam->valueType == BSL_PARAM_TYPE_INT32 && mdIdParam->value != NULL) { int32_t mdId = *(int32_t *)mdIdParam->value; ret = CRYPT_RSA_Ctrl(ctx, CRYPT_CTRL_SET_RSA_EMSA_PKCSV15, &mdId, sizeof(mdId)); } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static void InitRsaPubKeyParams(BSL_Param *params, uint32_t *index, uint8_t *buffer, uint32_t len) { (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_E, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_N, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; } static void InitRsaPrvKeyParams(BSL_Param *params, uint32_t *index, uint8_t *buffer, uint32_t len) { (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_D, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_P, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_Q, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_DP, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_DQ, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_QINV, BSL_PARAM_TYPE_OCTETS, buffer + ((*index) * len), len); (*index)++; } static void ExportRsaPssParams(const CRYPT_RSA_Ctx *ctx, BSL_Param *params, uint32_t *index) { (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_UINT32, (void *)(uintptr_t)&ctx->pad.para.pss.mdId, sizeof(uint32_t)); params[(*index)++].useLen = sizeof(uint32_t); (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_MGF1_ID, BSL_PARAM_TYPE_UINT32, (void *)(uintptr_t)&ctx->pad.para.pss.mgfId, sizeof(uint32_t)); params[(*index)++].useLen = sizeof(uint32_t); (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_SALTLEN, BSL_PARAM_TYPE_UINT32, (void *)(uintptr_t)&ctx->pad.para.pss.saltLen, sizeof(uint32_t)); params[(*index)++].useLen = sizeof(uint32_t); } static void ExportRsaPkcsParams(const CRYPT_RSA_Ctx *ctx, BSL_Param *params, uint32_t *index) { (void)BSL_PARAM_InitValue(&params[*index], CRYPT_PARAM_RSA_MD_ID, BSL_PARAM_TYPE_UINT32, (void *)(uintptr_t)&ctx->pad.para.pkcsv15.mdId, sizeof(uint32_t)); params[(*index)++].useLen = sizeof(uint32_t); } int32_t CRYPT_RSA_Export(const CRYPT_RSA_Ctx *ctx, BSL_Param *params) { if (ctx == NULL || params == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t index = 1; void *args = NULL; CRYPT_EAL_ProcessFuncCb processCb = NULL; uint32_t keyBits = CRYPT_RSA_GetBits(ctx); if (keyBits == 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } uint32_t bytes = BN_BITS_TO_BYTES(keyBits); BSL_Param rsaParams[13] = { {CRYPT_PARAM_RSA_BITS, BSL_PARAM_TYPE_UINT32, &keyBits, sizeof(uint32_t), sizeof(uint32_t)}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, BSL_PARAM_END}; int32_t ret = CRYPT_GetPkeyProcessParams(params, &processCb, &args); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint8_t *buffer = BSL_SAL_Calloc(1, keyBits * 8); if (buffer == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (ctx->pubKey != NULL) { InitRsaPubKeyParams(rsaParams, &index, buffer, bytes); ret = CRYPT_RSA_GetPubKeyEx(ctx, rsaParams); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(buffer); BSL_ERR_PUSH_ERROR(ret); return ret; } } if (ctx->prvKey != NULL) { InitRsaPrvKeyParams(rsaParams, &index, buffer, bytes); ret = CRYPT_RSA_GetPrvKeyEx(ctx, rsaParams); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(buffer); BSL_ERR_PUSH_ERROR(ret); return ret; } } if (ctx->pad.type == EMSA_PSS) { ExportRsaPssParams(ctx, rsaParams, &index); } else if (ctx->pad.type == EMSA_PKCSV15) { ExportRsaPkcsParams(ctx, rsaParams, &index); } for (uint32_t i = 0; i < index; i++) { rsaParams[i].valueLen = rsaParams[i].useLen; } ret = processCb(rsaParams, args); BSL_SAL_Free(buffer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } #endif // HITLS_CRYPTO_PROVIDER #endif /* HITLS_CRYPTO_RSA */
2301_79861745/bench_create
crypto/rsa/src/rsa_keygen.c
C
unknown
50,299
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include "crypt_types.h" #include "crypt_rsa.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "rsa_local.h" #include "crypt_errno.h" #include "securec.h" #include "bsl_sal.h" #include "crypt_params_key.h" static int32_t SetPrvPara(const CRYPT_RSA_PrvKey *prvKey, const CRYPT_RsaPrv *prv) { int32_t ret = BN_Bin2Bn(prvKey->n, prv->n, prv->nLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t bnBits = BN_Bits(prvKey->n); if (bnBits > RSA_MAX_MODULUS_BITS || bnBits < RSA_MIN_MODULUS_BITS) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } ret = BN_Bin2Bn(prvKey->d, prv->d, prv->dLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // d cannot be 0 or 1. The mathematical logic of e and d is that // d and e are reciprocal in mod((p-1) * (q-1)); When d is 1, e and d must be 1. When d is 0, e doesn't exist. if (BN_IsZero(prvKey->d) || BN_IsOne(prvKey->d)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } if (BN_Cmp(prvKey->n, prvKey->d) <= 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } if (prv->e != NULL) { ret = BN_Bin2Bn(prvKey->e, prv->e, prv->eLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (BN_Cmp(prvKey->n, prvKey->e) <= 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } } if (prv->p != NULL) { GOTO_ERR_IF_EX(BN_Bin2Bn(prvKey->p, prv->p, prv->pLen), ret); GOTO_ERR_IF_EX(BN_Bin2Bn(prvKey->q, prv->q, prv->qLen), ret); if (BN_IsZero(prvKey->p) == true || BN_IsZero(prvKey->q) == true) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } if (prv->dP != NULL) { GOTO_ERR_IF_EX(BN_Bin2Bn(prvKey->dP, prv->dP, prv->dPLen), ret); GOTO_ERR_IF_EX(BN_Bin2Bn(prvKey->dQ, prv->dQ, prv->dQLen), ret); GOTO_ERR_IF_EX(BN_Bin2Bn(prvKey->qInv, prv->qInv, prv->qInvLen), ret); } } ERR: return ret; } // If n and d are not NULL, p and q are optional. If p and q exist, qInv, dP, and dQ need to be calculated. static int32_t SetPrvBasicCheck(const CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPrv *prv) { if (ctx == NULL || prv == NULL || prv->n == NULL || prv->d == NULL || prv->nLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prv->nLen > RSA_MAX_MODULUS_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } // prv->p\q and prv->dP\dQ\qInv must be both empty or not. // If prv->p is empty, prv->dP must be empty. if ((prv->p == NULL) != (prv->q == NULL) || (prv->p == NULL && prv->dP != NULL) || ((prv->dP == NULL || prv->dQ == NULL || prv->qInv == NULL) && (prv->dP || prv->dQ || prv->qInv))) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NO_KEY_INFO); return CRYPT_RSA_NO_KEY_INFO; } return CRYPT_SUCCESS; } static int32_t SetPrvBnLenCheck(const CRYPT_RsaPrv *prv) { /* The length of n is used as the length of a BigNum. The lengths of d, p, and q are not greater than n. */ uint32_t bnBytes = prv->nLen; if (prv->dLen > bnBytes || prv->pLen > bnBytes || prv->qLen > bnBytes) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_SetPrvKey(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPrv *prv) { int32_t ret = SetPrvBasicCheck(ctx, prv); if (ret != CRYPT_SUCCESS) { return ret; } ret = SetPrvBnLenCheck(prv); if (ret != CRYPT_SUCCESS) { return ret; } CRYPT_RSA_Ctx *newCtx = CRYPT_RSA_NewCtx(); if (newCtx == NULL) { return CRYPT_MEM_ALLOC_FAIL; } newCtx->prvKey = RSA_NewPrvKey(prv->nLen * 8); // Bit length is obtained by multiplying byte length by 8. if (newCtx->prvKey == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(SetPrvPara(newCtx->prvKey, prv), ret); if (prv->p != NULL && prv->dP == NULL) { BN_Optimizer *optimizer = BN_OptimizerCreate(); if (optimizer == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = RSA_CalcPrvKey(newCtx->para, newCtx, optimizer); BN_OptimizerDestroy(optimizer); if (ret != CRYPT_SUCCESS) { goto ERR; } } RSA_FREE_PRV_KEY(ctx->prvKey); #ifdef HITLS_CRYPTO_RSA_BLINDING RSA_BlindFreeCtx(ctx->scBlind); ctx->scBlind = newCtx->scBlind; #endif ctx->prvKey = newCtx->prvKey; ctx->pad = newCtx->pad; BSL_SAL_ReferencesFree(&(newCtx->references)); BSL_SAL_FREE(newCtx); return ret; ERR: CRYPT_RSA_FreeCtx(newCtx); return ret; } static int32_t SetPubBasicCheck(const CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPub *pub) { if (ctx == NULL || pub == NULL || pub->n == NULL || pub->e == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pub->nLen > RSA_MAX_MODULUS_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } /* The length of n is used as the length of a BigNum, and the length of e is not greater than n. */ if (pub->eLen > pub->nLen) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_SetPubKey(CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPub *pub) { int32_t ret = SetPubBasicCheck(ctx, pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t bnBits; CRYPT_RSA_PubKey *newPub = NULL; (void)memset_s(&(ctx->pad), sizeof(RSAPad), 0, sizeof(RSAPad)); /* Bit length is obtained by multiplying byte length by 8. */ newPub = RSA_NewPubKey(pub->nLen * 8); if (newPub == NULL) { return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF(BN_Bin2Bn(newPub->n, pub->n, pub->nLen), ret); bnBits = BN_Bits(newPub->n); if (bnBits > RSA_MAX_MODULUS_BITS || bnBits < RSA_MIN_MODULUS_BITS) { ret = CRYPT_RSA_ERR_KEY_BITS; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(BN_Bin2Bn(newPub->e, pub->e, pub->eLen), ret); if (pub->nLen > RSA_SMALL_MODULUS_BYTES && BN_Bytes(newPub->e) > RSA_MAX_PUBEXP_BYTES) { ret = CRYPT_RSA_ERR_KEY_BITS; BSL_ERR_PUSH_ERROR(ret); goto ERR; } /** * n > e * e cannot be 0 or 1; The mathematical logic of e and d is that * d and e are reciprocal in mod((p - 1) * (q - 1)); * When e is 1, both e and d must be 1. When e is 0, d does not exist. */ if (BN_Cmp(newPub->n, newPub->e) <= 0 || BN_IsZero(newPub->e) || BN_IsOne(newPub->e)) { ret = CRYPT_RSA_ERR_INPUT_VALUE; BSL_ERR_PUSH_ERROR(ret); goto ERR; } newPub->mont = BN_MontCreate(newPub->n); if (newPub->mont == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } RSA_FREE_PUB_KEY(ctx->pubKey); ctx->pubKey = newPub; return ret; ERR: RSA_FREE_PUB_KEY(newPub); return ret; } static int32_t GetPrvBasicCheck(const CRYPT_RSA_Ctx *ctx, const CRYPT_RsaPrv *prv) { // ctx\ctx->prvKey\prv is not empty. // prv->p\q and prv->dP\dQ\qInv are both null or non-null. // If prv->p is empty, prv->dP is empty. if (ctx == NULL || ctx->prvKey == NULL || prv == NULL || ((prv->p == NULL) != (prv->q == NULL)) || ((prv->dP == NULL || prv->dQ == NULL || prv->qInv == NULL) && (prv->dP || prv->dQ || prv->qInv)) || (prv->p == NULL && prv->dP != NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_GetPrvKey(const CRYPT_RSA_Ctx *ctx, CRYPT_RsaPrv *prv) { int32_t ret = GetPrvBasicCheck(ctx, prv); if (ret != CRYPT_SUCCESS) { return ret; } GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->n, prv->n, &prv->nLen), ret); GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->d, prv->d, &prv->dLen), ret); if (prv->e != NULL) { GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->e, prv->e, &prv->eLen), ret); } if (prv->p != NULL) { GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->p, prv->p, &prv->pLen), ret); GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->q, prv->q, &prv->qLen), ret); } if (prv->dQ != NULL) { GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->dQ, prv->dQ, &prv->dQLen), ret); GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->dP, prv->dP, &prv->dPLen), ret); GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->qInv, prv->qInv, &prv->qInvLen), ret); } return CRYPT_SUCCESS; ERR: if (prv->d != NULL && prv->dLen != 0) { BSL_SAL_CleanseData(prv->d, prv->dLen); } if (prv->p != NULL && prv->pLen != 0) { BSL_SAL_CleanseData(prv->p, prv->pLen); } if (prv->q != NULL && prv->qLen != 0) { BSL_SAL_CleanseData(prv->q, prv->qLen); } if (prv->dQ != NULL && prv->dQLen != 0) { BSL_SAL_CleanseData(prv->dQ, prv->dQLen); } if (prv->dP != NULL && prv->dPLen != 0) { BSL_SAL_CleanseData(prv->dP, prv->dPLen); } if (prv->qInv != NULL && prv->qInvLen != 0) { BSL_SAL_CleanseData(prv->qInv, prv->qInvLen); } return ret; } int32_t CRYPT_RSA_GetPubKey(const CRYPT_RSA_Ctx *ctx, CRYPT_RsaPub *pub) { if (ctx == NULL || ctx->pubKey == NULL || pub == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = BN_Bn2Bin(ctx->pubKey->e, pub->e, &pub->eLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_Bn2Bin(ctx->pubKey->n, pub->n, &pub->nLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } #ifdef HITLS_BSL_PARAMS int32_t CRYPT_RSA_SetPrvKeyEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_RsaPrv prv = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_RSA_N, &prv.n, &prv.nLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_D, &prv.d, &prv.dLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_E, &prv.e, &prv.eLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_P, &prv.p, &prv.pLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_Q, &prv.q, &prv.qLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_DP, &prv.dP, &prv.dPLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_DQ, &prv.dQ, &prv.dQLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_QINV, &prv.qInv, &prv.qInvLen); return CRYPT_RSA_SetPrvKey(ctx, &prv); } int32_t CRYPT_RSA_SetPubKeyEx(CRYPT_RSA_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_RsaPub pub = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_RSA_N, &pub.n, &pub.nLen); (void)GetConstParamValue(para, CRYPT_PARAM_RSA_E, &pub.e, &pub.eLen); return CRYPT_RSA_SetPubKey(ctx, &pub); } int32_t CRYPT_RSA_GetPrvKeyEx(const CRYPT_RSA_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_RsaPrv prv = {0}; BSL_Param *paramN = GetParamValue(para, CRYPT_PARAM_RSA_N, &prv.n, &prv.nLen); BSL_Param *paramD = GetParamValue(para, CRYPT_PARAM_RSA_D, &prv.d, &prv.dLen); BSL_Param *paramE = GetParamValue(para, CRYPT_PARAM_RSA_E, &prv.e, &prv.eLen); BSL_Param *paramP = GetParamValue(para, CRYPT_PARAM_RSA_P, &prv.p, &prv.pLen); BSL_Param *paramQ = GetParamValue(para, CRYPT_PARAM_RSA_Q, &prv.q, &prv.qLen); BSL_Param *paramDP = GetParamValue(para, CRYPT_PARAM_RSA_DP, &prv.dP, &prv.dPLen); BSL_Param *paramDQ = GetParamValue(para, CRYPT_PARAM_RSA_DQ, &prv.dQ, &prv.dQLen); BSL_Param *paramQInv = GetParamValue(para, CRYPT_PARAM_RSA_QINV, &prv.qInv, &prv.qInvLen); int32_t ret = CRYPT_RSA_GetPrvKey(ctx, &prv); if (ret != CRYPT_SUCCESS) { return ret; } paramN->useLen = prv.nLen; paramD->useLen = prv.dLen; if (paramE != NULL) { paramE->useLen = prv.eLen; } if (paramP != NULL) { paramP->useLen = prv.pLen; } if (paramQ != NULL) { paramQ->useLen = prv.qLen; } if (paramDP != NULL) { paramDP->useLen = prv.dPLen; } if (paramDQ != NULL) { paramDQ->useLen = prv.dQLen; } if (paramQInv != NULL) { paramQInv->useLen = prv.qInvLen; } return ret; } int32_t CRYPT_RSA_GetPubKeyEx(const CRYPT_RSA_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_RsaPub pub = {0}; BSL_Param *paramN = GetParamValue(para, CRYPT_PARAM_RSA_N, &pub.n, &pub.nLen); BSL_Param *paramE = GetParamValue(para, CRYPT_PARAM_RSA_E, &pub.e, &pub.eLen); int32_t ret = CRYPT_RSA_GetPubKey(ctx, &pub); if (ret != CRYPT_SUCCESS) { return ret; } paramN->useLen = pub.nLen; paramE->useLen = pub.eLen; return ret; } #endif int32_t CRYPT_RSA_Cmp(const CRYPT_RSA_Ctx *a, const CRYPT_RSA_Ctx *b) { RETURN_RET_IF(a == NULL || b == NULL, CRYPT_NULL_INPUT); RETURN_RET_IF(a->pubKey == NULL || b->pubKey == NULL, CRYPT_RSA_NO_KEY_INFO); RETURN_RET_IF(BN_Cmp(a->pubKey->n, b->pubKey->n) != 0 || BN_Cmp(a->pubKey->e, b->pubKey->e) != 0, CRYPT_RSA_PUBKEY_NOT_EQUAL); return CRYPT_SUCCESS; } int32_t CRYPT_RSA_GetSecBits(const CRYPT_RSA_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return 0; } int32_t bits = (int32_t)CRYPT_RSA_GetBits(ctx); return BN_SecBits(bits, -1); } #ifdef HITLS_CRYPTO_RSA_CHECK #define RSA_CHECK_PQ_RECOVER 1 // recover p q and check. #define RSA_CHECK_PQD_CHECK 2 // check prime p q #define RSA_CHECK_CRT_CHECK 3 // check crt // m = 2^t * r, m != 0. cal Max(t) and bn = m / 2^t. static void CalMaxT(BN_BigNum *bn, uint32_t *res) { uint32_t t = 0; while (BN_GetBit(bn, t) == false) { t++; } *res = t; (void)BN_Rshift(bn, bn, t); // bn will decrease, and the memory will definitely be sufficient return; } static int32_t BasicKeypairCheck(const CRYPT_RSA_PubKey *pubKey, const CRYPT_RSA_PrvKey *prvKey) { if (pubKey->n == NULL || pubKey->e == NULL) { return CRYPT_RSA_ERR_NO_PUBKEY_INFO; } // Currently, the check for p and q being null is not supported. if (prvKey->n == NULL || prvKey->d == NULL) { return CRYPT_RSA_ERR_NO_PRVKEY_INFO; } uint32_t eBits1 = BN_Bits(pubKey->e); // not check e == NULL repeatedly. uint32_t eBits2 = BN_Bits(prvKey->e); // prvKey->e can be empty, unless in crt mode // e <= 2^16 or e >= 2^256 -> e shoule be [17, 256]. if ((eBits2 != 0 && eBits2 != eBits1) || eBits1 < 17 || eBits1 > 256 || !BN_IsOdd(pubKey->e)) { return CRYPT_RSA_ERR_E_VALUE; } uint32_t nBbits = BN_Bits(pubKey->n); if (nBbits % 2 != 0) { // mod 2 to check nBits is a positive even integer or not. return CRYPT_RSA_ERR_KEY_BITS; } int32_t ret = BN_Cmp(pubKey->n, prvKey->n); // If n_pub != n_priv if (ret != 0) { // not equal return CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; } ret = BN_SecBits((int32_t)nBbits, -1); // no need to consider prvLen. /* SP800-56B requires that its should in the interval [112, 256] * Because the current rsa specification supports 1024 bits, so the lower limit is 80. */ if (ret < 80 || ret > 256) { return CRYPT_RSA_ERR_KEY_BITS; } return CRYPT_SUCCESS; } /* * if lower < p < upper, return success, otherwise return error. */ static int32_t RangeCheck(const BN_BigNum *lower, const BN_BigNum *p, const BN_BigNum *upper) { int32_t ret = BN_Cmp(lower, p); if (ret >= 0) { return CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; } ret = BN_Cmp(p, upper); if (ret >= 0) { return CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; } return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_SP800_STRICT_CHECK /* * if (p < √2)(2nBits/2−1)) or (p > 2nBits/2 – 1), return error. */ static int32_t FactorPQcheck(const BN_BigNum *e, const BN_BigNum *p, const BN_BigNum *n1, const BN_BigNum *n2, BN_Optimizer *opt) { int32_t ret = OptimizerStart(opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } BN_BigNum *pSqr = OptimizerGetBn(opt, p->size); BN_BigNum *tmp = OptimizerGetBn(opt, p->size); if (tmp == NULL || pSqr == NULL) { OptimizerEnd(opt); BSL_ERR_PUSH_ERROR(CRYPT_BN_OPTIMIZER_GET_FAIL); return CRYPT_BN_OPTIMIZER_GET_FAIL; } GOTO_ERR_IF(BN_Sqr(pSqr, p, opt), ret); if (BN_Cmp(pSqr, n1) < 0) { // check (p < (√2)(2^(nBits/2−1)) ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); goto ERR; } if (BN_Cmp(p, n2) > 0) { // check (p > (2^(nBits - 1))) ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(BN_SubLimb(tmp, p, 1), ret); GOTO_ERR_IF(BN_Gcd(tmp, e, tmp, opt), ret); // check gcd(p-1, e_pub) != 1 if (!BN_IsOne(tmp)) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); } ERR: OptimizerEnd(opt); return ret; } #endif static int32_t FactorPrimeCheck(const BN_BigNum *n, const BN_BigNum *e, const BN_BigNum *p, const BN_BigNum *q, BN_Optimizer *opt) { int32_t ret = OptimizerStart(opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t nBits = BN_Bits(n); uint32_t checkTimes = nBits < 1536 ? 5 : 4; // ref. FIPS 186-5, Table B.1 uint32_t needRoom = nBits / BN_UINT_BITS; BN_BigNum *tmp1 = OptimizerGetBn(opt, needRoom); BN_BigNum *tmp2 = OptimizerGetBn(opt, needRoom); if (tmp1 == NULL || tmp2 == NULL) { OptimizerEnd(opt); BSL_ERR_PUSH_ERROR(CRYPT_BN_OPTIMIZER_GET_FAIL); return CRYPT_BN_OPTIMIZER_GET_FAIL; } GOTO_ERR_IF(BN_Mul(tmp1, p, q, opt), ret); if (BN_Cmp(tmp1, n) != 0) { // if n_pub != p * q. ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE); goto ERR; } #ifdef HITLS_CRYPTO_SP800_STRICT_CHECK // get ((√2)(2^(nBits/2 - 1)))^2 (void)BN_SetLimb(tmp1, 1); GOTO_ERR_IF(BN_Lshift(tmp1, tmp1, nBits - 2), ret); // secLen can guarantee nBits > 2. GOTO_ERR_IF(BN_Add(tmp1, tmp1, tmp1), ret); // get 2^(nBits/2) - 1. (void)BN_SetLimb(tmp2, 1); GOTO_ERR_IF(BN_Lshift(tmp2, tmp2, nBits << 1), ret); GOTO_ERR_IF(BN_SubLimb(tmp2, tmp2, 1), ret); GOTO_ERR_IF(FactorPQcheck(e, p, tmp1, tmp2, opt), ret); GOTO_ERR_IF(FactorPQcheck(e, q, tmp1, tmp2, opt), ret); #else (void)e; #endif GOTO_ERR_IF(BN_Sub(tmp1, p, q), ret); (void)BN_SetSign(tmp1, false); // tmp1 = |p - q| (void)BN_SetLimb(tmp2, 1); GOTO_ERR_IF(BN_Lshift(tmp2, tmp2, (nBits >> 1) - 100), ret); // 2^(nBits/2 - 100) if (BN_Cmp(tmp1, tmp2) <= 0) { // check |p - q| <= (2^(nBits/2 - 100)) ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = BN_PrimeCheck(p, checkTimes, opt, NULL); if (ret != CRYPT_SUCCESS) { if (ret == CRYPT_BN_NOR_CHECK_PRIME) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); goto ERR; } } ret = BN_PrimeCheck(q, checkTimes, opt, NULL); if (ret != CRYPT_SUCCESS) { if (ret == CRYPT_BN_NOR_CHECK_PRIME) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); } } ERR: OptimizerEnd(opt); return ret; } static int32_t FactorDCheck(const BN_BigNum *n, const BN_BigNum *e, const BN_BigNum *p, const BN_BigNum *q, const BN_BigNum *d, BN_Optimizer *opt) { int32_t ret = OptimizerStart(opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t nBits = BN_Bits(n); uint32_t needRoom = nBits / BN_UINT_BITS; BN_BigNum *tmp0 = OptimizerGetBn(opt, needRoom); BN_BigNum *tmp1 = OptimizerGetBn(opt, needRoom); BN_BigNum *tmp2 = OptimizerGetBn(opt, needRoom); if (tmp0 == NULL || tmp1 == NULL || tmp2 == NULL) { OptimizerEnd(opt); BSL_ERR_PUSH_ERROR(CRYPT_BN_OPTIMIZER_GET_FAIL); return CRYPT_BN_OPTIMIZER_GET_FAIL; } // get 2^(nBits / 2) (void)BN_SetLimb(tmp0, 1); GOTO_ERR_IF(BN_Lshift(tmp0, tmp0, nBits >> 1), ret); GOTO_ERR_IF(BN_SubLimb(tmp1, p, 1), ret); GOTO_ERR_IF(BN_SubLimb(tmp2, q, 1), ret); // tmp1 = LCM(p – 1, q – 1) GOTO_ERR_IF(BN_Lcm(tmp1, tmp1, tmp2, opt), ret); #ifdef HITLS_CRYPTO_SP800_STRICT_CHECK // In the original RSA paper <A Method for Obtaining Digital Signatures and Public-Key Cryptosystems>, // the Euler totient function is φ(n) = (p – 1)(q – 1), hence d < LCM(p – 1, q – 1) will be incompatible. // check 2^(nBits / 2) < d < LCM(p – 1, q – 1). GOTO_ERR_IF(RangeCheck(tmp0, d, tmp1), ret); #endif GOTO_ERR_IF(BN_Mul(tmp0, e, d, opt), ret); GOTO_ERR_IF(BN_Mod(tmp2, tmp0, tmp1, opt), ret); // check. 1 = (d * epub) mod LCM(p – 1, q – 1). if (!BN_IsOne(tmp2)) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); } ERR: OptimizerEnd(opt); return ret; } /* * Recover prime factors p and q from n, e, and d. * ref SP800.56b Appendix C */ static int32_t RecoverPrimeFactorsAndCheck(const CRYPT_RSA_Ctx *pubKey, const CRYPT_RSA_Ctx *prvKey, BN_Optimizer *opt) { int ret = OptimizerStart(opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } bool flag = false; uint32_t tFactor; uint32_t nBits = BN_Bits(pubKey->pubKey->n); uint32_t needRoom = nBits / BN_UINT_BITS; BN_BigNum *g = OptimizerGetBn(opt, needRoom); BN_BigNum *x = OptimizerGetBn(opt, needRoom); BN_BigNum *y = OptimizerGetBn(opt, needRoom); BN_BigNum *nSubOne = OptimizerGetBn(opt, needRoom); BN_BigNum *p = OptimizerGetBn(opt, needRoom); BN_BigNum *q = OptimizerGetBn(opt, needRoom); BN_BigNum *r = OptimizerGetBn(opt, needRoom); if (g == NULL || x == NULL || y == NULL || nSubOne == NULL || p == NULL || q == NULL || r == NULL) { OptimizerEnd(opt); BSL_ERR_PUSH_ERROR(CRYPT_BN_OPTIMIZER_GET_FAIL); return CRYPT_BN_OPTIMIZER_GET_FAIL; } GOTO_ERR_IF(BN_SubLimb(nSubOne, pubKey->pubKey->n, 1), ret); // n - 1 // step 1: compute r = d * e - 1 GOTO_ERR_IF(BN_Mul(r, prvKey->prvKey->d, pubKey->pubKey->e, opt), ret); // d * e GOTO_ERR_IF(BN_SubLimb(r, r, 1), ret); // d * e - 1 if (BN_IsOdd(r)) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); goto ERR; } // step 2: find t and m = (2^t) * r, r is the largest odd integer. CalMaxT(r, &tFactor); // r = m / 2^t // step 3: find prime factors p and q. for (uint32_t i = 0; i < 100; i++) { // try 100 times GOTO_ERR_IF(BN_RandRangeEx(LIBCTX_FROM_RSA_CTX(pubKey), g, pubKey->pubKey->n), ret); // rand(0, n) GOTO_ERR_IF(BN_ModExp(y, g, r, pubKey->pubKey->n, opt), ret); // y = g ^ r % n if (BN_IsOne(y) == true || BN_Cmp(y, nSubOne) == 0) { // y == 1 or y == n - 1 continue; } for (uint32_t j = 1; j < tFactor; j++) { // 1 -> t - 1 GOTO_ERR_IF(BN_ModSqr(x, y, pubKey->pubKey->n, opt), ret); // y ^ 2 mod n if (BN_IsOne(x) == true) { flag = true; break; } if (BN_Cmp(x, nSubOne) == 0) { continue; } GOTO_ERR_IF(BN_Copy(y, x), ret); // update y. } GOTO_ERR_IF(BN_ModSqr(x, y, pubKey->pubKey->n, opt), ret); // y ^ 2 mod n if (BN_IsOne(x) == true) { flag = true; break; } } // step 4: check if flag is true. if (!flag) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(BN_SubLimb(y, y, 1), ret); // y - 1 // step 5: compute p = gcd(y, n) and q = n / p. GOTO_ERR_IF(BN_Gcd(p, y, pubKey->pubKey->n, opt), ret); // p = gcd(y, n) GOTO_ERR_IF(BN_Div(q, NULL, pubKey->pubKey->n, p, opt), ret); // q = n / p GOTO_ERR_IF(FactorPrimeCheck(pubKey->pubKey->n, pubKey->pubKey->e, p, q, opt), ret); GOTO_ERR_IF(FactorDCheck(pubKey->pubKey->n, pubKey->pubKey->e, p, q, prvKey->prvKey->d, opt), ret); ERR: OptimizerEnd(opt); return ret; } static int32_t FactorCRTCheck(const CRYPT_RSA_PrvKey *prvKey, BN_Optimizer *opt) { int32_t ret = OptimizerStart(opt); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t nBits = BN_Bits(prvKey->n); uint32_t needRoom = nBits / BN_UINT_BITS; BN_BigNum *pMinusOne = OptimizerGetBn(opt, needRoom); BN_BigNum *qMinusOne = OptimizerGetBn(opt, needRoom); BN_BigNum *one = OptimizerGetBn(opt, needRoom); if (pMinusOne == NULL || qMinusOne == NULL || one == NULL) { OptimizerEnd(opt); BSL_ERR_PUSH_ERROR(CRYPT_BN_OPTIMIZER_GET_FAIL); return CRYPT_BN_OPTIMIZER_GET_FAIL; } GOTO_ERR_IF(BN_SubLimb(pMinusOne, prvKey->p, 1), ret); // p - 1 GOTO_ERR_IF(BN_SubLimb(qMinusOne, prvKey->q, 1), ret); // q - 1 (void)BN_SetLimb(one, 1); GOTO_ERR_IF(RangeCheck(one, prvKey->dP, pMinusOne), ret); // 1 < dP < (p – 1). GOTO_ERR_IF(RangeCheck(one, prvKey->dQ, qMinusOne), ret); // 1 < dQ < (q – 1). GOTO_ERR_IF(RangeCheck(one, prvKey->qInv, prvKey->p), ret); // 1 < qInv < p. GOTO_ERR_IF(BN_ModMul(pMinusOne, prvKey->dP, prvKey->e, pMinusOne, opt), ret); // (dP * e) mod (p - 1) GOTO_ERR_IF(BN_ModMul(qMinusOne, prvKey->dQ, prvKey->e, qMinusOne, opt), ret); // (dQ * e) mod (q - 1) GOTO_ERR_IF(BN_ModMul(one, prvKey->qInv, prvKey->q, prvKey->p, opt), ret); // (qInv * q) mod p if (!BN_IsOne(pMinusOne) || !BN_IsOne(qMinusOne) || !BN_IsOne(one)) { ret = CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE; BSL_ERR_PUSH_ERROR(ret); } ERR: OptimizerEnd(opt); return ret; } static int32_t CheckLevel(const CRYPT_RSA_PrvKey *prvKey) { if (!BN_IsZero(prvKey->e) && !BN_IsZero(prvKey->dP) && !BN_IsZero(prvKey->dQ) && !BN_IsZero(prvKey->qInv) && !BN_IsZero(prvKey->p) && !BN_IsZero(prvKey->q)) { return RSA_CHECK_CRT_CHECK; // check crt. } if (!BN_IsZero(prvKey->p) && !BN_IsZero(prvKey->q)) { return RSA_CHECK_PQD_CHECK; // check prime p q. } return RSA_CHECK_PQ_RECOVER; // recover p q and check. } /* * ref. SP800-56B 6.4.3.1 RSA Key-Pair Validation (Random Public Exponent) */ static int32_t RsaKeyPairCheck(const CRYPT_RSA_Ctx *pubKey, const CRYPT_RSA_Ctx *prvKey) { if (pubKey == NULL || prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pubKey->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_PUBKEY_INFO); return CRYPT_RSA_ERR_NO_PUBKEY_INFO; } if (prvKey->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_PRVKEY_INFO); return CRYPT_RSA_ERR_NO_PRVKEY_INFO; } /* basic check */ int32_t ret = BasicKeypairCheck(pubKey->pubKey, prvKey->prvKey); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } BN_Optimizer *opt = BN_OptimizerCreate(); if (opt == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } switch (CheckLevel(prvKey->prvKey)) { case RSA_CHECK_PQ_RECOVER: ret = RecoverPrimeFactorsAndCheck(pubKey, prvKey, opt); break; case RSA_CHECK_PQD_CHECK: /* prime p q check */ ret = FactorPrimeCheck(pubKey->pubKey->n, pubKey->pubKey->e, prvKey->prvKey->p, prvKey->prvKey->q, opt); if (ret != CRYPT_SUCCESS) { goto ERR; } /* factor d check */ ret = FactorDCheck(pubKey->pubKey->n, pubKey->pubKey->e, prvKey->prvKey->p, prvKey->prvKey->q, prvKey->prvKey->d, opt); break; default: /* prime p q check */ ret = FactorPrimeCheck(pubKey->pubKey->n, pubKey->pubKey->e, prvKey->prvKey->p, prvKey->prvKey->q, opt); if (ret != CRYPT_SUCCESS) { goto ERR; } /* factor d check */ ret = FactorDCheck(pubKey->pubKey->n, pubKey->pubKey->e, prvKey->prvKey->p, prvKey->prvKey->q, prvKey->prvKey->d, opt); if (ret != CRYPT_SUCCESS) { goto ERR; } ret = FactorCRTCheck(prvKey->prvKey, opt); /* factor crt check */ break; } ERR: BN_OptimizerDestroy(opt); return ret; } static int32_t RsaPrvKeyCheck(const CRYPT_RSA_Ctx *pkey) { if (pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pkey->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_PRVKEY_INFO); return CRYPT_RSA_ERR_NO_PRVKEY_INFO; } if (pkey->prvKey->n == NULL || pkey->prvKey->d == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_NO_PRVKEY_INFO); return CRYPT_RSA_ERR_NO_PRVKEY_INFO; } if (BN_IsZero(pkey->prvKey->n) || BN_IsZero(pkey->prvKey->d)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INVALID_PRVKEY); return CRYPT_RSA_ERR_INVALID_PRVKEY; } if (BN_Cmp(pkey->prvKey->n, pkey->prvKey->d) <= 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INVALID_PRVKEY); return CRYPT_RSA_ERR_INVALID_PRVKEY; } return CRYPT_SUCCESS; } int32_t CRYPT_RSA_Check(uint32_t checkType, const CRYPT_RSA_Ctx *pkey1, const CRYPT_RSA_Ctx *pkey2) { switch (checkType) { case CRYPT_PKEY_CHECK_KEYPAIR: return RsaKeyPairCheck(pkey1, pkey2); case CRYPT_PKEY_CHECK_PRVKEY: return RsaPrvKeyCheck(pkey1); default: BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } } #endif // HITLS_CRYPTO_RSA_CHECK #endif // HITLS_CRYPTO_RSA
2301_79861745/bench_create
crypto/rsa/src/rsa_keyop.c
C
unknown
31,572
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef RSA_LOCAL_H #define RSA_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include "crypt_rsa.h" #include "crypt_bn.h" #include "crypt_local_types.h" #include "crypt_types.h" #include "sal_atomic.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define HASH_MAX_MDSIZE (64) #define PARAMISNULL(a) ((a) == NULL || (a)->value == NULL) typedef struct RSA_BlindSt { BN_BigNum *r; BN_BigNum *rInv; } RSA_Blind; typedef struct { BN_BigNum *n; // pub key n needed for no padding BN_BigNum *d; // private key d needed for asn encoding BN_BigNum *p; // prime factor p BN_BigNum *q; // prime factor q BN_BigNum *dP; // exponent dP for CRT BN_BigNum *dQ; // exponent dQ for CRT BN_BigNum *qInv; // CRT coefficient qInv BN_BigNum *e; // public key e } CRYPT_RSA_PrvKey; typedef struct { BN_BigNum *n; // modulo Value - converted.Not in char BN_BigNum *e; // Exponent Value -converted.Not in char // Montgomery pre-calculation cache BN_Mont *mont; } CRYPT_RSA_PubKey; #ifdef HITLS_CRYPTO_ACVP_TESTS typedef struct { BN_BigNum *xp; // main seed for prime p BN_BigNum *xp1; // auxiliary seed1 for prime p BN_BigNum *xp2; // auxiliary seed2 for prime p BN_BigNum *xq; // main seed for prime q BN_BigNum *xq1; // auxiliary seed1 for prime q BN_BigNum *xq2; // auxiliary seed2 for prime q } RSA_FIPS_AUX_PRIME_SEEDS; typedef struct { union { RSA_FIPS_AUX_PRIME_SEEDS fipsPrimeSeeds; } primeSeed; } RSA_ACVP_TESTS; #endif struct RSA_Para { BN_BigNum *e; // Exponent Value -converted.Not in char uint32_t bits; // length in bits of modulus BN_BigNum *p; // prime factor p BN_BigNum *q; // prime factor q #ifdef HITLS_CRYPTO_ACVP_TESTS RSA_ACVP_TESTS acvpTests; #endif }; #ifdef HITLS_CRYPTO_RSA_BSSA typedef enum { RSABSSA = 1, /**< RSA Blind Signature with Appendix, ref RFC9474 */ } RSA_BlindType; typedef struct { RSA_BlindType type; /**< padding id */ union { RSA_Blind *bssa; } para; } RSA_BlindParam; #endif /** * @ingroup crypt_eal_pkey * * (For internal use)Set the padding mode of the RSA. The value 0 indicates that the padding mode is not set. */ typedef enum { EMSA_PKCSV15 = 1, /**< PKCS1-v1_5 complies with RFC8017 */ EMSA_PSS, /**< PSS complies with RFC8017 */ RSAES_OAEP, /**< OAEP complies with RFC8017 */ RSAES_PKCSV15, /**< RSAES_PKCSV15 complies with RFC8017 */ RSA_NO_PAD, RSAES_PKCSV15_TLS, /* Specific RSA pkcs1.5 padding verification process to prevent possible Bleichenbacher attacks */ } RSA_PadType; /** * @ingroup crypt_types * * Pkcsv15 padding mode, when RSA is used for signature. */ typedef struct { CRYPT_MD_AlgId mdId; /**< ID of the hash algorithm during pkcsv15 padding */ } RSA_PkcsV15Para; typedef struct { RSA_PadType type; /**< padding id */ union { RSA_PkcsV15Para pkcsv15; /**< pkcsv15 padding mode */ RSA_PadingPara pss; /**< pss padding mode */ RSA_PadingPara oaep; /**< oaep padding mode */ } para; /**< padding mode combination, including pss and pkcsv15 */ CRYPT_Data salt; // Used for the KAT test. } RSAPad; struct RSA_Ctx { CRYPT_RSA_PrvKey *prvKey; CRYPT_RSA_PubKey *pubKey; CRYPT_RSA_Para *para; #ifdef HITLS_CRYPTO_RSA_BLINDING RSA_Blind *scBlind; // Preventing side channel attacks #endif RSAPad pad; uint32_t flags; CRYPT_Data label; // Used for oaep padding BSL_SAL_RefCount references; #ifdef HITLS_CRYPTO_RSA_BSSA RSA_BlindParam *blindParam; #endif void *libCtx; char *mdAttr; }; #define LIBCTX_FROM_RSA_CTX(ctx) ((ctx) == NULL ? NULL : (ctx)->libCtx) #define MDATTR_FROM_RSA_CTX(ctx) ((ctx) == NULL ? NULL : (ctx)->mdAttr) CRYPT_RSA_PrvKey *RSA_NewPrvKey(uint32_t bits); CRYPT_RSA_PubKey *RSA_NewPubKey(uint32_t bits); void RSA_FreePrvKey(CRYPT_RSA_PrvKey *prvKey); void RSA_FreePubKey(CRYPT_RSA_PubKey *pubKey); int32_t RSA_CalcPrvKey(const CRYPT_RSA_Para *para, CRYPT_RSA_Ctx *ctx, BN_Optimizer *optimizer); void ShallowCopyCtx(CRYPT_RSA_Ctx *ctx, CRYPT_RSA_Ctx *newCtx); CRYPT_RSA_Para *CRYPT_RSA_DupPara(const CRYPT_RSA_Para *para); #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 int32_t CRYPT_RSA_UnPackPkcsV15Type1(uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); #endif #if defined(HITLS_CRYPTO_RSA_BLINDING) || defined(HITLS_CRYPTO_RSA_BSSA) /** * @ingroup rsa * @brief Create a blinding handle. * * @retval Return the blinding handle. */ RSA_Blind *RSA_BlindNewCtx(void); /** * @ingroup rsa * @brief Release the blinding handle. * * @param b [IN] blinding Handle. b is set NULL by the invoker. * * @retval none */ void RSA_BlindFreeCtx(RSA_Blind *b); /** * @ingroup rsa * @brief Multiply n by blinding factor A * * @param b [IN] Blinding Handle * @param data [IN] Input data * @param n [IN] n in the public key (n, e) * @param opt [IN] BigNum optimizer * * @retval Return the error code. */ int32_t RSA_BlindCovert(RSA_Blind *b, BN_BigNum *data, BN_BigNum *n, BN_Optimizer *opt); /** * @ingroup rsa * @brief Multiply n by the reverse blinding factor Ai * * @param b [IN] Blinding Handle * @param data [IN] Input data * @param n [IN] n in the public key (n, e) * @param opt [IN] BigNum optimizer * * @retval Return the error code. */ int32_t RSA_BlindInvert(RSA_Blind *b, BN_BigNum *data, BN_BigNum *n, BN_Optimizer *opt); /** * @ingroup rsa * @brief Create a new Blind parameter with the parameters e and m, * e in the public key (n, e), n in the public key (n, e) * * @param libCtx [IN] libctx * @param b [IN] Blinding Handle * @param e [IN] e in the public key (n, e) * @param n [IN] n in the public key (n, e) * @param bits [IN] bits of n * * @retval Return the error code. */ int32_t RSA_BlindCreateParam(void *libCtx, RSA_Blind *b, BN_BigNum *e, BN_BigNum *n, uint32_t bits, BN_Optimizer *opt); int32_t RSA_CreateBlind(RSA_Blind *b, uint32_t bits); #endif #define RSA_FREE_PRV_KEY(prvKey_) \ do { \ RSA_FreePrvKey((prvKey_)); \ (prvKey_) = NULL; \ } while (0) #define RSA_FREE_PUB_KEY(pubKey_) \ do { \ RSA_FreePubKey((pubKey_)); \ (pubKey_) = NULL; \ } while (0) #define RSA_FREE_PARA(para_) \ do { \ CRYPT_RSA_FreePara((para_)); \ (para_) = NULL; \ } while (0) #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_RSA #endif
2301_79861745/bench_create
crypto/rsa/src/rsa_local.h
C
unknown
7,366
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_RSA #include "crypt_rsa.h" #include "rsa_local.h" #include "crypt_errno.h" #include "crypt_types.h" #include "crypt_utils.h" #include "crypt_util_rand.h" #include "securec.h" #include "bsl_sal.h" #include "bsl_bytes.h" #include "bsl_err_internal.h" #define UINT32_SIZE 4 #ifdef HITLS_CRYPTO_RSA_EMSA_PSS // maskedDB: [in] maskDB from MGF // [out] maskedDB = DB xor maskDB // DB: PS || 0x01 || salt; // msBit: indicates the number of valid bits in the most significant bytes of the EM, // value 0 indicates that all bits are valid. static void MaskDB(uint8_t *maskedDB, uint32_t len, const uint8_t *salt, uint32_t saltLen, uint32_t msBit) { uint8_t *tmp = maskedDB + (len - saltLen) - 1; // init point to pos of 0x01 *tmp ^= 0x01; tmp++; uint32_t i; for (i = 0; i < saltLen; i++) { tmp[i] ^= salt[i]; } if (msBit != 0) { // Set the leftmost 8emLen - emBits bits of the leftmost octet in maskedDB to zero maskedDB[0] &= ((uint8_t)(0xFF >> (8 - msBit))); } } static int32_t PssEncodeLengthCheck(uint32_t modBits, uint32_t hLen, uint32_t saltLen, uint32_t dataLen, uint32_t padLen) { if (modBits < RSA_MIN_MODULUS_BITS || modBits > RSA_MAX_MODULUS_BITS) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_KEY_BITS); return CRYPT_RSA_ERR_KEY_BITS; } if (hLen > RSA_MAX_MODULUS_LEN || dataLen != hLen) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } uint32_t keyBytes = BN_BITS_TO_BYTES(modBits); if (keyBytes != padLen) { // The length required for padding does not match the key module length (API convention). BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } if (saltLen == (uint32_t)CRYPT_RSA_SALTLEN_TYPE_AUTOLEN) { return CRYPT_SUCCESS; } if (saltLen > RSA_MAX_MODULUS_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PSS_SALT_LEN); return CRYPT_RSA_ERR_PSS_SALT_LEN; } uint32_t emLen = keyBytes; // the octet length of EM will be one less than k if modBits - 1 is divisible by 8 and equal to k otherwise if (((modBits - 1) & 0x7) == 0) { emLen--; } if (emLen < hLen + saltLen + 2) { // RFC: If emLen < hLen + sLen + 2, output "encoding error" and stop. BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PSS_SALT_LEN); return CRYPT_RSA_ERR_PSS_SALT_LEN; } return CRYPT_SUCCESS; } #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_BSSA) static int32_t GenPssSalt(void *libCtx, CRYPT_Data *salt, const EAL_MdMethod *mdMethod, int32_t saltLen, uint32_t padBuffLen) { uint32_t hashLen = mdMethod->mdSize; if (saltLen == CRYPT_RSA_SALTLEN_TYPE_HASHLEN) { // saltLen is -1 salt->len = hashLen; } else if (saltLen == CRYPT_RSA_SALTLEN_TYPE_MAXLEN || saltLen == CRYPT_RSA_SALTLEN_TYPE_AUTOLEN) { // saltLen is -2 or -3 salt->len = padBuffLen - hashLen - 2; // salt, obtains from the DRBG } else { salt->len = (uint32_t)saltLen; } salt->data = BSL_SAL_Malloc(salt->len); if (salt->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } // Obtain the salt through the public random number. int32_t ret = CRYPT_RandEx(libCtx, salt->data, salt->len); if (ret != CRYPT_SUCCESS) { BSL_SAL_FREE(salt->data); BSL_ERR_PUSH_ERROR(ret); } return ret; } /** * EMSA-PSS Encoding Operation * +-----------+ * | M | * +-----------+ * | * V * Hash * | * V * +--------+----------+----------+ * M' = |Padding1| mHash | salt | * +--------+----------+----------+ * | * +--------+----------+ V * DB = |Padding2| salt | Hash * +--------+----------+ | * | | * V | * xor <--- MGF <---| maskDB = MGF(H, emLen - hLen - 1). * | | * | | * V V * +-------------------+----------+--+ * EM = | maskedDB | H |bc| * +-------------------+----------+--+ * Output EM data with a fixed length (keyBytes) to the pad buffer. * Add 0s to the first byte, if the EM length + 1 = keyBytes. * Of which: * The data is the mHash in the preceding figure. * M' = (0x)00 00 00 00 00 00 00 00 || mHash || salt * DB = PS || 0x01 || salt; DB is an octet string of length emLen - hLen - 1 * PS consisting of emLen - sLen - hLen - 2 zero octets, The length of PS may be 0. */ int32_t CRYPT_RSA_SetPss(CRYPT_RSA_Ctx *ctx, const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t saltLen, const uint8_t *data, uint32_t dataLen, uint8_t *pad, uint32_t padLen) { int32_t ret; if (ctx == NULL || hashMethod == NULL || mgfMethod == NULL || pad == NULL || data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_Data salt = {0}; bool kat = false; // mark if (ctx->pad.salt.data != NULL) { kat = true; salt.data = ctx->pad.salt.data; salt.len = ctx->pad.salt.len; ctx->pad.salt.data = NULL; ctx->pad.salt.len = 0; } else if (saltLen != 0) { // Generate a salt information to the salt. ret = GenPssSalt(LIBCTX_FROM_RSA_CTX(ctx), &salt, hashMethod, (int32_t)saltLen, padLen); RETURN_RET_IF((ret != CRYPT_SUCCESS), CRYPT_RSA_ERR_GEN_SALT); } RETURN_RET_IF((salt.data == NULL && salt.len != 0), CRYPT_RSA_ERR_PSS_SALT_DATA); uint32_t keyBits = CRYPT_RSA_GetBits(ctx); uint32_t hLen = hashMethod->mdSize; ret = PssEncodeLengthCheck(keyBits, hLen, salt.len, dataLen, padLen); if (ret != CRYPT_SUCCESS) { if ((kat != true) && (saltLen != 0)) { BSL_SAL_ClearFree(salt.data, salt.len); } return ret; } uint8_t *em = pad; uint32_t emLen = BN_BITS_TO_BYTES(keyBits); // the octet length of EM will be one less than k if modBits - 1 is divisible by 8 and equal to k otherwise uint32_t msBit = ((keyBits - 1) & 0x7); if (msBit == 0) { emLen--; *em = 0; em++; } em[emLen - 1] = 0xbc; // EM = maskedDB || H || 0xbc. // set H static const uint8_t zeros8[8] = {0}; const CRYPT_ConstData hashData[] = {{zeros8, sizeof(zeros8)}, {data, dataLen}, {salt.data, salt.len}}; const uint32_t maskedDBLen = emLen - hLen - 1; uint8_t *h = em + maskedDBLen; GOTO_ERR_IF(CRYPT_CalcHash(ctx->pad.para.pss.mdProvCtx, hashMethod, hashData, sizeof(hashData) / sizeof(hashData[0]), h, &hLen), ret); // set maskedDB GOTO_ERR_IF(CRYPT_Mgf1(ctx->pad.para.pss.mgfProvCtx, mgfMethod, h, hLen, em, maskedDBLen), ret); MaskDB(em, maskedDBLen, salt.data, salt.len, msBit); ERR: if ((kat != true) && (saltLen != 0)) { BSL_SAL_ClearFree(salt.data, salt.len); } return ret; } #endif // HITLS_CRYPTO_RSA_SIGN || HITLS_CRYPTO_RSA_BSSA #ifdef HITLS_CRYPTO_RSA_VERIFY static int32_t GetVerifySaltLen(const uint8_t *emData, const uint8_t *dbBuff, uint32_t maskedDBLen, uint32_t msBit, uint32_t *saltLen) { uint32_t i = 0; uint8_t *tmpBuff = (uint8_t *)BSL_SAL_Malloc(maskedDBLen); if (tmpBuff == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } (void)memcpy_s(tmpBuff, maskedDBLen, dbBuff, maskedDBLen); if (msBit != 0) { tmpBuff[0] &= ((uint8_t)(0xFF >> (8 - msBit))); // Set the leftmost 8emLen - emBits bits to zero } for (i = 0; i < maskedDBLen; i++) { tmpBuff[i] ^= emData[i]; if (tmpBuff[i] != 0) { break; } } if (i == maskedDBLen || tmpBuff[i] != 0x01) { BSL_SAL_FREE(tmpBuff); BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PSS_SALT_LEN); return CRYPT_RSA_ERR_PSS_SALT_LEN; } i++; BSL_SAL_FREE(tmpBuff); *saltLen = maskedDBLen - i; return CRYPT_SUCCESS; } static int32_t GetAndVerifyDB(void *provCtx, const EAL_MdMethod *mgfMethod, const CRYPT_Data *emData, const CRYPT_Data *dbBuff, uint32_t *saltLen, uint32_t msBit) { uint32_t maskedDBLen = dbBuff->len; uint32_t hLen = emData->len - maskedDBLen - 1; uint32_t tmpSaltLen = *saltLen; const uint8_t *h = emData->data + maskedDBLen; int32_t ret = CRYPT_Mgf1(provCtx, mgfMethod, h, hLen, dbBuff->data, dbBuff->len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (tmpSaltLen == (uint32_t)CRYPT_RSA_SALTLEN_TYPE_AUTOLEN) { ret = GetVerifySaltLen(emData->data, dbBuff->data, maskedDBLen, msBit, &tmpSaltLen); if (ret != CRYPT_SUCCESS) { return ret; } } // A ^ B == C => A ^ C == B MaskDB(dbBuff->data, dbBuff->len, h - tmpSaltLen, tmpSaltLen, msBit); if (memcmp(dbBuff->data, emData->data, maskedDBLen - tmpSaltLen) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } *saltLen = tmpSaltLen; return CRYPT_SUCCESS; } static int32_t VerifyH(void *provCtx, const EAL_MdMethod *hashMethod, const CRYPT_Data *mHash, const CRYPT_Data *salt, const CRYPT_Data *h, const CRYPT_Data *hBuff) { static const uint8_t zeros8[8] = {0}; const CRYPT_ConstData hashData[] = { {zeros8, sizeof(zeros8)}, {mHash->data, mHash->len}, {salt->data, salt->len} }; uint32_t hLen = hBuff->len; int32_t ret = CRYPT_CalcHash(provCtx, hashMethod, hashData, sizeof(hashData) / sizeof(hashData[0]), hBuff->data, &hLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (memcmp(h->data, hBuff->data, hLen) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } return CRYPT_SUCCESS; } // Reverse verification process of EMSA-PSS Encoding Operation: // MGF(H,maskedDBLen) ^ MaskedDB => DB' (PS||0x01||salt'), H' = Hash(padding1 || mHash || salt') == H ? int32_t CRYPT_RSA_VerifyPss(CRYPT_RSA_Ctx *ctx, const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t saltLen, const uint8_t *data, uint32_t dataLen, const uint8_t *pad, uint32_t padLen) { if (ctx == NULL || hashMethod == NULL || mgfMethod == NULL || pad == NULL || data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; uint32_t keyBits = CRYPT_RSA_GetBits(ctx); uint32_t hLen = hashMethod->mdSize; uint32_t saltLength = saltLen; if (saltLength == (uint32_t)CRYPT_RSA_SALTLEN_TYPE_HASHLEN) { // saltLength is -1 saltLength = (uint32_t)ctx->pad.para.pss.mdMeth.mdSize; } else if (saltLength == (uint32_t)CRYPT_RSA_SALTLEN_TYPE_MAXLEN) { // saltLength is -2 saltLength = (uint32_t)(padLen - ctx->pad.para.pss.mdMeth.mdSize - 2); // salt, obtains DRBG } RETURN_RET_IF_ERR(PssEncodeLengthCheck(keyBits, hLen, saltLength, dataLen, padLen), ret); // EM = maskedDB || H || 0xbc RETURN_RET_IF((pad[padLen - 1] != 0xbc), CRYPT_RSA_NOR_VERIFY_FAIL); const uint8_t *em = pad; uint32_t emLen = BN_BITS_TO_BYTES(keyBits); // the octet length of EM will be one less than k if modBits - 1 is divisible by 8 and equal to k otherwise uint32_t msBit = ((keyBits - 1) & 0x7); if (msBit == 0) { emLen--; em++; } // if msBit == 0, 8emLen == emBits, pad[0] should be 0 // the leftmost 8emLen - emBits bits of the leftmost octet in maskedDB should be 0 RETURN_RET_IF(((pad[0] >> msBit) != 0), CRYPT_RSA_NOR_VERIFY_FAIL); uint8_t *tmpBuff = BSL_SAL_Malloc(emLen); // for maskDB' / DB' and H' RETURN_RET_IF((tmpBuff == NULL), CRYPT_MEM_ALLOC_FAIL); const uint32_t maskedDBLen = emLen - hLen - 1; const CRYPT_Data dbBuff = {tmpBuff, maskedDBLen}; const CRYPT_Data emData = {(uint8_t *)(uintptr_t)em, emLen}; const CRYPT_Data mHash = {(uint8_t *)(uintptr_t)data, dataLen}; const CRYPT_Data h = {(uint8_t *)(uintptr_t)&em[maskedDBLen], hLen}; const CRYPT_Data hBuff = {&tmpBuff[maskedDBLen], hLen}; ret = GetAndVerifyDB(ctx->pad.para.pss.mgfProvCtx, mgfMethod, &emData, &dbBuff, &saltLength, msBit); if (ret != CRYPT_SUCCESS) { (void)memset_s(tmpBuff, emLen, 0, emLen); BSL_SAL_FREE(tmpBuff); BSL_ERR_PUSH_ERROR(ret); return ret; } const CRYPT_Data salt = {&tmpBuff[maskedDBLen - saltLength], saltLength}; ret = VerifyH(ctx->pad.para.pss.mdProvCtx, hashMethod, &mHash, &salt, &h, &hBuff); (void)memset_s(tmpBuff, emLen, 0, emLen); BSL_SAL_FREE(tmpBuff); return ret; } #endif // HITLS_CRYPTO_RSA_VERIFY #endif // HITLS_CRYPTO_RSA_EMSA_PSS #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 static int32_t PkcsSetLengthCheck(uint32_t emLen, uint32_t hashLen, uint32_t algIdentLen) { if (emLen > RSA_MAX_MODULUS_LEN || hashLen > RSA_MAX_MODULUS_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } if (hashLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } /* The length of the pad must exceed 11 bytes at least. tLen = hashLen + algIdentLen */ if (emLen < hashLen + algIdentLen + 11) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } static int32_t PkcsGetIdentifier(CRYPT_MD_AlgId hashId, CRYPT_Data *algIdentifier) { static uint8_t sha1TInfo[] = {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14}; static uint8_t sha224TInfo[] = {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c}; static uint8_t sha256TInfo[] = {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20}; static uint8_t sha384TInfo[] = {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30}; static uint8_t sha512TInfo[] = {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40}; static uint8_t md5TInfo[] = {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10}; static uint8_t sm3TInfo[] = {0x30, 0x30, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x81, 0x1c, 0xcf, 0x55, 0x01, 0x83, 0x11, 0x05, 0x00, 0x04, 0x20}; algIdentifier->data = NULL; algIdentifier->len = 0; if (hashId == CRYPT_MD_SHA1) { algIdentifier->data = (uint8_t *)sha1TInfo; algIdentifier->len = sizeof(sha1TInfo); } else if (hashId == CRYPT_MD_SHA224) { algIdentifier->data = (uint8_t *)sha224TInfo; algIdentifier->len = sizeof(sha224TInfo); } else if (hashId == CRYPT_MD_SHA256) { algIdentifier->data = (uint8_t *)sha256TInfo; algIdentifier->len = sizeof(sha256TInfo); } else if (hashId == CRYPT_MD_SHA384) { algIdentifier->data = (uint8_t *)sha384TInfo; algIdentifier->len = sizeof(sha384TInfo); } else if (hashId == CRYPT_MD_SHA512) { algIdentifier->data = (uint8_t *)sha512TInfo; algIdentifier->len = sizeof(sha512TInfo); } else if (hashId == CRYPT_MD_MD5) { algIdentifier->data = (uint8_t *)md5TInfo; algIdentifier->len = sizeof(md5TInfo); } else if (hashId == CRYPT_MD_SM3) { algIdentifier->data = (uint8_t *)sm3TInfo; algIdentifier->len = sizeof(sm3TInfo); } else { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_MD_ALGID); return CRYPT_RSA_ERR_MD_ALGID; } return CRYPT_SUCCESS; } // Pad output format:EM = 00 || 01 || PS || 00 || T; where T = algIdentifier || hash(M); // hash(M) is the input parameter data of this function. int32_t CRYPT_RSA_SetPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *data, uint32_t dataLen, uint8_t *pad, uint32_t padLen) { int32_t ret; uint32_t padSize; uint8_t *tmp = pad; uint32_t tmpLen = padLen; if (pad == NULL || data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_Data algIdentifier = {NULL, 0}; ret = PkcsGetIdentifier(hashId, &algIdentifier); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = PkcsSetLengthCheck(padLen, dataLen, algIdentifier.len); if (ret != CRYPT_SUCCESS) { return ret; } // Considering that the data space and pad space may overlap, // move the data to the specified position(the end of the pad). if (memmove_s(pad + (padLen - dataLen), dataLen, data, dataLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } *tmp = 0x0; tmp++; *tmp = 0x1; tmp++; tmpLen -= 2; // Skip the first 2 bytes. // PS length: padSize = padLen - dataLen - algIdentifier.len - 3 padSize = padLen - dataLen - algIdentifier.len - 3; if (memset_s(tmp, tmpLen, 0xff, padSize) != EOK) { // 0xff padded in PS BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } tmp += padSize; tmpLen -= padSize; *tmp = 0x0; tmp++; tmpLen--; if ((algIdentifier.len > 0) && memcpy_s(tmp, tmpLen, algIdentifier.data, algIdentifier.len) != EOK) { // padding when identifier exit BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_RSA_VERIFY int32_t CRYPT_RSA_VerifyPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *pad, uint32_t padLen, const uint8_t *data, uint32_t dataLen) { if (pad == NULL || data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (padLen == 0 || dataLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } uint8_t *padBuff = BSL_SAL_Malloc(padLen); if (padBuff == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret = CRYPT_RSA_SetPkcsV15Type1(hashId, data, dataLen, padBuff, padLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_FREE(padBuff); return ret; } if (memcmp(pad, padBuff, padLen) != 0) { BSL_SAL_FREE(padBuff); BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } BSL_SAL_FREE(padBuff); return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_RSA_VERIFY int32_t CRYPT_RSA_UnPackPkcsV15Type1(uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen) { uint8_t *index = data; uint32_t tmpLen = dataLen; // Format of the data to be decrypted is EB = 00 || 01 || PS || 00 || T. // The PS padding is at least 8. Therefore, the length of the data to be decrypted is at least 11. if (dataLen < 11) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } if (*index != 0x0 || *(index + 1) != 0x01) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } index += 2; // Skip first 2 bytes. tmpLen -= 2; // Skip first 2 bytes. uint32_t padNum = 0; while (*index == 0xff) { index++; tmpLen--; padNum++; } if (padNum < 8) { // The PS padding is at least 8. BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_PAD_NUM); return CRYPT_RSA_ERR_PAD_NUM; } if (tmpLen == 0 || *index != 0x0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } index++; tmpLen--; if (memcpy_s(out, *outLen, index, tmpLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } *outLen = tmpLen; return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_RSA_EMSA_PKCSV15 #ifdef HITLS_CRYPTO_RSAES_OAEP #ifdef HITLS_CRYPTO_RSA_ENCRYPT static int32_t OaepSetLengthCheck(uint32_t outLen, uint32_t inLen, uint32_t hashLen) { if (outLen > RSA_MAX_MODULUS_LEN || inLen > RSA_MAX_MODULUS_LEN || hashLen > HASH_MAX_MDSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } if (outLen == 0 || hashLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } // If mLen > k - 2hLen - 2, output "message too long" and stop. if (inLen + 2 * hashLen + 2 > outLen) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_ENC_BITS); return CRYPT_RSA_ERR_ENC_BITS; } return CRYPT_SUCCESS; } static int32_t OaepSetPs(const uint8_t *in, uint32_t inLen, uint8_t *db, uint32_t padLen, uint32_t hashLen) { uint8_t *ps = db + hashLen; // Generate a padding string PS consisting of k - mLen - 2hLen - 2 zero octets. The length of PS may be zero // This operation cannot be reversed because the OaepSetLengthCheck has checked the validity of the data. uint32_t psLen = padLen - inLen - 2 * hashLen - 2; // padding 0x00 (void)memset_s(ps, psLen, 0, psLen); ps += psLen; *ps = 0x01; ps++; /** * padLen minus twice hashLen, then subtract 2 bytes of fixed data, and subtract the padding length. * The remaining length is the plaintext length. */ if (inLen != 0 && memcpy_s(ps, padLen - 2 * hashLen - 2 - psLen, in, inLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } return CRYPT_SUCCESS; } static int32_t OaepSetMaskedDB(void *provCtx, const EAL_MdMethod *mgfMethod, uint8_t *db, uint8_t *seed, uint32_t padLen, uint32_t hashLen) { int32_t ret; uint32_t i; uint32_t maskedDBLen = padLen - hashLen - 1; uint8_t *maskedDB = (uint8_t *)BSL_SAL_Malloc(maskedDBLen); if (maskedDB == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = CRYPT_Mgf1(provCtx, mgfMethod, seed, hashLen, maskedDB, maskedDBLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } for (i = 0; i < maskedDBLen; i++) { db[i] ^= maskedDB[i]; } EXIT: BSL_SAL_CleanseData(maskedDB, maskedDBLen); BSL_SAL_FREE(maskedDB); return ret; } static int32_t OaepSetSeedMask(void *provCtx, const EAL_MdMethod *mgfMethod, uint8_t *db, uint8_t *seed, uint32_t padLen, uint32_t hashLen) { uint32_t i; int32_t ret; uint8_t seedmask[HASH_MAX_MDSIZE]; uint32_t maskedDBLen = padLen - hashLen - 1; ret = CRYPT_Mgf1(provCtx, mgfMethod, db, maskedDBLen, seedmask, hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } for (i = 0; i < hashLen; i++) { seed[i] ^= seedmask[i]; } EXIT: BSL_SAL_CleanseData(seedmask, hashLen); return ret; } /** * _________________________________________________________________ * * +----------+------+--+-------+ * DB = | lHash | PS |01| M | * +----------+------+--+-------+ * | * +----------+ | * | seed | | * +----------+ | * | | * |-------> MGF ---> xor * | | * +--+ V | * |00| xor <----- MGF <-----| * +--+ | | * | | | * V V V * +--+----------+----------------------------+ * EM = |00|maskedSeed| maskedDB | * +--+----------+----------------------------+ * _________________________________________________________________ * * Figure 1: EME-OAEP Encoding Operation <rfc8017> */ int32_t CRYPT_RSA_SetPkcs1Oaep(CRYPT_RSA_Ctx *ctx, const uint8_t *in, uint32_t inLen, uint8_t *pad, uint32_t padLen) { int32_t ret; const EAL_MdMethod *hashMethod = &ctx->pad.para.oaep.mdMeth; const EAL_MdMethod *mgfMethod = &ctx->pad.para.oaep.mgfMeth; if ((in == NULL && inLen != 0) || pad == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t hashLen = hashMethod->mdSize; /* If mLen > k - 2hLen - 2, output "message too long" and stop<rfc8017> k is output len, hLen is hashLen, mLen is inLen */ ret = OaepSetLengthCheck(padLen, inLen, hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *pad = 0x00; uint8_t *seed = pad + 1; // Generate a random octet string seed of length hLen<rfc8017> ret = CRYPT_RandEx(LIBCTX_FROM_RSA_CTX(ctx), seed, hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint8_t *db = seed + hashLen; // Calculate hash const CRYPT_ConstData data = {ctx->label.data, ctx->label.len}; ret = CRYPT_CalcHash(ctx->pad.para.oaep.mdProvCtx, hashMethod, &data, 1, db, &hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = OaepSetPs(in, inLen, db, padLen, hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // set maskedDB ret = OaepSetMaskedDB(ctx->pad.para.oaep.mgfProvCtx, mgfMethod, db, seed, padLen, hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // set seedmask ret = OaepSetSeedMask(ctx->pad.para.oaep.mgfProvCtx, mgfMethod, db, seed, padLen, hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } #endif // HITLS_CRYPTO_RSA_ENCRYPT #ifdef HITLS_CRYPTO_RSA_DECRYPT static int32_t OaepVerifyLengthCheck(uint32_t outLen, uint32_t inLen, uint32_t hashLen) { if (outLen > RSA_MAX_MODULUS_LEN || inLen > RSA_MAX_MODULUS_LEN || hashLen > HASH_MAX_MDSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } if (outLen == 0 || hashLen == 0 || inLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } // If k < 2hLen + 2, output "decryption error" and stop if (inLen < 2 * hashLen + 2) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } static int32_t OaepDecodeSeedMask(void *provCtx, const EAL_MdMethod *mgfMethod, const uint8_t *in, uint32_t inLen, CRYPT_Data *seedMask, uint32_t hashLen) { uint32_t i; int32_t ret; const uint8_t *maskedSeed = in + 1; uint32_t maskedDBLen = inLen - hashLen - 1; const uint8_t *maskedDB = maskedSeed + hashLen; ret = CRYPT_Mgf1(provCtx, mgfMethod, maskedDB, maskedDBLen, seedMask->data, hashLen); if (ret != CRYPT_SUCCESS) { return ret; } for (i = 0; i < hashLen; i++) { seedMask->data[i] ^= maskedSeed[i]; } return CRYPT_SUCCESS; } static int32_t OaepDecodeMaskedDB(void *provCtx, const EAL_MdMethod *mgfMethod, const CRYPT_Data *in, const uint8_t *seedMask, uint32_t hashLen, const CRYPT_Data *dbMaskData) { int32_t ret; uint32_t i; const uint8_t *maskedDB = in->data + 1 + hashLen; uint32_t maskedDBLen = in->len - hashLen - 1; ret = CRYPT_Mgf1(provCtx, mgfMethod, seedMask, hashLen, dbMaskData->data, maskedDBLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } for (i = 0; i < maskedDBLen; i++) { dbMaskData->data[i] ^= maskedDB[i]; } return ret; } static int32_t OaepVerifyHashMaskDB(void *provCtx, const EAL_MdMethod *hashMethod, CRYPT_Data *paramData, CRYPT_Data *dbMaskData, uint32_t hashLen, uint32_t *offset) { int32_t ret; uint8_t hashVal[HASH_MAX_MDSIZE]; CRYPT_ConstData data = {paramData->data, paramData->len}; ret = CRYPT_CalcHash(provCtx, hashMethod, &data, 1, hashVal, &hashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (memcmp(dbMaskData->data, hashVal, hashLen) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } *offset = hashLen; while ((*offset) < dbMaskData->len && dbMaskData->data[(*offset)] == 0) { (*offset)++; } if ((*offset) >= dbMaskData->len) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } if (dbMaskData->data[(*offset)] != 0x01) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } (*offset)++; return ret; } int32_t CRYPT_RSA_VerifyPkcs1Oaep(RSA_PadingPara *pad, const uint8_t *in, uint32_t inLen, const uint8_t *param, uint32_t paramLen, uint8_t *msg, uint32_t *msgLen) { if (pad == NULL || in == NULL || msg == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t hashLen = pad->mdMeth.mdSize; if (inLen <= (hashLen + 1)) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } uint32_t maskedDBLen = inLen - hashLen - 1; int32_t ret; uint32_t offset; uint8_t seedMask[HASH_MAX_MDSIZE]; CRYPT_Data seedData = { (uint8_t *)(uintptr_t)seedMask, HASH_MAX_MDSIZE }; CRYPT_Data paramData = { (uint8_t *)(uintptr_t)param, paramLen }; CRYPT_Data inData = { (uint8_t *)(uintptr_t)in, inLen }; uint8_t *maskDB = (uint8_t *)BSL_SAL_Malloc(maskedDBLen); if (maskDB == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } CRYPT_Data dbMaskData = { maskDB, maskedDBLen }; /* If k < 2hLen + 2, output "decryption error" and stop.<rfc8017> k is intLen , hLen is hashLen */ GOTO_ERR_IF_EX(OaepVerifyLengthCheck(*msgLen, inLen, hashLen), ret); GOTO_ERR_IF_EX(OaepDecodeSeedMask(pad->mgfProvCtx, &pad->mgfMeth, in, inLen, &seedData, hashLen), ret); GOTO_ERR_IF_EX(OaepDecodeMaskedDB(pad->mgfProvCtx, &pad->mgfMeth, &inData, seedMask, hashLen, &dbMaskData), ret); GOTO_ERR_IF_EX(OaepVerifyHashMaskDB(pad->mdProvCtx, &pad->mdMeth, &paramData, &dbMaskData, hashLen, &offset), ret); if (memcpy_s(msg, *msgLen, maskDB + offset, maskedDBLen - offset) != EOK) { ret = CRYPT_RSA_NOR_VERIFY_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } *msgLen = maskedDBLen - offset; ERR: BSL_SAL_CleanseData(maskDB, maskedDBLen); BSL_SAL_FREE(maskDB); return ret; } #endif // HITLS_CRYPTO_RSA_DECRYPT #endif // HITLS_CRYPTO_RSAES_OAEP #if defined(HITLS_CRYPTO_RSA_ENCRYPT) && \ (defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) || defined(HITLS_CRYPTO_RSAES_PKCSV15)) // Pad output format: EM = 00 || 02 || PS || 00 || M; where M indicates message. int32_t CRYPT_RSA_SetPkcsV15Type2(void *libCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t outLen) { // If mLen > k - 11, output "message too long" and stop.<rfc8017> if (inLen + 11 > outLen) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_BUFF_LEN_NOT_ENOUGH); return CRYPT_RSA_BUFF_LEN_NOT_ENOUGH; } int32_t ret; uint32_t i; uint8_t *ps = out + 2; uint32_t psLen = outLen - inLen - 3; uint8_t *msg = out + psLen + 3; *out = 0x00; *(out + 1) = 0x02; *(out + outLen - inLen - 2) = 0x00; // msg padding, outLen minus the 3-byte constant, ps length, and start padding. if (inLen != 0 && memcpy_s(msg, outLen - (psLen + 3), in, inLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } // cal ps ret = CRYPT_RandEx(libCtx, ps, psLen); if (ret != CRYPT_SUCCESS) { return ret; } ps[psLen] = 0; for (i = 0; i < psLen; i++) { if (*(ps + i) != 0) { continue; } do { // no zero ret = CRYPT_RandEx(libCtx, ps + i, 1); if (ret != CRYPT_SUCCESS) { return ret; } } while (*(ps + i) == 0); } return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_RSA_ENCRYPT && (EC_PKCSV15_TLS || EC_PKCSV15) #ifdef HITLS_CRYPTO_RSA_DECRYPT #ifdef HITLS_CRYPTO_RSAES_PKCSV15 int32_t CRYPT_RSA_VerifyPkcsV15Type2(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { uint32_t zeroIndex = 0; uint32_t index = ~(0); uint32_t firstZero = Uint32ConstTimeEqual(in[0], 0x00); uint32_t firstTwo = Uint32ConstTimeEqual(in[1], 0x02); // Check the ps starting from subscript 2. for (uint32_t i = 2; i < inLen; i++) { uint32_t equals0 = Uint32ConstTimeIsZero(in[i]); zeroIndex = Uint32ConstTimeSelect(index & equals0, i, zeroIndex); index = Uint32ConstTimeSelect(equals0, 0, index); } uint32_t valid = firstZero & firstTwo & (~index); // Pad output format: EM = 00 || 02 || PS || 00 || M; where M is a message, and PS must be >= 8. // Therefore, the subscript of the second 0 must be greater than or equal to 10. valid &= Uint32ConstTimeGe(zeroIndex, 10); zeroIndex++; if (valid == 0) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } if (inLen - zeroIndex > *outLen) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_NOR_VERIFY_FAIL); return CRYPT_RSA_NOR_VERIFY_FAIL; } (void)memcpy_s(out, *outLen, in + zeroIndex, inLen - zeroIndex); *outLen = inLen - zeroIndex; return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_RSAES_PKCSV15 #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS int32_t CRYPT_RSA_VerifyPkcsV15Type2TLS(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { uint32_t masterSecretLen = *outLen; uint32_t zeroIndex = 0; uint32_t index = ~(0); uint32_t fist = Uint32ConstTimeEqual(in[0], 0x00); uint32_t second = Uint32ConstTimeEqual(in[1], 0x02); for (uint32_t i = 2; i < inLen; i++) { uint32_t equals0 = Uint32ConstTimeIsZero(in[i]); zeroIndex = Uint32ConstTimeSelect(index & equals0, i, zeroIndex); index = Uint32ConstTimeSelect(equals0, 0, index); } uint32_t valid = fist & second & (~index); // Pad output format: EM = 00 || 02 || PS || 00 || M; where M is a message, and PS must be >= 8. // Therefore, the subscript of the second 0 must be greater than or equal to 10. valid &= Uint32ConstTimeGe(zeroIndex, 10); zeroIndex++; uint32_t secretLen = inLen - zeroIndex; valid &= ~(Uint32ConstTimeGt(secretLen, *outLen)); for (uint32_t i = 0; i < masterSecretLen; i++) { uint32_t mask = valid & Uint32ConstTimeLt(i, secretLen); uint32_t inIndex = mask & zeroIndex; out[i] = Uint8ConstTimeSelect(mask, *(in + inIndex + i), 0); } *outLen = secretLen; // if the 'plaintext' is PKCS15 , the valid should be 0xffffffff, else should be 0 // so return 0 for success, else return 0xffffffff return ~valid; } #endif // HITLS_CRYPTO_RSAES_PKCSV15_TLS #endif // HITLS_CRYPTO_RSA_DECRYPT #endif /* HITLS_CRYPTO_RSA */
2301_79861745/bench_create
crypto/rsa/src/rsa_padding.c
C
unknown
36,877
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_SCRYPT_H #define CRYPT_SCRYPT_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SCRYPT #include <stdint.h> #include "crypt_local_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct CryptScryptCtx CRYPT_SCRYPT_Ctx; typedef int32_t (*PBKDF2_PRF)(void *libCtx, const EAL_MacMethod *macMeth, CRYPT_MAC_AlgId macId, const EAL_MdMethod *mdMeth, const uint8_t *key, uint32_t keyLen, const uint8_t *salt, uint32_t saltLen, uint32_t iterCnt, uint8_t *out, uint32_t len); #define CRYPT_SCRYPT_Ctrl NULL /** * @ingroup SCRYPT * @brief Generate SCRYPT context. * * @retval Success: SCRYPT ctx. * Fails: NULL. */ CRYPT_SCRYPT_Ctx *CRYPT_SCRYPT_NewCtx(void); /** * @ingroup SCRYPT * @brief Generate SCRYPT context. * * @retval Success: SCRYPT ctx. * Fails: NULL. */ CRYPT_SCRYPT_Ctx *CRYPT_SCRYPT_NewCtxEx(void *libCtx); /** * @ingroup SCRYPT * @brief Set parameters for the SCRYPT context. * * @param ctx [in, out] Pointer to the SCRYPT context. * @param param [in] Either a MAC algorithm ID, a seed, a password, or a label. * * @retval Success: CRYPT_SUCCESS * For other error codes, see crypt_errno.h. */ int32_t CRYPT_SCRYPT_SetParam(CRYPT_SCRYPT_Ctx *ctx, const BSL_Param *param); /** * @ingroup SCRYPT * @brief Obtain the derived key based on the passed SCRYPT context.. * * @param ctx [in, out] Pointer to the SCRYPT context. * @param out [out] Derived key buffer. * @param len [in] Derived key buffer size. * * @retval Success: CRYPT_SUCCESS * For other error codes, see crypt_errno.h. */ int32_t CRYPT_SCRYPT_Derive(CRYPT_SCRYPT_Ctx *ctx, uint8_t *out, uint32_t len); /** * @ingroup SCRYPT * @brief SCRYPT deinitialization API * * @param ctx [in, out] Pointer to the SCRYPT context. * * @retval #CRYPT_SUCCESS Deinitialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SCRYPT_Deinit(CRYPT_SCRYPT_Ctx *ctx); /** * @ingroup SCRYPT * @brief free SCRYPT context. * * @param ctx [IN] SCRYPT handle */ void CRYPT_SCRYPT_FreeCtx(CRYPT_SCRYPT_Ctx *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_SCRYPT #endif // CRYPT_SCRYPT_H
2301_79861745/bench_create
crypto/scrypt/include/crypt_scrypt.h
C
unknown
2,802
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_SCRYPT #include <stdint.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_local_types.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "crypt_types.h" #include "crypt_scrypt.h" #include "eal_mac_local.h" #include "eal_md_local.h" #include "pbkdf2_local.h" #include "bsl_params.h" #include "crypt_params_key.h" #define SCRYPT_PR_MAX ((1 << 30) - 1) // Convert the little-endian array to the host order. #define SALSA_INPUT_TO_HOST(T, x) \ do { \ (x)[0] = CRYPT_LE32TOH((T)[0]); \ (x)[1] = CRYPT_LE32TOH((T)[1]); \ (x)[2] = CRYPT_LE32TOH((T)[2]); \ (x)[3] = CRYPT_LE32TOH((T)[3]); \ (x)[4] = CRYPT_LE32TOH((T)[4]); \ (x)[5] = CRYPT_LE32TOH((T)[5]); \ (x)[6] = CRYPT_LE32TOH((T)[6]); \ (x)[7] = CRYPT_LE32TOH((T)[7]); \ (x)[8] = CRYPT_LE32TOH((T)[8]); \ (x)[9] = CRYPT_LE32TOH((T)[9]); \ (x)[10] = CRYPT_LE32TOH((T)[10]); \ (x)[11] = CRYPT_LE32TOH((T)[11]); \ (x)[12] = CRYPT_LE32TOH((T)[12]); \ (x)[13] = CRYPT_LE32TOH((T)[13]); \ (x)[14] = CRYPT_LE32TOH((T)[14]); \ (x)[15] = CRYPT_LE32TOH((T)[15]); \ } while (0) // Convert the host order to little endian order. #define SALSA_OUTPUT_TO_LE32(T, x) \ do { \ (T)[0] = CRYPT_HTOLE32((x)[0] + CRYPT_LE32TOH((T)[0])); \ (T)[1] = CRYPT_HTOLE32((x)[1] + CRYPT_LE32TOH((T)[1])); \ (T)[2] = CRYPT_HTOLE32((x)[2] + CRYPT_LE32TOH((T)[2])); \ (T)[3] = CRYPT_HTOLE32((x)[3] + CRYPT_LE32TOH((T)[3])); \ (T)[4] = CRYPT_HTOLE32((x)[4] + CRYPT_LE32TOH((T)[4])); \ (T)[5] = CRYPT_HTOLE32((x)[5] + CRYPT_LE32TOH((T)[5])); \ (T)[6] = CRYPT_HTOLE32((x)[6] + CRYPT_LE32TOH((T)[6])); \ (T)[7] = CRYPT_HTOLE32((x)[7] + CRYPT_LE32TOH((T)[7])); \ (T)[8] = CRYPT_HTOLE32((x)[8] + CRYPT_LE32TOH((T)[8])); \ (T)[9] = CRYPT_HTOLE32((x)[9] + CRYPT_LE32TOH((T)[9])); \ (T)[10] = CRYPT_HTOLE32((x)[10] + CRYPT_LE32TOH((T)[10])); \ (T)[11] = CRYPT_HTOLE32((x)[11] + CRYPT_LE32TOH((T)[11])); \ (T)[12] = CRYPT_HTOLE32((x)[12] + CRYPT_LE32TOH((T)[12])); \ (T)[13] = CRYPT_HTOLE32((x)[13] + CRYPT_LE32TOH((T)[13])); \ (T)[14] = CRYPT_HTOLE32((x)[14] + CRYPT_LE32TOH((T)[14])); \ (T)[15] = CRYPT_HTOLE32((x)[15] + CRYPT_LE32TOH((T)[15])); \ } while (0) #define SCRYPT_ELEMENTSIZE 64 struct CryptScryptCtx { const EAL_MacMethod *macMeth; const EAL_MdMethod *mdMeth; PBKDF2_PRF pbkdf2Prf; uint8_t *password; uint32_t passLen; uint8_t *salt; uint32_t saltLen; uint32_t n; uint32_t r; uint32_t p; void *libCtx; // For provider usage, can be NULL if not used. }; /* This function is implemented by referring to the RFC standard. For details, see section 3 in https://www.rfc-editor.org/rfc/rfc7914.txt */ static void SCRYPT_Salsa20WordSpecification(uint32_t t[16]) { uint32_t x[16]; SALSA_INPUT_TO_HOST(t, x); for (int i = 0; i < 4; i++) { x[4] ^= ROTL32(x[0] + x[12], 7); x[8] ^= ROTL32(x[4] + x[0], 9); x[12] ^= ROTL32(x[8] + x[4], 13); x[0] ^= ROTL32(x[12] + x[8], 18); x[9] ^= ROTL32(x[5] + x[1], 7); x[13] ^= ROTL32(x[9] + x[5], 9); x[1] ^= ROTL32(x[13] + x[9], 13); x[5] ^= ROTL32(x[1] + x[13], 18); x[14] ^= ROTL32(x[10] + x[6], 7); x[2] ^= ROTL32(x[14] + x[10], 9); x[6] ^= ROTL32(x[2] + x[14], 13); x[10] ^= ROTL32(x[6] + x[2], 18); x[3] ^= ROTL32(x[15] + x[11], 7); x[7] ^= ROTL32(x[3] + x[15], 9); x[11] ^= ROTL32(x[7] + x[3], 13); x[15] ^= ROTL32(x[11] + x[7], 18); x[1] ^= ROTL32(x[0] + x[3], 7); x[2] ^= ROTL32(x[1] + x[0], 9); x[3] ^= ROTL32(x[2] + x[1], 13); x[0] ^= ROTL32(x[3] + x[2], 18); x[6] ^= ROTL32(x[5] + x[4], 7); x[7] ^= ROTL32(x[6] + x[5], 9); x[4] ^= ROTL32(x[7] + x[6], 13); x[5] ^= ROTL32(x[4] + x[7], 18); x[11] ^= ROTL32(x[10] + x[9], 7); x[8] ^= ROTL32(x[11] + x[10], 9); x[9] ^= ROTL32(x[8] + x[11], 13); x[10] ^= ROTL32(x[9] + x[8], 18); x[12] ^= ROTL32(x[15] + x[14], 7); x[13] ^= ROTL32(x[12] + x[15], 9); x[14] ^= ROTL32(x[13] + x[12], 13); x[15] ^= ROTL32(x[14] + x[13], 18); } SALSA_OUTPUT_TO_LE32(t, x); } static void SCRYPT_BlockMix(uint8_t *b, uint8_t *y, uint32_t r) { uint8_t *bTmp = b; uint8_t *y0 = y; uint8_t *y1 = y + (r << 6); /* RFC7914 section 4 In this implementation, the output Y is split and processed separately based on the description in section 4. The performance is slightly improved. 1. B' = Y, Y0 = Y, Y1 = Y[r], b=B, The block size of each Y is 64 bytes. The processing unit in this function is 64 bytes. 2. Y0 = B[2 * r - 1] ^ B[0] Salsa(Y0) Y1 = Y0 ^ B[1] Salsa(Y1) 3. for i = 1 to r -1 do b += 2 // Two blocks have been processed in step 2. Y0 += 1 // Process the next block of Y0. Y0 = b[0] ^ Y1 Salsa(Y0) Y1 += 1 Y1 = b[1] ^ Y0 Salsa(Y1) 4. B = Y // Copy Y to B. */ // r << 7 is equal to r * 128, where r * 128 is the block size of the algorithm. DATA32_XOR(b + (r << 7) - SCRYPT_ELEMENTSIZE, bTmp, y0, SCRYPT_ELEMENTSIZE); SCRYPT_Salsa20WordSpecification((uint32_t*)y0); DATA32_XOR(y0, bTmp + SCRYPT_ELEMENTSIZE, y1, SCRYPT_ELEMENTSIZE); SCRYPT_Salsa20WordSpecification((uint32_t*)y1); for (uint32_t i = 1; i < r; i++) { bTmp += 128; // Process two pieces of 64-bit(SCRYPT_ELEMENTSIZE) data in one cycle. 64 * 2 = 128 y0 += SCRYPT_ELEMENTSIZE; DATA32_XOR(y1, bTmp, y0, SCRYPT_ELEMENTSIZE); SCRYPT_Salsa20WordSpecification((uint32_t*)y0); y1 += SCRYPT_ELEMENTSIZE; DATA32_XOR(y0, bTmp + SCRYPT_ELEMENTSIZE, y1, SCRYPT_ELEMENTSIZE); SCRYPT_Salsa20WordSpecification((uint32_t*)y1); } (void)memcpy_s(b, r << 7, y, r << 7); // Length bit r of B and y: r << 7 } /* For details about this function, see section 5 in RFC7914 */ static void SCRYPT_ROMix(uint8_t *b, uint32_t n, uint32_t r, uint8_t *v, uint8_t *y) { uint32_t i; uint8_t *tmp = NULL; uint32_t blockSize = r << 7; for (i = 0, tmp = v; i < n; i++, tmp += blockSize) { (void)memcpy_s(tmp, blockSize, b, blockSize); SCRYPT_BlockMix(b, y, r); } for (i = 0; i < n; i++) { uint32_t j = GET_UINT32_LE(b, blockSize - 64) & (n - 1); // X= B, X = X ^ Vj DATA32_XOR(b, &v[j * blockSize], b, blockSize); SCRYPT_BlockMix(b, y, r); } } static int32_t SCRYPT_CheckParam(uint32_t n, uint32_t r, uint32_t p, const uint8_t *out, uint32_t len) { if (r == 0 || p == 0 || n <= 1 || ((n & (n - 1)) != 0)) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } if (p > SCRYPT_PR_MAX / r) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } /* r <= 3 indicates 16 * r < (sizeof(uint64_t) * 8 - 1) */ if ((r <= 3) && (n >= (((uint64_t)1) << (16 * r)))) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } /* (p * 128 * r < UINT32_MAX) && (32 * r * n * sizeof(uint32_t)) */ if ((r > ((UINT32_MAX / 128) / p)) || (n > ((UINT32_MAX / 128) / r))) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } /* malloc size:p * 128 * r + n * 128 * r + 128 *r */ if ((uint64_t)p * 128 * r + n * 128 * r + 128 * r > UINT32_MAX) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } if (out == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } return CRYPT_SUCCESS; } static int32_t SCRYPT_CheckPointer(PBKDF2_PRF pbkdf2Prf, const uint8_t *key, uint32_t keyLen, const uint8_t *salt, uint32_t saltLen) { if (pbkdf2Prf == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (key == NULL && keyLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (salt == NULL && saltLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SUCCESS; } /* For details about this function, see section 6 in RFC7914. */ int32_t CRYPT_SCRYPT(PBKDF2_PRF pbkdf2Prf, const EAL_MacMethod *macMeth, CRYPT_MAC_AlgId macId, const EAL_MdMethod *mdMeth, const uint8_t *key, uint32_t keyLen, const uint8_t *salt, uint32_t saltLen, uint32_t n, uint32_t r, uint32_t p, uint8_t *out, uint32_t len) { int32_t ret; // V in ROMix and BlockMix is allocated here, reducing memory application and release costs uint8_t *b = NULL, *v = NULL, *bi = NULL, *y = NULL; uint32_t bLen, blockSize, sumLen; if ((ret = SCRYPT_CheckParam(n, r, p, out, len)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if ((ret = SCRYPT_CheckPointer(pbkdf2Prf, key, keyLen, salt, saltLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } blockSize = r << 7; // block length: r << 7 (r * 128) bLen = blockSize * p; sumLen = bLen + blockSize * n + blockSize; b = BSL_SAL_Malloc(sumLen); if (b == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } v = b + bLen; y = v + blockSize * n; GOTO_ERR_IF(pbkdf2Prf(NULL, macMeth, macId, mdMeth, key, keyLen, salt, saltLen, 1, b, bLen), ret); bi = b; for (uint32_t i = 0; i < p; i++, bi += blockSize) { SCRYPT_ROMix(bi, n, r, v, y); } GOTO_ERR_IF(pbkdf2Prf(NULL, macMeth, macId, mdMeth, key, keyLen, b, bLen, 1, out, len), ret); ERR: BSL_SAL_FREE(b); return ret; } int32_t CRYPT_SCRYPT_SetMacMethod(CRYPT_SCRYPT_Ctx *ctx) { ctx->macMeth = EAL_MacFindDefaultMethod(CRYPT_MAC_HMAC_SHA256); if (ctx->macMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_METH_NULL_MEMBER); return CRYPT_EAL_ERR_METH_NULL_MEMBER; } ctx->mdMeth = EAL_MdFindDefaultMethod(CRYPT_MD_SHA256); if (ctx->mdMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_METH_NULL_MEMBER); return CRYPT_EAL_ERR_METH_NULL_MEMBER; } return CRYPT_SUCCESS; } int32_t CRYPT_SCRYPT_InitCtx(CRYPT_SCRYPT_Ctx *ctx) { int32_t ret = CRYPT_SCRYPT_SetMacMethod(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ctx->pbkdf2Prf = CRYPT_PBKDF2_HMAC; return CRYPT_SUCCESS; } CRYPT_SCRYPT_Ctx *CRYPT_SCRYPT_NewCtx(void) { CRYPT_SCRYPT_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(CRYPT_SCRYPT_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } int32_t ret = CRYPT_SCRYPT_InitCtx(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_FREE(ctx); return NULL; } return ctx; } #ifdef HITLS_CRYPTO_PROVIDER CRYPT_SCRYPT_Ctx *CRYPT_SCRYPT_NewCtxEx(void *libCtx) { CRYPT_SCRYPT_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(CRYPT_SCRYPT_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } int32_t ret = CRYPT_SCRYPT_InitCtx(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_FREE(ctx); return NULL; } ctx->libCtx = libCtx; return ctx; } #endif int32_t CRYPT_SCRYPT_SetPassWord(CRYPT_SCRYPT_Ctx *ctx, const uint8_t *password, uint32_t passLen) { if (password == NULL && passLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_ClearFree(ctx->password, ctx->passLen); ctx->password = BSL_SAL_Dump(password, passLen); if (ctx->password == NULL && passLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->passLen = passLen; return CRYPT_SUCCESS; } int32_t CRYPT_SCRYPT_SetSalt(CRYPT_SCRYPT_Ctx *ctx, const uint8_t *salt, uint32_t saltLen) { if (salt == NULL && saltLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_FREE(ctx->salt); ctx->salt = BSL_SAL_Dump(salt, saltLen); if (ctx->salt == NULL && saltLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->saltLen = saltLen; return CRYPT_SUCCESS; } int32_t CRYPT_SCRYPT_SetN(CRYPT_SCRYPT_Ctx *ctx, const uint32_t n) { if (n <= 1 || (n & (n - 1)) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } ctx->n = n; return CRYPT_SUCCESS; } int32_t CRYPT_SCRYPT_SetR(CRYPT_SCRYPT_Ctx *ctx, const uint32_t r) { if (r == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } ctx->r = r; return CRYPT_SUCCESS; } int32_t CRYPT_SCRYPT_SetP(CRYPT_SCRYPT_Ctx *ctx, const uint32_t p) { if (p == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_PARAM_ERROR); return CRYPT_SCRYPT_PARAM_ERROR; } ctx->p = p; return CRYPT_SUCCESS; } int32_t CRYPT_SCRYPT_SetParam(CRYPT_SCRYPT_Ctx *ctx, const BSL_Param *param) { uint32_t val = 0; uint32_t len = 0; const BSL_Param *temp = NULL; int32_t ret = CRYPT_SCRYPT_PARAM_ERROR; if (ctx == NULL || param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_PASSWORD)) != NULL) { GOTO_ERR_IF(CRYPT_SCRYPT_SetPassWord(ctx, temp->value, temp->valueLen), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_SALT)) != NULL) { GOTO_ERR_IF(CRYPT_SCRYPT_SetSalt(ctx, temp->value, temp->valueLen), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_N)) != NULL) { len = sizeof(val); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_N, BSL_PARAM_TYPE_UINT32, &val, &len), ret); GOTO_ERR_IF(CRYPT_SCRYPT_SetN(ctx, val), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_R)) != NULL) { len = sizeof(val); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_R, BSL_PARAM_TYPE_UINT32, &val, &len), ret); GOTO_ERR_IF(CRYPT_SCRYPT_SetR(ctx, val), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_P)) != NULL) { len = sizeof(val); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_P, BSL_PARAM_TYPE_UINT32, &val, &len), ret); GOTO_ERR_IF(CRYPT_SCRYPT_SetP(ctx, val), ret); } ERR: return ret; } int32_t CRYPT_SCRYPT_Derive(CRYPT_SCRYPT_Ctx *ctx, uint8_t *out, uint32_t len) { int32_t ret; uint8_t *b = NULL, *v = NULL, *bi = NULL, *y = NULL; uint32_t bLen, blockSize, sumLen; const EAL_MacMethod *macMeth = ctx->macMeth; const EAL_MdMethod *mdMeth = ctx->mdMeth; PBKDF2_PRF pbkdf2Prf = ctx->pbkdf2Prf; const uint8_t *password = ctx->password; uint32_t passLen = ctx->passLen; const uint8_t *salt = ctx->salt; uint32_t saltLen = ctx->saltLen; uint32_t n = ctx->n; uint32_t r = ctx->r; uint32_t p = ctx->p; if ((ret = SCRYPT_CheckParam(n, r, p, out, len)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if ((ret = SCRYPT_CheckPointer(pbkdf2Prf, password, passLen, salt, saltLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } blockSize = r << 7; bLen = blockSize * p; sumLen = bLen + blockSize * n + blockSize; if (sumLen < bLen) { BSL_ERR_PUSH_ERROR(CRYPT_SCRYPT_DATA_TOO_MAX); return CRYPT_SCRYPT_DATA_TOO_MAX; } b = BSL_SAL_Malloc(sumLen); if (b == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } v = b + bLen; y = v + blockSize * ctx->n; GOTO_ERR_IF(pbkdf2Prf(ctx->libCtx, macMeth, CRYPT_MAC_HMAC_SHA256, mdMeth, password, passLen, salt, saltLen, 1, b, bLen), ret); bi = b; for (uint32_t i = 0; i < p; i++, bi += blockSize) { SCRYPT_ROMix(bi, n, r, v, y); } GOTO_ERR_IF(pbkdf2Prf(ctx->libCtx, macMeth, CRYPT_MAC_HMAC_SHA256, mdMeth, password, passLen, b, bLen, 1, out, len), ret); ERR: BSL_SAL_FREE(b); return ret; } int32_t CRYPT_SCRYPT_Deinit(CRYPT_SCRYPT_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_ClearFree(ctx->password, ctx->passLen); BSL_SAL_FREE(ctx->salt); (void)memset_s(ctx, sizeof(CRYPT_SCRYPT_Ctx), 0, sizeof(CRYPT_SCRYPT_Ctx)); int32_t ret = CRYPT_SCRYPT_InitCtx(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } void CRYPT_SCRYPT_FreeCtx(CRYPT_SCRYPT_Ctx *ctx) { if (ctx == NULL) { return; } BSL_SAL_ClearFree(ctx->password, ctx->passLen); BSL_SAL_FREE(ctx->salt); BSL_SAL_Free(ctx); } #endif /* HITLS_CRYPTO_SCRYPT */
2301_79861745/bench_create
crypto/scrypt/src/scrypt.c
C
unknown
17,935
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_SHA1_H #define CRYPT_SHA1_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA1 #include <stdint.h> #include <stdlib.h> #include "crypt_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ /* Length of the message digest buffer. */ #define CRYPT_SHA1_DIGESTSIZE 20 /* Message processing block size */ #define CRYPT_SHA1_BLOCKSIZE 64 typedef struct CryptSha1Ctx CRYPT_SHA1_Ctx; #define CRYPT_SHA1_Squeeze NULL /** * @ingroup SHA1 * @brief Generate md context. * * @retval Success: sha1 ctx. * Fails: NULL. */ CRYPT_SHA1_Ctx *CRYPT_SHA1_NewCtx(void); /** * @ingroup SHA1 * @brief Generate md context. * * @param libCtx [IN] library context * @param algId [IN] algorithm id * * @retval Success: sha1 ctx. * Fails: NULL. */ CRYPT_SHA1_Ctx *CRYPT_SHA1_NewCtxEx(void *libCtx, int32_t algId); /** * @ingroup SHA1 * @brief free md context. * * @param ctx [IN] md handle */ void CRYPT_SHA1_FreeCtx(CRYPT_SHA1_Ctx *ctx); /** * @ingroup SHA1 * @brief This API is invoked to initialize the SHA-1 context. * * @param *ctx [in,out] Pointer to the SHA-1 context. * @param *param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS initialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SHA1_Init(CRYPT_SHA1_Ctx *ctx, BSL_Param *param); /** * @ingroup SHA1 * @brief Encode the input text and update the message digest. * * @param *ctx [in,out] Pointer to the SHA-1 context. * @param *in [in] Pointer to the data to be calculated * @param len [in] Length of the data to be calculated * * @retval #CRYPT_SUCCESS succeeded in updating the internal status of the digest. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. * @retval #CRYPT_SHA1_ERR_OVERFLOW input data length exceeds the maximum (2^64 bits) */ int32_t CRYPT_SHA1_Update(CRYPT_SHA1_Ctx *ctx, const uint8_t *in, uint32_t len); /** * @ingroup SHA1 * @brief Obtain the message digest based on the passed SHA-1 text. * * @param *ctx [in,out] Pointer to the SHA-1 context. * @param *out [in] Digest buffer * @param *len [in,out] Digest buffer size * * @retval #CRYPT_SUCCESS succeeded in obtaining the computed digest. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. * @retval #CRYPT_SHA1_ERR_OVERFLOW Input data length exceeds the maximum (2^64 bits). * @retval #CRYPT_SHA1_OUT_BUFF_LEN_NOT_ENOUGH The output buffer is insufficient. */ int32_t CRYPT_SHA1_Final(CRYPT_SHA1_Ctx *ctx, uint8_t *out, uint32_t *len); /** * @ingroup SHA1 * @brief SHA1 deinitialization API * @param *ctx [in,out] Pointer to the SHA-1 context. * * @retval #CRYPT_SUCCESS initialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SHA1_Deinit(CRYPT_SHA1_Ctx *ctx); /** * @ingroup SHA1 * @brief SHA1 copy CTX function * @param dest [out] Pointer to the dest SHA1 context. * @param src [in] Pointer to the original SHA1 context. * * @retval #CRYPT_SUCCESS initialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SHA1_CopyCtx(CRYPT_SHA1_Ctx *dst, const CRYPT_SHA1_Ctx *src); /** * @ingroup SHA1 * @brief SHA1 dup CTX function * @param src [in] Pointer to the original SHA1 context. * * @retval Success: sha1 ctx. * Fails: NULL. */ CRYPT_SHA1_Ctx *CRYPT_SHA1_DupCtx(const CRYPT_SHA1_Ctx *src); #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup SHA1 * @brief SHA1 get param function * @param ctx [in] Pointer to the SHA1 context. * @param param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS Success. * @retval #CRYPT_NULL_INPUT Pointer param is NULL * @retval #CRYPT_INVALID_ARG Pointer param is invalid */ int32_t CRYPT_SHA1_GetParam(CRYPT_SHA1_Ctx *ctx, BSL_Param *param); #else #define CRYPT_SHA1_GetParam NULL #endif #ifdef __cplusplus } #endif /* __cpluscplus */ #endif // HITLS_CRYPTO_SHA1 #endif // CRYPT_SHA1_H
2301_79861745/bench_create
crypto/sha1/include/crypt_sha1.h
C
unknown
4,617
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA1 #include "crypt_arm.h" .arch armv8-a+crypto .extern g_cryptArmCpuInfo .hidden g_cryptArmCpuInfo /* SHA1 used constant value. For the data source, see the RFC3174 document. * K(t) = 5A827999 ( 0 <= t <= 19) * K(t) = 6ED9EBA1 (20 <= t <= 39) * K(t) = 8F1BBCDC (40 <= t <= 59) * K(t) = CA62C1D6 (60 <= t <= 79) */ .data .balign 64 // Alignment based on the size of the read data block .type g_k, %object g_k: .long 0x5a827999 .long 0x6ed9eba1 .long 0x8f1bbcdc .long 0xca62c1d6 .size g_k, .-g_k .balign 64 // Alignment based on the size of the read data block .type g_kExt, %object g_kExt: .long 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999 //K_00_19 .long 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1 //K_20_39 .long 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc //K_40_59 .long 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6 //K_60_79 .size g_kExt, .-g_kExt /** * Macro Description: 32位Message block扩展Wi * input register: * wi_3: W[i-3] * wi_8: W[i-8] * wi_14: W[i-14] * wi_16: W[i-16] * temp1: temporary register * temp2: temporary register * Modify the register: wi_16 temp1 temp2 * Output register: * wi_16: Latest W[i] value, W(i) = S^1(W(i-3) XOR W(i-8) XOR W(i-14) XOR W(i-16)) * Function/Macro Call: NONE */ .macro MESSAGE_EXPAND wi_16, wi_14, wi_8, wi_3, temp1, temp2 eor \temp1, \wi_14, \wi_16 // W(i-14) XOR W(i-16) eor \temp2, \wi_3, \wi_8 // W(i-3) XOR W(i-8) eor \wi_16, \temp1, \temp2 // W(i-3) XOR W(i-8) XOR W(i-14) XOR W(i-16) ror \wi_16, \wi_16, #31 // Cyclic left shift 1 equals cyclic right shift 31 .endm /** * Macro Description: b、e Compute * input register: * k: Constant data * wi: Message block * a、b、e: Intermediate variable of hash value * f: f(B, C, D) * temp1-4: temporary register * Modify the register: b e temp3-temp4 * Output register: * b: Indicates the value after a cyclic update. * e: Indicates the value after a cyclic update. * Macro implementation: * e = S^5(A) + f(B, C, D) + E + W(i) + K(i) * b = S^30(B) * Function/Macro Call: NONE */ .macro CAL_B_E a, b, e, wi, k, f, temp3, temp4 add \temp3, \wi, \k // W(i) + K(i) ror \temp4, \a, #27 // S^5(A) Cyclic shift left 5 equal Cyclic shift right 27 ror \b, \b, #2 // b = S^30(B) Cyclic shift left 30 equal Cyclic shift right 2 add \temp4, \temp4, \temp3 // S^5(A) + W(i) + K(i) add \e, \e, \f // f(B, C, D) + E add \e, \e, \temp4 // f(B, C, D) + E + S^5(A) + W(i) + K(i) .endm /** * Macro Description: Message compression,0~19round data compression * input register: * k: Constant data * wi: Message block * a - h: Intermediate variable of hash value * temp1-4: temporary register * Modify the register: b e temp1-temp4 * Output register: * b: Indicates the value after a cyclic update. * e: Indicates the value after a cyclic update. * Macro implementation: f(B, C, D) = (B AND C) OR ((NOT B) AND D) * e = S^5(A) + f(B, C, D) + E + W(i) + K(i) * b = S^30(B) * Function/Macro Call: CAL_B_E */ .macro DATA_COMPRE_0_19 a, b, c, d, e, wi, k, temp1, temp2, temp3, temp4 and \temp1, \b, \c // b&c bic \temp2, \d, \b // d&(~b) orr \temp1, \temp1, \temp2 // f(B, C, D) CAL_B_E \a, \b, \e, \wi, \k, \temp1, \temp3, \temp4 .endm /** * Macro Description: Message compression,20~39、60~79round data compression * input register: * k: Constant data * wi: Message block * a - h: Intermediate variable of hash value * temp1-4: temporary register * Modify the register: b e temp1-temp4 * Output register: * b: Indicates the value after a cyclic update. * e: Indicates the value after a cyclic update. * Macro implementation: f(B, C, D) = B XOR C XOR D * e = S^5(A) + f(B, C, D) + E + W(i) + K(i) * b = S^30(B) * Function/Macro Call: CAL_B_E */ .macro DATA_COMPRE_20_39_60_79 a, b, c, d, e, wi, k, temp1, temp2, temp3, temp4 eor \temp2, \b, \c // b&c eor \temp1, \temp2, \d // f(B, C, D) = b&c&d CAL_B_E \a, \b, \e, \wi, \k, \temp1, \temp3, \temp4 .endm /** * Macro Description: Message compression,40~59round data compression * input register: * k: Constant data * wi: Message block * a - h: Intermediate variable of hash value * temp1-4: temporary register * Modify the register: b e temp1-temp4 * Output register: * b: Indicates the value after a cyclic update. * e: Indicates the value after a cyclic update. * Macro implementation: f(B, C, D) = (B AND C) OR (B AND D) OR (C AND D) * e = S^5(A) + f(B, C, D) + E + W(i) + K(i) * b = S^30(B) * Function/Macro Call: CAL_B_E */ .macro DATA_COMPRE_40_59 a, b, c, d, e, wi, k, temp1, temp2, temp3, temp4 and \temp1, \b, \c // b&c and \temp2, \b, \d // b&d and \temp3, \c, \d // c&d orr \temp1, \temp1, \temp2 // (b&c) or (b&d) orr \temp1, \temp1, \temp3 // f(B, C, D) CAL_B_E \a, \b, \e, \wi, \k, \temp1, \temp3, \temp4 .endm /** * Function Description: Perform SHA1 compression calculation based on the input message and update the hash value. * Function prototype: static const uint8_t *SHA1_Step(const uint8_t *input, uint32_t len, uint32_t *h) * Input register: * x0: Pointer to the input data address * x1: Message length * x2: Storage address of the hash value * Register usage: w0–w15 store message blocks, x/w16, w17, w28, and w29 are temporary registers, * and x30 stores the hash value address. a to e correspond to w20 to w24. w19 stores the k constant, * x25 stores the message pointer, and x26 stores the remaining message length. * Output register: x0 returns the address of the message for which sha1 calculation is not performed. * Function/Macro Call: DATA_COMPRE_0_19、DATA_COMPRE_20_39_60_79、DATA_COMPRE_40_59、MESSAGE_EXPAND、SHA1CryptoExt */ .text .balign 16 .global SHA1_Step .type SHA1_Step, %function SHA1_Step: .inst 0xd503233f // paciasp cmp x1, #64 b.lo .Lend_sha1 /* If the SHA1 cryptography extension instruction is supported, go to. */ adrp x5, g_cryptArmCpuInfo add x5, x5, :lo12:g_cryptArmCpuInfo ldr x6, [x5] tst x6, #CRYPT_ARM_SHA1 bne SHA1CryptoExt /* Extended instructions are not supported, Using Base Instructions, Open up stack space, push stack protection */ stp x29, x30, [sp, #-96]! stp x19, x20, [sp, #8*2] stp x21, x22, [sp, #8*4] stp x23, x24, [sp, #8*6] stp x25, x26, [sp, #8*8] stp x27, x28, [sp, #8*10] /* load a - e */ ldp w20, w21, [x2] ldp w22, w23, [x2, #4*2] ldr w24, [x2, #4*4] mov x30, x2 // x30 address for storing hash values mov x25, x0 // pointer to the x25 store message mov x26, x1 // x26: stores the remaining message length. .Lloop_sha1_compress: adrp x16, g_k add x16, x16, :lo12:g_k ldr w19, [x16] // load k1 ldp w0, w1, [x25] // load input value, load 64 bytes at a time ldp w2, w3, [x25, #4*2] ldp w4, w5, [x25, #4*4] ldp w6, w7, [x25, #4*6] ldp w8, w9, [x25, #4*8] ldp w10, w11, [x25, #4*10] ldp w12, w13, [x25, #4*12] ldp w14, w15, [x25, #4*14] add x25, x25, #64 // address offset: 64 bytes sub x26, x26, #64 // update the remaining address length. #ifndef HITLS_BIG_ENDIAN rev w0, w0 rev w1, w1 rev w2, w2 rev w3, w3 rev w4, w4 rev w5, w5 rev w6, w6 rev w7, w7 rev w8, w8 rev w9, w9 rev w10, w10 rev w11, w11 rev w12, w12 rev w13, w13 rev w14, w14 rev w15, w15 #endif /* 0~19round data compression */ /* a, b, c, d, e, wi, k, temp1, temp2, temp3, temp4 */ DATA_COMPRE_0_19 w20, w21, w22, w23, w24, w0, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w24, w20, w21, w22, w23, w1, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w23, w24, w20, w21, w22, w2, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w22, w23, w24, w20, w21, w3, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w21, w22, w23, w24, w20, w4, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w20, w21, w22, w23, w24, w5, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w24, w20, w21, w22, w23, w6, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w23, w24, w20, w21, w22, w7, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w22, w23, w24, w20, w21, w8, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w21, w22, w23, w24, w20, w9, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w20, w21, w22, w23, w24, w10, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w24, w20, w21, w22, w23, w11, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w23, w24, w20, w21, w22, w12, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w22, w23, w24, w20, w21, w13, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w21, w22, w23, w24, w20, w14, w19, w16, w17, w28, w29 DATA_COMPRE_0_19 w20, w21, w22, w23, w24, w15, w19, w16, w17, w28, w29 /* Message block extension calculation wi_16, wi_14, wi_8, wi_3, temp1, temp2 */ MESSAGE_EXPAND w0, w2, w8, w13, w16, w17 DATA_COMPRE_0_19 w24, w20, w21, w22, w23, w0, w19, w16, w17, w28, w29 MESSAGE_EXPAND w1, w3, w9, w14, w16, w17 DATA_COMPRE_0_19 w23, w24, w20, w21, w22, w1, w19, w16, w17, w28, w29 MESSAGE_EXPAND w2, w4, w10, w15, w16, w17 DATA_COMPRE_0_19 w22, w23, w24, w20, w21, w2, w19, w16, w17, w28, w29 MESSAGE_EXPAND w3, w5, w11, w0, w16, w17 DATA_COMPRE_0_19 w21, w22, w23, w24, w20, w3, w19, w16, w17, w28, w29 /* 20~39 round data compression */ adrp x16, g_k add x16, x16, :lo12:g_k ldr w19, [x16, #4] // load k2 MESSAGE_EXPAND w4, w6, w12, w1, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w4, w19, w16, w17, w28, w29 MESSAGE_EXPAND w5, w7, w13, w2, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w5, w19, w16, w17, w28, w29 MESSAGE_EXPAND w6, w8, w14, w3, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w6, w19, w16, w17, w28, w29 MESSAGE_EXPAND w7, w9, w15, w4, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w7, w19, w16, w17, w28, w29 MESSAGE_EXPAND w8, w10, w0, w5, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w8, w19, w16, w17, w28, w29 MESSAGE_EXPAND w9, w11, w1, w6, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w9, w19, w16, w17, w28, w29 MESSAGE_EXPAND w10, w12, w2, w7, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w10, w19, w16, w17, w28, w29 MESSAGE_EXPAND w11, w13, w3, w8, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w11, w19, w16, w17, w28, w29 MESSAGE_EXPAND w12, w14, w4, w9, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w12, w19, w16, w17, w28, w29 MESSAGE_EXPAND w13, w15, w5, w10, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w13, w19, w16, w17, w28, w29 MESSAGE_EXPAND w14, w0, w6, w11, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w14, w19, w16, w17, w28, w29 MESSAGE_EXPAND w15, w1, w7, w12, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w15, w19, w16, w17, w28, w29 MESSAGE_EXPAND w0, w2, w8, w13, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w0, w19, w16, w17, w28, w29 MESSAGE_EXPAND w1, w3, w9, w14, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w1, w19, w16, w17, w28, w29 MESSAGE_EXPAND w2, w4, w10, w15, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w2, w19, w16, w17, w28, w29 MESSAGE_EXPAND w3, w5, w11, w0, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w3, w19, w16, w17, w28, w29 MESSAGE_EXPAND w4, w6, w12, w1, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w4, w19, w16, w17, w28, w29 MESSAGE_EXPAND w5, w7, w13, w2, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w5, w19, w16, w17, w28, w29 MESSAGE_EXPAND w6, w8, w14, w3, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w6, w19, w16, w17, w28, w29 MESSAGE_EXPAND w7, w9, w15, w4, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w7, w19, w16, w17, w28, w29 /* 40~59 round data compression */ adrp x16, g_k add x16, x16, :lo12:g_k ldr w19, [x16, #8] // load k3 MESSAGE_EXPAND w8, w10, w0, w5, w16, w17 DATA_COMPRE_40_59 w20, w21, w22, w23, w24, w8, w19, w16, w17, w28, w29 MESSAGE_EXPAND w9, w11, w1, w6, w16, w17 DATA_COMPRE_40_59 w24, w20, w21, w22, w23, w9, w19, w16, w17, w28, w29 MESSAGE_EXPAND w10, w12, w2, w7, w16, w17 DATA_COMPRE_40_59 w23, w24, w20, w21, w22, w10, w19, w16, w17, w28, w29 MESSAGE_EXPAND w11, w13, w3, w8, w16, w17 DATA_COMPRE_40_59 w22, w23, w24, w20, w21, w11, w19, w16, w17, w28, w29 MESSAGE_EXPAND w12, w14, w4, w9, w16, w17 DATA_COMPRE_40_59 w21, w22, w23, w24, w20, w12, w19, w16, w17, w28, w29 MESSAGE_EXPAND w13, w15, w5, w10, w16, w17 DATA_COMPRE_40_59 w20, w21, w22, w23, w24, w13, w19, w16, w17, w28, w29 MESSAGE_EXPAND w14, w0, w6, w11, w16, w17 DATA_COMPRE_40_59 w24, w20, w21, w22, w23, w14, w19, w16, w17, w28, w29 MESSAGE_EXPAND w15, w1, w7, w12, w16, w17 DATA_COMPRE_40_59 w23, w24, w20, w21, w22, w15, w19, w16, w17, w28, w29 MESSAGE_EXPAND w0, w2, w8, w13, w16, w17 DATA_COMPRE_40_59 w22, w23, w24, w20, w21, w0, w19, w16, w17, w28, w29 MESSAGE_EXPAND w1, w3, w9, w14, w16, w17 DATA_COMPRE_40_59 w21, w22, w23, w24, w20, w1, w19, w16, w17, w28, w29 MESSAGE_EXPAND w2, w4, w10, w15, w16, w17 DATA_COMPRE_40_59 w20, w21, w22, w23, w24, w2, w19, w16, w17, w28, w29 MESSAGE_EXPAND w3, w5, w11, w0, w16, w17 DATA_COMPRE_40_59 w24, w20, w21, w22, w23, w3, w19, w16, w17, w28, w29 MESSAGE_EXPAND w4, w6, w12, w1, w16, w17 DATA_COMPRE_40_59 w23, w24, w20, w21, w22, w4, w19, w16, w17, w28, w29 MESSAGE_EXPAND w5, w7, w13, w2, w16, w17 DATA_COMPRE_40_59 w22, w23, w24, w20, w21, w5, w19, w16, w17, w28, w29 MESSAGE_EXPAND w6, w8, w14, w3, w16, w17 DATA_COMPRE_40_59 w21, w22, w23, w24, w20, w6, w19, w16, w17, w28, w29 MESSAGE_EXPAND w7, w9, w15, w4, w16, w17 DATA_COMPRE_40_59 w20, w21, w22, w23, w24, w7, w19, w16, w17, w28, w29 MESSAGE_EXPAND w8, w10, w0, w5, w16, w17 DATA_COMPRE_40_59 w24, w20, w21, w22, w23, w8, w19, w16, w17, w28, w29 MESSAGE_EXPAND w9, w11, w1, w6, w16, w17 DATA_COMPRE_40_59 w23, w24, w20, w21, w22, w9, w19, w16, w17, w28, w29 MESSAGE_EXPAND w10, w12, w2, w7, w16, w17 DATA_COMPRE_40_59 w22, w23, w24, w20, w21, w10, w19, w16, w17, w28, w29 MESSAGE_EXPAND w11, w13, w3, w8, w16, w17 DATA_COMPRE_40_59 w21, w22, w23, w24, w20, w11, w19, w16, w17, w28, w29 /* 60~79 round data compression */ adrp x16, g_k add x16, x16, :lo12:g_k ldr w19, [x16, #12] // load k4 MESSAGE_EXPAND w12, w14, w4, w9, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w12, w19, w16, w17, w28, w29 MESSAGE_EXPAND w13, w15, w5, w10, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w13, w19, w16, w17, w28, w29 MESSAGE_EXPAND w14, w0, w6, w11, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w14, w19, w16, w17, w28, w29 MESSAGE_EXPAND w15, w1, w7, w12, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w15, w19, w16, w17, w28, w29 MESSAGE_EXPAND w0, w2, w8, w13, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w0, w19, w16, w17, w28, w29 MESSAGE_EXPAND w1, w3, w9, w14, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w1, w19, w16, w17, w28, w29 MESSAGE_EXPAND w2, w4, w10, w15, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w2, w19, w16, w17, w28, w29 MESSAGE_EXPAND w3, w5, w11, w0, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w3, w19, w16, w17, w28, w29 MESSAGE_EXPAND w4, w6, w12, w1, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w4, w19, w16, w17, w28, w29 MESSAGE_EXPAND w5, w7, w13, w2, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w5, w19, w16, w17, w28, w29 MESSAGE_EXPAND w6, w8, w14, w3, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w6, w19, w16, w17, w28, w29 MESSAGE_EXPAND w7, w9, w15, w4, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w7, w19, w16, w17, w28, w29 MESSAGE_EXPAND w8, w10, w0, w5, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w8, w19, w16, w17, w28, w29 MESSAGE_EXPAND w9, w11, w1, w6, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w9, w19, w16, w17, w28, w29 MESSAGE_EXPAND w10, w12, w2, w7, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w10, w19, w16, w17, w28, w29 MESSAGE_EXPAND w11, w13, w3, w8, w16, w17 DATA_COMPRE_20_39_60_79 w20, w21, w22, w23, w24, w11, w19, w16, w17, w28, w29 MESSAGE_EXPAND w12, w14, w4, w9, w16, w17 DATA_COMPRE_20_39_60_79 w24, w20, w21, w22, w23, w12, w19, w16, w17, w28, w29 MESSAGE_EXPAND w13, w15, w5, w10, w16, w17 DATA_COMPRE_20_39_60_79 w23, w24, w20, w21, w22, w13, w19, w16, w17, w28, w29 MESSAGE_EXPAND w14, w0, w6, w11, w16, w17 DATA_COMPRE_20_39_60_79 w22, w23, w24, w20, w21, w14, w19, w16, w17, w28, w29 MESSAGE_EXPAND w15, w1, w7, w12, w16, w17 DATA_COMPRE_20_39_60_79 w21, w22, w23, w24, w20, w15, w19, w16, w17, w28, w29 /* load a - e */ ldp w0, w1, [x30] ldp w2, w3, [x30, #4*2] ldr w4, [x30, #4*4] /* H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E */ add w20, w20, w0 add w21, w21, w1 add w22, w22, w2 add w23, w23, w3 add w24, w24, w4 stp w20, w21, [x30] stp w22, w23, [x30, #4*2] str w24, [x30, #4*4] cmp x26, #64 b.hs .Lloop_sha1_compress /* returns the address of the message for which SHA1 calculation is not performed. */ mov x0, x25 /* pop-stack */ ldp x19, x20, [sp, #8*2] ldp x21, x22, [sp, #8*4] ldp x23, x24, [sp, #8*6] ldp x25, x26, [sp, #8*8] ldp x27, x28, [sp, #8*10] ldp x29, x30, [sp], #96 .Lend_sha1: .inst 0xd50323bf // autiasp ret .size SHA1_Step, .-SHA1_Step /** * Function Description: Based on the input message, compress the SHA1 dedicated instruction and * update the hash value. * Function prototype: static const uint8_t *SHA1CryptoExt(const uint8_t *input, uint32_t len, uint32_t *h) * Input register: * x0: Pointer to the input data address * x1: Message length * x2: Storage address of the hash value * Register usage: v0–v3 stores k0–k3, s5 stores e temporarily, v6 stores abcd, and v7 stores e, * V23–V26 stores w0–w15 and recycles w16–w79. V19–v22 stores w+k calculation results. * V16 is used as the 0 register. v17 stores abcd and v18 stores e. v16 is used together with v6 and v7. * Output register: x0 returns the address of the message for which sha1 calculation is not performed. * Function/Macro Call: NONE */ .text .balign 16 .type SHA1CryptoExt, %function SHA1CryptoExt: /* load k */ adrp x3, g_kExt add x3, x3, :lo12:g_kExt ld1 {v0.4s-v3.4s}, [x3] /* load a - e */ ld1 {v17.4s}, [x2] ld1 {v6.4s}, [x2], #16 ld1 {v18.s}[0], [x2] ld1 {v7.s}[0], [x2] sub x2, x2, #16 eor v16.16b, v16.16b, v16.16b .Lloop_sha1_ext_compress: /* load w */ ld1 {v23.4s-v26.4s}, [x0], #64 sub x1, x1, #64 // update the remaining address length. /* little endian inversion */ #ifndef HITLS_BIG_ENDIAN rev32 v23.16b, v23.16b rev32 v24.16b, v24.16b rev32 v25.16b, v25.16b rev32 v26.16b, v26.16b #endif add v19.4s, v0.4s, v23.4s // k0+w[3:0] add v20.4s, v0.4s, v24.4s // k0+w[4:7] add v21.4s, v0.4s, v25.4s // k0+w[11:8] add v22.4s, v0.4s, v26.4s // k0+w[15:12] /* [0:16] data compression */ sha1su0 v23.4s, v24.4s, v25.4s // w[16:20] sha1h s5, s6 // a -> e sha1c q6, s7, v19.4s // a, b, c, d -> a, b, c, d sha1su1 v23.4s, v26.4s sha1su0 v24.4s, v25.4s, v26.4s sha1h s7, s6 sha1c q6, s5, v20.4s sha1su1 v24.4s, v23.4s sha1su0 v25.4s, v26.4s, v23.4s sha1h s5, s6 sha1c q6, s7, v21.4s sha1su1 v25.4s, v24.4s sha1su0 v26.4s, v23.4s, v24.4s sha1h s7, s6 sha1c q6, s5, v22.4s sha1su1 v26.4s, v25.4s add v19.4s, v0.4s, v23.4s // k0+w[19:16] add v20.4s, v1.4s, v24.4s // k1+w[23:20] add v21.4s, v1.4s, v25.4s // k1+w[27:24] add v22.4s, v1.4s, v26.4s // k1+w[31:28] /* [16:20] data compression */ sha1su0 v23.4s, v24.4s, v25.4s sha1h s5, s6 sha1c q6, s7, v19.4s sha1su1 v23.4s, v26.4s /* [20:40] data compression */ sha1su0 v24.4s, v25.4s, v26.4s sha1h s7, s6 sha1p q6, s5, v20.4s sha1su1 v24.4s, v23.4s sha1su0 v25.4s, v26.4s, v23.4s sha1h s5, s6 sha1p q6, s7, v21.4s sha1su1 v25.4s, v24.4s sha1su0 v26.4s, v23.4s, v24.4s sha1h s7, s6 sha1p q6, s5, v22.4s sha1su1 v26.4s, v25.4s add v19.4s, v1.4s, v23.4s // k1+w[35:32] add v20.4s, v1.4s, v24.4s // k1+w[39:36] add v21.4s, v2.4s, v25.4s // k2+w[43:40] add v22.4s, v2.4s, v26.4s // k2+w[47:44] sha1su0 v23.4s, v24.4s, v25.4s sha1h s5, s6 sha1p q6, s7, v19.4s sha1su1 v23.4s, v26.4s sha1su0 v24.4s, v25.4s, v26.4s sha1h s7, s6 sha1p q6, s5, v20.4s sha1su1 v24.4s, v23.4s /* [40:60] data compression */ sha1su0 v25.4s, v26.4s, v23.4s sha1h s5, s6 sha1m q6, s7, v21.4s sha1su1 v25.4s, v24.4s sha1su0 v26.4s, v23.4s, v24.4s sha1h s7, s6 sha1m q6, s5, v22.4s sha1su1 v26.4s, v25.4s add v19.4s, v2.4s, v23.4s // k2+w[51:48] add v20.4s, v2.4s, v24.4s // k2+w[55:52] add v21.4s, v2.4s, v25.4s // k2+w[59:56] add v22.4s, v3.4s, v26.4s // k3+w[63:60] sha1su0 v23.4s, v24.4s, v25.4s sha1h s5, s6 sha1m q6, s7, v19.4s sha1su1 v23.4s, v26.4s sha1su0 v24.4s, v25.4s, v26.4s sha1h s7, s6 sha1m q6, s5, v20.4s sha1su1 v24.4s, v23.4s sha1su0 v25.4s, v26.4s, v23.4s sha1h s5, s6 sha1m q6, s7, v21.4s sha1su1 v25.4s, v24.4s /* [60:80] data compression */ sha1su0 v26.4s, v23.4s, v24.4s sha1h s7, s6 sha1p q6, s5, v22.4s sha1su1 v26.4s, v25.4s add v19.4s, v3.4s, v23.4s // k3+w[67:64] add v20.4s, v3.4s, v24.4s // k3+w[71:68] add v21.4s, v3.4s, v25.4s // k3+w[75:72] add v22.4s, v3.4s, v26.4s // k3+w[79:76] sha1h s5, s6 sha1p q6, s7, v19.4s sha1h s7, s6 sha1p q6, s5, v20.4s sha1h s5, s6 sha1p q6, s7, v21.4s sha1h s7, s6 sha1p q6, s5, v22.4s /* calculate H0 H1 H2 H3 H4 */ add v17.4s, v17.4s, v6.4s add v18.4s, v18.4s, v7.4s add v6.4s, v17.4s, v16.4s add v7.4s, v18.4s, v16.4s cmp x1, #64 b.hs .Lloop_sha1_ext_compress st1 {v17.4s}, [x2], #16 st1 {v18.s}[0], [x2] ret .size SHA1CryptoExt, .-SHA1CryptoExt #endif
2301_79861745/bench_create
crypto/sha1/src/asm/sha1_armv8.S
Motorola 68K Assembly
unknown
24,861
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA1 .file "sha1_x86_64.S" .text .set INPUT, %rdi .set LEN, %rsi .set HASH, %rdx .set A, %r8d .set B, %r9d .set C, %r10d .set D, %r11d .set E, %r12d .set TEMP, %r13d .set TEMP1, %r15d .set TEMP2, %ebx .set TEMP3, %eax .set BLK0, %xmm0 .set BLK1, %xmm1 .set BLK2, %xmm2 .set BLK3, %xmm3 .set ZERO, %ymm4 .set EXPAND0, %ymm5 .set EXPAND1, %ymm6 .set EXPAND2, %ymm7 .set EXPAND3, %ymm8 .set TEMP_W0, %ymm9 .set TEMP_W1, %ymm10 .set TEMP_W2, %ymm11 .set KNUM, %ymm12 /* sha1 constant value used */ .section .rodata .balign 64 .type g_k, %object g_k: .long 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999 // K_00_19 .long 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1 // K_20_39 .long 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc // K_40_59 .long 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6 // K_60_79 .size g_k, .-g_k /* inverted mask */ .balign 64 .type endian_mask, %object endian_mask: .long 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f .long 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f .size endian_mask, .-endian_mask /** * Macro Description: Message compression, 0 to 18 rounds of data compression, pre-computation Next round F0, b * Input register: *a - e, temp: Intermediate variable of hash value * addr: Stack Address, Kt+W * wkOffset: Kt+W read Offset * temp1-2: temporary register * Modify the register: a e temp temp1 temp2 * Output register: * a: Next round F0 * e: Indicates the value after a cyclic update. * temp: Next round B * Macro implementation: F0(b,c,d) = (b AND c) OR ((NOT b) AND d) * =(((b) & (c)) | ((~(b)) & (d))) * e = S^5(a) + F0(b,c,d) + e + W(i) + K(i) * temp = S^30(b) */ .macro ROUND00_18 a, temp, b, c, d, e, addr, wkOffset, temp1, temp2 addl \wkOffset(\addr), \e // e = e + W + KT andn \c, \a, \temp1 // Next (~(b)) & (d) addl \temp, \e // e = F0(b, c, d) + e + W + KT rorxl $27, \a, \temp2 // Temp2 = ROTL32(a, 5) rorxl $2, \a, \temp // Next ROTL32(b, 30) and \b, \a // Next ((b) & (c)) addl \temp2, \e // e = F0(b, c, d) + e + W + KT + S^5(a) or \temp1, \a // Next (((b) & (c)) | ((~(b)) & (d))) .endm /** * Macro Description: 0 to 18 rounds of message compression and 16 to 31 message extension, * pre-calculation Next round F0, b * Input register: *a - e, temp: Intermediate variable of hash value * addr: Stack Address, Kt+W * wkOffset: Kt+W read offset * temp1-2: temporary register * wt_16_13: w(t-16) ~ w(t-13) * wt_12_9: w(t-12) ~ w(t-9) * wt_8_5: w(t-8) ~ w(t-5) * wt_4_1: w(t-4) ~ w(t-1) * expand0: w(t) ~ w(t+3) * tempw0-2: temporary register * zero: register with a value of zero * knum: k constant value * Modify the register: a b c d e temp temp1 temp2 expand0 tempw0 tempw1 tempw2 * Output register: * a: Third round B * b: Value after four rounds of cyclic update * c: Next round F0 * d: Next round B * e: Fourth round B * temp: next b * expand0: Value after a round of extension * Macro implementation: f(b,c,d) = (b AND c) OR ((NOT b) AND d) * =(((b) & (c)) | ((~(b)) & (d))) * temp = S^5(a) + f(b,c,d) + e + W(i) + K(i) * b = S^30(b) * W(t ) = ROL(W(t-3) ^ W(t-8) ^ W(t-14) ^ W(t-16), 1) * W(t+1) = ROL(W(t-2) ^ W(t-7) ^ W(t-13) ^ W(t-15), 1) * W(t+2) = ROL(W(t-1) ^ W(t-6) ^ W(t-12) ^ W(t-14), 1) * W(t+3) = ROL(0 ^ W(t-5) ^ W(t-11) ^ W(t-13), 1) * W(t+3) = W(t+3) ^ ROL(W(t), 1) */ .macro ROUND00_18_EXPAND a, temp, b, c, d, e, addr, wkOffset, wt_16_13, wt_12_9, wt_8_5, wt_4_1, expand0 vpalignr $8, \wt_16_13, \wt_12_9, TEMP_W1 // Expand w(t-14) w(t-13) w(t-12) w(t-11) addl \wkOffset(\addr), \e // e = e + W + KT andn \c, \a, TEMP1 // Next (~(b)) & (d) addl \temp, \e // e = F0 + e + W + KT vpalignr $4, \wt_4_1, ZERO, TEMP_W0 // Expand w(t-3) w(t-2) w(t-1) 0 vpxor \wt_8_5, \wt_16_13, \expand0 // Expand w(t-8) ^ w(t-16) rorxl $27, \a, TEMP2 // Temp2 = ROTL32(a, 5) rorxl $2, \a, \temp // Next ROTL32(b, 30) and \b, \a // Next ((b) & (c)) vpxor TEMP_W1, \expand0, \expand0 // Expand w(t-14) ^ w(t-8) ^ w(t-16) addl TEMP2, \e // e = F0 + e + W + KT + S^5(a) or TEMP1, \a // Next F0 done addl \wkOffset + 4(\addr), \d // Next d = d + W + KT vpxor TEMP_W0, \expand0, TEMP_W0 // Expand tempw0 = w[t:t+4] before rol 1 andn \b, \e, TEMP1 // Next F0 addl \a, \d // d = F0 + d + W + KT rorxl $27, \e, TEMP2 // Temp2 = ROTL32(E, 5) rorxl $2, \e, \a // next ROTL32(E, 30) vpalignr $4, ZERO, TEMP_W0, TEMP_W1 // Expand tempw1 = 0 0 0 w(t) and \temp, \e // Next F0 addl TEMP2, \d // d = F0 + d + W + KT + S^5(E) or TEMP1, \e // Next F0 done vpsrld $31, TEMP_W0, \expand0 // Expand ROL(w(t), w(t+1), w(t+2), w(t+3),1) addl \wkOffset + 8(\addr), \c // c = c + W + KT vpaddd TEMP_W0, TEMP_W0, TEMP_W0 // Expand ROL(w(t), w(t+1), w(t+2), w(t+3),1) andn \temp, \d, TEMP1 // Next F0 addl \e, \c // c = F0 + c + W + KT rorxl $27, \d, TEMP2 // Temp2 = ROTL32(D, 5) rorxl $2, \d, \e // Next ROTL32(D, 30) vpsrld $30, TEMP_W1, TEMP_W2 // Expand ROL(w(t), 2) and \a, \d // Next F0 addl TEMP2, \c // c = F0 + c + W + KT + S^5(D) or TEMP1, \d // Next F0 done vpslld $2, TEMP_W1, TEMP_W1 // Expand ROL(w(t), 2) vpxor \expand0, TEMP_W0, \expand0 // Expand ROL(w(t), w(t+1), w(t+2), w(t+3),1) addl \wkOffset + 12(\addr), \b // b = b + W + KT andn \a, \c, TEMP1 // Next F0 vpxor TEMP_W2, TEMP_W1, TEMP_W0 // Expand ROL(w(t), 2) addl \d, \b // b = F0 + b + W + KT rorxl $27, \c, TEMP2 // Temp2 = ROTL32(C, 5) rorxl $2, \c, \d // Next ROTL32(C, 30) vpxor \expand0, TEMP_W0, \expand0 // Expand w[t:t+4] and \e, \c // Next F0 addl TEMP2, \b // b = F0 + b + W + KT + S^5(C) vpaddd KNUM,\expand0, TEMP_W0 // Expand w + k or TEMP1, \c // Next F0 done vmovdqa TEMP_W0, \wkOffset + 128(\addr) .endm /** * Macro Description: Message compression, 20~39, 60~79 round data compression, precomputation Next round F1, b * Input register: *a - e, temp: Intermediate variable of hash value * addr: Stack Address, Kt+W * wkOffset: Kt+W read offset * temp1-2: temporary register * Modify the register: a e temp temp1 temp2 * Output register: * a: Next round F1 * e: Indicates the value after a cyclic update. * temp: Next round B * Macro implementation: F1(b,c,d) = b XOR c XOR d * =(((b) ^ (c)) ^ (d)) * e = S^5(a) + F1(b,c,d) + e + W(i) + K(i) * temp = S^30(b) */ .macro ROUND20_39 a, temp, b, c, d, e, addr, wkOffset, temp1, temp2 addl \wkOffset(\addr), \e // e = e + W + KT addl \temp, \e // e = F1(b, c, d) + e + W + KT rorx $27, \a, TEMP2 // Temp2 = ROTL32(a, 5) rorx $2, \a, \temp // Next ROTL32(b, 30) xor \b, \a // Next (b) ^ (c) addl TEMP2, \e // e = F0(b, c, d) + e + W + KT + S^5(a) xor \c, \a // Next (b) ^ (c) ^ (d) .endm /** * Macro Description: 20~39, 60~79 round data compression, and 16-31 message extension, precomputation Next round F1, b * Input register: *a - e, temp: Intermediate variable of hash value * addr: Stack Address, Kt+W * wkOffset: Kt+W read offset * temp1-2: temporary register * wt_32_29: w(t-32) ~ w(t-29) * wt_28_25: w(t-28) ~ w(t-25) * wt_8_5: w(t-8) ~ w(t-5) * wt_4_1: w(t-4) ~ w(t-1) * expand0: w(t) ~ w(t+3) * zero: register with a value of zero * knum: k constant value * Modify the register: a b c d e temp temp1 temp2 wt_32_29 tempw0 * Output register: * a: Third round B value * b: Value after four rounds of cyclic update * c: Next round F1 * d: Next round B * e: Fourth round B value * temp: next b * expand0: Value after a round of extension * Macro implementation: F1(b,c,d) = b XOR c XOR d * =(((b) ^ (c)) ^ (d)) * e = S^5(a) + F1(b,c,d) + e + W(i) + K(i) * temp = S^30(b) * w(t) = ROL(w(t-3) ^ w(t-8) ^ w(t-14) ^ w(t-16), 1) * = ROL(w(t-6) ^ w(t-11) ^ w(t-17) ^ w(t-19) ^ * w(t-11) ^ w(t-16) ^ w(t-22) ^ w(t-24) ^ * w(t-17) ^ w(t-22) ^ w(t-28) ^ w(t-30) ^ * w(t-19) ^ w(t-24) ^ w(t-30) ^ w(t-32), 2) * = ROL(w(t-6) ^ w(t-16) ^ w(t-28) ^ w(t-32), 2) * w(t+1), w(t+2), w(t+3) in the same way */ .macro ROUND20_39_EXPAND a, temp, b, c, d, e, addr, wkOffset, wt_32_29, wt_28_25, wt_16_13, wt_8_5, wt_4_1, wkOffset2 vpalignr $8, \wt_8_5, \wt_4_1, TEMP_W0 // Expand w(t-6), w(t-5), w(t-4), w(t-3) vpxor \wt_32_29, \wt_16_13, \wt_32_29 // Expand wt_32_29 =w[t-32:t-28] ^ w[t-16:t-12] addl \wkOffset(\addr), \e // e = e + W + KT addl \temp, \e // e = F1(b, c, d) + e + W + KT rorx $27, \a, TEMP2 // temp2 = ROTL32(a, 5) rorx $2, \a, \temp // Next ROTL32(b, 30) vpxor \wt_32_29, \wt_28_25, \wt_32_29 // Expand wt_32_29 =w[t-32:t-28] ^ w[t-16:t-12]^ w[t-28:t-24] xor \b, \a // Next (b) ^ (c) addl TEMP2, \e // e = F0(b, c, d) + e + W + KT + S^5(a) xor \c, \a // Next F1 done addl \wkOffset + 4(\addr), \d // d = d + W + KT vpxor \wt_32_29, TEMP_W0, \wt_32_29 // Expand wt_32_29 =w[t-32] ^ w[t-16]^ w[t-28]^ w[t-6] addl \a, \d // d = F1 + d + W + KT rorx $27, \e, TEMP2 // Temp2 = ROTL32(e, 5) rorx $2, \e, \a // Next temp = ROTL32(e, 30) xor \temp, \e // Next F1 addl TEMP2, \d // Expand d = F1 + d + W + KT + S^5(e) vpsrld $30, \wt_32_29, TEMP_W0 // Expand ROL(wt_32_29,2) xor \b, \e // Next F1 done addl \wkOffset + 8(\addr), \c // c = c + W + KT addl \e, \c // c = F1 + c + W + KT rorx $27, \d, TEMP2 // Temp2 = ROTL32(e, 5) rorx $2, \d, \e // Next ROTL32(e, 30) vpslld $2, \wt_32_29, \wt_32_29 xor \a, \d // Next F1 addl TEMP2, \c // c = F1 + c + W + KT + S^5(e) xor \temp, \d // Next F1 done addl \wkOffset + 12(\addr), \b // b = b + W + KT vpxor \wt_32_29, TEMP_W0, \wt_32_29 // Expand ROL(wt_32_29,2) rorx $27, \c, TEMP2 // Temp2 = ROTL32(c, 5) addl \d, \b // b = F1 + b + W + KT rorx $2, \c, \d // Next ROTL32(c, 30) vpaddd KNUM, \wt_32_29, TEMP_W0 xor \e, \c // Next F1 addl TEMP2, \b // b = F1 + b + W + KT + S^5(c) xor \a, \c // Next F1 done vmovdqa TEMP_W0, \wkOffset2(\addr) .endm /** * Macro Description: Message compression, 40~59 round data compression, pre-computation Next round F2, b * Input register: *a - e, temp: Intermediate variable of hash value * addr: Stack Address, Kt+W * wkOffset: Kt+W read offset * temp1-2: temporary register * Modify the register: a e temp temp1 temp2 * Output register: * a: Next round F1 * e: Indicates the value after a cyclic update. * temp: Next round B * Macro implementation: F1(b,c,d) = (b AND c) OR (b AND d) OR (c AND d) * =((b^c) & (c^d) ^ c) * e = S^5(a) + F1(b,c,d) + e + W(i) + K(i) * temp = S^30(b) */ .macro ROUND40_59 a, temp, b, c, d, e, addr, wkOffset, temp1, temp2 addl \wkOffset(\addr), \e // e = e + W + KT mov \c, \temp1 addl \temp, \e // e = F2(b, c, d) + e + W + KT xor \b, \temp1 // Next (c^d) rorx $27, \a, \temp2 // Temp2 = ROTL32(a, 5) rorx $2, \a, \temp // Next ROTL32(b, 30) xor \b, \a // Next (b^c) addl \temp2, \e // e = F0(b, c, d) + e + W + KT + S^5(a) and \temp1, \a // Next (b^c) & (c^d) xor \b, \a // Next (((b^c)) & (c^d) ^ c) .endm /** * Macro Description: 40~59 round data compression, and 32 to 79 rounds of message extension, * precomputation Next round F2, b * Input register: *a - e, temp: Intermediate variable of hash value * addr: Stack Address, Kt+W * wkOffset: Kt+W read offset * temp1-2: temporary register * wt_32_29: w(t-32) ~ w(t-29) * wt_28_25: w(t-28) ~ w(t-25) * wt_8_5: w(t-8) ~ w(t-5) * wt_4_1: w(t-4) ~ w(t-1) * expand0: w(t) ~ w(t+3) * zero: register with a value of zero * knum: k constant value * Modify the register: a b c d e temp temp1 temp2 wt_32_29 tempw0 * Output register: * a: Third round B value * b: Value after four rounds of cyclic update * c: Next round F1 * d: Next round B * e: Fourth round B value * temp: next b * expand0: Value after a round of extension * Macro implementation: F1(b,c,d) = (b AND c) OR (b AND d) OR (c AND d) * =((b^c) & (c^d) ^ c) * e = S^5(a) + F1(b,c,d) + e + W(i) + K(i) * w(t) = ROL(w(t-3) ^ w(t-8) ^ w(t-14) ^ w(t-16), 1) * = ROL(w(t-6) ^ w(t-11) ^ w(t-17) ^ w(t-19) ^ * w(t-11) ^ w(t-16) ^ w(t-22) ^ w(t-24) ^ * w(t-17) ^ w(t-22) ^ w(t-28) ^ w(t-30) ^ * w(t-19) ^ w(t-24) ^ w(t-30) ^ w(t-32), 2) * = ROL(w(t-6) ^ w(t-16) ^ w(t-28) ^ w(t-32), 2) * w(t+1), w(t+2), w(t+3) in the same way */ .macro ROUND40_59_EXPAND a, temp, b, c, d, e, addr, wkOffset, wt_32_29, wt_28_25, wt_16_13, wt_8_5, wt_4_1, wkOffset2 vpalignr $8, \wt_8_5, \wt_4_1, TEMP_W0 // Expand w(t-6), w(t-5), w(t-4), w(t-3) vpxor \wt_32_29, \wt_16_13, \wt_32_29 // Expand wt_32_29 =w[t-32:t-28] ^ w[t-16:t-12] addl \wkOffset(\addr), \e // e = e + W + KT mov \c, TEMP1 addl \temp, \e // e = F2(b, c, d) + e + W + KT xor \b, TEMP1 // Next temp1 = (c^d) rorx $27, \a, TEMP2 // Temp2 = ROTL32(a, 5) rorx $2, \a, \temp // Next ROTL32(b, 30) vpxor \wt_32_29, \wt_28_25, \wt_32_29 // Expand wt_32_29 =w[t-32:t-28] ^ w[t-16:t-12]^ w[t-28:t-24] xor \b, \a // Next (b^c) addl TEMP2, \e // e = F0(b, c, d) + e + W + KT + S^5(a) and TEMP1, \a // Next (b^c) & (c^d) addl \wkOffset + 4(\addr), \d // d = d + W + KT xor \b, \a // Next (((b^c)) & (c^d) ^ c) vpxor \wt_32_29, TEMP_W0, \wt_32_29 // Expand wt_32_29 =w[t-32] ^ w[t-16]^ w[t-28]^ w[t-6] mov \b, TEMP1 addl \a, \d // d = F2 + d + W + KT xor \temp, TEMP1 // Next F2 rorx $27, \e, TEMP2 // Temp2 = ROTL32(e, 5) rorx $2, \e, \a // Next ROTL32(e, 30) addl \wkOffset + 8(\addr), \c // c = c + W + KT xor \temp, \e // Next F2 vpsrld $30, \wt_32_29, TEMP_W0 // Expand ROL(wt_32_29,2) and TEMP1, \e // Next F2 addl TEMP2, \d // d = F2 + d + W + KT + S^5(e) xor \temp, \e // Next F2 done mov \temp, TEMP1 addl \e, \c // c = F2 + c + W + KT xor \a, TEMP1 // Next F2 vpslld $2, \wt_32_29, \wt_32_29 rorx $27, \d, TEMP2 // Temp2 = ROTL32(d, 5) rorx $2, \d, \e // Next ROTL32(d, 30) xor \a, \d // Next F2 addl TEMP2, \c // c = F2 + c + W + KT + S^5(d) and TEMP1, \d // Next F2 addl \wkOffset + 12(\addr), \b // b = b + W + KT vpxor \wt_32_29, TEMP_W0, \wt_32_29 // Expand ROL(wt_32_29,2) xor \a, \d // Next F2 done mov \a, TEMP1 addl \d, \b // b = F2 + b + W + KT xor \e, TEMP1 // Next F2 rorx $27, \c, TEMP2 // Temp2 = ROTL32(c, 5) rorx $2, \c, \d // Next ROTL32(c, 30) xor \e, \c // Next F2 vpaddd KNUM, \wt_32_29, TEMP_W0 addl TEMP2, \b // b = F2 + b + W + KT + S^5(c) and TEMP1, \c // Next F2 xor \e, \c // Next F2 done vmovdqa TEMP_W0, \wkOffset2(\addr) .endm /** * Function Description: Perform SHA1 compression calculation based on the input message and update the hash value. * Function prototype: static const uint8_t *SHA1_Step(const uint8_t *input, uint32_t len, uint32_t *h) * Input register: * rdi: Pointer to the input data address * rsi: Message length * rdx: Storage address of the hash value * Register usage: r8~r12: A~E, r13: TEMP, r15, ebx, eax: temporary register, ymm0~ymm3: w0~w15 Message block, * ymm4: 0, ymm5~ymm8: extended message block, ymm9~ymm13: temporary register, ymm13: k+w value * Output register: rax Returns the address of the message for which SHA1 calculation is not performed. * Function/Macro Call: ROUND00_18, ROUND00_18_EXPAND, ROUND20_39, ROUND20_39_EXPAND, ROUND40_59, ROUND40_59_EXPAND */ .text .globl SHA1_Step .type SHA1_Step, @function SHA1_Step: .cfi_startproc cmp $64, LEN jb .Lend_sha1 push %rbx push %rbp push %r12 push %r13 push %r14 push %r15 mov %rsp, %r14 lea -1024(%rsp), %rsp // Apply for 1024-byte stack space. mov 0(HASH), A // r8~r13: a~e mov 4(HASH), B andq $-256, %rsp mov 8(HASH), C mov 12(HASH), D mov 16(HASH), E .Lloop_sha1_compress: .align 16 vmovdqu (INPUT), BLK0 // Loads the data of a block to the lower 128 bits // of the YMM register. vmovdqu 16(INPUT), BLK1 vmovdqu 32(INPUT), BLK2 sub $64, LEN vmovdqu 48(INPUT), BLK3 add $64, INPUT cmp $64, LEN // Check whether the remaining length is greater than 64. jb .Lsha1_compress vinserti128 $1, 0(INPUT), %ymm0, %ymm0 // Loads the data of a block to the upper 128 bits // of the ymm register. vinserti128 $1, 16(INPUT), %ymm1, %ymm1 vinserti128 $1, 32(INPUT), %ymm2, %ymm2 vinserti128 $1, 48(INPUT), %ymm3, %ymm3 add $64, INPUT .Lsha1_compress: vmovdqa endian_mask + 0(%rip), %ymm8 // Endian inversion mask leaq g_k + 0(%rip), %rbp // Get k vpshufb %ymm8, %ymm0, %ymm0 // Little endian to big endian vmovdqa 0(%rbp), KNUM vpshufb %ymm8, %ymm1, %ymm1 vpaddd KNUM, %ymm0, %ymm13 // w[0:15] + k0 vpshufb %ymm8, %ymm2, %ymm2 vmovdqa %ymm13, 0(%rsp) // wk push stack vpaddd KNUM, %ymm1, %ymm9 vpshufb %ymm8, %ymm3, %ymm3 vmovdqa %ymm9, 32(%rsp) vpaddd KNUM, %ymm2, %ymm10 vpxor %ymm4, %ymm4, %ymm4 mov C, TEMP // The first round F0 vmovdqa %ymm10, 64(%rsp) and B, TEMP // Round0 ((b) & (c)) andn D, B, TEMP2 // Round0 (~(b)) & (d) vpaddd KNUM, %ymm3, %ymm11 or TEMP2, TEMP // Round0 (((b) & (c)) | ((~(b)) & (d))) rol $30, B // Round0 B = ROTL32(B, 30) vmovdqa %ymm11, 96(%rsp) ROUND00_18_EXPAND A, TEMP, B, C, D, E, %rsp, 0, %ymm0, %ymm1, %ymm2, %ymm3, EXPAND0 vmovdqa 32(%rbp), KNUM ROUND00_18_EXPAND B, C, D, E, A, TEMP, %rsp, 32, %ymm1, %ymm2, %ymm3, EXPAND0, EXPAND1 ROUND00_18_EXPAND D, E, A, TEMP, B, C, %rsp, 64, %ymm2, %ymm3, EXPAND0, EXPAND1, EXPAND2 ROUND00_18_EXPAND A, TEMP, B, C, D, E, %rsp, 96, %ymm3, EXPAND0, EXPAND1, EXPAND2, EXPAND3 ROUND00_18 B, C, D, E, A, TEMP, %rsp, 128, TEMP1, TEMP2 ROUND00_18 TEMP, B, C, D, E, A, %rsp, 132, TEMP1, TEMP2 ROUND00_18 A, TEMP, B, C, D, E, %rsp, 136, TEMP1, TEMP2 // 18 addl 140( %rsp), D // D = DE + W + KT rorx $27, E, TEMP2 // TEMP2 = ROTL32(E, 5) addl A, D // D = F0 + D + W + KT rorx $2, E, A // Round20 ROTL32(E, 30) xor TEMP, E // Round20 (TEMP) ^ (E) addl TEMP2, D // D = F0 + D + W + KT + S^5(E) xor B, E // Round20 F1 ROUND20_39_EXPAND D, E, A, TEMP, B, C, %rsp, 160, %ymm0, %ymm1, EXPAND0, EXPAND2, EXPAND3, 256 ROUND20_39_EXPAND A, TEMP, B, C, D, E, %rsp, 192, %ymm1, %ymm2, EXPAND1, EXPAND3, %ymm0, 288 vmovdqa 64(%rbp), KNUM ROUND20_39_EXPAND B, C, D, E, A, TEMP, %rsp, 224, %ymm2, %ymm3, EXPAND2, %ymm0, %ymm1, 320 ROUND20_39_EXPAND D, E, A, TEMP, B, C, %rsp, 256, %ymm3, EXPAND0, EXPAND3, %ymm1, %ymm2, 352 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 288, TEMP1, TEMP2 ROUND20_39 E, A, TEMP, B, C, D, %rsp, 292, TEMP1, TEMP2 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 296, TEMP1, TEMP2 // 38 addl 300(%rsp), B // B = B + W + KT mov A, TEMP1 addl D, B // B = F1 + B + W + KT xor E, TEMP1 // Round40 (E^A) rorx $27, C, TEMP2 // TEMP2 = ROTL32(C, 5) rorx $2, C, D // Round40 ROTL32(C, 30) xor E, C // Round40 (E^C) addl TEMP2, B // B = F1 + B + W + KT + S^5(C) and TEMP1, C // Round40 (E^A) & (E^C) xor E, C // Round40 F2 ROUND40_59_EXPAND B, C, D, E, A, TEMP, %rsp, 320, EXPAND0, EXPAND1, %ymm0, %ymm2, %ymm3, 384 ROUND40_59_EXPAND D, E, A, TEMP, B, C, %rsp, 352, EXPAND1, EXPAND2, %ymm1, %ymm3, EXPAND0, 416 ROUND40_59_EXPAND A, TEMP, B, C, D, E, %rsp, 384, EXPAND2, EXPAND3, %ymm2, EXPAND0, EXPAND1, 448 vmovdqa 96(%rbp), KNUM ROUND40_59_EXPAND B, C, D, E, A, TEMP, %rsp, 416, EXPAND3, %ymm0, %ymm3, EXPAND1, EXPAND2, 480 ROUND40_59 D, E, A, TEMP, B, C, %rsp, 448, TEMP1, TEMP2 ROUND40_59 C, D, E, A, TEMP, B, %rsp, 452, TEMP1, TEMP2 ROUND40_59 B, C, D, E, A, TEMP, %rsp, 456, TEMP1, TEMP2 // 58 addl 460(%rsp), A // A = A + W + KT rorx $27, TEMP, TEMP2 // TEMP2 = ROTL32(TEMP, 5) addl B, A // A = F2 + A + W + KT rorx $2, TEMP, B // Round60 ROTL32(TEMP, 30) xor C, TEMP // Round60 (C) ^ (TEMP) addl TEMP2, A // A = F2 + A + W + KT + S^5(TEMP) xor D, TEMP // Round60 F0 ROUND20_39_EXPAND A, TEMP, B, C, D, E, %rsp, 480, %ymm0, %ymm1, EXPAND0, EXPAND2, EXPAND3, 512 ROUND20_39_EXPAND B, C, D, E, A, TEMP, %rsp, 512, %ymm1, %ymm2, EXPAND1, EXPAND3, %ymm0, 544 ROUND20_39_EXPAND D, E, A, TEMP, B, C, %rsp, 544, %ymm2, %ymm3, EXPAND2, %ymm0, %ymm1, 576 ROUND20_39_EXPAND A, TEMP, B, C, D, E, %rsp, 576, %ymm3, EXPAND0, EXPAND3, %ymm1, %ymm2, 608 ROUND20_39 B, C, D, E, A, TEMP, %rsp, 608, TEMP1, TEMP2 ROUND20_39 TEMP, B, C, D, E, A, %rsp, 612, TEMP1, TEMP2 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 616, TEMP1, TEMP2 // 78 addl 620(%rsp), D // D = D + W + KT add E, 4(HASH) // Update HASH lea (A, D), D // D = F1 + D + W + KT add TEMP, 8(HASH) rorx $27, E, TEMP2 // TEMP2 = ROTL32(E, 5) add B, 12(HASH) addl TEMP2, D // D = F1 + D + W + KT + S^5(E) add C, 16(HASH) mov 4(HASH), B add D, 0(HASH) mov 8(HASH), C mov 16(HASH), E mov 12(HASH), D mov 0(HASH), A cmp $64, LEN // Check whether the upper-bit register is calculated. jb .Lend_sha1_pre sub $64, LEN mov C, TEMP andn D, B, TEMP2 // TEMP2 = (~(b)) & (d) and B, TEMP // TEMP=((b) & (c)) or TEMP2, TEMP // TEMP = (((b) & (c)) | ((~(b)) & (d))) rol $30, B // B = ROTL32(B, 30) ROUND00_18 A, TEMP, B, C, D, E, %rsp, 16, TEMP1, TEMP2 ROUND00_18 E, A, TEMP, B, C, D, %rsp, 20, TEMP1, TEMP2 ROUND00_18 D, E, A, TEMP, B, C, %rsp, 24, TEMP1, TEMP2 ROUND00_18 C, D, E, A, TEMP, B, %rsp, 28, TEMP1, TEMP2 // Round 3 ROUND00_18 B, C, D, E, A, TEMP, %rsp, 48, TEMP1, TEMP2 ROUND00_18 TEMP, B, C, D, E, A, %rsp, 52, TEMP1, TEMP2 ROUND00_18 A, TEMP, B, C, D, E, %rsp, 56, TEMP1, TEMP2 ROUND00_18 E, A, TEMP, B, C, D, %rsp, 60, TEMP1, TEMP2 // Round 7 ROUND00_18 D, E, A, TEMP, B, C, %rsp, 80, TEMP1, TEMP2 ROUND00_18 C, D, E, A, TEMP, B, %rsp, 84, TEMP1, TEMP2 ROUND00_18 B, C, D, E, A, TEMP, %rsp, 88, TEMP1, TEMP2 ROUND00_18 TEMP, B, C, D, E, A, %rsp, 92, TEMP1, TEMP2 // Round 11 ROUND00_18 A, TEMP, B, C, D, E, %rsp, 112, TEMP1, TEMP2 ROUND00_18 E, A, TEMP, B, C, D, %rsp, 116, TEMP1, TEMP2 ROUND00_18 D, E, A, TEMP, B, C, %rsp, 120, TEMP1, TEMP2 ROUND00_18 C, D, E, A, TEMP, B, %rsp, 124, TEMP1, TEMP2 // Round 15 ROUND00_18 B, C, D, E, A, TEMP, %rsp, 144, TEMP1, TEMP2 ROUND00_18 TEMP, B, C, D, E, A, %rsp, 148, TEMP1, TEMP2 ROUND00_18 A, TEMP, B, C, D, E, %rsp, 152, TEMP1, TEMP2 // Round 18 addl 156( %rsp), D // D = D + W + KT rorx $27, E, TEMP2 // TEMP2 = ROTL32(E, 5) addl A, D // D = F0 + D + W + KT rorx $2, E, A // Round20 ROTL32(E, 30) xor TEMP, E // Round20 (TEMP) ^ (E) addl TEMP2, D // D = F0 + D + W + KT + S^5(E) xor B, E // Round20 F1 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 176, TEMP1, TEMP2 ROUND20_39 C, D, E, A, TEMP, B, %rsp, 180, TEMP1, TEMP2 ROUND20_39 B, C, D, E, A, TEMP, %rsp, 184, TEMP1, TEMP2 ROUND20_39 TEMP, B, C, D, E, A, %rsp, 188, TEMP1, TEMP2 // Round 23 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 208, TEMP1, TEMP2 ROUND20_39 E, A, TEMP, B, C, D, %rsp, 212, TEMP1, TEMP2 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 216, TEMP1, TEMP2 ROUND20_39 C, D, E, A, TEMP, B, %rsp, 220, TEMP1, TEMP2 // Round 27 ROUND20_39 B, C, D, E, A, TEMP, %rsp, 240, TEMP1, TEMP2 ROUND20_39 TEMP, B, C, D, E, A, %rsp, 244, TEMP1, TEMP2 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 248, TEMP1, TEMP2 ROUND20_39 E, A, TEMP, B, C, D, %rsp, 252, TEMP1, TEMP2 // Round 31 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 272, TEMP1, TEMP2 ROUND20_39 C, D, E, A, TEMP, B, %rsp, 276, TEMP1, TEMP2 ROUND20_39 B, C, D, E, A, TEMP, %rsp, 280, TEMP1, TEMP2 ROUND20_39 TEMP, B, C, D, E, A, %rsp, 284, TEMP1, TEMP2 // Round 35 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 304, TEMP1, TEMP2 ROUND20_39 E, A, TEMP, B, C, D, %rsp, 308, TEMP1, TEMP2 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 312, TEMP1, TEMP2 // Round 38 addl 316(%rsp), B // B = B + W + KT mov A, TEMP1 addl D, B // B = F1 + B + W + KT xor E, TEMP1 // Round40 (A^E) rorx $2, C, D // Round40 ROTL32(C, 30) rorx $27, C, TEMP2 // TEMP2 = ROTL32(C, 5) xor E, C // Round40 (E^C) addl TEMP2, B // B = F1 + B + W + KT + S^5(C) and TEMP1, C // Round40 (A^E) & (E^C) xor E, C // Round40 F2 ROUND40_59 B, C, D, E, A, TEMP, %rsp, 336, TEMP1, TEMP2 ROUND40_59 TEMP, B, C, D, E, A, %rsp, 340, TEMP1, TEMP2 ROUND40_59 A, TEMP, B, C, D, E, %rsp, 344, TEMP1, TEMP2 ROUND40_59 E, A, TEMP, B, C, D, %rsp, 348, TEMP1, TEMP2 // Round 43 ROUND40_59 D, E, A, TEMP, B, C, %rsp, 368, TEMP1, TEMP2 ROUND40_59 C, D, E, A, TEMP, B, %rsp, 372, TEMP1, TEMP2 ROUND40_59 B, C, D, E, A, TEMP, %rsp, 376, TEMP1, TEMP2 ROUND40_59 TEMP, B, C, D, E, A, %rsp, 380, TEMP1, TEMP2 // Round 47 ROUND40_59 A, TEMP, B, C, D, E, %rsp, 400, TEMP1, TEMP2 ROUND40_59 E, A, TEMP, B, C, D, %rsp, 404, TEMP1, TEMP2 ROUND40_59 D, E, A, TEMP, B, C, %rsp, 408, TEMP1, TEMP2 ROUND40_59 C, D, E, A, TEMP, B, %rsp, 412, TEMP1, TEMP2 // Round 51 ROUND40_59 B, C, D, E, A, TEMP, %rsp, 432, TEMP1, TEMP2 ROUND40_59 TEMP, B, C, D, E, A, %rsp, 436, TEMP1, TEMP2 ROUND40_59 A, TEMP, B, C, D, E, %rsp, 440, TEMP1, TEMP2 ROUND40_59 E, A, TEMP, B, C, D, %rsp, 444, TEMP1, TEMP2 // Round 55 ROUND40_59 D, E, A, TEMP, B, C, %rsp, 464, TEMP1, TEMP2 ROUND40_59 C, D, E, A, TEMP, B, %rsp, 468, TEMP1, TEMP2 ROUND40_59 B, C, D, E, A, TEMP, %rsp, 472, TEMP1, TEMP2 // Round 58 addl 476(%rsp), A // A = A + W + KT rorx $27, TEMP, TEMP2 // TEMP2 = ROTL32(TEMP, 5) addl B, A // A = F2 + A + W + KT rorx $2, TEMP, B // Round60 ROTL32(TEMP, 30) xor C, TEMP // Round60 (TEMP) ^ (c) addl TEMP2, A // A = F2 + A + W + KT + S^5(TEMP) xor D, TEMP // Round60 F1 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 496, TEMP1, TEMP2 ROUND20_39 E, A, TEMP, B, C, D, %rsp, 500, TEMP1, TEMP2 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 504, TEMP1, TEMP2 ROUND20_39 C, D, E, A, TEMP, B, %rsp, 508, TEMP1, TEMP2 // Round 63 ROUND20_39 B, C, D, E, A, TEMP, %rsp, 528, TEMP1, TEMP2 ROUND20_39 TEMP, B, C, D, E, A, %rsp, 532, TEMP1, TEMP2 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 536, TEMP1, TEMP2 ROUND20_39 E, A, TEMP, B, C, D, %rsp, 540, TEMP1, TEMP2 // Round 67 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 560, TEMP1, TEMP2 ROUND20_39 C, D, E, A, TEMP, B, %rsp, 564, TEMP1, TEMP2 ROUND20_39 B, C, D, E, A, TEMP, %rsp, 568, TEMP1, TEMP2 ROUND20_39 TEMP, B, C, D, E, A, %rsp, 572, TEMP1, TEMP2 // Round 71 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 592, TEMP1, TEMP2 ROUND20_39 E, A, TEMP, B, C, D, %rsp, 596, TEMP1, TEMP2 ROUND20_39 D, E, A, TEMP, B, C, %rsp, 600, TEMP1, TEMP2 ROUND20_39 C, D, E, A, TEMP, B, %rsp, 604, TEMP1, TEMP2 // Round 75 ROUND20_39 B, C, D, E, A, TEMP, %rsp, 624, TEMP1, TEMP2 ROUND20_39 TEMP, B, C, D, E, A, %rsp, 628, TEMP1, TEMP2 ROUND20_39 A, TEMP, B, C, D, E, %rsp, 632, TEMP1, TEMP2 // Round 78 addl 636(%rsp), D // D = D + W + KT add E, 4(HASH) // Update HASH add TEMP, 8(HASH) // Upadate H0~H5 lea (A, D), D // D = F1 + D + W + KT rorx $27, E, TEMP2 // TEMP2 = ROTL32(E, 5) add B, 12(HASH) add C, 16(HASH) addl TEMP2, D // D = F1 + D + W + KT + S^5(E) mov 4(HASH), B mov 8(HASH), C add D, 0(HASH) mov 16(HASH), E mov 12(HASH), D mov 0(HASH), A cmp $64, LEN jae .Lloop_sha1_compress .Lend_sha1_pre: mov %r14, %rsp pop %r15 pop %r14 pop %r13 pop %r12 pop %rbp pop %rbx .Lend_sha1: mov INPUT, %rax ret .cfi_endproc .size SHA1_Step, .-SHA1_Step #endif
2301_79861745/bench_create
crypto/sha1/src/asm/sha1_x86_64.S
Motorola 68K Assembly
unknown
38,616
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_SHA1) && !defined(HITLS_CRYPTO_SHA1_SMALL_MEM) #include <stdlib.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "crypt_sha1.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ /* e767 is because H is defined in SHA1 and MD5. But the both the macros are different. So masked this error */ #define K0 0x5A827999 #define K1 0x6ED9EBA1 #define K2 0x8F1BBCDC #define K3 0xCA62C1D6 #define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d))) #define F1(b, c, d) (((b) ^ (c)) ^ (d)) #define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) #define F3(b, c, d) (((b) ^ (c)) ^ (d)) #define ROUND00_15(s, a, b, c, d, e, temp, w, Kt) \ do { \ (temp) = ROTL32(a, 5) + F##Kt(b, c, d) + (e) + (w)[s] + K##Kt; \ (b) = ROTL32(b, 30); \ } while (0) #define ROUND16_79(t, a, b, c, d, e, temp, w, Kt) \ do { \ (w)[(t) & 0xF] = ROTL32( \ (w)[((t) + 13) & 0xF] ^ (w)[((t) + 8) & 0xF] ^ (w)[((t) + 2) & 0xF] ^ (w)[(t) & 0xF], 1); \ ROUND00_15((t) & 0xF, a, b, c, d, e, temp, w, Kt); \ } while (0) const uint8_t *SHA1_Step(const uint8_t *input, uint32_t len, uint32_t *h) { uint32_t temp; uint32_t w[16]; const uint8_t *data = input; uint32_t dataLen = len; while (dataLen >= CRYPT_SHA1_BLOCKSIZE) { /* Convert data into 32 bits for calculation. */ w[0] = GET_UINT32_BE(data, 0); w[1] = GET_UINT32_BE(data, 4); w[2] = GET_UINT32_BE(data, 8); w[3] = GET_UINT32_BE(data, 12); w[4] = GET_UINT32_BE(data, 16); w[5] = GET_UINT32_BE(data, 20); w[6] = GET_UINT32_BE(data, 24); w[7] = GET_UINT32_BE(data, 28); w[8] = GET_UINT32_BE(data, 32); w[9] = GET_UINT32_BE(data, 36); w[10] = GET_UINT32_BE(data, 40); w[11] = GET_UINT32_BE(data, 44); w[12] = GET_UINT32_BE(data, 48); w[13] = GET_UINT32_BE(data, 52); w[14] = GET_UINT32_BE(data, 56); w[15] = GET_UINT32_BE(data, 60); uint32_t a = h[0]; uint32_t b = h[1]; uint32_t c = h[2]; uint32_t d = h[3]; uint32_t e = h[4]; // Required by referring to section 6.2 in rfc3174. To ensure performance, // the variables A\b\c\d\e\TEMP are reused cyclically. ROUND00_15(0, a, b, c, d, e, temp, w, 0); ROUND00_15(1, temp, a, b, c, d, e, w, 0); ROUND00_15(2, e, temp, a, b, c, d, w, 0); ROUND00_15(3, d, e, temp, a, b, c, w, 0); ROUND00_15(4, c, d, e, temp, a, b, w, 0); ROUND00_15(5, b, c, d, e, temp, a, w, 0); ROUND00_15(6, a, b, c, d, e, temp, w, 0); ROUND00_15(7, temp, a, b, c, d, e, w, 0); ROUND00_15(8, e, temp, a, b, c, d, w, 0); ROUND00_15(9, d, e, temp, a, b, c, w, 0); ROUND00_15(10, c, d, e, temp, a, b, w, 0); ROUND00_15(11, b, c, d, e, temp, a, w, 0); ROUND00_15(12, a, b, c, d, e, temp, w, 0); ROUND00_15(13, temp, a, b, c, d, e, w, 0); ROUND00_15(14, e, temp, a, b, c, d, w, 0); ROUND00_15(15, d, e, temp, a, b, c, w, 0); ROUND16_79(16, c, d, e, temp, a, b, w, 0); ROUND16_79(17, b, c, d, e, temp, a, w, 0); ROUND16_79(18, a, b, c, d, e, temp, w, 0); ROUND16_79(19, temp, a, b, c, d, e, w, 0); ROUND16_79(20, e, temp, a, b, c, d, w, 1); ROUND16_79(21, d, e, temp, a, b, c, w, 1); ROUND16_79(22, c, d, e, temp, a, b, w, 1); ROUND16_79(23, b, c, d, e, temp, a, w, 1); ROUND16_79(24, a, b, c, d, e, temp, w, 1); ROUND16_79(25, temp, a, b, c, d, e, w, 1); ROUND16_79(26, e, temp, a, b, c, d, w, 1); ROUND16_79(27, d, e, temp, a, b, c, w, 1); ROUND16_79(28, c, d, e, temp, a, b, w, 1); ROUND16_79(29, b, c, d, e, temp, a, w, 1); ROUND16_79(30, a, b, c, d, e, temp, w, 1); ROUND16_79(31, temp, a, b, c, d, e, w, 1); ROUND16_79(32, e, temp, a, b, c, d, w, 1); ROUND16_79(33, d, e, temp, a, b, c, w, 1); ROUND16_79(34, c, d, e, temp, a, b, w, 1); ROUND16_79(35, b, c, d, e, temp, a, w, 1); ROUND16_79(36, a, b, c, d, e, temp, w, 1); ROUND16_79(37, temp, a, b, c, d, e, w, 1); ROUND16_79(38, e, temp, a, b, c, d, w, 1); ROUND16_79(39, d, e, temp, a, b, c, w, 1); ROUND16_79(40, c, d, e, temp, a, b, w, 2); ROUND16_79(41, b, c, d, e, temp, a, w, 2); ROUND16_79(42, a, b, c, d, e, temp, w, 2); ROUND16_79(43, temp, a, b, c, d, e, w, 2); ROUND16_79(44, e, temp, a, b, c, d, w, 2); ROUND16_79(45, d, e, temp, a, b, c, w, 2); ROUND16_79(46, c, d, e, temp, a, b, w, 2); ROUND16_79(47, b, c, d, e, temp, a, w, 2); ROUND16_79(48, a, b, c, d, e, temp, w, 2); ROUND16_79(49, temp, a, b, c, d, e, w, 2); ROUND16_79(50, e, temp, a, b, c, d, w, 2); ROUND16_79(51, d, e, temp, a, b, c, w, 2); ROUND16_79(52, c, d, e, temp, a, b, w, 2); ROUND16_79(53, b, c, d, e, temp, a, w, 2); ROUND16_79(54, a, b, c, d, e, temp, w, 2); ROUND16_79(55, temp, a, b, c, d, e, w, 2); ROUND16_79(56, e, temp, a, b, c, d, w, 2); ROUND16_79(57, d, e, temp, a, b, c, w, 2); ROUND16_79(58, c, d, e, temp, a, b, w, 2); ROUND16_79(59, b, c, d, e, temp, a, w, 2); ROUND16_79(60, a, b, c, d, e, temp, w, 3); ROUND16_79(61, temp, a, b, c, d, e, w, 3); ROUND16_79(62, e, temp, a, b, c, d, w, 3); ROUND16_79(63, d, e, temp, a, b, c, w, 3); ROUND16_79(64, c, d, e, temp, a, b, w, 3); ROUND16_79(65, b, c, d, e, temp, a, w, 3); ROUND16_79(66, a, b, c, d, e, temp, w, 3); ROUND16_79(67, temp, a, b, c, d, e, w, 3); ROUND16_79(68, e, temp, a, b, c, d, w, 3); ROUND16_79(69, d, e, temp, a, b, c, w, 3); ROUND16_79(70, c, d, e, temp, a, b, w, 3); ROUND16_79(71, b, c, d, e, temp, a, w, 3); ROUND16_79(72, a, b, c, d, e, temp, w, 3); ROUND16_79(73, temp, a, b, c, d, e, w, 3); ROUND16_79(74, e, temp, a, b, c, d, w, 3); ROUND16_79(75, d, e, temp, a, b, c, w, 3); ROUND16_79(76, c, d, e, temp, a, b, w, 3); ROUND16_79(77, b, c, d, e, temp, a, w, 3); ROUND16_79(78, a, b, c, d, e, temp, w, 3); ROUND16_79(79, temp, a, b, c, d, e, w, 3); // Let H0 = H0 + a, H1 = H1 + b, H2 = H2 + c, H3 = H3 + d, H4 = H4 + e. // Because A, B, C, D and E are reused, after the last round of conversion, A = e, b = temp, c = a, d = b, e = c h[0] += e; // H[0] += a h[1] += temp; // H[1] += b h[2] += a; // H[2] += c h[3] += b; // H[3] += d h[4] += c; // H[4] += e data += CRYPT_SHA1_BLOCKSIZE; dataLen -= CRYPT_SHA1_BLOCKSIZE; } return data; } #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA1 && !HITLS_CRYPTO_SHA1_SMALL_MEM
2301_79861745/bench_create
crypto/sha1/src/noasm_sha1.c
C
unknown
7,553
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * An implementation of sha1 that has 70% less in rom but lower performance. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_SHA1) && defined(HITLS_CRYPTO_SHA1_SMALL_MEM) #include <stdlib.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "crypt_sha1.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ /* e767 is because H is defined in SHA1 and MD5. But the both the macros are different. So masked this error */ #define K0 0x5A827999 #define K1 0x6ED9EBA1 #define K2 0x8F1BBCDC #define K3 0xCA62C1D6 typedef struct { uint32_t a; uint32_t b; uint32_t c; uint32_t d; uint32_t e; uint32_t w[16]; } SHA1_CTX; #define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d))) #define F1(b, c, d) (((b) ^ (c)) ^ (d)) #define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) #define F3(b, c, d) (((b) ^ (c)) ^ (d)) #define ROUND00_15(s, a, b, c, d, e, Kt) \ do { \ temp = ROTL32(a, 5) + F##Kt(b, c, d) + (e) + (ctx.w)[s] + K##Kt; \ e = d; \ d = c; \ c = ROTL32(b, 30); \ b = a; \ a = temp; \ } while (0) #define ROUND16_79(s, a, b, c, d, e, Kt) \ do { \ (ctx.w)[(s) & 0xF] = ROTL32( \ (ctx.w)[((s) + 13) & 0xF] ^ (ctx.w)[((s) + 8) & 0xF] ^ (ctx.w)[((s) + 2) & 0xF] ^ (ctx.w)[(s) & 0xF], 1); \ ROUND00_15((s) & 0xF, a, b, c, d, e, Kt); \ } while (0) const uint8_t *SHA1_Step(const uint8_t *input, uint32_t len, uint32_t *h) { SHA1_CTX ctx = {0}; uint32_t temp; const uint8_t *data = input; uint32_t dataLen = len; while (dataLen >= CRYPT_SHA1_BLOCKSIZE) { for (int i = 0; i < 16; ++i) { ctx.w[i] = GET_UINT32_BE(data, i * 4); } ctx.a = h[0]; ctx.b = h[1]; ctx.c = h[2]; ctx.d = h[3]; ctx.e = h[4]; // Round 0-15 for (uint32_t s = 0; s < 16; ++s) { ROUND00_15(s, ctx.a, ctx.b, ctx.c, ctx.d, ctx.e, 0); } // Round 16-79 for (uint32_t s = 16; s < 20; ++s) { ROUND16_79(s, ctx.a, ctx.b, ctx.c, ctx.d, ctx.e, 0); } for (uint32_t s = 20; s < 40; ++s) { ROUND16_79(s, ctx.a, ctx.b, ctx.c, ctx.d, ctx.e, 1); } for (uint32_t s = 40; s < 60; ++s) { ROUND16_79(s, ctx.a, ctx.b, ctx.c, ctx.d, ctx.e, 2); } for (uint32_t s = 60; s < 80; ++s) { ROUND16_79(s, ctx.a, ctx.b, ctx.c, ctx.d, ctx.e, 3); } h[0] += ctx.a; h[1] += ctx.b; h[2] += ctx.c; h[3] += ctx.d; h[4] += ctx.e; data += CRYPT_SHA1_BLOCKSIZE; dataLen -= CRYPT_SHA1_BLOCKSIZE; } return data; } #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA1 && HITLS_CRYPTO_SHA1_SMALL_MEM
2301_79861745/bench_create
crypto/sha1/src/noasm_sha1_small.c
C
unknown
3,381
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA1 #include <stdlib.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "sha1_core.h" #include "bsl_sal.h" #include "crypt_sha1.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ /* SHA-1 context structure */ struct CryptSha1Ctx { uint8_t m[CRYPT_SHA1_BLOCKSIZE]; /* store the remaining data which less than one block */ uint32_t h[CRYPT_SHA1_DIGESTSIZE / sizeof(uint32_t)]; /* store the intermediate data of the hash value */ uint32_t hNum, lNum; /* input data counter, maximum value 2 ^ 64 bits */ int32_t errorCode; /* Error code */ uint32_t count; /* Number of remaining data bytes less than one block, corresponding to the length of the m */ }; CRYPT_SHA1_Ctx *CRYPT_SHA1_NewCtx(void) { return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA1_Ctx)); } CRYPT_SHA1_Ctx *CRYPT_SHA1_NewCtxEx(void *libCtx, int32_t algId) { (void)libCtx; (void)algId; return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA1_Ctx)); } void CRYPT_SHA1_FreeCtx(CRYPT_SHA1_Ctx *ctx) { BSL_SAL_ClearFree(ctx, sizeof(CRYPT_SHA1_Ctx)); } /* e767 is because H is defined in SHA1 and MD5. But the both the macros are different. So masked this error */ int32_t CRYPT_SHA1_Init(CRYPT_SHA1_Ctx *ctx, BSL_Param *param) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void) param; (void)memset_s(ctx, sizeof(CRYPT_SHA1_Ctx), 0, sizeof(CRYPT_SHA1_Ctx)); /** * RFC3174 6.1 Initialize the H constants of the input ctx * These constants are provided by the standard */ ctx->h[0] = 0x67452301; ctx->h[1] = 0xefcdab89; ctx->h[2] = 0x98badcfe; ctx->h[3] = 0x10325476; ctx->h[4] = 0xc3d2e1f0; return CRYPT_SUCCESS; } int32_t CRYPT_SHA1_Deinit(CRYPT_SHA1_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_CleanseData((void *)(ctx), sizeof(CRYPT_SHA1_Ctx)); return CRYPT_SUCCESS; } int32_t CRYPT_SHA1_CopyCtx(CRYPT_SHA1_Ctx *dst, const CRYPT_SHA1_Ctx *src) { if (dst == NULL || src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memcpy_s(dst, sizeof(CRYPT_SHA1_Ctx), src, sizeof(CRYPT_SHA1_Ctx)); return CRYPT_SUCCESS; } CRYPT_SHA1_Ctx *CRYPT_SHA1_DupCtx(const CRYPT_SHA1_Ctx *src) { if (src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_SHA1_Ctx *newCtx = CRYPT_SHA1_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memcpy_s(newCtx, sizeof(CRYPT_SHA1_Ctx), src, sizeof(CRYPT_SHA1_Ctx)); return newCtx; } static int32_t SHA1_CheckIsCorrupted(CRYPT_SHA1_Ctx *ctx, uint32_t textLen) { uint32_t low = (ctx->lNum + (textLen << 3)) & 0xffffffffUL; if (low < ctx->lNum) { /* overflow */ if (++ctx->hNum == 0) { ctx->errorCode = CRYPT_SHA1_INPUT_OVERFLOW; BSL_ERR_PUSH_ERROR(CRYPT_SHA1_INPUT_OVERFLOW); return CRYPT_SHA1_INPUT_OVERFLOW; } } uint32_t high = ctx->hNum + (uint32_t)(textLen >> (32 - 3)); if (high < ctx->hNum) { /* overflow */ ctx->errorCode = CRYPT_SHA1_INPUT_OVERFLOW; BSL_ERR_PUSH_ERROR(CRYPT_SHA1_INPUT_OVERFLOW); return CRYPT_SHA1_INPUT_OVERFLOW; } ctx->hNum = high; ctx->lNum = low; return CRYPT_SUCCESS; } static int32_t SHA1_UpdateParamIsValid(CRYPT_SHA1_Ctx *ctx, const uint8_t *data, uint32_t nbytes) { if ((ctx == NULL) || (data == NULL && nbytes != 0)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->errorCode != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ctx->errorCode); return ctx->errorCode; } if (SHA1_CheckIsCorrupted(ctx, nbytes) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_SHA1_INPUT_OVERFLOW); return CRYPT_SHA1_INPUT_OVERFLOW; } return CRYPT_SUCCESS; } int32_t CRYPT_SHA1_Update(CRYPT_SHA1_Ctx *ctx, const uint8_t *in, uint32_t len) { int32_t ret = SHA1_UpdateParamIsValid(ctx, in, len); if (ret != CRYPT_SUCCESS) { return ret; } const uint8_t *data = in; uint32_t dataLen = len; uint32_t start = ctx->count; uint32_t left = CRYPT_SHA1_BLOCKSIZE - start; /* Check whether the user input data and cached data can form a block. */ if (dataLen < left) { (void)memcpy_s(&ctx->m[start], left, data, dataLen); ctx->count += dataLen; return CRYPT_SUCCESS; } /* Preferentially process the buf data and form a block with the user input data. */ if (start != 0) { (void)memcpy_s(&ctx->m[start], left, data, left); (void)SHA1_Step(ctx->m, CRYPT_SHA1_BLOCKSIZE, ctx->h); dataLen -= left; data += left; ctx->count = 0; } /* Cyclically process the input data */ data = SHA1_Step(data, dataLen, ctx->h); dataLen = len - (data - in); /* The remaining data is less than one block and stored in the buf. */ if (dataLen != 0) { (void)memcpy_s(ctx->m, CRYPT_SHA1_BLOCKSIZE, data, dataLen); ctx->count = dataLen; } return CRYPT_SUCCESS; } static int32_t SHA1_FinalParamIsValid(const CRYPT_SHA1_Ctx *ctx, const uint8_t *out, const uint32_t *outLen) { if ((ctx == NULL) || (out == NULL) || (outLen == NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (*outLen < CRYPT_SHA1_DIGESTSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SHA1_OUT_BUFF_LEN_NOT_ENOUGH); return CRYPT_SHA1_OUT_BUFF_LEN_NOT_ENOUGH; } if (ctx->errorCode != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ctx->errorCode); return ctx->errorCode; } return CRYPT_SUCCESS; } int32_t CRYPT_SHA1_Final(CRYPT_SHA1_Ctx *ctx, uint8_t *out, uint32_t *len) { int32_t ret = SHA1_FinalParamIsValid(ctx, out, len); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t padLen; uint32_t padPos; /* Add "1" to the end of the user data */ ctx->m[ctx->count] = 0x80; ctx->count++; /* If here is one complete data block, one complete data block is processed first. */ if (ctx->count == CRYPT_SHA1_BLOCKSIZE) { (void)SHA1_Step(ctx->m, CRYPT_SHA1_BLOCKSIZE, ctx->h); ctx->count = 0; } /* Calculate the padding position. */ padPos = ctx->count; padLen = CRYPT_SHA1_BLOCKSIZE - padPos; if (padLen < 8) { /* 64 bits (8 bytes) of 512 bits are reserved to pad "0" */ (void)memset_s(&ctx->m[padPos], padLen, 0, padLen); padPos = 0; padLen = CRYPT_SHA1_BLOCKSIZE; (void)SHA1_Step(ctx->m, CRYPT_SHA1_BLOCKSIZE, ctx->h); } /* offset 8 bytes, reserved for storing the data length */ (void)memset_s(&ctx->m[padPos], (padLen - 8), 0, (padLen - 8)); PUT_UINT32_BE(ctx->hNum, ctx->m, 56); /* The 56th byte starts to store the upper 32-bit data. */ PUT_UINT32_BE(ctx->lNum, ctx->m, 60); /* The 60th byte starts to store the lower 32-bit data. */ (void)SHA1_Step(ctx->m, CRYPT_SHA1_BLOCKSIZE, ctx->h); PUT_UINT32_BE(ctx->h[0], out, 0); PUT_UINT32_BE(ctx->h[1], out, 4); PUT_UINT32_BE(ctx->h[2], out, 8); PUT_UINT32_BE(ctx->h[3], out, 12); PUT_UINT32_BE(ctx->h[4], out, 16); *len = CRYPT_SHA1_DIGESTSIZE; return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SHA1_GetParam(CRYPT_SHA1_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA1_DIGESTSIZE, CRYPT_SHA1_BLOCKSIZE, param); } #endif #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA1
2301_79861745/bench_create
crypto/sha1/src/sha1.c
C
unknown
8,448
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef SHA1_CORE_H #define SHA1_CORE_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA1 #include <stdint.h> #ifdef __cplusplus extern "C" { #endif const uint8_t *SHA1_Step(const uint8_t *input, uint32_t len, uint32_t *h); #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA1 #endif // SHA1_CORE_H
2301_79861745/bench_create
crypto/sha1/src/sha1_core.h
C
unknown
853