22#include "bsl_err_internal.h"
23#include "crypt_errno.h"
24#include "crypt_algid.h"
25#include "crypt_local_types.h"
26#include "bsl_params.h"
33#define BITS_PER_BYTE 8
34#define SHIFTS_PER_BYTE 3
35#define BITSIZE(t) (sizeof(t) * BITS_PER_BYTE)
37#if defined(__GNUC__) || defined(__clang__)
38 #define LIKELY(x) __builtin_expect(!!(x), 1)
39 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
40 #define ALIGN32 __attribute__((aligned(32)))
41 #define ALIGN64 __attribute__((aligned(64)))
49#define FORCE_ADDR_ALIGN 1
50#if defined(__x86_64) || defined(__x86_64__) || defined(__aarch64__)
51 #undef FORCE_ADDR_ALIGN
54#define PUT_UINT32_BE(v, p, i) \
56 (p)[(i) + 0] = (uint8_t)((v) >> 24); \
57 (p)[(i) + 1] = (uint8_t)((v) >> 16); \
58 (p)[(i) + 2] = (uint8_t)((v) >> 8); \
59 (p)[(i) + 3] = (uint8_t)((v) >> 0); \
62#define PUT_UINT64_BE(v, p, i) \
64 (p)[(i) + 0] = (uint8_t)((v) >> 56); \
65 (p)[(i) + 1] = (uint8_t)((v) >> 48); \
66 (p)[(i) + 2] = (uint8_t)((v) >> 40); \
67 (p)[(i) + 3] = (uint8_t)((v) >> 32); \
68 (p)[(i) + 4] = (uint8_t)((v) >> 24); \
69 (p)[(i) + 5] = (uint8_t)((v) >> 16); \
70 (p)[(i) + 6] = (uint8_t)((v) >> 8); \
71 (p)[(i) + 7] = (uint8_t)((v) >> 0); \
74#define GET_UINT32_BE(p, i) \
76 ((uint32_t)(p)[(i) + 0] << 24) | \
77 ((uint32_t)(p)[(i) + 1] << 16) | \
78 ((uint32_t)(p)[(i) + 2] << 8) | \
79 ((uint32_t)(p)[(i) + 3] << 0) \
82#define PUT_UINT32_LE(v, p, i) \
84 (p)[(i) + 3] = (uint8_t)((v) >> 24); \
85 (p)[(i) + 2] = (uint8_t)((v) >> 16); \
86 (p)[(i) + 1] = (uint8_t)((v) >> 8); \
87 (p)[(i) + 0] = (uint8_t)((v) >> 0); \
90#define PUT_UINT64_LE(v, p, i) do { \
91 (p)[(i) + 7] = (uint8_t)((v) >> 56); \
92 (p)[(i) + 6] = (uint8_t)((v) >> 48); \
93 (p)[(i) + 5] = (uint8_t)((v) >> 40); \
94 (p)[(i) + 4] = (uint8_t)((v) >> 32); \
95 (p)[(i) + 3] = (uint8_t)((v) >> 24); \
96 (p)[(i) + 2] = (uint8_t)((v) >> 16); \
97 (p)[(i) + 1] = (uint8_t)((v) >> 8); \
98 (p)[(i) + 0] = (uint8_t)((v) >> 0); \
101#define GET_UINT64_LE(p, i) \
103 ((uint64_t)(p)[(i) + 7] << 56) | ((uint64_t)(p)[(i) + 6] << 48) | \
104 ((uint64_t)(p)[(i) + 5] << 40) | ((uint64_t)(p)[(i) + 4] << 32) | \
105 ((uint64_t)(p)[(i) + 3] << 24) | ((uint64_t)(p)[(i) + 2] << 16) | \
106 ((uint64_t)(p)[(i) + 1] << 8) | ((uint64_t)(p)[(i) + 0] << 0) \
116#define GOTO_ERR_IF(func, ret) do { \
118 if ((ret) != CRYPT_SUCCESS) { \
119 BSL_ERR_PUSH_ERROR((ret)); \
124#define GOTO_ERR_IF_EX(func, ret) do { \
126 if ((ret) != CRYPT_SUCCESS) { \
131#define GOTO_ERR_IF_TRUE(condition, ret) do { \
133 BSL_ERR_PUSH_ERROR((ret)); \
141#define RETURN_RET_IF(condition, ret) \
144 BSL_ERR_PUSH_ERROR(ret); \
150#define RETURN_RET_IF_ERR(func, ret) \
153 if ((ret) != CRYPT_SUCCESS) { \
154 BSL_ERR_PUSH_ERROR((ret)); \
159#define RETURN_RET_IF_ERR_EX(func, ret) \
162 if ((ret) != CRYPT_SUCCESS) { \
167#define BREAK_IF(condition) \
175#define GOTO_ERR_IF_SRC_NOT_NULL(dest, src, func, ret) \
177 if ((src) != NULL) { \
179 if ((dest) == NULL) { \
180 BSL_ERR_PUSH_ERROR((ret)); \
194#define DATA_XOR(a, b, r, len) \
196 uint32_t subscript; \
197 for (subscript = 0; subscript < (len); subscript++) { \
198 (r)[subscript] = (a)[subscript] ^ (b)[subscript]; \
212#ifdef FORCE_ADDR_ALIGN
213#define DATA32_XOR(a, b, r, len) \
215 for (uint32_t ii = 0; ii < (len); ii++) { \
216 (r)[ii] = (a)[ii] ^ (b)[ii]; \
220#define DATA32_XOR(a, b, r, len) \
222 for (uint32_t ii = 0; ii < (len); ii += 4) { \
223 *(uint32_t *)((uintptr_t)(r) + ii) = \
224 (*(const uint32_t *)((uintptr_t)(a) + ii)) ^ (*(const uint32_t *)((uintptr_t)(b) + ii)); \
238#define DATA64_XOR(a, b, r, len) \
241 uintptr_t aPtr = (uintptr_t)(a); \
242 uintptr_t bPtr = (uintptr_t)(b); \
243 uintptr_t rPtr = (uintptr_t)(r); \
244 if (((aPtr & 0x7) != 0) || ((bPtr & 0x7) != 0) || ((rPtr & 0x7) != 0)) { \
245 for (ii = 0; ii < (len); ii++) { \
246 (r)[ii] = (a)[ii] ^ (b)[ii]; \
249 for (ii = 0; ii < (len); ii += 8) { \
250 *(uint64_t *)((rPtr) + ii) = (*(const uint64_t *)((aPtr) + ii)) ^ (*(const uint64_t *)((bPtr) + ii)); \
265 uint8_t *out, uint32_t *outlen);
280int32_t CRYPT_Mgf1(
void *provCtx,
const EAL_MdMethod *hashMethod,
const uint8_t *seed,
const uint32_t seedLen,
281 uint8_t *mask, uint32_t maskLen);
291int32_t CRYPT_GetPkeyProcessParams(BSL_Param *params, CRYPT_EAL_ProcessFuncCb *processCb,
void **args);
293#if (defined(HITLS_CRYPTO_DH_CHECK) || defined(HITLS_CRYPTO_DSA_CHECK))
305int32_t CRYPT_FFC_KeyPairCheck(
const void *x,
const void *y,
const void *p,
const void *g);
317int32_t CRYPT_FFC_PrvCheck(
const void *x,
const void *p,
const void *q);
321#if defined(HITLS_CRYPTO_PROVIDER) && defined(HITLS_CRYPTO_MD)
335int32_t CRYPT_MdCommonGetParam(uint16_t mdSize, uint16_t mdBlockSize, BSL_Param *param);
338#if defined(HITLS_CRYPTO_PROVIDER) && (defined(HITLS_CRYPTO_RSA) || defined(HITLS_CRYPTO_ECDSA) || \
339 defined(HITLS_CRYPTO_DSA))
347int32_t CRYPT_PkeySetMdAttr(
const char *mdAttr, uint32_t len,
char **pkeyMdAttr);
351#define ROTL32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
353#define ROTR64(x, n) (((x) << (64 - (n))) | ((x) >> (n)))
355#define IS_BUF_NON_ZERO(out, outLen) (((out) != NULL) && ((outLen) > 0))
356#define CRYPT_IS_BUF_NON_ZERO(out, outLen) (((out) != NULL) && ((outLen) > 0))
357#define CRYPT_CHECK_DATA_INVALID(d) (((d)->data == NULL && (d)->len != 0))
358#define CRYPT_IsDataNull(d) ((d) == NULL || (d)->data == NULL || (d)->len == 0)
359#define CRYPT_IN_RANGE(x, range) ((x) >= (range)->min && (x) <= (range)->max)
360#define CRYPT_CHECK_BUF_INVALID(buf, len) (((buf) == NULL && (len) != 0))
361#define CRYPT_SWAP32(x) ((((x) & 0xff000000) >> 24) | \
362 (((x) & 0x00ff0000) >> 8) | \
363 (((x) & 0x0000ff00) << 8) | \
364 (((x) & 0x000000ff) << 24))
365#ifdef HITLS_BIG_ENDIAN
367#define CRYPT_HTONL(x) (x)
370#define GET_UINT32_LE(p, i) \
372 ((uint32_t)((const uint8_t *)(p))[(i) + 3] << 24) | \
373 ((uint32_t)((const uint8_t *)(p))[(i) + 2] << 16) | \
374 ((uint32_t)((const uint8_t *)(p))[(i) + 1] << 8) | \
375 ((uint32_t)((const uint8_t *)(p))[(i) + 0] << 0) \
379#define CRYPT_LE32TOH(x) CRYPT_SWAP32(x)
381#define CRYPT_HTOLE32(x) CRYPT_SWAP32(x)
385#define CRYPT_HTONL(x) CRYPT_SWAP32(x)
388#define GET_UINT32_LE(p, i) \
390 (((uintptr_t)(p) & 0x7) != 0) ? ((uint32_t)((const uint8_t *)(p))[(i) + 3] << 24) | \
391 ((uint32_t)((const uint8_t *)(p))[(i) + 2] << 16) | \
392 ((uint32_t)((const uint8_t *)(p))[(i) + 1] << 8) | \
393 ((uint32_t)((const uint8_t *)(p))[(i) + 0] << 0) \
394 : (*(uint32_t *)((uintptr_t)(p) + (i))) \
397#define CRYPT_LE32TOH(x) (x)
399#define CRYPT_HTOLE32(x) (x)
403#ifdef HITLS_BIG_ENDIAN
406#define GET_UINT16_LE(p, i) \
408 ((uint16_t)((const uint8_t *)(p))[(i) + 1] << 8) | \
409 ((uint16_t)((const uint8_t *)(p))[(i) + 0] << 0) \
413#define GET_UINT16_LE(p, i) \
415 (((uintptr_t)(p) & 0x7) != 0) ? ((uint16_t)((const uint8_t *)(p))[(i) + 1] << 8) | \
416 ((uint16_t)((const uint8_t *)(p))[(i) + 0] << 0) \
417 : (*(uint16_t *)((uint8_t *)(uintptr_t)(p) + (i))) \
421#define PUT_UINT16_LE(v, p, i) \
424 (p)[(i) + 1] = (uint8_t)((v) >> 8); \
425 (p)[(i) + 0] = (uint8_t)((v) >> 0); \
431static inline uint64_t Uint64FromBeBytes(
const uint8_t *bytes)
433 return (((uint64_t)bytes[0] << 56) |
434 ((uint64_t)bytes[1] << 48) |
435 ((uint64_t)bytes[2] << 40) |
436 ((uint64_t)bytes[3] << 32) |
437 ((uint64_t)bytes[4] << 24) |
438 ((uint64_t)bytes[5] << 16) |
439 ((uint64_t)bytes[6] << 8) |
443static inline void Uint64ToBeBytes(uint64_t v, uint8_t *bytes)
445 bytes[0] = (uint8_t)(v >> 56);
446 bytes[1] = (uint8_t)(v >> 48);
447 bytes[2] = (uint8_t)(v >> 40);
448 bytes[3] = (uint8_t)(v >> 32);
449 bytes[4] = (uint8_t)(v >> 24);
450 bytes[5] = (uint8_t)(v >> 16);
451 bytes[6] = (uint8_t)(v >> 8);
452 bytes[7] = (uint8_t)(v & 0xffu);
455#if defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_DSA) || \
456 defined(HITLS_CRYPTO_ECDSA) || defined(HITLS_CRYPTO_SM2_SIGN)
460#if defined(HITLS_CRYPTO_DSA) || defined(HITLS_CRYPTO_ECDSA) || defined(HITLS_CRYPTO_SM2_SIGN)
461typedef int32_t (*CheckSignMdCallBack)(
void *val);
462int32_t CRYPT_SetSignMdCtrl(
CRYPT_MD_AlgId *signMdId,
void *val, uint32_t len, CheckSignMdCallBack checkSignMdIdCb);
465static inline bool ParamIdIsValid(uint32_t
id,
const uint32_t *list, uint32_t num)
467 for (uint32_t i = 0; i < num; i++) {
475const BSL_Param *EAL_FindConstParam(
const BSL_Param *param, int32_t key);
477BSL_Param *EAL_FindParam(BSL_Param *param, int32_t key);
479static inline const BSL_Param *GetConstParamValue(
const BSL_Param *params, int32_t type,
480 uint8_t **value, uint32_t *valueLen)
482 const BSL_Param *temp = EAL_FindConstParam(params, type);
484 *value = temp->value;
485 if (valueLen != NULL) {
486 *valueLen = temp->valueLen;
492static inline BSL_Param *GetParamValue(BSL_Param *params, int32_t type, uint8_t **value, uint32_t *valueLen)
494 BSL_Param *temp = EAL_FindParam(params, type);
496 *value = temp->value;
497 if (valueLen != NULL) {
498 *valueLen = temp->valueLen;
504void GetCpuInstrSupportState(
void);
507#define CPU_ID_OUT_U32_CNT 4
514#define XCR0_BIT_SSE (1ULL << 1)
515#define XCR0_BIT_AVX (1ULL << 2)
516#define XCR0_BIT_OPMASK (1ULL << 5)
517#define XCR0_BIT_ZMM_LOW (1ULL << 6)
518#define XCR0_BIT_ZMM_HIGH (1ULL << 7)
521 uint32_t code1Out[CPU_ID_OUT_U32_CNT];
522 uint32_t code7Out[CPU_ID_OUT_U32_CNT];
524 bool osSupportAVX512;
525} CpuInstrSupportState;
527bool IsSupportAES(
void);
528bool IsSupportBMI1(
void);
529bool IsSupportBMI2(
void);
530bool IsSupportADX(
void);
531bool IsSupportAVX(
void);
532bool IsSupportAVX2(
void);
533bool IsSupportSSE(
void);
534bool IsSupportSSE2(
void);
535bool IsSupportSSE3(
void);
536bool IsSupportMOVBE(
void);
537bool IsSupportAVX512F(
void);
538bool IsSupportAVX512VL(
void);
539bool IsSupportAVX512BW(
void);
540bool IsSupportAVX512DQ(
void);
541bool IsSupportXSAVE(
void);
542bool IsSupportOSXSAVE(
void);
543bool IsOSSupportAVX(
void);
544bool IsOSSupportAVX512(
void);
546void GetCpuId(uint32_t eax, uint32_t ecx, uint32_t cpuId[CPU_ID_OUT_U32_CNT]);
548#elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
550bool IsSupportAES(
void);
551bool IsSupportPMULL(
void);
552bool IsSupportSHA1(
void);
553bool IsSupportSHA256(
void);
554bool IsSupportNEON(
void);
556#if defined(__aarch64__)
557bool IsSupportSM4(
void);
558bool IsSupportSHA512(
void);
CRYPT_MD_AlgId
定义 crypt_algid.h:68
定义 crypt_local_types.h:63