API参考
载入中...
搜索中...
未找到
crypt_utils.h
1/*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16#ifndef CRYPT_UTILS_H
17#define CRYPT_UTILS_H
18
19#include <stdint.h>
20#include <stdlib.h>
21#include <stdbool.h>
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"
27#include "crypt_params_key.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif // __cplusplus
32
33#define BITS_PER_BYTE 8
34#define SHIFTS_PER_BYTE 3
35#define BITSIZE(t) (sizeof(t) * BITS_PER_BYTE)
36
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)))
42#else
43 #define LIKELY(x) x
44 #define UNLIKELY(x) x
45 #define ALIGN32
46 #define ALIGN64
47#endif
48
49#define FORCE_ADDR_ALIGN 1
50#if defined(__x86_64) || defined(__x86_64__) || defined(__aarch64__)
51 #undef FORCE_ADDR_ALIGN
52#endif
53
54#define PUT_UINT32_BE(v, p, i) \
55do { \
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); \
60} while (0)
61
62#define PUT_UINT64_BE(v, p, i) \
63do { \
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); \
72} while (0)
73
74#define GET_UINT32_BE(p, i) \
75( \
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) \
80)
81
82#define PUT_UINT32_LE(v, p, i) \
83do { \
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); \
88} while (0)
89
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); \
99} while (0)
100
101#define GET_UINT64_LE(p, i) \
102( \
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) \
107)
108
112
116#define GOTO_ERR_IF(func, ret) do { \
117 (ret) = (func); \
118 if ((ret) != CRYPT_SUCCESS) { \
119 BSL_ERR_PUSH_ERROR((ret)); \
120 goto ERR; \
121 } \
122 } while (0)
123
124#define GOTO_ERR_IF_EX(func, ret) do { \
125 (ret) = (func); \
126 if ((ret) != CRYPT_SUCCESS) { \
127 goto ERR; \
128 } \
129 } while (0)
130
131#define GOTO_ERR_IF_TRUE(condition, ret) do { \
132 if (condition) { \
133 BSL_ERR_PUSH_ERROR((ret)); \
134 goto ERR; \
135 } \
136 } while (0)
137
141#define RETURN_RET_IF(condition, ret) \
142 do { \
143 if (condition) { \
144 BSL_ERR_PUSH_ERROR(ret); \
145 return ret; \
146 } \
147 } while (0)
148
149
150#define RETURN_RET_IF_ERR(func, ret) \
151 do { \
152 (ret) = (func); \
153 if ((ret) != CRYPT_SUCCESS) { \
154 BSL_ERR_PUSH_ERROR((ret)); \
155 return ret; \
156 } \
157 } while (0)
158
159#define RETURN_RET_IF_ERR_EX(func, ret) \
160 do { \
161 (ret) = (func); \
162 if ((ret) != CRYPT_SUCCESS) { \
163 return ret; \
164 } \
165 } while (0)
166
167#define BREAK_IF(condition) \
168 if (condition) { \
169 break; \
170 }
171
175#define GOTO_ERR_IF_SRC_NOT_NULL(dest, src, func, ret) \
176 do { \
177 if ((src) != NULL) { \
178 (dest) = (func); \
179 if ((dest) == NULL) { \
180 BSL_ERR_PUSH_ERROR((ret)); \
181 goto ERR; \
182 } \
183 } \
184 } while (0)
185
194#define DATA_XOR(a, b, r, len) \
195 do { \
196 uint32_t subscript; \
197 for (subscript = 0; subscript < (len); subscript++) { \
198 (r)[subscript] = (a)[subscript] ^ (b)[subscript]; \
199 } \
200 } while (0)
201
212#ifdef FORCE_ADDR_ALIGN
213#define DATA32_XOR(a, b, r, len) \
214 do { \
215 for (uint32_t ii = 0; ii < (len); ii++) { \
216 (r)[ii] = (a)[ii] ^ (b)[ii]; \
217 } \
218 } while (0)
219#else
220#define DATA32_XOR(a, b, r, len) \
221 do { \
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)); \
225 } \
226 } while (0)
227#endif
238#define DATA64_XOR(a, b, r, len) \
239 do { \
240 uint32_t ii; \
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]; \
247 } \
248 } else { \
249 for (ii = 0; ii < (len); ii += 8) { \
250 *(uint64_t *)((rPtr) + ii) = (*(const uint64_t *)((aPtr) + ii)) ^ (*(const uint64_t *)((bPtr) + ii)); \
251 } \
252 } \
253 } while (0)
254
264int32_t CRYPT_CalcHash(void *provCtx, const EAL_MdMethod *hashMethod, const CRYPT_ConstData *hashData, uint32_t size,
265 uint8_t *out, uint32_t *outlen);
266
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);
282
291int32_t CRYPT_GetPkeyProcessParams(BSL_Param *params, CRYPT_EAL_ProcessFuncCb *processCb, void **args);
292
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);
306
317int32_t CRYPT_FFC_PrvCheck(const void *x, const void *p, const void *q);
318
319#endif // HITLS_CRYPTO_DH_CHECK || HITLS_CRYPTO_DSA_CHECK
320
321#if defined(HITLS_CRYPTO_PROVIDER) && defined(HITLS_CRYPTO_MD)
335int32_t CRYPT_MdCommonGetParam(uint16_t mdSize, uint16_t mdBlockSize, BSL_Param *param);
336#endif
337
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);
348#endif
349
350/* Assumes that x is uint32_t and 0 < n < 32 */
351#define ROTL32(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
352
353#define ROTR64(x, n) (((x) << (64 - (n))) | ((x) >> (n))) // Assumes that x is uint64_t and 0 < n < 64
354
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
366
367#define CRYPT_HTONL(x) (x)
368
369// Interpret p + i as little endian order. The type of p must be uint8_t *.
370#define GET_UINT32_LE(p, i) \
371( \
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) \
376)
377
378// Convert little-endian order to host order
379#define CRYPT_LE32TOH(x) CRYPT_SWAP32(x)
380// Convert host order to little-endian order
381#define CRYPT_HTOLE32(x) CRYPT_SWAP32(x)
382
383#else
384
385#define CRYPT_HTONL(x) CRYPT_SWAP32(x)
386
387// Interpret p + i as little endian.
388#define GET_UINT32_LE(p, i) \
389( \
390 (((uintptr_t)(p) & 0x7) != 0) ? ((uint32_t)((const uint8_t *)(p))[(i) + 3] << 24) | /* 24: 4th byte */ \
391 ((uint32_t)((const uint8_t *)(p))[(i) + 2] << 16) | /* 16: 3rd byte */ \
392 ((uint32_t)((const uint8_t *)(p))[(i) + 1] << 8) | /* 8: 2nd byte */ \
393 ((uint32_t)((const uint8_t *)(p))[(i) + 0] << 0) /* 0: 1st byte */ \
394 : (*(uint32_t *)((uintptr_t)(p) + (i))) \
395)
396// Convert little-endian order to host order
397#define CRYPT_LE32TOH(x) (x)
398// Convert host order to little-endian order
399#define CRYPT_HTOLE32(x) (x)
400
401#endif
402
403#ifdef HITLS_BIG_ENDIAN
404
405// Interpret p + i as little endian. The type of p must be uint8_t *.
406#define GET_UINT16_LE(p, i) \
407( \
408 ((uint16_t)((const uint8_t *)(p))[(i) + 1] << 8) | \
409 ((uint16_t)((const uint8_t *)(p))[(i) + 0] << 0) \
410)
411#else
412// Interpret p + i as little endian.
413#define GET_UINT16_LE(p, i) \
414( \
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))) \
418)
419#endif
420
421#define PUT_UINT16_LE(v, p, i) \
422 do \
423 { \
424 (p)[(i) + 1] = (uint8_t)((v) >> 8); \
425 (p)[(i) + 0] = (uint8_t)((v) >> 0); \
426 } while (0)
427
431static inline uint64_t Uint64FromBeBytes(const uint8_t *bytes)
432{
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) |
440 (uint64_t)bytes[7]);
441}
442
443static inline void Uint64ToBeBytes(uint64_t v, uint8_t *bytes)
444{
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);
453}
454
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)
457uint32_t CRYPT_GetMdSizeById(CRYPT_MD_AlgId id);
458#endif
459
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);
463#endif
464
465static inline bool ParamIdIsValid(uint32_t id, const uint32_t *list, uint32_t num)
466{
467 for (uint32_t i = 0; i < num; i++) {
468 if (id == list[i]) {
469 return true;
470 }
471 }
472 return false;
473}
474
475const BSL_Param *EAL_FindConstParam(const BSL_Param *param, int32_t key);
476
477BSL_Param *EAL_FindParam(BSL_Param *param, int32_t key);
478
479static inline const BSL_Param *GetConstParamValue(const BSL_Param *params, int32_t type,
480 uint8_t **value, uint32_t *valueLen)
481{
482 const BSL_Param *temp = EAL_FindConstParam(params, type);
483 if (temp != NULL) {
484 *value = temp->value;
485 if (valueLen != NULL) {
486 *valueLen = temp->valueLen;
487 }
488 }
489 return temp;
490}
491
492static inline BSL_Param *GetParamValue(BSL_Param *params, int32_t type, uint8_t **value, uint32_t *valueLen)
493{
494 BSL_Param *temp = EAL_FindParam(params, type);
495 if (temp != NULL) {
496 *value = temp->value;
497 if (valueLen != NULL) {
498 *valueLen = temp->valueLen;
499 }
500 }
501 return temp;
502}
503
504void GetCpuInstrSupportState(void);
505
506#ifdef __x86_64__
507#define CPU_ID_OUT_U32_CNT 4
508#define EAX_OUT_IDX 0
509#define EBX_OUT_IDX 1
510#define ECX_OUT_IDX 2
511#define EDX_OUT_IDX 3
512
513/* %eax */
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)
519
520typedef struct {
521 uint32_t code1Out[CPU_ID_OUT_U32_CNT];
522 uint32_t code7Out[CPU_ID_OUT_U32_CNT];
523 bool osSupportAVX; /* input ecx = 0, output edx:eax bit 2 */
524 bool osSupportAVX512; /* input ecx = 0, output edx:eax bit 6 */
525} CpuInstrSupportState;
526
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);
545
546void GetCpuId(uint32_t eax, uint32_t ecx, uint32_t cpuId[CPU_ID_OUT_U32_CNT]);
547
548#elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
549
550bool IsSupportAES(void);
551bool IsSupportPMULL(void);
552bool IsSupportSHA1(void);
553bool IsSupportSHA256(void);
554bool IsSupportNEON(void);
555
556#if defined(__aarch64__)
557bool IsSupportSM4(void);
558bool IsSupportSHA512(void);
559#endif // __aarch64__
560
561#endif // __arm__ || __arm || __aarch64__
562
563#ifdef __cplusplus
564}
565#endif // __cplusplus
566
567#endif // CRYPT_UTILS_H
Parameter identifiers
CRYPT_MD_AlgId
定义 crypt_algid.h:68
定义 crypt_types.h:48
定义 crypt_local_types.h:63