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. */ #ifndef CRYPT_SHA2_H #define CRYPT_SHA2_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA2 #include <stdint.h> #include <stdlib.h> #include "crypt_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ /** @defgroup LLF SHA2 Low level function */ #ifdef HITLS_CRYPTO_SHA224 #define CRYPT_SHA2_224_BLOCKSIZE 64 #define CRYPT_SHA2_224_DIGESTSIZE 28 #endif // HITLS_CRYPTO_SHA224 #ifdef HITLS_CRYPTO_SHA256 #define CRYPT_SHA2_256_BLOCKSIZE 64 #define CRYPT_SHA2_256_DIGESTSIZE 32 #endif // HITLS_CRYPTO_SHA256 #ifdef HITLS_CRYPTO_SHA384 #define CRYPT_SHA2_384_BLOCKSIZE 128 #define CRYPT_SHA2_384_DIGESTSIZE 48 #endif // HITLS_CRYPTO_SHA384 #ifdef HITLS_CRYPTO_SHA512 #define CRYPT_SHA2_512_BLOCKSIZE 128 #define CRYPT_SHA2_512_DIGESTSIZE 64 #endif // HITLS_CRYPTO_SHA512 #define CRYPT_SHA2_224_Squeeze NULL #define CRYPT_SHA2_256_Squeeze NULL #define CRYPT_SHA2_384_Squeeze NULL #define CRYPT_SHA2_512_Squeeze NULL #ifdef HITLS_CRYPTO_SHA224 typedef struct CryptSha256Ctx CRYPT_SHA2_224_Ctx; #define CRYPT_SHA2_224_NewCtx CRYPT_SHA2_256_NewCtx #define CRYPT_SHA2_224_NewCtxEx CRYPT_SHA2_256_NewCtxEx #define CRYPT_SHA2_224_FreeCtx CRYPT_SHA2_256_FreeCtx #define CRYPT_SHA2_224_Deinit CRYPT_SHA2_256_Deinit #define CRYPT_SHA2_224_CopyCtx CRYPT_SHA2_256_CopyCtx #define CRYPT_SHA2_224_DupCtx CRYPT_SHA2_256_DupCtx #define CRYPT_SHA2_224_Update CRYPT_SHA2_256_Update #define CRYPT_SHA2_224_Final CRYPT_SHA2_256_Final /** * @defgroup CRYPT_SHA2_224_Init * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_224_Init(CRYPT_SHA2_224_Ctx *ctx) * @endcode * * @par Purpose * This is used to initialize the SHA224 ctx for a digest operation. * * @par Description * CRYPT_SHA2_224_Init function initializes the ctx for a digest operation. This function must be called before * CRYPT_SHA2_224_Update or CRYPT_SHA2_224_Final operations. This function will not allocate memory for any of the * ctx variables. Instead the caller is expected to pass a ctx pointer pointing to a valid memory location * (either locally or dynamically allocated). * * @param[in] ctx The sha224 ctx * @param *param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS ctx is initialized * @retval #CRYPT_NULL_INPUT ctx is NULL */ int32_t CRYPT_SHA2_224_Init(CRYPT_SHA2_224_Ctx *ctx, BSL_Param *param); #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup SHA224 * @brief SHA224 get param function * @param ctx [in] Pointer to the SHA224 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_SHA2_224_GetParam(CRYPT_SHA2_224_Ctx *ctx, BSL_Param *param); #else #define CRYPT_SHA2_224_GetParam NULL #endif #endif // HITLS_CRYPTO_SHA224 #ifdef HITLS_CRYPTO_SHA256 typedef struct CryptSha256Ctx CRYPT_SHA2_256_Ctx; /** * @ingroup SHA2_256 * @brief Generate md context. * * @retval Success: sha256 ctx. * Fails: NULL. */ CRYPT_SHA2_256_Ctx *CRYPT_SHA2_256_NewCtx(void); /** * @ingroup SHA2_256 * @brief Generate md context. * * @param libCtx [IN] library context * @param algId [IN] algorithm id * * @retval Success: sha256 ctx. * Fails: NULL. */ CRYPT_SHA2_256_Ctx *CRYPT_SHA2_256_NewCtxEx(void *libCtx, int32_t algId); /** * @ingroup SHA2_256 * @brief free md context. * * @param ctx [IN] md handle */ void CRYPT_SHA2_256_FreeCtx(CRYPT_SHA2_256_Ctx *ctx); /** * @defgroup CRYPT_SHA2_256_Init * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_256_Init(CRYPT_SHA2_256_Ctx *ctx) * @endcode * * @par Purpose * This is used to initialize the SHA256 ctx for a digest operation. * * @par Description * CRYPT_SHA2_256_Init function initializes the ctx for * a digest operation. This function must be called before * CRYPT_SHA2_256_Update or CRYPT_SHA2_256_Final operations. This function will not * allocate memory for any of the ctx variables. Instead the caller is * expected to pass a ctx pointer pointing to a valid memory location * (either locally or dynamically allocated). * * @param[in] ctx The sha256 ctx * @param *param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS ctx is initialized * @retval #CRYPT_NULL_INPUT ctx is NULL */ int32_t CRYPT_SHA2_256_Init(CRYPT_SHA2_256_Ctx *ctx, BSL_Param *param); /** * @defgroup CRYPT_SHA2_256_Update * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_256_Update(CRYPT_SHA2_256_Ctx *ctx, const uint8_t *data, usize_t nbytes) * @endcode * * @par Purpose * This is used to perform sha256 digest operation on chunks of data. * * @par Description * CRYPT_SHA2_256_Update function performs digest operation on * chunks of data. This method of digesting is used when data is * present in multiple buffers or not available all at once. * CRYPT_SHA2_256_Init must have been called before calling this * function. * * @param[in] ctx The sha256 ctx * @param[in] data The input data * @param[in] nbytes The input data length * * @retval #CRYPT_SUCCESS If partial digest is calculated * @retval #CRYPT_NULL_INPUT input arguments is NULL * @retval #CRYPT_SHA2_ERR_OVERFLOW input message is overflow */ int32_t CRYPT_SHA2_256_Update(CRYPT_SHA2_256_Ctx *ctx, const uint8_t *data, uint32_t nbytes); /** * @defgroup CRYPT_SHA2_256_Final * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_256_Final(CRYPT_SHA2_256_Ctx *ctx, uint8_t *digest, uint32_t *len) * @endcode * * @par Purpose * This is used to complete sha256 digest operation on remaining data, and is * called at the end of digest operation. * * @par Description * CRYPT_SHA2_256_Final function completes digest operation on remaining data, and * is called at the end of digest operation. * CRYPT_SHA2_256_Init must have been called before calling this function. This * function calculates the digest. The memory for digest must * already have been allocated. * * @param[in] ctx The sha256 ctx * @param[out] digest The digest * * @retval #CRYPT_SUCCESS If partial digest is calculated * @retval #CRYPT_NULL_INPUT input arguments is NULL * @retval #CRYPT_SHA2_ERR_OVERFLOW input message is overflow * @retval #CRYPT_SHA2_OUT_BUFF_LEN_NOT_ENOUGH output buffer is not enough */ int32_t CRYPT_SHA2_256_Final(CRYPT_SHA2_256_Ctx *ctx, uint8_t *digest, uint32_t *outlen); /** * @ingroup LLF Low Level Functions * * @brief SHA256 deinit function * * @param[in,out] ctx The SHA256 ctx * * @retval #CRYPT_SUCCESS initialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SHA2_256_Deinit(CRYPT_SHA2_256_Ctx *ctx); /** * @ingroup SHA256 * @brief SHA256 copy CTX function * @param dst [out] Pointer to the new SHA256 context. * @param src [in] Pointer to the original SHA256 context. */ int32_t CRYPT_SHA2_256_CopyCtx(CRYPT_SHA2_256_Ctx *dst, const CRYPT_SHA2_256_Ctx *src); /** * @ingroup SHA256 * @brief SHA256 dup CTX function * @param src [in] Pointer to the original SHA256 context. */ CRYPT_SHA2_256_Ctx *CRYPT_SHA2_256_DupCtx(const CRYPT_SHA2_256_Ctx *src); #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup SHA256 * @brief SHA256 get param function * @param ctx [in] Pointer to the SHA256 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_SHA2_256_GetParam(CRYPT_SHA2_256_Ctx *ctx, BSL_Param *param); #else #define CRYPT_SHA2_256_GetParam NULL #endif #endif // HITLS_CRYPTO_SHA256 #ifdef HITLS_CRYPTO_SHA384 typedef struct CryptSha2512Ctx CRYPT_SHA2_384_Ctx; #define CRYPT_SHA2_384_NewCtx CRYPT_SHA2_512_NewCtx #define CRYPT_SHA2_384_NewCtxEx CRYPT_SHA2_512_NewCtxEx #define CRYPT_SHA2_384_FreeCtx CRYPT_SHA2_512_FreeCtx #define CRYPT_SHA2_384_Deinit CRYPT_SHA2_512_Deinit #define CRYPT_SHA2_384_CopyCtx CRYPT_SHA2_512_CopyCtx #define CRYPT_SHA2_384_DupCtx CRYPT_SHA2_512_DupCtx #define CRYPT_SHA2_384_Update CRYPT_SHA2_512_Update #define CRYPT_SHA2_384_Final CRYPT_SHA2_512_Final /** * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_384_Init(CRYPT_SHA2_384_Ctx *ctx) * @endcode * * @par Purpose * This is used to initialize the SHA384 ctx for a digest operation. * * @par Description * CRYPT_SHA2_384_Init function initializes the ctx for a digest operation. This function must be called before * CRYPT_SHA2_384_Update or CRYPT_SHA2_384_Final operations. This function will not allocate memory for any of the * ctx variables. Instead the caller is expected to pass a ctx pointer pointing to a valid memory location * (either locally or dynamically allocated). * * @param[in,out] ctx The sha384 ctx * @param *param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS ctx is initialized * @retval #CRYPT_NULL_INPUT ctx is NULL */ int32_t CRYPT_SHA2_384_Init(CRYPT_SHA2_384_Ctx *ctx, BSL_Param *param); #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup SHA512 * @brief SHA512 get param function * @param ctx [in] Pointer to the SHA512 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_SHA2_384_GetParam(CRYPT_SHA2_384_Ctx *ctx, BSL_Param *param); #else #define CRYPT_SHA2_384_GetParam NULL #endif #endif // HITLS_CRYPTO_SHA384 #ifdef HITLS_CRYPTO_SHA512 typedef struct CryptSha2512Ctx CRYPT_SHA2_512_Ctx; /** * @ingroup SHA2_512 * @brief Generate md context. * * @retval Success: sha512 ctx. * Fails: NULL. */ CRYPT_SHA2_512_Ctx *CRYPT_SHA2_512_NewCtx(void); /** * @ingroup SHA2_512 * @brief Generate md context. * * @param libCtx [IN] library context * @param algId [IN] algorithm id * * @retval Success: sha512 ctx. * Fails: NULL. */ CRYPT_SHA2_512_Ctx *CRYPT_SHA2_512_NewCtxEx(void *libCtx, int32_t algId); /** * @ingroup SHA2_512 * @brief free md context. * * @param ctx [IN] md handle */ void CRYPT_SHA2_512_FreeCtx(CRYPT_SHA2_512_Ctx *ctx); /** * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_512_Init(CRYPT_SHA2_512_Ctx *ctx) * @endcode * * @par Purpose * This is used to initialize the SHA512 ctx for a digest operation. * * @par Description * CRYPT_SHA2_512_Init function initializes the ctx for a digest operation. This function must be called before * CRYPT_SHA2_512_Update or CRYPT_SHA2_512_Final operations. This function will not allocate memory for any of the * ctx variable. Instead the caller is expected to pass a ctx pointer pointing to a valid memory location * (either locally or dynamically allocated). * * @param[in,out] ctx The sha512 ctx * @param *param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS ctx is initialized * @retval #CRYPT_NULL_INPUT ctx is NULL */ int32_t CRYPT_SHA2_512_Init(CRYPT_SHA2_512_Ctx *ctx, BSL_Param *param); /** * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_512_Update(CRYPT_SHA2_512_Ctx *ctx, const uint8_t *data, usize_t nbytes) * @endcode * * @par Purpose * This is used to perform sha512 digest operation on chunks of data. * * @par Description * CRYPT_SHA2_512_Update function performs digest operation on chunks of data. This method of digesting is used when * data is present in multiple buffers or not available all at once. CRYPT_SHA2_512_Init must have been called before * calling this function. * * @param[in,out] ctx The sha512 ctx * @param[in] data The input data * @param[in] nbytes The input data length * * @retval #CRYPT_SUCCESS If partial digest is calculated * @retval #CRYPT_NULL_INPUT input arguments is NULL * @retval #CRYPT_SHA2_INPUT_OVERFLOW input message is overflow * @retval #CRYPT_SECUREC_FAIL secure c function fail. */ int32_t CRYPT_SHA2_512_Update(CRYPT_SHA2_512_Ctx *ctx, const uint8_t *data, uint32_t nbytes); /** * @ingroup LLF Low Level Functions * @par Prototype * @code * int32_t CRYPT_SHA2_512_Final(CRYPT_SHA2_512_Ctx *ctx, uint8_t *digest, uint32_t *len) * @endcode * * @par Purpose * This is used to complete sha512 digest operation on remaining data, and is called at the end of digest operation. * * @par Description * CRYPT_SHA2_512_Final function completes digest operation on remaining data, and is called at the end of digest * operation. CRYPT_SHA2_512_Init must have been called before calling this function. This function calculates the * digest. The memory for digest must already have been allocated. * * @param[in,out] ctx The sha512 ctx * @param[out] digest The digest * @param[in,out] len length of buffer * * @retval #CRYPT_SUCCESS If partial digest is calculated * @retval #CRYPT_NULL_INPUT input arguments is NULL * @retval #CRYPT_SHA2_INPUT_OVERFLOW input message is overflow * @retval #CRYPT_SHA2_OUT_BUFF_LEN_NOT_ENOUGH output buffer is not enough */ int32_t CRYPT_SHA2_512_Final(CRYPT_SHA2_512_Ctx *ctx, uint8_t *digest, uint32_t *len); /** * @ingroup LLF Low Level Functions * * @brief SHA512 deinit function * * @param[in,out] ctx The SHA512 ctx * * @retval #CRYPT_SUCCESS initialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SHA2_512_Deinit(CRYPT_SHA2_512_Ctx *ctx); /** * @ingroup SHA512 * @brief SHA512 copy CTX function * @param dst [out] Pointer to the new SHA512 context. * @param src [in] Pointer to the original SHA512 context. */ int32_t CRYPT_SHA2_512_CopyCtx(CRYPT_SHA2_512_Ctx *dst, const CRYPT_SHA2_512_Ctx *src); /** * @ingroup SHA512 * @brief SHA512 dup CTX function * @param src [in] Pointer to the original SHA512 context. */ CRYPT_SHA2_512_Ctx *CRYPT_SHA2_512_DupCtx(const CRYPT_SHA2_512_Ctx *src); #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup SHA512 * @brief SHA512 get param function * @param ctx [in] Pointer to the SHA512 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_SHA2_512_GetParam(CRYPT_SHA2_512_Ctx *ctx, BSL_Param *param); #else #define CRYPT_SHA2_512_GetParam NULL #endif #endif // HITLS_CRYPTO_SHA512 #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA2 #endif // CRYPT_SHA2_H
2301_79861745/bench_create
crypto/sha2/include/crypt_sha2.h
C
unknown
15,163
/* * 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_SHA256 #include "crypt_arm.h" .arch armv8-a+crypto /* sha256 used constant value. For the data source, see the RFC4634 document. */ .extern g_cryptArmCpuInfo .hidden g_cryptArmCpuInfo .section .rodata .balign 64 .K256: .long 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5 .long 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174 .long 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da .long 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967 .long 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85 .long 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070 .long 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3 .long 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 /* * Macro description: updates the 32-bit plaintext information. W * Input register: * wi_16: W[i-16] * wi_15: W[i-15] * wi_7: W[i-7] * wi_2: W[i-2] * Modify the register: wi_16 w17 w28 * Output register: * wi_16: Latest W[i] value, W[i] = sigma1(W[i-2]) + W[i-7] + sigma0(W[i-15]) + W[i-16] * Function/Macro Call:None */ .macro UPDATE_W wi_16, wi_15, wi_7, wi_2 ror w28, \wi_15, #7 ror w17, \wi_2, #17 eor w28, w28, \wi_15, ror#18 eor w17, w17, \wi_2, ror#19 eor w28, w28, \wi_15, lsr#3 // w28 = sigma0(w[i-15]) eor w17, w17, \wi_2, lsr#10 // w17 = sigma1(W[i-2]) add \wi_16, \wi_16, \wi_7 // + W[i-7] add \wi_16, \wi_16, w28 // + sigma0(w[i-15]) add \wi_16, \wi_16, w17 // + sigma1(W[i-2]) .endm /* * Macro description: Processes the update of a round of hash values in 64 rounds of compression. * Input register: * x19: Point to the address of the corresponding element in the g_k256 constant * wi: Plaintext data after processing * a - h: Intermediate variable of hash value * Modify the register: h d w16 w17 w28 w29 * Output register: * h: Indicates the value after a cyclic update. * d: Indicates the value after a cyclic update. * Function/Macro Call:None */ .macro ONE_ROUND wi, a, b, c, d, e, f, g, h ldr w16, [x19], #4 // K[i] and w17, \f, \e // e&f bic w28, \g, \e // g&(~e) add \h, \h, w16 // h += K[i] eor w29, \e, \e, ror#14 ror w16, \e, #6 orr w17, w17, w28 // Ch(e, f, g) = e&f | g&(~e) add \h, \h, \wi // h += W[i] eor w29, w16, w29, ror#11 // Sigma1(e) = ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25) eor w28, \a, \c // a^c eor w16, \a, \b // a^b add \h, \h, w29 // h += Sigma1(e) and w28, w28, w16 // (a^b)&(a^c) eor w29, \a, \a, ror#9 add \h, \h, w17 // h += Ch(e, f, g) eor w28, w28, \a // Maj(a, b, c) = ((a^b)&(a^c))^a = (a&b)^(b&c)^(a&c) ror w16, \a, #2 add \d, \d, \h // d += h add \h, \h, w28 // h += Maj(a, b, c) eor w29, w16, w29, ror#13 // Sigma0(a) = ROR(a, 2)^ROR(a, 13)^ROR(a, 22) add \h, \h, w29 // h += Sigma0(a) .endm /* * Function Description:Performs 64 rounds of compression calculation based on the input plaintext data * and updates the hash value. * Function prototype:void SHA256CompressMultiBlocks(uint32_t hash[8], const uint8_t *in, uint32_t num); * Input register: * x0: Storage address of the hash value * x1: Pointer to the input data address * x2: Number of 64 rounds of cycles * Modify the register: x0-x17 * Output register: None * Function/Macro Call: None * */ .text .balign 16 .global SHA256CompressMultiBlocks .type SHA256CompressMultiBlocks, %function SHA256CompressMultiBlocks: cbz x2, .Lend_sha256 /* If the SHA256 cryptography extension instruction is supported, go to. */ adrp x5, g_cryptArmCpuInfo ldr w6, [x5, #:lo12:g_cryptArmCpuInfo] tst w6, #CRYPT_ARM_SHA256 bne SHA256CryptoExt /* Extension instructions are not supported. Base instructions are used. */ stp x29, x30, [sp, #-112]! add x29, sp, #0 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 - h */ ldp w20, w21, [x0] ldp w22, w23, [x0, #4*2] ldp w24, w25, [x0, #4*4] ldp w26, w27, [x0, #4*6] str x0, [sp, #96] mov x16, x1 // Enter Value Address lsl x30, x2, #6 // Number of times to process 2^6 = 64 /* w0-w15 are used to record input values W[i] and temporary registers */ .Lloop_compress_64: /* Start a 64-round process */ sub x30, x30, #16 adrp x19, .K256 add x19, x19, :lo12:.K256 /* 8 bytes are loaded each time, and then two rounds are processed. */ ldp w0, w1, [x16] // load input value ldp w2, w3, [x16, #4*2] ldp w4, w5, [x16, #4*4] ldp w6, w7, [x16, #4*6] ldp w8, w9, [x16, #4*8] ldp w10, w11, [x16, #4*10] ldp w12, w13, [x16, #4*12] ldp w14, w15, [x16, #4*14] add x16, x16, #64 str x16, [sp, #104] #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 /* w16 w17 w28 w29 used as a temporary register */ ONE_ROUND w0, w20, w21, w22, w23, w24, w25, w26, w27 ONE_ROUND w1, w27, w20, w21, w22, w23, w24, w25, w26 ONE_ROUND w2, w26, w27, w20, w21, w22, w23, w24, w25 ONE_ROUND w3, w25, w26, w27, w20, w21, w22, w23, w24 ONE_ROUND w4, w24, w25, w26, w27, w20, w21, w22, w23 ONE_ROUND w5, w23, w24, w25, w26, w27, w20, w21, w22 ONE_ROUND w6, w22, w23, w24, w25, w26, w27, w20, w21 ONE_ROUND w7, w21, w22, w23, w24, w25, w26, w27, w20 ONE_ROUND w8, w20, w21, w22, w23, w24, w25, w26, w27 ONE_ROUND w9, w27, w20, w21, w22, w23, w24, w25, w26 ONE_ROUND w10, w26, w27, w20, w21, w22, w23, w24, w25 ONE_ROUND w11, w25, w26, w27, w20, w21, w22, w23, w24 ONE_ROUND w12, w24, w25, w26, w27, w20, w21, w22, w23 ONE_ROUND w13, w23, w24, w25, w26, w27, w20, w21, w22 ONE_ROUND w14, w22, w23, w24, w25, w26, w27, w20, w21 ONE_ROUND w15, w21, w22, w23, w24, w25, w26, w27, w20 .Lloop_compress_16_63: /* Start 16-31, 32-47, 48-63 compression */ sub x30, x30, #16 /* 0 */ UPDATE_W w0, w1, w9, w14 ONE_ROUND w0, w20, w21, w22, w23, w24, w25, w26, w27 /* 1 */ UPDATE_W w1, w2, w10, w15 ONE_ROUND w1, w27, w20, w21, w22, w23, w24, w25, w26 /* 2 */ UPDATE_W w2, w3, w11, w0 ONE_ROUND w2, w26, w27, w20, w21, w22, w23, w24, w25 /* 3 */ UPDATE_W w3, w4, w12, w1 ONE_ROUND w3, w25, w26, w27, w20, w21, w22, w23, w24 /* 4 */ UPDATE_W w4, w5, w13, w2 ONE_ROUND w4, w24, w25, w26, w27, w20, w21, w22, w23 /* 5 */ UPDATE_W w5, w6, w14, w3 ONE_ROUND w5, w23, w24, w25, w26, w27, w20, w21, w22 /* 6 */ UPDATE_W w6, w7, w15, w4 ONE_ROUND w6, w22, w23, w24, w25, w26, w27, w20, w21 /* 7 */ UPDATE_W w7, w8, w0, w5 ONE_ROUND w7, w21, w22, w23, w24, w25, w26, w27, w20 /* 8 */ UPDATE_W w8, w9, w1, w6 ONE_ROUND w8, w20, w21, w22, w23, w24, w25, w26, w27 /* 9 */ UPDATE_W w9, w10, w2, w7 ONE_ROUND w9, w27, w20, w21, w22, w23, w24, w25, w26 /* 10 */ UPDATE_W w10, w11, w3, w8 ONE_ROUND w10, w26, w27, w20, w21, w22, w23, w24, w25 /* 11 */ UPDATE_W w11, w12, w4, w9 ONE_ROUND w11, w25, w26, w27, w20, w21, w22, w23, w24 /* 12 */ UPDATE_W w12, w13, w5, w10 ONE_ROUND w12, w24, w25, w26, w27, w20, w21, w22, w23 /* 13 */ UPDATE_W w13, w14, w6, w11 ONE_ROUND w13, w23, w24, w25, w26, w27, w20, w21, w22 /* 14 */ UPDATE_W w14, w15, w7, w12 ONE_ROUND w14, w22, w23, w24, w25, w26, w27, w20, w21 /* 15 */ UPDATE_W w15, w0, w8, w13 ONE_ROUND w15, w21, w22, w23, w24, w25, w26, w27, w20 /* If the processing length is less than 64 bytes, the loop continues. */ tst x30, #63 bne .Lloop_compress_16_63 /* Stores a - h information. */ ldr x0, [sp, #96] ldp w10, w11, [x0] ldp w12, w13, [x0, #4*2] ldp w14, w15, [x0, #4*4] ldp w16, w17, [x0, #4*6] add w20, w20, w10 add w21, w21, w11 add w22, w22, w12 add w23, w23, w13 stp w20, w21, [x0] add w24, w24, w14 add w25, w25, w15 stp w22, w23, [x0, #4*2] add w26, w26, w16 add w27, w27, w17 stp w24, w25, [x0, #4*4] stp w26, w27, [x0, #4*6] ldr x16, [sp, #104] /* If the remaining length is not processed, the processing continues for 64 rounds. */ cbnz x30, .Lloop_compress_64 /* The function returns */ 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], #112 .Lend_sha256: ret .size SHA256CompressMultiBlocks, .-SHA256CompressMultiBlocks /* * Function Description:Performs 64 rounds of compression calculation based on the input plaintext data * and updates the hash value * Function prototype:void SHA256CryptoExt(uint32_t hash[8], const uint8_t *in, uint32_t num); * Input register: * x0: Storage address of the hash value * x1: Pointer to the input data address * x2: Number of 64 rounds of cycles * Modify the register: x1-x4, v0-v5, v16-v23 * Output register: None * Function/Macro Call: None * */ .text .balign 16 .type SHA256CryptoExt, %function SHA256CryptoExt: ld1 {v4.4s-v5.4s}, [x0] .Lloop_compress_64_ext: adrp x4, .K256 add x4, x4, :lo12:.K256 sub x2, x2, #1 /* 0-15 */ ld1 {v16.16b-v19.16b}, [x1], #64 mov v0.16b, v4.16b mov v1.16b, v5.16b rev32 v16.16b, v16.16b ld1 {v20.4s}, [x4], #16 rev32 v17.16b, v17.16b ld1 {v21.4s}, [x4], #16 rev32 v18.16b, v18.16b ld1 {v22.4s}, [x4], #16 add v20.4s, v20.4s, v16.4s rev32 v19.16b, v19.16b ld1 {v23.4s}, [x4], #16 sha256su0 v16.4s, v17.4s mov v2.16b, v0.16b sha256h q0, q1, v20.4s sha256h2 q1, q2, v20.4s add v21.4s, v21.4s, v17.4s sha256su1 v16.4s, v18.4s, v19.4s ld1 {v20.4s}, [x4], #16 sha256su0 v17.4s, v18.4s mov v3.16b, v0.16b sha256h q0, q1, v21.4s sha256h2 q1, q3, v21.4s add v22.4s, v22.4s, v18.4s sha256su1 v17.4s, v19.4s, v16.4s ld1 {v21.4s}, [x4], #16 sha256su0 v18.4s, v19.4s mov v2.16b, v0.16b sha256h q0, q1, v22.4s sha256h2 q1, q2, v22.4s add v23.4s, v23.4s, v19.4s sha256su1 v18.4s, v16.4s, v17.4s ld1 {v22.4s}, [x4], #16 sha256su0 v19.4s, v16.4s mov v3.16b, v0.16b sha256h q0, q1, v23.4s sha256h2 q1, q3, v23.4s add v20.4s, v20.4s, v16.4s sha256su1 v19.4s, v17.4s, v18.4s ld1 {v23.4s}, [x4], #16 /* 16-31 */ sha256su0 v16.4s, v17.4s mov v2.16b, v0.16b sha256h q0, q1, v20.4s sha256h2 q1, q2, v20.4s add v21.4s, v21.4s, v17.4s sha256su1 v16.4s, v18.4s, v19.4s ld1 {v20.4s}, [x4], #16 sha256su0 v17.4s, v18.4s mov v3.16b, v0.16b sha256h q0, q1, v21.4s sha256h2 q1, q3, v21.4s add v22.4s, v22.4s, v18.4s sha256su1 v17.4s, v19.4s, v16.4s ld1 {v21.4s}, [x4], #16 mov v2.16b, v0.16b sha256su0 v18.4s, v19.4s sha256h q0, q1, v22.4s sha256h2 q1, q2, v22.4s add v23.4s, v23.4s, v19.4s sha256su1 v18.4s, v16.4s, v17.4s ld1 {v22.4s}, [x4], #16 sha256su0 v19.4s, v16.4s mov v3.16b, v0.16b sha256h q0, q1, v23.4s sha256h2 q1, q3, v23.4s add v20.4s, v20.4s, v16.4s sha256su1 v19.4s, v17.4s, v18.4s ld1 {v23.4s}, [x4], #16 /* 32-47 */ sha256su0 v16.4s, v17.4s mov v2.16b, v0.16b sha256h q0, q1, v20.4s sha256h2 q1, q2, v20.4s add v21.4s, v21.4s, v17.4s sha256su1 v16.4s, v18.4s, v19.4s ld1 {v20.4s}, [x4], #16 sha256su0 v17.4s, v18.4s mov v3.16b, v0.16b sha256h q0, q1, v21.4s sha256h2 q1, q3, v21.4s add v22.4s, v22.4s, v18.4s sha256su1 v17.4s, v19.4s, v16.4s ld1 {v21.4s}, [x4], #16 sha256su0 v18.4s, v19.4s mov v2.16b, v0.16b sha256h q0, q1, v22.4s sha256h2 q1, q2, v22.4s add v23.4s, v23.4s, v19.4s sha256su1 v18.4s, v16.4s, v17.4s ld1 {v22.4s}, [x4], #16 sha256su0 v19.4s, v16.4s mov v3.16b, v0.16b sha256h q0, q1, v23.4s sha256h2 q1, q3, v23.4s add v20.4s, v20.4s, v16.4s sha256su1 v19.4s, v17.4s, v18.4s ld1 {v23.4s}, [x4], #16 /* 48-63 */ mov v2.16b, v0.16b sha256h q0, q1, v20.4s add v21.4s, v21.4s, v17.4s sha256h2 q1, q2, v20.4s mov v3.16b, v0.16b sha256h q0, q1, v21.4s add v22.4s, v22.4s, v18.4s sha256h2 q1, q3, v21.4s mov v2.16b, v0.16b sha256h q0, q1, v22.4s add v23.4s, v23.4s, v19.4s sha256h2 q1, q2, v22.4s mov v3.16b, v0.16b sha256h q0, q1, v23.4s sha256h2 q1, q3, v23.4s /* Add the original hash value */ add v4.4s, v4.4s, v0.4s add v5.4s, v5.4s, v1.4s cbnz x2, .Lloop_compress_64_ext /* Output result */ st1 {v4.4s-v5.4s}, [x0] ret .size SHA256CryptoExt, .-SHA256CryptoExt #endif
2301_79861745/bench_create
crypto/sha2/src/asm/sha2_256_armv8.S
Motorola 68K Assembly
unknown
15,516
/* * 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_SHA256 .file "sha2_256_x86_64.S" .set HashAddr, %rdi .set InAddr, %rsi .set NUM, %rdx .set tempFirst, %ebp .set tempThird, %ebx .set tempFifth, %edi .set avx2Temp1, %ymm4 .set avx2Temp2, %ymm5 .set avx2Temp3, %ymm6 .set avx2Temp4, %ymm7 .set avx2Temp5, %ymm10 .set avx2Temp6, %ymm11 .set avx2Temp7, %ymm15 .set BlockFrontMessageW3_0, %xmm0 .set BlockFrontMessageW7_4, %xmm1 .set BlockFrontMessageW11_8, %xmm2 .set BlockFrontMessageW15_12, %xmm3 .set g_maskMerge, %ymm12 .set g_maskShift, %ymm13 .set g_maskTransformEndian, %ymm14 /* Constant value used by sha256. For details about the data source, see the RFC4634 document. */ .section .rodata .align 64 .type g_K256, %object g_K256: .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 .long 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5 .long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 .long 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 .long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3 .long 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3 .long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 .long 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 .long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc .long 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc .long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da .long 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da .long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7 .long 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7 .long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 .long 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 .long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13 .long 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13 .long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 .long 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 .long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3 .long 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3 .long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070 .long 0xd192e819,0xd6990624,0xf40e3585,0x106aa070 .long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5 .long 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5 .long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 .long 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 .long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208 .long 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208 .long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 .long 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 .size g_K256, .-g_K256 /* Mask block */ .balign 64 .type g_mask, %object g_mask: .long 0x00010203,0x04050607, 0x08090a0b,0x0c0d0e0f .long 0x00010203,0x04050607, 0x08090a0b,0x0c0d0e0f .long 0x03020100,0x0b0a0908, 0xffffffff,0xffffffff .long 0x03020100,0x0b0a0908, 0xffffffff,0xffffffff .long 0xffffffff,0xffffffff, 0x03020100,0x0b0a0908 .long 0xffffffff,0xffffffff, 0x03020100,0x0b0a0908 .size g_mask, .-g_mask /* * Macro description: Processes the fast extension of four messages of two blocks at the same time * and completes the four-round compression function of the first block. * Input register: * WkAddr: Address of the stack space where wi+kt is located. * a - h: Intermediate variable of hash value * Modify the register: r8d-r15d, ebp, eax, ebx, ecx, edi, ymm0-ymm10 * Output register: * a-h: Value after four rounds of cyclic update * B3_0: Value after data extension * Naming convention: * B3_0: w3-w0 * B7_4: w7-w4 * B11_8: w11-w8 * B15_12: w15-w12 * Function/Macro Call:None * Implementation Description: * ONE_ROUND algorithm implementation: * For t = 0 to 63, T1 = h + BSIG1(e) + CH(e,f,g) + Kt + Wt * T2 = BSIG0(a) + MAJ(a,b,c) * h = g, g = f, f = e, e = d + T1, d = c, c = b, b = a, a = T1 + T2 * CH( x, y, z) = (x AND y) XOR ( (NOT x) AND z) CH(e,f,g) * MAJ(a, b, c) = (a AND b) XOR (a AND c) XOR (b AND c) * = CH(a^b, c, b) * = ((a XOR b) AND c) XOR ((NOT(a XOR b)) AND b) * = (b XOR c) AND (a XOR b) XOR b * BSIG0(x) = ROTR^2(x) XOR ROTR^13(x) XOR ROTR^22(x) BSIG0(a) * BSIG1(x) = ROTR^6(x) XOR ROTR^11(x) XOR ROTR^25(x) BSIG1(e) * Optimization idea: b xor c in the next round of MAJ is a xor b in the previous round of MAJ * to avoid redundant calculation. * * UPDATE_4W algorithm implementation: * For t = 0 to 15 Wt = W0_W15(input w0-w15) * For t = 16 to 63 Wt = SSIG1(W(t-2)) + W(t-7) + SSIG0(w(t-15)) + W(t-16) * SSIG0(x) = ROTR^7(x) XOR ROTR^18(x) XOR SHR^3(x) * SSIG1(x) = ROTR^17(x) XOR ROTR^19(x) XOR SHR^10(x) * Optimization idea: Optimization point 1: Each WI message block is 32-bit, and the xmm register is * a 128-bit register. Therefore, the common operation of four WI messages can be * performed at the same time (SSIG0, W(t-16), W(t-7)). * Due to the dependency of wi, four wis are calculated each time as the * optimal solution found so far. * Optimization point 2: The ymm register is a 256-bit register. Therefore, two rounds * of 128-bit calculation can be performed at the same time, and two blocks can be used * for the same calculation. */ .macro FOUR_ROUND_UPDATE_4W a, b, c, d, e, f, g, h, tempSwitch2, tempSwitch4, WkAddr, B3_0, B7_4, B11_8, B15_12 vpalignr $4,\B3_0,\B7_4,avx2Temp1 // avx2Temp1->w4_1 add \WkAddr(%rsp),\h // h += Kt + Wt and \e, tempFifth // e&f rorx $6, \e, \tempSwitch2 // ROTR^6(e) add tempFirst, \a // a += BSIG0(a) from last round rorx $11, \e, tempThird // ROTR^11(e) andn \g, \e, tempFirst // (~e)&g xor \tempSwitch2, tempThird // ROTR^6(e) ^ ROTR^11(e) xor tempFirst, tempFifth // CH(e,f,g) vpshufd $250, \B15_12, avx2Temp5 rorx $25, \e, \tempSwitch2 // ROTR^25(e) add tempFifth, \h // h += CH(e,f,g) xor \tempSwitch2, tempThird // BSIG1(e) vpalignr $4, \B11_8, \B15_12, avx2Temp2 // avx2Temp2->w12_9 vpslld $14, avx2Temp1, avx2Temp4 // w4_1<<datum line 14 rorx $2, \a, tempFirst // ROTR^2(a) mov \a, \tempSwitch2 // a add tempThird, \h // h += BSIG1(e)[h->T1] vpsrld $3, avx2Temp1, avx2Temp3 // w4_1>>datum line 3 rorx $13, \a, tempFifth // ROTR^13(a) xor \b, \tempSwitch2 // b^a for next round b^c add \h, \d // d += T1 vpsrld $10, avx2Temp5, avx2Temp6 // >>10 xor tempFifth, tempFirst // ROTR^2(a) ^ ROTR^13(a) and \tempSwitch2, \tempSwitch4 // (b^a) & (b^c) vpsrld $7, avx2Temp1, avx2Temp1 // >>7 vpaddd avx2Temp2, \B3_0, \B3_0 rorx $22, \a, tempThird // ROTR^22(a) add 4+\WkAddr(%rsp),\g // h += Kt + Wt xor \b, \tempSwitch4 // Maj(a,b,c) vpxor avx2Temp3, avx2Temp4, avx2Temp3 // 3 xor 14 mov \e, tempFifth // for next round f xor tempThird, tempFirst // BSIG0(a) vpsrlq $17, avx2Temp5, avx2Temp7 // >>17 add \tempSwitch4, \h // h += Maj(a,b,c) and \d, tempFifth // e&f rorx $6, \d, \tempSwitch4 add tempFirst, \h // a += BSIG0(a) from last round vpxor avx2Temp3, avx2Temp1, avx2Temp3 // 7xor14xor3 vpsrlq $19, avx2Temp5, avx2Temp5 // >>19 rorx $11, \d, tempThird andn \f, \d, tempFirst xor \tempSwitch4, tempThird vpsrld $11, avx2Temp1, avx2Temp1 // >>18 xor tempFirst, tempFifth rorx $25, \d, \tempSwitch4 add tempFifth, \g xor \tempSwitch4, tempThird vpslld $11, avx2Temp4, avx2Temp4 // <<25 rorx $2, \h, tempFirst mov \h, \tempSwitch4 add tempThird, \g rorx $13, \h,tempFifth xor \a, \tempSwitch4 vpxor avx2Temp7, avx2Temp6, avx2Temp7 // 17xor10 add \g, \c xor tempFifth, tempFirst vpxor avx2Temp3, avx2Temp1, avx2Temp3 // 7xor14xor3xor18 and \tempSwitch4, \tempSwitch2 rorx $22, \h, tempThird add 8+\WkAddr(%rsp),\f xor \a, \tempSwitch2 vpxor avx2Temp7, avx2Temp5, avx2Temp7 // 17xor10xor19 mov \d, tempFifth xor tempThird, tempFirst add \tempSwitch2, \g vpshufb g_maskMerge, avx2Temp7, avx2Temp7 // BSIG1 w15_14 vpxor avx2Temp3, avx2Temp4, avx2Temp3 // 7xor14xor3xor18xor25 and \c, tempFifth rorx $6, \c, \tempSwitch2 add tempFirst, \g rorx $11, \c, tempThird vpaddd avx2Temp3, \B3_0, \B3_0 // BSIG0+w(t-16)+w(t-7) andn \e, \c, tempFirst xor \tempSwitch2, tempThird xor tempFirst, tempFifth rorx $25, \c, \tempSwitch2 add tempFifth, \f xor \tempSwitch2, tempThird rorx $2, \g, tempFirst mov \g, \tempSwitch2 add tempThird, \f rorx $13, \g, tempFifth vpaddd \B3_0, avx2Temp7, \B3_0 // w17_16 xor \h, \tempSwitch2 add \f, \b xor tempFifth, tempFirst and \tempSwitch2, \tempSwitch4 vpshufd $80, \B3_0, avx2Temp1 rorx $22, \g, tempThird add 12+\WkAddr(%rsp),\e xor \h, \tempSwitch4 mov \c, tempFifth xor tempThird, tempFirst add \tempSwitch4, \f vpsrld $10, avx2Temp1, avx2Temp2 // >>10 and \b, tempFifth rorx $6, \b, \tempSwitch4 vpsrlq $17, avx2Temp1, avx2Temp3 // >>17 add tempFirst, \f rorx $11, \b, tempThird andn \d, \b, tempFirst xor \tempSwitch4, tempThird vpsrlq $19,avx2Temp1, avx2Temp1 // >>19 xor tempFirst, tempFifth rorx $25, \b, \tempSwitch4 add tempFifth, \e xor \tempSwitch4, tempThird vpxor avx2Temp2, avx2Temp3, avx2Temp3 // 10xor17 rorx $2, \f, tempFirst mov \f, \tempSwitch4 add tempThird, \e rorx $13, \f, tempFifth xor \g, \tempSwitch4 vpxor avx2Temp3, avx2Temp1, avx2Temp3 // 10xor17xor19 add \e, \a xor tempFifth, tempFirst and \tempSwitch4, \tempSwitch2 rorx $22, \f, tempThird vpshufb g_maskShift, avx2Temp3, avx2Temp3 // BSIG1(W17_16)Move to the desired location xor \g, \tempSwitch2 mov \b, tempFifth xor tempThird, tempFirst add \tempSwitch2, \e vpaddd avx2Temp3, \B3_0, \B3_0 // W19_16 .endm /* * Macro description: Processes the update of a round of hash values in 64 rounds of compression. * Input register: * wkAddr: wi+kt Stack space address. * a - h: Intermediate variable of hash value * Modify the register: r8d-r15d, ebp, eax, ebx, ecx, edi * Output register: * a-h: Indicates the value after a cyclic update. * Function/Macro Call:None * ONE_ROUND Algorithm Implementation: * For t = 0 to 63, T1 = h + BSIG1(e) + CH(e,f,g) + Kt + Wt * T2 = BSIG0(a) + MAJ(a,b,c) * h = g, g = f, f = e, e = d + T1, d = c, c = b, b = a, a = T1 + T2 * CH( x, y, z) = (x AND y) XOR ( (NOT x) AND z) CH(e,f,g) * MAJ(a, b, c) = (a AND b) XOR (a AND c) XOR (b AND c) * = CH(a^b, c, b) * = ((a XOR b) AND c) XOR ((NOT(a XOR b)) AND b) * = (b XOR c) AND (a XOR b) XOR b * BSIG0(x) = ROTR^2(x) XOR ROTR^13(x) XOR ROTR^22(x) BSIG0(a) * BSIG1(x) = ROTR^6(x) XOR ROTR^11(x) XOR ROTR^25(x) BSIG1(e) * Optimization idea: b xor c in the next round of MAJ is a xor b in the * previous round of MAJ to avoid redundant calculation. * Note: At the end of each round, the tempSwitch2 and tempSwitch4 of the next round need to be exchanged. */ .macro ONE_ROUND a, b, c, d, e, f, g, h, tempSwitch2, tempSwitch4, WkAddr rorx $11, \e, tempThird // ROTR^11(e) rorx $6, \e, \tempSwitch2 // ROTR^6(e) add tempFirst, \a // a += BSIG0(a) from last round and \e, tempFifth // e&f andn \g, \e, tempFirst // (~e)&g xor \tempSwitch2, tempThird // ROTR^6(e) ^ ROTR^11(e) add \WkAddr(%rsp),\h // h += Kt + Wt xor tempFirst, tempFifth // CH(e,f,g) rorx $25, \e, \tempSwitch2 // ROTR^25(e) add tempFifth, \h // h += CH(e,f,g) xor \tempSwitch2, tempThird // BSIG1(e) rorx $2, \a, tempFirst // ROTR^2(a) mov \a, \tempSwitch2 // a leal (tempThird, \h), \h // h += BSIG1(e)[h->T1] rorx $13, \a, tempFifth // ROTR^13(a) xor \b, \tempSwitch2 // b^a for next round b^c add \h, \d // d += T1 xor tempFifth, tempFirst // ROTR^2(a) ^ ROTR^13(a) and \tempSwitch2, \tempSwitch4 // (b^a) & (b^c) rorx $22, \a, tempThird // ROTR^22(a) xor \b, \tempSwitch4 // Maj(a,b,c) mov \e, tempFifth // for next round f xor tempThird, tempFirst // BSIG0(a) add \tempSwitch4, \h // h += Maj(a,b,c) .endm /* * Function description: Performs 64 rounds of compression calculation based on the input plaintext data and updates the hash value. * function prototype:void SHA256CompressMultiBlocks(uint32_t hash[8], const uint8_t *in, uint32_t num); * Input register: * rdi: Storage address of the hash value * rsi: Pointer to the input data address (Wi) * rdx: Number of 64 rounds of cycles. (You need to do several blocks, that is, you need to do several loops.) * Modify the register: r0-r14 * Output register: None * Function/Macro Call: None */ .text .globl SHA256CompressMultiBlocks .type SHA256CompressMultiBlocks,%function .align 4 SHA256CompressMultiBlocks: .cfi_startproc /* Determine whether to end the process directly. */ cmp $0, NUM je .LEND_SHA256 /* Pop-stack/push stack protection */ pushq %r14 pushq %rbx pushq %rbp pushq %r12 pushq %r13 pushq %r15 /* The pre-stored stack space and 32-byte address are aligned. The original RSP value is added to the stack and the mask is assigned. */ mov %rsp, %r14 mov 0(HashAddr), %r8d sub $600, %rsp vmovdqa g_mask + 0(%rip), g_maskTransformEndian mov 4(HashAddr), %r9d mov 8(HashAddr), %r10d and $-256, %rsp vmovdqa g_mask + 64(%rip), g_maskShift mov 12(HashAddr), %r11d mov %r14, 0(%rsp) /* r8d-r15d: a-h */ mov 16(HashAddr), %r12d mov 20(HashAddr), %r13d vmovdqa g_mask + 32(%rip), g_maskMerge mov 24(HashAddr), %r14d mov 28(HashAddr), %r15d .LEND_SHA256_LOOP: mov InAddr, %rcx /* Loads the data of a block to the lower 128 bits of the ymm register. */ vmovdqu 0(InAddr), BlockFrontMessageW3_0 vmovdqu 16(InAddr), BlockFrontMessageW7_4 vmovdqu 32(InAddr), BlockFrontMessageW11_8 vmovdqu 48(InAddr), BlockFrontMessageW15_12 /* block Judgment condition processing */ leaq 64(InAddr), InAddr cmp $1, NUM cmovne InAddr, %rcx // If num is greater than 1, rcx points to the next block. /* Load the data of another block to the upper 128 bits of the ymm register. */ vinserti128 $1, 0(%rcx), %ymm0, %ymm0 vinserti128 $1, 16(%rcx), %ymm1, %ymm1 vpshufb g_maskTransformEndian, %ymm0, %ymm0 mov NUM, 16(%rsp) vinserti128 $1, 32(%rcx), %ymm2, %ymm2 mov HashAddr, 24(%rsp) vpshufb g_maskTransformEndian, %ymm1, %ymm1 vinserti128 $1, 48(%rcx), %ymm3, %ymm3 vpshufb g_maskTransformEndian, %ymm2, %ymm2 add $64, %rcx leaq g_K256(%rip), NUM /* Little-endian order to big-endian order, wi + kt:ymm9-11*/ mov %rcx, 8(%rsp) leaq 32(%rsp), %rsp vpaddd 0(NUM), %ymm0, %ymm8 mov %r9d, %ecx vpaddd 32(NUM), %ymm1, %ymm9 vmovdqa %ymm8, 0(%rsp) vpshufb g_maskTransformEndian, %ymm3, %ymm3 xor %ebp, %ebp vpaddd 64(NUM), %ymm2, %ymm10 vmovdqu %ymm9, 32(%rsp) xor %r10d, %ecx vpaddd 96(NUM), %ymm3, %ymm11 mov %r13d, %edi vmovdqa %ymm10, 64(%rsp) vmovdqu %ymm11, 96(%rsp) .LEND_SHA256_ROUND_00_47: /* Next round wi + kt: ymm9-11, 16 rounds of compression + 4 rounds of message block expansion */ /* FOUR_ROUND_UPDATE_4W a, b, c, d, e, f, g, h, tempSwitch2,tempSwitch4, WkAddr,B3_0, B7_4, B11_8, B15_12 */ FOUR_ROUND_UPDATE_4W %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 0, %ymm0, %ymm1, %ymm2, %ymm3 leaq 128(NUM), NUM FOUR_ROUND_UPDATE_4W %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 32, %ymm1, %ymm2, %ymm3, %ymm0 vpaddd 0(NUM), %ymm0, %ymm8 FOUR_ROUND_UPDATE_4W %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 64, %ymm2, %ymm3, %ymm0, %ymm1 vpaddd 32(NUM), %ymm1, %ymm9 vmovdqa %ymm8, 128(%rsp) FOUR_ROUND_UPDATE_4W %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 96, %ymm3, %ymm0, %ymm1, %ymm2 vpaddd 64(NUM), %ymm2, %ymm10 vmovdqa %ymm9, 160(%rsp) vpaddd 96(NUM), %ymm3, %ymm11 vmovdqu %ymm10, 192(%rsp) vmovdqa %ymm11, 224(%rsp) /* Next round wi + kt: ymm9-11, 16 rounds of compression + 4 rounds of message block expansion */ /* FOUR_ROUND_UPDATE_4W a, b, c, d, e, f, g, h, tempSwitch2,tempSwitch4, WkAddr,B19_16, B23_20, B27_24, B31_27 */ FOUR_ROUND_UPDATE_4W %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 128, %ymm0, %ymm1, %ymm2, %ymm3 leaq 128(NUM), NUM FOUR_ROUND_UPDATE_4W %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 160, %ymm1, %ymm2, %ymm3, %ymm0 vpaddd 0(NUM), %ymm0, %ymm8 FOUR_ROUND_UPDATE_4W %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 192, %ymm2, %ymm3, %ymm0, %ymm1 vpaddd 32(NUM), %ymm1, %ymm9 vmovdqa %ymm8, 256(%rsp) FOUR_ROUND_UPDATE_4W %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 224, %ymm3, %ymm0, %ymm1, %ymm2 vpaddd 64(NUM), %ymm2, %ymm10 vmovdqa %ymm9, 288(%rsp) vpaddd 96(NUM), %ymm3, %ymm11 vmovdqu %ymm10, 320(%rsp) vmovdqa %ymm11, 352(%rsp) /* Next round wi + kt: ymm9-11, 16 rounds of compression + 4 rounds of message block expansion */ /* FOUR_ROUND_UPDATE_4W a, b, c, d, e, f, g, h, tempSwitch2,tempSwitch4, WkAddr,B35_32, B39_36, B43_40, B47_44 */ FOUR_ROUND_UPDATE_4W %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 256, %ymm0, %ymm1, %ymm2, %ymm3 leaq 128(NUM), NUM FOUR_ROUND_UPDATE_4W %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 288, %ymm1, %ymm2, %ymm3, %ymm0 vpaddd 0(NUM), %ymm0, %ymm8 FOUR_ROUND_UPDATE_4W %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 320, %ymm2, %ymm3, %ymm0, %ymm1 vpaddd 32(NUM), %ymm1, %ymm9 vmovdqa %ymm8, 384(%rsp) FOUR_ROUND_UPDATE_4W %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 352, %ymm3, %ymm0, %ymm1, %ymm2 vpaddd 64(NUM), %ymm2, %ymm10 vmovdqa %ymm9, 416(%rsp) vpaddd 96(NUM), %ymm3, %ymm11 vmovdqu %ymm10, 448(%rsp) vmovdqa %ymm11, 480(%rsp) .LEND_SHA256_ROUND_48_63: /* ONE_ROUND a, b, c, d, e, f, g, h, tempSwitch2, Fourth, WkAddr */ ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 384 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 388 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 392 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 396 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 416 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 420 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 424 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 428 ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 448 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 452 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 456 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 460 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 480 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 484 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 488 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 492 sub $32, %rsp add %ebp, %r8d // a+=BSIG0 mov 24(%rsp), HashAddr /* Update the storage hash value. */ add 0(HashAddr), %r8d add 4(HashAddr), %r9d mov %r8d, 0(HashAddr) add 8(HashAddr), %r10d mov %r9d, 4(HashAddr) add 12(HashAddr), %r11d mov %r10d, 8(HashAddr) add 16(HashAddr), %r12d mov 16(%rsp), NUM mov %r11d, 12(HashAddr) add 20(HashAddr), %r13d mov %r12d, 16(HashAddr) add 24(HashAddr), %r14d mov %r13d, 20(HashAddr) add 28(HashAddr), %r15d mov %r14d, 24(HashAddr) mov %r15d, 28(HashAddr) cmp $1, NUM je .LEND_SHA256_FINFISH_INITIAL /* Data compression of the second block */ xor %ebp, %ebp mov %r9d, %ecx xor %r10d, %ecx mov %r13d, %edi .LEND_SHA256_NEXT_BLOCK: /* 0-15 */ /* ONE_ROUND a, b, c, d, e, f, g, h, tempSwitch2,tempSwitch4, WkAddr */ ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 16+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 20+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 24+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 28+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 48+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 52+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 56+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 60+32 ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 80+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 84+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 88+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 92+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 112+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 116+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 120+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 124+32 /* 16-31 */ ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 16+128+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 20+128+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 24+128+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 28+128+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 48+128+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 52+128+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 56+128+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 60+128+32 ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 80+128+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 84+128+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 88+128+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 92+128+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 112+128+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 116+128+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 120+128+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 124+128+32 /* 32-47 */ ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 16+256+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 20+256+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 24+256+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 28+256+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 48+256+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 52+256+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 56+256+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 60+256+32 ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 80+256+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 84+256+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 88+256+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 92+256+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 112+256+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 116+256+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 120+256+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 124+256+32 /* 48-63 */ ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 16+384+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 20+384+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 24+384+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 28+384+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 48+384+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 52+384+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 56+384+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 60+384+32 ONE_ROUND %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %eax, %ecx, 80+384+32 ONE_ROUND %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %ecx, %eax, 84+384+32 ONE_ROUND %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %r13d, %eax, %ecx, 88+384+32 ONE_ROUND %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %r12d, %ecx, %eax, 92+384+32 ONE_ROUND %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %r11d, %eax, %ecx, 112+384+32 ONE_ROUND %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %r10d, %ecx, %eax, 116+384+32 ONE_ROUND %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %r9d, %eax, %ecx, 120+384+32 ONE_ROUND %r9d, %r10d, %r11d, %r12d, %r13d, %r14d, %r15d, %r8d, %ecx, %eax, 124+384+32 mov 24(%rsp), HashAddr lea (%ebp, %r8d), %r8d // a+=BSIG0 /* Update the storage hash value. */ add 0(HashAddr), %r8d add 4(HashAddr), %r9d mov %r8d, 0(HashAddr) add 8(HashAddr), %r10d mov %r9d, 4(HashAddr) add 12(HashAddr), %r11d mov %r10d, 8(HashAddr) add 16(HashAddr), %r12d mov %r11d, 12(HashAddr) add 20(HashAddr), %r13d mov %r12d, 16(HashAddr) mov 8(%rsp), InAddr add 24(HashAddr), %r14d mov %r13d, 20(HashAddr) mov 16(%rsp), NUM add 28(HashAddr), %r15d mov %r14d, 24(HashAddr) mov %r15d, 28(HashAddr) sub $2, NUM ja .LEND_SHA256_LOOP .LEND_SHA256_FINFISH_INITIAL: /* Registers and pointers are reset. */ mov 0(%rsp), %rsp popq %r15 popq %r13 popq %r12 popq %rbp popq %rbx popq %r14 .LEND_SHA256: ret .cfi_endproc .size SHA256CompressMultiBlocks, .-SHA256CompressMultiBlocks #endif
2301_79861745/bench_create
crypto/sha2/src/asm/sha2_256_x86_64.S
Unix Assembly
unknown
30,666
/* * 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_SHA512 .arch armv8-a+crypto /* sha512 used constant value. For the data source, see the RFC4634 document. */ .section .rodata .balign 64 .K512: .quad 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc .quad 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118 .quad 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2 .quad 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694 .quad 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65 .quad 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5 .quad 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4 .quad 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70 .quad 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df .quad 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b .quad 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30 .quad 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8 .quad 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8 .quad 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3 .quad 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec .quad 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b .quad 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178 .quad 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b .quad 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c .quad 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 /** * Macro description: Update the processed 64-bit plaintext information W. * Input register: * wi_16: W[i-16] * wi_15: W[i-15] * wi_7: W[i-7] * wi_2: W[i-2] * Modify the register: wi_16 x17 x28. * Output register: * wi_16: latest W[i] value, W[i] = sigma1(W[i-2]) + W[i-7] + sigma0(W[i-15]) + W[i-16] * Function/Macro Call: None */ .macro UPDATE_W wi_16, wi_15, wi_7, wi_2 ror x28, \wi_15, #1 ror x17, \wi_2, #19 eor x28, x28, \wi_15, ror#8 eor x17, x17, \wi_2, ror#61 eor x28, x28, \wi_15, lsr#7 eor x17, x17, \wi_2, lsr#6 add \wi_16, \wi_16, \wi_7 add \wi_16, \wi_16, x28 add \wi_16, \wi_16, x17 .endm /** * Macro description: Processes the update of a hash value in 80 rounds of compression. * Input register: * x19: indicates the address of the corresponding element in the g_k512 constant. * wi: plaintext data after processing * a - h: intermediate variable of the hash value * Modify the register: h d x16 x17 x28 x29 * Output register: * h: value after a round of cyclic update * d: value after a round of cyclic update * Function/Macro Call: None */ .macro ONE_ROUND wi, a, b, c, d, e, f, g, h ldr x16, [x19], #8 // K[i] add \h, \h, x16 // h += K[i] add \h, \h, \wi // h += W[i] and x17, \f, \e // e&f bic x28, \g, \e // g&(~e) orr x17, x17, x28 // Ch(e, f, g) = e&f | g&(~e) add \h, \h, x17 // h += Ch(e, f, g) eor x29, \e, \e, ror#23 ror x16, \e, #14 eor x29, x16, x29, ror#18 // Sigma1(e) = ROR(e, 14) ^ ROR(e, 18) ^ ROR(e, 41) add \h, \h, x29 // h += Sigma1(e) eor x17, \a, \b // a^b eor x28, \a, \c // a^c and x28, x28, x17 // (a^b)&(a^c) eor x28, x28, \a // Maj(a, b, c) = ((a^b)&(a^c))^a = (a&b)^(b&c)^(a&c) add \d, \d, \h // d += h add \h, \h, x28 // h += Maj(a, b, c) eor x29, \a, \a, ror#5 ror x16, \a, #28 eor x29, x16, x29, ror#34 // Sigma0(a) = ROR(a, 28)^ROR(a, 34)^ROR(a, 39) add \h, \h, x29 // h += Sigma0(a) .endm /** * Function description: Performs 80 rounds of compression calculation *based on the input plaintext data and updates the hash value. * Function prototype: void SHA512CompressMultiBlocks(uint64_t hash[8], const uint8_t *in, uint32_t num); * Input register: * x0: indicates the storage address of the hash value. * x1: pointer to the input data address * x2: number of 80 rounds of cycles. The value is the input data length divided by 128. * Change register: x0-x17. * Output register: None * Function/Macro Call: None * */ .text .balign 16 .global SHA512CompressMultiBlocks .type SHA512CompressMultiBlocks, %function SHA512CompressMultiBlocks: cbz x2, .Lend_sha512 stp x29, x30, [sp, #-112]! add x29, sp, #0 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 - h */ ldp x20, x21, [x0] ldp x22, x23, [x0, #8*2] ldp x24, x25, [x0, #8*4] ldp x26, x27, [x0, #8*6] str x0, [sp, #96] mov x16, x1 // input Value Address lsl x30, x2, #2 .Lloop_compress_80: /* Start 80 rounds of processing */ adrp x19, .K512 add x19, x19, :lo12:.K512 ldp x0, x1, [x16] // Load input values. ldp x2, x3, [x16, #8*2] ldp x4, x5, [x16, #8*4] ldp x6, x7, [x16, #8*6] ldp x8, x9, [x16, #8*8] ldp x10, x11, [x16, #8*10] ldp x12, x13, [x16, #8*12] ldp x14, x15, [x16, #8*14] add x16, x16, #8*16 str x16, [sp, #104] #ifndef HITLS_BIG_ENDIAN rev x0, x0 rev x1, x1 rev x2, x2 rev x3, x3 rev x4, x4 rev x5, x5 rev x6, x6 rev x7, x7 rev x8, x8 rev x9, x9 rev x10, x10 rev x11, x11 rev x12, x12 rev x13, x13 rev x14, x14 rev x15, x15 #endif /* x16 x17 x28 x29 used as a temporary register */ ONE_ROUND x0, x20, x21, x22, x23, x24, x25, x26, x27 ONE_ROUND x1, x27, x20, x21, x22, x23, x24, x25, x26 ONE_ROUND x2, x26, x27, x20, x21, x22, x23, x24, x25 ONE_ROUND x3, x25, x26, x27, x20, x21, x22, x23, x24 ONE_ROUND x4, x24, x25, x26, x27, x20, x21, x22, x23 ONE_ROUND x5, x23, x24, x25, x26, x27, x20, x21, x22 ONE_ROUND x6, x22, x23, x24, x25, x26, x27, x20, x21 ONE_ROUND x7, x21, x22, x23, x24, x25, x26, x27, x20 ONE_ROUND x8, x20, x21, x22, x23, x24, x25, x26, x27 ONE_ROUND x9, x27, x20, x21, x22, x23, x24, x25, x26 ONE_ROUND x10, x26, x27, x20, x21, x22, x23, x24, x25 ONE_ROUND x11, x25, x26, x27, x20, x21, x22, x23, x24 ONE_ROUND x12, x24, x25, x26, x27, x20, x21, x22, x23 ONE_ROUND x13, x23, x24, x25, x26, x27, x20, x21, x22 ONE_ROUND x14, x22, x23, x24, x25, x26, x27, x20, x21 ONE_ROUND x15, x21, x22, x23, x24, x25, x26, x27, x20 .Lloop_compress_16_79: /* Start 16 - 31, 32 - 47, 48 - 63, 64 - 79 compression */ sub x30, x30, #1 /* 0 */ UPDATE_W x0, x1, x9, x14 ONE_ROUND x0, x20, x21, x22, x23, x24, x25, x26, x27 /* 1 */ UPDATE_W x1, x2, x10, x15 ONE_ROUND x1, x27, x20, x21, x22, x23, x24, x25, x26 /* 2 */ UPDATE_W x2, x3, x11, x0 ONE_ROUND x2, x26, x27, x20, x21, x22, x23, x24, x25 /* 3 */ UPDATE_W x3, x4, x12, x1 ONE_ROUND x3, x25, x26, x27, x20, x21, x22, x23, x24 /* 4 */ UPDATE_W x4, x5, x13, x2 ONE_ROUND x4, x24, x25, x26, x27, x20, x21, x22, x23 /* 5 */ UPDATE_W x5, x6, x14, x3 ONE_ROUND x5, x23, x24, x25, x26, x27, x20, x21, x22 /* 6 */ UPDATE_W x6, x7, x15, x4 ONE_ROUND x6, x22, x23, x24, x25, x26, x27, x20, x21 /* 7 */ UPDATE_W x7, x8, x0, x5 ONE_ROUND x7, x21, x22, x23, x24, x25, x26, x27, x20 /* 8 */ UPDATE_W x8, x9, x1, x6 ONE_ROUND x8, x20, x21, x22, x23, x24, x25, x26, x27 /* 9 */ UPDATE_W x9, x10, x2, x7 ONE_ROUND x9, x27, x20, x21, x22, x23, x24, x25, x26 /* 10 */ UPDATE_W x10, x11, x3, x8 ONE_ROUND x10, x26, x27, x20, x21, x22, x23, x24, x25 /* 11 */ UPDATE_W x11, x12, x4, x9 ONE_ROUND x11, x25, x26, x27, x20, x21, x22, x23, x24 /* 12 */ UPDATE_W x12, x13, x5, x10 ONE_ROUND x12, x24, x25, x26, x27, x20, x21, x22, x23 /* 13 */ UPDATE_W x13, x14, x6, x11 ONE_ROUND x13, x23, x24, x25, x26, x27, x20, x21, x22 /* 14 */ UPDATE_W x14, x15, x7, x12 ONE_ROUND x14, x22, x23, x24, x25, x26, x27, x20, x21 /* 15 */ UPDATE_W x15, x0, x8, x13 ONE_ROUND x15, x21, x22, x23, x24, x25, x26, x27, x20 /* If the processing length is not 80, continue the loop. */ tst x30, #3 bne .Lloop_compress_16_79 /* Stores a - h information. */ ldr x0, [sp, #96] ldp x10, x11, [x0] ldp x12, x13, [x0, #8*2] ldp x14, x15, [x0, #8*4] ldp x16, x17, [x0, #8*6] add x20, x20, x10 add x21, x21, x11 add x22, x22, x12 add x23, x23, x13 add x24, x24, x14 add x25, x25, x15 add x26, x26, x16 add x27, x27, x17 stp x20, x21, [x0] stp x22, x23, [x0, #8*2] stp x24, x25, [x0, #8*4] stp x26, x27, [x0, #8*6] ldr x16, [sp, #104] /* If the remaining length is not processed, continue to process 80 rounds. */ cbnz x30, .Lloop_compress_80 /* The function returns */ 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], #112 .Lend_sha512: ret .size SHA512CompressMultiBlocks, .-SHA512CompressMultiBlocks #endif
2301_79861745/bench_create
crypto/sha2/src/asm/sha2_512_armv8.S
Unix Assembly
unknown
10,889
/* * 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_SHA512 .file "sha2_512_x86_64.S" .set TEMP1, %rbp .set TEMP2, %rax .set TEMP3, %rbx .set TEMP4, %rcx .set TEMP5, %rdi .set YTEMP1, %ymm8 .set YTEMP2, %ymm9 .set YTEMP3, %ymm10 .set YTEMP4, %ymm11 .set YTEMP5, %ymm12 .set YTEMP6, %ymm13 .set YTEMP7, %ymm14 .equ SHA512_wk, 0 .equ SHA512_in, SHA512_wk + 1280 .equ SHA512_hash, SHA512_in + 8 .equ SHA512_num, SHA512_hash + 8 .equ SHA512_rsp, SHA512_num + 8 .equ SHA512_size, SHA512_rsp + 8 .section .rodata .balign 64 .type g_k512,%object g_k512: .quad 0x428a2f98d728ae22, 0x7137449123ef65cd, 0x428a2f98d728ae22, 0x7137449123ef65cd .quad 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc .quad 0x3956c25bf348b538, 0x59f111f1b605d019, 0x3956c25bf348b538, 0x59f111f1b605d019 .quad 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118 .quad 0xd807aa98a3030242, 0x12835b0145706fbe, 0xd807aa98a3030242, 0x12835b0145706fbe .quad 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2 .quad 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1 .quad 0x9bdc06a725c71235, 0xc19bf174cf692694, 0x9bdc06a725c71235, 0xc19bf174cf692694 .quad 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3 .quad 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65 .quad 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x2de92c6f592b0275, 0x4a7484aa6ea6e483 .quad 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5 .quad 0x983e5152ee66dfab, 0xa831c66d2db43210, 0x983e5152ee66dfab, 0xa831c66d2db43210 .quad 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xb00327c898fb213f, 0xbf597fc7beef0ee4 .quad 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0xc6e00bf33da88fc2, 0xd5a79147930aa725 .quad 0x06ca6351e003826f, 0x142929670a0e6e70, 0x06ca6351e003826f, 0x142929670a0e6e70 .quad 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x27b70a8546d22ffc, 0x2e1b21385c26c926 .quad 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df .quad 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x650a73548baf63de, 0x766a0abb3c77b2a8 .quad 0x81c2c92e47edaee6, 0x92722c851482353b, 0x81c2c92e47edaee6, 0x92722c851482353b .quad 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xa2bfe8a14cf10364, 0xa81a664bbc423001 .quad 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xc24b8b70d0f89791, 0xc76c51a30654be30 .quad 0xd192e819d6ef5218, 0xd69906245565a910, 0xd192e819d6ef5218, 0xd69906245565a910 .quad 0xf40e35855771202a, 0x106aa07032bbd1b8, 0xf40e35855771202a, 0x106aa07032bbd1b8 .quad 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53 .quad 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8 .quad 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb .quad 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3 .quad 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x748f82ee5defb2fc, 0x78a5636f43172f60 .quad 0x84c87814a1f0ab72, 0x8cc702081a6439ec, 0x84c87814a1f0ab72, 0x8cc702081a6439ec .quad 0x90befffa23631e28, 0xa4506cebde82bde9, 0x90befffa23631e28, 0xa4506cebde82bde9 .quad 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xbef9a3f7b2c67915, 0xc67178f2e372532b .quad 0xca273eceea26619c, 0xd186b8c721c0c207, 0xca273eceea26619c, 0xd186b8c721c0c207 .quad 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178 .quad 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x06f067aa72176fba, 0x0a637dc5a2c898a6 .quad 0x113f9804bef90dae, 0x1b710b35131c471b, 0x113f9804bef90dae, 0x1b710b35131c471b .quad 0x28db77f523047d84, 0x32caab7b40c72493, 0x28db77f523047d84, 0x32caab7b40c72493 .quad 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c .quad 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a .quad 0x5fcb6fab3ad6faec, 0x6c44198c4a475817, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 .size g_k512, .-g_k512 .balign 64 .type g_endianMask,%object g_endianMask: .quad 0x0001020304050607, 0x08090a0b0c0d0e0f .quad 0x0001020304050607, 0x08090a0b0c0d0e0f .size g_endianMask, .-g_endianMask /** * Macro Description: Processes the update of the hash value in one round of 80 compressions. * input register: * addr: Stack space initial address * wkOffset: wi+k512 Data address offset * a - h: Intermediate variable of hash value * Modify the register:temp1, temp2, temp3, temp4, temp5 * Output register: * h: Indicates the value after a cyclic update. * d: Indicates the value after a cyclic update. * temp1: BSIG0(a) from last round * temp4: b^a for next round b^c * Function/Macro Call: None * Implementation Description: * T1 = h + BSIG1(e) + CH(e,f,g) + Kt + Wt * T2 = BSIG0(a) + MAJ(a,b,c) * CH(e, f, g) = (e AND f) XOR ((NOT e) AND g) * MAJ(a, b, c) = (a AND b) XOR (a AND c) XOR (b AND c) * = CH(a^b, c, b) * = ((a XOR b) AND c) XOR ((NOT(a XOR b)) AND b) * = (b XOR c) AND (a XOR b) XOR b * BSIG0(x) = ROTR^28(x) XOR ROTR^34(x) XOR ROTR^39(x) * BSIG1(x) = ROTR^14(x) XOR ROTR^18(x) XOR ROTR^41(x) * d += T1; h = T1 + T2 * Optimization Principle:asert b^c in temp4, temp1 equal 0, f in temp5 when round begin * mov b, temp4 * xor temp1, temp1 * xor c, temp4 * mov f, temp5 * swap temp2 temp4 for next round * add BSIG0(a) back to a when all round finished */ .macro ONE_ROUND a, b, c, d, e, f, g, h, temp1, temp2, temp3, temp4, temp5, addr, wkOffset // asert b^c in temp4, temp1 equal 0, f in temp5 when round begin addq \wkOffset(\addr), \h // h += Kt + Wt and \e, \temp5 // e&f rorx $14, \e, \temp2 // ROTR^14(e) addq \temp1, \a // a += BSIG0(a) from last round rorx $18, \e, \temp3 // ROTR^18(e) andn \g, \e, \temp1 // (~e)&g xor \temp2, \temp3 // ROTR^14(e) ^ ROTR^18(e) xor \temp1, \temp5 // CH(e,f,g) rorx $41, \e, \temp2 // ROTR^41(e) addq \temp5, \h // h += CH(e,f,g) xor \temp2, \temp3 // BSIG1(e) rorx $28, \a, \temp1 // ROTR^28(a) mov \a, \temp2 // a addq \temp3, \h // h += BSIG1(e) rorx $34, \a, \temp5 // ROTR^34(a) xor \b, \temp2 // b^a for next round b^c addq \h, \d // d += T1 xor \temp5, \temp1 // ROTR^14(a) ^ ROTR^34(a) and \temp2, \temp4 // (b^a) & (b^c) rorx $39, \a, \temp3 // ROTR^39(a) xor \b, \temp4 // Maj(a,b,c) mov \e, \temp5 // for next round f xor \temp3, \temp1 // BSIG0(a) addq \temp4, \h // h += Maj(a,b,c) // swap temp2 temp4 for next round // add BSIG0(a) back to a when all round finished .endm /** * Macro Description: Processes the update of two rounds of hash values in 80 rounds of compression, * and expands messages. * Input register: * addr: Stack space initial address * wkOffset: wi+k512 Data address offset * a - h: Intermediate variable of hash value * wi_17_16: W[i-16-15] * wi_15_14: W[i-15-14] * wi_7_6: W[i-7-6] * wi_9_8: W[i-7-8] * wi_3_2: W[i-3-2] * Modify the register:TEMP1, TEMP2, TEMP3, TEMP4, TEMP5, wi_17_16, YTEMP1, YTEMP2, YTEMP3, YTEMP4, YTEMP5, YTEMP6 * Output register: * h: Value after two rounds of cyclic update * d: Value after two rounds of cyclic update * TEMP1: BSIG0(a) from last round * TEMP4: b^a for next round b^c * wi_17_16: expanded message * Function/Macro Call: None * Implementation Description: * T1 = h + BSIG1(e) + CH(e,f,g) + Kt + Wt * T2 = BSIG0(a) + MAJ(a,b,c) * CH(e, f, g) = (e AND f) XOR ((NOT e) AND g) * MAJ(a, b, c) = (a AND b) XOR (a AND c) XOR (b AND c) * = CH(a^b, c, b) * = ((a XOR b) AND c) XOR ((NOT(a XOR b)) AND b) * = (b XOR c) AND (a XOR b) XOR b * BSIG0(x) = ROTR^28(x) XOR ROTR^34(x) XOR ROTR^39(x) * BSIG1(x) = ROTR^14(x) XOR ROTR^18(x) XOR ROTR^41(x) * d += T1; h = T1 + T2 * * wi_16: Latest W[i] value, W[i] = sigma1(W[i-2]) + W[i-7] + sigma0(W[i-15]) + W[i-16] * SSIG0(x) = ROTR^1(x) XOR ROTR^8(x) XOR SHR^7(x) * SSIG1(x) = ROTR^19(x) XOR ROTR^61(x) XOR SHR^6(x) * Optimization Principle:asert b^c in TEMP4, TEMP1 equal 0, f in TEMP5 when round begin * mov b, TEMP4 * xor TEMP1, TEMP1 * xor c, TEMP4 * mov f, TEMP5 * swap TEMP2 TEMP4 for next round * add BSIG0(a) back to a when all round finished */ .macro TWO_ROUND_UPDATE_2W a, b, c, d, e, f, g, h, wkOffset, wi_17_16, wi_15_14, wi_9_8, wi_7_6, wi_3_2 // 1st round vpalignr $8, \wi_17_16, \wi_15_14, YTEMP1 // wi_16_15 vpalignr $8, \wi_9_8, \wi_7_6, YTEMP7 // wi_8_7 addq \wkOffset(%rsi), \h // h += Kt + Wt and \e, TEMP5 // e&f vpsrlq $1, YTEMP1, YTEMP2 rorx $14, \e, TEMP2 // ROTR^14(e) addq TEMP1, \a // a += BSIG0(a) from last round vpsrlq $8, YTEMP1, YTEMP3 rorx $18, \e, TEMP3 // ROTR^18(e) andn \g, \e, TEMP1 // (~e)&g vpsrlq $7, YTEMP1, YTEMP4 xor TEMP2, TEMP3 // ROTR^14(e) ^ ROTR^18(e) xor TEMP1, TEMP5 // CH(e,f,g) vpsllq $63, YTEMP1, YTEMP5 rorx $41, \e, TEMP2 // ROTR^41(e) addq TEMP5, \h // h += CH(e,f,g) vpsllq $56, YTEMP1, YTEMP6 xor TEMP2, TEMP3 // BSIG1(e) rorx $28, \a, TEMP1 // ROTR^28(a) vpaddq YTEMP7, \wi_17_16, \wi_17_16 // W[i-17..16] + W[8..7] mov \a, TEMP2 // a addq TEMP3, \h // h += BSIG1(e) vpxor YTEMP5, YTEMP2, YTEMP2 // ROTR^1(wi_16_15) rorx $34, \a, TEMP5 // ROTR^34(a) xor \b, TEMP2 // b^a for next round b^c vpxor YTEMP6, YTEMP3, YTEMP3 // ROTR^8(wi_16_15) addq \h, \d // d += T1 xor TEMP5, TEMP1 // ROTR^14(a) ^ ROTR^34(a) vpxor YTEMP4, YTEMP2, YTEMP1 and TEMP2, TEMP4 // (b^a) & (b^c) rorx $39, \a, TEMP3 // ROTR^39(a) vpxor YTEMP3, YTEMP1, YTEMP1 // SSIG0(wi_16_15) xor \b, TEMP4 // Maj(a,b,c) mov \e, TEMP5 // for next round f vpaddq YTEMP1, \wi_17_16, \wi_17_16 // SSIG0(wi_16_15) + W[i-17..16] + W[8..7] xor TEMP3, TEMP1 // BSIG0(a) addq TEMP4, \h // h += Maj(a,b,c) // swap TEMP2 TEMP4 for next round // 2nd round // ror abcdefgh to habcdefg vpsrlq $19, \wi_3_2, YTEMP2 addq 8+\wkOffset(%rsi), \g // h += Kt + Wt and \d, TEMP5 // e&f vpsrlq $61, \wi_3_2, YTEMP3 rorx $14, \d, TEMP4 // ROTR^14(e) addq TEMP1, \h // a += BSIG0(a) from last round vpsrlq $6, \wi_3_2, YTEMP4 rorx $18, \d, TEMP3 // ROTR^18(e) andn \f, \d, TEMP1 // (~e)&g vpsllq $45, \wi_3_2, YTEMP5 xor TEMP4, TEMP3 // ROTR^14(e) ^ ROTR^18(e) xor TEMP1, TEMP5 // CH(e,f,g) vpsllq $3, \wi_3_2, YTEMP6 rorx $41, \d, TEMP4 // ROTR^41(e) addq TEMP5, \g // h += CH(e,f,g) vpxor YTEMP5, YTEMP2, YTEMP2 // ROTR^19(wi_3_2) xor TEMP4, TEMP3 // BSIG1(e) rorx $28, \h, TEMP1 // ROTR^28(a) vpxor YTEMP6, YTEMP3, YTEMP3 // ROTR^61(wi_3_2) mov \h, TEMP4 // a addq TEMP3, \g // h += BSIG1(e) vpxor YTEMP4, YTEMP2, YTEMP1 rorx $34, \h, TEMP5 // ROTR^34(a) xor \a, TEMP4 // b^a for next round b^c vpxor YTEMP3, YTEMP1, YTEMP1 // SSIG1(wi_3_2) addq \g, \c // d += T1 xor TEMP5, TEMP1 // ROTR^14(a) ^ ROTR^34(a) vpaddq YTEMP1, \wi_17_16, \wi_17_16 // SSIG0(wi_16_15) + W[i-17..16] + W[i-8..7] + SSIG1(wi_3_2) and TEMP4, TEMP2 // (b^a) & (b^c) rorx $39, \h, TEMP3 // ROTR^39(a) vpaddq \wkOffset(%rdx), \wi_17_16, YTEMP1 // wi + k xor \a, TEMP2 // Maj(a,b,c) mov \d, TEMP5 // for next round f vmovdqa YTEMP1, \wkOffset + 256(%rsi) xor TEMP3, TEMP1 // BSIG0(a) addq TEMP2, \g // h += Maj(a,b,c) // swap TEMP2 TEMP4 for next round // add BSIG0(a) back to a when all round finished .endm /** * Function description: Performs 80 rounds of compression calculation based on the input plaintext data and updates the hash value. * function prototype:void SHA512CompressMultiBlocks(uint64_t hash[8], const uint8_t *in, uint32_t num); * input register: * rdi:function prototype * rsi:Pointer to the input data address * rdx:Number of 80 rounds of cycles. The value is the length of the input data divided by 128. * Register usage:ymm0-ymm7 to participate in the calculation of message blocks (of two data blocks). * ymm8-ymm14 is temporary wide register * r8-r15 Storage a-h * The stack space temporarily stores wi+k512 (1280 bytes) and hash addresses、in、num * Output register:None * Function/Macro Call:UPDATE_W、ONE_ROUND * */ .text .balign 16 .global SHA512CompressMultiBlocks .type SHA512CompressMultiBlocks, %function SHA512CompressMultiBlocks: .cfi_startproc cmp $0, %rdx je .Lsha512end pushq %rbx pushq %rbp pushq %r12 pushq %r13 pushq %r14 pushq %r15 mov %rsp, %r14 sub $1320, %rsp and $-256, %rsp // 32-byte address alignment mov %r14, SHA512_rsp(%rsp) // rsp The original value is added to the stack. /* load A-H */ mov 0(%rdi), %r8 mov 8(%rdi), %r9 mov 16(%rdi), %r10 mov 24(%rdi), %r11 mov 32(%rdi), %r12 mov 40(%rdi), %r13 mov 48(%rdi), %r14 mov 56(%rdi), %r15 mov %rdi, SHA512_hash(%rsp) mov %rsi, SHA512_in(%rsp) // The input data address is stored in the stack. .Lsha512_loop: mov SHA512_in(%rsp), %rsi /* Loads the data of a block to the lower 128 bits of the ymm register. */ vmovdqu 0(%rsi), %xmm0 vmovdqu 16(%rsi), %xmm1 vmovdqu 32(%rsi), %xmm2 vmovdqu 48(%rsi), %xmm3 vmovdqu 64(%rsi), %xmm4 vmovdqu 80(%rsi), %xmm5 vmovdqu 96(%rsi), %xmm6 vmovdqu 112(%rsi), %xmm7 mov %rsi, %rcx add $128, %rsi cmp $1, %rdx cmovne %rsi, %rcx // If num is greater than 1, rcx points to the next block. mov %rdx, SHA512_num(%rsp) // Remaining nums are added to the stack. /* Loads the data of a block to the upper 128 bits of the ymm register. */ vinserti128 $1, 0(%rcx), %ymm0, %ymm0 vinserti128 $1, 16(%rcx), %ymm1, %ymm1 vinserti128 $1, 32(%rcx), %ymm2, %ymm2 vinserti128 $1, 48(%rcx), %ymm3, %ymm3 vinserti128 $1, 64(%rcx), %ymm4, %ymm4 vinserti128 $1, 80(%rcx), %ymm5, %ymm5 vinserti128 $1, 96(%rcx), %ymm6, %ymm6 vinserti128 $1, 112(%rcx),%ymm7, %ymm7 add $128, %rcx mov %rcx, SHA512_in(%rsp) // The input data address is stored in the stack. vmovdqa g_endianMask + 0(%rip), %ymm8 leaq g_k512 + 0(%rip), %rdx /* Little-endian order to big-endian order */ vpshufb %ymm8, %ymm0, %ymm0 vpshufb %ymm8, %ymm1, %ymm1 vpshufb %ymm8, %ymm2, %ymm2 vpshufb %ymm8, %ymm3, %ymm3 vpshufb %ymm8, %ymm4, %ymm4 vpshufb %ymm8, %ymm5, %ymm5 vpshufb %ymm8, %ymm6, %ymm6 vpshufb %ymm8, %ymm7, %ymm7 /* w[0..15] + k*/ vpaddq 0(%rdx), %ymm0, %ymm8 vpaddq 32(%rdx), %ymm1, %ymm9 vpaddq 64(%rdx), %ymm2, %ymm10 vpaddq 96(%rdx), %ymm3, %ymm11 vpaddq 128(%rdx), %ymm4, %ymm12 vpaddq 160(%rdx), %ymm5, %ymm13 vpaddq 192(%rdx), %ymm6, %ymm14 vpaddq 224(%rdx), %ymm7, %ymm15 /* wk push stack */ vmovdqa %ymm8, 0(%rsp) vmovdqa %ymm9, 32(%rsp) vmovdqa %ymm10, 64(%rsp) vmovdqa %ymm11, 96(%rsp) vmovdqa %ymm12, 128(%rsp) vmovdqa %ymm13, 160(%rsp) vmovdqa %ymm14, 192(%rsp) vmovdqa %ymm15, 224(%rsp) movq $4, 1312(%rsp) leaq 0(%rsp), %rsi mov %r9, %rcx // mov b, TEMP4 xor %rbp, %rbp // xor TEMP1, TEMP1 xor %r10, %rcx // xor c, TEMP4 mov %r13, %rdi // mov f, TEMP5 .Lround00_63: leaq 256(%rdx), %rdx TWO_ROUND_UPDATE_2W %r8, %r9, %r10, %r11, %r12, %r13, %r14, %r15, 0, %ymm0, %ymm1, %ymm4, %ymm5, %ymm7 TWO_ROUND_UPDATE_2W %r14, %r15, %r8, %r9, %r10, %r11, %r12, %r13, 32, %ymm1, %ymm2, %ymm5, %ymm6, %ymm0 TWO_ROUND_UPDATE_2W %r12, %r13, %r14, %r15, %r8, %r9, %r10, %r11, 64, %ymm2, %ymm3, %ymm6, %ymm7, %ymm1 TWO_ROUND_UPDATE_2W %r10, %r11, %r12, %r13, %r14, %r15, %r8, %r9, 96, %ymm3, %ymm4, %ymm7, %ymm0, %ymm2 TWO_ROUND_UPDATE_2W %r8, %r9, %r10, %r11, %r12, %r13, %r14, %r15, 128, %ymm4, %ymm5, %ymm0, %ymm1, %ymm3 TWO_ROUND_UPDATE_2W %r14, %r15, %r8, %r9, %r10, %r11, %r12, %r13, 160, %ymm5, %ymm6, %ymm1, %ymm2, %ymm4 TWO_ROUND_UPDATE_2W %r12, %r13, %r14, %r15, %r8, %r9, %r10, %r11, 192, %ymm6, %ymm7, %ymm2, %ymm3, %ymm5 TWO_ROUND_UPDATE_2W %r10, %r11, %r12, %r13, %r14, %r15, %r8, %r9, 224, %ymm7, %ymm0, %ymm3, %ymm4, %ymm6 leaq 256(%rsi), %rsi decq 1312(%rsp) jne .Lround00_63 /* round 64-79 */ ONE_ROUND %r8, %r9, %r10, %r11, %r12, %r13, %r14, %r15, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 0 ONE_ROUND %r15, %r8, %r9, %r10, %r11, %r12, %r13, %r14, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 8 ONE_ROUND %r14, %r15, %r8, %r9, %r10, %r11, %r12, %r13, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 32 ONE_ROUND %r13, %r14, %r15, %r8, %r9, %r10, %r11, %r12, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 40 ONE_ROUND %r12, %r13, %r14, %r15, %r8, %r9, %r10, %r11, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 64 ONE_ROUND %r11, %r12, %r13, %r14, %r15, %r8, %r9, %r10, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 72 ONE_ROUND %r10, %r11, %r12, %r13, %r14, %r15, %r8, %r9, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 96 ONE_ROUND %r9, %r10, %r11, %r12, %r13, %r14, %r15, %r8, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 104 ONE_ROUND %r8, %r9, %r10, %r11, %r12, %r13, %r14, %r15, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 128 ONE_ROUND %r15, %r8, %r9, %r10, %r11, %r12, %r13, %r14, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 136 ONE_ROUND %r14, %r15, %r8, %r9, %r10, %r11, %r12, %r13, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 160 ONE_ROUND %r13, %r14, %r15, %r8, %r9, %r10, %r11, %r12, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 168 ONE_ROUND %r12, %r13, %r14, %r15, %r8, %r9, %r10, %r11, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 192 ONE_ROUND %r11, %r12, %r13, %r14, %r15, %r8, %r9, %r10, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 200 ONE_ROUND %r10, %r11, %r12, %r13, %r14, %r15, %r8, %r9, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 224 ONE_ROUND %r9, %r10, %r11, %r12, %r13, %r14, %r15, %r8, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 232 addq %rbp, %r8 // a += BSIG0(a) from last round leaq -1024(%rsi), %rsi // rsi Point to the original address /* Update the hash value. */ mov SHA512_hash(%rsp), %rdi mov SHA512_num(%rsp), %rdx addq 0(%rdi), %r8 addq 8(%rdi), %r9 addq 16(%rdi), %r10 addq 24(%rdi), %r11 addq 32(%rdi), %r12 addq 40(%rdi), %r13 addq 48(%rdi), %r14 addq 56(%rdi), %r15 mov %r8, 0(%rdi) mov %r9, 8(%rdi) mov %r10, 16(%rdi) mov %r11, 24(%rdi) mov %r12, 32(%rdi) mov %r13, 40(%rdi) mov %r14, 48(%rdi) mov %r15, 56(%rdi) cmp $1, %rdx je .Lsha512_finish movq $10, 1312(%rsp) mov %r9, %rcx // mov b, TEMP4 xor %rbp, %rbp // xor TEMP1, TEMP1 xor %r10, %rcx // xor c, TEMP4 mov %r13, %rdi // mov f, TEMP5 .Lnext_block: ONE_ROUND %r8, %r9, %r10, %r11, %r12, %r13, %r14, %r15, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 16 ONE_ROUND %r15, %r8, %r9, %r10, %r11, %r12, %r13, %r14, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 24 ONE_ROUND %r14, %r15, %r8, %r9, %r10, %r11, %r12, %r13, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 48 ONE_ROUND %r13, %r14, %r15, %r8, %r9, %r10, %r11, %r12, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 56 ONE_ROUND %r12, %r13, %r14, %r15, %r8, %r9, %r10, %r11, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 80 ONE_ROUND %r11, %r12, %r13, %r14, %r15, %r8, %r9, %r10, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 88 ONE_ROUND %r10, %r11, %r12, %r13, %r14, %r15, %r8, %r9, %rbp, %rax, %rbx, %rcx, %rdi, %rsi, 112 ONE_ROUND %r9, %r10, %r11, %r12, %r13, %r14, %r15, %r8, %rbp, %rcx, %rbx, %rax, %rdi, %rsi, 120 leaq 128(%rsi), %rsi decq 1312(%rsp) jne .Lnext_block addq %rbp, %r8 // a += BSIG0(a) from last round leaq -1280(%rsi), %rsi // rsi Point to the original address /* Update the hash value. */ mov SHA512_hash(%rsp), %rdi addq 0(%rdi), %r8 addq 8(%rdi), %r9 addq 16(%rdi), %r10 addq 24(%rdi), %r11 addq 32(%rdi), %r12 addq 40(%rdi), %r13 addq 48(%rdi), %r14 addq 56(%rdi), %r15 mov %r8, 0(%rdi) mov %r9, 8(%rdi) mov %r10, 16(%rdi) mov %r11, 24(%rdi) mov %r12, 32(%rdi) mov %r13, 40(%rdi) mov %r14, 48(%rdi) mov %r15, 56(%rdi) sub $2, %rdx jne .Lsha512_loop .Lsha512_finish: mov SHA512_rsp(%rsp), %rsp popq %r15 popq %r14 popq %r13 popq %r12 popq %rbp popq %rbx .Lsha512end: ret .cfi_endproc .size SHA512CompressMultiBlocks, .-SHA512CompressMultiBlocks #endif
2301_79861745/bench_create
crypto/sha2/src/asm/sha2_512_x86_64.S
Unix Assembly
unknown
23,740
/* * 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_SHA256) && !defined(HITLS_CRYPTO_SHA256_SMALL_MEM) #include "crypt_sha2.h" #include "crypt_utils.h" #include "sha2_core.h" static const uint32_t K256[64] = { 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL, }; #define ROTR32(x, n) (((x) << (32 - (n))) | ((x) >> (n))) // Assumes that x is uint32_t and 0 < n < 32 #define S0(x) (ROTR32((x), 7) ^ ROTR32((x), 18) ^ ((x) >> 3)) #define S1(x) (ROTR32((x), 17) ^ ROTR32((x), 19) ^ ((x) >> 10)) #define R(w, t) \ (S1((w)[(t) - 2]) + (w)[(t) - 7] + \ S0((w)[(t) - 15]) + (w)[(t) - 16]) #define ROUND(a, b, c, d, e, f, g, h, i, k) \ do { \ /* constants: 6, 11, 25 */ \ (h) += (ROTR32((e), 6) ^ ROTR32((e), 11) ^ ROTR32((e), 25)) + \ ((g) ^ ((e) & ((f) ^ (g)))) + (k) + (i); \ (d) += (h); \ /* constants: 2, 13, 22 */ \ (h) += (ROTR32((a), 2) ^ ROTR32((a), 13) ^ ROTR32((a), 22)) + \ (((a) & ((b) | (c))) | ((b) & (c))); \ } while (0) static void CompressBlock(uint32_t state[8], const uint8_t block[CRYPT_SHA2_256_BLOCKSIZE]) { uint32_t w[64]; // RFC 6.2.1. Prepare the message schedule w: // For t = 0 to 15 // Wt = M(i)t for (unsigned i = 0; i < 16; i++) { // 16 rounds to prepare the message schedule w[i] = GET_UINT32_BE(block, 4 * (i)); /* 4 means bytes of uint32_t */ } // For t = 16 to 63 // Wt = SSIG1(w(t-2)) + w(t-7) + SSIG0(t-15) + w(t-16) // @perf: speed up about 18% than expanded in x86_64 // RFC 6.2.2. Initialize the working variables: // a, b, ..., g, h = H(i-1)[0..7] uint32_t a = state[0]; uint32_t b = state[1]; uint32_t c = state[2]; uint32_t d = state[3]; uint32_t e = state[4]; uint32_t f = state[5]; uint32_t g = state[6]; uint32_t h = state[7]; // RFC 6.2.3. Perform the main hash computation: for (unsigned i = 0; i < 16; i += 8) { /* 0 ~ 16 rounds to do hash computation, 8 rounds pre loop */ ROUND(a, b, c, d, e, f, g, h, w[i + 0], K256[i + 0]); ROUND(h, a, b, c, d, e, f, g, w[i + 1], K256[i + 1]); ROUND(g, h, a, b, c, d, e, f, w[i + 2], K256[i + 2]); ROUND(f, g, h, a, b, c, d, e, w[i + 3], K256[i + 3]); ROUND(e, f, g, h, a, b, c, d, w[i + 4], K256[i + 4]); ROUND(d, e, f, g, h, a, b, c, w[i + 5], K256[i + 5]); ROUND(c, d, e, f, g, h, a, b, w[i + 6], K256[i + 6]); ROUND(b, c, d, e, f, g, h, a, w[i + 7], K256[i + 7]); } for (unsigned i = 16; i < 64; i += 8) { /* 16 ~ 64 rounds to do hash computation, 8 rounds pre loop */ w[i + 0] = R(w, i + 0); ROUND(a, b, c, d, e, f, g, h, w[i + 0], K256[i + 0]); w[i + 1] = R(w, i + 1); ROUND(h, a, b, c, d, e, f, g, w[i + 1], K256[i + 1]); w[i + 2] = R(w, i + 2); ROUND(g, h, a, b, c, d, e, f, w[i + 2], K256[i + 2]); w[i + 3] = R(w, i + 3); ROUND(f, g, h, a, b, c, d, e, w[i + 3], K256[i + 3]); w[i + 4] = R(w, i + 4); ROUND(e, f, g, h, a, b, c, d, w[i + 4], K256[i + 4]); w[i + 5] = R(w, i + 5); ROUND(d, e, f, g, h, a, b, c, w[i + 5], K256[i + 5]); w[i + 6] = R(w, i + 6); ROUND(c, d, e, f, g, h, a, b, w[i + 6], K256[i + 6]); w[i + 7] = R(w, i + 7); ROUND(b, c, d, e, f, g, h, a, w[i + 7], K256[i + 7]); } // RFC 6.2.4. Compute the intermediate hash value H(i): // H(i) = [a, b, ..., g, h] + H(i-1)[0..7] state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; state[5] += f; state[6] += g; state[7] += h; } #undef ROTR32 #undef ROUND void SHA256CompressMultiBlocks(uint32_t hash[8], const uint8_t *in, uint32_t num) { uint32_t n = num; const uint8_t *p = in; while (n > 0) { CompressBlock(hash, p); p += CRYPT_SHA2_256_BLOCKSIZE; n--; } } #endif // HITLS_CRYPTO_SHA256 && !HITLS_CRYPTO_SHA256_SMALL_MEM
2301_79861745/bench_create
crypto/sha2/src/noasm_sha256.c
C
unknown
6,003
/* * 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 65% less in rom but lower performance. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_SHA256) && defined(HITLS_CRYPTO_SHA256_SMALL_MEM) #include "crypt_sha2.h" #include "crypt_utils.h" #include "sha2_core.h" // Move constants to .rodata section static const uint32_t K256[64] = { 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL, }; #define ROTR32(x, n) (((x) << (32 - (n))) | ((x) >> (n))) #define S0(x) (ROTR32((x), 7) ^ ROTR32((x), 18) ^ ((x) >> 3)) #define S1(x) (ROTR32((x), 17) ^ ROTR32((x), 19) ^ ((x) >> 10)) #define R(w, t) (S1((w)[(t) - 2]) + (w)[(t) - 7] + S0((w)[(t) - 15]) + (w)[(t)-16]) #define ROUND(a, b, c, d, e, f, g, h, i, k) do { \ uint32_t t1 = (h) + (ROTR32((e), 6) ^ ROTR32((e), 11) ^ ROTR32((e), 25)) + \ ((g) ^ ((e) & ((f) ^ (g)))) + (k) + (i); \ uint32_t t2 = (ROTR32((a), 2) ^ ROTR32((a), 13) ^ ROTR32((a), 22)) + (((a) & ((b) | (c))) | ((b) & (c))); \ (h) = (g); \ (g) = (f); \ (f) = (e); \ (e) = (d) + t1; \ (d) = (c); \ (c) = (b); \ (b) = (a); \ (a) = t1 + t2; \ } while (0) static void CompressBlock(uint32_t state[8], const uint8_t block[CRYPT_SHA2_256_BLOCKSIZE]) { uint32_t w[64]; // RFC 6234 6.2.2 Initialize the working variables uint32_t a = state[0]; uint32_t b = state[1]; uint32_t c = state[2]; uint32_t d = state[3]; uint32_t e = state[4]; uint32_t f = state[5]; uint32_t g = state[6]; uint32_t h = state[7]; // RFC 6234 6.2.1. Prepare the message schedule w: for (unsigned i = 0; i < 16; i++) { w[i] = GET_UINT32_BE(block, 4 * i); } // Expand message schedule for (unsigned i = 16; i < 64; i++) { w[i] = R(w, i); } // RFC 6234 6.2.3 Perform the main hash computation for (unsigned i = 0; i < 64; i++) { ROUND(a, b, c, d, e, f, g, h, w[i], K256[i]); } // RFC 6234 6.2.4. Compute the intermediate hash value H(i): state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e; state[5] += f; state[6] += g; state[7] += h; } #undef ROTR32 #undef ROUND void SHA256CompressMultiBlocks(uint32_t hash[8], const uint8_t *in, uint32_t num) { uint32_t n = num; const uint8_t *p = in; while (n--) { CompressBlock(hash, p); p += CRYPT_SHA2_256_BLOCKSIZE; } } #endif // HITLS_CRYPTO_SHA256 && HITLS_CRYPTO_SHA256_SMALL_MEM
2301_79861745/bench_create
crypto/sha2/src/noasm_sha256_small.c
C
unknown
3,916
/* * 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_SHA512) && !defined(HITLS_CRYPTO_SHA512_SMALL_MEM) #include "crypt_sha2.h" #include "crypt_utils.h" #include "sha2_core.h" #ifndef U64 #define U64(v) (uint64_t)(v) #endif // define in rfc 4634 static const uint64_t K512[80] = { U64(0x428a2f98d728ae22), U64(0x7137449123ef65cd), U64(0xb5c0fbcfec4d3b2f), U64(0xe9b5dba58189dbbc), U64(0x3956c25bf348b538), U64(0x59f111f1b605d019), U64(0x923f82a4af194f9b), U64(0xab1c5ed5da6d8118), U64(0xd807aa98a3030242), U64(0x12835b0145706fbe), U64(0x243185be4ee4b28c), U64(0x550c7dc3d5ffb4e2), U64(0x72be5d74f27b896f), U64(0x80deb1fe3b1696b1), U64(0x9bdc06a725c71235), U64(0xc19bf174cf692694), U64(0xe49b69c19ef14ad2), U64(0xefbe4786384f25e3), U64(0x0fc19dc68b8cd5b5), U64(0x240ca1cc77ac9c65), U64(0x2de92c6f592b0275), U64(0x4a7484aa6ea6e483), U64(0x5cb0a9dcbd41fbd4), U64(0x76f988da831153b5), U64(0x983e5152ee66dfab), U64(0xa831c66d2db43210), U64(0xb00327c898fb213f), U64(0xbf597fc7beef0ee4), U64(0xc6e00bf33da88fc2), U64(0xd5a79147930aa725), U64(0x06ca6351e003826f), U64(0x142929670a0e6e70), U64(0x27b70a8546d22ffc), U64(0x2e1b21385c26c926), U64(0x4d2c6dfc5ac42aed), U64(0x53380d139d95b3df), U64(0x650a73548baf63de), U64(0x766a0abb3c77b2a8), U64(0x81c2c92e47edaee6), U64(0x92722c851482353b), U64(0xa2bfe8a14cf10364), U64(0xa81a664bbc423001), U64(0xc24b8b70d0f89791), U64(0xc76c51a30654be30), U64(0xd192e819d6ef5218), U64(0xd69906245565a910), U64(0xf40e35855771202a), U64(0x106aa07032bbd1b8), U64(0x19a4c116b8d2d0c8), U64(0x1e376c085141ab53), U64(0x2748774cdf8eeb99), U64(0x34b0bcb5e19b48a8), U64(0x391c0cb3c5c95a63), U64(0x4ed8aa4ae3418acb), U64(0x5b9cca4f7763e373), U64(0x682e6ff3d6b2b8a3), U64(0x748f82ee5defb2fc), U64(0x78a5636f43172f60), U64(0x84c87814a1f0ab72), U64(0x8cc702081a6439ec), U64(0x90befffa23631e28), U64(0xa4506cebde82bde9), U64(0xbef9a3f7b2c67915), U64(0xc67178f2e372532b), U64(0xca273eceea26619c), U64(0xd186b8c721c0c207), U64(0xeada7dd6cde0eb1e), U64(0xf57d4f7fee6ed178), U64(0x06f067aa72176fba), U64(0x0a637dc5a2c898a6), U64(0x113f9804bef90dae), U64(0x1b710b35131c471b), U64(0x28db77f523047d84), U64(0x32caab7b40c72493), U64(0x3c9ebe0a15c9bebc), U64(0x431d67c49c100d4c), U64(0x4cc5d4becb3e42b6), U64(0x597f299cfc657e2a), U64(0x5fcb6fab3ad6faec), U64(0x6c44198c4a475817), }; #undef ROUND00_15 #undef ROUND16_79 #define SHA512_CH(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) #define SHA512_MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) #define SHA512_BSIG0(x) (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39)) #define SHA512_BSIG1(x) (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41)) #define SHA512_SSIG0(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ ((x) >> 7)) #define SHA512_SSIG1(x) (ROTR64(x, 19) ^ ROTR64(x, 61) ^ ((x) >> 6)) #define SHA512_ROUND(a, b, c, d, e, f, g, h, t, s, w) \ do { \ (h) += SHA512_BSIG1(e) + SHA512_CH(e, f, g) + K512[t] + (w)[s]; \ (d) += (h); \ (h) += SHA512_BSIG0(a) + SHA512_MAJ(a, b, c); \ } while (0) #define ROUND00_15(w, a, b, c, d, e, f, g, h, t, M) \ do { \ (w)[t] = Uint64FromBeBytes((M) + (t) * 8); \ SHA512_ROUND(a, b, c, d, e, f, g, h, t, t, w); \ } while (0) #define ROUND16_79(w, a, b, c, d, e, f, g, h, t, s) \ do { \ (w)[s] += SHA512_SSIG1((w)[((s) + 14) & 0xF]) + (w)[((s) + 9) & 0xF] + SHA512_SSIG0((w)[((s) + 1) & 0xF]); \ SHA512_ROUND(a, b, c, d, e, f, g, h, (t) + (s), s, w); \ } while (0) void SHA512CompressMultiBlocks(uint64_t hash[8], const uint8_t *bl, uint32_t bcnt) { uint32_t t; uint64_t w[16]; const uint8_t *block = bl; uint32_t blockn = bcnt; while (blockn > 0) { uint64_t a = hash[0]; uint64_t b = hash[1]; uint64_t c = hash[2]; uint64_t d = hash[3]; uint64_t e = hash[4]; uint64_t f = hash[5]; uint64_t g = hash[6]; uint64_t h = hash[7]; ROUND00_15(w, a, b, c, d, e, f, g, h, 0, block); ROUND00_15(w, h, a, b, c, d, e, f, g, 1, block); ROUND00_15(w, g, h, a, b, c, d, e, f, 2, block); ROUND00_15(w, f, g, h, a, b, c, d, e, 3, block); ROUND00_15(w, e, f, g, h, a, b, c, d, 4, block); ROUND00_15(w, d, e, f, g, h, a, b, c, 5, block); ROUND00_15(w, c, d, e, f, g, h, a, b, 6, block); ROUND00_15(w, b, c, d, e, f, g, h, a, 7, block); ROUND00_15(w, a, b, c, d, e, f, g, h, 8, block); ROUND00_15(w, h, a, b, c, d, e, f, g, 9, block); ROUND00_15(w, g, h, a, b, c, d, e, f, 10, block); ROUND00_15(w, f, g, h, a, b, c, d, e, 11, block); ROUND00_15(w, e, f, g, h, a, b, c, d, 12, block); ROUND00_15(w, d, e, f, g, h, a, b, c, 13, block); ROUND00_15(w, c, d, e, f, g, h, a, b, 14, block); ROUND00_15(w, b, c, d, e, f, g, h, a, 15, block); // 16th - 79th round of operation, corresponding to steps 1 and 3 in rfc6234 6.4 for (t = 16; t < 80; t += 16) { ROUND16_79(w, a, b, c, d, e, f, g, h, t, 0); ROUND16_79(w, h, a, b, c, d, e, f, g, t, 1); ROUND16_79(w, g, h, a, b, c, d, e, f, t, 2); ROUND16_79(w, f, g, h, a, b, c, d, e, t, 3); ROUND16_79(w, e, f, g, h, a, b, c, d, t, 4); ROUND16_79(w, d, e, f, g, h, a, b, c, t, 5); ROUND16_79(w, c, d, e, f, g, h, a, b, t, 6); ROUND16_79(w, b, c, d, e, f, g, h, a, t, 7); ROUND16_79(w, a, b, c, d, e, f, g, h, t, 8); ROUND16_79(w, h, a, b, c, d, e, f, g, t, 9); ROUND16_79(w, g, h, a, b, c, d, e, f, t, 10); ROUND16_79(w, f, g, h, a, b, c, d, e, t, 11); ROUND16_79(w, e, f, g, h, a, b, c, d, t, 12); ROUND16_79(w, d, e, f, g, h, a, b, c, t, 13); ROUND16_79(w, c, d, e, f, g, h, a, b, t, 14); ROUND16_79(w, b, c, d, e, f, g, h, a, t, 15); } // RFC6234 STEP 4: Compute the intermediate hash value H(i) hash[0] += a; hash[1] += b; hash[2] += c; hash[3] += d; hash[4] += e; hash[5] += f; hash[6] += g; hash[7] += h; block += CRYPT_SHA2_512_BLOCKSIZE; blockn--; } } #endif
2301_79861745/bench_create
crypto/sha2/src/noasm_sha512.c
C
unknown
6,740
/* * 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_SHA512) && defined(HITLS_CRYPTO_SHA512_SMALL_MEM) #include "crypt_sha2.h" #include "crypt_utils.h" #include "sha2_core.h" #ifndef U64 #define U64(v) (uint64_t)(v) #endif // define in rfc 4634 static const uint64_t K512[80] = { // Round 0-15 U64(0x428a2f98d728ae22), U64(0x7137449123ef65cd), U64(0xb5c0fbcfec4d3b2f), U64(0xe9b5dba58189dbbc), U64(0x3956c25bf348b538), U64(0x59f111f1b605d019), U64(0x923f82a4af194f9b), U64(0xab1c5ed5da6d8118), U64(0xd807aa98a3030242), U64(0x12835b0145706fbe), U64(0x243185be4ee4b28c), U64(0x550c7dc3d5ffb4e2), U64(0x72be5d74f27b896f), U64(0x80deb1fe3b1696b1), U64(0x9bdc06a725c71235), U64(0xc19bf174cf692694), // Round 16-31 U64(0xe49b69c19ef14ad2), U64(0xefbe4786384f25e3), U64(0x0fc19dc68b8cd5b5), U64(0x240ca1cc77ac9c65), U64(0x2de92c6f592b0275), U64(0x4a7484aa6ea6e483), U64(0x5cb0a9dcbd41fbd4), U64(0x76f988da831153b5), U64(0x983e5152ee66dfab), U64(0xa831c66d2db43210), U64(0xb00327c898fb213f), U64(0xbf597fc7beef0ee4), U64(0xc6e00bf33da88fc2), U64(0xd5a79147930aa725), U64(0x06ca6351e003826f), U64(0x142929670a0e6e70), // Round 32-47 U64(0x27b70a8546d22ffc), U64(0x2e1b21385c26c926), U64(0x4d2c6dfc5ac42aed), U64(0x53380d139d95b3df), U64(0x650a73548baf63de), U64(0x766a0abb3c77b2a8), U64(0x81c2c92e47edaee6), U64(0x92722c851482353b), U64(0xa2bfe8a14cf10364), U64(0xa81a664bbc423001), U64(0xc24b8b70d0f89791), U64(0xc76c51a30654be30), U64(0xd192e819d6ef5218), U64(0xd69906245565a910), U64(0xf40e35855771202a), U64(0x106aa07032bbd1b8), // Round 48-63 U64(0x19a4c116b8d2d0c8), U64(0x1e376c085141ab53), U64(0x2748774cdf8eeb99), U64(0x34b0bcb5e19b48a8), U64(0x391c0cb3c5c95a63), U64(0x4ed8aa4ae3418acb), U64(0x5b9cca4f7763e373), U64(0x682e6ff3d6b2b8a3), U64(0x748f82ee5defb2fc), U64(0x78a5636f43172f60), U64(0x84c87814a1f0ab72), U64(0x8cc702081a6439ec), U64(0x90befffa23631e28), U64(0xa4506cebde82bde9), U64(0xbef9a3f7b2c67915), U64(0xc67178f2e372532b), // Round 64-79 U64(0xca273eceea26619c), U64(0xd186b8c721c0c207), U64(0xeada7dd6cde0eb1e), U64(0xf57d4f7fee6ed178), U64(0x06f067aa72176fba), U64(0x0a637dc5a2c898a6), U64(0x113f9804bef90dae), U64(0x1b710b35131c471b), U64(0x28db77f523047d84), U64(0x32caab7b40c72493), U64(0x3c9ebe0a15c9bebc), U64(0x431d67c49c100d4c), U64(0x4cc5d4becb3e42b6), U64(0x597f299cfc657e2a), U64(0x5fcb6fab3ad6faec), U64(0x6c44198c4a475817) }; #undef ROUND00_15 #undef ROUND16_79 #define SHA512_CH(x, y, z) (((x) & (y)) ^ ((~(x)) & (z))) #define SHA512_MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) #define SHA512_BSIG0(x) (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39)) #define SHA512_BSIG1(x) (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41)) #define SHA512_SSIG0(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ ((x) >> 7)) #define SHA512_SSIG1(x) (ROTR64(x, 19) ^ ROTR64(x, 61) ^ ((x) >> 6)) // Prepare the message schedule W static inline void Sha512ExtendMessage(uint64_t *w, uint32_t t) { w[t & 0xF] += SHA512_SSIG1(w[(t - 2) & 0xF]) + w[(t - 7) & 0xF] + SHA512_SSIG0(w[(t - 15) & 0xF]); } // Perform the main hash computation static inline void Sha512Compress(uint64_t *state, uint64_t w, uint64_t k) { uint64_t t1 = state[7] + SHA512_BSIG1(state[4]) + SHA512_CH(state[4], state[5], state[6]) + k + w; uint64_t t2 = SHA512_BSIG0(state[0]) + SHA512_MAJ(state[0], state[1], state[2]); state[7] = state[6]; // h = g state[6] = state[5]; // g = f state[5] = state[4]; // f = e state[4] = state[3] + t1; // e = d + T1 state[3] = state[2]; // d = c state[2] = state[1]; // c = b state[1] = state[0]; // b = a state[0] = t1 + t2; // a = T1 + T2 } void SHA512CompressMultiBlocks(uint64_t hash[8], const uint8_t *bl, uint32_t bcnt) { uint32_t t; uint64_t state[8]; uint64_t w[16]; const uint8_t *block = bl; uint32_t blockn = bcnt; while (blockn > 0) { // Initialize the working variables for (t = 0; t < 8; t++) { state[t] = hash[t]; } // Handle the first 16 rounds for (t = 0; t < 16; t++) { w[t] = Uint64FromBeBytes(block + t * 8); Sha512Compress(state, w[t], K512[t]); } // 16th - 79th round of operation, corresponding to steps 1 and 3 in rfc6234 6.4 for (t = 16; t < 80; t++) { Sha512ExtendMessage(w, t); Sha512Compress(state, w[t & 0xF], K512[t]); } // RFC6234 STEP 4: Compute the intermediate hash value H(i) for (t = 0; t < 8; t++) { hash[t] += state[t]; } block += CRYPT_SHA2_512_BLOCKSIZE; blockn--; } } #endif // HITLS_CRYPTO_SHA512 && HITLS_CRYPTO_SHA512_SMALL_MEM
2301_79861745/bench_create
crypto/sha2/src/noasm_sha512_small.c
C
unknown
5,421
/* * 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_SHA256 #include "crypt_sha2.h" #include <stdlib.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "sha2_core.h" #include "bsl_sal.h" #include "crypt_types.h" struct CryptSha256Ctx { uint32_t h[CRYPT_SHA2_256_DIGESTSIZE / sizeof(uint32_t)]; /* 256 bits for SHA256 state */ uint32_t block[CRYPT_SHA2_256_BLOCKSIZE / sizeof(uint32_t)]; /* 512 bits block cache */ uint32_t lNum, hNum; /* input bits counter, max 2^64 bits */ uint32_t blocklen; /* block length */ uint32_t outlen; /* digest output length */ uint32_t errorCode; /* error Code */ }; CRYPT_SHA2_256_Ctx *CRYPT_SHA2_256_NewCtx(void) { return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA2_256_Ctx)); } CRYPT_SHA2_256_Ctx *CRYPT_SHA2_256_NewCtxEx(void *libCtx, int32_t algId) { (void)libCtx; (void)algId; return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA2_256_Ctx)); } void CRYPT_SHA2_256_FreeCtx(CRYPT_SHA2_256_Ctx *ctx) { BSL_SAL_ClearFree(ctx, sizeof(CRYPT_SHA2_256_Ctx)); } int32_t CRYPT_SHA2_256_Init(CRYPT_SHA2_256_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_SHA2_256_Ctx), 0, sizeof(CRYPT_SHA2_256_Ctx)); /** * @RFC 4634 6.1 SHA-224 and SHA-256 Initialization * SHA-256, the initial hash value, H(0): * H(0)0 = 6a09e667 * H(0)1 = bb67ae85 * H(0)2 = 3c6ef372 * H(0)3 = a54ff53a * H(0)4 = 510e527f * H(0)5 = 9b05688c * H(0)6 = 1f83d9ab * H(0)7 = 5be0cd19 */ ctx->h[0] = 0x6a09e667UL; ctx->h[1] = 0xbb67ae85UL; ctx->h[2] = 0x3c6ef372UL; ctx->h[3] = 0xa54ff53aUL; ctx->h[4] = 0x510e527fUL; ctx->h[5] = 0x9b05688cUL; ctx->h[6] = 0x1f83d9abUL; ctx->h[7] = 0x5be0cd19UL; ctx->outlen = CRYPT_SHA2_256_DIGESTSIZE; return CRYPT_SUCCESS; } int32_t CRYPT_SHA2_256_Deinit(CRYPT_SHA2_256_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_CleanseData((void *)(ctx), sizeof(CRYPT_SHA2_256_Ctx)); return CRYPT_SUCCESS; } int32_t CRYPT_SHA2_256_CopyCtx(CRYPT_SHA2_256_Ctx *dst, const CRYPT_SHA2_256_Ctx *src) { if (dst == NULL || src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memcpy_s(dst, sizeof(CRYPT_SHA2_256_Ctx), src, sizeof(CRYPT_SHA2_256_Ctx)); return CRYPT_SUCCESS; } CRYPT_SHA2_256_Ctx *CRYPT_SHA2_256_DupCtx(const CRYPT_SHA2_256_Ctx *src) { if (src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_SHA2_256_Ctx *newCtx = CRYPT_SHA2_256_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memcpy_s(newCtx, sizeof(CRYPT_SHA2_256_Ctx), src, sizeof(CRYPT_SHA2_256_Ctx)); return newCtx; } static int32_t CheckIsCorrupted(CRYPT_SHA2_256_Ctx *ctx, uint32_t nbytes); static int32_t UpdateParamIsValid(CRYPT_SHA2_256_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(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } if (CheckIsCorrupted(ctx, nbytes) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } return CRYPT_SUCCESS; } static int32_t CheckIsCorrupted(CRYPT_SHA2_256_Ctx *ctx, uint32_t nbytes) { uint32_t cnt0 = (ctx->lNum + (nbytes << SHIFTS_PER_BYTE)) & 0xffffffffUL; if (cnt0 < ctx->lNum) { /* overflow */ if (++ctx->hNum == 0) { ctx->errorCode = CRYPT_SHA2_INPUT_OVERFLOW; BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } } uint32_t cnt1 = ctx->hNum + (uint32_t)(nbytes >> (BITSIZE(uint32_t) - SHIFTS_PER_BYTE)); if (cnt1 < ctx->hNum) { /* overflow */ ctx->errorCode = CRYPT_SHA2_INPUT_OVERFLOW; BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } ctx->hNum = cnt1; ctx->lNum = cnt0; return CRYPT_SUCCESS; } int32_t CRYPT_SHA2_256_Update(CRYPT_SHA2_256_Ctx *ctx, const uint8_t *data, uint32_t nbytes) { int32_t ret = UpdateParamIsValid(ctx, data, nbytes); if (ret != CRYPT_SUCCESS) { return ret; } if (nbytes == 0) { return CRYPT_SUCCESS; } const uint8_t *d = data; uint32_t left = nbytes; uint32_t n = ctx->blocklen; uint8_t *p = (uint8_t *)ctx->block; if (left < CRYPT_SHA2_256_BLOCKSIZE - n) { if (memcpy_s(p + n, CRYPT_SHA2_256_BLOCKSIZE - n, d, left) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } ctx->blocklen += (uint32_t)left; return CRYPT_SUCCESS; } if ((n != 0) && (left >= CRYPT_SHA2_256_BLOCKSIZE - n)) { if (memcpy_s(p + n, CRYPT_SHA2_256_BLOCKSIZE - n, d, CRYPT_SHA2_256_BLOCKSIZE - n) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } SHA256CompressMultiBlocks(ctx->h, p, 1); n = CRYPT_SHA2_256_BLOCKSIZE - n; d += n; left -= n; ctx->blocklen = 0; (void)memset_s(p, CRYPT_SHA2_256_BLOCKSIZE, 0, CRYPT_SHA2_256_BLOCKSIZE); } n = (uint32_t)(left / CRYPT_SHA2_256_BLOCKSIZE); if (n > 0) { SHA256CompressMultiBlocks(ctx->h, d, n); n *= CRYPT_SHA2_256_BLOCKSIZE; d += n; left -= n; } if (left != 0) { ctx->blocklen = (uint32_t)left; if (memcpy_s((uint8_t *)ctx->block, CRYPT_SHA2_256_BLOCKSIZE, d, left) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } } return CRYPT_SUCCESS; } static int32_t FinalParamIsValid(const CRYPT_SHA2_256_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 < ctx->outlen) { BSL_ERR_PUSH_ERROR(CRYPT_SHA2_OUT_BUFF_LEN_NOT_ENOUGH); return CRYPT_SHA2_OUT_BUFF_LEN_NOT_ENOUGH; } if (ctx->errorCode == CRYPT_SHA2_INPUT_OVERFLOW) { BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } return CRYPT_SUCCESS; } int32_t CRYPT_SHA2_256_Final(CRYPT_SHA2_256_Ctx *ctx, uint8_t *digest, uint32_t *outlen) { int32_t ret = FinalParamIsValid(ctx, digest, outlen); if (ret != CRYPT_SUCCESS) { return ret; } uint8_t *p = (uint8_t *)ctx->block; uint32_t n = ctx->blocklen; p[n++] = 0x80; if (n > (CRYPT_SHA2_256_BLOCKSIZE - 8)) { /* 8 bytes to save bits of input */ (void)memset_s(p + n, CRYPT_SHA2_256_BLOCKSIZE - n, 0, CRYPT_SHA2_256_BLOCKSIZE - n); n = 0; SHA256CompressMultiBlocks(ctx->h, p, 1); } (void)memset_s(p + n, CRYPT_SHA2_256_BLOCKSIZE - n, 0, CRYPT_SHA2_256_BLOCKSIZE - 8 - n); /* 8 bytes to save bits of input */ p += CRYPT_SHA2_256_BLOCKSIZE - 8; /* 8 bytes to save bits of input */ PUT_UINT32_BE(ctx->hNum, p, 0); p += sizeof(uint32_t); PUT_UINT32_BE(ctx->lNum, p, 0); p += sizeof(uint32_t); p -= CRYPT_SHA2_256_BLOCKSIZE; SHA256CompressMultiBlocks(ctx->h, p, 1); ctx->blocklen = 0; (void)memset_s(p, CRYPT_SHA2_256_BLOCKSIZE, 0, CRYPT_SHA2_256_BLOCKSIZE); n = ctx->outlen / sizeof(uint32_t); for (uint32_t nn = 0; nn < n; nn++) { PUT_UINT32_BE(ctx->h[nn], digest, sizeof(uint32_t) * nn); } *outlen = ctx->outlen; return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SHA2_256_GetParam(CRYPT_SHA2_256_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA2_256_DIGESTSIZE, CRYPT_SHA2_256_BLOCKSIZE, param); } #endif #ifdef HITLS_CRYPTO_SHA224 int32_t CRYPT_SHA2_224_Init(CRYPT_SHA2_224_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_SHA2_224_Ctx), 0, sizeof(CRYPT_SHA2_224_Ctx)); /** * @RFC 4634 6.1 SHA-224 and SHA-256 Initialization * SHA-224, the initial hash value, H(0): * H(0)0 = c1059ed8 * H(0)1 = 367cd507 * H(0)2 = 3070dd17 * H(0)3 = f70e5939 * H(0)4 = ffc00b31 * H(0)5 = 68581511 * H(0)6 = 64f98fa7 * H(0)7 = befa4fa4 */ ctx->h[0] = 0xc1059ed8UL; ctx->h[1] = 0x367cd507UL; ctx->h[2] = 0x3070dd17UL; ctx->h[3] = 0xf70e5939UL; ctx->h[4] = 0xffc00b31UL; ctx->h[5] = 0x68581511UL; ctx->h[6] = 0x64f98fa7UL; ctx->h[7] = 0xbefa4fa4UL; ctx->outlen = CRYPT_SHA2_224_DIGESTSIZE; return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SHA2_224_GetParam(CRYPT_SHA2_224_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA2_224_DIGESTSIZE, CRYPT_SHA2_224_BLOCKSIZE, param); } #endif #endif // HITLS_CRYPTO_SHA224 #endif // HITLS_CRYPTO_SHA256
2301_79861745/bench_create
crypto/sha2/src/sha2_256.c
C
unknown
10,121
/* * 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_SHA512 #include "crypt_sha2.h" #include <stdlib.h> #include "securec.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "sha2_core.h" #include "bsl_sal.h" #include "crypt_types.h" #define SHA2_512_PADSIZE 112 // function : num1 += num2(num1 and num2 are 128bit int32_t) static int32_t Add128Bit(uint64_t *num1h, uint64_t *num1l, uint64_t num2h, uint64_t num2l) { uint64_t num1hTemp = *num1h; uint64_t num1lTemp = *num1l; uint64_t sumh = num1hTemp; uint64_t suml = num1lTemp + num2l; uint64_t carry = 0; if (suml < num1lTemp) { carry = 1; sumh += carry; } sumh += num2h; // num2h + carry >= 1, thus sumh shoud > num1hTemp; otherwise overflow if ((carry > 0 || num2h > 0) && sumh <= num1hTemp) { BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } *num1h = sumh; *num1l = suml; return 0; } struct CryptSha2512Ctx { uint64_t h[CRYPT_SHA2_512_DIGESTSIZE / sizeof(uint64_t)]; uint8_t block[CRYPT_SHA2_512_BLOCKSIZE]; uint64_t lNum, hNum; uint32_t num, mdlen; uint32_t errorCode; /* error Code */ }; static int32_t CheckIsCorrupted(CRYPT_SHA2_512_Ctx *ctx, uint32_t nbytes) { // bit len of data = len << 3, which may be 2^67, thus need to 2 uint64 to represent uint64_t bitLenl = (uint64_t)nbytes << 3; // low 64 bit // high 64 bit, right shift 61 to get higest 3 bit uint64_t bitLenh = sizeof(nbytes) >= sizeof(uint64_t) ? (uint64_t)nbytes >> 61 : 0; if (Add128Bit(&ctx->hNum, &ctx->lNum, bitLenh, bitLenl) != 0) { // overflow, the len of msg over 2 ^ 128; ctx->errorCode = CRYPT_SHA2_INPUT_OVERFLOW; BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } return CRYPT_SUCCESS; } CRYPT_SHA2_512_Ctx *CRYPT_SHA2_512_NewCtx(void) { return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA2_512_Ctx)); } CRYPT_SHA2_512_Ctx *CRYPT_SHA2_512_NewCtxEx(void *libCtx, int32_t algId) { (void)libCtx; (void)algId; return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA2_512_Ctx)); } void CRYPT_SHA2_512_FreeCtx(CRYPT_SHA2_512_Ctx *ctx) { BSL_SAL_ClearFree(ctx, sizeof(CRYPT_SHA2_512_Ctx)); } int32_t CRYPT_SHA2_512_Init(CRYPT_SHA2_512_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_SHA2_512_Ctx), 0, sizeof(CRYPT_SHA2_512_Ctx)); // see RFC6234 chapter 6.3 ctx->h[0] = U64(0x6a09e667f3bcc908); ctx->h[1] = U64(0xbb67ae8584caa73b); ctx->h[2] = U64(0x3c6ef372fe94f82b); ctx->h[3] = U64(0xa54ff53a5f1d36f1); ctx->h[4] = U64(0x510e527fade682d1); ctx->h[5] = U64(0x9b05688c2b3e6c1f); ctx->h[6] = U64(0x1f83d9abfb41bd6b); ctx->h[7] = U64(0x5be0cd19137e2179); ctx->mdlen = CRYPT_SHA2_512_DIGESTSIZE; return CRYPT_SUCCESS; } int32_t CRYPT_SHA2_512_Deinit(CRYPT_SHA2_512_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_CleanseData((void *)(ctx), sizeof(CRYPT_SHA2_512_Ctx)); return CRYPT_SUCCESS; } int32_t CRYPT_SHA2_512_CopyCtx(CRYPT_SHA2_512_Ctx *dst, const CRYPT_SHA2_512_Ctx *src) { if (dst == NULL || src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memcpy_s(dst, sizeof(CRYPT_SHA2_512_Ctx), src, sizeof(CRYPT_SHA2_512_Ctx)); return CRYPT_SUCCESS; } CRYPT_SHA2_512_Ctx *CRYPT_SHA2_512_DupCtx(const CRYPT_SHA2_512_Ctx *src) { if (src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_SHA2_512_Ctx *newCtx = CRYPT_SHA2_512_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memcpy_s(newCtx, sizeof(CRYPT_SHA2_512_Ctx), src, sizeof(CRYPT_SHA2_512_Ctx)); return newCtx; } static int32_t UpdateParamIsValid(CRYPT_SHA2_512_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_SHA2_INPUT_OVERFLOW) { BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } return CheckIsCorrupted(ctx, nbytes); } int32_t CRYPT_SHA2_512_Update(CRYPT_SHA2_512_Ctx *ctx, const uint8_t *data, uint32_t nbytes) { int32_t ret = UpdateParamIsValid(ctx, data, nbytes); if (ret != CRYPT_SUCCESS) { return ret; } if (nbytes == 0) { return CRYPT_SUCCESS; } const uint32_t n = CRYPT_SHA2_512_BLOCKSIZE - ctx->num; if (nbytes < n) { // if the data can't fill block, just copy data to block (void)memcpy_s(ctx->block + ctx->num, n, data, nbytes); ctx->num += (uint32_t)nbytes; return CRYPT_SUCCESS; } const uint8_t *d = data; uint32_t dataLen = nbytes; if (ctx->num != 0) { // fill the block first and compute (void)memcpy_s(ctx->block + ctx->num, n, data, n); ctx->num = 0; dataLen -= n; d += n; SHA512CompressMultiBlocks(ctx->h, ctx->block, 1); } SHA512CompressMultiBlocks(ctx->h, d, dataLen / CRYPT_SHA2_512_BLOCKSIZE); d += dataLen; dataLen &= (CRYPT_SHA2_512_BLOCKSIZE - 1); d -= dataLen; if (dataLen != 0) { // copy rest data to blcok if (memcpy_s(ctx->block, CRYPT_SHA2_512_BLOCKSIZE, d, dataLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } ctx->num = (uint32_t)dataLen; } return CRYPT_SUCCESS; } static int32_t FinalParamIsValid(const CRYPT_SHA2_512_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 < ctx->mdlen) { BSL_ERR_PUSH_ERROR(CRYPT_SHA2_OUT_BUFF_LEN_NOT_ENOUGH); return CRYPT_SHA2_OUT_BUFF_LEN_NOT_ENOUGH; } if (ctx->errorCode == CRYPT_SHA2_INPUT_OVERFLOW) { BSL_ERR_PUSH_ERROR(CRYPT_SHA2_INPUT_OVERFLOW); return CRYPT_SHA2_INPUT_OVERFLOW; } return CRYPT_SUCCESS; } int32_t CRYPT_SHA2_512_Final(CRYPT_SHA2_512_Ctx *ctx, uint8_t *digest, uint32_t *len) { int32_t ret = FinalParamIsValid(ctx, digest, len); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t pad; uint32_t n = ctx->num; uint8_t *block = ctx->block; block[n++] = 0x80; if (n > SHA2_512_PADSIZE) { pad = CRYPT_SHA2_512_BLOCKSIZE - n; (void)memset_s(block + n, pad, 0, pad); SHA512CompressMultiBlocks(ctx->h, block, 1); n = 0; pad = SHA2_512_PADSIZE; } else { pad = SHA2_512_PADSIZE - n; } (void)memset_s(block + n, pad, 0, pad); Uint64ToBeBytes(ctx->hNum, block + SHA2_512_PADSIZE); Uint64ToBeBytes(ctx->lNum, block + SHA2_512_PADSIZE + sizeof(uint64_t)); SHA512CompressMultiBlocks(ctx->h, block, 1); uint8_t *out = digest; uint32_t ncnt = ctx->mdlen >> 3; // MDSize / 8, calculate the number of times that values need to be assigned to out for (n = 0; n < ncnt; n++) { Uint64ToBeBytes(ctx->h[n], out); out += sizeof(uint64_t); } *len = ctx->mdlen; return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SHA2_512_GetParam(CRYPT_SHA2_512_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA2_512_DIGESTSIZE, CRYPT_SHA2_512_BLOCKSIZE, param); } #endif #ifdef HITLS_CRYPTO_SHA384 int32_t CRYPT_SHA2_384_Init(CRYPT_SHA2_384_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_SHA2_384_Ctx), 0, sizeof(CRYPT_SHA2_384_Ctx)); ctx->h[0] = U64(0xcbbb9d5dc1059ed8); ctx->h[1] = U64(0x629a292a367cd507); ctx->h[2] = U64(0x9159015a3070dd17); ctx->h[3] = U64(0x152fecd8f70e5939); ctx->h[4] = U64(0x67332667ffc00b31); ctx->h[5] = U64(0x8eb44a8768581511); ctx->h[6] = U64(0xdb0c2e0d64f98fa7); ctx->h[7] = U64(0x47b5481dbefa4fa4); ctx->mdlen = CRYPT_SHA2_384_DIGESTSIZE; return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SHA2_384_GetParam(CRYPT_SHA2_384_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA2_384_DIGESTSIZE, CRYPT_SHA2_384_BLOCKSIZE, param); } #endif #endif // HITLS_CRYPTO_SHA384 #endif // HITLS_CRYPTO_SHA512
2301_79861745/bench_create
crypto/sha2/src/sha2_512.c
C
unknown
9,332
/* * 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 SHA2_CORE_H #define SHA2_CORE_H #include <stdint.h> #include "hitls_build.h" #ifdef __cplusplus extern "C" { #endif #ifndef U64 #define U64(v) (uint64_t)(v) #endif #ifdef HITLS_CRYPTO_SHA256 void SHA256CompressMultiBlocks(uint32_t hash[8], const uint8_t *in, uint32_t num); #endif #ifdef HITLS_CRYPTO_SHA512 void SHA512CompressMultiBlocks(uint64_t hash[8], const uint8_t *bl, uint32_t bcnt); #endif #ifdef __cplusplus } #endif #endif // SHA2_CORE_H
2301_79861745/bench_create
crypto/sha2/src/sha2_core.h
C
unknown
1,007
/* * 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_SHA3_H #define CRYPT_SHA3_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA3 #include <stdint.h> #include <stdlib.h> #include "crypt_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ /** @defgroup LLF SHA3 Low level function */ /* SHA3-224 */ #define CRYPT_SHA3_224_BLOCKSIZE 144 // ((1600 - 224 * 2) / 8) #define CRYPT_SHA3_224_DIGESTSIZE 28 /* SHA3-256 */ #define CRYPT_SHA3_256_BLOCKSIZE 136 // ((1600 - 256 * 2) / 8) #define CRYPT_SHA3_256_DIGESTSIZE 32 /* SHA3-384 */ #define CRYPT_SHA3_384_BLOCKSIZE 104 // ((1600 - 384 * 2) / 8) #define CRYPT_SHA3_384_DIGESTSIZE 48 /* SHA3-512 */ #define CRYPT_SHA3_512_BLOCKSIZE 72 // ((1600 - 512 * 2) / 8) #define CRYPT_SHA3_512_DIGESTSIZE 64 /* SHAKE128 */ #define CRYPT_SHAKE128_BLOCKSIZE 168 // ((1600 - 128 * 2) / 8) #define CRYPT_SHAKE128_DIGESTSIZE 0 /* SHAKE256 */ #define CRYPT_SHAKE256_BLOCKSIZE 136 // ((1600 - 256 * 2) / 8) #define CRYPT_SHAKE256_DIGESTSIZE 0 typedef struct CryptSha3Ctx CRYPT_SHA3_Ctx; typedef CRYPT_SHA3_Ctx CRYPT_SHA3_224_Ctx; typedef CRYPT_SHA3_Ctx CRYPT_SHA3_256_Ctx; typedef CRYPT_SHA3_Ctx CRYPT_SHA3_384_Ctx; typedef CRYPT_SHA3_Ctx CRYPT_SHA3_512_Ctx; typedef CRYPT_SHA3_Ctx CRYPT_SHAKE128_Ctx; typedef CRYPT_SHA3_Ctx CRYPT_SHAKE256_Ctx; CRYPT_SHA3_Ctx *CRYPT_SHA3_NewCtx(void); #define CRYPT_SHA3_224_NewCtx CRYPT_SHA3_NewCtx #define CRYPT_SHA3_256_NewCtx CRYPT_SHA3_NewCtx #define CRYPT_SHA3_384_NewCtx CRYPT_SHA3_NewCtx #define CRYPT_SHA3_512_NewCtx CRYPT_SHA3_NewCtx #define CRYPT_SHAKE128_NewCtx CRYPT_SHA3_NewCtx #define CRYPT_SHAKE256_NewCtx CRYPT_SHA3_NewCtx CRYPT_SHA3_Ctx *CRYPT_SHA3_NewCtxEx(void *libCtx, int32_t algId); #define CRYPT_SHA3_224_NewCtxEx CRYPT_SHA3_NewCtxEx #define CRYPT_SHA3_256_NewCtxEx CRYPT_SHA3_NewCtxEx #define CRYPT_SHA3_384_NewCtxEx CRYPT_SHA3_NewCtxEx #define CRYPT_SHA3_512_NewCtxEx CRYPT_SHA3_NewCtxEx #define CRYPT_SHAKE128_NewCtxEx CRYPT_SHA3_NewCtxEx #define CRYPT_SHAKE256_NewCtxEx CRYPT_SHA3_NewCtxEx void CRYPT_SHA3_FreeCtx(CRYPT_SHA3_Ctx *ctx); #define CRYPT_SHA3_224_FreeCtx CRYPT_SHA3_FreeCtx #define CRYPT_SHA3_256_FreeCtx CRYPT_SHA3_FreeCtx #define CRYPT_SHA3_384_FreeCtx CRYPT_SHA3_FreeCtx #define CRYPT_SHA3_512_FreeCtx CRYPT_SHA3_FreeCtx #define CRYPT_SHAKE128_FreeCtx CRYPT_SHA3_FreeCtx #define CRYPT_SHAKE256_FreeCtx CRYPT_SHA3_FreeCtx // Initialize the context int32_t CRYPT_SHA3_224_Init(CRYPT_SHA3_224_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHA3_256_Init(CRYPT_SHA3_256_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHA3_384_Init(CRYPT_SHA3_384_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHA3_512_Init(CRYPT_SHA3_512_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHAKE128_Init(CRYPT_SHAKE128_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHAKE256_Init(CRYPT_SHAKE256_Ctx *ctx, BSL_Param *param); // Data update API int32_t CRYPT_SHA3_Update(CRYPT_SHA3_Ctx *ctx, const uint8_t *in, uint32_t len); #define CRYPT_SHA3_224_Update CRYPT_SHA3_Update #define CRYPT_SHA3_256_Update CRYPT_SHA3_Update #define CRYPT_SHA3_384_Update CRYPT_SHA3_Update #define CRYPT_SHA3_512_Update CRYPT_SHA3_Update #define CRYPT_SHAKE128_Update CRYPT_SHA3_Update #define CRYPT_SHAKE256_Update CRYPT_SHA3_Update // Padding and output the digest value int32_t CRYPT_SHA3_Final(CRYPT_SHA3_Ctx *ctx, uint8_t *out, uint32_t *len); #define CRYPT_SHA3_224_Final CRYPT_SHA3_Final #define CRYPT_SHA3_256_Final CRYPT_SHA3_Final #define CRYPT_SHA3_384_Final CRYPT_SHA3_Final #define CRYPT_SHA3_512_Final CRYPT_SHA3_Final #define CRYPT_SHAKE128_Final CRYPT_SHA3_Final #define CRYPT_SHAKE256_Final CRYPT_SHA3_Final int32_t CRYPT_SHA3_Squeeze(CRYPT_SHA3_Ctx *ctx, uint8_t *out, uint32_t len); #define CRYPT_SHA3_224_Squeeze NULL #define CRYPT_SHA3_256_Squeeze NULL #define CRYPT_SHA3_384_Squeeze NULL #define CRYPT_SHA3_512_Squeeze NULL #define CRYPT_SHAKE128_Squeeze CRYPT_SHA3_Squeeze #define CRYPT_SHAKE256_Squeeze CRYPT_SHA3_Squeeze // Clear the context int32_t CRYPT_SHA3_Deinit(CRYPT_SHA3_Ctx *ctx); #define CRYPT_SHA3_224_Deinit CRYPT_SHA3_Deinit #define CRYPT_SHA3_256_Deinit CRYPT_SHA3_Deinit #define CRYPT_SHA3_384_Deinit CRYPT_SHA3_Deinit #define CRYPT_SHA3_512_Deinit CRYPT_SHA3_Deinit #define CRYPT_SHAKE128_Deinit CRYPT_SHA3_Deinit #define CRYPT_SHAKE256_Deinit CRYPT_SHA3_Deinit // Copy the context int32_t CRYPT_SHA3_CopyCtx(CRYPT_SHA3_Ctx *dst, const CRYPT_SHA3_Ctx *src); #define CRYPT_SHA3_224_CopyCtx CRYPT_SHA3_CopyCtx #define CRYPT_SHA3_256_CopyCtx CRYPT_SHA3_CopyCtx #define CRYPT_SHA3_384_CopyCtx CRYPT_SHA3_CopyCtx #define CRYPT_SHA3_512_CopyCtx CRYPT_SHA3_CopyCtx #define CRYPT_SHAKE128_CopyCtx CRYPT_SHA3_CopyCtx #define CRYPT_SHAKE256_CopyCtx CRYPT_SHA3_CopyCtx // Dup the context CRYPT_SHA3_Ctx *CRYPT_SHA3_DupCtx(const CRYPT_SHA3_Ctx *src); #define CRYPT_SHA3_224_DupCtx CRYPT_SHA3_DupCtx #define CRYPT_SHA3_256_DupCtx CRYPT_SHA3_DupCtx #define CRYPT_SHA3_384_DupCtx CRYPT_SHA3_DupCtx #define CRYPT_SHA3_512_DupCtx CRYPT_SHA3_DupCtx #define CRYPT_SHAKE128_DupCtx CRYPT_SHA3_DupCtx #define CRYPT_SHAKE256_DupCtx CRYPT_SHA3_DupCtx #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SHA3_224_GetParam(CRYPT_SHA3_224_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHA3_256_GetParam(CRYPT_SHA3_256_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHA3_384_GetParam(CRYPT_SHA3_384_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHA3_512_GetParam(CRYPT_SHA3_512_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHAKE128_GetParam(CRYPT_SHAKE128_Ctx *ctx, BSL_Param *param); int32_t CRYPT_SHAKE256_GetParam(CRYPT_SHAKE256_Ctx *ctx, BSL_Param *param); #else #define CRYPT_SHA3_224_GetParam NULL #define CRYPT_SHA3_256_GetParam NULL #define CRYPT_SHA3_384_GetParam NULL #define CRYPT_SHA3_512_GetParam NULL #define CRYPT_SHAKE128_GetParam NULL #define CRYPT_SHAKE256_GetParam NULL #endif #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA3 #endif // CRYPT_SHA3_H
2301_79861745/bench_create
crypto/sha3/include/crypt_sha3.h
C
unknown
6,405
/* * 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_SHA3 #include "crypt_arm.h" .arch armv8-a+crypto /* * Status matrix using register aliases * A00~A04: x0~x4 * A10~A14: x5~x9 * A20~A24: x10~x14 * A30~A34: x15~x19 * A40~A44: x20~x24 * T0~T4: x25~x29 temporary calculation register */ A00 .req x0 A01 .req x1 A02 .req x2 A03 .req x3 A04 .req x4 A10 .req x5 A11 .req x6 A12 .req x7 A13 .req x8 A14 .req x9 A20 .req x10 A21 .req x11 A22 .req x12 A23 .req x13 A24 .req x14 A30 .req x15 A31 .req x16 A32 .req x17 A33 .req x18 A34 .req x19 A40 .req x20 A41 .req x21 A42 .req x22 A43 .req x23 A44 .req x24 T0 .req x25 T1 .req x26 T2 .req x27 T3 .req x28 T4 .req x29 /** * Macro Description: THETA mapping function * Input register: * A00~A44: x0~x24 State Matrix * T0~T4: x25~x29 temporary calculation register * Modify the register: * A00~A44: x0~x24 State Matrix * T0~T4: x25~x29 temporary calculation register * Output register: * A00~A44: x0~x24 The latest State Matrix, among them, The values of A10, A20, A30, * and A40 are temporarily stored by T0, T1, T2, T3. * T0~T3: x25 to x29 temporarily store the values of A10, A20, A30, and A40. * Function/Macro Call: None */ .macro THETA // for x in 0…4, C[x] = A[x,0] xor A[x,1] xor A[x,2] xor A[x,3] xor A[x,4] eor T0, A00, A10 eor T1, A01, A11 eor T2, A02, A12 eor T3, A03, A13 eor T4, A04, A14 stp A00, A10, [sp, #-16]! // Borrow A00 and A10 eor T0, T0, A20 eor T1, T1, A21 eor T2, T2, A22 eor T3, T3, A23 eor T4, T4, A24 eor T0, T0, A30 eor T1, T1, A31 eor T2, T2, A32 eor T3, T3, A33 eor T4, T4, A34 eor T0, T0, A40 eor T1, T1, A41 eor T2, T2, A42 eor T3, T3, A43 eor T4, T4, A44 // D[1] = C[0] xor rol(C[2],1) eor A00, T0, T2, ror#63 // Borrow A00 // D[2] = C[1] xor rol(C[3],1) eor A10, T1, T3, ror#63 // Borrow A10 // for y in 0…4, A[y][1] ^= D[1] eor A01, A01, A00 eor A11, A11, A00 eor A21, A21, A00 eor A31, A31, A00 eor A41, A41, A00 // D[3] = C[2] xor rol(C[4],1) eor T2, T2, T4, ror#63 // for y in 0…4, A[y][2] ^= D[2] eor A02, A02, A10 eor A12, A12, A10 eor A22, A22, A10 eor A32, A32, A10 eor A42, A42, A10 // D[4] = C[3] xor rol(C[0],1) eor T3, T3, T0, ror#63 // for y in 0…4, A[y][3] ^= D[3] eor A03, A03, T2 eor A13, A13, T2 eor A23, A23, T2 eor A33, A33, T2 eor A43, A43, T2 ldp A00, A10, [sp], #16 // Restore A00 and A10 // D[0] = C[4] xor rol(C[1],1) eor T4, T4, T1, ror#63 // for y in 0…4, A[y][4] ^= D[4] eor A04, A04, T3 eor A14, A14, T3 eor A24, A24, T3 eor A34, A34, T3 eor A44, A44, T3 // for y in 0…4, A[y][0] ^= D[0] eor A00, A00, T4 eor T0, A10, T4 // Store A10, A20, A30, and A40 in the rho phase in advance. eor T1, A20, T4 eor T2, A30, T4 eor T3, A40, T4 .endm /** * Macro Description: RHO mapping function and PI mapping function * Input register: * A00~A44: x0~x24 State Matrix among them, The values of A10, A20, A30, and A40 are temporarily stored by T0, * T1, T2, T3 in the THETA function. * T0~T3: x25 to x28: temporarily store the values of A10, A20, A30, and A40. * Modify the register: * A00~A44: x0~x24 State Matrix * Output register: * A00~A44: x0~x24 The latest State Matrix * Function/Macro Call: None * Implementation part: * for x in 0…4: for y in 0…4: A[x, y] = rol(A[y,3x+y], rhotates[y,3x+y]) */ .macro RHOPi ror A10, A03, #64-28 ror A20, A01, #64-1 ror A30, A04, #64-27 ror A40, A02, #64-62 ror A01, A11, #64-44 ror A02, A22, #64-43 ror A03, A33, #64-21 ror A04, A44, #64-14 ror A11, A14, #64-20 ror A22, A23, #64-25 ror A33, A32, #64-15 ror A44, A41, #64-2 ror A14, A42, #64-61 ror A23, A34, #64-8 ror A32, A21, #64-10 ror A41, A13, #64-55 ror A42, A24, #64-39 ror A34, A43, #64-56 ror A21, A12, #64-6 ror A13, A31, #64-45 ror A24, T3, #64-18 ror A43, T2, #64-41 ror A12, T1, #64-3 ror A31, T0, #64-36 .endm /** * Macro Description: CHI mapping function与IOTA mapping function * Input register: * A00~A44: x0~x24 State Matrix * T0~T3: x25~x28 temporary calculation register * Modify the register: * A00~A44: x0~x24 State Matrix * T0~T3: x25~x28 temporary calculation register * Output register: * A00~A44: x0~x24 The latest State Matrix * Function/Macro Call: None * Implementation part: * for x in 0…4: for y in 0…4: A[x, y] ^= not A[x, y+1] and A[x, y+2] * if x,y = 0,0: A[x, y] = A[x, y] xor iotas[i] */ .macro CHIOTA offset // for y in 0…4: A[0, y] ^= not A[0, y+1] and A[0, y+2] bic T0, A02, A01 bic T1, A01, A00 bic T2, A00, A04 bic T3, A03, A02 eor A00, A00, T0 eor A01, A01, T3 bic T0, A04, A03 eor A02, A02, T0 eor A03, A03, T2 eor A04, A04, T1 adrp x25, g_roundConstant add x25, x25, :lo12:g_roundConstant // x25 === T0 ldr T3, [x25, \offset*8] eor A00, A00, T3 // iota: A[0, 0] = A[0, 0] xor iotas[i] // for y in 0…4: A[1, y] ^= not A[1, y+1] and A[1, y+2] bic T0, A12, A11 bic T1, A11, A10 bic T2, A10, A14 bic T3, A13, A12 eor A10, A10, T0 eor A11, A11, T3 bic T0, A14, A13 eor A12, A12, T0 eor A13, A13, T2 eor A14, A14, T1 // for y in 0…4: A[2, y] ^= not A[2, y+1] and A[2, y+2] bic T0, A22, A21 bic T1, A21, A20 bic T2, A20, A24 bic T3, A23, A22 eor A20, A20, T0 eor A21, A21, T3 bic T0, A24, A23 eor A22, A22, T0 eor A23, A23, T2 eor A24, A24, T1 // for y in 0…4: A[3, y] ^= not A[3, y+1] and A[3, y+2] bic T0, A32, A31 bic T1, A31, A30 bic T2, A30, A34 bic T3, A33, A32 eor A30, A30, T0 eor A31, A31, T3 bic T0, A34, A33 eor A32, A32, T0 eor A33, A33, T2 eor A34, A34, T1 // for y in 0…4: A[4, y] ^= not A[4, y+1] and A[4, y+2] bic T0, A42, A41 bic T1, A41, A40 bic T2, A40, A44 bic T3, A43, A42 eor A40, A40, T0 eor A41, A41, T3 bic T0, A44, A43 eor A42, A42, T0 eor A43, A43, T2 eor A44, A44, T1 .endm /** * Macro Description: Round of phase mapping * Input register: * A00~A44: x0~x24 State Matrix * T0~T4: x25~x29 temporary calculation register * Modify the register: * A00~A44: x0~x24 State Matrix * T0~T4: x25~x29 temporary calculation register * Output register: * A00~A44: The latest State Matrix * Function/Macro Call: THETA RHOPi CHIOTA */ .macro ROUND offset THETA RHOPi CHIOTA \offset .endm .macro Keccak /* The length of the digest after extrusion is greater than r. Then, the digest is mapped and then extruded. */ stp x25, x26, [sp, #-32]! stp x27, x28, [sp, #8*2] /* Load states: x0~x24 */ ldp A00, A01, [x25] ldp A02, A03, [x25, #16] ldp A04, A10, [x25, #16*2] ldp A11, A12, [x25, #16*3] ldp A13, A14, [x25, #16*4] ldp A20, A21, [x25, #16*5] ldp A22, A23, [x25, #16*6] ldp A24, A30, [x25, #16*7] ldp A31, A32, [x25, #16*8] ldp A33, A34, [x25, #16*9] ldp A40, A41, [x25, #16*10] ldp A42, A43, [x25, #16*11] ldr A44, [x25, #16*12] /* Mapping */ ROUND #0 ROUND #1 ROUND #2 ROUND #3 ROUND #4 ROUND #5 ROUND #6 ROUND #7 ROUND #8 ROUND #9 ROUND #10 ROUND #11 ROUND #12 ROUND #13 ROUND #14 ROUND #15 ROUND #16 ROUND #17 ROUND #18 ROUND #19 ROUND #20 ROUND #21 ROUND #22 ROUND #23 ldp x25, x26, [sp], #8*2 ldp x27, x28, [sp], #8*2 /* Store states: x0~x24 */ stp A00, A01, [x25] stp A02, A03, [x25, #8*2] stp A04, A10, [x25, #8*4] stp A11, A12, [x25, #8*6] stp A13, A14, [x25, #8*8] stp A20, A21, [x25, #8*10] stp A22, A23, [x25, #8*12] stp A24, A30, [x25, #8*14] stp A31, A32, [x25, #8*16] stp A33, A34, [x25, #8*18] stp A40, A41, [x25, #8*20] stp A42, A43, [x25, #8*22] str A44, [x25, #8*24] mov x0, x25 mov x3, x28 .endm .section .rodata .balign 64 .type g_roundConstant, %object g_roundConstant: .quad 0x0000000000000001 .quad 0x0000000000008082 .quad 0x800000000000808a .quad 0x8000000080008000 .quad 0x000000000000808b .quad 0x0000000080000001 .quad 0x8000000080008081 .quad 0x8000000000008009 .quad 0x000000000000008a .quad 0x0000000000000088 .quad 0x0000000080008009 .quad 0x000000008000000a .quad 0x000000008000808b .quad 0x800000000000008b .quad 0x8000000000008089 .quad 0x8000000000008003 .quad 0x8000000000008002 .quad 0x8000000000000080 .quad 0x000000000000800a .quad 0x800000008000000a .quad 0x8000000080008081 .quad 0x8000000000008080 .quad 0x0000000080000001 .quad 0x8000000080008008 .size g_roundConstant, .-g_roundConstant /** * Function description: Perform shA3 absorption according to the input message. * Function prototype: const uint8_t *SHA3_Absorb(uint8_t *state, const uint8_t *in, uinT32_t inLen, uinT32_t r); * Input register: * x0: Pointer to the address of the State Matrix * x1: Pointer to the input data address * x2: Message length * x3: Different shA3 algorithms are executed based on the shA3 parameter r. * Register usage: A00~A44: x0~x24 State Matrix * T0~T4: x25~x29 temporary calculation register * Output register: x0 Returns the address of the message for which shA3 calculation is not performed. * Function/Macro Call: ROUND */ .text .balign 16 .global SHA3_Absorb .type SHA3_Absorb, %function SHA3_Absorb: AARCH64_PACIASP /* 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] stp x0, x1, [sp, #-32]! stp x2, x3, [sp, #8*2] mov x25, x0 mov x26, x1 mov x27, x2 mov x28, x3 cmp x2, x3 blo .Labsorb_end /* Load states: x0~x24 */ ldp A00, A01, [x25] ldp A02, A03, [x25, #16] ldp A04, A10, [x25, #16*2] ldp A11, A12, [x25, #16*3] ldp A13, A14, [x25, #16*4] ldp A20, A21, [x25, #16*5] ldp A22, A23, [x25, #16*6] ldp A24, A30, [x25, #16*7] ldp A31, A32, [x25, #16*8] ldp A33, A34, [x25, #16*9] ldp A40, A41, [x25, #16*10] ldp A42, A43, [x25, #16*11] ldr A44, [x25, #16*12] .Labsorb: /* Absorb from inputs according to r */ ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A00, A00, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A01, A01, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A02, A02, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A03, A03, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A04, A04, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A10, A10, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A11, A11, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A12, A12, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A13, A13, x25 cmp x28, #72 // SHA3_512: 72=8*9: (x0~x8) beq .Labsorb_mapping ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A14, A14, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A20, A20, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A21, A21, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A22, A22, x25 cmp x28, #104 // SHA3_384: 104=8*13: (x0~x12) beq .Labsorb_mapping ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A23, A23, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A24, A24, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A30, A30, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A31, A31, x25 cmp x28, #136 // SHA3_256: 136=8*17: (x0~x16) beq .Labsorb_mapping ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A32, A32, x25 cmp x28, #144 // SHA3_224: 144=8*18: (x0~x17) beq .Labsorb_mapping ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A33, A33, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A34, A34, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A40, A40, x25 cmp x28, #168 // SHAKE128: 168=8*21: (0~20) beq .Labsorb_mapping ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A41, A41, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A42, A42, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A43, A43, x25 ldr x25, [x26], #8 #ifdef HITLS_BIG_ENDIAN rev x25, x25 #endif eor A44, A44, x25 .Labsorb_mapping: /* Updating the Input Data Pointer and Length */ sub x27, x27, x28 stp x26, x27, [sp, #8] /* Mapping */ ROUND #0 ROUND #1 ROUND #2 ROUND #3 ROUND #4 ROUND #5 ROUND #6 ROUND #7 ROUND #8 ROUND #9 ROUND #10 ROUND #11 ROUND #12 ROUND #13 ROUND #14 ROUND #15 ROUND #16 ROUND #17 ROUND #18 ROUND #19 ROUND #20 ROUND #21 ROUND #22 ROUND #23 ldp x26, x27, [sp, #8] ldr x28, [sp, #24] cmp x27, x28 bhs .Labsorb /* Store states: x0~x24 */ ldr x25, [sp] stp A00, A01, [x25] stp A02, A03, [x25, #8*2] stp A04, A10, [x25, #8*4] stp A11, A12, [x25, #8*6] stp A13, A14, [x25, #8*8] stp A20, A21, [x25, #8*10] stp A22, A23, [x25, #8*12] stp A24, A30, [x25, #8*14] stp A31, A32, [x25, #8*16] stp A33, A34, [x25, #8*18] stp A40, A41, [x25, #8*20] stp A42, A43, [x25, #8*22] str A44, [x25, #8*24] .Labsorb_end: /* Return the remaining message address. */ mov x0, x26 /* End popping */ add sp, sp, #32 // skip x0~x3 ldp x29, x30, [sp], #8*2 ldp x19, x20, [sp], #8*2 ldp x21, x22, [sp], #8*2 ldp x23, x24, [sp], #8*2 ldp x25, x26, [sp], #8*2 ldp x27, x28, [sp], #8*2 AARCH64_AUTIASP ret .size SHA3_Absorb, .-SHA3_Absorb .balign 16 /** * Function description: Perform SHA3 squeezing to obtain the digest message. * Function prototyp: void SHA3_Squeeze(uint8_t *state, uint8_t *out, uinT32_t outLen, uinT32_t r, bool isNeedKeccak) * Input register: * x0: Pointer to the address of the State Matrix * x1: Pointer to the output summary address * x2: digist Length * x3: Different SHA3 algorithms are executed based on the SHA3 parameter r. * Register usage: A00~A44: x0~x24 State Matrix * T0~T4: x25~x29 temporary calculation register * Output register: x1: Pointer to the output summary address * Function/Macro Call: ROUND */ .global SHA3_Squeeze .type SHA3_Squeeze, %function SHA3_Squeeze: AARCH64_PACIASP /* 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] mov x25, x0 mov x26, x1 mov x27, x2 mov x28, x3 mov x30, x4 /* Cyclically squeezing message summaries from the State Matrix */ .Loop_squeeze: ldr x4, [x0], #8 cmp x27, #8 blo .Lsqueeze_tail // If the remaining length is less than 8 bytes, perform single-byte extrusion. #ifdef HITLS_BIG_ENDIAN rev x4, x4 #endif str x4, [x26], #8 // Perform 8-byte squeeze subs x27, x27, #8 beq .Lsqueeze_done subs x3, x3, #8 bhi .Loop_squeeze Keccak b .Loop_squeeze /* Single Byte Squeezing */ .Lsqueeze_tail: strb w4, [x26], #1 lsr x4, x4, #8 subs x27, x27, #1 beq .Lsqueeze_done strb w4, [x26], #1 lsr x4, x4, #8 subs x27, x27, #1 beq .Lsqueeze_done strb w4, [x26], #1 lsr x4, x4, #8 subs x27, x27, #1 beq .Lsqueeze_done strb w4, [x26], #1 lsr x4, x4, #8 subs x27, x27, #1 beq .Lsqueeze_done strb w4, [x26], #1 lsr x4, x4, #8 subs x27, x27, #1 beq .Lsqueeze_done strb w4, [x26], #1 lsr x4, x4, #8 subs x27, x27, #1 beq .Lsqueeze_done strb w4, [x26], #1 .Lsqueeze_done: /* End popping */ cmp x30, 0 beq .Lsqueeze_end Keccak .Lsqueeze_end: ldp x29, x30, [sp], #8*2 ldp x19, x20, [sp], #8*2 ldp x21, x22, [sp], #8*2 ldp x23, x24, [sp], #8*2 ldp x25, x26, [sp], #8*2 ldp x27, x28, [sp], #8*2 eor x0, x0, x0 AARCH64_AUTIASP ret .size SHA3_Squeeze, .-SHA3_Squeeze #endif
2301_79861745/bench_create
crypto/sha3/src/asm/sha3_armv8.S
Unix Assembly
unknown
18,154
/* * 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_SHA3 #include <stdlib.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "crypt_sha3.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ static void SHA3_Keccak(uint8_t *state); static void Round(const uint64_t *a, uint64_t *e, uint32_t i); #define ROL64(a, offset) ((((uint64_t)(a)) << (offset)) ^ (((uint64_t)(a)) >> (64 - (offset)))) // the rotation offsets, see https://keccak.team/keccak_specs_summary.html static const uint8_t g_rotationOffset[5][5] = { { 0, 1, 62, 28, 27 }, { 36, 44, 6, 55, 20 }, { 3, 10, 43, 25, 39 }, { 41, 45, 15, 21, 8 }, { 18, 2, 61, 56, 14 } }; // the round constants, see https://keccak.team/keccak_specs_summary.html static const uint64_t g_roundConstant[24] = { (uint64_t)0x0000000000000001, (uint64_t)0x0000000000008082, (uint64_t)0x800000000000808a, (uint64_t)0x8000000080008000, (uint64_t)0x000000000000808b, (uint64_t)0x0000000080000001, (uint64_t)0x8000000080008081, (uint64_t)0x8000000000008009, (uint64_t)0x000000000000008a, (uint64_t)0x0000000000000088, (uint64_t)0x0000000080008009, (uint64_t)0x000000008000000a, (uint64_t)0x000000008000808b, (uint64_t)0x800000000000008b, (uint64_t)0x8000000000008089, (uint64_t)0x8000000000008003, (uint64_t)0x8000000000008002, (uint64_t)0x8000000000000080, (uint64_t)0x000000000000800a, (uint64_t)0x800000008000000a, (uint64_t)0x8000000080008081, (uint64_t)0x8000000000008080, (uint64_t)0x0000000080000001, (uint64_t)0x8000000080008008 }; // Absorbing function of the sponge structure const uint8_t *SHA3_Absorb(uint8_t *state, const uint8_t *in, uint32_t inLen, uint32_t r) { const uint8_t *data = (const uint8_t *)in; uint64_t *pSt = (uint64_t *)state; uint32_t dataLen = inLen; // Divide one block data into some uint64_t data (8 bytes) and perform XOR with the status variable. uint32_t blockInWord = r / 8; while (dataLen >= r) { for (uint32_t i = 0; i < blockInWord; i++) { uint64_t oneLane = GET_UINT64_LE(data, i << 3); pSt[i] ^= oneLane; } // Process one block data. SHA3_Keccak(state); dataLen -= r; data += r; } return (const uint8_t *)data; } // Squeezing function of the sponge structure void SHA3_Squeeze(uint8_t *state, uint8_t *out, uint32_t outLen, uint32_t r, bool isNeedKeccak) { uint32_t dataLen = outLen; uint32_t copyLen; // Divide one block data into some uint64_t data (8 bytes) and perform XOR with the status variable. uint32_t blockInWord = r / 8; uint64_t *oneLane = (uint64_t *)state; uint8_t outTmp[168]; while (dataLen > 0) { copyLen = (dataLen > r) ? r : dataLen; for (uint32_t i = 0; i < blockInWord; i++) { PUT_UINT64_LE(oneLane[i], outTmp, i << 3); // left shift by 3 bits equals i * 8. } (void)memcpy_s(out + outLen - dataLen, dataLen, outTmp, copyLen); dataLen -= copyLen; if (dataLen > 0 || isNeedKeccak) { SHA3_Keccak(state); } } } static void SHA3_Keccak(uint8_t *state) { uint8_t stTmp[200] = {0}; // See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf // SHA3 depends on keccak-p[1600,24] for 24 rounds of cyclic calculation. for (uint32_t i = 0; i < 24; i += 2) { Round((uint64_t *)state, (uint64_t *)stTmp, i); Round((uint64_t *)stTmp, (uint64_t *)state, i + 1); } } // see section 2.4 Algorithm 1 in https://keccak.team/files/Keccak-implementation-3.2.pdf static void Round(const uint64_t *a, uint64_t *e, uint32_t i) { uint64_t c[5], d[5]; // The corresponding formula for calculating the indexes of array A and array E is (5 * x) + y, // the value of x is in [0, 4] and the value of y is [0, 4]. // The row coordinates of the array index correspond to y in the algorithm principle, // and the column coordinates correspond to x in the algorithm principle, for example, A[1, 1] = A[5 * 1 + 1] = A[6] // THETA operation c[0] = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]; c[1] = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]; c[2] = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]; c[3] = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]; c[4] = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]; d[0] = ROL64(c[1], 1) ^ c[4]; d[1] = ROL64(c[2], 1) ^ c[0]; d[2] = ROL64(c[3], 1) ^ c[1]; d[3] = ROL64(c[4], 1) ^ c[2]; d[4] = ROL64(c[0], 1) ^ c[3]; // THETA RHP Pi operation c[0] = a[0] ^ d[0]; c[1] = ROL64(a[6] ^ d[1], g_rotationOffset[1][1]); c[2] = ROL64(a[12] ^ d[2], g_rotationOffset[2][2]); c[3] = ROL64(a[18] ^ d[3], g_rotationOffset[3][3]); c[4] = ROL64(a[24] ^ d[4], g_rotationOffset[4][4]); // CHI IOTA operation, e[0] = c[0] ^ (~c[1] & c[2]) ^ g_roundConstant[i]; // CHI operation e[1] = c[1] ^ (~c[2] & c[3]); e[2] = c[2] ^ (~c[3] & c[4]); e[3] = c[3] ^ (~c[4] & c[0]); e[4] = c[4] ^ (~c[0] & c[1]); // THETA RHP Pi operation c[0] = ROL64(a[3] ^ d[3], g_rotationOffset[0][3]); c[1] = ROL64(a[9] ^ d[4], g_rotationOffset[1][4]); c[2] = ROL64(a[10] ^ d[0], g_rotationOffset[2][0]); c[3] = ROL64(a[16] ^ d[1], g_rotationOffset[3][1]); c[4] = ROL64(a[22] ^ d[2], g_rotationOffset[4][2]); // CHI operation e[5] = c[0] ^ (~c[1] & c[2]); e[6] = c[1] ^ (~c[2] & c[3]); e[7] = c[2] ^ (~c[3] & c[4]); e[8] = c[3] ^ (~c[4] & c[0]); e[9] = c[4] ^ (~c[0] & c[1]); // THETA RHP Pi operation c[0] = ROL64(a[1] ^ d[1], g_rotationOffset[0][1]); c[1] = ROL64(a[7] ^ d[2], g_rotationOffset[1][2]); c[2] = ROL64(a[13] ^ d[3], g_rotationOffset[2][3]); c[3] = ROL64(a[19] ^ d[4], g_rotationOffset[3][4]); c[4] = ROL64(a[20] ^ d[0], g_rotationOffset[4][0]); // CHI operation e[10] = c[0] ^ (~c[1] & c[2]); e[11] = c[1] ^ (~c[2] & c[3]); e[12] = c[2] ^ (~c[3] & c[4]); e[13] = c[3] ^ (~c[4] & c[0]); e[14] = c[4] ^ (~c[0] & c[1]); // THETA RHP Pi operation c[0] = ROL64(a[4] ^ d[4], g_rotationOffset[0][4]); c[1] = ROL64(a[5] ^ d[0], g_rotationOffset[1][0]); c[2] = ROL64(a[11] ^ d[1], g_rotationOffset[2][1]); c[3] = ROL64(a[17] ^ d[2], g_rotationOffset[3][2]); c[4] = ROL64(a[23] ^ d[3], g_rotationOffset[4][3]); // CHI operation e[15] = c[0] ^ (~c[1] & c[2]); e[16] = c[1] ^ (~c[2] & c[3]); e[17] = c[2] ^ (~c[3] & c[4]); e[18] = c[3] ^ (~c[4] & c[0]); e[19] = c[4] ^ (~c[0] & c[1]); // THETA RHP Pi operation c[0] = ROL64(a[2] ^ d[2], g_rotationOffset[0][2]); c[1] = ROL64(a[8] ^ d[3], g_rotationOffset[1][3]); c[2] = ROL64(a[14] ^ d[4], g_rotationOffset[2][4]); c[3] = ROL64(a[15] ^ d[0], g_rotationOffset[3][0]); c[4] = ROL64(a[21] ^ d[1], g_rotationOffset[4][1]); // CHI operation e[20] = c[0] ^ (~c[1] & c[2]); e[21] = c[1] ^ (~c[2] & c[3]); e[22] = c[2] ^ (~c[3] & c[4]); e[23] = c[3] ^ (~c[4] & c[0]); e[24] = c[4] ^ (~c[0] & c[1]); } #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA3
2301_79861745/bench_create
crypto/sha3/src/noasm_sha3.c
C
unknown
7,696
/* * 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_SHA3 #include <stdlib.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "sha3_core.h" #include "crypt_sha3.h" #include "crypt_types.h" struct CryptSha3Ctx { uint8_t state[200]; // State array, 200bytes is 1600bits uint32_t num; // Data length in the remaining buffer. uint32_t blockSize; // For example, BlockSize(sha3-224) = ((1600 - 224 * 2) / 8) bytes uint32_t mdSize; // sha3-224 corresponds to 28 bytes, sha3-256: 32 bytes, sha3-384: 48 bytes, sha3-512: 64 bytes // Non-integer multiple data cache. 168 = (1600 - 128 * 2) / 8, that is maximum block size used by shake_* uint8_t buf[168]; uint8_t padChr; // char for padding, sha3_* use 0x06 and shake_* use 0x1f bool squeeze; }; CRYPT_SHA3_Ctx *CRYPT_SHA3_NewCtx(void) { return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA3_Ctx)); } CRYPT_SHA3_Ctx *CRYPT_SHA3_NewCtxEx(void *libCtx, int32_t algId) { (void)libCtx; (void)algId; return BSL_SAL_Calloc(1, sizeof(CRYPT_SHA3_Ctx)); } void CRYPT_SHA3_FreeCtx(CRYPT_SHA3_Ctx *ctx) { BSL_SAL_ClearFree(ctx, sizeof(CRYPT_SHA3_Ctx)); } static int32_t CRYPT_SHA3_Init(CRYPT_SHA3_Ctx *ctx, uint32_t mdSize, uint32_t blockSize, uint8_t padChr) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memset_s(ctx, sizeof(CRYPT_SHA3_Ctx), 0, sizeof(CRYPT_SHA3_Ctx)); ctx->mdSize = mdSize; ctx->padChr = padChr; ctx->blockSize = blockSize; return CRYPT_SUCCESS; } int32_t CRYPT_SHA3_Update(CRYPT_SHA3_Ctx *ctx, const uint8_t *in, uint32_t len) { if (ctx == NULL || (in == NULL && len != 0)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len == 0) { return CRYPT_SUCCESS; } const uint8_t *data = in; uint32_t left = ctx->blockSize - ctx->num; uint32_t dataLen = len; if (ctx->num != 0) { if (dataLen < left) { (void)memcpy_s(ctx->buf + ctx->num, left, data, dataLen); ctx->num += dataLen; return CRYPT_SUCCESS; } // When the external input data is greater than the remaining space of the block, // copy the data of the remaining space. (void)memcpy_s(ctx->buf + ctx->num, left, data, left); (void)SHA3_Absorb(ctx->state, ctx->buf, ctx->blockSize, ctx->blockSize); dataLen -= left; data += left; ctx->num = 0; } data = SHA3_Absorb(ctx->state, data, dataLen, ctx->blockSize); dataLen = len - (data - in); if (dataLen != 0) { // copy the remaining data to the cache array (void)memcpy_s(ctx->buf, ctx->blockSize, data, dataLen); ctx->num = dataLen; } return CRYPT_SUCCESS; } int32_t CRYPT_SHA3_Final(CRYPT_SHA3_Ctx *ctx, uint8_t *out, uint32_t *len) { if (ctx == NULL || out == NULL || len == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (*len < ctx->mdSize) { BSL_ERR_PUSH_ERROR(CRYPT_SHA3_OUT_BUFF_LEN_NOT_ENOUGH); return CRYPT_SHA3_OUT_BUFF_LEN_NOT_ENOUGH; } uint32_t left = ctx->blockSize - ctx->num; uint32_t outLen = (ctx->mdSize == 0) ? *len : ctx->mdSize; (void)memset_s(ctx->buf + ctx->num, left, 0, left); ctx->buf[ctx->num] = ctx->padChr; ctx->buf[ctx->blockSize - 1] |= 0x80; // 0x80 is the last 1 of pad 10*1 mode (void)SHA3_Absorb(ctx->state, ctx->buf, ctx->blockSize, ctx->blockSize); SHA3_Squeeze(ctx->state, out, outLen, ctx->blockSize, false); *len = outLen; return CRYPT_SUCCESS; } int32_t CRYPT_SHA3_Squeeze(CRYPT_SHA3_Ctx *ctx, uint8_t *out, uint32_t len) { if (ctx == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (!ctx->squeeze) { uint32_t left = ctx->blockSize - ctx->num; (void)memset_s(ctx->buf + ctx->num, left, 0, left); ctx->buf[ctx->num] = ctx->padChr; ctx->buf[ctx->blockSize - 1] |= 0x80; // 0x80 is the last 1 of pad 10*1 mode (void)SHA3_Absorb(ctx->state, ctx->buf, ctx->blockSize, ctx->blockSize); ctx->num = 0; ctx->squeeze = true; } uint32_t tmpLen = len; uint8_t *outTmp = out; if (ctx->num != 0) { uint32_t outLen = (ctx->num > len) ? len : ctx->num; (void)memcpy_s(outTmp, outLen, ctx->buf + ctx->blockSize - ctx->num, outLen); ctx->num -= outLen; tmpLen -= outLen; outTmp += outLen; } if (tmpLen > ctx->blockSize) { uint32_t comLen = tmpLen / ctx->blockSize * ctx->blockSize; SHA3_Squeeze(ctx->state, outTmp, comLen, ctx->blockSize, true); outTmp += comLen; tmpLen -= comLen; } if (tmpLen != 0) { SHA3_Squeeze(ctx->state, ctx->buf, ctx->blockSize, ctx->blockSize, true); (void)memcpy_s(outTmp, tmpLen, ctx->buf, tmpLen); ctx->num = ctx->blockSize - tmpLen; } return CRYPT_SUCCESS; } int32_t CRYPT_SHA3_Deinit(CRYPT_SHA3_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_CleanseData(ctx, sizeof(CRYPT_SHA3_Ctx)); return CRYPT_SUCCESS; } int32_t CRYPT_SHA3_CopyCtx(CRYPT_SHA3_Ctx *dst, const CRYPT_SHA3_Ctx *src) { if (dst == NULL || src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memcpy_s(dst, sizeof(CRYPT_SHA3_Ctx), src, sizeof(CRYPT_SHA3_Ctx)); return CRYPT_SUCCESS; } CRYPT_SHA3_Ctx *CRYPT_SHA3_DupCtx(const CRYPT_SHA3_Ctx *src) { if (src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_SHA3_Ctx *newCtx = CRYPT_SHA3_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memcpy_s(newCtx, sizeof(CRYPT_SHA3_Ctx), src, sizeof(CRYPT_SHA3_Ctx)); return newCtx; } int32_t CRYPT_SHA3_224_Init(CRYPT_SHA3_224_Ctx *ctx, BSL_Param *param) { (void) param; // 0x06 is SHA3 padding character, see https://keccak.team/keccak_specs_summary.html return CRYPT_SHA3_Init(ctx, CRYPT_SHA3_224_DIGESTSIZE, CRYPT_SHA3_224_BLOCKSIZE, 0x06); } int32_t CRYPT_SHA3_256_Init(CRYPT_SHA3_256_Ctx *ctx, BSL_Param *param) { (void) param; // 0x06 is SHA3 padding character, see https://keccak.team/keccak_specs_summary.html return CRYPT_SHA3_Init(ctx, CRYPT_SHA3_256_DIGESTSIZE, CRYPT_SHA3_256_BLOCKSIZE, 0x06); } int32_t CRYPT_SHA3_384_Init(CRYPT_SHA3_384_Ctx *ctx, BSL_Param *param) { (void) param; // 0x06 is SHA3 padding character, see https://keccak.team/keccak_specs_summary.html return CRYPT_SHA3_Init(ctx, CRYPT_SHA3_384_DIGESTSIZE, CRYPT_SHA3_384_BLOCKSIZE, 0x06); } int32_t CRYPT_SHA3_512_Init(CRYPT_SHA3_512_Ctx *ctx, BSL_Param *param) { (void) param; // 0x06 is SHA3 padding character, see https://keccak.team/keccak_specs_summary.html return CRYPT_SHA3_Init(ctx, CRYPT_SHA3_512_DIGESTSIZE, CRYPT_SHA3_512_BLOCKSIZE, 0x06); } int32_t CRYPT_SHAKE128_Init(CRYPT_SHAKE128_Ctx *ctx, BSL_Param *param) { (void) param; // 0x1f is SHA3 padding character, see https://keccak.team/keccak_specs_summary.html return CRYPT_SHA3_Init(ctx, 0, CRYPT_SHAKE128_BLOCKSIZE, 0x1F); } int32_t CRYPT_SHAKE256_Init(CRYPT_SHAKE256_Ctx *ctx, BSL_Param *param) { (void) param; // 0x1f is SHA3 padding character, see https://keccak.team/keccak_specs_summary.html return CRYPT_SHA3_Init(ctx, 0, CRYPT_SHAKE256_BLOCKSIZE, 0x1F); } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SHA3_224_GetParam(CRYPT_SHA3_224_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA3_224_DIGESTSIZE, CRYPT_SHA3_224_BLOCKSIZE, param); } int32_t CRYPT_SHA3_256_GetParam(CRYPT_SHA3_256_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA3_256_DIGESTSIZE, CRYPT_SHA3_256_BLOCKSIZE, param); } int32_t CRYPT_SHA3_384_GetParam(CRYPT_SHA3_384_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA3_384_DIGESTSIZE, CRYPT_SHA3_384_BLOCKSIZE, param); } int32_t CRYPT_SHA3_512_GetParam(CRYPT_SHA3_512_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHA3_512_DIGESTSIZE, CRYPT_SHA3_512_BLOCKSIZE, param); } int32_t CRYPT_SHAKE128_GetParam(CRYPT_SHAKE128_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHAKE128_DIGESTSIZE, CRYPT_SHAKE128_BLOCKSIZE, param); } int32_t CRYPT_SHAKE256_GetParam(CRYPT_SHAKE256_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SHAKE128_DIGESTSIZE, CRYPT_SHAKE256_BLOCKSIZE, param); } #endif // HITLS_CRYPTO_PROVIDER #endif // HITLS_CRYPTO_SHA3
2301_79861745/bench_create
crypto/sha3/src/sha3.c
C
unknown
9,465
/* * 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 SHA3_CORE_H #define SHA3_CORE_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SHA3 #include <stdint.h> #ifdef __cplusplus extern "C" { #endif const uint8_t *SHA3_Absorb(uint8_t *state, const uint8_t *in, uint32_t inLen, uint32_t r); void SHA3_Squeeze(uint8_t *state, uint8_t *out, uint32_t outLen, uint32_t r, bool isNeedKeccak); #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SHA3 #endif // SHA3_CORE_H
2301_79861745/bench_create
crypto/sha3/src/sha3_core.h
C
unknown
966
/* * 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_SIPHASH_H #define CRYPT_SIPHASH_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SIPHASH #include <stdint.h> #include <stdlib.h> #include "crypt_local_types.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define SIPHASH_KEY_SIZE 16 // 128 bit #define SIPHASH_WORD_SIZE 8 // 64 bit #define DEFAULT_COMPRESSION_ROUND 2 #define DEFAULT_FINALIZATION_ROUND 4 // The siphash has only two output lengths: 8-byte and 16-byte. #define SIPHASH_MIN_DIGEST_SIZE 8 #define SIPHASH_MAX_DIGEST_SIZE 16 typedef struct SIPHASH_Ctx CRYPT_SIPHASH_Ctx; #define CRYPT_SIPHASH_SetParam NULL /** * @brief Create a new siphash context. * @param id [IN] MAC algorithm id * @retval Pointer to the created siphash context. */ CRYPT_SIPHASH_Ctx *CRYPT_SIPHASH_NewCtx(CRYPT_MAC_AlgId id); /** * @brief Create a new siphash context with external library context. * @param libCtx [in] External library context * @param id [in] siphash algorithm ID * @return Pointer to the siphash context */ CRYPT_SIPHASH_Ctx *CRYPT_SIPHASH_NewCtxEx(void *libCtx, CRYPT_MAC_AlgId id); /** * @brief Initialize the siphash context by using the key passed by the user. * @param ctx [IN] siphash context * @param key [IN] MAC symmetric key * @param len [IN] Key length. The length of the siphash key is fixed to 128 bits. * @param param [IN] param, reserved. * @retval #CRYPT_SUCCESS Succeeded. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. * #CRYPT_INVALID_ARG invalid input parameter. For example, the input key length is not 128 bits. */ int32_t CRYPT_SIPHASH_Init(CRYPT_SIPHASH_Ctx *ctx, const uint8_t *key, uint32_t keyLen, void *param); /** * @brief siphash update, supporting streaming update * @param ctx [IN] siphash context * @param in [IN] Point to the data buffer for MAC calculation. * @param inlen [IN] Length of the data to be calculated * @retval #CRYPT_SUCCESS Succeeded. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. */ int32_t CRYPT_SIPHASH_Update(CRYPT_SIPHASH_Ctx *ctx, const uint8_t *in, uint32_t inlen); /** * @brief siphash closeout calculation * @param ctx [IN] siphash context * @param out [OUT] Output data. Sufficient memory must be allocated to store CMAC results and cannot be null. * @param outlen [IN/OUT] Output data length * @retval #CRYPT_SUCCESS Succeeded. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. * @retval #CRYPT_SIPHASH_OUT_BUFF_LEN_NOT_ENOUGH The output buffer is insufficient. */ int32_t CRYPT_SIPHASH_Final(CRYPT_SIPHASH_Ctx *ctx, uint8_t *out, uint32_t *outlen); /** * @brief Re-initialize the siphash context * @param ctx [IN] siphash context * @retval #CRYPT_SUCCESS Succeeded. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. */ int32_t CRYPT_SIPHASH_Reinit(CRYPT_SIPHASH_Ctx *ctx); /** * @brief siphash de-initialization * @param ctx [IN] siphash context * @retval #CRYPT_SUCCESS Succeeded. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. */ int32_t CRYPT_SIPHASH_Deinit(CRYPT_SIPHASH_Ctx *ctx); /** * @brief siphash control * @param ctx [IN] siphash context * @param opt [IN] control option * @param val [IN]/[OUT] Control value * @param len [IN] control value length * @retval #CRYPT_SUCCESS Succeeded. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_SIPHASH_Ctrl(CRYPT_SIPHASH_Ctx *ctx, uint32_t opt, void *val, uint32_t len); /** * @brief siphash free context * @param ctx [IN] siphash context */ void CRYPT_SIPHASH_FreeCtx(CRYPT_SIPHASH_Ctx *ctx); #ifdef __cplusplus } #endif #endif /* HITLS_CRYPTO_SIPHASH */ #endif
2301_79861745/bench_create
crypto/siphash/include/crypt_siphash.h
C
unknown
4,397
/* * 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_SIPHASH #include <stdlib.h> #include <stdio.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_siphash.h" #include "eal_mac_local.h" #define SIPHASH_HALF_KEY_SIZE 8 #define BYTE_TO_BITS_RATIO 8 #define LROT_UINT64(num, bits) (uint64_t)(((num) << (bits)) | ((num) >> (64 - (bits)))) #define SIPHASH_SIX_OCTET_TO_BITS ((BYTE_TO_BITS_RATIO) * 6) #define SIPHASH_FIVE_OCTET_TO_BITS ((BYTE_TO_BITS_RATIO) * 5) #define SIPHASH_FOUR_OCTET_TO_BITS ((BYTE_TO_BITS_RATIO) * 4) #define SIPHASH_THREE_OCTET_TO_BITS ((BYTE_TO_BITS_RATIO) * 3) #define SIPHASH_TWO_OCTET_TO_BITS ((BYTE_TO_BITS_RATIO) * 2) #define SIPHASH_ONE_OCTET_TO_BITS ((BYTE_TO_BITS_RATIO) * 1) struct SIPHASH_Ctx { uint64_t state0; uint64_t state1; uint64_t state2; uint64_t state3; uint16_t compressionRounds; uint16_t finalizationRounds; uint32_t hashSize; uint32_t accInLen; uint32_t offset; uint8_t remainder[SIPHASH_WORD_SIZE]; }; static inline uint64_t BytesToUint64LittleEndian(const uint8_t key[SIPHASH_WORD_SIZE]) { uint64_t ret = 0ULL; for (uint32_t i = 0; i < SIPHASH_WORD_SIZE; i++) { ret = ret | (((uint64_t)key[i]) << (i * BYTE_TO_BITS_RATIO)); } return ret; } static void Uint64ToBytesLittleEndian(uint64_t src, uint8_t out[SIPHASH_WORD_SIZE]) { for (uint32_t i = 0; i < SIPHASH_WORD_SIZE; i++) { out[i] = (uint8_t)(src >> (i * BYTE_TO_BITS_RATIO)); } } static uint64_t DealLastWord(uint64_t lastWord, const uint8_t *bytes, size_t bytesLen) { uint64_t tmpLastWord = lastWord; switch (bytesLen) { case 7: // Do not need to run break from the case, fall through the switch-case. // The remaining 7 bytes are to be processed and shift to left by 6 bytes. tmpLastWord |= ((uint64_t)bytes[6]) << SIPHASH_SIX_OCTET_TO_BITS; /* fall-through */ case 6: tmpLastWord |= ((uint64_t)bytes[5]) << SIPHASH_FIVE_OCTET_TO_BITS; /* fall-through */ case 5: tmpLastWord |= ((uint64_t)bytes[4]) << SIPHASH_FOUR_OCTET_TO_BITS; /* fall-through */ case 4: tmpLastWord |= ((uint64_t)bytes[3]) << SIPHASH_THREE_OCTET_TO_BITS; /* fall-through */ case 3: tmpLastWord |= ((uint64_t)bytes[2]) << SIPHASH_TWO_OCTET_TO_BITS; /* fall-through */ case 2: tmpLastWord |= ((uint64_t)bytes[1]) << SIPHASH_ONE_OCTET_TO_BITS; /* fall-through */ case 1: tmpLastWord |= ((uint64_t)bytes[0]); /* fall-through */ default: // case 0 break; } return tmpLastWord; } static void SiproundOperation(uint64_t *state0, uint64_t *state1, uint64_t *state2, uint64_t *state3) { (*state0) += (*state1); (*state1) = LROT_UINT64(*state1, 13); (*state1) ^= (*state0); (*state0) = LROT_UINT64(*state0, 32); (*state2) += (*state3); (*state3) = LROT_UINT64(*state3, 16); (*state3) ^= (*state2); (*state0) += (*state3); (*state3) = LROT_UINT64(*state3, 21); (*state3) ^= (*state0); (*state2) += (*state1); (*state1) = LROT_UINT64(*state1, 17); (*state1) ^= (*state2); (*state2) = LROT_UINT64(*state2, 32); } static void UpdateInternalState(uint64_t curWord, CRYPT_SIPHASH_Ctx *ctx, uint16_t rounds) { (ctx->state3) ^= curWord; for (uint16_t j = 0; j < rounds; j++) { SiproundOperation(&(ctx->state0), &(ctx->state1), &(ctx->state2), &(ctx->state3)); } (ctx->state0) ^= curWord; } static int32_t CRYPT_SIPHASH_GetMacLen(const CRYPT_SIPHASH_Ctx *ctx, void *val, uint32_t len) { if (val == NULL || len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } *(uint32_t *)val = ctx->hashSize; return CRYPT_SUCCESS; } CRYPT_SIPHASH_Ctx *CRYPT_SIPHASH_NewCtx(CRYPT_MAC_AlgId id) { EAL_MacDepMethod macMethod = {0}; int32_t ret = EAL_MacFindDepMethod(id, NULL, NULL, &macMethod, NULL, false); if (ret != CRYPT_SUCCESS) { return NULL; } CRYPT_SIPHASH_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(CRYPT_SIPHASH_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } const EAL_SiphashMethod *method = macMethod.method.sip; uint16_t cRounds = method->compressionRounds; uint16_t dRounds = method->finalizationRounds; // fill compressionRounds and finalizationRounds ctx->compressionRounds = ((cRounds == 0) ? DEFAULT_COMPRESSION_ROUND : cRounds); ctx->finalizationRounds = ((dRounds == 0) ? DEFAULT_FINALIZATION_ROUND : dRounds); ctx->hashSize = method->hashSize; ctx->accInLen = 0; ctx->offset = 0; return ctx; } CRYPT_SIPHASH_Ctx *CRYPT_SIPHASH_NewCtxEx(void *libCtx, CRYPT_MAC_AlgId id) { (void)libCtx; return CRYPT_SIPHASH_NewCtx(id); } int32_t CRYPT_SIPHASH_Init(CRYPT_SIPHASH_Ctx *ctx, const uint8_t *key, uint32_t keyLen, void *param) { (void)param; if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } // invalid key size size_t hashSize = ctx->hashSize; if (keyLen != SIPHASH_KEY_SIZE) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } // invalid digest size if (!((hashSize == SIPHASH_MIN_DIGEST_SIZE) || (hashSize == SIPHASH_MAX_DIGEST_SIZE))) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } // split key byte array to two parts: k0, k1 uint64_t numKey0 = BytesToUint64LittleEndian(key); uint64_t numKey1 = BytesToUint64LittleEndian(key + SIPHASH_HALF_KEY_SIZE); // fill internal state ctx->state0 = numKey0 ^ 0x736f6d6570736575ULL; ctx->state1 = numKey1 ^ 0x646f72616e646f6dULL; ctx->state2 = numKey0 ^ 0x6c7967656e657261ULL; ctx->state3 = numKey1 ^ 0x7465646279746573ULL; if (hashSize == SIPHASH_MAX_DIGEST_SIZE) { ctx->state1 ^= 0xee; } return CRYPT_SUCCESS; } int32_t CRYPT_SIPHASH_Update(CRYPT_SIPHASH_Ctx *ctx, const uint8_t *in, uint32_t inlen) { if (ctx == NULL || (in == NULL && inlen != 0)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (inlen > UINT32_MAX - ctx->accInLen) { BSL_ERR_PUSH_ERROR(CRYPT_SIPHASH_INPUT_OVERFLOW); return CRYPT_SIPHASH_INPUT_OVERFLOW; } const uint8_t *tmpIn = in; uint32_t tmpInlen = inlen; ctx->accInLen += tmpInlen; uint64_t curWord = 0; if (ctx->offset != 0) { size_t emptySpaceLen = SIPHASH_WORD_SIZE - ctx->offset; if (tmpInlen < emptySpaceLen) { (void)memcpy_s(ctx->remainder + (ctx->offset), tmpInlen, tmpIn, tmpInlen); // update offset, emptySpaceLen shrinks ctx->offset += tmpInlen; return CRYPT_SUCCESS; } // fill ctx->remainder[SIPHASH_WORD_SIZE - ctx->offset] to ctx->remainder[SIPHASH_WORD_SIZE - 1] using in (void)memcpy_s(ctx->remainder + (ctx->offset), emptySpaceLen, tmpIn, emptySpaceLen); // update inlen tmpInlen -= (uint32_t)emptySpaceLen; // consume emptySpaceLen data of in tmpIn += emptySpaceLen; curWord = BytesToUint64LittleEndian(ctx->remainder); (void)UpdateInternalState(curWord, ctx, ctx->compressionRounds); } size_t remainLen = tmpInlen & (SIPHASH_WORD_SIZE - 1); // inlen mod 8 const uint8_t *lastWordPos = tmpIn + tmpInlen - remainLen; while (tmpIn != lastWordPos) { curWord = BytesToUint64LittleEndian(tmpIn); (void)UpdateInternalState(curWord, ctx, ctx->compressionRounds); tmpIn += SIPHASH_WORD_SIZE; } if (remainLen > 0) { (void)memcpy_s(ctx->remainder, remainLen, lastWordPos, remainLen); } ctx->offset = (uint32_t)remainLen; return CRYPT_SUCCESS; } int32_t CRYPT_SIPHASH_Final(CRYPT_SIPHASH_Ctx *ctx, uint8_t *out, uint32_t *outlen) { if (ctx == NULL || out == NULL || outlen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (*outlen < ctx->hashSize) { BSL_ERR_PUSH_ERROR(CRYPT_SIPHASH_OUT_BUFF_LEN_NOT_ENOUGH); return CRYPT_SIPHASH_OUT_BUFF_LEN_NOT_ENOUGH; } *outlen = ctx->hashSize; uint64_t mLen = ctx->accInLen; // message length uint64_t tmpLastWord = mLen << 56; // put (mLen mod 256) at high address size_t remainLen = ctx->offset; uint64_t curWord = DealLastWord(tmpLastWord, ctx->remainder, remainLen); (void)UpdateInternalState(curWord, ctx, ctx->compressionRounds); if (*outlen == SIPHASH_MIN_DIGEST_SIZE) { (ctx->state2) ^= 0xff; } else { (ctx->state2) ^= 0xee; } for (uint16_t j = 0; j < ctx->finalizationRounds; j++) { (void)SiproundOperation(&(ctx->state0), &(ctx->state1), &(ctx->state2), &(ctx->state3)); } uint64_t state = (ctx->state0) ^ (ctx->state1) ^ (ctx->state2) ^ (ctx->state3); (void)Uint64ToBytesLittleEndian(state, out); if (*outlen == SIPHASH_MIN_DIGEST_SIZE) { return CRYPT_SUCCESS; } (ctx->state1) ^= 0xdd; for (uint16_t j = 0; j < ctx->finalizationRounds; j++) { (void)SiproundOperation(&(ctx->state0), &(ctx->state1), &(ctx->state2), &(ctx->state3)); } state = (ctx->state0) ^ (ctx->state1) ^ (ctx->state2) ^ (ctx->state3); (void)Uint64ToBytesLittleEndian(state, out + SIPHASH_WORD_SIZE); return CRYPT_SUCCESS; } int32_t CRYPT_SIPHASH_Reinit(CRYPT_SIPHASH_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ctx->state0 = 0; ctx->state1 = 0; ctx->state2 = 0; ctx->state3 = 0; ctx->accInLen = 0; ctx->offset = 0; (void)memset_s(ctx->remainder, SIPHASH_WORD_SIZE, 0, SIPHASH_WORD_SIZE); return CRYPT_SUCCESS; } int32_t CRYPT_SIPHASH_Deinit(CRYPT_SIPHASH_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SUCCESS; } int32_t CRYPT_SIPHASH_Ctrl(CRYPT_SIPHASH_Ctx *ctx, uint32_t opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (opt) { case CRYPT_CTRL_GET_MACLEN: return CRYPT_SIPHASH_GetMacLen(ctx, val, len); default: break; } BSL_ERR_PUSH_ERROR(CRYPT_SIPHASH_ERR_UNSUPPORTED_CTRL_OPTION); return CRYPT_SIPHASH_ERR_UNSUPPORTED_CTRL_OPTION; } void CRYPT_SIPHASH_FreeCtx(CRYPT_SIPHASH_Ctx *ctx) { if (ctx != NULL) { BSL_SAL_Free(ctx); } } #endif /* HITLS_CRYPTO_SIPHASH */
2301_79861745/bench_create
crypto/siphash/src/siphash.c
C
unknown
11,401
/* * 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_SLH_DSA_H #define CRYPT_SLH_DSA_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SLH_DSA #include <stdint.h> #include "bsl_params.h" typedef struct SlhDsaCtx CryptSlhDsaCtx; typedef struct HashFuncs SlhDsaHashFuncs; typedef union Adrs SlhDsaAdrs; /** * @brief Create a new SLH-DSA context * * @return CryptSlhDsaCtx* Pointer to the new SLH-DSA context */ CryptSlhDsaCtx *CRYPT_SLH_DSA_NewCtx(void); /** * @brief Create a new SLH-DSA context * * @param libCtx Pointer to the library context * * @return CryptSlhDsaCtx* Pointer to the new SLH-DSA context */ CryptSlhDsaCtx *CRYPT_SLH_DSA_NewCtxEx(void *libCtx); /** * @brief Free a SLH-DSA context * * @param ctx Pointer to the SLH-DSA context */ void CRYPT_SLH_DSA_FreeCtx(CryptSlhDsaCtx *ctx); /** * @brief Generate a SLH-DSA key pair * * @param ctx Pointer to the SLH-DSA context */ int32_t CRYPT_SLH_DSA_Gen(CryptSlhDsaCtx *ctx); /** * @brief Sign data using SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param algId Algorithm ID * @param data Pointer to the data to sign * @param dataLen Length of the data * @param sign Pointer to the signature * @param signLen Length of the signature */ int32_t CRYPT_SLH_DSA_Sign(CryptSlhDsaCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); /** * @brief Verify data using SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param algId Algorithm ID * @param data Pointer to the data to verify * @param dataLen Length of the data * @param sign Pointer to the signature * @param signLen Length of the signature */ int32_t CRYPT_SLH_DSA_Verify(const CryptSlhDsaCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); /** * @brief Control function for SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param opt Option * @param val Value * @param len Length of the value */ int32_t CRYPT_SLH_DSA_Ctrl(CryptSlhDsaCtx *ctx, int32_t opt, void *val, uint32_t len); /** * @brief Get the public key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param pub Pointer to the public key */ int32_t CRYPT_SLH_DSA_GetPubKey(const CryptSlhDsaCtx *ctx, CRYPT_SlhDsaPub *pub); /** * @brief Get the private key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param prv Pointer to the private key */ int32_t CRYPT_SLH_DSA_GetPrvKey(const CryptSlhDsaCtx *ctx, CRYPT_SlhDsaPrv *prv); /** * @brief Set the public key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param pub Pointer to the public key */ int32_t CRYPT_SLH_DSA_SetPubKey(CryptSlhDsaCtx *ctx, const CRYPT_SlhDsaPub *pub); /** * @brief Set the private key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param prv Pointer to the private key */ int32_t CRYPT_SLH_DSA_SetPrvKey(CryptSlhDsaCtx *ctx, const CRYPT_SlhDsaPrv *prv); #ifdef HITLS_BSL_PARAMS /** * @brief Get the public key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param para Pointer to the public key */ int32_t CRYPT_SLH_DSA_GetPubKeyEx(const CryptSlhDsaCtx *ctx, BSL_Param *para); /** * @brief Get the private key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param para Pointer to the private key */ int32_t CRYPT_SLH_DSA_GetPrvKeyEx(const CryptSlhDsaCtx *ctx, BSL_Param *para); /** * @brief Set the public key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param para Pointer to the public key */ int32_t CRYPT_SLH_DSA_SetPubKeyEx(CryptSlhDsaCtx *ctx, const BSL_Param *para); /** * @brief Set the private key of SLH-DSA * * @param ctx Pointer to the SLH-DSA context * @param para Pointer to the private key */ int32_t CRYPT_SLH_DSA_SetPrvKeyEx(CryptSlhDsaCtx *ctx, const BSL_Param *para); #endif #ifdef HITLS_CRYPTO_SLH_DSA_CHECK /** * @brief Check the key pair of SLH-DSA * * @param checkType Check type * @param pkey1 Pointer to the first SLH-DSA context * @param pkey2 Pointer to the second SLH-DSA context * * @retval CRYPT_SUCCESS check success. * Others. For details, see error code in errno. */ int32_t CRYPT_SLH_DSA_Check(uint32_t checkType, const CryptSlhDsaCtx *pkey1, const CryptSlhDsaCtx *pkey2); #endif // HITLS_CRYPTO_SLH_DSA_CHECK #endif // HITLS_CRYPTO_SLH_DSA #endif // CRYPT_SLH_DSA_H
2301_79861745/bench_create
crypto/slh_dsa/include/crypt_slh_dsa.h
C
unknown
4,946
/* * 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_SLH_DSA #include <stddef.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "bsl_obj_internal.h" #include "bsl_asn1_internal.h" #include "crypt_errno.h" #include "crypt_algid.h" #include "crypt_util_rand.h" #include "eal_md_local.h" #include "crypt_slh_dsa.h" #include "slh_dsa_local.h" #include "slh_dsa_hash.h" #include "slh_dsa_fors.h" #include "slh_dsa_xmss.h" #include "slh_dsa_hypertree.h" #define MAX_DIGEST_SIZE 64 #define BYTE_BITS 8 #define SLH_DSA_PREFIX_LEN 2 #define ASN1_HEADER_LEN 2 #define SPLIT_CEIL(a, b) (((a) + (b) - 1) / (b)) #define SPLIT_BYTES(a) SPLIT_CEIL(a, BYTE_BITS) #define NUM_OF_CRYPT_SLH_DSA_ALGID 12 typedef struct { BSL_Param *pubSeed; BSL_Param *pubRoot; } SlhDsaPubKeyParam; typedef struct { BSL_Param *prvSeed; BSL_Param *prvPrf; BSL_Param *pubSeed; BSL_Param *pubRoot; } SlhDsaPrvKeyParam; // reference to FIPS-205, table 2 static uint32_t g_slhDsaN[NUM_OF_CRYPT_SLH_DSA_ALGID] = {16, 16, 16, 16, 24, 24, 24, 24, 32, 32, 32, 32}; static uint32_t g_slhDsaH[NUM_OF_CRYPT_SLH_DSA_ALGID] = {63, 63, 66, 66, 63, 63, 66, 66, 64, 64, 68, 68}; static uint32_t g_slhDsaD[NUM_OF_CRYPT_SLH_DSA_ALGID] = {7, 7, 22, 22, 7, 7, 22, 22, 8, 8, 17, 17}; static uint32_t g_slhDsaHp[NUM_OF_CRYPT_SLH_DSA_ALGID] = {9, 9, 3, 3, 9, 9, 3, 3, 8, 8, 4, 4}; // xmss height static uint32_t g_slhDsaA[NUM_OF_CRYPT_SLH_DSA_ALGID] = {12, 12, 6, 6, 14, 14, 8, 8, 14, 14, 9, 9}; static uint32_t g_slhDsaK[NUM_OF_CRYPT_SLH_DSA_ALGID] = {14, 14, 33, 33, 17, 17, 33, 33, 22, 22, 35, 35}; static uint32_t g_slhDsaM[NUM_OF_CRYPT_SLH_DSA_ALGID] = {30, 30, 34, 34, 39, 39, 42, 42, 47, 47, 49, 49}; static uint32_t g_slhDsaPkBytes[NUM_OF_CRYPT_SLH_DSA_ALGID] = {32, 32, 32, 32, 48, 48, 48, 48, 64, 64, 64, 64}; static uint32_t g_slhDsaSigBytes[NUM_OF_CRYPT_SLH_DSA_ALGID] = {7856, 7856, 17088, 17088, 16224, 16224, 35664, 35664, 29792, 29792, 49856, 49856}; static uint8_t g_secCategory[] = {1, 1, 1, 1, 3, 3, 3, 3, 5, 5, 5, 5}; // "UC" means uncompressed static void UCAdrsSetLayerAddr(SlhDsaAdrs *adrs, uint32_t layer) { PUT_UINT32_BE(layer, adrs->uc.layerAddr, 0); } static void UCAdrsSetTreeAddr(SlhDsaAdrs *adrs, uint64_t tree) { // Write 8-byte tree address starting from offset 4 in 12-byte treeAddr field PUT_UINT64_BE(tree, adrs->uc.treeAddr, 4); } static void UCAdrsSetType(SlhDsaAdrs *adrs, AdrsType type) { PUT_UINT32_BE(type, adrs->uc.type, 0); (void)memset_s(adrs->uc.padding, sizeof(adrs->uc.padding), 0, sizeof(adrs->uc.padding)); } static void UCAdrsSetKeyPairAddr(SlhDsaAdrs *adrs, uint32_t keyPair) { PUT_UINT32_BE(keyPair, adrs->uc.padding, 0); } static void UCAdrsSetChainAddr(SlhDsaAdrs *adrs, uint32_t chain) { PUT_UINT32_BE(chain, adrs->uc.padding, 4); // chain address is 4 bytes, start from 4-th byte } static void UCAdrsSetTreeHeight(SlhDsaAdrs *adrs, uint32_t height) { PUT_UINT32_BE(height, adrs->uc.padding, 4); // tree height is 4 bytes, start from 4-th byte } static void UCAdrsSetHashAddr(SlhDsaAdrs *adrs, uint32_t hash) { PUT_UINT32_BE(hash, adrs->uc.padding, 8); // hash address is 4 bytes, start from 8-th byte } static void UCAdrsSetTreeIndex(SlhDsaAdrs *adrs, uint32_t index) { PUT_UINT32_BE(index, adrs->uc.padding, 8); // tree index is 4 bytes, start from 8-th byte } static uint32_t UCAdrsGetTreeHeight(const SlhDsaAdrs *adrs) { return GET_UINT32_BE(adrs->uc.padding, 0); } static uint32_t UCAdrsGetTreeIndex(const SlhDsaAdrs *adrs) { return GET_UINT32_BE(adrs->uc.padding, 8); // tree index is 4 bytes, start from 8-th byte } static void UCAdrsCopyKeyPairAddr(SlhDsaAdrs *adrs, const SlhDsaAdrs *adrs2) { (void)memcpy_s(adrs->uc.padding, sizeof(adrs->uc.padding), adrs2->uc.padding, 4); // key pair address is 4 bytes, start from 4-th byte } static uint32_t UCAdrsGetAdrsLen(void) { return SLH_DSA_ADRS_LEN; } // "C" means compressed static void CAdrsSetLayerAddr(SlhDsaAdrs *adrs, uint32_t layer) { adrs->c.layerAddr = (uint8_t)layer; } static void CAdrsSetTreeAddr(SlhDsaAdrs *adrs, uint64_t tree) { // Write 8-byte tree address starting from offset 0 in 8-byte treeAddr field PUT_UINT64_BE(tree, adrs->c.treeAddr, 0); } static void CAdrsSetType(SlhDsaAdrs *adrs, AdrsType type) { adrs->c.type = type; (void)memset_s(adrs->c.padding, sizeof(adrs->c.padding), 0, sizeof(adrs->c.padding)); } static void CAdrsSetKeyPairAddr(SlhDsaAdrs *adrs, uint32_t keyPair) { PUT_UINT32_BE(keyPair, adrs->c.padding, 0); } static void CAdrsSetChainAddr(SlhDsaAdrs *adrs, uint32_t chain) { PUT_UINT32_BE(chain, adrs->c.padding, 4); // chain address is 4 bytes, start from 4-th byte } static void CAdrsSetTreeHeight(SlhDsaAdrs *adrs, uint32_t height) { PUT_UINT32_BE(height, adrs->c.padding, 4); // tree height is 4 bytes, start from 4-th byte } static void CAdrsSetHashAddr(SlhDsaAdrs *adrs, uint32_t hash) { PUT_UINT32_BE(hash, adrs->c.padding, 8); // hash address is 4 bytes, start from 8-th byte } static void CAdrsSetTreeIndex(SlhDsaAdrs *adrs, uint32_t index) { PUT_UINT32_BE(index, adrs->c.padding, 8); // tree index is 4 bytes, start from 8-th byte } static uint32_t CAdrsGetTreeHeight(const SlhDsaAdrs *adrs) { return GET_UINT32_BE(adrs->c.padding, 0); // tree height is 4 bytes, start from 0-th byte } static uint32_t CAdrsGetTreeIndex(const SlhDsaAdrs *adrs) { return GET_UINT32_BE(adrs->c.padding, 8); // tree index is 4 bytes, start from 8-th byte } static void CAdrsCopyKeyPairAddr(SlhDsaAdrs *adrs, const SlhDsaAdrs *adrs2) { (void)memcpy_s(adrs->c.padding, sizeof(adrs->c.padding), adrs2->c.padding, 4); // key pair address is 4 bytes, start from 4-th byte } static uint32_t CAdrsGetAdrsLen(void) { return SLH_DSA_ADRS_COMPRESSED_LEN; } static AdrsOps g_adrsOps[2] = {{ .setLayerAddr = UCAdrsSetLayerAddr, .setTreeAddr = UCAdrsSetTreeAddr, .setType = UCAdrsSetType, .setKeyPairAddr = UCAdrsSetKeyPairAddr, .setChainAddr = UCAdrsSetChainAddr, .setTreeHeight = UCAdrsSetTreeHeight, .setHashAddr = UCAdrsSetHashAddr, .setTreeIndex = UCAdrsSetTreeIndex, .getTreeHeight = UCAdrsGetTreeHeight, .getTreeIndex = UCAdrsGetTreeIndex, .copyKeyPairAddr = UCAdrsCopyKeyPairAddr, .getAdrsLen = UCAdrsGetAdrsLen, }, { .setLayerAddr = CAdrsSetLayerAddr, .setTreeAddr = CAdrsSetTreeAddr, .setType = CAdrsSetType, .setKeyPairAddr = CAdrsSetKeyPairAddr, .setChainAddr = CAdrsSetChainAddr, .setTreeHeight = CAdrsSetTreeHeight, .setHashAddr = CAdrsSetHashAddr, .setTreeIndex = CAdrsSetTreeIndex, .getTreeHeight = CAdrsGetTreeHeight, .getTreeIndex = CAdrsGetTreeIndex, .copyKeyPairAddr = CAdrsCopyKeyPairAddr, .getAdrsLen = CAdrsGetAdrsLen, }}; void BaseB(const uint8_t *x, uint32_t xLen, uint32_t b, uint32_t *out, uint32_t outLen) { uint32_t bit = 0; uint32_t o = 0; uint32_t xi = 0; for (uint32_t i = 0; i < outLen; i++) { while (bit < b && xi < xLen) { o = (o << BYTE_BITS) + x[xi]; bit += 8; xi++; } bit -= b; out[i] = o >> bit; // keep the remaining bits o &= (1 << bit) - 1; } } // ToInt(b[0:l]) mod 2^m static uint64_t ToIntMod(const uint8_t *b, uint32_t l, uint32_t m) { uint64_t ret = 0; for (uint32_t i = 0; i < l; i++) { ret = (ret << BYTE_BITS) + b[i]; } return ret & (~(uint64_t)0 >> (64 - m)); // mod 2^m is same to ~(uint64_t)0 >> (64 - m) } CryptSlhDsaCtx *CRYPT_SLH_DSA_NewCtx(void) { CryptSlhDsaCtx *ctx = (CryptSlhDsaCtx *)BSL_SAL_Calloc(sizeof(CryptSlhDsaCtx), 1); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } ctx->para.algId = 0; ctx->isPrehash = false; ctx->isDeterministic = false; return ctx; } CryptSlhDsaCtx *CRYPT_SLH_DSA_NewCtxEx(void *libCtx) { CryptSlhDsaCtx *ctx = CRYPT_SLH_DSA_NewCtx(); if (ctx == NULL) { return NULL; } ctx->libCtx = libCtx; return ctx; } void CRYPT_SLH_DSA_FreeCtx(CryptSlhDsaCtx *ctx) { if (ctx == NULL) { return; } BSL_SAL_Free(ctx->context); BSL_SAL_ClearFree(ctx->addrand, ctx->addrandLen); BSL_SAL_CleanseData(ctx->prvKey.seed, sizeof(ctx->prvKey.seed)); BSL_SAL_CleanseData(ctx->prvKey.prf, sizeof(ctx->prvKey.prf)); BSL_SAL_Free(ctx); } static bool CheckNotSlhDsaAlgId(int32_t algId) { if (algId > CRYPT_SLH_DSA_SHAKE_256F || algId < CRYPT_SLH_DSA_SHA2_128S) { return true; } return false; } int32_t CRYPT_SLH_DSA_Gen(CryptSlhDsaCtx *ctx) { int32_t ret; if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (CheckNotSlhDsaAlgId(ctx->para.algId)) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_ALGID); return CRYPT_SLHDSA_ERR_INVALID_ALGID; } uint32_t n = ctx->para.n; uint32_t d = ctx->para.d; uint32_t hp = ctx->para.hp; ret = CRYPT_RandEx(ctx->libCtx, ctx->prvKey.seed, n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = CRYPT_RandEx(ctx->libCtx, ctx->prvKey.prf, n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = CRYPT_RandEx(ctx->libCtx, ctx->prvKey.pub.seed, n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } SlhDsaAdrs adrs = {0}; ctx->adrsOps.setLayerAddr(&adrs, d - 1); uint8_t node[SLH_DSA_MAX_N] = {0}; ret = XmssNode(node, 0, hp, &adrs, ctx, NULL, 0); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.pub.root, n, node, n); ctx->keyType = SLH_DSA_PRVKEY | SLH_DSA_PUBKEY; return CRYPT_SUCCESS; } static int32_t GetAddRand(CryptSlhDsaCtx *ctx) { if (ctx->addrand != NULL) { // the additional rand is set. return CRYPT_SUCCESS; } if (!ctx->isDeterministic) { ctx->addrand = (uint8_t *)BSL_SAL_Malloc(ctx->para.n); if (ctx->addrand == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret = CRYPT_RandEx(ctx->libCtx, ctx->addrand, ctx->para.n); if (ret != CRYPT_SUCCESS) { return ret; } } else { // FIPS-204, Algorithm 19, line 2. // if is deterministic, use the public key seed as the random number. uint8_t *rand = (uint8_t *)BSL_SAL_Malloc(ctx->para.n); if (rand == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } (void)memcpy_s(rand, ctx->para.n, ctx->prvKey.pub.seed, ctx->para.n); ctx->addrand = rand; } ctx->addrandLen = ctx->para.n; return CRYPT_SUCCESS; } static void GetTreeAndLeafIdx(const uint8_t *digest, const CryptSlhDsaCtx *ctx, uint64_t *treeIdx, uint32_t *leafIdx) { uint32_t a = ctx->para.a; uint32_t k = ctx->para.k; uint32_t h = ctx->para.h; uint32_t d = ctx->para.d; uint32_t mdIdx = SPLIT_BYTES(k * a); uint32_t treeIdxLen = SPLIT_BYTES(h - h / d); uint32_t leafIdxLen = SPLIT_BYTES(h / d); *treeIdx = ToIntMod(digest + mdIdx, treeIdxLen, h - h / d); *leafIdx = (uint32_t)ToIntMod(digest + mdIdx + treeIdxLen, leafIdxLen, h / d); } static int32_t CRYPT_SLH_DSA_SignInternal(CryptSlhDsaCtx *ctx, const uint8_t *msg, uint32_t msgLen, uint8_t *sig, uint32_t *sigLen) { int32_t ret; uint32_t n = ctx->para.n; uint32_t a = ctx->para.a; uint32_t k = ctx->para.k; uint32_t sigBytes = ctx->para.sigBytes; uint32_t mdIdx = SPLIT_BYTES(k * a); uint64_t treeIdx; uint32_t leafIdx; if (*sigLen < sigBytes) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_SIG_LEN); return CRYPT_SLHDSA_ERR_INVALID_SIG_LEN; } SlhDsaAdrs adrs = {0}; uint32_t offset = 0; uint32_t left = *sigLen; ret = GetAddRand(ctx); if (ret != CRYPT_SUCCESS) { return ret; } ret = ctx->hashFuncs.prfmsg(ctx, ctx->addrand, msg, msgLen, sig); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } offset += n; uint8_t digest[SLH_DSA_MAX_M] = {0}; ret = ctx->hashFuncs.hmsg(ctx, sig, msg, msgLen, NULL, digest); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } GetTreeAndLeafIdx(digest, ctx, &treeIdx, &leafIdx); ctx->adrsOps.setTreeAddr(&adrs, treeIdx); ctx->adrsOps.setType(&adrs, FORS_TREE); ctx->adrsOps.setKeyPairAddr(&adrs, leafIdx); ret = ForsSign(digest, mdIdx, &adrs, ctx, sig + offset, &left); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint8_t pk[SLH_DSA_MAX_N] = {0}; ret = ForsPkFromSig(sig + n, left, digest, mdIdx, &adrs, ctx, pk); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } offset += left; left = *sigLen - offset; ret = HypertreeSign(pk, n, treeIdx, leafIdx, ctx, sig + offset, &left); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *sigLen = offset + left; return CRYPT_SUCCESS; } static int32_t CRYPT_SLH_DSA_VerifyInternal(const CryptSlhDsaCtx *ctx, const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen) { int32_t ret; uint32_t n = ctx->para.n; uint32_t a = ctx->para.a; uint32_t k = ctx->para.k; uint32_t sigBytes = ctx->para.sigBytes; uint32_t mdIdx = SPLIT_BYTES(k * a); uint64_t treeIdx; uint32_t leafIdx; if (sigLen != sigBytes) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_SIG_LEN); return CRYPT_SLHDSA_ERR_INVALID_SIG_LEN; } SlhDsaAdrs adrs = {0}; uint32_t offset = 0; uint8_t digest[SLH_DSA_MAX_M] = {0}; ret = ctx->hashFuncs.hmsg(ctx, sig, msg, msgLen, NULL, digest); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } offset += n; GetTreeAndLeafIdx(digest, ctx, &treeIdx, &leafIdx); ctx->adrsOps.setTreeAddr(&adrs, treeIdx); ctx->adrsOps.setType(&adrs, FORS_TREE); ctx->adrsOps.setKeyPairAddr(&adrs, leafIdx); uint8_t pk[SLH_DSA_MAX_N] = {0}; ret = ForsPkFromSig(sig + offset, (1 + a) * k * n, digest, mdIdx, &adrs, ctx, pk); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } offset += (1 + a) * k * n; ret = HypertreeVerify(pk, n, sig + offset, sigLen - offset, treeIdx, leafIdx, ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } static uint32_t GetMdSize(const EAL_MdMethod *hashMethod, int32_t hashId) { if (hashId == CRYPT_MD_SHAKE128) { return 32; // To use SHAKE128, generate a 32-byte digest. } else if (hashId == CRYPT_MD_SHAKE256) { return 64; // To use SHAKE256, generate a 64-byte digest. } return hashMethod->mdSize; } static int32_t MsgEncode(const CryptSlhDsaCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t **mpOut, uint32_t *mpLenOut) { int32_t ret; BslOidString *oid = NULL; uint32_t offset = 0; uint8_t prehash[MAX_DIGEST_SIZE] = {0}; uint32_t prehashLen = sizeof(prehash); uint32_t mpLen = SLH_DSA_PREFIX_LEN + ctx->contextLen; if (ctx->isPrehash) { oid = BSL_OBJ_GetOID((BslCid)algId); if (oid == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_PREHASH_ID_NOT_SUPPORTED); return CRYPT_SLHDSA_ERR_PREHASH_ID_NOT_SUPPORTED; } mpLen += 2 + oid->octetLen; // asn1 header length is 2 prehashLen = GetMdSize(EAL_MdFindDefaultMethod(algId), algId); const CRYPT_ConstData constData = {data, dataLen}; ret = CRYPT_CalcHash(NULL, EAL_MdFindDefaultMethod(algId), &constData, 1, prehash, &prehashLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } mpLen += prehashLen; } else { mpLen += dataLen; } uint8_t *mp = (uint8_t *)BSL_SAL_Malloc(mpLen); if (mp == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } mp[0] = ctx->isPrehash ? 1 : 0; mp[1] = (uint8_t)ctx->contextLen; (void)memcpy_s(mp + SLH_DSA_PREFIX_LEN, mpLen - SLH_DSA_PREFIX_LEN, ctx->context, ctx->contextLen); offset += SLH_DSA_PREFIX_LEN + ctx->contextLen; if (ctx->isPrehash) { // asn1 encoding of hash oid (mp + offset)[0] = BSL_ASN1_TAG_OBJECT_ID; (mp + offset)[1] = (uint8_t)oid->octetLen; offset += 2; // asn1 header length is 2 (void)memcpy_s(mp + offset, mpLen - offset, oid->octs, oid->octetLen); offset += oid->octetLen; (void)memcpy_s(mp + offset, mpLen - offset, prehash, prehashLen); } else { (void)memcpy_s(mp + offset, mpLen - offset, data, dataLen); } *mpOut = mp; *mpLenOut = mpLen; return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_Sign(CryptSlhDsaCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { int32_t ret; uint8_t *mp = NULL; uint32_t mpLen = 0; if (ctx == NULL || data == NULL || dataLen == 0 || sign == NULL || signLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = MsgEncode(ctx, algId, data, dataLen, &mp, &mpLen); if (ret != CRYPT_SUCCESS) { return ret; } ret = CRYPT_SLH_DSA_SignInternal(ctx, mp, mpLen, sign, signLen); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(mp); BSL_ERR_PUSH_ERROR(ret); return ret; } BSL_SAL_Free(mp); return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_Verify(const CryptSlhDsaCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen) { (void)algId; int32_t ret; uint8_t *mp = NULL; uint32_t mpLen = 0; if (ctx == NULL || data == NULL || dataLen == 0 || sign == NULL || signLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = MsgEncode(ctx, algId, data, dataLen, &mp, &mpLen); if (ret != CRYPT_SUCCESS) { return ret; } ret = CRYPT_SLH_DSA_VerifyInternal(ctx, mp, mpLen, sign, signLen); BSL_SAL_Free(mp); return ret; } static int32_t SlhDsaSetAlgId(CryptSlhDsaCtx *ctx, void *val, uint32_t len) { if (val == NULL || len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } CRYPT_PKEY_ParaId algId = *(CRYPT_PKEY_ParaId *)val; if (CheckNotSlhDsaAlgId(algId)) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_ALGID); return CRYPT_SLHDSA_ERR_INVALID_ALGID; } ctx->para.algId = algId; int32_t index = algId - CRYPT_SLH_DSA_SHA2_128S; ctx->para.n = g_slhDsaN[index]; ctx->para.h = g_slhDsaH[index]; ctx->para.d = g_slhDsaD[index]; ctx->para.hp = g_slhDsaHp[index]; ctx->para.a = g_slhDsaA[index]; ctx->para.k = g_slhDsaK[index]; ctx->para.m = g_slhDsaM[index]; ctx->para.pkBytes = g_slhDsaPkBytes[index]; ctx->para.sigBytes = g_slhDsaSigBytes[index]; ctx->para.secCategory = g_secCategory[index]; SlhDsaInitHashFuncs(ctx); if (ctx->para.isCompressed) { ctx->adrsOps = g_adrsOps[1]; } else { ctx->adrsOps = g_adrsOps[0]; } return CRYPT_SUCCESS; } static int32_t SetContextInfo(CryptSlhDsaCtx *ctx, void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (len > 255) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_CONTEXT_LEN_OVERFLOW); return CRYPT_SLHDSA_ERR_CONTEXT_LEN_OVERFLOW; } ctx->contextLen = len; BSL_SAL_Free(ctx->context); ctx->context = (uint8_t *)BSL_SAL_Malloc(len); if (ctx->context == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } (void)memcpy_s(ctx->context, len, val, len); return CRYPT_SUCCESS; } static int32_t SetAddrand(CryptSlhDsaCtx *ctx, void *val, uint32_t len) { if (val == NULL || len != ctx->para.n) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } BSL_SAL_FREE(ctx->addrand); uint8_t *rand = (uint8_t *)BSL_SAL_Malloc(len); if (rand == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } (void)memcpy_s(rand, len, val, len); ctx->addrand = rand; ctx->addrandLen = len; return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_Ctrl(CryptSlhDsaCtx *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) { case CRYPT_CTRL_SET_PARA_BY_ID: return SlhDsaSetAlgId(ctx, val, len); case CRYPT_CTRL_SET_PREHASH_FLAG: if (val == NULL || len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->isPrehash = (*(int32_t *)val != 0); return CRYPT_SUCCESS; case CRYPT_CTRL_SET_CTX_INFO: return SetContextInfo(ctx, val, len); case CRYPT_CTRL_GET_SLH_DSA_KEY_LEN: if (val == NULL || len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(uint32_t *)val = ctx->para.n; return CRYPT_SUCCESS; case CRYPT_CTRL_SET_DETERMINISTIC_FLAG: if (val == NULL || len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->isDeterministic = (*(int32_t *)val != 0); return CRYPT_SUCCESS; case CRYPT_CTRL_SET_SLH_DSA_ADDRAND: return SetAddrand(ctx, val, len); case CRYPT_CTRL_CLEAN_PUB_KEY: BSL_SAL_CleanseData(ctx->prvKey.pub.seed, sizeof(ctx->prvKey.pub.seed)); BSL_SAL_CleanseData(ctx->prvKey.pub.root, sizeof(ctx->prvKey.pub.root)); return CRYPT_SUCCESS; default: BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } } static int32_t PubKeyCheck(const CryptSlhDsaCtx *ctx, const CRYPT_SlhDsaPub *pub) { if (ctx == NULL || pub == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pub->seed == NULL || pub->root == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pub->len != ctx->para.n) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_KEYLEN); return CRYPT_SLHDSA_ERR_INVALID_KEYLEN; } return CRYPT_SUCCESS; } static int32_t PrvKeyCheck(const CryptSlhDsaCtx *ctx, const CRYPT_SlhDsaPrv *prv) { if (ctx == NULL || prv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prv->prf == NULL || prv->seed == NULL || prv->pub.root == NULL || prv->pub.seed == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prv->pub.len != ctx->para.n) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_KEYLEN); return CRYPT_SLHDSA_ERR_INVALID_KEYLEN; } return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_GetPubKey(const CryptSlhDsaCtx *ctx, CRYPT_SlhDsaPub *pub) { int32_t ret = PubKeyCheck(ctx, pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } pub->len = ctx->para.n; (void)memcpy_s(pub->seed, pub->len, ctx->prvKey.pub.seed, ctx->para.n); (void)memcpy_s(pub->root, pub->len, ctx->prvKey.pub.root, ctx->para.n); return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_GetPrvKey(const CryptSlhDsaCtx *ctx, CRYPT_SlhDsaPrv *prv) { int32_t ret = PrvKeyCheck(ctx, prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } prv->pub.len = ctx->para.n; (void)memcpy_s(prv->seed, prv->pub.len, ctx->prvKey.seed, ctx->para.n); (void)memcpy_s(prv->prf, prv->pub.len, ctx->prvKey.prf, ctx->para.n); (void)memcpy_s(prv->pub.seed, prv->pub.len, ctx->prvKey.pub.seed, ctx->para.n); (void)memcpy_s(prv->pub.root, prv->pub.len, ctx->prvKey.pub.root, ctx->para.n); return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_SetPubKey(CryptSlhDsaCtx *ctx, const CRYPT_SlhDsaPub *pub) { int32_t ret = PubKeyCheck(ctx, pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.pub.seed, ctx->para.n, pub->seed, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.root, ctx->para.n, pub->root, ctx->para.n); return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_SetPrvKey(CryptSlhDsaCtx *ctx, const CRYPT_SlhDsaPrv *prv) { int32_t ret = PrvKeyCheck(ctx, prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.seed, sizeof(ctx->prvKey.seed), prv->seed, ctx->para.n); (void)memcpy_s(ctx->prvKey.prf, sizeof(ctx->prvKey.prf), prv->prf, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.seed, sizeof(ctx->prvKey.pub.seed), prv->pub.seed, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.root, sizeof(ctx->prvKey.pub.root), prv->pub.root, ctx->para.n); return CRYPT_SUCCESS; } #ifdef HITLS_BSL_PARAMS static int32_t PubKeyParamCheck(const CryptSlhDsaCtx *ctx, BSL_Param *para, SlhDsaPubKeyParam *pub) { if (ctx == NULL || para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } pub->pubSeed = BSL_PARAM_FindParam(para, CRYPT_PARAM_SLH_DSA_PUB_SEED); pub->pubRoot = BSL_PARAM_FindParam(para, CRYPT_PARAM_SLH_DSA_PUB_ROOT); if (pub->pubSeed == NULL || pub->pubSeed->value == NULL || pub->pubRoot == NULL || pub->pubRoot->value == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pub->pubSeed->valueLen != ctx->para.n || pub->pubRoot->valueLen != ctx->para.n) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_KEYLEN); return CRYPT_SLHDSA_ERR_INVALID_KEYLEN; } return CRYPT_SUCCESS; } static int32_t PrvKeyParamCheck(const CryptSlhDsaCtx *ctx, BSL_Param *para, SlhDsaPrvKeyParam *prv) { if (ctx == NULL || para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } prv->prvSeed = BSL_PARAM_FindParam(para, CRYPT_PARAM_SLH_DSA_PRV_SEED); prv->prvPrf = BSL_PARAM_FindParam(para, CRYPT_PARAM_SLH_DSA_PRV_PRF); prv->pubSeed = BSL_PARAM_FindParam(para, CRYPT_PARAM_SLH_DSA_PUB_SEED); prv->pubRoot = BSL_PARAM_FindParam(para, CRYPT_PARAM_SLH_DSA_PUB_ROOT); if (prv->prvSeed == NULL || prv->prvSeed->value == NULL || prv->prvPrf == NULL || prv->prvPrf->value == NULL || prv->pubSeed == NULL || prv->pubSeed->value == NULL || prv->pubRoot == NULL || prv->pubRoot->value == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prv->prvSeed->valueLen != ctx->para.n || prv->prvPrf->valueLen != ctx->para.n || prv->pubSeed->valueLen != ctx->para.n || prv->pubRoot->valueLen != ctx->para.n) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_KEYLEN); return CRYPT_SLHDSA_ERR_INVALID_KEYLEN; } return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_GetPubKeyEx(const CryptSlhDsaCtx *ctx, BSL_Param *para) { SlhDsaPubKeyParam pub; int32_t ret = PubKeyParamCheck(ctx, para, &pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if ((ctx->keyType & SLH_DSA_PUBKEY) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_NO_PUBKEY); return CRYPT_SLHDSA_ERR_NO_PUBKEY; } pub.pubSeed->useLen = pub.pubRoot->useLen = ctx->para.n; (void)memcpy_s(pub.pubSeed->value, pub.pubSeed->valueLen, ctx->prvKey.pub.seed, ctx->para.n); (void)memcpy_s(pub.pubRoot->value, pub.pubRoot->valueLen, ctx->prvKey.pub.root, ctx->para.n); return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_GetPrvKeyEx(const CryptSlhDsaCtx *ctx, BSL_Param *para) { SlhDsaPrvKeyParam prv; int32_t ret = PrvKeyParamCheck(ctx, para, &prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if ((ctx->keyType & SLH_DSA_PRVKEY) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_NO_PRVKEY); return CRYPT_SLHDSA_ERR_NO_PRVKEY; } prv.prvSeed->useLen = ctx->para.n; prv.prvPrf->useLen = ctx->para.n; prv.pubSeed->useLen = ctx->para.n; prv.pubRoot->useLen = ctx->para.n; (void)memcpy_s(prv.prvSeed->value, prv.prvSeed->valueLen, ctx->prvKey.seed, ctx->para.n); (void)memcpy_s(prv.prvPrf->value, prv.prvPrf->valueLen, ctx->prvKey.prf, ctx->para.n); (void)memcpy_s(prv.pubSeed->value, prv.pubSeed->valueLen, ctx->prvKey.pub.seed, ctx->para.n); (void)memcpy_s(prv.pubRoot->value, prv.pubRoot->valueLen, ctx->prvKey.pub.root, ctx->para.n); return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_SetPubKeyEx(CryptSlhDsaCtx *ctx, const BSL_Param *para) { SlhDsaPubKeyParam pub; int32_t ret = PubKeyParamCheck(ctx, (BSL_Param *)(uintptr_t)para, &pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.pub.seed, ctx->para.n, pub.pubSeed->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.root, ctx->para.n, pub.pubRoot->value, ctx->para.n); ctx->keyType |= SLH_DSA_PUBKEY; return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_SetPrvKeyEx(CryptSlhDsaCtx *ctx, const BSL_Param *para) { SlhDsaPrvKeyParam prv; int32_t ret = PrvKeyParamCheck(ctx, (BSL_Param *)(uintptr_t)para, &prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.seed, sizeof(ctx->prvKey.seed), prv.prvSeed->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.prf, sizeof(ctx->prvKey.prf), prv.prvPrf->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.seed, sizeof(ctx->prvKey.pub.seed), prv.pubSeed->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.root, sizeof(ctx->prvKey.pub.root), prv.pubRoot->value, ctx->para.n); ctx->keyType |= SLH_DSA_PRVKEY; return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_SLH_DSA_CHECK static int32_t SlhDsaKeyPairCheck(const CryptSlhDsaCtx *pubKey, const CryptSlhDsaCtx *prvKey) { if (pubKey == NULL || prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (CheckNotSlhDsaAlgId(pubKey->para.algId) || CheckNotSlhDsaAlgId(prvKey->para.algId)) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_ALGID); return CRYPT_SLHDSA_ERR_INVALID_ALGID; } if (pubKey->para.algId != prvKey->para.algId) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_PAIRWISE_CHECK_FAIL); return CRYPT_SLHDSA_PAIRWISE_CHECK_FAIL; } if ((pubKey->keyType & SLH_DSA_PUBKEY) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_NO_PUBKEY); return CRYPT_SLHDSA_ERR_NO_PUBKEY; } if ((prvKey->keyType & SLH_DSA_PRVKEY) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_NO_PRVKEY); return CRYPT_SLHDSA_ERR_NO_PRVKEY; } SlhDsaAdrs adrs = {0}; prvKey->adrsOps.setLayerAddr(&adrs, prvKey->para.d - 1); uint8_t node[SLH_DSA_MAX_N] = {0}; int32_t ret = XmssNode(node, 0, prvKey->para.hp, &adrs, prvKey, NULL, 0); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (memcmp(node, pubKey->prvKey.pub.root, SLH_DSA_MAX_N) != 0) { ret = CRYPT_SLHDSA_PAIRWISE_CHECK_FAIL; BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_PAIRWISE_CHECK_FAIL); } return ret; } static int32_t SlhDsaPrvKeyCheck(const CryptSlhDsaCtx *prvKey) { if (prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (CheckNotSlhDsaAlgId(prvKey->para.algId)) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_INVALID_ALGID); return CRYPT_SLHDSA_ERR_INVALID_ALGID; } if ((prvKey->keyType & SLH_DSA_PRVKEY) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SLHDSA_ERR_NO_PRVKEY); return CRYPT_SLHDSA_ERR_NO_PRVKEY; } return CRYPT_SUCCESS; } int32_t CRYPT_SLH_DSA_Check(uint32_t checkType, const CryptSlhDsaCtx *pkey1, const CryptSlhDsaCtx *pkey2) { switch (checkType) { case CRYPT_PKEY_CHECK_KEYPAIR: return SlhDsaKeyPairCheck(pkey1, pkey2); case CRYPT_PKEY_CHECK_PRVKEY: return SlhDsaPrvKeyCheck(pkey1); default: BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } } #endif // HITLS_CRYPTO_SLH_DSA_CHECK #endif // HITLS_CRYPTO_SLH_DSA
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa.c
C
unknown
33,906
/* * 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_SLH_DSA #include <stdint.h> #include <stddef.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "slh_dsa_local.h" #include "slh_dsa_fors.h" int32_t ForsSign(const uint8_t *md, uint32_t mdLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *sig, uint32_t *sigLen) { int32_t ret = CRYPT_SLHDSA_ERR_INVALID_SIG_LEN; uint32_t n = ctx->para.n; uint32_t a = ctx->para.a; uint32_t k = ctx->para.k; if (*sigLen < (a + 1) * n * k) { return CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH; } uint32_t *indices = (uint32_t *)BSL_SAL_Malloc(k * sizeof(uint32_t)); if (indices == NULL) { return BSL_MALLOC_FAIL; } BaseB(md, mdLen, a, indices, k); uint32_t offset = 0; for (uint32_t i = 0; i < k; i++) { ret = ForsGenPrvKey(adrs, indices[i] + (i << a), ctx, sig + offset); if (ret != 0) { goto ERR; } offset += n; for (uint32_t j = 0; j < a; j++) { uint32_t s = (indices[i] >> j) ^ 1; ret = ForsNode((i << (a - j)) + s, j, adrs, ctx, sig + offset); if (ret != 0) { goto ERR; } offset += n; } } *sigLen = offset; ERR: BSL_SAL_Free(indices); return ret; } int32_t ForsPkFromSig(const uint8_t *sig, uint32_t sigLen, const uint8_t *md, uint32_t mdLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *pk) { int32_t ret; uint32_t *indices = NULL; uint8_t *root = NULL; uint32_t n = ctx->para.n; uint32_t a = ctx->para.a; uint32_t k = ctx->para.k; if (sigLen < (a + 1) * n * k) { return CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH; } indices = (uint32_t *)BSL_SAL_Malloc(k * sizeof(uint32_t)); if (indices == NULL) { ret = BSL_MALLOC_FAIL; goto ERR; } root = (uint8_t *)BSL_SAL_Malloc(n * k); if (root == NULL) { ret = BSL_MALLOC_FAIL; goto ERR; } BaseB(md, mdLen, a, indices, k); uint8_t node0[SLH_DSA_MAX_N] = {0}; uint8_t node1[SLH_DSA_MAX_N] = {0}; for (uint32_t i = 0; i < k; i++) { ctx->adrsOps.setTreeHeight(adrs, 0); ctx->adrsOps.setTreeIndex(adrs, (i << a) + indices[i]); ret = ctx->hashFuncs.f(ctx, adrs, sig + (a + 1) * n * i, n, node0); if (ret != 0) { goto ERR; } const uint8_t *auth = sig + (a + 1) * n * i + n; for (uint32_t j = 0; j < a; j++) { uint8_t tmp[SLH_DSA_MAX_N * 2]; ctx->adrsOps.setTreeHeight(adrs, j + 1); if (((indices[i] >> j) & 1) == 1) { ctx->adrsOps.setTreeIndex(adrs, (ctx->adrsOps.getTreeIndex(adrs) - 1) >> 1); (void)memcpy_s(tmp, sizeof(tmp), auth + j * n, n); (void)memcpy_s(tmp + n, sizeof(tmp) - n, node0, n); } else { ctx->adrsOps.setTreeIndex(adrs, ctx->adrsOps.getTreeIndex(adrs) >> 1); (void)memcpy_s(tmp, sizeof(tmp), node0, n); (void)memcpy_s(tmp + n, sizeof(tmp) - n, auth + j * n, n); } ret = ctx->hashFuncs.h(ctx, adrs, tmp, 2 * n, node1); if (ret != 0) { goto ERR; } (void)memcpy_s(node0, sizeof(node0), node1, sizeof(node1)); } (void)memcpy_s(root + i * n, (k - i) * n, node0, n); } SlhDsaAdrs forspkAdrs = *adrs; ctx->adrsOps.setType(&forspkAdrs, FORS_ROOTS); ctx->adrsOps.copyKeyPairAddr(&forspkAdrs, adrs); ret = ctx->hashFuncs.tl(ctx, &forspkAdrs, root, n * k, pk); if (ret != 0) { goto ERR; } ERR: BSL_SAL_Free(indices); BSL_SAL_Free(root); return ret; } int32_t ForsGenPrvKey(const SlhDsaAdrs *adrs, uint32_t idx, const CryptSlhDsaCtx *ctx, uint8_t *sk) { SlhDsaAdrs skadrs = *adrs; ctx->adrsOps.setType(&skadrs, FORS_PRF); ctx->adrsOps.copyKeyPairAddr(&skadrs, adrs); ctx->adrsOps.setTreeIndex(&skadrs, idx); return ctx->hashFuncs.prf(ctx, &skadrs, sk); } int32_t ForsNode(uint32_t idx, uint32_t height, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *node) { int32_t ret; uint32_t n = ctx->para.n; if (height == 0) { uint8_t sk[SLH_DSA_MAX_N] = {0}; ret = ForsGenPrvKey(adrs, idx, ctx, sk); if (ret != 0) { return ret; } ctx->adrsOps.setTreeHeight(adrs, height); ctx->adrsOps.setTreeIndex(adrs, idx); return ctx->hashFuncs.f(ctx, adrs, sk, n, node); } uint8_t dnode[SLH_DSA_MAX_N * 2]; ret = ForsNode(idx * 2, height - 1, adrs, ctx, dnode); if (ret != 0) { return ret; } ret = ForsNode(idx * 2 + 1, height - 1, adrs, ctx, dnode + n); if (ret != 0) { return ret; } ctx->adrsOps.setTreeHeight(adrs, height); ctx->adrsOps.setTreeIndex(adrs, idx); return ctx->hashFuncs.h(ctx, adrs, dnode, 2 * n, node); } #endif // HITLS_CRYPTO_SLH_DSA
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_fors.c
C
unknown
5,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. */ #ifndef CRYPT_SLH_DSA_FORS_H #define CRYPT_SLH_DSA_FORS_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SLH_DSA #include <stdint.h> #include "crypt_slh_dsa.h" #include "slh_dsa_local.h" /** * @brief Sign a message using FORS * * @param md Input message to sign (already hashed to appropriate length) * @param mdLen Length of the message * @param adrs Address structure for domain separation * @param ctx Context * @param sig Output signature * @param sigLen Length of the signature * @return int 0 on success, error code otherwise */ int32_t ForsSign(const uint8_t *md, uint32_t mdLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *sig, uint32_t *sigLen); /** * @brief Verify a FORS signature * * @param sig Input signature * @param sigLen Length of the signature * @param md Input message that was signed * @param mdLen Length of the message * @param adrs Address structure for domain separation * @param ctx Context * @param pk Output public key * @return int 0 if signature is valid, error code otherwise */ int32_t ForsPkFromSig(const uint8_t *sig, uint32_t sigLen, const uint8_t *md, uint32_t mdLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *pk); /** * @brief Generate a FORS private value * * @param adrs Address structure for domain separation * @param idx Tree index * @param ctx Context * @param sk Output private value, the length is n * @return int 0 on success, error code otherwise */ int32_t ForsGenPrvKey(const SlhDsaAdrs *adrs, uint32_t idx, const CryptSlhDsaCtx *ctx, uint8_t *sk); /** * @brief Generate a FORS node * * @param idx Tree index * @param height Height of the tree * @param adrs Address structure for domain separation * @param ctx Context * @param node Output node, the length is n * @return int 0 on success, error code otherwise */ int32_t ForsNode(uint32_t idx, uint32_t height, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *node); #endif // HITLS_CRYPTO_SLH_DSA #endif // CRYPT_SLH_DSA_FORS_H
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_fors.h
C
unknown
2,585
/* * 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_SLH_DSA #include "securec.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_algid.h" #include "crypt_types.h" #include "crypt_eal_md.h" #include "crypt_eal_mac.h" #include "eal_md_local.h" #include "slh_dsa_local.h" #include "slh_dsa_hash.h" #define SHA256_PADDING_LEN 64 #define SHA512_PADDING_LEN 128 static int32_t CalcMultiMsgHash(CRYPT_MD_AlgId mdId, const CRYPT_ConstData *hashData, uint32_t hashDataLen, uint8_t *out, uint32_t outLen) { uint8_t tmp[MAX_MDSIZE] = {0}; uint32_t tmpLen = sizeof(tmp); int32_t ret = CRYPT_CalcHash(NULL, EAL_MdFindDefaultMethod(mdId), hashData, hashDataLen, tmp, &tmpLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(out, outLen, tmp, outLen); return CRYPT_SUCCESS; } static int32_t PrfmsgShake256(const CryptSlhDsaCtx *ctx, const uint8_t *rand, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { uint32_t n = ctx->para.n; const CRYPT_ConstData hashData[] = {{ctx->prvKey.prf, n}, {rand, n}, {msg, msgLen}}; return CalcMultiMsgHash(CRYPT_MD_SHAKE256, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); } static int32_t HmsgShake256(const CryptSlhDsaCtx *ctx, const uint8_t *r, const uint8_t *msg, uint32_t msgLen, const uint8_t *idx, uint8_t *out) { (void)idx; uint32_t n = ctx->para.n; uint32_t m = ctx->para.m; const CRYPT_ConstData hashData[] = {{r, n}, {ctx->prvKey.pub.seed, n}, {ctx->prvKey.pub.root, n}, {msg, msgLen}}; return CalcMultiMsgHash(CRYPT_MD_SHAKE256, hashData, sizeof(hashData) / sizeof(hashData[0]), out, m); } static int32_t PrfShake256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, uint8_t *out) { uint32_t n = ctx->para.n; const CRYPT_ConstData hashData[] = { {ctx->prvKey.pub.seed, n}, {adrs->bytes, ctx->adrsOps.getAdrsLen()}, {ctx->prvKey.seed, n}}; return CalcMultiMsgHash(CRYPT_MD_SHAKE256, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); } static int32_t HShake256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { uint32_t n = ctx->para.n; const CRYPT_ConstData hashData[] = { {ctx->prvKey.pub.seed, n}, {adrs->bytes, ctx->adrsOps.getAdrsLen()}, {msg, msgLen}}; return CalcMultiMsgHash(CRYPT_MD_SHAKE256, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); } static int32_t TlShake256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { return HShake256(ctx, adrs, msg, msgLen, out); } static int32_t FShake256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { return HShake256(ctx, adrs, msg, msgLen, out); } static int32_t Prfmsg(const CryptSlhDsaCtx *ctx, const uint8_t *rand, const uint8_t *msg, uint32_t msgLen, uint8_t *out, CRYPT_MAC_AlgId macId) { int32_t ret; uint32_t n = ctx->para.n; uint8_t tmp[MAX_MDSIZE] = {0}; uint32_t tmpLen = sizeof(tmp); CRYPT_EAL_MacCtx *mdCtx = CRYPT_EAL_ProviderMacNewCtx(ctx->libCtx, macId, NULL); if (mdCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF_EX(CRYPT_EAL_MacInit(mdCtx, ctx->prvKey.prf, n), ret); GOTO_ERR_IF_EX(CRYPT_EAL_MacUpdate(mdCtx, rand, n), ret); GOTO_ERR_IF_EX(CRYPT_EAL_MacUpdate(mdCtx, msg, msgLen), ret); GOTO_ERR_IF_EX(CRYPT_EAL_MacFinal(mdCtx, tmp, &tmpLen), ret); (void)memcpy_s(out, n, tmp, n); ERR: CRYPT_EAL_MacFreeCtx(mdCtx); return ret; } static int32_t PrfmsgSha256(const CryptSlhDsaCtx *ctx, const uint8_t *rand, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { return Prfmsg(ctx, rand, msg, msgLen, out, CRYPT_MAC_HMAC_SHA256); } static int32_t PrfmsgSha512(const CryptSlhDsaCtx *ctx, const uint8_t *rand, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { return Prfmsg(ctx, rand, msg, msgLen, out, CRYPT_MAC_HMAC_SHA512); } static int32_t HmsgSha(const CryptSlhDsaCtx *ctx, const uint8_t *r, const uint8_t *seed, const uint8_t *root, const uint8_t *msg, uint32_t msgLen, uint8_t *out, CRYPT_MD_AlgId mdId) { int32_t ret; uint32_t m = ctx->para.m; uint32_t n = ctx->para.n; uint32_t tmpLen; uint8_t tmpSeed[2 * SLH_DSA_MAX_N + MAX_MDSIZE] = {0}; // 2 is for double uint32_t tmpSeedLen = 0; (void)memcpy_s(tmpSeed, sizeof(tmpSeed), r, n); (void)memcpy_s(tmpSeed + n, sizeof(tmpSeed) - n, seed, n); tmpSeedLen = n + n; tmpLen = CRYPT_EAL_MdGetDigestSize(mdId); const CRYPT_ConstData hashData[] = {{tmpSeed, tmpSeedLen}, {root, n}, {msg, msgLen}}; ret = CalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), tmpSeed + tmpSeedLen, tmpLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } tmpSeedLen += tmpLen; return CRYPT_Mgf1(NULL, EAL_MdFindDefaultMethod(mdId), tmpSeed, tmpSeedLen, out, m); } static int32_t HmsgSha256(const CryptSlhDsaCtx *ctx, const uint8_t *r, const uint8_t *msg, uint32_t msgLen, const uint8_t *idx, uint8_t *out) { (void)idx; return HmsgSha(ctx, r, ctx->prvKey.pub.seed, ctx->prvKey.pub.root, msg, msgLen, out, CRYPT_MD_SHA256); } static int32_t HmsgSha512(const CryptSlhDsaCtx *ctx, const uint8_t *r, const uint8_t *msg, uint32_t msgLen, const uint8_t *idx, uint8_t *out) { (void)idx; return HmsgSha(ctx, r, ctx->prvKey.pub.seed, ctx->prvKey.pub.root, msg, msgLen, out, CRYPT_MD_SHA512); } static int32_t PrfSha256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, uint8_t *out) { uint32_t n = ctx->para.n; uint8_t padding[SHA256_PADDING_LEN] = {0}; const CRYPT_ConstData hashData[] = {{ctx->prvKey.pub.seed, n}, {padding, sizeof(padding) - n}, {adrs->bytes, ctx->adrsOps.getAdrsLen()}, {ctx->prvKey.seed, n}}; return CalcMultiMsgHash(CRYPT_MD_SHA256, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); } static int32_t HSha256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { uint32_t n = ctx->para.n; uint8_t padding[SHA256_PADDING_LEN] = {0}; const CRYPT_ConstData hashData[] = {{ctx->prvKey.pub.seed, n}, {padding, sizeof(padding) - n}, {adrs->bytes, ctx->adrsOps.getAdrsLen()}, {msg, msgLen}}; return CalcMultiMsgHash(CRYPT_MD_SHA256, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); } static int32_t FSha256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { return HSha256(ctx, adrs, msg, msgLen, out); } static int32_t TlSha256(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { return HSha256(ctx, adrs, msg, msgLen, out); } static int32_t HSha512(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { uint32_t n = ctx->para.n; uint8_t padding[SHA512_PADDING_LEN] = {0}; const CRYPT_ConstData hashData[] = {{ctx->prvKey.pub.seed, n}, {padding, sizeof(padding) - n}, {adrs->bytes, ctx->adrsOps.getAdrsLen()}, {msg, msgLen}}; return CalcMultiMsgHash(CRYPT_MD_SHA512, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); } static int32_t TlSha512(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { return HSha512(ctx, adrs, msg, msgLen, out); } void SlhDsaInitHashFuncs(CryptSlhDsaCtx *ctx) { CRYPT_PKEY_ParaId algId = ctx->para.algId; SlhDsaHashFuncs *hashFuncs = &ctx->hashFuncs; if (algId == CRYPT_SLH_DSA_SHA2_128S || algId == CRYPT_SLH_DSA_SHA2_128F || algId == CRYPT_SLH_DSA_SHA2_192S || algId == CRYPT_SLH_DSA_SHA2_192F || algId == CRYPT_SLH_DSA_SHA2_256S || algId == CRYPT_SLH_DSA_SHA2_256F) { ctx->para.isCompressed = true; hashFuncs->prf = PrfSha256; hashFuncs->f = FSha256; if (ctx->para.secCategory == 1) { hashFuncs->prfmsg = PrfmsgSha256; hashFuncs->hmsg = HmsgSha256; hashFuncs->tl = TlSha256; hashFuncs->h = HSha256; } else { hashFuncs->prfmsg = PrfmsgSha512; hashFuncs->hmsg = HmsgSha512; hashFuncs->tl = TlSha512; hashFuncs->h = HSha512; } } else { ctx->para.isCompressed = false; hashFuncs->prfmsg = PrfmsgShake256; hashFuncs->hmsg = HmsgShake256; hashFuncs->prf = PrfShake256; hashFuncs->tl = TlShake256; hashFuncs->f = FShake256; hashFuncs->h = HShake256; } } #endif // HITLS_CRYPTO_SLH_DSA
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_hash.c
C
unknown
10,019
/* * 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 SLH_DSA_HASH_H #define SLH_DSA_HASH_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SLH_DSA #include <stdint.h> #include "bsl_params.h" #include "crypt_slh_dsa.h" #define MAX_MDSIZE 64 // The length "out" is n, the max length is SLH_DSA_MAX_N typedef int32_t (*SlhDsaPrf)(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, uint8_t *out); typedef int32_t (*SlhDsaTl)(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out); typedef int32_t (*SlhDsaH)(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out); typedef int32_t (*SlhDsaF)(const CryptSlhDsaCtx *ctx, const SlhDsaAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out); // The length of "prf", "rand" and "out" is n, the max length is SLH_DSA_MAX_N typedef int32_t (*SlhDsaPrfMsg)(const CryptSlhDsaCtx *ctx, const uint8_t *rand, const uint8_t *msg, uint32_t msgLen, uint8_t *out); // The length of "r", "seed" and "root" is n, the max length is SLH_DSA_MAX_N // the max length of "out" is SLH_DSA_MAX_M typedef int32_t (*SlhDsaHmsg)(const CryptSlhDsaCtx *ctx, const uint8_t *r, const uint8_t *msg, uint32_t msgLen, const uint8_t *idx, uint8_t *out); struct HashFuncs { SlhDsaPrf prf; SlhDsaTl tl; SlhDsaH h; SlhDsaF f; SlhDsaPrfMsg prfmsg; SlhDsaHmsg hmsg; }; void SlhDsaInitHashFuncs(CryptSlhDsaCtx *ctx); #endif // HITLS_CRYPTO_SLH_DSA #endif // SLH_DSA_HASH_H
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_hash.h
C
unknown
2,152
/* * 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_SLH_DSA #include <stdint.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "slh_dsa_local.h" #include "slh_dsa_xmss.h" #include "slh_dsa_hypertree.h" int32_t HypertreeSign(const uint8_t *msg, uint32_t msgLen, uint64_t treeIdx, uint32_t leafIdx, const CryptSlhDsaCtx *ctx, uint8_t *sig, uint32_t *sigLen) { int32_t ret; uint32_t n = ctx->para.n; uint32_t hp = ctx->para.hp; uint32_t d = ctx->para.d; uint32_t len = 2 * n + 3; uint32_t retLen = (len + hp) * n * d; uint32_t leafIdxTmp = leafIdx; uint64_t treeIdxTmp = treeIdx; if (*sigLen < retLen) { return CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH; } SlhDsaAdrs adrs = {0}; uint32_t offset = 0; uint32_t tmpLen = *sigLen; uint8_t root[MAX_MDSIZE] = {0}; // the msgLen is actually n. (void)memcpy_s(root, sizeof(root), msg, msgLen); for (uint32_t j = 0; j < d; j++) { if (j != 0) { leafIdxTmp = (uint32_t)(treeIdxTmp & ((1UL << hp) - 1)); treeIdxTmp = treeIdxTmp >> hp; ctx->adrsOps.setLayerAddr(&adrs, j); } ctx->adrsOps.setTreeAddr(&adrs, treeIdxTmp); tmpLen = retLen - offset; ret = XmssSign(root, n, leafIdxTmp, &adrs, ctx, sig + offset, &tmpLen, root); if (ret != CRYPT_SUCCESS) { return ret; } offset += tmpLen; } *sigLen = retLen; return CRYPT_SUCCESS; } int32_t HypertreeVerify(const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen, uint64_t treeIdx, uint32_t leafIdx, const CryptSlhDsaCtx *ctx) { int32_t ret; uint32_t n = ctx->para.n; uint32_t hp = ctx->para.hp; uint32_t d = ctx->para.d; uint32_t len = 2 * n + 3; uint32_t retLen = (len + hp) * n * d; uint32_t leafIdxTmp = leafIdx; uint64_t treeIdxTmp = treeIdx; if (sigLen < retLen) { return CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH; } SlhDsaAdrs adrs = {0}; uint32_t offset = 0; uint8_t node[MAX_MDSIZE] = {0}; // the msgLen is actually n. (void)memcpy_s(node, sizeof(node), msg, msgLen); for (uint32_t j = 0; j < d; j++) { if (j != 0) { leafIdxTmp = (uint32_t)(treeIdxTmp & ((1UL << hp) - 1)); treeIdxTmp = treeIdxTmp >> hp; ctx->adrsOps.setLayerAddr(&adrs, j); } ctx->adrsOps.setTreeAddr(&adrs, treeIdxTmp); ret = XmssPkFromSig(leafIdxTmp, sig + offset, sigLen - offset, node, n, &adrs, ctx, node); if (ret != CRYPT_SUCCESS) { return ret; } offset += (len + hp) * n; } if (memcmp(node, ctx->prvKey.pub.root, n) != 0) { return CRYPT_SLHDSA_ERR_HYPERTREE_VERIFY_FAIL; } return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_SLH_DSA
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_hypertree.c
C
unknown
3,451
/* * 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_SLH_DSA_HYPERTREE_H #define CRYPT_SLH_DSA_HYPERTREE_H #include <stdint.h> #include "slh_dsa_local.h" #ifdef HITLS_CRYPTO_SLH_DSA /** * @brief Sign a message using Hypertree * * @param msg Input message to sign * @param msgLen Length of the message * @param treeIdx Index of the tree to use * @param leafIdx Index of the leaf to use * @param ctx Context of SLH-DSA * @param sig Output signature * @param sigLen Length of the signature * @return int 0 on success, error code otherwise */ int32_t HypertreeSign(const uint8_t *msg, uint32_t msgLen, uint64_t treeIdx, uint32_t leafIdx, const CryptSlhDsaCtx *ctx, uint8_t *sig, uint32_t *sigLen); /** * @brief Verify a Hypertree signature * * @param msg Input message that was signed * @param msgLen Length of the message * @param sig Hypertree signature to verify * @param sigLen Length of the signature * @param treeIdx Index of the tree to use * @param leafIdx Index of the leaf to use * @param ctx Context of SLH-DSA * @return int 0 if signature is valid, error code otherwise */ int32_t HypertreeVerify(const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen, uint64_t treeIdx, uint32_t leafIdx, const CryptSlhDsaCtx *ctx); #endif // HITLS_CRYPTO_SLH_DSA #endif // CRYPT_SLH_DSA_HYPERTREE_H
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_hypertree.h
C
unknown
1,889
/* * 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 SLH_DSA_LOCAL_H #define SLH_DSA_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SLH_DSA #include <stdint.h> #include "bsl_params.h" #include "crypt_algid.h" #include "crypt_types.h" #include "crypt_utils.h" #include "slh_dsa_hash.h" #include "crypt_types.h" #define SLH_DSA_ADRS_LEN 32 #define SLH_DSA_ADRS_COMPRESSED_LEN 22 #define SLH_DSA_MAX_N 32 // Security parameter (hash output length) #define SLH_DSA_MAX_M 49 #define SLH_DSA_LGW 4 #define SLH_DSA_W 16 // 2^SLH_DSA_LGW #define SLH_DSA_PRVKEY 0x1 #define SLH_DSA_PUBKEY 0x10 typedef enum { WOTS_HASH, WOTS_PK, TREE, FORS_TREE, FORS_ROOTS, WOTS_PRF, FORS_PRF, } AdrsType; /** * @brief Address structure definition * * all the address is big-endian * it can be a address or a compressed address * Address: * | layer address | 4 bytes * | tree address | 12 bytes * | type | 4 bytes * | padding | 12 bytes * * Compressed Address: * | layer address | 1 bytes * | tree address | 8 bytes * | type | 1 bytes * | padding | 12 bytes * | hole | 10 bytes */ union Adrs { struct { uint8_t layerAddr[4]; uint8_t treeAddr[12]; uint8_t type[4]; uint8_t padding[12]; } uc; struct { uint8_t layerAddr; uint8_t treeAddr[8]; uint8_t type; uint8_t padding[12]; } c; struct { uint8_t layerAddr[4]; uint8_t treeAddr[8]; uint8_t type[4]; uint8_t padding[16]; } x; uint8_t bytes[SLH_DSA_ADRS_LEN]; }; // adrs operations functions typedef void (*AdrsSetLayerAddr)(SlhDsaAdrs *adrs, uint32_t layer); typedef void (*AdrsSetTreeAddr)(SlhDsaAdrs *adrs, uint64_t tree); typedef void (*AdrsSetType)(SlhDsaAdrs *adrs, AdrsType type); typedef void (*AdrsSetKeyPairAddr)(SlhDsaAdrs *adrs, uint32_t keyPair); typedef void (*AdrsSetChainAddr)(SlhDsaAdrs *adrs, uint32_t chain); typedef void (*AdrsSetTreeHeight)(SlhDsaAdrs *adrs, uint32_t height); typedef void (*AdrsSetHashAddr)(SlhDsaAdrs *adrs, uint32_t hash); typedef void (*AdrsSetTreeIndex)(SlhDsaAdrs *adrs, uint32_t index); typedef void (*AdrsSetKeyAndMask)(SlhDsaAdrs *adrs, uint32_t index); // for XMSS only typedef uint32_t (*AdrsGetTreeHeight)(const SlhDsaAdrs *adrs); typedef uint32_t (*AdrsGetTreeIndex)(const SlhDsaAdrs *adrs); typedef void (*AdrsCopyKeyPairAddr)(SlhDsaAdrs *adrs, const SlhDsaAdrs *adrs2); typedef uint32_t (*AdrsGetAdrsLen)(void); typedef struct { AdrsSetLayerAddr setLayerAddr; AdrsSetTreeAddr setTreeAddr; AdrsSetType setType; AdrsSetKeyPairAddr setKeyPairAddr; AdrsSetChainAddr setChainAddr; AdrsSetTreeHeight setTreeHeight; AdrsSetHashAddr setHashAddr; AdrsSetTreeIndex setTreeIndex; AdrsSetKeyAndMask setKeyAndMask; // for XMSS only AdrsGetTreeHeight getTreeHeight; AdrsGetTreeIndex getTreeIndex; AdrsCopyKeyPairAddr copyKeyPairAddr; AdrsGetAdrsLen getAdrsLen; } AdrsOps; // b can be 4, 6, 8, 9, 12, 14 // so use uint32_t to receive the BaseB value void BaseB(const uint8_t *x, uint32_t xLen, uint32_t b, uint32_t *out, uint32_t outLen); typedef struct { int algId; // CRYPT_PKEY_ParaId (SLH_DSA_AlgId or XMSS_AlgId) bool isCompressed; uint32_t n; uint32_t h; uint32_t d; uint32_t hp; uint32_t a; uint32_t k; uint32_t m; uint32_t secCategory; uint32_t pkBytes; uint32_t sigBytes; } SlhDsaPara; typedef struct { uint8_t seed[MAX_MDSIZE]; // pubkey seed for generating keys uint8_t root[MAX_MDSIZE]; // pubkey root for generating keys } SlhDsaPubKey; /** * @brief SLH-DSA private key structure */ typedef struct { uint8_t seed[MAX_MDSIZE]; // prvkey seed for generating keys uint8_t prf[MAX_MDSIZE]; // prvkey prf for generating keys uint64_t index; // the next unused WOTS+ key index, for XMSS only SlhDsaPubKey pub; } SlhDsaPrvKey; struct SlhDsaCtx { SlhDsaPara para; uint8_t *context; // user specific context uint32_t contextLen; // length of the user specific context bool isDeterministic; uint8_t *addrand; // optional random bytes, can be set through CTRL interface, or comes from RNG uint32_t addrandLen; // length of the optional random bytes bool isPrehash; bool isXmss; /* XMSS and SLH-DSA share common structure, * true : XMSS, false : SLH-DSA */ SlhDsaPrvKey prvKey; SlhDsaHashFuncs hashFuncs; AdrsOps adrsOps; uint8_t keyType; /* specify the key type */ void *libCtx; }; #endif // HITLS_CRYPTO_SLH_DSA #endif // SLH_DSA_LOCAL_H
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_local.h
C
unknown
5,215
/* * 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_SLH_DSA #include <stdint.h> #include <string.h> #include "securec.h" #include "bsl_errno.h" #include "crypt_errno.h" #include "bsl_sal.h" #include "slh_dsa_local.h" #include "slh_dsa_wots.h" static int32_t MsgToBaseW(const CryptSlhDsaCtx *ctx, const uint8_t *msg, uint32_t msgLen, uint32_t *out) { uint32_t n = ctx->para.n; uint32_t len1 = 2 * n; uint32_t len2 = 3; BaseB(msg, msgLen, SLH_DSA_LGW, out, len1); // todo: check if csum overflow uint64_t csum = 0; for (uint32_t i = 0; i < len1; i++) { csum += SLH_DSA_W - 1 - out[i]; } csum <<= SLH_DSA_LGW; uint8_t csumBytes[2]; csumBytes[0] = (uint8_t)(csum >> 8); csumBytes[1] = (uint8_t)csum; BaseB(csumBytes, 2, SLH_DSA_LGW, out + len1, len2); return 0; } int32_t WotsChain(const uint8_t *x, uint32_t xLen, uint32_t start, uint32_t end, const uint8_t *seed, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *output) { (void)seed; int32_t ret; uint8_t tmp[MAX_MDSIZE]; (void)memcpy_s(tmp, sizeof(tmp), x, xLen); uint32_t tmpLen = xLen; for (uint32_t i = start; i < start + end; i++) { ctx->adrsOps.setHashAddr(adrs, i); ret = ctx->hashFuncs.f(ctx, adrs, tmp, tmpLen, tmp); if (ret != 0) { return ret; } } (void)memcpy_s(output, tmpLen, tmp, tmpLen); return 0; } int WotsGeneratePublicKey(uint8_t *pub, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx) { int32_t ret; uint32_t n = ctx->para.n; uint32_t len = 2 * n + 3; SlhDsaAdrs skAdrs = *adrs; if (!ctx->isXmss) { ctx->adrsOps.setType(&skAdrs, WOTS_PRF); ctx->adrsOps.copyKeyPairAddr(&skAdrs, adrs); } uint8_t *tmp = (uint8_t *)BSL_SAL_Malloc(len * n); if (tmp == NULL) { return BSL_MALLOC_FAIL; } for (uint32_t i = 0; i < len; i++) { ctx->adrsOps.setChainAddr(&skAdrs, i); uint8_t sk[MAX_MDSIZE] = {0}; ret = ctx->hashFuncs.prf(ctx, &skAdrs, sk); if (ret != 0) { goto ERR; } ctx->adrsOps.setChainAddr(adrs, i); ret = WotsChain(sk, n, 0, SLH_DSA_W - 1, ctx->prvKey.pub.seed, adrs, ctx, (tmp + i * n)); if (ret != 0) { goto ERR; } } // compress public key SlhDsaAdrs wotspk = *adrs; ctx->adrsOps.setType(&wotspk, WOTS_PK); ctx->adrsOps.copyKeyPairAddr(&wotspk, adrs); ret = ctx->hashFuncs.tl(ctx, &wotspk, tmp, len * n, pub); ERR: BSL_SAL_Free(tmp); return ret; } int32_t WotsSign(uint8_t *sig, uint32_t *sigLen, const uint8_t *msg, uint32_t msgLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx) { int32_t ret; uint32_t n = ctx->para.n; uint32_t len = 2 * n + 3; if (*sigLen < len * n) { return CRYPT_BN_BUFF_LEN_NOT_ENOUGH; } uint32_t *msgw = (uint32_t *)BSL_SAL_Malloc(len * sizeof(uint32_t)); if (msgw == NULL) { return BSL_MALLOC_FAIL; } ret = MsgToBaseW(ctx, msg, msgLen, msgw); if (ret != 0) { goto ERR; } SlhDsaAdrs skAdrs = *adrs; if (!ctx->isXmss) { ctx->adrsOps.setType(&skAdrs, WOTS_PRF); ctx->adrsOps.copyKeyPairAddr(&skAdrs, adrs); } for (uint32_t i = 0; i < len; i++) { ctx->adrsOps.setChainAddr(&skAdrs, i); uint8_t sk[MAX_MDSIZE] = {0}; ret = ctx->hashFuncs.prf(ctx, &skAdrs, sk); if (ret != 0) { goto ERR; } ctx->adrsOps.setChainAddr(adrs, i); ret = WotsChain(sk, n, 0, msgw[i], ctx->prvKey.pub.seed, adrs, ctx, sig + i * n); if (ret != 0) { goto ERR; } } ERR: BSL_SAL_Free(msgw); *sigLen = len * n; return ret; } int32_t WotsPubKeyFromSig(const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *pub) { int32_t ret; uint32_t n = ctx->para.n; uint32_t len = 2 * n + 3; uint32_t *msgw = NULL; uint8_t *tmp = NULL; if (sigLen < len * n) { return CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH; } msgw = (uint32_t *)BSL_SAL_Malloc(len * sizeof(uint32_t)); if (msgw == NULL) { return BSL_MALLOC_FAIL; } ret = MsgToBaseW(ctx, msg, msgLen, msgw); if (ret != 0) { goto ERR; } tmp = (uint8_t *)BSL_SAL_Malloc(len * n); if (tmp == NULL) { ret = BSL_MALLOC_FAIL; goto ERR; } for (uint32_t i = 0; i < len; i++) { ctx->adrsOps.setChainAddr(adrs, i); ret = WotsChain(sig + i * n, n, msgw[i], SLH_DSA_W - 1 - msgw[i], ctx->prvKey.pub.seed, adrs, ctx, tmp + i * n); if (ret != 0) { goto ERR; } } SlhDsaAdrs wotspk = *adrs; ctx->adrsOps.setType(&wotspk, WOTS_PK); ctx->adrsOps.copyKeyPairAddr(&wotspk, adrs); ret = ctx->hashFuncs.tl(ctx, &wotspk, tmp, len * n, pub); ERR: BSL_SAL_Free(msgw); if (tmp != NULL) { BSL_SAL_Free(tmp); } return ret; } #endif // HITLS_CRYPTO_SLH_DSA
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_wots.c
C
unknown
5,653
/* * 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_SLH_DSA_WOTS_H #define CRYPT_SLH_DSA_WOTS_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SLH_DSA #include <stdint.h> #include "crypt_slh_dsa.h" /** * @brief Compute a WOTS+ public key from a private key * * @param pub Output WOTS+ public key * @param seed Public seed for chain computation * @param adrs Address structure for domain separation * @return int 0 on success, error code otherwise */ int WotsGeneratePublicKey(uint8_t *pub, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx); /** * @brief Sign a message using WOTS+ * * @param sig Output WOTS+ signature * @param sigLen Length of the signature * @param msg Input message to sign * @param msgLen Length of the message * @param adrs Address structure for domain separation * @param ctx SLH-DSA context * @return int 0 on success, error code otherwise */ int32_t WotsSign(uint8_t *sig, uint32_t *sigLen, const uint8_t *msg, uint32_t msgLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx); /** * @brief Compute a WOTS+ public key from a signature and message * * @param msg Input message that was signed * @param msgLen Length of the message * @param sig WOTS+ signature * @param sigLen Length of the signature * @param adrs Address structure for domain separation * @param ctx SLH-DSA context * @param pub Output reconstructed WOTS+ public key, the length is n * @return int 0 on success, error code otherwise */ int32_t WotsPubKeyFromSig(const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *pub); /** * @brief Compute a WOTS+ chain * * @param x Input private key value to chain * @param xLen Length of the input private key value * @param start Starting position in the chain * @param end Ending position in the chain * @param seed Public seed for chain computation * @param adrs Address structure for domain separation * @param ctx SLH-DSA context * @param output Output chain result, the length is n * @return int 0 on success, error code otherwise */ int32_t WotsChain(const uint8_t *x, uint32_t xLen, uint32_t start, uint32_t end, const uint8_t *seed, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *output); #endif // HITLS_CRYPTO_SLH_DSA #endif // CRYPT_SLH_DSA_WOTS_H
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_wots.h
C
unknown
2,873
/* * 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_SLH_DSA #include <stdint.h> #include <stddef.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "slh_dsa_local.h" #include "slh_dsa_xmss.h" #include "slh_dsa_wots.h" int32_t XmssNode(uint8_t *node, uint32_t idx, uint32_t height, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *AuthPath, uint32_t LeafIdx) { int32_t ret; if (node == NULL || adrs == NULL || ctx == NULL) { return CRYPT_NULL_INPUT; } uint32_t n = ctx->para.n; // If height is 0, compute WOTS+ public key if (height == 0) { ctx->adrsOps.setType(adrs, WOTS_HASH); ctx->adrsOps.setKeyPairAddr(adrs, idx); ret = WotsGeneratePublicKey(node, adrs, ctx); if (ret != CRYPT_SUCCESS) { return ret; } if (AuthPath && (idx == ((LeafIdx >> height) ^ 0x01))) { (void)memcpy_s(AuthPath + (height * n), n, node, n); } return CRYPT_SUCCESS; } // Compute internal node uint8_t leftNode[MAX_MDSIZE] = {0}; uint8_t rightNode[MAX_MDSIZE] = {0}; // Compute left child ret = XmssNode(leftNode, 2 * idx, height - 1, adrs, ctx, AuthPath, LeafIdx); if (ret != CRYPT_SUCCESS) { return ret; } // Compute right child ret = XmssNode(rightNode, 2 * idx + 1, height - 1, adrs, ctx, AuthPath, LeafIdx); if (ret != CRYPT_SUCCESS) { return ret; } // Hash children to get parent node ctx->adrsOps.setType(adrs, TREE); if (ctx->isXmss) { /* tree height is the 'lower' layer for xmss */ ctx->adrsOps.setTreeHeight(adrs, height - 1); } else { ctx->adrsOps.setTreeHeight(adrs, height); } ctx->adrsOps.setTreeIndex(adrs, idx); uint8_t tmp[MAX_MDSIZE * 2]; (void)memcpy_s(tmp, MAX_MDSIZE * 2, leftNode, n); (void)memcpy_s(tmp + n, MAX_MDSIZE * 2 - n, rightNode, n); ret = ctx->hashFuncs.h(ctx, adrs, tmp, 2 * n, node); if (ret != CRYPT_SUCCESS) { return ret; } if ((height != ctx->para.hp) && AuthPath && (idx == ((LeafIdx >> height) ^ 0x01))) { (void)memcpy_s(AuthPath + (height * n), n, node, n); } return CRYPT_SUCCESS; } int32_t XmssSign(const uint8_t *msg, size_t msgLen, uint32_t idx, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *sig, uint32_t *sigLen, uint8_t *root) { int32_t ret; uint32_t n = ctx->para.n; uint32_t hp = ctx->para.hp; uint32_t len = 2 * n + 3; if (*sigLen < (len + hp) * n) { return CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH; } ctx->adrsOps.setType(adrs, WOTS_HASH); ctx->adrsOps.setKeyPairAddr(adrs, idx); uint32_t tmpLen = len * n; ret = WotsSign(sig, &tmpLen, msg, (uint32_t)msgLen, adrs, ctx); if (ret != CRYPT_SUCCESS) { return ret; } ret = XmssNode(root, 0, hp, adrs, ctx, sig + (len * n), idx); if (ret != CRYPT_SUCCESS) { return ret; } *sigLen = (len + hp) * n; return CRYPT_SUCCESS; } int32_t XmssPkFromSig(uint32_t idx, const uint8_t *sig, uint32_t sigLen, const uint8_t *msg, uint32_t msgLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *pk) { int32_t ret; uint32_t n = ctx->para.n; uint32_t hp = ctx->para.hp; uint32_t len = 2 * n + 3; if (sigLen < (len + hp) * n) { return CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH; } ctx->adrsOps.setType(adrs, WOTS_HASH); ctx->adrsOps.setKeyPairAddr(adrs, idx); uint8_t node0[MAX_MDSIZE] = {0}; uint8_t node1[MAX_MDSIZE] = {0}; ret = WotsPubKeyFromSig(msg, msgLen, sig, sigLen, adrs, ctx, node0); if (ret != CRYPT_SUCCESS) { return ret; } ctx->adrsOps.setType(adrs, TREE); ctx->adrsOps.setTreeIndex(adrs, idx); for (uint32_t k = 0; k < hp; k++) { if (ctx->isXmss) { /* tree height is the 'lower' layer for xmss */ ctx->adrsOps.setTreeHeight(adrs, k); } else { ctx->adrsOps.setTreeHeight(adrs, k + 1); } uint8_t tmp[MAX_MDSIZE * 2]; if (((idx >> k) & 1) != 0) { (void)memcpy_s(tmp, sizeof(tmp), sig + (len + k) * n, n); (void)memcpy_s(tmp + n, sizeof(tmp) - n, node0, n); ctx->adrsOps.setTreeIndex(adrs, (ctx->adrsOps.getTreeIndex(adrs) - 1) >> 1); } else { (void)memcpy_s(tmp, sizeof(tmp), node0, n); (void)memcpy_s(tmp + n, sizeof(tmp) - n, sig + (len + k) * n, n); ctx->adrsOps.setTreeIndex(adrs, ctx->adrsOps.getTreeIndex(adrs) >> 1); } ret = ctx->hashFuncs.h(ctx, adrs, tmp, 2 * n, node1); if (ret != CRYPT_SUCCESS) { return ret; } (void)memcpy_s(node0, sizeof(node0), node1, sizeof(node1)); } (void)memcpy_s(pk, n, node0, n); return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_SLH_DSA
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_xmss.c
C
unknown
5,503
/* * 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_SLH_DSA_XMSS_H #define CRYPT_SLH_DSA_XMSS_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SLH_DSA #include <stdint.h> #include "crypt_slh_dsa.h" /** * @brief Sign a message using XMSS * * @param sig Output XMSS signature * @param sigLen Length of the signature * @param msg Input message to sign * @param msgLen Length of the message * @param idx Index of the used WOTS+ key pair * @param adrs Address structure for domain separation * @param ctx SLH-DSA context * @param root root node of XMSS tree * @return int 0 on success, error code otherwise */ int32_t XmssSign(const uint8_t *msg, size_t msgLen, uint32_t idx, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *sig, uint32_t *sigLen, uint8_t *root); /** * @brief Compute an internal node of the XMSS tree * * @param node Output internal node * @param idx Node index at the given height * @param height Node height in the tree * @param adrs Address structure for domain separation * @param ctx SLH-DSA context * @param AuthPath authentication path for the LeafIdx * @param LeafIdx WOTS+ key pair index * @return int 0 on success, error code otherwise */ int32_t XmssNode(uint8_t *node, uint32_t idx, uint32_t height, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *AuthPath, uint32_t LeafIdx); /** * @brief Compute a public key from a signature and message * * @param idx Index of the used WOTS+ key pair * @param sig Signature * @param sigLen Length of the signature * @param msg Message * @param msgLen Length of the message * @param adrs Address structure for domain separation * @param ctx SLH-DSA context * @param pk Output public key, the length is n * @return int 0 on success, error code otherwise */ int32_t XmssPkFromSig(uint32_t idx, const uint8_t *sig, uint32_t sigLen, const uint8_t *msg, uint32_t msgLen, SlhDsaAdrs *adrs, const CryptSlhDsaCtx *ctx, uint8_t *pk); #endif // HITLS_CRYPTO_SLH_DSA #endif // CRYPT_SLH_DSA_XMSS_H
2301_79861745/bench_create
crypto/slh_dsa/src/slh_dsa_xmss.h
C
unknown
2,563
/* * 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_SM2_H #define CRYPT_SM2_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM2 #include <stdint.h> #include "crypt_types.h" #include "crypt_ecc.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ typedef struct SM2_Ctx CRYPT_SM2_Ctx; /* SM2 parameter structure */ typedef struct EccPara CRYPT_Sm2Para; /** * @ingroup sm2 * @brief sm2 Allocate the context memory space. * * @retval (CRYPT_SM2_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_SM2_Ctx *CRYPT_SM2_NewCtx(void); /** * @ingroup sm2 * @brief sm2 Allocate the context memory space. * * @param libCtx [IN] Library context * * @retval (CRYPT_SM2_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_SM2_Ctx *CRYPT_SM2_NewCtxEx(void *libCtx); /** * @ingroup sm2 * @brief Copy the sm2 context. After the duplication is complete, invoke the CRYPT_SM2_FreeCtx to release the memory. * * @param ctx [IN] Source SM2 context * * @return CRYPT_SM2_Ctx SM2 context pointer= */ CRYPT_SM2_Ctx *CRYPT_SM2_DupCtx(CRYPT_SM2_Ctx *ctx); /** * @ingroup sm2 * @brief release sm2 key context structure * * @param ctx [IN] Context structure to be released. */ void CRYPT_SM2_FreeCtx(CRYPT_SM2_Ctx *ctx); /** * @ingroup sm2 * @brief sm2 Obtain the key length. * * @param ctx [IN] sm2 context structure * * @retval 0 The input is incorrect or the corresponding key structure does not contain valid key length. * @retval uint32_t Valid key length */ uint32_t CRYPT_SM2_GetBits(const CRYPT_SM2_Ctx *ctx); /** * @ingroup sm2 * @brief Generate the SM2 key pair. * * @param ctx [IN/OUT] sm2 context structure * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval ECC error code. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS The key pair is successfully generated. */ int32_t CRYPT_SM2_Gen(CRYPT_SM2_Ctx *ctx); #ifdef HITLS_CRYPTO_SM2_SIGN /** * @ingroup sm2 * @brief sm2 obtain the length of the signature data, in bytes. * * @param ctx [IN] sm2 context structure * * @retval 0 The input is incorrect or the corresponding key structure does not contain valid parameter data. * @retval uint32_t Length required for valid signature data */ uint32_t CRYPT_SM2_GetSignLen(const CRYPT_SM2_Ctx *ctx); /** * @ingroup sm2 * @brief SM2 Signature * * @param ctx [IN] sm2 context structure * @param algId [IN] md algId * @param data [IN] Data to be signed * @param dataLen [IN] Length of the data to be signed * @param sign [OUT] Signature data * @param signLen [IN/OUT] The input parameter is the space length of the sign, * and the output parameter is the valid length of the sign. * The required space can be obtained by calling CRYPT_SM2_GetSignLen. * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SM2_ERR_EMPTY_KEY The key cannot be empty. * @retval CRYPT_SM2_BUFF_LEN_NOT_ENOUGH The buffer length is insufficient. * @retval BN error. An error occurs in the internal BigNum operation. * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS Signed successfully. */ int32_t CRYPT_SM2_Sign(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); /** * @ingroup sm2 * @brief SM2 Verify the signature. * * @param ctx [IN] sm2 context structure * @param algId [IN] md algId * @param data [IN] Data to be signed * @param dataLen [IN] Length of the data to be signed * @param sign [IN] Signature data * @param signLen [IN] Valid length of the sign * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SM2_VERIFY_FAIL Failed to verify the signature. * @retval BN error. An error occurs in the internal BigNum operation. * @retval ECC error. An error occurred in the internal ECC calculation. * @retval DSA error. An error occurs in the DSA encoding and decoding part. * @retval CRYPT_SUCCESS The signature verification is successful. */ int32_t CRYPT_SM2_Verify(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); #endif /** * @ingroup sm2 * @brief SM2 Set the private key data. * * @param ctx [OUT] sm2 context structure * @param prv [IN] External private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_SM2_SetPrvKey(CRYPT_SM2_Ctx *ctx, const CRYPT_Sm2Prv *prv); /** * @ingroup sm2 * @brief SM2 Set the public key data. * * @param ctx [OUT] sm2 context structure * @param pub [IN] External public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_SM2_SetPubKey(CRYPT_SM2_Ctx *ctx, const CRYPT_DsaPub *pub); /** * @ingroup sm2 * @brief SM2 Obtain the private key data. * * @param ctx [IN] sm2 context structure * @param prv [OUT] External private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty. * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS obtained successfully. */ int32_t CRYPT_SM2_GetPrvKey(const CRYPT_SM2_Ctx *ctx, CRYPT_DsaPrv *prv); /** * @ingroup sm2 * @brief SM2 Obtain the public key data. * * @param ctx [IN] sm2 context structure * @param pub [OUT] External public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty. * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS Obtained successfully. */ int32_t CRYPT_SM2_GetPubKey(const CRYPT_SM2_Ctx *ctx, CRYPT_DsaPub *pub); #ifdef HITLS_BSL_PARAMS /** * @ingroup sm2 * @brief SM2 Set the private key data. * * @param ctx [OUT] sm2 context structure * @param para [IN] External private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_SM2_SetPrvKeyEx(CRYPT_SM2_Ctx *ctx, const BSL_Param *para); /** * @ingroup sm2 * @brief SM2 Set the public key data. * * @param ctx [OUT] sm2 context structure * @param para [IN] External public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_SM2_SetPubKeyEx(CRYPT_SM2_Ctx *ctx, const BSL_Param *para); /** * @ingroup sm2 * @brief SM2 Obtain the private key data. * * @param ctx [IN] sm2 context structure * @param para [OUT] External private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty. * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS obtained successfully. */ int32_t CRYPT_SM2_GetPrvKeyEx(const CRYPT_SM2_Ctx *ctx, BSL_Param *para); /** * @ingroup sm2 * @brief SM2 Obtain the public key data. * * @param ctx [IN] sm2 context structure * @param para [OUT] External public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty. * @retval ECC error. An error occurred in the internal ECC calculation. * @retval CRYPT_SUCCESS Obtained successfully. */ int32_t CRYPT_SM2_GetPubKeyEx(const CRYPT_SM2_Ctx *ctx, BSL_Param *para); #endif /** * @ingroup sm2 * @brief sm2 control interface * * @param ctx [IN/OUT] sm2 context structure * @param opt [IN] Operation mode. For details, see ECC_CtrlType. * @param val [IN] Input parameter * @param len [IN] val Length * * @retval CRYPT_SUCCESS set successfully. * @retval CRYPT_NULL_INPUT If any input parameter is empty * @retval For other error codes, see crypt_errno.h. */ int32_t CRYPT_SM2_Ctrl(CRYPT_SM2_Ctx *ctx, int32_t opt, void *val, uint32_t len); #ifdef HITLS_CRYPTO_SM2_EXCH /** * @ingroup sm2 * @brief sm2 Generate the shared key. * * @param selfCtx [IN] Local context structure * @param peerCtx [IN] Peer context structure * @param out [OUT] Generated shared key * @param outlen [IN/OUT] Length of the generated shared key * * @retval CRYPT_SUCCESS secceeded. * @retval CRYPT_NULL_INPUT If any input parameter is empty * @retval For other error codes, see crypt_errno.h. */ int32_t CRYPT_SM2_KapComputeKey(const CRYPT_SM2_Ctx *selfCtx, const CRYPT_SM2_Ctx *peerCtx, uint8_t *out, uint32_t *outlen); #endif #ifdef HITLS_CRYPTO_SM2_CRYPT /** * @ingroup sm2 * @brief sm2 Encryption * @param ctx [IN] Context structure * @param data [IN] Plaintext * @param datalen [IN] Plaintext length * @param out [OUT] Output ciphertext * @param outlen [OUT] Ciphertext length * * @retval CRYPT_SUCCESS secceeded. * @retval CRYPT_NULL_INPUT If any input parameter is empty * @retval For other error codes, see crypt_errno.h. */ int32_t CRYPT_SM2_Encrypt(CRYPT_SM2_Ctx *ctx, const uint8_t *data, uint32_t datalen, uint8_t *out, uint32_t *outlen); /** * @ingroup sm2 * @brief sm2 Decryption * @param ctx [IN] Context structure * @param data [IN] Received ciphertext * @param datalen [IN] Ciphertext length * @param out [OUT] Output plaintext after decryption * @param outlen [OUT] Length of the decrypted plaintext * * @retval CRYPT_SUCCESS secceeded. * @retval CRYPT_NULL_INPUT If any input parameter is empty * @retval For other error codes, see crypt_errno.h. */ int32_t CRYPT_SM2_Decrypt(CRYPT_SM2_Ctx *ctx, const uint8_t *data, uint32_t datalen, uint8_t *out, uint32_t *outlen); #endif /** * @ingroup sm2 * @brief sm2 Compare the public key and parameters. * * @param a [IN] sm2 context structure * @param b [IN] sm2 context structure * * @retval CRYPT_SUCCESS is the same * For other error codes, see crypt_errno.h. */ int32_t CRYPT_SM2_Cmp(const CRYPT_SM2_Ctx *a, const CRYPT_SM2_Ctx *b); /** * @ingroup sm2 * @brief sm2 get security bits * * @param ctx [IN] sm2 Context structure * * @retval security bits */ int32_t CRYPT_SM2_GetSecBits(const CRYPT_SM2_Ctx *ctx); /** * @ingroup sm2 * @brief sm2 import key * * @param ctx [IN/OUT] sm2 context structure * @param params [IN] key parameters */ int32_t CRYPT_SM2_Import(CRYPT_SM2_Ctx *ctx, const BSL_Param *params); /** * @ingroup sm2 * @brief sm2 export key * * @param ctx [IN] sm2 context structure * @param params [IN/OUT] key parameters */ int32_t CRYPT_SM2_Export(const CRYPT_SM2_Ctx *ctx, BSL_Param *params); #ifdef HITLS_CRYPTO_SM2_CHECK /** * @ingroup sm2 * @brief sm2 check public key * * @param checkType [IN] check type * @param pkey1 [IN] sm2 context structure * @param pkey2 [IN] sm2 context structure * * @retval CRYPT_SUCCESS is the same * Others. For details, see error code in errno. */ int32_t CRYPT_SM2_Check(uint32_t checkType, const CRYPT_SM2_Ctx *pkey1, const CRYPT_SM2_Ctx *pkey2); #endif // HITLS_CRYPTO_SM2_CHECK #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SM2 #endif // CRYPT_SM2_H
2301_79861745/bench_create
crypto/sm2/include/crypt_sm2.h
C
unknown
12,846
/* * 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_SM2_CRYPT #include <limits.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_types.h" #include "crypt_utils.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_bn.h" #include "crypt_ecc.h" #include "crypt_ecc_pkey.h" #include "crypt_local_types.h" #include "sm2_local.h" #include "crypt_sm2.h" #include "crypt_encode_internal.h" #define SM2_POINT_SINGLE_COORDINATE_LEN 32 #define SM2_POINT_COORDINATE_LEN 65 static void EncryptMemFree(ECC_Point *c1, ECC_Point *tmp, BN_BigNum *k, bool isInternal, BN_BigNum *order, uint8_t *c2) { ECC_FreePoint(c1); ECC_FreePoint(tmp); if (isInternal) { BN_Destroy(k); } BN_Destroy(order); BSL_SAL_FREE(c2); } static int32_t ParaCheckAndCalculate(CRYPT_SM2_Ctx *ctx, ECC_Point *tmp, BN_BigNum *k) { int32_t ret; // Check whether [h]PB is equal to infinity point. GOTO_ERR_IF(ECC_PointCheck(ctx->pkey->pubkey), ret); // Calculate [k] * PB GOTO_ERR_IF(ECC_PointMul(ctx->pkey->para, tmp, k, ctx->pkey->pubkey), ret); ERR: return ret; } static int32_t Sm3Hash(const EAL_MdMethod *hashMethod, const uint8_t *pbBuf, const uint8_t *data, uint32_t datalen, uint8_t *c3Buf, uint32_t *c3BufLen) { int32_t ret; void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); return ret; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, pbBuf + 1, SM2_POINT_SINGLE_COORDINATE_LEN), ret); // Horizontal coordinate x2 of PB GOTO_ERR_IF(hashMethod->update(mdCtx, data, datalen), ret); // M GOTO_ERR_IF(hashMethod->update(mdCtx, pbBuf + SM2_POINT_SINGLE_COORDINATE_LEN + 1, SM2_POINT_SINGLE_COORDINATE_LEN), ret); // Vertical coordinate y2 of PB // Calculated c3, in c3Buf GOTO_ERR_IF(hashMethod->final(mdCtx, c3Buf, c3BufLen), ret); ERR: hashMethod->freeCtx(mdCtx); return ret; } static int32_t IsDataZero(const uint8_t *data, uint32_t datalen) { uint8_t check = 0; for (uint32_t i = 0; i < datalen; i++) { check |= data[i]; } if (check == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_DECRYPT_FAIL); return CRYPT_SM2_DECRYPT_FAIL; } return CRYPT_SUCCESS; } static int32_t MemAllocCheck(const BN_BigNum *k, const BN_BigNum *order, const ECC_Point *c1, const ECC_Point *tmp, const uint8_t *c2) { if (k == NULL || order == NULL || c1 == NULL || tmp == NULL || c2 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } return CRYPT_SUCCESS; } static void XorCalculate(uint8_t *c2, const uint8_t *data, uint32_t datalen) { uint32_t i; for (i = 0; i < datalen; ++i) { c2[i] ^= data[i]; } return; } #ifdef HITLS_CRYPTO_ACVP_TESTS int32_t CRYPT_SM2_SetK(CRYPT_SM2_Ctx *ctx, uint8_t *val, uint32_t len) { if (ctx == NULL || val == NULL || len <= 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->paraEx.k != NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_K_REPEAT_SET_ERROR); return CRYPT_SM2_K_REPEAT_SET_ERROR; } BN_BigNum *k = BN_Create(CRYPT_SM2_GetBits(ctx)); if (k == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret = BN_Bin2Bn(k, val, len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if (BN_IsZero(k)) { BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG); ret = BSL_INVALID_ARG; goto EXIT; } ctx->paraEx.k = k; return CRYPT_SUCCESS; EXIT: BN_Destroy(k); return ret; } #endif static int32_t EncryptInputCheck(const CRYPT_SM2_Ctx *ctx, const uint8_t *data, uint32_t datalen, const uint8_t *out, const uint32_t *outlen) { // 0-length plaintext encryption is not supported. if (ctx == NULL || data == NULL || datalen == 0 || out == NULL || outlen == NULL || *outlen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t encodeLen = 0; int32_t ret = CRYPT_EAL_GetSm2EncryptDataEncodeLen(SM2_POINT_SINGLE_COORDINATE_LEN, SM2_POINT_SINGLE_COORDINATE_LEN, SM3_MD_SIZE, datalen, &encodeLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (*outlen < encodeLen) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_BUFF_LEN_NOT_ENOUGH); return CRYPT_SM2_BUFF_LEN_NOT_ENOUGH; } if (ctx->pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_EMPTY_KEY); return CRYPT_SM2_ERR_EMPTY_KEY; } if (ctx->pkey->pubkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_NO_PUBKEY); return CRYPT_SM2_NO_PUBKEY; } return CRYPT_SUCCESS; } int32_t CRYPT_SM2_Encrypt(CRYPT_SM2_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) { return ret; } uint32_t i; BN_BigNum *k = NULL; bool isInternal = false; #ifdef HITLS_CRYPTO_ACVP_TESTS k = ctx->paraEx.k; #endif if (k == NULL) { k = BN_Create(CRYPT_SM2_GetBits(ctx)); isInternal = true; } BN_BigNum *order = ECC_GetParaN(ctx->pkey->para); ECC_Point *c1 = ECC_NewPoint(ctx->pkey->para); ECC_Point *tmp = ECC_NewPoint(ctx->pkey->para); uint32_t buflen = SM2_POINT_COORDINATE_LEN; uint8_t c1Buf[SM2_POINT_COORDINATE_LEN]; uint8_t tmpBuf[SM2_POINT_COORDINATE_LEN]; uint8_t *c2 = BSL_SAL_Malloc(datalen); uint8_t c3Buf[SM3_MD_SIZE]; uint32_t c3BufLen = SM3_MD_SIZE; CRYPT_SM2_EncryptData encData = { // +1: Skip one byte for '04' .x = c1Buf + 1, .xLen = SM2_POINT_SINGLE_COORDINATE_LEN, .y = c1Buf + SM2_POINT_SINGLE_COORDINATE_LEN + 1, .yLen = SM2_POINT_SINGLE_COORDINATE_LEN, .hash = c3Buf, .hashLen = c3BufLen, .cipher = c2, .cipherLen = datalen, }; GOTO_ERR_IF(MemAllocCheck(k, order, c1, tmp, c2), ret); for (i = 0; i < CRYPT_ECC_TRY_MAX_CNT; i++) { #ifdef HITLS_CRYPTO_ACVP_TESTS if (isInternal) { #endif GOTO_ERR_IF(BN_RandRangeEx(ctx->pkey->libCtx, k, order), ret); if (BN_IsZero(k)) { continue; } #ifdef HITLS_CRYPTO_ACVP_TESTS } #endif // c1 = k * G GOTO_ERR_IF(ECC_PointMul(ctx->pkey->para, c1, k, NULL), ret); // Convert the point format into binary data stream and save the data stream in tmpbuf. GOTO_ERR_IF(ECC_EncodePoint(ctx->pkey->para, c1, c1Buf, &buflen, CRYPT_POINT_UNCOMPRESSED), ret); GOTO_ERR_IF(ParaCheckAndCalculate(ctx, tmp, k), ret); GOTO_ERR_IF(ECC_EncodePoint(ctx->pkey->para, tmp, tmpBuf, &buflen, CRYPT_POINT_UNCOMPRESSED), ret); // Calculate the kdf. GOTO_ERR_IF(KdfGmt0032012(c2, &datalen, tmpBuf + 1, buflen - 1, ctx->hashMethod), ret); if (IsDataZero(c2, datalen) == CRYPT_SUCCESS) { break; } } if (i == CRYPT_ECC_TRY_MAX_CNT) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_TRY_CNT); ret = CRYPT_SM2_ERR_TRY_CNT; goto ERR; } // Bitwise XOR XorCalculate(c2, data, datalen); // x2 || M || y2, calculate the hash value GOTO_ERR_IF(Sm3Hash(ctx->hashMethod, tmpBuf, data, datalen, c3Buf, &c3BufLen), ret); GOTO_ERR_IF(CRYPT_EAL_EncodeSm2EncryptData(&encData, out, outlen), ret); ERR: EncryptMemFree(c1, tmp, k, isInternal, order, c2); return ret; } static int32_t IsUEqualToC3(const uint8_t *data, const uint8_t *sm3Buf, uint32_t sm3BufLen) { uint8_t check = 0; for (uint32_t i = 0; i < sm3BufLen; i++) { check |= sm3Buf[i] ^ data[i + SM2_POINT_COORDINATE_LEN]; } if (check != 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_DECRYPT_FAIL); return CRYPT_SM2_DECRYPT_FAIL; } return CRYPT_SUCCESS; } static int32_t DecryptInputCheck(const CRYPT_SM2_Ctx *ctx, const uint8_t *data, uint32_t datalen, const uint8_t *out, const uint32_t *outlen) { // 0-length plaintext decryption is not supported. if (ctx == NULL || data == NULL || datalen == 0 || out == NULL || outlen == NULL || *outlen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_EMPTY_KEY); return CRYPT_SM2_ERR_EMPTY_KEY; } if (ctx->pkey->prvkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_NO_PRVKEY); return CRYPT_SM2_NO_PRVKEY; } return CRYPT_SUCCESS; } static int32_t DecodeEncryptData(const uint8_t *data, uint32_t datalen, uint8_t **decode, const uint8_t **cipher, uint32_t *cipherLen) { *decode = BSL_SAL_Calloc(1u, datalen); if (*decode == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } // Add uncompressed point identifier (*decode)[0] = 0x04; CRYPT_SM2_EncryptData encData = { .x = *decode + 1, // Reserve one byte for '04' .xLen = SM2_POINT_SINGLE_COORDINATE_LEN, .y = *decode + SM2_POINT_SINGLE_COORDINATE_LEN + 1, .yLen = SM2_POINT_SINGLE_COORDINATE_LEN, .hash = *decode + SM2_POINT_COORDINATE_LEN, .hashLen = SM3_MD_SIZE, .cipher = *decode + SM2_POINT_COORDINATE_LEN + SM3_MD_SIZE, .cipherLen = datalen - SM2_POINT_COORDINATE_LEN - SM3_MD_SIZE }; int32_t ret = CRYPT_EAL_DecodeSm2EncryptData(data, datalen, &encData); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(*decode); *decode = NULL; BSL_ERR_PUSH_ERROR(ret); return ret; } // Return cipher related information *cipher = encData.cipher; *cipherLen = encData.cipherLen; return CRYPT_SUCCESS; } int32_t CRYPT_SM2_Decrypt(CRYPT_SM2_Ctx *ctx, const uint8_t *data, uint32_t datalen, uint8_t *out, uint32_t *outlen) { // take out the c1 int32_t ret = DecryptInputCheck(ctx, data, datalen, out, outlen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint8_t *decode = NULL; const uint8_t *cipher = NULL; uint32_t cipherLen = 0; ret = DecodeEncryptData(data, datalen, &decode, &cipher, &cipherLen); if (ret != CRYPT_SUCCESS) { return ret; } if (*outlen < cipherLen) { BSL_SAL_Free(decode); BSL_ERR_PUSH_ERROR(CRYPT_SM2_BUFF_LEN_NOT_ENOUGH); return CRYPT_SM2_BUFF_LEN_NOT_ENOUGH; } uint8_t sm3Buf[SM3_MD_SIZE]; uint32_t sm3BufLen = SM3_MD_SIZE; uint32_t tmplen = SM2_POINT_COORDINATE_LEN; uint8_t tmpBuf[SM2_POINT_COORDINATE_LEN]; ECC_Point *c1 = ECC_NewPoint(ctx->pkey->para); ECC_Point *tmp = ECC_NewPoint(ctx->pkey->para); uint8_t *t = BSL_SAL_Malloc(cipherLen); if (c1 == NULL || tmp == NULL || t == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(ECC_DecodePoint(ctx->pkey->para, c1, decode, SM2_POINT_COORDINATE_LEN), ret); // Calculate [dB]C1 = (x2, y2) and save it to the point tmp. GOTO_ERR_IF(ECC_PointMul(ctx->pkey->para, tmp, ctx->pkey->prvkey, c1), ret); // Extract x and y of the point tmp and save them to tmpbuf. GOTO_ERR_IF(ECC_EncodePoint(ctx->pkey->para, tmp, tmpBuf, &tmplen, CRYPT_POINT_UNCOMPRESSED), ret); // Calculate the kdf(x2 || y2, cipherLen). GOTO_ERR_IF(KdfGmt0032012(t, &cipherLen, tmpBuf + 1, tmplen - 1, ctx->hashMethod), ret); // Check whether t is all 0s. If yes, report an error and exit. GOTO_ERR_IF(IsDataZero(t, cipherLen), ret); // Calculate M' = C2 ^ t // Bitwise XOR, and the result is still stored in t. for (uint32_t i = 0; i < cipherLen; ++i) { t[i] ^= cipher[i]; } // Calculate hash(x2 || t || y2) GOTO_ERR_IF(Sm3Hash(ctx->hashMethod, tmpBuf, t, cipherLen, sm3Buf, &sm3BufLen), ret); // Check whether u is equal to c3. GOTO_ERR_IF(IsUEqualToC3(decode, sm3Buf, sm3BufLen), ret); // The verification is successful. M' is the last plaintext. (void)memcpy_s(out, *outlen, t, cipherLen); *outlen = cipherLen; ERR: BSL_SAL_FREE(decode); ECC_FreePoint(c1); ECC_FreePoint(tmp); BSL_SAL_CleanseData((void*)t, cipherLen); BSL_SAL_FREE(t); return ret; } #endif // HITLS_CRYPTO_SM2_CRYPT
2301_79861745/bench_create
crypto/sm2/src/sm2_crypt.c
C
unknown
13,150
/* * 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_SM2_EXCH) || defined(HITLS_CRYPTO_SM2_CRYPT) #include <stdbool.h> #include "crypt_errno.h" #include "crypt_types.h" #include "crypt_utils.h" #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_bn.h" #include "crypt_ecc.h" #include "crypt_ecc_pkey.h" #include "crypt_local_types.h" #include "crypt_sm2.h" #include "sm2_local.h" /* GM/T003_2012 Defined Key Derive Function */ int32_t KdfGmt0032012(uint8_t *out, const uint32_t *outlen, const uint8_t *z, uint32_t zlen, const EAL_MdMethod *hashMethod) { if (out == NULL || outlen == NULL || *outlen == 0 || (z == NULL && zlen != 0) || hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t counter; uint8_t ctr[4]; uint32_t mdlen; int32_t ret; uint32_t len = MAX_MD_SIZE; void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); uint8_t dgst[MAX_MD_SIZE]; uint8_t *tmp = out; uint32_t tmplen = *outlen; if (mdCtx == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } mdlen = (uint32_t)hashMethod->mdSize; for (counter = 1;; counter++) { GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); PUT_UINT32_BE(counter, ctr, 0); GOTO_ERR_IF(hashMethod->update(mdCtx, z, zlen), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, ctr, sizeof(ctr)), ret); GOTO_ERR_IF(hashMethod->final(mdCtx, dgst, &len), ret); if (tmplen > mdlen) { (void)memcpy_s(tmp, tmplen, dgst, mdlen); tmp += mdlen; tmplen -= mdlen; } else { (void)memcpy_s(tmp, tmplen, dgst, tmplen); (void)memset_s(dgst, mdlen, 0, mdlen); break; } } ERR: hashMethod->freeCtx(mdCtx); return ret; } void Sm2CleanR(CRYPT_SM2_Ctx *ctx) { BN_Destroy(ctx->r); ctx->r = NULL; ECC_FreePoint(ctx->pointR); ctx->pointR = NULL; return; } static int32_t Sm2CalculateKey(const CRYPT_SM2_Ctx *selfCtx, const CRYPT_SM2_Ctx *peerCtx, ECC_Point *uorv, uint8_t *out, uint32_t *outlen) { uint32_t keyBits = CRYPT_SM2_GetBits(selfCtx); uint32_t elementLen = (keyBits + 7) / 8; // Multiply keyBits by 8. Add 7 to round up the result. int32_t ret; uint32_t bufLen = elementLen * 2 + SM3_MD_SIZE * 2 + 1; /* add 1 byte tag; 2: 2 coordinates x and y, 2 z values */ uint32_t dataLen = 0; // length of actual data; uint32_t curLen = 0; // length of buffer reserved for the current operation. uint8_t *buf = (uint8_t *)BSL_SAL_Calloc(bufLen, sizeof(uint8_t)); if (buf == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } /* 1 : Get public key for uorv, Notice: the first byte is a tag, not a valid char */ curLen = elementLen * 2 + 1; // add 1 byte tag; 2: 2 coordinates x and y GOTO_ERR_IF(ECC_EncodePoint(selfCtx->pkey->para, uorv, buf, &curLen, CRYPT_POINT_UNCOMPRESSED), ret); dataLen += curLen; if (selfCtx->server == 1) { /* SIDE A, Z_A || Z_B, server is initiator(Z_A), client is responder(Z_B) */ curLen = SM3_MD_SIZE; GOTO_ERR_IF_EX(Sm2ComputeZDigest(selfCtx, buf + dataLen, &curLen), ret); dataLen += curLen; } /* Caculate Peer z */ curLen = SM3_MD_SIZE; GOTO_ERR_IF_EX(Sm2ComputeZDigest(peerCtx, buf + dataLen, &curLen), ret); dataLen += curLen; if (selfCtx->server == 0) { /* SIDE B */ curLen = SM3_MD_SIZE; GOTO_ERR_IF_EX(Sm2ComputeZDigest(selfCtx, buf + dataLen, &curLen), ret); dataLen += curLen; } GOTO_ERR_IF(KdfGmt0032012(out, outlen, (const uint8_t *)(buf + 1), dataLen - 1, selfCtx->hashMethod), ret); ERR: BSL_SAL_FREE(buf); return ret; } static int32_t IsParamValid(const CRYPT_SM2_Ctx *selfCtx, const CRYPT_SM2_Ctx *peerCtx) { if (selfCtx->pkey->prvkey == NULL || peerCtx->pkey->pubkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_EMPTY_KEY); return CRYPT_SM2_ERR_EMPTY_KEY; } if (selfCtx->hashMethod == NULL || peerCtx->hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_NO_HASH_METHOD); return CRYPT_SM2_ERR_NO_HASH_METHOD; } if (peerCtx->pointR == NULL || selfCtx->r == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_R_NOT_SET); return CRYPT_SM2_R_NOT_SET; } if (selfCtx->pkey->pubkey == NULL) { int32_t ret = ECC_GenPublicKey(selfCtx->pkey); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } return CRYPT_SUCCESS; } void BnMemDestroy(BN_BigNum *xs, BN_BigNum *xp, BN_BigNum *t, BN_BigNum *twoPowerW, BN_BigNum *order) { BN_Destroy(xs); BN_Destroy(xp); BN_Destroy(t); BN_Destroy(twoPowerW); BN_Destroy(order); } static int32_t Sm3MsgHash(const EAL_MdMethod *hashMethod, const uint8_t *yBuf, const uint8_t *hashBuf, uint8_t *out, uint32_t *outlen, uint8_t tag) { int32_t ret; void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); return ret; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, &tag, 1), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, yBuf, SM3_MD_SIZE), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, hashBuf, SM3_MD_SIZE), ret); GOTO_ERR_IF(hashMethod->final(mdCtx, out, outlen), ret); ERR: hashMethod->freeCtx(mdCtx); return ret; } static int32_t Sm3InnerHash(const EAL_MdMethod *hashMethod, const uint8_t *coordinate, const uint8_t *zBuf, uint32_t zlen, const uint8_t *rBuf, uint8_t *out, uint32_t *outlen) { int32_t ret; void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); return ret; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, coordinate, SM2_X_LEN), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, zBuf, zlen), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, rBuf, SM2_TWO_POINT_COORDINATE_LEN), ret); GOTO_ERR_IF(hashMethod->final(mdCtx, out, outlen), ret); ERR: hashMethod->freeCtx(mdCtx); return ret; } int32_t Sm2KapFinalCheck(CRYPT_SM2_Ctx *sCtx, CRYPT_SM2_Ctx *pCtx, ECC_Point *uorv) { int32_t ret; uint32_t len = SM3_MD_SIZE; uint8_t r1Buf[SM2_POINT_COORDINATE_LEN]; uint8_t r2Buf[SM2_POINT_COORDINATE_LEN]; uint8_t rBuf[SM2_TWO_POINT_COORDINATE_LEN]; uint8_t xBuf[SM2_X_LEN]; uint8_t yBuf[SM2_X_LEN]; uint8_t zBuf[SM2_POINT_COORDINATE_LEN - 1]; uint8_t stmpBuf[SM3_MD_SIZE]; uint32_t buflen = SM2_POINT_COORDINATE_LEN; uint32_t zlen = 0; uint8_t tag1 = 0x03; uint8_t tag2 = 0x02; // Xv GOTO_ERR_IF(ECC_EncodePoint(sCtx->pkey->para, uorv, r1Buf, &buflen, CRYPT_POINT_UNCOMPRESSED), ret); (void)memcpy_s(xBuf, SM2_X_LEN, r1Buf + 1, SM2_X_LEN); (void)memcpy_s(yBuf, SM2_X_LEN, r1Buf + 1 + SM2_X_LEN, SM2_X_LEN); // Calculate ZA || ZB if (sCtx->server == 1) { /* SIDE A, Z_A || Z_B, server is initiator(Z_A), client is responder(Z_B) */ GOTO_ERR_IF_EX(Sm2ComputeZDigest(sCtx, zBuf, &len), ret); zlen += len; GOTO_ERR_IF(ECC_EncodePoint(sCtx->pkey->para, sCtx->pointR, r1Buf, &buflen, CRYPT_POINT_UNCOMPRESSED), ret); GOTO_ERR_IF(ECC_EncodePoint(sCtx->pkey->para, pCtx->pointR, r2Buf, &buflen, CRYPT_POINT_UNCOMPRESSED), ret); } /* Calculate Peer z */ GOTO_ERR_IF_EX(Sm2ComputeZDigest(pCtx, zBuf + zlen, &len), ret); zlen += len; if (sCtx->server == 0) { /* SIDE B */ GOTO_ERR_IF_EX(Sm2ComputeZDigest(sCtx, zBuf + zlen, &len), ret); zlen += len; GOTO_ERR_IF(ECC_EncodePoint(sCtx->pkey->para, pCtx->pointR, r1Buf, &buflen, CRYPT_POINT_UNCOMPRESSED), ret); GOTO_ERR_IF(ECC_EncodePoint(sCtx->pkey->para, sCtx->pointR, r2Buf, &buflen, CRYPT_POINT_UNCOMPRESSED), ret); tag1 = 0x02; tag2 = 0x03; } (void)memcpy_s(rBuf, SM2_TWO_POINT_COORDINATE_LEN, r1Buf + 1, SM2_POINT_COORDINATE_LEN - 1); (void)memcpy_s(rBuf + SM2_POINT_COORDINATE_LEN - 1, SM2_TWO_POINT_COORDINATE_LEN - SM2_POINT_COORDINATE_LEN + 1, r2Buf + 1, SM2_POINT_COORDINATE_LEN - 1); // Calculate the hash value. GOTO_ERR_IF_EX(Sm3InnerHash(sCtx->hashMethod, xBuf, zBuf, zlen, rBuf, stmpBuf, &len), ret); // Calculate the hash value sent to the peer end. GOTO_ERR_IF_EX(Sm3MsgHash(sCtx->hashMethod, yBuf, stmpBuf, sCtx->sumSend, &len, tag1), ret); // Computes the hash value for validation GOTO_ERR_IF_EX(Sm3MsgHash(sCtx->hashMethod, yBuf, stmpBuf, sCtx->sumCheck, &len, tag2), ret); sCtx->isSumValid = 1; return ret; ERR: sCtx->isSumValid = 0; // Reset checksum validity flag return ret; } static int SM2_PKG_Kdf(const CRYPT_SM2_Ctx *ctx, uint8_t *in, const uint32_t inLen, uint8_t *out, uint32_t *outLen) { int32_t ret; const uint32_t shareKeyLen = 16; const EAL_MdMethod *hashMethod = ctx->hashMethod; uint8_t *tmp = BSL_SAL_Malloc(hashMethod->mdSize); uint32_t tmpLen = hashMethod->mdSize; void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL || tmp == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, in, inLen), ret); GOTO_ERR_IF(hashMethod->final(mdCtx, tmp, &tmpLen), ret); if (memcpy_s(out, *outLen, tmp, shareKeyLen) != EOK) { ret = CRYPT_SECUREC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } *outLen = shareKeyLen; ERR: hashMethod->freeCtx(mdCtx); BSL_SAL_ClearFree(tmp, hashMethod->mdSize); return ret; } static int32_t SM2_PKGComputeKey(const CRYPT_SM2_Ctx *selfCtx, const CRYPT_SM2_Ctx *peerCtx, uint8_t *out, uint32_t *outlen) { if (selfCtx->pkey == NULL || peerCtx->pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (selfCtx->hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_NO_HASH_METHOD); return CRYPT_SM2_ERR_NO_HASH_METHOD; } int32_t ret; uint8_t sharePointCode[65] = {0}; uint32_t codeLen = sizeof(sharePointCode); const ECC_Pkey *eccPkey = selfCtx->pkey; BN_BigNum *tmpPrvkey = BN_Dup(eccPkey->prvkey); ECC_Point *sharePoint = ECC_NewPoint(eccPkey->para); if ((tmpPrvkey == NULL) || (sharePoint == NULL)) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(ECC_PointMul(eccPkey->para, sharePoint, eccPkey->prvkey, peerCtx->pkey->pubkey), ret); GOTO_ERR_IF(ECC_PointCheck(sharePoint), ret); GOTO_ERR_IF_EX(ECC_EncodePoint(eccPkey->para, sharePoint, sharePointCode, &codeLen, CRYPT_POINT_UNCOMPRESSED), ret); GOTO_ERR_IF_EX(SM2_PKG_Kdf(selfCtx, sharePointCode + 1, codeLen - 1, out, outlen), ret); ERR: BN_Destroy(tmpPrvkey); ECC_FreePoint(sharePoint); return ret; } int32_t CRYPT_SM2_KapComputeKey(const CRYPT_SM2_Ctx *selfCtx, const CRYPT_SM2_Ctx *peerCtx, uint8_t *out, uint32_t *outlen) { if (selfCtx == NULL || peerCtx == NULL || out == NULL || outlen == NULL || *outlen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (selfCtx->pkgImpl != 0) { return SM2_PKGComputeKey(selfCtx, peerCtx, out, outlen); } ECC_Point *uorv = ECC_NewPoint(selfCtx->pkey->para); uint32_t keyBits = CRYPT_SM2_GetBits(selfCtx); BN_BigNum *xs = BN_Create(keyBits); BN_BigNum *xp = BN_Create(keyBits); BN_BigNum *t = BN_Create(keyBits); BN_BigNum *twoPowerW = BN_Create(keyBits); BN_BigNum *order = ECC_GetParaN(selfCtx->pkey->para); uint32_t w; int32_t ret; BN_Optimizer *opt = BN_OptimizerCreate(); if (uorv == NULL || xs == NULL || xp == NULL || t == NULL || twoPowerW == NULL || order == NULL || opt == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(IsParamValid(selfCtx, peerCtx), ret); /* Second: Caculate -- w */ // w is equal to the number of digits of n rounded up, divided by 2, and then subtracted by 1. w = (BN_Bits(order) + 1) / 2 - 1; GOTO_ERR_IF(BN_Zeroize(twoPowerW), ret); GOTO_ERR_IF(BN_SetBit(twoPowerW, w), ret); /* Third: Caculate -- X = 2 ^ w + (x & (2 ^ w - 1)) = 2 ^ w + (x mod 2 ^ w) */ /* Get x */ GOTO_ERR_IF(ECC_GetPointDataX(selfCtx->pkey->para, selfCtx->pointR, xs), ret); GOTO_ERR_IF(ECC_GetPointDataX(peerCtx->pkey->para, peerCtx->pointR, xp), ret); /* x mod 2 ^ w */ /* Caculate Self x */ GOTO_ERR_IF(BN_Mod(xs, xs, twoPowerW, opt), ret); GOTO_ERR_IF(BN_Add(xs, xs, twoPowerW), ret); /* Caculate Peer x */ GOTO_ERR_IF(BN_Mod(xp, xp, twoPowerW, opt), ret); GOTO_ERR_IF(BN_Add(xp, xp, twoPowerW), ret); /* Forth: Caculate t */ GOTO_ERR_IF(BN_ModMul(t, xs, selfCtx->r, order, opt), ret); GOTO_ERR_IF(BN_ModAddQuick(t, t, selfCtx->pkey->prvkey, order, opt), ret); /* Fifth: Caculate V or U */ GOTO_ERR_IF(ECC_PointMul(peerCtx->pkey->para, uorv, xp, peerCtx->pointR), ret); /* P + [x]R */ GOTO_ERR_IF(ECC_PointAddAffine(selfCtx->pkey->para, uorv, uorv, peerCtx->pkey->pubkey), ret); GOTO_ERR_IF(ECC_PointMul(selfCtx->pkey->para, uorv, t, uorv), ret); /* Detect uorv is in */ GOTO_ERR_IF(ECC_PointCheck(uorv), ret); /* Sixth: Caculate Key -- Need Xuorv, Yuorv, Zc, Zs, klen */ GOTO_ERR_IF_EX(Sm2CalculateKey(selfCtx, peerCtx, uorv, out, outlen), ret); GOTO_ERR_IF_EX(Sm2KapFinalCheck((CRYPT_SM2_Ctx *)(uintptr_t)selfCtx, (CRYPT_SM2_Ctx *)(uintptr_t)peerCtx, uorv), ret); ERR: BnMemDestroy(xs, xp, t, twoPowerW, order); ECC_FreePoint(uorv); Sm2CleanR((CRYPT_SM2_Ctx *)(uintptr_t)selfCtx); BN_OptimizerDestroy(opt); return ret; } #endif
2301_79861745/bench_create
crypto/sm2/src/sm2_exch.c
C
unknown
14,731
/* * 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 SM2_LOCAL_H #define SM2_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM2 #include <stdint.h> #include "crypt_sm2.h" #include "crypt_local_types.h" #include "crypt_ecc_pkey.h" #include "sal_atomic.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define SM2_MAX_ID_BITS 65535 #define SM2_MAX_ID_LENGTH (SM2_MAX_ID_BITS / 8) #define SM2_MAX_PUBKEY_DATA_LENGTH 256 #define MAX_MD_SIZE 64 #define SM3_MD_SIZE 32 #define SM2_POINT_SINGLE_COORDINATE_LEN 32 #define SM2_POINT_COORDINATE_LEN 65 #define SM2_TWO_POINT_COORDINATE_LEN 128 #define SM2_X_LEN 32 #ifdef HITLS_CRYPTO_ACVP_TESTS typedef struct { BN_BigNum *k; // random k } SM2_ParaEx; #endif /* SM2 key context */ struct SM2_Ctx { ECC_Pkey *pkey; uint32_t pkgImpl; ECC_Point *pointR; // Local R const EAL_MdMethod *hashMethod; BN_BigNum *r; // Local r uint8_t *userId; // User ID uint32_t userIdLen; // the length of User ID int32_t server; // 1: the initiator, 0: the receiver, and the default value is 1. uint8_t sumCheck[SM3_MD_SIZE]; // Hash value used as a check uint8_t sumSend[SM3_MD_SIZE]; // Hash value sent to the peer end uint8_t isSumValid; // Indicates whether the checksum is valid. 1: valid; 0: invalid. BSL_SAL_RefCount references; #ifdef HITLS_CRYPTO_ACVP_TESTS SM2_ParaEx paraEx; #endif }; /** * @ingroup sm2 * @brief The sm2 invokes the SM3 to calculate the hash value. * * @param ctx [IN] sm2 context structure * @param out [IN/OUT] Hash value * @param outLen [IN/OUT] Length of the hash value * * @retval CRYPT_SUCCESS calculated successfully. * @retval Other: The calculation fails. For details about the return value type, see crypt_errno.h. */ int32_t Sm2ComputeZDigest(const CRYPT_SM2_Ctx *ctx, uint8_t *out, uint32_t *outLen); #if defined(HITLS_CRYPTO_SM2_EXCH) || defined(HITLS_CRYPTO_SM2_CRYPT) /** * @ingroup sm2 * @brief sm2 kdf function * * @param out [IN/OUT] Calculation result * @param outlen [IN/OUT] Output data length * @param z [IN] Input data * @param zlen [IN] Length of the input data * @param hashMethod [IN] hash method * * @retval CRYPT_SUCCESS calculated successfully. * @retval Other: The calculation fails. For details about the return value type, see crypt_errno.h. */ int32_t KdfGmt0032012(uint8_t *out, const uint32_t *outlen, const uint8_t *z, uint32_t zlen, const EAL_MdMethod *hashMethod); #ifdef HITLS_CRYPTO_ACVP_TESTS /** * @ingroup sm2 * @brief set random k for the sm2 context * * @param ctx [IN] Source SM2 context * @param para [IN] random k */ int32_t CRYPT_SM2_SetK(CRYPT_SM2_Ctx *ctx, uint8_t *val, uint32_t len); #endif #endif #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_SM2 #endif // SM2_LOCAL_H
2301_79861745/bench_create
crypto/sm2/src/sm2_local.h
C
unknown
3,307
/* * 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_SM2 #include <stdbool.h> #include "crypt_errno.h" #include "crypt_types.h" #include "crypt_utils.h" #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_bn.h" #include "crypt_encode_internal.h" #include "crypt_ecc.h" #include "crypt_ecc_pkey.h" #include "crypt_local_types.h" #include "crypt_sm2.h" #include "sm2_local.h" #include "eal_md_local.h" #include "crypt_params_key.h" static int32_t Sm2SetUserId(CRYPT_SM2_Ctx *ctx, const uint8_t *val, uint32_t len) { ctx->userId = BSL_SAL_Calloc(len, 1u); if (ctx->userId == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } (void) memcpy_s(ctx->userId, len, val, len); ctx->userIdLen = len; return CRYPT_SUCCESS; } CRYPT_SM2_Ctx *CRYPT_SM2_NewCtx(void) { CRYPT_SM2_Ctx *ctx = BSL_SAL_Calloc(1u, sizeof(CRYPT_SM2_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } ctx->pkey = ECC_PkeyNewCtx(CRYPT_ECC_SM2); if (ctx->pkey == NULL) { CRYPT_SM2_FreeCtx(ctx); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } const EAL_MdMethod *mdMethod = EAL_MdFindDefaultMethod(CRYPT_MD_SM3); if (mdMethod == NULL) { CRYPT_SM2_FreeCtx(ctx); BSL_ERR_PUSH_ERROR(CRYPT_EVENT_ERR); return NULL; } ctx->hashMethod = (const EAL_MdMethod *)mdMethod; ctx->server = 1; // Indicates the initiator by default. ctx->isSumValid = 0; // checksum is invalid by default. BSL_SAL_ReferencesInit(&(ctx->references)); return ctx; } CRYPT_SM2_Ctx *CRYPT_SM2_NewCtxEx(void *libCtx) { CRYPT_SM2_Ctx *ctx = CRYPT_SM2_NewCtx(); if (ctx == NULL) { return NULL; } ctx->pkey->libCtx = libCtx; ECC_SetLibCtx(ctx->pkey->libCtx, ctx->pkey->para); return ctx; } CRYPT_SM2_Ctx *CRYPT_SM2_DupCtx(CRYPT_SM2_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_SM2_Ctx *newCtx = BSL_SAL_Calloc(1u, sizeof(CRYPT_SM2_Ctx)); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } GOTO_ERR_IF_SRC_NOT_NULL(newCtx->pkey, ctx->pkey, ECC_DupCtx(ctx->pkey), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newCtx->pointR, ctx->pointR, ECC_DupPoint(ctx->pointR), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newCtx->r, ctx->r, BN_Dup(ctx->r), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newCtx->userId, ctx->userId, BSL_SAL_Dump(ctx->userId, ctx->userIdLen), CRYPT_MEM_ALLOC_FAIL); newCtx->userIdLen = ctx->userIdLen; newCtx->pkgImpl = ctx->pkgImpl; newCtx->hashMethod = ctx->hashMethod; newCtx->server = ctx->server; newCtx->isSumValid = ctx->isSumValid; BSL_SAL_ReferencesInit(&(newCtx->references)); (void)memcpy_s(newCtx->sumCheck, SM3_MD_SIZE, ctx->sumCheck, SM3_MD_SIZE); (void)memcpy_s(newCtx->sumSend, SM3_MD_SIZE, ctx->sumSend, SM3_MD_SIZE); return newCtx; ERR: CRYPT_SM2_FreeCtx(newCtx); return NULL; } void CRYPT_SM2_FreeCtx(CRYPT_SM2_Ctx *ctx) { int val = 0; if (ctx == NULL) { return; } BSL_SAL_AtomicDownReferences(&(ctx->references), &val); if (val > 0) { return; } BSL_SAL_ReferencesFree(&(ctx->references)); ECC_FreeCtx(ctx->pkey); BSL_SAL_FREE(ctx->userId); BN_Destroy(ctx->r); ECC_FreePoint(ctx->pointR); #ifdef HITLS_CRYPTO_ACVP_TESTS BN_Destroy(ctx->paraEx.k); #endif BSL_SAL_FREE(ctx); return; } int32_t Sm2ComputeZDigest(const CRYPT_SM2_Ctx *ctx, uint8_t *out, uint32_t *outLen) { int32_t ret; if (ctx->userIdLen >= (UINT16_MAX / 8)) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ID_TOO_LARGE); return CRYPT_SM2_ID_TOO_LARGE; } /* 2-byte id length in bits */ uint16_t entl = (uint16_t)(8 * ctx->userIdLen); uint8_t eByte = (uint8_t)(entl >> 8); uint8_t maxPubData[SM2_MAX_PUBKEY_DATA_LENGTH] = {0}; CRYPT_Sm2Pub pub = {maxPubData, SM2_MAX_PUBKEY_DATA_LENGTH}; uint32_t keyBits = CRYPT_SM2_GetBits(ctx); BN_BigNum *a = ECC_GetParaA(ctx->pkey->para); BN_BigNum *b = ECC_GetParaB(ctx->pkey->para); BN_BigNum *xG = ECC_GetParaX(ctx->pkey->para); BN_BigNum *yG = ECC_GetParaY(ctx->pkey->para); void *mdCtx = ctx->hashMethod->newCtx(NULL, ctx->hashMethod->id); uint8_t *buf = BSL_SAL_Calloc(1u, keyBits); if (a == NULL || b == NULL || xG == NULL || yG == NULL || buf == NULL || mdCtx == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } BSL_Param tmpPara[2] = {{CRYPT_PARAM_EC_PUBKEY, BSL_PARAM_TYPE_OCTETS, maxPubData, SM2_MAX_PUBKEY_DATA_LENGTH, 0}, BSL_PARAM_END}; GOTO_ERR_IF(CRYPT_SM2_GetPubKeyEx(ctx, tmpPara), ret); pub.len = tmpPara[0].useLen; GOTO_ERR_IF(ctx->hashMethod->init(mdCtx, NULL), ret); // User A has a distinguishable identifier IDA with a length of entlenA bits, // and ENTLA is two bytes converted from an integer entlenA // H256(ENTLA || IDA || a || b || xG || yG || xA || yA) GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, &eByte, 1), ret); // ENTLA eByte = entl & 0xFF; GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, &eByte, 1), ret); // ENTLA if (ctx->userIdLen > 0) { GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, ctx->userId, ctx->userIdLen), ret); // IDA } GOTO_ERR_IF_EX(BN_Bn2Bin(a, buf, &keyBits), ret); GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, buf, keyBits), ret); // a GOTO_ERR_IF_EX(BN_Bn2Bin(b, buf, &keyBits), ret); GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, buf, keyBits), ret); // b GOTO_ERR_IF_EX(BN_Bn2Bin(xG, buf, &keyBits), ret); GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, buf, keyBits), ret); // xG keyBits = CRYPT_SM2_GetBits(ctx); GOTO_ERR_IF_EX(BN_Bn2Bin(yG, buf, &keyBits), ret); GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, buf, keyBits), ret); // yG GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, pub.data + 1, pub.len - 1), ret); // xA and yA GOTO_ERR_IF(ctx->hashMethod->final(mdCtx, out, outLen), ret); ERR: ctx->hashMethod->freeCtx(mdCtx); BN_Destroy(a); BN_Destroy(b); BN_Destroy(xG); BN_Destroy(yG); BSL_SAL_FREE(buf); return ret; } #ifdef HITLS_CRYPTO_SM2_SIGN static int32_t Sm2ComputeMsgHash(const CRYPT_SM2_Ctx *ctx, const uint8_t *msg, uint32_t msgLen, BN_BigNum *e) { int ret; uint8_t out[SM3_MD_SIZE]; uint32_t outLen = sizeof(out); void *mdCtx = ctx->hashMethod->newCtx(NULL, ctx->hashMethod->id); if (mdCtx == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF_EX(Sm2ComputeZDigest(ctx, out, &outLen), ret); GOTO_ERR_IF(ctx->hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, out, outLen), ret); GOTO_ERR_IF(ctx->hashMethod->update(mdCtx, msg, msgLen), ret); GOTO_ERR_IF(ctx->hashMethod->final(mdCtx, out, &outLen), ret); GOTO_ERR_IF_EX(BN_Bin2Bn(e, out, outLen), ret); ERR: ctx->hashMethod->freeCtx(mdCtx); return ret; } #endif uint32_t CRYPT_SM2_GetBits(const CRYPT_SM2_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return 0; } return ECC_PkeyGetBits(ctx->pkey); } int32_t CRYPT_SM2_SetPrvKey(CRYPT_SM2_Ctx *ctx, const CRYPT_Sm2Prv *prv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeySetPrvKey(ctx->pkey, prv); } int32_t CRYPT_SM2_SetPubKey(CRYPT_SM2_Ctx *ctx, const CRYPT_Sm2Pub *pub) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeySetPubKey(ctx->pkey, pub); } int32_t CRYPT_SM2_GetPrvKey(const CRYPT_SM2_Ctx *ctx, CRYPT_Sm2Prv *prv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeyGetPrvKey(ctx->pkey, prv); } int32_t CRYPT_SM2_GetPubKey(const CRYPT_SM2_Ctx *ctx, CRYPT_Sm2Pub *pub) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeyGetPubKey(ctx->pkey, pub); } #ifdef HITLS_BSL_PARAMS int32_t CRYPT_SM2_SetPrvKeyEx(CRYPT_SM2_Ctx *ctx, const BSL_Param *para) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeySetPrvKeyEx(ctx->pkey, para); } int32_t CRYPT_SM2_SetPubKeyEx(CRYPT_SM2_Ctx *ctx, const BSL_Param *para) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeySetPubKeyEx(ctx->pkey, para); } int32_t CRYPT_SM2_GetPrvKeyEx(const CRYPT_SM2_Ctx *ctx, BSL_Param *para) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeyGetPrvKeyEx(ctx->pkey, para); } int32_t CRYPT_SM2_GetPubKeyEx(const CRYPT_SM2_Ctx *ctx, BSL_Param *para) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeyGetPubKeyEx(ctx->pkey, para); } #endif int32_t CRYPT_SM2_Gen(CRYPT_SM2_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeyGen(ctx->pkey); } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SM2_Import(CRYPT_SM2_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; const BSL_Param *prv = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_EC_PRVKEY); const BSL_Param *pub = BSL_PARAM_FindConstParam(params, CRYPT_PARAM_EC_PUBKEY); if (prv != NULL) { ret = CRYPT_SM2_SetPrvKeyEx(ctx, prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } if (pub != NULL) { ret = CRYPT_SM2_SetPubKeyEx(ctx, pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } return CRYPT_SUCCESS; } int32_t CRYPT_SM2_Export(const CRYPT_SM2_Ctx *ctx, BSL_Param *params) { if (ctx == NULL || params == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t index = 0; uint32_t keyBytes = (CRYPT_SM2_GetBits(ctx) + 7) / 8; CRYPT_EAL_ProcessFuncCb processCb = NULL; void *args = NULL; BSL_Param sm2Params[3] = {0}; 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, keyBytes * 2); if (buffer == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (ctx->pkey->prvkey != NULL) { (void)BSL_PARAM_InitValue(&sm2Params[index], CRYPT_PARAM_EC_PRVKEY, BSL_PARAM_TYPE_OCTETS, buffer, keyBytes); ret = CRYPT_SM2_GetPrvKeyEx(ctx, sm2Params); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(buffer); BSL_ERR_PUSH_ERROR(ret); return ret; } sm2Params[index].valueLen = sm2Params[index].useLen; index++; } if (ctx->pkey->pubkey != NULL) { (void)BSL_PARAM_InitValue(&sm2Params[index], CRYPT_PARAM_EC_PUBKEY, BSL_PARAM_TYPE_OCTETS, buffer, keyBytes); ret = CRYPT_SM2_GetPubKeyEx(ctx, sm2Params); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(buffer); BSL_ERR_PUSH_ERROR(ret); return ret; } sm2Params[index].valueLen = sm2Params[index].useLen; index++; } ret = processCb(sm2Params, args); BSL_SAL_Free(buffer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } #endif #ifdef HITLS_CRYPTO_SM2_SIGN uint32_t CRYPT_SM2_GetSignLen(const CRYPT_SM2_Ctx *ctx) { if (ctx == NULL || ctx->pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return 0; } uint32_t qLen = (ECC_ParaBits(ctx->pkey->para) / 8) + 1; uint32_t maxSignLen = 0; int32_t ret = CRYPT_EAL_GetSignEncodeLen(qLen, qLen, &maxSignLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return 0; } return maxSignLen; } static int32_t Sm2SignCore(const CRYPT_SM2_Ctx *ctx, BN_BigNum *e, BN_BigNum *r, BN_BigNum *s) { uint32_t keyBits = CRYPT_SM2_GetBits(ctx); BN_BigNum *k = BN_Create(keyBits); BN_BigNum *tmp = BN_Create(keyBits); // An extra bit is allocated to prevent the number of bits in the result of adding BNs from exceeding the keybits. BN_BigNum *t = BN_Create(keyBits + 1); BN_BigNum *paraN = ECC_GetParaN(ctx->pkey->para); ECC_Point *pt = ECC_NewPoint(ctx->pkey->para); BN_Optimizer *opt = BN_OptimizerCreate(); int32_t ret, i; if ((k == NULL) || (tmp == NULL) || (t == NULL) || (pt == NULL) || (paraN == NULL) || (opt == NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto ERR; } for (i = 0; i < CRYPT_ECC_TRY_MAX_CNT; i++) { GOTO_ERR_IF(BN_RandRangeEx(ctx->pkey->libCtx, k, paraN), ret); if (BN_IsZero(k)) { continue; } // pt = k * G GOTO_ERR_IF(ECC_PointMul(ctx->pkey->para, pt, k, NULL), ret); // r = (e + pt->x) mod n GOTO_ERR_IF(ECC_GetPointDataX(ctx->pkey->para, pt, tmp), ret); GOTO_ERR_IF(BN_ModAdd(r, e, tmp, paraN, opt), ret); // if r == 0 || r + k == n, then restart GOTO_ERR_IF(BN_Add(t, r, k), ret); if (BN_IsZero(r) || BN_Cmp(t, paraN) == 0) { continue; } // prvkey * r mod n == (r * dA) mod n GOTO_ERR_IF(BN_ModMul(s, ctx->pkey->prvkey, r, paraN, opt), ret); // k - prvkey * r mod n GOTO_ERR_IF(BN_ModSub(s, k, s, paraN, opt), ret); // 1/(1 + d) mod n, tmp stores 1/(1 + d) GOTO_ERR_IF(BN_AddLimb(t, ctx->pkey->prvkey, 1), ret); GOTO_ERR_IF(ECC_ModOrderInv(ctx->pkey->para, tmp, t), ret); // s = (1/(1+d)) * (k - prvkey * r) mod n GOTO_ERR_IF(BN_ModMul(s, tmp, s, paraN, opt), ret); // if s == 0, then restart if (BN_IsZero(s) != true) { break; } } if (i >= CRYPT_ECC_TRY_MAX_CNT) { ret = CRYPT_SM2_ERR_TRY_CNT; BSL_ERR_PUSH_ERROR(ret); } ERR: BN_Destroy(k); BN_Destroy(tmp); BN_Destroy(t); BN_Destroy(paraN); ECC_FreePoint(pt); BN_OptimizerDestroy(opt); return ret; } int32_t KeyCheckAndPubGen(const CRYPT_SM2_Ctx *ctx) { int32_t ret; if (ctx->pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_EMPTY_KEY); return CRYPT_SM2_ERR_EMPTY_KEY; } if (ctx->pkey->prvkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_NO_PRVKEY); return CRYPT_SM2_NO_PRVKEY; } if (ctx->pkey->pubkey != NULL) { return CRYPT_SUCCESS; } ret = ECC_GenPublicKey(ctx->pkey); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t CRYPT_SM2_Sign(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { int32_t ret; if (algId != CRYPT_MD_SM3) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID); return CRYPT_EAL_ERR_ALGID; } if ((ctx == NULL) || (sign == NULL) || (signLen == NULL) || ((data == NULL) && (dataLen != 0))) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = KeyCheckAndPubGen(ctx); if (ret != CRYPT_SUCCESS) { return ret; } if (ctx->hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_NO_HASH_METHOD); return CRYPT_SM2_ERR_NO_HASH_METHOD; } if (*signLen < CRYPT_SM2_GetSignLen(ctx)) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_BUFF_LEN_NOT_ENOUGH); return CRYPT_SM2_BUFF_LEN_NOT_ENOUGH; } uint32_t keyBits = CRYPT_SM2_GetBits(ctx); BN_BigNum *r = BN_Create(keyBits); BN_BigNum *s = BN_Create(keyBits); BN_BigNum *d = BN_Create(keyBits); if (r == NULL || s == NULL || d == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF_EX(Sm2ComputeMsgHash(ctx, data, dataLen, d), ret); GOTO_ERR_IF_EX(Sm2SignCore(ctx, d, r, s), ret); ret = CRYPT_EAL_EncodeSign(r, s, sign, signLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } ERR: BN_Destroy(r); BN_Destroy(s); BN_Destroy(d); return ret; } static int32_t VerifyCheckSign(const CRYPT_SM2_Ctx *ctx, BN_BigNum *r, BN_BigNum *s) { if (ctx->pkey->para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BN_BigNum *paraN = ECC_GetParaN(ctx->pkey->para); if (paraN == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if ((BN_Cmp(r, paraN) >= 0) || (BN_Cmp(s, paraN) >= 0)) { BN_Destroy(paraN); BSL_ERR_PUSH_ERROR(CRYPT_SM2_VERIFY_FAIL); return CRYPT_SM2_VERIFY_FAIL; } BN_Destroy(paraN); if (BN_IsZero(r) || BN_IsZero(s)) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_VERIFY_FAIL); return CRYPT_SM2_VERIFY_FAIL; } return CRYPT_SUCCESS; } static int32_t Sm2VerifyCore(const CRYPT_SM2_Ctx *ctx, BN_BigNum *e, const BN_BigNum *r, const BN_BigNum *s) { uint32_t keyBits = CRYPT_SM2_GetBits(ctx); BN_BigNum *t = BN_Create(keyBits); ECC_Point *tpt = ECC_NewPoint(ctx->pkey->para); BN_BigNum *tptX = BN_Create(keyBits); BN_Optimizer *opt = BN_OptimizerCreate(); BN_BigNum *paraN = ECC_GetParaN(ctx->pkey->para); int32_t ret; if ((t == NULL) || (tpt == NULL) || (tptX == NULL) || (paraN == NULL) || (opt == NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto ERR; } // B5: calculate t = (r' + s') modn, verification failed if t=0 GOTO_ERR_IF_EX(BN_ModAddQuick(t, r, s, paraN, opt), ret); if (BN_IsZero(t)) { ret = CRYPT_SM2_VERIFY_FAIL; BSL_ERR_PUSH_ERROR(ret); } // calculate the point (x1', y1')=[s']G + [t]PA GOTO_ERR_IF(ECC_PointMulAdd(ctx->pkey->para, tpt, s, t, ctx->pkey->pubkey), ret); GOTO_ERR_IF_EX(ECC_GetPointDataX(ctx->pkey->para, tpt, tptX), ret); // calculate R=(e'+x1') modn, verification pass if yes, otherwise failed GOTO_ERR_IF_EX(BN_ModAdd(t, e, tptX, paraN, opt), ret); if (BN_Cmp(r, t) != 0) { ret = CRYPT_SM2_VERIFY_FAIL; BSL_ERR_PUSH_ERROR(ret); } ERR: BN_Destroy(t); BN_Destroy(paraN); ECC_FreePoint(tpt); BN_Destroy(tptX); BN_OptimizerDestroy(opt); return ret; } static int32_t IsParaVaild(const CRYPT_SM2_Ctx *ctx) { if (ctx->pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_EMPTY_KEY); return CRYPT_SM2_ERR_EMPTY_KEY; } if (ctx->pkey->pubkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_NO_PUBKEY); return CRYPT_SM2_NO_PUBKEY; } if (ctx->hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_NO_HASH_METHOD); return CRYPT_SM2_ERR_NO_HASH_METHOD; } return CRYPT_SUCCESS; } int32_t CRYPT_SM2_Verify(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen) { if (algId != CRYPT_MD_SM3) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_ALGID); return CRYPT_EAL_ERR_ALGID; } if ((ctx == NULL) || ((data == NULL) && (dataLen != 0)) || (sign == NULL) || (signLen == 0)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t keyBits = CRYPT_SM2_GetBits(ctx); int32_t ret = IsParaVaild(ctx); if (ret != CRYPT_SUCCESS) { return ret; } BN_BigNum *r = BN_Create(keyBits); BN_BigNum *s = BN_Create(keyBits); BN_BigNum *e = BN_Create(keyBits); if (r == NULL || s == NULL || e == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF_EX(Sm2ComputeMsgHash(ctx, data, dataLen, e), ret); GOTO_ERR_IF(CRYPT_EAL_DecodeSign(sign, signLen, r, s), ret); // Verify that r->s and s->s are within the range of 1~n-1. GOTO_ERR_IF_EX(VerifyCheckSign(ctx, r, s), ret); GOTO_ERR_IF_EX(Sm2VerifyCore(ctx, e, r, s), ret); ERR: BN_Destroy(r); BN_Destroy(s); BN_Destroy(e); return ret; } #endif static void Sm2Clean(CRYPT_SM2_Ctx *ctx) { BN_Destroy(ctx->r); ctx->r = NULL; ECC_FreePoint(ctx->pointR); ctx->pointR = NULL; ctx->isSumValid = 0; return; } static int32_t Sm2GenerateR(CRYPT_SM2_Ctx *ctx, void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; Sm2Clean(ctx); uint32_t keyBits = CRYPT_SM2_GetBits(ctx); int32_t tryNum = 0; BN_BigNum *order = ECC_GetParaN(ctx->pkey->para); ctx->r = BN_Create(keyBits); ctx->pointR = ECC_NewPoint(ctx->pkey->para); BN_BigNum *tmp = BN_Create(keyBits); if (order == NULL || ctx->r == NULL || ctx->pointR == NULL || tmp == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } for (; tryNum < CRYPT_ECC_TRY_MAX_CNT; tryNum++) { GOTO_ERR_IF_EX(BN_RandRangeEx(ctx->pkey->libCtx, ctx->r, order), ret); if (!BN_IsZero(ctx->r)) { break; } } if (tryNum >= CRYPT_ECC_TRY_MAX_CNT) { ret = CRYPT_SM2_ERR_TRY_CNT; BSL_ERR_PUSH_ERROR(CRYPT_SM2_ERR_TRY_CNT); goto ERR; } GOTO_ERR_IF(ECC_PointMul(ctx->pkey->para, ctx->pointR, ctx->r, NULL), ret); GOTO_ERR_IF(ECC_GetPointDataX(ctx->pkey->para, ctx->pointR, tmp), ret); GOTO_ERR_IF(ECC_EncodePoint(ctx->pkey->para, ctx->pointR, (uint8_t *)val, &len, CRYPT_POINT_UNCOMPRESSED), ret); BN_Destroy(tmp); BN_Destroy(order); return ret; ERR: BN_Destroy(tmp); BN_Destroy(order); Sm2Clean(ctx); return ret; } static int32_t Sm2SetR(CRYPT_SM2_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; Sm2Clean(ctx); ECC_Point *rs = ECC_NewPoint(ctx->pkey->para); if (rs == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = ECC_DecodePoint(ctx->pkey->para, rs, (const uint8_t *)val, len); if (ret != CRYPT_SUCCESS) { ECC_FreePoint(rs); BSL_ERR_PUSH_ERROR(ret); return ret; } ctx->pointR = rs; return ret; } static int32_t Sm2SetRandom(CRYPT_SM2_Ctx *ctx, const void *val, uint32_t len) { int32_t ret; uint32_t keyBits = CRYPT_SM2_GetBits(ctx); BN_BigNum *order = ECC_GetParaN(ctx->pkey->para); ctx->r = BN_Create(keyBits); ctx->pointR = ECC_NewPoint(ctx->pkey->para); BN_BigNum *tmp = BN_Create(keyBits); if (order == NULL || ctx->r == NULL || ctx->pointR == NULL || tmp == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = BN_Bin2Bn(ctx->r, (const uint8_t *)val, len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(ECC_PointMul(ctx->pkey->para, ctx->pointR, ctx->r, NULL), ret); GOTO_ERR_IF(ECC_GetPointDataX(ctx->pkey->para, ctx->pointR, tmp), ret); BN_Destroy(order); BN_Destroy(tmp); return ret; ERR: BN_Destroy(order); BN_Destroy(tmp); Sm2Clean(ctx); return ret; } static int32_t Sm2GetSumSend(CRYPT_SM2_Ctx *ctx, void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; if (ctx->isSumValid != 1) { ret = CRYPT_SM2_ERR_S_NOT_SET; BSL_ERR_PUSH_ERROR(ret); return ret; } if (len != SM3_MD_SIZE) { ret = CRYPT_SM2_BUFF_LEN_NOT_ENOUGH; BSL_ERR_PUSH_ERROR(ret); return ret; } ret = memcpy_s((uint8_t *)val, len, ctx->sumSend, SM3_MD_SIZE); if (ret != EOK) { ret = CRYPT_SM2_ERR_GET_S; BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } /* consttime memcmp function */ static int32_t IsDataEqual(const uint8_t *data1, const uint8_t *data2, uint32_t len) { uint8_t check = 0; for (uint32_t i = 0; i < len; i++) { check |= data1[i] ^ data2[i]; } if (check != 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_EXCH_VERIFY_FAIL); return CRYPT_SM2_EXCH_VERIFY_FAIL; } return CRYPT_SUCCESS; } static int32_t Sm2DoCheck(CRYPT_SM2_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; if (ctx->isSumValid != 1) { ret = CRYPT_SM2_ERR_S_NOT_SET; BSL_ERR_PUSH_ERROR(ret); return ret; } if (len != SM3_MD_SIZE) { ret = CRYPT_SM2_ERR_DATA_LEN; BSL_ERR_PUSH_ERROR(ret); return ret; } ret = IsDataEqual(ctx->sumCheck, val, len); if (ret != CRYPT_SUCCESS) { ctx->isSumValid = 0; } return ret; } static int32_t CtrlServerSet(CRYPT_SM2_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_SM2_ERR_CTRL_LEN); return CRYPT_SM2_ERR_CTRL_LEN; } const int32_t t = *(const int32_t *)val; if (t != 0 && t != 1) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_INVALID_SERVER_TYPE); return CRYPT_SM2_INVALID_SERVER_TYPE; } ctx->server = t; return CRYPT_SUCCESS; } static int32_t CtrlUserId(CRYPT_SM2_Ctx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len == 0 || len > SM2_MAX_ID_LENGTH) { BSL_ERR_PUSH_ERROR(CRYPT_ECC_PKEY_ERR_CTRL_LEN); return CRYPT_ECC_PKEY_ERR_CTRL_LEN; } BSL_SAL_FREE(ctx->userId); return Sm2SetUserId(ctx, val, len); } static int32_t Sm2SetPKG(CRYPT_SM2_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_SM2_ERR_CTRL_LEN); return CRYPT_SM2_ERR_CTRL_LEN; } if (*(const uint32_t *)val != 0 && *(const uint32_t *)val != 1) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->pkgImpl = *(const uint32_t *)val; return CRYPT_SUCCESS; } static int32_t SM2UpReferences(CRYPT_SM2_Ctx *ctx, void *val, uint32_t len) { if (val == NULL || len != (uint32_t)sizeof(int)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return BSL_SAL_AtomicUpReferences(&(ctx->references), (int *)val); } static int32_t CRYPT_SM2_GetLen(const CRYPT_SM2_Ctx *ctx, GetLenFunc func, void *val, uint32_t len) { if (val == NULL || len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } *(int32_t *)val = func(ctx); return CRYPT_SUCCESS; } int32_t CRYPT_SM2_Ctrl(CRYPT_SM2_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 = CRYPT_SM2_ERR_UNSUPPORTED_CTRL_OPTION; switch (opt) { case CRYPT_CTRL_GET_BITS: return CRYPT_SM2_GetLen(ctx, (GetLenFunc)CRYPT_SM2_GetBits, val, len); #ifdef HITLS_CRYPTO_SM2_SIGN case CRYPT_CTRL_GET_SIGNLEN: return CRYPT_SM2_GetLen(ctx, (GetLenFunc)CRYPT_SM2_GetSignLen, val, len); #endif case CRYPT_CTRL_GET_SECBITS: return CRYPT_SM2_GetLen(ctx, (GetLenFunc)CRYPT_SM2_GetSecBits, val, len); case CRYPT_CTRL_SET_SM2_SERVER: ret = CtrlServerSet(ctx, val, len); break; case CRYPT_CTRL_SET_SM2_USER_ID: ret = CtrlUserId(ctx, val, len); break; case CRYPT_CTRL_GENE_SM2_R: ret = Sm2GenerateR(ctx, val, len); break; case CRYPT_CTRL_SET_SM2_R: ret = Sm2SetR(ctx, val, len); break; #ifdef HITLS_CRYPTO_ACVP_TESTS case CRYPT_CTRL_SET_SM2_K: ret = CRYPT_SM2_SetK(ctx, val, len); break; #endif case CRYPT_CTRL_SET_SM2_RANDOM: ret = Sm2SetRandom(ctx, val, len); break; case CRYPT_CTRL_GET_SM2_SEND_CHECK: ret = Sm2GetSumSend(ctx, val, len); break; case CRYPT_CTRL_SM2_DO_CHECK: ret = Sm2DoCheck(ctx, val, len); break; case CRYPT_CTRL_SET_SM2_PKG: ret = Sm2SetPKG(ctx, val, len); break; case CRYPT_CTRL_UP_REFERENCES: ret = SM2UpReferences(ctx, val, len); break; default: ret = ECC_PkeyCtrl(ctx->pkey, opt, val, len); break; } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t CRYPT_SM2_Cmp(const CRYPT_SM2_Ctx *a, const CRYPT_SM2_Ctx *b) { if (a == NULL || b == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ECC_PkeyCmp(a->pkey, b->pkey); } int32_t CRYPT_SM2_GetSecBits(const CRYPT_SM2_Ctx *ctx) { if (ctx == NULL || ctx->pkey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return 0; } return ECC_GetSecBits(ctx->pkey->para); } #ifdef HITLS_CRYPTO_SM2_CHECK int32_t CRYPT_SM2_Check(uint32_t checkType, const CRYPT_SM2_Ctx *pkey1, const CRYPT_SM2_Ctx *pkey2) { int32_t ret; switch (checkType) { case CRYPT_PKEY_CHECK_KEYPAIR: if (pkey1 == NULL || pkey2 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = ECC_PkeyCheck(pkey1->pkey, pkey2->pkey, checkType); break; case CRYPT_PKEY_CHECK_PRVKEY: if (pkey1 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = ECC_PkeyCheck(pkey1->pkey, NULL, checkType); break; default: BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ret == CRYPT_ECC_PAIRWISE_CHECK_FAIL) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_PAIRWISE_CHECK_FAIL); return CRYPT_SM2_PAIRWISE_CHECK_FAIL; } if (ret == CRYPT_ECC_INVALID_PRVKEY) { BSL_ERR_PUSH_ERROR(CRYPT_SM2_INVALID_PRVKEY); return CRYPT_SM2_INVALID_PRVKEY; } return ret; // may be other error occurred. } #endif // HITLS_CRYPTO_SM2_CHECK #endif
2301_79861745/bench_create
crypto/sm2/src/sm2_sign.c
C
unknown
31,840
/* * 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_SM3_H #define CRYPT_SM3_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM3 #include <stdint.h> #include <stdlib.h> #include "crypt_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define CRYPT_SM3_BLOCKSIZE 64 #define CRYPT_SM3_DIGESTSIZE 32 typedef struct CryptSm3Ctx CRYPT_SM3_Ctx; #define CRYPT_SM3_Squeeze NULL /** * @ingroup SM3 * @brief Generate md context. * * @retval Success: sm3 ctx. * Fails: NULL. */ CRYPT_SM3_Ctx *CRYPT_SM3_NewCtx(void); /** * @ingroup SM3 * @brief Generate md context. * * @param libCtx [IN] library context * @param algId [IN] algorithm id * * @retval Success: sm3 ctx. * Fails: NULL. */ CRYPT_SM3_Ctx *CRYPT_SM3_NewCtxEx(void *libCtx, int32_t algId); /** * @ingroup SM3 * @brief free md context. * * @param ctx [IN] md handle */ void CRYPT_SM3_FreeCtx(CRYPT_SM3_Ctx *ctx); /** * @ingroup SM3 * @brief This API is used to initialize the SM3 context. * * @param ctx [in,out] SM3 context pointer. * @param *param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS initialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SM3_Init(CRYPT_SM3_Ctx *ctx, BSL_Param *param); /** * @ingroup SM3 * @brief Encode the text and update the message digest. * * @param ctx [in,out] SM3 context pointer. * @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_SM3_INPUT_OVERFLOW Accumulated input data length exceeds the maximum (2^64 bits). */ int32_t CRYPT_SM3_Update(CRYPT_SM3_Ctx *ctx, const uint8_t *in, uint32_t len); /** * @ingroup SM3 * @brief Obtain the message digest based on the passed SM3 text. * * @param ctx [in,out] SM3 context pointer. * @param out [in] digest buffer * @param outLen [in,out] digest buffer size * * @retval #CRYPT_SUCCESS succeeded in updating the internal status of the digest. * @retval #CRYPT_NULL_INPUT The input parameter is NULL. * @retval #CRYPT_SM3_OUT_BUFF_LEN_NOT_ENOUGH The output buffer is insufficient. */ int32_t CRYPT_SM3_Final(CRYPT_SM3_Ctx *ctx, uint8_t *out, uint32_t *outLen); /** * @ingroup SM3 * @brief SM3 Deinitialization API * @param ctx [in,out] SM3 context pointer. * * @retval #CRYPT_SUCCESS Deinitialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_SM3_Deinit(CRYPT_SM3_Ctx *ctx); /** * @ingroup SM3 * @brief Copy SM3 CTX function * @param dst [out] SM3 context pointer. * @param src [in] Pointer to the original SM3 context. * * @retval #CRYPT_SUCCESS Copy succeeded. * @retval #CRYPT_NULL_INPUT Pointer src is NULL */ int32_t CRYPT_SM3_CopyCtx(CRYPT_SM3_Ctx *dst, const CRYPT_SM3_Ctx *src); /** * @ingroup SM3 * @brief Dup SM3 CTX function * @param src [in] Pointer to the original SM3 context. * * @retval Success: sm3 ctx. * Fails: NULL. */ CRYPT_SM3_Ctx *CRYPT_SM3_DupCtx(const CRYPT_SM3_Ctx *src); #ifdef HITLS_CRYPTO_PROVIDER /** * @ingroup SM3 * @brief SM3 get param function * @param ctx [in] Pointer to the SM3 context. * @param param [in] Pointer to the parameter. * * @retval #CRYPT_SUCCESS initialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL * @retval #CRYPT_INVALID_ARG Pointer param is invalid */ int32_t CRYPT_SM3_GetParam(CRYPT_SM3_Ctx *ctx, BSL_Param *param); #else #define CRYPT_SM3_GetParam NULL #endif // HITLS_CRYPTO_PROVIDER #ifdef __cplusplus } #endif /* __cpluscplus */ #endif // HITLS_CRYPTO_SM3 #endif // CRYPT_SM3_H
2301_79861745/bench_create
crypto/sm3/include/crypt_sm3.h
C
unknown
4,358
/* * 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. * * ----------------------------------------------------------------------------- * * ARMv7 assembly optimization for SM3: * Contributors: Zhao Runchen, Li Xukai, Wang Weijia * Affiliation: Shandong University and Quan Cheng Laboratory * Date: 2025.7.10 * * ----------------------------------------------------------------------------- */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM3 .syntax unified .arch armv7 .thumb // State update function for 0~15 rounds, sp register points to the message w[i], r0 is the constant 0x79cc4519 .macro RF0 a b c d e f g h i LDR r1, [sp], #0x04 // r1 = w[i] LDR r2, [sp, #0x0C] // r2 = w[i+4] EOR r2, r1 // r2 = w[i] ^ w[i+4] ADD \h, r1 // h += w[i] (r1 is free now) ADD \d, r2 // d += w[i] ^ w[i + 4] (r2 is free now) ADD r1, \e, r0, ROR #(32-\i%32)%32 // r1 = ss1 = ((a <<< 12) + e + T[i]) <<< 7 ADD r1, r1, \a, ROR #20 ROR r1, #25 EOR r3, \e, \f // h += (e ^ f ^ g) + ss1 EOR r3, \g ADD \h, r3 ADD \h, r1 EOR r3, \h, \h, ROR #23 // h = h ^ (h <<< 9) ^ (h <<< 17) EOR \h, r3, \h, ROR #15 EOR r2, r1, \a, ROR #20 // r2 = ss2 = (a <<< 12) ^ ss1 EOR r3, \a, \b // d += (a ^ b ^ c)+ ss2 EOR r3, \c ADD \d, r3 ADD \d, r2 ROR \b, #23 // b = b <<< 9, f = f <<< 19 ROR \f, #13 .endm // State update function for rounds 16~63, sp register points to the message w[i], r0 is the constant 0x7a879d8a .macro RF1 a b c d e f g h i LDR r1, [sp], #0x04 // r1 = w[i] LDR r2, [sp, #0x0C] // r2 = w[i+4] EOR r2, r1 // r2 = w[i] ^ w[i+4] ADD \h, r1 // h += w[i] (r1 is free now) ADD \d, r2 // d += w[i] ^ w[i + 4] (r2 is free now) ADD r1, \e, r0, ROR #(32-\i%32)%32 // r1 = ss1 = ((a <<< 12) + e + T[i]) <<< 7 ADD r1, r1, \a, ROR #20 ROR r1, #25 EOR r3, \f, \g // h += (e & f) | (~e & g) + ss1 = ((f ^ g) & e) ^ g + ss1 AND r3, \e EOR r3, \g ADD \h, r3 ADD \h, r1 EOR r3, \h, \h, ROR #23 // h = P0(h) = h ^ (h <<< 9) ^ (h <<< 17) EOR \h, r3, \h, ROR #15 EOR r2, r1, \a, ROR #20 // ss2 = (a <<< 12) ^ ss1 (r1 is free now) EOR r3, \b, \c // d += ((a & b) | (a & c) | (b & c)) + ss2 = (a & (b | c)) | ((b & c)) + ss2 AND r3, \a AND r1, \b, \c EOR r3, r1 ADD \d, r3 ADD \d, r2 ROR \b, #23 // b = b <<< 9, f = f <<< 19 ROR \f, #13 .endm // Message expansion: w[i+16] = P1(w[i] ^ w[i+7] ^ (w[i+13] <<< 15)) ^ (w[i+3] <<< 7) ^ w[i+10] // P1(x) = x ^ x <<< 15 ^ x <<< 23 = x ^ x >>> 17 ^ x >>> 9 // Since the width of the sliding registers (w0-w13) is 14, there are no additional registers available for calculating P1(x). // Therefore, w7 will be used as a temporary register here and restored from memory later. // We hope you can have a better way to avoid reading the memory one more time. .macro MSGEXP w0 w3 w7 w10 w13 i LDR \w13, [sp, #((13 + \i) << 2)] EOR \w0, \w0, \w7 EOR \w0, \w0, \w13, ROR #17 EOR \w7, \w0, \w0, ROR #17 EOR \w0, \w7, \w0, ROR #9 EOR \w0, \w0, \w3, ROR #25 EOR \w0, \w10 LDR \w7, [sp, #((7 + \i) << 2)] STR \w0, [sp, #((16 + \i) << 2)] .endm // void SM3_CompressAsm(uint32_t state[8], const uint8_t *data, uint32_t blockCnt); .globl SM3_CompressAsm .type SM3_CompressAsm, %function .align 4 SM3_CompressAsm: PUSH {v1-ip, lr} .Lloop_start: SUBS r2, r2, 1 BCC .Lloop_end PUSH {r0-r2} SUB sp, sp, #(52<<2) ADD r1, #0x40 LDR v3, [r1, #-4]! LDR v2, [r1, #-4]! LDR v1, [r1, #-4]! REV v1, v1 REV v2, v2 REV v3, v3 PUSH {v1-v3} LDR r12, [r1, #-4]! LDR r11, [r1, #-4]! LDR r10, [r1, #-4]! LDR r9, [r1, #-4]! LDR r8, [r1, #-4]! LDR r7, [r1, #-4]! LDR r6, [r1, #-4]! LDR r5, [r1, #-4]! LDR r4, [r1, #-4]! LDR r3, [r1, #-4]! LDR r2, [r1, #-4]! LDR r0, [r1, #-8]! LDR r1, [r1, #4] REV r0, r0 REV r1, r1 REV r2, r2 REV r3, r3 REV r4, r4 REV r5, r5 REV r6, r6 REV r7, r7 REV r8, r8 REV r9, r9 REV r10, r10 REV r11, r11 REV r12, r12 PUSH {r0-r12} MSGEXP r0 r3 r7 r10 r14 0 MSGEXP r1 r4 r8 r11 r0 1 MSGEXP r2 r5 r9 r12 r1 2 MSGEXP r3 r6 r10 r14 r2 3 MSGEXP r4 r7 r11 r0 r3 4 MSGEXP r5 r8 r12 r1 r4 5 MSGEXP r6 r9 r14 r2 r5 6 MSGEXP r7 r10 r0 r3 r6 7 MSGEXP r8 r11 r1 r4 r7 8 MSGEXP r9 r12 r2 r5 r8 9 MSGEXP r10 r14 r3 r6 r9 10 MSGEXP r11 r0 r4 r7 r10 11 MSGEXP r12 r1 r5 r8 r11 12 MSGEXP r14 r2 r6 r9 r12 13 MSGEXP r0 r3 r7 r10 r14 14 MSGEXP r1 r4 r8 r11 r0 15 MSGEXP r2 r5 r9 r12 r1 16 MSGEXP r3 r6 r10 r14 r2 17 MSGEXP r4 r7 r11 r0 r3 18 MSGEXP r5 r8 r12 r1 r4 19 MSGEXP r6 r9 r14 r2 r5 20 MSGEXP r7 r10 r0 r3 r6 21 MSGEXP r8 r11 r1 r4 r7 22 MSGEXP r9 r12 r2 r5 r8 23 MSGEXP r10 r14 r3 r6 r9 24 MSGEXP r11 r0 r4 r7 r10 25 MSGEXP r12 r1 r5 r8 r11 26 MSGEXP r14 r2 r6 r9 r12 27 MSGEXP r0 r3 r7 r10 r14 28 MSGEXP r1 r4 r8 r11 r0 29 MSGEXP r2 r5 r9 r12 r1 30 MSGEXP r3 r6 r10 r14 r2 31 MSGEXP r4 r7 r11 r0 r3 32 MSGEXP r5 r8 r12 r1 r4 33 MSGEXP r6 r9 r14 r2 r5 34 MSGEXP r7 r10 r0 r3 r6 35 MSGEXP r8 r11 r1 r4 r7 36 MSGEXP r9 r12 r2 r5 r8 37 MSGEXP r10 r14 r3 r6 r9 38 MSGEXP r11 r0 r4 r7 r10 39 MSGEXP r12 r1 r5 r8 r11 40 MSGEXP r14 r2 r6 r9 r12 41 MSGEXP r0 r3 r7 r10 r14 42 MSGEXP r1 r4 r8 r11 r0 43 MSGEXP r2 r5 r9 r12 r1 44 MSGEXP r3 r6 r10 r14 r2 45 MSGEXP r4 r7 r11 r0 r3 46 MSGEXP r5 r8 r12 r1 r4 47 MSGEXP r6 r9 r14 r2 r5 48 MSGEXP r7 r10 r0 r3 r6 49 MSGEXP r8 r11 r1 r4 r7 50 MSGEXP r9 r12 r2 r5 r8 51 // Load the state. LDR r0, =0x79cc4519 LDR r1, [sp, #(68 << 2)] LDM r1, {v1-v8} // Note: Since the LDR offset relative to the current PC value cannot exceed 4KB in ARMV7, // and there are approximately 2000 lines of instructions inside this function that are out of the offset range, // we declare the literal pool here and skip it. B 1f .ltorg 1: // 0-15 RF0 v1 v2 v3 v4 v5 v6 v7 v8 0 RF0 v4 v1 v2 v3 v8 v5 v6 v7 1 RF0 v3 v4 v1 v2 v7 v8 v5 v6 2 RF0 v2 v3 v4 v1 v6 v7 v8 v5 3 RF0 v1 v2 v3 v4 v5 v6 v7 v8 4 RF0 v4 v1 v2 v3 v8 v5 v6 v7 5 RF0 v3 v4 v1 v2 v7 v8 v5 v6 6 RF0 v2 v3 v4 v1 v6 v7 v8 v5 7 RF0 v1 v2 v3 v4 v5 v6 v7 v8 8 RF0 v4 v1 v2 v3 v8 v5 v6 v7 9 RF0 v3 v4 v1 v2 v7 v8 v5 v6 10 RF0 v2 v3 v4 v1 v6 v7 v8 v5 11 RF0 v1 v2 v3 v4 v5 v6 v7 v8 12 RF0 v4 v1 v2 v3 v8 v5 v6 v7 13 RF0 v3 v4 v1 v2 v7 v8 v5 v6 14 RF0 v2 v3 v4 v1 v6 v7 v8 v5 15 // 16-31 LDR r0 , =0x7a879d8a RF1 v1 v2 v3 v4 v5 v6 v7 v8 16 RF1 v4 v1 v2 v3 v8 v5 v6 v7 17 RF1 v3 v4 v1 v2 v7 v8 v5 v6 18 RF1 v2 v3 v4 v1 v6 v7 v8 v5 19 RF1 v1 v2 v3 v4 v5 v6 v7 v8 20 RF1 v4 v1 v2 v3 v8 v5 v6 v7 21 RF1 v3 v4 v1 v2 v7 v8 v5 v6 22 RF1 v2 v3 v4 v1 v6 v7 v8 v5 23 RF1 v1 v2 v3 v4 v5 v6 v7 v8 24 RF1 v4 v1 v2 v3 v8 v5 v6 v7 25 RF1 v3 v4 v1 v2 v7 v8 v5 v6 26 RF1 v2 v3 v4 v1 v6 v7 v8 v5 27 RF1 v1 v2 v3 v4 v5 v6 v7 v8 28 RF1 v4 v1 v2 v3 v8 v5 v6 v7 29 RF1 v3 v4 v1 v2 v7 v8 v5 v6 30 RF1 v2 v3 v4 v1 v6 v7 v8 v5 31 // 32-47 RF1 v1 v2 v3 v4 v5 v6 v7 v8 32 RF1 v4 v1 v2 v3 v8 v5 v6 v7 33 RF1 v3 v4 v1 v2 v7 v8 v5 v6 34 RF1 v2 v3 v4 v1 v6 v7 v8 v5 35 RF1 v1 v2 v3 v4 v5 v6 v7 v8 36 RF1 v4 v1 v2 v3 v8 v5 v6 v7 37 RF1 v3 v4 v1 v2 v7 v8 v5 v6 38 RF1 v2 v3 v4 v1 v6 v7 v8 v5 39 RF1 v1 v2 v3 v4 v5 v6 v7 v8 40 RF1 v4 v1 v2 v3 v8 v5 v6 v7 41 RF1 v3 v4 v1 v2 v7 v8 v5 v6 42 RF1 v2 v3 v4 v1 v6 v7 v8 v5 43 RF1 v1 v2 v3 v4 v5 v6 v7 v8 44 RF1 v4 v1 v2 v3 v8 v5 v6 v7 45 RF1 v3 v4 v1 v2 v7 v8 v5 v6 46 RF1 v2 v3 v4 v1 v6 v7 v8 v5 47 // 48-63 RF1 v1 v2 v3 v4 v5 v6 v7 v8 48 RF1 v4 v1 v2 v3 v8 v5 v6 v7 49 RF1 v3 v4 v1 v2 v7 v8 v5 v6 50 RF1 v2 v3 v4 v1 v6 v7 v8 v5 51 RF1 v1 v2 v3 v4 v5 v6 v7 v8 52 RF1 v4 v1 v2 v3 v8 v5 v6 v7 53 RF1 v3 v4 v1 v2 v7 v8 v5 v6 54 RF1 v2 v3 v4 v1 v6 v7 v8 v5 55 RF1 v1 v2 v3 v4 v5 v6 v7 v8 56 RF1 v4 v1 v2 v3 v8 v5 v6 v7 57 RF1 v3 v4 v1 v2 v7 v8 v5 v6 58 RF1 v2 v3 v4 v1 v6 v7 v8 v5 59 RF1 v1 v2 v3 v4 v5 v6 v7 v8 60 RF1 v4 v1 v2 v3 v8 v5 v6 v7 61 RF1 v3 v4 v1 v2 v7 v8 v5 v6 62 RF1 v2 v3 v4 v1 v6 v7 v8 v5 63 // Load the state back and update it. ADD sp, sp, #16 LDR ip, [sp] LDM ip!, {r0-r3} EOR v1, r0 EOR v2, r1 EOR v3, r2 EOR v4, r3 LDM ip!, {r0-r3} EOR v5, r0 EOR v6, r1 EOR v7, r2 EOR v8, r3 STMDB ip, {v1-v8} POP {r0-r2} ADD r1, r1, #0x40 B .Lloop_start .Lloop_end: POP {v1-ip, lr} MOV pc, lr .end #endif
2301_79861745/bench_create
crypto/sm3/src/asm/sm3_armv7.S
Unix Assembly
unknown
10,036
/* * 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_SM3 #include "crypt_arm.h" .arch armv8-a+crypto // The first 16 of the compression function, w13 is Tj. .macro first16 A B C D E F G H W1 W2 ror w13, w13, #31 ror w10, \A, #20 add w9, \E, w10 eor w12, \E, \F ror \F, \F, #13 eor w12, w12, \G add w12, w12, \H add w9, w9, w13 ror w9, w9, #25 add w12, w12, w9 eor w10, w10, w9 add w12, w12, \W1 eor \H, w12, w12, ror #23 ror w9, w12, #15 eor \H, \H, w9 eor w11, \A, \B ror \B, \B, #23 eor w11, w11, \C add w11, w11, \D add w11, w11, w10 eor w9, \W1, \W2 add \D, w11, w9 .endm // Compress the last 48 of the function, w13 is Tj .macro second48 A B C D E F G H W1 W2 ror w13, w13, #31 orr w11, \B, \C eor w12, \F, \G ror \F, \F, #13 ror w10, \A, #20 add w9, w10, \E and w14, \A, w11 and w12, w12, \E eor w12, w12, \G add w12, w12, \H add w9, w9, w13 ror w9, w9, #25 add w12, w12, w9 eor w10, w10, w9 add w12, w12, \W1 and w11, \B, \C ror \B, \B, #23 orr w11, w11, w14 eor w9, \W1, \W2 add w11, w11, \D add w11, w11, w10 add \D, w11, w9 eor \H, w12, w12, ror #23 ror w9, w12, #15 eor \H, \H, w9 .endm // void SM3_CompressAsm(uint32_t state[8], const uint8_t *data, uint32_t blockCnt); .globl SM3_CompressAsm .type SM3_CompressAsm, %function .align 4 SM3_CompressAsm: AARCH64_PACIASP sub sp, sp, 128 stp x19, x20, [sp] stp x21, x22, [sp, #16] stp x23, x24, [sp, #32] stp x25, x26, [sp, #48] // According to the calling convention, this function needs to be saved. stp d8, d9, [sp, #64] stp d10, d11, [sp, #80] stp d12, d13, [sp, #96] stp d14, d15, [sp, #112] sub sp, sp, 64 mov x25, sp sub sp, sp, 64 mov x26, sp mov x22, x0 // x22: state mov x23, x1 // x23: data mov w24, w2 // x24: blockCnt // w0-w7: ABCDEFGH word register in"SM3 cryptographic hash algorithm" ldp w0, w1, [x22] ldp w2, w3, [x22, #8] ldp w4, w5, [x22, #16] ldp w6, w7, [x22, #24] prfm pldl1keep, [x23, #64] blocksloop_1: subs w24, w24, #1 bmi end // Due to the SM3 feature, only three messages can be extended in parallel. // You need to use ext to ensure that the data meets the requirements for calculation. // To reduce the delay, the message expansion is calculated together with the compression function, // and the compression function is calculated three times for every three Ws. // v0-v3 message group w0-w15 ld1 {v0.4s-v3.4s}, [x23] #ifndef HITLS_BIG_ENDIAN rev32 v0.16B, v0.16B rev32 v1.16B, v1.16B rev32 v2.16B, v2.16B rev32 v3.16B, v3.16B #endif ldp w15, w20, [x23] ldp w19, w21, [x23, #16] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w19, w19 rev w20, w20 rev w21, w21 #endif ext v24.16b, v3.16b, v3.16b, #4 // 13, 14, 15 ext v25.16b, v0.16b, v1.16b, #12 // 3, 4, 5 ext v23.16b, v1.16b, v2.16b, #12 // 7, 8, 9 ext v26.16b, v2.16b, v3.16b, #8 // 10, 11, 12 eor v27.16b, v0.16b, v23.16b // w13: constant Tj , 0 <= j <= 16 mov w13, #0x228c movk w13, #0xbce6, lsl #16 // Message grouping: Wj−3 ≪ 15, Wj−13 ≪ 7 shl v21.4s, v24.4s, #15 shl v22.4s, v25.4s, #7 sri v21.4s, v24.4s, #17 // 13, 14, 15<<<15 sri v22.4s, v25.4s, #25 // 3, 4, 5<<<7 first16 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v26.16b first16 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 // permutation function P1: X ^ (X ≪ 15) ^ (X ≪ 23) shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x23, #8] ldp w19, w21, [x23, #24] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w19, w19 rev w20, w20 rev w21, w21 #endif eor v27.16b, v27.16b, v30.16b first16 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v4.16b, v27.16b, v28.16b // 2:19, 20, 21 ext v23.16b, v1.16b, v2.16b, #8 // 6, 7, 8 eor v27.16b, v25.16b, v26.16b first16 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 shl v21.4s, v4.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v4.4s, #17 // 16, 17, 18<<<15 sri v22.4s, v23.4s, #25 // 6, 7, 8<<<7 ldp w15, w20, [x23, #16] ldp w19, w21, [x23, #32] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w19, w19 rev w20, w20 rev w21, w21 #endif eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v24.16b first16 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b first16 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 eor v27.16b, v27.16b, v30.16b mov v4.s[3], v4.s[2] // Due to ext requirements, to fill s[3] eor v5.16b, v27.16b, v28.16b // 3:22, 23, 24 ext v25.16b, v2.16b, v3.16b, #4 // 9, 10, 11 eor v27.16b, v23.16b, v24.16b ldp w15, w20, [x23, #24] ldp w19, w21, [x23, #40] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w19, w19 rev w20, w20 rev w21, w21 #endif shl v21.4s, v5.4s, #15 shl v22.4s, v25.4s, #7 sri v21.4s, v5.4s, #17 // 19, 20, 21<<<15 sri v22.4s, v25.4s, #25 // 9, 10, 11<<<7 first16 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v4.16b first16 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x23, #32] ldp w19, w21, [x23, #48] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w19, w19 rev w20, w20 rev w21, w21 #endif first16 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v27.16b, v30.16b mov v5.s[3], v5.s[2] // Due to ext requirements, to fill s[3] eor v6.16b, v27.16b, v28.16b // 4:25, 26, 27 eor v27.16b, v25.16b, v4.16b shl v21.4s, v6.4s, #15 shl v22.4s, v3.4s, #7 sri v21.4s, v6.4s, #17 // 22, 23, 24<<<15 sri v22.4s, v3.4s, #25 // 12, 13, 14<<<7 first16 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v5.16b ldp w15, w20, [x23, #40] ldp w19, w21, [x23, #56] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w19, w19 rev w20, w20 rev w21, w21 #endif shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 first16 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v27.16b, v27.16b, v29.16b first16 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 eor v27.16b, v27.16b, v30.16b mov v6.s[3], v6.s[2] // Due to ext requirements, to fill s[3] eor v7.16b, v27.16b, v28.16b // 5:28, 29, 30 ext v23.16b, v3.16b, v4.16b, #12 // 15, 16, 17 eor v27.16b, v3.16b, v5.16b st1 {v4.4s-v7.4s}, [x25] // There is a redundant data for every four 32-bit bits of the stored data. // The data needs to be read in a skip manner. shl v21.4s, v7.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v7.4s, #17 // 25, 26, 27<<<15 sri v22.4s, v23.4s, #25 // 15, 16, 17<<<7 ldp w15, w20, [x23, #48] ldp w19, w21, [x25] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w20, w20 #endif first16 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v6.16b shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b first16 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 ldp w15, w20, [x23, #56] #ifndef HITLS_BIG_ENDIAN rev w15, w15 rev w20, w20 #endif add x23, x23, #64 prfm pldl1keep, [x23, #64] ldr w19, [x25, #8] ldr w21, [x25, #16] eor v27.16b, v27.16b, v30.16b first16 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 mov v7.s[3], v7.s[2] // Due to ext requirements, to fill s[3] eor v8.16b, v27.16b, v28.16b // Message extension completed. Continue with the next 48 compression. ext v24.16b, v4.16b, v5.16b, #12 // 18, 19, 20 eor v27.16b, v23.16b, v6.16b first16 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 shl v21.4s, v8.4s, #15 shl v22.4s, v24.4s, #7 sri v21.4s, v8.4s, #17 // 28, 29, 30<<<15 sri v22.4s, v24.4s, #25 // 18, 19, 20<<<7 ldp w15, w20, [x25] ldp w19, w21, [x25, #20] eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v7.16b // w13: constant Tj , 17 <= j <= 63 mov w13, #0x3d43 movk w13, #0xcec5, lsl #16 second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 eor v27.16b, v27.16b, v30.16b mov v8.s[3], v8.s[2] // Due to ext requirements, to fill s[3] eor v9.16b, v27.16b, v28.16b // 7:34, 35, 36 ext v23.16b, v5.16b, v6.16b, #12 // 21, 22, 23 eor v27.16b, v24.16b, v7.16b ldr w15, [x25, #8] ldr w20, [x25, #16] ldp w19, w21, [x25, #32] shl v21.4s, v9.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v9.4s, #17 // 31, 32, 33<<<15 sri v22.4s, v23.4s, #25 // 21, 22, 23<<<7 second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v8.16b second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x25, #20] ldr w19, [x25, #40] ldr w21, [x25, #48] eor v27.16b, v27.16b, v30.16b second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 mov v9.s[3], v9.s[2] // Due to ext requirements, to fill s[3] eor v10.16b, v27.16b, v28.16b // 8:37, 38, 39 ext v24.16b, v6.16b, v7.16b, #12 // 24, 25, 26 eor v27.16b, v23.16b, v8.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 shl v21.4s, v10.4s, #15 shl v22.4s, v24.4s, #7 sri v21.4s, v10.4s, #17 // 34, 35, 36<<<15 sri v22.4s, v24.4s, #25 // 24, 25, 26<<<7 ldp w15, w20, [x25, #32] ldp w19, w21, [x25, #52] eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v9.16b second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 eor v27.16b, v27.16b, v30.16b mov v10.s[3], v10.s[2] // Due to ext requirements, to fill s[3] eor v11.16b, v27.16b, v28.16b // 9:40, 41, 42 ext v23.16b, v7.16b, v8.16b, #12 // 27, 28, 29 eor v27.16b, v24.16b, v9.16b st1 {v8.4s-v11.4s}, [x26] shl v21.4s, v11.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v11.4s, #17 // 37, 38, 39<<<15 sri v22.4s, v23.4s, #25 // 27, 28, 29<<<7 ldr w15, [x25, #40] ldr w20, [x25, #48] ldp w19, w21, [x26] second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v10.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x25, #52] ldr w19, [x26, #8] ldr w21, [x26, #16] eor v27.16b, v27.16b, v30.16b second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 mov v11.s[3], v11.s[2] // Due to ext requirements, to fill s[3] eor v12.16b, v27.16b, v28.16b // 10:43, 44, 45 ext v24.16b, v8.16b, v9.16b, #12 // 30, 31, 32 eor v27.16b, v23.16b, v10.16b second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 shl v21.4s, v12.4s, #15 shl v22.4s, v24.4s, #7 sri v21.4s, v12.4s, #17 // 40, 41, 42<<<15 sri v22.4s, v24.4s, #25 // 30, 31, 32<<<7 ldp w15, w20, [x26] ldp w19, w21, [x26, #20] eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v11.16b second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 eor v27.16b, v27.16b, v30.16b mov v12.s[3], v12.s[2] // Due to ext requirements, to fill s[3] eor v13.16b, v27.16b, v28.16b // 11:46, 47, 48 ext v23.16b, v9.16b, v10.16b, #12 // 33, 34, 35 eor v27.16b, v24.16b, v11.16b ldr w15, [x26, #8] ldr w20, [x26, #16] ldp w19, w21, [x26, #32] shl v21.4s, v13.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v13.4s, #17 // 43, 44, 45<<<15 sri v22.4s, v23.4s, #25 // 33, 34, 35<<<7 second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v12.16b second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x26, #20] ldr w19, [x26, #40] ldr w21, [x26, #48] second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v27.16b, v30.16b mov v13.s[3], v13.s[2] // Due to ext requirements, to fill s[3] eor v14.16b, v27.16b, v28.16b // 12:49, 50, 51 ext v24.16b, v10.16b, v11.16b, #12 // 36, 37, 38 eor v27.16b, v23.16b, v12.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 shl v21.4s, v14.4s, #15 shl v22.4s, v24.4s, #7 sri v21.4s, v14.4s, #17 // 46, 47, 48<<<15 sri v22.4s, v24.4s, #25 // 36, 37, 38<<<7 ldp w15, w20, [x26, #32] ldp w19, w21, [x26, #52] eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v13.16b second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 eor v27.16b, v27.16b, v30.16b mov v14.s[3], v14.s[2] // Due to ext requirements, to fill s[3] eor v15.16b, v27.16b, v28.16b // 13:52, 53, 54 ext v23.16b, v11.16b, v12.16b, #12 // 39, 40, 41 eor v27.16b, v24.16b, v13.16b st1 {v12.4s-v15.4s}, [x25] shl v21.4s, v15.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v15.4s, #17 // 49, 50, 51<<<15 sri v22.4s, v23.4s, #25 // 39, 40, 41<<<7 ldr w15, [x26, #40] ldr w20, [x26, #48] ldp w19, w21, [x25] second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v14.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x26, #52] ldr w19, [x25, #8] ldr w21, [x25, #16] second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v27.16b, v27.16b, v30.16b mov v15.s[3], v15.s[2] // Due to ext requirements, to fill s[3] eor v16.16b, v27.16b, v28.16b // 14:55, 56, 57 ext v24.16b, v12.16b, v13.16b, #12 // 42, 43, 44 eor v27.16b, v23.16b, v14.16b shl v21.4s, v16.4s, #15 shl v22.4s, v24.4s, #7 sri v21.4s, v16.4s, #17 // 52, 53, 54<<<15 sri v22.4s, v24.4s, #25 // 42, 43, 44<<<7 second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v15.16b ldp w15, w20, [x25] ldp w19, w21, [x25, #20] shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v27.16b, v30.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 mov v16.s[3], v16.s[2] // Due to ext requirements, to fill s[3] eor v17.16b, v27.16b, v28.16b // 15:58, 59, 60 ext v23.16b, v13.16b, v14.16b, #12 // 45, 46, 47 eor v27.16b, v24.16b, v15.16b shl v21.4s, v17.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v17.4s, #17 // 55, 56, 57<<<15 sri v22.4s, v23.4s, #25 // 45, 46, 47<<<7 ldr w15, [x25, #8] ldr w20, [x25, #16] ldp w19, w21, [x25, #32] second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v16.16b second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x25, #20] ldr w19, [x25, #40] ldr w21, [x25, #48] second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v27.16b, v30.16b eor v18.16b, v27.16b, v28.16b // 16:61, 62, 63 ext v24.16b, v14.16b, v15.16b, #12 // 48, 49, 50 eor v27.16b, v23.16b, v16.16b shl v21.4s, v18.4s, #15 shl v22.4s, v24.4s, #7 sri v21.4s, v18.4s, #17 // 58, 59, 60<<<15 sri v22.4s, v24.4s, #25 // 48, 49, 50<<<7 second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v17.16b ldp w15, w20, [x25, #32] ldp w19, w21, [x25, #52] shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v27.16b, v27.16b, v30.16b second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 eor v19.16b, v27.16b, v28.16b // 17:64, 65, 66 ext v23.16b, v15.16b, v16.16b, #12 // 51, 52, 53 eor v27.16b, v24.16b, v17.16b st1 {v16.4s-v19.4s}, [x26] shl v21.4s, v19.4s, #15 shl v22.4s, v23.4s, #7 sri v21.4s, v19.4s, #17 // 61, 62, 63<<<15 sri v22.4s, v23.4s, #25 // 51, 52, 53<<<7 ldr w15, [x25, #40] ldr w20, [x25, #48] ldp w19, w21, [x26] second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v18.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b ldp w15, w20, [x25, #52] ldr w19, [x26, #8] ldr w21, [x26, #16] eor v27.16b, v27.16b, v30.16b second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 eor v20.16b, v27.16b, v28.16b // 18:67 ext v24.16b, v16.16b, v17.16b, #12 // 54, 55, 56 eor v27.16b, v23.16b, v18.16b shl v21.4s, v20.4s, #15 shl v22.4s, v24.4s, #7 sri v21.4s, v20.4s, #17 // 64, 65, 66<<<15 sri v22.4s, v24.4s, #25 // 54, 55, 56<<<7 second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 eor v27.16b, v21.16b, v27.16b eor v28.16b, v22.16b, v19.16b ldp w15, w20, [x26] ldp w19, w21, [x26, #20] shl v29.4s, v27.4s, #15 shl v30.4s, v27.4s, #23 sri v29.4s, v27.4s, #17 sri v30.4s, v27.4s, #9 eor v27.16b, v27.16b, v29.16b second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 eor v27.16b, v27.16b, v30.16b second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 eor v21.16b, v27.16b, v28.16b ldr w15, [x26, #8] ldr w20, [x26, #16] ldp w19, w21, [x26, #32] second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 st1 {v20.4s-v21.4s}, [x25] second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 ldp w15, w20, [x26, #20] ldr w19, [x26, #40] ldr w21, [x26, #48] second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 ldp w15, w20, [x26, #32] ldp w19, w21, [x26, #52] second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 ldr w15, [x26, #40] ldr w20, [x26, #48] ldp w19, w21, [x25] second48 w0 w1 w2 w3 w4 w5 w6 w7 w15 w19 second48 w3 w0 w1 w2 w7 w4 w5 w6 w20 w21 ldp w15, w20, [x26, #52] ldr w19, [x25, #8] ldr w21, [x25, #16] second48 w2 w3 w0 w1 w6 w7 w4 w5 w15 w19 second48 w1 w2 w3 w0 w5 w6 w7 w4 w20 w21 ldp w9, w10, [x22] // XOR with the previous hash result ldp w11, w12, [x22, #8] ldp w13, w14, [x22, #16] ldp w15, w19, [x22, #24] eor w0, w0, w9 eor w1, w1, w10 eor w2, w2, w11 eor w3, w3, w12 eor w4, w4, w13 eor w5, w5, w14 eor w6, w6, w15 eor w7, w7, w19 stp w0, w1, [x22] // Result saving stp w2, w3, [x22, #8] stp w4, w5, [x22, #16] stp w6, w7, [x22, #24] b blocksloop_1 end: add sp, sp, 128 ldp x19, x20, [sp] ldp x21, x22, [sp, #16] ldp x23, x24, [sp, #32] ldp x25, x26, [sp, #48] ldp d8, d9, [sp, #64] ldp d10, d11, [sp, #80] ldp d12, d13, [sp, #96] ldp d14, d15, [sp, #112] add sp, sp, 128 AARCH64_AUTIASP ret .size SM3_CompressAsm,.-SM3_CompressAsm #endif
2301_79861745/bench_create
crypto/sm3/src/asm/sm3_armv8.S
Motorola 68K Assembly
unknown
21,152
/* * 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_SM3 .file "sm3_x86_64.s" .text .set A,%r8d .set B,%r9d .set C,%r10d .set D,%r11d .set E,%r12d .set F,%r13d .set G,%r14d .set H,%r15d .set STATE,%rdi .set DATA,%rsi .set NUM,%rdx .set ADDR,%rax .set BOOL_OUT,%eax .set SS1,%ebx .set SS2,%eax .set X0,%xmm0 .set X1,%xmm1 .set X2,%xmm2 .set X3,%xmm3 .set X4,%xmm4 .set X5,%xmm5 .set X6,%xmm6 .set X7,%xmm7 .set R16,%xmm13 .set R24,%xmm14 .set SHUFFLEMASK,%xmm15 .macro FF0 X Y Z # X ^ Y ^ Z movl \X,%eax xorl \Y,%eax xorl \Z,%eax .endm .macro FF1 X Y Z # (X & Y) | (X & Z) | (Y & Z) # = (X & (Y | Z)) | (Y & Z) movl \Y,%eax movl %eax,%ebx orl \Z,%eax andl \Z,%ebx andl \X,%eax orl %ebx,%eax .endm .macro GG0 X Y Z FF0 \X \Y \Z .endm .macro GG1 X Y Z # (X & Y) | (~X & Z) movl \X,%ebx andn \Z,%ebx,%eax andl \Y,%ebx orl %ebx,%eax .endm .macro P0 X rorx $15,\X,%eax rorx $23,\X,%ebx xorl %eax,\X xorl %ebx,\X .endm .macro P1 X rorx $9,\X,%eax rorx $17,\X,%ebx xorl %eax,\X xorl %ebx,\X .endm .macro ROUND FF GG Ar Br Cr Dr Er Fr Gr Hr TJ # A <<< 12 rorx $20,\Ar,%eax # SS1 (%ebx) <- ((A <<< 12) + E + (Tj <<< (jmod32))) <<< 7 # pre-computed TJ = Tj <<< (jmod32) movl %eax,%ebx addl \Er,%ebx addl $\TJ,%ebx rorx $25,%ebx,SS1 # SS2 (%eax) <- SS1 ^ (A <<< 12) xorl SS1,SS2 # TT1 (D) <- FF(A,B,C) + D + SS2 + W(i)' # TT2 (H) <- GG(E,F,G) + H + SS1 + W(i) addl SS2,\Dr addl SS1,\Hr # FF(A,B,C) \FF \Ar \Br \Cr addl BOOL_OUT,\Dr # GG(E,F,G) \GG \Er \Fr \Gr addl BOOL_OUT,\Hr # B <- B <<< 9 rorx $23,\Br,\Br # F <- F <<< 19 rorx $13,\Fr,\Fr # P0(TT2) P0 \Hr .endm .macro ROUND_00_15 Ar Br Cr Dr Er Fr Gr Hr TJ WADDR WPADDR # H <- H + W(i) # D <- D + W(i)' addl \WADDR(%rsp),\Hr addl \WPADDR(%rsp),\Dr ROUND FF0 GG0 \Ar \Br \Cr \Dr \Er \Fr \Gr \Hr \TJ .endm .macro ROUND_16_63 Ar Br Cr Dr Er Fr Gr Hr TJ WADDR WPADDR # H <- H + W(i) # D <- D + W(i)' addl \WADDR(%rsp),\Hr addl \WPADDR(%rsp),\Dr ROUND FF1 GG1 \Ar \Br \Cr \Dr \Er \Fr \Gr \Hr \TJ .endm .macro ROTATE IN OUT LEFT RIGHT vpslld $\LEFT,\IN,%xmm6 vpsrld $\RIGHT,\IN,%xmm7 vpxor %xmm6,%xmm7,\OUT .endm .macro WORD_SCHEDULER_00_11 I # W'(i) <- W(i) ^ W(i+4) # i = 0, ... ,11 movl \I(%rsp), %ecx # load W(i) xorl \I+4*4(%rsp),%ecx # W'(i) <- W(i) ^ W(i+4) movl %ecx,284(%rsp) # store W(i)' .endm .macro WORD_SCHEDULER_12_63 I # W(i) <- P1( W(i-16) ^ W(i-9) ^ ( W(i-3) <<< 15 ) ) ^ ( W(i-13) <<< 7 ) ^ W(i-6) # i = 12, ... ,63 rorx $17,\I+13*4(%rsp),%ecx # W(i-3) xorl \I(%rsp),%ecx # W(i-16) xorl \I+7*4(%rsp),%ecx # W(i-9) P1 %ecx rorx $25,\I+3*4(%rsp),%eax # W(i-13) xorl \I+10*4(%rsp),%eax # W(i-6) xorl %eax,%ecx # Store W(i) and W'(i) movl %ecx,\I+16*4(%rsp) # store W(i) xorl \I+12*4(%rsp),%ecx # W'(i) <- W(i) ^ W(i+4) movl %ecx,284(%rsp) # store W(i)' .endm .macro LOAD_WORD_FOR_SCHEDULER START vmovdqu \START(%rsp),X0 vmovdqu \START+12(%rsp),X1 vmovdqu \START+28(%rsp),X2 vmovdqu \START+40(%rsp),X3 vmovdqu \START+48(%rsp),X4 vmovdqu \START+52(%rsp),X5 .endm .macro LOAD_WORD_FOR_SCHEDULER_FAST START W0 W1 W2 W3 W4 W5 vmovdqu \START+12(%rsp),\W1 vmovdqu \START+48(%rsp),\W4 vmovdqu \START+52(%rsp),\W5 .endm .macro MESSAGE_SCHEDULER START W0 W1 W2 W3 W4 W5 vpxor \W2,\W0,\W0 ROTATE \W5,\W2,15,17 vpxor \W2,\W0,\W0 # P1 vpshufb R16,\W0,X6 vpshufb R24,\W0,X7 vpxor X6,X7,X7 ROTATE X7,X7,31,1 vpxor X7,\W0,\W0 ROTATE \W1,\W2,7,25 vpxor \W2,\W0,\W0 vpxor \W3,\W0,\W0 # W'(i) <- W(i) ^ W(i+4) vpxor \W0,\W4,\W4 vmovdqu \W0,\START+64(%rsp) vmovdqu \W4,284(%rsp) .endm .macro MESSAGE_SCHEDULER_FAST START W0 W1 W2 W3 W4 W5 LOAD_WORD_FOR_SCHEDULER_FAST \START \W0 \W1 \W2 \W3 \W4 \W5 MESSAGE_SCHEDULER \START \W0 \W1 \W2 \W3 \W4 \W5 .endm ##### SM3 ##### # void SM3_CompressSIMD(uint32_t state[8], const uint8_t *data, uint32_t blockCnt) # state|out %rdi 32 bytes # p %rsi # num %rdx .globl SM3_CompressSIMD .type SM3_CompressSIMD, @function .align 64 SM3_CompressSIMD: testq NUM,NUM jz .Lsm3_avx_ret # Store Registers subq $348,%rsp movq %rbx,300(%rsp) movq %rbp,8+300(%rsp) movq %r12,16+300(%rsp) movq %r13,24+300(%rsp) movq %r14,32+300(%rsp) movq %r15,40+300(%rsp) .Lsm3_avx_init: leaq MASKS(%rip),ADDR vmovdqa (ADDR),SHUFFLEMASK vmovdqa 16(ADDR),R16 vmovdqa 32(ADDR),R24 .Lsm3_avx_update: # Load Data (Big Endian) vmovdqu (DATA),%xmm0 vmovdqu 16(DATA),%xmm1 vmovdqu 32(DATA),%xmm2 vmovdqu 48(DATA),%xmm3 vpshufb SHUFFLEMASK,%xmm0,%xmm0 vpshufb SHUFFLEMASK,%xmm1,%xmm1 vpshufb SHUFFLEMASK,%xmm2,%xmm2 vpshufb SHUFFLEMASK,%xmm3,%xmm3 vmovdqu %xmm0,(%rsp) vmovdqu %xmm1,16(%rsp) vmovdqu %xmm2,32(%rsp) vmovdqu %xmm3,48(%rsp) vpxor %xmm1,%xmm0,%xmm0 vpxor %xmm2,%xmm1,%xmm1 vpxor %xmm3,%xmm2,%xmm2 # Load State movl (STATE),A movl 4(STATE),B movl 8(STATE),C movl 12(STATE),D movl 16(STATE),E movl 20(STATE),F movl 24(STATE),G movl 28(STATE),H # ROUND 0-11 vmovdqu %xmm0,284(%rsp) ROUND_00_15 A B C D E F G H 0x79CC4519 0 284 ROUND_00_15 D A B C H E F G 0xF3988A32 4 288 ROUND_00_15 C D A B G H E F 0xE7311465 8 292 ROUND_00_15 B C D A F G H E 0xCE6228CB 12 296 vmovdqu %xmm1,284(%rsp) ROUND_00_15 A B C D E F G H 0x9CC45197 16 284 ROUND_00_15 D A B C H E F G 0x3988A32F 20 288 ROUND_00_15 C D A B G H E F 0x7311465E 24 292 ROUND_00_15 B C D A F G H E 0xE6228CBC 28 296 vmovdqu %xmm2,284(%rsp) ROUND_00_15 A B C D E F G H 0xCC451979 32 284 ROUND_00_15 D A B C H E F G 0x988A32F3 36 288 ROUND_00_15 C D A B G H E F 0x311465E7 40 292 ROUND_00_15 B C D A F G H E 0x6228CBCE 44 296 # ROUND 12-15 LOAD_WORD_FOR_SCHEDULER 0 MESSAGE_SCHEDULER 0 X0 X1 X2 X3 X4 X5 ROUND_00_15 A B C D E F G H 0xC451979C 48 284 ROUND_00_15 D A B C H E F G 0x88A32F39 52 288 ROUND_00_15 C D A B G H E F 0x11465E73 56 292 MESSAGE_SCHEDULER_FAST 12 X1 X0 X3 X5 X4 X2 ROUND_00_15 B C D A F G H E 0x228CBCE6 60 284 # ROUND 16-63 ROUND_16_63 A B C D E F G H 0x9D8A7A87 64 288 ROUND_16_63 D A B C H E F G 0x3B14F50F 68 292 MESSAGE_SCHEDULER_FAST 24 X0 X1 X5 X2 X4 X3 ROUND_16_63 C D A B G H E F 0x7629EA1E 72 284 ROUND_16_63 B C D A F G H E 0xEC53D43C 76 288 ROUND_16_63 A B C D E F G H 0xD8A7A879 80 292 MESSAGE_SCHEDULER_FAST 36 X1 X0 X2 X3 X4 X5 ROUND_16_63 D A B C H E F G 0xB14F50F3 84 284 ROUND_16_63 C D A B G H E F 0x629EA1E7 88 288 ROUND_16_63 B C D A F G H E 0xC53D43CE 92 292 MESSAGE_SCHEDULER_FAST 48 X0 X1 X3 X5 X4 X2 ROUND_16_63 A B C D E F G H 0x8A7A879D 96 284 ROUND_16_63 D A B C H E F G 0x14F50F3B 100 288 ROUND_16_63 C D A B G H E F 0x29EA1E76 104 292 MESSAGE_SCHEDULER_FAST 60 X1 X0 X5 X2 X4 X3 ROUND_16_63 B C D A F G H E 0x53D43CEC 108 284 ROUND_16_63 A B C D E F G H 0xA7A879D8 112 288 ROUND_16_63 D A B C H E F G 0x4F50F3B1 116 292 MESSAGE_SCHEDULER_FAST 72 X0 X1 X2 X3 X4 X5 ROUND_16_63 C D A B G H E F 0x9EA1E762 120 284 ROUND_16_63 B C D A F G H E 0x3D43CEC5 124 288 ROUND_16_63 A B C D E F G H 0x7A879D8A 128 292 MESSAGE_SCHEDULER_FAST 84 X1 X0 X3 X5 X4 X2 ROUND_16_63 D A B C H E F G 0xF50F3B14 132 284 ROUND_16_63 C D A B G H E F 0xEA1E7629 136 288 ROUND_16_63 B C D A F G H E 0xD43CEC53 140 292 MESSAGE_SCHEDULER_FAST 96 X0 X1 X5 X2 X4 X3 ROUND_16_63 A B C D E F G H 0xA879D8A7 144 284 ROUND_16_63 D A B C H E F G 0x50F3B14F 148 288 ROUND_16_63 C D A B G H E F 0xA1E7629E 152 292 MESSAGE_SCHEDULER_FAST 108 X1 X0 X2 X3 X4 X5 ROUND_16_63 B C D A F G H E 0x43CEC53D 156 284 ROUND_16_63 A B C D E F G H 0x879D8A7A 160 288 ROUND_16_63 D A B C H E F G 0x0F3B14F5 164 292 MESSAGE_SCHEDULER_FAST 120 X0 X1 X3 X5 X4 X2 ROUND_16_63 C D A B G H E F 0x1E7629EA 168 284 ROUND_16_63 B C D A F G H E 0x3CEC53D4 172 288 ROUND_16_63 A B C D E F G H 0x79D8A7A8 176 292 MESSAGE_SCHEDULER_FAST 132 X1 X0 X5 X2 X4 X3 ROUND_16_63 D A B C H E F G 0xF3B14F50 180 284 ROUND_16_63 C D A B G H E F 0xE7629EA1 184 288 ROUND_16_63 B C D A F G H E 0xCEC53D43 188 292 MESSAGE_SCHEDULER_FAST 144 X0 X1 X2 X3 X4 X5 ROUND_16_63 A B C D E F G H 0x9D8A7A87 192 284 ROUND_16_63 D A B C H E F G 0x3B14F50F 196 288 ROUND_16_63 C D A B G H E F 0x7629EA1E 200 292 MESSAGE_SCHEDULER_FAST 156 X1 X0 X3 X5 X4 X2 ROUND_16_63 B C D A F G H E 0xEC53D43C 204 284 ROUND_16_63 A B C D E F G H 0xD8A7A879 208 288 ROUND_16_63 D A B C H E F G 0xB14F50F3 212 292 MESSAGE_SCHEDULER_FAST 168 X0 X1 X5 X2 X4 X3 ROUND_16_63 C D A B G H E F 0x629EA1E7 216 284 ROUND_16_63 B C D A F G H E 0xC53D43CE 220 288 ROUND_16_63 A B C D E F G H 0x8A7A879D 224 292 MESSAGE_SCHEDULER_FAST 180 X1 X0 X2 X3 X4 X5 ROUND_16_63 D A B C H E F G 0x14F50F3B 228 284 ROUND_16_63 C D A B G H E F 0x29EA1E76 232 288 ROUND_16_63 B C D A F G H E 0x53D43CEC 236 292 MESSAGE_SCHEDULER_FAST 192 X0 X1 X3 X5 X4 X2 ROUND_16_63 A B C D E F G H 0xA7A879D8 240 284 ROUND_16_63 D A B C H E F G 0x4F50F3B1 244 288 ROUND_16_63 C D A B G H E F 0x9EA1E762 248 292 WORD_SCHEDULER_12_63 204 ROUND_16_63 B C D A F G H E 0x3D43CEC5 252 284 xorl A,(STATE) xorl B,4(STATE) xorl C,8(STATE) xorl D,12(STATE) xorl E,16(STATE) xorl F,20(STATE) xorl G,24(STATE) xorl H,28(STATE) leaq 64(DATA),DATA decq NUM jz .Lsm3_avx_final jmp .Lsm3_avx_update .Lsm3_avx_final: vzeroall # Clear Context xorq %r8,%r8 xorq %r9,%r9 xorq %r10,%r10 xorq %r11,%r11 # Restore Registers movq 300(%rsp),%rbx movq 8+300(%rsp),%rbp movq 16+300(%rsp),%r12 movq 24+300(%rsp),%r13 movq 32+300(%rsp),%r14 movq 40+300(%rsp),%r15 addq $348,%rsp .Lsm3_avx_ret: ret .size SM3_CompressSIMD, .-SM3_CompressSIMD ##### SM3 ##### # void SM3_CompressAsm(uint32_t state[8], const uint8_t *data, uint32_t blockCnt) # state|out %rdi 32 bytes # p %rsi # num %rdx .globl SM3_CompressAsm .type SM3_CompressAsm, @function .align 64 SM3_CompressAsm: testq NUM,NUM jz .Lsm3_ret # Store Registers subq $348,%rsp movq %rbx,300(%rsp) movq %rbp,8+300(%rsp) movq %r12,16+300(%rsp) movq %r13,24+300(%rsp) movq %r14,32+300(%rsp) movq %r15,40+300(%rsp) .Lsm3_loop: # Load Data (Big Endian) movl (DATA),%r8d movl 4(DATA),%r9d movl 8(DATA),%r10d movl 12(DATA),%r11d movbe %r8d,(%rsp) movbe %r9d,4(%rsp) movbe %r10d,8(%rsp) movbe %r11d,12(%rsp) movl 16(DATA),%r8d movl 20(DATA),%r9d movl 24(DATA),%r10d movl 28(DATA),%r11d movbe %r8d,16(%rsp) movbe %r9d,20(%rsp) movbe %r10d,24(%rsp) movbe %r11d,28(%rsp) movl 32(DATA),%r8d movl 36(DATA),%r9d movl 40(DATA),%r10d movl 44(DATA),%r11d movbe %r8d,32(%rsp) movbe %r9d,36(%rsp) movbe %r10d,40(%rsp) movbe %r11d,44(%rsp) movl 48(DATA),%r8d movl 52(DATA),%r9d movl 56(DATA),%r10d movl 60(DATA),%r11d movbe %r8d,48(%rsp) movbe %r9d,52(%rsp) movbe %r10d,56(%rsp) movbe %r11d,60(%rsp) # Load State movl (STATE),A movl 4(STATE),B movl 8(STATE),C movl 12(STATE),D movl 16(STATE),E movl 20(STATE),F movl 24(STATE),G movl 28(STATE),H # ROUND 0-11 WORD_SCHEDULER_00_11 0 ROUND_00_15 A B C D E F G H 0x79CC4519 0 284 WORD_SCHEDULER_00_11 4 ROUND_00_15 D A B C H E F G 0xF3988A32 4 284 WORD_SCHEDULER_00_11 8 ROUND_00_15 C D A B G H E F 0xE7311465 8 284 WORD_SCHEDULER_00_11 12 ROUND_00_15 B C D A F G H E 0xCE6228CB 12 284 WORD_SCHEDULER_00_11 16 ROUND_00_15 A B C D E F G H 0x9CC45197 16 284 WORD_SCHEDULER_00_11 20 ROUND_00_15 D A B C H E F G 0x3988A32F 20 284 WORD_SCHEDULER_00_11 24 ROUND_00_15 C D A B G H E F 0x7311465E 24 284 WORD_SCHEDULER_00_11 28 ROUND_00_15 B C D A F G H E 0xE6228CBC 28 284 WORD_SCHEDULER_00_11 32 ROUND_00_15 A B C D E F G H 0xCC451979 32 284 WORD_SCHEDULER_00_11 36 ROUND_00_15 D A B C H E F G 0x988A32F3 36 284 WORD_SCHEDULER_00_11 40 ROUND_00_15 C D A B G H E F 0x311465E7 40 284 WORD_SCHEDULER_00_11 44 ROUND_00_15 B C D A F G H E 0x6228CBCE 44 284 # ROUND 12-15 WORD_SCHEDULER_12_63 0 ROUND_00_15 A B C D E F G H 0xC451979C 48 284 WORD_SCHEDULER_12_63 4 ROUND_00_15 D A B C H E F G 0x88A32F39 52 284 WORD_SCHEDULER_12_63 8 ROUND_00_15 C D A B G H E F 0x11465E73 56 284 WORD_SCHEDULER_12_63 12 ROUND_00_15 B C D A F G H E 0x228CBCE6 60 284 # ROUND 16-63 WORD_SCHEDULER_12_63 16 ROUND_16_63 A B C D E F G H 0x9D8A7A87 64 284 WORD_SCHEDULER_12_63 20 ROUND_16_63 D A B C H E F G 0x3B14F50F 68 284 WORD_SCHEDULER_12_63 24 ROUND_16_63 C D A B G H E F 0x7629EA1E 72 284 WORD_SCHEDULER_12_63 28 ROUND_16_63 B C D A F G H E 0xEC53D43C 76 284 WORD_SCHEDULER_12_63 32 ROUND_16_63 A B C D E F G H 0xD8A7A879 80 284 WORD_SCHEDULER_12_63 36 ROUND_16_63 D A B C H E F G 0xB14F50F3 84 284 WORD_SCHEDULER_12_63 40 ROUND_16_63 C D A B G H E F 0x629EA1E7 88 284 WORD_SCHEDULER_12_63 44 ROUND_16_63 B C D A F G H E 0xC53D43CE 92 284 WORD_SCHEDULER_12_63 48 ROUND_16_63 A B C D E F G H 0x8A7A879D 96 284 WORD_SCHEDULER_12_63 52 ROUND_16_63 D A B C H E F G 0x14F50F3B 100 284 WORD_SCHEDULER_12_63 56 ROUND_16_63 C D A B G H E F 0x29EA1E76 104 284 WORD_SCHEDULER_12_63 60 ROUND_16_63 B C D A F G H E 0x53D43CEC 108 284 WORD_SCHEDULER_12_63 64 ROUND_16_63 A B C D E F G H 0xA7A879D8 112 284 WORD_SCHEDULER_12_63 68 ROUND_16_63 D A B C H E F G 0x4F50F3B1 116 284 WORD_SCHEDULER_12_63 72 ROUND_16_63 C D A B G H E F 0x9EA1E762 120 284 WORD_SCHEDULER_12_63 76 ROUND_16_63 B C D A F G H E 0x3D43CEC5 124 284 WORD_SCHEDULER_12_63 80 ROUND_16_63 A B C D E F G H 0x7A879D8A 128 284 WORD_SCHEDULER_12_63 84 ROUND_16_63 D A B C H E F G 0xF50F3B14 132 284 WORD_SCHEDULER_12_63 88 ROUND_16_63 C D A B G H E F 0xEA1E7629 136 284 WORD_SCHEDULER_12_63 92 ROUND_16_63 B C D A F G H E 0xD43CEC53 140 284 WORD_SCHEDULER_12_63 96 ROUND_16_63 A B C D E F G H 0xA879D8A7 144 284 WORD_SCHEDULER_12_63 100 ROUND_16_63 D A B C H E F G 0x50F3B14F 148 284 WORD_SCHEDULER_12_63 104 ROUND_16_63 C D A B G H E F 0xA1E7629E 152 284 WORD_SCHEDULER_12_63 108 ROUND_16_63 B C D A F G H E 0x43CEC53D 156 284 WORD_SCHEDULER_12_63 112 ROUND_16_63 A B C D E F G H 0x879D8A7A 160 284 WORD_SCHEDULER_12_63 116 ROUND_16_63 D A B C H E F G 0x0F3B14F5 164 284 WORD_SCHEDULER_12_63 120 ROUND_16_63 C D A B G H E F 0x1E7629EA 168 284 WORD_SCHEDULER_12_63 124 ROUND_16_63 B C D A F G H E 0x3CEC53D4 172 284 WORD_SCHEDULER_12_63 128 ROUND_16_63 A B C D E F G H 0x79D8A7A8 176 284 WORD_SCHEDULER_12_63 132 ROUND_16_63 D A B C H E F G 0xF3B14F50 180 284 WORD_SCHEDULER_12_63 136 ROUND_16_63 C D A B G H E F 0xE7629EA1 184 284 WORD_SCHEDULER_12_63 140 ROUND_16_63 B C D A F G H E 0xCEC53D43 188 284 WORD_SCHEDULER_12_63 144 ROUND_16_63 A B C D E F G H 0x9D8A7A87 192 284 WORD_SCHEDULER_12_63 148 ROUND_16_63 D A B C H E F G 0x3B14F50F 196 284 WORD_SCHEDULER_12_63 152 ROUND_16_63 C D A B G H E F 0x7629EA1E 200 284 WORD_SCHEDULER_12_63 156 ROUND_16_63 B C D A F G H E 0xEC53D43C 204 284 WORD_SCHEDULER_12_63 160 ROUND_16_63 A B C D E F G H 0xD8A7A879 208 284 WORD_SCHEDULER_12_63 164 ROUND_16_63 D A B C H E F G 0xB14F50F3 212 284 WORD_SCHEDULER_12_63 168 ROUND_16_63 C D A B G H E F 0x629EA1E7 216 284 WORD_SCHEDULER_12_63 172 ROUND_16_63 B C D A F G H E 0xC53D43CE 220 284 WORD_SCHEDULER_12_63 176 ROUND_16_63 A B C D E F G H 0x8A7A879D 224 284 WORD_SCHEDULER_12_63 180 ROUND_16_63 D A B C H E F G 0x14F50F3B 228 284 WORD_SCHEDULER_12_63 184 ROUND_16_63 C D A B G H E F 0x29EA1E76 232 284 WORD_SCHEDULER_12_63 188 ROUND_16_63 B C D A F G H E 0x53D43CEC 236 284 WORD_SCHEDULER_12_63 192 ROUND_16_63 A B C D E F G H 0xA7A879D8 240 284 WORD_SCHEDULER_12_63 196 ROUND_16_63 D A B C H E F G 0x4F50F3B1 244 284 WORD_SCHEDULER_12_63 200 ROUND_16_63 C D A B G H E F 0x9EA1E762 248 284 WORD_SCHEDULER_12_63 204 ROUND_16_63 B C D A F G H E 0x3D43CEC5 252 284 xorl A,(STATE) xorl B,4(STATE) xorl C,8(STATE) xorl D,12(STATE) xorl E,16(STATE) xorl F,20(STATE) xorl G,24(STATE) xorl H,28(STATE) leaq 64(DATA),DATA decq NUM jz .Lsm3_final jmp .Lsm3_loop .Lsm3_final: # Clear Context xorq %r8,%r8 xorq %r9,%r9 xorq %r10,%r10 xorq %r11,%r11 # Restore Registers movq 300(%rsp),%rbx movq 8+300(%rsp),%rbp movq 16+300(%rsp),%r12 movq 24+300(%rsp),%r13 movq 32+300(%rsp),%r14 movq 40+300(%rsp),%r15 addq $348,%rsp .Lsm3_ret: ret .size SM3_CompressAsm, .-SM3_CompressAsm .section .rodata .align 64 MASKS: # .shuffle_mask: (%rax) .byte 3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12 # left rotations # .r16: 16(%rax) .byte 2,3,0,1,6,7,4,5,10,11,8,9,14,15,12,13 # .r24: 32(%rax) .byte 1,2,3,0,5,6,7,4,9,10,11,8,13,14,15,12 #endif
2301_79861745/bench_create
crypto/sm3/src/asm/sm3_x86_64.s
Motorola 68K Assembly
unknown
16,809
/* * 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_SM3 #include "sm3_local.h" #include "crypt_utils.h" void SM3_Compress(uint32_t state[8], const uint8_t *data, uint32_t blockCnt) { return SM3_CompressAsm(state, data, blockCnt); } #endif // HITLS_CRYPTO_SM3
2301_79861745/bench_create
crypto/sm3/src/asm_sm3.c
C
unknown
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_SM3 #include <stdint.h> #include "crypt_utils.h" #include "bsl_sal.h" #include "crypt_sm3.h" #define K0 0x79cc4519U #define K1 0xf3988a32U #define K2 0xe7311465U #define K3 0xce6228cbU #define K4 0x9cc45197U #define K5 0x3988a32fU #define K6 0x7311465eU #define K7 0xe6228cbcU #define K8 0xcc451979U #define K9 0x988a32f3U #define K10 0x311465e7U #define K11 0x6228cbceU #define K12 0xc451979cU #define K13 0x88a32f39U #define K14 0x11465e73U #define K15 0x228cbce6U #define K16 0x9d8a7a87U #define K17 0x3b14f50fU #define K18 0x7629ea1eU #define K19 0xec53d43cU #define K20 0xd8a7a879U #define K21 0xb14f50f3U #define K22 0x629ea1e7U #define K23 0xc53d43ceU #define K24 0x8a7a879dU #define K25 0x14f50f3bU #define K26 0x29ea1e76U #define K27 0x53d43cecU #define K28 0xa7a879d8U #define K29 0x4f50f3b1U #define K30 0x9ea1e762U #define K31 0x3d43cec5U #define K32 0x7a879d8aU #define K33 0xf50f3b14U #define K34 0xea1e7629U #define K35 0xd43cec53U #define K36 0xa879d8a7U #define K37 0x50f3b14fU #define K38 0xa1e7629eU #define K39 0x43cec53dU #define K40 0x879d8a7aU #define K41 0x0f3b14f5U #define K42 0x1e7629eaU #define K43 0x3cec53d4U #define K44 0x79d8a7a8U #define K45 0xf3b14f50U #define K46 0xe7629ea1U #define K47 0xcec53d43U #define K48 0x9d8a7a87U #define K49 0x3b14f50fU #define K50 0x7629ea1eU #define K51 0xec53d43cU #define K52 0xd8a7a879U #define K53 0xb14f50f3U #define K54 0x629ea1e7U #define K55 0xc53d43ceU #define K56 0x8a7a879dU #define K57 0x14f50f3bU #define K58 0x29ea1e76U #define K59 0x53d43cecU #define K60 0xa7a879d8U #define K61 0x4f50f3b1U #define K62 0x9ea1e762U #define K63 0x3d43cec5U #define P0(x) ((x) ^ ROTL32((x), 9) ^ ROTL32((x), 17)) #define P1(x) ((x) ^ ROTL32((x), 15) ^ ROTL32((x), 23)) #define FF0(x, y, z) ((x) ^ (y) ^ (z)) #define FF1(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) #define GG0(x, y, z) ((x) ^ (y) ^ (z)) #define GG1(x, y, z) (((x) & (y)) | (~(x) & (z))) #define ROUND(A, B, C, D, E, F, G, H, K, FF, GG, Wj, Wi) do { \ uint32_t a12 = ROTL32((A), 12); \ uint32_t ss1 = ROTL32(a12 + (E) + (K), 7); \ uint32_t ss2 = ss1 ^ a12; \ uint32_t tt1 = FF((A), (B), (C)) + (D) + ss2 + (Wi); \ uint32_t tt2 = GG((E), (F), (G)) + (H) + ss1 + (Wj); \ (H) = tt1; (D) = P0(tt2); \ (B) = ROTL32((B), 9); (F) = ROTL32((F), 19); \ } while (0) #define ROUND00_15(A, B, C, D, E, F, G, H, K, Wj, Wi) \ ROUND(A, B, C, D, E, F, G, H, K, FF0, GG0, Wj, Wi) #define ROUND16_63(A, B, C, D, E, F, G, H, K, Wj, Wi) \ ROUND(A, B, C, D, E, F, G, H, K, FF1, GG1, Wj, Wi) #define EXPAND(W1, W2, W3, W4, W5) \ (P1((W1) ^ (W2) ^ ROTL32((W3), 15)) ^ ROTL32((W4), 7) ^ (W5)) /* see the GM standard document GM/T 0004-2012 chapter 5.3.3 */ void SM3_Compress(uint32_t state[8], const uint8_t *data, uint32_t blockCnt) { uint32_t w[16] = {0}; const uint8_t *input = data; uint32_t count = blockCnt; while (count > 0) { /* converts data to 32 bits for calculation */ w[0] = GET_UINT32_BE(input, 0); w[1] = GET_UINT32_BE(input, 4); w[2] = GET_UINT32_BE(input, 8); w[3] = GET_UINT32_BE(input, 12); w[4] = GET_UINT32_BE(input, 16); w[5] = GET_UINT32_BE(input, 20); w[6] = GET_UINT32_BE(input, 24); w[7] = GET_UINT32_BE(input, 28); w[8] = GET_UINT32_BE(input, 32); w[9] = GET_UINT32_BE(input, 36); w[10] = GET_UINT32_BE(input, 40); w[11] = GET_UINT32_BE(input, 44); w[12] = GET_UINT32_BE(input, 48); w[13] = GET_UINT32_BE(input, 52); w[14] = GET_UINT32_BE(input, 56); w[15] = GET_UINT32_BE(input, 60); uint32_t a = state[0]; uint32_t b = state[1]; uint32_t c = state[2]; uint32_t d = state[3]; uint32_t e = state[4]; uint32_t f = state[5]; uint32_t g = state[6]; uint32_t h = state[7]; // 0 ~ 15 round ROUND00_15(a, b, c, d, e, f, g, h, K0, w[0], w[0] ^ w[4]); ROUND00_15(h, a, b, c, d, e, f, g, K1, w[1], w[1] ^ w[5]); ROUND00_15(g, h, a, b, c, d, e, f, K2, w[2], w[2] ^ w[6]); ROUND00_15(f, g, h, a, b, c, d, e, K3, w[3], w[3] ^ w[7]); ROUND00_15(e, f, g, h, a, b, c, d, K4, w[4], w[4] ^ w[8]); ROUND00_15(d, e, f, g, h, a, b, c, K5, w[5], w[5] ^ w[9]); ROUND00_15(c, d, e, f, g, h, a, b, K6, w[6], w[6] ^ w[10]); ROUND00_15(b, c, d, e, f, g, h, a, K7, w[7], w[7] ^ w[11]); ROUND00_15(a, b, c, d, e, f, g, h, K8, w[8], w[8] ^ w[12]); ROUND00_15(h, a, b, c, d, e, f, g, K9, w[9], w[9] ^ w[13]); ROUND00_15(g, h, a, b, c, d, e, f, K10, w[10], w[10] ^ w[14]); ROUND00_15(f, g, h, a, b, c, d, e, K11, w[11], w[11] ^ w[15]); w[0] = EXPAND(w[0], w[7], w[13], w[3], w[10]); ROUND00_15(e, f, g, h, a, b, c, d, K12, w[12], w[12] ^ w[0]); w[1] = EXPAND(w[1], w[8], w[14], w[4], w[11]); ROUND00_15(d, e, f, g, h, a, b, c, K13, w[13], w[13] ^ w[1]); w[2] = EXPAND(w[2], w[9], w[15], w[5], w[12]); ROUND00_15(c, d, e, f, g, h, a, b, K14, w[14], w[14] ^ w[2]); w[3] = EXPAND(w[3], w[10], w[0], w[6], w[13]); ROUND00_15(b, c, d, e, f, g, h, a, K15, w[15], w[15] ^ w[3]); // 16 ~ 63 round w[4] = EXPAND(w[4], w[11], w[1], w[7], w[14]); ROUND16_63(a, b, c, d, e, f, g, h, K16, w[0], w[0] ^ w[4]); w[5] = EXPAND(w[5], w[12], w[2], w[8], w[15]); ROUND16_63(h, a, b, c, d, e, f, g, K17, w[1], w[1] ^ w[5]); w[6] = EXPAND(w[6], w[13], w[3], w[9], w[0]); ROUND16_63(g, h, a, b, c, d, e, f, K18, w[2], w[2] ^ w[6]); w[7] = EXPAND(w[7], w[14], w[4], w[10], w[1]); ROUND16_63(f, g, h, a, b, c, d, e, K19, w[3], w[3] ^ w[7]); w[8] = EXPAND(w[8], w[15], w[5], w[11], w[2]); ROUND16_63(e, f, g, h, a, b, c, d, K20, w[4], w[4] ^ w[8]); w[9] = EXPAND(w[9], w[0], w[6], w[12], w[3]); ROUND16_63(d, e, f, g, h, a, b, c, K21, w[5], w[5] ^ w[9]); w[10] = EXPAND(w[10], w[1], w[7], w[13], w[4]); ROUND16_63(c, d, e, f, g, h, a, b, K22, w[6], w[6] ^ w[10]); w[11] = EXPAND(w[11], w[2], w[8], w[14], w[5]); ROUND16_63(b, c, d, e, f, g, h, a, K23, w[7], w[7] ^ w[11]); w[12] = EXPAND(w[12], w[3], w[9], w[15], w[6]); ROUND16_63(a, b, c, d, e, f, g, h, K24, w[8], w[8] ^ w[12]); w[13] = EXPAND(w[13], w[4], w[10], w[0], w[7]); ROUND16_63(h, a, b, c, d, e, f, g, K25, w[9], w[9] ^ w[13]); w[14] = EXPAND(w[14], w[5], w[11], w[1], w[8]); ROUND16_63(g, h, a, b, c, d, e, f, K26, w[10], w[10] ^ w[14]); w[15] = EXPAND(w[15], w[6], w[12], w[2], w[9]); ROUND16_63(f, g, h, a, b, c, d, e, K27, w[11], w[11] ^ w[15]); w[0] = EXPAND(w[0], w[7], w[13], w[3], w[10]); ROUND16_63(e, f, g, h, a, b, c, d, K28, w[12], w[12] ^ w[0]); w[1] = EXPAND(w[1], w[8], w[14], w[4], w[11]); ROUND16_63(d, e, f, g, h, a, b, c, K29, w[13], w[13] ^ w[1]); w[2] = EXPAND(w[2], w[9], w[15], w[5], w[12]); ROUND16_63(c, d, e, f, g, h, a, b, K30, w[14], w[14] ^ w[2]); w[3] = EXPAND(w[3], w[10], w[0], w[6], w[13]); ROUND16_63(b, c, d, e, f, g, h, a, K31, w[15], w[15] ^ w[3]); w[4] = EXPAND(w[4], w[11], w[1], w[7], w[14]); ROUND16_63(a, b, c, d, e, f, g, h, K32, w[0], w[0] ^ w[4]); w[5] = EXPAND(w[5], w[12], w[2], w[8], w[15]); ROUND16_63(h, a, b, c, d, e, f, g, K33, w[1], w[1] ^ w[5]); w[6] = EXPAND(w[6], w[13], w[3], w[9], w[0]); ROUND16_63(g, h, a, b, c, d, e, f, K34, w[2], w[2] ^ w[6]); w[7] = EXPAND(w[7], w[14], w[4], w[10], w[1]); ROUND16_63(f, g, h, a, b, c, d, e, K35, w[3], w[3] ^ w[7]); w[8] = EXPAND(w[8], w[15], w[5], w[11], w[2]); ROUND16_63(e, f, g, h, a, b, c, d, K36, w[4], w[4] ^ w[8]); w[9] = EXPAND(w[9], w[0], w[6], w[12], w[3]); ROUND16_63(d, e, f, g, h, a, b, c, K37, w[5], w[5] ^ w[9]); w[10] = EXPAND(w[10], w[1], w[7], w[13], w[4]); ROUND16_63(c, d, e, f, g, h, a, b, K38, w[6], w[6] ^ w[10]); w[11] = EXPAND(w[11], w[2], w[8], w[14], w[5]); ROUND16_63(b, c, d, e, f, g, h, a, K39, w[7], w[7] ^ w[11]); w[12] = EXPAND(w[12], w[3], w[9], w[15], w[6]); ROUND16_63(a, b, c, d, e, f, g, h, K40, w[8], w[8] ^ w[12]); w[13] = EXPAND(w[13], w[4], w[10], w[0], w[7]); ROUND16_63(h, a, b, c, d, e, f, g, K41, w[9], w[9] ^ w[13]); w[14] = EXPAND(w[14], w[5], w[11], w[1], w[8]); ROUND16_63(g, h, a, b, c, d, e, f, K42, w[10], w[10] ^ w[14]); w[15] = EXPAND(w[15], w[6], w[12], w[2], w[9]); ROUND16_63(f, g, h, a, b, c, d, e, K43, w[11], w[11] ^ w[15]); w[0] = EXPAND(w[0], w[7], w[13], w[3], w[10]); ROUND16_63(e, f, g, h, a, b, c, d, K44, w[12], w[12] ^ w[0]); w[1] = EXPAND(w[1], w[8], w[14], w[4], w[11]); ROUND16_63(d, e, f, g, h, a, b, c, K45, w[13], w[13] ^ w[1]); w[2] = EXPAND(w[2], w[9], w[15], w[5], w[12]); ROUND16_63(c, d, e, f, g, h, a, b, K46, w[14], w[14] ^ w[2]); w[3] = EXPAND(w[3], w[10], w[0], w[6], w[13]); ROUND16_63(b, c, d, e, f, g, h, a, K47, w[15], w[15] ^ w[3]); w[4] = EXPAND(w[4], w[11], w[1], w[7], w[14]); ROUND16_63(a, b, c, d, e, f, g, h, K48, w[0], w[0] ^ w[4]); w[5] = EXPAND(w[5], w[12], w[2], w[8], w[15]); ROUND16_63(h, a, b, c, d, e, f, g, K49, w[1], w[1] ^ w[5]); w[6] = EXPAND(w[6], w[13], w[3], w[9], w[0]); ROUND16_63(g, h, a, b, c, d, e, f, K50, w[2], w[2] ^ w[6]); w[7] = EXPAND(w[7], w[14], w[4], w[10], w[1]); ROUND16_63(f, g, h, a, b, c, d, e, K51, w[3], w[3] ^ w[7]); w[8] = EXPAND(w[8], w[15], w[5], w[11], w[2]); ROUND16_63(e, f, g, h, a, b, c, d, K52, w[4], w[4] ^ w[8]); w[9] = EXPAND(w[9], w[0], w[6], w[12], w[3]); ROUND16_63(d, e, f, g, h, a, b, c, K53, w[5], w[5] ^ w[9]); w[10] = EXPAND(w[10], w[1], w[7], w[13], w[4]); ROUND16_63(c, d, e, f, g, h, a, b, K54, w[6], w[6] ^ w[10]); w[11] = EXPAND(w[11], w[2], w[8], w[14], w[5]); ROUND16_63(b, c, d, e, f, g, h, a, K55, w[7], w[7] ^ w[11]); w[12] = EXPAND(w[12], w[3], w[9], w[15], w[6]); ROUND16_63(a, b, c, d, e, f, g, h, K56, w[8], w[8] ^ w[12]); w[13] = EXPAND(w[13], w[4], w[10], w[0], w[7]); ROUND16_63(h, a, b, c, d, e, f, g, K57, w[9], w[9] ^ w[13]); w[14] = EXPAND(w[14], w[5], w[11], w[1], w[8]); ROUND16_63(g, h, a, b, c, d, e, f, K58, w[10], w[10] ^ w[14]); w[15] = EXPAND(w[15], w[6], w[12], w[2], w[9]); ROUND16_63(f, g, h, a, b, c, d, e, K59, w[11], w[11] ^ w[15]); w[0] = EXPAND(w[0], w[7], w[13], w[3], w[10]); ROUND16_63(e, f, g, h, a, b, c, d, K60, w[12], w[12] ^ w[0]); w[1] = EXPAND(w[1], w[8], w[14], w[4], w[11]); ROUND16_63(d, e, f, g, h, a, b, c, K61, w[13], w[13] ^ w[1]); w[2] = EXPAND(w[2], w[9], w[15], w[5], w[12]); ROUND16_63(c, d, e, f, g, h, a, b, K62, w[14], w[14] ^ w[2]); w[3] = EXPAND(w[3], w[10], w[0], w[6], w[13]); ROUND16_63(b, c, d, e, f, g, h, a, K63, w[15], w[15] ^ w[3]); state[0] ^= a; state[1] ^= b; state[2] ^= c; state[3] ^= d; state[4] ^= e; state[5] ^= f; state[6] ^= g; state[7] ^= h; input += CRYPT_SM3_BLOCKSIZE; count--; } } #endif // HITLS_CRYPTO_SM3
2301_79861745/bench_create
crypto/sm3/src/noasm_sm3.c
C
unknown
12,340
/* * 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 SM3_LOCAL_H #define SM3_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM3 #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ void SM3_Compress(uint32_t state[8], const uint8_t *data, uint32_t blockCnt); /* assembly interface */ void SM3_CompressAsm(uint32_t state[8], const uint8_t *data, uint32_t blockCnt); #ifdef __cplusplus } #endif /* __cpluscplus */ #endif // HITLS_CRYPTO_SM3 #endif // SM3_LOCAL_H
2301_79861745/bench_create
crypto/sm3/src/sm3_local.h
C
unknown
1,000
/* * 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_SM3 #include <stdlib.h> #include <stdint.h> #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "crypt_sm3.h" #include "sm3_local.h" #include "bsl_sal.h" #include "crypt_types.h" struct CryptSm3Ctx { uint32_t h[CRYPT_SM3_DIGESTSIZE / sizeof(uint32_t)]; /* store the intermediate data of the hash value */ uint32_t hNum, lNum; /* input data counter, maximum value 2 ^ 64 bits */ uint8_t block[CRYPT_SM3_BLOCKSIZE]; /* store the remaining data which less than one block */ /* Number of remaining bytes in 'block' arrary that are stored less than one block */ uint32_t num; }; CRYPT_SM3_Ctx *CRYPT_SM3_NewCtx(void) { return BSL_SAL_Calloc(1, sizeof(CRYPT_SM3_Ctx)); } CRYPT_SM3_Ctx *CRYPT_SM3_NewCtxEx(void *libCtx, int32_t algId) { (void)libCtx; (void)algId; return BSL_SAL_Calloc(1, sizeof(CRYPT_SM3_Ctx)); } void CRYPT_SM3_FreeCtx(CRYPT_SM3_Ctx *ctx) { BSL_SAL_ClearFree(ctx, sizeof(CRYPT_SM3_Ctx)); } int32_t CRYPT_SM3_Init(CRYPT_SM3_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_SM3_Ctx), 0, sizeof(CRYPT_SM3_Ctx)); /* GM/T 0004-2012 chapter 4.1 */ ctx->h[0] = 0x7380166F; ctx->h[1] = 0x4914B2B9; ctx->h[2] = 0x172442D7; ctx->h[3] = 0xDA8A0600; ctx->h[4] = 0xA96F30BC; ctx->h[5] = 0x163138AA; ctx->h[6] = 0xE38DEE4D; ctx->h[7] = 0xB0FB0E4E; return CRYPT_SUCCESS; } int32_t CRYPT_SM3_Deinit(CRYPT_SM3_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memset_s(ctx, sizeof(CRYPT_SM3_Ctx), 0, sizeof(CRYPT_SM3_Ctx)); return CRYPT_SUCCESS; } static uint32_t IsInputOverflow(CRYPT_SM3_Ctx *ctx, uint32_t nbytes) { uint32_t cnt0 = ctx->lNum + (nbytes << SHIFTS_PER_BYTE); if (cnt0 < ctx->lNum) { if (++ctx->hNum == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM3_INPUT_OVERFLOW); return CRYPT_SM3_INPUT_OVERFLOW; } } uint32_t cnt1 = ctx->hNum + (uint32_t)(nbytes >> (BITSIZE(uint32_t) - SHIFTS_PER_BYTE)); if (cnt1 < ctx->hNum) { BSL_ERR_PUSH_ERROR(CRYPT_SM3_INPUT_OVERFLOW); return CRYPT_SM3_INPUT_OVERFLOW; } ctx->hNum = cnt1; ctx->lNum = cnt0; return CRYPT_SUCCESS; } static int32_t IsUpdateParamValid(CRYPT_SM3_Ctx *ctx, const uint8_t *in, uint32_t len) { if ((ctx == NULL) || (in == NULL && len != 0)) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (IsInputOverflow(ctx, len) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_SM3_INPUT_OVERFLOW); return CRYPT_SM3_INPUT_OVERFLOW; } return CRYPT_SUCCESS; } int32_t CRYPT_SM3_Update(CRYPT_SM3_Ctx *ctx, const uint8_t *in, uint32_t len) { int32_t ret = IsUpdateParamValid(ctx, in, len); if (ret != CRYPT_SUCCESS) { return ret; } if (len == 0) { return CRYPT_SUCCESS; } const uint8_t *data = in; uint32_t dataLen = len; uint32_t left = CRYPT_SM3_BLOCKSIZE - ctx->num; if (ctx->num != 0) { if (dataLen < left) { (void)memcpy_s(ctx->block + ctx->num, left, data, dataLen); ctx->num += dataLen; return CRYPT_SUCCESS; } // When the external input data is greater than the remaining space of the block, // copy the data which is the same length as the remaining space. (void)memcpy_s(ctx->block + ctx->num, left, data, left); SM3_Compress(ctx->h, ctx->block, 1); dataLen -= left; data += left; ctx->num = 0; } uint32_t blockCnt = dataLen / CRYPT_SM3_BLOCKSIZE; if (blockCnt > 0) { SM3_Compress(ctx->h, data, blockCnt); blockCnt *= CRYPT_SM3_BLOCKSIZE; data += blockCnt; dataLen -= blockCnt; } if (dataLen != 0) { // copy the remaining data to the cache array (void)memcpy_s(ctx->block, CRYPT_SM3_BLOCKSIZE, data, dataLen); ctx->num = dataLen; } return CRYPT_SUCCESS; } static int32_t IsFinalParamValid(const CRYPT_SM3_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_SM3_DIGESTSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM3_OUT_BUFF_LEN_NOT_ENOUGH); return CRYPT_SM3_OUT_BUFF_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } int32_t CRYPT_SM3_Final(CRYPT_SM3_Ctx *ctx, uint8_t *out, uint32_t *outLen) { int32_t ret = IsFinalParamValid(ctx, out, outLen); if (ret != CRYPT_SUCCESS) { return ret; } ctx->block[ctx->num++] = 0x80; /* 0x80 means add '1' to the end of the message */ uint8_t *block = ctx->block; uint32_t num = ctx->num; uint32_t left = CRYPT_SM3_BLOCKSIZE - num; if (left < 8) { /* less than 8 bytes which insufficient for storing data length data */ (void)memset_s(block + num, left, 0, left); SM3_Compress(ctx->h, ctx->block, 1); num = 0; left = CRYPT_SM3_BLOCKSIZE; } (void)memset_s(block + num, left - 8, 0, left - 8); block += CRYPT_SM3_BLOCKSIZE - 8; PUT_UINT32_BE(ctx->hNum, block, 0); block += sizeof(uint32_t); PUT_UINT32_BE(ctx->lNum, block, 0); SM3_Compress(ctx->h, ctx->block, 1); ctx->num = 0; 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); PUT_UINT32_BE(ctx->h[5], out, 20); PUT_UINT32_BE(ctx->h[6], out, 24); PUT_UINT32_BE(ctx->h[7], out, 28); *outLen = CRYPT_SM3_DIGESTSIZE; return CRYPT_SUCCESS; } int32_t CRYPT_SM3_CopyCtx(CRYPT_SM3_Ctx *dst, const CRYPT_SM3_Ctx *src) { if (dst == NULL || src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memcpy_s(dst, sizeof(CRYPT_SM3_Ctx), src, sizeof(CRYPT_SM3_Ctx)); return CRYPT_SUCCESS; } CRYPT_SM3_Ctx *CRYPT_SM3_DupCtx(const CRYPT_SM3_Ctx *src) { if (src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_SM3_Ctx *newCtx = CRYPT_SM3_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memcpy_s(newCtx, sizeof(CRYPT_SM3_Ctx), src, sizeof(CRYPT_SM3_Ctx)); return newCtx; } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_SM3_GetParam(CRYPT_SM3_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_SM3_DIGESTSIZE, CRYPT_SM3_BLOCKSIZE, param); } #endif // HITLS_CRYPTO_PROVIDER #endif /* HITLS_CRYPTO_SM3 */
2301_79861745/bench_create
crypto/sm3/src/sm3_public.c
C
unknown
7,524
/* * 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_SM4_H #define CRYPT_SM4_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM4 #include <stdint.h> #include <string.h> #include <stdbool.h> #include "crypt_types.h" #include "crypt_local_types.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #define CRYPT_SM4_BLOCKSIZE 16 #define CRYPT_SM4_BLOCKSIZE_16 256 #define CRYPT_SM4_ROUNDS 32 typedef struct { uint8_t iv[CRYPT_SM4_BLOCKSIZE]; uint32_t rk[CRYPT_SM4_ROUNDS]; } CRYPT_SM4_Ctx; /** * @brief SM4 Set the encryption and decryption key. * * @param [IN] ctx SM4 context * @param [IN] key Key * @param [IN] keyLen Key length * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_SetKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t keyLen); /** * @brief SM4 encryption. The data length must be an integer multiple of 16. * * @param [IN] ctx SM4 context * @param [IN] in Data to be encrypted * @param [OUT] out Encrypted data * @param [IN] length Data length * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length); /** * @brief SM4 decryption. The data length must be an integer multiple of 16. * * @param [IN] ctx SM4 context * @param [IN] in Data to be decrypted * @param [OUT] out Decrypted Data * @param [IN] length Data length * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length); /** * @brief Clear the SM4 context * * @param [IN] ctx sm4 context */ void CRYPT_SM4_Clean(CRYPT_SM4_Ctx *ctx); #ifdef HITLS_CRYPTO_XTS /** * @brief SM4 Set the encryption key. * * @param ctx [IN] sm4 Context * @param key [IN] Key. The first 16 bytes are data_key, and the last 16 bytes are tweak_key. * @param keyLen [IN] Key length * * @retval #CRYPT_SUCCESS succeeded. * @retval #CRYPT_NULL_INPUT ctx or key is NULL. * @retval #CRYPT_SM4_ERR_KEY_LEN The key length is not equal to 32. */ int32_t CRYPT_SM4_XTS_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); /** * @brief SM4 Set the decryption key. * * @param ctx [IN] sm4 Context * @param key [IN] Key * @param keyLen [IN] Key length * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_XTS_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); /** * @brief Clear SM4_xts context * * @param [IN] ctx sm4 context */ void CRYPT_SM4_XTS_Clean(CRYPT_SM4_Ctx *ctx); /** * @brief SM4 XTS mode encryption * @param ctx [IN] sm4 Context * @param in [IN] Data to be decrypted * @param out [OUT] Decrypted data * @param len [IN] Length of the decrypted data * @param iv [IN] Set IV * * @retval #CRYPT_SUCCESS succeeded. * @retval #CRYPT_NULL_INPUT ctx,in,out is NULL * @retval #CRYPT_SM4_DATALEN_ERROR The length of the decrypted data is less than 16 bytes. */ int32_t CRYPT_SM4_XTS_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); /** * @brief SM4 XTS mode encryption * @param ctx [IN] sm4 Context * @param in [IN] Data to be encrypted * @param out [OUT] Encrypted data * @param len [IN] Length of the encrypted data * @param iv [IN] Set IV * * @retval #CRYPT_SUCCESS succeeded. * @retval #CRYPT_NULL_INPUT ctx/in/out is NULL * @retval #CRYPT_SM4_DATALEN_ERROR The length of the encrypted data is less than 16. */ int32_t CRYPT_SM4_XTS_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); #endif /** * @brief SM4 Set the encryption key (optimized). * * @param [IN] ctx SM4 context * @param [IN] key Key * @param [IN] len Key length * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); /** * @brief SM4 Set the decryption key (optimized). * * @param [IN] ctx SM4 context * @param [IN] key Key * @param [IN] len Key length * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len); #ifdef HITLS_CRYPTO_ECB /** * @brief SM4 ECB mode encryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be encrypted * @param out [OUT] Encrypted data * @param len [IN] Length of the encrypted data * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_ECB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); /** * @brief SM4 ECB mode decryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be decrypted * @param out [OUT] Decrypted data * @param len [IN] Length of the decrypted data * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_ECB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_CBC /** * @brief SM4 CBC mode encryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be encrypted * @param out [OUT] Encrypted data * @param len [IN] Length of the encrypted data * @param iv [IN] Set IV * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_CBC_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); /** * @brief SM4 CBC mode decryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be decrypted * @param out [OUT] decrypted data * @param len [IN] Length of the decrypted data * @param iv [IN] Set IV * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_CBC_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); #endif #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM) /** * @brief SM4 CTR mode encryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be encrypted * @param out [OUT] Encrypted data * @param len [IN] Length of the encrypted data * @param iv [IN] Set IV * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_CTR_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); /** * @brief SM4 CTR mode decryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be decrypted * @param out [OUT] decrypted data * @param len [IN] Length of the decrypted data * @param iv [IN] Set IV * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_CTR_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv); #endif #ifdef HITLS_CRYPTO_OFB /** * @brief SM4 OFB mode encryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be encrypted * @param out [OUT] Encrypted data * @param len [IN] Length of the encrypted data * @param iv [IN] Set IV * @param offset [OUT] Length of less than one block * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_OFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset); /** * @brief SM4 OFB mode decryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be decrypted * @param out [OUT] decrypted data * @param len [IN] Length of the decrypted data * @param iv [IN] Set IV * @param offset [OUT] Length of less than one block * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_OFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset); #endif #ifdef HITLS_CRYPTO_CFB /** * @brief SM4 CFB mode encryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be encrypted * @param out [OUT] Encrypted data * @param len [IN] Length of the encrypted data * @param iv [IN] Set IV * @param offset [OUT] Length of less than one block. * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_CFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset); /** * @brief SM4 CFB mode decryption (optimized). * @param ctx [IN] sm4 Context * @param in [IN] Data to be decrypted * @param out [OUT] decrypted data * @param len [IN] Length of the decrypted data * @param iv [IN] Set IV * @param offset [OUT] Length of less than one block. * * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t CRYPT_SM4_CFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset); #endif #ifdef __cplusplus } #endif /* __cplusplus */ #endif // HITLS_CRYPTO_SM4 #endif // CRYPT_SM4_H
2301_79861745/bench_create
crypto/sm4/include/crypt_sm4.h
C
unknown
10,045
/* * 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_SM4 #include "crypt_arm.h" .arch armv8-a+crypto rk0 .req v12 rk1 .req v13 rka .req v14 rkb .req v15 rk2 .req v20 rkc .req v21 vtmp0 .req v0 vtmp1 .req v1 vtmp2 .req v2 vtmp3 .req v3 vtmp4 .req v24 vtmp5 .req v25 vtmp6 .req v22 vtmp7 .req v23 data0 .req v4 data1 .req v5 data2 .req v6 data3 .req v7 datax0 .req v8 datax1 .req v9 datax2 .req v10 datax3 .req v11 vtmpx0 .req v12 vtmpx1 .req v13 vtmpx2 .req v14 vtmpx3 .req v15 data10 .req v16 data11 .req v17 data12 .req v18 data13 .req v19 MaskV .req v26 TAHMatV .req v27 TALMatV .req v28 ATAHMatV .req v29 ATALMatV .req v30 ANDMaskV .req v31 MaskQ .req q26 TAHMatQ .req q27 TALMatQ .req q28 ATAHMatQ .req q29 ATALMatQ .req q30 ANDMaskQ .req q31 vtmp5q .req q25 vtmp6q .req q22 vtmp7q .req q23 inp .req x0 outp .req x1 blocks .req w2 rks .req x3 wtmp0 .req w7 wtmp1 .req w8 wtmp2 .req w9 ptr .req x10 counter .req w11 word0 .req w12 word1 .req w13 word2 .req w14 word3 .req w15 xword1 .req x13 tbox0 .req x19 tbox1 .req x20 tbox2 .req x21 tbox3 .req x22 len .req x2 ivp .req x4 ctr .req w5 ivec .req v3 ivec1 .req v15 .section .rodata .align 4 .Ltbox1: .word 0xd55b5b8e, 0x924242d0, 0xeaa7a74d, 0xfdfbfb06, 0xcf3333fc, 0xe2878765, 0x3df4f4c9, 0xb5dede6b, 0x1658584e .word 0xb4dada6e, 0x14505044, 0xc10b0bca, 0x28a0a088, 0xf8efef17, 0x2cb0b09c, 0x05141411, 0x2bacac87, 0x669d9dfb .word 0x986a6af2, 0x77d9d9ae, 0x2aa8a882, 0xbcfafa46, 0x04101014, 0xc00f0fcf, 0xa8aaaa02, 0x45111154, 0x134c4c5f .word 0x269898be, 0x4825256d, 0x841a1a9e, 0x0618181e, 0x9b6666fd, 0x9e7272ec, 0x4309094a, 0x51414110, 0xf7d3d324 .word 0x934646d5, 0xecbfbf53, 0x9a6262f8, 0x7be9e992, 0x33ccccff, 0x55515104, 0x0b2c2c27, 0x420d0d4f, 0xeeb7b759 .word 0xcc3f3ff3, 0xaeb2b21c, 0x638989ea, 0xe7939374, 0xb1cece7f, 0x1c70706c, 0xaba6a60d, 0xca2727ed, 0x08202028 .word 0xeba3a348, 0x975656c1, 0x82020280, 0xdc7f7fa3, 0x965252c4, 0xf9ebeb12, 0x74d5d5a1, 0x8d3e3eb3, 0x3ffcfcc3 .word 0xa49a9a3e, 0x461d1d5b, 0x071c1c1b, 0xa59e9e3b, 0xfff3f30c, 0xf0cfcf3f, 0x72cdcdbf, 0x175c5c4b, 0xb8eaea52 .word 0x810e0e8f, 0x5865653d, 0x3cf0f0cc, 0x1964647d, 0xe59b9b7e, 0x87161691, 0x4e3d3d73, 0xaaa2a208, 0x69a1a1c8 .word 0x6aadadc7, 0x83060685, 0xb0caca7a, 0x70c5c5b5, 0x659191f4, 0xd96b6bb2, 0x892e2ea7, 0xfbe3e318, 0xe8afaf47 .word 0x0f3c3c33, 0x4a2d2d67, 0x71c1c1b0, 0x5759590e, 0x9f7676e9, 0x35d4d4e1, 0x1e787866, 0x249090b4, 0x0e383836 .word 0x5f797926, 0x628d8def, 0x59616138, 0xd2474795, 0xa08a8a2a, 0x259494b1, 0x228888aa, 0x7df1f18c, 0x3bececd7 .word 0x01040405, 0x218484a5, 0x79e1e198, 0x851e1e9b, 0xd7535384, 0x00000000, 0x4719195e, 0x565d5d0b, 0x9d7e7ee3 .word 0xd04f4f9f, 0x279c9cbb, 0x5349491a, 0x4d31317c, 0x36d8d8ee, 0x0208080a, 0xe49f9f7b, 0xa2828220, 0xc71313d4 .word 0xcb2323e8, 0x9c7a7ae6, 0xe9abab42, 0xbdfefe43, 0x882a2aa2, 0xd14b4b9a, 0x41010140, 0xc41f1fdb, 0x38e0e0d8 .word 0xb7d6d661, 0xa18e8e2f, 0xf4dfdf2b, 0xf1cbcb3a, 0xcd3b3bf6, 0xfae7e71d, 0x608585e5, 0x15545441, 0xa3868625 .word 0xe3838360, 0xacbaba16, 0x5c757529, 0xa6929234, 0x996e6ef7, 0x34d0d0e4, 0x1a686872, 0x54555501, 0xafb6b619 .word 0x914e4edf, 0x32c8c8fa, 0x30c0c0f0, 0xf6d7d721, 0x8e3232bc, 0xb3c6c675, 0xe08f8f6f, 0x1d747469, 0xf5dbdb2e .word 0xe18b8b6a, 0x2eb8b896, 0x800a0a8a, 0x679999fe, 0xc92b2be2, 0x618181e0, 0xc30303c0, 0x29a4a48d, 0x238c8caf .word 0xa9aeae07, 0x0d343439, 0x524d4d1f, 0x4f393976, 0x6ebdbdd3, 0xd6575781, 0xd86f6fb7, 0x37dcdceb, 0x44151551 .word 0xdd7b7ba6, 0xfef7f709, 0x8c3a3ab6, 0x2fbcbc93, 0x030c0c0f, 0xfcffff03, 0x6ba9a9c2, 0x73c9c9ba, 0x6cb5b5d9 .word 0x6db1b1dc, 0x5a6d6d37, 0x50454515, 0x8f3636b9, 0x1b6c6c77, 0xadbebe13, 0x904a4ada, 0xb9eeee57, 0xde7777a9 .word 0xbef2f24c, 0x7efdfd83, 0x11444455, 0xda6767bd, 0x5d71712c, 0x40050545, 0x1f7c7c63, 0x10404050, 0x5b696932 .word 0xdb6363b8, 0x0a282822, 0xc20707c5, 0x31c4c4f5, 0x8a2222a8, 0xa7969631, 0xce3737f9, 0x7aeded97, 0xbff6f649 .word 0x2db4b499, 0x75d1d1a4, 0xd3434390, 0x1248485a, 0xbae2e258, 0xe6979771, 0xb6d2d264, 0xb2c2c270, 0x8b2626ad .word 0x68a5a5cd, 0x955e5ecb, 0x4b292962, 0x0c30303c, 0x945a5ace, 0x76ddddab, 0x7ff9f986, 0x649595f1, 0xbbe6e65d .word 0xf2c7c735, 0x0924242d, 0xc61717d1, 0x6fb9b9d6, 0xc51b1bde, 0x86121294, 0x18606078, 0xf3c3c330, 0x7cf5f589 .word 0xefb3b35c, 0x3ae8e8d2, 0xdf7373ac, 0x4c353579, 0x208080a0, 0x78e5e59d, 0xedbbbb56, 0x5e7d7d23, 0x3ef8f8c6 .word 0xd45f5f8b, 0xc82f2fe7, 0x39e4e4dd, 0x49212168 .Ltbox2: .word 0x5b5b8ed5, 0x4242d092, 0xa7a74dea, 0xfbfb06fd, 0x3333fccf, 0x878765e2, 0xf4f4c93d, 0xdede6bb5, 0x58584e16 .word 0xdada6eb4, 0x50504414, 0x0b0bcac1, 0xa0a08828, 0xefef17f8, 0xb0b09c2c, 0x14141105, 0xacac872b, 0x9d9dfb66 .word 0x6a6af298, 0xd9d9ae77, 0xa8a8822a, 0xfafa46bc, 0x10101404, 0x0f0fcfc0, 0xaaaa02a8, 0x11115445, 0x4c4c5f13 .word 0x9898be26, 0x25256d48, 0x1a1a9e84, 0x18181e06, 0x6666fd9b, 0x7272ec9e, 0x09094a43, 0x41411051, 0xd3d324f7 .word 0x4646d593, 0xbfbf53ec, 0x6262f89a, 0xe9e9927b, 0xccccff33, 0x51510455, 0x2c2c270b, 0xd0d4f42, 0xb7b759ee .word 0x3f3ff3cc, 0xb2b21cae, 0x8989ea63, 0x939374e7, 0xcece7fb1, 0x70706c1c, 0xa6a60dab, 0x2727edca, 0x20202808 .word 0xa3a348eb, 0x5656c197, 0x02028082, 0x7f7fa3dc, 0x5252c496, 0xebeb12f9, 0xd5d5a174, 0x3e3eb38d, 0xfcfcc33f .word 0x9a9a3ea4, 0x1d1d5b46, 0x1c1c1b07, 0x9e9e3ba5, 0xf3f30cff, 0xcfcf3ff0, 0xcdcdbf72, 0x5c5c4b17, 0xeaea52b8 .word 0x0e0e8f81, 0x65653d58, 0xf0f0cc3c, 0x64647d19, 0x9b9b7ee5, 0x16169187, 0x3d3d734e, 0xa2a208aa, 0xa1a1c869 .word 0xadadc76a, 0x06068583, 0xcaca7ab0, 0xc5c5b570, 0x9191f465, 0x6b6bb2d9, 0x2e2ea789, 0xe3e318fb, 0xafaf47e8 .word 0x3c3c330f, 0x2d2d674a, 0xc1c1b071, 0x59590e57, 0x7676e99f, 0xd4d4e135, 0x7878661e, 0x9090b424, 0x3838360e .word 0x7979265f, 0x8d8def62, 0x61613859, 0x474795d2, 0x8a8a2aa0, 0x9494b125, 0x8888aa22, 0xf1f18c7d, 0xececd73b .word 0x04040501, 0x8484a521, 0xe1e19879, 0x1e1e9b85, 0x535384d7, 0x00000000, 0x19195e47, 0x5d5d0b56, 0x7e7ee39d .word 0x4f4f9fd0, 0x9c9cbb27, 0x49491a53, 0x31317c4d, 0xd8d8ee36, 0x08080a02, 0x9f9f7be4, 0x828220a2, 0x1313d4c7 .word 0x2323e8cb, 0x7a7ae69c, 0xabab42e9, 0xfefe43bd, 0x2a2aa288, 0x4b4b9ad1, 0x01014041, 0x1f1fdbc4, 0xe0e0d838 .word 0xd6d661b7, 0x8e8e2fa1, 0xdfdf2bf4, 0xcbcb3af1, 0x3b3bf6cd, 0xe7e71dfa, 0x8585e560, 0x54544115, 0x868625a3 .word 0x838360e3, 0xbaba16ac, 0x7575295c, 0x929234a6, 0x6e6ef799, 0xd0d0e434, 0x6868721a, 0x55550154, 0xb6b619af .word 0x4e4edf91, 0xc8c8fa32, 0xc0c0f030, 0xd7d721f6, 0x3232bc8e, 0xc6c675b3, 0x8f8f6fe0, 0x7474691d, 0xdbdb2ef5 .word 0x8b8b6ae1, 0xb8b8962e, 0x0a0a8a80, 0x9999fe67, 0x2b2be2c9, 0x8181e061, 0x0303c0c3, 0xa4a48d29, 0x8c8caf23 .word 0xaeae07a9, 0x3434390d, 0x4d4d1f52, 0x3939764f, 0xbdbdd36e, 0x575781d6, 0x6f6fb7d8, 0xdcdceb37, 0x15155144 .word 0x7b7ba6dd, 0xf7f709fe, 0x3a3ab68c, 0xbcbc932f, 0x0c0c0f03, 0xffff03fc, 0xa9a9c26b, 0xc9c9ba73, 0xb5b5d96c .word 0xb1b1dc6d, 0x6d6d375a, 0x45451550, 0x3636b98f, 0x6c6c771b, 0xbebe13ad, 0x4a4ada90, 0xeeee57b9, 0x7777a9de .word 0xf2f24cbe, 0xfdfd837e, 0x44445511, 0x6767bdda, 0x71712c5d, 0x05054540, 0x7c7c631f, 0x40405010, 0x6969325b .word 0x6363b8db, 0x2828220a, 0x0707c5c2, 0xc4c4f531, 0x2222a88a, 0x969631a7, 0x3737f9ce, 0xeded977a, 0xf6f649bf .word 0xb4b4992d, 0xd1d1a475, 0x434390d3, 0x48485a12, 0xe2e258ba, 0x979771e6, 0xd2d264b6, 0xc2c270b2, 0x2626ad8b .word 0xa5a5cd68, 0x5e5ecb95, 0x2929624b, 0x30303c0c, 0x5a5ace94, 0xddddab76, 0xf9f9867f, 0x9595f164, 0xe6e65dbb .word 0xc7c735f2, 0x24242d09, 0x1717d1c6, 0xb9b9d66f, 0x1b1bdec5, 0x12129486, 0x60607818, 0xc3c330f3, 0xf5f5897c .word 0xb3b35cef, 0xe8e8d23a, 0x7373acdf, 0x3535794c, 0x8080a020, 0xe5e59d78, 0xbbbb56ed, 0x7d7d235e, 0xf8f8c63e .word 0x5f5f8bd4, 0x2f2fe7c8, 0xe4e4dd39, 0x21216849 .Ltbox3: .word 0x5b8ed55b, 0x42d09242, 0xa74deaa7, 0xfb06fdfb, 0x33fccf33, 0x8765e287, 0xf4c93df4, 0xde6bb5de, 0x584e1658 .word 0xda6eb4da, 0x50441450, 0x0bcac10b, 0xa08828a0, 0xef17f8ef, 0xb09c2cb0, 0x14110514, 0xac872bac, 0x9dfb669d .word 0x6af2986a, 0xd9ae77d9, 0xa8822aa8, 0xfa46bcfa, 0x10140410, 0x0fcfc00f, 0xaa02a8aa, 0x11544511, 0x4c5f134c .word 0x98be2698, 0x256d4825, 0x1a9e841a, 0x181e0618, 0x66fd9b66, 0x72ec9e72, 0x094a4309, 0x41105141, 0xd324f7d3 .word 0x46d59346, 0xbf53ecbf, 0x62f89a62, 0xe9927be9, 0xccff33cc, 0x51045551, 0x2c270b2c, 0x0d4f420d, 0xb759eeb7 .word 0x3ff3cc3f, 0xb21caeb2, 0x89ea6389, 0x9374e793, 0xce7fb1ce, 0x706c1c70, 0xa60daba6, 0x27edca27, 0x20280820 .word 0xa348eba3, 0x56c19756, 0x02808202, 0x7fa3dc7f, 0x52c49652, 0xeb12f9eb, 0xd5a174d5, 0x3eb38d3e, 0xfcc33ffc .word 0x9a3ea49a, 0x1d5b461d, 0x1c1b071c, 0x9e3ba59e, 0xf30cfff3, 0xcf3ff0cf, 0xcdbf72cd, 0x5c4b175c, 0xea52b8ea .word 0x0e8f810e, 0x653d5865, 0xf0cc3cf0, 0x647d1964, 0x9b7ee59b, 0x16918716, 0x3d734e3d, 0xa208aaa2, 0xa1c869a1 .word 0xadc76aad, 0x06858306, 0xca7ab0ca, 0xc5b570c5, 0x91f46591, 0x6bb2d96b, 0x2ea7892e, 0xe318fbe3, 0xaf47e8af .word 0x3c330f3c, 0x2d674a2d, 0xc1b071c1, 0x590e5759, 0x76e99f76, 0xd4e135d4, 0x78661e78, 0x90b42490, 0x38360e38 .word 0x79265f79, 0x8def628d, 0x61385961, 0x4795d247, 0x8a2aa08a, 0x94b12594, 0x88aa2288, 0xf18c7df1, 0xecd73bec .word 0x04050104, 0x84a52184, 0xe19879e1, 0x1e9b851e, 0x5384d753, 0x00000000, 0x195e4719, 0x5d0b565d, 0x7ee39d7e .word 0x4f9fd04f, 0x9cbb279c, 0x491a5349, 0x317c4d31, 0xd8ee36d8, 0x080a0208, 0x9f7be49f, 0x8220a282, 0x13d4c713 .word 0x23e8cb23, 0x7ae69c7a, 0xab42e9ab, 0xfe43bdfe, 0x2aa2882a, 0x4b9ad14b, 0x01404101, 0x1fdbc41f, 0xe0d838e0 .word 0xd661b7d6, 0x8e2fa18e, 0xdf2bf4df, 0xcb3af1cb, 0x3bf6cd3b, 0xe71dfae7, 0x85e56085, 0x54411554, 0x8625a386 .word 0x8360e383, 0xba16acba, 0x75295c75, 0x9234a692, 0x6ef7996e, 0xd0e434d0, 0x68721a68, 0x55015455, 0xb619afb6 .word 0x4edf914e, 0xc8fa32c8, 0xc0f030c0, 0xd721f6d7, 0x32bc8e32, 0xc675b3c6, 0x8f6fe08f, 0x74691d74, 0xdb2ef5db .word 0x8b6ae18b, 0xb8962eb8, 0x0a8a800a, 0x99fe6799, 0x2be2c92b, 0x81e06181, 0x03c0c303, 0xa48d29a4, 0x8caf238c .word 0xae07a9ae, 0x34390d34, 0x4d1f524d, 0x39764f39, 0xbdd36ebd, 0x5781d657, 0x6fb7d86f, 0xdceb37dc, 0x15514415 .word 0x7ba6dd7b, 0xf709fef7, 0x3ab68c3a, 0xbc932fbc, 0x0c0f030c, 0xff03fcff, 0xa9c26ba9, 0xc9ba73c9, 0xb5d96cb5 .word 0xb1dc6db1, 0x6d375a6d, 0x45155045, 0x36b98f36, 0x6c771b6c, 0xbe13adbe, 0x4ada904a, 0xee57b9ee, 0x77a9de77 .word 0xf24cbef2, 0xfd837efd, 0x44551144, 0x67bdda67, 0x712c5d71, 0x05454005, 0x7c631f7c, 0x40501040, 0x69325b69 .word 0x63b8db63, 0x28220a28, 0x07c5c207, 0xc4f531c4, 0x22a88a22, 0x9631a796, 0x37f9ce37, 0xed977aed, 0xf649bff6 .word 0xb4992db4, 0xd1a475d1, 0x4390d343, 0x485a1248, 0xe258bae2, 0x9771e697, 0xd264b6d2, 0xc270b2c2, 0x26ad8b26 .word 0xa5cd68a5, 0x5ecb955e, 0x29624b29, 0x303c0c30, 0x5ace945a, 0xddab76dd, 0xf9867ff9, 0x95f16495, 0xe65dbbe6 .word 0xc735f2c7, 0x242d0924, 0x17d1c617, 0xb9d66fb9, 0x1bdec51b, 0x12948612, 0x60781860, 0xc330f3c3, 0xf5897cf5 .word 0xb35cefb3, 0xe8d23ae8, 0x73acdf73, 0x35794c35, 0x80a02080, 0xe59d78e5, 0xbb56edbb, 0x7d235e7d, 0xf8c63ef8 .word 0x5f8bd45f, 0x2fe7c82f, 0xe4dd39e4, 0x21684921 .Ltbox4: .word 0x8ed55b5b, 0xd0924242, 0x4deaa7a7, 0x06fdfbfb, 0xfccf3333, 0x65e28787, 0xc93df4f4, 0x6bb5dede, 0x4e165858 .word 0x6eb4dada, 0x44145050, 0xcac10b0b, 0x8828a0a0, 0x17f8efef, 0x9c2cb0b0, 0x11051414, 0x872bacac, 0xfb669d9d .word 0xf2986a6a, 0xae77d9d9, 0x822aa8a8, 0x46bcfafa, 0x14041010, 0xcfc00f0f, 0x02a8aaaa, 0x54451111, 0x5f134c4c .word 0xbe269898, 0x6d482525, 0x9e841a1a, 0x1e061818, 0xfd9b6666, 0xec9e7272, 0x4a430909, 0x10514141, 0x24f7d3d3 .word 0xd5934646, 0x53ecbfbf, 0xf89a6262, 0x927be9e9, 0xff33cccc, 0x04555151, 0x270b2c2c, 0x4f420d0d, 0x59eeb7b7 .word 0xf3cc3f3f, 0x1caeb2b2, 0xea638989, 0x74e79393, 0x7fb1cece, 0x6c1c7070, 0x0daba6a6, 0xedca2727, 0x28082020 .word 0x48eba3a3, 0xc1975656, 0x80820202, 0xa3dc7f7f, 0xc4965252, 0x12f9ebeb, 0xa174d5d5, 0xb38d3e3e, 0xc33ffcfc .word 0x3ea49a9a, 0x5b461d1d, 0x1b071c1c, 0x3ba59e9e, 0x0cfff3f3, 0x3ff0cfcf, 0xbf72cdcd, 0x4b175c5c, 0x52b8eaea .word 0x8f810e0e, 0x3d586565, 0xcc3cf0f0, 0x7d196464, 0x7ee59b9b, 0x91871616, 0x734e3d3d, 0x08aaa2a2, 0xc869a1a1 .word 0xc76aadad, 0x85830606, 0x7ab0caca, 0xb570c5c5, 0xf4659191, 0xb2d96b6b, 0xa7892e2e, 0x18fbe3e3, 0x47e8afaf .word 0x330f3c3c, 0x674a2d2d, 0xb071c1c1, 0x0e575959, 0xe99f7676, 0xe135d4d4, 0x661e7878, 0xb4249090, 0x360e3838 .word 0x265f7979, 0xef628d8d, 0x38596161, 0x95d24747, 0x2aa08a8a, 0xb1259494, 0xaa228888, 0x8c7df1f1, 0xd73becec .word 0x05010404, 0xa5218484, 0x9879e1e1, 0x9b851e1e, 0x84d75353, 0x00000000, 0x5e471919, 0x0b565d5d, 0xe39d7e7e .word 0x9fd04f4f, 0xbb279c9c, 0x1a534949, 0x7c4d3131, 0xee36d8d8, 0x0a020808, 0x7be49f9f, 0x20a28282, 0xd4c71313 .word 0xe8cb2323, 0xe69c7a7a, 0x42e9abab, 0x43bdfefe, 0xa2882a2a, 0x9ad14b4b, 0x40410101, 0xdbc41f1f, 0xd838e0e0 .word 0x61b7d6d6, 0x2fa18e8e, 0x2bf4dfdf, 0x3af1cbcb, 0xf6cd3b3b, 0x1dfae7e7, 0xe5608585, 0x41155454, 0x25a38686 .word 0x60e38383, 0x16acbaba, 0x295c7575, 0x34a69292, 0xf7996e6e, 0xe434d0d0, 0x721a6868, 0x01545555, 0x19afb6b6 .word 0xdf914e4e, 0xfa32c8c8, 0xf030c0c0, 0x21f6d7d7, 0xbc8e3232, 0x75b3c6c6, 0x6fe08f8f, 0x691d7474, 0x2ef5dbdb .word 0x6ae18b8b, 0x962eb8b8, 0x8a800a0a, 0xfe679999, 0xe2c92b2b, 0xe0618181, 0xc0c30303, 0x8d29a4a4, 0xaf238c8c .word 0x07a9aeae, 0x390d3434, 0x1f524d4d, 0x764f3939, 0xd36ebdbd, 0x81d65757, 0xb7d86f6f, 0xeb37dcdc, 0x51441515 .word 0xa6dd7b7b, 0x09fef7f7, 0xb68c3a3a, 0x932fbcbc, 0x0f030c0c, 0x03fcffff, 0xc26ba9a9, 0xba73c9c9, 0xd96cb5b5 .word 0xdc6db1b1, 0x375a6d6d, 0x15504545, 0xb98f3636, 0x771b6c6c, 0x13adbebe, 0xda904a4a, 0x57b9eeee, 0xa9de7777 .word 0x4cbef2f2, 0x837efdfd, 0x55114444, 0xbdda6767, 0x2c5d7171, 0x45400505, 0x631f7c7c, 0x50104040, 0x325b6969 .word 0xb8db6363, 0x220a2828, 0xc5c20707, 0xf531c4c4, 0xa88a2222, 0x31a79696, 0xf9ce3737, 0x977aeded, 0x49bff6f6 .word 0x992db4b4, 0xa475d1d1, 0x90d34343, 0x5a124848, 0x58bae2e2, 0x71e69797, 0x64b6d2d2, 0x70b2c2c2, 0xad8b2626 .word 0xcd68a5a5, 0xcb955e5e, 0x624b2929, 0x3c0c3030, 0xce945a5a, 0xab76dddd, 0x867ff9f9, 0xf1649595, 0x5dbbe6e6 .word 0x35f2c7c7, 0x2d092424, 0xd1c61717, 0xd66fb9b9, 0xdec51b1b, 0x94861212, 0x78186060, 0x30f3c3c3, 0x897cf5f5 .word 0x5cefb3b3, 0xd23ae8e8, 0xacdf7373, 0x794c3535, 0xa0208080, 0x9d78e5e5, 0x56edbbbb, 0x235e7d7d, 0xc63ef8f8 .word 0x8bd45f5f, 0xe7c82f2f, 0xdd39e4e4, 0x68492121 #ifdef HITLS_BIG_ENDIAN .Lxts_magic: .quad 0x0101010101010101,0x0101010101010187 .Lsbox_magic: .quad 0x0306090c0f020508,0x0b0e0104070a0d00 .quad 0x22581a6002783a40,0x62185a2042387a00 .quad 0xc10bb67c4a803df7,0x15df62a89e54e923 .quad 0x1407c6d56c7fbead,0xb9aa6b78c1d21300 .quad 0xe383c1a1fe9edcbc,0x6404462679195b3b .quad 0x0E0D0C0F0A09080B,0x0605040702010003 .quad 0x0D0C0F0E09080B0A,0x0504070601000302 .quad 0x0C0F0E0D080B0A09,0x0407060500030201 #else .Lxts_magic: .quad 0x0101010101010187,0x0101010101010101 .Lsbox_magic: .quad 0x0b0e0104070a0d00,0x0306090c0f020508 .quad 0x62185a2042387a00,0x22581a6002783a40 .quad 0x15df62a89e54e923,0xc10bb67c4a803df7 .quad 0xb9aa6b78c1d21300,0x1407c6d56c7fbead .quad 0x6404462679195b3b,0xe383c1a1fe9edcbc .quad 0x0605040702010003,0x0E0D0C0F0A09080B .quad 0x0504070601000302,0x0D0C0F0E09080B0A .quad 0x0407060500030201,0x0C0F0E0D080B0A09 #endif .macro LoadSbox adrp x15,.Lsbox_magic add x15,x15,:lo12:.Lsbox_magic ldr MaskQ, [x15] ldr TAHMatQ, [x15, #16] ldr TALMatQ, [x15, #32] ldr ATAHMatQ, [x15, #48] ldr ATALMatQ, [x15, #64] ldr vtmp5q, [x15, #80] ldr vtmp6q, [x15, #96] ldr vtmp7q, [x15, #112] .endm .macro round x1, x2, x3, x4, rk eor word0,\x2, \x3 eor word0, word0, \rk eor word0, word0, \x4 and word1, word0, #0xff ldr word1, [tbox0,xword1,lsl #2] eor \x1, word1, \x1 ubfx word1, word0,#8,#8 ldr word1, [tbox1, xword1, lsl #2] eor \x1, word1, \x1 ubfx word1, word0, #16, #8 ldr word1,[tbox2, xword1, lsl #2] eor \x1, word1, \x1 lsr word1, word0, #24 ldr word1, [tbox3, xword1, lsl #2] eor \x1, word1, \x1 .endm .macro EncRound4 offset1, offset2 ldp word2, word3,[rks, \offset1] round w8, w9, w10, w11, word2 round w9, w10, w11, w8, word3 ldp word2, word3,[rks, \offset2] round w10, w11, w8, w9, word2 round w11, w8, w9, w10, word3 .endm .macro EncRound EncRound4 0, 8 EncRound4 16, 24 EncRound4 32, 40 EncRound4 48, 56 EncRound4 64, 72 EncRound4 80, 88 EncRound4 96, 104 EncRound4 112, 120 .endm .macro transpose dat0s, dat1s, dat2s, dat3s, dat0d, dat1d, dat2d, dat3d, vt0s, vt1s, vt2s, vt3s, vt0d, vt1d, vt2d, vt3d zip1 \vt0s, \dat0s, \dat1s zip2 \vt1s, \dat0s, \dat1s zip1 \vt2s, \dat2s, \dat3s zip2 \vt3s, \dat2s, \dat3s zip1 \dat0d, \vt0d, \vt2d zip2 \dat1d, \vt0d, \vt2d zip1 \dat2d, \vt1d, \vt3d zip2 \dat3d, \vt1d, \vt3d .endm .macro Encrypt1blkNorevCtr mov w8,ivec.s[0] mov w9,ivec.s[1] mov w10,ivec.s[2] mov w11,ivec.s[3] EncRound mov ivec.s[0],w11 mov ivec.s[1],w10 mov ivec.s[2],w9 mov ivec.s[3],w8 #ifndef HITLS_BIG_ENDIAN rev32 v3.16b,v3.16b #endif .endm # matrix multiplication Mat*x = (lowerMat*x) ^ (higherMat*x) .macro MulMatrix x, higherMat, lowerMat, tmp ushr \tmp, \x, 4 and \x, \x, ANDMaskV.16b tbl \x, {\lowerMat}, \x tbl \tmp, {\higherMat}, \tmp eor \x, \x, \tmp .endm # matrix multiplication Mat*x = (lowerMat*x) ^ (higherMat*x) .macro MulMatrixOut x, higherMat, lowerMat, tmp, out ushr \tmp, \x, 4 and \x, \x, ANDMaskV.16b tbl \x, {\lowerMat}, \x tbl \tmp, {\higherMat}, \tmp eor \out, \x, \tmp .endm # Sbox operations for 4-lane of words .macro Sbox dat, dat2 movi ANDMaskV.16b, #0x0f // optimize Sbox using AESE instruction tbl v0.16b, {\dat}, MaskV.16b MulMatrix v0.16b, TAHMatV.16b, TALMatV.16b, v24.16b eor v1.16b, v1.16b, v1.16b aese v0.16b, v1.16b MulMatrix v0.16b, ATAHMatV.16b, ATALMatV.16b, v24.16b mov \dat, v0.16b // linear transformation ushr v0.4s, \dat2,32-2 ushr v1.4s, \dat2,32-10 ushr v2.4s, \dat2,32-18 ushr v3.4s, \dat2,32-24 sli v0.4s, \dat2,2 sli v1.4s, \dat2,10 sli v2.4s, \dat2,18 sli v3.4s, \dat2,24 eor v24.16b, v0.16b, \dat eor v24.16b, v24.16b, v1.16b eor \dat, v2.16b, v3.16b eor \dat, \dat, v24.16b .endm # sm4 for 4-lanes of data, in neon registers data0/data1/data2/data3 .macro Sm44blks kptr ldp wtmp0, wtmp1,[\kptr],8 dup rk0.4s, wtmp0 dup rk1.4s, wtmp1 // B0 ^= SBOX(B1 ^ B2 ^ B3 ^ RK0) eor rka.16b, v6.16b, v7.16b eor rk0.16b, v5.16b, rk0.16b eor rk0.16b, rka.16b, rk0.16b Sbox rk0.16b, rk0.4s eor v4.16b, v4.16b, rk0.16b // B1 ^= SBOX(B0 ^ B2 ^ B3 ^ RK1) eor rka.16b, rka.16b, v4.16b eor rk1.16b, rka.16b, rk1.16b Sbox rk1.16b, rk1.4s ldp wtmp0, wtmp1,[\kptr],8 eor v5.16b,v5.16b, rk1.16b dup rk0.4s, wtmp0 dup rk1.4s, wtmp1 // B2 ^= SBOX(B0 ^ B1 ^ B3 ^ RK2) eor rka.16b, v4.16b, v5.16b eor rk0.16b, v7.16b, rk0.16b eor rk0.16b, rka.16b, rk0.16b Sbox rk0.16b, rk0.4s eor v6.16b, v6.16b, rk0.16b // B3 ^= SBOX(B0 ^ B1 ^ B2 ^ RK3) eor rka.16b, rka.16b, v6.16b eor rk1.16b, rka.16b, rk1.16b Sbox rk1.16b, rk1.4s eor v7.16b, v7.16b, rk1.16b .endm .macro Encrypt4blks mov ptr, rks mov counter,#8 10: Sm44blks ptr subs counter, counter,#1 b.ne 10b #ifndef HITLS_BIG_ENDIAN rev32 v3.16b,v4.16b rev32 v2.16b,v5.16b rev32 v1.16b,v6.16b rev32 v0.16b,v7.16b #else mov v3.16b,v4.16b mov v2.16b,v5.16b mov v1.16b,v6.16b mov v0.16b,v7.16b #endif .endm # Sbox operation for 8-lane of words .macro SboxDouble dat datx movi ANDMaskV.16b, #0x0f // optimize Sbox using AESE instruction tbl v0.16b, {rk0.16b}, MaskV.16b tbl v1.16b, {rk1.16b}, MaskV.16b MulMatrix v0.16b, TAHMatV.16b, TALMatV.16b, v24.16b MulMatrix v1.16b, TAHMatV.16b, TALMatV.16b, v24.16b eor vtmp5.16b, vtmp5.16b, vtmp5.16b aese v0.16b,vtmp5.16b aese v1.16b,vtmp5.16b MulMatrixOut v0.16b, ATAHMatV.16b, ATALMatV.16b, v24.16b, rk0.16b MulMatrixOut v1.16b, ATAHMatV.16b, ATALMatV.16b, v24.16b, rk1.16b // linear transformation ushr v0.4s,rk0.4s,32-2 ushr vtmp5.4s,rk1.4s,32-2 ushr v1.4s,rk0.4s,32-10 ushr v2.4s,rk0.4s,32-18 ushr v3.4s,rk0.4s,32-24 sli v0.4s,rk0.4s,2 sli vtmp5.4s,rk1.4s,2 sli v1.4s,rk0.4s,10 sli v2.4s,rk0.4s,18 sli v3.4s,rk0.4s,24 eor v24.16b,v0.16b,rk0.16b eor v24.16b,v24.16b,v1.16b eor rk0.16b,v2.16b,v3.16b eor rk0.16b,rk0.16b,v24.16b ushr v1.4s,rk1.4s,32-10 ushr v2.4s,rk1.4s,32-18 ushr v3.4s,rk1.4s,32-24 sli v1.4s,rk1.4s,10 sli v2.4s,rk1.4s,18 sli v3.4s,rk1.4s,24 eor v24.16b,vtmp5.16b,rk1.16b eor v24.16b,v24.16b,v1.16b eor rk1.16b,v2.16b,v3.16b eor rk1.16b,rk1.16b,v24.16b .endm .macro SboxThree dat, datx, dat1 movi ANDMaskV.16b, #0x0f // optimize sbox using AESE instruction tbl v0.16b, {\dat}, MaskV.16b tbl v1.16b, {\datx}, MaskV.16b tbl v2.16b, {\dat1}, MaskV.16b eor v3.16b, v3.16b, v3.16b MulMatrix v0.16b, TAHMatV.16b, TALMatV.16b, v24.16b MulMatrix v1.16b, TAHMatV.16b, TALMatV.16b, v24.16b aese v0.16b, v3.16b MulMatrix v2.16b, TAHMatV.16b, TALMatV.16b, v24.16b aese v1.16b, v3.16b aese v2.16b, v3.16b MulMatrixOut v0.16b, ATAHMatV.16b, ATALMatV.16b, v24.16b, \dat MulMatrixOut v1.16b, ATAHMatV.16b, ATALMatV.16b, v24.16b, \datx MulMatrixOut v2.16b, ATAHMatV.16b, ATALMatV.16b, v24.16b, \dat1 // linear transformation tbl v0.16b, {\dat}, vtmp5.16b // shitf left 8 tbl v1.16b, {\datx}, vtmp5.16b tbl v2.16b, {\dat1}, vtmp5.16b tbl v3.16b, {\dat}, v22.16b // shitf left 16 tbl v24.16b, {\datx}, v22.16b tbl ANDMaskV.16b, {\dat1}, v22.16b eor v0.16b, v0.16b, \dat eor v1.16b, v1.16b, \datx eor v2.16b, v2.16b, \dat1 eor v0.16b, v0.16b, v3.16b eor v1.16b, v1.16b, v24.16b eor v2.16b, v2.16b, ANDMaskV.16b shl v3.4s, v0.4s, #2 // shift left by 2 bits, equivalent to v12<<2 xor v12<<10 xor v12<<18 sri v3.4s, v0.4s, #30 shl v24.4s, v1.4s, #2 sri v24.4s, v1.4s, #30 shl ANDMaskV.4s, v2.4s, #2 sri ANDMaskV.4s, v2.4s, #30 tbl v0.16b, {\dat}, v23.16b // shitf left 24 tbl v1.16b, {\datx}, v23.16b tbl v2.16b, {\dat1}, v23.16b eor \dat, \dat, v3.16b eor \datx, \datx, v24.16b eor \dat1, \dat1, ANDMaskV.16b eor \dat, v0.16b, \dat eor \datx, v1.16b, \datx eor \dat1, v2.16b, \dat1 .endm .macro Sm48blks kptr ldp wtmp0, wtmp1,[\kptr],8 // B0 ^= SBOX(B1 ^ B2 ^ B3 ^ RK0) dup rk0.4s, wtmp0 eor rka.16b,v6.16b,v7.16b eor rkb.16b,v10.16b,v11.16b eor v0.16b,v5.16b,rk0.16b eor v1.16b,v9.16b,rk0.16b eor rk0.16b, rka.16b,v0.16b eor rk1.16b, rkb.16b,v1.16b SboxDouble eor v4.16b, v4.16b, rk0.16b eor v8.16b,v8.16b, rk1.16b // B1 ^= SBOX(B0 ^ B2 ^ B3 ^ RK1) dup rk1.4s, wtmp1 eor rka.16b,rka.16b,v4.16b eor rkb.16b,rkb.16b,v8.16b eor rk0.16b,rka.16b,rk1.16b eor rk1.16b,rkb.16b,rk1.16b SboxDouble ldp wtmp0, wtmp1,[\kptr],8 eor v5.16b,v5.16b,rk0.16b eor v9.16b,v9.16b,rk1.16b // B2 ^= SBOX(B0 ^ B1 ^ B3 ^ RK2) dup rk0.4s, wtmp0 eor rka.16b,v4.16b,v5.16b eor rkb.16b,v8.16b,v9.16b eor v0.16b,v7.16b,rk0.16b eor v1.16b,v11.16b,rk0.16b eor rk0.16b,rka.16b,v0.16b eor rk1.16b,rkb.16b,v1.16b SboxDouble eor v6.16b,v6.16b,rk0.16b eor v10.16b,v10.16b,rk1.16b // B3 ^= SBOX(B0 ^ B1 ^ B2 ^ RK3) dup rk1.4s, wtmp1 eor rka.16b,rka.16b,v6.16b eor rkb.16b,rkb.16b,v10.16b eor rk0.16b,rka.16b,rk1.16b eor rk1.16b,rkb.16b,rk1.16b SboxDouble eor v7.16b,v7.16b,rk0.16b eor v11.16b,v11.16b,rk1.16b .endm .macro Sm412blks kptr ldp wtmp0,wtmp1,[\kptr],8 // B0 ^= SBOX(B1 ^ B2 ^ B3 ^ RK0) dup rk0.4s,wtmp0 eor rka.16b,v6.16b,v7.16b eor rkb.16b,v10.16b,v11.16b eor rkc.16b,v18.16b,v19.16b eor v0.16b,v5.16b,rk0.16b eor v1.16b,v9.16b,rk0.16b eor v2.16b,v17.16b,rk0.16b eor rk0.16b,rka.16b,v0.16b eor rk1.16b,rkb.16b,v1.16b eor rk2.16b,rkc.16b,v2.16b SboxThree rk0.16b, rk1.16b, rk2.16b eor v4.16b,v4.16b,rk0.16b eor v8.16b,v8.16b,rk1.16b eor v16.16b,v16.16b,rk2.16b // B1 ^= SBOX(B0 ^ B2 ^ B3 ^ RK1) dup rk1.4s,wtmp1 eor rka.16b,rka.16b,v4.16b eor rkb.16b,rkb.16b,v8.16b eor rkc.16b,rkc.16b,v16.16b eor rk0.16b,rka.16b,rk1.16b eor rk2.16b,rkc.16b,rk1.16b eor rk1.16b,rkb.16b,rk1.16b SboxThree rk0.16b, rk1.16b, rk2.16b ldp wtmp0,wtmp1,[\kptr],8 eor v5.16b,v5.16b,rk0.16b eor v9.16b,v9.16b,rk1.16b eor v17.16b,v17.16b,rk2.16b // B2 ^= SBOX(B0 ^ B1 ^ B3 ^ RK2) dup rk0.4s,wtmp0 eor rka.16b,v4.16b,v5.16b eor rkb.16b,v8.16b,v9.16b eor rkc.16b,v16.16b,v17.16b eor v0.16b,v7.16b,rk0.16b eor v1.16b,v11.16b,rk0.16b eor v2.16b,v19.16b,rk0.16b eor rk0.16b,rka.16b,v0.16b eor rk1.16b,rkb.16b,v1.16b eor rk2.16b,rkc.16b,v2.16b SboxThree rk0.16b, rk1.16b, rk2.16b eor v6.16b,v6.16b,rk0.16b eor v10.16b,v10.16b,rk1.16b eor v18.16b,v18.16b,rk2.16b // B3 ^= SBOX(B0 ^ B1 ^ B2 ^ RK3) dup rk1.4s,wtmp1 eor rka.16b,rka.16b,v6.16b eor rkb.16b,rkb.16b,v10.16b eor rkc.16b,rkc.16b,v18.16b eor rk0.16b,rka.16b,rk1.16b eor rk2.16b,rkc.16b,rk1.16b eor rk1.16b,rkb.16b,rk1.16b SboxThree rk0.16b, rk1.16b, rk2.16b eor v7.16b,v7.16b,rk0.16b eor v11.16b,v11.16b,rk1.16b eor v19.16b,v19.16b,rk2.16b .endm .macro Encrypt8blks mov ptr, rks mov counter, #8 10: Sm48blks ptr subs counter, counter,#1 b.ne 10b #ifndef HITLS_BIG_ENDIAN rev32 v3.16b,v4.16b rev32 v2.16b,v5.16b rev32 v1.16b,v6.16b rev32 v0.16b,v7.16b rev32 v7.16b,v8.16b rev32 v6.16b,v9.16b rev32 v5.16b,v10.16b rev32 v4.16b,v11.16b #else mov v3.16b,v4.16b mov v2.16b,v5.16b mov v1.16b,v6.16b mov v0.16b,v7.16b mov v7.16b,v8.16b mov v6.16b,v9.16b mov v5.16b,v10.16b mov v4.16b,v11.16b #endif .endm .macro Encrypt12blks mov ptr, rks mov counter, #8 10: Sm412blks ptr subs counter,counter,#1 b.ne 10b // last reverse transform #ifndef HITLS_BIG_ENDIAN rev32 v3.16b,v4.16b rev32 v2.16b,v5.16b rev32 v1.16b,v6.16b rev32 v0.16b,v7.16b rev32 v7.16b,v8.16b rev32 v6.16b,v9.16b rev32 v5.16b,v10.16b rev32 v4.16b,v11.16b rev32 v11.16b,v16.16b rev32 v10.16b,v17.16b rev32 v9.16b,v18.16b rev32 v8.16b,v19.16b #else mov v3.16b,v4.16b mov v2.16b,v5.16b mov v1.16b,v6.16b mov v0.16b,v7.16b mov v7.16b,v8.16b mov v6.16b,v9.16b mov v5.16b,v10.16b mov v4.16b,v11.16b mov v11.16b,v16.16b mov v10.16b,v17.16b mov v9.16b,v18.16b mov v8.16b,v19.16b #endif .endm .text .type Sm4Enc4blks,%function .align 4 Sm4Enc4blks: AARCH64_PACIASP Encrypt4blks AARCH64_AUTIASP ret .size Sm4Enc4blks,.-Sm4Enc4blks .type Sm4Enc8blks,%function .align 4 Sm4Enc8blks: AARCH64_PACIASP Encrypt8blks AARCH64_AUTIASP ret .size Sm4Enc8blks,.-Sm4Enc8blks .type Sm4Enc12blks,%function .align 4 Sm4Enc12blks: AARCH64_PACIASP Encrypt12blks AARCH64_AUTIASP ret .size Sm4Enc12blks,.-Sm4Enc12blks .globl Vpsm4EcbEncrypt .type Vpsm4EcbEncrypt,%function .align 5 Vpsm4EcbEncrypt: AARCH64_PACIASP // convert length into blocks lsr x2,x2,4 stp d8,d9,[sp,#-112]! stp d10,d11,[sp,#16] stp d12,d13,[sp,#32] stp d14,d15,[sp,#48] stp x29,x30,[sp,#64] stp x19,x20,[sp,#80] stp x21,x22,[sp,#96] LoadSbox .Lecb_12_blocks_process: cmp blocks,#12 b.lt .Lecb_8_blocks_process ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[inp],#64 ld4 {v8.4s,v9.4s,v10.4s,v11.4s},[inp],#64 ld4 {v16.4s,v17.4s,v18.4s,v19.4s},[inp],#64 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b rev32 v16.16b,v16.16b rev32 v17.16b,v17.16b rev32 v18.16b,v18.16b rev32 v19.16b,v19.16b #endif bl Sm4Enc12blks st4 {v0.4s,v1.4s,v2.4s,v3.4s},[outp],#64 st4 {v4.4s,v5.4s,v6.4s,v7.4s},[outp],#64 st4 {v8.4s,v9.4s,v10.4s,v11.4s},[outp],#64 subs blocks,blocks,#12 b.gt .Lecb_12_blocks_process b 100f .Lecb_8_blocks_process: cmp blocks, #8 b.lt .Lecb_4_blocks_process ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[x0],#64 ld4 {v8.4s,v9.4s,v10.4s,v11.4s},[x0],#64 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b #endif bl Sm4Enc8blks st4 {v0.4s,v1.4s,v2.4s,v3.4s},[x1],#64 st4 {v4.4s,v5.4s,v6.4s,v7.4s},[x1],#64 subs blocks,blocks,#8 b.gt .Lecb_8_blocks_process b 100f .Lecb_4_blocks_process: cmp blocks,#4 b.lt 1f ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[x0],#64 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b, v4.16b rev32 v5.16b, v5.16b rev32 v6.16b, v6.16b rev32 v7.16b, v7.16b #endif bl Sm4Enc4blks st4 {v0.4s,v1.4s,v2.4s,v3.4s},[x1],#64 sub blocks,blocks,#4 1: // process last block cmp blocks,#1 b.lt 100f b.gt 1f adrp x19, .Ltbox1 add x19,x19,:lo12:.Ltbox1 adrp x20, .Ltbox2 add x20,x20,:lo12:.Ltbox2 adrp x21, .Ltbox3 add x21,x21,:lo12:.Ltbox3 adrp x22, .Ltbox4 add x22,x22,:lo12:.Ltbox4 ldp w8,w9,[inp],#8 ldp w10,w11,[inp],#8 #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif EncRound #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif stp w11,w10,[outp] stp w9,w8,[outp,#8] b 100f 1: // process last 2 blocks ld4 {v4.s,v5.s,v6.s,v7.s}[0],[inp],#16 ld4 {v4.s,v5.s,v6.s,v7.s}[1],[inp],#16 cmp blocks,#2 b.gt 1f #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks st4 {v0.s,v1.s,v2.s,v3.s}[0],[outp],#16 st4 {v0.s,v1.s,v2.s,v3.s}[1],[outp] b 100f // 1: // process last 3 blocks ld4 {v4.s,v5.s,v6.s,v7.s}[2],[inp],#16 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks st4 {v0.s,v1.s,v2.s,v3.s}[0],[outp],#16 st4 {v0.s,v1.s,v2.s,v3.s}[1],[outp],#16 st4 {v0.s,v1.s,v2.s,v3.s}[2],[outp] 100: ldp d10,d11,[sp,#16] ldp d12,d13,[sp,#32] ldp d14,d15,[sp,#48] ldp x29,x30,[sp,#64] ldp x19,x20,[sp,#80] ldp x21,x22,[sp,#96] ldp d8,d9,[sp],#112 AARCH64_AUTIASP ret .size Vpsm4EcbEncrypt,.-Vpsm4EcbEncrypt .globl Vpsm4CbcEncrypt .type Vpsm4CbcEncrypt,%function .align 5 Vpsm4CbcEncrypt: AARCH64_PACIASP lsr len,len,4 stp x29,x30,[sp,#-48]! stp x19,x20,[sp,#16] stp x21,x22,[sp,#32] // load tbox adrp x19, .Ltbox1 add x19,x19,:lo12:.Ltbox1 adrp x20, .Ltbox2 add x20,x20,:lo12:.Ltbox2 adrp x21, .Ltbox3 add x21,x21,:lo12:.Ltbox3 adrp x22, .Ltbox4 add x22,x22,:lo12:.Ltbox4 cbz w5,.Ldec // load iv ldp w8,w9,[ivp] ldp w10,w11,[ivp,#8] .Lcbc_1_block_enc: subs blocks,blocks,#1 b.lt 2f ldp w6,w7,[inp],#8 ldp w16,w17,[inp],#8 eor w8,w8,w6 eor w9,w9,w7 eor w10,w10,w16 eor w11,w11,w17 #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif EncRound #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif // reverse to store mov w6,w8 mov w8,w11 mov w11,w6 mov w7,w9 mov w9,w10 mov w10,w7 stp w8,w9,[outp],#8 stp w10,w11,[outp],#8 b .Lcbc_1_block_enc 2: // save back IV stp w8,w9,[ivp] stp w10,w11,[ivp,#8] ldp x19,x20,[sp,#16] ldp x21,x22,[sp,#32] ldp x29,x30,[sp],#48 AARCH64_AUTIASP ret .Ldec: LoadSbox // decryption mode starts stp d8,d9,[sp,#-64]! stp d10,d11,[sp,#16] stp d12,d13,[sp,#32] stp d14,d15,[sp,#48] .Lcbc_12_blocks_dec: cmp w2,#12 b.lt .Lcbc_8_blocks_dec ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[x0] add x10,x0,#64 ld4 {v8.4s,v9.4s,v10.4s,v11.4s},[x10] add x10,x10,#64 ld4 {v16.4s,v17.4s,v18.4s,v19.4s},[x10] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b rev32 v16.16b,v16.16b rev32 v17.16b,v17.16b rev32 v18.16b,v18.16b rev32 v19.16b,v19.16b #endif bl Sm4Enc12blks // transpose to xor iv transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v16.4s,v17.4s,v18.4s,v19.4s,v16.2d,v17.2d,v18.2d,v19.2d transpose v4.4s,v5.4s,v6.4s,v7.4s,v4.2d,v5.2d,v6.2d,v7.2d,v16.4s,v17.4s,v18.4s,v19.4s,v16.2d,v17.2d,v18.2d,v19.2d transpose v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d,v16.4s,v17.4s,v18.4s,v19.4s,v16.2d,v17.2d,v18.2d,v19.2d ld1 {ivec1.4s},[ivp] ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[inp],#64 eor v0.16b,v0.16b,ivec1.16b ld1 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v1.16b,v1.16b,v16.16b eor v2.16b,v2.16b,v17.16b eor v3.16b,v3.16b,v18.16b st1 {v0.4s,v1.4s,v2.4s,v3.4s},[outp],#64 eor v4.16b,v4.16b,v19.16b eor v5.16b,v5.16b,v12.16b eor v6.16b,v6.16b,v13.16b eor v7.16b,v7.16b,v14.16b st1 {v4.4s,v5.4s,v6.4s,v7.4s},[outp],#64 ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[inp],#64 eor v8.16b,v8.16b,v15.16b eor v9.16b,v9.16b,v16.16b eor v10.16b,v10.16b,v17.16b eor v11.16b,v11.16b,v18.16b st1 {v8.4s,v9.4s,v10.4s,v11.4s},[outp],#64 // save back iv st1 {v19.4s}, [ivp] subs blocks,blocks,#12 b.gt .Lcbc_12_blocks_dec b 100f .Lcbc_8_blocks_dec: cmp blocks,#8 b.lt 1f ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[inp] add ptr, inp, #64 ld4 {v8.4s,v9.4s,v10.4s,v11.4s},[ptr] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b #endif bl Sm4Enc8blks transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d transpose v4.4s,v5.4s,v6.4s,v7.4s,v4.2d,v5.2d,v6.2d,v7.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d ld1 {ivec1.4s},[ivp] ld1 {v8.4s,v9.4s,v10.4s,v11.4s},[inp],#64 // note ivec1 and v15 are resuing the same register // care needs to be taken to avoid conflict eor v0.16b,v0.16b,ivec1.16b ld1 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v1.16b,v1.16b,v8.16b eor v2.16b,v2.16b,v9.16b eor v3.16b,v3.16b,v10.16b // save back IV st1 {v15.4s}, [ivp] eor v4.16b,v4.16b,v11.16b eor v5.16b,v5.16b,v12.16b eor v6.16b,v6.16b,v13.16b eor v7.16b,v7.16b,v14.16b st1 {v0.4s,v1.4s,v2.4s,v3.4s},[outp],#64 st1 {v4.4s,v5.4s,v6.4s,v7.4s},[outp],#64 subs blocks,blocks,#8 b.gt .Lcbc_8_blocks_dec b.eq 100f 1: ld1 {ivec1.4s},[ivp] .Lcbc_4_blocks_dec: cmp blocks,#4 b.lt 1f ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[inp] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks ld1 {v4.4s,v5.4s,v6.4s,v7.4s},[inp],#64 transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d eor v0.16b,v0.16b,ivec1.16b eor v1.16b,v1.16b,v4.16b orr v15.16b,v7.16b,v7.16b eor v2.16b,v2.16b,v5.16b eor v3.16b,v3.16b,v6.16b st1 {v0.4s,v1.4s,v2.4s,v3.4s},[outp],#64 // save back IV st1 {v7.4s}, [ivp] subs blocks,blocks,#4 b.gt .Lcbc_4_blocks_dec b 100f 1: // last block subs blocks,blocks,#1 b.lt 100f b.gt 1f // load iv ldp w6,w7,[ivp] ldp w16,w17,[ivp,#8] ldp w8,w9,[inp] ldp w10,w11,[inp,#8] // store back iv stp w8,w9,[ivp] stp w10,w11,[ivp,#8] #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif EncRound #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif eor w11,w11,w6 eor w10,w10,w7 eor w9,w9,w16 eor w8,w8,w17 stp w11,w10,[outp],#8 stp w9,w8,[outp],#8 b 100f 1: // last two blocks ld4 {v4.s,v5.s,v6.s,v7.s}[0], [inp] add ptr,inp,#16 ld4 {v4.s,v5.s,v6.s,v7.s}[1],[ptr],#16 subs blocks,blocks,1 b.gt 1f #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks ld1 {v4.4s,v5.4s},[inp],#32 transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d eor v0.16b,v0.16b,ivec1.16b eor v1.16b,v1.16b,v4.16b st1 {v0.4s,v1.4s},[outp],#32 // save back IV st1 {v5.4s}, [ivp] b 100f 1: // last 3 blocks ld4 {v4.s,v5.s,v6.s,v7.s}[2],[ptr] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks ld1 {v4.4s,v5.4s,v6.4s},[inp],#48 transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d eor v0.16b,v0.16b,ivec1.16b eor v1.16b,v1.16b,v4.16b eor v2.16b,v2.16b,v5.16b st1 {v0.4s,v1.4s,v2.4s},[outp],#48 // save back IV st1 {v6.4s}, [ivp] 100: ldp d10,d11,[sp,#16] ldp d12,d13,[sp,#32] ldp d14,d15,[sp,#48] ldp d8,d9,[sp],#64 ldp x19,x20,[sp,#16] ldp x21,x22,[sp,#32] ldp x29,x30,[sp],#48 AARCH64_AUTIASP ret .size Vpsm4CbcEncrypt,.-Vpsm4CbcEncrypt # void Vpsm4Ctr32EncryptBlocks(const uint8_t *in, uint8_t *out, uint64_t blocks, const uint32_t *key, uint8_t *iv); .globl Vpsm4Ctr32EncryptBlocks .type Vpsm4Ctr32EncryptBlocks,%function .align 5 Vpsm4Ctr32EncryptBlocks: AARCH64_PACIASP ld1 {ivec.4s},[ivp] #ifndef HITLS_BIG_ENDIAN rev32 v3.16b,v3.16b #endif LoadSbox cmp blocks,#1 b.ne 1f // fast processing for one single block without // context saving overhead stp x19,x20,[sp,#-32]! stp x21,x22,[sp,#16] adrp x19, .Ltbox1 add x19,x19,:lo12:.Ltbox1 adrp x20, .Ltbox2 add x20,x20,:lo12:.Ltbox2 adrp x21, .Ltbox3 add x21,x21,:lo12:.Ltbox3 adrp x22, .Ltbox4 add x22,x22,:lo12:.Ltbox4 Encrypt1blkNorevCtr ld1 {v4.4s},[inp] eor v4.16b,v4.16b,ivec.16b st1 {v4.4s},[outp] ldp x21,x22,[sp,#16] ldp x19,x20,[sp],#32 ldr ctr,[ivp,#12] #ifndef HITLS_BIG_ENDIAN rev ctr,ctr #endif add ctr,ctr,#1 #ifndef HITLS_BIG_ENDIAN rev ctr,ctr #endif str ctr,[ivp,#12] AARCH64_AUTIASP ret 1: stp d8,d9,[sp,#-112]! stp d10,d11,[sp,#16] stp d12,d13,[sp,#32] stp d14,d15,[sp,#48] stp x29,x30,[sp,#64] stp x19,x20,[sp,#80] stp x21,x22,[sp,#96] mov word0, ivec.s[0] mov word1, ivec.s[1] mov word2, ivec.s[2] mov ctr, ivec.s[3] .Lctr32_4_blocks_process: cmp blocks,#4 b.lt 1f dup v4.4s,word0 dup v5.4s,word1 dup v6.4s,word2 mov v7.s[0],w5 add ctr,ctr,#1 mov v7.s[1],ctr add ctr,ctr,#1 mov v7.s[2],ctr add ctr,ctr,#1 mov v7.s[3],ctr add ctr,ctr,#1 cmp blocks,#8 b.ge .Lctr32_8_blocks_process bl Sm4Enc4blks ld4 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v0.16b,v0.16b,v12.16b eor v1.16b,v1.16b,v13.16b eor v2.16b,v2.16b,v14.16b eor v3.16b,v3.16b,v15.16b st4 {v0.4s,v1.4s,v2.4s,v3.4s},[outp],#64 subs blocks,blocks,#4 b.ne .Lctr32_4_blocks_process b 100f .Lctr32_8_blocks_process: dup v8.4s,word0 dup v9.4s,word1 dup v10.4s,word2 mov v11.s[0],ctr add ctr,ctr,#1 mov v11.s[1],ctr add ctr,ctr,#1 mov v11.s[2],ctr add ctr,ctr,#1 mov v11.s[3],ctr add ctr,ctr,#1 cmp blocks,#12 b.ge .Lctr32_12_blocks_process bl Sm4Enc8blks ld4 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 ld4 {v8.4s,v9.4s,v10.4s,v11.4s},[inp],#64 eor v0.16b,v0.16b,v12.16b eor v1.16b,v1.16b,v13.16b eor v2.16b,v2.16b,v14.16b eor v3.16b,v3.16b,v15.16b eor v4.16b,v4.16b,v8.16b eor v5.16b,v5.16b,v9.16b eor v6.16b,v6.16b,v10.16b eor v7.16b,v7.16b,v11.16b st4 {v0.4s,v1.4s,v2.4s,v3.4s},[outp],#64 st4 {v4.4s,v5.4s,v6.4s,v7.4s},[outp],#64 subs blocks,blocks,#8 b.ne .Lctr32_4_blocks_process b 100f .Lctr32_12_blocks_process: dup v16.4s,word0 dup v17.4s,word1 dup v18.4s,word2 mov v19.s[0],ctr add ctr,ctr,#1 mov v19.s[1],ctr add ctr,ctr,#1 mov v19.s[2],ctr add ctr,ctr,#1 mov v19.s[3],ctr add ctr,ctr,#1 bl Sm4Enc12blks ld4 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v0.16b,v0.16b,v12.16b eor v1.16b,v1.16b,v13.16b eor v2.16b,v2.16b,v14.16b eor v3.16b,v3.16b,v15.16b st4 {v0.4s,v1.4s,v2.4s,v3.4s},[outp],#64 ld4 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v4.16b,v4.16b,v12.16b eor v5.16b,v5.16b,v13.16b eor v6.16b,v6.16b,v14.16b eor v7.16b,v7.16b,v15.16b st4 {v4.4s,v5.4s,v6.4s,v7.4s},[outp],#64 ld4 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v8.16b,v8.16b,v12.16b eor v9.16b,v9.16b,v13.16b eor v10.16b,v10.16b,v14.16b eor v11.16b,v11.16b,v15.16b st4 {v8.4s,v9.4s,v10.4s,v11.4s},[outp],#64 subs blocks,blocks,#12 b.ne .Lctr32_4_blocks_process b 100f 1: // last block processing subs blocks,blocks,#1 b.lt 100f b.gt 1f mov ivec.s[0],word0 mov ivec.s[1],word1 mov ivec.s[2],word2 mov ivec.s[3],ctr add ctr,ctr,#1 adrp x19, .Ltbox1 add x19,x19,:lo12:.Ltbox1 adrp x20, .Ltbox2 add x20,x20,:lo12:.Ltbox2 adrp x21, .Ltbox3 add x21,x21,:lo12:.Ltbox3 adrp x22, .Ltbox4 add x22,x22,:lo12:.Ltbox4 Encrypt1blkNorevCtr ld1 {v4.4s},[inp] eor v4.16b,v4.16b,ivec.16b st1 {v4.4s},[outp] b 100f 1: // last 2 blocks processing dup v4.4s,word0 dup v5.4s,word1 dup v6.4s,word2 mov v7.s[0],ctr add ctr,ctr,#1 mov v7.s[1],ctr subs blocks,blocks,#1 b.ne 1f add ctr,ctr,#1 bl Sm4Enc4blks ld4 {v12.s,v13.s,v14.s,v15.s}[0],[inp],#16 ld4 {v12.s,v13.s,v14.s,v15.s}[1],[inp],#16 eor v0.16b,v0.16b,v12.16b eor v1.16b,v1.16b,v13.16b eor v2.16b,v2.16b,v14.16b eor v3.16b,v3.16b,v15.16b st4 {v0.s,v1.s,v2.s,v3.s}[0],[outp],#16 st4 {v0.s,v1.s,v2.s,v3.s}[1],[outp],#16 b 100f 1: // last 3 blocks processing add ctr,ctr,#1 mov v7.s[2],ctr add ctr,ctr,#1 bl Sm4Enc4blks ld4 {v12.s,v13.s,v14.s,v15.s}[0],[inp],#16 ld4 {v12.s,v13.s,v14.s,v15.s}[1],[inp],#16 ld4 {v12.s,v13.s,v14.s,v15.s}[2],[inp],#16 eor v0.16b,v0.16b,v12.16b eor v1.16b,v1.16b,v13.16b eor v2.16b,v2.16b,v14.16b eor v3.16b,v3.16b,v15.16b st4 {v0.s,v1.s,v2.s,v3.s}[0],[outp],#16 st4 {v0.s,v1.s,v2.s,v3.s}[1],[outp],#16 st4 {v0.s,v1.s,v2.s,v3.s}[2],[outp],#16 100: ldp d10,d11,[sp,#16] ldp d12,d13,[sp,#32] ldp d14,d15,[sp,#48] ldp x29,x30,[sp,#64] ldp x19,x20,[sp,#80] ldp x21,x22,[sp,#96] ldp d8,d9,[sp],#112 #ifndef HITLS_BIG_ENDIAN rev ctr, ctr #endif str ctr, [ivp,#12] AARCH64_AUTIASP ret .size Vpsm4Ctr32EncryptBlocks,.-Vpsm4Ctr32EncryptBlocks .globl Vpsm4XtsCipher .type Vpsm4XtsCipher,%function .align 5 Vpsm4XtsCipher: AARCH64_PACIASP stp x19, x20, [sp, #-0x10]! stp x21, x22, [sp, #-0x10]! stp x23, x24, [sp, #-0x10]! stp x25, x26, [sp, #-0x10]! stp x27, x28, [sp, #-0x10]! stp x29, x30, [sp, #-0x10]! stp d8, d9, [sp, #-0x10]! stp d10, d11, [sp, #-0x10]! stp d12, d13, [sp, #-0x10]! stp d14, d15, [sp, #-0x10]! sub sp, sp, #192 mov x24, sp mov x26,x3 mov x27,x4 mov w28,w6 ld1 {v16.4s}, [x5] LoadSbox and x29,x2,#0x0F // convert length into blocks lsr x2,x2,4 cmp x2,#1 b.lt .Lxts_cipher_return cmp x29,0 // If the encryption/decryption Length is N times of 16, // the all blocks are encrypted/decrypted in .xts_encrypt_blocks b.eq .xts_encrypt_blocks // If the encryption/decryption length is not N times of 16, // the last two blocks are encrypted/decrypted in .last_2blks_tweak or .only_2blks_tweak // the other blocks are encrypted/decrypted in .xts_encrypt_blocks subs x2,x2,#1 b.eq .only_2blks_tweak .xts_encrypt_blocks: rbit v16.16b,v16.16b #ifdef HITLS_BIG_ENDIAN rev32 v16.16b,v16.16b #endif mov x12,v16.d[0] mov x13,v16.d[1] mov w7,0x87 extr x9,x13,x13,#32 extr x15,x13,x12,#63 and w8,w7,w9,asr#31 eor x14,x8,x12,lsl#1 mov w7,0x87 extr x9,x15,x15,#32 extr x17,x15,x14,#63 and w8,w7,w9,asr#31 eor x16,x8,x14,lsl#1 mov w7,0x87 extr x9,x17,x17,#32 extr x19,x17,x16,#63 and w8,w7,w9,asr#31 eor x18,x8,x16,lsl#1 .Lxts_12_blocks_process: mov x24, sp cmp x2,#12 b.lt .Lxts_8_blocks_process mov v16.d[0],x12 mov v16.d[1],x13 #ifdef HITLS_BIG_ENDIAN rev32 v16.16b,v16.16b #endif mov w7,0x87 extr x9,x19,x19,#32 extr x13,x19,x18,#63 and w8,w7,w9,asr#31 eor x12,x8,x18,lsl#1 mov v17.d[0],x14 mov v17.d[1],x15 #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b #endif mov w7,0x87 extr x9,x13,x13,#32 extr x15,x13,x12,#63 and w8,w7,w9,asr#31 eor x14,x8,x12,lsl#1 mov v18.d[0],x16 mov v18.d[1],x17 #ifdef HITLS_BIG_ENDIAN rev32 v18.16b,v18.16b #endif mov w7,0x87 extr x9,x15,x15,#32 extr x17,x15,x14,#63 and w8,w7,w9,asr#31 eor x16,x8,x14,lsl#1 mov v19.d[0],x18 mov v19.d[1],x19 #ifdef HITLS_BIG_ENDIAN rev32 v19.16b,v19.16b #endif mov w7,0x87 extr x9,x17,x17,#32 extr x19,x17,x16,#63 and w8,w7,w9,asr#31 eor x18,x8,x16,lsl#1 ld1 {v4.4s,v5.4s,v6.4s,v7.4s},[x0],#64 rbit v16.16b,v16.16b rbit v17.16b,v17.16b rbit v18.16b,v18.16b rbit v19.16b,v19.16b eor v4.16b, v4.16b, v16.16b eor v5.16b, v5.16b, v17.16b eor v6.16b, v6.16b, v18.16b eor v7.16b, v7.16b, v19.16b ld1 {v8.4s,v9.4s,v10.4s,v11.4s},[x0],#64 st1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 mov v16.d[0],x12 mov v16.d[1],x13 #ifdef HITLS_BIG_ENDIAN rev32 v16.16b,v16.16b #endif mov w7,0x87 extr x9,x19,x19,#32 extr x13,x19,x18,#63 and w8,w7,w9,asr#31 eor x12,x8,x18,lsl#1 mov v17.d[0],x14 mov v17.d[1],x15 #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b #endif mov w7,0x87 extr x9,x13,x13,#32 extr x15,x13,x12,#63 and w8,w7,w9,asr#31 eor x14,x8,x12,lsl#1 mov v18.d[0],x16 mov v18.d[1],x17 #ifdef HITLS_BIG_ENDIAN rev32 v18.16b,v18.16b #endif mov w7,0x87 extr x9,x15,x15,#32 extr x17,x15,x14,#63 and w8,w7,w9,asr#31 eor x16,x8,x14,lsl#1 mov v19.d[0],x18 mov v19.d[1],x19 #ifdef HITLS_BIG_ENDIAN rev32 v19.16b,v19.16b #endif mov w7,0x87 extr x9,x17,x17,#32 extr x19,x17,x16,#63 and w8,w7,w9,asr#31 eor x18,x8,x16,lsl#1 rbit v16.16b,v16.16b rbit v17.16b,v17.16b rbit v18.16b,v18.16b rbit v19.16b,v19.16b eor v8.16b, v8.16b, v16.16b eor v9.16b, v9.16b, v17.16b eor v10.16b, v10.16b, v18.16b eor v11.16b, v11.16b, v19.16b ld1 {v0.4s,v1.4s,v2.4s,v3.4s},[x0],#64 st1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 mov v16.d[0],x12 mov v16.d[1],x13 #ifdef HITLS_BIG_ENDIAN rev32 v16.16b,v16.16b #endif mov w7,0x87 extr x9,x19,x19,#32 extr x13,x19,x18,#63 and w8,w7,w9,asr#31 eor x12,x8,x18,lsl#1 mov v17.d[0],x14 mov v17.d[1],x15 #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b #endif mov w7,0x87 extr x9,x13,x13,#32 extr x15,x13,x12,#63 and w8,w7,w9,asr#31 eor x14,x8,x12,lsl#1 mov v18.d[0],x16 mov v18.d[1],x17 #ifdef HITLS_BIG_ENDIAN rev32 v18.16b,v18.16b #endif mov w7,0x87 extr x9,x15,x15,#32 extr x17,x15,x14,#63 and w8,w7,w9,asr#31 eor x16,x8,x14,lsl#1 mov v19.d[0],x18 mov v19.d[1],x19 #ifdef HITLS_BIG_ENDIAN rev32 v19.16b,v19.16b #endif mov w7,0x87 extr x9,x17,x17,#32 extr x19,x17,x16,#63 and w8,w7,w9,asr#31 eor x18,x8,x16,lsl#1 rbit v16.16b,v16.16b rbit v17.16b,v17.16b rbit v18.16b,v18.16b rbit v19.16b,v19.16b eor v0.16b, v0.16b, v16.16b eor v1.16b, v1.16b, v17.16b eor v2.16b, v2.16b, v18.16b eor v3.16b, v3.16b, v19.16b st1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 mov v16.16b,v0.16b mov v17.16b,v1.16b mov v18.16b,v2.16b mov v19.16b,v3.16b #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b rev32 v16.16b,v16.16b rev32 v17.16b,v17.16b rev32 v18.16b,v18.16b rev32 v19.16b,v19.16b #endif zip1 v0.4s,v4.4s,v5.4s zip2 v1.4s,v4.4s,v5.4s zip1 v2.4s,v6.4s,v7.4s zip2 v3.4s,v6.4s,v7.4s zip1 v4.2d,v0.2d,v2.2d zip2 v5.2d,v0.2d,v2.2d zip1 v6.2d,v1.2d,v3.2d zip2 v7.2d,v1.2d,v3.2d zip1 v0.4s,v8.4s,v9.4s zip2 v1.4s,v8.4s,v9.4s zip1 v2.4s,v10.4s,v11.4s zip2 v3.4s,v10.4s,v11.4s zip1 v8.2d,v0.2d,v2.2d zip2 v9.2d,v0.2d,v2.2d zip1 v10.2d,v1.2d,v3.2d zip2 v11.2d,v1.2d,v3.2d zip1 v0.4s,v16.4s,v17.4s zip2 v1.4s,v16.4s,v17.4s zip1 v2.4s,v18.4s,v19.4s zip2 v3.4s,v18.4s,v19.4s zip1 v16.2d,v0.2d,v2.2d zip2 v17.2d,v0.2d,v2.2d zip1 v18.2d,v1.2d,v3.2d zip2 v19.2d,v1.2d,v3.2d bl Sm4Enc12blks zip1 v16.4s,v0.4s,v1.4s zip2 v17.4s,v0.4s,v1.4s zip1 v18.4s,v2.4s,v3.4s zip2 v19.4s,v2.4s,v3.4s zip1 v0.2d,v16.2d,v18.2d zip2 v1.2d,v16.2d,v18.2d zip1 v2.2d,v17.2d,v19.2d zip2 v3.2d,v17.2d,v19.2d zip1 v16.4s,v4.4s,v5.4s zip2 v17.4s,v4.4s,v5.4s zip1 v18.4s,v6.4s,v7.4s zip2 v19.4s,v6.4s,v7.4s zip1 v4.2d,v16.2d,v18.2d zip2 v5.2d,v16.2d,v18.2d zip1 v6.2d,v17.2d,v19.2d zip2 v7.2d,v17.2d,v19.2d zip1 v16.4s,v8.4s,v9.4s zip2 v17.4s,v8.4s,v9.4s zip1 v18.4s,v10.4s,v11.4s zip2 v19.4s,v10.4s,v11.4s zip1 v8.2d,v16.2d,v18.2d zip2 v9.2d,v16.2d,v18.2d zip1 v10.2d,v17.2d,v19.2d zip2 v11.2d,v17.2d,v19.2d mov x24, sp ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 eor v0.16b, v0.16b, v16.16b eor v1.16b, v1.16b, v17.16b eor v2.16b, v2.16b, v18.16b eor v3.16b, v3.16b, v19.16b ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 eor v4.16b, v4.16b, v16.16b eor v5.16b, v5.16b, v17.16b eor v6.16b, v6.16b, v18.16b eor v7.16b, v7.16b, v19.16b ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 eor v8.16b, v8.16b, v16.16b eor v9.16b, v9.16b, v17.16b eor v10.16b, v10.16b, v18.16b eor v11.16b, v11.16b, v19.16b // save the last tweak mov v24.16b,v19.16b st1 {v0.4s,v1.4s,v2.4s,v3.4s},[x1],#64 st1 {v4.4s,v5.4s,v6.4s,v7.4s},[x1],#64 st1 {v8.4s,v9.4s,v10.4s,v11.4s},[x1],#64 subs x2,x2,#12 b.gt .Lxts_12_blocks_process b 100f .Lxts_8_blocks_process: mov x24, sp cmp x2,#8 mov v16.d[0],x12 mov v16.d[1],x13 #ifdef HITLS_BIG_ENDIAN rev32 v16.16b,v16.16b #endif mov w7,0x87 extr x9,x19,x19,#32 extr x13,x19,x18,#63 and w8,w7,w9,asr#31 eor x12,x8,x18,lsl#1 mov v17.d[0],x14 mov v17.d[1],x15 #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b #endif mov w7,0x87 extr x9,x13,x13,#32 extr x15,x13,x12,#63 and w8,w7,w9,asr#31 eor x14,x8,x12,lsl#1 mov v18.d[0],x16 mov v18.d[1],x17 #ifdef HITLS_BIG_ENDIAN rev32 v18.16b,v18.16b #endif mov w7,0x87 extr x9,x15,x15,#32 extr x17,x15,x14,#63 and w8,w7,w9,asr#31 eor x16,x8,x14,lsl#1 mov v19.d[0],x18 mov v19.d[1],x19 #ifdef HITLS_BIG_ENDIAN rev32 v19.16b,v19.16b #endif mov w7,0x87 extr x9,x17,x17,#32 extr x19,x17,x16,#63 and w8,w7,w9,asr#31 eor x18,x8,x16,lsl#1 b.lt .Lxts_4_blocks_process ld1 {v4.4s,v5.4s,v6.4s,v7.4s},[x0],#64 rbit v16.16b,v16.16b rbit v17.16b,v17.16b rbit v18.16b,v18.16b rbit v19.16b,v19.16b eor v4.16b, v4.16b, v16.16b eor v5.16b, v5.16b, v17.16b eor v6.16b, v6.16b, v18.16b eor v7.16b, v7.16b, v19.16b ld1 {v8.4s,v9.4s,v10.4s,v11.4s},[x0],#64 st1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24], #64 mov v16.d[0],x12 mov v16.d[1],x13 #ifdef HITLS_BIG_ENDIAN rev32 v16.16b,v16.16b #endif mov w7,0x87 extr x9,x19,x19,#32 extr x13,x19,x18,#63 and w8,w7,w9,asr#31 eor x12,x8,x18,lsl#1 mov v17.d[0],x14 mov v17.d[1],x15 #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b #endif mov w7,0x87 extr x9,x13,x13,#32 extr x15,x13,x12,#63 and w8,w7,w9,asr#31 eor x14,x8,x12,lsl#1 mov v18.d[0],x16 mov v18.d[1],x17 #ifdef HITLS_BIG_ENDIAN rev32 v18.16b,v18.16b #endif mov w7,0x87 extr x9,x15,x15,#32 extr x17,x15,x14,#63 and w8,w7,w9,asr#31 eor x16,x8,x14,lsl#1 mov v19.d[0],x18 mov v19.d[1],x19 #ifdef HITLS_BIG_ENDIAN rev32 v19.16b,v19.16b #endif mov w7,0x87 extr x9,x17,x17,#32 extr x19,x17,x16,#63 and w8,w7,w9,asr#31 eor x18,x8,x16,lsl#1 rbit v16.16b,v16.16b rbit v17.16b,v17.16b rbit v18.16b,v18.16b rbit v19.16b,v19.16b eor v8.16b, v8.16b, v16.16b eor v9.16b, v9.16b, v17.16b eor v10.16b, v10.16b, v18.16b eor v11.16b, v11.16b, v19.16b st1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b #endif zip1 v0.4s,v4.4s,v5.4s zip2 v1.4s,v4.4s,v5.4s zip1 v2.4s,v6.4s,v7.4s zip2 v3.4s,v6.4s,v7.4s zip1 v4.2d,v0.2d,v2.2d zip2 v5.2d,v0.2d,v2.2d zip1 v6.2d,v1.2d,v3.2d zip2 v7.2d,v1.2d,v3.2d zip1 v0.4s,v8.4s,v9.4s zip2 v1.4s,v8.4s,v9.4s zip1 v2.4s,v10.4s,v11.4s zip2 v3.4s,v10.4s,v11.4s zip1 v8.2d,v0.2d,v2.2d zip2 v9.2d,v0.2d,v2.2d zip1 v10.2d,v1.2d,v3.2d zip2 v11.2d,v1.2d,v3.2d bl Sm4Enc8blks zip1 v8.4s,v0.4s,v1.4s zip2 v9.4s,v0.4s,v1.4s zip1 v10.4s,v2.4s,v3.4s zip2 v11.4s,v2.4s,v3.4s zip1 v0.2d,v8.2d,v10.2d zip2 v1.2d,v8.2d,v10.2d zip1 v2.2d,v9.2d,v11.2d zip2 v3.2d,v9.2d,v11.2d zip1 v8.4s,v4.4s,v5.4s zip2 v9.4s,v4.4s,v5.4s zip1 v10.4s,v6.4s,v7.4s zip2 v11.4s,v6.4s,v7.4s zip1 v4.2d,v8.2d,v10.2d zip2 v5.2d,v8.2d,v10.2d zip1 v6.2d,v9.2d,v11.2d zip2 v7.2d,v9.2d,v11.2d mov x24, sp ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 eor v0.16b, v0.16b, v16.16b eor v1.16b, v1.16b, v17.16b eor v2.16b, v2.16b, v18.16b eor v3.16b, v3.16b, v19.16b ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[x24],#64 eor v4.16b, v4.16b, v16.16b eor v5.16b, v5.16b, v17.16b eor v6.16b, v6.16b, v18.16b eor v7.16b, v7.16b, v19.16b // save the last tweak mov v24.16b,v19.16b st1 {v0.4s,v1.4s,v2.4s,v3.4s},[x1],#64 st1 {v4.4s,v5.4s,v6.4s,v7.4s},[x1],#64 subs x2,x2,#8 b.gt .Lxts_8_blocks_process b 100f .Lxts_4_blocks_process: cmp x2,#4 b.lt 1f ld1 {v4.4s,v5.4s,v6.4s,v7.4s},[x0],#64 rbit v16.16b,v16.16b rbit v17.16b,v17.16b rbit v18.16b,v18.16b rbit v19.16b,v19.16b eor v4.16b, v4.16b, v16.16b eor v5.16b, v5.16b, v17.16b eor v6.16b, v6.16b, v18.16b eor v7.16b, v7.16b, v19.16b #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif zip1 v0.4s,v4.4s,v5.4s zip2 v1.4s,v4.4s,v5.4s zip1 v2.4s,v6.4s,v7.4s zip2 v3.4s,v6.4s,v7.4s zip1 v4.2d,v0.2d,v2.2d zip2 v5.2d,v0.2d,v2.2d zip1 v6.2d,v1.2d,v3.2d zip2 v7.2d,v1.2d,v3.2d bl Sm4Enc4blks zip1 v4.4s,v0.4s,v1.4s zip2 v5.4s,v0.4s,v1.4s zip1 v6.4s,v2.4s,v3.4s zip2 v7.4s,v2.4s,v3.4s zip1 v0.2d,v4.2d,v6.2d zip2 v1.2d,v4.2d,v6.2d zip1 v2.2d,v5.2d,v7.2d zip2 v3.2d,v5.2d,v7.2d eor v0.16b, v0.16b, v16.16b eor v1.16b, v1.16b, v17.16b eor v2.16b, v2.16b, v18.16b eor v3.16b, v3.16b, v19.16b st1 {v0.4s,v1.4s,v2.4s,v3.4s},[x1],#64 sub x2,x2,#4 // save the last tweak mov v24.16b,v19.16b mov v16.d[0],x12 mov v16.d[1],x13 #ifdef HITLS_BIG_ENDIAN rev32 v16.16b,v16.16b #endif mov w7,0x87 extr x9,x19,x19,#32 extr x13,x19,x18,#63 and w8,w7,w9,asr#31 eor x12,x8,x18,lsl#1 mov v17.d[0],x14 mov v17.d[1],x15 #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b #endif mov w7,0x87 extr x9,x13,x13,#32 extr x15,x13,x12,#63 and w8,w7,w9,asr#31 eor x14,x8,x12,lsl#1 mov v18.d[0],x16 mov v18.d[1],x17 #ifdef HITLS_BIG_ENDIAN rev32 v18.16b,v18.16b #endif mov w7,0x87 extr x9,x15,x15,#32 extr x17,x15,x14,#63 and w8,w7,w9,asr#31 eor x16,x8,x14,lsl#1 mov v19.d[0],x18 mov v19.d[1],x19 #ifdef HITLS_BIG_ENDIAN rev32 v19.16b,v19.16b #endif mov w7,0x87 extr x9,x17,x17,#32 extr x19,x17,x16,#63 and w8,w7,w9,asr#31 eor x18,x8,x16,lsl#1 1: // process last block cmp x2,#1 b.lt 100f b.gt 1f ld1 {v4.4s},[x0],#16 rbit v16.16b,v16.16b eor v4.16b, v4.16b, v16.16b #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b #endif mov x10,x3 mov w11,#8 mov w12,v4.s[0] mov w13,v4.s[1] mov w14,v4.s[2] mov w15,v4.s[3] 10: ldp w7,w8,[x10],8 // B0 ^= SBOX(B1 ^ B2 ^ B3 ^ RK0) eor w6,w14,w15 eor w9,w7,w13 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w12,w12,w6 // B1 ^= SBOX(B0 ^ B2 ^ B3 ^ RK1) eor w6,w14,w15 eor w9,w12,w8 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 ldp w7,w8,[x10],8 eor w13,w13,w6 // B2 ^= SBOX(B0 ^ B1 ^ B3 ^ RK2) eor w6,w12,w13 eor w9,w7,w15 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w14,w14,w6 // B3 ^= SBOX(B0 ^ B1 ^ B2 ^ RK3) eor w6,w12,w13 eor w9,w14,w8 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w15,w15,w6 subs w11,w11,#1 b.ne 10b mov v4.s[0],w15 mov v4.s[1],w14 mov v4.s[2],w13 mov v4.s[3],w12 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b #endif eor v4.16b, v4.16b, v16.16b st1 {v4.4s},[x1],#16 // save the last tweak mov v24.16b,v16.16b b 100f 1: // process last 2 blocks cmp x2,#2 b.gt 1f ld1 {v4.4s,v5.4s},[x0],#32 rbit v16.16b,v16.16b rbit v17.16b,v17.16b eor v4.16b, v4.16b, v16.16b eor v5.16b, v5.16b, v17.16b #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b #endif zip1 v0.4s,v4.4s,v5.4s zip2 v1.4s,v4.4s,v5.4s zip1 v2.4s,v6.4s,v7.4s zip2 v3.4s,v6.4s,v7.4s zip1 v4.2d,v0.2d,v2.2d zip2 v5.2d,v0.2d,v2.2d zip1 v6.2d,v1.2d,v3.2d zip2 v7.2d,v1.2d,v3.2d bl Sm4Enc4blks zip1 v4.4s,v0.4s,v1.4s zip2 v5.4s,v0.4s,v1.4s zip1 v6.4s,v2.4s,v3.4s zip2 v7.4s,v2.4s,v3.4s zip1 v0.2d,v4.2d,v6.2d zip2 v1.2d,v4.2d,v6.2d zip1 v2.2d,v5.2d,v7.2d zip2 v3.2d,v5.2d,v7.2d eor v0.16b, v0.16b, v16.16b eor v1.16b, v1.16b, v17.16b st1 {v0.4s,v1.4s},[x1],#32 // save the last tweak mov v24.16b,v17.16b b 100f 1: // process last 3 blocks ld1 {v4.4s,v5.4s,v6.4s},[x0],#48 rbit v16.16b,v16.16b rbit v17.16b,v17.16b rbit v18.16b,v18.16b eor v4.16b, v4.16b, v16.16b eor v5.16b, v5.16b, v17.16b eor v6.16b, v6.16b, v18.16b #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b #endif zip1 v0.4s,v4.4s,v5.4s zip2 v1.4s,v4.4s,v5.4s zip1 v2.4s,v6.4s,v7.4s zip2 v3.4s,v6.4s,v7.4s zip1 v4.2d,v0.2d,v2.2d zip2 v5.2d,v0.2d,v2.2d zip1 v6.2d,v1.2d,v3.2d zip2 v7.2d,v1.2d,v3.2d bl Sm4Enc4blks zip1 v4.4s,v0.4s,v1.4s zip2 v5.4s,v0.4s,v1.4s zip1 v6.4s,v2.4s,v3.4s zip2 v7.4s,v2.4s,v3.4s zip1 v0.2d,v4.2d,v6.2d zip2 v1.2d,v4.2d,v6.2d zip1 v2.2d,v5.2d,v7.2d zip2 v3.2d,v5.2d,v7.2d eor v0.16b, v0.16b, v16.16b eor v1.16b, v1.16b, v17.16b eor v2.16b, v2.16b, v18.16b st1 {v0.4s,v1.4s,v2.4s},[x1],#48 // save the last tweak mov v24.16b,v18.16b 100: cmp x29,0 b.eq .Lxts_cipher_return // This branch calculates the last two tweaks, // while the encryption/decryption length is larger than 32 .last_2blks_tweak: #ifdef HITLS_BIG_ENDIAN rev32 v24.16b,v24.16b #endif rbit v2.16b,v24.16b adrp x26, .Lxts_magic add x26, x26, :lo12:.Lxts_magic ldr q0, [x26] shl v17.16b, v2.16b, #1 ext v1.16b, v2.16b, v2.16b,#15 ushr v1.16b, v1.16b, #7 mul v1.16b, v1.16b, v0.16b eor v17.16b, v17.16b, v1.16b rbit v17.16b,v17.16b rbit v2.16b,v17.16b adrp x26, .Lxts_magic add x26, x26, :lo12:.Lxts_magic ldr q0, [x26] shl v18.16b, v2.16b, #1 ext v1.16b, v2.16b, v2.16b,#15 ushr v1.16b, v1.16b, #7 mul v1.16b, v1.16b, v0.16b eor v18.16b, v18.16b, v1.16b rbit v18.16b,v18.16b b .Lxts_check_dec // This branch calculates the last two tweaks, // while the encryption/decryption length is equal to 32, who only need two tweaks .only_2blks_tweak: mov v17.16b,v16.16b #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b #endif rbit v2.16b,v17.16b adrp x26, .Lxts_magic add x26, x26, :lo12:.Lxts_magic ldr q0, [x26] shl v18.16b, v2.16b, #1 ext v1.16b, v2.16b, v2.16b,#15 ushr v1.16b, v1.16b, #7 mul v1.16b, v1.16b, v0.16b eor v18.16b, v18.16b, v1.16b rbit v18.16b,v18.16b b .Lxts_check_dec // Determine whether encryption or decryption is required. // The last two tweaks need to be swapped for decryption. .Lxts_check_dec: // encryption:1 decryption:0 cmp w28,1 b.eq .Lxts_prcess_last_2blks mov v0.16B,v17.16b mov v17.16B,v18.16b mov v18.16B,v0.16b .Lxts_prcess_last_2blks: #ifdef HITLS_BIG_ENDIAN rev32 v17.16b,v17.16b rev32 v18.16b,v18.16b #endif ld1 {v4.4s},[x0],#16 eor v4.16b, v4.16b, v17.16b #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b #endif mov x10,x3 mov w11,#8 mov w12,v4.s[0] mov w13,v4.s[1] mov w14,v4.s[2] mov w15,v4.s[3] 10: ldp w7,w8,[x10],8 // B0 ^= SBOX(B1 ^ B2 ^ B3 ^ RK0) eor w6,w14,w15 eor w9,w7,w13 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w12,w12,w6 // B1 ^= SBOX(B0 ^ B2 ^ B3 ^ RK1) eor w6,w14,w15 eor w9,w12,w8 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 ldp w7,w8,[x10],8 eor w13,w13,w6 // B2 ^= SBOX(B0 ^ B1 ^ B3 ^ RK2) eor w6,w12,w13 eor w9,w7,w15 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w14,w14,w6 // B3 ^= SBOX(B0 ^ B1 ^ B2 ^ RK3) eor w6,w12,w13 eor w9,w14,w8 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w15,w15,w6 subs w11,w11,#1 b.ne 10b mov v4.s[0],w15 mov v4.s[1],w14 mov v4.s[2],w13 mov v4.s[3],w12 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b #endif eor v4.16b, v4.16b, v17.16b st1 {v4.4s},[x1],#16 sub x26,x1,16 .Lxts_loop: subs x29,x29,1 ldrb w7,[x26,x29] ldrb w8,[x0,x29] strb w8,[x26,x29] strb w7,[x1,x29] b.gt .Lxts_loop ld1 {v4.4s}, [x26] eor v4.16b, v4.16b, v18.16b #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b #endif mov x10,x3 mov w11,#8 mov w12,v4.s[0] mov w13,v4.s[1] mov w14,v4.s[2] mov w15,v4.s[3] 10: ldp w7,w8,[x10],8 // B0 ^= SBOX(B1 ^ B2 ^ B3 ^ RK0) eor w6,w14,w15 eor w9,w7,w13 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w12,w12,w6 // B1 ^= SBOX(B0 ^ B2 ^ B3 ^ RK1) eor w6,w14,w15 eor w9,w12,w8 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 ldp w7,w8,[x10],8 eor w13,w13,w6 // B2 ^= SBOX(B0 ^ B1 ^ B3 ^ RK2) eor w6,w12,w13 eor w9,w7,w15 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w14,w14,w6 // B3 ^= SBOX(B0 ^ B1 ^ B2 ^ RK3) eor w6,w12,w13 eor w9,w14,w8 eor w6,w6,w9 movi v31.16b, #0x0f mov v3.s[0],w6 // optimize sbox using AESE instruction tbl v0.16b, {v3.16b}, v26.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v28.16b}, v0.16b tbl v2.16b, {v27.16b}, v2.16b eor v0.16b, v0.16b, v2.16b eor v1.16b, v1.16b, v1.16b aese v0.16b,v1.16b ushr v2.16b, v0.16b, 4 and v0.16b, v0.16b, v31.16b tbl v0.16b, {v30.16b}, v0.16b tbl v2.16b, {v29.16b}, v2.16b eor v0.16b, v0.16b, v2.16b mov w7,v0.s[0] eor w6,w7,w7,ror #32-2 eor w6,w6,w7,ror #32-10 eor w6,w6,w7,ror #32-18 eor w6,w6,w7,ror #32-24 eor w15,w15,w6 subs w11,w11,#1 b.ne 10b mov v4.s[0],w15 mov v4.s[1],w14 mov v4.s[2],w13 mov v4.s[3],w12 #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b #endif eor v4.16b, v4.16b, v18.16b st1 {v4.4s}, [x26] .Lxts_cipher_return: add sp, sp, #192 ldp d14, d15, [sp], #0x10 ldp d12, d13, [sp], #0x10 ldp d10, d11, [sp], #0x10 ldp d8, d9, [sp], #0x10 ldp x29, x30, [sp], #0x10 ldp x27, x28, [sp], #0x10 ldp x25, x26, [sp], #0x10 ldp x23, x24, [sp], #0x10 ldp x21, x22, [sp], #0x10 ldp x19, x20, [sp], #0x10 AARCH64_AUTIASP ret .size Vpsm4XtsCipher,.-Vpsm4XtsCipher .globl Vpsm4Cfb128Encrypt .type Vpsm4Cfb128Encrypt,%function .align 5 Vpsm4Cfb128Encrypt: AARCH64_PACIASP stp x29,x30,[sp,#-80]! add x29,sp,#0 stp x19,x20,[sp,#16] stp x21,x22,[sp,#32] stp x23,x8,[sp,#48] stp x16,x17,[sp,#64] // load tbox adrp x19, .Ltbox1 add x19,x19,:lo12:.Ltbox1 adrp x20, .Ltbox2 add x20,x20,:lo12:.Ltbox2 adrp x21, .Ltbox3 add x21,x21,:lo12:.Ltbox3 adrp x22, .Ltbox4 add x22,x22,:lo12:.Ltbox4 // load num ldr w23,[x5] cbz w23,.Lcfb128_enc_update .Lcfb128_enc_init: ldrb w7,[ivp,x23] ldrb w8,[inp] eor w7,w7,w8 strb w7,[outp] strb w7,[ivp,x23] add inp,inp,#1 add outp,outp,#1 add w23,w23,#1 sub len,len,#1 cmp w23,#16 b.eq .Lcfb128_enc_init_final cbz len,.Lcfb128_enc_ret b .Lcfb128_enc_init .Lcfb128_enc_init_final: mov w23,#0 .Lcfb128_enc_update: cbz len,.Lcfb128_enc_ret // load iv ldp w8,w9,[ivp] ldp w10,w11,[ivp,#8] #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif EncRound #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif // save back IV stp w11,w10,[ivp] stp w9,w8,[ivp,#8] cmp len,#16 b.lt .Lcfb128_enc_final // xor with plain ldp w6,w7,[inp],#8 ldp w16,w17,[inp],#8 eor w11,w11,w6 eor w10,w10,w7 eor w9,w9,w16 eor w8,w8,w17 stp w11,w10,[outp],#8 stp w9,w8,[outp],#8 // save back IV stp w11,w10,[ivp] stp w9,w8,[ivp,#8] sub len,len,#16 b .Lcfb128_enc_update .Lcfb128_enc_final: ldrb w7,[ivp,x23] ldrb w8,[inp] eor w7,w7,w8 strb w7,[outp] strb w7,[ivp,x23] add inp,inp,#1 add outp,outp,#1 add w23,w23,#1 subs len,len,#1 b.ne .Lcfb128_enc_final .Lcfb128_enc_ret: // store num str w23,[x5] // restore register ldp x19,x20,[sp,#16] ldp x21,x22,[sp,#32] ldp x23,x8,[sp,#48] ldp x16,x17,[sp,#64] ldp x29,x30,[sp],#80 AARCH64_AUTIASP ret .size Vpsm4Cfb128Encrypt,.-Vpsm4Cfb128Encrypt # void Vpsm4Cfb128Decrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, int *num); .globl Vpsm4Cfb128Decrypt .type Vpsm4Cfb128Decrypt,%function .align 5 Vpsm4Cfb128Decrypt: AARCH64_PACIASP stp x29,x30,[sp,#-128]! stp x19,x20,[sp,#16] stp x21,x22,[sp,#32] stp x23,x24,[sp,#48] stp d8,d9,[sp,#64] stp d10,d11,[sp,#80] stp d12,d13,[sp,#96] stp d14,d15,[sp,#112] // load tbox adrp x19, .Ltbox1 add x19,x19,:lo12:.Ltbox1 adrp x20, .Ltbox2 add x20,x20,:lo12:.Ltbox2 adrp x21, .Ltbox3 add x21,x21,:lo12:.Ltbox3 adrp x22, .Ltbox4 add x22,x22,:lo12:.Ltbox4 LoadSbox // load num ldr w23,[x5] cbz w23,.Lcfb128_12_blocks_dec .Lcfb128_dec_init: ldrb w7,[ivp,x23] ldrb w8,[inp] eor w7,w7,w8 strb w7,[outp] // store in to iv strb w8,[ivp,x23] add inp,inp,#1 add outp,outp,#1 subs len,len,#1 add w23,w23,#1 and w23,w23,#15 b.eq 100f cbz w23,.Lcfb128_12_blocks_dec b .Lcfb128_dec_init .Lcfb128_12_blocks_dec: cmp len,#192 b.lt .Lcfb128_8_blocks_dec ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[inp] // append iv as last element ld4 {v4.s,v5.s,v6.s,v7.s}[3],[ivp] add ptr,inp,#48 ld4 {v8.4s,v9.4s,v10.4s,v11.4s},[ptr] add ptr,ptr,#64 ld4 {v16.4s,v17.4s,v18.4s,v19.4s},[ptr] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b rev32 v16.16b,v16.16b rev32 v17.16b,v17.16b rev32 v18.16b,v18.16b rev32 v19.16b,v19.16b #endif bl Sm4Enc12blks transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v16.4s,v17.4s,v18.4s,v19.4s,v16.2d,v17.2d,v18.2d,v19.2d transpose v4.4s,v5.4s,v6.4s,v7.4s,v4.2d,v5.2d,v6.2d,v7.2d,v16.4s,v17.4s,v18.4s,v19.4s,v16.2d,v17.2d,v18.2d,v19.2d transpose v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d,v16.4s,v17.4s,v18.4s,v19.4s,v16.2d,v17.2d,v18.2d,v19.2d ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[inp],#64 ld1 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v0.16b,v0.16b,v17.16b eor v1.16b,v1.16b,v18.16b eor v2.16b,v2.16b,v19.16b eor v3.16b,v3.16b,v16.16b // save plainText decrypted from iv as first one st1 {v3.4s},[outp],#16 st1 {v0.4s,v1.4s,v2.4s},[outp],#48 eor v4.16b,v4.16b,v12.16b eor v5.16b,v5.16b,v13.16b eor v6.16b,v6.16b,v14.16b eor v7.16b,v7.16b,v15.16b st1 {v4.4s,v5.4s,v6.4s,v7.4s},[outp],#64 ld1 {v16.4s,v17.4s,v18.4s,v19.4s},[inp],#64 eor v8.16b,v8.16b,v16.16b eor v9.16b,v9.16b,v17.16b eor v10.16b,v10.16b,v18.16b eor v11.16b,v11.16b,v19.16b st1 {v8.4s,v9.4s,v10.4s,v11.4s},[outp],#64 // save back IV st1 {v19.4s}, [ivp] subs len,len,#192 b.gt .Lcfb128_12_blocks_dec b.eq 100f .Lcfb128_8_blocks_dec: cmp len,#128 b.lt .Lcfb128_4_blocks_dec ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[inp] // append iv as last element ld4 {v4.s,v5.s,v6.s,v7.s}[3],[ivp] add ptr,inp,#48 ld4 {v8.4s,v9.4s,v10.4s,v11.4s},[ptr] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b rev32 v8.16b,v8.16b rev32 v9.16b,v9.16b rev32 v10.16b,v10.16b rev32 v11.16b,v11.16b #endif bl Sm4Enc8blks transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d transpose v4.4s,v5.4s,v6.4s,v7.4s,v4.2d,v5.2d,v6.2d,v7.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d ld1 {v8.4s,v9.4s,v10.4s,v11.4s},[inp],#64 ld1 {v12.4s,v13.4s,v14.4s,v15.4s},[inp],#64 eor v0.16b,v0.16b,v9.16b eor v1.16b,v1.16b,v10.16b eor v2.16b,v2.16b,v11.16b eor v3.16b,v3.16b,v8.16b // save back IV st1 {v15.4s}, [ivp] eor v4.16b,v4.16b,v12.16b eor v5.16b,v5.16b,v13.16b eor v6.16b,v6.16b,v14.16b eor v7.16b,v7.16b,v15.16b st1 {v3.4s},[outp],#16 st1 {v0.4s,v1.4s,v2.4s},[outp],#48 st1 {v4.4s,v5.4s,v6.4s,v7.4s},[outp],#64 subs len,len,#128 b.gt .Lcfb128_8_blocks_dec b.eq 100f .Lcfb128_4_blocks_dec: cmp len,#64 b.lt .Llast_block ld4 {v4.4s,v5.4s,v6.4s,v7.4s},[inp] // append iv as last element ld4 {v4.s,v5.s,v6.s,v7.s}[3],[ivp] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks ld1 {v4.4s,v5.4s,v6.4s,v7.4s},[inp],#64 transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d eor v0.16b,v0.16b,v5.16b eor v1.16b,v1.16b,v6.16b eor v2.16b,v2.16b,v7.16b eor v3.16b,v3.16b,v4.16b st1 {v3.4s},[outp],#16 st1 {v0.4s,v1.4s,v2.4s},[outp],#48 // save back IV st1 {v7.4s}, [ivp] subs len,len,#64 b.gt .Lcfb128_4_blocks_dec b.eq 100f .Llast_block: // last block cmp len,#16 b.gt .Llast_2_blocks 1: // load in ldp w6,w7,[inp] ldp w16,w17,[inp,#8] // load iv ldp w8,w9,[ivp] ldp w10,w11,[ivp,#8] #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif EncRound #ifndef HITLS_BIG_ENDIAN rev w8,w8 rev w9,w9 rev w10,w10 rev w11,w11 #endif // save encrypted iv stp w11,w10,[ivp] stp w9,w8,[ivp,#8] cmp len,#16 b.lt .Lcfb128_dec_final stp w6,w7,[ivp] stp w16,w17,[ivp,#8] eor w11,w11,w6 eor w10,w10,w7 eor w9,w9,w16 eor w8,w8,w17 stp w11,w10,[outp],#8 stp w9,w8,[outp],#8 add inp,inp,#16 subs len,len,#16 b.gt 1b b.eq 100f b .Lcfb128_dec_final .Llast_2_blocks: // last two blocks ld4 {v4.s,v5.s,v6.s,v7.s}[0],[ivp] mov ptr,inp ld4 {v4.s,v5.s,v6.s,v7.s}[1],[ptr],#16 cmp x2,#32 b.gt .Llast_3_blocks b.lt 1b 1: #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks ld1 {v4.4s,v5.4s},[inp],#32 transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d eor v0.16b,v0.16b,v4.16b eor v1.16b,v1.16b,v5.16b st1 {v0.4s,v1.4s},[outp],#32 // save back IV st1 {v5.4s}, [ivp] subs len,len,#32 b.eq 100f b .Llast_block .Llast_3_blocks: // last 3 blocks cmp len,#48 b.lt 1b ld4 {v4.s,v5.s,v6.s,v7.s}[2],[ptr] #ifndef HITLS_BIG_ENDIAN rev32 v4.16b,v4.16b rev32 v5.16b,v5.16b rev32 v6.16b,v6.16b rev32 v7.16b,v7.16b #endif bl Sm4Enc4blks ld1 {v4.4s,v5.4s,v6.4s},[inp],#48 transpose v0.4s,v1.4s,v2.4s,v3.4s,v0.2d,v1.2d,v2.2d,v3.2d,v8.4s,v9.4s,v10.4s,v11.4s,v8.2d,v9.2d,v10.2d,v11.2d eor v0.16b,v0.16b,v4.16b eor v1.16b,v1.16b,v5.16b eor v2.16b,v2.16b,v6.16b st1 {v0.4s,v1.4s,v2.4s},[outp],#48 // save back IV st1 {v6.4s}, [ivp] subs len,len,#48 b.eq 100f b .Llast_block .Lcfb128_dec_final: ldrb w7,[ivp,x23] ldrb w8,[inp] eor w7,w7,w8 strb w7,[outp] // store in to iv strb w8,[ivp,x23] add inp,inp,#1 add outp,outp,#1 add w23,w23,#1 subs len,len,#1 b.ne .Lcfb128_dec_final 100: // store num str w23,[x5] ldp x19,x20,[sp,#16] ldp x21,x22,[sp,#32] ldp x23,x24,[sp,#48] ldp d8,d9,[sp,#64] ldp d10,d11,[sp,#80] ldp d12,d13,[sp,#96] ldp d14,d15,[sp,#112] ldp x29,x30,[sp],#128 AARCH64_AUTIASP ret .size Vpsm4Cfb128Decrypt,.-Vpsm4Cfb128Decrypt #endif
2301_79861745/bench_create
crypto/sm4/src/asm/crypt_sm4_armv8.S
Unix Assembly
unknown
76,228
/* * 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_SM4 #include "crypt_arm.h" #define VTMP0 V8 #define VTMP1 V9 #define VTMP2 V10 #define DATA0 V16 #define MaskV v26 #define TAHMatV v27 #define TALMatV v28 #define ATAHMatV v29 #define ATALMatV v30 #define ANDMaskV v31 #define MaskQ q26 #define TAHMatQ q27 #define TALMatQ q28 #define ATAHMatQ q29 #define ATALMatQ q30 #define ANDMaskQ q31 .section .rodata .align 4 #ifdef HITLS_BIG_ENDIAN .qtmp0: .quad 0x0101010101010101,0x0101010101010187 .Lsbox_magic: .quad 0x0306090c0f020508,0x0b0e0104070a0d00 .quad 0x22581a6002783a40,0x62185a2042387a00 .quad 0xc10bb67c4a803df7,0x15df62a89e54e923 .quad 0x1407c6d56c7fbead,0xb9aa6b78c1d21300 .quad 0xe383c1a1fe9edcbc,0x6404462679195b3b .quad 0x0f0f0f0f0f0f0f0f,0x0f0f0f0f0f0f0f0f #else .qtmp0: .quad 0x0101010101010187,0x0101010101010101 .Lsbox_magic: .quad 0x0b0e0104070a0d00,0x0306090c0f020508 .quad 0x62185a2042387a00,0x22581a6002783a40 .quad 0x15df62a89e54e923,0xc10bb67c4a803df7 .quad 0xb9aa6b78c1d21300,0x1407c6d56c7fbead .quad 0x6404462679195b3b,0xe383c1a1fe9edcbc .quad 0x0f0f0f0f0f0f0f0f,0x0f0f0f0f0f0f0f0f #endif .Lck: .long 0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269 .long 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9 .long 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249 .long 0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9 .long 0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229 .long 0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299 .long 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209 .long 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279 .Lfk: .long 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc .Lshuffles: .long 0x07060504, 0x0B0A0908, 0x0F0E0D0C, 0x03020100 #ifndef HITLS_BIG_ENDIAN #define REV32_EQ(DST, SRC) \ rev32 DST.16b,DST.16b ; #else #define REV32_EQ(DST, SRC) \ /*rev32 eq is null in armeb */ ; #endif .macro LOAD_SBOX_MATRIX adrp x15,.Lsbox_magic add x15,x15,:lo12:.Lsbox_magic ldr MaskQ, [x15] ldr TAHMatQ, [x15, #16] ldr TALMatQ, [x15, #32] ldr ATAHMatQ, [x15, #48] ldr ATALMatQ, [x15, #64] ldr ANDMaskQ, [x15, #80] .endm /* matrix multiplication Mat*x = (lowerMat*x) ^ (higherMat*x) */ #define MUL_MATRIX(X, HIGHERMAT, LOWERMAT, TMP) \ ushr TMP.16b, X.16b, 4 ; \ and X.16b, X.16b, ANDMaskV.16b ; \ tbl X.16b, {LOWERMAT.16b}, X.16b ; \ tbl TMP.16b, {HIGHERMAT.16b}, TMP.16b ; \ eor X.16b, X.16b, TMP.16b ; .arch armv8-a+crypto .text #define USER_KEY x0 #define ROUND_KEY1 x1 #define ENC1 w2 #define POINTER1 x5 #define SCHEDULES x6 #define WTMP w7 #define ROUND_KEY2 w8 #define V_KEY v5 #define V_FK v6 #define V_MAP v7 /* * void vpsm4_ex_set_key(const unsigned char *userKey, SM4_KEY *key, int enc); * generate sm4 rounk key context * USER_KEY => userKey; * ROUND_KEY1 => key ; * if encryption:ENC=>enc */ .type vpsm4_ex_set_key,%function .align 4 vpsm4_ex_set_key: AARCH64_PACIASP stp x29, x30, [sp, #-0x30]! stp d8, d9, [sp, #0x10] stp d10, d11, [sp, #0x20] ld1 {V_KEY.4s},[USER_KEY] LOAD_SBOX_MATRIX REV32_EQ(V_KEY,V_KEY) adrp POINTER1,.Lshuffles add POINTER1,POINTER1,:lo12:.Lshuffles ld1 {V_MAP.4s},[POINTER1] adrp POINTER1,.Lfk add POINTER1,POINTER1,:lo12:.Lfk ld1 {V_FK.4s},[POINTER1] eor V_KEY.16b,V_KEY.16b,V_FK.16b mov SCHEDULES,#32 adrp POINTER1,.Lck add POINTER1,POINTER1,:lo12:.Lck movi VTMP0.16b,#64 cbnz ENC1,1f add ROUND_KEY1,ROUND_KEY1,124 1: // loop mov WTMP,V_KEY.s[1] ldr ROUND_KEY2,[POINTER1],#4 eor ROUND_KEY2,ROUND_KEY2,WTMP mov WTMP,V_KEY.s[2] eor ROUND_KEY2,ROUND_KEY2,WTMP mov WTMP,V_KEY.s[3] eor ROUND_KEY2,ROUND_KEY2,WTMP /* optimize sbox using AESE instruction */ mov DATA0.s[0],ROUND_KEY2 tbl VTMP0.16b, {DATA0.16b}, MaskV.16b MUL_MATRIX(VTMP0, TAHMatV, TALMatV, VTMP2) eor VTMP1.16b, VTMP1.16b, VTMP1.16b aese VTMP0.16b,VTMP1.16b MUL_MATRIX(VTMP0, ATAHMatV, ATALMatV, VTMP2) mov WTMP,VTMP0.s[0] /* linear transformation */ eor ROUND_KEY2,WTMP,WTMP,ror #19 eor ROUND_KEY2,ROUND_KEY2,WTMP,ror #9 mov WTMP,V_KEY.s[0] eor ROUND_KEY2,ROUND_KEY2,WTMP mov V_KEY.s[0],ROUND_KEY2 cbz ENC1,2f str ROUND_KEY2,[ROUND_KEY1],#4 b 3f 2: // set encrypt key str ROUND_KEY2,[ROUND_KEY1],#-4 3: // final tbl V_KEY.16b,{V_KEY.16b},V_MAP.16b subs SCHEDULES,SCHEDULES,#1 b.ne 1b /*clear register for temp key */ eor V_KEY.16b, V_KEY.16b, V_KEY.16b eor ROUND_KEY2, ROUND_KEY2, ROUND_KEY2 ldp d10, d11, [sp, #0x20] ldp d8, d9, [sp, #0x10] ldp x29, x30, [sp], #0x30 AARCH64_AUTIASP ret .size vpsm4_ex_set_key,.-vpsm4_ex_set_key /* * void Vpsm4SetEncryptKey(const unsigned char *userKey, SM4_KEY *key); * generate SM4 encrypt round KEY context * x0 => userKey; x1 => key */ .globl Vpsm4SetEncryptKey .type Vpsm4SetEncryptKey,%function .align 5 Vpsm4SetEncryptKey: AARCH64_PACIASP stp x29,x30,[sp,#-16]! mov w2,1 bl vpsm4_ex_set_key ldp x29,x30,[sp],#16 AARCH64_AUTIASP ret .size Vpsm4SetEncryptKey,.-Vpsm4SetEncryptKey /* * void Vpsm4SetDecryptKey(const unsigned char *userKey, SM4_KEY *key); * generate SM4 decryption round KEY context * x0 => userKey; x1 => key */ .globl Vpsm4SetDecryptKey .type Vpsm4SetDecryptKey,%function .align 5 Vpsm4SetDecryptKey: AARCH64_PACIASP stp x29,x30,[sp,#-16]! mov w2,0 bl vpsm4_ex_set_key ldp x29,x30,[sp],#16 AARCH64_AUTIASP ret .size Vpsm4SetDecryptKey,.-Vpsm4SetDecryptKey #endif
2301_79861745/bench_create
crypto/sm4/src/asm/crypt_sm4_ex_armv8.S
Unix Assembly
unknown
6,469
/* * 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_SM4 .file "crypt_sm4_macro_x86_64.s" .macro MUL_MATRIX A0 HI_MASK LO_MASK vpsrlw $4,\A0,%ymm5 vpand %ymm14,\A0,%ymm6 vpand %ymm14,%ymm5,%ymm5 vpshufb %ymm6,\LO_MASK,%ymm6 vpshufb %ymm5,\HI_MASK,%ymm5 vpxor %ymm6,%ymm5,\A0 .endm .macro SM4_MM256_AESENCLAST A0 vextractf128 $0,\A0,%xmm5 vextractf128 $1,\A0,%xmm6 vpxor %xmm12,%xmm12,%xmm12 vaesenclast %xmm12,%xmm5,%xmm5 vaesenclast %xmm12,%xmm6,%xmm6 subq $16, %rsp vmovdqu %xmm5, (%rsp) vbroadcasti128 (%rsp), %ymm5 vinsertf128 $1,%xmm6,%ymm5,\A0 addq $16, %rsp .endm .macro SM4_MM256_ROL A0 A1 Imm1 Imm2 vpslld \Imm1,\A1,%ymm5 vpsrld \Imm2,\A1,%ymm6 vpxor %ymm5,%ymm6,%ymm12 vpxor %ymm12,\A0,\A0 .endm ##### SBOX Extend Tables (1 Table, 256*4 bytes): SBOX_0, SBOX_1, SBOX_2, SBOX_3 ##### ##### MASK: XOR SHUFFLE LOAD STORE ##### .section .rodata .align 64 SBOX4X_MASK: #SBOX_0: .long 0xd55b5b8e, 0x924242d0, 0xeaa7a74d, 0xfdfbfb06, 0xcf3333fc, 0xe2878765, 0x3df4f4c9, 0xb5dede6b, 0x1658584e, 0xb4dada6e, 0x14505044, 0xc10b0bca, 0x28a0a088, 0xf8efef17, 0x2cb0b09c, 0x5141411 .long 0x2bacac87, 0x669d9dfb, 0x986a6af2, 0x77d9d9ae, 0x2aa8a882, 0xbcfafa46, 0x4101014, 0xc00f0fcf, 0xa8aaaa02, 0x45111154, 0x134c4c5f, 0x269898be, 0x4825256d, 0x841a1a9e, 0x618181e, 0x9b6666fd .long 0x9e7272ec, 0x4309094a, 0x51414110, 0xf7d3d324, 0x934646d5, 0xecbfbf53, 0x9a6262f8, 0x7be9e992, 0x33ccccff, 0x55515104, 0xb2c2c27, 0x420d0d4f, 0xeeb7b759, 0xcc3f3ff3, 0xaeb2b21c, 0x638989ea .long 0xe7939374, 0xb1cece7f, 0x1c70706c, 0xaba6a60d, 0xca2727ed, 0x8202028, 0xeba3a348, 0x975656c1, 0x82020280, 0xdc7f7fa3, 0x965252c4, 0xf9ebeb12, 0x74d5d5a1, 0x8d3e3eb3, 0x3ffcfcc3, 0xa49a9a3e .long 0x461d1d5b, 0x71c1c1b, 0xa59e9e3b, 0xfff3f30c, 0xf0cfcf3f, 0x72cdcdbf, 0x175c5c4b, 0xb8eaea52, 0x810e0e8f, 0x5865653d, 0x3cf0f0cc, 0x1964647d, 0xe59b9b7e, 0x87161691, 0x4e3d3d73, 0xaaa2a208 .long 0x69a1a1c8, 0x6aadadc7, 0x83060685, 0xb0caca7a, 0x70c5c5b5, 0x659191f4, 0xd96b6bb2, 0x892e2ea7, 0xfbe3e318, 0xe8afaf47, 0xf3c3c33, 0x4a2d2d67, 0x71c1c1b0, 0x5759590e, 0x9f7676e9, 0x35d4d4e1 .long 0x1e787866, 0x249090b4, 0xe383836, 0x5f797926, 0x628d8def, 0x59616138, 0xd2474795, 0xa08a8a2a, 0x259494b1, 0x228888aa, 0x7df1f18c, 0x3bececd7, 0x1040405, 0x218484a5, 0x79e1e198, 0x851e1e9b .long 0xd7535384, 0x0, 0x4719195e, 0x565d5d0b, 0x9d7e7ee3, 0xd04f4f9f, 0x279c9cbb, 0x5349491a, 0x4d31317c, 0x36d8d8ee, 0x208080a, 0xe49f9f7b, 0xa2828220, 0xc71313d4, 0xcb2323e8, 0x9c7a7ae6 .long 0xe9abab42, 0xbdfefe43, 0x882a2aa2, 0xd14b4b9a, 0x41010140, 0xc41f1fdb, 0x38e0e0d8, 0xb7d6d661, 0xa18e8e2f, 0xf4dfdf2b, 0xf1cbcb3a, 0xcd3b3bf6, 0xfae7e71d, 0x608585e5, 0x15545441, 0xa3868625 .long 0xe3838360, 0xacbaba16, 0x5c757529, 0xa6929234, 0x996e6ef7, 0x34d0d0e4, 0x1a686872, 0x54555501, 0xafb6b619, 0x914e4edf, 0x32c8c8fa, 0x30c0c0f0, 0xf6d7d721, 0x8e3232bc, 0xb3c6c675, 0xe08f8f6f .long 0x1d747469, 0xf5dbdb2e, 0xe18b8b6a, 0x2eb8b896, 0x800a0a8a, 0x679999fe, 0xc92b2be2, 0x618181e0, 0xc30303c0, 0x29a4a48d, 0x238c8caf, 0xa9aeae07, 0xd343439, 0x524d4d1f, 0x4f393976, 0x6ebdbdd3 .long 0xd6575781, 0xd86f6fb7, 0x37dcdceb, 0x44151551, 0xdd7b7ba6, 0xfef7f709, 0x8c3a3ab6, 0x2fbcbc93, 0x30c0c0f, 0xfcffff03, 0x6ba9a9c2, 0x73c9c9ba, 0x6cb5b5d9, 0x6db1b1dc, 0x5a6d6d37, 0x50454515 .long 0x8f3636b9, 0x1b6c6c77, 0xadbebe13, 0x904a4ada, 0xb9eeee57, 0xde7777a9, 0xbef2f24c, 0x7efdfd83, 0x11444455, 0xda6767bd, 0x5d71712c, 0x40050545, 0x1f7c7c63, 0x10404050, 0x5b696932, 0xdb6363b8 .long 0xa282822, 0xc20707c5, 0x31c4c4f5, 0x8a2222a8, 0xa7969631, 0xce3737f9, 0x7aeded97, 0xbff6f649, 0x2db4b499, 0x75d1d1a4, 0xd3434390, 0x1248485a, 0xbae2e258, 0xe6979771, 0xb6d2d264, 0xb2c2c270 .long 0x8b2626ad, 0x68a5a5cd, 0x955e5ecb, 0x4b292962, 0xc30303c, 0x945a5ace, 0x76ddddab, 0x7ff9f986, 0x649595f1, 0xbbe6e65d, 0xf2c7c735, 0x924242d, 0xc61717d1, 0x6fb9b9d6, 0xc51b1bde, 0x86121294 .long 0x18606078, 0xf3c3c330, 0x7cf5f589, 0xefb3b35c, 0x3ae8e8d2, 0xdf7373ac, 0x4c353579, 0x208080a0, 0x78e5e59d, 0xedbbbb56, 0x5e7d7d23, 0x3ef8f8c6, 0xd45f5f8b, 0xc82f2fe7, 0x39e4e4dd, 0x49212168 #SBOX_1: .long 0x5b5b8ed5, 0x4242d092, 0xa7a74dea, 0xfbfb06fd, 0x3333fccf, 0x878765e2, 0xf4f4c93d, 0xdede6bb5, 0x58584e16, 0xdada6eb4, 0x50504414, 0xb0bcac1, 0xa0a08828, 0xefef17f8, 0xb0b09c2c, 0x14141105 .long 0xacac872b, 0x9d9dfb66, 0x6a6af298, 0xd9d9ae77, 0xa8a8822a, 0xfafa46bc, 0x10101404, 0xf0fcfc0, 0xaaaa02a8, 0x11115445, 0x4c4c5f13, 0x9898be26, 0x25256d48, 0x1a1a9e84, 0x18181e06, 0x6666fd9b .long 0x7272ec9e, 0x9094a43, 0x41411051, 0xd3d324f7, 0x4646d593, 0xbfbf53ec, 0x6262f89a, 0xe9e9927b, 0xccccff33, 0x51510455, 0x2c2c270b, 0xd0d4f42, 0xb7b759ee, 0x3f3ff3cc, 0xb2b21cae, 0x8989ea63 .long 0x939374e7, 0xcece7fb1, 0x70706c1c, 0xa6a60dab, 0x2727edca, 0x20202808, 0xa3a348eb, 0x5656c197, 0x2028082, 0x7f7fa3dc, 0x5252c496, 0xebeb12f9, 0xd5d5a174, 0x3e3eb38d, 0xfcfcc33f, 0x9a9a3ea4 .long 0x1d1d5b46, 0x1c1c1b07, 0x9e9e3ba5, 0xf3f30cff, 0xcfcf3ff0, 0xcdcdbf72, 0x5c5c4b17, 0xeaea52b8, 0xe0e8f81, 0x65653d58, 0xf0f0cc3c, 0x64647d19, 0x9b9b7ee5, 0x16169187, 0x3d3d734e, 0xa2a208aa .long 0xa1a1c869, 0xadadc76a, 0x6068583, 0xcaca7ab0, 0xc5c5b570, 0x9191f465, 0x6b6bb2d9, 0x2e2ea789, 0xe3e318fb, 0xafaf47e8, 0x3c3c330f, 0x2d2d674a, 0xc1c1b071, 0x59590e57, 0x7676e99f, 0xd4d4e135 .long 0x7878661e, 0x9090b424, 0x3838360e, 0x7979265f, 0x8d8def62, 0x61613859, 0x474795d2, 0x8a8a2aa0, 0x9494b125, 0x8888aa22, 0xf1f18c7d, 0xececd73b, 0x4040501, 0x8484a521, 0xe1e19879, 0x1e1e9b85 .long 0x535384d7, 0x0, 0x19195e47, 0x5d5d0b56, 0x7e7ee39d, 0x4f4f9fd0, 0x9c9cbb27, 0x49491a53, 0x31317c4d, 0xd8d8ee36, 0x8080a02, 0x9f9f7be4, 0x828220a2, 0x1313d4c7, 0x2323e8cb, 0x7a7ae69c .long 0xabab42e9, 0xfefe43bd, 0x2a2aa288, 0x4b4b9ad1, 0x1014041, 0x1f1fdbc4, 0xe0e0d838, 0xd6d661b7, 0x8e8e2fa1, 0xdfdf2bf4, 0xcbcb3af1, 0x3b3bf6cd, 0xe7e71dfa, 0x8585e560, 0x54544115, 0x868625a3 .long 0x838360e3, 0xbaba16ac, 0x7575295c, 0x929234a6, 0x6e6ef799, 0xd0d0e434, 0x6868721a, 0x55550154, 0xb6b619af, 0x4e4edf91, 0xc8c8fa32, 0xc0c0f030, 0xd7d721f6, 0x3232bc8e, 0xc6c675b3, 0x8f8f6fe0 .long 0x7474691d, 0xdbdb2ef5, 0x8b8b6ae1, 0xb8b8962e, 0xa0a8a80, 0x9999fe67, 0x2b2be2c9, 0x8181e061, 0x303c0c3, 0xa4a48d29, 0x8c8caf23, 0xaeae07a9, 0x3434390d, 0x4d4d1f52, 0x3939764f, 0xbdbdd36e .long 0x575781d6, 0x6f6fb7d8, 0xdcdceb37, 0x15155144, 0x7b7ba6dd, 0xf7f709fe, 0x3a3ab68c, 0xbcbc932f, 0xc0c0f03, 0xffff03fc, 0xa9a9c26b, 0xc9c9ba73, 0xb5b5d96c, 0xb1b1dc6d, 0x6d6d375a, 0x45451550 .long 0x3636b98f, 0x6c6c771b, 0xbebe13ad, 0x4a4ada90, 0xeeee57b9, 0x7777a9de, 0xf2f24cbe, 0xfdfd837e, 0x44445511, 0x6767bdda, 0x71712c5d, 0x5054540, 0x7c7c631f, 0x40405010, 0x6969325b, 0x6363b8db .long 0x2828220a, 0x707c5c2, 0xc4c4f531, 0x2222a88a, 0x969631a7, 0x3737f9ce, 0xeded977a, 0xf6f649bf, 0xb4b4992d, 0xd1d1a475, 0x434390d3, 0x48485a12, 0xe2e258ba, 0x979771e6, 0xd2d264b6, 0xc2c270b2 .long 0x2626ad8b, 0xa5a5cd68, 0x5e5ecb95, 0x2929624b, 0x30303c0c, 0x5a5ace94, 0xddddab76, 0xf9f9867f, 0x9595f164, 0xe6e65dbb, 0xc7c735f2, 0x24242d09, 0x1717d1c6, 0xb9b9d66f, 0x1b1bdec5, 0x12129486 .long 0x60607818, 0xc3c330f3, 0xf5f5897c, 0xb3b35cef, 0xe8e8d23a, 0x7373acdf, 0x3535794c, 0x8080a020, 0xe5e59d78, 0xbbbb56ed, 0x7d7d235e, 0xf8f8c63e, 0x5f5f8bd4, 0x2f2fe7c8, 0xe4e4dd39, 0x21216849 #SBOX_2: .long 0x5b8ed55b, 0x42d09242, 0xa74deaa7, 0xfb06fdfb, 0x33fccf33, 0x8765e287, 0xf4c93df4, 0xde6bb5de, 0x584e1658, 0xda6eb4da, 0x50441450, 0xbcac10b, 0xa08828a0, 0xef17f8ef, 0xb09c2cb0, 0x14110514 .long 0xac872bac, 0x9dfb669d, 0x6af2986a, 0xd9ae77d9, 0xa8822aa8, 0xfa46bcfa, 0x10140410, 0xfcfc00f, 0xaa02a8aa, 0x11544511, 0x4c5f134c, 0x98be2698, 0x256d4825, 0x1a9e841a, 0x181e0618, 0x66fd9b66 .long 0x72ec9e72, 0x94a4309, 0x41105141, 0xd324f7d3, 0x46d59346, 0xbf53ecbf, 0x62f89a62, 0xe9927be9, 0xccff33cc, 0x51045551, 0x2c270b2c, 0xd4f420d, 0xb759eeb7, 0x3ff3cc3f, 0xb21caeb2, 0x89ea6389 .long 0x9374e793, 0xce7fb1ce, 0x706c1c70, 0xa60daba6, 0x27edca27, 0x20280820, 0xa348eba3, 0x56c19756, 0x2808202, 0x7fa3dc7f, 0x52c49652, 0xeb12f9eb, 0xd5a174d5, 0x3eb38d3e, 0xfcc33ffc, 0x9a3ea49a .long 0x1d5b461d, 0x1c1b071c, 0x9e3ba59e, 0xf30cfff3, 0xcf3ff0cf, 0xcdbf72cd, 0x5c4b175c, 0xea52b8ea, 0xe8f810e, 0x653d5865, 0xf0cc3cf0, 0x647d1964, 0x9b7ee59b, 0x16918716, 0x3d734e3d, 0xa208aaa2 .long 0xa1c869a1, 0xadc76aad, 0x6858306, 0xca7ab0ca, 0xc5b570c5, 0x91f46591, 0x6bb2d96b, 0x2ea7892e, 0xe318fbe3, 0xaf47e8af, 0x3c330f3c, 0x2d674a2d, 0xc1b071c1, 0x590e5759, 0x76e99f76, 0xd4e135d4 .long 0x78661e78, 0x90b42490, 0x38360e38, 0x79265f79, 0x8def628d, 0x61385961, 0x4795d247, 0x8a2aa08a, 0x94b12594, 0x88aa2288, 0xf18c7df1, 0xecd73bec, 0x4050104, 0x84a52184, 0xe19879e1, 0x1e9b851e .long 0x5384d753, 0x0, 0x195e4719, 0x5d0b565d, 0x7ee39d7e, 0x4f9fd04f, 0x9cbb279c, 0x491a5349, 0x317c4d31, 0xd8ee36d8, 0x80a0208, 0x9f7be49f, 0x8220a282, 0x13d4c713, 0x23e8cb23, 0x7ae69c7a .long 0xab42e9ab, 0xfe43bdfe, 0x2aa2882a, 0x4b9ad14b, 0x1404101, 0x1fdbc41f, 0xe0d838e0, 0xd661b7d6, 0x8e2fa18e, 0xdf2bf4df, 0xcb3af1cb, 0x3bf6cd3b, 0xe71dfae7, 0x85e56085, 0x54411554, 0x8625a386 .long 0x8360e383, 0xba16acba, 0x75295c75, 0x9234a692, 0x6ef7996e, 0xd0e434d0, 0x68721a68, 0x55015455, 0xb619afb6, 0x4edf914e, 0xc8fa32c8, 0xc0f030c0, 0xd721f6d7, 0x32bc8e32, 0xc675b3c6, 0x8f6fe08f .long 0x74691d74, 0xdb2ef5db, 0x8b6ae18b, 0xb8962eb8, 0xa8a800a, 0x99fe6799, 0x2be2c92b, 0x81e06181, 0x3c0c303, 0xa48d29a4, 0x8caf238c, 0xae07a9ae, 0x34390d34, 0x4d1f524d, 0x39764f39, 0xbdd36ebd .long 0x5781d657, 0x6fb7d86f, 0xdceb37dc, 0x15514415, 0x7ba6dd7b, 0xf709fef7, 0x3ab68c3a, 0xbc932fbc, 0xc0f030c, 0xff03fcff, 0xa9c26ba9, 0xc9ba73c9, 0xb5d96cb5, 0xb1dc6db1, 0x6d375a6d, 0x45155045 .long 0x36b98f36, 0x6c771b6c, 0xbe13adbe, 0x4ada904a, 0xee57b9ee, 0x77a9de77, 0xf24cbef2, 0xfd837efd, 0x44551144, 0x67bdda67, 0x712c5d71, 0x5454005, 0x7c631f7c, 0x40501040, 0x69325b69, 0x63b8db63 .long 0x28220a28, 0x7c5c207, 0xc4f531c4, 0x22a88a22, 0x9631a796, 0x37f9ce37, 0xed977aed, 0xf649bff6, 0xb4992db4, 0xd1a475d1, 0x4390d343, 0x485a1248, 0xe258bae2, 0x9771e697, 0xd264b6d2, 0xc270b2c2 .long 0x26ad8b26, 0xa5cd68a5, 0x5ecb955e, 0x29624b29, 0x303c0c30, 0x5ace945a, 0xddab76dd, 0xf9867ff9, 0x95f16495, 0xe65dbbe6, 0xc735f2c7, 0x242d0924, 0x17d1c617, 0xb9d66fb9, 0x1bdec51b, 0x12948612 .long 0x60781860, 0xc330f3c3, 0xf5897cf5, 0xb35cefb3, 0xe8d23ae8, 0x73acdf73, 0x35794c35, 0x80a02080, 0xe59d78e5, 0xbb56edbb, 0x7d235e7d, 0xf8c63ef8, 0x5f8bd45f, 0x2fe7c82f, 0xe4dd39e4, 0x21684921 #SBOX_3: .long 0x8ed55b5b, 0xd0924242, 0x4deaa7a7, 0x6fdfbfb, 0xfccf3333, 0x65e28787, 0xc93df4f4, 0x6bb5dede, 0x4e165858, 0x6eb4dada, 0x44145050, 0xcac10b0b, 0x8828a0a0, 0x17f8efef, 0x9c2cb0b0, 0x11051414 .long 0x872bacac, 0xfb669d9d, 0xf2986a6a, 0xae77d9d9, 0x822aa8a8, 0x46bcfafa, 0x14041010, 0xcfc00f0f, 0x2a8aaaa, 0x54451111, 0x5f134c4c, 0xbe269898, 0x6d482525, 0x9e841a1a, 0x1e061818, 0xfd9b6666 .long 0xec9e7272, 0x4a430909, 0x10514141, 0x24f7d3d3, 0xd5934646, 0x53ecbfbf, 0xf89a6262, 0x927be9e9, 0xff33cccc, 0x4555151, 0x270b2c2c, 0x4f420d0d, 0x59eeb7b7, 0xf3cc3f3f, 0x1caeb2b2, 0xea638989 .long 0x74e79393, 0x7fb1cece, 0x6c1c7070, 0xdaba6a6, 0xedca2727, 0x28082020, 0x48eba3a3, 0xc1975656, 0x80820202, 0xa3dc7f7f, 0xc4965252, 0x12f9ebeb, 0xa174d5d5, 0xb38d3e3e, 0xc33ffcfc, 0x3ea49a9a .long 0x5b461d1d, 0x1b071c1c, 0x3ba59e9e, 0xcfff3f3, 0x3ff0cfcf, 0xbf72cdcd, 0x4b175c5c, 0x52b8eaea, 0x8f810e0e, 0x3d586565, 0xcc3cf0f0, 0x7d196464, 0x7ee59b9b, 0x91871616, 0x734e3d3d, 0x8aaa2a2 .long 0xc869a1a1, 0xc76aadad, 0x85830606, 0x7ab0caca, 0xb570c5c5, 0xf4659191, 0xb2d96b6b, 0xa7892e2e, 0x18fbe3e3, 0x47e8afaf, 0x330f3c3c, 0x674a2d2d, 0xb071c1c1, 0xe575959, 0xe99f7676, 0xe135d4d4 .long 0x661e7878, 0xb4249090, 0x360e3838, 0x265f7979, 0xef628d8d, 0x38596161, 0x95d24747, 0x2aa08a8a, 0xb1259494, 0xaa228888, 0x8c7df1f1, 0xd73becec, 0x5010404, 0xa5218484, 0x9879e1e1, 0x9b851e1e .long 0x84d75353, 0x0, 0x5e471919, 0xb565d5d, 0xe39d7e7e, 0x9fd04f4f, 0xbb279c9c, 0x1a534949, 0x7c4d3131, 0xee36d8d8, 0xa020808, 0x7be49f9f, 0x20a28282, 0xd4c71313, 0xe8cb2323, 0xe69c7a7a .long 0x42e9abab, 0x43bdfefe, 0xa2882a2a, 0x9ad14b4b, 0x40410101, 0xdbc41f1f, 0xd838e0e0, 0x61b7d6d6, 0x2fa18e8e, 0x2bf4dfdf, 0x3af1cbcb, 0xf6cd3b3b, 0x1dfae7e7, 0xe5608585, 0x41155454, 0x25a38686 .long 0x60e38383, 0x16acbaba, 0x295c7575, 0x34a69292, 0xf7996e6e, 0xe434d0d0, 0x721a6868, 0x1545555, 0x19afb6b6, 0xdf914e4e, 0xfa32c8c8, 0xf030c0c0, 0x21f6d7d7, 0xbc8e3232, 0x75b3c6c6, 0x6fe08f8f .long 0x691d7474, 0x2ef5dbdb, 0x6ae18b8b, 0x962eb8b8, 0x8a800a0a, 0xfe679999, 0xe2c92b2b, 0xe0618181, 0xc0c30303, 0x8d29a4a4, 0xaf238c8c, 0x7a9aeae, 0x390d3434, 0x1f524d4d, 0x764f3939, 0xd36ebdbd .long 0x81d65757, 0xb7d86f6f, 0xeb37dcdc, 0x51441515, 0xa6dd7b7b, 0x9fef7f7, 0xb68c3a3a, 0x932fbcbc, 0xf030c0c, 0x3fcffff, 0xc26ba9a9, 0xba73c9c9, 0xd96cb5b5, 0xdc6db1b1, 0x375a6d6d, 0x15504545 .long 0xb98f3636, 0x771b6c6c, 0x13adbebe, 0xda904a4a, 0x57b9eeee, 0xa9de7777, 0x4cbef2f2, 0x837efdfd, 0x55114444, 0xbdda6767, 0x2c5d7171, 0x45400505, 0x631f7c7c, 0x50104040, 0x325b6969, 0xb8db6363 .long 0x220a2828, 0xc5c20707, 0xf531c4c4, 0xa88a2222, 0x31a79696, 0xf9ce3737, 0x977aeded, 0x49bff6f6, 0x992db4b4, 0xa475d1d1, 0x90d34343, 0x5a124848, 0x58bae2e2, 0x71e69797, 0x64b6d2d2, 0x70b2c2c2 .long 0xad8b2626, 0xcd68a5a5, 0xcb955e5e, 0x624b2929, 0x3c0c3030, 0xce945a5a, 0xab76dddd, 0x867ff9f9, 0xf1649595, 0x5dbbe6e6, 0x35f2c7c7, 0x2d092424, 0xd1c61717, 0xd66fb9b9, 0xdec51b1b, 0x94861212 .long 0x78186060, 0x30f3c3c3, 0x897cf5f5, 0x5cefb3b3, 0xd23ae8e8, 0xacdf7373, 0x794c3535, 0xa0208080, 0x9d78e5e5, 0x56edbbbb, 0x235e7d7d, 0xc63ef8f8, 0x8bd45f5f, 0xe7c82f2f, 0xdd39e4e4, 0x68492121 #.xor_mask: .long 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF #.shuffle_mask: .byte 3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12,3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12 #.load_mask: .long 0,4,8,12,16,20,24,28 #.store_mask: .long 0,8,16,24,1,9,17,25 #below are for AESNI #.aes_mask: 128+4096(%rax) .byte 0x00, 0x0d, 0x0a, 0x07, 0x04, 0x01, 0x0e, 0x0b, 0x08, 0x05, 0x02, 0x0f, 0x0c, 0x09, 0x06, 0x03 .byte 0x00, 0x0d, 0x0a, 0x07, 0x04, 0x01, 0x0e, 0x0b, 0x08, 0x05, 0x02, 0x0f, 0x0c, 0x09, 0x06, 0x03 #.aes_hi_mask_0: 160+4096(%rax) .byte 0x00, 0x7a, 0x38, 0x42, 0x20, 0x5a, 0x18, 0x62, 0x40, 0x3a, 0x78, 0x02, 0x60, 0x1a, 0x58, 0x22 .byte 0x00, 0x7a, 0x38, 0x42, 0x20, 0x5a, 0x18, 0x62, 0x40, 0x3a, 0x78, 0x02, 0x60, 0x1a, 0x58, 0x22 #.aes_hi_mask_1: 192+4096(%rax) .byte 0x00, 0x13, 0xd2, 0xc1, 0x78, 0x6b, 0xaa, 0xb9, 0xad, 0xbe, 0x7f, 0x6c, 0xd5, 0xc6, 0x07, 0x14 .byte 0x00, 0x13, 0xd2, 0xc1, 0x78, 0x6b, 0xaa, 0xb9, 0xad, 0xbe, 0x7f, 0x6c, 0xd5, 0xc6, 0x07, 0x14 #.aes_lo_mask_0: 224+4096(%rax) .byte 0x00, 0xca, 0x77, 0xbd, 0x8b, 0x41, 0xfc, 0x36, 0xd4, 0x1e, 0xa3, 0x69, 0x5f, 0x95, 0x28, 0xe2 .byte 0x00, 0xca, 0x77, 0xbd, 0x8b, 0x41, 0xfc, 0x36, 0xd4, 0x1e, 0xa3, 0x69, 0x5f, 0x95, 0x28, 0xe2 #.aes_lo_mask_1: 256+4096(%rax) .byte 0x00, 0x60, 0x22, 0x42, 0x1d, 0x7d, 0x3f, 0x5f, 0x87, 0xe7, 0xa5, 0xc5, 0x9a, 0xfa, 0xb8, 0xd8 .byte 0x00, 0x60, 0x22, 0x42, 0x1d, 0x7d, 0x3f, 0x5f, 0x87, 0xe7, 0xa5, 0xc5, 0x9a, 0xfa, 0xb8, 0xd8 #.aes_and_mask: 288+4096(%rax) .byte 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f .byte 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f #.aes_mask_ta: 320 .byte 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35 .byte 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35 #.aes_mask_ata: 352 .byte 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59 .byte 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59, 59 #endif
2301_79861745/bench_create
crypto/sm4/src/asm/crypt_sm4_macro_x86_64.s
Unix Assembly
unknown
16,207
/* * 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_SM4 .file "crypt_sm4_modes_macro_x86_64.s" .set TMP0,%ymm8 .set TMP1,%ymm9 .set TMP2,%ymm10 .set TMP3,%ymm11 .set TMP4,%ymm12 .set TMP5,%ymm13 .set TMP6,%ymm14 .set TMP7,%ymm15 .set AES_AND_MASK,%ymm14 .set AES_MASK,%ymm15 .set TMP0x,%xmm8 .set TMP1x,%xmm9 .set TMP2x,%xmm10 .set TMP3x,%xmm11 .set TMP4x,%xmm12 .set TMP5x,%xmm13 .set PreConst, 0x3e .set PostConst, 0xd3 .set PreAffineTReg, %xmm10 .set PostAffineTReg, %xmm11 .set PreAffineTRegBLOCK16,%ymm14 .set PostAffineTRegBLOCK16,%ymm15 .macro SM4_SBOX A B PreReg PostReg TMP vgf2p8affineqb $PreConst,\PreReg,\A,\TMP vgf2p8affineinvqb $PostConst,\PostReg,\TMP,\B .endm .macro KeyExpandLTrans A B movl \A,\B movl \A,%r14d roll $13,%r14d xorl %r14d,\B movl \A,%r14d roll $23,%r14d xorl %r14d,\B .endm .macro LTrans A B movl \A,\B movl \A,%r14d roll $2,%r14d xorl %r14d,\B movl \A,%r14d roll $10,%r14d xorl %r14d,\B movl \A,%r14d roll $18,%r14d xorl %r14d,\B movl \A,%r14d roll $24,%r14d xorl %r14d,\B .endm /* * x4 = x1 ^ x2 ^ x3 ^ *(rk + i); * x4 = SBOX(x4); * x0 = x0 ^ L32(x4); */ .macro SM4_ROUND A0 A1 A2 A3 RKey No # x4 = x1 ^ x2 ^ x3 ^ *(rk + i); movl \No(\RKey), T0 xorl \A1, T0 xorl \A2, T0 xorl \A3, T0 # x0 = x0 ^ (SBOX_0[x4 & 0xff]) ^ (SBOX_1[(x4 >> 8) & 0xff]) ^ (SBOX_2[(x4 >> 16) & 0xff]) ^ (SBOX_3[(x4 >> 24) & 0xff]); movzbl T0BL, T1 xorl (ADDR,T1_64,4), \A0 shrl $8, T0 movzbl T0BL, T1 xorl 1024(ADDR,T1_64,4), \A0 shrl $8, T0 movzbl T0BL, T1 xorl 2048(ADDR,T1_64,4), \A0 shrl $8, T0 xorl 3072(ADDR,T0_64,4), \A0 .endm .macro MATRIX_MUL X HI_MASK LO_MASK vpsrlw $4,\X,TMP4 vpand AES_AND_MASK,\X,TMP5 vpand AES_AND_MASK,TMP4,TMP4 vpshufb TMP5,\LO_MASK,TMP5 vpshufb TMP4,\HI_MASK,TMP4 vpxor TMP5,TMP4,\X .endm .macro MATRIX_TRANSPOSE A0 A1 A2 A3 vpunpckhdq \A1,\A0,TMP0 vpunpckhdq \A3,\A2,TMP1 vpunpckldq \A1,\A0,\A0 vpunpckldq \A3,\A2,\A2 vpunpckhqdq \A2,\A0,\A1 vpunpcklqdq \A2,\A0,\A0 vpunpckhqdq TMP1,TMP0,\A3 vpunpcklqdq TMP1,TMP0,\A2 .endm .macro SM4_LT X Y # load LR masks (8 16 24) vmovdqa 288+4096(%rax),TMP3 vmovdqa 320+4096(%rax),TMP4 vmovdqa 352+4096(%rax),TMP5 vpshufb TMP5,\Y,TMP2 vpxor TMP2,\X,\X vpxor \Y,\X,\X vpshufb TMP3,\Y,TMP2 vpshufb TMP4,\Y,TMP5 vpxor TMP5,TMP2,TMP2 vpxor \Y,TMP2,TMP2 vpslld $2,TMP2,TMP3 vpsrld $30,TMP2,TMP4 vpxor TMP4,TMP3,TMP3 vpxor TMP3,\X,\X .endm # load aes_mask and aes_and_mask prewards .macro SM4_AVX2_AES_2_ROUND A0 A1 A2 A3 B0 B1 B2 B3 RKey No # load round key vpbroadcastd \No(\RKey),TMP0 # S-BOX inputs vpxor \B1,TMP0,TMP1 vpxor \B2,TMP1,TMP1 vpxor \B3,TMP1,TMP1 vpxor \A1,TMP0,TMP0 vpxor \A2,TMP0,TMP0 vpxor \A3,TMP0,TMP0 # inverse shift rows vpshufb AES_MASK,TMP0,TMP0 vpshufb AES_MASK,TMP1,TMP1 # SM4 with AESENCLAST : SM4-S(x) = A2(AES-S(A1(x))) # load aes_hi_mask_0 & aes_lo_mask_0 vmovdqa 128+4096(%rax),TMP2 vmovdqa 160+4096(%rax),TMP3 # inner affine transform : A1(x) = M1*x + C1 MATRIX_MUL TMP0 TMP2 TMP3 MATRIX_MUL TMP1 TMP2 TMP3 # aesni instruction : AES-S(x) # perform AES-S transformation vmovdqa 192+4096(%rax),TMP4 vextracti128 $1,TMP0,TMP2x vextracti128 $1,TMP1,TMP3x vaesenclast TMP4x,TMP0x,TMP0x vaesenclast TMP4x,TMP1x,TMP1x vaesenclast TMP4x,TMP2x,TMP2x vaesenclast TMP4x,TMP3x,TMP3x vinserti128 $1,TMP2x,TMP0,TMP0 vinserti128 $1,TMP3x,TMP1,TMP1 # load aes_hi_mask_1 & aes_lo_mask_1 vmovdqa 224+4096(%rax),TMP2 vmovdqa 256+4096(%rax),TMP3 # outer affine transform : A2(x) = M2*x + C2 MATRIX_MUL TMP0 TMP2 TMP3 MATRIX_MUL TMP1 TMP2 TMP3 # linear transform : 4 parallel left rotations SM4_LT \A0 TMP0 SM4_LT \B0 TMP1 .endm # gfni implementation .macro SM4_AVX2_GFNI_2_ROUND A0 A1 A2 A3 B0 B1 B2 B3 RKey No # load round key vpbroadcastd \No(\RKey),TMP0 # S-BOX inputs vpxor \B1,TMP0,TMP1 vpxor \B2,TMP1,TMP1 vpxor \B3,TMP1,TMP1 vpxor \A1,TMP0,TMP0 vpxor \A2,TMP0,TMP0 vpxor \A3,TMP0,TMP0 # sbox SM4_SBOX TMP1 TMP1 PreAffineTRegBLOCK16 PostAffineTRegBLOCK16 %ymm10 SM4_SBOX TMP0 TMP0 PreAffineTRegBLOCK16 PostAffineTRegBLOCK16 %ymm10 # linear transform : 4 parallel left rotations SM4_LT \A0 TMP0 SM4_LT \B0 TMP1 .endm .macro SM4_SERIAL_ROUNDS xorq T0_64, T0_64 xorq T1_64, T1_64 SM4_ROUND W0 W1 W2 W3 RK 0 SM4_ROUND W1 W2 W3 W0 RK 4 SM4_ROUND W2 W3 W0 W1 RK 8 SM4_ROUND W3 W0 W1 W2 RK 12 SM4_ROUND W0 W1 W2 W3 RK 16 SM4_ROUND W1 W2 W3 W0 RK 20 SM4_ROUND W2 W3 W0 W1 RK 24 SM4_ROUND W3 W0 W1 W2 RK 28 SM4_ROUND W0 W1 W2 W3 RK 32 SM4_ROUND W1 W2 W3 W0 RK 36 SM4_ROUND W2 W3 W0 W1 RK 40 SM4_ROUND W3 W0 W1 W2 RK 44 SM4_ROUND W0 W1 W2 W3 RK 48 SM4_ROUND W1 W2 W3 W0 RK 52 SM4_ROUND W2 W3 W0 W1 RK 56 SM4_ROUND W3 W0 W1 W2 RK 60 SM4_ROUND W0 W1 W2 W3 RK 64 SM4_ROUND W1 W2 W3 W0 RK 68 SM4_ROUND W2 W3 W0 W1 RK 72 SM4_ROUND W3 W0 W1 W2 RK 76 SM4_ROUND W0 W1 W2 W3 RK 80 SM4_ROUND W1 W2 W3 W0 RK 84 SM4_ROUND W2 W3 W0 W1 RK 88 SM4_ROUND W3 W0 W1 W2 RK 92 SM4_ROUND W0 W1 W2 W3 RK 96 SM4_ROUND W1 W2 W3 W0 RK 100 SM4_ROUND W2 W3 W0 W1 RK 104 SM4_ROUND W3 W0 W1 W2 RK 108 SM4_ROUND W0 W1 W2 W3 RK 112 SM4_ROUND W1 W2 W3 W0 RK 116 SM4_ROUND W2 W3 W0 W1 RK 120 SM4_ROUND W3 W0 W1 W2 RK 124 .endm .macro SM4_AVX2_AES_2_ROUNDS SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 0 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 4 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 8 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 12 SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 16 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 20 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 24 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 28 SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 32 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 36 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 40 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 44 SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 48 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 52 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 56 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 60 SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 64 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 68 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 72 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 76 SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 80 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 84 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 88 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 92 SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 96 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 100 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 104 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 108 SM4_AVX2_AES_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 112 SM4_AVX2_AES_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 116 SM4_AVX2_AES_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 120 SM4_AVX2_AES_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 124 .endm .macro SM4_AVX2_GFNI_2_ROUNDS SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 0 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 4 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 8 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 12 SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 16 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 20 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 24 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 28 SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 32 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 36 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 40 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 44 SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 48 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 52 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 56 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 60 SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 64 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 68 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 72 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 76 SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 80 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 84 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 88 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 92 SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 96 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 100 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 104 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 108 SM4_AVX2_GFNI_2_ROUND X0 X1 X2 X3 Y0 Y1 Y2 Y3 RK 112 SM4_AVX2_GFNI_2_ROUND X1 X2 X3 X0 Y1 Y2 Y3 Y0 RK 116 SM4_AVX2_GFNI_2_ROUND X2 X3 X0 X1 Y2 Y3 Y0 Y1 RK 120 SM4_AVX2_GFNI_2_ROUND X3 X0 X1 X2 Y3 Y0 Y1 Y2 RK 124 .endm ##### SBOX Extend Tables (1 Table, 256*4 bytes): SBOX_0, SBOX_1, SBOX_2, SBOX_3 ##### .section .rodata .balign 64 .PreAffinT: .quad 0x4c287db91a22505d .PostAffinT: .quad 0xf3ab34a974a6b589 .align 64 SBOX4X_MASK: #SBOX_0: .long 0xd55b5b8e, 0x924242d0, 0xeaa7a74d, 0xfdfbfb06, 0xcf3333fc, 0xe2878765, 0x3df4f4c9, 0xb5dede6b, 0x1658584e, 0xb4dada6e, 0x14505044, 0xc10b0bca, 0x28a0a088, 0xf8efef17, 0x2cb0b09c, 0x05141411 .long 0x2bacac87, 0x669d9dfb, 0x986a6af2, 0x77d9d9ae, 0x2aa8a882, 0xbcfafa46, 0x04101014, 0xc00f0fcf, 0xa8aaaa02, 0x45111154, 0x134c4c5f, 0x269898be, 0x4825256d, 0x841a1a9e, 0x0618181e, 0x9b6666fd .long 0x9e7272ec, 0x4309094a, 0x51414110, 0xf7d3d324, 0x934646d5, 0xecbfbf53, 0x9a6262f8, 0x7be9e992, 0x33ccccff, 0x55515104, 0x0b2c2c27, 0x420d0d4f, 0xeeb7b759, 0xcc3f3ff3, 0xaeb2b21c, 0x638989ea .long 0xe7939374, 0xb1cece7f, 0x1c70706c, 0xaba6a60d, 0xca2727ed, 0x08202028, 0xeba3a348, 0x975656c1, 0x82020280, 0xdc7f7fa3, 0x965252c4, 0xf9ebeb12, 0x74d5d5a1, 0x8d3e3eb3, 0x3ffcfcc3, 0xa49a9a3e .long 0x461d1d5b, 0x071c1c1b, 0xa59e9e3b, 0xfff3f30c, 0xf0cfcf3f, 0x72cdcdbf, 0x175c5c4b, 0xb8eaea52, 0x810e0e8f, 0x5865653d, 0x3cf0f0cc, 0x1964647d, 0xe59b9b7e, 0x87161691, 0x4e3d3d73, 0xaaa2a208 .long 0x69a1a1c8, 0x6aadadc7, 0x83060685, 0xb0caca7a, 0x70c5c5b5, 0x659191f4, 0xd96b6bb2, 0x892e2ea7, 0xfbe3e318, 0xe8afaf47, 0x0f3c3c33, 0x4a2d2d67, 0x71c1c1b0, 0x5759590e, 0x9f7676e9, 0x35d4d4e1 .long 0x1e787866, 0x249090b4, 0x0e383836, 0x5f797926, 0x628d8def, 0x59616138, 0xd2474795, 0xa08a8a2a, 0x259494b1, 0x228888aa, 0x7df1f18c, 0x3bececd7, 0x01040405, 0x218484a5, 0x79e1e198, 0x851e1e9b .long 0xd7535384, 0x0, 0x4719195e, 0x565d5d0b, 0x9d7e7ee3, 0xd04f4f9f, 0x279c9cbb, 0x5349491a, 0x4d31317c, 0x36d8d8ee, 0x0208080a, 0xe49f9f7b, 0xa2828220, 0xc71313d4, 0xcb2323e8, 0x9c7a7ae6 .long 0xe9abab42, 0xbdfefe43, 0x882a2aa2, 0xd14b4b9a, 0x41010140, 0xc41f1fdb, 0x38e0e0d8, 0xb7d6d661, 0xa18e8e2f, 0xf4dfdf2b, 0xf1cbcb3a, 0xcd3b3bf6, 0xfae7e71d, 0x608585e5, 0x15545441, 0xa3868625 .long 0xe3838360, 0xacbaba16, 0x5c757529, 0xa6929234, 0x996e6ef7, 0x34d0d0e4, 0x1a686872, 0x54555501, 0xafb6b619, 0x914e4edf, 0x32c8c8fa, 0x30c0c0f0, 0xf6d7d721, 0x8e3232bc, 0xb3c6c675, 0xe08f8f6f .long 0x1d747469, 0xf5dbdb2e, 0xe18b8b6a, 0x2eb8b896, 0x800a0a8a, 0x679999fe, 0xc92b2be2, 0x618181e0, 0xc30303c0, 0x29a4a48d, 0x238c8caf, 0xa9aeae07, 0x0d343439, 0x524d4d1f, 0x4f393976, 0x6ebdbdd3 .long 0xd6575781, 0xd86f6fb7, 0x37dcdceb, 0x44151551, 0xdd7b7ba6, 0xfef7f709, 0x8c3a3ab6, 0x2fbcbc93, 0x030c0c0f, 0xfcffff03, 0x6ba9a9c2, 0x73c9c9ba, 0x6cb5b5d9, 0x6db1b1dc, 0x5a6d6d37, 0x50454515 .long 0x8f3636b9, 0x1b6c6c77, 0xadbebe13, 0x904a4ada, 0xb9eeee57, 0xde7777a9, 0xbef2f24c, 0x7efdfd83, 0x11444455, 0xda6767bd, 0x5d71712c, 0x40050545, 0x1f7c7c63, 0x10404050, 0x5b696932, 0xdb6363b8 .long 0x0a282822, 0xc20707c5, 0x31c4c4f5, 0x8a2222a8, 0xa7969631, 0xce3737f9, 0x7aeded97, 0xbff6f649, 0x2db4b499, 0x75d1d1a4, 0xd3434390, 0x1248485a, 0xbae2e258, 0xe6979771, 0xb6d2d264, 0xb2c2c270 .long 0x8b2626ad, 0x68a5a5cd, 0x955e5ecb, 0x4b292962, 0x0c30303c, 0x945a5ace, 0x76ddddab, 0x7ff9f986, 0x649595f1, 0xbbe6e65d, 0xf2c7c735, 0x0924242d, 0xc61717d1, 0x6fb9b9d6, 0xc51b1bde, 0x86121294 .long 0x18606078, 0xf3c3c330, 0x7cf5f589, 0xefb3b35c, 0x3ae8e8d2, 0xdf7373ac, 0x4c353579, 0x208080a0, 0x78e5e59d, 0xedbbbb56, 0x5e7d7d23, 0x3ef8f8c6, 0xd45f5f8b, 0xc82f2fe7, 0x39e4e4dd, 0x49212168 #SBOX_1: .long 0x5b5b8ed5, 0x4242d092, 0xa7a74dea, 0xfbfb06fd, 0x3333fccf, 0x878765e2, 0xf4f4c93d, 0xdede6bb5, 0x58584e16, 0xdada6eb4, 0x50504414, 0x0b0bcac1, 0xa0a08828, 0xefef17f8, 0xb0b09c2c, 0x14141105 .long 0xacac872b, 0x9d9dfb66, 0x6a6af298, 0xd9d9ae77, 0xa8a8822a, 0xfafa46bc, 0x10101404, 0x0f0fcfc0, 0xaaaa02a8, 0x11115445, 0x4c4c5f13, 0x9898be26, 0x25256d48, 0x1a1a9e84, 0x18181e06, 0x6666fd9b .long 0x7272ec9e, 0x09094a43, 0x41411051, 0xd3d324f7, 0x4646d593, 0xbfbf53ec, 0x6262f89a, 0xe9e9927b, 0xccccff33, 0x51510455, 0x2c2c270b, 0x0d0d4f42, 0xb7b759ee, 0x3f3ff3cc, 0xb2b21cae, 0x8989ea63 .long 0x939374e7, 0xcece7fb1, 0x70706c1c, 0xa6a60dab, 0x2727edca, 0x20202808, 0xa3a348eb, 0x5656c197, 0x02028082, 0x7f7fa3dc, 0x5252c496, 0xebeb12f9, 0xd5d5a174, 0x3e3eb38d, 0xfcfcc33f, 0x9a9a3ea4 .long 0x1d1d5b46, 0x1c1c1b07, 0x9e9e3ba5, 0xf3f30cff, 0xcfcf3ff0, 0xcdcdbf72, 0x5c5c4b17, 0xeaea52b8, 0x0e0e8f81, 0x65653d58, 0xf0f0cc3c, 0x64647d19, 0x9b9b7ee5, 0x16169187, 0x3d3d734e, 0xa2a208aa .long 0xa1a1c869, 0xadadc76a, 0x06068583, 0xcaca7ab0, 0xc5c5b570, 0x9191f465, 0x6b6bb2d9, 0x2e2ea789, 0xe3e318fb, 0xafaf47e8, 0x3c3c330f, 0x2d2d674a, 0xc1c1b071, 0x59590e57, 0x7676e99f, 0xd4d4e135 .long 0x7878661e, 0x9090b424, 0x3838360e, 0x7979265f, 0x8d8def62, 0x61613859, 0x474795d2, 0x8a8a2aa0, 0x9494b125, 0x8888aa22, 0xf1f18c7d, 0xececd73b, 0x04040501, 0x8484a521, 0xe1e19879, 0x1e1e9b85 .long 0x535384d7, 0x0, 0x19195e47, 0x5d5d0b56, 0x7e7ee39d, 0x4f4f9fd0, 0x9c9cbb27, 0x49491a53, 0x31317c4d, 0xd8d8ee36, 0x08080a02, 0x9f9f7be4, 0x828220a2, 0x1313d4c7, 0x2323e8cb, 0x7a7ae69c .long 0xabab42e9, 0xfefe43bd, 0x2a2aa288, 0x4b4b9ad1, 0x01014041, 0x1f1fdbc4, 0xe0e0d838, 0xd6d661b7, 0x8e8e2fa1, 0xdfdf2bf4, 0xcbcb3af1, 0x3b3bf6cd, 0xe7e71dfa, 0x8585e560, 0x54544115, 0x868625a3 .long 0x838360e3, 0xbaba16ac, 0x7575295c, 0x929234a6, 0x6e6ef799, 0xd0d0e434, 0x6868721a, 0x55550154, 0xb6b619af, 0x4e4edf91, 0xc8c8fa32, 0xc0c0f030, 0xd7d721f6, 0x3232bc8e, 0xc6c675b3, 0x8f8f6fe0 .long 0x7474691d, 0xdbdb2ef5, 0x8b8b6ae1, 0xb8b8962e, 0x0a0a8a80, 0x9999fe67, 0x2b2be2c9, 0x8181e061, 0x0303c0c3, 0xa4a48d29, 0x8c8caf23, 0xaeae07a9, 0x3434390d, 0x4d4d1f52, 0x3939764f, 0xbdbdd36e .long 0x575781d6, 0x6f6fb7d8, 0xdcdceb37, 0x15155144, 0x7b7ba6dd, 0xf7f709fe, 0x3a3ab68c, 0xbcbc932f, 0x0c0c0f03, 0xffff03fc, 0xa9a9c26b, 0xc9c9ba73, 0xb5b5d96c, 0xb1b1dc6d, 0x6d6d375a, 0x45451550 .long 0x3636b98f, 0x6c6c771b, 0xbebe13ad, 0x4a4ada90, 0xeeee57b9, 0x7777a9de, 0xf2f24cbe, 0xfdfd837e, 0x44445511, 0x6767bdda, 0x71712c5d, 0x05054540, 0x7c7c631f, 0x40405010, 0x6969325b, 0x6363b8db .long 0x2828220a, 0x0707c5c2, 0xc4c4f531, 0x2222a88a, 0x969631a7, 0x3737f9ce, 0xeded977a, 0xf6f649bf, 0xb4b4992d, 0xd1d1a475, 0x434390d3, 0x48485a12, 0xe2e258ba, 0x979771e6, 0xd2d264b6, 0xc2c270b2 .long 0x2626ad8b, 0xa5a5cd68, 0x5e5ecb95, 0x2929624b, 0x30303c0c, 0x5a5ace94, 0xddddab76, 0xf9f9867f, 0x9595f164, 0xe6e65dbb, 0xc7c735f2, 0x24242d09, 0x1717d1c6, 0xb9b9d66f, 0x1b1bdec5, 0x12129486 .long 0x60607818, 0xc3c330f3, 0xf5f5897c, 0xb3b35cef, 0xe8e8d23a, 0x7373acdf, 0x3535794c, 0x8080a020, 0xe5e59d78, 0xbbbb56ed, 0x7d7d235e, 0xf8f8c63e, 0x5f5f8bd4, 0x2f2fe7c8, 0xe4e4dd39, 0x21216849 #SBOX_2: .long 0x5b8ed55b, 0x42d09242, 0xa74deaa7, 0xfb06fdfb, 0x33fccf33, 0x8765e287, 0xf4c93df4, 0xde6bb5de, 0x584e1658, 0xda6eb4da, 0x50441450, 0x0bcac10b, 0xa08828a0, 0xef17f8ef, 0xb09c2cb0, 0x14110514 .long 0xac872bac, 0x9dfb669d, 0x6af2986a, 0xd9ae77d9, 0xa8822aa8, 0xfa46bcfa, 0x10140410, 0x0fcfc00f, 0xaa02a8aa, 0x11544511, 0x4c5f134c, 0x98be2698, 0x256d4825, 0x1a9e841a, 0x181e0618, 0x66fd9b66 .long 0x72ec9e72, 0x094a4309, 0x41105141, 0xd324f7d3, 0x46d59346, 0xbf53ecbf, 0x62f89a62, 0xe9927be9, 0xccff33cc, 0x51045551, 0x2c270b2c, 0x0d4f420d, 0xb759eeb7, 0x3ff3cc3f, 0xb21caeb2, 0x89ea6389 .long 0x9374e793, 0xce7fb1ce, 0x706c1c70, 0xa60daba6, 0x27edca27, 0x20280820, 0xa348eba3, 0x56c19756, 0x02808202, 0x7fa3dc7f, 0x52c49652, 0xeb12f9eb, 0xd5a174d5, 0x3eb38d3e, 0xfcc33ffc, 0x9a3ea49a .long 0x1d5b461d, 0x1c1b071c, 0x9e3ba59e, 0xf30cfff3, 0xcf3ff0cf, 0xcdbf72cd, 0x5c4b175c, 0xea52b8ea, 0x0e8f810e, 0x653d5865, 0xf0cc3cf0, 0x647d1964, 0x9b7ee59b, 0x16918716, 0x3d734e3d, 0xa208aaa2 .long 0xa1c869a1, 0xadc76aad, 0x06858306, 0xca7ab0ca, 0xc5b570c5, 0x91f46591, 0x6bb2d96b, 0x2ea7892e, 0xe318fbe3, 0xaf47e8af, 0x3c330f3c, 0x2d674a2d, 0xc1b071c1, 0x590e5759, 0x76e99f76, 0xd4e135d4 .long 0x78661e78, 0x90b42490, 0x38360e38, 0x79265f79, 0x8def628d, 0x61385961, 0x4795d247, 0x8a2aa08a, 0x94b12594, 0x88aa2288, 0xf18c7df1, 0xecd73bec, 0x04050104, 0x84a52184, 0xe19879e1, 0x1e9b851e .long 0x5384d753, 0x0, 0x195e4719, 0x5d0b565d, 0x7ee39d7e, 0x4f9fd04f, 0x9cbb279c, 0x491a5349, 0x317c4d31, 0xd8ee36d8, 0x080a0208, 0x9f7be49f, 0x8220a282, 0x13d4c713, 0x23e8cb23, 0x7ae69c7a .long 0xab42e9ab, 0xfe43bdfe, 0x2aa2882a, 0x4b9ad14b, 0x01404101, 0x1fdbc41f, 0xe0d838e0, 0xd661b7d6, 0x8e2fa18e, 0xdf2bf4df, 0xcb3af1cb, 0x3bf6cd3b, 0xe71dfae7, 0x85e56085, 0x54411554, 0x8625a386 .long 0x8360e383, 0xba16acba, 0x75295c75, 0x9234a692, 0x6ef7996e, 0xd0e434d0, 0x68721a68, 0x55015455, 0xb619afb6, 0x4edf914e, 0xc8fa32c8, 0xc0f030c0, 0xd721f6d7, 0x32bc8e32, 0xc675b3c6, 0x8f6fe08f .long 0x74691d74, 0xdb2ef5db, 0x8b6ae18b, 0xb8962eb8, 0x0a8a800a, 0x99fe6799, 0x2be2c92b, 0x81e06181, 0x03c0c303, 0xa48d29a4, 0x8caf238c, 0xae07a9ae, 0x34390d34, 0x4d1f524d, 0x39764f39, 0xbdd36ebd .long 0x5781d657, 0x6fb7d86f, 0xdceb37dc, 0x15514415, 0x7ba6dd7b, 0xf709fef7, 0x3ab68c3a, 0xbc932fbc, 0x0c0f030c, 0xff03fcff, 0xa9c26ba9, 0xc9ba73c9, 0xb5d96cb5, 0xb1dc6db1, 0x6d375a6d, 0x45155045 .long 0x36b98f36, 0x6c771b6c, 0xbe13adbe, 0x4ada904a, 0xee57b9ee, 0x77a9de77, 0xf24cbef2, 0xfd837efd, 0x44551144, 0x67bdda67, 0x712c5d71, 0x05454005, 0x7c631f7c, 0x40501040, 0x69325b69, 0x63b8db63 .long 0x28220a28, 0x07c5c207, 0xc4f531c4, 0x22a88a22, 0x9631a796, 0x37f9ce37, 0xed977aed, 0xf649bff6, 0xb4992db4, 0xd1a475d1, 0x4390d343, 0x485a1248, 0xe258bae2, 0x9771e697, 0xd264b6d2, 0xc270b2c2 .long 0x26ad8b26, 0xa5cd68a5, 0x5ecb955e, 0x29624b29, 0x303c0c30, 0x5ace945a, 0xddab76dd, 0xf9867ff9, 0x95f16495, 0xe65dbbe6, 0xc735f2c7, 0x242d0924, 0x17d1c617, 0xb9d66fb9, 0x1bdec51b, 0x12948612 .long 0x60781860, 0xc330f3c3, 0xf5897cf5, 0xb35cefb3, 0xe8d23ae8, 0x73acdf73, 0x35794c35, 0x80a02080, 0xe59d78e5, 0xbb56edbb, 0x7d235e7d, 0xf8c63ef8, 0x5f8bd45f, 0x2fe7c82f, 0xe4dd39e4, 0x21684921 #SBOX_3: .long 0x8ed55b5b, 0xd0924242, 0x4deaa7a7, 0x06fdfbfb, 0xfccf3333, 0x65e28787, 0xc93df4f4, 0x6bb5dede, 0x4e165858, 0x6eb4dada, 0x44145050, 0xcac10b0b, 0x8828a0a0, 0x17f8efef, 0x9c2cb0b0, 0x11051414 .long 0x872bacac, 0xfb669d9d, 0xf2986a6a, 0xae77d9d9, 0x822aa8a8, 0x46bcfafa, 0x14041010, 0xcfc00f0f, 0x02a8aaaa, 0x54451111, 0x5f134c4c, 0xbe269898, 0x6d482525, 0x9e841a1a, 0x1e061818, 0xfd9b6666 .long 0xec9e7272, 0x4a430909, 0x10514141, 0x24f7d3d3, 0xd5934646, 0x53ecbfbf, 0xf89a6262, 0x927be9e9, 0xff33cccc, 0x04555151, 0x270b2c2c, 0x4f420d0d, 0x59eeb7b7, 0xf3cc3f3f, 0x1caeb2b2, 0xea638989 .long 0x74e79393, 0x7fb1cece, 0x6c1c7070, 0x0daba6a6, 0xedca2727, 0x28082020, 0x48eba3a3, 0xc1975656, 0x80820202, 0xa3dc7f7f, 0xc4965252, 0x12f9ebeb, 0xa174d5d5, 0xb38d3e3e, 0xc33ffcfc, 0x3ea49a9a .long 0x5b461d1d, 0x1b071c1c, 0x3ba59e9e, 0x0cfff3f3, 0x3ff0cfcf, 0xbf72cdcd, 0x4b175c5c, 0x52b8eaea, 0x8f810e0e, 0x3d586565, 0xcc3cf0f0, 0x7d196464, 0x7ee59b9b, 0x91871616, 0x734e3d3d, 0x08aaa2a2 .long 0xc869a1a1, 0xc76aadad, 0x85830606, 0x7ab0caca, 0xb570c5c5, 0xf4659191, 0xb2d96b6b, 0xa7892e2e, 0x18fbe3e3, 0x47e8afaf, 0x330f3c3c, 0x674a2d2d, 0xb071c1c1, 0x0e575959, 0xe99f7676, 0xe135d4d4 .long 0x661e7878, 0xb4249090, 0x360e3838, 0x265f7979, 0xef628d8d, 0x38596161, 0x95d24747, 0x2aa08a8a, 0xb1259494, 0xaa228888, 0x8c7df1f1, 0xd73becec, 0x05010404, 0xa5218484, 0x9879e1e1, 0x9b851e1e .long 0x84d75353, 0x0, 0x5e471919, 0x0b565d5d, 0xe39d7e7e, 0x9fd04f4f, 0xbb279c9c, 0x1a534949, 0x7c4d3131, 0xee36d8d8, 0x0a020808, 0x7be49f9f, 0x20a28282, 0xd4c71313, 0xe8cb2323, 0xe69c7a7a .long 0x42e9abab, 0x43bdfefe, 0xa2882a2a, 0x9ad14b4b, 0x40410101, 0xdbc41f1f, 0xd838e0e0, 0x61b7d6d6, 0x2fa18e8e, 0x2bf4dfdf, 0x3af1cbcb, 0xf6cd3b3b, 0x1dfae7e7, 0xe5608585, 0x41155454, 0x25a38686 .long 0x60e38383, 0x16acbaba, 0x295c7575, 0x34a69292, 0xf7996e6e, 0xe434d0d0, 0x721a6868, 0x01545555, 0x19afb6b6, 0xdf914e4e, 0xfa32c8c8, 0xf030c0c0, 0x21f6d7d7, 0xbc8e3232, 0x75b3c6c6, 0x6fe08f8f .long 0x691d7474, 0x2ef5dbdb, 0x6ae18b8b, 0x962eb8b8, 0x8a800a0a, 0xfe679999, 0xe2c92b2b, 0xe0618181, 0xc0c30303, 0x8d29a4a4, 0xaf238c8c, 0x07a9aeae, 0x390d3434, 0x1f524d4d, 0x764f3939, 0xd36ebdbd .long 0x81d65757, 0xb7d86f6f, 0xeb37dcdc, 0x51441515, 0xa6dd7b7b, 0x09fef7f7, 0xb68c3a3a, 0x932fbcbc, 0x0f030c0c, 0x03fcffff, 0xc26ba9a9, 0xba73c9c9, 0xd96cb5b5, 0xdc6db1b1, 0x375a6d6d, 0x15504545 .long 0xb98f3636, 0x771b6c6c, 0x13adbebe, 0xda904a4a, 0x57b9eeee, 0xa9de7777, 0x4cbef2f2, 0x837efdfd, 0x55114444, 0xbdda6767, 0x2c5d7171, 0x45400505, 0x631f7c7c, 0x50104040, 0x325b6969, 0xb8db6363 .long 0x220a2828, 0xc5c20707, 0xf531c4c4, 0xa88a2222, 0x31a79696, 0xf9ce3737, 0x977aeded, 0x49bff6f6, 0x992db4b4, 0xa475d1d1, 0x90d34343, 0x5a124848, 0x58bae2e2, 0x71e69797, 0x64b6d2d2, 0x70b2c2c2 .long 0xad8b2626, 0xcd68a5a5, 0xcb955e5e, 0x624b2929, 0x3c0c3030, 0xce945a5a, 0xab76dddd, 0x867ff9f9, 0xf1649595, 0x5dbbe6e6, 0x35f2c7c7, 0x2d092424, 0xd1c61717, 0xd66fb9b9, 0xdec51b1b, 0x94861212 .long 0x78186060, 0x30f3c3c3, 0x897cf5f5, 0x5cefb3b3, 0xd23ae8e8, 0xacdf7373, 0x794c3535, 0xa0208080, 0x9d78e5e5, 0x56edbbbb, 0x235e7d7d, 0xc63ef8f8, 0x8bd45f5f, 0xe7c82f2f, 0xdd39e4e4, 0x68492121 ##### MASKS ##### #.shuffle_mask_128: 4096(%rax) .byte 15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 #.shuffle_mask_32: 32+4096(%rax) .byte 3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12,3,2,1,0,7,6,5,4,11,10,9,8,15,14,13,12 # below are for AESNI #.aes_mask: 64+4096(%rax) .byte 0x00, 0x0d, 0x0a, 0x07, 0x04, 0x01, 0x0e, 0x0b, 0x08, 0x05, 0x02, 0x0f, 0x0c, 0x09, 0x06, 0x03 .byte 0x00, 0x0d, 0x0a, 0x07, 0x04, 0x01, 0x0e, 0x0b, 0x08, 0x05, 0x02, 0x0f, 0x0c, 0x09, 0x06, 0x03 #.aes_and_mask: 96+4096(%rax) .byte 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f .byte 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f #.aes_hi_mask_0: 128+4096(%rax) .byte 0x00, 0xa2, 0x49, 0xeb, 0x09, 0xab, 0x40, 0xe2, 0x12, 0xb0, 0x5b, 0xf9, 0x1b, 0xb9, 0x52, 0xf0 .byte 0x00, 0xa2, 0x49, 0xeb, 0x09, 0xab, 0x40, 0xe2, 0x12, 0xb0, 0x5b, 0xf9, 0x1b, 0xb9, 0x52, 0xf0 #.aes_lo_mask_0: 160+4096(%rax) .byte 0x01, 0x07, 0x72, 0x74, 0xe4, 0xe2, 0x97, 0x91, 0x57, 0x51, 0x24, 0x22, 0xb2, 0xb4, 0xc1, 0xc7 .byte 0x01, 0x07, 0x72, 0x74, 0xe4, 0xe2, 0x97, 0x91, 0x57, 0x51, 0x24, 0x22, 0xb2, 0xb4, 0xc1, 0xc7 #.all_zero_mask: 192+4096(%rax) .long 0,0,0,0,0,0,0,0 #.aes_hi_mask_1: 224+4096(%rax) .byte 0x00, 0xdc, 0xaf, 0x73, 0xdd, 0x01, 0x72, 0xae, 0xbf, 0x63, 0x10, 0xcc, 0x62, 0xbe, 0xcd, 0x11 .byte 0x00, 0xdc, 0xaf, 0x73, 0xdd, 0x01, 0x72, 0xae, 0xbf, 0x63, 0x10, 0xcc, 0x62, 0xbe, 0xcd, 0x11 #.aes_lo_mask_1: 256+4096(%rax) .byte 0x34, 0x08, 0x9d, 0xa1, 0xce, 0xf2, 0x67, 0x5b, 0x82, 0xbe, 0x2b, 0x17, 0x78, 0x44, 0xd1, 0xed .byte 0x34, 0x08, 0x9d, 0xa1, 0xce, 0xf2, 0x67, 0x5b, 0x82, 0xbe, 0x2b, 0x17, 0x78, 0x44, 0xd1, 0xed # left rotations #.r08: 288+4096(%rax) .byte 3,0,1,2,7,4,5,6,11,8,9,10,15,12,13,14,3,0,1,2,7,4,5,6,11,8,9,10,15,12,13,14 #.r16: 320+4096(%rax) .byte 2,3,0,1,6,7,4,5,10,11,8,9,14,15,12,13,2,3,0,1,6,7,4,5,10,11,8,9,14,15,12,13 #.r24: 352+4096(%rax) .byte 1,2,3,0,5,6,7,4,9,10,11,8,13,14,15,12,1,2,3,0,5,6,7,4,9,10,11,8,13,14,15,12 #endif
2301_79861745/bench_create
crypto/sm4/src/asm/crypt_sm4_modes_macro_x86_64.s
Unix Assembly
unknown
24,215
/* * 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_SM4 #include "crypt_sm4_modes_macro_x86_64.s" .file "crypt_sm4_modes_x86_64.S" .text .extern g_cpuState .hidden g_cpuState .set X0,%ymm0 .set X1,%ymm1 .set X2,%ymm2 .set X3,%ymm3 .set Y0,%ymm4 .set Y1,%ymm5 .set Y2,%ymm6 .set Y3,%ymm7 .set ADDR,%rax .set IN,%rdi .set OUT,%rsi .set LEN,%rdx .set BLOCKS,%rdx .set RK,%rcx .set IV,%r8 .set TWEAK,%r8 .set TWEAK_MASK,%r9 .set ENC,%r9d .set HI,%r12 .set LO,%r13 .set HI_TMP,%r14 .set LO_TMP,%r15 .set T0,%r10d .set T0BL,%r10b .set T1,%r11d .set T0_64,%r10 .set T1_64,%r11 .set W0,%r12d .set W1,%r13d .set W2,%r14d .set W3,%r15d .macro LOAD_DATA vmovdqu (IN),X0 vmovdqu 32(IN),X1 vmovdqu 64(IN),X2 vmovdqu 96(IN),X3 vmovdqu 128(IN),Y0 vmovdqu 128+32(IN),Y1 vmovdqu 128+64(IN),Y2 vmovdqu 128+96(IN),Y3 .endm .macro XOR_DATA vpxor (IN),X0,X0 vpxor 32(IN),X1,X1 vpxor 64(IN),X2,X2 vpxor 96(IN),X3,X3 vpxor 128(IN),Y0,Y0 vpxor 128+32(IN),Y1,Y1 vpxor 128+64(IN),Y2,Y2 vpxor 128+96(IN),Y3,Y3 .endm .macro CHECK_GFNI re tmp xorl \re, \re movl $0x100, \tmp andl g_cpuState+24(%rip), \tmp # get gfni flag orl \tmp, \re movl $0x20, \tmp andl g_cpuState+20(%rip), \tmp # check avx2 flag orl \tmp, \re cmpl $0x120, \re # code7Out[EAX] & (1<<5)) | code7Out[ECX_OUT_IDX] & (1<<8)) .endm .macro SM4_CRYPT_GFNI_BLOCK16 # load affine matric vpbroadcastq .PreAffinT(%rip),PreAffineTRegBLOCK16 vpbroadcastq .PostAffinT(%rip),PostAffineTRegBLOCK16 vmovdqa 32+4096(ADDR),TMP0 # vmovdqa 64+4096(ADDR),AES_MASK # vmovdqa 96+4096(ADDR),AES_AND_MASK vpshufb TMP0,X0,X0 vpshufb TMP0,X1,X1 vpshufb TMP0,X2,X2 vpshufb TMP0,X3,X3 vpshufb TMP0,Y0,Y0 vpshufb TMP0,Y1,Y1 vpshufb TMP0,Y2,Y2 vpshufb TMP0,Y3,Y3 # Pack SIMD Vectors MATRIX_TRANSPOSE X0 X1 X2 X3 MATRIX_TRANSPOSE Y0 Y1 Y2 Y3 # AVX2 Rounds SM4_AVX2_GFNI_2_ROUNDS # Restore SIMD Vectors MATRIX_TRANSPOSE X0 X1 X2 X3 MATRIX_TRANSPOSE Y0 Y1 Y2 Y3 # Reverse Transformation vmovdqa 4096(ADDR),TMP0 vpshufb TMP0,X0,X0 vpshufb TMP0,X1,X1 vpshufb TMP0,X2,X2 vpshufb TMP0,X3,X3 vpshufb TMP0,Y0,Y0 vpshufb TMP0,Y1,Y1 vpshufb TMP0,Y2,Y2 vpshufb TMP0,Y3,Y3 .endm .macro SM4_CRYPT_AESNI_BLOCK16 vmovdqa 32+4096(ADDR),TMP0 vmovdqa 64+4096(ADDR),AES_MASK vmovdqa 96+4096(ADDR),AES_AND_MASK vpshufb TMP0,X0,X0 vpshufb TMP0,X1,X1 vpshufb TMP0,X2,X2 vpshufb TMP0,X3,X3 vpshufb TMP0,Y0,Y0 vpshufb TMP0,Y1,Y1 vpshufb TMP0,Y2,Y2 vpshufb TMP0,Y3,Y3 # Pack SIMD Vectors MATRIX_TRANSPOSE X0 X1 X2 X3 MATRIX_TRANSPOSE Y0 Y1 Y2 Y3 # AVX2 Rounds SM4_AVX2_AES_2_ROUNDS # Restore SIMD Vectors MATRIX_TRANSPOSE X0 X1 X2 X3 MATRIX_TRANSPOSE Y0 Y1 Y2 Y3 # Reverse Transformation vmovdqa 4096(ADDR),TMP0 vpshufb TMP0,X0,X0 vpshufb TMP0,X1,X1 vpshufb TMP0,X2,X2 vpshufb TMP0,X3,X3 vpshufb TMP0,Y0,Y0 vpshufb TMP0,Y1,Y1 vpshufb TMP0,Y2,Y2 vpshufb TMP0,Y3,Y3 .endm .macro STORE_RESULTS vmovdqu X0,0(OUT) vmovdqu X1,32(OUT) vmovdqu X2,64(OUT) vmovdqu X3,96(OUT) vmovdqu Y0,128(OUT) vmovdqu Y1,128+32(OUT) vmovdqu Y2,128+64(OUT) vmovdqu Y3,128+96(OUT) .endm .macro CLEAR_CONTEXT xorl T0,T0 xorl T1,T1 xorl W0,W0 xorl W1,W1 xorl W2,W2 xorl W3,W3 .endm ##### SM4-CBC ##### # void SM4_CBC_Encrypt(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key, unsigned char *iv, const int enc) # in %rdi # out %rsi # len %rdx # rk %rcx # iv %r8 # enc %r9d .globl SM4_CBC_Encrypt .type SM4_CBC_Encrypt, @function .align 64 SM4_CBC_Encrypt: # Store Registers subq $72,%rsp movq %rbx,(%rsp) movq %rbp,8(%rsp) movq %r9,16(%rsp) movq %r10,24(%rsp) movq %r11,32(%rsp) movq %r12,40(%rsp) movq %r13,48(%rsp) movq %r14,56(%rsp) movq %r15,64(%rsp) # Get Address leaq SBOX4X_MASK(%rip),ADDR testl ENC,ENC jz .Lcbc_decrypt .Lcbc_encrypt: cmpq $16,LEN jl .Lcbc_ret # Load Data movl (IN),W0 movl 4(IN),W1 movl 8(IN),W2 movl 12(IN),W3 # XOR IV xorl (IV),W0 xorl 4(IV),W1 xorl 8(IV),W2 xorl 12(IV),W3 bswap W0 bswap W1 bswap W2 bswap W3 # Serial Rounds SM4_SERIAL_ROUNDS # Store Results bswap W0 bswap W1 bswap W2 bswap W3 movl W3,(OUT) movl W2,4(OUT) movl W1,8(OUT) movl W0,12(OUT) movl W3,(IV) movl W2,4(IV) movl W1,8(IV) movl W0,12(IV) leaq 16(IN),IN leaq 16(OUT),OUT subq $16,LEN jmp .Lcbc_encrypt .Lcbc_decrypt: cmpq $256,LEN jl .Lcbc_dec .Lcbc_dec16: LOAD_DATA CHECK_GFNI %r9d %r10d jl .Lcbc_dec_aesni .Lcbc_dec_gfni: SM4_CRYPT_GFNI_BLOCK16 jmp .Lafter_cbc_dec .Lcbc_dec_aesni: SM4_CRYPT_AESNI_BLOCK16 .Lafter_cbc_dec: vmovdqu (IV),TMP0x vmovdqu (IN),TMP1x vinserti128 $1,TMP1x,TMP0,TMP0 vmovdqu 240(IN),TMP2x vmovdqu TMP2x,(IV) vpxor TMP0,X0,X0 vpxor 16(IN),X1,X1 vpxor 32+16(IN),X2,X2 vpxor 64+16(IN),X3,X3 vpxor 96+16(IN),Y0,Y0 vpxor 128+16(IN),Y1,Y1 vpxor 160+16(IN),Y2,Y2 vpxor 192+16(IN),Y3,Y3 STORE_RESULTS leaq 256(IN),IN leaq 256(OUT),OUT subq $256,LEN cmpq $256,LEN jl .Lcbc_dec16_ret jmp .Lcbc_dec16 .Lcbc_dec16_ret: vzeroall .Lcbc_dec: cmpq $16,LEN jl .Lcbc_ret # Load Data movl (IN),W0 movl 4(IN),W1 movl 8(IN),W2 movl 12(IN),W3 bswap W0 bswap W1 bswap W2 bswap W3 # Serial Rounds SM4_SERIAL_ROUNDS # Store Result bswap W0 bswap W1 bswap W2 bswap W3 xorl (IV),W3 xorl 4(IV),W2 xorl 8(IV),W1 xorl 12(IV),W0 movq (IN),%r10 movq %r10,(IV) movq 8(IN),%r10 movq %r10,8(IV) movl W3,(OUT) movl W2,4(OUT) movl W1,8(OUT) movl W0,12(OUT) leaq 16(IN),IN leaq 16(OUT),OUT subq $16,LEN jmp .Lcbc_dec .Lcbc_ret: CLEAR_CONTEXT # Restore Registers movq (%rsp),%rbx movq 8(%rsp),%rbp movq 16(%rsp),%rax movq 24(%rsp),%r10 movq 32(%rsp),%r11 movq 40(%rsp),%r12 movq 48(%rsp),%r13 movq 56(%rsp),%r14 movq 64(%rsp),%r15 addq $72,%rsp ret .size SM4_CBC_Encrypt, .-SM4_CBC_Encrypt ##### SM4-ECB ##### # void SM4_ECB_Encrypt(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key) # in %rdi # out %rsi # len %rdx # key %rcx .globl SM4_ECB_Encrypt .type SM4_ECB_Encrypt, @function .align 64 SM4_ECB_Encrypt: # Store Registers subq $32,%rsp movq %r12,(%rsp) movq %r13,8(%rsp) movq %r14,16(%rsp) movq %r15,24(%rsp) # Get Address leaq SBOX4X_MASK(%rip),ADDR .Lecb_encrypt: cmpq $256,LEN jl .Lecb_enc .Lecb_enc16: LOAD_DATA CHECK_GFNI %r12d %r13d jl .Lecb_enc_aesni .Lecb_enc_gfni: SM4_CRYPT_GFNI_BLOCK16 jmp .Lafter_ecb_enc .Lecb_enc_aesni: SM4_CRYPT_AESNI_BLOCK16 .Lafter_ecb_enc: STORE_RESULTS leaq 256(IN),IN leaq 256(OUT),OUT subq $256,LEN cmpq $256,LEN jl .Lecb_enc16_ret jmp .Lecb_enc16 .Lecb_enc16_ret: vzeroall .Lecb_enc: cmpq $16,LEN jl .Lecb_ret # Load Data movl (IN),W0 movl 4(IN),W1 movl 8(IN),W2 movl 12(IN),W3 bswap W0 bswap W1 bswap W2 bswap W3 # Serial Rounds SM4_SERIAL_ROUNDS # Store Result bswap W0 bswap W1 bswap W2 bswap W3 movl W3,(OUT) movl W2,4(OUT) movl W1,8(OUT) movl W0,12(OUT) leaq 16(IN),IN leaq 16(OUT),OUT subq $16,LEN jmp .Lecb_enc .Lecb_ret: CLEAR_CONTEXT # Restore Registers movq (%rsp),%r12 movq 8(%rsp),%r13 movq 16(%rsp),%r14 movq 24(%rsp),%r15 addq $32,%rsp ret .size SM4_ECB_Encrypt, .-SM4_ECB_Encrypt ##### SM4-CFB ENC ##### # void SM4_CFB128_Encrypt(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key, unsigned char *iv, int *num) # in %rdi # out %rsi # len %rdx # rk %rcx # iv %r8 # num %r9d .globl SM4_CFB128_Encrypt .type SM4_CFB128_Encrypt, @function .align 64 SM4_CFB128_Encrypt: # Store Registers subq $72,%rsp movq %rbx,(%rsp) movq %rbp,8(%rsp) movq %r9,16(%rsp) movq %r10,24(%rsp) movq %r11,32(%rsp) movq %r12,40(%rsp) movq %r13,48(%rsp) movq %r14,56(%rsp) movq %r15,64(%rsp) # Load Num movl (%r9),%r9d cmpl $0,%r9d je .Lcfb128_enc_update .Lcfb128_enc_init: movb 0(IV,%r9,1),%al xorb (IN),%al movb %al,(OUT) movb %al,0(IV,%r9,1) leaq 1(IN),IN leaq 1(OUT),OUT incl %r9d decq LEN cmpl $16,%r9d je .Lcfb128_enc_update cmpq $0,LEN je .Lcfb128_enc_ret jmp .Lcfb128_enc_init .Lcfb128_enc_update: movl $0,%r9d # Get Address leaq SBOX4X_MASK(%rip),ADDR .Lcfb128_enc_loop: cmpq $0,LEN je .Lcfb128_enc_ret movl $0,%r9d # Load IV movl (IV),W0 movl 4(IV),W1 movl 8(IV),W2 movl 12(IV),W3 bswap W0 bswap W1 bswap W2 bswap W3 # Serial Rounds SM4_SERIAL_ROUNDS # Store Results bswap W0 bswap W1 bswap W2 bswap W3 movl W3,(IV) movl W2,4(IV) movl W1,8(IV) movl W0,12(IV) cmpq $16,LEN jl .Lcfb128_enc_final xorl (IN),W3 xorl 4(IN),W2 xorl 8(IN),W1 xorl 12(IN),W0 movl W3,(OUT) movl W2,4(OUT) movl W1,8(OUT) movl W0,12(OUT) movl W3,(IV) movl W2,4(IV) movl W1,8(IV) movl W0,12(IV) leaq 16(IN),IN leaq 16(OUT),OUT subq $16,LEN jmp .Lcfb128_enc_loop .Lcfb128_enc_final: movb 0(IV,%r9,1),%al xorb (IN),%al movb %al,(OUT) movb %al,0(IV,%r9,1) leaq 1(IN),IN leaq 1(OUT),OUT incl %r9d decq LEN jnz .Lcfb128_enc_final .Lcfb128_enc_ret: CLEAR_CONTEXT # Restore Registers movq (%rsp),%rbx movq 8(%rsp),%rbp movq 16(%rsp),%rax movq 24(%rsp),%r10 movq 32(%rsp),%r11 movq 40(%rsp),%r12 movq 48(%rsp),%r13 movq 56(%rsp),%r14 movq 64(%rsp),%r15 addq $72,%rsp # Store Num movl %r9d,(%rax) ret .size SM4_CFB128_Encrypt, .-SM4_CFB128_Encrypt ##### SM4-CFB DEC ##### # void SM4_CFB128_Decrypt(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key, unsigned char *iv, int *num) # in %rdi # out %rsi # len %rdx # rk %rcx # iv %r8 # num %r9d .globl SM4_CFB128_Decrypt .type SM4_CFB128_Decrypt, @function .align 64 SM4_CFB128_Decrypt: # Store Registers subq $72,%rsp movq %rbx,(%rsp) movq %rbp,8(%rsp) movq %r9,16(%rsp) movq %r10,24(%rsp) movq %r11,32(%rsp) movq %r12,40(%rsp) movq %r13,48(%rsp) movq %r14,56(%rsp) movq %r15,64(%rsp) # Load Num movl (%r9),%r9d cmpl $0,%r9d je .Lcfb128_dec_update .Lcfb128_dec_init: movb 0(IV,%r9,1),%al movb (IN),%bl xorb %bl,%al movb %al,(OUT) movb %bl,0(IV,%r9,1) leaq 1(IN),IN leaq 1(OUT),OUT incl %r9d decq LEN cmpl $16,%r9d je .Lcfb128_dec_update cmpq $0,LEN je .Lcfb128_dec_ret jmp .Lcfb128_dec_init .Lcfb128_dec_update: # Get Address leaq SBOX4X_MASK(%rip),ADDR movl $0,%r9d cmpq $256,LEN jl .Lcfb128_dec .Lcfb128_dec16: vmovdqu (IV),TMP0x vmovdqu (IN),TMP1x vinserti128 $1,TMP1x,TMP0,TMP0 vmovdqu 240(IN),TMP2x vmovdqu TMP2x,(IV) vmovdqu TMP0,X0 vmovdqu 16(IN),X1 vmovdqu 32+16(IN),X2 vmovdqu 64+16(IN),X3 vmovdqu 96+16(IN),Y0 vmovdqu 128+16(IN),Y1 vmovdqu 160+16(IN),Y2 vmovdqu 192+16(IN),Y3 CHECK_GFNI %r10d %r11d jl .Lcfb128_dec_aesni .Lcfb128_dec_gfni: SM4_CRYPT_GFNI_BLOCK16 jmp .Lafter_cfb128_dec .Lcfb128_dec_aesni: SM4_CRYPT_AESNI_BLOCK16 .Lafter_cfb128_dec: XOR_DATA STORE_RESULTS leaq 256(IN),IN leaq 256(OUT),OUT subq $256,LEN cmpq $256,LEN jl .Lcfb128_dec16_ret jmp .Lcfb128_dec16 .Lcfb128_dec16_ret: vzeroall .Lcfb128_dec: cmpq $0,LEN je .Lcfb128_dec_ret .Lcfb128_dec1: # Load IV movl (IV),W0 movl 4(IV),W1 movl 8(IV),W2 movl 12(IV),W3 bswap W0 bswap W1 bswap W2 bswap W3 # Serial Rounds SM4_SERIAL_ROUNDS # Store Results bswap W0 bswap W1 bswap W2 bswap W3 movl W3,(IV) movl W2,4(IV) movl W1,8(IV) movl W0,12(IV) cmpq $16,LEN jl .Lcfb128_dec_final movq (IN),%rbx movq %rbx,(IV) movq 8(IN),%rbx movq %rbx,8(IV) xorq %rbx,%rbx xorl (IN),W3 xorl 4(IN),W2 xorl 8(IN),W1 xorl 12(IN),W0 movl W3,(OUT) movl W2,4(OUT) movl W1,8(OUT) movl W0,12(OUT) leaq 16(IN),IN leaq 16(OUT),OUT subq $16,LEN cmpq $0,LEN je .Lcfb128_dec_ret jmp .Lcfb128_dec1 .Lcfb128_dec_final: movb 0(IV,%r9,1),%al movb (IN),%bl xorb %bl,%al movb %al,(OUT) movb %bl,0(IV,%r9,1) leaq 1(IN),IN leaq 1(OUT),OUT incl %r9d decq LEN jnz .Lcfb128_dec_final .Lcfb128_dec_ret: CLEAR_CONTEXT # Restore Registers movq (%rsp),%rbx movq 8(%rsp),%rbp movq 16(%rsp),%rax movq 24(%rsp),%r10 movq 32(%rsp),%r11 movq 40(%rsp),%r12 movq 48(%rsp),%r13 movq 56(%rsp),%r14 movq 64(%rsp),%r15 addq $72,%rsp # Store Num movl %r9d,(%rax) ret .size SM4_CFB128_Decrypt, .-SM4_CFB128_Decrypt ##### SM4-OFB ##### # void SM4_OFB_Encrypt(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key, unsigned char *iv, int *num) # in %rdi # out %rsi # len %rdx # rk %rcx # iv %r8 # num %r9d .globl SM4_OFB_Encrypt .type SM4_OFB_Encrypt, @function .align 64 SM4_OFB_Encrypt: # Store Registers subq $72,%rsp movq %rbx,(%rsp) movq %rbp,8(%rsp) movq %r9,16(%rsp) movq %r10,24(%rsp) movq %r11,32(%rsp) movq %r12,40(%rsp) movq %r13,48(%rsp) movq %r14,56(%rsp) movq %r15,64(%rsp) # Load Num movl (%r9),%r9d cmpl $0,%r9d jz .Lofb128_enc_update .Lofb128_enc_init: movb 0(IV,%r9,1),%al xorb (IN),%al movb %al,(OUT) leaq 1(IN),IN leaq 1(OUT),OUT incl %r9d decq LEN cmpl $16,%r9d je .Lofb128_enc_update cmpq $0,LEN je .Lofb128_enc_ret jmp .Lofb128_enc_init .Lofb128_enc_update: movl $0,%r9d # Get Address leaq SBOX4X_MASK(%rip),ADDR .Lofb128_enc_loop: cmpq $0,LEN je .Lofb128_enc_ret # Load IV movl (IV),W0 movl 4(IV),W1 movl 8(IV),W2 movl 12(IV),W3 bswap W0 bswap W1 bswap W2 bswap W3 # Serial Rounds SM4_SERIAL_ROUNDS # Store Results bswap W0 bswap W1 bswap W2 bswap W3 movl W3,(IV) movl W2,4(IV) movl W1,8(IV) movl W0,12(IV) cmpq $16,LEN jl .Lofb128_enc_final xorl (IN),W3 xorl 4(IN),W2 xorl 8(IN),W1 xorl 12(IN),W0 movl W3,(OUT) movl W2,4(OUT) movl W1,8(OUT) movl W0,12(OUT) leaq 16(IN),IN leaq 16(OUT),OUT subq $16,LEN jmp .Lofb128_enc_loop .Lofb128_enc_final: movb 0(IV,%r9,1),%al xorb (IN),%al movb %al,(OUT) leaq 1(IN),IN leaq 1(OUT),OUT incl %r9d decq LEN jnz .Lofb128_enc_final .Lofb128_enc_ret: CLEAR_CONTEXT # Restore Registers movq (%rsp),%rbx movq 8(%rsp),%rbp movq 16(%rsp),%rax movq 24(%rsp),%r10 movq 32(%rsp),%r11 movq 40(%rsp),%r12 movq 48(%rsp),%r13 movq 56(%rsp),%r14 movq 64(%rsp),%r15 addq $72,%rsp # Store Num movl %r9d,(%rax) ret .size SM4_OFB_Encrypt, .-SM4_OFB_Encrypt ##### SM4-CTR32 ##### # NOTE: the IV/counter CTR mode is big-endian. .align 64 .Lmovbe12: .byte 0,1,2,3,4,5,6,7,8,9,10,11,15,14,13,12,0,1,2,3,4,5,6,7,8,9,10,11,15,14,13,12 .Lone: .long 0,0,0,1 .macro INCREMENT_COUNTER movbe 12(IV),%ebx incl %ebx movbe %ebx,12(IV) .endm .macro LOAD_ECOUNT_BUF SINK vpaddd TMP1x,TMP2x,TMP3x vpaddd TMP1x,TMP3x,TMP4x vinserti128 $1,TMP3x,TMP2,TMP2 vpshufb TMP0,TMP2,TMP2 vmovdqa TMP2,\SINK vmovdqa TMP4x,TMP2x .endm .macro LOAD_ECOUNT_BUF_ALL vmovdqa .Lmovbe12(%rip),TMP0 vmovdqa .Lone(%rip),TMP1x vmovdqu (IV),TMP2x vpshufb TMP0x,TMP2x,TMP2x LOAD_ECOUNT_BUF X0 LOAD_ECOUNT_BUF X1 LOAD_ECOUNT_BUF X2 LOAD_ECOUNT_BUF X3 LOAD_ECOUNT_BUF Y0 LOAD_ECOUNT_BUF Y1 LOAD_ECOUNT_BUF Y2 LOAD_ECOUNT_BUF Y3 vpshufb TMP0x,TMP2x,TMP2x vmovdqu TMP2x,(IV) .endm # void SM4_CTR_EncryptBlocks(const unsigned char *in, unsigned char *out, size_t blocks, const SM4_KEY *key, const unsigned char *iv) # in %rdi # out %rsi # blocks %rdx # rk %rcx # iv %r8 .globl SM4_CTR_EncryptBlocks .type SM4_CTR_EncryptBlocks, @function .align 64 SM4_CTR_EncryptBlocks: # Get Address leaq SBOX4X_MASK(%rip),ADDR # Store Registers subq $88,%rsp movq %rbx,(%rsp) movq %rbp,8(%rsp) movq %r8,16(%rsp) movq %r9,24(%rsp) movq %r10,32(%rsp) movq %r11,40(%rsp) movq %r12,48(%rsp) movq %r13,56(%rsp) movq %r14,64(%rsp) movq %r15,72(%rsp) movq %rdx,80(%rsp) cmpq $16,BLOCKS jl .Lctr32_enc .Lctr32_enc16: LOAD_ECOUNT_BUF_ALL CHECK_GFNI %r9d %r10d jl .Lctr32_enc_aesni .Lctr32_enc_gfni: SM4_CRYPT_GFNI_BLOCK16 jmp .Lafter_ctr32_enc .Lctr32_enc_aesni: SM4_CRYPT_AESNI_BLOCK16 .Lafter_ctr32_enc: XOR_DATA STORE_RESULTS leaq 256(IN),IN leaq 256(OUT),OUT subq $16,BLOCKS cmpq $16,BLOCKS jl .Lctr32_enc16_ret jmp .Lctr32_enc16 .Lctr32_enc16_ret: vzeroall .Lctr32_enc: cmpq $0,BLOCKS je .Lctr32_ret # Load IV movl (IV),W0 movl 4(IV),W1 movl 8(IV),W2 movl 12(IV),W3 bswap W0 bswap W1 bswap W2 bswap W3 # Serial Rounds SM4_SERIAL_ROUNDS # Store Results bswap W0 bswap W1 bswap W2 bswap W3 xorl (IN),W3 xorl 4(IN),W2 xorl 8(IN),W1 xorl 12(IN),W0 movl W3,(OUT) movl W2,4(OUT) movl W1,8(OUT) movl W0,12(OUT) leaq 16(IN),IN leaq 16(OUT),OUT decq BLOCKS INCREMENT_COUNTER jmp .Lctr32_enc .Lctr32_ret: CLEAR_CONTEXT # Restore Registers movq (%rsp),%rbx movq 8(%rsp),%rbp movq 16(%rsp),%r8 movq 24(%rsp),%r9 movq 32(%rsp),%r10 movq 40(%rsp),%r11 movq 48(%rsp),%r12 movq 56(%rsp),%r13 movq 64(%rsp),%r14 movq 72(%rsp),%r15 movq 80(%rsp),%rdx addq $88,%rsp ret .size SM4_CTR_EncryptBlocks, .-SM4_CTR_EncryptBlocks ##### SM4-XTS ##### .align 16 .Lxts_tweak_mask: .long 0,0xe1000000 .macro GALOIS_FIELD_MUL Idx xorq LO_TMP,LO_TMP testq $1,LO cmovnzq TWEAK_MASK,LO_TMP shrd $1,HI,LO shrq $1,HI xorq LO_TMP,HI movbe HI,\Idx(TWEAK) movbe LO,\Idx+8(TWEAK) .endm .macro GALOIS_FIELD_MUL_16_INNER GALOIS_FIELD_MUL 16 # T2:T1->T2 GALOIS_FIELD_MUL 32 # T3:T2->T3 GALOIS_FIELD_MUL 48 # T4:T3->T4 GALOIS_FIELD_MUL 64 # T5:T4->T5 GALOIS_FIELD_MUL 80 # T6:T5->T6 GALOIS_FIELD_MUL 96 # T7:T6->T7 GALOIS_FIELD_MUL 112 # T8:T7->T8 GALOIS_FIELD_MUL 128 # T9:T8->T9 GALOIS_FIELD_MUL 144 # T10:T9->T10 GALOIS_FIELD_MUL 160 # T11:T10->T11 GALOIS_FIELD_MUL 176 # T12:T11->T12 GALOIS_FIELD_MUL 192 # T13:T12->T13 GALOIS_FIELD_MUL 208 # T14:T13->T14 GALOIS_FIELD_MUL 224 # T15:T14->T15 GALOIS_FIELD_MUL 240 .endm .macro XOR_TWEAK vpxor (TWEAK),X0,X0 vpxor 32(TWEAK),X1,X1 vpxor 64(TWEAK),X2,X2 vpxor 96(TWEAK),X3,X3 vpxor 128(TWEAK),Y0,Y0 vpxor 128+32(TWEAK),Y1,Y1 vpxor 128+64(TWEAK),Y2,Y2 vpxor 128+96(TWEAK),Y3,Y3 .endm .macro SM4_XTS_16_EN_INNER LOAD_DATA XOR_TWEAK CHECK_GFNI %r15d %r14d jl .Lxts_enc_aesni .Lxts_enc_gfni: SM4_CRYPT_GFNI_BLOCK16 jmp .Lafter_xts_enc .Lxts_enc_aesni: SM4_CRYPT_AESNI_BLOCK16 .Lafter_xts_enc: XOR_TWEAK STORE_RESULTS .endm # void SM4_XTS_Encrypt_Blocks(const unsigned char *in, unsigned char *out, size_t len, const SM4_KEY *key, unsigned char *t) # in %rdi # out %rsi # len %rdx # key %rcx # t %r8 .globl SM4_XTS_Encrypt_Blocks .type SM4_XTS_Encrypt_Blocks, @function .align 64 SM4_XTS_Encrypt_Blocks: cmpq $256,LEN jl .Lxts_ret # Store Registers subq $56,%rsp movq %r9,(%rsp) movq %r10,8(%rsp) movq %r11,16(%rsp) movq %r12,24(%rsp) movq %r13,32(%rsp) movq %r14,40(%rsp) movq %r15,48(%rsp) # Get Address leaq SBOX4X_MASK(%rip),ADDR # Load tweak mask movq .Lxts_tweak_mask(%rip),TWEAK_MASK # T0: Initial movbe (TWEAK),HI movbe 8(TWEAK),LO .Lxts_update: GALOIS_FIELD_MUL_16_INNER SM4_XTS_16_EN_INNER leaq 256(IN),IN leaq 256(OUT),OUT subq $256,LEN cmpq $256,LEN jl .Lxts_final # T15: Initial movbe 240(TWEAK),HI movbe 248(TWEAK),LO # T0:T15->T0 GALOIS_FIELD_MUL 0 jmp .Lxts_update .Lxts_final: # Clear Context vzeroall # Restore Registers movq (%rsp),%r9 movq 8(%rsp),%r10 movq 16(%rsp),%r11 movq 24(%rsp),%r12 movq 32(%rsp),%r13 movq 40(%rsp),%r14 movq 48(%rsp),%r15 addq $56,%rsp .Lxts_ret: ret .size SM4_XTS_Encrypt_Blocks, .-SM4_XTS_Encrypt_Blocks #endif
2301_79861745/bench_create
crypto/sm4/src/asm/crypt_sm4_modes_x86_64.S
Unix Assembly
unknown
19,839
/* * 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_SM4 #include "crypt_sm4_modes_macro_x86_64.s" .file "crypt_sm4_x86_64.S" .text .set W0, %r8d .set W1, %r9d .set W2, %r10d .set W3, %r11d .set T0, %eax .set T0BL, %al .set T1, %ecx .set T0_64, %rax .set T1_64, %rcx .set RK, %rdx .set ADDR, %rdi .set RoundKey, %rsi .set CK, %rdx .macro SETKEY_ROUND RKey A0 A1 A2 A3 CK_ADDR No_CK No_RK # x4 = x1 ^ x2 ^ x3 ^ *(CK + i); # x4 = S32(x4); # x4 = x0 ^ L_Key32(x4); # *(rk + i) = x4; # x4 = x1 ^ x2 ^ x3 ^ *(CK + i); movl \No_CK(\CK_ADDR), T0 xorl \A1, T0 xorl \A2, T0 xorl \A3, T0 # x0 = x0 ^ (SBOX_KEY_0[x4 & 0xff]) ^ (SBOX_KEY_1[(x4 >> 8) & 0xff]) ^ (SBOX_KEY_2[(x4 >> 16) & 0xff]) ^ (SBOX_KEY_3[(x4 >> 24) & 0xff]); movzbl T0BL, T1 xorl (ADDR,T1_64,4), \A0 shrl $8, T0 movzbl T0BL, T1 xorl 1024(ADDR,T1_64,4), \A0 shrl $8, T0 movzbl T0BL, T1 xorl 2048(ADDR,T1_64,4), \A0 shrl $8, T0 xorl 3072(ADDR,T0_64,4), \A0 # enc round *(rk + i) = x4; # dec round *(rk + 31 - i) = x4; movl \A0, \No_RK(\RKey) .endm ##### SM4 Encryption ##### # void SM4_Encrypt(const unsigned char *in, unsigned char *out, const SM4_KEY *key) # %rdi in plain ptr 16 bytes # %rsi out cipher ptr 16 bytes # %rdx key round key ptr 128 bytes .globl SM4_Encrypt .type SM4_Encrypt, @function .align 64 SM4_Encrypt: ##### Load Data ##### movl (%rdi), W0 movl 4(%rdi), W1 movl 8(%rdi), W2 movl 12(%rdi), W3 bswap W0 bswap W1 bswap W2 bswap W3 ##### Load SBOX ADDRESS ##### leaq SBOX4X_MASK(%rip), ADDR ##### Serial Rounds ##### SM4_SERIAL_ROUNDS ##### Store Results ##### bswap W0 bswap W1 bswap W2 bswap W3 movl W3, (%rsi) movl W2, 4(%rsi) movl W1, 8(%rsi) movl W0, 12(%rsi) ##### Clear Context ##### xorl W0, W0 xorl W1, W1 xorl W2, W2 xorl W3, W3 xorl T0, T0 xorl T1, T1 ret .size SM4_Encrypt, .-SM4_Encrypt ##### SM4 SET ROUND KEY ##### # void SM4_SetEncKey(const unsigned char *userKey, SM4_KEY *key) # %rdi userKey: user key ptr 16 bytes # %rsi key: round key ptr 128 bytes .globl SM4_SetEncKey .type SM4_SetEncKey, @function .align 64 SM4_SetEncKey: ##### Load MK ##### movl (%rdi), W0 movl 4(%rdi), W1 movl 8(%rdi), W2 movl 12(%rdi), W3 bswap W0 bswap W1 bswap W2 bswap W3 ##### Load SBOX ADDRESS ##### leaq SBOX_KEY_X4(%rip), ADDR leaq CK_NUM(%rip), CK xorq T0_64, T0_64 xorq T1_64, T1_64 ##### XOR FK ##### xorl $0xa3b1bac6, W0 xorl $0x56aa3350, W1 xorl $0x677d9197, W2 xorl $0xb27022dc, W3 ##### ROUNDS ##### SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 0 0 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 4 4 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 8 8 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 12 12 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 16 16 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 20 20 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 24 24 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 28 28 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 32 32 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 36 36 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 40 40 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 44 44 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 48 48 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 52 52 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 56 56 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 60 60 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 64 64 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 68 68 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 72 72 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 76 76 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 80 80 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 84 84 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 88 88 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 92 92 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 96 96 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 100 100 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 104 104 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 108 108 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 112 112 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 116 116 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 120 120 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 124 124 ##### Clear Context ##### xorl W0, W0 xorl W1, W1 xorl W2, W2 xorl W3, W3 xorl T0, T0 xorl T1, T1 ret .size SM4_SetEncKey, .-SM4_SetEncKey # void SM4_SetDecKey(const unsigned char *userKey, SM4_KEY *key) # %rdi userKey: user key ptr 16 bytes # %rsi key: round key ptr 128 bytes .globl SM4_SetDecKey .type SM4_SetDecKey, @function .align 64 SM4_SetDecKey: ##### Load MK ##### movl (%rdi), W0 movl 4(%rdi), W1 movl 8(%rdi), W2 movl 12(%rdi), W3 bswap W0 bswap W1 bswap W2 bswap W3 ##### Load SBOX ADDRESS ##### leaq SBOX_KEY_X4(%rip), ADDR leaq CK_NUM(%rip), CK xorq T0_64, T0_64 xorq T1_64, T1_64 ##### XOR FK ##### xorl $0xa3b1bac6, W0 xorl $0x56aa3350, W1 xorl $0x677d9197, W2 xorl $0xb27022dc, W3 ##### ROUNDS ##### SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 0 124 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 4 120 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 8 116 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 12 112 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 16 108 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 20 104 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 24 100 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 28 96 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 32 92 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 36 88 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 40 84 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 44 80 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 48 76 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 52 72 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 56 68 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 60 64 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 64 60 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 68 56 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 72 52 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 76 48 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 80 44 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 84 40 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 88 36 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 92 32 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 96 28 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 100 24 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 104 20 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 108 16 SETKEY_ROUND RoundKey W0 W1 W2 W3 CK 112 12 SETKEY_ROUND RoundKey W1 W2 W3 W0 CK 116 8 SETKEY_ROUND RoundKey W2 W3 W0 W1 CK 120 4 SETKEY_ROUND RoundKey W3 W0 W1 W2 CK 124 0 ##### Clear Context ##### xorl W0, W0 xorl W1, W1 xorl W2, W2 xorl W3, W3 xorl T0, T0 xorl T1, T1 ret .size SM4_SetDecKey, .-SM4_SetDecKey ##### SBOX Extend Tables (1 Table, 256*4 bytes) for round key: SBOX_KEY_0, SBOX_KEY_1, SBOX_KEY_2, SBOX_KEY_3 ##### .section .rodata .align 64 CK_NUM: .long 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9 .long 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279 SBOX_KEY_X4: #SBOX_KEY_0: .long 0x6b1ac0d6, 0x48120090, 0x749d20e9, 0x7f1fc0fe, 0x661980cc, 0x709c20e1, 0x1e87a03d, 0x5b96e0b7, 0x0b02c016, 0x5b16c0b6, 0x0a028014, 0x611840c2, 0x14050028, 0x7d9f60fb, 0x1605802c, 0x0280a005 .long 0x1585602b, 0x338ce067, 0x4d13409a, 0x3b0ec076, 0x1505402a, 0x5f17c0be, 0x02008004, 0x619860c3, 0x551540aa, 0x22088044, 0x09826013, 0x1304c026, 0x24892049, 0x4310c086, 0x0300c006, 0x4c932099 .long 0x4e13809c, 0x21084042, 0x280a0050, 0x7a1e80f4, 0x48922091, 0x779de0ef, 0x4c130098, 0x3d0f407a, 0x19866033, 0x2a0a8054, 0x0581600b, 0x21886043, 0x769da0ed, 0x6799e0cf, 0x561580ac, 0x310c4062 .long 0x721c80e4, 0x599660b3, 0x0e03801c, 0x549520a9, 0x649920c9, 0x04010008, 0x741d00e8, 0x4a92a095, 0x40100080, 0x6f9be0df, 0x4a128094, 0x7d1f40fa, 0x3a8ea075, 0x4791e08f, 0x1f87e03f, 0x5314c0a6 .long 0x2388e047, 0x0380e007, 0x5394e0a7, 0x7e1f80fc, 0x799e60f3, 0x398e6073, 0x0b82e017, 0x5d1740ba, 0x41906083, 0x2c8b2059, 0x1e07803c, 0x0c832019, 0x731cc0e6, 0x4290a085, 0x2789e04f, 0x541500a8 .long 0x340d0068, 0x358d606b, 0x40902081, 0x591640b2, 0x388e2071, 0x320c8064, 0x6d1b40da, 0x4591608b, 0x7c1f00f8, 0x759d60eb, 0x0781e00f, 0x2589604b, 0x380e0070, 0x2b0ac056, 0x4e93a09d, 0x1a86a035 .long 0x0f03c01e, 0x12048024, 0x0701c00e, 0x2f0bc05e, 0x318c6063, 0x2c0b0058, 0x689a20d1, 0x511440a2, 0x1284a025, 0x11044022, 0x3e0f807c, 0x1d87603b, 0x00802001, 0x10842021, 0x3c0f0078, 0x4390e087 .long 0x6a1a80d4, 0x0, 0x2308c046, 0x2b8ae057, 0x4f93e09f, 0x699a60d3, 0x1384e027, 0x290a4052, 0x2609804c, 0x1b06c036, 0x01004002, 0x739ce0e7, 0x501400a0, 0x621880c4, 0x641900c8, 0x4f13c09e .long 0x751d40ea, 0x5f97e0bf, 0x4511408a, 0x691a40d2, 0x20080040, 0x6398e0c7, 0x1c070038, 0x5a96a0b5, 0x519460a3, 0x7b9ee0f7, 0x791e40f2, 0x6719c0ce, 0x7c9f20f9, 0x308c2061, 0x0a82a015, 0x509420a1 .long 0x701c00e0, 0x5715c0ae, 0x2e8ba05d, 0x521480a4, 0x4d93609b, 0x1a068034, 0x0d03401a, 0x2a8aa055, 0x5695a0ad, 0x49926093, 0x19064032, 0x18060030, 0x7a9ea0f5, 0x4611808c, 0x589620b1, 0x719c60e3 .long 0x0e83a01d, 0x7b1ec0f6, 0x711c40e2, 0x1705c02e, 0x41104082, 0x330cc066, 0x651940ca, 0x300c0060, 0x601800c0, 0x14852029, 0x11846023, 0x559560ab, 0x0681a00d, 0x298a6053, 0x2709c04e, 0x378de06f .long 0x6a9aa0d5, 0x6d9b60db, 0x1b86e037, 0x2288a045, 0x6f1bc0de, 0x7e9fa0fd, 0x4711c08e, 0x1785e02f, 0x01806003, 0x7f9fe0ff, 0x350d406a, 0x390e4072, 0x368da06d, 0x360d806c, 0x2d8b605b, 0x288a2051 .long 0x4691a08d, 0x0d83601b, 0x5795e0af, 0x49124092, 0x5d9760bb, 0x6e9ba0dd, 0x5e1780bc, 0x3f8fe07f, 0x08822011, 0x6c9b20d9, 0x2e0b805c, 0x20882041, 0x0f83e01f, 0x08020010, 0x2d0b405a, 0x6c1b00d8 .long 0x0501400a, 0x609820c1, 0x18862031, 0x44110088, 0x5294a0a5, 0x6699a0cd, 0x3d8f607b, 0x5e97a0bd, 0x1685a02d, 0x3a0e8074, 0x681a00d0, 0x09024012, 0x5c1700b8, 0x729ca0e5, 0x5a1680b4, 0x581600b0 .long 0x44912089, 0x348d2069, 0x4b92e097, 0x2509404a, 0x0601800c, 0x4b12c096, 0x3b8ee077, 0x3f0fc07e, 0x328ca065, 0x5c9720b9, 0x789e20f1, 0x04812009, 0x6298a0c5, 0x370dc06e, 0x6318c0c6, 0x42108084 .long 0x0c030018, 0x781e00f0, 0x3e8fa07d, 0x761d80ec, 0x1d07403a, 0x6e1b80dc, 0x2689a04d, 0x10040020, 0x3c8f2079, 0x771dc0ee, 0x2f8be05f, 0x1f07c03e, 0x6b9ae0d7, 0x659960cb, 0x1c872039, 0x24090048 #SBOX_KEY_1: .long 0x1ac0d66b, 0x12009048, 0x9d20e974, 0x1fc0fe7f, 0x1980cc66, 0x9c20e170, 0x87a03d1e, 0x96e0b75b, 0x02c0160b, 0x16c0b65b, 0x0280140a, 0x1840c261, 0x05002814, 0x9f60fb7d, 0x05802c16, 0x80a00502 .long 0x85602b15, 0x8ce06733, 0x13409a4d, 0x0ec0763b, 0x05402a15, 0x17c0be5f, 0x00800402, 0x9860c361, 0x1540aa55, 0x08804422, 0x82601309, 0x04c02613, 0x89204924, 0x10c08643, 0x00c00603, 0x9320994c .long 0x13809c4e, 0x08404221, 0x0a005028, 0x1e80f47a, 0x92209148, 0x9de0ef77, 0x1300984c, 0x0f407a3d, 0x86603319, 0x0a80542a, 0x81600b05, 0x88604321, 0x9da0ed76, 0x99e0cf67, 0x1580ac56, 0x0c406231 .long 0x1c80e472, 0x9660b359, 0x03801c0e, 0x9520a954, 0x9920c964, 0x01000804, 0x1d00e874, 0x92a0954a, 0x10008040, 0x9be0df6f, 0x1280944a, 0x1f40fa7d, 0x8ea0753a, 0x91e08f47, 0x87e03f1f, 0x14c0a653 .long 0x88e04723, 0x80e00703, 0x94e0a753, 0x1f80fc7e, 0x9e60f379, 0x8e607339, 0x82e0170b, 0x1740ba5d, 0x90608341, 0x8b20592c, 0x07803c1e, 0x8320190c, 0x1cc0e673, 0x90a08542, 0x89e04f27, 0x1500a854 .long 0x0d006834, 0x8d606b35, 0x90208140, 0x1640b259, 0x8e207138, 0x0c806432, 0x1b40da6d, 0x91608b45, 0x1f00f87c, 0x9d60eb75, 0x81e00f07, 0x89604b25, 0x0e007038, 0x0ac0562b, 0x93a09d4e, 0x86a0351a .long 0x03c01e0f, 0x04802412, 0x01c00e07, 0x0bc05e2f, 0x8c606331, 0x0b00582c, 0x9a20d168, 0x1440a251, 0x84a02512, 0x04402211, 0x0f807c3e, 0x87603b1d, 0x80200100, 0x84202110, 0x0f00783c, 0x90e08743 .long 0x1a80d46a, 0x0, 0x08c04623, 0x8ae0572b, 0x93e09f4f, 0x9a60d369, 0x84e02713, 0x0a405229, 0x09804c26, 0x06c0361b, 0x00400201, 0x9ce0e773, 0x1400a050, 0x1880c462, 0x1900c864, 0x13c09e4f .long 0x1d40ea75, 0x97e0bf5f, 0x11408a45, 0x1a40d269, 0x08004020, 0x98e0c763, 0x0700381c, 0x96a0b55a, 0x9460a351, 0x9ee0f77b, 0x1e40f279, 0x19c0ce67, 0x9f20f97c, 0x8c206130, 0x82a0150a, 0x9420a150 .long 0x1c00e070, 0x15c0ae57, 0x8ba05d2e, 0x1480a452, 0x93609b4d, 0x0680341a, 0x03401a0d, 0x8aa0552a, 0x95a0ad56, 0x92609349, 0x06403219, 0x06003018, 0x9ea0f57a, 0x11808c46, 0x9620b158, 0x9c60e371 .long 0x83a01d0e, 0x1ec0f67b, 0x1c40e271, 0x05c02e17, 0x10408241, 0x0cc06633, 0x1940ca65, 0x0c006030, 0x1800c060, 0x85202914, 0x84602311, 0x9560ab55, 0x81a00d06, 0x8a605329, 0x09c04e27, 0x8de06f37 .long 0x9aa0d56a, 0x9b60db6d, 0x86e0371b, 0x88a04522, 0x1bc0de6f, 0x9fa0fd7e, 0x11c08e47, 0x85e02f17, 0x80600301, 0x9fe0ff7f, 0x0d406a35, 0x0e407239, 0x8da06d36, 0x0d806c36, 0x8b605b2d, 0x8a205128 .long 0x91a08d46, 0x83601b0d, 0x95e0af57, 0x12409249, 0x9760bb5d, 0x9ba0dd6e, 0x1780bc5e, 0x8fe07f3f, 0x82201108, 0x9b20d96c, 0x0b805c2e, 0x88204120, 0x83e01f0f, 0x02001008, 0x0b405a2d, 0x1b00d86c .long 0x01400a05, 0x9820c160, 0x86203118, 0x11008844, 0x94a0a552, 0x99a0cd66, 0x8f607b3d, 0x97a0bd5e, 0x85a02d16, 0x0e80743a, 0x1a00d068, 0x02401209, 0x1700b85c, 0x9ca0e572, 0x1680b45a, 0x1600b058 .long 0x91208944, 0x8d206934, 0x92e0974b, 0x09404a25, 0x01800c06, 0x12c0964b, 0x8ee0773b, 0x0fc07e3f, 0x8ca06532, 0x9720b95c, 0x9e20f178, 0x81200904, 0x98a0c562, 0x0dc06e37, 0x18c0c663, 0x10808442 .long 0x0300180c, 0x1e00f078, 0x8fa07d3e, 0x1d80ec76, 0x07403a1d, 0x1b80dc6e, 0x89a04d26, 0x04002010, 0x8f20793c, 0x1dc0ee77, 0x8be05f2f, 0x07c03e1f, 0x9ae0d76b, 0x9960cb65, 0x8720391c, 0x09004824 #SBOX_KEY_2: .long 0xc0d66b1a, 0x00904812, 0x20e9749d, 0xc0fe7f1f, 0x80cc6619, 0x20e1709c, 0xa03d1e87, 0xe0b75b96, 0xc0160b02, 0xc0b65b16, 0x80140a02, 0x40c26118, 0x00281405, 0x60fb7d9f, 0x802c1605, 0xa0050280 .long 0x602b1585, 0xe067338c, 0x409a4d13, 0xc0763b0e, 0x402a1505, 0xc0be5f17, 0x80040200, 0x60c36198, 0x40aa5515, 0x80442208, 0x60130982, 0xc0261304, 0x20492489, 0xc0864310, 0xc0060300, 0x20994c93 .long 0x809c4e13, 0x40422108, 0x0050280a, 0x80f47a1e, 0x20914892, 0xe0ef779d, 0x00984c13, 0x407a3d0f, 0x60331986, 0x80542a0a, 0x600b0581, 0x60432188, 0xa0ed769d, 0xe0cf6799, 0x80ac5615, 0x4062310c .long 0x80e4721c, 0x60b35996, 0x801c0e03, 0x20a95495, 0x20c96499, 0x00080401, 0x00e8741d, 0xa0954a92, 0x00804010, 0xe0df6f9b, 0x80944a12, 0x40fa7d1f, 0xa0753a8e, 0xe08f4791, 0xe03f1f87, 0xc0a65314 .long 0xe0472388, 0xe0070380, 0xe0a75394, 0x80fc7e1f, 0x60f3799e, 0x6073398e, 0xe0170b82, 0x40ba5d17, 0x60834190, 0x20592c8b, 0x803c1e07, 0x20190c83, 0xc0e6731c, 0xa0854290, 0xe04f2789, 0x00a85415 .long 0x0068340d, 0x606b358d, 0x20814090, 0x40b25916, 0x2071388e, 0x8064320c, 0x40da6d1b, 0x608b4591, 0x00f87c1f, 0x60eb759d, 0xe00f0781, 0x604b2589, 0x0070380e, 0xc0562b0a, 0xa09d4e93, 0xa0351a86 .long 0xc01e0f03, 0x80241204, 0xc00e0701, 0xc05e2f0b, 0x6063318c, 0x00582c0b, 0x20d1689a, 0x40a25114, 0xa0251284, 0x40221104, 0x807c3e0f, 0x603b1d87, 0x20010080, 0x20211084, 0x00783c0f, 0xe0874390 .long 0x80d46a1a, 0x0, 0xc0462308, 0xe0572b8a, 0xe09f4f93, 0x60d3699a, 0xe0271384, 0x4052290a, 0x804c2609, 0xc0361b06, 0x40020100, 0xe0e7739c, 0x00a05014, 0x80c46218, 0x00c86419, 0xc09e4f13 .long 0x40ea751d, 0xe0bf5f97, 0x408a4511, 0x40d2691a, 0x00402008, 0xe0c76398, 0x00381c07, 0xa0b55a96, 0x60a35194, 0xe0f77b9e, 0x40f2791e, 0xc0ce6719, 0x20f97c9f, 0x2061308c, 0xa0150a82, 0x20a15094 .long 0x00e0701c, 0xc0ae5715, 0xa05d2e8b, 0x80a45214, 0x609b4d93, 0x80341a06, 0x401a0d03, 0xa0552a8a, 0xa0ad5695, 0x60934992, 0x40321906, 0x00301806, 0xa0f57a9e, 0x808c4611, 0x20b15896, 0x60e3719c .long 0xa01d0e83, 0xc0f67b1e, 0x40e2711c, 0xc02e1705, 0x40824110, 0xc066330c, 0x40ca6519, 0x0060300c, 0x00c06018, 0x20291485, 0x60231184, 0x60ab5595, 0xa00d0681, 0x6053298a, 0xc04e2709, 0xe06f378d .long 0xa0d56a9a, 0x60db6d9b, 0xe0371b86, 0xa0452288, 0xc0de6f1b, 0xa0fd7e9f, 0xc08e4711, 0xe02f1785, 0x60030180, 0xe0ff7f9f, 0x406a350d, 0x4072390e, 0xa06d368d, 0x806c360d, 0x605b2d8b, 0x2051288a .long 0xa08d4691, 0x601b0d83, 0xe0af5795, 0x40924912, 0x60bb5d97, 0xa0dd6e9b, 0x80bc5e17, 0xe07f3f8f, 0x20110882, 0x20d96c9b, 0x805c2e0b, 0x20412088, 0xe01f0f83, 0x00100802, 0x405a2d0b, 0x00d86c1b .long 0x400a0501, 0x20c16098, 0x20311886, 0x00884411, 0xa0a55294, 0xa0cd6699, 0x607b3d8f, 0xa0bd5e97, 0xa02d1685, 0x80743a0e, 0x00d0681a, 0x40120902, 0x00b85c17, 0xa0e5729c, 0x80b45a16, 0x00b05816 .long 0x20894491, 0x2069348d, 0xe0974b92, 0x404a2509, 0x800c0601, 0xc0964b12, 0xe0773b8e, 0xc07e3f0f, 0xa065328c, 0x20b95c97, 0x20f1789e, 0x20090481, 0xa0c56298, 0xc06e370d, 0xc0c66318, 0x80844210 .long 0x00180c03, 0x00f0781e, 0xa07d3e8f, 0x80ec761d, 0x403a1d07, 0x80dc6e1b, 0xa04d2689, 0x00201004, 0x20793c8f, 0xc0ee771d, 0xe05f2f8b, 0xc03e1f07, 0xe0d76b9a, 0x60cb6599, 0x20391c87, 0x00482409 #SBOX_KEY_3: .long 0xd66b1ac0, 0x90481200, 0xe9749d20, 0xfe7f1fc0, 0xcc661980, 0xe1709c20, 0x3d1e87a0, 0xb75b96e0, 0x160b02c0, 0xb65b16c0, 0x140a0280, 0xc2611840, 0x28140500, 0xfb7d9f60, 0x2c160580, 0x050280a0 .long 0x2b158560, 0x67338ce0, 0x9a4d1340, 0x763b0ec0, 0x2a150540, 0xbe5f17c0, 0x04020080, 0xc3619860, 0xaa551540, 0x44220880, 0x13098260, 0x261304c0, 0x49248920, 0x864310c0, 0x060300c0, 0x994c9320 .long 0x9c4e1380, 0x42210840, 0x50280a00, 0xf47a1e80, 0x91489220, 0xef779de0, 0x984c1300, 0x7a3d0f40, 0x33198660, 0x542a0a80, 0x0b058160, 0x43218860, 0xed769da0, 0xcf6799e0, 0xac561580, 0x62310c40 .long 0xe4721c80, 0xb3599660, 0x1c0e0380, 0xa9549520, 0xc9649920, 0x08040100, 0xe8741d00, 0x954a92a0, 0x80401000, 0xdf6f9be0, 0x944a1280, 0xfa7d1f40, 0x753a8ea0, 0x8f4791e0, 0x3f1f87e0, 0xa65314c0 .long 0x472388e0, 0x070380e0, 0xa75394e0, 0xfc7e1f80, 0xf3799e60, 0x73398e60, 0x170b82e0, 0xba5d1740, 0x83419060, 0x592c8b20, 0x3c1e0780, 0x190c8320, 0xe6731cc0, 0x854290a0, 0x4f2789e0, 0xa8541500 .long 0x68340d00, 0x6b358d60, 0x81409020, 0xb2591640, 0x71388e20, 0x64320c80, 0xda6d1b40, 0x8b459160, 0xf87c1f00, 0xeb759d60, 0x0f0781e0, 0x4b258960, 0x70380e00, 0x562b0ac0, 0x9d4e93a0, 0x351a86a0 .long 0x1e0f03c0, 0x24120480, 0x0e0701c0, 0x5e2f0bc0, 0x63318c60, 0x582c0b00, 0xd1689a20, 0xa2511440, 0x251284a0, 0x22110440, 0x7c3e0f80, 0x3b1d8760, 0x01008020, 0x21108420, 0x783c0f00, 0x874390e0 .long 0xd46a1a80, 0x0, 0x462308c0, 0x572b8ae0, 0x9f4f93e0, 0xd3699a60, 0x271384e0, 0x52290a40, 0x4c260980, 0x361b06c0, 0x02010040, 0xe7739ce0, 0xa0501400, 0xc4621880, 0xc8641900, 0x9e4f13c0 .long 0xea751d40, 0xbf5f97e0, 0x8a451140, 0xd2691a40, 0x40200800, 0xc76398e0, 0x381c0700, 0xb55a96a0, 0xa3519460, 0xf77b9ee0, 0xf2791e40, 0xce6719c0, 0xf97c9f20, 0x61308c20, 0x150a82a0, 0xa1509420 .long 0xe0701c00, 0xae5715c0, 0x5d2e8ba0, 0xa4521480, 0x9b4d9360, 0x341a0680, 0x1a0d0340, 0x552a8aa0, 0xad5695a0, 0x93499260, 0x32190640, 0x30180600, 0xf57a9ea0, 0x8c461180, 0xb1589620, 0xe3719c60 .long 0x1d0e83a0, 0xf67b1ec0, 0xe2711c40, 0x2e1705c0, 0x82411040, 0x66330cc0, 0xca651940, 0x60300c00, 0xc0601800, 0x29148520, 0x23118460, 0xab559560, 0x0d0681a0, 0x53298a60, 0x4e2709c0, 0x6f378de0 .long 0xd56a9aa0, 0xdb6d9b60, 0x371b86e0, 0x452288a0, 0xde6f1bc0, 0xfd7e9fa0, 0x8e4711c0, 0x2f1785e0, 0x03018060, 0xff7f9fe0, 0x6a350d40, 0x72390e40, 0x6d368da0, 0x6c360d80, 0x5b2d8b60, 0x51288a20 .long 0x8d4691a0, 0x1b0d8360, 0xaf5795e0, 0x92491240, 0xbb5d9760, 0xdd6e9ba0, 0xbc5e1780, 0x7f3f8fe0, 0x11088220, 0xd96c9b20, 0x5c2e0b80, 0x41208820, 0x1f0f83e0, 0x10080200, 0x5a2d0b40, 0xd86c1b00 .long 0x0a050140, 0xc1609820, 0x31188620, 0x88441100, 0xa55294a0, 0xcd6699a0, 0x7b3d8f60, 0xbd5e97a0, 0x2d1685a0, 0x743a0e80, 0xd0681a00, 0x12090240, 0xb85c1700, 0xe5729ca0, 0xb45a1680, 0xb0581600 .long 0x89449120, 0x69348d20, 0x974b92e0, 0x4a250940, 0x0c060180, 0x964b12c0, 0x773b8ee0, 0x7e3f0fc0, 0x65328ca0, 0xb95c9720, 0xf1789e20, 0x09048120, 0xc56298a0, 0x6e370dc0, 0xc66318c0, 0x84421080 .long 0x180c0300, 0xf0781e00, 0x7d3e8fa0, 0xec761d80, 0x3a1d0740, 0xdc6e1b80, 0x4d2689a0, 0x20100400, 0x793c8f20, 0xee771dc0, 0x5f2f8be0, 0x3e1f07c0, 0xd76b9ae0, 0xcb659960, 0x391c8720, 0x48240900 #endif
2301_79861745/bench_create
crypto/sm4/src/asm/crypt_sm4_x86_64.S
Unix Assembly
unknown
20,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_SM4 #include <stdlib.h> #include <stdbool.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "crypt_sm4.h" /** * <<<: Cyclic shift to the left * ⊕: XOR * S-box: (see GB/T 32907-2016 6.2 a or GM/T 0002-2012 6.2 1) * L(B) = B⊕(B <<< 2)⊕(B <<< 10)⊕(B <<< 18)⊕(B <<< 24) * XBOX_0[i] = L(SBOX[i]) */ static const uint32_t XBOX_0[] = { 0xd55b5b8e, 0x924242d0, 0xeaa7a74d, 0xfdfbfb06, 0xcf3333fc, 0xe2878765, 0x3df4f4c9, 0xb5dede6b, 0x1658584e, 0xb4dada6e, 0x14505044, 0xc10b0bca, 0x28a0a088, 0xf8efef17, 0x2cb0b09c, 0x05141411, 0x2bacac87, 0x669d9dfb, 0x986a6af2, 0x77d9d9ae, 0x2aa8a882, 0xbcfafa46, 0x04101014, 0xc00f0fcf, 0xa8aaaa02, 0x45111154, 0x134c4c5f, 0x269898be, 0x4825256d, 0x841a1a9e, 0x0618181e, 0x9b6666fd, 0x9e7272ec, 0x4309094a, 0x51414110, 0xf7d3d324, 0x934646d5, 0xecbfbf53, 0x9a6262f8, 0x7be9e992, 0x33ccccff, 0x55515104, 0x0b2c2c27, 0x420d0d4f, 0xeeb7b759, 0xcc3f3ff3, 0xaeb2b21c, 0x638989ea, 0xe7939374, 0xb1cece7f, 0x1c70706c, 0xaba6a60d, 0xca2727ed, 0x08202028, 0xeba3a348, 0x975656c1, 0x82020280, 0xdc7f7fa3, 0x965252c4, 0xf9ebeb12, 0x74d5d5a1, 0x8d3e3eb3, 0x3ffcfcc3, 0xa49a9a3e, 0x461d1d5b, 0x071c1c1b, 0xa59e9e3b, 0xfff3f30c, 0xf0cfcf3f, 0x72cdcdbf, 0x175c5c4b, 0xb8eaea52, 0x810e0e8f, 0x5865653d, 0x3cf0f0cc, 0x1964647d, 0xe59b9b7e, 0x87161691, 0x4e3d3d73, 0xaaa2a208, 0x69a1a1c8, 0x6aadadc7, 0x83060685, 0xb0caca7a, 0x70c5c5b5, 0x659191f4, 0xd96b6bb2, 0x892e2ea7, 0xfbe3e318, 0xe8afaf47, 0x0f3c3c33, 0x4a2d2d67, 0x71c1c1b0, 0x5759590e, 0x9f7676e9, 0x35d4d4e1, 0x1e787866, 0x249090b4, 0x0e383836, 0x5f797926, 0x628d8def, 0x59616138, 0xd2474795, 0xa08a8a2a, 0x259494b1, 0x228888aa, 0x7df1f18c, 0x3bececd7, 0x01040405, 0x218484a5, 0x79e1e198, 0x851e1e9b, 0xd7535384, 0x00000000, 0x4719195e, 0x565d5d0b, 0x9d7e7ee3, 0xd04f4f9f, 0x279c9cbb, 0x5349491a, 0x4d31317c, 0x36d8d8ee, 0x0208080a, 0xe49f9f7b, 0xa2828220, 0xc71313d4, 0xcb2323e8, 0x9c7a7ae6, 0xe9abab42, 0xbdfefe43, 0x882a2aa2, 0xd14b4b9a, 0x41010140, 0xc41f1fdb, 0x38e0e0d8, 0xb7d6d661, 0xa18e8e2f, 0xf4dfdf2b, 0xf1cbcb3a, 0xcd3b3bf6, 0xfae7e71d, 0x608585e5, 0x15545441, 0xa3868625, 0xe3838360, 0xacbaba16, 0x5c757529, 0xa6929234, 0x996e6ef7, 0x34d0d0e4, 0x1a686872, 0x54555501, 0xafb6b619, 0x914e4edf, 0x32c8c8fa, 0x30c0c0f0, 0xf6d7d721, 0x8e3232bc, 0xb3c6c675, 0xe08f8f6f, 0x1d747469, 0xf5dbdb2e, 0xe18b8b6a, 0x2eb8b896, 0x800a0a8a, 0x679999fe, 0xc92b2be2, 0x618181e0, 0xc30303c0, 0x29a4a48d, 0x238c8caf, 0xa9aeae07, 0x0d343439, 0x524d4d1f, 0x4f393976, 0x6ebdbdd3, 0xd6575781, 0xd86f6fb7, 0x37dcdceb, 0x44151551, 0xdd7b7ba6, 0xfef7f709, 0x8c3a3ab6, 0x2fbcbc93, 0x030c0c0f, 0xfcffff03, 0x6ba9a9c2, 0x73c9c9ba, 0x6cb5b5d9, 0x6db1b1dc, 0x5a6d6d37, 0x50454515, 0x8f3636b9, 0x1b6c6c77, 0xadbebe13, 0x904a4ada, 0xb9eeee57, 0xde7777a9, 0xbef2f24c, 0x7efdfd83, 0x11444455, 0xda6767bd, 0x5d71712c, 0x40050545, 0x1f7c7c63, 0x10404050, 0x5b696932, 0xdb6363b8, 0x0a282822, 0xc20707c5, 0x31c4c4f5, 0x8a2222a8, 0xa7969631, 0xce3737f9, 0x7aeded97, 0xbff6f649, 0x2db4b499, 0x75d1d1a4, 0xd3434390, 0x1248485a, 0xbae2e258, 0xe6979771, 0xb6d2d264, 0xb2c2c270, 0x8b2626ad, 0x68a5a5cd, 0x955e5ecb, 0x4b292962, 0x0c30303c, 0x945a5ace, 0x76ddddab, 0x7ff9f986, 0x649595f1, 0xbbe6e65d, 0xf2c7c735, 0x0924242d, 0xc61717d1, 0x6fb9b9d6, 0xc51b1bde, 0x86121294, 0x18606078, 0xf3c3c330, 0x7cf5f589, 0xefb3b35c, 0x3ae8e8d2, 0xdf7373ac, 0x4c353579, 0x208080a0, 0x78e5e59d, 0xedbbbb56, 0x5e7d7d23, 0x3ef8f8c6, 0xd45f5f8b, 0xc82f2fe7, 0x39e4e4dd, 0x49212168, }; /* XBOX_1[i] = XBOX_0[i] <<< 8 */ static const uint32_t XBOX_1[] = { 0x5b5b8ed5, 0x4242d092, 0xa7a74dea, 0xfbfb06fd, 0x3333fccf, 0x878765e2, 0xf4f4c93d, 0xdede6bb5, 0x58584e16, 0xdada6eb4, 0x50504414, 0x0b0bcac1, 0xa0a08828, 0xefef17f8, 0xb0b09c2c, 0x14141105, 0xacac872b, 0x9d9dfb66, 0x6a6af298, 0xd9d9ae77, 0xa8a8822a, 0xfafa46bc, 0x10101404, 0x0f0fcfc0, 0xaaaa02a8, 0x11115445, 0x4c4c5f13, 0x9898be26, 0x25256d48, 0x1a1a9e84, 0x18181e06, 0x6666fd9b, 0x7272ec9e, 0x09094a43, 0x41411051, 0xd3d324f7, 0x4646d593, 0xbfbf53ec, 0x6262f89a, 0xe9e9927b, 0xccccff33, 0x51510455, 0x2c2c270b, 0xd0d4f42, 0xb7b759ee, 0x3f3ff3cc, 0xb2b21cae, 0x8989ea63, 0x939374e7, 0xcece7fb1, 0x70706c1c, 0xa6a60dab, 0x2727edca, 0x20202808, 0xa3a348eb, 0x5656c197, 0x02028082, 0x7f7fa3dc, 0x5252c496, 0xebeb12f9, 0xd5d5a174, 0x3e3eb38d, 0xfcfcc33f, 0x9a9a3ea4, 0x1d1d5b46, 0x1c1c1b07, 0x9e9e3ba5, 0xf3f30cff, 0xcfcf3ff0, 0xcdcdbf72, 0x5c5c4b17, 0xeaea52b8, 0x0e0e8f81, 0x65653d58, 0xf0f0cc3c, 0x64647d19, 0x9b9b7ee5, 0x16169187, 0x3d3d734e, 0xa2a208aa, 0xa1a1c869, 0xadadc76a, 0x06068583, 0xcaca7ab0, 0xc5c5b570, 0x9191f465, 0x6b6bb2d9, 0x2e2ea789, 0xe3e318fb, 0xafaf47e8, 0x3c3c330f, 0x2d2d674a, 0xc1c1b071, 0x59590e57, 0x7676e99f, 0xd4d4e135, 0x7878661e, 0x9090b424, 0x3838360e, 0x7979265f, 0x8d8def62, 0x61613859, 0x474795d2, 0x8a8a2aa0, 0x9494b125, 0x8888aa22, 0xf1f18c7d, 0xececd73b, 0x04040501, 0x8484a521, 0xe1e19879, 0x1e1e9b85, 0x535384d7, 0x00000000, 0x19195e47, 0x5d5d0b56, 0x7e7ee39d, 0x4f4f9fd0, 0x9c9cbb27, 0x49491a53, 0x31317c4d, 0xd8d8ee36, 0x08080a02, 0x9f9f7be4, 0x828220a2, 0x1313d4c7, 0x2323e8cb, 0x7a7ae69c, 0xabab42e9, 0xfefe43bd, 0x2a2aa288, 0x4b4b9ad1, 0x01014041, 0x1f1fdbc4, 0xe0e0d838, 0xd6d661b7, 0x8e8e2fa1, 0xdfdf2bf4, 0xcbcb3af1, 0x3b3bf6cd, 0xe7e71dfa, 0x8585e560, 0x54544115, 0x868625a3, 0x838360e3, 0xbaba16ac, 0x7575295c, 0x929234a6, 0x6e6ef799, 0xd0d0e434, 0x6868721a, 0x55550154, 0xb6b619af, 0x4e4edf91, 0xc8c8fa32, 0xc0c0f030, 0xd7d721f6, 0x3232bc8e, 0xc6c675b3, 0x8f8f6fe0, 0x7474691d, 0xdbdb2ef5, 0x8b8b6ae1, 0xb8b8962e, 0x0a0a8a80, 0x9999fe67, 0x2b2be2c9, 0x8181e061, 0x0303c0c3, 0xa4a48d29, 0x8c8caf23, 0xaeae07a9, 0x3434390d, 0x4d4d1f52, 0x3939764f, 0xbdbdd36e, 0x575781d6, 0x6f6fb7d8, 0xdcdceb37, 0x15155144, 0x7b7ba6dd, 0xf7f709fe, 0x3a3ab68c, 0xbcbc932f, 0x0c0c0f03, 0xffff03fc, 0xa9a9c26b, 0xc9c9ba73, 0xb5b5d96c, 0xb1b1dc6d, 0x6d6d375a, 0x45451550, 0x3636b98f, 0x6c6c771b, 0xbebe13ad, 0x4a4ada90, 0xeeee57b9, 0x7777a9de, 0xf2f24cbe, 0xfdfd837e, 0x44445511, 0x6767bdda, 0x71712c5d, 0x05054540, 0x7c7c631f, 0x40405010, 0x6969325b, 0x6363b8db, 0x2828220a, 0x0707c5c2, 0xc4c4f531, 0x2222a88a, 0x969631a7, 0x3737f9ce, 0xeded977a, 0xf6f649bf, 0xb4b4992d, 0xd1d1a475, 0x434390d3, 0x48485a12, 0xe2e258ba, 0x979771e6, 0xd2d264b6, 0xc2c270b2, 0x2626ad8b, 0xa5a5cd68, 0x5e5ecb95, 0x2929624b, 0x30303c0c, 0x5a5ace94, 0xddddab76, 0xf9f9867f, 0x9595f164, 0xe6e65dbb, 0xc7c735f2, 0x24242d09, 0x1717d1c6, 0xb9b9d66f, 0x1b1bdec5, 0x12129486, 0x60607818, 0xc3c330f3, 0xf5f5897c, 0xb3b35cef, 0xe8e8d23a, 0x7373acdf, 0x3535794c, 0x8080a020, 0xe5e59d78, 0xbbbb56ed, 0x7d7d235e, 0xf8f8c63e, 0x5f5f8bd4, 0x2f2fe7c8, 0xe4e4dd39, 0x21216849, }; /* XBOX_2[i] = XBOX_0[i] <<< 16 */ static const uint32_t XBOX_2[] = { 0x5b8ed55b, 0x42d09242, 0xa74deaa7, 0xfb06fdfb, 0x33fccf33, 0x8765e287, 0xf4c93df4, 0xde6bb5de, 0x584e1658, 0xda6eb4da, 0x50441450, 0x0bcac10b, 0xa08828a0, 0xef17f8ef, 0xb09c2cb0, 0x14110514, 0xac872bac, 0x9dfb669d, 0x6af2986a, 0xd9ae77d9, 0xa8822aa8, 0xfa46bcfa, 0x10140410, 0x0fcfc00f, 0xaa02a8aa, 0x11544511, 0x4c5f134c, 0x98be2698, 0x256d4825, 0x1a9e841a, 0x181e0618, 0x66fd9b66, 0x72ec9e72, 0x094a4309, 0x41105141, 0xd324f7d3, 0x46d59346, 0xbf53ecbf, 0x62f89a62, 0xe9927be9, 0xccff33cc, 0x51045551, 0x2c270b2c, 0x0d4f420d, 0xb759eeb7, 0x3ff3cc3f, 0xb21caeb2, 0x89ea6389, 0x9374e793, 0xce7fb1ce, 0x706c1c70, 0xa60daba6, 0x27edca27, 0x20280820, 0xa348eba3, 0x56c19756, 0x02808202, 0x7fa3dc7f, 0x52c49652, 0xeb12f9eb, 0xd5a174d5, 0x3eb38d3e, 0xfcc33ffc, 0x9a3ea49a, 0x1d5b461d, 0x1c1b071c, 0x9e3ba59e, 0xf30cfff3, 0xcf3ff0cf, 0xcdbf72cd, 0x5c4b175c, 0xea52b8ea, 0x0e8f810e, 0x653d5865, 0xf0cc3cf0, 0x647d1964, 0x9b7ee59b, 0x16918716, 0x3d734e3d, 0xa208aaa2, 0xa1c869a1, 0xadc76aad, 0x06858306, 0xca7ab0ca, 0xc5b570c5, 0x91f46591, 0x6bb2d96b, 0x2ea7892e, 0xe318fbe3, 0xaf47e8af, 0x3c330f3c, 0x2d674a2d, 0xc1b071c1, 0x590e5759, 0x76e99f76, 0xd4e135d4, 0x78661e78, 0x90b42490, 0x38360e38, 0x79265f79, 0x8def628d, 0x61385961, 0x4795d247, 0x8a2aa08a, 0x94b12594, 0x88aa2288, 0xf18c7df1, 0xecd73bec, 0x04050104, 0x84a52184, 0xe19879e1, 0x1e9b851e, 0x5384d753, 0x00000000, 0x195e4719, 0x5d0b565d, 0x7ee39d7e, 0x4f9fd04f, 0x9cbb279c, 0x491a5349, 0x317c4d31, 0xd8ee36d8, 0x080a0208, 0x9f7be49f, 0x8220a282, 0x13d4c713, 0x23e8cb23, 0x7ae69c7a, 0xab42e9ab, 0xfe43bdfe, 0x2aa2882a, 0x4b9ad14b, 0x01404101, 0x1fdbc41f, 0xe0d838e0, 0xd661b7d6, 0x8e2fa18e, 0xdf2bf4df, 0xcb3af1cb, 0x3bf6cd3b, 0xe71dfae7, 0x85e56085, 0x54411554, 0x8625a386, 0x8360e383, 0xba16acba, 0x75295c75, 0x9234a692, 0x6ef7996e, 0xd0e434d0, 0x68721a68, 0x55015455, 0xb619afb6, 0x4edf914e, 0xc8fa32c8, 0xc0f030c0, 0xd721f6d7, 0x32bc8e32, 0xc675b3c6, 0x8f6fe08f, 0x74691d74, 0xdb2ef5db, 0x8b6ae18b, 0xb8962eb8, 0x0a8a800a, 0x99fe6799, 0x2be2c92b, 0x81e06181, 0x03c0c303, 0xa48d29a4, 0x8caf238c, 0xae07a9ae, 0x34390d34, 0x4d1f524d, 0x39764f39, 0xbdd36ebd, 0x5781d657, 0x6fb7d86f, 0xdceb37dc, 0x15514415, 0x7ba6dd7b, 0xf709fef7, 0x3ab68c3a, 0xbc932fbc, 0x0c0f030c, 0xff03fcff, 0xa9c26ba9, 0xc9ba73c9, 0xb5d96cb5, 0xb1dc6db1, 0x6d375a6d, 0x45155045, 0x36b98f36, 0x6c771b6c, 0xbe13adbe, 0x4ada904a, 0xee57b9ee, 0x77a9de77, 0xf24cbef2, 0xfd837efd, 0x44551144, 0x67bdda67, 0x712c5d71, 0x05454005, 0x7c631f7c, 0x40501040, 0x69325b69, 0x63b8db63, 0x28220a28, 0x07c5c207, 0xc4f531c4, 0x22a88a22, 0x9631a796, 0x37f9ce37, 0xed977aed, 0xf649bff6, 0xb4992db4, 0xd1a475d1, 0x4390d343, 0x485a1248, 0xe258bae2, 0x9771e697, 0xd264b6d2, 0xc270b2c2, 0x26ad8b26, 0xa5cd68a5, 0x5ecb955e, 0x29624b29, 0x303c0c30, 0x5ace945a, 0xddab76dd, 0xf9867ff9, 0x95f16495, 0xe65dbbe6, 0xc735f2c7, 0x242d0924, 0x17d1c617, 0xb9d66fb9, 0x1bdec51b, 0x12948612, 0x60781860, 0xc330f3c3, 0xf5897cf5, 0xb35cefb3, 0xe8d23ae8, 0x73acdf73, 0x35794c35, 0x80a02080, 0xe59d78e5, 0xbb56edbb, 0x7d235e7d, 0xf8c63ef8, 0x5f8bd45f, 0x2fe7c82f, 0xe4dd39e4, 0x21684921, }; /* XBOX_3[i] = XBOX_0[i] <<< 24 */ static const uint32_t XBOX_3[] = { 0x8ed55b5b, 0xd0924242, 0x4deaa7a7, 0x06fdfbfb, 0xfccf3333, 0x65e28787, 0xc93df4f4, 0x6bb5dede, 0x4e165858, 0x6eb4dada, 0x44145050, 0xcac10b0b, 0x8828a0a0, 0x17f8efef, 0x9c2cb0b0, 0x11051414, 0x872bacac, 0xfb669d9d, 0xf2986a6a, 0xae77d9d9, 0x822aa8a8, 0x46bcfafa, 0x14041010, 0xcfc00f0f, 0x02a8aaaa, 0x54451111, 0x5f134c4c, 0xbe269898, 0x6d482525, 0x9e841a1a, 0x1e061818, 0xfd9b6666, 0xec9e7272, 0x4a430909, 0x10514141, 0x24f7d3d3, 0xd5934646, 0x53ecbfbf, 0xf89a6262, 0x927be9e9, 0xff33cccc, 0x04555151, 0x270b2c2c, 0x4f420d0d, 0x59eeb7b7, 0xf3cc3f3f, 0x1caeb2b2, 0xea638989, 0x74e79393, 0x7fb1cece, 0x6c1c7070, 0x0daba6a6, 0xedca2727, 0x28082020, 0x48eba3a3, 0xc1975656, 0x80820202, 0xa3dc7f7f, 0xc4965252, 0x12f9ebeb, 0xa174d5d5, 0xb38d3e3e, 0xc33ffcfc, 0x3ea49a9a, 0x5b461d1d, 0x1b071c1c, 0x3ba59e9e, 0x0cfff3f3, 0x3ff0cfcf, 0xbf72cdcd, 0x4b175c5c, 0x52b8eaea, 0x8f810e0e, 0x3d586565, 0xcc3cf0f0, 0x7d196464, 0x7ee59b9b, 0x91871616, 0x734e3d3d, 0x08aaa2a2, 0xc869a1a1, 0xc76aadad, 0x85830606, 0x7ab0caca, 0xb570c5c5, 0xf4659191, 0xb2d96b6b, 0xa7892e2e, 0x18fbe3e3, 0x47e8afaf, 0x330f3c3c, 0x674a2d2d, 0xb071c1c1, 0x0e575959, 0xe99f7676, 0xe135d4d4, 0x661e7878, 0xb4249090, 0x360e3838, 0x265f7979, 0xef628d8d, 0x38596161, 0x95d24747, 0x2aa08a8a, 0xb1259494, 0xaa228888, 0x8c7df1f1, 0xd73becec, 0x05010404, 0xa5218484, 0x9879e1e1, 0x9b851e1e, 0x84d75353, 0x00000000, 0x5e471919, 0x0b565d5d, 0xe39d7e7e, 0x9fd04f4f, 0xbb279c9c, 0x1a534949, 0x7c4d3131, 0xee36d8d8, 0x0a020808, 0x7be49f9f, 0x20a28282, 0xd4c71313, 0xe8cb2323, 0xe69c7a7a, 0x42e9abab, 0x43bdfefe, 0xa2882a2a, 0x9ad14b4b, 0x40410101, 0xdbc41f1f, 0xd838e0e0, 0x61b7d6d6, 0x2fa18e8e, 0x2bf4dfdf, 0x3af1cbcb, 0xf6cd3b3b, 0x1dfae7e7, 0xe5608585, 0x41155454, 0x25a38686, 0x60e38383, 0x16acbaba, 0x295c7575, 0x34a69292, 0xf7996e6e, 0xe434d0d0, 0x721a6868, 0x01545555, 0x19afb6b6, 0xdf914e4e, 0xfa32c8c8, 0xf030c0c0, 0x21f6d7d7, 0xbc8e3232, 0x75b3c6c6, 0x6fe08f8f, 0x691d7474, 0x2ef5dbdb, 0x6ae18b8b, 0x962eb8b8, 0x8a800a0a, 0xfe679999, 0xe2c92b2b, 0xe0618181, 0xc0c30303, 0x8d29a4a4, 0xaf238c8c, 0x07a9aeae, 0x390d3434, 0x1f524d4d, 0x764f3939, 0xd36ebdbd, 0x81d65757, 0xb7d86f6f, 0xeb37dcdc, 0x51441515, 0xa6dd7b7b, 0x09fef7f7, 0xb68c3a3a, 0x932fbcbc, 0x0f030c0c, 0x03fcffff, 0xc26ba9a9, 0xba73c9c9, 0xd96cb5b5, 0xdc6db1b1, 0x375a6d6d, 0x15504545, 0xb98f3636, 0x771b6c6c, 0x13adbebe, 0xda904a4a, 0x57b9eeee, 0xa9de7777, 0x4cbef2f2, 0x837efdfd, 0x55114444, 0xbdda6767, 0x2c5d7171, 0x45400505, 0x631f7c7c, 0x50104040, 0x325b6969, 0xb8db6363, 0x220a2828, 0xc5c20707, 0xf531c4c4, 0xa88a2222, 0x31a79696, 0xf9ce3737, 0x977aeded, 0x49bff6f6, 0x992db4b4, 0xa475d1d1, 0x90d34343, 0x5a124848, 0x58bae2e2, 0x71e69797, 0x64b6d2d2, 0x70b2c2c2, 0xad8b2626, 0xcd68a5a5, 0xcb955e5e, 0x624b2929, 0x3c0c3030, 0xce945a5a, 0xab76dddd, 0x867ff9f9, 0xf1649595, 0x5dbbe6e6, 0x35f2c7c7, 0x2d092424, 0xd1c61717, 0xd66fb9b9, 0xdec51b1b, 0x94861212, 0x78186060, 0x30f3c3c3, 0x897cf5f5, 0x5cefb3b3, 0xd23ae8e8, 0xacdf7373, 0x794c3535, 0xa0208080, 0x9d78e5e5, 0x56edbbbb, 0x235e7d7d, 0xc63ef8f8, 0x8bd45f5f, 0xe7c82f2f, 0xdd39e4e4, 0x68492121, }; /** * TE(a,b,c,d) = LE(SBOX[a],SBOX[b],SBOX[c],SBOX[d]) * = LE(SBOX[a],0,0,0)⊕LE(0,SBOX[b],0,0)⊕LE(0,0,SBOX[c],0)⊕LE(0,0,0,SBOX[d]) * = LE(SBOX[a] << 24)⊕LE(SBOX[b] << 16)⊕LE(SBOX[c] << 8)⊕LE(SBOX[d]) * = LE(SBOX[a] <<< 24)⊕LE(SBOX[b] <<< 16)⊕LE(SBOX[c] <<< 8)⊕LE(SBOX[d]) * = (LE(SBOX[a]) <<< 24)⊕(LE(SBOX[b]) <<< 16)⊕(LE(SBOX[c]) <<< 8)⊕LE(SBOX[d]) * = (XBOX_0[a] <<< 24)⊕(XBOX_0[b] <<< 16)⊕(XBOX_0[c] <<< 8)⊕XBOX_0[d] * = XBOX_3[a]⊕XBOX_2[b]⊕XBOX_1[c]⊕XBOX_0[d] * F(Xi,Xi+1,Xi+2,Xi+3,rki) = Xi⊕TE(Xi+1⊕Xi+2⊕Xi+3⊕rki) */ #define CRYPT_SM4_ROUND(t, x0, x1, x2, x3, rk, sbox) \ do { \ (t) = (x1) ^ (x2) ^ (x3) ^ (rk); \ (x0) ^= (sbox##_3)[((t) >> 24) & 0xff]; \ (x0) ^= (sbox##_2)[((t) >> 16) & 0xff]; \ (x0) ^= (sbox##_1)[((t) >> 8) & 0xff]; \ (x0) ^= (sbox##_0)[(t) & 0xff]; \ } while (0) /* X(i+4) = F(Xi Xi+1 Xi+2 Xi+3 rk[i]),i = 0,1,...,31; */ #define ENC_ROUND_FUNCTION(t, x0, x1, x2, x3, roundKey, sbox) \ for (int i = 0; i < 32; i += 4) { \ CRYPT_SM4_ROUND((t), (x0), (x1), (x2), (x3), (roundKey)[(i) + 0], sbox); \ CRYPT_SM4_ROUND((t), (x1), (x2), (x3), (x0), (roundKey)[(i) + 1], sbox); \ CRYPT_SM4_ROUND((t), (x2), (x3), (x0), (x1), (roundKey)[(i) + 2], sbox); \ CRYPT_SM4_ROUND((t), (x3), (x0), (x1), (x2), (roundKey)[(i) + 3], sbox); \ } /* X(i+4) = F(Xi Xi+1 Xi+2 Xi+3 rk[31 - i]),i = 0,1,...,31; */ #define DEC_ROUND_FUNCTION(t, x0, x1, x2, x3, roundKey, sbox) \ for (int i = 28; i >= 0; i -= 4) { \ CRYPT_SM4_ROUND((t), (x0), (x1), (x2), (x3), (roundKey)[(i) + 3], sbox); \ CRYPT_SM4_ROUND((t), (x1), (x2), (x3), (x0), (roundKey)[(i) + 2], sbox); \ CRYPT_SM4_ROUND((t), (x2), (x3), (x0), (x1), (roundKey)[(i) + 1], sbox); \ CRYPT_SM4_ROUND((t), (x3), (x0), (x1), (x2), (roundKey)[(i) + 0], sbox); \ } /* enc is true: encrypt, enc is false: decrypt */ static void SM4_Crypt(uint8_t *out, const uint8_t *in, const uint32_t *rk, uint32_t x[5], bool enc) { x[0] = GET_UINT32_BE(in, 0); // x[0]: 4 bytes starting from index 0 of the in x[1] = GET_UINT32_BE(in, 4); // x[1]: 4 bytes starting from index 4 of the in x[2] = GET_UINT32_BE(in, 8); // x[2]: 4 bytes starting from index 8 of the in x[3] = GET_UINT32_BE(in, 12); // x[3]: 4 bytes starting from index 12 of the in /* Round function */ if (enc) { ENC_ROUND_FUNCTION(x[4], x[0], x[1], x[2], x[3], rk, XBOX); // Encryption } else { DEC_ROUND_FUNCTION(x[4], x[0], x[1], x[2], x[3], rk, XBOX); // Decryption } /* Reverse R(X32 X33 X34 X35) = (X35 X34 X33 X32) */ PUT_UINT32_BE(x[3], out, 0); // x[3] put into the 4 bytes starting from index 0 of the out PUT_UINT32_BE(x[2], out, 4); // x[2] put into the 4 bytes starting from index 4 of the out PUT_UINT32_BE(x[1], out, 8); // x[1] put into the 4 bytes starting from index 8 of the out PUT_UINT32_BE(x[0], out, 12); // x[0] put into the 4 bytes starting from index 12 of the out } static int32_t CRYPT_SM4_Crypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length, bool enc) { if (ctx == NULL || in == NULL || out == NULL || length == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (length % CRYPT_SM4_BLOCKSIZE != 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } uint32_t x[5]; // Used as a temporary variable. uint32_t blocks = length / CRYPT_SM4_BLOCKSIZE; for (uint32_t i = 0; i < blocks; i++) { SM4_Crypt(out + i * CRYPT_SM4_BLOCKSIZE, in + i * CRYPT_SM4_BLOCKSIZE, ctx->rk, x, enc); } if (!enc) { (void)memset_s(x, sizeof(x), 0, sizeof(x)); } return CRYPT_SUCCESS; } int32_t CRYPT_SM4_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length) { return CRYPT_SM4_Crypt(ctx, in, out, length, true); } int32_t CRYPT_SM4_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t length) { return CRYPT_SM4_Crypt(ctx, in, out, length, false); } void CRYPT_SM4_Clean(CRYPT_SM4_Ctx *ctx) { BSL_SAL_CleanseData((void *)(ctx), sizeof(CRYPT_SM4_Ctx)); } #endif // HITLS_CRYPTO_SM4
2301_79861745/bench_create
crypto/sm4/src/crypt_sm4.c
C
unknown
18,542
/* * 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_SM4 #include "crypt_sm4_armv8.h" #include "crypt_sm4.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #ifdef HITLS_CRYPTO_XTS // key[0..16]: data key // key[16..32]: tweak key int32_t CRYPT_SM4_XTS_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != XTS_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } if (memcmp(key, key + CRYPT_SM4_BLOCKSIZE, CRYPT_SM4_BLOCKSIZE) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_UNSAFE_KEY); return CRYPT_SM4_UNSAFE_KEY; } CRYPT_SM4_Ctx *tmk = (CRYPT_SM4_Ctx *)&ctx[1]; Vpsm4SetEncryptKey(key, (SM4_KEY *)ctx->rk); Vpsm4SetEncryptKey(key + CRYPT_SM4_BLOCKSIZE, (SM4_KEY *)tmk->rk); return CRYPT_SUCCESS; } // key[0..16]: data key // key[16..32]: tweak key int32_t CRYPT_SM4_XTS_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != XTS_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } if (memcmp(key, key + CRYPT_SM4_BLOCKSIZE, CRYPT_SM4_BLOCKSIZE) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_UNSAFE_KEY); return CRYPT_SM4_UNSAFE_KEY; } CRYPT_SM4_Ctx *tmk = (CRYPT_SM4_Ctx *)&ctx[1]; Vpsm4SetDecryptKey(key, (SM4_KEY *)ctx->rk); Vpsm4SetEncryptKey(key + CRYPT_SM4_BLOCKSIZE, (SM4_KEY *)tmk->rk); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_XTS_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { CRYPT_SM4_Ctx *tmk = NULL; if (ctx == NULL || iv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } tmk = (CRYPT_SM4_Ctx *)&ctx[1]; Vpsm4XtsCipher(in, out, len, (const SM4_KEY *)ctx->rk, (const SM4_KEY *)tmk->rk, iv, 1); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_XTS_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { CRYPT_SM4_Ctx *tmk = NULL; if (ctx == NULL || iv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } tmk = (CRYPT_SM4_Ctx *)&ctx[1]; Vpsm4XtsCipher(in, out, len, (const SM4_KEY *)ctx->rk, (const SM4_KEY *)tmk->rk, iv, 0); return CRYPT_SUCCESS; } #endif int32_t CRYPT_SM4_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != SM4_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } Vpsm4SetEncryptKey(key, (SM4_KEY *)ctx->rk); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != SM4_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } Vpsm4SetDecryptKey(key, (SM4_KEY *)ctx->rk); return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_ECB int32_t CRYPT_SM4_ECB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } Vpsm4EcbEncrypt(in, out, len, ctx->rk); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_ECB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } Vpsm4EcbEncrypt(in, out, len, ctx->rk); return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_CBC int32_t CRYPT_SM4_CBC_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len % CRYPT_SM4_BLOCKSIZE != 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } Vpsm4CbcEncrypt(in, out, len, ctx->rk, iv, 1); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_CBC_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len % CRYPT_SM4_BLOCKSIZE != 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } Vpsm4CbcEncrypt(in, out, len, ctx->rk, iv, 0); return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_CBC #ifdef HITLS_CRYPTO_CFB int32_t CRYPT_SM4_CFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int tmp = *offset; Vpsm4Cfb128Encrypt(in, out, len, ctx->rk, iv, &tmp); *offset = (uint8_t)tmp; return CRYPT_SUCCESS; } int32_t CRYPT_SM4_CFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int tmp = *offset; Vpsm4Cfb128Decrypt(in, out, len, ctx->rk, iv, &tmp); *offset = (uint8_t)tmp; return CRYPT_SUCCESS; } #endif #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM) int32_t CRYPT_SM4_CTR_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } Vpsm4Ctr32EncryptBlocks(in, out, len, ctx->rk, iv); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_CTR_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } Vpsm4Ctr32EncryptBlocks(in, out, len, ctx->rk, iv); return CRYPT_SUCCESS; } #endif #endif // HITLS_CRYPTO_SM4
2301_79861745/bench_create
crypto/sm4/src/crypt_sm4_armv8.c
C
unknown
7,473
/* * 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_SM4_ARMV8_H #define CRYPT_SM4_ARMV8_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM4 #include <stdint.h> #include <stddef.h> #define XTS_KEY_LEN 32 #define SM4_KEY_LEN 16 typedef struct SM4_KEY_st { uint32_t rk[XTS_KEY_LEN]; } SM4_KEY; void Vpsm4SetEncryptKey(const unsigned char *userKey, SM4_KEY *key); void Vpsm4SetDecryptKey(const unsigned char *userKey, SM4_KEY *key); #ifdef HITLS_CRYPTO_XTS void Vpsm4XtsCipher(const unsigned char *in, unsigned char *out, uint32_t length, const SM4_KEY *key1, const SM4_KEY *key2, const uint8_t *iv, uint32_t enc); #endif #ifdef HITLS_CRYPTO_CBC void Vpsm4CbcEncrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, const int enc); #endif #ifdef HITLS_CRYPTO_ECB void Vpsm4EcbEncrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key); #endif #ifdef HITLS_CRYPTO_CFB void Vpsm4Cfb128Encrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, int *num); void Vpsm4Cfb128Decrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, int *num); #endif #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM) void Vpsm4Ctr32EncryptBlocks(const uint8_t *in, uint8_t *out, uint64_t blocks, const uint32_t *key, uint8_t *iv); #endif #endif // HITLS_CRYPTO_SM4 #endif
2301_79861745/bench_create
crypto/sm4/src/crypt_sm4_armv8.h
C
unknown
1,907
/* * 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_SM4 #include "securec.h" #include "bsl_sal.h" #include "crypt_sm4.h" void CRYPT_SM4_XTS_Clean(CRYPT_SM4_Ctx *ctx) { if (ctx == NULL) { return; } BSL_SAL_CleanseData((void *)(ctx), sizeof(CRYPT_SM4_Ctx) * 2); // cipher context has 2 method contexts in xts mode } #endif /* HITLS_CRYPTO_SM4 */
2301_79861745/bench_create
crypto/sm4/src/crypt_sm4_public.c
C
unknown
933
/* * 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_SM4 #include "crypt_sm4_x86_64.h" #include "crypt_sm4.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "securec.h" #define XTS_KEY_LEN 32 #define SM4_KEY_LEN 16 #define XTS_POLYNOMIAL 0xe1 #define LAST_BLOCK_HEAD 240 #define BYTE_MOST_SIG 128 #define BYTE 8 void SM4_XTS_Calculate_Tweak(unsigned char *t, const unsigned int idx) { uint32_t j; uint8_t tweak_in, tweak_out; tweak_in = 0; for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { tweak_out = (t[idx + j] << (BYTE - 1)) & BYTE_MOST_SIG; t[j] = (t[idx + j] >> 1) + tweak_in; tweak_in = tweak_out; } if (tweak_out) { t[0] ^= XTS_POLYNOMIAL; } } static void SM4_XTS_Encrypt_Helper(uint32_t left, const uint32_t dataLen, uint8_t* t, uint8_t *x, const uint8_t* plain, uint8_t* cipher, const uint32_t* dataRk) { uint32_t i, j; uint32_t init; init = dataLen - left; if (left >= CRYPT_SM4_BLOCKSIZE) { left = left % CRYPT_SM4_BLOCKSIZE; for (i = init; i < (dataLen - left); i += CRYPT_SM4_BLOCKSIZE) { for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { t[j + CRYPT_SM4_BLOCKSIZE] = t[j]; } for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { x[j] = plain[i + j] ^ t[j]; } SM4_Encrypt(x, cipher + i, dataRk); for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { cipher[i + j] = cipher[i + j] ^ t[j]; } SM4_XTS_Calculate_Tweak(t, CRYPT_SM4_BLOCKSIZE); } } init = dataLen - left; if (left != 0) { for (i = 0; i < left; i++) { cipher[init + i] = cipher[init - CRYPT_SM4_BLOCKSIZE + i]; x[i] = plain[init + i]; } for (i = left; i < CRYPT_SM4_BLOCKSIZE; i++) { x[i] = cipher[init - CRYPT_SM4_BLOCKSIZE + i]; } for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) { x[i] = x[i] ^ t[i]; } SM4_Encrypt(x, cipher + init - CRYPT_SM4_BLOCKSIZE, dataRk); for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) { cipher[init - CRYPT_SM4_BLOCKSIZE + i] = cipher[init - CRYPT_SM4_BLOCKSIZE + i] ^ t[i]; } } } int32_t SM4_XTS_En(uint8_t* cipher, const uint8_t* plain, const uint32_t* dataRk, const uint8_t* tweak, const uint32_t dataLen) { uint32_t left; uint8_t x[CRYPT_SM4_BLOCKSIZE_16] = {0}; uint8_t t[CRYPT_SM4_BLOCKSIZE_16] = {0}; if (dataLen < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } left = dataLen % CRYPT_SM4_BLOCKSIZE_16; // MODES_XTS_Ctrl has TW = Enc_K2(iv) done memcpy_s(t, CRYPT_SM4_BLOCKSIZE_16, tweak, CRYPT_SM4_BLOCKSIZE); if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) { SM4_XTS_Encrypt_Blocks(plain, cipher, dataLen, dataRk, t); } if (left == 0) { return CRYPT_SUCCESS; } else { if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) { SM4_XTS_Calculate_Tweak(t, LAST_BLOCK_HEAD); } SM4_XTS_Encrypt_Helper(left, dataLen, t, x, plain, cipher, dataRk); } return CRYPT_SUCCESS; } static void SM4_XTS_Decrypt_Helper(uint32_t left, const uint32_t dataLen, uint8_t* t, uint8_t *x, uint8_t* plain, const uint8_t* cipher, const uint32_t* dataRk) { uint32_t i, j; uint32_t init; init = dataLen - left; if (left >= CRYPT_SM4_BLOCKSIZE) { left = left % CRYPT_SM4_BLOCKSIZE; for (i = init; i < (dataLen - left); i += CRYPT_SM4_BLOCKSIZE) { for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { t[j + CRYPT_SM4_BLOCKSIZE] = t[j]; } for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { x[j] = cipher[i + j] ^ t[j]; } SM4_Decrypt(x, plain + i, dataRk); for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { plain[i + j] = plain[i + j] ^ t[j]; } SM4_XTS_Calculate_Tweak(t, CRYPT_SM4_BLOCKSIZE); } } init = dataLen - left; if (left != 0) { // recompute // m-T for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { x[j] = cipher[init - CRYPT_SM4_BLOCKSIZE + j] ^ t[j]; } SM4_Decrypt(x, plain + init - CRYPT_SM4_BLOCKSIZE, dataRk); for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { plain[init - CRYPT_SM4_BLOCKSIZE + j] = plain[init - CRYPT_SM4_BLOCKSIZE + j] ^ t[j]; } for (i = 0; i < left; i++) { plain[init + i] = plain[init - CRYPT_SM4_BLOCKSIZE + i]; x[i] = cipher[init + i]; } for (i = left; i < CRYPT_SM4_BLOCKSIZE; i++) { x[i] = plain[init - CRYPT_SM4_BLOCKSIZE + i]; } // (m-1)-T for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) { x[i] = x[i] ^ t[CRYPT_SM4_BLOCKSIZE + i]; } SM4_Decrypt(x, plain + init - CRYPT_SM4_BLOCKSIZE, dataRk); for (i = 0; i < CRYPT_SM4_BLOCKSIZE; i++) { plain[init - CRYPT_SM4_BLOCKSIZE + i] = plain[init - CRYPT_SM4_BLOCKSIZE + i] ^ t[CRYPT_SM4_BLOCKSIZE + i]; } } } int32_t SM4_XTS_De(uint8_t* plain, const uint8_t* cipher, const uint32_t* dataRk, const uint8_t* tweak, const uint32_t dataLen) { uint32_t j; uint32_t left; uint8_t t[CRYPT_SM4_BLOCKSIZE_16] = {0}; uint8_t x[CRYPT_SM4_BLOCKSIZE_16] = {0}; if (dataLen < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); // need push error code for error point return CRYPT_SM4_ERR_MSG_LEN; } left = dataLen % CRYPT_SM4_BLOCKSIZE_16; // MODES_XTS_Ctrl has TW = Enc_K2(iv) done (void)memcpy_s(t, CRYPT_SM4_BLOCKSIZE_16, tweak, CRYPT_SM4_BLOCKSIZE); if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) { SM4_XTS_Encrypt_Blocks(cipher, plain, dataLen, dataRk, t); } if (left != 0) { if (dataLen >= CRYPT_SM4_BLOCKSIZE_16) { SM4_XTS_Calculate_Tweak(t, LAST_BLOCK_HEAD); for (j = 0; j < CRYPT_SM4_BLOCKSIZE; j++) { t[j + CRYPT_SM4_BLOCKSIZE] = t[j + LAST_BLOCK_HEAD]; } } SM4_XTS_Decrypt_Helper(left, dataLen, t, x, plain, cipher, dataRk); } return CRYPT_SUCCESS; } // key[0..16]: data key // key[16..32]: tweak key int32_t CRYPT_SM4_XTS_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { CRYPT_SM4_Ctx *tmk = NULL; if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != XTS_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } if (memcmp(key, key + CRYPT_SM4_BLOCKSIZE, CRYPT_SM4_BLOCKSIZE) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_UNSAFE_KEY); return CRYPT_SM4_UNSAFE_KEY; } tmk = (CRYPT_SM4_Ctx *)&ctx[1]; SM4_SetEncKey(key, ctx->rk); SM4_SetEncKey(key + CRYPT_SM4_BLOCKSIZE, tmk->rk); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_XTS_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { CRYPT_SM4_Ctx *tmk = NULL; if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != XTS_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } if (memcmp(key, key + CRYPT_SM4_BLOCKSIZE, CRYPT_SM4_BLOCKSIZE) == 0) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_UNSAFE_KEY); return CRYPT_SM4_UNSAFE_KEY; } tmk = (CRYPT_SM4_Ctx *)&ctx[1]; SM4_SetDecKey(key, ctx->rk); SM4_SetEncKey(key + CRYPT_SM4_BLOCKSIZE, tmk->rk); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_XTS_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL || iv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return SM4_XTS_En(out, in, ctx->rk, iv, len); } int32_t CRYPT_SM4_XTS_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL || iv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return SM4_XTS_De(out, in, ctx->rk, iv, len); } int32_t CRYPT_SM4_SetEncryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != SM4_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } SM4_SetEncKey(key, ctx->rk); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_SetDecryptKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != SM4_KEY_LEN) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } SM4_SetDecKey(key, ctx->rk); return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_ECB int32_t SM4_ECB_Crypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } SM4_ECB_Encrypt(in, out, len, ctx->rk); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_ECB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return SM4_ECB_Crypt(ctx, in, out, len); } int32_t CRYPT_SM4_ECB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return SM4_ECB_Crypt(ctx, in, out, len); } #endif #ifdef HITLS_CRYPTO_CBC int32_t CRYPT_SM4_CBC_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } SM4_CBC_Encrypt(in, out, len, ctx->rk, iv, 1); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_CBC_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_MSG_LEN); return CRYPT_SM4_ERR_MSG_LEN; } SM4_CBC_Encrypt(in, out, len, ctx->rk, iv, 0); return CRYPT_SUCCESS; } #endif #ifdef HITLS_CRYPTO_OFB int32_t SM4_OFB_Crypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int tmp = *offset; SM4_OFB_Encrypt(in, out, len, ctx->rk, iv, &tmp); *offset = (uint8_t)tmp; return CRYPT_SUCCESS; } int32_t CRYPT_SM4_OFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset) { return SM4_OFB_Crypt(ctx, in, out, len, iv, offset); } int32_t CRYPT_SM4_OFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset) { return SM4_OFB_Crypt(ctx, in, out, len, iv, offset); } #endif #ifdef HITLS_CRYPTO_CFB int32_t CRYPT_SM4_CFB_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int tmp = *offset; SM4_CFB128_Encrypt(in, out, len, ctx->rk, iv, &tmp); *offset = (uint8_t)tmp; return CRYPT_SUCCESS; } int32_t CRYPT_SM4_CFB_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv, uint8_t *offset) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int tmp = *offset; SM4_CFB128_Decrypt(in, out, len, ctx->rk, iv, &tmp); *offset = (uint8_t)tmp; return CRYPT_SUCCESS; } #endif #if defined(HITLS_CRYPTO_CTR) || defined(HITLS_CRYPTO_GCM) int32_t CRYPT_SM4_CTR_Encrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } SM4_CTR_EncryptBlocks(in, out, len, ctx->rk, iv); return CRYPT_SUCCESS; } int32_t CRYPT_SM4_CTR_Decrypt(CRYPT_SM4_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, uint8_t *iv) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } SM4_CTR_EncryptBlocks(in, out, len, ctx->rk, iv); return CRYPT_SUCCESS; } #endif #endif /* HITLS_CRYPTO_SM4 */
2301_79861745/bench_create
crypto/sm4/src/crypt_sm4_x86_64.c
C
unknown
14,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. */ #ifndef CRYPT_SM4_X86_64_H #define CRYPT_SM4_X86_64_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_SM4 #include <stdint.h> void SM4_SetEncKey(const uint8_t *key, uint32_t *rk); void SM4_SetDecKey(const uint8_t *key, uint32_t *rk); void SM4_Encrypt(const uint8_t *in, uint8_t *out, const uint32_t *key); #define SM4_Decrypt SM4_Encrypt // SM4 XTS void SM4_XTS_Encrypt_Blocks(const uint8_t *in, uint8_t *out, uint32_t len, const uint32_t *key, uint8_t *t); void SM4_ECB_Encrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key); void SM4_CBC_Encrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, const int enc); void SM4_OFB_Encrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, int *num); void SM4_CFB128_Encrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, int *num); void SM4_CFB128_Decrypt(const uint8_t *in, uint8_t *out, uint64_t len, const uint32_t *key, uint8_t *iv, int *num); void SM4_CTR_EncryptBlocks(const uint8_t *in, uint8_t *out, uint64_t blocks, const uint32_t *key, const uint8_t *iv); #endif /* HITLS_CRYPTO_SM4 */ #endif
2301_79861745/bench_create
crypto/sm4/src/crypt_sm4_x86_64.h
C
unknown
1,786
/* * 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_SM4 #include <stdlib.h> #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "crypt_sm4.h" /* System parameter FK (originating GB/T 32907-2016 7.3 b or GM/T 0002-2012 7.3 2) */ static const uint32_t FK[] = {0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc}; /* Fixed parameter CK (originating GB/T 32907-2016 7.3 c or GM/T 0002-2012 7.3 3) */ static const uint32_t CK[] = { 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279, }; /** * <<<: Cyclic shift to the left * ⊕: XOR * S-box: (originating GB/T 32907-2016 6.2 a or GM/T 0002-2012 6.2 1) * LE(B) = B⊕(B <<< 13)⊕(B <<< 23) * KBOX_0[i] = LE(SBOX[i]) */ static const uint32_t KBOX_0[] = { 0x6b1ac0d6, 0x48120090, 0x749d20e9, 0x7f1fc0fe, 0x661980cc, 0x709c20e1, 0x1e87a03d, 0x5b96e0b7, 0x0b02c016, 0x5b16c0b6, 0x0a028014, 0x611840c2, 0x14050028, 0x7d9f60fb, 0x1605802c, 0x0280a005, 0x1585602b, 0x338ce067, 0x4d13409a, 0x3b0ec076, 0x1505402a, 0x5f17c0be, 0x02008004, 0x619860c3, 0x551540aa, 0x22088044, 0x09826013, 0x1304c026, 0x24892049, 0x4310c086, 0x0300c006, 0x4c932099, 0x4e13809c, 0x21084042, 0x280a0050, 0x7a1e80f4, 0x48922091, 0x779de0ef, 0x4c130098, 0x3d0f407a, 0x19866033, 0x2a0a8054, 0x0581600b, 0x21886043, 0x769da0ed, 0x6799e0cf, 0x561580ac, 0x310c4062, 0x721c80e4, 0x599660b3, 0x0e03801c, 0x549520a9, 0x649920c9, 0x04010008, 0x741d00e8, 0x4a92a095, 0x40100080, 0x6f9be0df, 0x4a128094, 0x7d1f40fa, 0x3a8ea075, 0x4791e08f, 0x1f87e03f, 0x5314c0a6, 0x2388e047, 0x0380e007, 0x5394e0a7, 0x7e1f80fc, 0x799e60f3, 0x398e6073, 0x0b82e017, 0x5d1740ba, 0x41906083, 0x2c8b2059, 0x1e07803c, 0x0c832019, 0x731cc0e6, 0x4290a085, 0x2789e04f, 0x541500a8, 0x340d0068, 0x358d606b, 0x40902081, 0x591640b2, 0x388e2071, 0x320c8064, 0x6d1b40da, 0x4591608b, 0x7c1f00f8, 0x759d60eb, 0x0781e00f, 0x2589604b, 0x380e0070, 0x2b0ac056, 0x4e93a09d, 0x1a86a035, 0x0f03c01e, 0x12048024, 0x0701c00e, 0x2f0bc05e, 0x318c6063, 0x2c0b0058, 0x689a20d1, 0x511440a2, 0x1284a025, 0x11044022, 0x3e0f807c, 0x1d87603b, 0x00802001, 0x10842021, 0x3c0f0078, 0x4390e087, 0x6a1a80d4, 0x00000000, 0x2308c046, 0x2b8ae057, 0x4f93e09f, 0x699a60d3, 0x1384e027, 0x290a4052, 0x2609804c, 0x1b06c036, 0x01004002, 0x739ce0e7, 0x501400a0, 0x621880c4, 0x641900c8, 0x4f13c09e, 0x751d40ea, 0x5f97e0bf, 0x4511408a, 0x691a40d2, 0x20080040, 0x6398e0c7, 0x1c070038, 0x5a96a0b5, 0x519460a3, 0x7b9ee0f7, 0x791e40f2, 0x6719c0ce, 0x7c9f20f9, 0x308c2061, 0x0a82a015, 0x509420a1, 0x701c00e0, 0x5715c0ae, 0x2e8ba05d, 0x521480a4, 0x4d93609b, 0x1a068034, 0x0d03401a, 0x2a8aa055, 0x5695a0ad, 0x49926093, 0x19064032, 0x18060030, 0x7a9ea0f5, 0x4611808c, 0x589620b1, 0x719c60e3, 0x0e83a01d, 0x7b1ec0f6, 0x711c40e2, 0x1705c02e, 0x41104082, 0x330cc066, 0x651940ca, 0x300c0060, 0x601800c0, 0x14852029, 0x11846023, 0x559560ab, 0x0681a00d, 0x298a6053, 0x2709c04e, 0x378de06f, 0x6a9aa0d5, 0x6d9b60db, 0x1b86e037, 0x2288a045, 0x6f1bc0de, 0x7e9fa0fd, 0x4711c08e, 0x1785e02f, 0x01806003, 0x7f9fe0ff, 0x350d406a, 0x390e4072, 0x368da06d, 0x360d806c, 0x2d8b605b, 0x288a2051, 0x4691a08d, 0x0d83601b, 0x5795e0af, 0x49124092, 0x5d9760bb, 0x6e9ba0dd, 0x5e1780bc, 0x3f8fe07f, 0x08822011, 0x6c9b20d9, 0x2e0b805c, 0x20882041, 0x0f83e01f, 0x08020010, 0x2d0b405a, 0x6c1b00d8, 0x0501400a, 0x609820c1, 0x18862031, 0x44110088, 0x5294a0a5, 0x6699a0cd, 0x3d8f607b, 0x5e97a0bd, 0x1685a02d, 0x3a0e8074, 0x681a00d0, 0x09024012, 0x5c1700b8, 0x729ca0e5, 0x5a1680b4, 0x581600b0, 0x44912089, 0x348d2069, 0x4b92e097, 0x2509404a, 0x0601800c, 0x4b12c096, 0x3b8ee077, 0x3f0fc07e, 0x328ca065, 0x5c9720b9, 0x789e20f1, 0x04812009, 0x6298a0c5, 0x370dc06e, 0x6318c0c6, 0x42108084, 0x0c030018, 0x781e00f0, 0x3e8fa07d, 0x761d80ec, 0x1d07403a, 0x6e1b80dc, 0x2689a04d, 0x10040020, 0x3c8f2079, 0x771dc0ee, 0x2f8be05f, 0x1f07c03e, 0x6b9ae0d7, 0x659960cb, 0x1c872039, 0x24090048, }; /* KBOX_1[i] = KBOX_0[i] <<< 8 */ static const uint32_t KBOX_1[] = { 0x1ac0d66b, 0x12009048, 0x9d20e974, 0x1fc0fe7f, 0x1980cc66, 0x9c20e170, 0x87a03d1e, 0x96e0b75b, 0x02c0160b, 0x16c0b65b, 0x0280140a, 0x1840c261, 0x05002814, 0x9f60fb7d, 0x05802c16, 0x80a00502, 0x85602b15, 0x8ce06733, 0x13409a4d, 0x0ec0763b, 0x05402a15, 0x17c0be5f, 0x00800402, 0x9860c361, 0x1540aa55, 0x08804422, 0x82601309, 0x04c02613, 0x89204924, 0x10c08643, 0x00c00603, 0x9320994c, 0x13809c4e, 0x08404221, 0x0a005028, 0x1e80f47a, 0x92209148, 0x9de0ef77, 0x1300984c, 0x0f407a3d, 0x86603319, 0x0a80542a, 0x81600b05, 0x88604321, 0x9da0ed76, 0x99e0cf67, 0x1580ac56, 0x0c406231, 0x1c80e472, 0x9660b359, 0x03801c0e, 0x9520a954, 0x9920c964, 0x01000804, 0x1d00e874, 0x92a0954a, 0x10008040, 0x9be0df6f, 0x1280944a, 0x1f40fa7d, 0x8ea0753a, 0x91e08f47, 0x87e03f1f, 0x14c0a653, 0x88e04723, 0x80e00703, 0x94e0a753, 0x1f80fc7e, 0x9e60f379, 0x8e607339, 0x82e0170b, 0x1740ba5d, 0x90608341, 0x8b20592c, 0x07803c1e, 0x8320190c, 0x1cc0e673, 0x90a08542, 0x89e04f27, 0x1500a854, 0x0d006834, 0x8d606b35, 0x90208140, 0x1640b259, 0x8e207138, 0x0c806432, 0x1b40da6d, 0x91608b45, 0x1f00f87c, 0x9d60eb75, 0x81e00f07, 0x89604b25, 0x0e007038, 0x0ac0562b, 0x93a09d4e, 0x86a0351a, 0x03c01e0f, 0x04802412, 0x01c00e07, 0x0bc05e2f, 0x8c606331, 0x0b00582c, 0x9a20d168, 0x1440a251, 0x84a02512, 0x04402211, 0x0f807c3e, 0x87603b1d, 0x80200100, 0x84202110, 0x0f00783c, 0x90e08743, 0x1a80d46a, 0x00000000, 0x08c04623, 0x8ae0572b, 0x93e09f4f, 0x9a60d369, 0x84e02713, 0x0a405229, 0x09804c26, 0x06c0361b, 0x00400201, 0x9ce0e773, 0x1400a050, 0x1880c462, 0x1900c864, 0x13c09e4f, 0x1d40ea75, 0x97e0bf5f, 0x11408a45, 0x1a40d269, 0x08004020, 0x98e0c763, 0x0700381c, 0x96a0b55a, 0x9460a351, 0x9ee0f77b, 0x1e40f279, 0x19c0ce67, 0x9f20f97c, 0x8c206130, 0x82a0150a, 0x9420a150, 0x1c00e070, 0x15c0ae57, 0x8ba05d2e, 0x1480a452, 0x93609b4d, 0x0680341a, 0x03401a0d, 0x8aa0552a, 0x95a0ad56, 0x92609349, 0x06403219, 0x06003018, 0x9ea0f57a, 0x11808c46, 0x9620b158, 0x9c60e371, 0x83a01d0e, 0x1ec0f67b, 0x1c40e271, 0x05c02e17, 0x10408241, 0x0cc06633, 0x1940ca65, 0x0c006030, 0x1800c060, 0x85202914, 0x84602311, 0x9560ab55, 0x81a00d06, 0x8a605329, 0x09c04e27, 0x8de06f37, 0x9aa0d56a, 0x9b60db6d, 0x86e0371b, 0x88a04522, 0x1bc0de6f, 0x9fa0fd7e, 0x11c08e47, 0x85e02f17, 0x80600301, 0x9fe0ff7f, 0x0d406a35, 0x0e407239, 0x8da06d36, 0x0d806c36, 0x8b605b2d, 0x8a205128, 0x91a08d46, 0x83601b0d, 0x95e0af57, 0x12409249, 0x9760bb5d, 0x9ba0dd6e, 0x1780bc5e, 0x8fe07f3f, 0x82201108, 0x9b20d96c, 0x0b805c2e, 0x88204120, 0x83e01f0f, 0x02001008, 0x0b405a2d, 0x1b00d86c, 0x01400a05, 0x9820c160, 0x86203118, 0x11008844, 0x94a0a552, 0x99a0cd66, 0x8f607b3d, 0x97a0bd5e, 0x85a02d16, 0x0e80743a, 0x1a00d068, 0x02401209, 0x1700b85c, 0x9ca0e572, 0x1680b45a, 0x1600b058, 0x91208944, 0x8d206934, 0x92e0974b, 0x09404a25, 0x01800c06, 0x12c0964b, 0x8ee0773b, 0x0fc07e3f, 0x8ca06532, 0x9720b95c, 0x9e20f178, 0x81200904, 0x98a0c562, 0x0dc06e37, 0x18c0c663, 0x10808442, 0x0300180c, 0x1e00f078, 0x8fa07d3e, 0x1d80ec76, 0x07403a1d, 0x1b80dc6e, 0x89a04d26, 0x04002010, 0x8f20793c, 0x1dc0ee77, 0x8be05f2f, 0x07c03e1f, 0x9ae0d76b, 0x9960cb65, 0x8720391c, 0x09004824, }; /* KBOX_2[i] = KBOX_0[i] <<< 16 */ static const uint32_t KBOX_2[] = { 0xc0d66b1a, 0x00904812, 0x20e9749d, 0xc0fe7f1f, 0x80cc6619, 0x20e1709c, 0xa03d1e87, 0xe0b75b96, 0xc0160b02, 0xc0b65b16, 0x80140a02, 0x40c26118, 0x00281405, 0x60fb7d9f, 0x802c1605, 0xa0050280, 0x602b1585, 0xe067338c, 0x409a4d13, 0xc0763b0e, 0x402a1505, 0xc0be5f17, 0x80040200, 0x60c36198, 0x40aa5515, 0x80442208, 0x60130982, 0xc0261304, 0x20492489, 0xc0864310, 0xc0060300, 0x20994c93, 0x809c4e13, 0x40422108, 0x0050280a, 0x80f47a1e, 0x20914892, 0xe0ef779d, 0x00984c13, 0x407a3d0f, 0x60331986, 0x80542a0a, 0x600b0581, 0x60432188, 0xa0ed769d, 0xe0cf6799, 0x80ac5615, 0x4062310c, 0x80e4721c, 0x60b35996, 0x801c0e03, 0x20a95495, 0x20c96499, 0x00080401, 0x00e8741d, 0xa0954a92, 0x00804010, 0xe0df6f9b, 0x80944a12, 0x40fa7d1f, 0xa0753a8e, 0xe08f4791, 0xe03f1f87, 0xc0a65314, 0xe0472388, 0xe0070380, 0xe0a75394, 0x80fc7e1f, 0x60f3799e, 0x6073398e, 0xe0170b82, 0x40ba5d17, 0x60834190, 0x20592c8b, 0x803c1e07, 0x20190c83, 0xc0e6731c, 0xa0854290, 0xe04f2789, 0x00a85415, 0x0068340d, 0x606b358d, 0x20814090, 0x40b25916, 0x2071388e, 0x8064320c, 0x40da6d1b, 0x608b4591, 0x00f87c1f, 0x60eb759d, 0xe00f0781, 0x604b2589, 0x0070380e, 0xc0562b0a, 0xa09d4e93, 0xa0351a86, 0xc01e0f03, 0x80241204, 0xc00e0701, 0xc05e2f0b, 0x6063318c, 0x00582c0b, 0x20d1689a, 0x40a25114, 0xa0251284, 0x40221104, 0x807c3e0f, 0x603b1d87, 0x20010080, 0x20211084, 0x00783c0f, 0xe0874390, 0x80d46a1a, 0x00000000, 0xc0462308, 0xe0572b8a, 0xe09f4f93, 0x60d3699a, 0xe0271384, 0x4052290a, 0x804c2609, 0xc0361b06, 0x40020100, 0xe0e7739c, 0x00a05014, 0x80c46218, 0x00c86419, 0xc09e4f13, 0x40ea751d, 0xe0bf5f97, 0x408a4511, 0x40d2691a, 0x00402008, 0xe0c76398, 0x00381c07, 0xa0b55a96, 0x60a35194, 0xe0f77b9e, 0x40f2791e, 0xc0ce6719, 0x20f97c9f, 0x2061308c, 0xa0150a82, 0x20a15094, 0x00e0701c, 0xc0ae5715, 0xa05d2e8b, 0x80a45214, 0x609b4d93, 0x80341a06, 0x401a0d03, 0xa0552a8a, 0xa0ad5695, 0x60934992, 0x40321906, 0x00301806, 0xa0f57a9e, 0x808c4611, 0x20b15896, 0x60e3719c, 0xa01d0e83, 0xc0f67b1e, 0x40e2711c, 0xc02e1705, 0x40824110, 0xc066330c, 0x40ca6519, 0x0060300c, 0x00c06018, 0x20291485, 0x60231184, 0x60ab5595, 0xa00d0681, 0x6053298a, 0xc04e2709, 0xe06f378d, 0xa0d56a9a, 0x60db6d9b, 0xe0371b86, 0xa0452288, 0xc0de6f1b, 0xa0fd7e9f, 0xc08e4711, 0xe02f1785, 0x60030180, 0xe0ff7f9f, 0x406a350d, 0x4072390e, 0xa06d368d, 0x806c360d, 0x605b2d8b, 0x2051288a, 0xa08d4691, 0x601b0d83, 0xe0af5795, 0x40924912, 0x60bb5d97, 0xa0dd6e9b, 0x80bc5e17, 0xe07f3f8f, 0x20110882, 0x20d96c9b, 0x805c2e0b, 0x20412088, 0xe01f0f83, 0x00100802, 0x405a2d0b, 0x00d86c1b, 0x400a0501, 0x20c16098, 0x20311886, 0x00884411, 0xa0a55294, 0xa0cd6699, 0x607b3d8f, 0xa0bd5e97, 0xa02d1685, 0x80743a0e, 0x00d0681a, 0x40120902, 0x00b85c17, 0xa0e5729c, 0x80b45a16, 0x00b05816, 0x20894491, 0x2069348d, 0xe0974b92, 0x404a2509, 0x800c0601, 0xc0964b12, 0xe0773b8e, 0xc07e3f0f, 0xa065328c, 0x20b95c97, 0x20f1789e, 0x20090481, 0xa0c56298, 0xc06e370d, 0xc0c66318, 0x80844210, 0x00180c03, 0x00f0781e, 0xa07d3e8f, 0x80ec761d, 0x403a1d07, 0x80dc6e1b, 0xa04d2689, 0x00201004, 0x20793c8f, 0xc0ee771d, 0xe05f2f8b, 0xc03e1f07, 0xe0d76b9a, 0x60cb6599, 0x20391c87, 0x00482409, }; /* KBOX_3[i] = KBOX_0[i] <<< 24 */ static const uint32_t KBOX_3[] = { 0xd66b1ac0, 0x90481200, 0xe9749d20, 0xfe7f1fc0, 0xcc661980, 0xe1709c20, 0x3d1e87a0, 0xb75b96e0, 0x160b02c0, 0xb65b16c0, 0x140a0280, 0xc2611840, 0x28140500, 0xfb7d9f60, 0x2c160580, 0x050280a0, 0x2b158560, 0x67338ce0, 0x9a4d1340, 0x763b0ec0, 0x2a150540, 0xbe5f17c0, 0x04020080, 0xc3619860, 0xaa551540, 0x44220880, 0x13098260, 0x261304c0, 0x49248920, 0x864310c0, 0x060300c0, 0x994c9320, 0x9c4e1380, 0x42210840, 0x50280a00, 0xf47a1e80, 0x91489220, 0xef779de0, 0x984c1300, 0x7a3d0f40, 0x33198660, 0x542a0a80, 0x0b058160, 0x43218860, 0xed769da0, 0xcf6799e0, 0xac561580, 0x62310c40, 0xe4721c80, 0xb3599660, 0x1c0e0380, 0xa9549520, 0xc9649920, 0x08040100, 0xe8741d00, 0x954a92a0, 0x80401000, 0xdf6f9be0, 0x944a1280, 0xfa7d1f40, 0x753a8ea0, 0x8f4791e0, 0x3f1f87e0, 0xa65314c0, 0x472388e0, 0x070380e0, 0xa75394e0, 0xfc7e1f80, 0xf3799e60, 0x73398e60, 0x170b82e0, 0xba5d1740, 0x83419060, 0x592c8b20, 0x3c1e0780, 0x190c8320, 0xe6731cc0, 0x854290a0, 0x4f2789e0, 0xa8541500, 0x68340d00, 0x6b358d60, 0x81409020, 0xb2591640, 0x71388e20, 0x64320c80, 0xda6d1b40, 0x8b459160, 0xf87c1f00, 0xeb759d60, 0x0f0781e0, 0x4b258960, 0x70380e00, 0x562b0ac0, 0x9d4e93a0, 0x351a86a0, 0x1e0f03c0, 0x24120480, 0x0e0701c0, 0x5e2f0bc0, 0x63318c60, 0x582c0b00, 0xd1689a20, 0xa2511440, 0x251284a0, 0x22110440, 0x7c3e0f80, 0x3b1d8760, 0x01008020, 0x21108420, 0x783c0f00, 0x874390e0, 0xd46a1a80, 0x00000000, 0x462308c0, 0x572b8ae0, 0x9f4f93e0, 0xd3699a60, 0x271384e0, 0x52290a40, 0x4c260980, 0x361b06c0, 0x02010040, 0xe7739ce0, 0xa0501400, 0xc4621880, 0xc8641900, 0x9e4f13c0, 0xea751d40, 0xbf5f97e0, 0x8a451140, 0xd2691a40, 0x40200800, 0xc76398e0, 0x381c0700, 0xb55a96a0, 0xa3519460, 0xf77b9ee0, 0xf2791e40, 0xce6719c0, 0xf97c9f20, 0x61308c20, 0x150a82a0, 0xa1509420, 0xe0701c00, 0xae5715c0, 0x5d2e8ba0, 0xa4521480, 0x9b4d9360, 0x341a0680, 0x1a0d0340, 0x552a8aa0, 0xad5695a0, 0x93499260, 0x32190640, 0x30180600, 0xf57a9ea0, 0x8c461180, 0xb1589620, 0xe3719c60, 0x1d0e83a0, 0xf67b1ec0, 0xe2711c40, 0x2e1705c0, 0x82411040, 0x66330cc0, 0xca651940, 0x60300c00, 0xc0601800, 0x29148520, 0x23118460, 0xab559560, 0x0d0681a0, 0x53298a60, 0x4e2709c0, 0x6f378de0, 0xd56a9aa0, 0xdb6d9b60, 0x371b86e0, 0x452288a0, 0xde6f1bc0, 0xfd7e9fa0, 0x8e4711c0, 0x2f1785e0, 0x03018060, 0xff7f9fe0, 0x6a350d40, 0x72390e40, 0x6d368da0, 0x6c360d80, 0x5b2d8b60, 0x51288a20, 0x8d4691a0, 0x1b0d8360, 0xaf5795e0, 0x92491240, 0xbb5d9760, 0xdd6e9ba0, 0xbc5e1780, 0x7f3f8fe0, 0x11088220, 0xd96c9b20, 0x5c2e0b80, 0x41208820, 0x1f0f83e0, 0x10080200, 0x5a2d0b40, 0xd86c1b00, 0x0a050140, 0xc1609820, 0x31188620, 0x88441100, 0xa55294a0, 0xcd6699a0, 0x7b3d8f60, 0xbd5e97a0, 0x2d1685a0, 0x743a0e80, 0xd0681a00, 0x12090240, 0xb85c1700, 0xe5729ca0, 0xb45a1680, 0xb0581600, 0x89449120, 0x69348d20, 0x974b92e0, 0x4a250940, 0x0c060180, 0x964b12c0, 0x773b8ee0, 0x7e3f0fc0, 0x65328ca0, 0xb95c9720, 0xf1789e20, 0x09048120, 0xc56298a0, 0x6e370dc0, 0xc66318c0, 0x84421080, 0x180c0300, 0xf0781e00, 0x7d3e8fa0, 0xec761d80, 0x3a1d0740, 0xdc6e1b80, 0x4d2689a0, 0x20100400, 0x793c8f20, 0xee771dc0, 0x5f2f8be0, 0x3e1f07c0, 0xd76b9ae0, 0xcb659960, 0x391c8720, 0x48240900, }; #define KROUND(t, k0, k1, k2, k3, ck, sbox, rki) \ do { \ (t) = (k1) ^ (k2) ^ (k3) ^ (ck); \ (k0) ^= (sbox##_3)[((t) >> 24) & 0xff]; \ (k0) ^= (sbox##_2)[((t) >> 16) & 0xff]; \ (k0) ^= (sbox##_1)[((t) >> 8) & 0xff]; \ (k0) ^= (sbox##_0)[(t) & 0xff]; \ (rki) = (k0); \ } while (0) /* Generate a round key */ #define KROUND_FUNCTION(t, k0, k1, k2, k3, sbox, rk) \ for (int i = 0; i < 32; i += 4) { \ KROUND((t), (k0), (k1), (k2), (k3), CK[(i) + 0], sbox, (rk)[(i) + 0]); \ KROUND((t), (k1), (k2), (k3), (k0), CK[(i) + 1], sbox, (rk)[(i) + 1]); \ KROUND((t), (k2), (k3), (k0), (k1), CK[(i) + 2], sbox, (rk)[(i) + 2]); \ KROUND((t), (k3), (k0), (k1), (k2), CK[(i) + 3], sbox, (rk)[(i) + 3]); \ } int32_t CRYPT_SM4_SetKey(CRYPT_SM4_Ctx *ctx, const uint8_t *key, uint32_t keyLen) { if (ctx == NULL || key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (keyLen != CRYPT_SM4_BLOCKSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_SM4_ERR_KEY_LEN); return CRYPT_SM4_ERR_KEY_LEN; } volatile uint32_t k0, k1, k2, k3; volatile uint32_t t; k0 = GET_UINT32_BE(key, 0) ^ FK[0]; // k0: 4 bytes starting from the 0th index of the key⊕FK[0] k1 = GET_UINT32_BE(key, 4) ^ FK[1]; // k1: 4 bytes starting from the 4th index of the key⊕FK[1] k2 = GET_UINT32_BE(key, 8) ^ FK[2]; // k2: 4 bytes starting from the 8th index of the key⊕FK[2] k3 = GET_UINT32_BE(key, 12) ^ FK[3]; // k3: 4 bytes starting from the 12th index of the key⊕FK[3] KROUND_FUNCTION(t, k0, k1, k2, k3, KBOX, ctx->rk); k0 = 0; k1 = 0; k2 = 0; k3 = 0; t = 0; return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_SM4
2301_79861745/bench_create
crypto/sm4/src/sm4_key.c
C
unknown
16,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. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) #include "crypt_local_types.h" #include "crypt_utils.h" typedef struct { CRYPT_MD_AlgId id; uint32_t mdSize; } CRYPT_MdInfo; uint32_t CRYPT_GetMdSizeById(CRYPT_MD_AlgId id) { // need synchronize with enum CRYPT_MD_AlgId static CRYPT_MdInfo mdInfo[] = { {.id = CRYPT_MD_MD5, .mdSize = 16}, {.id = CRYPT_MD_SHA1, .mdSize = 20}, {.id = CRYPT_MD_SHA224, .mdSize = 28}, {.id = CRYPT_MD_SHA256, .mdSize = 32}, {.id = CRYPT_MD_SHA384, .mdSize = 48}, {.id = CRYPT_MD_SHA512, .mdSize = 64}, {.id = CRYPT_MD_SHA3_224, .mdSize = 28}, {.id = CRYPT_MD_SHA3_256, .mdSize = 32}, {.id = CRYPT_MD_SHA3_384, .mdSize = 48}, {.id = CRYPT_MD_SHA3_512, .mdSize = 64}, {.id = CRYPT_MD_SHAKE128, .mdSize = 0}, {.id = CRYPT_MD_SHAKE256, .mdSize = 0}, {.id = CRYPT_MD_SM3, .mdSize = 32}, {.id = CRYPT_MD_MAX, .mdSize = 0}, }; uint32_t i = 0; while (mdInfo[i].id != CRYPT_MD_MAX) { if (mdInfo[i].id == id) { return mdInfo[i].mdSize; } i++; } return 0; } #endif
2301_79861745/bench_create
crypto/util/crypt_util_algId.c
C
unknown
1,762
/* * 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_PROVIDER) && defined(HITLS_CRYPTO_MD) #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_types.h" #include "crypt_utils.h" #include "bsl_params.h" int32_t CRYPT_MdCommonGetParam(uint16_t mdSize, uint16_t mdBlockSize, BSL_Param *param) { if (param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_INVALID_ARG; BSL_Param *paramPtr = param; while (paramPtr->key != 0) { if (paramPtr->key == CRYPT_PARAM_MD_DIGEST_SIZE) { ret = BSL_PARAM_SetValue(paramPtr, CRYPT_PARAM_MD_DIGEST_SIZE, BSL_PARAM_TYPE_UINT16, &mdSize, sizeof(mdSize)); if (ret != CRYPT_SUCCESS) { return ret; } } else if (paramPtr->key == CRYPT_PARAM_MD_BLOCK_SIZE) { ret = BSL_PARAM_SetValue(paramPtr, CRYPT_PARAM_MD_BLOCK_SIZE, BSL_PARAM_TYPE_UINT16, &mdBlockSize, sizeof(mdBlockSize)); if (ret != CRYPT_SUCCESS) { return ret; } } paramPtr++; } return ret; } #endif
2301_79861745/bench_create
crypto/util/crypt_util_md.c
C
unknown
1,704
/* * 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_EMSA_PSS) || defined(HITLS_CRYPTO_RSAES_OAEP) || defined(HITLS_CRYPTO_SLH_DSA) #include <stdlib.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "crypt_types.h" #include "crypt_eal_md.h" #include "crypt_utils.h" #define UINT32_SIZE 4 #define HASH_MAX_MDSIZE (64) // outlen should be hash len int32_t CRYPT_CalcHash(void *provCtx, const EAL_MdMethod *hashMethod, const CRYPT_ConstData *hashData, uint32_t size, uint8_t *out, uint32_t *outlen) { void *mdCtx = hashMethod->newCtx(provCtx, hashMethod->id); if (mdCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret = hashMethod->init(mdCtx, NULL); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } for (uint32_t i = 0; i < size; i++) { ret = hashMethod->update(mdCtx, hashData[i].data, hashData[i].len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } } ret = hashMethod->final(mdCtx, out, outlen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: hashMethod->freeCtx(mdCtx); return ret; } int32_t CRYPT_Mgf1(void *provCtx, const EAL_MdMethod *hashMethod, const uint8_t *seed, const uint32_t seedLen, uint8_t *mask, uint32_t maskLen) { uint32_t hashLen = hashMethod->mdSize; if (hashLen > HASH_MAX_MDSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_RSA_ERR_INPUT_VALUE); return CRYPT_RSA_ERR_INPUT_VALUE; } uint8_t md[HASH_MAX_MDSIZE]; uint8_t counter[UINT32_SIZE]; const CRYPT_ConstData hashData[] = { {seed, seedLen}, // mgfSeed {counter, sizeof(counter)} // counter }; int32_t ret = CRYPT_RSA_ERR_INPUT_VALUE; uint32_t i, outLen, partLen; for (i = 0, outLen = 0; outLen < maskLen; i++, outLen += partLen) { PUT_UINT32_BE(i, counter, 0); ret = CRYPT_CalcHash(provCtx, hashMethod, hashData, sizeof(hashData) / sizeof(hashData[0]), md, &hashLen); if (ret != CRYPT_SUCCESS) { goto EXIT; } // Output the leading maskLen octets of T as the octet string mask partLen = (outLen + hashLen <= maskLen) ? hashLen : (maskLen - outLen); if (memcpy_s(mask + outLen, maskLen - outLen, md, partLen) != EOK) { ret = CRYPT_SECUREC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } } EXIT: BSL_SAL_CleanseData(md, sizeof(md)); return ret; } #endif
2301_79861745/bench_create
crypto/util/crypt_util_mgf.c
C
unknown
3,162
/* * 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 "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_utils.h" #if defined(HITLS_CRYPTO_PROVIDER) && defined(HITLS_CRYPTO_PKEY) #include "crypt_params_key.h" int32_t CRYPT_GetPkeyProcessParams(BSL_Param *params, CRYPT_EAL_ProcessFuncCb *processCb, void **args) { BSL_Param *processParam = BSL_PARAM_FindParam(params, CRYPT_PARAM_PKEY_PROCESS_FUNC); if (processParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } int32_t ret = BSL_PARAM_GetPtrValue(processParam, CRYPT_PARAM_PKEY_PROCESS_FUNC, BSL_PARAM_TYPE_FUNC_PTR, (void **)processCb, NULL); if (ret != BSL_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (*processCb == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } BSL_Param *argsParam = BSL_PARAM_FindParam(params, CRYPT_PARAM_PKEY_PROCESS_ARGS); if (argsParam != NULL) { GOTO_ERR_IF(BSL_PARAM_GetPtrValue(argsParam, CRYPT_PARAM_PKEY_PROCESS_ARGS, BSL_PARAM_TYPE_CTX_PTR, args, NULL), ret); } ERR: return ret; } #endif #if (defined(HITLS_CRYPTO_DH_CHECK) || defined(HITLS_CRYPTO_DSA_CHECK)) #include "crypt_bn.h" /* * check safe-prime group (no q) FFC private key */ static int32_t FFCSafePrimePrvCheck(const BN_BigNum *x, const BN_BigNum *p) { if (x == NULL || p == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_SUCCESS; uint32_t N = BN_Bits(p); // N: agreed-upon bit length BN_BigNum *max = BN_Create(N); BN_BigNum *one = BN_Create(1); if (max == NULL || one == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto EXIT; } (void)BN_SetLimb(max, 1); (void)BN_SetLimb(one, 1); ret = BN_Lshift(max, max, N); // max = 2^N if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // x >= 1 if (BN_Cmp(one, x) >= 0) { ret = CRYPT_INVALID_KEY; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } (void)BN_SubLimb(max, max, 1); if (BN_Cmp(x, max) > 0) { // x > 2^N - 1 ret = CRYPT_INVALID_KEY; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } EXIT: BN_Destroy(max); BN_Destroy(one); return ret; } int32_t CRYPT_FFC_PrvCheck(const void *x, const void *p, const void *q) { if (x == NULL || p == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (q != NULL) { int32_t ret; BN_BigNum *qTmp = BN_Create(BN_Bits(q)); if (qTmp == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = BN_SubLimb(qTmp, q, 1); if (ret != CRYPT_SUCCESS) { BN_Destroy(qTmp); BSL_ERR_PUSH_ERROR(ret); return ret; } // check 1 <= x <= q - 1 if (BN_IsZero(x) == true || BN_IsNegative(x) == true || BN_Cmp(x, qTmp) > 0) { ret = CRYPT_INVALID_KEY; BSL_ERR_PUSH_ERROR(CRYPT_INVALID_KEY); } BN_Destroy(qTmp); return ret; } return FFCSafePrimePrvCheck(x, p); } /* * SP800-56a 5.6.2.1.4 * for check an FFC key pair is valid. */ int32_t CRYPT_FFC_KeyPairCheck(const void *x, const void *y, const void *p, const void *g) { int32_t ret; if (x == NULL || y == NULL || p == NULL || g == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BN_Mont *mont = BN_MontCreate(p); BN_BigNum *yTmp = BN_Create(BN_Bits(p)); if (yTmp == NULL || mont == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = BN_MontExpConsttime(yTmp, g, x, mont, NULL); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } if (BN_Cmp(yTmp, y) != 0) { ret = CRYPT_PAIRWISE_CHECK_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ERR: BN_Destroy(yTmp); BN_MontDestroy(mont); return ret; } #endif // HITLS_CRYPTO_DH_CHECK || HITLS_CRYPTO_DSA_CHECK #if defined(HITLS_CRYPTO_PROVIDER) && (defined(HITLS_CRYPTO_RSA) || defined(HITLS_CRYPTO_ECDSA) || \ defined(HITLS_CRYPTO_DSA)) #include "securec.h" int32_t CRYPT_PkeySetMdAttr(const char *mdAttr, uint32_t len, char **pkeyMdAttr) { if (mdAttr == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } BSL_SAL_FREE(*pkeyMdAttr); *pkeyMdAttr = BSL_SAL_Malloc(len + 1); // +1 for '\0' if (*pkeyMdAttr == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } (void)memcpy_s(*pkeyMdAttr, len + 1, mdAttr, len); (*pkeyMdAttr)[len] = '\0'; return CRYPT_SUCCESS; } #endif
2301_79861745/bench_create
crypto/util/crypt_util_pkey.c
C
unknown
5,452
/* * 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_CURVE25519) || \ defined(HITLS_CRYPTO_RSA) || defined(HITLS_CRYPTO_BN_RAND) #include <stdlib.h> #include "crypt_errno.h" #include "bsl_err_internal.h" #include "crypt_util_rand.h" static CRYPT_EAL_RandFunc g_randFunc = NULL; static CRYPT_EAL_RandFuncEx g_randFuncEx = NULL; void CRYPT_RandRegist(CRYPT_EAL_RandFunc func) { g_randFunc = func; } CRYPT_EAL_RandFunc CRYPT_RandRegistGet(void) { return g_randFunc; } int32_t CRYPT_Rand(uint8_t *rand, uint32_t randLen) { if (g_randFunc == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NO_REGIST_RAND); return CRYPT_NO_REGIST_RAND; } int32_t ret = g_randFunc(rand, randLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } void CRYPT_RandRegistEx(CRYPT_EAL_RandFuncEx func) { g_randFuncEx = func; } CRYPT_EAL_RandFuncEx CRYPT_RandRegistExGet(void) { return g_randFuncEx; } int32_t CRYPT_RandEx(void *libCtx, uint8_t *rand, uint32_t randLen) { #if defined(HITLS_CRYPTO_PROVIDER) int32_t ret = 0; if (g_randFuncEx != NULL) { ret = g_randFuncEx(libCtx, rand, randLen); } else if (g_randFunc != NULL) { ret = g_randFunc(rand, randLen); } else { ret = CRYPT_NO_REGIST_RAND; } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; #else (void) libCtx; return CRYPT_Rand(rand, randLen); #endif } #endif
2301_79861745/bench_create
crypto/util/crypt_util_rand.c
C
unknown
2,034
/* * 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_XMSS_H #define CRYPT_XMSS_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_XMSS #include <stdint.h> #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ typedef struct SlhDsaCtx CryptXmssCtx; typedef struct HashFuncs XmssHashFuncs; typedef union Adrs XmssAdrs; /** * @brief Allocate XMSS context memory space. * * @retval (CryptXmssCtx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CryptXmssCtx *CRYPT_XMSS_NewCtx(void); // create key structure /** * @brief Allocate XMSS context memory space. * * @param libCtx [IN] Library context * * @retval (CryptXmssCtx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CryptXmssCtx *CRYPT_XMSS_NewCtxEx(void *libCtx); /** * @brief release XMSS key context structure * * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker. */ void CRYPT_XMSS_FreeCtx(CryptXmssCtx *ctx); /** * @brief Generate the XMSS key pair. * * @param ctx [IN/OUT] XMSS context structure * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SUCCESS The key pair is successfully generated. */ int32_t CRYPT_XMSS_Gen(CryptXmssCtx *ctx); /** * @brief Sign data using XMSS * * @param ctx Pointer to the XMSS context * @param algId Algorithm ID * @param data Pointer to the data to sign * @param dataLen Length of the data * @param sign Pointer to the signature * @param signLen Length of the signature */ int32_t CRYPT_XMSS_Sign(CryptXmssCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); /** * @brief Verify data using XMSS * * @param ctx Pointer to the XMSS context * @param algId Algorithm ID * @param data Pointer to the data to verify * @param dataLen Length of the data * @param sign Pointer to the signature * @param signLen Length of the signature */ int32_t CRYPT_XMSS_Verify(const CryptXmssCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); /** * @brief Control function for XMSS * * @param ctx Pointer to the XMSS context * @param opt Option * @param val Value * @param len Length of the value */ int32_t CRYPT_XMSS_Ctrl(CryptXmssCtx *ctx, int32_t opt, void *val, uint32_t len); /** * @brief Get the public key of XMSS * * @param ctx Pointer to the XMSS context * @param para Pointer to the public key */ int32_t CRYPT_XMSS_GetPubKey(const CryptXmssCtx *ctx, BSL_Param *para); /** * @brief Get the private key of XMSS * * @param ctx Pointer to the XMSS context * @param para Pointer to the private key */ int32_t CRYPT_XMSS_GetPrvKey(const CryptXmssCtx *ctx, BSL_Param *para); /** * @brief Set the public key of XMSS * * @param ctx Pointer to the XMSS context * @param para Pointer to the public key */ int32_t CRYPT_XMSS_SetPubKey(CryptXmssCtx *ctx, const BSL_Param *para); /** * @brief Set the private key of XMSS * * @param ctx Pointer to the XMSS context * @param para Pointer to the private key */ int32_t CRYPT_XMSS_SetPrvKey(CryptXmssCtx *ctx, const BSL_Param *para); #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_XMSS #endif // CRYPT_XMSS_H
2301_79861745/bench_create
crypto/xmss/include/crypt_xmss.h
C
unknown
3,967
/* * 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_XMSS #include "securec.h" #include "bsl_sal.h" #include "crypt_errno.h" #include "crypt_algid.h" #include "crypt_util_rand.h" #include "eal_md_local.h" #include "crypt_xmss.h" #include "slh_dsa_local.h" #include "slh_dsa_xmss.h" #include "slh_dsa_hypertree.h" #include "xmss_hash.h" #define XMSS_ADRS_LEN SLH_DSA_ADRS_LEN typedef SlhDsaPara XmssPara; typedef struct { BSL_Param *pubSeed; BSL_Param *pubRoot; } XmssPubKeyParam; typedef struct { BSL_Param *prvIndex; BSL_Param *prvSeed; BSL_Param *prvPrf; BSL_Param *pubSeed; BSL_Param *pubRoot; } XmssPrvKeyParam; /* params isCompressed, a, k, m, secCategory are not used for xmss */ static const XmssPara XmssParaTable[] = { /* { algId, isCompressed, n, h, d, hp, a, k, m, secCategory, pkBytes, sigBytes }, */ { CRYPT_XMSS_SHA2_10_256, 0, 32, 10, 1, 10, 0, 0, 0, 0, 68, 2500 }, { CRYPT_XMSS_SHA2_16_256, 0, 32, 16, 1, 16, 0, 0, 0, 0, 68, 2692 }, { CRYPT_XMSS_SHA2_20_256, 0, 32, 20, 1, 20, 0, 0, 0, 0, 68, 2820 }, { CRYPT_XMSS_SHA2_10_512, 0, 64, 10, 1, 10, 0, 0, 0, 0, 132, 9092 }, { CRYPT_XMSS_SHA2_16_512, 0, 64, 16, 1, 16, 0, 0, 0, 0, 132, 9476 }, { CRYPT_XMSS_SHA2_20_512, 0, 64, 20, 1, 20, 0, 0, 0, 0, 132, 9732 }, { CRYPT_XMSS_SHAKE_10_256, 0, 32, 10, 1, 10, 0, 0, 0, 0, 68, 2500 }, { CRYPT_XMSS_SHAKE_16_256, 0, 32, 16, 1, 16, 0, 0, 0, 0, 68, 2692 }, { CRYPT_XMSS_SHAKE_20_256, 0, 32, 20, 1, 20, 0, 0, 0, 0, 68, 2820 }, { CRYPT_XMSS_SHAKE_10_512, 0, 64, 10, 1, 10, 0, 0, 0, 0, 132, 9092 }, { CRYPT_XMSS_SHAKE_16_512, 0, 64, 16, 1, 16, 0, 0, 0, 0, 132, 9476 }, { CRYPT_XMSS_SHAKE_20_512, 0, 64, 20, 1, 20, 0, 0, 0, 0, 132, 9732 }, { CRYPT_XMSS_SHA2_10_192, 0, 24, 10, 1, 10, 0, 0, 0, 0, 52, 1492 }, { CRYPT_XMSS_SHA2_16_192, 0, 24, 16, 1, 16, 0, 0, 0, 0, 52, 1636 }, { CRYPT_XMSS_SHA2_20_192, 0, 24, 20, 1, 20, 0, 0, 0, 0, 52, 1732 }, { CRYPT_XMSS_SHAKE256_10_256, 0, 32, 10, 1, 10, 0, 0, 0, 0, 68, 2500 }, { CRYPT_XMSS_SHAKE256_16_256, 0, 32, 16, 1, 16, 0, 0, 0, 0, 68, 2692 }, { CRYPT_XMSS_SHAKE256_20_256, 0, 32, 20, 1, 20, 0, 0, 0, 0, 68, 2820 }, { CRYPT_XMSS_SHAKE256_10_192, 0, 24, 10, 1, 10, 0, 0, 0, 0, 52, 1492 }, { CRYPT_XMSS_SHAKE256_16_192, 0, 24, 16, 1, 16, 0, 0, 0, 0, 52, 1636 }, { CRYPT_XMSS_SHAKE256_20_192, 0, 24, 20, 1, 20, 0, 0, 0, 0, 52, 1732 }, { CRYPT_XMSSMT_SHA2_20_2_256, 0, 32, 20, 2, 10, 0, 0, 0, 0, 68, 4963 }, { CRYPT_XMSSMT_SHA2_20_4_256, 0, 32, 20, 4, 5, 0, 0, 0, 0, 68, 9251 }, { CRYPT_XMSSMT_SHA2_40_2_256, 0, 32, 40, 2, 20, 0, 0, 0, 0, 68, 5605 }, { CRYPT_XMSSMT_SHA2_40_4_256, 0, 32, 40, 4, 10, 0, 0, 0, 0, 68, 9893 }, { CRYPT_XMSSMT_SHA2_40_8_256, 0, 32, 40, 8, 5, 0, 0, 0, 0, 68, 18469 }, { CRYPT_XMSSMT_SHA2_60_3_256, 0, 32, 60, 3, 20, 0, 0, 0, 0, 68, 8392 }, { CRYPT_XMSSMT_SHA2_60_6_256, 0, 32, 60, 6, 10, 0, 0, 0, 0, 68, 14824 }, { CRYPT_XMSSMT_SHA2_60_12_256, 0, 32, 60, 12, 5, 0, 0, 0, 0, 68, 27688 }, { CRYPT_XMSSMT_SHA2_20_2_512, 0, 64, 20, 2, 10, 0, 0, 0, 0, 132, 18115 }, { CRYPT_XMSSMT_SHA2_20_4_512, 0, 64, 20, 4, 5, 0, 0, 0, 0, 132, 34883 }, { CRYPT_XMSSMT_SHA2_40_2_512, 0, 64, 40, 2, 20, 0, 0, 0, 0, 132, 19397 }, { CRYPT_XMSSMT_SHA2_40_4_512, 0, 64, 40, 4, 10, 0, 0, 0, 0, 132, 36165 }, { CRYPT_XMSSMT_SHA2_40_8_512, 0, 64, 40, 8, 5, 0, 0, 0, 0, 132, 69701 }, { CRYPT_XMSSMT_SHA2_60_3_512, 0, 64, 60, 3, 20, 0, 0, 0, 0, 132, 29064 }, { CRYPT_XMSSMT_SHA2_60_6_512, 0, 64, 60, 6, 10, 0, 0, 0, 0, 132, 54216 }, { CRYPT_XMSSMT_SHA2_60_12_512, 0, 64, 60, 12, 5, 0, 0, 0, 0, 132, 104520 }, { CRYPT_XMSSMT_SHAKE_20_2_256, 0, 32, 20, 2, 10, 0, 0, 0, 0, 68, 4963 }, { CRYPT_XMSSMT_SHAKE_20_4_256, 0, 32, 20, 4, 5, 0, 0, 0, 0, 68, 9251 }, { CRYPT_XMSSMT_SHAKE_40_2_256, 0, 32, 40, 2, 20, 0, 0, 0, 0, 68, 5605 }, { CRYPT_XMSSMT_SHAKE_40_4_256, 0, 32, 40, 4, 10, 0, 0, 0, 0, 68, 9893 }, { CRYPT_XMSSMT_SHAKE_40_8_256, 0, 32, 40, 8, 5, 0, 0, 0, 0, 68, 18469 }, { CRYPT_XMSSMT_SHAKE_60_3_256, 0, 32, 60, 3, 20, 0, 0, 0, 0, 68, 8392 }, { CRYPT_XMSSMT_SHAKE_60_6_256, 0, 32, 60, 6, 10, 0, 0, 0, 0, 68, 14824 }, { CRYPT_XMSSMT_SHAKE_60_12_256, 0, 32, 60, 12, 5, 0, 0, 0, 0, 68, 27688 }, { CRYPT_XMSSMT_SHAKE_20_2_512, 0, 64, 20, 2, 10, 0, 0, 0, 0, 132, 18115 }, { CRYPT_XMSSMT_SHAKE_20_4_512, 0, 64, 20, 4, 5, 0, 0, 0, 0, 132, 34883 }, { CRYPT_XMSSMT_SHAKE_40_2_512, 0, 64, 40, 2, 20, 0, 0, 0, 0, 132, 19397 }, { CRYPT_XMSSMT_SHAKE_40_4_512, 0, 64, 40, 4, 10, 0, 0, 0, 0, 132, 36165 }, { CRYPT_XMSSMT_SHAKE_40_8_512, 0, 64, 40, 8, 5, 0, 0, 0, 0, 132, 69701 }, { CRYPT_XMSSMT_SHAKE_60_3_512, 0, 64, 60, 3, 20, 0, 0, 0, 0, 132, 29064 }, { CRYPT_XMSSMT_SHAKE_60_6_512, 0, 64, 60, 6, 10, 0, 0, 0, 0, 132, 54216 }, { CRYPT_XMSSMT_SHAKE_60_12_512, 0, 64, 60, 12, 5, 0, 0, 0, 0, 132, 104520 }, { CRYPT_XMSSMT_SHA2_20_2_192, 0, 24, 20, 2, 10, 0, 0, 0, 0, 52, 2955 }, { CRYPT_XMSSMT_SHA2_20_4_192, 0, 24, 20, 4, 5, 0, 0, 0, 0, 52, 5403 }, { CRYPT_XMSSMT_SHA2_40_2_192, 0, 24, 40, 2, 20, 0, 0, 0, 0, 52, 3437 }, { CRYPT_XMSSMT_SHA2_40_4_192, 0, 24, 40, 4, 10, 0, 0, 0, 0, 52, 5885 }, { CRYPT_XMSSMT_SHA2_40_8_192, 0, 24, 40, 8, 5, 0, 0, 0, 0, 52, 10781 }, { CRYPT_XMSSMT_SHA2_60_3_192, 0, 24, 60, 3, 20, 0, 0, 0, 0, 52, 5144 }, { CRYPT_XMSSMT_SHA2_60_6_192, 0, 24, 60, 6, 10, 0, 0, 0, 0, 52, 8816 }, { CRYPT_XMSSMT_SHA2_60_12_192, 0, 24, 60, 12, 5, 0, 0, 0, 0, 52, 16160 }, { CRYPT_XMSSMT_SHAKE256_20_2_256, 0, 32, 20, 2, 10, 0, 0, 0, 0, 68, 4963 }, { CRYPT_XMSSMT_SHAKE256_20_4_256, 0, 32, 20, 4, 5, 0, 0, 0, 0, 68, 9251 }, { CRYPT_XMSSMT_SHAKE256_40_2_256, 0, 32, 40, 2, 20, 0, 0, 0, 0, 68, 5605 }, { CRYPT_XMSSMT_SHAKE256_40_4_256, 0, 32, 40, 4, 10, 0, 0, 0, 0, 68, 9893 }, { CRYPT_XMSSMT_SHAKE256_40_8_256, 0, 32, 40, 8, 5, 0, 0, 0, 0, 68, 18469 }, { CRYPT_XMSSMT_SHAKE256_60_3_256, 0, 32, 60, 3, 20, 0, 0, 0, 0, 68, 8392 }, { CRYPT_XMSSMT_SHAKE256_60_6_256, 0, 32, 60, 6, 10, 0, 0, 0, 0, 68, 14824 }, { CRYPT_XMSSMT_SHAKE256_60_12_256, 0, 32, 60, 12, 5, 0, 0, 0, 0, 68, 27688 }, { CRYPT_XMSSMT_SHAKE256_20_2_192, 0, 24, 20, 2, 10, 0, 0, 0, 0, 52, 2955 }, { CRYPT_XMSSMT_SHAKE256_20_4_192, 0, 24, 20, 4, 5, 0, 0, 0, 0, 52, 5403 }, { CRYPT_XMSSMT_SHAKE256_40_2_192, 0, 24, 40, 2, 20, 0, 0, 0, 0, 52, 3437 }, { CRYPT_XMSSMT_SHAKE256_40_4_192, 0, 24, 40, 4, 10, 0, 0, 0, 0, 52, 5885 }, { CRYPT_XMSSMT_SHAKE256_40_8_192, 0, 24, 40, 8, 5, 0, 0, 0, 0, 52, 10781 }, { CRYPT_XMSSMT_SHAKE256_60_3_192, 0, 24, 60, 3, 20, 0, 0, 0, 0, 52, 5144 }, { CRYPT_XMSSMT_SHAKE256_60_6_192, 0, 24, 60, 6, 10, 0, 0, 0, 0, 52, 8816 }, { CRYPT_XMSSMT_SHAKE256_60_12_192, 0, 24, 60, 12, 5, 0, 0, 0, 0, 52, 16160 }, }; // “X” means xmss static void XAdrsSetLayerAddr(XmssAdrs *adrs, uint32_t layer) { PUT_UINT32_BE(layer, adrs->x.layerAddr, 0); } static void XAdrsSetTreeAddr(XmssAdrs *adrs, uint64_t tree) { PUT_UINT64_BE(tree, adrs->x.treeAddr, 0); } static void XAdrsSetType(XmssAdrs *adrs, AdrsType type) { PUT_UINT32_BE(type, adrs->x.type, 0); (void)memset_s(adrs->x.padding, sizeof(adrs->x.padding), 0, sizeof(adrs->x.padding)); } static void XAdrsSetKeyPairAddr(XmssAdrs *adrs, uint32_t keyPair) { PUT_UINT32_BE(keyPair, adrs->x.padding, 0); } static void XAdrsSetChainAddr(XmssAdrs *adrs, uint32_t chain) { PUT_UINT32_BE(chain, adrs->x.padding, 4); // chain address is 4 bytes, start from 4-th byte } static void XAdrsSetTreeHeight(XmssAdrs *adrs, uint32_t height) { PUT_UINT32_BE(height, adrs->x.padding, 4); // tree height is 4 bytes, start from 4-th byte } static void XAdrsSetHashAddr(XmssAdrs *adrs, uint32_t hash) { PUT_UINT32_BE(hash, adrs->x.padding, 8); // hash address is 4 bytes, start from 8-th byte } static void XAdrsSetTreeIndex(XmssAdrs *adrs, uint32_t index) { PUT_UINT32_BE(index, adrs->x.padding, 8); // tree index is 4 bytes, start from 8-th byte } static void XAdrsSetKeyAndMask(XmssAdrs *adrs, uint32_t KeyAndMask) { PUT_UINT32_BE(KeyAndMask, adrs->x.padding, 12); // KeyAndMask is 4 bytes, start from 12-th byte } static uint32_t XAdrsGetTreeHeight(const XmssAdrs *adrs) { return GET_UINT32_BE(adrs->x.padding, 4); } static uint32_t XAdrsGetTreeIndex(const XmssAdrs *adrs) { return GET_UINT32_BE(adrs->x.padding, 8); // tree index is 4 bytes, start from 8-th byte } static void XAdrsCopyKeyPairAddr(XmssAdrs *adrs, const XmssAdrs *adrs2) { (void)memcpy_s(adrs->x.padding, sizeof(adrs->x.padding), adrs2->x.padding, 4); // key pair address is 4 bytes, start from 4-th byte } static uint32_t XAdrsGetAdrsLen() { return XMSS_ADRS_LEN; } CryptXmssCtx *CRYPT_XMSS_NewCtx(void) { CryptXmssCtx *ctx = (CryptXmssCtx *)BSL_SAL_Calloc(sizeof(CryptXmssCtx), 1); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } ctx->isXmss = true; ctx->para.algId = 0; return ctx; } CryptXmssCtx *CRYPT_XMSS_NewCtxEx(void *libCtx) { CryptXmssCtx *ctx = CRYPT_XMSS_NewCtx(); if (ctx == NULL) { return NULL; } ctx->libCtx = libCtx; return ctx; } void CRYPT_XMSS_FreeCtx(CryptXmssCtx *ctx) { if (ctx == NULL) { return; } BSL_SAL_CleanseData(ctx->prvKey.seed, sizeof(ctx->prvKey.seed)); BSL_SAL_CleanseData(ctx->prvKey.prf, sizeof(ctx->prvKey.prf)); BSL_SAL_Free(ctx); } static bool CheckNotXmssAlgId(int32_t algId) { if (algId > CRYPT_XMSSMT_SHAKE256_60_12_192 || algId < CRYPT_XMSS_SHA2_10_256) { return true; } return false; } int32_t CRYPT_XMSS_Gen(CryptXmssCtx *ctx) { int32_t ret; if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (CheckNotXmssAlgId(ctx->para.algId)) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_ALGID); return CRYPT_XMSS_ERR_INVALID_ALGID; } uint32_t n = ctx->para.n; uint32_t d = ctx->para.d; uint32_t hp = ctx->para.hp; ret = CRYPT_RandEx(ctx->libCtx, ctx->prvKey.seed, n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = CRYPT_RandEx(ctx->libCtx, ctx->prvKey.prf, n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = CRYPT_RandEx(ctx->libCtx, ctx->prvKey.pub.seed, n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } XmssAdrs adrs; (void)memset_s(&adrs, sizeof(XmssAdrs), 0, sizeof(XmssAdrs)); ctx->adrsOps.setLayerAddr(&adrs, d - 1); uint8_t node[MAX_MDSIZE] = {0}; ret = XmssNode(node, 0, hp, &adrs, ctx, NULL, 0); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.pub.root, n, node, n); /* init the private key index to 0 */ ctx->prvKey.index = 0; return CRYPT_SUCCESS; } /* integer to big-endian bytes */ static void U64toBytes(uint8_t *out, uint32_t outlen, uint64_t in) { for (int32_t i = outlen - 1; i >= 0; i--) { out[i] = in & 0xff; in = in >> 8; } } /* big-endian bytes to integer. */ static uint64_t BytestoU64(const uint8_t *in, uint32_t inlen) { uint64_t ret = 0; for (; inlen > 0; in++, inlen--) { ret = ret << 8; ret |= in[0]; } return ret; } static int32_t CRYPT_XMSS_SignInternal(CryptXmssCtx *ctx, const uint8_t *msg, uint32_t msgLen, uint8_t *sig, uint32_t *sigLen) { int32_t ret; uint32_t n = ctx->para.n; uint32_t d = ctx->para.d; uint32_t h = ctx->para.h; uint32_t hp = ctx->para.hp; uint32_t sigBytes = ctx->para.sigBytes; uint64_t index = ctx->prvKey.index; uint64_t treeIdx; uint32_t leafIdx; uint32_t IdxBytes; uint8_t idx[MAX_MDSIZE] = {0}; if (*sigLen < sigBytes) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_SIG_LEN); return CRYPT_XMSS_ERR_INVALID_SIG_LEN; } if (h == 64) { /* we do not use the last signature while total height 64, * otherwisw, index will wrap. */ if (index == ((1ULL << h) - 1)) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_KEY_EXPIRED); return CRYPT_XMSS_ERR_KEY_EXPIRED; } } else { if (index > ((1ULL << h) - 1)) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_KEY_EXPIRED); return CRYPT_XMSS_ERR_KEY_EXPIRED; } } /* increment the private key index */ /* An implementation MUST NOT output the signature before the private key is updated. */ ctx->prvKey.index++; uint32_t offset = 0; uint32_t left = 0; if (d == 1) { /* XMSS, 4-bytes index_bytes*/ IdxBytes = 4; } else { /* XMSSMT, (ceil(h / 8))-bytes index_bytes */ IdxBytes = (h + 7) / 8; } U64toBytes(sig, IdxBytes, index); offset += IdxBytes; PUT_UINT64_BE(index, idx, sizeof(idx) - 8); ret = ctx->hashFuncs.prfmsg(ctx, idx + sizeof(idx) - 32, NULL, 0, sig + offset); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint8_t digest[MAX_MDSIZE] = {0}; ret = ctx->hashFuncs.hmsg(ctx, sig + offset, msg, msgLen, idx + sizeof(idx) - n, digest); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } offset += n; left = *sigLen - offset; leafIdx = index & ((1UL << hp) - 1); treeIdx = index >> hp; ret = HypertreeSign(digest, n, treeIdx, leafIdx, ctx, sig + offset, &left); *sigLen = offset + left; return CRYPT_SUCCESS; } int32_t CRYPT_XMSS_Sign(CryptXmssCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { (void)algId; int32_t ret; if (ctx == NULL || data == NULL || dataLen == 0 || sign == NULL || signLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = CRYPT_XMSS_SignInternal(ctx, data, dataLen, sign, signLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } static int32_t CRYPT_XMSS_VerifyInternal(const CryptXmssCtx *ctx, const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen) { int32_t ret; uint32_t n = ctx->para.n; uint32_t d = ctx->para.d; uint32_t h = ctx->para.h; uint32_t hp = ctx->para.hp; uint32_t sigBytes = ctx->para.sigBytes; uint64_t index; uint64_t treeIdx; uint32_t leafIdx; uint32_t IdxBytes; uint8_t idx[MAX_MDSIZE] = {0}; if (sigLen != sigBytes) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_SIG_LEN); return CRYPT_XMSS_ERR_INVALID_SIG_LEN; } uint32_t offset = 0; if (d == 1) { /* XMSS, 4-bytes index_bytes*/ IdxBytes = 4; } else { /* XMSSMT, (ceil(h / 8))-bytes index_bytes */ IdxBytes = (h + 7) / 8; } index = BytestoU64(sig, IdxBytes); offset += IdxBytes; PUT_UINT64_BE(index, idx, sizeof(idx) - 8); uint8_t digest[MAX_MDSIZE] = {0}; ret = ctx->hashFuncs.hmsg(ctx, sig + offset, msg, msgLen, idx + sizeof(idx) - n, digest); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } offset += n; leafIdx = index & ((1UL << hp) - 1); treeIdx = index >> hp; ret = HypertreeVerify(digest, n, sig + offset, sigLen - offset, treeIdx, leafIdx, ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } int32_t CRYPT_XMSS_Verify(const CryptXmssCtx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen) { (void)algId; int32_t ret; if (ctx == NULL || data == NULL || dataLen == 0 || sign == NULL || signLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } ret = CRYPT_XMSS_VerifyInternal(ctx, data, dataLen, sign, signLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } static const XmssPara *FindXmssPara(CRYPT_PKEY_ParaId algId) { for (uint32_t i = 0; i < sizeof(XmssParaTable)/sizeof(XmssParaTable[0]); i++) { if ((CRYPT_PKEY_ParaId)XmssParaTable[i].algId == algId) { return &XmssParaTable[i]; } } return NULL; } static int32_t XmssSetAlgId(CryptXmssCtx *ctx, CRYPT_PKEY_ParaId algId) { int32_t ret; const XmssPara *para = NULL; static const AdrsOps XadrsOps = { .setLayerAddr = XAdrsSetLayerAddr, .setTreeAddr = XAdrsSetTreeAddr, .setType = XAdrsSetType, .setKeyPairAddr = XAdrsSetKeyPairAddr, .setChainAddr = XAdrsSetChainAddr, .setTreeHeight = XAdrsSetTreeHeight, .setHashAddr = XAdrsSetHashAddr, .setTreeIndex = XAdrsSetTreeIndex, .setKeyAndMask = XAdrsSetKeyAndMask, .getTreeHeight = XAdrsGetTreeHeight, .getTreeIndex = XAdrsGetTreeIndex, .copyKeyPairAddr = XAdrsCopyKeyPairAddr, .getAdrsLen = XAdrsGetAdrsLen, }; para = FindXmssPara(algId); if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_ALGID); return CRYPT_XMSS_ERR_INVALID_ALGID; } ctx->para = *para; ret = XmssInitHashFuncs(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_ALGID); return CRYPT_XMSS_ERR_INVALID_ALGID; } ctx->adrsOps = XadrsOps; return CRYPT_SUCCESS; } int32_t CRYPT_XMSS_Ctrl(CryptXmssCtx *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) { case CRYPT_CTRL_SET_PARA_BY_ID: if (val == NULL || len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } CRYPT_PKEY_ParaId algId = *(CRYPT_PKEY_ParaId *)val; if (CheckNotXmssAlgId(algId)) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_ALGID); return CRYPT_XMSS_ERR_INVALID_ALGID; } return XmssSetAlgId(ctx, algId); case CRYPT_CTRL_GET_XMSS_KEY_LEN: if (val == NULL || len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(uint32_t *)val = ctx->para.n; return CRYPT_SUCCESS; default: BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } } static int32_t XPubKeyParamCheck(const CryptXmssCtx *ctx, BSL_Param *para, XmssPubKeyParam *pub) { if (ctx == NULL || para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } pub->pubSeed = BSL_PARAM_FindParam(para, CRYPT_PARAM_XMSS_PUB_SEED); pub->pubRoot = BSL_PARAM_FindParam(para, CRYPT_PARAM_XMSS_PUB_ROOT); if (pub->pubSeed == NULL || pub->pubSeed->value == NULL || pub->pubRoot == NULL || pub->pubRoot->value == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pub->pubSeed->valueLen != ctx->para.n || pub->pubRoot->valueLen != ctx->para.n) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_KEYLEN); return CRYPT_XMSS_ERR_INVALID_KEYLEN; } return CRYPT_SUCCESS; } int32_t CRYPT_XMSS_GetPubKey(const CryptXmssCtx *ctx, BSL_Param *para) { XmssPubKeyParam pub; int32_t ret = XPubKeyParamCheck(ctx, para, &pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } pub.pubSeed->useLen = pub.pubRoot->useLen = ctx->para.n; (void)memcpy_s(pub.pubSeed->value, pub.pubSeed->valueLen, ctx->prvKey.pub.seed, ctx->para.n); (void)memcpy_s(pub.pubRoot->value, pub.pubRoot->valueLen, ctx->prvKey.pub.root, ctx->para.n); return CRYPT_SUCCESS; } static int32_t XPrvKeyParamCheck(const CryptXmssCtx *ctx, BSL_Param *para, XmssPrvKeyParam *prv) { if (ctx == NULL || para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } prv->prvIndex = BSL_PARAM_FindParam(para, CRYPT_PARAM_XMSS_PRV_INDEX); prv->prvSeed = BSL_PARAM_FindParam(para, CRYPT_PARAM_XMSS_PRV_SEED); prv->prvPrf = BSL_PARAM_FindParam(para, CRYPT_PARAM_XMSS_PRV_PRF); prv->pubSeed = BSL_PARAM_FindParam(para, CRYPT_PARAM_XMSS_PUB_SEED); prv->pubRoot = BSL_PARAM_FindParam(para, CRYPT_PARAM_XMSS_PUB_ROOT); if (prv->prvIndex == NULL || prv->prvIndex->value == NULL || prv->prvSeed == NULL || prv->prvSeed->value == NULL || prv->prvPrf == NULL || prv->prvPrf->value == NULL || prv->pubSeed == NULL || prv->pubSeed->value == NULL || prv->pubRoot == NULL || prv->pubRoot->value == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prv->prvIndex->valueLen != sizeof(ctx->prvKey.index) || prv->prvSeed->valueLen != ctx->para.n || prv->prvPrf->valueLen != ctx->para.n || prv->pubSeed->valueLen != ctx->para.n || prv->pubRoot->valueLen != ctx->para.n) { BSL_ERR_PUSH_ERROR(CRYPT_XMSS_ERR_INVALID_KEYLEN); return CRYPT_XMSS_ERR_INVALID_KEYLEN; } return CRYPT_SUCCESS; } int32_t CRYPT_XMSS_GetPrvKey(const CryptXmssCtx *ctx, BSL_Param *para) { XmssPrvKeyParam prv; uint64_t index = ctx->prvKey.index; int32_t ret = XPrvKeyParamCheck(ctx, para, &prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } prv.prvSeed->useLen = ctx->para.n; prv.prvPrf->useLen = ctx->para.n; prv.pubSeed->useLen = ctx->para.n; prv.pubRoot->useLen = ctx->para.n; (void)memcpy_s(prv.prvSeed->value, prv.prvSeed->valueLen, ctx->prvKey.seed, ctx->para.n); (void)memcpy_s(prv.prvPrf->value, prv.prvPrf->valueLen, ctx->prvKey.prf, ctx->para.n); (void)memcpy_s(prv.pubSeed->value, prv.pubSeed->valueLen, ctx->prvKey.pub.seed, ctx->para.n); (void)memcpy_s(prv.pubRoot->value, prv.pubRoot->valueLen, ctx->prvKey.pub.root, ctx->para.n); return BSL_PARAM_SetValue(prv.prvIndex, CRYPT_PARAM_XMSS_PRV_INDEX, BSL_PARAM_TYPE_UINT64, &index, sizeof(index)); } int32_t CRYPT_XMSS_SetPubKey(CryptXmssCtx *ctx, const BSL_Param *para) { XmssPubKeyParam pub; int32_t ret = XPubKeyParamCheck(ctx, (BSL_Param *)(uintptr_t)para, &pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.pub.seed, ctx->para.n, pub.pubSeed->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.root, ctx->para.n, pub.pubRoot->value, ctx->para.n); return CRYPT_SUCCESS; } int32_t CRYPT_XMSS_SetPrvKey(CryptXmssCtx *ctx, const BSL_Param *para) { XmssPrvKeyParam prv; uint32_t tmplen = sizeof(ctx->prvKey.index); int32_t ret = XPrvKeyParamCheck(ctx, (BSL_Param *)(uintptr_t)para, &prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(ctx->prvKey.seed, sizeof(ctx->prvKey.seed), prv.prvSeed->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.prf, sizeof(ctx->prvKey.prf), prv.prvPrf->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.seed, sizeof(ctx->prvKey.pub.seed), prv.pubSeed->value, ctx->para.n); (void)memcpy_s(ctx->prvKey.pub.root, sizeof(ctx->prvKey.pub.root), prv.pubRoot->value, ctx->para.n); return BSL_PARAM_GetValue(prv.prvIndex, CRYPT_PARAM_XMSS_PRV_INDEX, BSL_PARAM_TYPE_UINT64, &ctx->prvKey.index, &tmplen); } #endif // HITLS_CRYPTO_XMSS
2301_79861745/bench_create
crypto/xmss/src/xmss.c
C
unknown
24,610
/* * 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_XMSS #include "securec.h" #include "bsl_sal.h" #include "eal_md_local.h" #include "crypt_xmss.h" #include "crypt_algid.h" #include "xmss_hash.h" #include "slh_dsa_local.h" #define PADDING_F 0 #define PADDING_H 1 #define PADDING_HASH 2 #define PADDING_PRF 3 #define PADDING_PRF_KEYGEN 4 static int32_t XCalcMultiMsgHash(CRYPT_MD_AlgId mdId, const CRYPT_ConstData *hashData, uint32_t hashDataLen, uint8_t *out, uint32_t outLen) { uint8_t tmp[MAX_MDSIZE] = {0}; uint32_t tmpLen = sizeof(tmp); int32_t ret = CRYPT_CalcHash(NULL, EAL_MdFindDefaultMethod(mdId), hashData, hashDataLen, tmp, &tmpLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(out, outLen, tmp, outLen); return CRYPT_SUCCESS; } # define IMPLEMENT_XHashFuncs(name, mdId, padding_len) \ static int32_t XPrf##name(const CryptXmssCtx *ctx, const XmssAdrs *adrs, uint8_t *out) \ { \ uint32_t n = ctx->para.n; \ uint8_t padding[MAX_MDSIZE] = {0}; \ const CRYPT_ConstData hashData[] = {{padding, padding_len}, \ {ctx->prvKey.seed, n}, \ {ctx->prvKey.pub.seed, n}, \ {adrs->bytes, ctx->adrsOps.getAdrsLen()}}; \ PUT_UINT32_BE(PADDING_PRF_KEYGEN, padding, padding_len - 4); \ return XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); \ } \ static int32_t Prfmsg##name(const CryptXmssCtx *ctx, const uint8_t *idx, const uint8_t *msg, uint32_t msgLen, \ uint8_t *out) \ { \ (void)msg; \ (void)msgLen; \ uint32_t n = ctx->para.n; \ uint8_t padding[MAX_MDSIZE] = {0}; \ const CRYPT_ConstData hashData[] = {{padding, padding_len}, \ {ctx->prvKey.prf, n}, \ {idx, 32}}; \ PUT_UINT32_BE(PADDING_PRF, padding, padding_len - 4); \ return XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); \ } \ static int32_t Hmsg##name(const CryptXmssCtx *ctx, const uint8_t *r, const uint8_t *msg, uint32_t msgLen, \ const uint8_t *idx, uint8_t *out) \ { \ uint32_t n = ctx->para.n; \ uint8_t padding[MAX_MDSIZE] = {0}; \ const CRYPT_ConstData hashData[] = {{padding, padding_len}, \ {r, n}, \ {ctx->prvKey.pub.root, n}, \ {idx, n}, \ {msg, msgLen}}; \ PUT_UINT32_BE(PADDING_HASH, padding, padding_len - 4); \ return XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), out, n); \ } \ /* A step of Chaining Function */ \ static int32_t XF##name(const CryptXmssCtx *ctx, const XmssAdrs *adrs, const uint8_t *msg, uint32_t msgLen, \ uint8_t *out) \ { \ (void)msgLen; \ int32_t ret; \ XmssAdrs xadrs = *adrs; \ uint32_t n = ctx->para.n; \ uint8_t padding[MAX_MDSIZE] = {0}; \ uint8_t key[MAX_MDSIZE]; \ uint8_t bitmask[MAX_MDSIZE]; \ const CRYPT_ConstData hashData[] = {{padding, padding_len}, \ {ctx->prvKey.pub.seed, n}, \ {xadrs.bytes, ctx->adrsOps.getAdrsLen()}}; \ const CRYPT_ConstData hashData1[] = {{padding, padding_len}, \ {key, n}, \ {bitmask, n}}; \ PUT_UINT32_BE(PADDING_PRF, padding, padding_len - 4); \ /* n-byte key */ \ ctx->adrsOps.setKeyAndMask(&xadrs, 0); \ ret = XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), key, n); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ /* n-byte BM */ \ ctx->adrsOps.setKeyAndMask(&xadrs, 1); \ ret = XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), bitmask, n); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ PUT_UINT32_BE(PADDING_F, padding, padding_len - 4); \ for (uint32_t i = 0; i < n; i++) { \ bitmask[i] = msg[i] ^ bitmask[i]; \ } \ return XCalcMultiMsgHash(mdId, hashData1, sizeof(hashData1) / sizeof(hashData1[0]), out, n); \ } \ /* RAND_HASH : compress Left child node and Right child node to parent node */ \ static int32_t XH##name(const CryptXmssCtx *ctx, const XmssAdrs *adrs, const uint8_t *msg, uint32_t msgLen, \ uint8_t *out) \ { \ (void)msgLen; \ int32_t ret; \ XmssAdrs xadrs = *adrs; \ uint32_t n = ctx->para.n; \ uint8_t padding[MAX_MDSIZE] = {0}; \ uint8_t key[MAX_MDSIZE]; \ uint8_t bitmask[2 * MAX_MDSIZE]; \ const CRYPT_ConstData hashData[] = {{padding, padding_len}, \ {ctx->prvKey.pub.seed, n}, \ {xadrs.bytes, ctx->adrsOps.getAdrsLen()}}; \ const CRYPT_ConstData hashData1[] = {{padding, padding_len}, \ {key, n}, \ {bitmask, 2 * n}}; \ PUT_UINT32_BE(PADDING_PRF, padding, padding_len - 4); \ /* n-byte key */ \ ctx->adrsOps.setKeyAndMask(&xadrs, 0); \ ret = XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), key, n); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ /* n-byte BM_0 */ \ ctx->adrsOps.setKeyAndMask(&xadrs, 1); \ ret = XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), bitmask, n); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ /* n-byte BM_1 */ \ ctx->adrsOps.setKeyAndMask(&xadrs, 2); \ ret = XCalcMultiMsgHash(mdId, hashData, sizeof(hashData) / sizeof(hashData[0]), bitmask + n, n); \ if (ret != CRYPT_SUCCESS) { \ return ret; \ } \ PUT_UINT32_BE(PADDING_H, padding, padding_len - 4); \ for (uint32_t i = 0; i < 2 * n; i++) { \ bitmask[i] = msg[i] ^ bitmask[i]; \ } \ return XCalcMultiMsgHash(mdId, hashData1, sizeof(hashData1) / sizeof(hashData1[0]), out, n); \ } /* L-tree : compress len * n-bytes WOTS+ public key to n-bytes leaf node of main hash tree */ static int32_t XTl(const CryptXmssCtx *ctx, const XmssAdrs *adrs, const uint8_t *msg, uint32_t msgLen, uint8_t *out) { int32_t ret; XmssAdrs xadrs = *adrs; uint32_t n = ctx->para.n; uint32_t len = 2 * n + 3; /* uint8_t node[len][n] */ uint8_t *node = (uint8_t *)BSL_SAL_Malloc(len * n); if (node == NULL) { return BSL_MALLOC_FAIL; } (void)memcpy_s(node, len * n, msg, msgLen); for (uint32_t h = 0; len > 1; h++) { ctx->adrsOps.setTreeHeight(&xadrs, h); for (uint32_t i = 0; i < len/2; i++) { ctx->adrsOps.setTreeIndex(&xadrs, i); /* node[i][0] * / \ * / \ * / \ * / \ * node[i*2][0] node[i*2+1][0] */ ret = ctx->hashFuncs.h(ctx, &xadrs, node + (i * 2 * n), 2 * n, node + (i * n)); if (ret != CRYPT_SUCCESS) { goto ERR; } } /* An L-tree is an unbalanced binary hash tree */ if (len & 1) { (void)memcpy_s(node + (len/2 * n), (len * n) - (len/2 * n), node + (len - 1) * n, n); len = len/2 + 1; } else { len = len/2; } } (void)memcpy_s(out, n, node, n); ret = CRYPT_SUCCESS; ERR: BSL_SAL_Free(node); return ret; } IMPLEMENT_XHashFuncs(Sha256, CRYPT_MD_SHA256, 32) IMPLEMENT_XHashFuncs(Sha256_192, CRYPT_MD_SHA256, 4) IMPLEMENT_XHashFuncs(Sha512, CRYPT_MD_SHA512, 64) IMPLEMENT_XHashFuncs(Shake128, CRYPT_MD_SHAKE128, 32) IMPLEMENT_XHashFuncs(Shake256, CRYPT_MD_SHAKE256, 64) IMPLEMENT_XHashFuncs(Shake256_256, CRYPT_MD_SHAKE256, 32) IMPLEMENT_XHashFuncs(Shake256_192, CRYPT_MD_SHAKE256, 4) int32_t XmssInitHashFuncs(CryptXmssCtx *ctx) { static const XmssHashFuncs XHashFuncsSha256 = { .prf = XPrfSha256, .tl = XTl, .h = XHSha256, .f = XFSha256, .prfmsg = PrfmsgSha256, .hmsg = HmsgSha256, }; static const XmssHashFuncs XHashFuncsSha256_192 = { .prf = XPrfSha256_192, .tl = XTl, .h = XHSha256_192, .f = XFSha256_192, .prfmsg = PrfmsgSha256_192, .hmsg = HmsgSha256_192, }; static const XmssHashFuncs XHashFuncsSha512 = { .prf = XPrfSha512, .tl = XTl, .h = XHSha512, .f = XFSha512, .prfmsg = PrfmsgSha512, .hmsg = HmsgSha512, }; static const XmssHashFuncs XHashFuncsShake128 = { .prf = XPrfShake128, .tl = XTl, .h = XHShake128, .f = XFShake128, .prfmsg = PrfmsgShake128, .hmsg = HmsgShake128, }; static const XmssHashFuncs XHashFuncsShake256 = { .prf = XPrfShake256, .tl = XTl, .h = XHShake256, .f = XFShake256, .prfmsg = PrfmsgShake256, .hmsg = HmsgShake256, }; static const XmssHashFuncs XHashFuncsShake256_256 = { .prf = XPrfShake256_256, .tl = XTl, .h = XHShake256_256, .f = XFShake256_256, .prfmsg = PrfmsgShake256_256, .hmsg = HmsgShake256_256, }; static const XmssHashFuncs XHashFuncsShake256_192 = { .prf = XPrfShake256_192, .tl = XTl, .h = XHShake256_192, .f = XFShake256_192, .prfmsg = PrfmsgShake256_192, .hmsg = HmsgShake256_192, }; CRYPT_PKEY_ParaId algId = ctx->para.algId; switch (algId) { case CRYPT_XMSS_SHA2_10_256: case CRYPT_XMSS_SHA2_16_256: case CRYPT_XMSS_SHA2_20_256: ctx->hashFuncs = XHashFuncsSha256; break; case CRYPT_XMSS_SHA2_10_512: case CRYPT_XMSS_SHA2_16_512: case CRYPT_XMSS_SHA2_20_512: ctx->hashFuncs = XHashFuncsSha512; break; case CRYPT_XMSS_SHAKE_10_256: case CRYPT_XMSS_SHAKE_16_256: case CRYPT_XMSS_SHAKE_20_256: ctx->hashFuncs = XHashFuncsShake128; break; case CRYPT_XMSS_SHAKE_10_512: case CRYPT_XMSS_SHAKE_16_512: case CRYPT_XMSS_SHAKE_20_512: ctx->hashFuncs = XHashFuncsShake256; break; case CRYPT_XMSS_SHA2_10_192: case CRYPT_XMSS_SHA2_16_192: case CRYPT_XMSS_SHA2_20_192: ctx->hashFuncs = XHashFuncsSha256_192; break; case CRYPT_XMSS_SHAKE256_10_256: case CRYPT_XMSS_SHAKE256_16_256: case CRYPT_XMSS_SHAKE256_20_256: ctx->hashFuncs = XHashFuncsShake256_256; break; case CRYPT_XMSS_SHAKE256_10_192: case CRYPT_XMSS_SHAKE256_16_192: case CRYPT_XMSS_SHAKE256_20_192: ctx->hashFuncs = XHashFuncsShake256_192; break; case CRYPT_XMSSMT_SHA2_20_2_256: case CRYPT_XMSSMT_SHA2_20_4_256: case CRYPT_XMSSMT_SHA2_40_2_256: case CRYPT_XMSSMT_SHA2_40_4_256: case CRYPT_XMSSMT_SHA2_40_8_256: case CRYPT_XMSSMT_SHA2_60_3_256: case CRYPT_XMSSMT_SHA2_60_6_256: case CRYPT_XMSSMT_SHA2_60_12_256: ctx->hashFuncs = XHashFuncsSha256; break; case CRYPT_XMSSMT_SHA2_20_2_512: case CRYPT_XMSSMT_SHA2_20_4_512: case CRYPT_XMSSMT_SHA2_40_2_512: case CRYPT_XMSSMT_SHA2_40_4_512: case CRYPT_XMSSMT_SHA2_40_8_512: case CRYPT_XMSSMT_SHA2_60_3_512: case CRYPT_XMSSMT_SHA2_60_6_512: case CRYPT_XMSSMT_SHA2_60_12_512: ctx->hashFuncs = XHashFuncsSha512; break; case CRYPT_XMSSMT_SHAKE_20_2_256: case CRYPT_XMSSMT_SHAKE_20_4_256: case CRYPT_XMSSMT_SHAKE_40_2_256: case CRYPT_XMSSMT_SHAKE_40_4_256: case CRYPT_XMSSMT_SHAKE_40_8_256: case CRYPT_XMSSMT_SHAKE_60_3_256: case CRYPT_XMSSMT_SHAKE_60_6_256: case CRYPT_XMSSMT_SHAKE_60_12_256: ctx->hashFuncs = XHashFuncsShake128; break; case CRYPT_XMSSMT_SHAKE_20_2_512: case CRYPT_XMSSMT_SHAKE_20_4_512: case CRYPT_XMSSMT_SHAKE_40_2_512: case CRYPT_XMSSMT_SHAKE_40_4_512: case CRYPT_XMSSMT_SHAKE_40_8_512: case CRYPT_XMSSMT_SHAKE_60_3_512: case CRYPT_XMSSMT_SHAKE_60_6_512: case CRYPT_XMSSMT_SHAKE_60_12_512: ctx->hashFuncs = XHashFuncsShake256; break; case CRYPT_XMSSMT_SHA2_20_2_192: case CRYPT_XMSSMT_SHA2_20_4_192: case CRYPT_XMSSMT_SHA2_40_2_192: case CRYPT_XMSSMT_SHA2_40_4_192: case CRYPT_XMSSMT_SHA2_40_8_192: case CRYPT_XMSSMT_SHA2_60_3_192: case CRYPT_XMSSMT_SHA2_60_6_192: case CRYPT_XMSSMT_SHA2_60_12_192: ctx->hashFuncs = XHashFuncsSha256_192; break; case CRYPT_XMSSMT_SHAKE256_20_2_256: case CRYPT_XMSSMT_SHAKE256_20_4_256: case CRYPT_XMSSMT_SHAKE256_40_2_256: case CRYPT_XMSSMT_SHAKE256_40_4_256: case CRYPT_XMSSMT_SHAKE256_40_8_256: case CRYPT_XMSSMT_SHAKE256_60_3_256: case CRYPT_XMSSMT_SHAKE256_60_6_256: case CRYPT_XMSSMT_SHAKE256_60_12_256: ctx->hashFuncs = XHashFuncsShake256_256; break; case CRYPT_XMSSMT_SHAKE256_20_2_192: case CRYPT_XMSSMT_SHAKE256_20_4_192: case CRYPT_XMSSMT_SHAKE256_40_2_192: case CRYPT_XMSSMT_SHAKE256_40_4_192: case CRYPT_XMSSMT_SHAKE256_40_8_192: case CRYPT_XMSSMT_SHAKE256_60_3_192: case CRYPT_XMSSMT_SHAKE256_60_6_192: case CRYPT_XMSSMT_SHAKE256_60_12_192: ctx->hashFuncs = XHashFuncsShake256_192; break; default: return CRYPT_XMSS_ERR_INVALID_ALGID; } return CRYPT_SUCCESS; } #endif // HITLS_CRYPTO_XMSS
2301_79861745/bench_create
crypto/xmss/src/xmss_hash.c
C
unknown
22,485
/* * 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 XMSS_HASH_H #define XMSS_HASH_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_XMSS #include <stdint.h> int32_t XmssInitHashFuncs(CryptXmssCtx *ctx); #endif // HITLS_CRYPTO_XMSS #endif // XMSS_HASH_H
2301_79861745/bench_create
crypto/xmss/src/xmss_hash.h
C
unknown
754
/* * 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 AUTH_ERRNO_H #define AUTH_ERRNO_H #ifdef __cplusplus extern "C" { #endif typedef enum { HITLS_AUTH_SUCCESS = 0, /* Operation completed successfully */ HITLS_AUTH_PRIVPASS_INVALID_INPUT = 0x05010001, /* Invalid input parameters */ HITLS_AUTH_PRIVPASS_INVALID_CMD, /* Invalid command */ HITLS_AUTH_PRIVPASS_INVALID_ALG, /* Invalid algorithm specified */ HITLS_AUTH_PRIVPASS_INVALID_TOEKN_PROTOCOL_TYPE, /* Invalid protocol type */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_TYPE, /* Invalid token type */ HITLS_AUTH_PRIVPASS_BUFFER_NOT_ENOUGH, /* Buffer size is insufficient */ HITLS_AUTH_PRIVPASS_INVALID_CRYPTO_METHOD, /* Invalid cryptographic method */ HITLS_AUTH_PRIVPASS_INVALID_CRYPTO_CALLBACK_TYPE, /* Invalid cryptographic callback type */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_CHALLENGE_PARAM, /* Invalid token challenge param */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_CHALLENGE, /* Invalid token challenge */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_CHALLENGE_REQ, /* Invalid token challenge request */ HITLS_AUTH_PRIVPASS_NO_TOKEN_CHALLENGE_TYPE, /* Token challenge type is missing */ HITLS_AUTH_PRIVPASS_NO_TOKEN_CHALLENGE_ISSUERNAME, /* Token challenge issuer name is missing */ HITLS_AUTH_PRIVPASS_NO_TOKEN_CHALLENGE_REDEMPTION, /* Token challenge redemption context is missing */ HITLS_AUTH_PRIVPASS_INVALID_ISSUER_NAME, /* Invalid issuer name */ HITLS_AUTH_PRIVPASS_INVALID_REDEMPTION, /* Invalid redemption */ HITLS_AUTH_PRIVPASS_INVALID_ORIGIN_INFO, /* Invalid origin info */ HITLS_AUTH_PRIVPASS_NO_TOKEN_CHALLENGE_REQUEST, /* Token challenge request is missing */ HITLS_AUTH_PRIVPASS_NO_PUBKEY_INFO, /* Public key information is missing */ HITLS_AUTH_PRIVPASS_NO_PRVKEY_INFO, /* Private key information is missing */ HITLS_AUTH_PRIVPASS_NO_KEYPAIR_CHECK_CALLBACK, /* Key pair check callback is not set */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_REQUEST, /* Invalid token request */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_RESPONSE, /* Invalid token response */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_INSTANCE, /* Invalid token instance */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_KEYID, /* Invalid token key id */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_BLINDED_MSG, /* Invalid blinded message in token */ HITLS_AUTH_PRIVPASS_INVALID_TOKEN_CHALLENGE_DIGEST, /* Invalid token challenge digest */ HITLS_AUTH_PRIVPASS_CHECK_KEYPAIR_FAILED, /* Key pair verification failed */ HITLS_AUTH_PRIVPASS_INVALID_PUBKEY_TYPE, /* Invalid pubkey type, now only support rsa */ HITLS_AUTH_PRIVPASS_INVALID_PUBKEY_PADDING_INFO, /* Invalid pubkey padding info, now only support rsa-pss */ HITLS_AUTH_PRIVPASS_INVALID_PUBKEY_PADDING_MD, /* Invalid pubkey padding md, now only support rsa-pss-sha384 */ HITLS_AUTH_PRIVPASS_INVALID_PUBKEY_BITS, /* Invalid pubkey bits, now only support rsa-2048 */ HITLS_AUTH_PRIVPASS_INVALID_PRVKEY_TYPE, /* Invalid prikey type, now only support rsa */ HITLS_AUTH_PRIVPASS_INVALID_PRVKEY_BITS, /* Invalid prikey bits, now only support rsa-2048 */ HITLS_AUTH_PRIVPASS_NO_ISSUERNAME, /* No issuer name in token challenge */ HITLS_AUTH_PRIVPASS_NO_RESPONSE_INFO, /* No response info in token response */ HITLS_AUTH_PRIVPASS_NO_BLINDEDMSG, /* No blinded message in token request */ HITLS_AUTH_PRIVPASS_NO_AUTHENTICATOR, /* No authenticator in token */ } HITLS_AUTH_ERRNO; #ifdef __cplusplus } #endif #endif // AUTH_ERRNO_H
2301_79861745/bench_create
include/auth/auth_errno.h
C
unknown
4,472
/* * 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 AUTH_PARAMS_H #define AUTH_PARAMS_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /* Base value for Private Pass Token parameters */ #define AUTH_PARAM_PRIVPASS_TOKEN 20000 #define AUTH_PARAM_PRIVPASS_TOKENCHALLENGE_REQUEST (AUTH_PARAM_PRIVPASS_TOKEN + 1) #define AUTH_PARAM_PRIVPASS_TOKENCHALLENGE_TYPE (AUTH_PARAM_PRIVPASS_TOKEN + 2) #define AUTH_PARAM_PRIVPASS_TOKENCHALLENGE_ISSUERNAME (AUTH_PARAM_PRIVPASS_TOKEN + 3) #define AUTH_PARAM_PRIVPASS_TOKENCHALLENGE_REDEMPTION (AUTH_PARAM_PRIVPASS_TOKEN + 4) #define AUTH_PARAM_PRIVPASS_TOKENCHALLENGE_ORIGININFO (AUTH_PARAM_PRIVPASS_TOKEN + 5) #define AUTH_PARAM_PRIVPASS_TOKENREQUEST_TYPE (AUTH_PARAM_PRIVPASS_TOKEN + 6) #define AUTH_PARAM_PRIVPASS_TOKENREQUEST_TRUNCATEDTOKENKEYID (AUTH_PARAM_PRIVPASS_TOKEN + 7) #define AUTH_PARAM_PRIVPASS_TOKENREQUEST_BLINDEDMSG (AUTH_PARAM_PRIVPASS_TOKEN + 9) #define AUTH_PARAM_PRIVPASS_TOKENRESPONSE_INFO (AUTH_PARAM_PRIVPASS_TOKEN + 10) #define AUTH_PARAM_PRIVPASS_TOKEN_TYPE (AUTH_PARAM_PRIVPASS_TOKEN + 11) #define AUTH_PARAM_PRIVPASS_TOKEN_NONCE (AUTH_PARAM_PRIVPASS_TOKEN + 12) #define AUTH_PARAM_PRIVPASS_TOKEN_CHALLENGEDIGEST (AUTH_PARAM_PRIVPASS_TOKEN + 13) #define AUTH_PARAM_PRIVPASS_TOKEN_TOKENKEYID (AUTH_PARAM_PRIVPASS_TOKEN + 14) #define AUTH_PARAM_PRIVPASS_TOKEN_AUTHENTICATOR (AUTH_PARAM_PRIVPASS_TOKEN + 15) #define AUTH_PARAM_PRIVPASS_CTX_TOKENKEYID (AUTH_PARAM_PRIVPASS_TOKEN + 16) #define AUTH_PARAM_PRIVPASS_CTX_TRUNCATEDTOKENKEYID (AUTH_PARAM_PRIVPASS_TOKEN + 17) #define AUTH_PARAM_PRIVPASS_CTX_NONCE (AUTH_PARAM_PRIVPASS_TOKEN + 18) #ifdef __cplusplus } #endif #endif
2301_79861745/bench_create
include/auth/auth_params.h
C
unknown
2,592
/* * 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 AUTH_PRIVPASS_TOKEN_H #define AUTH_PRIVPASS_TOKEN_H #include <stdint.h> #include "bsl_params.h" #include "bsl_obj.h" #ifdef __cplusplus extern "C" { #endif /** * @ingroup auth_privpass * * priv pass context structure. */ typedef struct PrivPass_Ctx HITLS_AUTH_PrivPassCtx; /** * @ingroup auth_privpass * * priv pass token structure. */ typedef struct PrivPass_Token HITLS_AUTH_PrivPassToken; /* Token types for different stages of the Private Pass protocol */ typedef enum { HITLS_AUTH_PRIVPASS_TOKEN_CHALLENGE_REQUEST = 1, // Initial request for challenge HITLS_AUTH_PRIVPASS_TOKEN_CHALLENGE = 2, // Challenge from server HITLS_AUTH_PRIVPASS_TOKEN_REQUEST = 3, // Token request with blinded message HITLS_AUTH_PRIVPASS_TOKEN_RESPONSE = 4, // Server's response with blind signature HITLS_AUTH_PRIVPASS_TOKEN_INSTANCE = 5, // Final token instance } HITLS_AUTH_PrivPassTokenType; /* Token types for different stages of the Private Pass protocol */ typedef enum { HITLS_AUTH_PRIVPASS_PRV_VERIFY_TOKENS = 1, // Private key verification tokens HITLS_AUTH_PRIVPASS_PUB_VERIFY_TOKENS = 2, // Public key verification tokens } HITLS_AUTH_PrivPassType; /* Commands for token operations and parameter retrieval */ typedef enum { HITLS_AUTH_PRIVPASS_GET_TOKENCHALLENGEREQUEST_INFO = 1, /** Get the challenge request information from token */ HITLS_AUTH_PRIVPASS_GET_TOKENCHALLENGE_TYPE = 2, /** Get the type of token challenge */ HITLS_AUTH_PRIVPASS_GET_TOKENCHALLENGE_ISSUERNAME = 3, /** Get the issuer name from token challenge */ HITLS_AUTH_PRIVPASS_GET_TOKENCHALLENGE_REDEMPTION = 4, /** Get the redemption information from token challenge */ HITLS_AUTH_PRIVPASS_GET_TOKENCHALLENGE_ORIGININFO = 5, /** Get the origin information from token challenge */ HITLS_AUTH_PRIVPASS_GET_TOKENREQUEST_TYPE = 6, /** Get the type of token request */ HITLS_AUTH_PRIVPASS_GET_TOKENREQUEST_TRUNCATEDTOKENKEYID = 7, /** Get the truncated tokenKey id from tokenRequest */ HITLS_AUTH_PRIVPASS_GET_TOKENREQUEST_BLINDEDMSG = 8, /** Get the blinded message from token request */ HITLS_AUTH_PRIVPASS_GET_TOKENRESPONSE_INFO = 9, /** Get the origin information from token response */ HITLS_AUTH_PRIVPASS_GET_TOKEN_TYPE = 10, /** Get the nonce value from token */ HITLS_AUTH_PRIVPASS_GET_TOKEN_NONCE = 11, /** Get the nonce value from token */ HITLS_AUTH_PRIVPASS_GET_TOKEN_CHALLENGEDIGEST = 12, /** Get the challenge digest from token */ HITLS_AUTH_PRIVPASS_GET_TOKEN_TOKENKEYID = 13, /** Get the token key id from token */ HITLS_AUTH_PRIVPASS_GET_TOKEN_AUTHENTICATOR = 14, /** Get the authenticator from token */ HITLS_AUTH_PRIVPASS_GET_CTX_TOKENKEYID = 15, /** Get the token key id from ctx */ HITLS_AUTH_PRIVPASS_GET_CTX_TRUNCATEDTOKENKEYID = 16, /** Get the truncated token key id from ctx */ HITLS_AUTH_PRIVPASS_GET_CTX_NONCE = 17, /** Get the nonce from ctx */ } HITLS_AUTH_PrivPassCmd; typedef enum { HITLS_AUTH_PRIVPASS_CRYPTO_RSA = BSL_CID_RSA, HITLS_AUTH_PRIVPASS_CRYPTO_SHA256 = BSL_CID_SHA256, HITLS_AUTH_PRIVPASS_CRYPTO_SHA384 = BSL_CID_SHA384, } HITLS_AUTH_PrivPassCryptAlgId; typedef enum { HITLS_AUTH_PRIVPASS_NEW_PKEY_CTX_CB = 1, HITLS_AUTH_PRIVPASS_FREE_PKEY_CTX_CB = 2, HITLS_AUTH_PRIVPASS_DIGEST_CB = 3, HITLS_AUTH_PRIVPASS_BLIND_CB = 4, HITLS_AUTH_PRIVPASS_UNBLIND_CB = 5, HITLS_AUTH_PRIVPASS_SIGNDATA_CB = 6, HITLS_AUTH_PRIVPASS_VERIFY_CB = 7, HITLS_AUTH_PRIVPASS_DECODE_PUBKEY_CB = 8, HITLS_AUTH_PRIVPASS_DECODE_PRVKEY_CB = 9, HITLS_AUTH_PRIVPASS_CHECK_KEYPAIR_CB = 10, HITLS_AUTH_PRIVPASS_RANDOM_CB = 11, } HITLS_AUTH_PrivPassCryptCbType; /** * @ingroup auth_privpass * @brief Creates a new public/private key context for the specified algorithm. * * @param libCtx [IN] Library context * @param attrName [IN] Specify expected attribute values * @param algId [IN] Algorithm identifier, defined in HITLS_AUTH_PrivPassCryptAlgId. * * @retval Pointer to the created key context. * NULL, if the operation fails. */ typedef void *(*HITLS_AUTH_PrivPassNewPkeyCtx)(void *libCtx, const char *attrName, int32_t algId); /** * @ingroup auth_privpass * @brief Frees a previously allocated key context. * * @param pkeyCtx [IN] Key context to be freed */ typedef void (*HITLS_AUTH_PrivPassFreePkeyCtx)(void *pkeyCtx); /** * @ingroup auth_privpass * @brief Computes a cryptographic digest of the input data. * @param libCtx [IN] Library context * @param attrName [IN] Specify expected attribute values * @param algId [IN] Algorithm identifier, defined in HITLS_AUTH_PrivPassCryptAlgId. * @param input [IN] Input data to be hashed * @param inputLen [IN] Length of input data * @param digest [OUT] Buffer to store the computed digest * @param digestLen [IN/OUT] Size of digest buffer/Length of computed digest * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassDigest)(void *libCtx, const char *attrName, int32_t algId, const uint8_t *input, uint32_t inputLen, uint8_t *digest, uint32_t *digestLen); /** * @ingroup auth_privpass * @brief Blinds data using the key context and hash algorithm for blind signature protocol. The default algorithm * callback implementation is supported only from RSASSA-PSS. * * @param pkeyCtx [IN] Key context * @param algId [IN] hash algorithm identifier * @param data [IN] Data to be blinded * @param dataLen [IN] Length of input data * @param blindedData [OUT] Buffer to store blinded data * @param blindedDataLen [IN/OUT] Size of buffer/Length of blinded data * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassBlind)(void *pkeyCtx, int32_t algId, const uint8_t *data, uint32_t dataLen, uint8_t *blindedData, uint32_t *blindedDataLen); /** * @ingroup auth_privpass * @brief Unblinds previously blinded data to reveal the actual signature. The default algorithm callback * implementation is supported only from RSASSA-PSS. * * @param pkeyCtx [IN] Key context * @param blindedData [IN] Blinded data to be unblinded * @param blindedDataLen [IN] Length of blinded data * @param data [OUT] Buffer to store unblinded data * @param dataLen [IN/OUT] Size of buffer/Length of unblinded data * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassUnblind)(void *pkeyCtx, const uint8_t *blindedData, uint32_t blindedDataLen, uint8_t *data, uint32_t *dataLen); /** * @ingroup auth_privpass * @brief Signs data using the private key context. * * @param pkeyCtx [IN] Private key context * @param data [IN] Data to be signed * @param dataLen [IN] Length of input data * @param sign [OUT] Buffer to store signature * @param signLen [IN/OUT] Size of buffer/Length of signature * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassSignData)(void *pkeyCtx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); /** * @ingroup auth_privpass * @brief Verifies a signature using the public key context. * * @param pkeyCtx [IN] Public key context * @param algId [IN] hash algorithm identifier * @param data [IN] Original data * @param dataLen [IN] Length of data * @param sign [IN] Signature to verify * @param signLen [IN] Length of signature * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassVerify)(void *pkeyCtx, int32_t algId, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); /** * @ingroup auth_privpass * @brief Decodes a public key and gen a key ctx. The default algorithm callback implementation is supported only from * a DER-encoded SubjectPublicKeyInfo (SPKI) object using the RSASSA-PSS OID. * * @param libCtx [IN] Library context * @param attrName [IN] Specify expected attribute values * @param pubKey [IN] A DER-encoded SubjectPublicKeyInfo (SPKI) object using the RSASSA-PSS OID * @param pubKeyLen [IN] Length of public key data * @param pkeyCtx [OUT] Pointer to store created key context * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassDecodePubKey)(void *libCtx, const char *attrName, uint8_t *pubKey, uint32_t pubKeyLen, void **pkeyCtx); /** * @ingroup auth_privpass * @brief Decodes a private key and gen a key ctx. The default algorithm callback implementation is supported only from * PEM-encoded PKCS #8 unencrypted RSA issuer private key. * * @param libCtx [IN] Library context * @param attrName [IN] Specify expected attribute values * @param param [IN] Parameters may need by private key decoding. * @param prvKey [IN] A PEM-encoded PKCS #8 RSA unencrypted issuer private Key * @param prvKeyLen [IN] Length of private key data * @param pkeyCtx [OUT] Pointer to store created key context * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassDecodePrvKey)(void *libCtx, const char *attrName, void *param, uint8_t *prvKey, uint32_t prvKeyLen, void **pkeyCtx); /** * @ingroup auth_privpass * @brief Verifies that a public/private key pair matches. * * @param pubKeyCtx [IN] Public key context * @param prvKeyCtx [IN] Private key context * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassCheckKeyPair)(void *pubKeyCtx, void *prvKeyCtx); /** * @ingroup auth_privpass * @brief Generates random bytes. * * @param buffer [IN] Buffer to store random bytes * @param bufferLen [IN] Length of buffer * * @retval #0, if successful. * other error codes, failed. */ typedef int32_t (*HITLS_AUTH_PrivPassRandom)(uint8_t *buffer, uint32_t bufferLen); /** * @ingroup auth_privpass * @brief Create a new PrivPass context object, all library callbacks by default are setted when created. * @param tokenType [IN] Type of token to create, defined in HITLS_AUTH_PrivPassTokenType. * * @retval HITLS_AUTH_PrivPassCtx pointer. * NULL, if the operation fails. */ HITLS_AUTH_PrivPassCtx *HITLS_AUTH_PrivPassNewCtx(int32_t protocolType); /** * @ingroup auth_privpass * @brief Free a PrivPass context object. * * @param ctx [IN] Context to be freed */ void HITLS_AUTH_PrivPassFreeCtx(HITLS_AUTH_PrivPassCtx *ctx); /** * @ingroup auth_privpass * @brief Create a new PrivPass token object. * * @param tokenType [IN] Type of token to create, defined in HITLS_AUTH_PrivPassTokenType. * * @retval HITLS_AUTH_PrivPassToken pointer. * NULL, if the operation fails. */ HITLS_AUTH_PrivPassToken *HITLS_AUTH_PrivPassNewToken(int32_t tokenType); /** * @ingroup auth_privpass * @brief Free a PrivPass token object. * * @param object [IN] Token to be freed */ void HITLS_AUTH_PrivPassFreeToken(HITLS_AUTH_PrivPassToken *object); /** * @ingroup auth_privpass * @brief Set cryptographic callback functions for the context. When setting callbacks, * the input callbacks will be checked. Non-NULL callbacks will override the default callbacks. * * @param ctx [IN/OUT] PrivPass context * @param cbType [IN] Callback type, defined in PrivPassCryptCbType * @param cryptCb [IN] Callback functions to be set * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassSetCryptCb(HITLS_AUTH_PrivPassCtx *ctx, int32_t cbType, void *cryptCb); /** * @ingroup auth_privpass * @brief Serialize a PrivPass token object to binary format, If the object == NULL, outbufferlen returns * the length required for serialization * * @param ctx [IN] PrivPass context * @param object [IN] Token to serialize * @param buffer [OUT] Buffer to store serialized data * @param outBuffLen [IN/OUT] Length of the serialized data * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassSerialization(HITLS_AUTH_PrivPassCtx *ctx, const HITLS_AUTH_PrivPassToken *object, uint8_t *buffer, uint32_t *outBuffLen); /** * @ingroup auth_privpass * @brief Deserialize binary data into a PrivPass token object. The object needs to be freed by the caller * using HITLS_AUTH_PrivPassFreeToken * * @param ctx [IN] PrivPass context * @param tokenType [IN] Expected token type * @param buffer [IN] Serialized data buffer * @param buffLen [IN] Length of serialized data * @param object [OUT] Pointer to store deserialized token * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassDeserialization(HITLS_AUTH_PrivPassCtx *ctx, int32_t tokenType, const uint8_t *buffer, uint32_t buffLen, HITLS_AUTH_PrivPassToken **object); /** * @ingroup auth_privpass * @brief Generate a token challenge. The challenge token is generated based on * the input param. The construct of param refer to auth_params.h. * @param ctx [IN] PrivPass context * @param param [IN] Parameters for challenge generation, the param is limited to the library specification, * the argument passed by the caller should ensure that the serialized length cannot exceed the upper limit. * The tokenType, issuerName, redemption must be contained in the param, and originalInfo can be NULL. * @param challenge [OUT] Generated challenge token * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassGenTokenChallenge(HITLS_AUTH_PrivPassCtx *ctx, const BSL_Param *param, HITLS_AUTH_PrivPassToken **challenge); /** * @ingroup auth_privpass * @brief Generate a token request. * * @param ctx [IN] PrivPass context * @param tokenChallenge [IN] Challenge token * @param tokenRequest [OUT] Generated request token * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassGenTokenReq(HITLS_AUTH_PrivPassCtx *ctx, const HITLS_AUTH_PrivPassToken *tokenChallenge, HITLS_AUTH_PrivPassToken **tokenRequest); /** * @ingroup auth_privpass * @brief Generate a token response. * * @param ctx [IN] PrivPass context * @param tokenRequest [IN] Request token * @param tokenResponse [OUT] Generated response token * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassGenTokenResponse(HITLS_AUTH_PrivPassCtx *ctx, const HITLS_AUTH_PrivPassToken *tokenRequest, HITLS_AUTH_PrivPassToken **tokenResponse); /** * @ingroup auth_privpass * @brief Generate final token. * * @param ctx [IN] PrivPass context * @param tokenChallenge [IN] Challenge token * @param tokenResponse [IN] Response token * @param token [OUT] Generated final token * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassGenToken(HITLS_AUTH_PrivPassCtx *ctx, const HITLS_AUTH_PrivPassToken *tokenChallenge, const HITLS_AUTH_PrivPassToken *tokenResponse, HITLS_AUTH_PrivPassToken **token); /** * @ingroup auth_privpass * @brief Verify the validity of a token. * * @param ctx [IN] PrivPass context * @param tokenChallenge [IN] Challenge token * @param token [IN] Token to verify * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassVerifyToken(HITLS_AUTH_PrivPassCtx *ctx, const HITLS_AUTH_PrivPassToken *tokenChallenge, const HITLS_AUTH_PrivPassToken *token); /** * @ingroup auth_privpass * @brief Set the public key for the ctx. We support the repeated setting of the public key. If the ctx * contains the private key when the public key is set, we will check whether the public key * matches the private key. If its not match, an exception is returned. * * @param ctx [IN] PrivPass context * @param pki [IN] A DER-encoded SubjectPublicKeyInfo (SPKI) object using the RSASSA-PSS OID * @param pkiLen [IN] Length of public key data * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassSetPubkey(HITLS_AUTH_PrivPassCtx *ctx, uint8_t *pki, uint32_t pkiLen); /** * @ingroup auth_privpass * @brief Set the private key for the ctx. We support the repeated setting of the private key. If the ctx * contains the public key when the private key is set, we will check whether the private key * matches the public key. If its not match, an exception is returned. * @param ctx [IN] PrivPass context * @param param [IN] Parameters may need by private key decoding. * @param ski [IN] A PEM-encoded PKCS #8 RSA unencrypted issuer private key * @param skiLen [IN] Length of private key data * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassSetPrvkey(HITLS_AUTH_PrivPassCtx *ctx, void *param, uint8_t *ski, uint32_t skiLen); /** * @ingroup auth_privpass * @brief Control interface for getting/setting various parameters in token object. * * @param object [IN] token object * @param cmd [IN] Command to execute, defined in HITLS_AUTH_PrivPassCmd * @param param [IN/OUT] Command parameters * @param paramLen [IN] Length of parameters * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassTokenCtrl(HITLS_AUTH_PrivPassToken *object, int32_t cmd, void *param, uint32_t paramLen); /** * @ingroup auth_privpass * @brief Control interface for getting/setting various parameters in Priv-Pass Ctx. * * @param ctx [IN] PrivPass context * @param cmd [IN] Command to execute, defined in HITLS_AUTH_PrivPassCmd * @param param [IN/OUT] Command parameters * @param paramLen [IN] Length of parameters * * @retval #HITLS_AUTH_SUCCESS, if successful. * For other error codes, see auth_errno.h. */ int32_t HITLS_AUTH_PrivPassCtxCtrl(HITLS_AUTH_PrivPassCtx *ctx, int32_t cmd, void *param, uint32_t paramLen); #ifdef __cplusplus } #endif #endif // AUTH_PRIVPASS_TOKEN_H
2301_79861745/bench_create
include/auth/auth_privpass_token.h
C
unknown
19,272
/* * 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 BSL_ASN1_H #define BSL_ASN1_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif #define BSL_ASN1_CLASS_UNIVERSAL 0x0 /* bit8 0, bit7 0 */ #define BSL_ASN1_CLASS_APPLICATION 0x40 /* bit8 0, bit7 1 */ #define BSL_ASN1_CLASS_CTX_SPECIFIC 0x80 /* bit8 1, bit7 0 */ #define BSL_ASN1_CLASS_PRIVATE 0xC0 /* bit8 1, bit7 1 */ #define BSL_ASN1_TAG_CONSTRUCTED 0x20 /* ASN1 tag from x.680 */ #define BSL_ASN1_TAG_BOOLEAN 0x01 #define BSL_ASN1_TAG_INTEGER 0x02 #define BSL_ASN1_TAG_BITSTRING 0x03 #define BSL_ASN1_TAG_OCTETSTRING 0x04 #define BSL_ASN1_TAG_NULL 0x05 #define BSL_ASN1_TAG_OBJECT_ID 0x06 #define BSL_ASN1_TAG_OBJECT_DESCP 0x07 #define BSL_ASN1_TAG_INSTANCE_OF 0x08 #define BSL_ASN1_TAG_REAL 0x09 #define BSL_ASN1_TAG_ENUMERATED 0x0A #define BSL_ASN1_TAG_EMBEDDED_PDV 0x0B #define BSL_ASN1_TAG_UTF8STRING 0x0C #define BSL_ASN1_TAG_RALATIVE_ID 0x0D #define BSL_ASN1_TAG_TIME 0x0E #define BSL_ASN1_TAG_SEQUENCE 0x10 #define BSL_ASN1_TAG_SET 0x11 #define BSL_ASN1_TAG_PRINTABLESTRING 0x13 #define BSL_ASN1_TAG_IA5STRING 0x16 #define BSL_ASN1_TAG_UTCTIME 0x17 #define BSL_ASN1_TAG_GENERALIZEDTIME 0x18 #define BSL_ASN1_TAG_BMPSTRING 0x1E /* Custom types, use private class to prevent conflicts */ #define BSL_ASN1_TAG_CHOICE (BSL_ASN1_CLASS_PRIVATE | 1) #define BSL_ASN1_TAG_ANY (BSL_ASN1_CLASS_PRIVATE | 2) #define BSL_ASN1_TAG_EMPTY 0x00 /* Empty tag, used to indicate that the tag is not encoded */ /* The current value is flags, is used to guide asn1 encoding or decoding */ #define BSL_ASN1_FLAG_OPTIONAL 1 /* The current value is default, is used to guide asn1 encoding or decoding */ #define BSL_ASN1_FLAG_DEFAULT 2 /* Only parsing or encoding headers, and child nodes are not traversed */ #define BSL_ASN1_FLAG_HEADERONLY 4 /* The implied values are of the same type */ #define BSL_ASN1_FLAG_SAME 8 #define BSL_ASN1_List BslList typedef struct _BSL_ASN1_Buffer { uint8_t tag; uint32_t len; uint8_t *buff; } BSL_ASN1_Buffer; typedef struct _BSL_ASN1_BitString { uint8_t *buff; uint32_t len; uint8_t unusedBits; } BSL_ASN1_BitString; typedef struct _BSL_ASN1_TemplateItem { /* exptect tag */ uint8_t tag; /* corresponding to the tag flag */ uint8_t flags : 5; uint8_t depth : 3; } BSL_ASN1_TemplateItem; typedef struct _BSL_ASN1_Template { BSL_ASN1_TemplateItem *templItems; uint32_t templNum; } BSL_ASN1_Template; typedef struct _BSL_ASN1_DecodeListParam { uint32_t layer; uint8_t *expTag; } BSL_ASN1_DecodeListParam; /** * @ingroup bsl_asn1 * @brief The extension function for template decoding is used to handle decoding of uncertain data types. * * @param type [IN] BSL_ASN1_CALLBACK_TYPE * @param idx [IN] The position of the data to be processed in the template. * @param data [IN] The data to be processed. * @param expVal [OUT] Output value. */ typedef int32_t(*BSL_ASN1_DecTemplCallBack)(int32_t type, uint32_t idx, void *data, void *expVal); /** * @ingroup bsl_asn1 * @brief The extension function for template decoding is used to convert an ASN item into a list. * * @param layer [IN] The layer of a list, used to construct the name node will use. * @param asn [IN] The asn1 item to be decoded. * @param cbParam [IN/OUT] The other parameters for decoding. * @param list [OUT] Output value. */ typedef int32_t(*BSL_ASN1_ParseListAsnItem)(uint32_t layer, BSL_ASN1_Buffer *asn, void *cbParam, BSL_ASN1_List *list); /** * @ingroup bsl_asn1 * @brief Decode the tag and length fields of an ASN.1 TLV structure and validate against expected tag. * * @param tag [IN] Expected ASN.1 tag value to validate against. * @param encode [IN/OUT] Pointer to buffer containing encoded data. Updated to point after tag and length fields. * @param encLen [IN/OUT] Length of remaining encoded data. Updated to reflect bytes consumed. * @param valLen [OUT] Length of the value field in bytes. * @retval BSL_SUCCESS Successfully decoded tag and length fields. * BSL_NULL_INPUT Invalid NULL parameters. * BSL_INVALID_ARG Buffer too small. * BSL_ASN1_ERR_MISMATCH_TAG Tag does not match expected value. * Other error codes see bsl_errno.h. */ int32_t BSL_ASN1_DecodeTagLen(uint8_t tag, uint8_t **encode, uint32_t *encLen, uint32_t *valLen); /** * @ingroup bsl_asn1 * @brief Template encoding method. * * @attention * 1. For SET types: The elements in the template should be sorted into tag order. * 2. The type for the following types of BSL_ASN1_Buffer.buff are as follows: * a. BSL_ASN1_TAG_BOOLEAN: bool * * b. BSL_ASN1_TAG_BITSTRING: BSL_ASN1_BitString * * c. BSL_ASN1_TAG_UTCTIME|BSL_ASN1_TAG_GENERALIZEDTIME: BSL_TIME * * * @param templ [IN] Encoding template. * @param asnArr [IN] List of data to be encoded. * @param arrNum [IN] The number of data to be encoded, which is determined by the template. * @param encode [OUT] Encoding result. * @param encLen [OUT] Encoding length. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_ASN1_EncodeTemplate(BSL_ASN1_Template *templ, BSL_ASN1_Buffer *asnArr, uint32_t arrNum, uint8_t **encode, uint32_t *encLen); /** * @ingroup bsl_asn1 * @brief Template decoding method. * * @param templ [IN] Encoding template. * @param decTemlCb [IN] Function for handling uncertain types of data. * @param encode [IN/OUT] Data to be decoded. Update the offset after decoding. * @param encLen [IN/OUT] The length of the data to be decoded. * @param asnArr [OUT] List of data to be decoded. * @param arrNum [IN] The number of data to be encoded, which is determined by the template. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_ASN1_DecodeTemplate(BSL_ASN1_Template *templ, BSL_ASN1_DecTemplCallBack decTemlCb, uint8_t **encode, uint32_t *encLen, BSL_ASN1_Buffer *asnArr, uint32_t arrNum); /** * @ingroup bsl_asn1 * @brief Encoding data of type 'SEQUENCE OF' or 'SET OF'. * * @attention * 1. BSL_ASN1_TAG_SEQUENCE is type 'SEQUENCE OF'. * 2. BSL_ASN1_TAG_SET is type 'SET OF'. * 3. The sorting in 'SET OF' is currently not supported. * * @param tag [IN] BSL_ASN1_TAG_SEQUENCE or BSL_ASN1_TAG_SET * @param listSize [IN] The number of elements in the list. * @param templ [IN] Template for elements in the list. * @param asnArr [IN] List of data to be encoded. * @param arrNum [IN] The number of data to be encoded. * @param out [OUT] Encoding result. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_ASN1_EncodeListItem(uint8_t tag, uint32_t listSize, BSL_ASN1_Template *templ, BSL_ASN1_Buffer *asnArr, uint32_t arrNum, BSL_ASN1_Buffer *out); /** * @ingroup bsl_asn1 * @brief Decoding data of type 'SEQUENCE OF' or 'SET OF'. * * @param param [IN] The parameters of the data to be decoded. * @param asn [IN] The data to be decoded. * @param parseListItemCb [IN] User defined callback function used to convert an ASN item into a list. * @param cbParam [IN/OUT] The parameters in the callback function. * @param list [OUT] Decoding result. * @retval BSL_SUCCESS, success. * Other error codes see the bsl_errno.h. */ int32_t BSL_ASN1_DecodeListItem(BSL_ASN1_DecodeListParam *param, BSL_ASN1_Buffer *asn, BSL_ASN1_ParseListAsnItem parseListItemCb, void *cbParam, BSL_ASN1_List *list); #ifdef __cplusplus } #endif #endif // BSL_ASN1_H
2301_79861745/bench_create
include/bsl/bsl_asn1.h
C
unknown
8,209
/* * 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 BSL_BASE64_H #define BSL_BASE64_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif #define HITLS_BASE64_CTX_BUF_LENGTH 80 #define HITLS_BASE64_CTX_LENGTH 48 /* Input length (len) divided by 3, rounded up, multiplied by 4, then add the number of newline characters,and add the length of the context buffer */ #define HITLS_BASE64_ENCODE_LENGTH(len) \ ((((len) + 2) / 3 * 4) + ((len) / HITLS_BASE64_CTX_LENGTH + 1) * 2 + HITLS_BASE64_CTX_BUF_LENGTH) #define HITLS_BASE64_DECODE_LENGTH(len) (((len) + 3) / 4 * 3 + HITLS_BASE64_CTX_BUF_LENGTH) /* * When writing, it makes all the data written on one line without a newline character at the end; * When reading, it expects all data to be on one line (regardless of whether there is a trailing newline character) */ #define BSL_BASE64_FLAGS_NO_NEWLINE 0x01 typedef struct BASE64_ControlBlock BSL_Base64Ctx; /** * @ingroup bsl_base64 * @brief Encode the specified buffer into the base64 format. * @par Description: The function converts the DER code to base64 format, * In the case of the encoding is successful, The user needs to release the dstBuf memory after the dstBuf is * used up. The user needs to allocate space dstBuf in advance. dstBufLen indicates the length of the allocate space. * @attention None * @param srcBuf [IN] Passed buff buffer. * @param srcBufLen [IN] Input buff buffer length. * @param dstBuf [OUT] Output buff buffer. * @param dstBufLen [OUT] Number of encoded bytes excluding the terminator (a multiple of 4) * @return Error code */ int32_t BSL_BASE64_Encode(const uint8_t *srcBuf, const uint32_t srcBufLen, char *dstBuf, uint32_t *dstBufLen); /** * @ingroup bsl_base64 * @brief Decode the specified buffer into the DER format. * @par Description: This function converts the specified base64 format into the DER format, In the case of the * decoding is successful, the user needs to release the dstBuf memory after the dstBuf is used up. * @attention None * @param srcBuf [IN] Passed buff buffer. * @param srcBufLen [IN] Input buff buffer length. * @param dstBufLen [OUT] Encoding length obtained after decoding. * @param dstBuf [OUT] Encoding string obtained after decoding. * @retval when success , return BSL_SUCCESS; Otherwise, return error code */ int32_t BSL_BASE64_Decode(const char *srcBuf, const uint32_t srcBufLen, uint8_t *dstBuf, uint32_t *dstBufLen); /** * @ingroup bsl_base64 * @brief Generate Stream Encoding Context. * @par Description: generate BSL_Base64Ctx. * @param ctx [IN] Base64 context * @retval void */ BSL_Base64Ctx *BSL_BASE64_CtxNew(void); /** * @ingroup bsl_base64 * @brief Release the stream encoding context. * @par Description: release BSL_Base64Ctx. * @param ctx [IN] Base64 context * @retval void */ void BSL_BASE64_CtxFree(BSL_Base64Ctx *ctx); /** * @ingroup bsl_base64 * @brief Clear stream encoding context. * @par Description: clear BSL_Base64Ctx. * @param ctx [IN] Base64 context * @retval void */ void BSL_BASE64_CtxClear(BSL_Base64Ctx *ctx); /** * @ingroup bsl_base64 * @brief Initialize stream encoding. * @par Description: initialize the context. * @param ctx [IN] Base64 context * @retval In the case of success, return BSL_SUCCESS; Otherwise, returned error code. */ int32_t BSL_BASE64_EncodeInit(BSL_Base64Ctx *ctx); /** * @ingroup bsl_base64 * @brief Encodes a specified buffer into the Base64 format. * @par Description: If the length of the data to be encoded is less than one line or one block, * the data is stored in encData of the context for the next input by user. Until one block is * satisfied or we encountered the last line. * @param srcBuf [IN] Passed buff buffer. * @param srcBufLen [IN] Input buff buffer length. * @param dstBuf [OUT] String obtained after encoding. * @param dstBufLen [OUT] Length obtained after encoding. * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. */ int32_t BSL_BASE64_EncodeUpdate(BSL_Base64Ctx *ctx, const uint8_t *srcBuf, uint32_t srcBufLen, char *dstBuf, uint32_t *dstBufLen); /** * @ingroup bsl_base64 * @brief Encode the specified buffer into the Base64 format. * @par Description: Encode the remaining characters stored in the context buffer. * @param dstBufLen [OUT] Length obtained after encoding. * @param dstBuf [OUT] String obtained after encoding. * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. */ int32_t BSL_BASE64_EncodeFinal(BSL_Base64Ctx *ctx, char *dstBuf, uint32_t *dstBufLen); /** * @ingroup bsl_base64 * @brief Initialize stream decoding. * @par Description: Initialize the context. * @param ctx [IN] Base64 context * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. */ int32_t BSL_BASE64_DecodeInit(BSL_Base64Ctx *ctx); /** * @ingroup bsl_base64 * @brief Decode the specified buffer into the DER format. * @par Description: Block decoding is performed for each full block, * Otherwise, the padding is less one block are placed in the context for decodeFinal processing. * @param srcBuf [IN] Passed buff buffer. * @param srcBufLen [IN] Input buff buffer length. * @param dstBuf [OUT] String obtained after decoding. * @param dstBufLen [OUT] Length obtained after decoding. * @retval In the case of success, return BSL_SUCCESS; Otherwise, returned error code. */ int32_t BSL_BASE64_DecodeUpdate(BSL_Base64Ctx *ctx, const char *srcBuf, const uint32_t srcBufLen, uint8_t *dstBuf, uint32_t *dstBufLen); /** * @ingroup bsl_base64 * @brief Decode the specified buffer into the DER format. * @par Description: Decode the remaining characters stored in the context buffer. * @param dstBufLen [OUT] Length obtained after decoding. * @param dstBuf [OUT] String obtained after decoding. * @retval In the case of success, return BSL_SUCCESS. Otherwise, returned error code. */ int32_t BSL_BASE64_DecodeFinal(BSL_Base64Ctx *ctx, uint8_t *dstBuf, uint32_t *dstBufLen); /** * @ingroup bsl_base64 * @brief Set the flag * @par Description: sets the context flags * @param ctx [IN] Input context. * @param flags [IN] Flags to be set. * @retval In the case of success, return BSL_SUCCESS; Otherwise, returned error code. */ int32_t BSL_BASE64_SetFlags(BSL_Base64Ctx *ctx, uint32_t flags); #ifdef __cplusplus } #endif #endif
2301_79861745/bench_create
include/bsl/bsl_base64.h
C
unknown
7,142
/* * 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 bsl * @brief Base Support Layer */ /** * @defgroup bsl_err * @ingroup bsl * @brief error module */ #ifndef BSL_ERR_H #define BSL_ERR_H #include <stdint.h> #include <stdbool.h> #include "bsl_errno.h" #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_err * @brief Start value of the customized level-1 error module. * * Start value of the customized level-1 error module. The value 0x80 is 128. */ #define BSL_ERR_NEW_MODULE 0x80 /** * @ingroup bsl_err * @brief Initialize Error code module. * * The user must call this interface to initialize. * * @attention NONE * @retval #BSL_SUCCESS, error code module is successfully initialized. * @retval #BSL_MALLOC_FAIL, memory space is insufficient and thread lock space cannot be applied for. * @retval #BSL_SAL_ERR_UNKNOWN, thread lock initialization failed. */ int32_t BSL_ERR_Init(void); /** * @ingroup bsl_err * @brief Error code module deinitialization. * * Called by the user when the process exits. * * @attention none */ void BSL_ERR_DeInit(void); /** * @ingroup bsl_err * @brief Delete the error stack * * Delete the error stack, which is called when a process or thread exits. * * @attention This function must be called when the thread exits. Otherwise, memory leakage occurs. * @param isRemoveAll [IN] Indicates whether to delete all error stacks. * The value is true when a process exits and false when a thread exits. */ void BSL_ERR_RemoveErrorStack(bool isRemoveAll); /** * @ingroup bsl_err * @brief Obtains the error code of the last push in the error stack. * * This API is called when an error occurs on a HiTLS interface to obtain the error code. * The interface can be called continuously. The error code returned each time forms * the error stack of the interface until BSL_SUCCESS is returned. * * @attention None. * @retval Error code. The most significant 16 bits indicate the ID of the module where the error occurs, * and the least significant 16 bits indicate the cause number. */ int32_t BSL_ERR_GetLastError(void); /** * @ingroup bsl_err * @brief Obtains the error code, file name, and line number of the last push message in the error stack. * * When an error occurs in a HiTLS interface, the user obtains an error code, file name, and line * number. The obtained information is deleted from the error stack. * The interface can be called continuously. The error code returned each time forms the error stack of * the interface until BSL_SUCCESS is returned. * * @attention If either of the two parameters is null, the file name and line number cannot be obtained * @param file [OUT] Obtains the name of the file where the error occurs, excluding the directory path * @param lineNo [OUT] Obtain the line number of the file where the error occurs * @retval Error code. The most significant 16 bits indicate the ID of the module where the error occurs, * and the least significant 16 bits indicate the cause number. */ int32_t BSL_ERR_GetLastErrorFileLine(const char **file, uint32_t *lineNo); /** * @ingroup bsl_err * @brief Obtain the error code, file name, and line number of the last push message in the error stack. * * When an error occurs on a HiTLS interface, the user obtains an error code, file name, and line number. * The obtained information is not deleted from the error stack. * * @attention If either of the two parameters is null, the file name and line number cannot be obtained. * @param file [OUT] Obtains the name of the file where the error occurs, excluding the directory path. * @param lineNo [OUT] Obtain the line number of the file where the error occurs. * @retval Error code. The most significant 16 bits indicate the ID of the module where the error occurs, * and the least significant 16 bits indicate the cause number. */ int32_t BSL_ERR_PeekLastErrorFileLine(const char **file, uint32_t *lineNo); /** * @ingroup bsl_err * @brief Obtain the earliest push error code in the error stack. * * This API is called when an error occurs on a HiTLS API to obtain the error code. * The API can be called all the time. * The error code returned each time forms the error stack of the interface until BSL_SUCCESS is returned. * * @attention None. * @retval Error code. The most significant 16 bits indicate the ID of the module where the error occurs, * and the least significant 16 bits indicate the cause number. */ int32_t BSL_ERR_GetError(void); /** * @ingroup bsl_err * @brief Obtain the error code, file name, and line number of the earliest push message in the error stack. * * The user calls this API after an error occurs on a HiTLS API to obtain an error code, * file name, and line number. The obtained information will be deleted from the error stack. * This API can be called continuously. The returned error code forms the error stack of the * API until BSL_SUCCESS is returned. * * @attention If either of the two parameters is null, the file name and line number cannot be obtained. * @param file [OUT] Obtains the name of the file where the error occurs, excluding the directory path. * @param lineNo [OUT] Obtain the line number of the file where the error occurs. * @retval Error code. The most significant 16 bits indicate the ID of the module where the error occurs, * and the least significant 16 bits indicate the cause number. */ int32_t BSL_ERR_GetErrorFileLine(const char **file, uint32_t *lineNo); /** * @ingroup bsl_err * @brief Obtain the error code, file name, and line number of the earliest push message in the error stack. * * When an error occurs on a HiTLS interface, the user obtains an error code, file name, and line number. * The obtained information is not deleted from the error stack. * * @attention If either of the two parameters is null, the file name and line number cannot be obtained. * @param file [OUT] Obtains the name of the file where the error occurs, excluding the directory path. * @param lineNo [OUT] Obtain the line number of the file where the error occurs. * @retval Error code. The most significant 16 bits indicate the ID of the module where the error occurs, * and the least significant 16 bits indicate the cause number. */ int32_t BSL_ERR_PeekErrorFileLine(const char **file, uint32_t *lineNo); /** * @ingroup bsl_err * @brief Clear the error stack. * * If an error is detected after the HiTLS API is called, if the error information is ignored, * call this API to clear the error information before calling the HiTLS API again. * * @attention None */ void BSL_ERR_ClearError(void); /** * @ingroup bsl_err * @brief Add error description. */ typedef struct { int32_t error; /**< Error code */ const char *string; /**< Description string corresponding to an error code. */ } BSL_ERR_Desc; /** * @ingroup bsl_err * @brief Add an error description string to an error code. * * The error description string is added to the error code. * The error description can be extended to the user side. * * @attention This function is thread-safe. It stores only string pointers and does not perform deep * copy. The same error can be added multiple times and overwrites the previously added error. * @param descList [IN] BSL_ERR_Desc array * @param num [IN] Length of descList * @retval #BSL_SUCCESS. * @retval For details, see bsl_errno.h */ int32_t BSL_ERR_AddErrStringBatch(const BSL_ERR_Desc *descList, uint32_t num); /** * @ingroup bsl_err * @brief Delete the error description * * The error description is deleted. * If BSL_ERR_AddErrStringBatch is called, you need to use this API to release the memory. * * @attention This API must be called when a process exits. * Otherwise, memory leakage occurs. Called before releasing the lock. */ void BSL_ERR_RemoveErrStringBatch(void); /** * @ingroup bsl_err * @brief Obtain the error description string based on the error code. * * Obtain the corresponding error description string based on the error code. * * @attention None * @param error [IN] Error code * @retval Error description */ const char *BSL_ERR_GetString(int32_t error); /** * @ingroup bsl_err * @brief Set the pop-up flag at the level of the current error stack. * * Set the pop-up flag. * * @attention none * @retval #BSL_ERR_ERR_ACQUIRE_WRITE_LOCK_FAIL, failed to obtain the write lock. * @retval #BSL_ERR_ERR_NO_STACK, no error stack. * @retval #BSL_ERR_ERR_NO_ERROR, no error. * @retval #BSL_SUCCESS, the flag is set successfully. */ int32_t BSL_ERR_SetMark(void); /** * @ingroup bsl_err * @brief Pop to the marked error stack level and clear the mark * * Pop up to the error stack level of the mark and clear the mark * * @attention none * @retval #BSL_ERR_ERR_ACQUIRE_WRITE_LOCK_FAIL, failed to obtain the write lock. * @retval #BSL_ERR_ERR_NO_STACK, no error stack. * @retval #BSL_ERR_ERR_NO_ERROR, no error. * @retval #BSL_ERR_ERR_NO_Mark, no mark. * @retval #BSL_SUCCESS, pop-up succeeded. */ int32_t BSL_ERR_PopToMark(void); /** * @ingroup bsl_err * @brief Clear the latest flag in the error stack. * * Clear the latest flag in the error stack. * * @attention None. * @retval #BSL_ERR_ERR_ACQUIRE_WRITE_LOCK_FAIL, failed to obtain the write lock. * @retval #BSL_ERR_ERR_NO_STACK, no error stack. * @retval #BSL_SUCCESS, cleared successfully. */ int32_t BSL_ERR_ClearLastMark(void); #ifdef __cplusplus } #endif #endif // BSL_ERR_H
2301_79861745/bench_create
include/bsl/bsl_err.h
C
unknown
10,141
/* * 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 bsl_errno * @ingroup bsl * @brief error number module */ #ifndef BSL_ERRNO_H #define BSL_ERRNO_H #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_errno * @brief Return success */ #define BSL_SUCCESS 0 /** * @ingroup bsl_errno * * Return values of the BSL module range from 0x03000001 to 0x03ffffff. */ enum BSL_ERROR { /* Common return value start from 0x03000001. */ BSL_NULL_INPUT = 0x03000000, /**< NULL input. */ BSL_INTERNAL_EXCEPTION, /**< Error occurs when calling internal BSL functions */ BSL_MALLOC_FAIL, /**< Error occurs when allocating memory */ BSL_MEMCPY_FAIL, /**< Error occurs when calling memcpy_s. */ BSL_MEMMOVE_FAIL, /**< Error occurs when calling memmove. */ BSL_INVALID_ARG, /**< Invalid arguments. */ BSL_DUMP_FAIL, /**< Error occurs when duplicating memory */ /* The return value of the SAL submodule starts from 0x03010001. */ /* The return value of the SAL submodule starts from 0x03010001. */ BSL_SAL_ERR_UNKNOWN = 0x03010001, /**< Unknown error. */ BSL_SAL_ERR_BAD_PARAM, /**< Parameter incorrect. */ BSL_SAL_ERR_FILE_OPEN, /**< Open file error. */ BSL_SAL_ERR_FILE_READ, /**< File reading error. */ BSL_SAL_ERR_FILE_WRITE, /**< File writing error. */ BSL_SAL_ERR_FILE_LENGTH, /**< Obtaining the file length error. */ BSL_SAL_ERR_FILE_TELL, /**< Error in obtaining the file pointer offset. */ BSL_SAL_ERR_FILE_SEEK, /**< Failed to set pointer position of file. */ BSL_SAL_ERR_FILE_SET_ATTR, /**< Setting file attribute is incorrect. */ BSL_SAL_ERR_FILE_GET_ATTR, /**< Error in obtaining file attributes. */ BSL_SAL_FILE_NO_REG_FUNC, BSL_SAL_ERR_DL_NOT_FOUND, /**< dl not found. */ BSL_SAL_ERR_DL_LOAD_FAIL, /**< Error occured when loading dynamic library. */ BSL_SAL_ERR_DL_UNLOAAD_FAIL, /**< Error occured when unloading dynamic library. */ BSL_SAL_ERR_DL_NON_FUNCTION, /**< dl doesn't find function. */ BSL_SAL_ERR_DL_LOOKUP_METHOD, /**< Error occurred when looking up dl method. */ BSL_SAL_ERR_DL_PATH_EXCEED, /**< Path exceeds the maximum length. */ BSL_SAL_DL_NO_REG_FUNC, /**< The dl-related function is not registered. */ BSL_SAL_NOT_FILE_EOF, BSL_SAL_THREAD_LOCK_NO_REG_FUNC, BSL_SAL_ERR_NO_MEMORY, /* The return value of the LOG submodule starts from 0x03020001. */ BSL_LOG_ERR_BAD_PARAM = 0x03020001, /**< Bad parameter. */ /* The return value of the TLV submodule starts from 0x03030001. */ BSL_TLV_ERR_BAD_PARAM = 0x03030001, /**< Bad parameter. */ BSL_TLV_ERR_NO_WANT_TYPE, /**< No TLV found. */ /* The return value of the ERR submodule starts from 0x03040001. */ BSL_ERR_ERR_ACQUIRE_READ_LOCK_FAIL = 0x03040001, /**< Failed to obtain the read lock. */ BSL_ERR_ERR_ACQUIRE_WRITE_LOCK_FAIL, /**< Failed to obtain the write lock. */ BSL_ERR_ERR_NO_STACK, /**< Error stack is empty. */ BSL_ERR_ERR_NO_ERROR, /**< Error stack is NULL. */ BSL_ERR_ERR_NO_MARK, /**< Error stack has no mark. */ BSL_SAL_TIME_NO_REG_FUNC = 0x03050001, BSL_SAL_TIME_SYS_ERROR, /**< Function gettimeofday failed. */ /* The return value of the UIO submodule starts from 0x03060001. */ BSL_UIO_FAIL = 0x03060001, BSL_UIO_IO_EXCEPTION, BSL_UIO_IO_BUSY, BSL_UIO_MEM_GROW_FAIL, BSL_UIO_REF_MAX, BSL_UIO_MEM_ALLOC_FAIL, BSL_UIO_IO_EOF, BSL_UIO_WRITE_NOT_ALLOWED, BSL_UIO_UNINITIALIZED, BSL_UIO_MEM_NOT_NULL, BSL_UIO_CB_NOT_SET, BSL_UIO_CTRL_INVALID_PARAM, BSL_UIO_FILE_OPEN_FAIL, BSL_UIO_EXIST_CONTEXT_NOT_RELEASED, BSL_UIO_BUF_TOO_LONG, /* The return value of the LIST submodule starts from 0x03070001. */ BSL_LIST_INVALID_LIST_CURRENT = 0x03070001, /**< Current node pointer is NULL */ BSL_LIST_MALLOC_FAIL, BSL_LIST_DATA_NOT_AVAILABLE, /**< Data of current node is NULL */ BSL_LIST_FULL, /**< Number of nodes has reached its limit */ /* The return value of the UI submodule starts from 0x03080001. */ BSL_UI_WRITE_ERROR = 0x03080001, BSL_UI_FGETS_ERROR, BSL_UI_STDIN_END_ERROR, BSL_UI_OPERATION_ERROR, BSL_UI_READ_LEN_TOO_SHORT, BSL_UI_READ_BUFF_TOO_LONG, BSL_UI_METHOD_INVALID_TYPE, BSL_UI_CONSTRUCT_PROMPT_ERROR, BSL_UI_CREATE_OBJECT_ERROR, BSL_UI_OUTPUT_BUFF_TOO_SHORT, BSL_UI_INVALID_DATA_TYPE, BSL_UI_INVALID_DATA_ARG, BSL_UI_INVALID_DATA_RESULT, BSL_UI_VERIFY_BUFF_FAILED, BSL_UI_MEM_ALLOC_FAIL, /* The return value of the BASE64 submodule starts from 0x030a0001. */ BSL_BASE64_INVALID = 0x030a0001, BSL_BASE64_BUF_NOT_ENOUGH, BSL_BASE64_DATA_NOT_ENOUGH, BSL_BASE64_WRITE_FAILED, BSL_BASE64_READ_FAILED, BSL_BASE64_DATA_AFTER_PADDING, BSL_BASE64_ILLEGALLY_MODIFIED, BSL_BASE64_ENCODE_FAILED, BSL_BASE64_DECODE_FAILED, BSL_BASE64_HEADER, BSL_BASE64_INVALID_CHARACTER, BSL_BASE64_INVALID_ENCODE, BSL_SAL_ERR_NET_NOBLOCK = 0x030b0001, BSL_SAL_ERR_NET_SOCKCLOSE, /**< Error occured when closing a socket. */ BSL_SAL_ERR_NET_SETSOCKOPT, /**< Error occured when setting a socket option. */ BSL_SAL_ERR_NET_GETSOCKOPT, /**< Error occured when getting a socket option. */ BSL_SAL_ERR_NET_LISTEN, /**< Error occured when listening a socket. */ BSL_SAL_ERR_NET_BIND, /**< Error occured when binding a socket */ BSL_SAL_ERR_NET_CONNECT, /**< Error occured when building a connection. */ BSL_SAL_ERR_NET_IOCTL, /**< Error occured when calling ioctl. */ BSL_SAL_NET_NO_REG_FUNC, BSL_CONF_FAIL = 0x030c0001, BSL_CONF_INIT_FAIL, BSL_CONF_LOAD_FAIL, BSL_CONF_MEM_ALLOC_FAIL, BSL_CONF_FREE_FAIL, BSL_CONF_NOT_NUM, BSL_CONF_PARSE_FAIL, BSL_CONF_COPY_ARGS_FAILED, BSL_CONF_CONTEXT_ERR, BSL_CONF_GET_FAIL, BSL_CONF_VALUE_NOT_FOUND, BSL_CONF_CTRL_INVALID_PARAM, BSL_CONF_DUMP_FAIL, BSL_CONF_BUFF_OVERFLOW, BSL_CONF_INVALID_NAME, BSL_PARAMS_INVALID_KEY = 0x030f0001, BSL_PARAMS_INVALID_TYPE, BSL_PARAMS_LEN_NOT_ENOUGH, BSL_PARAMS_MISMATCH, BSL_PARAMS_OUT_LIMIT, BSL_ASN1_FAIL = 0x03100001, BSL_ASN1_ERR_DECODE_BOOL, BSL_ASN1_ERR_NO_CALLBACK, BSL_ASN1_ERR_MAX_DEPTH, BSL_ASN1_ERR_OVERFLOW, BSL_ASN1_ERR_TAG_EXPECTED, BSL_ASN1_ERR_DECODE_LEN, BSL_ASN1_ERR_MAX_LEN_NUM, BSL_ASN1_ERR_DECODE_INT, BSL_ASN1_ERR_DECODE_BIT_STRING, BSL_ASN1_ERR_DECODE_UTC_TIME, BSL_ASN1_ERR_DECODE_TIME, BSL_ASN1_ERR_DECODE_GENERAL_TIME, BSL_ASN1_ERR_CHECK_TIME, BSL_ASN1_ERR_EXCEED_LIST_DEPTH, BSL_ASN1_ERR_MISMATCH_TAG, BSL_ASN1_ERR_BUFF_NOT_ENOUGH, BSL_ASN1_ERR_ENCODE_FAIL, BSL_ASN1_ERR_ENCODE_ASN_LACK, BSL_ASN1_ERR_ENCODE_ASN_TOO_MUCH, BSL_ASN1_ERR_ENCODE_BOOL, BSL_ASN1_ERR_ENCODE_INT, BSL_ASN1_ERR_ENCODE_BIT_STRING, BSL_ASN1_ERR_ENCODE_UTC_TIME, BSL_ASN1_ERR_ENCODE_GENERALIZED_TIME, BSL_ASN1_ERR_PRINTF, BSL_ASN1_ERR_PRINTF_IO_ERR, BSL_ASN1_ERR_LEN_OVERFLOW, BSL_PEM_INVALID = 0x03110001, BSL_PEM_DATA_NOT_ENOUGH, BSL_PEM_SYMBOL_NOT_FOUND, BSL_PEM_NO_PWD, BSL_PRINT_ERR_FMT = 0x03120001, BSL_PRINT_ERR_BUF, BSL_PRINT_ERR_NUMBER, BSL_OBJ_ERR_INSERT_HASH_TABLE = 0x03130001, BSL_OBJ_ERR_FIND_HASH_TABLE, BSL_OBJ_INVALID_HASH_TABLE, }; #ifdef __cplusplus } #endif #endif // BSL_ERRNO_H
2301_79861745/bench_create
include/bsl/bsl_errno.h
C
unknown
8,622
/* * 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 bsl_init * @ingroup bsl * @brief initialization */ #ifndef BSL_INIT_H #define BSL_INIT_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_init * @brief Initialize the BSL module. * * The user must call this interface to initialize. * * @attention None. * @retval #BSL_SUCCESS, error code module is successfully initialized. * @retval #BSL_MALLOC_FAIL, memory space is insufficient and thread lock space cannot be applied for. * @retval #BSL_SAL_ERR_UNKNOWN, thread lock initialization failed. */ int32_t BSL_GLOBAL_Init(void); /** * @ingroup bsl_init * @brief Deinitialize the BSL module. * * The user calls this interface when the process exits. * * @attention None */ int32_t BSL_GLOBAL_DeInit(void); #ifdef __cplusplus } #endif #endif // BSL_INIT_H
2301_79861745/bench_create
include/bsl/bsl_init.h
C
unknown
1,368
/* * 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 bsl_list * @ingroup bsl * @brief linked list */ #ifndef BSL_LIST_H #define BSL_LIST_H #include <stdint.h> #include "bsl_errno.h" #include "bsl_sal.h" #ifdef __cplusplus extern "C" { #endif /* for handling ASN.1 SET OF type */ /** * @ingroup bsl_list * */ typedef struct BslListNode { struct BslListNode *prev; /**< The previous node in the list */ struct BslListNode *next; /**< The next node in the list */ void *data; /**< This must be the last field of this structure */ } BslListNode; /** * @ingroup bsl_list * */ typedef struct BslList { BslListNode *first; /**< The first node in the list */ BslListNode *last; /**< The last node in the list */ BslListNode *curr; /**< The current node in the list */ int32_t count; /**< count of elements */ int32_t dataSize; /**< Memory needed for each node data */ } BslList; /** * @ingroup bsl_list * * the enum for specifying whether to add the element before/after the * current element. It is used in BSL_LIST_AddElement() * @datastruct BSL_LIST_POS_BEFORE Indication to to add the element before the current element. * @datastruct BSL_LIST_POS_AFTER Indication to to add the element after the current element. * @datastruct BSL_LIST_POS_BEGIN Indication to to add the element at the beginning of the list. * @datastruct BSL_LIST_POS_END Indication to to add the element at the end of the list. */ typedef enum { BSL_LIST_POS_BEFORE, /**< Indication to to add the element before the current element */ BSL_LIST_POS_AFTER, /**< Indication to to add the element after the current element */ BSL_LIST_POS_BEGIN, /**< Indication to to add the element at the beginning of the list */ BSL_LIST_POS_END /**< Indication to to add the element at the end of the list */ } BslListPosition; /** * @ingroup bsl_list * * This is a pointer to the list comparison function used in BSL_LIST_Search function. * It takes two pointers and compares them based on a criteria. If the two are equal a zero is returned. * If the first should preceed the second, a negative is returned. Else a positive value is returned. */ typedef int32_t (*BSL_LIST_PFUNC_CMP)(const void *, const void *); /** * @ingroup bsl_list * * This is a pointer to the free function. * The free function takes a pointer to data structure to be freed and must return void. */ typedef void (*BSL_LIST_PFUNC_FREE)(void *); /** * @ingroup bsl_list * * This is a pointer to the Copy function. * The copy function takes a pointer to data structure to be freed and must return void. */ typedef void *(*BSL_LIST_PFUNC_DUP)(const void *); /* The following macros return the specified element of the list. They do not change the current list pointer. */ /* returns the current element */ #define BSL_LIST_CURR_ELMT(pList) ((pList) ? ((pList)->curr ? ((pList)->curr->data) : NULL) : NULL) /* returns the next element */ #define BSL_LIST_NEXT_ELMT(pList) \ ((pList) ? ((pList)->curr ? ((pList)->curr->next ? ((pList)->curr->next->data) : NULL) : NULL) : NULL) /* returns the previous element */ #define BSL_LIST_PREV_ELMT(pList) \ ((pList) ? ((pList)->curr ? ((pList)->curr->prev ? ((pList)->curr->prev->data) : NULL) : NULL) : NULL) /* returns the last element */ #define BSL_LIST_LAST_ELMT(pList) ((pList) ? ((pList)->last ? ((pList)->last->data) : NULL) : NULL) /* returns the first element */ #define BSL_LIST_FIRST_ELMT(pList) ((pList) ? ((pList)->first ? ((pList)->first->data) : NULL) : NULL) /* checks if the list is NULL */ #define BSL_LIST_EMPTY(pList) (((pList) != NULL) ? ((pList)->count == 0) : 0) /* returns the number of nodes in the list */ #define BSL_LIST_COUNT(pList) ((pList) ? ((pList)->count) : 0) /* checks if current node is the end */ #define BSL_LIST_IS_END(pList) ((pList) ? (NULL == (pList)->curr) : 0) /* checks if current node is the first one */ #define BSL_LIST_IS_START(pList) ((pList) ? ((pList)->first == (pList)->curr) : 0) /* Get the next element */ #define BSL_LIST_GET_NEXT(pList) ((pList) ? (BSL_LIST_Next(pList) ? BSL_LIST_CURR_ELMT(pList) : NULL) : NULL) /* Get the previous element */ #define BSL_LIST_GET_PREV(pList) ((pList) ? (BSL_LIST_Prev(pList) ? BSL_LIST_CURR_ELMT(pList) : NULL) : NULL) /* Get the first element */ #define BSL_LIST_GET_FIRST(pList) ((pList) ? (BSL_LIST_First(pList) ? BSL_LIST_CURR_ELMT(pList) : NULL) : NULL) /* Get the last element */ #define BSL_LIST_GET_LAST(pList) ((pList) ? (BSL_LIST_Last(pList) ? BSL_LIST_CURR_ELMT(pList) : NULL) : NULL) /** * @ingroup bsl_list * * Delete all the nodes in the list and then frees the header */ #define BSL_LIST_FREE(pList, pFreeFunc) \ do { \ BSL_LIST_DeleteAll((pList), pFreeFunc); \ if (NULL != (pList)) { \ BSL_SAL_Free(pList); \ (pList) = NULL; \ } \ } while (0) #define SEC_INT_ERROR (-2) /** * @ingroup bsl_list * * This function sets the max element in BSL_LIST.Default value is 10000000 (10 Million). * * @param iMaxElements [IN] Max allowed element in BSL_LIST. It should be in range[0xffff, 0xfffffff] * @retval #BSL_INVALID_ARG If input falls outside the range. * @retval #BSL_SUCCESS If successful. */ int32_t BSL_LIST_SetMaxElements(int32_t iMaxElements); /** * @ingroup bsl_list * * This function returns the max allowed elements in BSL_LIST. * * @retval int32_t Max configured elements in BSL_LIST */ int32_t BSL_LIST_GetMaxElements(void); /** * @ingroup bsl_list * * This function creates a new node before, after or at the begining or end of the current node. If the list was already * NULL, the node will be added as the only node.The current pointer is changed to point to the newly added node in the * list. If the current pointer is NULL then this operation fails. * * @param pList [IN] The list * @param pData [IN] The element to be added * @param enPosition [IN] Whether the element is to be added before or after the list * @retval The error code. * @retval #BSL_SUCCESS If successful. */ int32_t BSL_LIST_AddElement(BslList *pList, void *pData, BslListPosition enPosition); /** * @ingroup bsl_list * * This function deletes all the nodes of the list but does not delete the list header. * * @param pList [IN] The list * @param pfFreeFunc [IN] The freefunction to free the data pointer in each node */ void BSL_LIST_DeleteAll(BslList *pList, BSL_LIST_PFUNC_FREE pfFreeFunc); /** * @ingroup bsl_list * * This function deletes the current element of list. * * @param pList [IN] The list * @param pfFreeFunc [IN] The pointer to the free function of data */ void BSL_LIST_DeleteCurrent(BslList *pList, BSL_LIST_PFUNC_FREE pfFreeFunc); /** * @ingroup bsl_list * * This function detaches the current element from the list, the current node will be freed, but the data contained * in the current node will not be freed.Also the pList->first, pList->curr and pList->last will be appropriately * updated. If the current node is the last node, then pList->curr will point to its previous node after detachment, * else it will point to its next node. * * @param pList [IN] The list */ void BSL_LIST_DetachCurrent(BslList *pList); /** * @ingroup bsl_list * * This function searches a list based on the comparator function * supplied (3rd param). The second param is given to the * comparator as its second param and each data item on the * list is given as its first param while searching. The * comparator must return 0 to indicate a match. * * @param pList [IN] The list * @param pSearchFor [IN] The element to be searched * @param pSearcher [IN] The pointer to the comparison function of data * @retval Void* The element which was found [Void*] * @retval Void* If none found [NULL] */ void *BSL_LIST_Search(BslList *pList, const void *pSearchFor, BSL_LIST_PFUNC_CMP pSearcher, int32_t *pstErr); /** * @ingroup bsl_list * * This function returns the node at the given index in the list, starting at 0. * * @param pList [IN] The list * @param ulIndex [IN] The index in the list * @retval Void* The element which was found [Void*] * @retval Void* If none found [NULL] */ void *BSL_LIST_GetIndexNode(uint32_t ulIndex, BslList *pList); /** * @ingroup bsl_list * * This function dups a list by copying the list by creating a copy of list * and returns the destinaton list pointer. * * @param pSrcList [IN] The list * @param pFuncCpy [IN] The dup function for the data in the node * @param pfFreeFunc [IN] The pointer to the free function for the data in the node of data * @retval BslList* The duplicated List pointer [BslList*] * @retval BslList* If dup failed or memory allocation fails.[NULL] */ BslList *BSL_LIST_Copy(BslList *pSrcList, BSL_LIST_PFUNC_DUP pFuncCpy, BSL_LIST_PFUNC_FREE pfFreeFunc); /** * @ingroup bsl_list * * This function sorts the list using the comparison function provided. * * @param pList [IN] The list * @param pfCmp [IN] The comparison function * @retval BslList* If unsuccessful [NULL] * @retval BslList* If successful [The destination sorted list] */ BslList *BSL_LIST_Sort(BslList *pList, BSL_LIST_PFUNC_CMP pfCmp); /** * @ingroup bsl_list * * This function is used to create a new list. * * @param dataSize [IN] Size of the data inside the list node * @retval BslList* An NULL list [BslList*] */ BslList *BSL_LIST_New(int32_t dataSize); /** * @ingroup bsl_list * * This function returns the data of the current element in the list. * * @param pstList [IN] Input list * @retval void* Data at the current element in the list [void*] * @retval void* If the current element does not exist in the list [NULL] * @retval void* If memory allocation fails. [NULL] */ void *BSL_LIST_Curr(const BslList *pstList); /** * @ingroup bsl_list * * This function returns the data at the first element of the list. * * @param pstList [IN] the list * @retval void* Data at the first element of the list [void*] * @retval void* If the first element does not exist [NULL] */ void *BSL_LIST_First(BslList *pstList); /** * @ingroup bsl_list * * This function returns the data at the last element of the list. * * @param pstList [IN] The list * @retval void* Data at the last element of the list [void*] * @retval void* If the last element does not exist [NULL] */ void *BSL_LIST_Last(BslList *pstList); /** * @ingroup bsl_list * * This function advances the current pointer by one and returns the data address of the new * current node. If the current pointer is off the list, the new current node * will be the first node of the list (unless the list is NULL). * * @param pstList [IN] The list * @retval void* Pointer to the next element in the list [void*] * @retval void* If the next element does not exist [NULL] */ void *BSL_LIST_Next(BslList *pstList); /** * @ingroup bsl_list * * backs up the current pointer by one and returns the data address of the new * current node. If the current pointer is off the list, the new current node * will be the last node of the list (unless the list is NULL). * * @param pstList [IN] The list * @retval void* Pointer to the previous element in the list [void*] * @retval void* If the previous element does not exist[NULL] */ void *BSL_LIST_Prev(BslList *pstList); /** * @ingroup bsl_list * * This function returns the index (starting a 0 for the first element) * of the given element in the given list. * Returns -1, if the element is not in the list. * Assumes that the list node contains a single pointer. * * @param elmt [IN] The element whose index is to be retrieved * @param pstList [IN] The list to which the element belongs to * @retval int32_t The index of the specified element in the given list [int32_t] * @retval int32_t If the element is not found in the list [-1] */ int32_t BSL_LIST_GetElmtIndex(const void *elmt, BslList *pstList); /** * @ingroup bsl_list * * This function is used to concatenate list 2 to list 1. * * @param pDestList [IN] The list to which the 2nd list is to be concatenated to. * @param pSrcList [IN] The list which is to be concatenated. * @retval BslList* The concatenated list. [BslList*] */ BslList *BSL_LIST_Concat(BslList *pDestList, const BslList *pSrcList); /** * @ingroup bsl_list * * This function is used to free the Asn list. * * @param pstList [IN] list Pointer to the Asn list which has to be freed * @retval void This function does not return any value. */ void BSL_LIST_FreeWithoutData(BslList *pstList); /** * @ingroup bsl_list * * This function is used to reverse the linked list. * * @param pstList [IN] Pointer to the list which has to be reversed * @retval void This function does not return any value. */ void BSL_LIST_RevList(BslList *pstList); /** * @ingroup bsl_list * * This function set the max qsort Size.Default value is 100000 * * @param uiQsortSize [IN] Max Buff Size. it should in range of [10000, 67108864] Default value is 100000 * @retval int32_t BSL_SUCCESS on success BSL_INVALID_ARG on Failure. */ int32_t BSL_LIST_SetMaxQsortCount(uint32_t uiQsortSize); /** * @ingroup bsl_list * * This function returns the MAX qsort Size * * @retval uint32_t Returns the max qsort Size. */ uint32_t BSL_LIST_GetMaxQsortCount(void); /** * @ingroup bsl_list * * Delete all the nodes in the list. * But it does not delete the data pointers inside the list nodes. * It is used only after sort to delete the input list to the sort function. * * @param pList [IN] The list. */ void BSL_LIST_DeleteAllAfterSort(BslList *pList); /** * @ingroup bsl_list * * This function returns the first element of the list. * * @param list [IN] The list. * @retval BslListNode* first element of the list [BslListNode*] * @retval BslListNode* If the first element does not exist [NULL] */ BslListNode *BSL_LIST_FirstNode(const BslList *list); /** * @ingroup bsl_list * * This function returns the data of the passed list node. * * @param pstNode [IN] The node. * @retval void* Data of the passed list node. [void*] * @retval void* If the data is not present in the list node. [NULL] */ void *BSL_LIST_GetData(const BslListNode *pstNode); /** * @ingroup bsl_list * * This function advances the current reference pointer by one and returns the * new current node. If the current reference pointer is off the list, * the new current node will be the first node of the list * (unless the list is NULL). * * @param pstList [IN] The list. * @param pstListNode [IN] The list node. * @retval BslListNode* Pointer to next element in the list. [void*] * @retval BslListNode* If the next element does not exist. [NULL] */ BslListNode *BSL_LIST_GetNextNode(const BslList *pstList, const BslListNode *pstListNode); /** * @ingroup bsl_list * * This function backs up the current reference pointer by one and returns the * new current node. * * @param pstListNode [IN] The list node. * @retval BslListNode* Pointer to the previous element in the list * @retval BslListNode* If the previous element does not exist[NULL] */ BslListNode *BSL_LIST_GetPrevNode(const BslListNode *pstListNode); /** * @ingroup bsl_list * * This function deletes the matching input node from the input list. * * @param pstList [IN] The list. * @param pstListNode [IN] The current reference node. * @param pfFreeFunc [IN] The pointer to the free function of data. */ void BSL_LIST_DeleteNode(BslList *pstList, const BslListNode *pstListNode, BSL_LIST_PFUNC_FREE pfFreeFunc); /** * @ingroup bsl_list * * This function detaches the matching input node from the input list. * The node will be freed but, the data contained in the * node will not be freed, and also the pList->first, pList->curr, * and pList->last will be appropriately updated. If the matching node * is the last node, then pList->curr will point to its previous node * after detachment, else it will point to its next node. * * @param pstList [IN] The list. * @param pstListNode [in/out] when it is input parameter, it is the list node to be detached. */ void BSL_LIST_DetachNode(BslList *pstList, BslListNode **pstListNode); #ifdef __cplusplus } #endif /* __cplusplus */ #endif // BSL_LIST_H
2301_79861745/bench_create
include/bsl/bsl_list.h
C
unknown
16,927
/* * 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 bsl_log * @ingroup bsl * @brief log module */ #ifndef BSL_LOG_H #define BSL_LOG_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_log * * Audit log level */ #define BSL_LOG_LEVEL_SEC 0U /** * @ingroup bsl_log * * Emergency log level */ #define BSL_LOG_LEVEL_FATAL 1U /** * @ingroup bsl_log * * Error log level */ #define BSL_LOG_LEVEL_ERR 2U /** * @ingroup bsl_log * * Warning log level */ #define BSL_LOG_LEVEL_WARN 3U /** * @ingroup bsl_log * * Information log level */ #define BSL_LOG_LEVEL_INFO 4U /** * @ingroup bsl_log * * Debug log level */ #define BSL_LOG_LEVEL_DEBUG 5U /** * @ingroup bsl_log * * HiTLS version string */ #ifndef OPENHITLS_VERSION_S #define OPENHITLS_VERSION_S "openHiTLS 0.2.0 15 May 2025" #endif #ifndef OPENHITLS_VERSION_I #define OPENHITLS_VERSION_I 0x00200000ULL #endif #define HITLS_VERSION_LEN 150 /** * @ingroup bsl_log * @brief Obtain the openHiTLS version string. * * @attention The length of the received version string must be greater than or equal to HITLS_VERSION_LEN. * @param version [OUT] openHiTLS current version string. * @param versionLen [IN/OUT] String length of the current openHiTLS version. * @retval #BSL_SUCCESS, if success. * @retval #BSL_LOG_ERR_MEMCPY, memory copy failure. */ int32_t BSL_LOG_GetVersion(char *version, uint32_t *versionLen); /** * @ingroup bsl_log * @brief Obtain the openHiTLS version number. * * @retval openHiTLS version number. */ uint64_t BSL_LOG_GetVersionNum(void); /** * @ingroup bsl_log * @brief Binlog type, other types can be extended. */ #define BSL_LOG_BINLOG_TYPE_RUN 0x01 /** * @ingroup bsl_log * @brief Fixed-length callback type of binlogs. * * The function format of this type cannot contain %s, the number of parameters is less than four, add 0s. * More than four parameters must be called multiple times. */ typedef void (*BSL_LOG_BinLogFixLenFunc)(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para1, void *para2, void *para3, void *para4); /** * @ingroup bsl_log * @brief Callback type for variable-length binlogs. * * This type function format contains only one %s, If no %s exists, use BSL_LOG_BinLogFixLenFunc type. * If there are more than one %s, call the interface multiple times. */ typedef void (*BSL_LOG_BinLogVarLenFunc)(uint32_t logId, uint32_t logLevel, uint32_t logType, void *format, void *para); /** * @ingroup bsl_log * @brief Register the parameter type of the binlog callback function. */ typedef struct { BSL_LOG_BinLogFixLenFunc fixLenFunc; /**< 4 parameter callback */ BSL_LOG_BinLogVarLenFunc varLenFunc; /**< 1 parameter callback */ } BSL_LOG_BinLogFuncs; /** * @ingroup bsl_log * @brief Set the fixed-length and variable-length callback function for binlogs. * * @attention The input parameter can be NULL. * @param funcs [IN] Callback function pointer collection of the binlog. * The parameter cannot be null, but the member of the structure can be null. * @retval #BSL_SUCCESS. */ int32_t BSL_LOG_RegBinLogFunc(const BSL_LOG_BinLogFuncs *funcs); /** * @ingroup bsl_log * @brief Set the level of binlogs. * * @attention The level must be valid. * @param level [IN] Level of the binlogs. The valid values are BSL_LOG_LEVEL_SEC, BSL_LOG_LEVEL_FATAL, * BSL_LOG_LEVEL_ERR, BSL_LOG_LEVEL_WARN, BSL_LOG_LEVEL_INFO, BSL_LOG_LEVEL_DEBUG * @retval #BSL_SUCCESS. * @retval #BSL_LOG_ERR_BAD_PARAM, invalid input parameter. */ int32_t BSL_LOG_SetBinLogLevel(uint32_t level); /** * @ingroup bsl_log * @brief Obtain the level of binlogs. * * @retval Level of the binlog. The value can be BSL_LOG_LEVEL_SEC, BSL_LOG_LEVEL_FATAL, BSL_LOG_LEVEL_ERR, * BSL_LOG_LEVEL_WARN, BSL_LOG_LEVEL_INFO, BSL_LOG_LEVEL_DEBUG */ uint32_t BSL_LOG_GetBinLogLevel(void); #ifdef __cplusplus } #endif #endif // BSL_LOG_H
2301_79861745/bench_create
include/bsl/bsl_log.h
C
unknown
4,550
/* * 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 bsl_obj * @ingroup bsl * @brief object module */ #ifndef BSL_OBJ_H #define BSL_OBJ_H #include <stdbool.h> #include <stdint.h> #include "bsl_types.h" #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_obj * All algorithm ID */ typedef enum { BSL_CID_UNKNOWN = 0, /**< Unknown alg id */ BSL_CID_RC4 = 1, /* identifies the RC4 algorithm */ BSL_CID_DES_ECB = 2, /* identifies DES algorithm in ECB mode */ BSL_CID_DES_CBC = 3, /* identifies DES algorithm in CBC mode */ BSL_CID_DES_OFB = 4, /* identifies DES algorithm in OFB mode */ BSL_CID_DES_CFB = 5, /* identifies DES algorithm in CFB mode */ BSL_CID_SCB2_128_ECB = 6, /* identifies SCB2-128 algorithm in ECB mode */ BSL_CID_SCB2_128_CBC = 7, /* identifies SCB2-128 algorithm in CBC mode */ BSL_CID_SCB2_256_ECB = 8, /* identifies SCB2-256 algorithm in ECB mode */ BSL_CID_SCB2_256_CBC = 9, BSL_CID_DES_EDE_ECB = 10, /* identifies 2 key triple DES algorithm in ECB mode */ BSL_CID_DES_EDE_CBC = 11, /* identifies 2 key triple DES algorithm in CBC mode */ BSL_CID_DES_EDE_OFB = 12, /* identifies 2 key triple DES algorithm in OFB mode */ BSL_CID_DES_EDE_CFB = 13, /* identifies 2 key triple DES algorithm in CFB mode */ BSL_CID_DES_EDE3_ECB = 14, /* identifies 3 key triple DES algorithm in ECB mode */ BSL_CID_DES_EDE3_CBC = 15, /* identifies 3 key triple DES algorithm in CBC mode */ BSL_CID_DES_EDE3_OFB = 16, /* identifies 3 key triple DES algorithm in OFB mode */ BSL_CID_DES_EDE3_CFB = 17, /* identifies 3 key triple DES algorithm in CFB mode */ BSL_CID_AES128_ECB = 18, /* identifies AES-128 algorithm in ECB mode */ BSL_CID_AES128_CBC = 19, /* identifies AES-128 algorithm in CBC mode */ BSL_CID_AES128_OFB = 20, /* identifies AES-128 algorithm in OFB mode */ BSL_CID_AES128_CFB = 21, /* identifies AES-128 algorithm in CFB mode */ BSL_CID_AES192_ECB = 22, /* identifies AES-192 algorithm in ECB mode */ BSL_CID_AES192_CBC = 23, /* identifies AES-192 algorithm in CBC mode */ BSL_CID_AES192_OFB = 24, /* identifies AES-192 algorithm in OFB mode */ BSL_CID_AES192_CFB = 25, /* identifies AES-192 algorithm in CFB mode */ BSL_CID_AES256_ECB = 26, /* identifies AES-256 algorithm in ECB mode */ BSL_CID_AES256_CBC = 27, /* identifies AES-256 algorithm in CBC mode */ BSL_CID_AES256_OFB = 28, /* identifies AES-256 algorithm in OFB mode */ BSL_CID_AES256_CFB = 29, /* identifies AES-256 algorithm in CFB mode */ BSL_CID_KASUMI_ECB = 30, /* identifies Kasumi algorithm in ECB mode */ BSL_CID_KASUMI_CBC = 31, /* identifies Kasumi algorithm in CBC mode */ BSL_CID_KASUMI_OFB = 32, /* identifies Kasumi algorithm in OFB mode */ BSL_CID_KASUMI_CFB = 33, /* identifies Kasumi algorithm in CFB mode */ BSL_CID_RSA = 34, /* identifies the RSA algorithm */ BSL_CID_DSA = 35, /* identifies the DSA algorithm */ BSL_CID_ECDSA = 36, /* identifies the ECDSA algorithm */ BSL_CID_ECDSA192 = 37, /* identifies the ECDSA192 algorithm */ BSL_CID_DH = 38, /* identifies the Diffie-Hellman algorithm */ BSL_CID_ECDH = 39, /* identifies the EC Diffie-Hellman algorithm */ BSL_CID_MD5 = 40, /* identifies the MD5 hash algorithm */ BSL_CID_SHA1 = 41, /* identifies the SHA1 hash algorithm */ BSL_CID_SHA224 = 42, /* identifies the SHA224 hash algorithm */ BSL_CID_SHA256 = 43, /* identifies the SHA256 hash algorithm */ BSL_CID_SHA384 = 44, /* identifies the SHA384 hash algorithm */ BSL_CID_SHA512 = 45, /* identifies the SHA512 hash algorithm */ BSL_CID_HMAC_MD5 = 46, /* identifies hmac with MD5 */ BSL_CID_HMAC_SHA1 = 47, /* identifies hmac with SHA1 */ BSL_CID_HMAC_SHA224 = 48, /* identifies hmac with SHA224 */ BSL_CID_HMAC_SHA256 = 49, /* identifies hmac with SHA256 */ BSL_CID_HMAC_SHA384 = 50, /* identifies hmac with SHA384 */ BSL_CID_HMAC_SHA512 = 51, /* identifies hmac with SHA512 */ BSL_CID_MD5WITHRSA = 52, /* identifies signature using MD5 and RSA */ BSL_CID_SHA1WITHRSA = 53, /* identifies signature using SHA1 and RSA */ BSL_CID_SHA1WITHRSAOLD = 54, /* identifies signature using SHA1 and RSA (coresponds to old Oid) */ BSL_CID_DSAWITHSHA1 = 55, /* identifies signature using SHA1 and DSA */ BSL_CID_DSAWITHSHA1_2 = 56, /* identifies signature using SHA1 and DSA */ BSL_CID_ECDSAWITHSHA1 = 57, /* identifies signature using SHA1 and ECDSA */ BSL_CID_ECDSAWITHSHA224 = 58, /* identifies signature using SHA224 and ECDSA */ BSL_CID_ECDSAWITHSHA256 = 59, /* identifies signature using SHA256 and ECDSA */ BSL_CID_ECDSAWITHSHA384 = 60, /* identifies signature using SHA384 and ECDSA */ BSL_CID_ECDSAWITHSHA512 = 61, /* identifies signature using SHA512 and ECDSA */ BSL_CID_ECDSA192WITHSHA256 = 62, /* identifies signature using SHA256 and ECDSA-192 bit */ BSL_CID_SHA256WITHRSAENCRYPTION = 63, /* identifies signature using SHA256 and RSA */ BSL_CID_SHA384WITHRSAENCRYPTION = 64, /* identifies signature using SHA384 and RSA */ BSL_CID_SHA512WITHRSAENCRYPTION = 65, /* identifies signature using SHA512 and RSA */ /* RFC 3279 */ BSL_CID_KEYEXCHANGEALGORITHM = 66, /* identifies Key exchange algorithm */ BSL_CID_PKCS1 = 67, /* identifies PKCS1 */ BSL_CID_ANSI_X9_62 = 68, /* identifies ANSI_X9_62 */ BSL_CID_ECSIGTYPE = 69, /* identifies ECSIGTYPE */ BSL_CID_FIELDTYPE = 70, /* identifies Field Type */ BSL_CID_PRIME_FIELD = 71, /* identifies PRIME Field */ BSL_CID_CHARACTERISTIC_TWO_FIELD = 72, /* identifies Characterstic Two field */ BSL_CID_CHARACTERISTIC_TWO_BASIS = 73, /* identifies Characterstic Two Basis */ BSL_CID_GNBASIS = 74, /* identifies GNBASIS */ BSL_CID_TPBASIS = 75, /* identifies TPBASIS */ BSL_CID_PPBASIS = 76, /* identifies PPBASIS */ BSL_CID_PUBLICKEYTYPE = 77, /* identifies PUBLICKEYTYPE */ BSL_CID_ELLIPTICCURVE = 78, /* identifies ELLIPTICCURVE */ BSL_CID_C_TWOCURVE = 79, /* identifies C_TWOCURVE */ BSL_CID_C2PNB163V1 = 80, /* identifies C2PNB163V1 */ BSL_CID_C2PNB163V2 = 81, /* identifies C2PNB163V2 */ BSL_CID_C2PNB163V3 = 82, /* identifies C2PNB163V3 */ BSL_CID_C2PNB176W1 = 83, /* identifies C2PNB176W1 */ BSL_CID_C2TNB191V1 = 84, /* identifies C2TNB191V1 */ BSL_CID_C2TNB191V2 = 85, /* identifies C2TNB191V2 */ BSL_CID_C2TNB191V3 = 86, /* identifies C2TNB191V3 */ BSL_CID_C2ONB191V4 = 87, /* identifies C2ONB191V4 */ BSL_CID_C2ONB191V5 = 88, /* identifies C2ONB191V5 */ BSL_CID_C2PNB208W1 = 89, /* identifies C2PNB208W1 */ BSL_CID_C2TNB239V1 = 90, /* identifies C2TNB239V1 */ BSL_CID_C2TNB239V2 = 91, /* identifies C2TNB239V2 */ BSL_CID_C2TNB239V3 = 92, /* identifies C2TNB239V3 */ BSL_CID_C2ONB239V4 = 93, /* identifies C2ONB239V4 */ BSL_CID_C2ONB239V5 = 94, /* identifies C2ONB239V5 */ BSL_CID_C2PNB272W1 = 95, /* identifies C2PNB272W1 */ BSL_CID_C2PNB304W1 = 96, /* identifies C2PNB304W1 */ BSL_CID_C2TNB359V1 = 97, /* identifies C2TNB359V1 */ BSL_CID_C2PNB368W1 = 98, /* identifies C2PNB368W1 */ BSL_CID_C2TNB431R1 = 99, /* identifies C2TNB431R1 */ BSL_CID_PRIMECURVE = 100, /* identifies PRIMECURVE */ BSL_CID_PRIME192V1 = 101, /* identifies PRIME192V1 */ BSL_CID_PRIME192V2 = 102, /* identifies PRIME192V2 */ BSL_CID_PRIME192V3 = 103, /* identifies PRIME192V3 */ BSL_CID_PRIME239V1 = 104, /* identifies PRIME239V1 */ BSL_CID_PRIME239V2 = 105, /* identifies PRIME239V2 */ BSL_CID_PRIME239V3 = 106, /* identifies PRIME239V3 */ BSL_CID_PRIME256V1 = 107, /* identifies PRIME256V1 */ /* SCEP */ BSL_CID_VERISIGN = 108, /* identifies VERISIGN */ BSL_CID_PKI = 109, /* identifies PKI */ BSL_CID_ATTRIBUTES = 110, /* identifies ATTRIBUTES */ BSL_CID_MESSAGETYPE = 111, /* identifies MESSAGETYPE */ BSL_CID_PKISTATUS = 112, /* identifies PKISTATUS */ BSL_CID_FAILINFO = 113, /* identifies FAILINFO */ BSL_CID_SENDERNONCE = 114, /* identifies SENDERNONCE */ BSL_CID_RECIPIENTNONCE = 115, /* identifies RECIPIENTNONCE */ BSL_CID_TRANSID = 116, /* identifies TRANSID */ BSL_CID_EXTENSIONREQ = 117, /* identifies EXTENSIONREQ */ /* PKCS 5 */ BSL_CID_RSADSI = 118, /* identifies RSADSI */ BSL_CID_PKCS = 119, /* identifies PKCS */ BSL_CID_PKCS5 = 120, /* identifies PKCS5 */ BSL_CID_PBKDF2 = 121, /* identifies PBKDF2 */ BSL_CID_PBE_MD2WITHDESCBC = 122, BSL_CID_PBE_MD2WITHRC2CBC = 123, BSL_CID_PBE_MD5WITHDESCBC = 124, /* identifies PBE_MD5WITHDESCBC */ BSL_CID_PBE_MD5WITHRC2CBC = 125, BSL_CID_PBE_SHA1WITHDESCBC = 126, /* identifies PBE_SHA1WITHDESCBC */ BSL_CID_PBE_SHA1WITHRC2CBC = 127, BSL_CID_PBES2 = 128, /* identifies PBES2 */ BSL_CID_PBMAC1 = 129, /* identifies PBMAC1 */ BSL_CID_DIGESTALGORITHM = 130, /* identifies DIGESTALGORITHM */ BSL_CID_ENCRYPTIONALGORITHM = 131, /* identifies ENCRYPTIONALGORITHM */ BSL_CID_RC2CBC = 132, /* identifies RC2CBC */ BSL_CID_RC5_CBC_PAD = 133, /* identifies RC5_CBC_PAD */ BSL_CID_RSAES_OAEP = 134, /* from pkcs1 */ /* identifies RSAES_OAEP */ /* OCSP */ BSL_CID_PKIX_OCSP_BASIC = 135, /* identifies OCSP_BASIC */ BSL_CID_PKIX_OCSP_NONCE = 136, /* identifies OCSP_NONCE */ BSL_CID_PKIX_OCSP_CRL = 137, /* identifies OCSP_CRL */ BSL_CID_PKIX_OCSP_RESPONSE = 138, /* identifies OCSP_RESPONSE */ BSL_CID_PKIX_OCSP_NOCHECK = 139, /* identifies OCSP_NOCHECK */ BSL_CID_PKIX_OCSP_ARCHIVE_CUTOFF = 140, /* identifies OCSP_ARCHIVE_CUTOFF */ BSL_CID_PKIX_OCSP_SERVICE_LOCATOR = 141, /* identifies OCSP_SERVICE_LOCATOR */ /* PKCS 10 */ BSL_CID_CHALLENGE_PWD_ATTR = 142, /* identifies Challenge PWD Attr */ BSL_CID_EXTENSIONREQUEST = 143, /* identifies EXTENSIONREQUEST */ /* FROM PKIXEXPLICIT */ BSL_CID_PKIX = 144, /* identifies PKIX */ BSL_CID_PE = 145, /* identifies PE */ BSL_CID_QT = 146, /* identifies QT */ BSL_CID_KP = 147, /* identifies KP */ BSL_CID_AD = 148, /* identifies AD */ BSL_CID_QT_CPS = 149, /* identifies CPS */ BSL_CID_QT_UNOTICE = 150, /* identifies UNOTICE */ BSL_CID_AD_OCSP = 151, /* identifies OCSP */ BSL_CID_AD_CAISSUERS = 152, /* identifies CAISSUERS */ BSL_CID_AD_TIMESTAMPING = 153, /* identifies TIMESTAMPING */ BSL_CID_AD_CAREPOSITORY = 154, /* identifies CAREPOSITORY */ BSL_CID_AT = 155, /* identifies AT */ BSL_CID_AT_NAME = 156, /* identifies NAME */ BSL_CID_AT_SURNAME = 157, /* identifies SURNAME */ BSL_CID_AT_GIVENNAME = 158, /* identifies GIVENNAME */ BSL_CID_AT_INITIALS = 159, /* identifies INITIALS */ BSL_CID_AT_GENERATIONQUALIFIER = 160, /* identifies GENERATIONQUALIFIER */ BSL_CID_AT_COMMONNAME = 161, /* identifies COMMONNAME */ BSL_CID_AT_LOCALITYNAME = 162, /* identifies LOCALITYNAME */ BSL_CID_AT_STATEORPROVINCENAME = 163, /* identifies STATEORPROVINCENAME */ BSL_CID_AT_ORGANIZATIONNAME = 164, /* identifies ORGANIZATIONNAME */ BSL_CID_AT_ORGANIZATIONALUNITNAME = 165, /* identifies ORGANIZATIONALUNITNAME */ BSL_CID_AT_TITLE = 166, /* identifies TITLE */ BSL_CID_AT_DNQUALIFIER = 167, /* identifies DNQUALIFIER */ BSL_CID_AT_COUNTRYNAME = 168, /* identifies COUNTRYNAME */ BSL_CID_AT_SERIALNUMBER = 169, /* identifies SERIALNUMBER */ BSL_CID_AT_PSEUDONYM = 170, /* identifies PSEUDONYM */ BSL_CID_DOMAINCOMPONENT = 171, /* identifies DOMAINCOMPONENT */ BSL_CID_EMAILADDRESS = 172, /* identifies EMAILADDRESS */ /* PKIXIMPLICIT */ BSL_CID_CE = 173, /* identifies CE */ BSL_CID_CE_AUTHORITYKEYIDENTIFIER = 174, /* identifies AUTHORITYKEYIDENTIFIER */ BSL_CID_CE_SUBJECTKEYIDENTIFIER = 175, /* identifies SUBJECTKEYIDENTIFIER */ BSL_CID_CE_KEYUSAGE = 176, /* identifies KEYUSAGE */ BSL_CID_CE_PRIVATEKEYUSAGEPERIOD = 177, /* identifies PRIVATEKEYUSAGEPERIOD */ BSL_CID_CE_CERTIFICATEPOLICIES = 178, /* identifies CERTIFICATEPOLICIES */ BSL_CID_ANYPOLICY = 179, /* identifies ANYPOLICY */ BSL_CID_CE_POLICYMAPPINGS = 180, /* identifies POLICYMAPPINGS */ BSL_CID_CE_SUBJECTALTNAME = 181, /* identifies SUBJECTALTNAME */ BSL_CID_CE_ISSUERALTNAME = 182, /* identifies ISSUERALTNAME */ BSL_CID_CE_SUBJECTDIRECTORYATTRIBUTES = 183, /* identifies SUBJECTDIRECTORYATTRIBUTES */ BSL_CID_CE_BASICCONSTRAINTS = 184, /* identifies BASICCONSTRAINTS */ BSL_CID_CE_NAMECONSTRAINTS = 185, /* identifies NAMECONSTRAINTS */ BSL_CID_CE_POLICYCONSTRAINTS = 186, /* identifies POLICYCONSTRAINTS */ BSL_CID_CE_CRLDISTRIBUTIONPOINTS = 187, /* identifies CRLDISTRIBUTIONPOINTS */ BSL_CID_CE_EXTKEYUSAGE = 188, /* identifies EXTKEYUSAGE */ BSL_CID_ANYEXTENDEDKEYUSAGE = 189, /* identifies ANYEXTENDEDKEYUSAGE */ BSL_CID_KP_SERVERAUTH = 190, /* identifies SERVERAUTH */ BSL_CID_KP_CLIENTAUTH = 191, /* identifies CLIENTAUTH */ BSL_CID_KP_CODESIGNING = 192, /* identifies CODESIGNING */ BSL_CID_KP_EMAILPROTECTION = 193, /* identifies EMAILPROTECTION */ BSL_CID_KP_TIMESTAMPING = 194, /* identifies TIMESTAMPING */ BSL_CID_KP_OCSPSIGNING = 195, /* identifies OCSPSIGNING */ BSL_CID_KP_IPSECIKE = 196, /* identifies IPSECIKE */ BSL_CID_CE_INHIBITANYPOLICY = 197, /* identifies INHIBITANYPOLICY */ BSL_CID_CE_FRESHESTCRL = 198, /* identifies FRESHESTCRL */ BSL_CID_PE_AUTHORITYINFOACCESS = 199, /* identifies AUTHORITYINFOACCESS */ BSL_CID_PE_SUBJECTINFOACCESS = 200, /* identifies SUBJECTINFOACCESS */ BSL_CID_CE_CRLNUMBER = 201, /* identifies CRLNUMBER */ BSL_CID_CE_ISSUINGDISTRIBUTIONPOINT = 202, /* identifies ISSUINGDISTRIBUTIONPOINT */ BSL_CID_CE_DELTACRLINDICATOR = 203, /* identifies DELTACRLINDICATOR */ BSL_CID_CE_CRLREASONS = 204, /* identifies CRLREASONS */ BSL_CID_CE_CERTIFICATEISSUER = 205, /* identifies CERTIFICATEISSUER */ BSL_CID_CE_HOLDINSTRUCTIONCODE = 206, /* identifies HOLDINSTRUCTIONCODE */ BSL_CID_HOLDINSTRUCTION = 207, /* identifies HOLDINSTRUCTION */ BSL_CID_HOLDINSTRUCTION_NONE = 208, /* identifies HOLDINSTRUCTION_NONE */ BSL_CID_HOLDINSTRUCTION_CALLISSUER = 209, /* identifies HOLDINSTRUCTION_CALLISSUER */ BSL_CID_HOLDINSTRUCTION_REJECT = 210, /* identifies HOLDINSTRUCTION_REJECT */ BSL_CID_CE_INVALIDITYDATE = 211, /* identifies INVALIDITYDATE */ BSL_CID_PDA_DATEOFBIRTH = 212, /* identifies DATEOFBIRTH */ BSL_CID_PDA_PLACEOFBIRTH = 213, /* identifies PLACEOFBIRTH */ BSL_CID_PDA_GENDER = 214, /* identifies GENDER */ BSL_CID_PDA_COUNTRYOFCITIZENSHIP = 215, /* identifies COUNTRYOFCITIZENSHIP */ BSL_CID_PDA_COUNTRYOFRESIDENCE = 216, /* identifies COUNTRYOFRESIDENCE */ BSL_CID_PDA = 217, /* identifies PDA */ BSL_CID_ON_PERMANENTIDENTIFIER = 218, /* identifies PERMANENTIDENTIFIER */ BSL_CID_ON = 219, /* identifies ON */ BSL_CID_CE_DOMAININFO = 220, /* identifies DOMAININFO */ /* CMP */ BSL_CID_PASSWORDBASEDMAC = 221, /* identifies PWD Based MAC */ BSL_CID_DHBASEDMAC = 222, /* identifies DH Based MAC */ BSL_CID_IT = 223, /* identifies IT */ BSL_CID_CAPROTENCCERT = 224, /* identifies CAPROTENCCERT */ BSL_CID_SIGNKEYPAIRTYPES = 225, /* identifies Sign KeyPair Types */ BSL_CID_ENCKEYPAIRTYPES = 226, /* identifies KeyPair Types */ BSL_CID_PREFERREDSYMMALG = 227, /* identifies Preferred Symmetric Algo */ BSL_CID_CAKEYUPDATEINFO = 228, /* identifies CA Key Update Info */ BSL_CID_CURRENTCRL = 229, /* identifies Current CRL */ BSL_CID_CONFIRMWAITTIME = 230, /* identifies ConfirmWaitTime */ /* CRMF */ BSL_CID_PKIP = 231, /* identifies PKIP */ BSL_CID_REGCTRL = 232, /* identifies REGCTRL */ BSL_CID_REGCTRL_REGTOKEN = 233, /* identifies REGTOKEN */ BSL_CID_REGCTRL_AUTHENTICATOR = 234, /* identifies AUTHENTICATOR */ BSL_CID_REGCTRL_PKIPUBLICATIONINFO = 235, /* identifies PKIPUBLICATIONINFO */ BSL_CID_REGCTRL_PKIARCHIVEOPTIONS = 236, /* identifies PKIARCHIVEOPTIONS */ BSL_CID_REGCTRL_OLDCERTID = 237, /* identifies OLDCERTID */ BSL_CID_REGCTRL_PROTOCOLENCRKEY = 238, /* identifies PROTOCOLENCRKEY */ BSL_CID_REGINFO = 239, /* identifies REGINFO */ BSL_CID_REGINFO_UTF8PAIRS = 240, /* identifies UTF8PAIRS */ BSL_CID_REGINFO_CERTREQ = 241, /* identifies CERTREQ */ /* PKCS12 */ BSL_CID_PKCS12 = 242, /* identifies PKCS12 */ BSL_CID_PKCS12PBEIDS = 243, /* identifies PKCS12 PBE */ BSL_CID_PBE_SHAWITH128BITRC4 = 244, /* identifies PBE Algo (SHAWITH128BITRC4) */ BSL_CID_PBE_SHAWITH40BITRC4 = 245, /* identifies PBE Algo (SHAWITH40BITRC4) */ BSL_CID_PBE_SHAWITH3KEY_TRIPLE_DESCBC = 246, /* identifies PBE Algo (SHAWITH3KEY_TRIPLE_DESCBC) */ BSL_CID_PBE_SHAWITH2KEY_TRIPLE_DESCBC = 247, /* identifies PBE Algo (SHAWITH2KEY_TRIPLE_DESCBC) */ BSL_CID_PBE_SHAWITH128BIT_RC2CBC = 248, /* identifies PBE Algo (SHAWITH128BIT_RC2CBC) */ BSL_CID_PBE_SHAWITH40BIT_RC2CBC = 249, /* identifies PBE Algo (SHAWITH40BIT_RC2CBC) */ BSL_CID_BAGTYPES = 250, /* identifies Bag Types */ BSL_CID_KEYBAG = 251, /* identifies Key Bag */ BSL_CID_PKCS8SHROUDEDKEYBAG = 252, /* identifies Bag Types */ BSL_CID_CERTBAG = 253, /* identifies CERT Bag */ BSL_CID_CRLBAG = 254, /* identifies CRL Bag */ BSL_CID_SECRETBAG = 255, /* identifies Secret Bag */ BSL_CID_SAFECONTENTSBAG = 256, /* identifies Safe Content Bag */ BSL_CID_X509CERTIFICATE = 257, /* identifies x509 Certificate */ BSL_CID_SDSICERTIFICATE = 258, /* identifies SDSI Certificate */ BSL_CID_FRIENDLYNAME = 259, /* identifies Freidnly Name */ BSL_CID_LOCALKEYID = 260, /* identifies Local Key ID */ /* auth_frame */ BSL_CID_CERTIFICATEREVOCATIONLIST = 261, /* identifies Certificate Revocation List */ /* PKCS7 & 9 */ BSL_CID_PKCS7 = 262, /* identifies PKCS7 */ BSL_CID_PKCS7_SIMPLEDATA = 263, /* identifies PKCS7 Simple Data */ BSL_CID_PKCS7_SIGNEDDATA = 264, /* identifies PKCS7 Signed Data */ BSL_CID_PKCS7_ENVELOPEDDATA = 265, /* identifies PKCS7 Enveloped Data */ BSL_CID_PKCS7_SIGNED_ENVELOPEDDATA = 266, /* identifies PKCS7 Signed Enveloped Data */ BSL_CID_PKCS7_DIGESTEDDATA = 267, /* identifies PKCS7 Degested Data */ BSL_CID_PKCS7_ENCRYPTEDDATA = 268, /* identifies PKCS7 Encrypted Data */ BSL_CID_PKCS9 = 269, /* identifies PKCS9 */ BSL_CID_PKCS9_AT_CONTENTTYPE = 270, /* identifies PKCS9 Content Type */ BSL_CID_PKCS9_AT_MESSAGEDIGEST = 271, /* identifies PKCS9 Message Digest */ BSL_CID_PKCS9_AT_SIGNINGTIME = 272, /* identifies PKCS9 Signing time */ BSL_CID_PKCS9_AT_COUNTERSIGNATURE = 273, /* identifies PKCS9 Counter Signature */ BSL_CID_PKCS9_AT_RANDOMNONCE = 274, /* identifies PKCS9 Signed Enveloped Data */ BSL_CID_PKCS9_AT_SEQUENCENUMBER = 275, /* identifies PKCS9 Sequence number */ BSL_CID_MD4 = 276, /* identifies MD4 hash algorithm */ BSL_CID_HMAC_MD4 = 277, /* identifies hmac with MD4 */ BSL_CID_CMAC_AES = 278, /* identifies CMAC-AES */ BSL_CID_CMAC_TDES = 279, /* identifies CMAC-Triple DES */ BSL_CID_RNG_HW = 280, /* identifies TRNG */ BSL_CID_RNG_SW = 281, /* identifies PRNG */ BSL_CID_XCBC_AES = 282, /* identifies XCBC-MAC-AES */ BSL_CID_RC2_ECB = 283, /* identifies RC2 algorithm in ECB mode */ BSL_CID_RC2_CBC = 284, /* identifies RC2 algorithm in CBC mode */ BSL_CID_RC2_OFB = 285, /* identifies RC2 algorithm in OFB mode */ BSL_CID_RC2_CFB = 286, /* identifies RC2 algorithm in CFB mode */ BSL_CID_MD5_SHA1 = 287, BSL_CID_SECP384R1 = 288, /* identifies NIST prime curve 384 */ BSL_CID_SECP521R1 = 289, /* identifies NIST prime curve 521 */ BSL_CID_SM3 = 290, /* identifies SM3 hash algorithm */ BSL_CID_HMAC_SM3 = 291, /* identifies hmac with SM3 */ BSL_CID_SM2DSAWITHSM3 = 292, /* identifies BSL_CID_SM2DSAWITHSM3 */ BSL_CID_SM2DSAWITHSHA1 = 293, /* identifies BSL_CID_SM2DSAWITHSHA1 */ BSL_CID_SM2DSAWITHSHA256 = 294, /* identifies BSL_CID_SM2DSAWITHSHA256 */ BSL_CID_SM2PRIME256 = 295, /* identifies BSL_CID_PRIME256SM2 */ BSL_CID_SM2DSA = 296, /* identifies SM2 DSA */ BSL_CID_SM2KEP = 297, /* BSL_CID_SM2KEP */ BSL_CID_SM2PKEA = 298, /* BSL_CID_SM2PKEA */ BSL_CID_AES128_GCM = 299, /* Identifies the AES128 algorithm in GCM mode */ BSL_CID_AES192_GCM = 300, /* Identifies the AES128 algorithm in GCM mode */ BSL_CID_AES256_GCM = 301, /* Identifies the AES256 algorithm in GCM mode */ BSL_CID_AES128_CTR = 302, /* Identifies the AES128 algorithm in CTR mode */ BSL_CID_AES192_CTR = 303, /* Identifies the AES128 algorithm in CTR mode */ BSL_CID_AES256_CTR = 304, /* Identifies the AES128 algorithm in CTR mode */ BSL_CID_UNSTRUCTURED_NAME = 305, /* identifies unstructuredName */ BSL_CID_UNSTRUCTURED_ADDR = 306, /* identifies unstructuredAddress */ BSL_CID_BF_ECB = 307, /* Identifies the Blowfish algorithm in ECB mode */ BSL_CID_BF_CBC = 308, /* Identifies the Blowfish algorithm in CBC mode */ BSL_CID_BF_CFB = 309, /* Identifies the Blowfish algorithm in CFB mode */ BSL_CID_BF_OFB = 310, /* Identifies the Blowfish algorithm in OFB mode */ BSL_CID_AES128_CCM = 311, BSL_CID_AES192_CCM = 312, BSL_CID_AES256_CCM = 313, BSL_CID_AT_STREETADDRESS = 314, /* Identifies the streetAddress in EV certs */ BSL_CID_AT_BUSINESSCATEGORY = 315, /* Identifies the businessCategory in EV certs */ BSL_CID_AT_POSTALCODE = 316, /* Identifies the postalCode in EV certs */ BSL_CID_JD_LOCALITYNAME = 317, /* Identifies the streetAddress in EV certs */ BSL_CID_JD_STATEORPROVINCENAME = 318, /* Identifies the jurisdictionLocalityName in EV certs */ BSL_CID_JD_COUNTRYNAME = 319, /* Identifies the jurisdictionCountryName in EV certs */ BSL_CID_HMAC_SHA1_DIGEST = 320, BSL_CID_NIST_PRIME224 = 321, /* NIST Curve P-224 */ BSL_CID_NIST_C2PNB163K = 322, /* NIST Binary Curve 163K */ BSL_CID_NIST_C2PNB163B = 323, /* NIST Binary Curve 163B */ BSL_CID_NIST_C2TNB233K = 324, /* NIST Binary Curve 233K */ BSL_CID_NIST_C2TNB233B = 325, /* NIST Binary Curve 233B */ BSL_CID_NIST_C2PNB283K = 326, /* NIST Binary Curve 283K */ BSL_CID_NIST_C2PNB283B = 327, /* NIST Binary Curve 283B */ BSL_CID_NIST_C2TNB409K = 328, /* NIST Binary Curve 409K */ BSL_CID_NIST_C2TNB409B = 329, /* NIST Binary Curve 409B */ BSL_CID_NIST_C2PNB571K = 330, /* NIST Binary Curve 571K */ BSL_CID_NIST_C2PNB571B = 331, /* NIST Binary Curve 571B */ BSL_CID_PBE_HMACSHA512WITHAES256_CBC = 332, BSL_CID_CE_SKAE = 333, /* Identifies SKAE extension */ BSL_CID_ED25519 = 334, /* Identifies ED25519 algorithm */ BSL_CID_X25519 = 335, /* Identifies X25519 algorithm */ BSL_CID_RSASSAPSS = 336, /* Identifies RSASSAPSS algorithm */ BSL_CID_MGF1 = 337, /* Identifies MaskGen algorithm */ BSL_CID_SCRYPT = 338, /* Identifieds Scrypt KDF algorithm */ BSL_CID_PBES1 = 339, /* Identifieds PBES1 KDF algorithm */ BSL_CID_KDF2 = 340, /* Identifieds KDF2 KDF algorithm */ BSL_CID_DOT16KDF = 341, /* Identifieds dot16 KDF algorithm */ BSL_CID_SM4 = 342, /* Identifieds SM4 algorithm */ BSL_CID_SM4_ECB = 343, /* Identifieds SM4 ECB algorithm */ BSL_CID_SM4_CBC = 344, /* Identifieds SM4 CBC algorithm */ BSL_CID_KWRAP_AES = 345, /* Identifieds AES KWRAP algorithm */ BSL_CID_KWRAP_SM4 = 346, /* Identifieds SM4 KWRAP algorithm */ BSL_CID_CMAC_SM4 = 347, /* identifies CMAC SM4 */ BSL_CID_SM3WITHRSAENCRYPTION = 348, /* identifies signature using SM3 and RSA */ BSL_CID_HARDWAREMODULENAME = 349, BSL_CID_AT_DESCRIPTION = 350, BSL_CID_DECODE_UNKNOWN = 1000, BSL_CID_NULL = 1001, BSL_CID_HMAC_SHA3_224 = 2000, /* identifies hmac with SHA3_224 */ BSL_CID_HMAC_SHA3_256 = 2001, /* identifies hmac with SHA3_256 */ BSL_CID_HMAC_SHA3_384 = 2002, /* identifies hmac with SHA3_384 */ BSL_CID_HMAC_SHA3_512 = 2003, /* identifies hmac with SHA3_512 */ BSL_CID_DSAWITHSHA256 = 2004, /* identifies signature using SHA256 and DSA */ BSL_CID_DSAWITHSHA224 = 2005, /* identifies signature using SHA224 and DSA */ BSL_CID_DSAWITHSHA384 = 2006, /* identifies signature using SHA384 and DSA */ BSL_CID_DSAWITHSHA512 = 2007, /* identifies signature using SHA512 and DSA */ BSL_CID_SHA224WITHRSAENCRYPTION = 2008, /* identifies signature using SHA224 and RSA */ BSL_CID_SHA3_224 = 2009, BSL_CID_SHA3_256 = 2010, BSL_CID_SHA3_384 = 2011, BSL_CID_SHA3_512 = 2012, BSL_CID_SHAKE128 = 2013, BSL_CID_SHAKE256 = 2014, BSL_CID_HMAC_MD5_SHA1 = 2015, BSL_CID_CMAC_AES128 = 2016, BSL_CID_CMAC_AES192 = 2017, BSL_CID_CMAC_AES256 = 2018, BSL_CID_GMAC_AES128 = 2019, BSL_CID_GMAC_AES192 = 2020, BSL_CID_GMAC_AES256 = 2021, BSL_CID_AES128_XTS = 2022, BSL_CID_AES256_XTS = 2023, BSL_CID_AES128_WRAP_NOPAD = 2024, BSL_CID_AES192_WRAP_NOPAD = 2025, BSL_CID_AES256_WRAP_NOPAD = 2026, BSL_CID_AES128_WRAP_PAD = 2027, BSL_CID_AES192_WRAP_PAD = 2028, BSL_CID_AES256_WRAP_PAD = 2029, BSL_CID_CHACHA20_POLY1305 = 2030, BSL_CID_SM4_XTS = 2031, BSL_CID_SM4_CTR = 2032, BSL_CID_SM4_GCM = 2033, BSL_CID_SM4_CFB = 2034, BSL_CID_SM4_OFB = 2035, BSL_CID_KDFTLS12 = 2036, BSL_CID_HKDF = 2037, BSL_CID_RAND_SHA1 = 2038, BSL_CID_RAND_SHA224 = 2039, BSL_CID_RAND_SHA256 = 2040, BSL_CID_RAND_SHA384 = 2041, BSL_CID_RAND_SHA512 = 2042, BSL_CID_RAND_SM3 = 2043, BSL_CID_RAND_HMAC_SHA1 = 2044, BSL_CID_RAND_HMAC_SHA224 = 2045, BSL_CID_RAND_HMAC_SHA256 = 2046, BSL_CID_RAND_HMAC_SHA384 = 2047, BSL_CID_RAND_HMAC_SHA512 = 2048, BSL_CID_RAND_AES128_CTR = 2049, BSL_CID_RAND_AES192_CTR = 2050, BSL_CID_RAND_AES256_CTR = 2051, BSL_CID_RAND_AES128_CTR_DF = 2052, BSL_CID_RAND_AES192_CTR_DF = 2053, BSL_CID_RAND_AES256_CTR_DF = 2054, BSL_CID_RAND_SM4_CTR_DF = 2055, BSL_CID_ED448 = 2056, BSL_CID_X448 = 2057, BSL_CID_DH_RFC2409_768 = 2060, BSL_CID_DH_RFC2409_1024 = 2061, BSL_CID_DH_RFC3526_1536 = 2062, BSL_CID_DH_RFC3526_2048 = 2063, BSL_CID_DH_RFC3526_3072 = 2064, BSL_CID_DH_RFC3526_4096 = 2065, BSL_CID_DH_RFC3526_6144 = 2066, BSL_CID_DH_RFC3526_8192 = 2067, BSL_CID_DH_RFC7919_2048 = 2068, BSL_CID_DH_RFC7919_3072 = 2069, BSL_CID_DH_RFC7919_4096 = 2070, BSL_CID_DH_RFC7919_6144 = 2071, BSL_CID_DH_RFC7919_8192 = 2072, BSL_CID_ECC_BRAINPOOLP256R1 = 2073, BSL_CID_ECC_BRAINPOOLP384R1 = 2074, BSL_CID_ECC_BRAINPOOLP512R1 = 2075, BSL_CID_SIPHASH64 = 2076, BSL_CID_SIPHASH128 = 2077, // Netscape BSL_CID_NETSCAPE = 2078, BSL_CID_NS_CERTEXT = 2079, BSL_CID_NS_DATATYPE = 2080, BSL_CID_NS_CERTTYPE = 2081, BSL_CID_NS_BASEURL = 2082, BSL_CID_NS_REVOCATIOPNURL = 2083, BSL_CID_NS_CAREVOCATIONURL = 2084, BSL_CID_NS_RENEWALURL = 2085, BSL_CID_NS_CAPOLICYURL = 2086, BSL_CID_NS_SSLSERVERNAME = 2087, BSL_CID_NS_COMMENT = 2088, BSL_CID_NS_CERTSEQUENCE = 2089, BSL_CID_NS_SGC = 2090, BSL_CID_EC192WAPI = 2091, BSL_CID_CBC_MAC_SM4 = 2092, BSL_CID_EC_PUBLICKEY = 2093, /* identifies EC_PUBLICKEY */ BSL_CID_AT_USERID = 2094, BSL_CID_PKCS7_CONTENTINFO = 2095, BSL_CID_PKCS12KDF = 2096, BSL_CID_ML_KEM = 2100, BSL_CID_ML_DSA = 2101, BSL_CID_HYBRID_KEM = 2102, BSL_CID_X25519_MLKEM512 = 2103, BSL_CID_X25519_MLKEM768 = 2104, BSL_CID_X25519_MLKEM1024 = 2105, BSL_CID_X448_MLKEM512 = 2106, BSL_CID_X448_MLKEM768 = 2107, BSL_CID_X448_MLKEM1024 = 2108, BSL_CID_ECDH_NISTP256_MLKEM512 = 2109, BSL_CID_ECDH_NISTP256_MLKEM768 = 2110, BSL_CID_ECDH_NISTP256_MLKEM1024 = 2111, BSL_CID_ECDH_NISTP384_MLKEM512 = 2112, BSL_CID_ECDH_NISTP384_MLKEM768 = 2113, BSL_CID_ECDH_NISTP384_MLKEM1024 = 2114, BSL_CID_ECDH_NISTP521_MLKEM512 = 2115, BSL_CID_ECDH_NISTP521_MLKEM768 = 2116, BSL_CID_ECDH_NISTP521_MLKEM1024 = 2117, BSL_CID_ML_DSA_44 = 2120, BSL_CID_ML_DSA_65 = 2121, BSL_CID_ML_DSA_87 = 2122, BSL_CID_ML_KEM_512 = 2123, BSL_CID_ML_KEM_768 = 2124, BSL_CID_ML_KEM_1024 = 2125, BSL_CID_SLH_DSA_SHA2_128S = 2126, BSL_CID_SLH_DSA_SHAKE_128S = 2127, BSL_CID_SLH_DSA_SHA2_128F = 2128, BSL_CID_SLH_DSA_SHAKE_128F = 2129, BSL_CID_SLH_DSA_SHA2_192S = 2130, BSL_CID_SLH_DSA_SHAKE_192S = 2131, BSL_CID_SLH_DSA_SHA2_192F = 2132, BSL_CID_SLH_DSA_SHAKE_192F = 2133, BSL_CID_SLH_DSA_SHA2_256S = 2134, BSL_CID_SLH_DSA_SHAKE_256S = 2135, BSL_CID_SLH_DSA_SHA2_256F = 2136, BSL_CID_SLH_DSA_SHAKE_256F = 2137, BSL_CID_XMSS_SHA2_10_256 = 2138, BSL_CID_XMSS_SHA2_16_256 = 2139, BSL_CID_XMSS_SHA2_20_256 = 2140, BSL_CID_XMSS_SHA2_10_512 = 2141, BSL_CID_XMSS_SHA2_16_512 = 2142, BSL_CID_XMSS_SHA2_20_512 = 2143, BSL_CID_XMSS_SHAKE_10_256 = 2144, BSL_CID_XMSS_SHAKE_16_256 = 2145, BSL_CID_XMSS_SHAKE_20_256 = 2146, BSL_CID_XMSS_SHAKE_10_512 = 2147, BSL_CID_XMSS_SHAKE_16_512 = 2148, BSL_CID_XMSS_SHAKE_20_512 = 2149, BSL_CID_XMSS_SHA2_10_192 = 2150, BSL_CID_XMSS_SHA2_16_192 = 2151, BSL_CID_XMSS_SHA2_20_192 = 2152, BSL_CID_XMSS_SHAKE256_10_256 = 2153, BSL_CID_XMSS_SHAKE256_16_256 = 2154, BSL_CID_XMSS_SHAKE256_20_256 = 2155, BSL_CID_XMSS_SHAKE256_10_192 = 2156, BSL_CID_XMSS_SHAKE256_16_192 = 2157, BSL_CID_XMSS_SHAKE256_20_192 = 2158, BSL_CID_XMSSMT_SHA2_20_2_256 = 2159, BSL_CID_XMSSMT_SHA2_20_4_256 = 2160, BSL_CID_XMSSMT_SHA2_40_2_256 = 2161, BSL_CID_XMSSMT_SHA2_40_4_256 = 2162, BSL_CID_XMSSMT_SHA2_40_8_256 = 2163, BSL_CID_XMSSMT_SHA2_60_3_256 = 2164, BSL_CID_XMSSMT_SHA2_60_6_256 = 2165, BSL_CID_XMSSMT_SHA2_60_12_256 = 2166, BSL_CID_XMSSMT_SHA2_20_2_512 = 2167, BSL_CID_XMSSMT_SHA2_20_4_512 = 2168, BSL_CID_XMSSMT_SHA2_40_2_512 = 2169, BSL_CID_XMSSMT_SHA2_40_4_512 = 2170, BSL_CID_XMSSMT_SHA2_40_8_512 = 2171, BSL_CID_XMSSMT_SHA2_60_3_512 = 2172, BSL_CID_XMSSMT_SHA2_60_6_512 = 2173, BSL_CID_XMSSMT_SHA2_60_12_512 = 2174, BSL_CID_XMSSMT_SHAKE_20_2_256 = 2175, BSL_CID_XMSSMT_SHAKE_20_4_256 = 2176, BSL_CID_XMSSMT_SHAKE_40_2_256 = 2177, BSL_CID_XMSSMT_SHAKE_40_4_256 = 2178, BSL_CID_XMSSMT_SHAKE_40_8_256 = 2179, BSL_CID_XMSSMT_SHAKE_60_3_256 = 2180, BSL_CID_XMSSMT_SHAKE_60_6_256 = 2181, BSL_CID_XMSSMT_SHAKE_60_12_256 = 2182, BSL_CID_XMSSMT_SHAKE_20_2_512 = 2183, BSL_CID_XMSSMT_SHAKE_20_4_512 = 2184, BSL_CID_XMSSMT_SHAKE_40_2_512 = 2185, BSL_CID_XMSSMT_SHAKE_40_4_512 = 2186, BSL_CID_XMSSMT_SHAKE_40_8_512 = 2187, BSL_CID_XMSSMT_SHAKE_60_3_512 = 2188, BSL_CID_XMSSMT_SHAKE_60_6_512 = 2189, BSL_CID_XMSSMT_SHAKE_60_12_512 = 2190, BSL_CID_XMSSMT_SHA2_20_2_192 = 2191, BSL_CID_XMSSMT_SHA2_20_4_192 = 2192, BSL_CID_XMSSMT_SHA2_40_2_192 = 2193, BSL_CID_XMSSMT_SHA2_40_4_192 = 2194, BSL_CID_XMSSMT_SHA2_40_8_192 = 2195, BSL_CID_XMSSMT_SHA2_60_3_192 = 2196, BSL_CID_XMSSMT_SHA2_60_6_192 = 2197, BSL_CID_XMSSMT_SHA2_60_12_192 = 2198, BSL_CID_XMSSMT_SHAKE256_20_2_256 = 2199, BSL_CID_XMSSMT_SHAKE256_20_4_256 = 2200, BSL_CID_XMSSMT_SHAKE256_40_2_256 = 2201, BSL_CID_XMSSMT_SHAKE256_40_4_256 = 2202, BSL_CID_XMSSMT_SHAKE256_40_8_256 = 2203, BSL_CID_XMSSMT_SHAKE256_60_3_256 = 2204, BSL_CID_XMSSMT_SHAKE256_60_6_256 = 2205, BSL_CID_XMSSMT_SHAKE256_60_12_256 = 2206, BSL_CID_XMSSMT_SHAKE256_20_2_192 = 2207, BSL_CID_XMSSMT_SHAKE256_20_4_192 = 2208, BSL_CID_XMSSMT_SHAKE256_40_2_192 = 2209, BSL_CID_XMSSMT_SHAKE256_40_4_192 = 2210, BSL_CID_XMSSMT_SHAKE256_40_8_192 = 2211, BSL_CID_XMSSMT_SHAKE256_60_3_192 = 2212, BSL_CID_XMSSMT_SHAKE256_60_6_192 = 2213, BSL_CID_XMSSMT_SHAKE256_60_12_192 = 2214, BSL_CID_CE_ISSUERALTERNATIVENAME = 2300, BSL_CID_CE_AUTHORITYINFORMATIONACCESS = 2301, BSL_CID_SM9 = 5201, BSL_CID_ECC_SM9 = 5202, BSL_CID_PAILLIER = 5203, BSL_CID_ELGAMAL = 5204, BSL_CID_SLH_DSA = 5205, /**< Identifies SLH-DSA algorithm */ BSL_CID_RSASSAPSAE = 5206, /**< Identifies RSASSAPSAE algorithm */ BSL_CID_XMSS = 5207, BSL_CID_MAC_AEAD = 5300, BSL_CID_AES128_CCM8, BSL_CID_AES256_CCM8, BSL_CID_PBKDF1, /* identifies PBKDF1 */ BSL_CID_MAX, BSL_CID_EXTEND = 0x60000000, } BslCid; typedef struct { uint32_t octetLen; char *octs; uint32_t flags; } BslOidString; /** * @ingroup bsl_obj * @brief Create an object identifier mapping * @param[in] octs The octs buff for octets * @param[in] octetLen The length of the octs buff * @param[in] oidName The name of the object identifier * @param[in] cid The algorithm ID to map to * @return HITLS_OK on success, error code on failure */ int32_t BSL_OBJ_Create(char *octs, uint32_t octetLen, const char *oidName, int32_t cid); /** * @ingroup bsl_obj * @brief Create a signature algorithm ID mapping * @param[in] signId The signature algorithm ID * @param[in] asymId The asymmetric algorithm ID * @param[in] hashId The hash algorithm ID * @return HITLS_OK on success, error code on failure */ int32_t BSL_OBJ_CreateSignId(int32_t signId, int32_t asymId, int32_t hashId); /** * @ingroup bsl_obj * @brief Get the object identifier string from the algorithm ID * @param[in] inputCid The algorithm ID * @return The object identifier string */ BslOidString *BSL_OBJ_GetOID(BslCid ulCID); /** * @ingroup bsl_obj * @brief Get the algorithm ID from the object identifier string * @param[in] oid The object identifier string * @return The algorithm ID */ BslCid BSL_OBJ_GetCID(const BslOidString *oidStr); /** * @ingroup bsl_obj * @brief Get oid number string from the hex string * @param[in] oid The hex string * @param[in] len The hex string length * @return Oid number string */ char *BSL_OBJ_GetOidNumericString(const uint8_t *oid, uint32_t len); /** * @ingroup bsl_obj * @brief Get oid hex string from the number string * @param[in] oid The number string * @param[in] len The number string length * @param[out] outLen The length of hex string * @return Oid hex number */ uint8_t *BSL_OBJ_GetOidFromNumericString(const char *oid, uint32_t len, uint32_t *outLen); #ifdef __cplusplus } #endif #endif // BSL_OBJ_H
2301_79861745/bench_create
include/bsl/bsl_obj.h
C
unknown
37,541
/* * 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 bsl_param * @ingroup bsl * @brief bsl param */ #ifndef BSL_PARAMS_H #define BSL_PARAMS_H #include <stdint.h> #include <stddef.h> #include "bsl_list.h" #ifdef __cplusplus extern "C" { #endif #define BSL_PARAM_END {0, 0, NULL, 0, 0} typedef enum { BSL_PARAM_TYPE_UINT32_PTR, BSL_PARAM_TYPE_OCTETS_PTR, BSL_PARAM_TYPE_FUNC_PTR, BSL_PARAM_TYPE_CTX_PTR, BSL_PARAM_TYPE_UINT8, BSL_PARAM_TYPE_UINT16, BSL_PARAM_TYPE_UINT32, BSL_PARAM_TYPE_UINT64, BSL_PARAM_TYPE_BOOL, BSL_PARAM_TYPE_INT32, BSL_PARAM_TYPE_OCTETS, BSL_PARAM_TYPE_UTF8_STR, BSL_PARAM_TYPE_SIZE_T, BSL_PARAM_TYPE_SIZE_T_PTR } BSL_PARAM_VALUE_TYPE; typedef struct BslParam { int32_t key; uint32_t valueType; void *value; uint32_t valueLen; uint32_t useLen; } BSL_Param; typedef struct BslParamMaker BSL_ParamMaker; /** * @brief Initialize a BSL parameter structure * @details Initializes a single BSL_Param structure by setting its key, type, value, and length * * @param param [IN] Pointer to the BSL_Param structure to be initialized * @param key [IN] Parameter key value, refer to crypt_params_key.h * @param type [IN] Parameter value type, refer to BSL_PARAM_VALUE_TYPE enum * @param val [IN] Pointer to the parameter value * @param valueLen [IN] Length of the parameter value * * @return int32_t Returns the operation result * - BSL_SUCCESS indicates successful initialization * - Other values indicate initialization failure */ int32_t BSL_PARAM_InitValue(BSL_Param *param, int32_t key, uint32_t type, void *val, uint32_t valueLen); /** * @brief Set BSL parameter value * @details Updates the value in an existing BSL_Param structure * * @param param [IN] Pointer to the BSL_Param structure * @param key [IN] Parameter key value, refer to crypt_params_key.h * @param type [IN] Parameter value type, refer to BSL_PARAM_VALUE_TYPE enum * @param val [IN] Pointer to the new parameter value * @param len [IN] Length of the new parameter value * * @return int32_t Returns the operation result * - BSL_SUCCESS indicates successful setting * - Other values indicate setting failure */ int32_t BSL_PARAM_SetValue(BSL_Param *param, int32_t key, uint32_t type, void *val, uint32_t len); /** * @brief Get pointer to BSL parameter value * @details Retrieves a pointer to the parameter value for the specified key without copying data * * @param param [IN] Pointer to the BSL_Param structure * @param key [IN] Parameter key value, refer to crypt_params_key.h * @param type [IN] Parameter value type, refer to BSL_PARAM_VALUE_TYPE enum * @param val [OUT] Pointer to store the parameter value pointer * @param valueLen [OUT] Pointer to store the parameter value length * * @return int32_t Returns the operation result * - BSL_SUCCESS indicates successful retrieval * - Other values indicate retrieval failure */ int32_t BSL_PARAM_GetPtrValue(const BSL_Param *param, int32_t key, uint32_t type, void **val, uint32_t *valueLen); /** * @brief Get BSL parameter value * @details Retrieves the parameter value for the specified key by copying data to the provided buffer * * @param param [IN] Pointer to the BSL_Param structure * @param key [IN] Parameter key value, refer to crypt_params_key.h * @param type [IN] Parameter value type, refer to BSL_PARAM_VALUE_TYPE enum * @param val [OUT] Buffer pointer to store the parameter value * @param valueLen [OUT] Pointer to store the parameter value length * * @return int32_t Returns the operation result * - BSL_SUCCESS indicates successful retrieval * - Other values indicate retrieval failure */ int32_t BSL_PARAM_GetValue(const BSL_Param *param, int32_t key, uint32_t type, void *val, uint32_t *valueLen); /** * @brief Find BSL parameter by key * @details Searches for a parameter with the specified key in the parameter array * * @param param [IN] Pointer to the BSL_Param structure array * @param key [IN] Parameter key value to search for * * @return const BSL_Param* Returns pointer to the found parameter * - Non-NULL indicates the parameter was found * - NULL indicates the parameter was not found */ const BSL_Param *BSL_PARAM_FindConstParam(const BSL_Param *param, int32_t key); /** * @brief Find BSL parameter by key * @details Searches for a parameter with the specified key in the parameter array * * @param param [IN] Pointer to the BSL_Param structure array * @param key [IN] Parameter key value to search for * * @return BSL_Param* Returns pointer to the found parameter * - Non-NULL indicates the parameter was found * - NULL indicates the parameter was not found */ BSL_Param *BSL_PARAM_FindParam(BSL_Param *param, int32_t key); /** * @brief Free BSL parameter * * @param param [IN] Pointer to the BSL_Param structure array */ void BSL_PARAM_Free(BSL_Param *param); /** * @brief Create a BSL parameter maker * * @return BSL_ParamMaker* Returns pointer to the parameter maker */ BSL_ParamMaker *BSL_PARAM_MAKER_New(void); /** * @brief Push value to BSL parameter maker * * @param maker [IN] Pointer to the BSL_ParamMaker structure * @param key [IN] Parameter key value, refer to crypt_params_key.h * @param type [IN] Parameter value type, refer to BSL_PARAM_VALUE_TYPE enum * @param value [IN] Pointer to the new parameter value * @param len [IN] Length of the new parameter value * * @return int32_t Returns the operation result * - BSL_SUCCESS indicates successful setting * - Other values indicate setting failure */ int32_t BSL_PARAM_MAKER_PushValue(BSL_ParamMaker *maker, int32_t key, uint32_t type, void *value, uint32_t len); /** * @brief Push value to BSL parameter maker * * @param maker [IN] Pointer to the BSL_ParamMaker structure * @param key [IN] Parameter key value, refer to crypt_params_key.h * @param type [IN] Parameter value type, refer to BSL_PARAM_VALUE_TYPE enum * @param value [IN] Pointer to the new parameter value * @param len [IN] Length of the new parameter value * * @return int32_t Returns the operation result * - BSL_SUCCESS indicates successful setting * - Other values indicate setting failure * @note The value will be depth copy. */ int32_t BSL_PARAM_MAKER_DeepPushValue(BSL_ParamMaker *maker, int32_t key, uint32_t type, void *value, uint32_t len); /** * @brief BSL parameter maker to BSL parameter * * @param param [IN] Pointer to the BSL_Param structure array * * @return BSL_ParamMaker* Returns pointer to the parameter maker */ BSL_Param *BSL_PARAM_MAKER_ToParam(BSL_ParamMaker *maker); /** * @brief Free BSL parameter maker * * @param param [IN] Pointer to the BSL_Param structure array */ void BSL_PARAM_MAKER_Free(BSL_ParamMaker *maker); #ifdef __cplusplus } #endif #endif
2301_79861745/bench_create
include/bsl/bsl_params.h
C
unknown
7,431
/* * 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 bsl_sal * @ingroup bsl * @brief System Abstraction Layer */ #ifndef BSL_SAL_H #define BSL_SAL_H #include <stdint.h> #include <stdbool.h> #include <stddef.h> #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_sal * * Thread lock handle, the corresponding structure is provided by the user during registration. */ typedef void *BSL_SAL_ThreadLockHandle; /** * @ingroup bsl_sal * * Thread handle, the corresponding structure is provided by the user during registration. */ typedef void *BSL_SAL_ThreadId; /** * @ingroup bsl_sal * * mutex */ typedef void *BSL_SAL_Mutex; /** * @ingroup bsl_sal * * Condition handle, the corresponding structure is provided by the user during registration. */ typedef void *BSL_SAL_CondVar; /** * @ingroup bsl_sal * @brief Allocate memory space. * * Allocate memory space. * * @attention None * @param size [IN] Size of the allocated memory * @retval If the application is successful, returned the pointer pointing to the memory. * @retval If the application failed, return NULL. */ void *BSL_SAL_Malloc(uint32_t size); /** * @ingroup bsl_sal * @brief Allocate and clear the memory space. * * Allocate and clear the memory space. The maximum size of UINT32_MAX is allocated. * * @attention num*size should not have overflow wrap. * @param num [IN] Number of allocated memory. * @param size [IN] Size of each memory. * @retval If the application is successful, returned the pointer pointing to the memory. * @retval If the application failed, return NULL. */ void *BSL_SAL_Calloc(uint32_t num, uint32_t size); /** * @ingroup bsl_sal * @brief Duplicate the memory space. * * @param src Source memory address * @param size Total memory size * @retval If the allocation is successful, returned the pointer pointing to the memory. * @retval If the allocation failed, return NULL. */ void *BSL_SAL_Dump(const void *src, uint32_t size); /** * @ingroup bsl_sal * @brief Release the specified memory. * * Release the specified memory. * * @attention NONE. * @param value [IN] Pointer to the memory space to be released. */ void BSL_SAL_Free(void *value); /** * @ingroup bsl_sal * @brief Memory expansion * * Memory expansion function. * * @attention None. * @param addr [IN] Original memory address. * @param newSize [IN] Extended memory size. * @param oldSize [IN] Memory size before expansion. * @retval void* indicates successful, the extended memory address is returned. * @retval NULL indicates failed, return NULL. */ void *BSL_SAL_Realloc(void *addr, uint32_t newSize, uint32_t oldSize); /** * @ingroup bsl_sal * @brief Set sensitive information to zero. * * @param ptr [IN] Memory to be zeroed * @param size [IN] Length of the memory to be zeroed out */ void BSL_SAL_CleanseData(void *ptr, uint32_t size); /** * @ingroup bsl_sal * @brief Clear sensitive information and release memory. * * @param ptr [IN] Pointer to the memory to be released * @param size [IN] Length of the memory to be zeroed out */ void BSL_SAL_ClearFree(void *ptr, uint32_t size); #define BSL_SAL_FREE(value_) \ do { \ if ((value_) != NULL) { \ BSL_SAL_Free((void *)(value_)); \ (value_) = NULL; \ } \ } while (0) #define BSL_SAL_ONCE_INIT 0 // equal to PTHREAD_ONCE_INIT, the pthread symbol is masked. /** * @ingroup bsl_sal * @brief Create a thread lock. * * Create a thread lock. * * @attention none * @param lock [IN/OUT] Lock handle * @retval #BSL_SUCCESS, created successfully. * @retval #BSL_MALLOC_FAIL, memory space is insufficient and failed to apply for process lock space. * @retval #BSL_SAL_ERR_UNKNOWN, thread lock initialization failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error, the value of lock is NULL. */ int32_t BSL_SAL_ThreadLockNew(BSL_SAL_ThreadLockHandle *lock); /** * @ingroup bsl_sal * @brief Lock the read operation. * * Lock the read operation. * * @attention none * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ int32_t BSL_SAL_ThreadReadLock(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Lock the write operation. * * Lock the write operation. * * @attention none * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ int32_t BSL_SAL_ThreadWriteLock(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Unlock * * Unlock * * @attention unlock: Locks that have been unlocked are undefined behavior and are not allowed by default. * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM parameter error. The value of lock is NULL. */ int32_t BSL_SAL_ThreadUnlock(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Release the thread lock. * * Release the thread lock. * * @attention By default, repeated release is prohibited. * @param lock [IN] Lock handle. */ void BSL_SAL_ThreadLockFree(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Obtain the thread ID. * * Obtain the thread ID. * * @attention none * @retval Thread ID */ uint64_t BSL_SAL_ThreadGetId(void); /** * @ingroup bsl_sal * @brief run once: Use the initialization callback. * * @attention This function should not be a cancel, otherwise the default implementation of run * once seems to have never been called. */ typedef void (*BSL_SAL_ThreadInitRoutine)(void); /** * @ingroup bsl_sal * @brief Execute only once. * * Run the init Func command only once. * * @attention The current version does not support registration. * @param onceControl [IN] Record the execution status. * @param initFunc [IN] Initialization function. * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_BAD_PARAM, input parameter is abnormal. * @retval #BSL_SAL_ERR_UNKNOWN, the default run once failed. */ int32_t BSL_SAL_ThreadRunOnce(uint32_t *onceControl, BSL_SAL_ThreadInitRoutine initFunc); /** * @ingroup bsl_sal * @brief Create a thread. * * Create a thread. * * @attention none * @param thread [IN/OUT] Thread ID * @param startFunc [IN] Thread function * @param arg [IN] Thread function parameters * @retval #BSL_SUCCESS, created successfully. * @retval #BSL_SAL_ERR_UNKNOWN, Failed to create a thread. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. */ int32_t BSL_SAL_ThreadCreate(BSL_SAL_ThreadId *thread, void *(*startFunc)(void *), void *arg); /** * @ingroup bsl_sal * @brief Close the thread. * * Close the thread. * * @attention none * @param thread [IN] Thread ID */ void BSL_SAL_ThreadClose(BSL_SAL_ThreadId thread); /** * @ingroup bsl_sal * @brief Create a condition variable. * * Create a condition variable. * * @attention none * @param condVar [IN] Condition variable * @retval #BSL_SUCCESS, created successfully. * @retval #BSL_SAL_ERR_UNKNOWN, failed to create a condition variable. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of condVar is NULL. */ int32_t BSL_SAL_CreateCondVar(BSL_SAL_CondVar *condVar); /** * @ingroup bsl_sal * @brief The waiting time ends or the signal is obtained. * * The waiting time ends or the signal is obtained. * * @attention None * @param condVar [IN] Condition variable * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, function failure * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of condVar is NULL. */ int32_t BSL_SAL_CondSignal(BSL_SAL_CondVar condVar); /** * @ingroup bsl_sal * @brief The waiting time ends or the signal is obtained. * * The waiting time ends or the signal is obtained. * * @attention None * @param condMutex [IN] Mutex * @param condVar [IN] Condition variable * @param timeout [IN] Time * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, fails. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of condMutex or condVar is null. */ int32_t BSL_SAL_CondTimedwaitMs(BSL_SAL_Mutex condMutex, BSL_SAL_CondVar condVar, int32_t timeout); /** * @ingroup bsl_sal * @brief Delete a condition variable. * * Delete a condition variable. * * @attention none * @param condVar [IN] Condition variable * @retval #BSL_SUCCESS, Succeeded in deleting the condition variable. * @retval #BSL_SAL_ERR_UNKNOWN, Failed to delete the condition variable. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of condVar is NULL. */ int32_t BSL_SAL_DeleteCondVar(BSL_SAL_CondVar condVar); typedef void *bsl_sal_file_handle; // Pointer to file handle /** * @ingroup bsl_sal * @brief Open a file. * * Open the file and ensure that the entered path is standardized. * * @attention None * @param stream [OUT] File handle * @param path [IN] File path * @param mode [IN] Reading mode * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_FILE_OPEN, failed to be opened. * @retval #BSL_NULL_INPUT, parameter error. */ int32_t BSL_SAL_FileOpen(bsl_sal_file_handle *stream, const char *path, const char *mode); /** * @ingroup bsl_sal * @brief Close the file. * * Close the file. * * @attention none * @param stream [IN] File handle * @retval NA */ void BSL_SAL_FileClose(bsl_sal_file_handle stream); /** * @ingroup bsl_sal * @brief Read the file. * * Read the file. * The actual memory of the interface is 1 more than the real length of the read file, * which is used to add '\0' after the end of the read file content, and the outgoing parameter len is the real * data length, excluding '\0'. * * @attention none * @param stream [IN] File handle * @param buffer [IN] Buffer for reading data * @param size [IN] The unit of reading. * @param num [IN] Number of data records to be read * @param len [OUT] Read the data length. * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, fails. * @retval #BSL_NULL_INPUT, Incorrect parameter */ int32_t BSL_SAL_FileRead(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len); /** * @ingroup bsl_sal * @brief Write a file * * Write File * * @attention none * @param stream [IN] File handle * @param buffer [IN] Data to be written * @param size [IN] Write the unit * @param num [IN] Number of written data * @retval #BSL_SUCCESS, succeeded * @retval #BSL_SAL_ERR_UNKNOWN, fails * @retval #BSL_NULL_INPUT, parameter error */ int32_t BSL_SAL_FileWrite(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num); /** * @ingroup bsl_sal * @brief Obtain the file length. * * Obtain the file length. * * @attention none * @param path [IN] File path * @param len [OUT] File length * @retval #BSL_SUCCESS, succeeded * @retval #BSL_SAL_ERR_UNKNOWN, fails * @retval #BSL_NULL_INPUT, parameter error */ int32_t BSL_SAL_FileLength(const char *path, size_t *len); /** * @ingroup bsl_sal * @brief Basic time data structure definition. */ typedef struct { uint16_t year; /**< Year. the value range is [0, 65535]. */ uint8_t month; /**< Month. the value range is [1, 12]. */ uint8_t day; /**< Day, the value range is [1, 31]. */ uint8_t hour; /**< Hour, the value range is [0, 23]. */ uint8_t minute; /**< Minute, the value range is [0, 59]. */ uint16_t millSec; /**< Millisecond, the value range is [0, 999]. */ uint8_t second; /**< Second, the value range is [0, 59]. */ uint16_t microSec; /**< Microseconds, the value range is [0, 999]. */ } BSL_TIME; /** * @ingroup bsl_sal * @brief Unix Time structure definition. */ typedef int64_t BslUnixTime; /** * @ingroup bsl_sal * @brief Prototype of the callback function for obtaining the time * * Prototype definition of the callback function for obtaining the time. */ typedef BslUnixTime (*BslTimeFunc)(void); /** * @ingroup bsl_sal * @brief Interface for registering the function for obtaining the system time * You can use this API to register the system time obtaining function. * * This interface can be registered for multiple times. After the registration is * successful, the registration cannot be NULL again. * Description of the time range: * Users can use the Linux system at most 2038 per year. * The lower limit of the time is 1970 - 1 - 1 0: 0: 0. * It is recommended that users use this minimum intersection, i.e., the bounds of * years are 1970-1-1 0:0:0 ~ 2038-01-19 03:14:08. * * @param func [IN] Register the function for obtaining the system time */ void BSL_SAL_SysTimeFuncReg(BslTimeFunc func); /** * @ingroup bsl_sal * @brief Compare Two Dates * * @param dateA [IN] The first date * @param dateB [IN] The second date * @param diffSeconds [OUT] Number of seconds between two dates * @retval BslTimeCmpResult Comparison result of two dates * @retval #BSL_TIME_CMP_ERROR - Error in comparison * @retval #BSL_TIME_CMP_EQUAL - The two dates are consistent. * @retval #BSL_TIME_DATE_BEFORE - The first date is before the second date. * @retval #BSL_TIME_DATE_AFTER - The first date is after the second */ int32_t BSL_SAL_DateTimeCompare(const BSL_TIME *dateA, const BSL_TIME *dateB, int64_t *diffSec); /** * @ingroup bsl_sal * @brief Obtain the system time. * * Obtain the system time. * * @attention none * @param sysTime [out] Time * @retval #BSL_SUCCESS, obtained the time successfully. * @retval #BSL_SAL_ERR_BAD_PARAM, the value of cb is null. * @retval #BSL_INTERNAL_EXCEPTION, an exception occurred when obtaining the time. */ int32_t BSL_SAL_SysTimeGet(BSL_TIME *sysTime); /** * @ingroup bsl_sal * @brief Obtain the Unix time. * * Obtain the Unix time. * * @retval Return the Unix time. */ BslUnixTime BSL_SAL_CurrentSysTimeGet(void); /** * @ingroup bsl_sal * @brief Convert the date in the BslSysTime format to the UTC time format. * * Convert the date in the BslSysTime format to the UTC time format. * * @attention None * @param dateTime [IN] Date and time * @param utcTime [OUT] UTC time * @retval #BSL_SUCCESS, time is successfully converted. * @retval #BSL_INTERNAL_EXCEPTION, an exception occurred when obtaining the time. */ int32_t BSL_SAL_DateToUtcTimeConvert(const BSL_TIME *dateTime, int64_t *utcTime); /** * @ingroup bsl_sal * @brief Convert the date in the BslUnixTime format to the BslSysTime format. * * Convert the date in the BslUnixTime format to the BslSysTime format. * * @attention none * @param utcTime [IN] UTC time * @param sysTime [OUT] BslSysTime Time * @retval #BSL_SUCCESS, time is converted successfully * @retval #BSL_SAL_ERR_BAD_PARAM, the value of utcTime exceeds the upper limit or the value of sysTime is null. */ int32_t BSL_SAL_UtcTimeToDateConvert(int64_t utcTime, BSL_TIME *sysTime); /** * @ingroup bsl_sal * @brief Compare two dates, accurate to microseconds. * * Compare two dates, accurate to microseconds * * @attention None * @param dateA [IN] Time * @param dateB [IN] Time * @retval #BslTimeCmpResult Comparison result of two dates * @retval #BSL_TIME_CMP_ERROR - An error occurred in the comparison. * @retval #BSL_TIME_CMP_EQUAL - The two dates are consistent. * @retval #BSL_TIME_DATE_BEFORE - The first date is on the second * @retval #BSL_TIME_DATE_ AFTER - The first date is after the second */ int32_t BSL_SAL_DateTimeCompareByUs(const BSL_TIME *dateA, const BSL_TIME *dateB); /** * @ingroup bsl_sal * @brief Sleep the current thread * * Sleep the current thread * * @attention none * @param time [IN] Sleep time */ void BSL_SAL_Sleep(uint32_t time); /** * @ingroup bsl_sal * @brief Obtain the number of ticks that the system has experienced since startup. * * Obtain the system time. * * @attention none * @retval Number of ticks */ long BSL_SAL_Tick(void); /** * @ingroup bsl_sal * @brief Obtain the number of system ticks per second. * * Obtain the system time. * * @attention none * @retval Number of ticks per second */ long BSL_SAL_TicksPerSec(void); /** * @ingroup bsl_sal_net * @brief socket address. * * It should be defined like following union in linux, to cover various socket addresses. * union SockAddr { * struct sockaddr addr; * struct sockaddr_in6 addrIn6; * struct sockaddr_in addrIn; * struct sockaddr_un addrUn; * }; * */ typedef void *BSL_SAL_SockAddr; /** * @ingroup bsl_sal * @brief Socket address information * * It should be defined like 'struct addinfo' in linux, * struct addrinfo { * int ai_flags; * int ai_family; * int ai_socktype; * int ai_protocol; * socklen_t ai_addrlen; * struct sockaddr *ai_addr; * char *ai_canonname; * struct addrinfo *ai_next; * }; */ typedef void *BSL_SAL_SockAddrInfo; /** * @ingroup bsl_sal * @brief Create a BSL_SAL_SockAddr * * @return New BSL_SAL_SockAddr object */ typedef int32_t (*BslSalSockAddrNew)(BSL_SAL_SockAddr *sockAddr); /** * @ingroup bsl_sal * @brief Release the UIO_Addr object. * * @param uioAddr [IN] UIO_Addr object */ typedef void (*BslSalSockAddrFree)(BSL_SAL_SockAddr sockAddr); #define SAL_IPV4 2 /* IPv4 Internet protocols */ #define SAL_IPV6 10 /* IPv6 Internet protocols */ /** * @ingroup bsl_sal * @brief Obtain the UIO_Addr protocal family * * @param sockAddr [IN] UIO_Addr object * @retval Return 0 if the address is not valid. * @retval Return SAL_IPV4 if the address is IPv4. * @retval Return SAL_IPV6 if the address is IPv6. */ typedef int32_t (*BslSalSockAddrGetFamily)(const BSL_SAL_SockAddr sockAddr); /** * @ingroup bsl_sal * @brief Obtain the size of the BSL_SAL_SockAddr address. * @details Only for internal use * * @param sockAddr [IN] UIO object * @retval Address size, if the address is not valid, return 0 */ typedef uint32_t (*BslSalSockAddrSize)(const BSL_SAL_SockAddr sockAddr); /** * @ingroup bsl_sal * @brief Copy the BSL_SAL_SockAddr address. * * @param src [IN] Source address * @param dst [OUT] Destination address */ typedef void (*BslSalSockAddrCopy)(BSL_SAL_SockAddr dst, const BSL_SAL_SockAddr src); /** * @ingroup bsl_sal * @brief Socket creation interface * * Socket creation interface. * * @attention none * @param af [IN] Socket specifies the protocol set. * @param type [IN] Socket type * @param protocol [IN] Protocol type * @retval If the creation is successful, a non-negative value is returned. * @retval Otherwise, a negative value is returned. */ int32_t BSL_SAL_Socket(int32_t af, int32_t type, int32_t protocol); /** * @ingroup bsl_sal * @brief Close the socket * * Close the socket * * @attention none * @param sockId [IN] Socket file descriptor ID * @retval If the operation succeeds, BSL_SUCCESS is returned. * @retval If the operation fails, BSL_SAL_ERR_NET_SOCKCLOSE is returned. */ int32_t BSL_SAL_SockClose(int32_t sockId); /** * @ingroup bsl_sal * @brief Set the socket * * Set the socket * * @attention none * @param sockId [IN] Socket file descriptor ID * @param level [IN] Level of the option to be set. * @param name [IN] Options to be set * @param val [IN] Value of the option. * @param len [IN] val Length * @retval If the operation succeeds, BSL_SUCCESS is returned * @retval If the operation fails, BSL_SAL_ERR_NET_SETSOCKOPT is returned. */ int32_t BSL_SAL_SetSockopt(int32_t sockId, int32_t level, int32_t name, const void *val, int32_t len); #define SAL_PROTO_IP_LEVEL 0 /* IPv4 level */ #define SAL_PROTO_IPV6_LEVEL 41 /* IPv6 level */ #define SAL_MTU_OPTION 14 /* Retrieve the current known path MTU of the current socket */ #define SAL_IPV6_MTU_OPTION 24 /* Retrieve the current known path MTU of the current socket for IPv6 */ /** * @ingroup bsl_sal * @brief Get the socket * * Get the socket * * @attention none * @param sockId [IN] Socket file descriptor ID * @param level [IN] Level of the option to be set. * SAL_PROTO_IP_LEVEL: ipv4 level * SAL_PROTO_IPV6_LEVEL: ipv6 level * @param name [IN] Options to be set * SAL_MTU_OPTION: ipv4 mtu option * SAL_IPV6_MTU_OPTION: ipv6 mtu option * @param val [OUT] Value of the option * @param len [OUT] val Length * @retval If the operation succeeds, BSL_SUCCESS is returned */ int32_t BSL_SAL_GetSockopt(int32_t sockId, int32_t level, int32_t name, void *val, int32_t *len); /** * @ingroup bsl_sal * @brief Listening socket * * Listen socket * * @attention none * @param sockId [IN] Socket file descriptor ID * @param backlog [IN] Length of the receiving queue * @retval If the operation succeeds, BSL_SUCCESS is returned. * @retval If the operation fails, BSL_SAL_ERR_NET_LISTEN is returned. */ int32_t BSL_SAL_SockListen(int32_t sockId, int32_t backlog); /** * @ingroup bsl_sal * @brief Binding a socket * * Binding Socket * * @attention None * @param sockId [IN] Socket file descriptor ID * @param addr [IN] Specify the address. * @param len [IN] Address length * @retval If the operation succeeds, BSL_SUCCESS is returned. * @retval If the operation fails, BSL_SAL_ERR_NET_BIND is returned. */ int32_t BSL_SAL_SockBind(int32_t sockId, BSL_SAL_SockAddr addr, size_t len); /** * @ingroup bsl_sal * @brief Initiate a connection. * * Initiate a connection. * * @attention none * @param sockId [IN] Socket file descriptor ID * @param addr [IN] Address to be connected * @param len [IN] Address length * @retval If the operation succeeds, BSL_SUCCESS is returned * @retval If the operation fails, BSL_SAL_ERR_NET_CONNECT is returned. */ int32_t BSL_SAL_SockConnect(int32_t sockId, BSL_SAL_SockAddr addr, size_t len); /** * @ingroup bsl_sal * @brief Send a message. * * Send messages * * @attention none * @param sockId [IN] Socket file descriptor ID * @param msg [IN] Message sent * @param len [IN] Information length * @param flags [IN] is generally set to 0. * @retval If the operation succeeds, the length of the sent data is returned. * @retval If the operation fails, a negative value is returned. * @retval If the operation times out or the peer end disables the function, the value 0 is returned. */ int32_t BSL_SAL_SockSend(int32_t sockId, const void *msg, size_t len, int32_t flags); /** * @ingroup bsl_sal * @brief Receive the message. * * Receive information * * @attention none * @param sockfd [IN] Socket file descriptor ID * @param buff [IN] Buffer for receiving information * @param len [IN] Length of the buffer * @param flags [IN] is generally set to 0. * @retval If the operation succeeds, the received data length is returned. * @retval If the operation fails, a negative value is returned. * @retval If the operation times out or the peer end disables the function, the value 0 is returned. */ int32_t BSL_SAL_SockRecv(int32_t sockfd, void *buff, size_t len, int32_t flags); /** * @ingroup bsl_sal * @brief Check the socket descriptor. * * Check the socket descriptor. * * @attention None * @param nfds [IN] Total number of file descriptors that are listened on * @param readfds [IN] Readable file descriptor (optional) * @param writefds [IN] Descriptor of a writable file. This parameter is optional. * @param exceptfds [IN] Exception file descriptor (optional) * @param timeout [IN] Set the timeout interval. * @retval If the operation succeeds, Number of ready descriptors are returned; * @retval If the operation fails, a negative value is returned; * @retval If the operation times out, 0 is returned */ int32_t BSL_SAL_Select(int32_t nfds, void *readfds, void *writefds, void *exceptfds, void *timeout); /** * @ingroup bsl_sal * @brief Device control interface function * * Device control interface function * * @attention None * @param sockId [IN] Socket file descriptor ID * @param cmd [IN] Interaction protocol * @param arg [IN] Parameter * @retval If the operation succeeds, BSL_SUCCESS is returned. * @retval If the operation fails, BSL_SAL_ERR_NET_IOCTL is returned. */ int32_t BSL_SAL_Ioctlsocket(int32_t sockId, long cmd, unsigned long *arg); /** * @ingroup bsl_sal * @brief Obtain the last error corresponding to the socket. * * Obtain the last error corresponding to the socket. * * @attention none * @retval Return the corresponding error. */ int32_t BSL_SAL_SockGetLastSocketError(void); /** * @ingroup bsl_sal * @brief String comparison * * String comparison * * @attention None. * @param str1 [IN] First string to be compared. * @param str2 [IN] Second string to be compared. * @retval If the parameter is abnormal, BSL_NULL_INPUT is returned. * @retval If the strings are the same, 0 is returned; * Otherwise, the difference between different characters is returned. */ int32_t BSL_SAL_StrcaseCmp(const char *str1, const char *str2); /** * @ingroup bsl_sal * @brief Search for the corresponding character position in a string. * * Search for the corresponding character position in a string. * * @attention None. * @param str [IN] String * @param character [IN] Character to be searched for * @param count [IN] Range to be found * @retval If a character is found, the position of the character is returned; * Otherwise, NULL is returned. */ void *BSL_SAL_Memchr(const char *str, int32_t character, size_t count); /** * @ingroup bsl_sal * @brief Convert string to number * * Convert string to number * * @attention None. * @param str [IN] String to be converted. * @retval If the conversion is successful, the corresponding number is returned; * Otherwise, the value 0 is returned. */ int32_t BSL_SAL_Atoi(const char *str); /** * @ingroup bsl_sal * @brief Obtain the length of a given string. * * Obtain the length of a given string. * * @attention None. * @param string [IN] String to obtain the length. * @param count [IN] Maximum length * @retval If the parameter is abnormal, return 0. * @retval If the length of a string is greater than the count, return count. * Otherwise, the actual length of the string is returned. */ uint32_t BSL_SAL_Strnlen(const char *string, uint32_t count); typedef enum { BSL_SAL_MEM_MALLOC = 0X0100, BSL_SAL_MEM_FREE, BSL_SAL_THREAD_LOCK_NEW_CB_FUNC = 0X0200, BSL_SAL_THREAD_LOCK_FREE_CB_FUNC, BSL_SAL_THREAD_LOCK_READ_LOCK_CB_FUNC, BSL_SAL_THREAD_LOCK_WRITE_LOCK_CB_FUNC, BSL_SAL_THREAD_LOCK_UNLOCK_CB_FUNC, BSL_SAL_THREAD_GET_ID_CB_FUNC, BSL_SAL_NET_WRITE_CB_FUNC = 0x0300, BSL_SAL_NET_READ_CB_FUNC, BSL_SAL_NET_SOCK_CB_FUNC, BSL_SAL_NET_SOCK_CLOSE_CB_FUNC, BSL_SAL_NET_SET_SOCK_OPT_CB_FUNC, BSL_SAL_NET_GET_SOCK_OPT_CB_FUNC, BSL_SAL_NET_SOCK_LISTEN_CB_FUNC, BSL_SAL_NET_SOCK_BIND_CB_FUNC, BSL_SAL_NET_SOCK_CONNECT_CB_FUNC, BSL_SAL_NET_SOCK_SEND_CB_FUNC, BSL_SAL_NET_SOCK_RECV_CB_FUNC, BSL_SAL_NET_SELECT_CB_FUNC, BSL_SAL_NET_IOCTL_CB_FUNC, BSL_SAL_NET_SOCKGETLASTSOCKETERROR_CB_FUNC, BSL_SAL_NET_SOCKADDR_NEW_CB_FUNC, BSL_SAL_NET_SOCKADDR_FREE_CB_FUNC, BSL_SAL_NET_SOCKADDR_SIZE_CB_FUNC, BSL_SAL_NET_SENDTO_CB_FUNC, BSL_SAL_NET_RECVFROM_CB_FUNC, BSL_SAL_NET_GETFAMILY_CB_FUNC, BSL_SAL_TIME_GET_UTC_TIME_CB_FUNC = 0x0400, BSL_SAL_TIME_DATE_TO_STR_CONVERT_CB_FUNC, BSL_SAL_TIME_SYS_TIME_GET_CB_FUNC, BSL_SAL_TIME_UTC_TIME_TO_DATE_CONVERT_CB_FUNC, BSL_SAL_TIME_SLEEP_CB_FUNC, BSL_SAL_TIME_TICK_CB_FUNC, BSL_SAL_TIME_TICK_PER_SEC_CB_FUNC, BSL_SAL_FILE_OPEN_CB_FUNC = 0X0500, /* BslSalFileOpen */ BSL_SAL_FILE_READ_CB_FUNC, /* BslSalFileRead */ BSL_SAL_FILE_WRITE_CB_FUNC, /* BslSalFileWrite */ BSL_SAL_FILE_CLOSE_CB_FUNC, /* BslSalFileClose */ BSL_SAL_FILE_LENGTH_CB_FUNC, /* BslSalFileLength */ BSL_SAL_FILE_ERROR_CB_FUNC, /* BslSalFileError */ BSL_SAL_FILE_TELL_CB_FUNC, /* BslSalFileTell */ BSL_SAL_FILE_SEEK_CB_FUNC, /* BslSalFileSeek */ BSL_SAL_FILE_GETS_CB_FUNC, /* BslSalFGets */ BSL_SAL_FILE_PUTS_CB_FUNC, /* BslSalFPuts */ BSL_SAL_FILE_FLUSH_CB_FUNC, /* BslSalFlush */ BSL_SAL_FILE_EOF_CB_FUNC, /* BslSalFeof */ BSL_SAL_FILE_SET_ATTR_FUNC, /* BslSalFSetAttr */ BSL_SAL_FILE_GET_ATTR_FUNC, /* BslSalFGetAttr */ BSL_SAL_DL_OPEN_CB_FUNC = 0x0700, BSL_SAL_DL_CLOSE_CB_FUNC, BSL_SAL_DL_SYM_CB_FUNC, BSL_SAL_MAX_FUNC_CB = 0xffff } BSL_SAL_CB_FUNC_TYPE; /** * @ingroup bsl_sal * @brief Allocate a memory block. * * Allocate a memory block. * * @param size [IN] Size of the allocated memory. * @retval: Not NULL, The start address of the allocated memory when memory is allocated successfully. * @retval NULL, Memory allocation failure. */ typedef void *(*BslSalMalloc)(uint32_t size); /** * @ingroup bsl_sal * @brief Reclaim a memory block allocated by pfMalloc. * * Reclaim a block of memory allocated by pfMalloc. * * @param addr [IN] Start address of the memory allocated by pfMalloc. */ typedef void (*BslSalFree)(void *addr); /** * @ingroup bsl_sal * @brief Create a thread lock. * * Create a thread lock. * * @param lock [IN/OUT] Lock handle * @retval #BSL_SUCCESS, created successfully. * @retval #BSL_MALLOC_FAIL, memory space is insufficient and thread lock space cannot be applied for. * @retval #BSL_SAL_ERR_UNKNOWN, thread lock initialization failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ typedef int32_t (*BslSalThreadLockNew)(BSL_SAL_ThreadLockHandle *lock); /** * @ingroup bsl_sal * @brief Release the thread lock. * * Release the thread lock. Ensure that the lock can be released when other threads obtain the lock. * * @param lock [IN] Lock handle */ typedef void (*BslSalThreadLockFree)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Lock the read operation. * * Lock the read operation. * * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ typedef int32_t (*BslSalThreadReadLock)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Lock the write operation. * * Lock the write operation. * * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ typedef int32_t (*BslSalThreadWriteLock)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Unlock * * Unlock * * @param lock [IN] Lock handle * @retval #BSL_SUCCESS, succeeded. * @retval #BSL_SAL_ERR_UNKNOWN, operation failed. * @retval #BSL_SAL_ERR_BAD_PARAM, parameter error. The value of lock is NULL. */ typedef int32_t (*BslSalThreadUnlock)(BSL_SAL_ThreadLockHandle lock); /** * @ingroup bsl_sal * @brief Obtain the thread ID. * * Obtain the thread ID. * * @retval Thread ID */ typedef uint64_t (*BslSalThreadGetId)(void); /** * @ingroup bsl_sal * @brief Open the file. * * @param stream [OUT] A pointer to the file handle that will be initialized upon successful opening of the file. * @param path [IN] A string specifying the path to the file to be opened. * @param mode [IN] A string specifying the access mode for the file. * @return If the operation succeeds, BSL_SUCCESS is returned. * If the file open fails, BSL_SAL_ERR_FILE_OPEN is returned. * If parameter error, BSL_NULL_INPUT is returned. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFileOpen)(bsl_sal_file_handle *stream, const char *path, const char *mode); /** * @ingroup bsl_sal * @brief Read the file. * * @param stream [IN] The file handle representing the file to be read. * @param buffer [OUT] A pointer to the buffer where the read data will be stored. * @param size [IN] The size of each element in the buffer (in bytes). * @param num [IN] The number of elements in the buffer. * @param len [OUT] A pointer to a variable where the actual number of bytes read will be stored. * @return If the file is successfully read, BSL_SUCCESS is returned. * If the file read fails, BSL_SAL_ERR_FILE_READ is returned. * If parameter error, BSL_NULL_INPUT is returned. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFileRead)(bsl_sal_file_handle stream, void *buffer, size_t size, size_t num, size_t *len); /** * @ingroup bsl_sal * @brief Write the file * * @param stream [IN] The file handle representing the file to be written. * @param buffer [IN] A pointer to the buffer containing the data to be written. * @param size [IN] The size of each element in the buffer (in bytes). * @param num [IN] The number of elements in the buffer. * @return If the file is successfully write, BSL_SUCCESS is returned. * If the file read fails, BSL_SAL_ERR_FILE_WRITE is returned. * If parameter error, BSL_NULL_INPUT is returned. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFileWrite)(bsl_sal_file_handle stream, const void *buffer, size_t size, size_t num); /** * @ingroup bsl_sal * @brief Close the file. * * @param stream [IN] The file handle representing the file to be closed. * @return NA * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef void (*BslSalFileClose)(bsl_sal_file_handle stream); /** * @ingroup bsl_sal * @brief Obtain the file length. * * @param path [IN] The path to the file whose length needs to be obtained. * @param len [OUT] A pointer to store the length of the file. * @return If the file length is obtained successfully, BSL_SUCCESS is returned. * If the file read fails, BSL_SAL_ERR_FILE_LENGTH is returned. * If parameter error, BSL_NULL_INPUT is returned. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFileLength)(const char *path, size_t *len); /** * @ingroup bsl_sal * @brief Test the error indicator for the given stream. * * @param stream [IN] The file handle representing the stream to check. * @return If the error indicator associated with the stream was set or the stream is null, false is returned. * Otherwise, true is returned. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef bool (*BslSalFileError)(bsl_sal_file_handle stream); /** * @ingroup bsl_sal * @brief Get the current file position in stream. * * @param stream [IN] The file handle representing the stream to check. * @param pos [OUT] A pointer to store the current file position. * @return If the file position is obtained successfully, BSL_SUCCESS is returned. * If fail to get the file position, BSL_SAL_ERR_FILE_TELL is returned. * If parameter error, BSL_NULL_INPUT is returned. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFileTell)(bsl_sal_file_handle stream, long *pos); /** * @ingroup bsl_sal * @brief Change the current file position associated with stream to a new location within the file. * * @param stream [IN] The file handle representing the stream to modify. * @param offset [IN] The number of bytes to move the file pointer from the origin. * @param origin [IN] The reference point for the new file position. * @return If successful, BSL_SUCCESS is returned. * If fail to change the file position, BSL_SAL_ERR_FILE_SEEK is returned. * If parameter error, BSL_NULL_INPUT is returned. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFileSeek)(bsl_sal_file_handle stream, long offset, int32_t origin); /** * @ingroup bsl_sal * @brief Read a line from the stream and store it into the buffer. * * @param stream [IN] The file handle representing the stream to read from. * @param buf [OUT] The buffer where the read data will be stored. * @param readLen [IN] The maximum number of characters to read. * @return If successful, the same read buffer is returned. * Otherwise, return NULL. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef char *(*BslSalFGets)(bsl_sal_file_handle stream, char *buf, int32_t readLen); /** * @ingroup bsl_sal * @brief Write a string to the specified stream. * * @param stream [IN] The file handle representing the stream to write to. * @param buf [IN] The string to write to the stream. * @return If successful, return true. Otherwise, return false. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef bool (*BslSalFPuts)(bsl_sal_file_handle stream, const char *buf); /** * @ingroup bsl_sal * @brief Flush cache buffer associated with the specified output stream. * * @param stream [IN] The file handle representing the stream to flush. * @return If successful, return true. Otherwise, return false. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef bool (*BslSalFlush)(bsl_sal_file_handle stream); /** * @ingroup bsl_sal * @brief Indicate whether the end-of-file flag is set for the given stream. * * @param stream [IN] The file handle representing the stream to check. * @return If successful, return BSL_SUCCESS. Otherwise, see bsl_errno.h. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFeof)(bsl_sal_file_handle stream); /** * @ingroup bsl_sal * @brief Set the attributes associated with the terminal referred to by the open stream. * * @param stream [IN] The file handle representing the stream to modify. * @param cmd [IN] The command specifying which attribute to set. * @param arg [IN] The argument providing the value for the attribute. * @return If successful, BSL_SUCCESS is returned. * If fail to set, BSL_SAL_ERR_FILE_SET_ATTR is returned. * If parameter error, BSL_NULL_INPUT is returned. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFSetAttr)(bsl_sal_file_handle stream, int cmd, const void *arg); /** * @ingroup bsl_sal * @brief Get the attributes associated with the terminal referred to by the open stream. * * @param stream [IN] The file handle representing the stream to check. * @param arg [OUT] A pointer to store the retrieved attributes. * @return If successful, BSL_SUCCESS is returned. * If fail to get, BSL_SAL_ERR_FILE_GET_ATTR is returned. * If parameter error, BSL_NULL_INPUT is returned * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BslSalFGetAttr)(bsl_sal_file_handle stream, void *arg); /** * @ingroup bsl_sal * @brief Get the length of the file. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_FILE_LENGTH: get file length fails. * @retval #BSL_NULL_INPUT: parameter error. */ typedef int32_t (*BslSalFileLength)(const char *path, size_t *len); /** * @ingroup bsl_sal * @brief Get the system time. * * @retval System time in int64_t format. */ typedef int64_t (*BslSalGetSysTime)(void); /** * @ingroup bsl_sal * @brief Convert date to string. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_INTERNAL_EXCEPTION: conversion fails. */ typedef uint32_t (*BslSalDateToStrConvert)(const BSL_TIME *dateTime, char *timeStr, size_t len); /** * @ingroup bsl_sal * @brief Get the system time. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_BAD_PARAM: parameter error. * @retval #BSL_INTERNAL_EXCEPTION: an exception occurred when obtaining the time. */ typedef uint32_t (*BslSalSysTimeGet)(BSL_TIME *sysTime); /** * @ingroup bsl_sal * @brief Convert UTC time to date. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_BAD_PARAM: parameter error. */ typedef uint32_t (*BslSalUtcTimeToDateConvert)(int64_t utcTime, BSL_TIME *sysTime); /** * @ingroup bsl_sal * @brief Sleep for a specified time. */ typedef void (*BslSalSleep)(uint32_t time); /** * @ingroup bsl_sal * @brief Get the system tick count. * * @retval System tick count. */ typedef long (*BslSalTick)(void); /** * @ingroup bsl_sal * @brief Get the number of ticks per second. * * @retval Number of ticks per second. */ typedef long (*BslSalTicksPerSec)(void); /** * @ingroup bsl_sal * @brief Write data. * * @retval Positive integer: number of bytes written. * @retval Negative integer: write operation failed. */ typedef int32_t (*BslSalWrite)(int32_t fd, const void *buf, uint32_t len, int32_t *err); /** * @ingroup bsl_sal * @brief Read data. * * @retval Positive integer: number of bytes read. * @retval Negative integer: read operation failed. */ typedef int32_t (*BslSalRead)(int32_t fd, void *buf, uint32_t len, int32_t *err); /** * @ingroup bsl_sal * @brief Create a socket. * * @retval Non-negative integer: socket file descriptor. * @retval Negative integer: socket creation failed. */ typedef int32_t (*BslSalSocket)(int32_t af, int32_t type, int32_t protocol); /** * @ingroup bsl_sal * @brief Close a socket. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_NET_SOCKCLOSE: socket close fails. */ typedef int32_t (*BslSalSockClose)(int32_t sockId); /** * @ingroup bsl_sal * @brief Set socket options. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_NET_SETSOCKOPT: set socket option fails. */ typedef int32_t (*BslSalSetSockopt)(int32_t sockId, int32_t level, int32_t name, const void *val, int32_t len); /** * @ingroup bsl_sal * @brief Get socket options. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_NET_GETSOCKOPT: get socket option fails. */ typedef int32_t (*BslSalGetSockopt)(int32_t sockId, int32_t level, int32_t name, void *val, int32_t *len); /** * @ingroup bsl_sal * @brief Listen for socket connections. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_NET_LISTEN: socket listen fails. */ typedef int32_t (*BslSalSockListen)(int32_t sockId, int32_t backlog); /** * @ingroup bsl_sal * @brief Bind a socket to an address. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_NET_BIND: socket bind fails. */ typedef int32_t (*BslSalSockBind)(int32_t sockId, BSL_SAL_SockAddr addr, size_t len); /** * @ingroup bsl_sal * @brief Connect a socket to a remote address. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_NET_CONNECT: socket connect fails. */ typedef int32_t (*BslSalSockConnect)(int32_t sockId, BSL_SAL_SockAddr addr, size_t len); /** * @ingroup bsl_sal * @brief Send data through a socket. * * @retval Positive integer: number of bytes sent. * @retval Negative integer: send operation failed. */ typedef int32_t (*BslSalSockSend)(int32_t sockId, const void *msg, size_t len, int32_t flags); /** * @ingroup bsl_sal * @brief Receive data from a socket. * * @retval Positive integer: number of bytes received. * @retval Negative integer: receive operation failed. */ typedef int32_t (*BslSalSockRecv)(int32_t sockfd, void *buff, size_t len, int32_t flags); /** * @ingroup bsl_sal * @brief Same as linux funciton "sendto" * * @param sock [IN] Socket descriptor. * @param buf [IN] The buffer containing the data to be sent. * @param len [IN] Length of the buffer. * @param flags [IN] The type of message transmission. * @param address [IN] Points to a sockaddr structure containing the destination address. * @param addrLen [IN] Length of the sockaddr structure. * @param err [OUT] The error code if "sendto" failed. * @return BSL_SUCCESS, success. * Otherwise, failure. */ typedef int32_t (*BslSalNetSendTo)(int32_t sock, const void *buf, size_t len, int32_t flags, void *address, int32_t addrLen, int32_t *err); /** * @ingroup bsl_salZ * @brief Same as linux funciton "recvfrom" * @param sock [IN] Socket descriptor. * @param buf [IN] The buffer where the message should be stored. * @param len [IN] Length of the buffer. * @param flags [IN] The type of message transmission. * @param address [IN] A null pointer, or points to a sockaddr structure in which the sending address is to be stored. * @param addrLen [IN] Either a null pointer, if address is a null pointer, or a pointer to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address. * @param err [OUT] The error code if "recvfrom" failed. * @return BSL_SUCCESS, success. * Otherwise, failure. */ typedef int32_t (*BslSalNetRecvFrom)(int32_t sock, void *buf, size_t len, int32_t flags, void *address, int32_t *addrLen, int32_t *err); /** * @ingroup bsl_sal * @brief Monitor multiple file descriptors for readiness. * * @retval Positive integer: number of ready descriptors. * @retval 0: timeout occurred. * @retval Negative integer: select operation failed. */ typedef int32_t (*BslSalSelect)(int32_t nfds, void *readfds, void *writefds, void *exceptfds, void *timeout); /** * @ingroup bsl_sal * @brief Perform I/O control on a socket. * * @retval #BSL_SUCCESS: succeeded. * @retval #BSL_SAL_ERR_NET_IOCTL: ioctl operation fails. */ typedef int32_t (*BslSalIoctlsocket)(int32_t sockId, long cmd, unsigned long *arg); /** * @ingroup bsl_sal * @brief Get the last socket error. * * @retval Error code of the last socket operation. */ typedef int32_t (*BslSalSockGetLastSocketError)(void); /** * @ingroup bsl_sal * @brief Control callback functions for SAL (System Abstraction Layer). * * This function is used to control and register callback functions for different SAL modules * such as network, time, and file operations. * * @attention None * @param funcType [IN] Type of the callback function to be controlled * @param funcCb [IN] Pointer to the callback function * @retval #BSL_SUCCESS Callback function controlled successfully * @retval #BSL_SAL_ERR_BAD_PARAM Invalid function type or callback pointer * @retval Other error codes specific to the SAL module */ int32_t BSL_SAL_CallBack_Ctrl(BSL_SAL_CB_FUNC_TYPE funcType, void *funcCb); /** * @ingroup bsl_sal * @brief Load a dynamic library for dl. * * Load a dynamic library for dl. * * @attention None. * @param fileName [IN] Name of the file to be loaded. * @param handle [OUT] Pointer to store the handle of the loaded library. * @retval If the operation is successful, BSL_SUCCESS is returned; * Otherwise, an error code is returned. */ int32_t BSL_SAL_LoadLib(const char *fileName, void **handle); /** * @ingroup bsl_sal * @brief Unload a dynamic library for dl. * * Unload a dynamic library for dl. * * @attention None. * @param handle [IN] Handle of the library to be unloaded. * @retval If the operation is successful, BSL_SUCCESS is returned; * Otherwise, an error code is returned. */ int32_t BSL_SAL_UnLoadLib(void *handle); /** * @ingroup bsl_sal * @brief Get the address of the initialization function for dl. * * Get the address of the initialization function for dl. * * @attention None. * @param handle [IN] Handle of the loaded library. * @param funcName [IN] Name of the function. * @param func [OUT] Pointer to store the address of the function. * @retval If the operation is successful, BSL_SUCCESS is returned; * Otherwise, an error code is returned. */ int32_t BSL_SAL_GetFuncAddress(void *handle, const char *funcName, void **func); // Define command enumeration typedef enum { BSL_SAL_LIB_FMT_OFF = 0, /* Do not enable named conversion */ BSL_SAL_LIB_FMT_SO = 1, BSL_SAL_LIB_FMT_LIBSO = 2, BSL_SAL_LIB_FMT_LIBDLL = 3, BSL_SAL_LIB_FMT_DLL = 4 } BSL_SAL_LibFmtCmd; /** * @ingroup bsl_sal * @brief Convert filename to full library path for dl. * * Convert filename to full library name for dl according to the specified format and directory. * * @attention None. * @param cmd [IN] Command specifying the conversion format. * @param fileName [IN] Original filename. * @param name [OUT] Pointer to store the converted full name. * @retval If the operation is successful, BSL_OK is returned; * Otherwise, an error code is returned. */ int32_t BSL_SAL_LibNameFormat(BSL_SAL_LibFmtCmd cmd, const char *fileName, char **name); /** * @ingroup bsl_sal * @brief Loading dynamic libraries. * * Loading dynamic libraries. * * @param fileName [IN] Path of dl * @param handle [OUT] Dynamic library handle * @retval #BSL_SUCCESS Succeeded. * @retval #BSL_SAL_ERR_DL_NOT_FOUND Library file not found. * @retval #BSL_SAL_ERR_DL_LOAD_FAIL Failed to load the library. */ typedef int32_t (*BslSalLoadLib)(const char *fileName, void **handle); /** * @ingroup bsl_sal * @brief Close dynamic library. * * Close dynamic library. * * @param handle [IN] Dynamic library handle * @retval #BSL_SUCCESS Succeeded. * @retval #BSL_SAL_ERR_DL_UNLOAAD_FAIL Failed to unload the library. */ typedef int32_t (*BslSalUnLoadLib)(void *handle); /** * @ingroup bsl_sal * @brief Get function symbol from dynamic library. * * Get function symbol from dynamic library. * * @param handle [IN] Dynamic library handle * @param funcName [IN] Function name * @param func [OUT] Function pointer * @retval #BSL_SUCCESS Succeeded. * @retval #BSL_SAL_ERR_DL_NON_FUNCTION Symbol found but is not a function. * @retval #BSL_SAL_ERR_DL_LOOKUP_METHOD Failed to lookup the function. */ typedef int32_t (*BslSalGetFunc)(void *handle, const char *funcName, void **func); #ifdef __cplusplus } #endif #endif // BSL_SAL_H
2301_79861745/bench_create
include/bsl/bsl_sal.h
C
unknown
51,667
/* * 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 bsl_uio * @ingroup bsl * @brief uio module */ #ifndef BSL_TYPES_H #define BSL_TYPES_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif typedef enum { BSL_FORMAT_UNKNOWN, BSL_FORMAT_PEM, BSL_FORMAT_ASN1, BSL_FORMAT_PFX_COM, BSL_FORMAT_PKCS12, BSL_FORMAT_OBJECT, } BSL_ParseFormat; typedef struct { uint8_t *data; uint32_t dataLen; } BSL_Buffer; #ifdef __cplusplus } #endif #endif // BSL_TYPES_H
2301_79861745/bench_create
include/bsl/bsl_types.h
C
unknown
1,007
/* * 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 BSL_UI_H #define BSL_UI_H #include <stdint.h> #include <stdbool.h> #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_ui * @brief BSL_UI method types */ typedef enum { BSL_UIM_NONE = 0, BSL_UIM_OPEN, BSL_UIM_WRITE, BSL_UIM_READ, BSL_UIM_CLOSE } BSL_UI_MethodTypes; /** * @ingroup bsl_ui * @brief BSL_UI data types */ typedef enum { BSL_UI_DATA_NONE = 0, BSL_UI_DATA_READ, BSL_UI_DATA_WRITE } BSL_UI_DataTypes; /** * @ingroup bsl_ui * @brief BSL_UI data flags */ typedef enum { BSL_UI_DATA_FLAG_NONE = 0, // Setting the Echo Display 01, follow with 010 0100 BSL_UI_DATA_FLAG_ECHO = 0x1, BSL_UI_DATA_FLAG_USER = 0x10000 } BSL_UI_DataFlags; /** * @ingroup bsl_ui * @brief BSL_UI_Ctrl get parameters */ typedef struct { uint32_t flags; char *buff; uint32_t buffLen; char *verifyBuff; } BSL_UI_CtrlRGetParam; /** * @ingroup bsl_ui * @brief BSL_UI read pwd parameters */ typedef struct { const char *desc; const char *name; bool verify; } BSL_UI_ReadPwdParam; typedef struct UI_Control BSL_UI; typedef struct UI_ControlMethod BSL_UI_Method; typedef struct UI_ControlDataPack BSL_UI_DataPack; /** * @ingroup bsl_ui * @brief Function pointer type for opening a UI. * * @param ui [IN] Pointer to the BSL_UI structure representing the UI. * @return BSL_SUCCESS, success. * @return Otherwise, failure. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BSL_UI_Open) (BSL_UI *ui); /** * @ingroup bsl_ui * @brief Function pointer type for writing data to a UI. * * @param ui [IN] Pointer to the BSL_UI structure representing the UI. * @param data [IN] Pointer to the BSL_UI_DataPack structure containing the data to be written. * @return Returns 0 on success, or a negative error code on failure. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BSL_UI_Write) (BSL_UI *ui, BSL_UI_DataPack *data); /** * @ingroup bsl_ui * @brief Function pointer type for reading data from a UI. * * This function is called to read data from the user interface (UI). * * @param ui [IN] Pointer to the BSL_UI structure representing the UI. * @param data [OUT] Pointer to the BSL_UI_DataPack structure where the read data will be stored. * @return BSL_SUCCESS, success. * @return Otherwise, failure. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BSL_UI_Read) (BSL_UI *ui, BSL_UI_DataPack *data); /** * @ingroup bsl_ui * @brief Function pointer type for closing a UI. * * @param ui [IN] Pointer to the BSL_UI structure representing the UI. * @return BSL_SUCCESS, success. * @return Otherwise, failure. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BSL_UI_Close) (BSL_UI *ui); /** * @ingroup bsl_ui * @brief Function pointer type for a data check callback. * * @param ui [IN] Pointer to the BSL_UI structure representing the UI. * @param buff [IN] Pointer to the buffer containing the data to be checked. * @param buffLen [IN] Length of the data buffer. * @param callBackData [IN] Pointer to user-defined data passed to the callback. * @return Returns 0 if the data is valid, or a negative error code if the data is invalid. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. */ typedef int32_t (*BSL_UI_CheckDataCallBack) (BSL_UI *ui, char *buff, uint32_t buffLen, void *callBackData); /** * @ingroup bsl_ui * @brief The method function is NULL. UI of common default processing functions. * Otherwise, use user-defined functions to create UIs. * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param method [IN] UI function * * @return If success, BSL_SUCCESS is returned. * If fails, NULL is returned. */ BSL_UI *BSL_UI_New(const BSL_UI_Method *method); /** * @ingroup bsl_ui * @brief Release the UI * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param ui [IN] UI object * * @return None */ void BSL_UI_Free(BSL_UI *ui); /** * @ingroup bsl_ui * @brief Create a BSL_UI_Method * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @return If success, the BSL_UI_Method object is returned; * If fails, NULL is returned. */ BSL_UI_Method *BSL_UI_MethodNew(void); /** * @ingroup bsl_ui * @brief Release the BSL_UI_Method * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param method [IN] BSL_UI_Method Object * * @return None */ void BSL_UI_MethodFree(BSL_UI_Method *method); /** * @ingroup bsl_ui * @brief If the ui parameter is NULL, obtain the default UI processing function. * Otherwise, obtain the UI processing function. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param ui [IN] UI object * * @return If success, the BSL_UI_Method object is returned; * If fails, NULL is returned. */ const BSL_UI_Method *BSL_UI_GetOperMethod(const BSL_UI *ui); /** * @ingroup bsl_ui * @brief Set the BSL UI method. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param method [IN] BSL_UI_Method Object * @param type [IN] Method type. For details, see BSL_UI_MethodTypes. * @param func [IN] Method to be set. For details, see the callback prototype. * * @return If success, the BSL_SUCCESS is returned. * If fail, other values are returned. */ int32_t BSL_UI_SetMethod(BSL_UI_Method *method, uint8_t type, void *func); /** * @ingroup bsl_ui * @brief Obtain the BSL UI method. * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param method [IN] BSL_UI_Method Object * @param type [IN] Method type. For details, see BSL_UI_MethodTypes. * @param func [OUT] Pointer to the obtained method function. * * @return If success, the BSL_SUCCESS is returned. * If fail, other values are returned. */ int32_t BSL_UI_GetMethod(const BSL_UI_Method *method, uint8_t type, void **func); /** * @ingroup bsl_ui * @brief Read the pwd tool function of the user input * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param param [IN] The user need input parameter marked as "BSL_UI_ReadPwdParam" that required by pwd tool function. desc: indicates the string description, name: indicates the string object name, and verify indicates whether the verification is required. * @param buff [OUT] Indicates the obtained PWD buff. * @param buffLen [IN/OUT] Indicates the obtained PWD buff length. * @param checkDataCallBack [IN] BSL_UI_CheckDataCallBack checks the input string callback, the value NULL * indicates that the check is not required. * @param callBackData [IN] BSL_UI_CheckDataCallBack, check the user data of the input string callback. * * @return If success, the BSL_SUCCESS is returned. * If fail, other values are returned. */ int32_t BSL_UI_ReadPwdUtil(BSL_UI_ReadPwdParam *param, char *buff, uint32_t *buffLen, const BSL_UI_CheckDataCallBack checkDataCallBack, void *callBackData); /** * @ingroup bsl_ui * @brief Create a DataPack object * @attention * Thread safe : Thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @return If success, the BSL_UI_DataPack object is returned. * If fail, other values are returned. */ BSL_UI_DataPack *BSL_UI_DataPackNew(void); /** * @ingroup bsl_ui * @brief Release the BSL_UI_DataPack * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param data [IN] BSL_UI_DataPack Object * * @return None */ void BSL_UI_DataPackFree(BSL_UI_DataPack *data); /** * @ingroup bsl_ui * @brief Set the BSL_UI_DataPack data by type * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param data [IN] BSL_UI_DataPack Object * @param type [IN] Type of the control command. * @param parg [IN] Pointer to additional command arguments. * @param larg [IN] Large parameter for the command. * * @return If success, BSL_SUCCESS is returned. * Else, other values are returned. */ int32_t BSL_UI_DataCtrl(BSL_UI_DataPack *data, uint32_t type, void *parg, uint32_t larg); /** * @ingroup bsl_ui * @brief Obtaining user data result * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param data [IN] Data object to be obtained. * @param result [OUT] User data result. * @param result_len [OUT] User data result length. * * @return If success, BSL_SUCCESS is returned. * Else, other values are returned. */ int32_t BSL_UI_GetDataResult(BSL_UI_DataPack *data, char **result, uint32_t *resultLen); /** * @ingroup bsl_ui * @brief Construct a prompt message * @attention * Thread safe : Not thread-safe function. * Blocking risk : No blocking. * Time consuming : Not time-consuming. * * @param objectDesc [IN] Object description in the prompt message. * @param objectName [IN] Object name in the prompt message. * * @return If success, constructed prompt string is returned. * Else, NULL is returned. */ char *BSL_UI_ConstructPrompt(const char *objectDesc, const char *objectName); #ifdef __cplusplus } #endif #endif
2301_79861745/bench_create
include/bsl/bsl_ui.h
C
unknown
11,106
/* * 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 bsl_uio * @ingroup bsl * @brief uio module */ #ifndef BSL_UIO_H #define BSL_UIO_H #include <stddef.h> #include <stdint.h> #include <stdbool.h> #ifdef __cplusplus extern "C" { #endif /** * @ingroup bsl_uio * @brief UIO module control structure */ typedef struct UIO_ControlBlock BSL_UIO; /** * @ingroup bsl_uio * @brief BSL_UIO_BufMem structure */ typedef struct { size_t length; char *data; size_t max; } BSL_UIO_BufMem; typedef int32_t (*BslUioWriteCb)(BSL_UIO *uio, const void *buf, uint32_t len, uint32_t *writeLen); typedef int32_t (*BslUioReadCb)(BSL_UIO *uio, void *buf, uint32_t len, uint32_t *readLen); typedef int32_t (*BslUioCtrlCb)(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg); typedef int32_t (*BslUioCreateCb)(BSL_UIO *uio); typedef int32_t (*BslUioDestroyCb)(BSL_UIO *uio); typedef int32_t (*BslUioPutsCb)(BSL_UIO *uio, const char *buf, uint32_t *writeLen); typedef int32_t (*BslUioGetsCb)(BSL_UIO *uio, char *buf, uint32_t *readLen); typedef struct BSL_UIO_MethodStruct BSL_UIO_Method; /** * @ingroup bsl_uio * @brief userData release function */ typedef void (*BSL_UIO_USERDATA_FREE_FUNC)(void *); /** * @ingroup bsl_uio * @brief Transmission protocol enumeration */ typedef enum { BSL_UIO_TCP, BSL_UIO_UDP, BSL_UIO_SCTP, BSL_UIO_MEM, BSL_UIO_FILE, BSL_UIO_BUFFER, BSL_UIO_UNKNOWN, /* Unknown protocol should not appear */ BSL_UIO_EXTEND = 10000, /* extension value */ } BSL_UIO_TransportType; #define IS_TRANSTYPE_DATAGRAM(transportType) ((transportType) == BSL_UIO_SCTP || (transportType) == BSL_UIO_UDP) /** * @ingroup bsl_uio * @brief Sctp auth key, hitls Use the BSL_UIO_Method.ctrl method to transfer the BSL_UIO_SCTP_ADD_AUTH_SHARED_KEY * instruction to notify the user that the auth key needs to be set. */ typedef struct { uint16_t shareKeyId; uint16_t authKeySize; const uint8_t *authKey; } BSL_UIO_SctpAuthKey; /** * @ingroup bsl_uio * @brief BSL_UIO_CtrlParameter controls the I/O callback function. Hitls notifies the * user of the function to be implemented */ typedef enum { /* The cmd(0-0x99) used by the abstraction layer and the uio * implemented by the user cannot reuse these values. */ BSL_UIO_GET_INIT = 0x0, BSL_UIO_GET_WRITE_NUM, BSL_UIO_GET_READ_NUM, /* Public use 0x100 */ BSL_UIO_SET_PEER_IP_ADDR = 0x100, BSL_UIO_GET_PEER_IP_ADDR, BSL_UIO_SET_FD, BSL_UIO_GET_FD, BSL_UIO_FLUSH, BSL_UIO_RESET, BSL_UIO_PENDING, BSL_UIO_WPENDING, BSL_UIO_SET_BUFFER_SIZE, /* UDP uses 0x2XX */ BSL_UIO_UDP_SET_CONNECTED = 0x200, BSL_UIO_UDP_GET_MTU_OVERHEAD, BSL_UIO_UDP_QUERY_MTU, BSL_UIO_UDP_MTU_EXCEEDED, /* SCTP uses 0x3XX */ BSL_UIO_SCTP_CHECK_PEER_AUTH = 0x300, BSL_UIO_SCTP_ADD_AUTH_SHARED_KEY, BSL_UIO_SCTP_ACTIVE_AUTH_SHARED_KEY, BSL_UIO_SCTP_DEL_PRE_AUTH_SHARED_KEY, BSL_UIO_SCTP_SND_BUFF_IS_EMPTY, BSL_UIO_SCTP_GET_SEND_STREAM_ID, BSL_UIO_SCTP_SET_APP_STREAM_ID, BSL_UIO_SCTP_MASK_APP_MESSAGE, BSL_UIO_SCTP_SET_CALLBACK, /* MEM uses 0x4XX */ BSL_UIO_MEM_NEW_BUF = 0x400, BSL_UIO_MEM_GET_PTR, BSL_UIO_MEM_SET_EOF, BSL_UIO_MEM_GET_EOF, BSL_UIO_MEM_GET_INFO, BSL_UIO_FILE_OPEN = 0x500, BSL_UIO_FILE_PTR, BSL_UIO_FILE_GET_EOF, } BSL_UIO_CtrlParameter; typedef enum { BSL_UIO_CREATE_CB, BSL_UIO_DESTROY_CB, BSL_UIO_WRITE_CB, BSL_UIO_READ_CB, BSL_UIO_CTRL_CB, BSL_UIO_PUTS_CB, BSL_UIO_GETS_CB, } BSL_UIO_METHOD_TYPE; #define BSL_UIO_FILE_READ 0x02 #define BSL_UIO_FILE_WRITE 0x04 #define BSL_UIO_FILE_APPEND 0x08 #define BSL_UIO_FILE_TEXT 0x10 #define BSL_UIO_FLAGS_READ 0x01 #define BSL_UIO_FLAGS_WRITE 0x02 #define BSL_UIO_FLAGS_IO_SPECIAL 0x04 #define BSL_UIO_FLAGS_RWS (BSL_UIO_FLAGS_READ | BSL_UIO_FLAGS_WRITE | BSL_UIO_FLAGS_IO_SPECIAL) #define BSL_UIO_FLAGS_SHOULD_RETRY 0x08 #define BSL_UIO_FLAGS_MEM_READ_ONLY 0x10 /* This flag can be set only by uio_mem */ #define BSL_UIO_FLAGS_BASE64_NO_NEWLINE 0x20 #define BSL_UIO_FLAGS_BASE64_PEM 0x40 typedef struct { uint8_t *addr; uint32_t size; } BSL_UIO_CtrlGetPeerIpAddrParam; /** * @ingroup bsl_uio * @brief Creating uio method structure * * @retval uio method structure pointer */ BSL_UIO_Method *BSL_UIO_NewMethod(void); /** * @ingroup bsl_uio * @brief set uio method type * * @param meth [IN] uio method structure * @param type [IN] type * @retval #BSL_SUCCESS * @retval #BSL_NULL_INPUT */ int32_t BSL_UIO_SetMethodType(BSL_UIO_Method *meth, int32_t type); /** * @ingroup bsl_uio * @brief set uio method callback * * @param meth [IN] uio method structure * @param type [IN] callback type * @param func [IN] callback pointer * @retval #BSL_SUCCESS * @retval #BSL_INVALID_ARG */ int32_t BSL_UIO_SetMethod(BSL_UIO_Method *meth, int32_t type, void *func); /** * @ingroup bsl_uio * @brief free uio Method * * @param meth [IN] uio method structure * @retval void */ void BSL_UIO_FreeMethod(BSL_UIO_Method *meth); /** * @ingroup bsl_uio * @brief obtain the default MEM UIO * * @retval pointer to the MEM UIO method */ const BSL_UIO_Method *BSL_UIO_MemMethod(void); /** * @ingroup bsl_uio * * @brief obtain the default FILE UIO method * @return pointer to the FILE UIO method */ const BSL_UIO_Method *BSL_UIO_FileMethod(void); /** * @ingroup bsl_uio * @brief obtain the default SCTP UIO * * @retval pointer to the SCTP UIO method */ const BSL_UIO_Method *BSL_UIO_SctpMethod(void); /** * @ingroup bsl_uio * @brief obtain the default TCP UIO method * * @retval pointer to the TCP UIO method */ const BSL_UIO_Method *BSL_UIO_TcpMethod(void); /** * @ingroup bsl_uio * @brief obtain the default UDP UIO method * * @retval pointer to the UDP UIO method */ const BSL_UIO_Method *BSL_UIO_UdpMethod(void); /** * @ingroup bsl_uio * @brief obtain the default buffer UIO * * @retval pointer to the Buffer UIO method */ const BSL_UIO_Method *BSL_UIO_BufferMethod(void); /** * @ingroup bsl_uio * @brief Create a UIO object * * @param method [IN] UIO method structure * @retval UIO, created successfully * @retval NULL UIO, creation failure */ BSL_UIO *BSL_UIO_New(const BSL_UIO_Method *method); /** * @ingroup bsl_uio * @brief Release the UIO object. * * @param uio [IN] UIO object. */ void BSL_UIO_Free(BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Write data to the UIO object * * @param uio [IN] uio object. * @param data [IN] Data to be written. * @param len [IN] Data length. * @param writeLen [OUT] Length of the data that is successfully written. * @retval #BSL_SUCCESS, indicating that the data is successfully written. * @retval #BSL_INTERNAL_EXCEPTION, an unexpected internal error occurs. * @retval #BSL_UIO_IO_BUSY, indicating that the underlying I/O is busy. * @retval #BSL_UIO_IO_EXCEPTION, The I/O is abnormal. * @retval #BSL_UIO_FAIL,invalid parameter. */ int32_t BSL_UIO_Write(BSL_UIO *uio, const void *data, uint32_t len, uint32_t *writeLen); /** * @ingroup bsl_uio * @brief Read data from the UIO object. * * @param uio [IN] uio object. * @param data [IN] Buffer for receiving data * @param len [IN] Length of the received data buffer. * @param readLen [OUT] Length of the received data. * @retval #BSL_SUCCESS, The data is read successfully(Determined based on the actual receive length, * if the length is 0 means no data is read.) * @retval #BSL_INTERNAL_EXCEPTION, an unexpected internal error occurs. * @retval #BSL_UIO_FAIL, invalid parameter. * @retval #BSL_UIO_IO_EXCEPTION, IO is abnormal. */ int32_t BSL_UIO_Read(BSL_UIO *uio, void *data, uint32_t len, uint32_t *readLen); /** * @ingroup bsl_uio * @brief Process specific UIO implementations by cmd * * @param uio [IN] UIO object * @param cmd [IN] Different cmd processes perform different operations on UIO objects. * @param larg [IN] Determined by cmd. For details, see the following * @param parg [IN/OUT] Determined by cmd. For details, see the following * @retval #BSL_SUCCESS * @retval Non-BSL_SUCCESS, for details, see bsl_errno.h. * * @brief set the peer IP address in the UIO object * * The address format is a binary address in network byte order, with a length of 4 or 16 bytes. * A generated cookie will be provided for use by the HelloVerifyRequest for dtls. * * @param uio [IN] UIO object * @param cmd [IN] BSL_UIO_SET_PEER_IP_ADDR * @param larg [IN] Size of the peer address: The length must be 4 or 16 * @param parg [IN] Peer address * * @brief Obtain the peer IP address from the UIO object * * The obtained address is in the network byte order binary address format. * The input length must be greater than the configured size. * The purpose is to provide a generated cookie for use by the HelloVerifyRequest of the dtls. * * @param uio [IN] UIO object * @param cmd [IN] BSL_UIO_GET_PEER_IP_ADDR * @param larg [IN] 0 * @param parg [IN] BSL_UIO_CtrlGetPeerIpAddrParam *, include: * addr [IN/OUT] Peer address, * size [IN/OUT] IN: size of the input buffer OUT: size of the output peer address * * @brief Obtain the stream ID sent by the SCTP from the UIO object. * * This API needs to be called by users in BSL_UIO_Method.write * and send SCTP messages based on the obtained stream ID * * @param uio [IN] UIO object * @param cmd [IN] BSL_UIO_SCTP_GET_SEND_STREAM_ID * @param larg [IN] 0 * @param parg [IN/OUT] ID of the sent stream, uint16_t* Type * * @brief Set the stream ID of the app message sent by the SCTP in the UIO object. * * If a service message needs to be processed by a specific stream ID, this interface can be called. * * @param uio [IN] UIO object * @param cmd [IN] BSL_UIO_SCTP_SET_APP_STREAM_ID * @param larg [IN] App stream ID. The value ranges from 0 to 65535 * @param parg [IN] NULL */ int32_t BSL_UIO_Ctrl(BSL_UIO *uio, int32_t cmd, int32_t larg, void *parg); /** * @ingroup bsl_uio * @brief Write a string to the UIO object. * * @param uio [IN] uio object. * @param buf [IN] A null-terminated string to be written. * @param writeLen [OUT] Length of the data that is successfully written. * @retval #BSL_SUCCESS, Writing succeeded. * @retval #BSL_INTERNAL_EXCEPTION, an unexpected internal error occurs. * @retval #BSL_UIO_IO_BUSY, indicating that the underlying I/O is busy. * @retval #BSL_UIO_IO_EXCEPTION, IO abnormal. * @retval #BSL_UIO_FAIL, invalid parameter. */ int32_t BSL_UIO_Puts(BSL_UIO *uio, const char *buf, uint32_t *writeLen); /** * @ingroup bsl_uio * @brief Reads a string from the UIO object * * @param uio [IN] uio object. * @param buf [IN] Buffer that accepts a line of strings * @param readLen [IN/OUT] Length of the buffer for receiving data/Length of the data that is successfully read * @retval #BSL_SUCCESS (Determine the value based on the actual receive length. * if the length is 0 means no data is read.) * @retval #BSL_INTERNAL_EXCEPTION, an unexpected internal error occurs. * @retval #BSL_UIO_FAIL, invalid parameter. * @retval #BSL_UIO_IO_EXCEPTION, IO abnormal. */ int32_t BSL_UIO_Gets(BSL_UIO *uio, char *buf, uint32_t *readLen); /** * @ingroup bsl_uio * @brief Set the UIO init. * * @param uio [IN] UIO object * @param init [IN] init value */ void BSL_UIO_SetInit(BSL_UIO *uio, bool init); /** * @ingroup bsl_uio * @brief Obtain the UIO transmission protocol type * * @param uio [IN] UIO object. * @retval protocol type */ int32_t BSL_UIO_GetTransportType(const BSL_UIO *uio); /** * @ingroup bsl_uio * * @brief Obtain the UIO transmission protocol type * @param uio [IN] UIO object. * @param uioType [IN] Type of the protocol to be obtained. * @return TRUE, Succeeded in obtaining the UIO type. * @return FALSE, Failed to obtain the UIO type. */ bool BSL_UIO_GetUioChainTransportType(BSL_UIO *uio, const BSL_UIO_TransportType uioType); /** * @ingroup bsl_uio * @brief Set the user data in the UIO object * * UIO will not modify the user data, user can add some information * for the UIO, and get the information by use BSL_UIO_GetUserData function; After you set user data by calling * BSL_UIO_SetUserData, you need to call BSL_UIO_SetUserData again before calling BSL_UIO_Free to set * user data to null to ensure that all memory is released. * * @param uio [IN] UIO object. * @param data [IN] User data pointer * @retval #BSL_SUCCESS, success. * @retval #BSL_NULL_INPUT, invalid null pointer. */ int32_t BSL_UIO_SetUserData(BSL_UIO *uio, void *data); /** * @ingroup bsl_uio * @brief Release the user data set in the UIO object. * * Free uio->userData at BSL_UIO_Free. * * @param uio [IN] UIO object * @param data [IN] Pointer to the function for releasing user data * @retval #BSL_SUCCESS, success. * @retval #BSL_NULL_INPUT, invalid null pointer. */ int32_t BSL_UIO_SetUserDataFreeFunc(BSL_UIO *uio, BSL_UIO_USERDATA_FREE_FUNC userDataFreeFunc); /** * @ingroup bsl_uio * @brief Obtain the user data in the UIO object. * * The user data comes from users, and tls will not change any thing * for user data, user can add some customize information. * * @param uio [IN] UIO object. * @retval Succeeded in obtaining the data structure pointer stored by the user. * @retval NULL, the obtained data does not exist. */ void *BSL_UIO_GetUserData(const BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Obtains whether resources associated with the UIO are closed by the UIO. * * @param uio [OUT] UIO object * @retval ture The resources associated with the UIO are closed by the UIO. * @retval false The resources associated with the UIO are not closed by the UIO. */ bool BSL_UIO_GetIsUnderlyingClosedByUio(const BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Set whether resources associated with the UIO are closed by the UIO. * * @param uio [IN/OUT] UIO object * @param close [IN] true UIO-associated resources are closed by the UIO. * false The resources associated with the UIO are not closed by the UIO. */ void BSL_UIO_SetIsUnderlyingClosedByUio(BSL_UIO *uio, bool close); /** * @ingroup bsl_uio * @brief Method for obtaining the UIO * * @param uio [IN/OUT] UIO object * @retval UIO method */ const BSL_UIO_Method *BSL_UIO_GetMethod(const BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Obtain the implementation-related context. * * @param uio [IN] UIO object * @retval Implementation-related context pointer */ void *BSL_UIO_GetCtx(const BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Set the implementation-related context. * * @param uio [IN] UIO object * @param ctx [IN] Implement the relevant context pointer. */ void BSL_UIO_SetCtx(BSL_UIO *uio, void *ctx); /** * @ingroup bsl_uio * @brief Set the fd of the UIO object * * @param uio [IN] UIO object * @param fd [IN] File Descriptor fd */ void BSL_UIO_SetFD(BSL_UIO *uio, int fd); /** * @ingroup bsl_uio * @brief Set the UIO object flag. * * @param uio [IN] UIO object * @param flags [IN] flag * @retval #BSL_SUCCESS, succeeded. * @retval Other reference: bsl_errno.h. */ int32_t BSL_UIO_SetFlags(BSL_UIO *uio, uint32_t flags); /** * @ingroup bsl_uio * @brief Clear the UIO object flag * * @param uio [IN] UIO object * @param flags [IN] flag * @retval #BSL_SUCCESS, succeeded. * @retval Other reference: bsl_errno.h. */ int32_t BSL_UIO_ClearFlags(BSL_UIO *uio, uint32_t flags); /** * @ingroup bsl_uio * @brief Check the UIO object flag * * @param uio [IN] UIO object * @param flags [IN] To-be-checked flag * @param out [OUT] Mark the detection result * @retval #BSL_SUCCESS, succeeded. * @retval Other reference: bsl_errno.h */ uint32_t BSL_UIO_TestFlags(const BSL_UIO *uio, uint32_t flags, uint32_t *out); /** * @ingroup bsl_uio * @brief Set the value of uio reference counting to 1 * * @attention Call BSL_UIO_Free to decrease the value of reference counting by 1 * @param uio [IN] uio object * @retval #BSL_SUCCESS, the setting is successful. * @retval #BSL_INTERNAL_EXCEPTION, an unexpected internal error occurs. * @retval #BSL_UIO_REF_MAX, The number of UIO objects has reached the maximum. */ int32_t BSL_UIO_UpRef(BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Add a UIO object to the tail of the chain. * * @attention The reference counting of the added UIO object will not increase by 1. * @param uio [IN] uio object * @param tail [IN] UIO object added to the tail * @retval #BSL_SUCCESS, success. * @retval Non-BSL_SUCCESS, failure. For details, see bsl_errno.h. */ int32_t BSL_UIO_Append(BSL_UIO *uio, BSL_UIO *tail); /** * @ingroup bsl_uio * @brief Pop UIO object from the chain. * * @attention The reference counting of the added UIO object does not decrease by 1. * @param uio [IN] UIO object of the pop-up link. * @retval The next UIO object in the chain. */ BSL_UIO *BSL_UIO_PopCurrent(BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Release UIO object b and its subsequent chains. * * @attention: The release starts from b. * If the reference counting of a UIO object in the chain is greater than or equal to 1, the release stops * @param uio [IN] First UIO object in the UIO object chain to be released */ void BSL_UIO_FreeChain(BSL_UIO *uio); /** * @ingroup bsl_uio * @brief Obtain the next UIO object in the chain. * * @param uio [IN] UIO object * @retval Next UIO object in the chain. */ BSL_UIO *BSL_UIO_Next(BSL_UIO *uio); #ifdef __cplusplus } #endif #endif // BSL_UIO_H
2301_79861745/bench_create
include/bsl/bsl_uio.h
C
unknown
18,307
/* * 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 * @brief crypto module */ /** * @defgroup crypt_algid * @ingroup crypt * @brief id of algorithms */ #ifndef CRYPT_ALGID_H #define CRYPT_ALGID_H #include "bsl_obj.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus /** * @ingroup crypt_algid * * RAND algorithm ID */ typedef enum { CRYPT_RAND_SHA1 = BSL_CID_RAND_SHA1, CRYPT_RAND_SHA224 = BSL_CID_RAND_SHA224, CRYPT_RAND_SHA256 = BSL_CID_RAND_SHA256, CRYPT_RAND_SHA384 = BSL_CID_RAND_SHA384, CRYPT_RAND_SHA512 = BSL_CID_RAND_SHA512, CRYPT_RAND_HMAC_SHA1 = BSL_CID_RAND_HMAC_SHA1, CRYPT_RAND_HMAC_SHA224 = BSL_CID_RAND_HMAC_SHA224, CRYPT_RAND_HMAC_SHA256 = BSL_CID_RAND_HMAC_SHA256, CRYPT_RAND_HMAC_SHA384 = BSL_CID_RAND_HMAC_SHA384, CRYPT_RAND_HMAC_SHA512 = BSL_CID_RAND_HMAC_SHA512, CRYPT_RAND_AES128_CTR = BSL_CID_RAND_AES128_CTR, CRYPT_RAND_AES192_CTR = BSL_CID_RAND_AES192_CTR, CRYPT_RAND_AES256_CTR = BSL_CID_RAND_AES256_CTR, CRYPT_RAND_AES128_CTR_DF = BSL_CID_RAND_AES128_CTR_DF, CRYPT_RAND_AES192_CTR_DF = BSL_CID_RAND_AES192_CTR_DF, CRYPT_RAND_AES256_CTR_DF = BSL_CID_RAND_AES256_CTR_DF, CRYPT_RAND_SM3 = BSL_CID_RAND_SM3, CRYPT_RAND_SM4_CTR_DF = BSL_CID_RAND_SM4_CTR_DF, CRYPT_RAND_ALGID_MAX = BSL_CID_UNKNOWN } CRYPT_RAND_AlgId; /** * @ingroup crypt_algid * * Hash algorithm ID */ typedef enum { CRYPT_MD_MD5 = BSL_CID_MD5, CRYPT_MD_SHA1 = BSL_CID_SHA1, CRYPT_MD_SHA224 = BSL_CID_SHA224, CRYPT_MD_SHA256 = BSL_CID_SHA256, CRYPT_MD_SHA384 = BSL_CID_SHA384, CRYPT_MD_SHA512 = BSL_CID_SHA512, CRYPT_MD_SHA3_224 = BSL_CID_SHA3_224, CRYPT_MD_SHA3_256 = BSL_CID_SHA3_256, CRYPT_MD_SHA3_384 = BSL_CID_SHA3_384, CRYPT_MD_SHA3_512 = BSL_CID_SHA3_512, CRYPT_MD_SHAKE128 = BSL_CID_SHAKE128, CRYPT_MD_SHAKE256 = BSL_CID_SHAKE256, CRYPT_MD_SM3 = BSL_CID_SM3, CRYPT_MD_MAX = BSL_CID_UNKNOWN } CRYPT_MD_AlgId; /** * @ingroup crypt_algid * * MAC algorithm ID */ typedef enum { CRYPT_MAC_HMAC_MD5 = BSL_CID_HMAC_MD5, CRYPT_MAC_HMAC_SHA1 = BSL_CID_HMAC_SHA1, CRYPT_MAC_HMAC_SHA224 = BSL_CID_HMAC_SHA224, CRYPT_MAC_HMAC_SHA256 = BSL_CID_HMAC_SHA256, CRYPT_MAC_HMAC_SHA384 = BSL_CID_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512 = BSL_CID_HMAC_SHA512, CRYPT_MAC_HMAC_SHA3_224 = BSL_CID_HMAC_SHA3_224, CRYPT_MAC_HMAC_SHA3_256 = BSL_CID_HMAC_SHA3_256, CRYPT_MAC_HMAC_SHA3_384 = BSL_CID_HMAC_SHA3_384, CRYPT_MAC_HMAC_SHA3_512 = BSL_CID_HMAC_SHA3_512, CRYPT_MAC_HMAC_SM3 = BSL_CID_HMAC_SM3, CRYPT_MAC_CMAC_AES128 = BSL_CID_CMAC_AES128, CRYPT_MAC_CMAC_AES192 = BSL_CID_CMAC_AES192, CRYPT_MAC_CMAC_AES256 = BSL_CID_CMAC_AES256, CRYPT_MAC_CMAC_SM4 = BSL_CID_CMAC_SM4, CRYPT_MAC_CBC_MAC_SM4 = BSL_CID_CBC_MAC_SM4, CRYPT_MAC_GMAC_AES128 = BSL_CID_GMAC_AES128, CRYPT_MAC_GMAC_AES192 = BSL_CID_GMAC_AES192, CRYPT_MAC_GMAC_AES256 = BSL_CID_GMAC_AES256, CRYPT_MAC_SIPHASH64 = BSL_CID_SIPHASH64, CRYPT_MAC_SIPHASH128 = BSL_CID_SIPHASH128, CRYPT_MAC_MAX = BSL_CID_UNKNOWN } CRYPT_MAC_AlgId; /** * @ingroup crypt_algid * * Asymmetric algorithm ID */ typedef enum { CRYPT_PKEY_DSA = BSL_CID_DSA, CRYPT_PKEY_ED25519 = BSL_CID_ED25519, CRYPT_PKEY_X25519 = BSL_CID_X25519, CRYPT_PKEY_RSA = BSL_CID_RSA, CRYPT_PKEY_DH = BSL_CID_DH, CRYPT_PKEY_ECDSA = BSL_CID_ECDSA, CRYPT_PKEY_ECDH = BSL_CID_ECDH, CRYPT_PKEY_SM2 = BSL_CID_SM2DSA, CRYPT_PKEY_PAILLIER = BSL_CID_PAILLIER, CRYPT_PKEY_ELGAMAL = BSL_CID_ELGAMAL, CRYPT_PKEY_SLH_DSA = BSL_CID_SLH_DSA, CRYPT_PKEY_ML_KEM = BSL_CID_ML_KEM, CRYPT_PKEY_ML_DSA = BSL_CID_ML_DSA, CRYPT_PKEY_HYBRID_KEM = BSL_CID_HYBRID_KEM, CRYPT_PKEY_XMSS = BSL_CID_XMSS, CRYPT_PKEY_MAX = BSL_CID_UNKNOWN } CRYPT_PKEY_AlgId; /** * @ingroup cipher_algid * @brief Symmetric algorithm mode ID * * There is a mapping relationship with the g_ealCipherMethod list. Attention any modification must be synchronized. */ typedef enum { CRYPT_CIPHER_AES128_CBC = BSL_CID_AES128_CBC, CRYPT_CIPHER_AES192_CBC = BSL_CID_AES192_CBC, CRYPT_CIPHER_AES256_CBC = BSL_CID_AES256_CBC, CRYPT_CIPHER_AES128_CTR = BSL_CID_AES128_CTR, CRYPT_CIPHER_AES192_CTR = BSL_CID_AES192_CTR, CRYPT_CIPHER_AES256_CTR = BSL_CID_AES256_CTR, CRYPT_CIPHER_AES128_ECB = BSL_CID_AES128_ECB, CRYPT_CIPHER_AES192_ECB = BSL_CID_AES192_ECB, CRYPT_CIPHER_AES256_ECB = BSL_CID_AES256_ECB, CRYPT_CIPHER_AES128_XTS = BSL_CID_AES128_XTS, CRYPT_CIPHER_AES256_XTS = BSL_CID_AES256_XTS, CRYPT_CIPHER_AES128_CCM = BSL_CID_AES128_CCM, CRYPT_CIPHER_AES192_CCM = BSL_CID_AES192_CCM, CRYPT_CIPHER_AES256_CCM = BSL_CID_AES256_CCM, CRYPT_CIPHER_AES128_GCM = BSL_CID_AES128_GCM, CRYPT_CIPHER_AES192_GCM = BSL_CID_AES192_GCM, CRYPT_CIPHER_AES256_GCM = BSL_CID_AES256_GCM, CRYPT_CIPHER_CHACHA20_POLY1305 = BSL_CID_CHACHA20_POLY1305, CRYPT_CIPHER_SM4_XTS = BSL_CID_SM4_XTS, CRYPT_CIPHER_SM4_CBC = BSL_CID_SM4_CBC, CRYPT_CIPHER_SM4_ECB = BSL_CID_SM4_ECB, CRYPT_CIPHER_SM4_CTR = BSL_CID_SM4_CTR, CRYPT_CIPHER_SM4_GCM = BSL_CID_SM4_GCM, CRYPT_CIPHER_SM4_CFB = BSL_CID_SM4_CFB, CRYPT_CIPHER_SM4_OFB = BSL_CID_SM4_OFB, CRYPT_CIPHER_AES128_CFB = BSL_CID_AES128_CFB, CRYPT_CIPHER_AES192_CFB = BSL_CID_AES192_CFB, CRYPT_CIPHER_AES256_CFB = BSL_CID_AES256_CFB, CRYPT_CIPHER_AES128_OFB = BSL_CID_AES128_OFB, CRYPT_CIPHER_AES192_OFB = BSL_CID_AES192_OFB, CRYPT_CIPHER_AES256_OFB = BSL_CID_AES256_OFB, CRYPT_CIPHER_MAX = BSL_CID_UNKNOWN, } CRYPT_CIPHER_AlgId; /** * @ingroup crypt_algid * * Parameter ID of an asymmetric algorithm. The most significant 16 bits indicate the algorithm ID, * and the least significant 16 bits map the ID definition of the algorithm LowLevel. */ typedef enum { CRYPT_DH_RFC2409_768 = BSL_CID_DH_RFC2409_768, CRYPT_DH_RFC2409_1024 = BSL_CID_DH_RFC2409_1024, CRYPT_DH_RFC3526_1536 = BSL_CID_DH_RFC3526_1536, CRYPT_DH_RFC3526_2048 = BSL_CID_DH_RFC3526_2048, CRYPT_DH_RFC3526_3072 = BSL_CID_DH_RFC3526_3072, CRYPT_DH_RFC3526_4096 = BSL_CID_DH_RFC3526_4096, CRYPT_DH_RFC3526_6144 = BSL_CID_DH_RFC3526_6144, CRYPT_DH_RFC3526_8192 = BSL_CID_DH_RFC3526_8192, CRYPT_DH_RFC7919_2048 = BSL_CID_DH_RFC7919_2048, CRYPT_DH_RFC7919_3072 = BSL_CID_DH_RFC7919_3072, CRYPT_DH_RFC7919_4096 = BSL_CID_DH_RFC7919_4096, CRYPT_DH_RFC7919_6144 = BSL_CID_DH_RFC7919_6144, CRYPT_DH_RFC7919_8192 = BSL_CID_DH_RFC7919_8192, CRYPT_ECC_NISTP224 = BSL_CID_NIST_PRIME224, CRYPT_ECC_NISTP256 = BSL_CID_PRIME256V1, CRYPT_ECC_NISTP384 = BSL_CID_SECP384R1, CRYPT_ECC_NISTP521 = BSL_CID_SECP521R1, CRYPT_ECC_BRAINPOOLP256R1 = BSL_CID_ECC_BRAINPOOLP256R1, CRYPT_ECC_BRAINPOOLP384R1 = BSL_CID_ECC_BRAINPOOLP384R1, CRYPT_ECC_BRAINPOOLP512R1 = BSL_CID_ECC_BRAINPOOLP512R1, CRYPT_ECC_SM2 = BSL_CID_SM2PRIME256, CRYPT_HYBRID_X25519_MLKEM512 = BSL_CID_X25519_MLKEM512, CRYPT_HYBRID_X25519_MLKEM768 = BSL_CID_X25519_MLKEM768, CRYPT_HYBRID_X25519_MLKEM1024 = BSL_CID_X25519_MLKEM1024, CRYPT_HYBRID_ECDH_NISTP256_MLKEM512 = BSL_CID_ECDH_NISTP256_MLKEM512, CRYPT_HYBRID_ECDH_NISTP256_MLKEM768 = BSL_CID_ECDH_NISTP256_MLKEM768, CRYPT_HYBRID_ECDH_NISTP256_MLKEM1024 = BSL_CID_ECDH_NISTP256_MLKEM1024, CRYPT_HYBRID_ECDH_NISTP384_MLKEM512 = BSL_CID_ECDH_NISTP384_MLKEM512, CRYPT_HYBRID_ECDH_NISTP384_MLKEM768 = BSL_CID_ECDH_NISTP384_MLKEM768, CRYPT_HYBRID_ECDH_NISTP384_MLKEM1024 = BSL_CID_ECDH_NISTP384_MLKEM1024, CRYPT_HYBRID_ECDH_NISTP521_MLKEM512 = BSL_CID_ECDH_NISTP521_MLKEM512, CRYPT_HYBRID_ECDH_NISTP521_MLKEM768 = BSL_CID_ECDH_NISTP521_MLKEM768, CRYPT_HYBRID_ECDH_NISTP521_MLKEM1024 = BSL_CID_ECDH_NISTP521_MLKEM1024, CRYPT_MLDSA_TYPE_MLDSA_44 = BSL_CID_ML_DSA_44, CRYPT_MLDSA_TYPE_MLDSA_65 = BSL_CID_ML_DSA_65, CRYPT_MLDSA_TYPE_MLDSA_87 = BSL_CID_ML_DSA_87, CRYPT_KEM_TYPE_MLKEM_512 = BSL_CID_ML_KEM_512, CRYPT_KEM_TYPE_MLKEM_768 = BSL_CID_ML_KEM_768, CRYPT_KEM_TYPE_MLKEM_1024 = BSL_CID_ML_KEM_1024, CRYPT_SLH_DSA_SHA2_128S = BSL_CID_SLH_DSA_SHA2_128S, CRYPT_SLH_DSA_SHAKE_128S = BSL_CID_SLH_DSA_SHAKE_128S, CRYPT_SLH_DSA_SHA2_128F = BSL_CID_SLH_DSA_SHA2_128F, CRYPT_SLH_DSA_SHAKE_128F = BSL_CID_SLH_DSA_SHAKE_128F, CRYPT_SLH_DSA_SHA2_192S = BSL_CID_SLH_DSA_SHA2_192S, CRYPT_SLH_DSA_SHAKE_192S = BSL_CID_SLH_DSA_SHAKE_192S, CRYPT_SLH_DSA_SHA2_192F = BSL_CID_SLH_DSA_SHA2_192F, CRYPT_SLH_DSA_SHAKE_192F = BSL_CID_SLH_DSA_SHAKE_192F, CRYPT_SLH_DSA_SHA2_256S = BSL_CID_SLH_DSA_SHA2_256S, CRYPT_SLH_DSA_SHAKE_256S = BSL_CID_SLH_DSA_SHAKE_256S, CRYPT_SLH_DSA_SHA2_256F = BSL_CID_SLH_DSA_SHA2_256F, CRYPT_SLH_DSA_SHAKE_256F = BSL_CID_SLH_DSA_SHAKE_256F, CRYPT_XMSS_SHA2_10_256 = BSL_CID_XMSS_SHA2_10_256, CRYPT_XMSS_SHA2_16_256 = BSL_CID_XMSS_SHA2_16_256, CRYPT_XMSS_SHA2_20_256 = BSL_CID_XMSS_SHA2_20_256, CRYPT_XMSS_SHA2_10_512 = BSL_CID_XMSS_SHA2_10_512, CRYPT_XMSS_SHA2_16_512 = BSL_CID_XMSS_SHA2_16_512, CRYPT_XMSS_SHA2_20_512 = BSL_CID_XMSS_SHA2_20_512, CRYPT_XMSS_SHAKE_10_256 = BSL_CID_XMSS_SHAKE_10_256, CRYPT_XMSS_SHAKE_16_256 = BSL_CID_XMSS_SHAKE_16_256, CRYPT_XMSS_SHAKE_20_256 = BSL_CID_XMSS_SHAKE_20_256, CRYPT_XMSS_SHAKE_10_512 = BSL_CID_XMSS_SHAKE_10_512, CRYPT_XMSS_SHAKE_16_512 = BSL_CID_XMSS_SHAKE_16_512, CRYPT_XMSS_SHAKE_20_512 = BSL_CID_XMSS_SHAKE_20_512, CRYPT_XMSS_SHA2_10_192 = BSL_CID_XMSS_SHA2_10_192, CRYPT_XMSS_SHA2_16_192 = BSL_CID_XMSS_SHA2_16_192, CRYPT_XMSS_SHA2_20_192 = BSL_CID_XMSS_SHA2_20_192, CRYPT_XMSS_SHAKE256_10_256 = BSL_CID_XMSS_SHAKE256_10_256, CRYPT_XMSS_SHAKE256_16_256 = BSL_CID_XMSS_SHAKE256_16_256, CRYPT_XMSS_SHAKE256_20_256 = BSL_CID_XMSS_SHAKE256_20_256, CRYPT_XMSS_SHAKE256_10_192 = BSL_CID_XMSS_SHAKE256_10_192, CRYPT_XMSS_SHAKE256_16_192 = BSL_CID_XMSS_SHAKE256_16_192, CRYPT_XMSS_SHAKE256_20_192 = BSL_CID_XMSS_SHAKE256_20_192, CRYPT_XMSSMT_SHA2_20_2_256 = BSL_CID_XMSSMT_SHA2_20_2_256, CRYPT_XMSSMT_SHA2_20_4_256 = BSL_CID_XMSSMT_SHA2_20_4_256, CRYPT_XMSSMT_SHA2_40_2_256 = BSL_CID_XMSSMT_SHA2_40_2_256, CRYPT_XMSSMT_SHA2_40_4_256 = BSL_CID_XMSSMT_SHA2_40_4_256, CRYPT_XMSSMT_SHA2_40_8_256 = BSL_CID_XMSSMT_SHA2_40_8_256, CRYPT_XMSSMT_SHA2_60_3_256 = BSL_CID_XMSSMT_SHA2_60_3_256, CRYPT_XMSSMT_SHA2_60_6_256 = BSL_CID_XMSSMT_SHA2_60_6_256, CRYPT_XMSSMT_SHA2_60_12_256 = BSL_CID_XMSSMT_SHA2_60_12_256, CRYPT_XMSSMT_SHA2_20_2_512 = BSL_CID_XMSSMT_SHA2_20_2_512, CRYPT_XMSSMT_SHA2_20_4_512 = BSL_CID_XMSSMT_SHA2_20_4_512, CRYPT_XMSSMT_SHA2_40_2_512 = BSL_CID_XMSSMT_SHA2_40_2_512, CRYPT_XMSSMT_SHA2_40_4_512 = BSL_CID_XMSSMT_SHA2_40_4_512, CRYPT_XMSSMT_SHA2_40_8_512 = BSL_CID_XMSSMT_SHA2_40_8_512, CRYPT_XMSSMT_SHA2_60_3_512 = BSL_CID_XMSSMT_SHA2_60_3_512, CRYPT_XMSSMT_SHA2_60_6_512 = BSL_CID_XMSSMT_SHA2_60_6_512, CRYPT_XMSSMT_SHA2_60_12_512 = BSL_CID_XMSSMT_SHA2_60_12_512, CRYPT_XMSSMT_SHAKE_20_2_256 = BSL_CID_XMSSMT_SHAKE_20_2_256, CRYPT_XMSSMT_SHAKE_20_4_256 = BSL_CID_XMSSMT_SHAKE_20_4_256, CRYPT_XMSSMT_SHAKE_40_2_256 = BSL_CID_XMSSMT_SHAKE_40_2_256, CRYPT_XMSSMT_SHAKE_40_4_256 = BSL_CID_XMSSMT_SHAKE_40_4_256, CRYPT_XMSSMT_SHAKE_40_8_256 = BSL_CID_XMSSMT_SHAKE_40_8_256, CRYPT_XMSSMT_SHAKE_60_3_256 = BSL_CID_XMSSMT_SHAKE_60_3_256, CRYPT_XMSSMT_SHAKE_60_6_256 = BSL_CID_XMSSMT_SHAKE_60_6_256, CRYPT_XMSSMT_SHAKE_60_12_256 = BSL_CID_XMSSMT_SHAKE_60_12_256, CRYPT_XMSSMT_SHAKE_20_2_512 = BSL_CID_XMSSMT_SHAKE_20_2_512, CRYPT_XMSSMT_SHAKE_20_4_512 = BSL_CID_XMSSMT_SHAKE_20_4_512, CRYPT_XMSSMT_SHAKE_40_2_512 = BSL_CID_XMSSMT_SHAKE_40_2_512, CRYPT_XMSSMT_SHAKE_40_4_512 = BSL_CID_XMSSMT_SHAKE_40_4_512, CRYPT_XMSSMT_SHAKE_40_8_512 = BSL_CID_XMSSMT_SHAKE_40_8_512, CRYPT_XMSSMT_SHAKE_60_3_512 = BSL_CID_XMSSMT_SHAKE_60_3_512, CRYPT_XMSSMT_SHAKE_60_6_512 = BSL_CID_XMSSMT_SHAKE_60_6_512, CRYPT_XMSSMT_SHAKE_60_12_512 = BSL_CID_XMSSMT_SHAKE_60_12_512, CRYPT_XMSSMT_SHA2_20_2_192 = BSL_CID_XMSSMT_SHA2_20_2_192, CRYPT_XMSSMT_SHA2_20_4_192 = BSL_CID_XMSSMT_SHA2_20_4_192, CRYPT_XMSSMT_SHA2_40_2_192 = BSL_CID_XMSSMT_SHA2_40_2_192, CRYPT_XMSSMT_SHA2_40_4_192 = BSL_CID_XMSSMT_SHA2_40_4_192, CRYPT_XMSSMT_SHA2_40_8_192 = BSL_CID_XMSSMT_SHA2_40_8_192, CRYPT_XMSSMT_SHA2_60_3_192 = BSL_CID_XMSSMT_SHA2_60_3_192, CRYPT_XMSSMT_SHA2_60_6_192 = BSL_CID_XMSSMT_SHA2_60_6_192, CRYPT_XMSSMT_SHA2_60_12_192 = BSL_CID_XMSSMT_SHA2_60_12_192, CRYPT_XMSSMT_SHAKE256_20_2_256 = BSL_CID_XMSSMT_SHAKE256_20_2_256, CRYPT_XMSSMT_SHAKE256_20_4_256 = BSL_CID_XMSSMT_SHAKE256_20_4_256, CRYPT_XMSSMT_SHAKE256_40_2_256 = BSL_CID_XMSSMT_SHAKE256_40_2_256, CRYPT_XMSSMT_SHAKE256_40_4_256 = BSL_CID_XMSSMT_SHAKE256_40_4_256, CRYPT_XMSSMT_SHAKE256_40_8_256 = BSL_CID_XMSSMT_SHAKE256_40_8_256, CRYPT_XMSSMT_SHAKE256_60_3_256 = BSL_CID_XMSSMT_SHAKE256_60_3_256, CRYPT_XMSSMT_SHAKE256_60_6_256 = BSL_CID_XMSSMT_SHAKE256_60_6_256, CRYPT_XMSSMT_SHAKE256_60_12_256 = BSL_CID_XMSSMT_SHAKE256_60_12_256, CRYPT_XMSSMT_SHAKE256_20_2_192 = BSL_CID_XMSSMT_SHAKE256_20_2_192, CRYPT_XMSSMT_SHAKE256_20_4_192 = BSL_CID_XMSSMT_SHAKE256_20_4_192, CRYPT_XMSSMT_SHAKE256_40_2_192 = BSL_CID_XMSSMT_SHAKE256_40_2_192, CRYPT_XMSSMT_SHAKE256_40_4_192 = BSL_CID_XMSSMT_SHAKE256_40_4_192, CRYPT_XMSSMT_SHAKE256_40_8_192 = BSL_CID_XMSSMT_SHAKE256_40_8_192, CRYPT_XMSSMT_SHAKE256_60_3_192 = BSL_CID_XMSSMT_SHAKE256_60_3_192, CRYPT_XMSSMT_SHAKE256_60_6_192 = BSL_CID_XMSSMT_SHAKE256_60_6_192, CRYPT_XMSSMT_SHAKE256_60_12_192 = BSL_CID_XMSSMT_SHAKE256_60_12_192, CRYPT_PKEY_PARAID_MAX = BSL_CID_UNKNOWN } CRYPT_PKEY_ParaId; /** * @ingroup crypt_algid * * Elliptic Curve Point Encoding Format */ typedef enum { CRYPT_POINT_COMPRESSED, CRYPT_POINT_UNCOMPRESSED, /**< default format. */ CRYPT_POINT_HYBRID, CRYPT_POINT_MAX } CRYPT_PKEY_PointFormat; /** * @ingroup crypt_algid * * KDF algorithm ID */ typedef enum { CRYPT_KDF_SCRYPT = BSL_CID_SCRYPT, CRYPT_KDF_PBKDF2 = BSL_CID_PBKDF2, CRYPT_KDF_KDFTLS12 = BSL_CID_KDFTLS12, CRYPT_KDF_HKDF = BSL_CID_HKDF, CRYPT_KDF_MAX = BSL_CID_UNKNOWN } CRYPT_KDF_AlgId; #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_ALGID_H
2301_79861745/bench_create
include/crypto/crypt_algid.h
C
unknown
14,909
/* * 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_cipher * @ingroup crypt * @brief cipher suites */ #ifndef CRYPT_EAL_CIPHER_H #define CRYPT_EAL_CIPHER_H #include <stdint.h> #include <stdbool.h> #include "crypt_algid.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct CryptEalCipherCtx CRYPT_EAL_CipherCtx; /** * @ingroup crypt_eal_cipher, Not supported in provider * @brief Check whether the given symmetric algorithm ID is valid. * * @param id [IN] Symmetric algorithm ID. * @retval Valid, true is returned. * Invalid, false is returned. */ bool CRYPT_EAL_CipherIsValidAlgId(CRYPT_CIPHER_AlgId id); /** * @ingroup crypt_eal_cipher * @brief Generate symmetric encryption and decryption handles. * * @attention If the function is called by an external user and the error stack is concerned, * it is recommended that BSL_ERR_ClearError() be called before this function is called. * @param id [IN] Symmetric encryption/decryption algorithm ID. * @retval Success: cipher ctx. * Fails: NULL. */ CRYPT_EAL_CipherCtx *CRYPT_EAL_CipherNewCtx(CRYPT_CIPHER_AlgId id); /** * @ingroup crypt_eal_cipher * @brief Generate symmetric encryption and decryption handles in the providers * * @param libCtx [IN] Library context * @param algId [IN] Symmetric encryption/decryption algorithm ID. * @param attrName [IN] Specify expected attribute values * @retval Success: cipher ctx. * Fails: NULL. */ CRYPT_EAL_CipherCtx *CRYPT_EAL_ProviderCipherNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t algId, const char *attrName); /** * @ingroup crypt_eal_cipher * @brief Release the symmetric encryption/decryption handle. Clear sensitive information before releasing the handle. * * @attention If the function is called by an external user and the error stack is concerned, it is recommended * that BSL_ERR_ClearError() be called before this function is called. * @param ctx [IN] Symmetric encryption/decryption handle. The CTX is set null by the caller. */ void CRYPT_EAL_CipherFreeCtx(CRYPT_EAL_CipherCtx *ctx); /** * @ingroup crypt_eal_cipher * @brief Initialize the symmetric encryption/decryption handle. The key cannot be null. Except the ECB mode, * other modes iv cannot be null. * * The length of iv must be the same as the block length (this requirement is not required in ECB mode). * The block length can be obtained through CRYPT_CTRL_GET_BLOCKSIZE of CRYPT_EAL_CipherCtrl. * CRYPT_EAL_CipherInit can be called repeatedly at any stage, resets the key and iv, and clears the cached data. * * @attention If the function is called by an external user and the error stack is concerned, * you are advised to call BSL_ERR_ClearError() before calling this function. * * @param ctx [IN] Symmetric encryption/decryption handle * @param key [IN] Key * @param keyLen [IN] Key length * @param iv [IN] Initialization vector * @param ivLen [IN] Initialize the vector length. * @param enc [IN] True: encryption; False: decryption * @retval #CRYPT_SUCCESS, success. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_CipherInit(CRYPT_EAL_CipherCtx *ctx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); /** * @ingroup crypt_eal_cipher * @brief Deinitialize the handle and restore the handle to the state, * when the CRYPT_EAL_CipherNewCtx function is called. * * @attention If the function is called by an external user and the error stack is concerned, * you are advised to call BSL_ERR_ClearError() before calling this function. * @param ctx [IN] Symmetric encryption/decryption handle */ void CRYPT_EAL_CipherDeinit(CRYPT_EAL_CipherCtx *ctx); /** * @ingroup crypt_eal_cipher * * Re-initialize the handle, retain the key, reset the IV, and clear the cache and sensitive data. * Except the ECB mode, other modes iv cannot be null. The setting of iv must be based on the corresponding * algorithm ID. For details, see the mapping in CRYPT_EAL_CipherInit. * * @param ctx [IN] Symmetric encryption/decryption handle * @param iv [IN] Vector * @param ivlen [IN] Vector length */ int32_t CRYPT_EAL_CipherReinit(CRYPT_EAL_CipherCtx *ctx, uint8_t *iv, uint32_t ivLen); /** * @ingroup crypt_eal_cipher * * Continuously enter encrypted and decrypted data. * CRYPT_EAL_CipherUpdate should be used in conjunction with CRYPT_EAL_CipherFinal, after one or more calls to * CRYPT_EAL_CipherUpdate, Call CRYPT_EAL_CipherFinal. With the exception of SM4_XTS mode, multiple calls to * CRYPT_EAL_CipherUpdate and CRYPT_EAL_CipherFinal are not supported. * * @attention If the function is called by an external user and the error stack is concerned, it is recommended * that BSL_ERR_ClearError() be called before this function is called. * * @param ctx [IN] Symmetric encryption and decryption handle * @param in [IN] Continuously input data * @param inLen [IN] Length of continuously input data * @param out [OUT] Output data * @param outLen [IN/OUT] Input: For CBC and ECB block encryption, you are advised to set outLen > inLen + blockSize. * For CTR and XTS stream encryption, you are advised to set outLen >= inLen. blockSize can be obtained by using * CRYPT_CTRL_GET_BLOCKSIZE of CRYPT_EAL_CipherCtrl. * Output: Length of the encrypted data. If the block encryption algorithm is used and the length of the last data * to be processed is insufficient, the output value of outLen is 0. * eg: CBC and ECB block encryption * 1. Encrypted data is input for the first time, and inLen is less than blockSize. * In this case, the output value of outLen is 0. * 2. In the first input encrypted data length, inLen is an integer multiple of blockSize. * In this case, outLen is equal to inLen. * 3. In the first input encrypted data length, inLen > blockSize and not an integer multiple of blockSize. * In this case, outLen < inLen. * 4. Enter the encrypted data for multiple times. (inLen% blockSize) + cache (CTX cache data) >= blockSize. * At this point outLen = (inlen / blockSize) * blockSize + blockSize * CTR outLen equals inLen. * In XTS mode, update reserves the last two blocks for final processing, If the total length of the input data * plus the buffer is less than 32 blocks, the output is 0. * 1. When data is input for the first time, outLen = (inLen / 16 - 2) * 16. * 2. Enter the encrypted data for multiple times. At this time, outLen = ((inLen + cache) / 16 - 2) * 16. * In SM4_XTS mode, after calling CRYPT_EAL_CipherUpdate, you need to use CRYPT_EAL_CipherInit or * CRYPT_EAL_CipherReinit to reset the key or iv. * @retval #CRYPT_SUCCESS, success. * Other error codes see the crypt_errno.h. */ int32_t CRYPT_EAL_CipherUpdate(CRYPT_EAL_CipherCtx *ctx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_cipher * @brief Fill the data with the size of the block and output the encrypted data; the AEAD tag is obtained * through CRYPT_EAL_CipherCtrl. * For block encryption algorithms such as CBC and ECB, padding must be set, In XTS mode, final needs * to be called to obtain the last two blocks. * @attention If the function is called by an external user and the error stack is concerned, * you are advised to call BSL_ERR_ClearError() before calling this function. * * @param ctx [IN] Symmetric encryption/decryption handle * @param out [OUT] Output the encrypted data * @param outLen [IN/OUT] Input: outLen >= blockSize * Output: The output value for stream encryption is 0. * If padding is set for CBC and ECB block encryption, the output value of outLen is blockSize. * If the padding is not set for CBC and ECB block encryption and CTX contains cached data, an error is reported. * If padding is not set for CBC and ECB block encryption, and no data is cached in the CTX, the output value of * outLen is 0. * @retval #CRYPT_SUCCESS, success. * Other error codes see the crypt_errno.h. */ int32_t CRYPT_EAL_CipherFinal(CRYPT_EAL_CipherCtx *ctx, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_cipher * * Set the mode ctx parameters in the CTX. * parameter data type Length(len):number of data bytes * CRYPT_CTRL_GET_IV uint8_t array The length of the IV depends on the corresponding algorithm, see the mapping in CRYPT_EAL_CipherInit * CRYPT_CTRL_SET_AAD uint8_t array It is used only for AEAD calculation. The length is related to the corresponding AEAD algorithm. * CRYPT_CTRL_GET_TAG uint8_t array It is used only for AEAD calculation. The length is the tagLen value set by the user. * CRYPT_CTRL_SET_TAGLEN uint32_t length(len) 4 * CRYPT_CTRL_SET_MSGLEN uint64_t length(len) 8 * CRYPT_CTRL_SET_FEEDBACKSIZE uint32_t length(len) 4 * CRYPT_CTRL_GET_FEEDBACKSIZE uint32_t pointer sizeof(*uint32_t) * CRYPT_CTRL_GET_BLOCKSIZE uint32_t length(len) 4 * * @attention If the function is called by an external user and the error stack is concerned, * it is recommended that BSL_ERR_ClearError() be called before this function is called. * @param ctx [IN] Symmetric encryption/decryption handle * @param type [IN] Parameter type * @param data [IN/OUT] Input and output data * @param len [OUT] Data length * @retval #CRYPT_SUCCESS, success. * error codes see the crypt_errno.h */ int32_t CRYPT_EAL_CipherCtrl(CRYPT_EAL_CipherCtx *ctx, int32_t type, void *data, uint32_t len); /** * @ingroup crypt_eal_cipher * @brief Set the padding mode. * * @param ctx Symmetric encryption/decryption handle * @param type Padding type * @retval #CRYPT_SUCCESS, success. * Error codes see crypt_errno.h */ int32_t CRYPT_EAL_CipherSetPadding(CRYPT_EAL_CipherCtx *ctx, CRYPT_PaddingType type); /** * @ingroup crypt_eal_cipher * @brief Obtain the padding type. * * @param ctx Symmetric encryption/decryption handle * @retval Return mode */ int32_t CRYPT_EAL_CipherGetPadding(CRYPT_EAL_CipherCtx *ctx); /** * @ingroup crypt_eal_cipher * @brief Obtain the type of an algorithm based on the algorithm ID. * * @param id [IN] Symmetric algorithm ID. * @param type [IN] Attribute type to be obtained. * @param infoValue [OUT] Obtained attribute data. * @retval CRYPT_SUCCESS, success * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_CipherGetInfo(CRYPT_CIPHER_AlgId id, int32_t type, uint32_t *infoValue); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_CIPHER_H
2301_79861745/bench_create
include/crypto/crypt_eal_cipher.h
C
unknown
11,326
/* * 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_cmvp * @ingroup crypt * @brief EAL CMVP header */ #ifndef CRYPT_EAL_CMVP_H #define CRYPT_EAL_CMVP_H #include <stdint.h> #include "bsl_params.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus /** * @ingroup crypt_eal_cmvp * @brief The type of the self-test. */ typedef enum { CRYPT_CMVP_INTEGRITY_TEST = 0, /**< Integrity test */ CRYPT_CMVP_KAT_TEST, /**< Known Answer Test */ CRYPT_CMVP_RANDOMNESS_TEST, /**< Randomness test */ CRYPT_CMVP_MAX } CRYPT_CMVP_SELFTEST_TYPE; typedef enum { /* Set the self test flag of drbg, 0: disable, 1: enable */ CRYPT_CTRL_SET_SELFTEST_FLAG = CRYPT_CTRL_RAND_MAX + 1, } CRYPT_CMVP_RandCtrl; /** * @ingroup crypt_eal_cmvp * @brief Log function for provider. * * @param oper [IN] The operation type. * @param type [IN] The algorithm type. * @param id [IN] The algorithm ID. * @param err [IN] The error code. */ typedef void (*CRYPT_EAL_CMVP_LogFunc)(CRYPT_EVENT_TYPE oper, CRYPT_ALGO_TYPE type, int32_t id, int32_t err); typedef struct EAL_SelftestCtx CRYPT_SelftestCtx; /** * @ingroup crypt_eal_cmvp * @brief Create a new context for self-test. * * @param libCtx [IN] The library context, if NULL, return NULL. * @param attrName [IN] Specify expected attribute values. * * @retval #CRYPT_SelftestCtx pointer. * NULL, if the operation fails. */ CRYPT_SelftestCtx *CRYPT_CMVP_SelftestNewCtx(CRYPT_EAL_LibCtx *libCtx, const char *attrName); /** * @ingroup crypt_eal_cmvp * @brief Get the version of the provider. * * @param ctx [IN] The self-test context. * * @retval The string pointer of the version of the provider. * NULL, if the operation fails. */ const char *CRYPT_CMVP_GetVersion(CRYPT_SelftestCtx *ctx); /** * @ingroup crypt_eal_cmvp * @brief Run the self-test. * * @param ctx [IN] The self-test context. * @param param [IN] This is a self-check parameter and must contain CRYPT_CMVP_SELFTEST_TYPE. * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_CMVP_Selftest(CRYPT_SelftestCtx *ctx, const BSL_Param *param); /** * @ingroup crypt_eal_cmvp * @brief Free the context of the self-test. * * @param ctx [IN] The self-test context. */ void CRYPT_CMVP_SelftestFreeCtx(CRYPT_SelftestCtx *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_CMVP_H
2301_79861745/bench_create
include/crypto/crypt_eal_cmvp.h
C
unknown
2,995
/* * 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_encode * @ingroup crypt * @brief pubkey encode/decode of crypto module */ #ifndef CRYPT_EAL_ENCODE_H #define CRYPT_EAL_ENCODE_H #include <stdint.h> #include "bsl_params.h" #include "bsl_types.h" #include "bsl_list.h" #include "crypt_eal_pkey.h" #include "crypt_eal_provider.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct CRYPT_DecoderCtx CRYPT_DECODER_Ctx; /** * @brief Create a decoder context for the specified format and type * * @param libCtx EAL library context * @param keyType Decoding target type (e.g., CRYPT_ALG_ID_RSA, CRYPT_ALG_ID_EC) * @param attrName Attribute name for specific type decoding (can be NULL) * @return CRYPT_DECODER_Ctx* Decoder context, returns NULL on failure */ CRYPT_DECODER_Ctx *CRYPT_DECODE_ProviderNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t keyType, const char *attrName); /** * @brief Free the decoder context * * @param ctx Decoder context */ void CRYPT_DECODE_Free(CRYPT_DECODER_Ctx *ctx); /** * @brief Set decoder parameters * * @param ctx Decoder context * @param param Parameter * @return int32_t CRYPT_SUCCESS on success, error code on failure */ int32_t CRYPT_DECODE_SetParam(CRYPT_DECODER_Ctx *ctx, const BSL_Param *param); /** * @brief Get decoder parameters * * @param ctx Decoder context * @param param Parameter (output) * @return int32_t CRYPT_SUCCESS on success, error code on failure */ int32_t CRYPT_DECODE_GetParam(CRYPT_DECODER_Ctx *ctx, BSL_Param *param); /** * @brief Perform decoding operation * * @param ctx Decoder context * @param input Input data * @param inParam Input parameter * @param out Output object to store decoding results * @return int32_t CRYPT_SUCCESS on success, error code on failure */ int32_t CRYPT_DECODE_Decode(CRYPT_DECODER_Ctx *ctx, const BSL_Param *inParam, BSL_Param **outParam); /** * @brief Free the output data * * @param ctx Decoder context * @param data Output data */ void CRYPT_DECODE_FreeOutData(CRYPT_DECODER_Ctx *ctx, BSL_Param *outData); typedef struct CRYPT_DECODER_PoolCtx CRYPT_DECODER_PoolCtx; /** * @brief Command codes for CRYPT_DECODE_PoolCtrl function */ typedef enum { /** Set the target format */ CRYPT_DECODE_POOL_CMD_SET_TARGET_FORMAT, /** Set the target type */ CRYPT_DECODE_POOL_CMD_SET_TARGET_TYPE, /** Set the not free out data */ CRYPT_DECODE_POOL_CMD_SET_FLAG_FREE_OUT_DATA, } CRYPT_DECODE_POOL_CMD; /** * @brief Create a decoder pool context * * @param libCtx EAL library context * @param attrName Provider attribute name, can be NULL * @param format Input data format (e.g., BSL_FORMAT_PEM, BSL_FORMAT_DER) * @param type Decoding target type (e.g., CRYPT_ALG_ID_RSA, CRYPT_ALG_ID_EC) * @return CRYPT_DECODER_PoolCtx* Decoder pool context on success, NULL on failure */ CRYPT_DECODER_PoolCtx *CRYPT_DECODE_PoolNewCtx(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType, const char *format, const char *type); /** * @brief Free a decoder pool context * * @param poolCtx Decoder pool context */ void CRYPT_DECODE_PoolFreeCtx(CRYPT_DECODER_PoolCtx *poolCtx); /** * @brief Decode the input data with the decoder chain * * @param poolCtx Decoder pool context * @param inParam Input data * @param outParam Output Data * @return int32_t CRYPT_SUCCESS on success, error code on failure */ int32_t CRYPT_DECODE_PoolDecode(CRYPT_DECODER_PoolCtx *poolCtx, const BSL_Param *inParam, BSL_Param **outParam); /** * @brief Control operation for decoder pool * * @param poolCtx Decoder pool context * @param cmd Control command * @param val The value of the control command * @param valLen The length of the value * @return int32_t CRYPT_SUCCESS on success, error code on failure */ int32_t CRYPT_DECODE_PoolCtrl(CRYPT_DECODER_PoolCtx *poolCtx, int32_t cmd, void *val, int32_t valLen); /** * @ingroup crypt_eal_encode * @brief Decode formatted buffer of pkey * * @param format [IN] the buffer format. * @param type [IN] the type of pkey. * @param encode [IN] the encoded asn1 buffer. * @param pwd [IN] the password, maybe NULL for unencrypted private key / public key. * @param pwdlen [IN] the length of password. * @param ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the ans1 buffer. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_DecodeBuffKey(int32_t format, int32_t type, BSL_Buffer *encode, const uint8_t *pwd, uint32_t pwdlen, CRYPT_EAL_PkeyCtx **ealPKey); /** * @ingroup crypt_eal_encode * @brief Decode formatted buffer of pkey with provider * * @param libCtx [IN] the library context of provider. * @param attrName [IN] provider attribute name, maybe NULL. * @param keyType [IN] the type of pkey. * @param format [IN] the buffer format. * @param type [IN] the type of pkey. * @param encode [IN] the encoded asn1 buffer. * @param pwd [IN] the password buffer, maybe NULL for unencrypted private key / public key. * @param ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the ans1 buffer. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderDecodeBuffKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType, const char *format, const char *type, BSL_Buffer *encode, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey); /** * @ingroup crypt_eal_encode * @brief Decode formatted file of pkey * * @param format [IN] the file format. * @param type [IN] the type of pkey. * @param path [IN] the encoded file path. * @param pwd [IN] the password, maybe NULL for unencrypted private key / public key. * @param pwdlen [IN] the length of password. * @param ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the path. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_DecodeFileKey(int32_t format, int32_t type, const char *path, uint8_t *pwd, uint32_t pwdlen, CRYPT_EAL_PkeyCtx **ealPKey); /** * @ingroup crypt_eal_encode * @brief Decode formatted file of pkey with extended parameters * * @param libCtx [IN] the library context of provider. * @param attrName [IN] provider attribute name, maybe NULL. * @param format [IN] the file format. * @param type [IN] the type of pkey. * @param path [IN] the encoded file path. * @param pwd [IN] the password buffer, maybe NULL for unencrypted private key / public key. * @param ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the path. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderDecodeFileKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType, const char *format, const char *type, const char *path, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey); /** * @ingroup crypt_eal_encode * @brief Encode formatted buffer of pkey * * @param ealPKey [IN] CRYPT_EAL_PkeyCtx to encode. * @param encodeParam [IN] pkcs8 encode params. * @param format [IN] the buffer format. * @param type [IN] the type of pkey. * @param encode [OUT] the encoded asn1 buffer. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_EncodeBuffKey(CRYPT_EAL_PkeyCtx *ealPKey, const CRYPT_EncodeParam *encodeParam, int32_t format, int32_t type, BSL_Buffer *encode); /** * @ingroup crypt_eal_encode * @brief Encode formatted buffer of pkey with provider * * @param libCtx [IN] the library context of provider. * @param attrName [IN] provider attribute name, maybe NULL. * @param ealPKey [IN] CRYPT_EAL_PkeyCtx to encode. * @param encodeParam [IN] pkcs8 encode params. * @param format [IN] the buffer format. * @param type [IN] the type of pkey. * @param encode [OUT] the encoded asn1 buffer. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderEncodeBuffKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, CRYPT_EAL_PkeyCtx *ealPKey, const CRYPT_EncodeParam *encodeParam, const char *format, const char *type, BSL_Buffer *encode); /** * @ingroup crypt_eal_encode * @brief Encode formatted file of pkey * * @param ealPKey [IN] CRYPT_EAL_PkeyCtx to encode. * @param encodeParam [IN] pkcs8 encode params. * @param format [IN] the file format. * @param type [IN] the type of pkey. * @param path [IN] the encoded file path. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_EncodeFileKey(CRYPT_EAL_PkeyCtx *ealPKey, const CRYPT_EncodeParam *encodeParam, int32_t format, int32_t type, const char *path); /** * @ingroup crypt_eal_encode * @brief Encode formatted file of pkey with provider * * @param libCtx [IN] the library context of provider. * @param attrName [IN] provider attribute name, maybe NULL. * @param ealPKey [IN] CRYPT_EAL_PkeyCtx to encode. * @param encodeParam [IN] pkcs8 encode params. * @param format [IN] the file format. * @param type [IN] the type of pkey. * @param path [IN] the encoded file path. * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderEncodeFileKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, CRYPT_EAL_PkeyCtx *ealPKey, const CRYPT_EncodeParam *encodeParam, const char *format, const char *type, const char *path); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_ENCODE_H
2301_79861745/bench_create
include/crypto/crypt_eal_codecs.h
C
unknown
10,209
/* * 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_EAL_ENTROPY_H #define CRYPT_EAL_ENTROPY_H #include <stdbool.h> #include <stdint.h> #include "crypt_algid.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif typedef struct CryptEalEntropySource CRYPT_EAL_Es; /** * @ingroup crypt_eal_entropy * @brief The log function for entropy source to log noise source collection result. * * @param ret [IN] The return value of the noise source collection. */ typedef void (*CRYPT_EAL_EsLogFunc)(int32_t ret); /** * @ingroup crypt_eal_entropy * @brief Generate entropy source handle. * @attention If the function is called by an external user and the error stack is concerned, * it is recommended that BSL_ERR_ClearError() be called before this function is called. * * @return Success: entropy source ctx. * Fails: NULL. */ CRYPT_EAL_Es *CRYPT_EAL_EsNew(void); /** * @ingroup crypt_eal_entropy * @brief Release the entropy source handle. * @attention If the function is called by an external user and the error stack is concerned, it is recommended * that BSL_ERR_ClearError() be called before this function is called. * * @param es [IN] the entropy source handle. The CTX is set null by the caller. * @return None */ void CRYPT_EAL_EsFree(CRYPT_EAL_Es *es); /** * @ingroup crypt_eal_entropy * @brief Initialize the handle. * @attention If the function is called by an external user and the error stack is concerned, * you are advised to call BSL_ERR_ClearError() before calling this function. * * @param es [IN] the entropy source handle. * @return CRYPT_SUCCESS,success * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_EsInit(CRYPT_EAL_Es *es); /** * @ingroup crypt_eal_entropy * @brief Set the mode ctx parameters in the CTX. * parameter data type Length(len):number of data bytes * CRYPT_ENTROPY_SET_CF string Adjust the length of the function type name. For example, if the function type name is sm3-df, the length is 6. This interface can be invoked only once before the CRYPT_EAL_EsInit interface is invoked. * CRYPT_ENTROPY_SET_POOL_SIZE uint32_t Specifies the size of the entropy pool. The recommended value ranges from 512 to 4096. The default value is 4096. Can only be called before CRYPT_EAL_EsInit interface. * CRYPT_ENTROPY_ADD_NS CRYPT_EAL_NsPara Add a noise source.Repeated noise sources cannot be added. Whether a noise source is repeated is determined based on the name. The length is the size of the CRYPT_EAL_NsPara structure. Can only be called before CRYPT_EAL_EsInit interface. * CRYPT_ENTROPY_REMOVE_NS string Length of the entropy source name. Can only be called before CRYPT_EAL_EsInit interface. When an entropy source is created, two noise sources are carried by default, that is, timeStamp and CPU-Jitter. If the noise sources are not required, you can delete them by using this interface. * CRYPT_ENTROPY_ENABLE_TEST bool Sets whether to enable the health test, length is 1. Can only be called before CRYPT_EAL_EsInit interface. * CRYPT_ENTROPY_GET_STATE uint32_t Obtains the current entropy source status, length is 4. Can only be called after CRYPT_EAL_EsInit interface. * CRYPT_ENTROPY_GET_POOL_SIZE uint32_t Obtains the total entropy pool capacity, length is 4. Can only be called after CRYPT_EAL_EsInit interface. * CRYPT_ENTROPY_POOL_GET_CURRSIZE uint32_t Obtains the current entropy pool capacity, length is 4. Can only be called after CRYPT_EAL_EsInit interface. * CRYPT_ENTROPY_GET_CF_SIZE uint32_t Get the size of the conditioning function, length is 4. Can only be called after CRYPT_EAL_EsInit interface. * @attention If the function is called by an external user and the error stack is concerned, * it is recommended that BSL_ERR_ClearError() be called before this function is called. * @param es [IN] the entropy source handle * @param type [IN] Parameter type * @param data [IN/OUT] Input and output data * @param len [IN] Data length * @return Success response: CRYPT_SUCCESS * error codes see the crypt_errno.h */ int32_t CRYPT_EAL_EsCtrl(CRYPT_EAL_Es *es, int32_t type, void *data, uint32_t len); /** * @ingroup crypt_eal_entropy * @brief Get Entropy Output. * * @param es [IN] the entropy source handle. * @param data [OUT] Output data * @param len [IN] Data length * @return CRYPT_SUCCESS, success * Other error codes see crypt_errno.h */ uint32_t CRYPT_EAL_EsEntropyGet(CRYPT_EAL_Es *es, uint8_t *data, uint32_t len); typedef struct EAL_SeedPool CRYPT_EAL_SeedPoolCtx; /** * @ingroup crypt_eal_entropy * @brief Creating an seed pool. * * @param isCreateNullPool [IN] Whether the entropy pool provides a default entropy source. * @return success: seed pool ctx. * failed: NULL */ CRYPT_EAL_SeedPoolCtx *CRYPT_EAL_SeedPoolNew(bool isCreateNullPool); /** * @ingroup crypt_eal_entropy * @brief Adding an entropy source. * * @param ctx [IN] seed pool ctx. * @param para [IN] Entropy Source para. * @return Success: CRYPT_SUCCESS. * failed: Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_SeedPoolAddEs(CRYPT_EAL_SeedPoolCtx *ctx, const CRYPT_EAL_EsPara *para); /** * @ingroup crypt_eal_entropy * @brief get entropy data. * * @param ctx [IN] seed pool ctx. * @param entropy [OUT] obtained entropy data. * @param strength [IN] the amount of entropy required. * @param lenRange [IN] entropy data range. * @return null. */ int32_t CRYPT_EAL_SeedPoolGetEntropy(CRYPT_EAL_SeedPoolCtx *ctx, CRYPT_Data *entropy, uint32_t strength, const CRYPT_Range *lenRange); /** * @ingroup crypt_eal_entropy * @brief release entropy source. * * @param ctx [IN] seed pool ctx. * @return null. */ void CRYPT_EAL_SeedPoolFree(CRYPT_EAL_SeedPoolCtx *ctx); #ifdef __cplusplus } #endif #endif
2301_79861745/bench_create
include/crypto/crypt_eal_entropy.h
C
unknown
7,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. */ /** * @defgroup crypt_eal_hpke * @ingroup crypt * @brief hpke of crypto module */ #ifndef CRYPT_EAL_HPKE_H #define CRYPT_EAL_HPKE_H #include <stdint.h> #include "crypt_errno.h" #include "crypt_eal_pkey.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef enum { CRYPT_HPKE_MODE_BASE = 0x00, CRYPT_HPKE_MODE_PSK = 0x01, CRYPT_HPKE_MODE_AUTH = 0x02, CRYPT_HPKE_MODE_AUTH_PSK = 0x03 } CRYPT_HPKE_Mode; typedef enum { CRYPT_KEM_DHKEM_P256_HKDF_SHA256 = 0x0010, CRYPT_KEM_DHKEM_P384_HKDF_SHA384 = 0x0011, CRYPT_KEM_DHKEM_P521_HKDF_SHA512 = 0x0012, CRYPT_KEM_DHKEM_X25519_HKDF_SHA256 = 0x0020, } CRYPT_HPKE_KEM_AlgId; typedef enum { CRYPT_KDF_HKDF_SHA256 = 0x0001, CRYPT_KDF_HKDF_SHA384 = 0x0002, CRYPT_KDF_HKDF_SHA512 = 0x0003 } CRYPT_HPKE_KDF_AlgId; typedef enum { CRYPT_AEAD_AES_128_GCM = 0x0001, CRYPT_AEAD_AES_256_GCM = 0x0002, CRYPT_AEAD_CHACHA20_POLY1305 = 0x0003, CRYPT_AEAD_EXPORT_ONLY = 0xffff } CRYPT_HPKE_AEAD_AlgId; typedef struct { CRYPT_HPKE_KEM_AlgId kemId; CRYPT_HPKE_KDF_AlgId kdfId; CRYPT_HPKE_AEAD_AlgId aeadId; } CRYPT_HPKE_CipherSuite; typedef enum { CRYPT_HPKE_SENDER = 0, CRYPT_HPKE_RECIPIENT = 1, } CRYPT_HPKE_Role; typedef struct CRYPT_EAL_HpkeCtx CRYPT_EAL_HpkeCtx; /** * @ingroup crypt_eal_hpke * @brief Generate a key pair for HPKE using the specified cipher suite and input key material * * This function generates a key pair for HPKE using the provided cipher suite and input key material. * The generated key pair is returned in a CRYPT_EAL_PkeyCtx structure. * * @param libCtx [IN] The library context * @param attrName [IN] Specify expected attribute values * @param cipherSuite [IN] The HPKE cipher suite to be used for key generation * @param ikm [IN] The input key material for key generation * @param ikmLen [IN] The length of the input key material * @param pkey [OUT] A pointer to a pointer to the generated CRYPT_EAL_PkeyCtx structure * * @retval #CRYPT_SUCCESS if the key pair is generated successfully * Other error codes defined in crypt_errno.h if an error occurs */ int32_t CRYPT_EAL_HpkeGenerateKeyPair(CRYPT_EAL_LibCtx *libCtx, const char *attrName, CRYPT_HPKE_CipherSuite cipherSuite, uint8_t *ikm, uint32_t ikmLen, CRYPT_EAL_PkeyCtx **pkey); /** * @ingroup crypt_eal_hpke * @brief Create a new HPKE context * * @param libCtx [IN] Library context * @param attrName [IN] Specify expected attribute values * @param role [IN] HPKE role (sender or recipient) * @param mode [IN] HPKE mode * @param cipherSuite [IN] HPKE cipher suite containing KEM, KDF and AEAD algorithms * * @retval CRYPT_EAL_HpkeCtx pointer if successful, NULL if failed */ CRYPT_EAL_HpkeCtx *CRYPT_EAL_HpkeNewCtx(CRYPT_EAL_LibCtx *libCtx, const char *attrName, CRYPT_HPKE_Role role, CRYPT_HPKE_Mode mode, CRYPT_HPKE_CipherSuite cipherSuite); /** * @ingroup crypt_eal_hpke * @brief Get the length of the encapsulated key for the specified cipher suite * * @param cipherSuite [IN] HPKE cipher suite * @param encapKeyLen [OUT] Length of the encapsulated key * * @retval #CRYPT_SUCCESS if successful * Other error codes defined in crypt_errno.h if an error occurs */ int32_t CRYPT_EAL_HpkeGetEncapKeyLen(CRYPT_HPKE_CipherSuite cipherSuite, uint32_t *encapKeyLen); /** * @ingroup crypt_eal_hpke * @brief Setup HPKE base mode for sender * * This function only sets up the HPKE context for the sender in the base mode and psk mode. * It takes the sender's private key, the recipient's public key, and additional * information to generate an encapsulated key. * * @param ctx [IN] HPKE context for the sender * @param pkey [IN] Private key context for the sender, if set to NULL, will generate a keypair randomly * @param info [IN] Additional information for the key setup * @param infoLen [IN] Length of the additional information * @param pkR [IN] Recipient's public key. For ec key, the format is 04 || X || Y, for X25519 key, the format is X. * @param pkRLen [IN] Length of the recipient's public key * @param encapKey [OUT] Buffer to store the encapsulated key * @param encapKeyLen [IN/OUT] On input, the length of the buffer; on output, the length of the encapsulated key * * @retval #CRYPT_SUCCESS if the setup is successful * Other error codes defined in crypt_errno.h if an error occurs */ int32_t CRYPT_EAL_HpkeSetupSender(CRYPT_EAL_HpkeCtx *ctx, CRYPT_EAL_PkeyCtx *pkey, uint8_t *info, uint32_t infoLen, uint8_t *pkR, uint32_t pkRLen, uint8_t *encapKey, uint32_t *encapKeyLen); /** * @ingroup crypt_eal_hpke * @brief Seal (encrypt) data using HPKE context * * @param ctx [IN] HPKE context * @param aad [IN] Additional authenticated data * @param aadLen [IN] Length of additional authenticated data * @param plainText [IN] Plaintext to encrypt * @param plainTextLen [IN] Length of plaintext * @param cipherText [OUT] Ciphertext output buffer, if set to NULL, only return the ciphertext length * @param cipherTextLen [IN/OUT] On input, the length of the buffer; on output, the length of the ciphertext * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeSeal(CRYPT_EAL_HpkeCtx *ctx, uint8_t *aad, uint32_t aadLen, const uint8_t *plainText, uint32_t plainTextLen, uint8_t *cipherText, uint32_t *cipherTextLen); /** * @ingroup crypt_eal_hpke * @brief Setup HPKE for the recipient * * This function sets up the HPKE context for the recipient only in the base mode and psk mode. * It takes the recipient's private key, additional information, and the encapsulated key to generate the shared secret. * * @param ctx [IN] HPKE context for the recipient * @param pkey [IN] Private key context for the recipient * @param info [IN] Additional information for the key setup * @param infoLen [IN] Length of the additional information * @param encapKey [IN] Encapsulated key input buffer * @param encapKeyLen [IN] Length of the encapsulated key * * @retval #CRYPT_SUCCESS if the setup is successful * Other error codes defined in crypt_errno.h if an error occurs */ int32_t CRYPT_EAL_HpkeSetupRecipient(CRYPT_EAL_HpkeCtx *ctx, CRYPT_EAL_PkeyCtx *pkey, uint8_t *info, uint32_t infoLen, uint8_t *encapKey, uint32_t encapKeyLen); /** * @ingroup crypt_eal_hpke * @brief Open an HPKE-encrypted message * * @param ctx [IN] HPKE context for decryption * @param aad [IN] Additional authenticated data * @param aadLen [IN] Length of the additional authenticated data * @param cipherText [IN] The encrypted message to be decrypted * @param cipherTextLen [IN] Length of the encrypted message * @param plainText [OUT] Buffer to store the decrypted message * @param plainTextLen [IN/OUT] On input, the length of the buffer; on output, the length of the decrypted message * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeOpen(CRYPT_EAL_HpkeCtx *ctx, uint8_t *aad, uint32_t aadLen, const uint8_t *cipherText, uint32_t cipherTextLen, uint8_t *plainText, uint32_t *plainTextLen); /** * @ingroup crypt_eal_hpke * @brief Export a secret from the HPKE context * * @param ctx [IN] HPKE context * @param info [IN] Additional information for the export * @param infoLen [IN] Length of the additional information * @param key [OUT] Buffer to store the exported secret * @param keyLen [IN] Length of the buffer for the exported secret * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeExportSecret(CRYPT_EAL_HpkeCtx *ctx, uint8_t *info, uint32_t infoLen, uint8_t *key, uint32_t keyLen); /** * @ingroup crypt_eal_hpke * @brief Set the sequence number for the HPKE context * * @param ctx [IN] HPKE context * @param seq [IN] Sequence number to be set * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeSetSeq(CRYPT_EAL_HpkeCtx *ctx, uint64_t seq); /** * @ingroup crypt_eal_hpke * @brief Retrieve the sequence number from the HPKE context * * @param ctx [IN] HPKE context * @param seq [OUT] Buffer to store the retrieved sequence number * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeGetSeq(CRYPT_EAL_HpkeCtx *ctx, uint64_t *seq); /** * @ingroup crypt_eal_hpke * @brief Retrieve the shared secret from the HPKE context * * @param ctx [IN] HPKE context * @param buff [OUT] Buffer to store the shared secret * @param buffLen [IN/OUT] On input, the length of the buffer; on output, the length of the shared secret * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeGetSharedSecret(CRYPT_EAL_HpkeCtx *ctx, uint8_t *buff, uint32_t *buffLen); /** * @ingroup crypt_eal_hpke * @brief Set the shared secret in the HPKE context * * This function set the shared secret and generate the hpke key info. * * @param ctx [IN] HPKE context * @param info [IN] Additional information for the shared secret * @param infoLen [IN] Length of the additional information * @param buff [IN] Buffer containing the shared secret * @param buffLen [IN] Length of the shared secret * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeSetSharedSecret(CRYPT_EAL_HpkeCtx *ctx, uint8_t *info, uint32_t infoLen, uint8_t *buff, uint32_t buffLen); /** * @ingroup crypt_eal_hpke * @brief Free HPKE context and associated resources * * @param ctx [IN] HPKE context to free */ void CRYPT_EAL_HpkeFreeCtx(CRYPT_EAL_HpkeCtx *ctx); /** * @ingroup crypt_eal_hpke * @brief Setup psk and pskId for mode_psk and mode_auth_psk * * @param ctx [IN] HPKE context * @param psk [IN] Pre-shared key (PSK) used for the key exchange * @param pskLen [IN] Length of the pre-shared key (PSK) in bytes * @param pskId [IN] Identifier for the pre-shared key (PSK) * @param pskIdLen [IN] Length of the PSK identifier in bytes * * @retval #CRYPT_SUCCESS if the setup is successful * Other error codes defined in crypt_errno.h if an error occurs */ int32_t CRYPT_EAL_HpkeSetPsk(CRYPT_EAL_HpkeCtx *ctx,uint8_t* psk,uint32_t pskLen,uint8_t* pskId,uint32_t pskIdLen); /** * @ingroup crypt_eal_hpke * @brief Set the authentication private key in the HPKE context * * @param ctx [IN] HPKE context * @param pkey [IN] Private key context for authentication * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeSetAuthPriKey(CRYPT_EAL_HpkeCtx *ctx, CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_hpke * @brief Set the authentication public key in the HPKE context * * @param ctx [IN] HPKE context * @param pub [IN] Public key buffer * @param pubLen [IN] Length of the public key buffer * * @retval #CRYPT_SUCCESS if successful * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_HpkeSetAuthPubKey(CRYPT_EAL_HpkeCtx *ctx, uint8_t *pub, uint32_t pubLen); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_HPKE_H
2301_79861745/bench_create
include/crypto/crypt_eal_hpke.h
C
unknown
11,756
/* * 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 using during provider development */ #ifndef CRYPT_EAL_IMPLPROVIDER_H #define CRYPT_EAL_IMPLPROVIDER_H #include <stdint.h> #include "bsl_params.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define CRYPT_EAL_FUNC_END {0, NULL} #define CRYPT_EAL_ALGINFO_END {0, NULL, NULL} typedef struct { int32_t algId; // implemented algorithm id, such as aes128cbc, rsa sign const CRYPT_EAL_Func *implFunc; // implemented algorithm callback const char *attr; // implemented algorithm attribute } CRYPT_EAL_AlgInfo; typedef void (CRYPT_EAL_CvtVoid)(void); /* capFuncs */ #define CRYPT_EAL_CAP_GETENTROPY 1 // Callback definition CRYPT_EAL_GetEntropyCb, in crypt_types.h #define CRYPT_EAL_CAP_CLEANENTROPY 2 // Callback definition CRYPT_EAL_CleanEntropyCb #define CRYPT_EAL_CAP_GETNONCE 3 // Callback definition CRYPT_EAL_GetNonceCb #define CRYPT_EAL_CAP_CLEANNONCE 4 // Callback definition CRYPT_EAL_CleanNonceCb /* get information from mgrCtx, such as entropy source context */ #define CRYPT_EAL_CAP_MGRCTXCTRL 5 typedef int32_t (*CRYPT_EAL_ProvMgrCtrlCb)(void *mgrCtx, int32_t cmd, void *val, uint32_t valLen); typedef enum { CRYPT_EAL_MGR_GETSEEDCTX, CRYPT_EAL_MGR_GETLIBCTX, } CRYPT_EAL_PROVMGRCTRL; /* outFuncs */ #define CRYPT_EAL_PROVCB_FREE 1 #define CRYPT_EAL_PROVCB_QUERY 2 #define CRYPT_EAL_PROVCB_CTRL 3 #define CRYPT_EAL_PROVCB_GETCAPS 4 typedef void (*CRYPT_EAL_ProvFreeCb)(void *provCtx); #define CRYPT_EAL_OPERAID_SYMMCIPHER 1 #define CRYPT_EAL_OPERAID_KEYMGMT 2 #define CRYPT_EAL_OPERAID_SIGN 3 #define CRYPT_EAL_OPERAID_ASYMCIPHER 4 #define CRYPT_EAL_OPERAID_KEYEXCH 5 #define CRYPT_EAL_OPERAID_KEM 6 #define CRYPT_EAL_OPERAID_HASH 7 #define CRYPT_EAL_OPERAID_MAC 8 #define CRYPT_EAL_OPERAID_KDF 9 #define CRYPT_EAL_OPERAID_RAND 10 #define CRYPT_EAL_OPERAID_DECODER 11 #define CRYPT_EAL_OPERAID_SELFTEST 12 typedef int32_t (*CRYPT_EAL_ProvQueryCb)(void *provCtx, int32_t operaId, CRYPT_EAL_AlgInfo **algInfos); /* Used for obtaining provider information through the eal layer interface */ typedef int32_t (*CRYPT_EAL_ProvCtrlCb)(void *provCtx, int32_t cmd, void *val, uint32_t valLen); #define CRYPT_EAL_GET_GROUP_CAP 1 #define CRYPT_EAL_GET_SIGALG_CAP 2 /** * @brief Callback function type for processing provider capabilities * * @param params [IN] Parameters containing capability information * @param args [IN] User-provided arguments for capability processing * * @retval #CRYPT_SUCCESS if processing succeeds * Other error codes see the crypt_errno.h */ typedef int32_t (*CRYPT_EAL_ProcessFuncCb)(const BSL_Param *params, void *args); /* Used for obtaining the capabilities of provider through the eal layer interface */ typedef int32_t (*CRYPT_EAL_ProvGetCapsCb)(void *provCtx, int32_t cmd, CRYPT_EAL_ProcessFuncCb cb, void *args); /** * @ingroup crypt_eal_provider * @brief Provider initialization function prototype, * * @param mgrCtx [IN] framework generates context for each provider, for provider, it is opaque * @param param [IN] parameters passed transparently when loading the provider interface * @param capFuncs [IN] The ability passed from the framework to provider, such as entropy source * @param outFuncs [OUT] The callback returned by provider, such as entropy source * @param provCtx [OUT] provider context * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ typedef int32_t (*CRYPT_EAL_ImplProviderInit)(CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, CRYPT_EAL_Func *capFuncs, CRYPT_EAL_Func **outFuncs, void **provCtx); // CRYPT_EAL_OPERAID_SYMMCIPHER #define CRYPT_EAL_IMPLCIPHER_NEWCTX 1 #define CRYPT_EAL_IMPLCIPHER_INITCTX 2 #define CRYPT_EAL_IMPLCIPHER_UPDATE 3 #define CRYPT_EAL_IMPLCIPHER_FINAL 4 #define CRYPT_EAL_IMPLCIPHER_DEINITCTX 5 #define CRYPT_EAL_IMPLCIPHER_CTRL 6 #define CRYPT_EAL_IMPLCIPHER_FREECTX 7 typedef void *(*CRYPT_EAL_ImplCipherNewCtx)(void *provCtx, int32_t algId); typedef int32_t (*CRYPT_EAL_ImplCipherInitCtx)(void *ctx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, BSL_Param *param, bool enc); typedef int32_t (*CRYPT_EAL_ImplCipherUpdate)(void *ctx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplCipherFinal)(void *ctx, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplCipherDeinitCtx)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplCipherCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen); typedef void (*CRYPT_EAL_ImplCipherFreeCtx)(void *ctx); // CRYPT_EAL_OPERAID_KEYMGMT #define CRYPT_EAL_IMPLPKEYMGMT_NEWCTX 1 #define CRYPT_EAL_IMPLPKEYMGMT_SETPARAM 2 #define CRYPT_EAL_IMPLPKEYMGMT_GETPARAM 3 #define CRYPT_EAL_IMPLPKEYMGMT_GENKEY 4 #define CRYPT_EAL_IMPLPKEYMGMT_SETPRV 5 #define CRYPT_EAL_IMPLPKEYMGMT_SETPUB 6 #define CRYPT_EAL_IMPLPKEYMGMT_GETPRV 7 #define CRYPT_EAL_IMPLPKEYMGMT_GETPUB 8 #define CRYPT_EAL_IMPLPKEYMGMT_DUPCTX 9 #define CRYPT_EAL_IMPLPKEYMGMT_CHECK 10 #define CRYPT_EAL_IMPLPKEYMGMT_COMPARE 11 #define CRYPT_EAL_IMPLPKEYMGMT_CTRL 12 #define CRYPT_EAL_IMPLPKEYMGMT_FREECTX 13 #define CRYPT_EAL_IMPLPKEYMGMT_IMPORT 14 #define CRYPT_EAL_IMPLPKEYMGMT_EXPORT 15 typedef void *(*CRYPT_EAL_ImplPkeyMgmtNewCtx)(void *provCtx, int32_t algId); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtSetParam)(void *ctx, const BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtGetParam)(void *ctx, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtGenKey)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtSetPrv)(void *ctx, const BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtSetPub)(void *ctx, const BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtGetPrv)(const void *ctx, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtGetPub)(const void *ctx, BSL_Param *param); typedef void *(*CRYPT_EAL_ImplPkeyMgmtDupCtx)(const void *ctx); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtCheck)(uint32_t checkType, const void *ctx1, const void *ctx2); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtCompare)(const void *ctx1, const void *ctx2); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen); typedef void (*CRYPT_EAL_ImplPkeyMgmtFreeCtx)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtImport)(void *ctx, const BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplPkeyMgmtExport)(const void *ctx, BSL_Param *param); // CRYPT_EAL_OPERAID_SIGN #define CRYPT_EAL_IMPLPKEYSIGN_SIGN 1 #define CRYPT_EAL_IMPLPKEYSIGN_SIGNDATA 2 #define CRYPT_EAL_IMPLPKEYSIGN_VERIFY 3 #define CRYPT_EAL_IMPLPKEYSIGN_VERIFYDATA 4 #define CRYPT_EAL_IMPLPKEYSIGN_RECOVER 5 #define CRYPT_EAL_IMPLPKEYSIGN_BLIND 6 #define CRYPT_EAL_IMPLPKEYSIGN_UNBLIND 7 typedef int32_t (*CRYPT_EAL_ImplPkeySign)(void *ctx, int32_t mdAlgId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); typedef int32_t (*CRYPT_EAL_ImplPkeySignData)(void *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); typedef int32_t (*CRYPT_EAL_ImplPkeyVerify)(const void *ctx, int32_t mdAlgId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t signLen); typedef int32_t (*CRYPT_EAL_ImplPkeyVerifyData)(const void *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t signLen); typedef int32_t (*CRYPT_EAL_ImplPkeyRecover)(const void *ctx, uint8_t *sign, uint32_t signLen, uint8_t *data, uint32_t *dataLen); typedef int32_t (*CRYPT_EAL_ImplPkeyBlind)(void *ctx, int32_t mdAlgId, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplPkeyUnBlind)(const void *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); // CRYPT_EAL_OPERAID_ASYMCIPHER #define CRYPT_EAL_IMPLPKEYCIPHER_ENCRYPT 1 #define CRYPT_EAL_IMPLPKEYCIPHER_DECRYPT 2 #define CRYPT_EAL_IMPLPKEYCIPHER_HEADD 3 #define CRYPT_EAL_IMPLPKEYCIPHER_HEMUL 4 typedef int32_t (*CRYPT_EAL_ImplPkeyEncrypt)(void *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplPkeyDecrypt)(void *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplPkeyHEAdd)(const void *ctx, const BSL_Param *input, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplPkeyHEMul)(const void *ctx, const BSL_Param *input, uint8_t *out, uint32_t *outLen); // CRYPT_EAL_OPERAID_KEYEXCH #define CRYPT_EAL_IMPLPKEYEXCH_EXCH 1 typedef int32_t (*CRYPT_EAL_ImplPkeyExch)(const void *ctx, const void *pubCtx, uint8_t *out, uint32_t *outLen); // CRYPT_EAL_OPERAID_KEM #define CRYPT_EAL_IMPLPKEYKEM_ENCAPSULATE_INIT 1 #define CRYPT_EAL_IMPLPKEYKEM_DECAPSULATE_INIT 2 #define CRYPT_EAL_IMPLPKEYKEM_ENCAPSULATE 3 #define CRYPT_EAL_IMPLPKEYKEM_DECAPSULATE 4 typedef int32_t (*CRYPT_EAL_ImplPkeyEncapsInit)(const void *ctx, BSL_Param *params); typedef int32_t (*CRYPT_EAL_ImplPkeyDecapsInit)(const void *ctx, BSL_Param *params); typedef int32_t (*CRYPT_EAL_ImplPkeyKemEncapsulate)(const void *ctx, uint8_t *cipher, uint32_t *cipherLen, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplPkeyKemDecapsulate)(const void *ctx, uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); // CRYPT_EAL_OPERAID_HASH #define CRYPT_EAL_IMPLMD_NEWCTX 1 #define CRYPT_EAL_IMPLMD_INITCTX 2 #define CRYPT_EAL_IMPLMD_UPDATE 3 #define CRYPT_EAL_IMPLMD_FINAL 4 #define CRYPT_EAL_IMPLMD_DEINITCTX 5 #define CRYPT_EAL_IMPLMD_DUPCTX 6 #define CRYPT_EAL_IMPLMD_CTRL 7 // not support #define CRYPT_EAL_IMPLMD_FREECTX 8 #define CRYPT_EAL_IMPLMD_SQUEEZE 9 #define CRYPT_EAL_IMPLMD_COPYCTX 10 #define CRYPT_EAL_IMPLMD_GETPARAM 11 typedef void *(*CRYPT_EAL_ImplMdNewCtx)(void *provCtx, int32_t algId); typedef int32_t (*CRYPT_EAL_ImplMdInitCtx)(void *ctx, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplMdUpdate)(void *ctx, const uint8_t *input, uint32_t len); typedef int32_t (*CRYPT_EAL_ImplMdFinal)(void *ctx, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplMdDeInitCtx)(void *ctx); typedef void *(*CRYPT_EAL_ImplMdDupCtx)(const void *ctx); typedef int32_t (*CRYPT_EAL_ImplMdCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen); // not support typedef void (*CRYPT_EAL_ImplMdFreeCtx)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplMdSqueeze)(void *ctx, uint8_t *out, uint32_t len); typedef int32_t (*CRYPT_EAL_ImplMdCopyCtx)(void *dst, const void *src); typedef int32_t (*CRYPT_EAL_ImplMdGetParam)(void *ctx, BSL_Param *param); // CRYPT_EAL_OPERAID_MAC #define CRYPT_EAL_IMPLMAC_NEWCTX 1 #define CRYPT_EAL_IMPLMAC_INIT 2 #define CRYPT_EAL_IMPLMAC_UPDATE 3 #define CRYPT_EAL_IMPLMAC_FINAL 4 #define CRYPT_EAL_IMPLMAC_DEINITCTX 5 #define CRYPT_EAL_IMPLMAC_REINITCTX 6 #define CRYPT_EAL_IMPLMAC_CTRL 7 #define CRYPT_EAL_IMPLMAC_FREECTX 8 #define CRYPT_EAL_IMPLMAC_SETPARAM 9 typedef void *(*CRYPT_EAL_ImplMacNewCtx)(void *provCtx, int32_t algId); typedef int32_t (*CRYPT_EAL_ImplMacInit)(void *ctx, const uint8_t *key, uint32_t len, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplMacUpdate)(void *ctx, const uint8_t *input, uint32_t len); typedef int32_t (*CRYPT_EAL_ImplMacFinal)(void *ctx, uint8_t *out, uint32_t *outLen); typedef int32_t (*CRYPT_EAL_ImplMacDeInitCtx)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplMacReInitCtx)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplMacCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen); typedef void (*CRYPT_EAL_ImplMacFreeCtx)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplMacSetParam)(void *ctx, const BSL_Param *param); // CRYPT_EAL_OPERAID_KDF #define CRYPT_EAL_IMPLKDF_NEWCTX 1 #define CRYPT_EAL_IMPLKDF_SETPARAM 2 #define CRYPT_EAL_IMPLKDF_DERIVE 3 #define CRYPT_EAL_IMPLKDF_DEINITCTX 4 #define CRYPT_EAL_IMPLKDF_CTRL 5 // not support #define CRYPT_EAL_IMPLKDF_FREECTX 6 typedef void *(*CRYPT_EAL_ImplKdfNewCtx)(void *provCtx, int32_t algId); typedef int32_t (*CRYPT_EAL_ImplKdfSetParam)(void *ctx, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplKdfDerive)(void *ctx, uint8_t *key, uint32_t keyLen); typedef int32_t (*CRYPT_EAL_ImplKdfDeInitCtx)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplKdfCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen); // not support typedef void (*CRYPT_EAL_ImplKdfFreeCtx)(void *ctx); // CRYPT_EAL_OPERAID_RAND #define CRYPT_EAL_IMPLRAND_DRBGNEWCTX 1 #define CRYPT_EAL_IMPLRAND_DRBGINST 2 #define CRYPT_EAL_IMPLRAND_DRBGUNINST 3 #define CRYPT_EAL_IMPLRAND_DRBGGEN 4 #define CRYPT_EAL_IMPLRAND_DRBGRESEED 5 #define CRYPT_EAL_IMPLRAND_DRBGCTRL 6 #define CRYPT_EAL_IMPLRAND_DRBGFREECTX 7 typedef void *(*CRYPT_EAL_ImplRandDrbgNewCtx)(void *provCtx, int32_t algId, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplRandDrbgInst)(void *ctx, const uint8_t *pers, uint32_t persLen, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplRandDrbgUnInst)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplRandDrbgGen)(void *ctx, uint8_t *out, uint32_t outLen, const uint8_t *addin, uint32_t adinLen, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplRandDrbgReSeed)(void *ctx, const uint8_t *addin, uint32_t addinLen, BSL_Param *param); typedef int32_t (*CRYPT_EAL_ImplRandDrbgCtrl)(void *ctx, int32_t cmd, void *val, uint32_t valLen); typedef void (*CRYPT_EAL_ImplRandDrbgFreeCtx)(void *ctx); // CRYPT_EAL_OPERAID_DECODER #define CRYPT_DECODER_IMPL_NEWCTX 1 #define CRYPT_DECODER_IMPL_SETPARAM 2 #define CRYPT_DECODER_IMPL_GETPARAM 3 #define CRYPT_DECODER_IMPL_DECODE 4 #define CRYPT_DECODER_IMPL_FREEOUTDATA 5 #define CRYPT_DECODER_IMPL_FREECTX 6 typedef void *(*CRYPT_DECODER_IMPL_NewCtx)(void *provCtx); typedef int32_t (*CRYPT_DECODER_IMPL_SetParam)(void *ctx, const BSL_Param *param); typedef int32_t (*CRYPT_DECODER_IMPL_GetParam)(void *ctx, BSL_Param *param); typedef int32_t (*CRYPT_DECODER_IMPL_Decode)(void *ctx, const BSL_Param *inParam, BSL_Param **outParam); typedef void (*CRYPT_DECODER_IMPL_FreeOutData)(void *ctx, BSL_Param *outData); typedef void (*CRYPT_DECODER_IMPL_FreeCtx)(void *ctx); // CRYPT_EAL_OPERAID_SELFTEST #define CRYPT_EAL_IMPLSELFTEST_NEWCTX 1 #define CRYPT_EAL_IMPLSELFTEST_GETVERSION 2 #define CRYPT_EAL_IMPLSELFTEST_SELFTEST 3 #define CRYPT_EAL_IMPLSELFTEST_FREECTX 4 typedef void *(*CRYPT_EAL_ImplSelftestNewCtx)(void *provCtx); typedef const char *(*CRYPT_EAL_ImplSelftestGetVersion)(void *ctx); typedef int32_t (*CRYPT_EAL_ImplSelftestSelftest)(void *ctx, const BSL_Param *param); typedef void (*CRYPT_EAL_ImplSelftestFreeCtx)(void *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_IMPLPROVIDER_H
2301_79861745/bench_create
include/crypto/crypt_eal_implprovider.h
C
unknown
15,614
/* * 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_method * @ingroup crypt * @brief methods of crypto */ #ifndef CRYPT_EAL_INIT_H #define CRYPT_EAL_INIT_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif // __cplusplus #define CRYPT_EAL_INIT_CPU 0x01 #define CRYPT_EAL_INIT_BSL 0x02 #define CRYPT_EAL_INIT_RAND 0x04 #define CRYPT_EAL_INIT_PROVIDER 0x08 #define CRYPT_EAL_INIT_LOCK 0x10 #define CRYPT_EAL_INIT_PROVIDER_RAND 0x20 #define CRYPT_EAL_INIT_ALL (CRYPT_EAL_INIT_CPU | CRYPT_EAL_INIT_BSL | CRYPT_EAL_INIT_RAND | \ CRYPT_EAL_INIT_PROVIDER | CRYPT_EAL_INIT_PROVIDER_RAND | CRYPT_EAL_INIT_LOCK) /** * @ingroup crypt_method * @brief CRYPTO initialization * * @param opts [IN] Bit information to be initialized, the first three bits are used at present. * The first bit is CRYPT_EAL_INIT_CPU marked as "CPU ", the second bit is BSL * CRYPT_EAL_INIT_BSL marked as "BSL", and the third bit is CRYPT_EAL_INIT_RAND * marked as "RAND". * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_Init(uint64_t opts); /** * @ingroup crypt_method * @brief release the CRYPTO initialization memory. * * @param opts [IN] information about the bits to be deinitialized, which is the same as that of CRYPT_EAL_Init. */ void CRYPT_EAL_Cleanup(uint64_t opts); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_INIT_H
2301_79861745/bench_create
include/crypto/crypt_eal_init.h
C
unknown
2,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. */ /** * @defgroup crypt_eal_kdf * @ingroup crypt * @brief kdf of crypto module */ #ifndef CRYPT_EAL_KDF_H #define CRYPT_EAL_KDF_H #include <stdbool.h> #include <stdint.h> #include "crypt_algid.h" #include "crypt_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct EalKdfCtx CRYPT_EAL_KdfCTX; /** * @ingroup crypt_eal_kdf, Not supported in provider * @brief Check whether the given kdf algorithm ID is valid. * * @param id [IN] kdf algorithm ID. * @retval Valid, true is returned. * Invalid, false is returned. */ bool CRYPT_EAL_KdfIsValidAlgId(CRYPT_KDF_AlgId id); /** * @ingroup crypt_eal_kdf * @brief Generate kdf handles in the providers * * @param libCtx [IN] Library context * @param attrName [IN] Specify expected attribute values * @param algId [IN] kdf algorithm ID. * @retval Success: kdf ctx. * Fails: NULL. */ CRYPT_EAL_KdfCTX *CRYPT_EAL_ProviderKdfNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t algId, const char *attrName); /** * @ingroup crypt_eal_kdf * @brief Generate kdf handles * @param algId [IN] kdf algorithm ID. * @retval Success: kdf ctx. * Fails: NULL. */ CRYPT_EAL_KdfCTX *CRYPT_EAL_KdfNewCtx(CRYPT_KDF_AlgId algId); /** * @ingroup crypt_eal_kdf * @brief Set the parameters of Algorithm kdf * * @param ctx [IN] kdf context * @param param [IN] parameters * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_KdfSetParam(CRYPT_EAL_KdfCTX *ctx, const BSL_Param *param); /** * @ingroup crypt_eal_kdf * @brief Derived key * * @param ctx [IN] kdf context * @param key [OUT] Derived key * @param keyLen [IN] Specify the key derivation length * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_KdfDerive(CRYPT_EAL_KdfCTX *ctx, uint8_t *key, uint32_t keyLen); /** * @ingroup crypt_eal_kdf * @brief Deinitialize the context of kdf * * @param ctx [IN] kdf context * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_KdfDeInitCtx(CRYPT_EAL_KdfCTX *ctx); /** * @ingroup crypt_eal_kdf * @brief Free the context of kdf * * @param ctx [IN] kdf context * */ void CRYPT_EAL_KdfFreeCtx(CRYPT_EAL_KdfCTX *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_KDF_H
2301_79861745/bench_create
include/crypto/crypt_eal_kdf.h
C
unknown
2,925
/* * 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_mac * @ingroup crypt * @brief mac of crypto module */ #ifndef CRYPT_EAL_MAC_H #define CRYPT_EAL_MAC_H #include <stdbool.h> #include <stdint.h> #include "bsl_params.h" #include "crypt_algid.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif typedef struct EAL_MacCtx CRYPT_EAL_MacCtx; /** * @ingroup crypt_eal_mac * @brief Check whether the id is Valid MAC algorithm ID. * * @param id [IN] MAC algorithm ID * * @retval true, if valid. * false, if invalid. */ bool CRYPT_EAL_MacIsValidAlgId(CRYPT_MAC_AlgId id); /** * @ingroup crypt_eal_mac * @brief Apply for a MAC context. * * @param id [IN] MAC algorithm ID * * @retval CRYPT_EAL_MacCtx Pointer. * NULL, if the operation fails. */ CRYPT_EAL_MacCtx *CRYPT_EAL_MacNewCtx(CRYPT_MAC_AlgId id); /** * @ingroup crypt_eal_mac * @brief Create an MAC context in the providers. * * @param libCtx [IN] Library context * @param algId [IN] mac algorithm ID. * @param attrName [IN] Specify expected attribute values * * @retval CRYPT_EAL_MacCtx pointer. * NULL, if the operation fails. */ CRYPT_EAL_MacCtx *CRYPT_EAL_ProviderMacNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t algId, const char *attrName); /** * @ingroup crypt_eal_mac * @brief Release the MAC context memory. * * @param ctx [IN] MAC context, ctx set NULL by caller. */ void CRYPT_EAL_MacFreeCtx(CRYPT_EAL_MacCtx *ctx); /** * @ingroup crypt_eal_mac * * MAC algorithm initialize the context, which is used after the CRYPT_EAL_MacNewCtx interface is called. * The initialization interface can be used at any time during the calculation, note that the last calculation data * is cleared after the initialization interface is called. * * @param ctx [IN] MAC context * @param key [IN] Key, The length specifications are as follows: * HMAC:Any integer greater than or equal to 0 * The length of HMAC-SHA1, HMAC-SHA224, and HMAC-SHA256 must be less than 2^64 bits, * the length of HMAC-SHA384 and HMAC-SHA512 must be less than 2^128 bits. * HMAC-SHA3 series has no limit on length * CMAC: The length of CMAC-AES128 must be 128 bits, and the length of CMAC-AES192 must be 192 bits. * The length of CMAC-AES256 must be 256 bits. * @param len [IN] Key length * * @retval #CRYPT_SUCCESS, initialization succeeded. * @retval #CRYPT_NULL_INPUT, pointer ctx parameter or key parameter is NULL. * @retval #CRYPT_AES_ERR_KEYLEN, the key length of the AES & CMAC algorithm is incorrect. * Other error codes see the crypt_errno.h. */ int32_t CRYPT_EAL_MacInit(CRYPT_EAL_MacCtx *ctx, const uint8_t *key, uint32_t len); /** * @ingroup crypt_eal_mac * @brief Continuously input the MAC data. * * This command is used only after the CRYPT_EAL_MacInit interface is successfully called. * * @param ctx [IN] MAC context * @param in [IN] Input data, when the variable is null, the len parameter must be 0. * Otherwise, an error is reported. * @param len [IN] Input data length, the value can be 0. * * @retval #CRYPT_SUCCESS, succeeded in updating the internal status of the digest. * @retval #CRYPT_NULL_INPUT, the input parameter is NULL. * @retval #CRYPT_EAL_ERR_STATE, status error. * @retval #CRYPT_SHA1_INPUT_OVERFLOW, the length of the HMAC-SHA1 input data exceeds the maximum value. * @retval #CRYPT_SHA2_INPUT_OVERFLOW, the length of the HMAC-SHA224, HMAC-SHA256, HMAC-SHA384, or HMAC-SHA512 * input data exceeds the maximum value, Other error codes see the crypt_errno.h. */ int32_t CRYPT_EAL_MacUpdate(CRYPT_EAL_MacCtx *ctx, const uint8_t *in, uint32_t len); /** * @ingroup crypt_eal_mac * @brief Output the MAC result. * * This API must be used after the CRYPT_EAL_MacInit API is successfully executed, during the process, you * do not need to call the CRYPT_EAL_MacUpdate API. * MAC output length. HMAC-SHA1 corresponds to 20 bytes, HMAC-SHA224 corresponds to 28 bytes, and HMAC-SHA256 * corresponds to 32 bytes. HMAC-SHA384 corresponds to 48 bytes, HMAC-SHA512 corresponds to 64 bytes, and CMAC-AES * corresponds to 16 bytes. HMAC-SHA3-224 corresponds to 28 bytes, HMAC-SHA3-256 corresponds to 32 bytes, * HMAC-SHA3-384 corresponds to 48 bytes, and HMAC-SHA3-512 corresponds to 64 bytes. * * @param ctx [IN] MAC context * @param out [OUT] Output data. Sufficient memory must be allocated to store MAC results and cannot be null. * @param len [IN/OUT] Output data length. The input parameter must specify the out length, * which must be greater than or equal to the length generated by the MAC. * The output parameter is the output length of the MAC. * * @retval #CRYPT_SUCCESS, calculation succeeded. * @retval #CRYPT_NULL_INPUT, the input parameter is NULL. * @retval #CRYPT_EAL_ERR_STATE, status incorrect. * @retval #CRYPT_HMAC_OUT_BUFF_LEN_NOT_ENOUGH, the length of the output buffer in the HMAC algorithm is insufficient. * @retval #CRYPT_CMAC_OUT_BUFF_LEN_NOT_ENOUGH, the length of the output buffer in the CMAC algorithm is insufficient. * @retval #CRYPT_SHA1_INPUT_OVERFLOW, the length of the HMAC-SHA1 input data exceeds the maximum. * @retval #CRYPT_SHA2_INPUT_OVERFLOW, the length of the input data in HMAC-SHA224, HMAC-SHA256, HMAC-SHA384, or * HMAC-SHA512 exceeds the maximum value. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_MacFinal(CRYPT_EAL_MacCtx *ctx, uint8_t *out, uint32_t *len); /** * @ingroup crypt_eal_mac * @brief Deinitialization function. * * If calculation is required after this function is called, it needs to be initialized again. * * @param ctx [IN] MAC context */ void CRYPT_EAL_MacDeinit(CRYPT_EAL_MacCtx *ctx); /** * @ingroup crypt_eal_mac * @brief Re-initialize with the information retained in ctx. * * @attention Doesn't need call the init interface again for initialization, it is equivalent to the combination * of the deinit and init interfaces. * @param ctx [IN] MAC context * @retval #CRYPT_SUCCESS, reinit succeeded. * @retval #CRYPT_NULL_INPUT, the input parameter is NULL. */ int32_t CRYPT_EAL_MacReinit(CRYPT_EAL_MacCtx *ctx); /** * @ingroup crypt_eal_mac * @brief Through the context, obtain the output MAC length of the corresponding algorithm. * * @param ctx [IN] MAC context * @retval The MAC length corresponding to the context. */ uint32_t CRYPT_EAL_GetMacLen(const CRYPT_EAL_MacCtx *ctx); /** * @ingroup crypt_eal_mac * @brief Set algorithm parameters. This API must be called after the CRYPT_EAL_MacInit API is called. * This API supports only the GMAC algorithm. * * Parameter Data Type len stands for length, and in represents the number of bytes * CRYPT_CTRL_SET_IV uint8_t array Length of IV * CRYPT_CTRL_SET_TAGLEN uint32_t 4 bytes, sizeof(uint32_t) * CRYPT_CTRL_GET_MACLEN * * @param ctx [IN] MAC context * @param type [IN] Set parameter type. * @param in [IN] Input data * @param len [IN] Input data length * @retval #CRYPT_SUCCESS, parameters are set successfully. * @retval #CRYPT_EAL_ERR_STATE, status incorrect. * @retval #CRYPT_EAL_MAC_CTRL_TYPE_ERROR, the parameter type is set incorrect. * @retval #CRYPT_EAL_ERR_ALGID, algorithm ID exclude GMAC. * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_MacCtrl(CRYPT_EAL_MacCtx *ctx, int32_t type, void *in, uint32_t len); /** * @ingroup crypt_eal_mac * @brief Set algorithm parameters. * * @param ctx [IN] MAC context * @param param [IN] Parameter * @retval #CRYPT_SUCCESS, parameters are set successfully. * @retval #CRYPT_NULL_INPUT, the input parameter is NULL. * Other error codes see crypt_errno.h */ int32_t CRYPT_EAL_MacSetParam(CRYPT_EAL_MacCtx *ctx, const BSL_Param *param); #ifdef __cplusplus } // end extern "C" #endif #endif // CRYPT_EAL_MAC_H
2301_79861745/bench_create
include/crypto/crypt_eal_mac.h
C
unknown
8,668
/* * 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_md * @ingroup crypt * @brief md algorithms of crypto module */ #ifndef CRYPT_EAL_MD_H #define CRYPT_EAL_MD_H #include <stdbool.h> #include <stdint.h> #include "crypt_algid.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct EAL_MdCtx CRYPT_EAL_MdCTX; /** * @ingroup crypt_eal_md * @brief Create the MD context. * * After the calculation is complete, call the CRYPT_EAL_MdFreeCtx interface to release the memory. * * @param id [IN] Algorithm ID * @retval CRYPT_EAL_MdCTX, MD context pointer. * NULL, if the operation fails. */ CRYPT_EAL_MdCTX *CRYPT_EAL_MdNewCtx(CRYPT_MD_AlgId id); /** * @ingroup crypt_eal_md * @brief Create a md context in the providers. * * @param libCtx [IN] Library context, if NULL, use the default provider * @param algId [IN] md algorithm ID. * @param attrName [IN] Specify expected attribute values * * @retval CRYPT_EAL_PkeyCtx pointer. * NULL, if the operation fails. */ CRYPT_EAL_MdCTX *CRYPT_EAL_ProviderMdNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t algId, const char *attrName); /** * @ingroup crypt_eal_md * @brief Check whether the id is valid MD algorithm ID. Not supported in provider * * @param id [IN] MD algorithm ID. * @retval true, If the value is valid. * false, If the value is invalid. */ bool CRYPT_EAL_MdIsValidAlgId(CRYPT_MD_AlgId id); /** * @ingroup crypt_eal_md * @brief Return the MD algorithm ID. * * @param pkey [IN] MD context * @retval ID, MD algorithm ID. * CRYPT_MD_MAX, which indicates invalid ID or the input parameter is null. */ int32_t CRYPT_EAL_MdGetId(CRYPT_EAL_MdCTX *ctx); /** * @ingroup crypt_eal_md * @brief Copy the MD context. * * @param to [IN/OUT] Target MD context * @param from [IN] Source MD context * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_MdCopyCtx(CRYPT_EAL_MdCTX *to, const CRYPT_EAL_MdCTX *from); /** * @ingroup crypt_eal_md * @brief Copy the MD context. * * Note that need to call the CRYPT_EAL_MdFreeCtx interface to release the memory after the duplication is complete. * * @param ctx [IN] Source MD context * @retval CRYPT_EAL_MdCTX, MD context pointer. * NULL, if the operation fails. */ CRYPT_EAL_MdCTX *CRYPT_EAL_MdDupCtx(const CRYPT_EAL_MdCTX *ctx); /** * @ingroup crypt_eal_md * @brief Release the MD context. * * @param ctx [IN] MD context. which is created by using the CRYPT_EAL_MdNewCtx interface and need to be set * NULL by caller. * @retval Void, no return value. */ void CRYPT_EAL_MdFreeCtx(CRYPT_EAL_MdCTX *ctx); /** * @ingroup crypt_eal_md * @brief Initialize the MD context. * * @param ctx [IN/OUT] MD context, which is created by using the CRYPT_EAL_MdNewCtx interface. * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_MdInit(CRYPT_EAL_MdCTX *ctx); /** * @ingroup crypt_eal_md * @brief Continuously input the data to be digested. * * @param ctx [IN/OUT] MD context, which is created by using the CRYPT_EAL_MdNewCtx interface. * @param data [IN] Data to be digested. * @param len [IN] Data length. * The maximum length of sha384 and sha512 is [0, 2^128 bits). * The maximum total length of sha1, sha224, sha256, sm3, and md5 is [0, 2^64 bits). * The maximum length at a time is [0, 0xffffffff]. * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_MdUpdate(CRYPT_EAL_MdCTX *ctx, const uint8_t *data, uint32_t len); /** * @ingroup crypt_eal_md * @brief Generate output from the sponge construction's squeezing phase. * * This interface implements the squeeze capability of sponge-based hash functions (e.g. SHAKE). * Can be called multiple times to generate additional output. Must be called after finalization. * * @param ctx [IN/OUT] MD context (must be in squeezed state) * @param out [OUT] Buffer to store squeezed output * @param len [IN] Input: requested output length (must be <= buffer size) * @retval #CRYPT_SUCCESS * #CRYPT_E_SHORT_BUFFER if output buffer is too small * For other error codes, see crypt_errno.h */ int32_t CRYPT_EAL_MdSqueeze(CRYPT_EAL_MdCTX *ctx, uint8_t *out, uint32_t len); /** * @ingroup crypt_eal_md * @brief Complete the digest and output the final digest result. * * @param ctx [IN/OUT] MD context, which is created by using the CRYPT_EAL_MdNewCtx interface. * @param out [OUT] Digest result cache, which needs to be created and managed by users. * @param len [IN/OUT] The input parameter indicates the length of the buffer marked as "out", and the output * parameter indicates the valid length of the obtained "out". The length must be greater than or equal to * the hash length of the corresponding algorithm, the hash length can be obtained through the * CRYPT_EAL_MdGetDigestSize interface. * Requires user creation management. * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_MdFinal(CRYPT_EAL_MdCTX *ctx, uint8_t *out, uint32_t *len); /** * @ingroup crypt_eal_md * @brief Obtain the digest length of the algorithm output. Not supported in provider * * @param id [IN] Algorithm ID * @retval Digest length, if successful. * 0, if failed(in this case, the ID is invalid). */ uint32_t CRYPT_EAL_MdGetDigestSize(CRYPT_MD_AlgId id); /** * @ingroup crypt_eal_md * @brief Calculate the data digest. Not supported in provider * * @param id [IN] Algorithm ID * @param in [IN] Data to be digested * @param len [IN] Data length * @param out [OUT] Digest result * @param len [IN/OUT] The input parameter indicates the length of the buffer marked as "out", and the output * parameter indicates the valid length of the obtained "out". * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_Md(CRYPT_MD_AlgId id, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_md * @brief Deinitialize the function. * * If need to be calculated after the CRYPT_EAL_MdDeinit is called, it needs to be initialized again. * * @param ctx [IN] Md Context */ int32_t CRYPT_EAL_MdDeinit(CRYPT_EAL_MdCTX *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_MD_H
2301_79861745/bench_create
include/crypto/crypt_eal_md.h
C
unknown
7,048
/* * 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_pkey * @ingroup crypt * @brief the asym key module */ #ifndef CRYPT_EAL_PKEY_H #define CRYPT_EAL_PKEY_H #include <stdbool.h> #include <stdint.h> #include "crypt_algid.h" #include "crypt_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus /** * @ingroup crypt_eal_pkey * * EAL public key structure */ typedef struct { CRYPT_PKEY_AlgId id; /**< Public Key Algorithm ID */ union { CRYPT_RsaPub rsaPub; /**< RSA public key structure */ CRYPT_DsaPub dsaPub; /**< DSA public key structure */ CRYPT_DhPub dhPub; /**< DH public key structure */ CRYPT_EccPub eccPub; /**< ECC public key structure */ CRYPT_Curve25519Pub curve25519Pub; /**< ed25519/x25519 public key structure */ CRYPT_PaillierPub paillierPub; /**< Paillier public key structure */ CRYPT_KemEncapsKey kemEk; /**< kem encaps key structure */ CRYPT_ElGamalPub elgamalPub; /**< Elgamal public key structure */ CRYPT_MlDsaPub mldsaPub; /**< MLDSA public key structure */ CRYPT_SlhDsaPub slhDsaPub; /**< SLH-DSA public key structure */ CRYPT_XmssPub xmssPub; /**< XMSS public key structure */ } key; /**< Public key union of all algorithms */ } CRYPT_EAL_PkeyPub; #define CRYPT_EAL_PKEY_UNKNOWN_OPERATE 0 #define CRYPT_EAL_PKEY_CIPHER_OPERATE 1 #define CRYPT_EAL_PKEY_EXCH_OPERATE 2 #define CRYPT_EAL_PKEY_SIGN_OPERATE 4 #define CRYPT_EAL_PKEY_KEM_OPERATE 8 /** * @ingroup crypt_eal_pkey * * EAL private key structure */ typedef struct { CRYPT_PKEY_AlgId id; /**< private key algorithm ID */ union { CRYPT_RsaPrv rsaPrv; /**< RSA private key structure */ CRYPT_DsaPrv dsaPrv; /**< DSA private key structure */ CRYPT_DhPrv dhPrv; /**< DH private key structure */ CRYPT_EccPrv eccPrv; /**< ECC private key structure */ CRYPT_Curve25519Prv curve25519Prv; /**< ed25519/x25519 private key structure */ CRYPT_PaillierPrv paillierPrv; /**< Paillier private key structure */ CRYPT_KemDecapsKey kemDk; /**< kem decaps key structure */ CRYPT_ElGamalPrv elgamalPrv; /**< ElGamal private key structure */ CRYPT_MlDsaPrv mldsaPrv; /**< MLDSA private key structure */ CRYPT_SlhDsaPrv slhDsaPrv; /**< SLH-DSA private key structure */ CRYPT_XmssPrv xmssPrv; /**< XMSS private key structure */ } key; /**<Private key union of all algorithms */ } CRYPT_EAL_PkeyPrv; /** * @ingroup crypt_eal_pkey * * Structure used by the Para parameter of the asymmetric algorithm, including the algorithm ID and the * para combination of the corresponding algorithm. */ typedef struct { CRYPT_PKEY_AlgId id; /**< asymmetric algorithm ID */ union { CRYPT_RsaPara rsaPara; /**< RSA Para structure */ CRYPT_DsaPara dsaPara; /**< DSA Para structure */ CRYPT_DhPara dhPara; /**< DH Para structure */ CRYPT_EccPara eccPara; /**< ECC Para structure */ CRYPT_PaillierPara paillierPara; /**< Paillier Para structure */ CRYPT_ElGamalPara elgamalPara; /**< ElGamal Para structure */ } para; /**<Para union of all algorithms */ } CRYPT_EAL_PkeyPara; /** * @ingroup crypt_eal_pkey * * Pkey session structure. */ typedef struct EAL_PkeyCtx CRYPT_EAL_PkeyCtx; /** * @ingroup crypt_eal_pkey * @brief Check whether the id is valid asymmetric algorithm ID. * * @param id [IN] Asymmetric algorithm ID * * @retval true, if the value is valid. * false, if the value is invalid. */ bool CRYPT_EAL_PkeyIsValidAlgId(CRYPT_PKEY_AlgId id); /* Pkey external interface */ /** * @ingroup crypt_eal_pkey * @brief Create an asymmetric key pair structure. * * @param id [IN] Algorithm ID * * @retval CRYPT_EAL_PkeyCtx pointer. * NULL, if the operation fails. */ CRYPT_EAL_PkeyCtx *CRYPT_EAL_PkeyNewCtx(CRYPT_PKEY_AlgId id); /** * @ingroup crypt_eal_pkey * @brief Create an asymmetric key pair structure in the providers. * * @param libCtx [IN] Library context * @param algId [IN] Asymmetric algorithm ID. * @param pkeyOperType [IN] Specify operation type. * @param attrName [IN] Specify expected attribute values * * @retval CRYPT_EAL_PkeyCtx pointer. * NULL, if the operation fails. */ CRYPT_EAL_PkeyCtx *CRYPT_EAL_ProviderPkeyNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t algId, uint32_t pkeyOperType, const char *attrName); /** * @ingroup crypt_eal_pkey * @brief Copy the pkey context. * @note to and from must has identical key management. * * @param to [IN/OUT] Target pkey context * @param from [IN] Source pkey context * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyCopyCtx(CRYPT_EAL_PkeyCtx *to, const CRYPT_EAL_PkeyCtx *from); /** * @ingroup crypt_eal_pkey * @brief Copy the Pkey context. * After the duplication is complete, call the CRYPT_EAL_PkeyFreeCtx interface to release the memory. * * @param ctx [IN] Source Pkey context * * @retval CRYPT_EAL_PkeyCtx, Pkey context pointer. * NULL, if the operation fails. */ CRYPT_EAL_PkeyCtx *CRYPT_EAL_PkeyDupCtx(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Release the asymmetric key pair structure. * * @param pkey [IN] Pkey context, which need to be set NULL by the caller. */ void CRYPT_EAL_PkeyFreeCtx(CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Set the key parameters, the key parameter marked as "para" is applied for and released by the caller. * * @param pkey [IN/OUT] Structure of the key pair to be set * @param para [IN] Parameter * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySetPara(CRYPT_EAL_PkeyCtx *pkey, const CRYPT_EAL_PkeyPara *para); /** * @ingroup crypt_eal_pkey * @brief Set the key parameters. * * @param pkey [IN/OUT] Structure of the key pair to be set * @param param [IN] Parameter * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySetParaEx(CRYPT_EAL_PkeyCtx *pkey, const BSL_Param *param); /** * @ingroup crypt_eal_pkey * @brief Obtain the key parameter, the key parameter marked as "para" is applied for and released by the caller. * * @param pkey [IN] Key pair structure * @param para [OUT] Parameter to be received * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyGetPara(const CRYPT_EAL_PkeyCtx *pkey, CRYPT_EAL_PkeyPara *para); /** * @ingroup crypt_eal_pkey * @brief Set key parameters. * * @param pkey [IN/OUT] Structure of the key pair to be set. * @param id [IN] Parameter ID. * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySetParaById(CRYPT_EAL_PkeyCtx *pkey, CRYPT_PKEY_ParaId id); /** * @ingroup crypt_eal_pkey * @brief Generate the key data. * * @param pkey [IN/OUT] Key pair structure for receiving key data. * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyGen(CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Set the public key. The caller applies for and releases memory for the public key marked as "key". * * @param pkey [OUT] Key pair structure for receiving key data * @param key [IN] Public key data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySetPub(CRYPT_EAL_PkeyCtx *pkey, const CRYPT_EAL_PkeyPub *key); /** * @ingroup crypt_eal_pkey * @brief Extended interface to set the public key. * * This function is an extended version of CRYPT_EAL_PkeySetPub, which allows passing additional parameters * to meet more complex public key setting requirements. * * @param pkey [OUT] Key pair structure for receiving key data * @param param [IN] Public key data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySetPubEx(CRYPT_EAL_PkeyCtx *pkey, const BSL_Param *param); /** * @ingroup crypt_eal_pkey * @brief Set the private key. The caller applies for and releases memory for the private key marked as "key". * * @param pkey [OUT] Key pair structure for receiving key data * @param key [IN] Private key data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySetPrv(CRYPT_EAL_PkeyCtx *pkey, const CRYPT_EAL_PkeyPrv *key); /** * @ingroup crypt_eal_pkey * @brief Extended interface to set the private key. * * This function is an extended version of CRYPT_EAL_PkeySetPrv, which allows passing additional parameters * to meet more complex public key setting requirements. * * @param pkey [OUT] Key pair structure for receiving key data * @param param [IN] Private key data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySetPrvEx(CRYPT_EAL_PkeyCtx *pkey, const BSL_Param *param); /** * @ingroup crypt_eal_pkey * @brief Obtain the public key. The caller applies for and releases memory for the public key marked as "key". * * @param pkey [IN] Key session * @param key [OUT] Public key data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyGetPub(const CRYPT_EAL_PkeyCtx *pkey, CRYPT_EAL_PkeyPub *key); /** * @ingroup crypt_eal_pkey * @brief Extended interface to obtain the public key. * * This function is an extended version of CRYPT_EAL_PkeyGetPub, which allows passing parameters * through the BSL_Param structure to meet more complex public key acquisition requirements. * * @param pkey [IN] Key session * @param param [IN] parameters * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyGetPubEx(const CRYPT_EAL_PkeyCtx *pkey, BSL_Param *param); /** * @ingroup crypt_eal_pkey * @brief Obtain the private key. The caller applies for and releases memory for the private key marked as "key". * * @param pkey [IN] Key session * @param key [OUT] Private key data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyGetPrv(const CRYPT_EAL_PkeyCtx *pkey, CRYPT_EAL_PkeyPrv *key); /** * @ingroup crypt_eal_pkey * @brief Extended interface to obtain the private key. * * This function is an extended version of CRYPT_EAL_PkeyGetPrv, which allows passing parameters * through the BSL_Param structure to meet more complex public key acquisition requirements. * * @param pkey [IN] Key session * @param param [OUT] Private key data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyGetPrvEx(const CRYPT_EAL_PkeyCtx *pkey, BSL_Param *param); /** * @ingroup crypt_eal_pkey * @brief Signature interface * * @param pkey [IN] Key session * @param id [IN] Hash algorithm ID. * @param data [IN] Plaintext data * @param dataLen [IN] Plaintext length. The maximum length is [0, 0xffffffff]. * @param sign [OUT] Signature data. The length of the memory buff used to save the signature must be * greater than or equal to the key modulo length. * @param signLen [OUT/IN] Length of the signature data, You can obtain the value by calling * CRYPT_EAL_PkeyGetSignLen. * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeySign(const CRYPT_EAL_PkeyCtx *pkey, CRYPT_MD_AlgId id, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); /** * @ingroup crypt_eal_pkey * @brief Signature verification interface * * @param pkey [IN] Key session * @param id [IN] Hash algorithm ID. * @param data [IN] Plaintext data * @param dataLen [IN] Plaintext length. The maximum length is [0,0xffffffff]. * @param sign [IN] Signature data * @param signLen [IN] Length of the signature data * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyVerify(const CRYPT_EAL_PkeyCtx *pkey, CRYPT_MD_AlgId id, const uint8_t *data, uint32_t dataLen, const uint8_t *sign, uint32_t signLen); /** * @ingroup crypt_eal_pkey * @brief Sign hash data * * @param pkey [IN] Key session * @param hash [IN] Hash data * @param hashLen [IN] Hash length. * When RSA is used for signature, the hash length should correspond to the * digest length of the hash algorithm on which the padding method depends. * @param sign [OUT] Signature data. The length of the memory buff used to save the signature * must be greater than or equal to the key module length. * @param signLen [OUT/IN] Length of the signature data. * The value can be obtained by calling CRYPT_EAL_PkeyGetSignLen. * * @retval #CRYPT_SUCCESS, if successful. * For other error codes see crypt_errno.h */ int32_t CRYPT_EAL_PkeySignData(const CRYPT_EAL_PkeyCtx *pkey, const uint8_t *hash, uint32_t hashLen, uint8_t *sign, uint32_t *signLen); /** * @ingroup crypt_eal_pkey * @brief Verify the signature of the hash data * * @param pkey [IN] Key session * @param hash [IN] Hash data * @param hashLen [IN] Hash length. * When RSA is used for signature, the hash length should correspond to the digest * length of the hash algorithm on which the padding method depends. * @param sign [IN] Signature data * @param signLen [IN] Length of the signature data * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyVerifyData(const CRYPT_EAL_PkeyCtx *pkey, const uint8_t *hash, uint32_t hashLen, const uint8_t *sign, uint32_t signLen); /** * @ingroup crypt_eal_pkey * @brief Encrypt data. * * @param pkey [IN] Key session * @param data [IN] Input plaintext data. * @param dataLen [IN] Input plaintext data length. * @param out [OUT] Encrypted data. The buff length of the memory used to store the encrypted data * must be greater than or equal to the key modulus length. * @param outLen [OUT/IN] Encrypted data length. * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyEncrypt(const CRYPT_EAL_PkeyCtx *pkey, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_pkey * @brief Decrypt the data. * * @param pkey [IN] Key session * @param data [IN] Input ciphertext data. * @param dataLen [IN] Input ciphertext data length. * @param out [OUT] Decrypted data * @param outLen [OUT/IN] Length of the decrypted data. * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyDecrypt(const CRYPT_EAL_PkeyCtx *pkey, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_pkey * @brief Homomorphic addition operations * * @param pkey [IN] Addition Context * @param input [IN] Input ciphertext data * @param out [OUT] The result of the addition operation * @param outLen [OUT/IN] Pointer to the length of the addition result * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyHEAdd(const CRYPT_EAL_PkeyCtx *pkey, const BSL_Param *input, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_pkey * @brief Homomorphic multiplication operations * * @param pkey [IN] Multiplication Context * @param input [IN] Input ciphertext data * @param out [OUT] The result of the multiplication operation * @param outLen [OUT/IN] Pointer to the length of the multiplication result * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyHEMul(const CRYPT_EAL_PkeyCtx *pkey, const BSL_Param *input, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_pkey * @brief Check whether the public and private keys match. * @note pubKey and prvKey must has identical key management. * * @param pubKey [IN] Public key * @param prvKey [IN] private key * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyPairCheck(CRYPT_EAL_PkeyCtx *pubKey, CRYPT_EAL_PkeyCtx *prvKey); /** * @ingroup crypt_eal_pkey * @brief Check the private key is valid. * * @param prvKey [IN] Private key * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyPrvCheck(CRYPT_EAL_PkeyCtx *prvKey); /** * @ingroup crypt_eal_pkey * @brief Compute the shared key. * * @param pkey [IN] Key session * @param pubKey [IN] Public key session * @param share [OUT] Shared key * @param shareLen [IN/OUT] The input parameter is the share space length, and the output parameter is the * valid share space length, the required space can be obtained by calling the CRYPT_EAL_PkeyGetKeyLen interface. * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyComputeShareKey(const CRYPT_EAL_PkeyCtx *pkey, const CRYPT_EAL_PkeyCtx *pubKey, uint8_t *share, uint32_t *shareLen); /** * @ingroup crypt_eal_pkey * @brief Obtain the number of bytes in the key length. * * @param pkey [IN] Key session * * @retval Key length, if successful. * 0, if failed. */ uint32_t CRYPT_EAL_PkeyGetKeyLen(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Obtain the key security strength. Only supports CRYPT_PKEY_RSA and CRYPT_PKEY_ECDSA. * * @param pkey [IN] Key session * * @retval Key security strength, if successful. * 0, if failed. */ uint32_t CRYPT_EAL_PkeyGetSecurityBits(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Obtain the number of bits in the key length. * * @param pkey [IN] Key session * * @retval Number of key bits, if successful. * 0, if failed. */ uint32_t CRYPT_EAL_PkeyGetKeyBits(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Obtains the signature length of the key for signature, only support algorithm that can be signed. * * @param pkey [IN] Key session * * @retval Signature length, if successful. * 0, if failed. */ uint32_t CRYPT_EAL_PkeyGetSignLen(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Make specific option for setting/obtain, supported option can see the structure of CRYPT_PkeyCtrl. * * @param pkey [IN] Key session * @param opt [IN] Option information * @param val [IN/OUT] Data to be set/obtained * @param len [IN] Length of the data marked as "val" * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyCtrl(CRYPT_EAL_PkeyCtx *pkey, int32_t opt, void *val, uint32_t len); /** * @ingroup crypt_eal_pkey * @brief Perform blind operation on input data using the specified algorithm. * For RSA BSSA, users need to ensure sufficient entropy in the message if the input has low entropy. * @param pkey [IN] Key session * @param id [IN] md Id for input. * @param input [IN] Data to be blinded * @param inputLen [IN] Length of input data * @param out [OUT] Blinded output data * @param outLen [OUT] Length of blinded data * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyBlind(CRYPT_EAL_PkeyCtx *pkey, CRYPT_MD_AlgId id, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_pkey * @brief Perform unblind operation on blinded data. * * @param pkey [IN] Key session * @param input [IN] Blinded data to be unblinded * @param inputLen [IN] Length of blinded data * @param out [OUT] Unblinded output data * @param outLen [OUT] Length of unblinded data * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyUnBlind(CRYPT_EAL_PkeyCtx *pkey, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); /** * @ingroup crypt_eal_pkey * @brief Obtain the key algorithm type. * * @param pkey [IN] Key session * * @retval Key algorithm type */ CRYPT_PKEY_AlgId CRYPT_EAL_PkeyGetId(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Obtain the key algorithm parameter ID. * * @param pkey [IN] Key session * * @retval Algorithm parameter ID */ CRYPT_PKEY_ParaId CRYPT_EAL_PkeyGetParaId(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Compare keys or parameters * * @param a [IN] Key session * @param b [IN] Key session * * @retval #CRYPT_SUCCESS, a and b are the same(include both a and b are null) * @retval #CRYPT_NULL_INPUT, incorrect null pointer input. * @retval For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyCmp(const CRYPT_EAL_PkeyCtx *a, const CRYPT_EAL_PkeyCtx *b); /** * @ingroup crypt_eal_pkey * @brief Set the user's personal data. * * @param pkey [IN] Key session * @param data [IN] Pointer to the user's personal data * * @retval #CRYPT_SUCCESS, if successful. * @retval #CRYPT_NULL_INPUT, if pkey is NULL. */ int32_t CRYPT_EAL_PkeySetExtData(CRYPT_EAL_PkeyCtx *pkey, void *data); /** * @ingroup crypt_eal_pkey * @brief Obtain the user's personal data. * * @param pkey [IN] Key session * * @retval void*(user personal data pointer), which indicates successful. * NULL, which indicates failed. */ void *CRYPT_EAL_PkeyGetExtData(const CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief EAL layer reference counting auto-increment * * @param pkey [IN] Key session * * @retval #CRYPT_SUCCESS. * For other error codes see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyUpRef(CRYPT_EAL_PkeyCtx *pkey); /** * @ingroup crypt_eal_pkey * @brief Initialize asymmetric key encapsulation context * * @param pkey [in] Pointer to the key context * @param params [in] Algorithm parameters * * @retval #CRYPT_SUCCESS. * For other error codes see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyEncapsInit(CRYPT_EAL_PkeyCtx *pkey, BSL_Param *params); /** * @ingroup crypt_eal_pkey * @brief Initialize asymmetric key decapsulation context * * @param pkey [in] Pointer to the key context * @param params [in] Algorithm parameters * * @retval #CRYPT_SUCCESS. * For other error codes see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyDecapsInit(CRYPT_EAL_PkeyCtx *pkey, BSL_Param *params); /** * @ingroup crypt_eal_pkey * @brief Perform key encapsulation operation * * @param pkey [in] Initialized key context * @param cipher [out] Output buffer for encapsulated ciphertext * @param cipherLen [in,out] Input: buffer capacity, Output: actual ciphertext length * @param sharekey [out] Output buffer for shared secret * @param shareKeyLen [in,out] Input: buffer capacity, Output: actual secret length * * @retval #CRYPT_SUCCESS. * For other error codes see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyEncaps(const CRYPT_EAL_PkeyCtx *pkey, uint8_t *cipher, uint32_t *cipherLen, uint8_t *sharekey, uint32_t *shareKeyLen); /** * @ingroup crypt_eal_pkey * @brief Perform key decapsulation operation * * @param pkey [in] Initialized key context * @param cipher [in] Input encapsulated ciphertext * @param cipherLen [in] Length of the input ciphertext * @param sharekey [out] Output buffer for shared secret * @param shareKeyLen [in,out] Input: buffer capacity, Output: actual secret length * * @retval #CRYPT_SUCCESS. * For other error codes see crypt_errno.h. */ int32_t CRYPT_EAL_PkeyDecaps( const CRYPT_EAL_PkeyCtx *pkey, const uint8_t *cipher, uint32_t cipherLen, uint8_t *sharekey, uint32_t *shareKeyLen); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_PKEY_H
2301_79861745/bench_create
include/crypto/crypt_eal_pkey.h
C
unknown
25,286
/* * 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 introduced when then provider is used */ #ifndef CRYPT_EAL_PROVIDER_H #define CRYPT_EAL_PROVIDER_H #include <stdint.h> #include "crypt_types.h" #include "bsl_sal.h" #include "bsl_params.h" #include "crypt_eal_implprovider.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus /** * @ingroup crypt_eal_provider * @brief create Library context * * @retval return Library context */ CRYPT_EAL_LibCtx *CRYPT_EAL_LibCtxNew(void); /** * @ingroup crypt_eal_provider * @brief free Library context * @param libCtx [IN] Library context * */ void CRYPT_EAL_LibCtxFree(CRYPT_EAL_LibCtx *libCtx); /** * @ingroup crypt_eal_provider * @brief Provider load interface * * @param libCtx [IN] Library context * @param providerName [IN] provider name * @param param [IN] parameter is transparently passed to the initialization function of the underlying provider * @param cmd [IN] Command specifying the conversion format for the provider library name. * This parameter is used to determine how the provider library name should be * converted or formatted. Possible values are: * - BSL_SAL_LIB_FMT_SO: Convert to .so format * - BSL_SAL_LIB_FMT_LIBSO: Convert to lib*.so format * - BSL_SAL_LIB_FMT_LIBDLL: Convert to lib*.dll format * - BSL_SAL_LIB_FMT_DLL: Convert to .dll format * The specific conversion is handled by the BSL_SAL_LibNameFormat function. * @param mgrCtx [OUT] Provider context * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderLoad(CRYPT_EAL_LibCtx *libCtx, BSL_SAL_LibFmtCmd cmd, const char *providerName, BSL_Param *param, CRYPT_EAL_ProvMgrCtx **mgrCtx); /** * @ingroup crypt_eal_provider * @brief Provider register interface * * @param libCtx [IN] Library context * @param providerName [IN] provider name * @param init [IN] Provider initialization function * @param param [IN] parameter is transparently passed to the initialization function of the underlying provider * @param mgrCtx [OUT] Provider context * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderRegister(CRYPT_EAL_LibCtx *libCtx, const char *providerName, CRYPT_EAL_ImplProviderInit init, BSL_Param *param, CRYPT_EAL_ProvMgrCtx **mgrCtx); /** * @ingroup crypt_eal_provider * @brief Check if the provider is loaded * * @param libCtx [IN] Library context * @param cmd [IN] Command specifying the conversion format for the provider library name. * @param providerName [IN] provider name * @param isLoaded [OUT] Whether the provider is loaded * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderIsLoaded(CRYPT_EAL_LibCtx *libCtx, BSL_SAL_LibFmtCmd cmd, const char *providerName, bool *isLoaded); /** * @ingroup crypt_eal_provider * @brief Control provider interface * * @param ctx [IN] Provider context * @param cmd [IN] Control command * @param val [IN/OUT] Value associated with the command * @param valLen [IN] Length of the value * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderCtrl(CRYPT_EAL_ProvMgrCtx *ctx, int32_t cmd, void *val, uint32_t valLen); /** * @ingroup crypt_eal_provider * @brief Get and process provider capabilities * * @param ctx [IN] Provider context * @param cmd [IN] Command to specify which capabilities to retrieve * @param cb [IN] Callback function to process the retrieved capabilities * @param args [IN] Arguments to be passed to the callback function * * @retval #CRYPT_SUCCESS if capability retrieval and processing succeeds * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderGetCaps(CRYPT_EAL_ProvMgrCtx *ctx, int32_t cmd, CRYPT_EAL_ProcessFuncCb cb, void *args); /** * @ingroup crypt_eal_provider * @brief Provider unload interface * * @param libCtx [IN] Library context * @param cmd [IN] Command specifying the conversion format for the provider library name. * This parameter is used to determine how the provider library name should be * converted or formatted. Possible values are: * - BSL_SAL_LIB_FMT_SO: Convert to .so format * - BSL_SAL_LIB_FMT_LIBSO: Convert to lib*.so format * - BSL_SAL_LIB_FMT_LIBDLL: Convert to lib*.dll format * - BSL_SAL_LIB_FMT_DLL: Convert to .dll format * The specific conversion is handled by the BSL_SAL_LibNameFormat function. * @param providerName [IN] provider name * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderUnload(CRYPT_EAL_LibCtx *libCtx, BSL_SAL_LibFmtCmd cmd, const char *providerName); /** * @ingroup crypt_eal_provider * @brief Set the path to load the provider and support duplicate settings. * Repeating settings will release the previous path. * * @param libCtx [IN] Library context * @param serchPath [IN] the path to load the provider * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderSetLoadPath(CRYPT_EAL_LibCtx *libCtx, const char *searchPath); /** * @ingroup crypt_eal_provider * @brief Get function implementations from provider based on operation ID, algorithm ID and attributes * * @param libCtx [IN] Library context * @param operaId [IN] Operation ID * @param algId [IN] Algorithm ID * @param attribute [IN] Attribute string for matching provider capabilities * @param funcs [OUT] Retrieved function implementations * @param provCtx [OUT] Provider context associated with the functions * * @retval #CRYPT_SUCCESS, if success. * Other error codes see the crypt_errno.h */ 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); /** * @brief Callback function type for processing a single provider * * @param ctx [IN] Provider context for the current provider being processed * @param args [IN] User-provided arguments for provider processing * * @retval #CRYPT_SUCCESS if processing succeeds * Other error codes see the crypt_errno.h */ typedef int32_t (*CRYPT_EAL_ProviderProcessCb)(CRYPT_EAL_ProvMgrCtx *ctx, void *args); /** * @ingroup crypt_eal_provider * @brief Process all loaded providers with the specified callback function * * This function iterates through all providers loaded in the given library context * and applies the specified callback function to each provider. It allows performing * a common operation across all loaded providers. * * @param ctx [IN] Library context containing the providers to process * @param cb [IN] Callback function to be applied to each provider * @param args [IN] Arguments to be passed to the callback function * * @retval #CRYPT_SUCCESS if all providers were processed successfully * The first error code encountered if any provider processing fails * Other error codes see the crypt_errno.h */ int32_t CRYPT_EAL_ProviderProcessAll(CRYPT_EAL_LibCtx *ctx, CRYPT_EAL_ProviderProcessCb cb, void *args); #ifdef __cplusplus } #endif // __cplusplus #endif // CRYPT_EAL_PROVIDER_H
2301_79861745/bench_create
include/crypto/crypt_eal_provider.h
C
unknown
8,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. */ /** * @defgroup crypt_eal_rand * @ingroup crypt * @brief random number module */ #ifndef CRYPT_EAL_RAND_H #define CRYPT_EAL_RAND_H #include <stdbool.h> #include <stdint.h> #include "crypt_algid.h" #include "crypt_types.h" #include "crypt_eal_provider.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /** * @ingroup crypt_eal_rand * @brief rand generate callback * * rand[out] randomdata * randLen[in] len * * @return int32_t, defined by users. */ typedef int32_t (*CRYPT_EAL_RandFunc)(uint8_t *rand, uint32_t randLen); /** * @ingroup crypt_eal_rand * @brief set rand func callback * * func[in] rand func * * @return void. */ void CRYPT_EAL_SetRandCallBack(CRYPT_EAL_RandFunc func); /** * @ingroup crypt_eal_rand * @brief rand generate callback * * ctx[in] ctx * rand[out] randomdata * randLen[in] len * * @return int32_t, defined by users. */ typedef int32_t (*CRYPT_EAL_RandFuncEx)(void *ctx, uint8_t *rand, uint32_t randLen); /** * @ingroup crypt_eal_rand * @brief set rand func callback * * func[in] rand func * * @return void. */ void CRYPT_EAL_SetRandCallBackEx(CRYPT_EAL_RandFuncEx func); /** * @ingroup crypt_eal_rand * @brief Random number initialization interface. This interface does not support multiple threads. * * Initialize global random number to RAND, Entropy sources and addtional random numbers in the seed material * which implemented by HiTLS. and this value is provided by the user. if user not provid the entropy source * (seedMeth and seedCtx are both NULL), the default software entropy source is used. * In addition, this interface does not support multiple threads. * The global random number is initialized to the random generation algorithm described in Nist 800-90a. * Application scenarios are as follows: * 1. seedMeth == NULL && seedCtx == NULL ====> Use the default system entropy source in AES_CTR mode * (that is, non-DF cannot use the default entropy source). * 2. seedMeth == NULL && seedCtx != NULL ===> Error report. * 3. seedMeth != NULL ====> This function can be used normally, seedCtx is not restricted, but make sure * seedMeth can handle all kinds of situations. * * @attention: Support obtain or generate random numbers with multithreading, but not support initialization * and deinitialization with multithreading. * @param id [IN] RAND id * @param seedMeth [IN] Seed method, which can be set NULL with seedCtx, The default entropy source is used * or provided by the user. * @param seedCtx [IN] Seed context information, which can be set NULL, But the seedMeth provided by the user can * handle the situation where seedCtx is NULL. * Generally, seedCtx needs to contain data such as entropy and nonce. * @param pers [IN] Personal data, which can be NULL. * @param persLen [IN] Length of the personal data, the length ranges from [0,0x7FFFFFF0]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_RandInit(CRYPT_RAND_AlgId id, CRYPT_RandSeedMethod *seedMeth, void *seedCtx, const uint8_t *pers, uint32_t persLen); /** * @ingroup crypt_eal_rand * @brief Random number initialization in the providers. * * @param libCtx [IN] Library context * @param algId [IN] rand algorithm ID. * @param attrName [IN] Specify expected attribute values * @param pers [IN] Personal data, which can be NULL. * @param persLen [IN] Personal data length. the range is [0,0x7FFFFFF0]. * @param param [IN] Transparent transmission of underlying parameters * * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_ProviderRandInitCtx(CRYPT_EAL_LibCtx *libCtx, int32_t algId, const char *attrName, const uint8_t *pers, uint32_t persLen, BSL_Param *param); /** * @ingroup crypt_eal_rand * @brief Deinitializing the global RAND interface, this interface does not support multiple threads. * * @retval void, no return value. */ void CRYPT_EAL_RandDeinit(void); /** * @ingroup crypt_eal_rand * @brief Deinitializing the libCtx RAND interface, this interface does not support multiple threads. * * @param libCtx [IN] Library context * * @retval void, no return value. */ void CRYPT_EAL_RandDeinitEx(CRYPT_EAL_LibCtx *libCtx); /** * @ingroup crypt_eal_rand * @brief Generate a random number. * * The addtional data marked as "addin" can be NULL, and additional data specified by the user. * This interface does not support multiple threads. * * @param byte [OUT] Output random numbers, the memory is provided by the user. * @param len [IN] Required random number length, the maximum length is (0, 65536]. * @param addin [IN] Addtional data, which can set be NULL. * @param addinLen [IN] Addtional data length, the maximum length is[0,0x7FFFFFF0]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_RandbytesWithAdin(uint8_t *byte, uint32_t len, uint8_t *addin, uint32_t addinLen); /** * @ingroup crypt_eal_rand * @brief Generate a random number. * * The addtional data marked as "addin" can be NULL, and additional data specified by the user. * This interface does not support multiple threads. * * @param libCtx [IN] Library context * @param byte [OUT] Output random numbers, the memory is provided by the user. * @param len [IN] Required random number length, the maximum length is (0, 65536]. * @param addin [IN] Addtional data, which can set be NULL. * @param addinLen [IN] Addtional data length, the maximum length is[0,0x7FFFFFF0]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_RandbytesWithAdinEx(CRYPT_EAL_LibCtx *libCtx, uint8_t *byte, uint32_t len, uint8_t *addin, uint32_t addinLen); /** * @ingroup crypt_eal_rand * * Generate a random number, which is equivalent to CRYPT_EAL_RandbytesWithAdin(bytes, len, NULL, 0). * This interface supports multi-thread access. * * @param byte [OUT] Used to store output random numbers, the memory is provided by the user. * @param len [IN] Required random number length, the length range is(0, 65536]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_Randbytes(uint8_t *byte, uint32_t len); /** * @ingroup crypt_eal_rand * * Generate a random number * This interface supports multi-thread access. * * @param libCtx [IN] Library context * @param byte [OUT] Used to store output random numbers, the memory is provided by the user. * @param len [IN] Required random number length, the length range is(0, 65536]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_RandbytesEx(CRYPT_EAL_LibCtx *libCtx, uint8_t *byte, uint32_t len); /** * @ingroup crypt_eal_rand * @brief Regenerate the seed. * * @attention The addtional data can set be NULL, and this interface supports multi-thread access. * @param addin [IN] Additional data, which can set be NULL. * @param addinLen [IN] Addtional data length, the range is [0,0x7FFFFFF0]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. * * @note After forking, it is necessary to manually supplement the entropy source for the new program */ int32_t CRYPT_EAL_RandSeedWithAdin(uint8_t *addin, uint32_t addinLen); /** * @ingroup crypt_eal_rand * * Regenerate the seed, which is equivalent to CRYPT_EAL_RandSeedWithAdin(NULL, 0), and the interface * supports multi-thread access. * * @retval #CRYPT_SUCCESS * For other error codes, see crypt_errno.h. * * @note After forking, it is necessary to manually supplement the entropy source for the new program */ int32_t CRYPT_EAL_RandSeed(void); /** * @ingroup crypt_eal_rand * * Regenerate the seed, which is equivalent to CRYPT_EAL_RandSeedWithAdin(NULL, 0), and the interface * supports multi-thread access. * @param libCtx [IN] Library context * @retval #CRYPT_SUCCESS * For other error codes, see crypt_errno.h. * * @note After forking, it is necessary to manually supplement the entropy source for the new program */ int32_t CRYPT_EAL_RandSeedEx(CRYPT_EAL_LibCtx *libCtx); typedef struct EAL_RndCtx CRYPT_EAL_RndCtx; /** * @ingroup CRYPT_EAL_DrbgNew * @brief Random number initialization interface, and this interface does not support multiple threads. * * Initial DRBG with HiTLS, entropy source and addtional random number in the seed material * are provided by users. This interface does not support multi-threading, the initial random number is * the random number generation algorithm described in Nist 800-90a. * Usage scenes are as follows: * 1. seedMeth == NULL && seedCtx == NULL ====> Use the default system entropy source in AES_CTR mode * (that is, non-DF cannot use the default entropy source). * 2. seedMeth == NULL && seedCtx != NULL ===> error reported. * 3. seedMeth != NULL ====> This function can be used normally, seedCtx function is not restricted, * but make sure seedMeth can handle all kinds of situations. * * @attention Initialization and deinitialization * @param id [IN] RAND id * @param seedMeth [IN] Seed method, this parameter and seedCtx can be null at the same time. The default entropy * source is used or provided by the user. * @param seedCtx [IN] Seed context information, which can be NULL, but the seedMeth provided by the user needs * to be able to handle the situation where seedCtx is null. * seedCtx generally needs to contain the entropy source marked as "entropy", additional random number "nonce", and * other data. * @retval DRBG handle, if successful. * NULL, if failed. */ CRYPT_EAL_RndCtx *CRYPT_EAL_DrbgNew(CRYPT_RAND_AlgId id, CRYPT_RandSeedMethod *seedMeth, void *seedCtx); /** * @ingroup crypt_eal_rand * @brief Random number initialization in the providers. * * @param libCtx [IN] Library context * @param algId [IN] rand algorithm ID. * @param attrName [IN] Specify expected attribute values * @param param [IN] Transparent transmission of underlying parameters * * @retval Success: DRBG ctx. * Fails: NULL. */ CRYPT_EAL_RndCtx *CRYPT_EAL_ProviderDrbgNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t algId, const char *attrName, BSL_Param *param); /** * @ingroup CRYPT_EAL_DrbgDeinit * @brief CRYPT_EAL_DrbgDeinit Deinitialization interface, this interface does not support multiple threads. * * @param ctx [IN] DRBG handle * @retval Void, no value is returned. */ void CRYPT_EAL_DrbgDeinit(CRYPT_EAL_RndCtx *ctx); /** * @ingroup crypt_eal_rand * @brief Generate a random number. * * @attention The addtional data can be NULL, user specifies the addtional data, * and the interface supports multi-thread access. * @param ctx [IN] DRBG handle * @param byte [OUT] Outputs random numbers. the memory is provided by the user. * @param len [IN] Required random number length. the range is (0, 65536]. * @param addin [IN] Addtional data, which can be NULL. * @param addinLen [IN] Addtional data length. the range is [0,0x7FFFFFF0]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_DrbgbytesWithAdin(CRYPT_EAL_RndCtx *ctx, uint8_t *byte, uint32_t len, uint8_t *addin, uint32_t addinLen); /** * @ingroup crypt_eal_rand * * Generate a random number, which is equivalent to CRYPT_EAL_RandbytesWithAdin(bytes, len, NULL, 0). * This interface supports multi-thread access. * * @param ctx [IN] DRBG handle * @param byte [OUT] Used to store output random numbers. the memory is provided by the user. * @param len [IN] Required random number length. the range is (0, 65536]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see the crypt_errno.h file. */ int32_t CRYPT_EAL_Drbgbytes(CRYPT_EAL_RndCtx *ctx, uint8_t *byte, uint32_t len); /** * @ingroup crypt_eal_rand * @brief Regenerate the seed. The addtional data can be NULL. This interface supports multi-thread access. * * @param ctx [IN] DRBG handle * @param addin [IN] Addtional data, which can be null. * @param addinLen [IN] Addtional data length. The maximum length is [0,0x7FFFFFF0]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_DrbgSeedWithAdin(CRYPT_EAL_RndCtx *ctx, uint8_t *addin, uint32_t addinLen); /** * @ingroup crypt_eal_rand * @brief Regenerate the seed, which is equivalent to CRYPT_EAL_RandSeedWithAdin(NULL, 0). * * @attention This interface supports multi-thread access. * @param ctx [IN] DRBG handle. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_DrbgSeed(CRYPT_EAL_RndCtx *ctx); /** * @ingroup crypt_eal_rand * @brief Check whether the id is valid Rand algorithm ID. * * @param id [IN] Rand algorithm ID. * * @retval true, if valid. * false, if invalid. */ bool CRYPT_EAL_RandIsValidAlgId(CRYPT_RAND_AlgId id); /** * @ingroup crypt_eal_rand * @brief Instantiate the DRBG. * * This function instantiates the Deterministic Random Bit Generator (DRBG) with personalization string. * It supports multi-thread access. * * @param ctx [IN] DRBG handle * @param pers [IN] Personal data, which can be NULL. * @param persLen [IN] Personal data length. the range is [0,0x7FFFFFF0]. * @retval #CRYPT_SUCCESS, if successful. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_DrbgInstantiate(CRYPT_EAL_RndCtx *ctx, const uint8_t *pers, uint32_t persLen); /** * @ingroup crypt_eal_rand * @brief get or set rand param * * @param ctx [IN] rand context * @param cmd [IN] Option information * @param val [IN/OUT] Data to be set/obtained * @param valLen [IN] Length of the data marked as "val" * * @retval #CRYPT_SUCCESS. * For other error codes, see crypt_errno.h. */ int32_t CRYPT_EAL_DrbgCtrl(CRYPT_EAL_RndCtx *ctx, int32_t cmd, void *val, uint32_t valLen); /** * @ingroup crypt_eal_rand * @brief Get the seed of Primary DRBG. * * @param isParentEntropy [IN] If true, return primary DRBG; otherwise, return g_globalRndCtx. * * @retval DRBG handle. */ CRYPT_EAL_RndCtx *CRYPT_EAL_GetSeedCtx(bool isParentEntropy); #ifdef __cplusplus } #endif #endif // CRYPT_EAL_RAND_H
2301_79861745/bench_create
include/crypto/crypt_eal_rand.h
C
unknown
15,188
/* * 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_errno * @ingroup crypt * @brief error number module of crypto module */ #ifndef CRYPT_ERRNO_H #define CRYPT_ERRNO_H #ifdef __cplusplus extern "C" { #endif /** * @ingroup crypt_errno * @brief Return success */ #define CRYPT_SUCCESS 0 /** * @ingroup crypt_errno * * CRYPTO module return value. */ enum CRYPT_ERROR { CRYPT_NULL_INPUT = 0x01010001, /**< Null pointer input error, bufferLen is 0. */ CRYPT_SECUREC_FAIL, /**< Security function returns an error. */ CRYPT_MEM_ALLOC_FAIL, /**< Failed to apply for memory. */ CRYPT_NO_REGIST_RAND, /**< The global random number is not registered.*/ CRYPT_ERR_ALGID, /**< Incorrect algorithm ID. */ CRYPT_INVALID_ARG, /**< Invalid input parameter. */ CRYPT_NOT_SUPPORT, /**< unsupported operation. */ CRYPT_INCONSISTENT_OPERATION, /**< Inconsistent operation. */ CRYPT_INVALID_KEY, /**< invalid key. */ CRYPT_PAIRWISE_CHECK_FAIL, /**< key-pair check failed. */ CRYPT_BN_BUFF_LEN_NOT_ENOUGH = 0x01020001, /**< Insufficient buffer length. */ CRYPT_BN_SPACE_NOT_ENOUGH, /**< Insufficient big number space. */ CRYPT_BN_BITS_TOO_MAX, /**< The maximum bit limit is exceeded of the big number. */ CRYPT_BN_RAND_GEN_FAIL, /**< Failed to generate the random number. */ CRYPT_BN_OPTIMIZER_STACK_FULL, /**< Optimizer stack is full. */ CRYPT_BN_NO_NEGATIVE_ZERO, /**< The big number is set to a positive number only. */ CRYPT_BN_ERR_RAND_ZERO, /**< Generates a random number smaller than 0. */ CRYPT_BN_ERR_RAND_NEGATIVE, /**< Generate a negative random number. */ CRYPT_BN_ERR_RAND_TOP_BOTTOM, /**< The top or bottom is invalid during random number generation. */ CRYPT_BN_ERR_RAND_BITS_NOT_ENOUGH, /**< The bit is too small during random number generation. */ CRYPT_BN_OPTIMIZER_GET_FAIL, /**< Failed to obtain the space from the optimizer. */ CRYPT_BN_ERR_DIVISOR_ZERO, /**< The divisor cannot be 0. */ CRYPT_BN_ERR_EXP_NO_NEGATIVE, /**< The value of exponent cannot be negative. */ CRYPT_BN_MONT_BASE_TOO_MAX, /**< Montgomery module exponentiation base is too large. */ CRYPT_BN_NOR_GEN_PRIME, /**< Prime Number Generation Failure. */ CRYPT_BN_NOR_CHECK_PRIME, /**< prime number check failed. */ CRYPT_BN_ERR_GCD_NO_ZERO, /**< The maximum common divisor cannot contain 0. */ CRYPT_BN_ERR_NO_INVERSE, /**< Cannot obtain the inverse module. */ CRYPT_BN_ERR_SQRT_PARA, /**< The parameter is incorrect when modulus square root. */ CRYPT_BN_ERR_LEGENDE_DATA, /**< Failed to find a specific number for z to p's Legendre sign (z|p) equal to -1 when calculating the square root. */ CRYPT_BN_ERR_NO_SQUARE_ROOT, /**< The square root cannot be found. */ CRYPT_BN_ERR_MASKCOPY_LEN, /**< Data lengths are inconsistent when data is copied with masks. */ CRYPT_BN_ERR_QUICK_MODDATA, /**< Uses the BN_ModNistEccMul and BN_ModNistEccSqr interfaces, the module data is not supported. */ CRYPT_BN_FLAG_INVALID, /**< Invalid big number flag. */ CRYPT_BN_CONVERT_INPUT_INVALID, /**< Invalid input parameter of big number strings. */ CRYPT_BN_NOT_SUPPORT_EXTENSION, /**< The big number does not support dynamic extension. */ CRYPT_BN_INPUT_INVALID, /**< Invalid external big number input. */ CRYPT_BN_BITS_INVALID, /**< The bits of the big number exceeds the limit. */ CRYPT_BN_ERR_SWAP_LEN, /**< Data lengths are inconsistent when data is swapped with masks. */ CRYPT_RSA_BUFF_LEN_NOT_ENOUGH = 0x01030001, /**< The buffer length is insufficient. */ CRYPT_RSA_NO_KEY_INFO, /**< Lacks valid key information. */ CRYPT_RSA_ERR_KEY_BITS, /**< Incorrect key length. */ CRYPT_RSA_ERR_E_VALUE, /**< The value of parameter e is incorrect. */ CRYPT_RSA_NOR_KEYGEN_FAIL, /**< Key generation failure, it's normal error. */ CRYPT_RSA_NOR_VERIFY_FAIL, /**< Failed to verify the signature. it's normal error. */ CRYPT_RSA_ERR_ENC_BITS, /**< Incorrect length of the encrypted plaintext of the public key. */ CRYPT_RSA_ERR_DEC_BITS, /**< Incorrect length of the decrypted ciphertext of the private key. */ CRYPT_RSA_ERR_PSS_SALT_LEN, /**< Incorrect salt length of the PSS operation. */ CRYPT_RSA_ERR_PSS_SALT_DATA, /**< PSS operation salt data error, failed to compare the salt extracted during signature verification with the user's input. */ CRYPT_RSA_ERR_PKCSV15_SALT_LEN, /**< Incorrect salt length of the PKCSV15 operation. */ CRYPT_RSA_ERR_PKCSV15_SALT_DATA, /**< PKCSV15 salt data error. */ CRYPT_RSA_ERR_INPUT_VALUE, /**< Some special values, which are used as input errors. */ CRYPT_RSA_ERR_MD_ALGID, /**< The hash ID of the input parameter is incorrect when the pkcs1.5 padding mode is set. */ CRYPT_RSA_PAD_NO_SET_ERROR, /**< Padding information is not set when using RSA key for signature verification. */ CRYPT_RSA_CTRL_NOT_SUPPORT_ERROR, /**< The Ctrl type is not supported When RSA is used for Ctrl. */ CRYPT_RSA_SET_SALT_NOT_PSS_ERROR, /**< When the padding type of the key is not pss, and set the salt information, return failure. */ CRYPT_RSA_SET_EMS_PKCSV15_LEN_ERROR,/**< Sets the PKCSV15 padding information, the length of the input data is incorrect and return failure. */ CRYPT_RSA_SET_EMS_PSS_LEN_ERROR, /**< Sets the PSS padding information, the length of the input data is incorrect, and return failure. */ CRYPT_RSA_SET_RSAES_OAEP_LEN_ERROR, /**< Sets the OAEP padding information, the length of the input data is incorrect and return failure. */ CRYPT_RSA_SET_FLAG_LEN_ERROR, /**< The length of the input data is incorrect and return failure When sets the flag. */ CRYPT_RSA_FLAG_NOT_SUPPORT_ERROR, /**< Unsupported flag. */ CRYPT_RSA_ERR_SALT_LEN, /**< Salt length error. */ CRYPT_RSA_ERR_ALGID, /**< The hash ID of the input parameter is incorrect or conflict occurs when sets the signature, signature verification, and padding parameters. */ CRYPT_RSA_ERR_GEN_SALT, /**< An error is returned when salt information fails to be generated during PSS signature. */ CRYPT_RSA_ERR_ENC_INPUT_NOT_ENOUGH, /**< The plaintext length is too short for RSA NO PAD encryption. */ CRYPT_RSA_ERR_DATA_LEN, /**< Incorrect encryption length. */ CRYPT_RSA_ERR_PAD_NUM, /**< Incorrect padding length. */ CRYPT_RSA_PUBKEY_NOT_EQUAL, /**< RSA public keys are not equal. */ CRYPT_RSA_KEYPAIRWISE_CONSISTENCY_FAILURE, /**< RSA pair-wise consistency failure. */ CRYPT_RSA_ERR_BLIND_TYPE, /**< Invalid RSA blinding type. Only RSA-BSSA is currently supported. */ CRYPT_RSA_ERR_NO_BLIND_INFO, /**< RSA blinding information is missing. The blind/unblind operation requires previous blinding parameters. */ CRYPT_RSA_ERR_NO_PUBKEY_INFO, /**< The rsa pub key is missing. */ CRYPT_RSA_PADDING_NOT_SUPPORTED, /**< The specified RSA padding mode is not supported in blinding. */ CRYPT_RSA_ERR_BSSA_PARAM, /**< The param of bssa is not invalid. */ CRYPT_RSA_GET_SALT_LEN_ERROR, /**< The input length of getting salt-len is incorrect. */ CRYPT_RSA_GET_SALT_NOT_PSS_ERROR, /**< When the padding type of the key is not pss, and get the salt len. */ CRYPT_RSA_ERR_PSS_PARAMS, /**< The parameter is error when the padding type of the key is pss. */ CRYPT_RSA_ERR_NO_PRVKEY_INFO, /**< The rsa prv key is missing. */ CRYPT_RSA_ERR_INVALID_PRVKEY, /**< The private key is invalid. */ CRYPT_EAL_BUFF_LEN_NOT_ENOUGH = 0x01040001, /**< Insufficient buffer length. */ CRYPT_EAL_BUFF_LEN_TOO_LONG, /**< Insufficient buffer length. */ CRYPT_EAL_ERR_ALGID, /**< Incorrect algorithm ID. */ CRYPT_EAL_ALG_NOT_SUPPORT, /**< Algorithm not supported, algorithm behavior not supported. */ CRYPT_EAL_ERR_NEW_PARA_FAIL, /**< Failed to generate parameters. */ CRYPT_EAL_ERR_RAND_WORKING, /**< DRBG is in the working state. */ CRYPT_EAL_ERR_RAND_NO_WORKING, /**< DRBG is not working. */ CRYPT_EAL_ERR_METH_NULL_MEMBER, /**< The method variable member is NULL. */ CRYPT_EAL_ERR_GLOBAL_DRBG_NULL, /**< The global DRBG is null. */ CRYPT_EAL_ERR_DRBG_REPEAT_INIT, /**< DRBG is initialized repeatedly. */ CRYPT_EAL_ERR_DRBG_INIT_FAIL, /**< DRBG initialization failure. */ CRYPT_EAL_ERR_STATE, /**< The usage process is incorrect. For example, run the update command without running the init command. For details, see related algorithms. */ CRYPT_EAL_CIPHER_DATA_ERROR, /**< Data error occurs when unpadding the decrypted data. For X923, the last bit is the length of the original data, and the rest data is 0, if this requirement is not met, an error is reported. For pkcs, all padding data is (the length of the padding data - the length of the original data), if this requirement is not met,an error will be reported. For ISO7816, the first bit of padding data is 0x80, and the other bits are 0, if this requirement is not met, an error will be reported. */ CRYPT_EAL_PADDING_NOT_SUPPORT, /**< Unsupported padding. */ CRYPT_EAL_CIPHER_CTRL_ERROR, /**< CRYPT_EAL_CipherCtrl interface unsupported CTRL type. */ CRYPT_EAL_CIPHER_FINAL_WITH_AEAD_ERROR, /**< An error occurs when the final operation is performed on the AEAD algorithm. */ CRYPT_EAL_PKEY_CTRL_ERROR, /**< When the CRYPT_EAL_PkeyCtrl interface performs CTRL, the function is not supported or the input length is incorrect. */ CRYPT_EAL_MAC_CTRL_TYPE_ERROR, /**< When the CRYPT_EAL_PkeyCtrl interface performs CTRL, the function is not supported or the input length is incorrect. */ CRYPT_EAL_PKEY_DUP_ERROR, /**< Pkey context duplicate failure. */ CRYPT_EAL_PKEY_CMP_DIFF_KEY_TYPE, /**< Pkey comparison failure: different algorithm types. */ CRYPT_EAL_ERR_PART_OVERLAP, /**< Some memory overlap. */ CRYPT_EAL_INTO_TYPE_NOT_SUPPORT, /**< The info type is not supported. */ CRYPT_EAL_ALG_ASM_NOT_SUPPORT, /**< Algorithm assembly is not supported. */ CRYPT_EAL_CIPHER_ERR_NEWCTX, CRYPT_EAL_PKEY_CHECK_ERROR, /**< Pkey check failure. */ CRYPT_EAL_MD_METH_NULL, CRYPT_SHA2_INPUT_OVERFLOW = 0x01050001, /**< The length of the input data exceeds the maximum processing range of SHA2. */ CRYPT_SHA2_OUT_BUFF_LEN_NOT_ENOUGH, /**< The length of the buffer that storing the output result is insufficient. */ CRYPT_DRBG_ERR_STATE = 0x01060001, /**< DRBG status error. */ CRYPT_DRBG_FAIL_GET_ENTROPY, /**< Failed to obtain the entropy. */ CRYPT_DRBG_FAIL_GET_NONCE, /**< Failed to obtain the nonce. */ CRYPT_DRBG_ALG_NOT_SUPPORT, /**< Does not support the given algorithm. */ CRYPT_DRBG_INVALID_LEN, /**< Incorrect data length. */ CRYPT_DRBG_PARAM_ERROR, /**< Incorrect input parameter. */ CRYPT_CURVE25519_NO_PUBKEY = 0x01080001, /**< No public key. */ CRYPT_CURVE25519_NO_PRVKEY, /**< No private key. */ CRYPT_CURVE25519_KEYLEN_ERROR, /**< Incorrect key length. */ CRYPT_CURVE25519_SIGNLEN_ERROR, /**< Incorrect signature length. */ CRYPT_CURVE25519_HASH_METH_ERROR, /**< Hash method is not SHA512. */ CRYPT_CURVE25519_VERIFY_FAIL, /**< Signature verification fails due to incorrect signature. */ CRYPT_CURVE25519_NO_HASH_METHOD, /**< Hash method not set. */ CRYPT_CURVE25519_UNSUPPORTED_CTRL_OPTION, /**< Unsupported mode of operation. */ CRYPT_CURVE25519_KEY_COMPUTE_FAILED, /**< Failed to generate the shared key. */ CRYPT_CURVE25519_INVALID_PUBKEY, /**< Invalid public key. */ CRYPT_CURVE25519_PUBKEY_NOT_EQUAL, /**< Public keys are not equal. */ CRYPT_CURVE25519_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_CURVE25519_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_SHA1_INPUT_OVERFLOW = 0x01090001, /**< The length of the input data exceeds the maximum processing range of SHA1. */ CRYPT_SHA1_OUT_BUFF_LEN_NOT_ENOUGH, /**< The length of the buffer that storing the output result is insufficient. */ CRYPT_ENTROPY_RCT_FAILURE = 0x010A0001, /**< RCT detection fails, restart the entropy source. */ CRYPT_ENTROPY_APT_FAILURE, /**< APT detection fails, restart the entropy source. */ CRYPT_ENTROPY_CONDITION_FAILURE, /**< Processing method error after invoking. */ CRYPT_ENTROPY_RANGE_ERROR, /**< Entropy source generation range error */ CRYPT_ENTROPY_ECF_ALG_ERROR, /**< Entropy source conditioning algorithm is incorrect. */ CRYPT_ENTROPY_ECF_IS_ERROR, /**< Entropy source conditioning is incorrect. */ CRYPT_ENTROPY_ES_CREATE_ERROR, /**< Entropy pool creation error. */ CRYPT_ENTROPY_ES_STATE_ERROR, /**< Incorrect entropy pool status. */ CRYPT_ENTROPY_ES_CTRL_ERROR, /**< Incorrect entropy pool settings. */ CRYPT_ENTROPY_ES_NO_NS, /**< No available noise source in the entropy pool. */ CRYPT_ENTROPY_ES_NS_NOT_FOUND, /**< Noise source not found. */ CRYPT_ENTROPY_ES_DUP_NS, /**< Noise source Repetition. */ CRYPT_ENTROPY_ES_NS_NOT_AVA, /**< Noise source not available. */ CRYPT_ENTROPY_ES_NS_FULL, /**< Noise source list is full. */ CRYPT_ENTROPY_ES_CF_NOT_SUPPORT, /**< Nonditioning function not supported. */ CRYPT_ENTROPY_ES_CF_ERROR, /**< Nonditioning function error. */ CRYPT_ENTROPY_ES_ENTROPY_NOT_ENOUGH, /**< Not getting enough entropy. */ CRYPT_ENTROPY_ES_POOL_ERROR, /**< Entropy pool error. */ CRYPT_ENTROPY_ES_POOL_INSUFFICIENT, /**< Entropy pool capacity is insufficient. */ CRYPT_ENTROPY_CTRL_INVALID_PARAM, /**< Entropy invalid parameter. */ CRYPT_DSA_BUFF_LEN_NOT_ENOUGH = 0x010B0001, /**< Insufficient buffer length. */ CRYPT_DSA_ERR_KEY_PARA, /**< Incorrect key parameter data. */ CRYPT_DSA_ERR_KEY_INFO, /**< Incorrect key information. */ CRYPT_DSA_VERIFY_FAIL, /**< Verification failure. */ CRYPT_DSA_ERR_TRY_CNT, /**< Key generation and signature fail to be generated within the specified number of attempts. */ CRYPT_DSA_DECODE_FAIL, /**< Data decoding fails, the data does not meet the decoding requirements. */ CRYPT_DSA_UNSUPPORTED_CTRL_OPTION, /**< Unsupported mode of operation. */ CRYPT_DSA_PARA_ERROR, /**< The value of the key parameter does not meet the requirements. The ctx command does not contain necessary parameter information. */ CRYPT_DSA_PUBKEY_NOT_EQUAL, /**< Public keys are not equal. */ CRYPT_DSA_PARA_NOT_EQUAL, /**< Key parameters are not equal. */ CRYPT_DSA_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_DSA_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_HMAC_OUT_BUFF_LEN_NOT_ENOUGH = 0x010C0001, /**< The length of the buffer that storing the output result is insufficient. */ CRYPT_HMAC_ERR_UNSUPPORTED_CTRL_OPTION, /**< Unsupport the control type. */ CRYPT_HMAC_ERR_NO_MD_LIB_CTX, /**< MD library context not set. */ CRYPT_HMAC_PARAM_ERROR, /**< Incorrect input parameter. */ CRYPT_DH_BUFF_LEN_NOT_ENOUGH = 0x010D0001, /**< The buffer length is insufficient. */ CRYPT_DH_PARA_ERROR, /**< The value of the key parameter does not meet the requirements, the ctx command does not contain necessary parameter information. */ CRYPT_DH_KEYINFO_ERROR, /**< The value of the public and private keys do not meet the requirements, the ctx does not contain the necessary public and private keys. */ CRYPT_DH_RAND_GENERATE_ERROR, /**< Key generation fails within the specified number of attempts. */ CRYPT_DH_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_DH_UNSUPPORTED_CTRL_OPTION, /**< Unsupported mode of operation. */ CRYPT_DH_CREATE_PARA_FAIL, /**< Failed to create the p, q, and g parameters of the DH algorithm. */ CRYPT_DH_PUBKEY_NOT_EQUAL, /**< Public keys are not equal. */ CRYPT_DH_PARA_NOT_EQUAL, /**< DH key parameters are not equal. */ CRYPT_DH_SET_FLAG_LEN_ERROR, /**< The length of the input data is incorrect and return failure when setting the flag. */ CRYPT_DH_FLAG_NOT_SUPPORT_ERROR, /**< Unsupported flag. */ CRYPT_DH_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_CHACHA20_KEYLEN_ERROR = 0x010E0001, /**< The key length input is incorrect during key setting. */ CRYPT_CHACHA20_NONCELEN_ERROR, /**< The length of the input nounce is incorrect when you set the nounce. */ CRYPT_CHACHA20_COUNTLEN_ERROR, /**< The length of the input count is incorrect when you set the count. */ CRYPT_CHACHA20_NO_KEYINFO, /**< Lack of valid key information during encryption and decryption. */ CRYPT_CHACHA20_NO_NONCEINFO, /**< Lack of valid nounce information during encryption and decryption. */ CRYPT_CHACHA20_CTRLTYPE_ERROR, /**< The input type is not supported when the ctrl interface is used. */ CRYPT_AES_ERR_KEYLEN = 0x010F0001, /**< Incorrect key length. */ CRYPT_MODES_TAGLEN_ERROR = 0x01100001, /**< In AEAD mode, the length of the TAG is incorrect when the tag is obtained and verified. */ CRYPT_MODES_IVLEN_ERROR, /**< The length of the input IV is incorrect when setting the IV. */ CRYPT_MODES_KEYUSE_TOOMANY_TIME, /**< In GCM mode, the number of times that a key can be used for encryption and decryption is limited. When the number of times that a key is used exceeds the limit, an error is reported. */ CRYPT_MODES_CRYPTLEN_OVERFLOW, /**< In AEAD mode, the length of the plaintext or ciphertext input for a single encryption exceeds the limit. */ CRYPT_MODES_CTRL_TAGLEN_ERROR, /**< In GCM or CCM mode, the length of the input parameter or the length of the input parameter data is incorrect when the ctrl interface is used to set the tag length. */ CRYPT_MODES_AAD_REPEAT_SET_ERROR, /**< In the AEAD mode, the AAD information is set repeatedly. */ CRYPT_MODE_BUFF_LEN_NOT_ENOUGH, /**< The buffer length is insufficient. */ CRYPT_MODE_ERR_INPUT_LEN, /**< The function input length is not the expected length. */ CRYPT_MODES_CTRL_TYPE_ERROR, /**< The input type is not supported when the ctrl interface is used. */ CRYPT_MODES_AAD_IS_SET_ERROR, /**< In ccm mode, an error is returned when the tagLen and msgLen are set after the aad is set. */ CRYPT_MODES_MSGLEN_OVERFLOW, /**< In ccm mode, the length of the input message during encryption and decryption exceeds the set msgLen. */ CRYPT_MODES_CTRL_MSGLEN_ERROR, /**< In ccm mode, When the ctrl interface is used to set the msg length, the input parameter length or the input parameter data length is incorrect. (This specification is affected by ivLen.) */ CRYPT_MODES_MSGLEN_LEFT_ERROR, /**< In ccm mode, when the ctrl interface is used to obtain the tag, the length of the encrypted and decrypted messages does not reach the configured number. As a result, an error occurs. */ CRYPT_MODES_ERR_KEYLEN, /**< Incorrect key length set. */ CRYPT_MODES_ERR_KEY, /**< Incorrect key set. */ CRYPT_MODES_ERR_FEEDBACKSIZE, /**< The operation are not support by the algorithm on which the pattern depends on. */ CRYPT_MODES_METHODS_NOT_SUPPORT, /**< Mode depends does not support the behavior. */ CRYPT_MODES_FEEDBACKSIZE_NOT_SUPPORT, /**< The algorithm does not support the setting of feedbacksize. */ CRYPT_MODES_PADDING_NOT_SUPPORT, /**< Unsupported padding. */ CRYPT_HKDF_DKLEN_OVERFLOW = 0x01110001, /**< The length of the derived key exceeds the maximum. */ CRYPT_HKDF_NOT_SUPPORTED, /**< Unsupport HKDF algorithm. */ CRYPT_HKDF_PARAM_ERROR, /**< Incorrect input parameter. */ CRYPT_HKDF_ERR_MAC_ID_NOT_SET, /**< Mac id not set. */ CRYPT_HKDF_ERR_MAC_METH, /**< Mac method err. */ CRYPT_CMAC_OUT_BUFF_LEN_NOT_ENOUGH = 0x01120001, /**< The length of the buffer that storing the output result is insufficient. */ CRYPT_CMAC_INPUT_OVERFLOW, /**< The input length exceeds the limit. As a result, the integer type is reversed. */ CRYPT_CMAC_ERR_UNSUPPORTED_CTRL_OPTION, /**< Unsupport the control type. */ CRYPT_GMAC_ERR_UNSUPPORTED_CTRL_OPTION, /**< Unsupport the control type. */ CRYPT_SCRYPT_PARAM_ERROR = 0x01130001, /**< Incorrect input parameter. */ CRYPT_SCRYPT_NOT_SUPPORTED, /**< Unsupport the SCRYPT algorithm. */ CRYPT_SCRYPT_DATA_TOO_MAX, /**< The data calculated by the SCRYPT algorithm is too large. */ CRYPT_PBKDF2_PARAM_ERROR = 0x01150001, /**< Incorrect input parameter. */ CRYPT_PBKDF2_NOT_SUPPORTED, /**< Does not support the PBKDF2 algorithm. */ CRYPT_PBKDF2_ERR_MAC_METH, /**< Mac method err. */ CRYPT_PBKDF2_ERR_MAC_ID_NOT_SET, /**< Mac id not set. */ CRYPT_ECC_POINT_AT_INFINITY = 0x01160001, /**< Point at infinity. */ CRYPT_ECC_POINT_NOT_ON_CURVE, /**< Point is not on the curve. */ CRYPT_ECC_POINT_ERR_CURVE_ID, /**< Curve ID is inconsistent or incorrect. */ CRYPT_ECC_POINT_WINDOW_TOO_MAX, /**< Window is too max. */ CRYPT_ECC_POINT_NOT_EQUAL, /**< The two points are not equal. */ CRYPT_ECC_POINT_BLIND_WITH_ZERO, /**< The random number generated during point salting is 0. */ CRYPT_ECC_POINT_NOT_AFFINE, /**< Point is not affine coordinates. */ CRYPT_ECC_NOT_SUPPORT, /**< This function is not supported. */ CRYPT_ECC_POINT_MUL_ERR_K_LEN, /** The scalar length exceeds the curve specification when using the dot multiplication function */ CRYPT_ECC_BUFF_LEN_NOT_ENOUGH, /**< Insufficient buffer length. */ CRYPT_ECC_ERR_POINT_FORMAT, /**< The encoding format input during point encoding is incorrect. */ CRYPT_ECC_ERR_POINT_CODE, /**< Incorrect point code information. */ CRYPT_ECC_PKEY_ERR_UNSUPPORTED_CTRL_OPTION, /**< Unsupport the control type. */ CRYPT_ECC_PKEY_ERR_EMPTY_KEY, /**< Key is null. */ CRYPT_ECC_PKEY_ERR_INVALID_POINT_FORMAT, /**< Invalid dot format. */ CRYPT_ECC_PKEY_ERR_CTRL_LEN, /**< Control input parameter is incorrect. */ CRYPT_ECC_PKEY_ERR_INVALID_PRIVATE_KEY, /**< Invalid private key. */ CRYPT_ECC_PKEY_ERR_INVALID_PUBLIC_KEY, /**< Invalid public key. */ CRYPT_ECC_PKEY_ERR_TRY_CNT, /**< Key generation or generater signature fail within the specified number of attempts. */ CRYPT_ECC_PKEY_ERR_SIGN_LEN, /**< Invalid sign length */ CRYPT_ECC_ERR_PARA, /**< Incorrect curve parameter. */ CRYPT_ECC_INVERSE_INPUT_ZERO, /** Modulo inverse input is 0. */ CRYPT_ECC_KEY_PUBKEY_NOT_EQUAL, /**< ECC public keys are not equal. */ CRYPT_ECC_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_ECC_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_SHA3_OUT_BUFF_LEN_NOT_ENOUGH = 0x01170001, /**< Insufficient buffer length for storing output results. */ CRYPT_SHA3_INVALID_STATE, /**< Invalid state. */ CRYPT_ECDH_ERR_UNSUPPORT_CURVE_TYPE = 0x01180001, /**< Unsupported curve type. */ CRYPT_ECDH_ERR_EMPTY_KEY, /**< Key is null. */ CRYPT_ECDH_ERR_INVALID_COFACTOR, /**< Invalid cofactor value. */ CRYPT_ECDH_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_ECDH_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_ECDSA_ERR_EMPTY_KEY = 0x01190001, /**< Key is NULL. */ CRYPT_ECDSA_ERR_TRY_CNT, /**< Key generation and generate signature fail within the specified number of attempts. */ CRYPT_ECDSA_VERIFY_FAIL, /**< Verification failure. */ CRYPT_ECDSA_ERR_UNSUPPORTED_CTRL_OPTION, /**< Unsupport the control type. */ CRYPT_ECDSA_BUFF_LEN_NOT_ENOUGH, /**< BUFF insufficient length. */ CRYPT_ECDSA_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_ECDSA_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_SM3_INPUT_OVERFLOW = 0x011A0001, /**< The length of the input data exceeds the maximum processing range of the SM3. */ CRYPT_SM3_OUT_BUFF_LEN_NOT_ENOUGH, /**< The length of the buffer that storing the output result is insufficient. */ CRYPT_SM4_ERR_IV_LEN = 0x011B0001, /**< Wrong key length set. */ CRYPT_SM4_ERR_MSG_LEN, /**< Wrong data length is set. */ CRYPT_SM4_ERR_KEY_LEN, /**< Wrong key length is set. */ CRYPT_SM4_UNSAFE_KEY, /**< DataKey is the same as tweakKey. */ CRYPT_MD5_INPUT_OVERFLOW = 0x011D0001, /**< The length of the input data exceeds the maximum processing range of the MD5. */ CRYPT_MD5_OUT_BUFF_LEN_NOT_ENOUGH, /**< The length of the buffer that storing the output result is insufficient. */ CRYPT_MD_ERR_NEWCTX, /**< create md ctx failed. */ CRYPT_SM2_BUFF_LEN_NOT_ENOUGH = 0x01200001, /**< Insufficient buffer length. */ CRYPT_SM2_NO_PUBKEY, /**< SM2 the public key is not set. */ CRYPT_SM2_NO_PRVKEY, /**< SM2 The private key is not set. */ CRYPT_SM2_ERR_EMPTY_KEY, /**< SM2 key is null. */ CRYPT_SM2_ERR_TRY_CNT, /**< Key generation and generate signature fail within the specified number of attempts. */ CRYPT_SM2_VERIFY_FAIL, /**< verification failure. */ CRYPT_SM2_ERR_UNSUPPORTED_CTRL_OPTION, /**< Unsupported control type. */ CRYPT_SM2_ERR_NO_HASH_METHOD, /**< No hash method information. */ CRYPT_SM2_USERID_NOT_SET, /**< Unset userID. */ CRYPT_SM2_R_NOT_SET, /**< The peer R value is not set. */ CRYPT_SM2_INVALID_SERVER_TYPE, /**< The user is neither the initiator nor the recipient. */ CRYPT_SM2_ERR_CTRL_LEN, /**< Incorrect ctrl length. */ CRYPT_SM2_DECRYPT_FAIL, /**< Decryption failure. */ CRYPT_SM2_ERR_DATA_LEN, /**< Incorrect data length. */ CRYPT_SM2_ERR_GET_S, /**< Failed to obtain the checksum. */ CRYPT_SM2_ERR_S_NOT_SET, /**< Unset checksum. */ CRYPT_SM2_EXCH_VERIFY_FAIL, /**< Key Negotiation Failure. */ CRYPT_SM2_DECODE_FAIL, /**< Data decoding fails, the data does not meet the decoding requirements. */ CRYPT_SM2_ID_TOO_LARGE, /**< User id to large. */ CRYPT_SM2_K_REPEAT_SET_ERROR, /**< the random k is set repeatedly*/ CRYPT_SM2_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_SM2_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_KDFTLS12_NOT_SUPPORTED = 0x01210001, /**< Unsupport the KDFTLS12 algorithm. */ CRYPT_KDFTLS12_PARAM_ERROR, /**< Incorrect input parameter. */ CRYPT_KDFTLS12_ERR_MAC_METH, /**< Mac method err. */ CRYPT_KDFTLS12_ERR_MAC_ID_NOT_SET, /**< Mac id not set. */ CRYPT_SIPHASH_OUT_BUFF_LEN_NOT_ENOUGH = 0x01220001, /**< The buffer size for storing the output result is insufficient. */ CRYPT_SIPHASH_INPUT_OVERFLOW, CRYPT_SIPHASH_ERR_UNSUPPORTED_CTRL_OPTION, /**< Unsupport the control type. */ CRYPT_CBC_MAC_ERR_CTRL_LEN = 0x01240001, CRYPT_CBC_MAC_ERR_UNSUPPORTED_CTRL_OPTION, CRYPT_CBC_MAC_PADDING_NOT_SET, CRYPT_CBC_MAC_PADDING_NOT_SUPPORT, CRYPT_CBC_MAC_OUT_BUFF_LEN_NOT_ENOUGH, CRYPT_SEED_POOL_NEW_ERROR = 0x01290001, /**< The length of the key input is incorrect when setting the key. */ CRYPT_SEED_POOL_STATE_ERROR, /**< Incorrect seed pool status. */ CRYPT_SEED_POOL_ES_LIST_FULL, /**< The number of entropy sources exceeds the upper limit. */ CRYPT_SEED_POOL_NO_SUFFICIENT_ENTROPY, /**< The seed pool cannot provide sufficient entropy. */ CRYPT_SEED_POOL_NO_ENTROPY_SOURCE, /**< The seed pool has no entropy source. */ CRYPT_SEED_POOL_NO_ENTROPY_OBTAINED, /**< No entropy data is obtained from the seed pool. */ CRYPT_SEED_POOL_NOT_MEET_REQUIREMENT, /**< The entropy data does not meet the requirements. */ CRYPT_ENTROPY_CTX_CREATE_FAILED, /**< Failed to create the handle for obtaining the entropy. */ CRYPT_MLKEM_KEYLEN_ERROR = 0x01300001, /**< Incorrect input data length. */ CRYPT_MLKEM_LEN_NOT_ENOUGH, /**<The buffer size of output is insufficient. */ CRYPT_MLKEM_KEY_NOT_SET, /**<The encaps or decaps key not set. */ CRYPT_MLKEM_KEYINFO_NOT_SET, /**<The algorithm not set. */ CRYPT_MLKEM_KEY_NOT_EQUAL, /**< The MLKEM keys are not equal. */ CRYPT_MLKEM_CTRL_NOT_SUPPORT, /**< The Ctrl type is not supported.*/ CRYPT_MLKEM_CTRL_INIT_REPEATED, /**< The CTX cannot be initialized repeatedly.*/ CRYPT_MLKEM_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_MLKEM_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_HPKE_ERR_GEN_ASYM_KEY = 0x01310001, /**< HPKE Generate asymmetric key error. */ CRYPT_HPKE_ERR_AEAD_TAG, /**< Failed to verify AEAD tag when decrypt. */ CRYPT_HPKE_ERR_CALL, /**< It is not appropriate to call this function. */ CRYPT_HPKE_FAILED_FETCH_CIPHER, /**< Failed to fetch cipher. */ CRYPT_HPKE_FAILED_FETCH_PKEY, /**< Failed to fetch pkey. */ CRYPT_HPKE_FAILED_FETCH_KDF, /**< Failed to fetch kdf. */ CRYPT_DECODE_ASN1_BUFF_NUM_NOT_ENOUGH = 0x01320001, /**< The input number of BSL_ANS1_Buffer is not enough. */ CRYPT_DECODE_UNSUPPORTED_PUBKEY_TYPE, /**< Unsupported pubkey type */ CRYPT_DECODE_UNSUPPORTED_PKCS8_TYPE, /**< Unsupported pkcs8 type */ CRYPT_DECODE_PKCS8_INVALID_ALGO_PARAM, /**< pkcs8 has no valid algorithm parameters */ CRYPT_DECODE_UNKNOWN_OID, /**< Unknown OID */ CRYPT_DECODE_ASN1_BUFF_FAILED, /**< decode asn1 buffer failed. */ CRYPT_DECODE_NO_SUPPORT_TYPE, /**< decode no support key type. */ CRYPT_DECODE_NO_SUPPORT_FORMAT, /**< decode no support key format. */ CRYPT_DECODE_PKCS8_INVALID_ITER, /**< pkcs8 invalid iter num */ CRYPT_DECODE_PKCS8_INVALID_KEYLEN, /**< pkcs8 invalid keylen */ CRYPT_DECODE_ERR_RSSPSS_GET_ANY_TAG, /**< decode rsapss param failed. */ CRYPT_DECODE_ERR_RSSPSS, /**< decode rsapss param failed. */ CRYPT_DECODE_ERR_RSSPSS_MD, /**< rsapss md is invalid. */ CRYPT_DECODE_ERR_RSSPSS_MGF1MD, /**< rsapss mgf1md is invalid. */ CRYPT_DECODE_ERR_RSSPSS_TRAILER, /**< rsapss trailer field is invalid. */ CRYPT_DECODE_PKCS7_INVALIDE_ENCRYPTDATA_TYPE, /**< Invaild pkcs7-encryptedData. */ CRYPT_DECODE_UNSUPPORTED_PKCS7_TYPE, /**< Unsupported pkcs7 type */ CRYPT_DECODE_UNSUPPORTED_ENCRYPT_TYPE, /**< Unsupported encrypt type */ CRYPT_DECODE_BUFF_NOT_ENOUGH, /**< The input buffer space is not enough */ CRYPT_DECODE_ASN1_BUFF_LEN_ZERO, /**< The decoding length of asn1 buffer is zero. */ CRYPT_DECODE_ERR_NO_DECODER, /**< No decoder found. */ CRYPT_DECODE_ERR_NO_USABLE_DECODER, /**< No decoder found. */ CRYPT_DECODE_RETRY, /**< Retry decode. */ CRYPT_DECODE_ERR_CURR_NODE_NOT_FOUND, /**< Current node not found. */ CRYPT_DECODE_ERR_NO_KEY_TYPE, /**< No key type found. */ CRYPT_DECODE_ERR_KEY_TYPE_NOT_MATCH, /**< Key type not match. */ CRYPT_ENCODE_NO_SUPPORT_TYPE = 0x01330001, /**< encode no support key type. */ CRYPT_ENCODE_NO_SUPPORT_FORMAT, /**< encode no support key format. */ CRYPT_ENCODE_ERR_RSA_PAD, /**< rsa pad err. */ CRYPT_ENCODE_BUFF_NOT_ENOUGH, /**< The input buffer space is not enough */ CRYPT_ENCODE_ERR_SIGN_LEN_OVERFLOW, /**< The r and s length is too large. */ CRYPT_ENCODE_ERR_SM2_ENCRYPT_DATA_LEN_OVERFLOW, /**< The sm2 encrypt data length is too large. */ CRYPT_DECODE_PRINT_UNSUPPORT_ALG = 0x01340001, /**< Failed to print unsupported alg. */ CRYPT_DECODE_PRINT_NO_KEY, /**< Failed to print key. */ CRYPT_DECODE_PRINT_KEYBITS, /**< Failed to print key bist. */ CRYPT_DECODE_PRINT_MODULUS, /**< Failed to print modulus. */ CRYPT_DECODE_PRINT_EXPONENT, /**< Failed to print exponent. */ CRYPT_DECODE_PRINT_RSAPSS_PARA, /**< Failed to print rsapss para. */ CRYPT_DECODE_PRINT_ECC_PUB, /**< Failed to print ecc pubkey. */ CRYPT_DECODE_PRINT_ECC_OID, /**< Failed to print ecc oid. */ CRYPT_PROVIDER_ERR_UNEXPECTED_IMPL = 0x01350001, /**< Unexpected impl */ CRYPT_PROVIDER_ERR_IMPL_NULL, CRYPT_PROVIDER_NOT_FOUND, /**< Provider not found. */ CRYPT_PROVIDER_NOT_SUPPORT, CRYPT_PROVIDER_ERR_ATTRIBUTE, CRYPT_PROVIDER_INVALID_LIB_CTX, CRYPT_MLDSA_KEYINFO_NOT_SET = 0x01360001, /**< The algorithm not set. */ CRYPT_MLDSA_CTRL_NOT_SUPPORT, /**< The Ctrl type is not supported. */ CRYPT_MLDSA_PAD_TOO_LONG, /**< The pad is too long. */ CRYPT_MLDSA_KEYLEN_ERROR, /**< Incorrect input data length. */ CRYPT_MLDSA_SIGN_DATA_ERROR, /**< Invalid signature value. */ CRYPT_MLDSA_VERIFY_FAIL, /**< Failed to verify the signature. */ CRYPT_MLDSA_KEY_NOT_SET, /**< The public key or private not set. */ CRYPT_MLDSA_LEN_NOT_ENOUGH, /**< The buffer size of output is insufficient. */ CRYPT_MLDSA_KEY_NOT_EQUAL, /**< The MLDSA keys are not equal. */ CRYPT_MLDSA_CTRL_INIT_REPEATED, /**< The CTX cannot be initialized repeatedly.*/ CRYPT_MLDSA_SET_KEY_FAILED, /**< Failed to set the key. */ CRYPT_MLDSA_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_MLDSA_INVALID_PRVKEY, /**< Invalid private key. */ CRYPT_MLDSA_INVALID_PUBKEY, /**< Invalid public key. */ CRYPT_ELGAMAL_BUFF_LEN_NOT_ENOUGH = 0x01370001, /**< The buffer length is insufficient. */ CRYPT_ELGAMAL_NO_KEY_INFO, /**< Lacks valid key information. */ CRYPT_ELGAMAL_ERR_KEY_BITS, /**< Incorrect key length. */ CRYPT_ELGAMAL_ERR_ENC_BITS, /**< Incorrect length of the encrypted plaintext of the public key. */ CRYPT_ELGAMAL_ERR_DEC_BITS, /**< Incorrect length of the decrypted ciphertext of the private key. */ CRYPT_ELGAMAL_ERR_KEY_KBITS, /**< Incorrect key length. */ CRYPT_ELGAMAL_ERR_KEY_BITS_KBITS, /**< Incorrect key length. */ CRYPT_ELGAMAL_ERR_ENC_KBITS, /**< Incorrect length of the encrypted plaintext of the public key. */ CRYPT_ELGAMAL_ERR_DEC_KBITS, /**< Incorrect length of the decrypted ciphertext of the private key. */ CRYPT_ELGAMAL_ERR_INPUT_VALUE, /**< Some special values, which are used as input errors. */ CRYPT_ELGAMAL_CTRL_NOT_SUPPORT_ERROR, /**< The Ctrl type is not supported When elgamal is used for Ctrl. */ CRYPT_SLHDSA_ERR_INVALID_ALGID = 0x01380001, /**< The algorithm id is invalid. */ CRYPT_SLHDSA_ERR_INVALID_SIG_LEN, /**< The signature length is invalid. */ CRYPT_SLHDSA_ERR_INVALID_KEYLEN, /**< The key length is invalid. */ CRYPT_SLHDSA_ERR_SIG_LEN_NOT_ENOUGH, /**< The signature length is not enough. */ CRYPT_SLHDSA_ERR_HYPERTREE_VERIFY_FAIL, /**< Hypertree verify failed. */ CRYPT_SLHDSA_ERR_PREHASH_ID_NOT_SUPPORTED, /**< Prehash id is not supported. */ CRYPT_SLHDSA_ERR_CONTEXT_LEN_OVERFLOW, /**< Context length is overflow. */ CRYPT_SLHDSA_PAIRWISE_CHECK_FAIL, /**< The public and private keys are inconsistent. */ CRYPT_SLHDSA_ERR_NO_PUBKEY, /**< No public key. */ CRYPT_SLHDSA_ERR_NO_PRVKEY, /**< No private key. */ CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH = 0x01390001, /**< The buffer length is insufficient. */ CRYPT_PAILLIER_NO_KEY_INFO, /**< Lacks valid key information. */ CRYPT_PAILLIER_ERR_KEY_BITS, /**< Incorrect key length. */ CRYPT_PAILLIER_ERR_ENC_BITS, /**< Incorrect length of the encrypted plaintext of the public key. */ CRYPT_PAILLIER_ERR_DEC_BITS, /**< Incorrect length of the decrypted ciphertext of the private key. */ CRYPT_PAILLIER_ERR_INPUT_VALUE, /**< Some special values, which are used as input errors. */ CRYPT_PAILLIER_CTRL_NOT_SUPPORT_ERROR, /**< The Ctrl type is not supported When paillier is used for Ctrl. */ CRYPT_XMSS_ERR_INVALID_ALGID = 0x013A0001, /**< The algorithm id is invalid. */ CRYPT_XMSS_ERR_INVALID_SIG_LEN, /**< The signature length is invalid. */ CRYPT_XMSS_ERR_INVALID_KEYLEN, /**< The key length is invalid. */ CRYPT_XMSS_ERR_KEY_EXPIRED, /**< The key has expired. */ CRYPT_CMVP_COMMON_ERR = 0x013B0001, /**< Common error in CMVP selftest. */ CRYPT_CMVP_ERR_INTEGRITY, /**< Integrity error in CMVP selftest. */ CRYPT_CMVP_RANDOMNESS_ERR, /**< Randomness error in CMVP selftest. */ CRYPT_CMVP_ERR_ALGO_SELFTEST, /**< Algorithm selftest error in CMVP selftest. */ CRYPT_CMVP_ERR_PAIRWISETEST, /**< Pairwise test error in CMVP selftest. */ CRYPT_CMVP_ERR_PARAM_CHECK, /**< Parameter check error in CMVP selftest. */ }; #ifdef __cplusplus } #endif #endif // CRYPT_ERRNO_H
2301_79861745/bench_create
include/crypto/crypt_errno.h
C
unknown
46,806
/* * 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_PARAMS_KEY_H #define CRYPT_PARAMS_KEY_H #include <stdint.h> #ifdef __cplusplus extern "C" { #endif #define CRYPT_PARAM_PKEY_BASE 0 #define CRYPT_PARAM_PKEY_ENCODE_PUBKEY (CRYPT_PARAM_PKEY_BASE + 1) #define CRYPT_PARAM_PKEY_PROCESS_FUNC (CRYPT_PARAM_PKEY_BASE + 2) #define CRYPT_PARAM_PKEY_PROCESS_ARGS (CRYPT_PARAM_PKEY_BASE + 3) #define CRYPT_PARAM_PKEY_HE_CIPHERTEXT1 (CRYPT_PARAM_PKEY_BASE + 4) #define CRYPT_PARAM_PKEY_HE_CIPHERTEXT2 (CRYPT_PARAM_PKEY_BASE + 5) #define CRYPT_PARAM_KDF_BASE 100 #define CRYPT_PARAM_KDF_PASSWORD (CRYPT_PARAM_KDF_BASE + 1) #define CRYPT_PARAM_KDF_MAC_ID (CRYPT_PARAM_KDF_BASE + 2) #define CRYPT_PARAM_KDF_SALT (CRYPT_PARAM_KDF_BASE + 3) #define CRYPT_PARAM_KDF_ITER (CRYPT_PARAM_KDF_BASE + 4) #define CRYPT_PARAM_KDF_MODE (CRYPT_PARAM_KDF_BASE + 5) #define CRYPT_PARAM_KDF_KEY (CRYPT_PARAM_KDF_BASE + 6) #define CRYPT_PARAM_KDF_PRK (CRYPT_PARAM_KDF_BASE + 7) #define CRYPT_PARAM_KDF_INFO (CRYPT_PARAM_KDF_BASE + 8) #define CRYPT_PARAM_KDF_EXLEN (CRYPT_PARAM_KDF_BASE + 9) #define CRYPT_PARAM_KDF_LABEL (CRYPT_PARAM_KDF_BASE + 11) #define CRYPT_PARAM_KDF_SEED (CRYPT_PARAM_KDF_BASE + 12) #define CRYPT_PARAM_KDF_N (CRYPT_PARAM_KDF_BASE + 13) #define CRYPT_PARAM_KDF_P (CRYPT_PARAM_KDF_BASE + 14) #define CRYPT_PARAM_KDF_R (CRYPT_PARAM_KDF_BASE + 15) #define CRYPT_PARAM_EC_BASE 200 #define CRYPT_PARAM_EC_PUBKEY (CRYPT_PARAM_EC_BASE + 1) #define CRYPT_PARAM_EC_PRVKEY (CRYPT_PARAM_EC_BASE + 2) #define CRYPT_PARAM_EC_P (CRYPT_PARAM_EC_BASE + 3) #define CRYPT_PARAM_EC_A (CRYPT_PARAM_EC_BASE + 4) #define CRYPT_PARAM_EC_B (CRYPT_PARAM_EC_BASE + 5) #define CRYPT_PARAM_EC_N (CRYPT_PARAM_EC_BASE + 6) #define CRYPT_PARAM_EC_H (CRYPT_PARAM_EC_BASE + 7) #define CRYPT_PARAM_EC_X (CRYPT_PARAM_EC_BASE + 8) #define CRYPT_PARAM_EC_Y (CRYPT_PARAM_EC_BASE + 9) #define CRYPT_PARAM_EC_CURVE_ID (CRYPT_PARAM_EC_BASE + 10) #define CRYPT_PARAM_DH_BASE 300 #define CRYPT_PARAM_DH_PUBKEY (CRYPT_PARAM_DH_BASE + 1) #define CRYPT_PARAM_DH_PRVKEY (CRYPT_PARAM_DH_BASE + 2) #define CRYPT_PARAM_DH_P (CRYPT_PARAM_DH_BASE + 3) #define CRYPT_PARAM_DH_Q (CRYPT_PARAM_DH_BASE + 4) #define CRYPT_PARAM_DH_G (CRYPT_PARAM_DH_BASE + 5) #define CRYPT_PARAM_DSA_BASE 400 #define CRYPT_PARAM_DSA_PUBKEY (CRYPT_PARAM_DSA_BASE + 1) #define CRYPT_PARAM_DSA_PRVKEY (CRYPT_PARAM_DSA_BASE + 2) #define CRYPT_PARAM_DSA_P (CRYPT_PARAM_DSA_BASE + 3) #define CRYPT_PARAM_DSA_Q (CRYPT_PARAM_DSA_BASE + 4) #define CRYPT_PARAM_DSA_G (CRYPT_PARAM_DSA_BASE + 5) #define CRYPT_PARAM_DSA_ALGID (CRYPT_PARAM_DSA_BASE + 6) #define CRYPT_PARAM_DSA_PBITS (CRYPT_PARAM_DSA_BASE + 7) #define CRYPT_PARAM_DSA_QBITS (CRYPT_PARAM_DSA_BASE + 8) #define CRYPT_PARAM_DSA_SEEDLEN (CRYPT_PARAM_DSA_BASE + 9) #define CRYPT_PARAM_DSA_GINDEX (CRYPT_PARAM_DSA_BASE + 10) #define CRYPT_PARAM_PAILLIER_BASE 500 #define CRYPT_PARAM_PAILLIER_N (CRYPT_PARAM_PAILLIER_BASE + 1) #define CRYPT_PARAM_PAILLIER_G (CRYPT_PARAM_PAILLIER_BASE + 2) #define CRYPT_PARAM_PAILLIER_N2 (CRYPT_PARAM_PAILLIER_BASE + 3) #define CRYPT_PARAM_PAILLIER_LAMBDA (CRYPT_PARAM_PAILLIER_BASE + 4) #define CRYPT_PARAM_PAILLIER_MU (CRYPT_PARAM_PAILLIER_BASE + 5) #define CRYPT_PARAM_PAILLIER_P (CRYPT_PARAM_PAILLIER_BASE + 6) #define CRYPT_PARAM_PAILLIER_Q (CRYPT_PARAM_PAILLIER_BASE + 7) #define CRYPT_PARAM_PAILLIER_BITS (CRYPT_PARAM_PAILLIER_BASE + 8) #define CRYPT_PARAM_RAND_BASE 600 #define CRYPT_PARAM_RAND_SEEDCTX (CRYPT_PARAM_RAND_BASE + 1) #define CRYPT_PARAM_RAND_PR (CRYPT_PARAM_RAND_BASE + 2) #define CRYPT_PARAM_RAND_SEED_GETENTROPY (CRYPT_PARAM_RAND_BASE + 3) #define CRYPT_PARAM_RAND_SEED_CLEANENTROPY (CRYPT_PARAM_RAND_BASE + 4) #define CRYPT_PARAM_RAND_SEED_GETNONCE (CRYPT_PARAM_RAND_BASE + 5) #define CRYPT_PARAM_RAND_SEED_CLEANNONCE (CRYPT_PARAM_RAND_BASE + 6) #define CRYPT_PARAM_RAND_RESEED_INTERVAL (CRYPT_PARAM_RAND_BASE + 7) #define CRYPT_PARAM_RAND_RESEED_TIME (CRYPT_PARAM_RAND_BASE + 8) #define CRYPT_PARAM_CURVE25519_BASE 700 #define CRYPT_PARAM_CURVE25519_PUBKEY (CRYPT_PARAM_CURVE25519_BASE + 1) #define CRYPT_PARAM_CURVE25519_PRVKEY (CRYPT_PARAM_CURVE25519_BASE + 2) #define CRYPT_PARAM_ELGAMAL_BASE 800 #define CRYPT_PARAM_ELGAMAL_P (CRYPT_PARAM_ELGAMAL_BASE + 1) #define CRYPT_PARAM_ELGAMAL_G (CRYPT_PARAM_ELGAMAL_BASE + 2) #define CRYPT_PARAM_ELGAMAL_X (CRYPT_PARAM_ELGAMAL_BASE + 3) #define CRYPT_PARAM_ELGAMAL_Y (CRYPT_PARAM_ELGAMAL_BASE + 4) #define CRYPT_PARAM_ELGAMAL_Q (CRYPT_PARAM_ELGAMAL_BASE + 5) #define CRYPT_PARAM_ELGAMAL_BITS (CRYPT_PARAM_ELGAMAL_BASE + 6) #define CRYPT_PARAM_ELGAMAL_KBITS (CRYPT_PARAM_ELGAMAL_BASE + 7) #define CRYPT_PARAM_RSA_BASE 900 #define CRYPT_PARAM_RSA_N (CRYPT_PARAM_RSA_BASE + 1) #define CRYPT_PARAM_RSA_E (CRYPT_PARAM_RSA_BASE + 2) #define CRYPT_PARAM_RSA_D (CRYPT_PARAM_RSA_BASE + 3) #define CRYPT_PARAM_RSA_P (CRYPT_PARAM_RSA_BASE + 4) #define CRYPT_PARAM_RSA_Q (CRYPT_PARAM_RSA_BASE + 5) #define CRYPT_PARAM_RSA_DQ (CRYPT_PARAM_RSA_BASE + 6) #define CRYPT_PARAM_RSA_DP (CRYPT_PARAM_RSA_BASE + 7) #define CRYPT_PARAM_RSA_QINV (CRYPT_PARAM_RSA_BASE + 8) #define CRYPT_PARAM_RSA_BITS (CRYPT_PARAM_RSA_BASE + 9) #define CRYPT_PARAM_RSA_SALTLEN (CRYPT_PARAM_RSA_BASE + 10) #define CRYPT_PARAM_RSA_MD_ID (CRYPT_PARAM_RSA_BASE + 11) #define CRYPT_PARAM_RSA_MGF1_ID (CRYPT_PARAM_RSA_BASE + 12) #define CRYPT_PARAM_RSA_XP (CRYPT_PARAM_RSA_BASE + 13) #define CRYPT_PARAM_RSA_XQ (CRYPT_PARAM_RSA_BASE + 14) #define CRYPT_PARAM_RSA_XP1 (CRYPT_PARAM_RSA_BASE + 15) #define CRYPT_PARAM_RSA_XP2 (CRYPT_PARAM_RSA_BASE + 16) #define CRYPT_PARAM_RSA_XQ1 (CRYPT_PARAM_RSA_BASE + 17) #define CRYPT_PARAM_RSA_XQ2 (CRYPT_PARAM_RSA_BASE + 18) #define CRYPT_PARAM_ML_KEM_BASE 1400 #define CRYPT_PARAM_ML_KEM_PRVKEY (CRYPT_PARAM_ML_KEM_BASE + 1) #define CRYPT_PARAM_ML_KEM_PUBKEY (CRYPT_PARAM_ML_KEM_BASE + 2) #define CRYPT_PARAM_ML_DSA_BASE 1500 #define CRYPT_PARAM_ML_DSA_PRVKEY (CRYPT_PARAM_ML_DSA_BASE + 1) #define CRYPT_PARAM_ML_DSA_PUBKEY (CRYPT_PARAM_ML_DSA_BASE + 2) #define CRYPT_PARAM_HYBRID_BASE 1600 #define CRYPT_PARAM_HYBRID_PRVKEY (CRYPT_PARAM_HYBRID_BASE + 1) #define CRYPT_PARAM_HYBRID_PUBKEY (CRYPT_PARAM_HYBRID_BASE + 2) #define CRYPT_PARAM_SLH_DSA_BASE 1700 #define CRYPT_PARAM_SLH_DSA_PRV_SEED (CRYPT_PARAM_SLH_DSA_BASE + 1) #define CRYPT_PARAM_SLH_DSA_PRV_PRF (CRYPT_PARAM_SLH_DSA_BASE + 2) #define CRYPT_PARAM_SLH_DSA_PUB_SEED (CRYPT_PARAM_SLH_DSA_BASE + 3) #define CRYPT_PARAM_SLH_DSA_PUB_ROOT (CRYPT_PARAM_SLH_DSA_BASE + 4) #define CRYPT_PARAM_MD_BASE 1800 #define CRYPT_PARAM_MD_DIGEST_SIZE (CRYPT_PARAM_MD_BASE + 1) #define CRYPT_PARAM_MD_BLOCK_SIZE (CRYPT_PARAM_MD_BASE + 2) #define CRYPT_PARAM_MD_ATTR (CRYPT_PARAM_MD_BASE + 3) #define CRYPT_PARAM_XMSS_BASE 1900 #define CRYPT_PARAM_XMSS_PRV_INDEX (CRYPT_PARAM_XMSS_BASE + 1) #define CRYPT_PARAM_XMSS_PRV_SEED (CRYPT_PARAM_XMSS_BASE + 2) #define CRYPT_PARAM_XMSS_PRV_PRF (CRYPT_PARAM_XMSS_BASE + 3) #define CRYPT_PARAM_XMSS_PUB_SEED (CRYPT_PARAM_XMSS_BASE + 4) #define CRYPT_PARAM_XMSS_PUB_ROOT (CRYPT_PARAM_XMSS_BASE + 5) #define CRYPT_PARAM_DECODE_BASE 4000 #define CRYPT_PARAM_DECODE_OUTPUT_FORMAT (CRYPT_PARAM_DECODE_BASE + 1) #define CRYPT_PARAM_DECODE_OUTPUT_TYPE (CRYPT_PARAM_DECODE_BASE + 2) #define CRYPT_PARAM_DECODE_PASSWORD (CRYPT_PARAM_DECODE_BASE + 3) #define CRYPT_PARAM_DECODE_KEY_TYPE (CRYPT_PARAM_DECODE_BASE + 4) #define CRYPT_PARAM_DECODE_BUFFER_DATA (CRYPT_PARAM_DECODE_BASE + 5) #define CRYPT_PARAM_DECODE_OBJECT_DATA (CRYPT_PARAM_DECODE_BASE + 6) #define CRYPT_PARAM_DECODE_OBJECT_TYPE (CRYPT_PARAM_DECODE_BASE + 7) #define CRYPT_PARAM_DECODE_PKEY_EXPORT_METHOD_FUNC (CRYPT_PARAM_DECODE_BASE + 8) #define CRYPT_PARAM_DECODE_PKEY_DUP_METHOD_FUNC (CRYPT_PARAM_DECODE_BASE + 9) #define CRYPT_PARAM_DECODE_PKEY_FREE_METHOD_FUNC (CRYPT_PARAM_DECODE_BASE + 10) #define CRYPT_PARAM_DECODE_LIB_CTX (CRYPT_PARAM_DECODE_BASE + 11) #define CRYPT_PARAM_DECODE_TARGET_ATTR_NAME (CRYPT_PARAM_DECODE_BASE + 12) #define CRYPT_PARAM_DECODE_PROVIDER_CTX (CRYPT_PARAM_DECODE_BASE + 13) #define CRYPT_PARAM_DECODE_FLAG_FREE_OUTDATA (CRYPT_PARAM_DECODE_BASE + 14) #define CRYPT_PARAM_CAP_TLS_GROUP_BASE 5000 #define CRYPT_PARAM_CAP_TLS_GROUP_IANA_GROUP_NAME (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 1) #define CRYPT_PARAM_CAP_TLS_GROUP_IANA_GROUP_ID (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 2) #define CRYPT_PARAM_CAP_TLS_GROUP_PARA_ID (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 3) #define CRYPT_PARAM_CAP_TLS_GROUP_ALG_ID (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 4) #define CRYPT_PARAM_CAP_TLS_GROUP_SEC_BITS (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 5) #define CRYPT_PARAM_CAP_TLS_GROUP_VERSION_BITS (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 6) #define CRYPT_PARAM_CAP_TLS_GROUP_IS_KEM (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 7) #define CRYPT_PARAM_CAP_TLS_GROUP_PUBKEY_LEN (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 8) #define CRYPT_PARAM_CAP_TLS_GROUP_SHAREDKEY_LEN (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 9) #define CRYPT_PARAM_CAP_TLS_GROUP_CIPHERTEXT_LEN (CRYPT_PARAM_CAP_TLS_GROUP_BASE + 10) #define CRYPT_PARAM_CAP_TLS_SIGNALG_BASE 5100 #define CRYPT_PARAM_CAP_TLS_SIGNALG_IANA_SIGN_NAME (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 1) #define CRYPT_PARAM_CAP_TLS_SIGNALG_IANA_SIGN_ID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 2) #define CRYPT_PARAM_CAP_TLS_SIGNALG_KEY_TYPE (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 3) #define CRYPT_PARAM_CAP_TLS_SIGNALG_KEY_TYPE_OID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 4) #define CRYPT_PARAM_CAP_TLS_SIGNALG_KEY_TYPE_NAME (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 5) #define CRYPT_PARAM_CAP_TLS_SIGNALG_PARA_ID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 6) #define CRYPT_PARAM_CAP_TLS_SIGNALG_PARA_OID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 7) #define CRYPT_PARAM_CAP_TLS_SIGNALG_PARA_NAME (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 8) #define CRYPT_PARAM_CAP_TLS_SIGNALG_SIGNWITHMD_ID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 9) #define CRYPT_PARAM_CAP_TLS_SIGNALG_SIGNWITHMD_OID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 10) #define CRYPT_PARAM_CAP_TLS_SIGNALG_SIGNWITHMD_NAME (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 11) #define CRYPT_PARAM_CAP_TLS_SIGNALG_SIGN_ID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 12) #define CRYPT_PARAM_CAP_TLS_SIGNALG_MD_ID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 13) #define CRYPT_PARAM_CAP_TLS_SIGNALG_MD_OID (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 14) #define CRYPT_PARAM_CAP_TLS_SIGNALG_MD_NAME (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 15) #define CRYPT_PARAM_CAP_TLS_SIGNALG_SEC_BITS (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 16) #define CRYPT_PARAM_CAP_TLS_SIGNALG_CHAIN_VERSION_BITS (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 17) #define CRYPT_PARAM_CAP_TLS_SIGNALG_CERT_VERSION_BITS (CRYPT_PARAM_CAP_TLS_SIGNALG_BASE + 18) #define CRYPT_PARAM_CMVP_BASE 5200 #define CRYPT_PARAM_CMVP_LOG_FUNC (CRYPT_PARAM_CMVP_BASE + 1) #define CRYPT_PARAM_CMVP_SELFTEST_TYPE (CRYPT_PARAM_CMVP_BASE + 2) #define CRYPT_PARAM_CMVP_RANDOM (CRYPT_PARAM_CMVP_BASE + 3) #define CRYPT_PARAM_CMVP_INTERNAL_LIBCTX_FLAG (CRYPT_PARAM_CMVP_BASE + 4) #ifdef __cplusplus } #endif #endif
2301_79861745/bench_create
include/crypto/crypt_params_key.h
C
unknown
15,650