code
stringlengths
1
1.05M
repo_name
stringlengths
6
83
path
stringlengths
3
242
language
stringclasses
222 values
license
stringclasses
20 values
size
int64
1
1.05M
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_MD5 .file "md5_x86_64.S" .set TEMP1, %r14d .set TEMP2, %r15d .set T, %r13d .set W, %r12d .set T_ORIGIN_ADDR, %rcx .set HASH, %rdi .set INPUT, %rsi .set NUM, %rdx .set S11, 7 .set S12, 12 .set S13, 17 .set S14, 22 .set S21, 5 .set S22, 9 .set S23, 14 .set S24, 20 .set S31, 4 .set S32, 11 .set S33, 16 .set S34, 23 .set S41, 6 .set S42, 10 .set S43, 15 .set S44, 21 .set A, %r8d .set B, %r9d .set C, %r10d .set D, %r11d /* MD5 Used constant value. For details about the data source, see the RFC1321 document. */ .text .align 64 .type g_tMd5, %object g_tMd5: .long 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE .long 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501 .long 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE .long 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821 .long 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA .long 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8 .long 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED .long 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A .long 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C .long 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70 .long 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05 .long 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665 .long 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039 .long 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1 .long 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1 .long 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391 .size g_tMd5, .-g_tMd5 /* * Macro description: The FF function processes the update of a hash value in a round of 0-15 compression. * Input register: * wAddr: sequence corresponding to W (wi) * tAddr: order (ti) corresponding to t * a - d: intermediate variable of the hash value * Change register: r8d-r15d * Output register: * a: indicates the value after a round of cyclic update. * Function/Macro Call: None * Implementation description: * Parameter: S11->28, S12->48, S13->17, S14->22 * T2 = BSIG0(a) + MAJ(a,b,c) * a = b + ROTL32(F(a,b,c)+x+ac),s) * F(X,Y,Z) = XY v not(X) Z * G(X,Y,Z) = XZ v Y not(Z) * H(X,Y,Z) = X xor Y xor Z * I(X,Y,Z) = Y xor (X v not(Z)) */ .macro FF_ONE_ROUND a, b, c, d, wAddr, s, tAddr, w, t, temp1, temp2 mov \tAddr(T_ORIGIN_ADDR), \t mov \wAddr(INPUT), \w /* F(b, c, d) ((b & c) | ((~b) & d)) */ mov \b, \temp1 andn \d, \b, \temp2 // (~b) & d and \c, \temp1 // b & c or \temp1, \temp2 // (b & c) | ((~b) & d) /* (a) += F((b), (c), (d)) + (\w) + (\t) */ add \w, \a add \t, \a add \temp2, \a /* (a) = ROTL32((a), (s)) */ rol $\s, \a /* (a) += (b) */ add \b, \a .endm /* * Macro description: The GG function updates a round of hash values in rounds 16-31 compression. * Input register: * wAddr: sequence corresponding to W (wi) * tAddr: order (ti) corresponding to t * a - d: intermediate variable of the hash value * Change register: r8d-r15d * Output register: * a: indicates the value after a round of cyclic update. * Function/Macro Call: None * Implementation description: * 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 * G(x, y, z) (((x) & (z)) | ((y) & (~(z)))) * (a) += G((b), (c), (d)) + (x) + (ac); * (a) = ROTL32((a), (s)); * (a) += (b); */ .macro GG_ONE_ROUND a, b, c, d, wAddr, s, tAddr, w, t, temp1, temp2 mov \tAddr(T_ORIGIN_ADDR), \t mov \wAddr(INPUT), \w /* G(x, y, z) ((b & d) | (c & (~d))) */ mov \b, \temp1 and \d, \temp1 andn \c, \d, \temp2 or \temp1, \temp2 /* (a) += G((b), (c), (d)) + (\w) + (t) */ add \t, \a add \w, \a add \temp2, \a /* (a) = ROTL32((a), (s)) */ rol $\s, \a /* (a) += (b) */ add \b, \a .endm /* * Macro description: The HH function processes the update of a hash value in a round of 32-47 compression. * Input register: * wAddr: sequence corresponding to W (wi) * tAddr: order (ti) corresponding to t * a - d: intermediate variable of the hash value * Change register: r8d-r15d * Output register: * a: indicates the value after a round of cyclic update. * Function/Macro Call: None * Implementation description: * * H(x, y, z) ((x) ^ (y) ^ (z)) * (a) += H((b), (c), (d)) + (x) + (ac); * (a) = ROTL32((a), (s)); * (a) += (b); * b and c ->next c and d * swap \temp2 temp4 for next round */ .macro HH_ONE_ROUND a, b, c, d, wAddr, s, tAddr, w, t, temp1, temp2 mov \tAddr(T_ORIGIN_ADDR), \t mov \wAddr(INPUT), \w /* H(x, y, z) (b ^ c ^ d) */ mov \b, \temp1 xor \d, \temp1 xor \c, \temp1 /* (a) += H((b), (c), (d)) + (\w) + (\t) */ add \t, \a add \w, \a add \temp1, \a /* (a) = ROTL32((a), (s)) */ rol $\s, \a /* (a) += (b) */ add \b, \a .endm /* * Macro description: Processes the update of a hash value in a round of 48-63 compression. * Input register: * wAddr: Sequence corresponding to W (wi) * tAddr: Order (ti) corresponding to t * a - d: Intermediate variable of the hash value * Change register: r8d-r15d. * Output register: * a: indicates the value after a round of cyclic update. * Function/Macro Call: None * Implementation description: * 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 * I(x, y, z) ((y) ^ ((x) | (~(z)))) * (a) += I((b), (c), (d)) + (x) + (ac); \ * (a) = ROTL32((a), (s)); \ * (a) += (b); * swap \temp2 temp4 for next round */ .macro II_ONE_ROUND a, b, c, d, wAddr, s, tAddr, w, t, temp1, temp2 mov \tAddr(T_ORIGIN_ADDR), \t mov \wAddr(INPUT), \w /* I(b, c, d) (c ^ (b | (~d))) */ mov \d, \temp1 not \temp1 or \b, \temp1 xor \c, \temp1 /* (a) += I((b), (c), (d)) + (\w) + (\t); */ add \t, \a add \w, \a add \temp1, \a /* (a) = ROTL32((a), (s)) */ rol $\s, \a /* (a) += (b) */ add \b, \a .endm /* * Function description: Performs 64 rounds of compression calculation * based on the input plaintext data and updates the hash value. * Function prototype: void MD5_Compress(uint32_t hash[32], const uint8_t *in, uint32_t num); * Input register: * rdi: Indicates the storage address of the hash value. * rsi: Pointer to the input data address (Wi) * rdx: Indicates the number of 64 rounds of cycles. * (You need to do several blocks, that is, you need to do several loops.) * Change register: rsi, r8d-r15d, rcx. * Output register: None * Function/Macro Call: FF_ONE_ROUND, GG_ONE_ROUND, HH_ONE_ROUND, II_ONE_ROUND */ .text .globl MD5_Compress .type MD5_Compress,%function .align 4 MD5_Compress: .cfi_startproc /* Push stack and pop stack protection */ pushq %r14 pushq %rbx pushq %rbp pushq %r12 pushq %r13 pushq %r15 /* r8d-r10d: a-d */ mov 0(%rdi), A mov 4(%rdi), B mov 8(%rdi), C mov 12(%rdi), D .Lmd5_loop: leaq g_tMd5(%rip), T_ORIGIN_ADDR /* LEND_MD5_FF_ROUND_ROUND_0_15 */ /* FF_ONE_ROUND a, b, c, d, wAddr, s, tAddr, w, t, temp1, temp2 */ FF_ONE_ROUND A, B, C, D, 0, S11, 0, W, T, TEMP1, TEMP2 FF_ONE_ROUND D, A, B, C, 4, S12, 4, W, T, TEMP1, TEMP2 FF_ONE_ROUND C, D, A, B, 8, S13, 8, W, T, TEMP1, TEMP2 FF_ONE_ROUND B, C, D, A, 12, S14, 12, W, T, TEMP1, TEMP2 FF_ONE_ROUND A, B, C, D, 16, S11, 16, W, T, TEMP1, TEMP2 FF_ONE_ROUND D, A, B, C, 20, S12, 20, W, T, TEMP1, TEMP2 FF_ONE_ROUND C, D, A, B, 24, S13, 24, W, T, TEMP1, TEMP2 FF_ONE_ROUND B, C, D, A, 28, S14, 28, W, T, TEMP1, TEMP2 FF_ONE_ROUND A, B, C, D, 32, S11, 32, W, T, TEMP1, TEMP2 FF_ONE_ROUND D, A, B, C, 36, S12, 36, W, T, TEMP1, TEMP2 FF_ONE_ROUND C, D, A, B, 40, S13, 40, W, T, TEMP1, TEMP2 FF_ONE_ROUND B, C, D, A, 44, S14, 44, W, T, TEMP1, TEMP2 FF_ONE_ROUND A, B, C, D, 48, S11, 48, W, T, TEMP1, TEMP2 FF_ONE_ROUND D, A, B, C, 52, S12, 52, W, T, TEMP1, TEMP2 FF_ONE_ROUND C, D, A, B, 56, S13, 56, W, T, TEMP1, TEMP2 FF_ONE_ROUND B, C, D, A, 60, S14, 60, W, T, TEMP1, TEMP2 /* LEND_MD5_GG_ROUND_ROUND_16_31 */ /* GG_ONE_ROUND a, b, c, d, wAddr, s, tAddr, w, t, temp1, temp2 */ GG_ONE_ROUND A, B, C, D, 4, S21, 64, W, T, TEMP1, TEMP2 GG_ONE_ROUND D, A, B, C, 24, S22, 68, W, T, TEMP1, TEMP2 GG_ONE_ROUND C, D, A, B, 44, S23, 72, W, T, TEMP1, TEMP2 GG_ONE_ROUND B, C, D, A, 0, S24, 76, W, T, TEMP1, TEMP2 GG_ONE_ROUND A, B, C, D, 20, S21, 80, W, T, TEMP1, TEMP2 GG_ONE_ROUND D, A, B, C, 40, S22, 84, W, T, TEMP1, TEMP2 GG_ONE_ROUND C, D, A, B, 60, S23, 88, W, T, TEMP1, TEMP2 GG_ONE_ROUND B, C, D, A, 16, S24, 92, W, T, TEMP1, TEMP2 GG_ONE_ROUND A, B, C, D, 36, S21, 96, W, T, TEMP1, TEMP2 GG_ONE_ROUND D, A, B, C, 56, S22, 100, W, T, TEMP1, TEMP2 GG_ONE_ROUND C, D, A, B, 12, S23, 104, W, T, TEMP1, TEMP2 GG_ONE_ROUND B, C, D, A, 32, S24, 108, W, T, TEMP1, TEMP2 GG_ONE_ROUND A, B, C, D, 52, S21, 112, W, T, TEMP1, TEMP2 GG_ONE_ROUND D, A, B, C, 8, S22, 116, W, T, TEMP1, TEMP2 GG_ONE_ROUND C, D, A, B, 28, S23, 120, W, T, TEMP1, TEMP2 GG_ONE_ROUND B, C, D, A, 48, S24, 124, W, T, TEMP1, TEMP2 /* LEND_MD5_HH_ROUND_ROUND_32_47 */ /* HH_ONE_ROUND a,b,c,d,wAddr,s,tAddr, w, t, temp1, temp2 */ HH_ONE_ROUND A, B, C, D, 20, S31, 128, W, T, TEMP1, TEMP2 HH_ONE_ROUND D, A, B, C, 32, S32, 132, W, T, TEMP1, TEMP2 HH_ONE_ROUND C, D, A, B, 44, S33, 136, W, T, TEMP1, TEMP2 HH_ONE_ROUND B, C, D, A, 56, S34, 140, W, T, TEMP1, TEMP2 HH_ONE_ROUND A, B, C, D, 4, S31, 144, W, T, TEMP1, TEMP2 HH_ONE_ROUND D, A, B, C, 16, S32, 148, W, T, TEMP1, TEMP2 HH_ONE_ROUND C, D, A, B, 28, S33, 152, W, T, TEMP1, TEMP2 HH_ONE_ROUND B, C, D, A, 40, S34, 156, W, T, TEMP1, TEMP2 HH_ONE_ROUND A, B, C, D, 52, S31, 160, W, T, TEMP1, TEMP2 HH_ONE_ROUND D, A, B, C, 0, S32, 164, W, T, TEMP1, TEMP2 HH_ONE_ROUND C, D, A, B, 12, S33, 168, W, T, TEMP1, TEMP2 HH_ONE_ROUND B, C, D, A, 24, S34, 172, W, T, TEMP1, TEMP2 HH_ONE_ROUND A, B, C, D, 36, S31, 176, W, T, TEMP1, TEMP2 HH_ONE_ROUND D, A, B, C, 48, S32, 180, W, T, TEMP1, TEMP2 HH_ONE_ROUND C, D, A, B, 60, S33, 184, W, T, TEMP1, TEMP2 HH_ONE_ROUND B, C, D, A, 8, S34, 188, W, T, TEMP1, TEMP2 /* LEND_MD5_II_ROUND_ROUND_48_63 */ /* II_ONE_ROUND a, b,c,d,wAddr,s,tAddr, w, t, temp1, temp2 */ II_ONE_ROUND A, B, C, D, 0, S41, 192, W, T, TEMP1, TEMP2 II_ONE_ROUND D, A, B, C, 28, S42, 196, W, T, TEMP1, TEMP2 II_ONE_ROUND C, D, A, B, 56, S43, 200, W, T, TEMP1, TEMP2 II_ONE_ROUND B, C, D, A, 20, S44, 204, W, T, TEMP1, TEMP2 II_ONE_ROUND A, B, C, D, 48, S41, 208, W, T, TEMP1, TEMP2 II_ONE_ROUND D, A, B, C, 12, S42, 212, W, T, TEMP1, TEMP2 II_ONE_ROUND C, D, A, B, 40, S43, 216, W, T, TEMP1, TEMP2 II_ONE_ROUND B, C, D, A, 4, S44, 220, W, T, TEMP1, TEMP2 II_ONE_ROUND A, B, C, D, 32, S41, 224, W, T, TEMP1, TEMP2 II_ONE_ROUND D, A, B, C, 60, S42, 228, W, T, TEMP1, TEMP2 II_ONE_ROUND C, D, A, B, 24, S43, 232, W, T, TEMP1, TEMP2 II_ONE_ROUND B, C, D, A, 52, S44, 236, W, T, TEMP1, TEMP2 II_ONE_ROUND A, B, C, D, 16, S41, 240, W, T, TEMP1, TEMP2 II_ONE_ROUND D, A, B, C, 44, S42, 244, W, T, TEMP1, TEMP2 II_ONE_ROUND C, D, A, B, 8, S43, 248, W, T, TEMP1, TEMP2 II_ONE_ROUND B, C, D, A, 36, S44, 252, W, T, TEMP1, TEMP2 /* Update the storage hash value. */ add 0(%rdi), A add 4(%rdi), B add 8(%rdi), C add 12(%rdi), D mov A, 0(%rdi) mov B, 4(%rdi) mov C, 8(%rdi) mov D, 12(%rdi) lea 64(INPUT), INPUT sub $1, NUM ja .Lmd5_loop .LEND_MD5_FINFISH_INITIAL: /* Registers and pointers are reset. */ popq %r15 popq %r13 popq %r12 popq %rbp popq %rbx popq %r14 ret .cfi_endproc .size MD5_Compress, .-MD5_Compress #endif
2301_79861745/bench_create
crypto/md5/src/asm/md5_x86_64.S
Unix Assembly
unknown
13,655
/* * 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_MD5 #include "securec.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "md5_core.h" #include "crypt_md5.h" #include "bsl_sal.h" #include "crypt_types.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define CRYPT_MD5_DIGESTSIZE 16 #define CRYPT_MD5_BLOCKSIZE 64 /* md5 ctx */ struct CryptMdCtx { uint32_t h[CRYPT_MD5_DIGESTSIZE / sizeof(uint32_t)]; /* store the intermediate data of the hash value */ uint8_t block[CRYPT_MD5_BLOCKSIZE]; /* store the remaining data of less than one block */ uint32_t hNum, lNum; /* input data counter, maximum value 2 ^ 64 bits */ /* Number of remaining bytes in 'block' arrary that are stored less than one block */ uint32_t num; }; CRYPT_MD5_Ctx *CRYPT_MD5_NewCtx(void) { return BSL_SAL_Calloc(1, sizeof(CRYPT_MD5_Ctx)); } CRYPT_MD5_Ctx *CRYPT_MD5_NewCtxEx(void *libCtx, int32_t algId) { (void)libCtx; (void)algId; return BSL_SAL_Calloc(1, sizeof(CRYPT_MD5_Ctx)); } void CRYPT_MD5_FreeCtx(CRYPT_MD5_Ctx *ctx) { BSL_SAL_ClearFree(ctx, sizeof(CRYPT_MD5_Ctx)); } int32_t CRYPT_MD5_Init(CRYPT_MD5_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_MD5_Ctx), 0, sizeof(CRYPT_MD5_Ctx)); /* Set the initial values of A, B, C, and D according to step 3 in section 3.3 of RFC1321. */ ctx->h[0] = 0x67452301; ctx->h[1] = 0xefcdab89; ctx->h[2] = 0x98badcfe; ctx->h[3] = 0x10325476; return CRYPT_SUCCESS; } int32_t CRYPT_MD5_Deinit(CRYPT_MD5_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memset_s(ctx, sizeof(CRYPT_MD5_Ctx), 0, sizeof(CRYPT_MD5_Ctx)); return CRYPT_SUCCESS; } static uint32_t IsInputOverflow(CRYPT_MD5_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_MD5_INPUT_OVERFLOW); return CRYPT_MD5_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_MD5_INPUT_OVERFLOW); return CRYPT_MD5_INPUT_OVERFLOW; } ctx->hNum = cnt1; ctx->lNum = cnt0; return CRYPT_SUCCESS; } static int32_t IsUpdateParamValid(CRYPT_MD5_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_MD5_INPUT_OVERFLOW); return CRYPT_MD5_INPUT_OVERFLOW; } return CRYPT_SUCCESS; } int32_t CRYPT_MD5_Update(CRYPT_MD5_Ctx *ctx, const uint8_t *in, uint32_t len) { int32_t ret = IsUpdateParamValid(ctx, in, len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (len == 0) { return CRYPT_SUCCESS; } const uint8_t *data = in; uint32_t dataLen = len; uint32_t left = CRYPT_MD5_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); MD5_Compress(ctx->h, ctx->block, 1); dataLen -= left; data += left; ctx->num = 0; } uint32_t blockCnt = dataLen / CRYPT_MD5_BLOCKSIZE; if (blockCnt > 0) { MD5_Compress(ctx->h, data, blockCnt); blockCnt *= CRYPT_MD5_BLOCKSIZE; data += blockCnt; dataLen -= blockCnt; } if (dataLen != 0) { // Copy the remaining data to the cache array. (void)memcpy_s(ctx->block, CRYPT_MD5_BLOCKSIZE, data, dataLen); ctx->num = dataLen; } return CRYPT_SUCCESS; } static int32_t IsFinalParamValid(const CRYPT_MD5_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_MD5_DIGESTSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_MD5_OUT_BUFF_LEN_NOT_ENOUGH); return CRYPT_MD5_OUT_BUFF_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } int32_t CRYPT_MD5_Final(CRYPT_MD5_Ctx *ctx, uint8_t *out, uint32_t *outLen) { int32_t ret = IsFinalParamValid(ctx, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ctx->block[ctx->num++] = 0x80; /* 0x80 indicates that '1' is appended to the end of a message. */ uint8_t *block = ctx->block; uint32_t num = ctx->num; uint32_t left = CRYPT_MD5_BLOCKSIZE - num; if (left < 8) { /* Less than 8 bytes, insufficient for storing data of the accumulated data length(lNum&hNum). */ (void)memset_s(block + num, left, 0, left); MD5_Compress(ctx->h, ctx->block, 1); num = 0; left = CRYPT_MD5_BLOCKSIZE; } (void)memset_s(block + num, left - 8, 0, left - 8); /* 8 byte is used to store data of accumulated data length. */ block += CRYPT_MD5_BLOCKSIZE - 8; /* 8 byte is used to store data of the accumulated data length(lNum&hNum). */ PUT_UINT32_LE(ctx->lNum, block, 0); block += sizeof(uint32_t); PUT_UINT32_LE(ctx->hNum, block, 0); MD5_Compress(ctx->h, ctx->block, 1); ctx->num = 0; PUT_UINT32_LE(ctx->h[0], out, 0); PUT_UINT32_LE(ctx->h[1], out, 4); PUT_UINT32_LE(ctx->h[2], out, 8); PUT_UINT32_LE(ctx->h[3], out, 12); *outLen = CRYPT_MD5_DIGESTSIZE; return CRYPT_SUCCESS; } int32_t CRYPT_MD5_CopyCtx(CRYPT_MD5_Ctx *dst, const CRYPT_MD5_Ctx *src) { if (dst == NULL || src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memcpy_s(dst, sizeof(CRYPT_MD5_Ctx), src, sizeof(CRYPT_MD5_Ctx)); return CRYPT_SUCCESS; } CRYPT_MD5_Ctx *CRYPT_MD5_DupCtx(const CRYPT_MD5_Ctx *src) { if (src == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_MD5_Ctx *newCtx = CRYPT_MD5_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memcpy_s(newCtx, sizeof(CRYPT_MD5_Ctx), src, sizeof(CRYPT_MD5_Ctx)); return newCtx; } #ifdef HITLS_CRYPTO_PROVIDER int32_t CRYPT_MD5_GetParam(CRYPT_MD5_Ctx *ctx, BSL_Param *param) { (void)ctx; return CRYPT_MdCommonGetParam(CRYPT_MD5_DIGESTSIZE, CRYPT_MD5_BLOCKSIZE, param); } #endif #ifdef __cplusplus } #endif /* __cpluscplus */ #endif // HITLS_CRYPTO_MD5
2301_79861745/bench_create
crypto/md5/src/md5.c
C
unknown
7,678
/* * 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 MD5_CORE_H #define MD5_CORE_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_MD5 #include <stdint.h> #ifdef __cplusplus extern "C" { #endif void MD5_Compress(uint32_t state[4], const uint8_t *data, uint32_t blockCnt); #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_MD5 #endif // MD5_CORE_H
2301_79861745/bench_create
crypto/md5/src/md5_core.h
C
unknown
851
/* * 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_MD5 #include "securec.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "md5_core.h" #include "crypt_md5.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ /* F, G, H and I are basic MD5 functions. */ #define F(x, y, z) (((x) & (y)) | ((~(x)) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~(z)))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~(z)))) #define FF(a, b, c, d, x, s, ac) \ do { \ (a) += F((b), (c), (d)) + (x) + (ac); \ (a) = ROTL32((a), (s)); \ (a) += (b); \ } while (0) #define GG(a, b, c, d, x, s, ac) \ do { \ (a) += G((b), (c), (d)) + (x) + (ac); \ (a) = ROTL32((a), (s)); \ (a) += (b); \ } while (0) #define HH(a, b, c, d, x, s, ac) \ do { \ (a) += H((b), (c), (d)) + (x) + (ac); \ (a) = ROTL32((a), (s)); \ (a) += (b); \ } while (0) #define II(a, b, c, d, x, s, ac) \ do { \ (a) += I((b), (c), (d)) + (x) + (ac); \ (a) = ROTL32((a), (s)); \ (a) += (b); \ } while (0) /* Constants for MD5_Compress routine. */ #define S11 7 #define S12 12 #define S13 17 #define S14 22 #define S21 5 #define S22 9 #define S23 14 #define S24 20 #define S31 4 #define S32 11 #define S33 16 #define S34 23 #define S41 6 #define S42 10 #define S43 15 #define S44 21 static const uint32_t T[64] = { 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501, 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821, 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8, 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A, 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70, 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665, 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1, 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391, }; /* see RFC1321 chapter 3.4 Step 4 https://www.rfc-editor.org/rfc/rfc1321 */ void MD5_Compress(uint32_t state[4], const uint8_t *data, uint32_t blockCnt) { uint32_t w[16] = {0}; const uint8_t *input = data; uint32_t count = blockCnt; while (count > 0) { /* convert data to 32 bits for calculation */ w[0] = GET_UINT32_LE(input, 0); w[1] = GET_UINT32_LE(input, 4); w[2] = GET_UINT32_LE(input, 8); w[3] = GET_UINT32_LE(input, 12); w[4] = GET_UINT32_LE(input, 16); w[5] = GET_UINT32_LE(input, 20); w[6] = GET_UINT32_LE(input, 24); w[7] = GET_UINT32_LE(input, 28); w[8] = GET_UINT32_LE(input, 32); w[9] = GET_UINT32_LE(input, 36); w[10] = GET_UINT32_LE(input, 40); w[11] = GET_UINT32_LE(input, 44); w[12] = GET_UINT32_LE(input, 48); w[13] = GET_UINT32_LE(input, 52); w[14] = GET_UINT32_LE(input, 56); w[15] = GET_UINT32_LE(input, 60); uint32_t a = state[0]; uint32_t b = state[1]; uint32_t c = state[2]; uint32_t d = state[3]; FF(a, b, c, d, w[0], S11, T[0]); FF(d, a, b, c, w[1], S12, T[1]); FF(c, d, a, b, w[2], S13, T[2]); FF(b, c, d, a, w[3], S14, T[3]); FF(a, b, c, d, w[4], S11, T[4]); FF(d, a, b, c, w[5], S12, T[5]); FF(c, d, a, b, w[6], S13, T[6]); FF(b, c, d, a, w[7], S14, T[7]); FF(a, b, c, d, w[8], S11, T[8]); FF(d, a, b, c, w[9], S12, T[9]); FF(c, d, a, b, w[10], S13, T[10]); FF(b, c, d, a, w[11], S14, T[11]); FF(a, b, c, d, w[12], S11, T[12]); FF(d, a, b, c, w[13], S12, T[13]); FF(c, d, a, b, w[14], S13, T[14]); FF(b, c, d, a, w[15], S14, T[15]); GG(a, b, c, d, w[1], S21, T[16]); GG(d, a, b, c, w[6], S22, T[17]); GG(c, d, a, b, w[11], S23, T[18]); GG(b, c, d, a, w[0], S24, T[19]); GG(a, b, c, d, w[5], S21, T[20]); GG(d, a, b, c, w[10], S22, T[21]); GG(c, d, a, b, w[15], S23, T[22]); GG(b, c, d, a, w[4], S24, T[23]); GG(a, b, c, d, w[9], S21, T[24]); GG(d, a, b, c, w[14], S22, T[25]); GG(c, d, a, b, w[3], S23, T[26]); GG(b, c, d, a, w[8], S24, T[27]); GG(a, b, c, d, w[13], S21, T[28]); GG(d, a, b, c, w[2], S22, T[29]); GG(c, d, a, b, w[7], S23, T[30]); GG(b, c, d, a, w[12], S24, T[31]); HH(a, b, c, d, w[5], S31, T[32]); HH(d, a, b, c, w[8], S32, T[33]); HH(c, d, a, b, w[11], S33, T[34]); HH(b, c, d, a, w[14], S34, T[35]); HH(a, b, c, d, w[1], S31, T[36]); HH(d, a, b, c, w[4], S32, T[37]); HH(c, d, a, b, w[7], S33, T[38]); HH(b, c, d, a, w[10], S34, T[39]); HH(a, b, c, d, w[13], S31, T[40]); HH(d, a, b, c, w[0], S32, T[41]); HH(c, d, a, b, w[3], S33, T[42]); HH(b, c, d, a, w[6], S34, T[43]); HH(a, b, c, d, w[9], S31, T[44]); HH(d, a, b, c, w[12], S32, T[45]); HH(c, d, a, b, w[15], S33, T[46]); HH(b, c, d, a, w[2], S34, T[47]); II(a, b, c, d, w[0], S41, T[48]); II(d, a, b, c, w[7], S42, T[49]); II(c, d, a, b, w[14], S43, T[50]); II(b, c, d, a, w[5], S44, T[51]); II(a, b, c, d, w[12], S41, T[52]); II(d, a, b, c, w[3], S42, T[53]); II(c, d, a, b, w[10], S43, T[54]); II(b, c, d, a, w[1], S44, T[55]); II(a, b, c, d, w[8], S41, T[56]); II(d, a, b, c, w[15], S42, T[57]); II(c, d, a, b, w[6], S43, T[58]); II(b, c, d, a, w[13], S44, T[59]); II(a, b, c, d, w[4], S41, T[60]); II(d, a, b, c, w[11], S42, T[61]); II(c, d, a, b, w[2], S43, T[62]); II(b, c, d, a, w[9], S44, T[63]); state[0] += a; state[1] += b; state[2] += c; state[3] += d; input += CRYPT_MD5_BLOCKSIZE; count--; } } #ifdef __cplusplus } #endif /* __cpluscplus */ #endif // HITLS_CRYPTO_MD5
2301_79861745/bench_create
crypto/md5/src/noasm_md5.c
C
unknown
7,292
/* * 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_ML_DSA_H #define CRYPT_ML_DSA_H #include <stdint.h> #include "crypt_types.h" #include "bsl_params.h" typedef struct CryptMlDsaCtx CRYPT_ML_DSA_Ctx; CRYPT_ML_DSA_Ctx *CRYPT_ML_DSA_NewCtx(void); CRYPT_ML_DSA_Ctx *CRYPT_ML_DSA_NewCtxEx(void *libCtx); void CRYPT_ML_DSA_FreeCtx(CRYPT_ML_DSA_Ctx *ctx); CRYPT_ML_DSA_Ctx *CRYPT_ML_DSA_DupCtx(CRYPT_ML_DSA_Ctx *ctx); int32_t CRYPT_ML_DSA_Ctrl(CRYPT_ML_DSA_Ctx *ctx, CRYPT_PkeyCtrl opt, void *val, uint32_t len); int32_t CRYPT_ML_DSA_GenKey(CRYPT_ML_DSA_Ctx *ctx); int32_t CRYPT_ML_DSA_Sign(CRYPT_ML_DSA_Ctx *ctx, int32_t hashId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen); int32_t CRYPT_ML_DSA_Verify(CRYPT_ML_DSA_Ctx *ctx, int32_t hashId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t signLen); int32_t CRYPT_ML_DSA_SetPrvKey(CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPrv *prv); int32_t CRYPT_ML_DSA_SetPubKey(CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPub *pub); int32_t CRYPT_ML_DSA_GetPrvKey(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPrv *prv); int32_t CRYPT_ML_DSA_GetPubKey(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPub *pub); #ifdef HITLS_BSL_PARAMS int32_t CRYPT_ML_DSA_SetPrvKeyEx(CRYPT_ML_DSA_Ctx *ctx, const BSL_Param *para); int32_t CRYPT_ML_DSA_SetPubKeyEx(CRYPT_ML_DSA_Ctx *ctx, const BSL_Param *para); int32_t CRYPT_ML_DSA_GetPrvKeyEx(const CRYPT_ML_DSA_Ctx *ctx, BSL_Param *para); int32_t CRYPT_ML_DSA_GetPubKeyEx(const CRYPT_ML_DSA_Ctx *ctx, BSL_Param *para); #endif int32_t CRYPT_ML_DSA_Cmp(const CRYPT_ML_DSA_Ctx *a, const CRYPT_ML_DSA_Ctx *b); #ifdef HITLS_CRYPTO_MLDSA_CHECK /** * @ingroup mldsa * @brief check the key pair consistency * * @param checkType [IN] check type * @param pkey1 [IN] mldsa key context structure * @param pkey2 [IN] mldsa key context structure * * @retval CRYPT_SUCCESS check success. * Others. For details, see error code in errno. */ int32_t CRYPT_ML_DSA_Check(uint32_t checkType, const CRYPT_ML_DSA_Ctx *pkey1, const CRYPT_ML_DSA_Ctx *pkey2); #endif // HITLS_CRYPTO_MLDSA_CHECK #endif // CRYPT_ML_DSA_H
2301_79861745/bench_create
crypto/mldsa/include/crypt_mldsa.h
C
unknown
2,625
/* * 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_MLDSA #include "securec.h" #include "crypt_errno.h" #include "crypt_util_rand.h" #include "crypt_utils.h" #include "crypt_algid.h" #include "bsl_errno.h" #include "bsl_sal.h" #include "bsl_obj_internal.h" #include "bsl_err_internal.h" #include "ml_dsa_local.h" #include "eal_md_local.h" // These data from NIST.FIPS.204 Table 1 and Table 2. static const CRYPT_ML_DSA_Info MLDSA_PARAMETERTER_44 = {4, 4, 2, 39, 78, (1 << 17), ((MLDSA_Q - 1) / 88), 80, 128, 1312, 2560, 2420}; static const CRYPT_ML_DSA_Info MLDSA_PARAMETERTER_65 = {6, 5, 4, 49, 196, (1 << 19), ((MLDSA_Q - 1) / 32), 55, 192, 1952, 4032, 3309}; static const CRYPT_ML_DSA_Info MLDSA_PARAMETERTER_87 = {8, 7, 2, 60, 120, (1 << 19), ((MLDSA_Q - 1) / 32), 75, 256, 2592, 4896, 4627}; static const CRYPT_ML_DSA_Info *g_mldsaInfo[] = {&MLDSA_PARAMETERTER_44, &MLDSA_PARAMETERTER_65, &MLDSA_PARAMETERTER_87}; const CRYPT_ML_DSA_Info *CRYPT_ML_DSA_GetInfo(uint32_t k) { if (k == CRYPT_MLDSA_TYPE_MLDSA_44) { return g_mldsaInfo[0]; } else if (k == CRYPT_MLDSA_TYPE_MLDSA_65) { return g_mldsaInfo[1]; } else if (k == CRYPT_MLDSA_TYPE_MLDSA_87) { return g_mldsaInfo[2]; } return NULL; } CRYPT_ML_DSA_Ctx *CRYPT_ML_DSA_NewCtx(void) { CRYPT_ML_DSA_Ctx *keyCtx = BSL_SAL_Calloc(1, sizeof(CRYPT_ML_DSA_Ctx)); if (keyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } keyCtx->needEncodeCtx = true; keyCtx->isMuMsg = false; keyCtx->deterministicSignFlag = false; keyCtx->needPreHash = false; BSL_SAL_ReferencesInit(&(keyCtx->references)); return keyCtx; } CRYPT_ML_DSA_Ctx *CRYPT_ML_DSA_NewCtxEx(void *libCtx) { CRYPT_ML_DSA_Ctx *ctx = CRYPT_ML_DSA_NewCtx(); if (ctx == NULL) { return NULL; } ctx->libCtx = libCtx; return ctx; } void CRYPT_ML_DSA_FreeCtx(CRYPT_ML_DSA_Ctx *ctx) { if (ctx == NULL) { return; } int ret = 0; BSL_SAL_AtomicDownReferences(&(ctx->references), &ret); if (ret > 0) { return; } BSL_SAL_ClearFree(ctx->prvKey, ctx->prvLen); BSL_SAL_FREE(ctx->pubKey); BSL_SAL_FREE(ctx->ctxInfo); BSL_SAL_ReferencesFree(&(ctx->references)); BSL_SAL_Free(ctx); } CRYPT_ML_DSA_Ctx *CRYPT_ML_DSA_DupCtx(CRYPT_ML_DSA_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_ML_DSA_Ctx *newCtx = CRYPT_ML_DSA_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } newCtx->info = ctx->info; GOTO_ERR_IF_SRC_NOT_NULL(newCtx->pubKey, ctx->pubKey, BSL_SAL_Dump(ctx->pubKey, ctx->pubLen), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newCtx->prvKey, ctx->prvKey, BSL_SAL_Dump(ctx->prvKey, ctx->prvLen), CRYPT_MEM_ALLOC_FAIL); newCtx->pubLen = ctx->pubLen; newCtx->prvLen = ctx->prvLen; newCtx->needEncodeCtx = ctx->needEncodeCtx; newCtx->isMuMsg = ctx->isMuMsg; newCtx->deterministicSignFlag = ctx->deterministicSignFlag; newCtx->needPreHash = ctx->needPreHash; newCtx->libCtx = ctx->libCtx; return newCtx; ERR: CRYPT_ML_DSA_FreeCtx(newCtx); return NULL; } static int32_t MlDSASetAlgInfo(CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (len != sizeof(int32_t) || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ctx->info != NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_CTRL_INIT_REPEATED); return CRYPT_MLDSA_CTRL_INIT_REPEATED; } ctx->info = CRYPT_ML_DSA_GetInfo(*(int32_t *)val); if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } return CRYPT_SUCCESS; } static int32_t MLDSAGetSignLen(const CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (ctx == NULL || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } *(int32_t *)val = ctx->info->signatureLen; return CRYPT_SUCCESS; } static int32_t MLDSAGetSecBits(const CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (ctx == NULL || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (len != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(int32_t *)val = ctx->info->secBits; return CRYPT_SUCCESS; } static int32_t MlDSASetEncodeFlag(CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (len != sizeof(int32_t) || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->needEncodeCtx = (*(int32_t *)val != 0); return CRYPT_SUCCESS; } static int32_t MlDSASetMsgFlag(CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (len != sizeof(int32_t) || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->isMuMsg = (*(int32_t *)val != 0); return CRYPT_SUCCESS; } static int32_t MlDSASetDeterministicSignFlag(CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (len != sizeof(int32_t) || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->deterministicSignFlag = (*(int32_t *)val != 0); return CRYPT_SUCCESS; } static int32_t MlDSASetPreHashFlag(CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (len != sizeof(int32_t) || val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ctx->needPreHash = (*(int32_t *)val != 0); return CRYPT_SUCCESS; } int32_t MLDSASetctxInfo(CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { if (len > MLDSA_MAX_CTX_BYTES) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYLEN_ERROR); return CRYPT_MLDSA_KEYLEN_ERROR; } if (ctx->ctxInfo != NULL) { BSL_SAL_FREE(ctx->ctxInfo); ctx->ctxLen = 0; } if (val == NULL && len == 0) { ctx->needEncodeCtx = true; return CRYPT_SUCCESS; } ctx->ctxInfo = BSL_SAL_Dump((uint8_t *)val, len); if (ctx->ctxInfo == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->ctxLen = len; ctx->needEncodeCtx = true; return CRYPT_SUCCESS; } static int32_t MLDSAGetPubKeyLen(const CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { RETURN_RET_IF(val == NULL, CRYPT_NULL_INPUT); RETURN_RET_IF((ctx->info == NULL), CRYPT_MLDSA_KEYINFO_NOT_SET); RETURN_RET_IF((len != sizeof(uint32_t)), CRYPT_INVALID_ARG); *(uint32_t *)val = ctx->info->publicKeyLen; return CRYPT_SUCCESS; } static int32_t MLDSAGetPrvKeyLen(const CRYPT_ML_DSA_Ctx *ctx, void *val, uint32_t len) { RETURN_RET_IF(val == NULL, CRYPT_NULL_INPUT); RETURN_RET_IF((ctx->info == NULL), CRYPT_MLDSA_KEYINFO_NOT_SET); RETURN_RET_IF((len != sizeof(uint32_t)), CRYPT_INVALID_ARG); *(uint32_t *)val = ctx->info->privateKeyLen; return CRYPT_SUCCESS; } static int32_t MLDSACleanPubKey(CRYPT_ML_DSA_Ctx *ctx) { if (ctx->pubKey != NULL) { BSL_SAL_CleanseData(ctx->pubKey, ctx->pubLen); BSL_SAL_FREE(ctx->pubKey); ctx->pubLen = 0; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_Ctrl(CRYPT_ML_DSA_Ctx *ctx, CRYPT_PkeyCtrl opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch ((uint32_t)opt) { case CRYPT_CTRL_SET_PARA_BY_ID: return MlDSASetAlgInfo(ctx, val, len); case CRYPT_CTRL_GET_SIGNLEN: return MLDSAGetSignLen(ctx, val, len); case CRYPT_CTRL_GET_SECBITS: return MLDSAGetSecBits(ctx, val, len); case CRYPT_CTRL_SET_CTX_INFO: return MLDSASetctxInfo(ctx, val, len); case CRYPT_CTRL_SET_MLDSA_ENCODE_FLAG: return MlDSASetEncodeFlag(ctx, val, len); case CRYPT_CTRL_SET_MLDSA_MUMSG_FLAG: return MlDSASetMsgFlag(ctx, val, len); case CRYPT_CTRL_SET_DETERMINISTIC_FLAG: return MlDSASetDeterministicSignFlag(ctx, val, len); case CRYPT_CTRL_SET_PREHASH_FLAG: return MlDSASetPreHashFlag(ctx, val, len); case CRYPT_CTRL_GET_PUBKEY_LEN: return MLDSAGetPubKeyLen(ctx, val, len); case CRYPT_CTRL_GET_PRVKEY_LEN: return MLDSAGetPrvKeyLen(ctx, val, len); case CRYPT_CTRL_CLEAN_PUB_KEY: return MLDSACleanPubKey(ctx); default: BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_CTRL_NOT_SUPPORT); return CRYPT_MLDSA_CTRL_NOT_SUPPORT; } } static int32_t MLDSACreateKeyBuf(CRYPT_ML_DSA_Ctx *ctx) { if (ctx->pubKey == NULL) { ctx->pubKey = BSL_SAL_Malloc(ctx->info->publicKeyLen); if (ctx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->pubLen = ctx->info->publicKeyLen; } if (ctx->prvKey == NULL) { ctx->prvKey = BSL_SAL_Malloc(ctx->info->privateKeyLen); if (ctx->prvKey == NULL) { BSL_SAL_FREE(ctx->pubKey); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->prvLen = ctx->info->privateKeyLen; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_GenKey(CRYPT_ML_DSA_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (MLDSACreateKeyBuf(ctx) != CRYPT_SUCCESS) { return CRYPT_MEM_ALLOC_FAIL; } uint8_t seed[MLDSA_SEED_BYTES_LEN]; int32_t ret = CRYPT_RandEx(ctx->libCtx, seed, MLDSA_SEED_BYTES_LEN); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = MLDSA_KeyGenInternal(ctx, seed); BSL_SAL_CleanseData(seed, MLDSA_SEED_BYTES_LEN); return ret; } static int32_t MLDSA_SignArgCheck(CRYPT_ML_DSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { if (ctx == NULL || data == NULL || dataLen == 0 || sign == NULL || signLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (ctx->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_SET); return CRYPT_MLDSA_KEY_NOT_SET; } if (*signLen < ctx->info->signatureLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_LEN_NOT_ENOUGH); return CRYPT_MLDSA_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } static int32_t MLDSA_VerifyArgCheck(CRYPT_ML_DSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t signLen) { if (ctx == NULL || data == NULL || dataLen == 0 || sign == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (ctx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_SET); return CRYPT_MLDSA_KEY_NOT_SET; } if (signLen != ctx->info->signatureLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_LEN_NOT_ENOUGH); return CRYPT_MLDSA_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_SetPrvKey(CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPrv *prv) { if (ctx == NULL || prv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (prv->len != ctx->info->privateKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYLEN_ERROR); return CRYPT_MLDSA_KEYLEN_ERROR; } if (ctx->prvKey != NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_SET_KEY_FAILED); return CRYPT_MLDSA_SET_KEY_FAILED; } ctx->prvKey = BSL_SAL_Malloc(ctx->info->privateKeyLen); if (ctx->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->prvLen = ctx->info->privateKeyLen; (void)memcpy_s(ctx->prvKey, ctx->prvLen, prv->data, prv->len); return CRYPT_SUCCESS; } #ifdef HITLS_BSL_PARAMS int32_t CRYPT_ML_DSA_SetPrvKeyEx(CRYPT_ML_DSA_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_MlDsaPrv prv = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_ML_DSA_PRVKEY, &prv.data, &prv.len); return CRYPT_ML_DSA_SetPrvKey(ctx, &prv); } int32_t CRYPT_ML_DSA_SetPubKeyEx(CRYPT_ML_DSA_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_MlDsaPub pub = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_ML_DSA_PUBKEY, &pub.data, &pub.len); return CRYPT_ML_DSA_SetPubKey(ctx, &pub); } int32_t CRYPT_ML_DSA_GetPrvKeyEx(const CRYPT_ML_DSA_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_MlDsaPrv prv = {0}; BSL_Param *paramPrv = GetParamValue(para, CRYPT_PARAM_ML_DSA_PRVKEY, &prv.data, &(prv.len)); int32_t ret = CRYPT_ML_DSA_GetPrvKey(ctx, &prv); if (ret != CRYPT_SUCCESS) { return ret; } paramPrv->useLen = prv.len; return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_GetPubKeyEx(const CRYPT_ML_DSA_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_MlDsaPub pub = {0}; BSL_Param *paramPub = GetParamValue(para, CRYPT_PARAM_ML_DSA_PUBKEY, &pub.data, &(pub.len)); int32_t ret = CRYPT_ML_DSA_GetPubKey(ctx, &pub); if (ret != CRYPT_SUCCESS) { return ret; } paramPub->useLen = pub.len; return CRYPT_SUCCESS; } #endif int32_t CRYPT_ML_DSA_SetPubKey(CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPub *pub) { if (ctx == NULL || pub == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (pub->len != ctx->info->publicKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYLEN_ERROR); return CRYPT_MLDSA_KEYLEN_ERROR; } if (ctx->pubKey != NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_SET_KEY_FAILED); return CRYPT_MLDSA_SET_KEY_FAILED; } ctx->pubKey = BSL_SAL_Malloc(ctx->info->publicKeyLen); if (ctx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->pubLen = ctx->info->publicKeyLen; (void)memcpy_s(ctx->pubKey, ctx->pubLen, pub->data, pub->len); return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_GetPrvKey(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPrv *prv) { if (ctx == NULL || prv == NULL || prv->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_SET); return CRYPT_MLDSA_KEY_NOT_SET; } if (memcpy_s(prv->data, prv->len, ctx->prvKey, ctx->prvLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_LEN_NOT_ENOUGH); return CRYPT_MLDSA_LEN_NOT_ENOUGH; } prv->len = ctx->prvLen; return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_GetPubKey(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_MlDsaPub *pub) { if (ctx == NULL || pub == NULL || pub->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_SET); return CRYPT_MLDSA_KEY_NOT_SET; } if (memcpy_s(pub->data, pub->len, ctx->pubKey, ctx->pubLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_LEN_NOT_ENOUGH); return CRYPT_MLDSA_LEN_NOT_ENOUGH; } pub->len = ctx->pubLen; return CRYPT_SUCCESS; } static int32_t MLDSACmpKey(uint8_t *a, uint32_t aLen, uint8_t *b, uint32_t bLen) { if (aLen != bLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_EQUAL); return CRYPT_MLDSA_KEY_NOT_EQUAL; } if (a != NULL && b != NULL) { if (memcmp(a, b, aLen) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_EQUAL); return CRYPT_MLDSA_KEY_NOT_EQUAL; } } if (a == NULL && b == NULL) { return CRYPT_SUCCESS; } if (a == NULL || b == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_EQUAL); return CRYPT_MLDSA_KEY_NOT_EQUAL; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_Cmp(const CRYPT_ML_DSA_Ctx *a, const CRYPT_ML_DSA_Ctx *b) { if (a == NULL || b == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (a->info != b->info) { // The value of info must be one of the g_mldsaInfo arrays. BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEY_NOT_EQUAL); return CRYPT_MLDSA_KEY_NOT_EQUAL; } if (MLDSACmpKey(a->prvKey, a->prvLen, b->prvKey, b->prvLen) != CRYPT_SUCCESS) { return CRYPT_MLDSA_KEY_NOT_EQUAL; } if (MLDSACmpKey(a->pubKey, a->pubLen, b->pubKey, b->pubLen) != CRYPT_SUCCESS) { return CRYPT_MLDSA_KEY_NOT_EQUAL; } return CRYPT_SUCCESS; } static uint32_t MLDSAGetMdSize(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 MLDSAPreHashEncode(CRYPT_ML_DSA_Ctx *ctx, int32_t hashId, const uint8_t *data, uint32_t dataLen, CRYPT_Data *msg) { int32_t ret = CRYPT_SUCCESS; // The maximum value of ctx->ctxLen is 255. if (dataLen > (UINT32_MAX - MLDSA_SIGN_PREFIX_BYTES - ctx->ctxLen)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } BslOidString *oidInfo = BSL_OBJ_GetOID(hashId); RETURN_RET_IF(oidInfo == NULL, CRYPT_ERR_ALGID); const EAL_MdMethod *hashMethod = EAL_MdFindDefaultMethod(hashId); RETURN_RET_IF(hashMethod == NULL, CRYPT_EAL_ALG_NOT_SUPPORT); uint32_t mdSize = MLDSAGetMdSize(hashMethod, hashId); msg->len = MLDSA_SIGN_PREFIX_BYTES + ctx->ctxLen + MLDSA_SIGN_PREFIX_BYTES + oidInfo->octetLen + mdSize; msg->data = BSL_SAL_Malloc(msg->len); RETURN_RET_IF(msg->data == NULL, CRYPT_MEM_ALLOC_FAIL); uint8_t *ptr = msg->data; uint32_t tmpLen = msg->len; ptr[0] = 1; ptr[1] = (uint8_t)ctx->ctxLen; ptr += MLDSA_SIGN_PREFIX_BYTES; tmpLen -= MLDSA_SIGN_PREFIX_BYTES; if (ctx->ctxInfo != NULL && ctx->ctxLen > 0) { (void)memcpy_s(ptr, msg->len - MLDSA_SIGN_PREFIX_BYTES, ctx->ctxInfo, ctx->ctxLen); ptr += ctx->ctxLen; tmpLen -= ctx->ctxLen; } ptr[0] = 0x06; // tag of objectId ptr[1] = (uint8_t)oidInfo->octetLen; ptr += MLDSA_SIGN_PREFIX_BYTES; tmpLen -= MLDSA_SIGN_PREFIX_BYTES; (void)memcpy_s(ptr, tmpLen, oidInfo->octs, oidInfo->octetLen); ptr += oidInfo->octetLen; tmpLen -= oidInfo->octetLen; void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { BSL_SAL_Free(msg->data); BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, data, dataLen), ret); GOTO_ERR_IF(hashMethod->final(mdCtx, ptr, &tmpLen), ret); ERR: hashMethod->freeCtx(mdCtx); if (ret != CRYPT_SUCCESS) { BSL_SAL_FREE(msg->data); } return ret; } static int32_t MLDSAEncodeInputData(CRYPT_ML_DSA_Ctx *ctx, int32_t hashId, const uint8_t *data, uint32_t dataLen, CRYPT_Data *msg) { int32_t ret; if (ctx->isMuMsg || ctx->needEncodeCtx == false) { msg->data = BSL_SAL_Dump(data, dataLen); RETURN_RET_IF(msg->data == NULL, CRYPT_MEM_ALLOC_FAIL); msg->len = dataLen; return CRYPT_SUCCESS; } // The maximum value of ctx->ctxLen is 255. if (dataLen > (UINT32_MAX - MLDSA_SIGN_PREFIX_BYTES - ctx->ctxLen)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ctx->needPreHash) { RETURN_RET_IF_ERR(MLDSAPreHashEncode(ctx, hashId, data, dataLen, msg), ret); return CRYPT_SUCCESS; } msg->len = dataLen + ctx->ctxLen + MLDSA_SIGN_PREFIX_BYTES; msg->data = BSL_SAL_Malloc(msg->len); if (msg->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } msg->data[0] = 0; msg->data[1] = (uint8_t)ctx->ctxLen; if (ctx->ctxInfo != NULL && ctx->ctxLen > 0) { (void)memcpy_s(msg->data + MLDSA_SIGN_PREFIX_BYTES, msg->len - MLDSA_SIGN_PREFIX_BYTES, ctx->ctxInfo, ctx->ctxLen); } (void)memcpy_s(msg->data + MLDSA_SIGN_PREFIX_BYTES + ctx->ctxLen, msg->len - MLDSA_SIGN_PREFIX_BYTES - ctx->ctxLen, data, dataLen); return CRYPT_SUCCESS; } // Algorithm 4 HashML-DSA.Sign(sk, M, ctx, PH) int32_t CRYPT_ML_DSA_Sign(CRYPT_ML_DSA_Ctx *ctx, int32_t hashId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t *signLen) { int32_t ret = MLDSA_SignArgCheck(ctx, data, dataLen, sign, signLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint8_t signSeed[MLDSA_SEED_BYTES_LEN] = { 0 }; if (ctx->deterministicSignFlag == false) { ret = CRYPT_RandEx(ctx->libCtx, signSeed, MLDSA_SEED_BYTES_LEN); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); } CRYPT_Data msg = { 0 }; RETURN_RET_IF_ERR(MLDSAEncodeInputData(ctx, hashId, data, dataLen, &msg), ret); ret = MLDSA_SignInternal(ctx, &msg, sign, signLen, signSeed); BSL_SAL_Free(msg.data); BSL_SAL_CleanseData(signSeed, sizeof(signSeed)); return ret; } // Algorithm 5 HashML-DSA.Verify(pk, M, 𝜎, ctx, PH) int32_t CRYPT_ML_DSA_Verify(CRYPT_ML_DSA_Ctx *ctx, int32_t hashId, const uint8_t *data, uint32_t dataLen, uint8_t *sign, uint32_t signLen) { int32_t ret = MLDSA_VerifyArgCheck(ctx, data, dataLen, sign, signLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } CRYPT_Data msg = { 0 }; RETURN_RET_IF_ERR(MLDSAEncodeInputData(ctx, hashId, data, dataLen, &msg), ret); ret = MLDSA_VerifyInternal(ctx, &msg, sign, signLen); BSL_SAL_Free(msg.data); return ret; } #ifdef HITLS_CRYPTO_MLDSA_CHECK static int32_t MLDSAKeyPairCheck(const CRYPT_ML_DSA_Ctx *pubKey, const CRYPT_ML_DSA_Ctx *prvKey) { if (pubKey == NULL || prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pubKey->info == NULL || prvKey->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (pubKey->info->secBits != prvKey->info->secBits) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_PAIRWISE_CHECK_FAIL); return CRYPT_MLDSA_PAIRWISE_CHECK_FAIL; } if (pubKey->pubKey == NULL || pubKey->pubLen != pubKey->info->publicKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_INVALID_PUBKEY); return CRYPT_MLDSA_INVALID_PUBKEY; } if (prvKey->prvKey == NULL || prvKey->prvLen != prvKey->info->privateKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_INVALID_PRVKEY); return CRYPT_MLDSA_INVALID_PRVKEY; } uint8_t *pub = BSL_SAL_Malloc(pubKey->info->publicKeyLen); if (pub == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret = MLDSA_CalPub(prvKey, pub, pubKey->info->publicKeyLen); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(pub); return ret; } if (memcmp(pub, pubKey->pubKey, pubKey->info->publicKeyLen) != 0) { BSL_SAL_Free(pub); BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_PAIRWISE_CHECK_FAIL); return CRYPT_MLDSA_PAIRWISE_CHECK_FAIL; } BSL_SAL_Free(pub); return CRYPT_SUCCESS; } static int32_t MLDSAPrvKeyCheck(const CRYPT_ML_DSA_Ctx *prvKey) { if (prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prvKey->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_KEYINFO_NOT_SET); return CRYPT_MLDSA_KEYINFO_NOT_SET; } if (prvKey->prvKey == NULL || prvKey->prvLen != prvKey->info->privateKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_INVALID_PRVKEY); return CRYPT_MLDSA_INVALID_PRVKEY; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_DSA_Check(uint32_t checkType, const CRYPT_ML_DSA_Ctx *pkey1, const CRYPT_ML_DSA_Ctx *pkey2) { switch (checkType) { case CRYPT_PKEY_CHECK_KEYPAIR: return MLDSAKeyPairCheck(pkey1, pkey2); case CRYPT_PKEY_CHECK_PRVKEY: return MLDSAPrvKeyCheck(pkey1); default: BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } } #endif // HITLS_CRYPTO_MLDSA_CHECK #endif
2301_79861745/bench_create
crypto/mldsa/src/ml_dsa.c
C
unknown
26,554
/* * 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_MLDSA #include "securec.h" #include "bsl_errno.h" #include "bsl_sal.h" #include "crypt_utils.h" #include "crypt_sha3.h" #include "crypt_errno.h" #include "crypt_util_rand.h" #include "bsl_err_internal.h" #include "ml_dsa_local.h" #include "eal_md_local.h" #define BITS_OF_BYTE 8 #define MLDSA_SET_VECTOR_MEM(ptr, buf) {ptr = buf; buf += MLDSA_N;} static int32_t HashFuncH(const uint8_t *inPutA, uint32_t lenA, const uint8_t *inPutB, uint32_t lenB, uint8_t *out, uint32_t outLen) { uint32_t len = outLen; int32_t ret = 0; const EAL_MdMethod *hashMethod = EAL_MdFindDefaultMethod(CRYPT_MD_SHAKE256); if (hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_NOT_SUPPORT); return CRYPT_EAL_ALG_NOT_SUPPORT; } void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, inPutA, lenA), ret); if (inPutB != NULL) { GOTO_ERR_IF(hashMethod->update(mdCtx, inPutB, lenB), ret); } GOTO_ERR_IF(hashMethod->final(mdCtx, out, &len), ret); ERR: hashMethod->freeCtx(mdCtx); return ret; } typedef struct { int32_t *bufAddr; uint32_t bufSize; int32_t *matrix[MLDSA_K_MAX][MLDSA_L_MAX]; int32_t *s2[MLDSA_K_MAX]; int32_t *t0[MLDSA_K_MAX]; int32_t *t1[MLDSA_K_MAX]; int32_t *s1[MLDSA_L_MAX]; int32_t *s1Ntt[MLDSA_L_MAX]; } MLDSA_KeyGenMatrixSt; static void MLDSASetMatrixMem(uint8_t k, uint8_t l, int32_t *matrix[MLDSA_K_MAX][MLDSA_L_MAX], int32_t *buf) { for (uint8_t i = 0; i < k; i++) { for (uint8_t j = 0; j < l; j++) { matrix[i][j] = buf; buf += MLDSA_N; } } } static int32_t MLDSAKeyGenCreateMatrix(uint8_t k, uint8_t l, MLDSA_KeyGenMatrixSt *st) { // Key generation requires 3 two-dimensional arrays of length k and 2 of length l. st->bufSize = (k * l + 3 * k + 2 * l) * MLDSA_N * sizeof(int32_t); int32_t *buf = BSL_SAL_Malloc(st->bufSize); if (buf == NULL) { return BSL_MALLOC_FAIL; } st->bufAddr = buf; // Used to free memory. MLDSASetMatrixMem(k, l, st->matrix, buf); buf += k * l * MLDSA_N; for (uint8_t i = 0; i < k; i++) { MLDSA_SET_VECTOR_MEM(st->t0[i], buf); MLDSA_SET_VECTOR_MEM(st->t1[i], buf); MLDSA_SET_VECTOR_MEM(st->s2[i], buf); } for (uint8_t i = 0; i < l; i++) { MLDSA_SET_VECTOR_MEM(st->s1[i], buf); MLDSA_SET_VECTOR_MEM(st->s1Ntt[i], buf); } return CRYPT_SUCCESS; } typedef struct { int32_t *bufAddr; uint32_t bufSize; int32_t *matrix[MLDSA_K_MAX][MLDSA_L_MAX]; int32_t *t0[MLDSA_K_MAX]; int32_t *r0[MLDSA_K_MAX]; int32_t *s2[MLDSA_K_MAX]; int32_t *cs2[MLDSA_K_MAX]; int32_t *ct0[MLDSA_K_MAX]; int32_t *h[MLDSA_K_MAX]; int32_t *w[MLDSA_K_MAX]; int32_t *w1[MLDSA_K_MAX]; int32_t *s1[MLDSA_L_MAX]; int32_t *y[MLDSA_L_MAX]; int32_t *z[MLDSA_L_MAX]; } MLDSA_SignMatrixSt; static int32_t MLDSASignCreateMatrix(uint8_t k, uint8_t l, MLDSA_SignMatrixSt *st) { // The signature requires 8 two-dimensional arrays of length k and 3 of length l. st->bufSize = (k * l + 8 * k + 3 * l) * MLDSA_N * sizeof(int32_t); int32_t *buf = BSL_SAL_Malloc(st->bufSize); if (buf == NULL) { return BSL_MALLOC_FAIL; } st->bufAddr = buf; // Used to free memory. MLDSASetMatrixMem(k, l, st->matrix, buf); buf += k * l * MLDSA_N; for (uint8_t i = 0; i < k; i++) { MLDSA_SET_VECTOR_MEM(st->r0[i], buf); MLDSA_SET_VECTOR_MEM(st->t0[i], buf); MLDSA_SET_VECTOR_MEM(st->s2[i], buf); MLDSA_SET_VECTOR_MEM(st->cs2[i], buf); MLDSA_SET_VECTOR_MEM(st->ct0[i], buf); MLDSA_SET_VECTOR_MEM(st->h[i], buf); MLDSA_SET_VECTOR_MEM(st->w[i], buf); MLDSA_SET_VECTOR_MEM(st->w1[i], buf); } for (uint8_t i = 0; i < l; i++) { MLDSA_SET_VECTOR_MEM(st->s1[i], buf); MLDSA_SET_VECTOR_MEM(st->y[i], buf); MLDSA_SET_VECTOR_MEM(st->z[i], buf); } return CRYPT_SUCCESS; } typedef struct { int32_t *bufAddr; uint32_t bufSize; int32_t *matrix[MLDSA_K_MAX][MLDSA_L_MAX]; int32_t *t1[MLDSA_K_MAX]; int32_t *h[MLDSA_K_MAX]; int32_t *w[MLDSA_K_MAX]; int32_t *z[MLDSA_L_MAX]; } MLDSA_VerifyMatrixSt; static int32_t MLDSAVerifyCreateMatrix(uint8_t k, uint8_t l, MLDSA_VerifyMatrixSt *st) { // Signature verification requires 3 two-dimensional arrays of length k and 1 of length l. st->bufSize = (k * l + 3 * k + l) * MLDSA_N * sizeof(int32_t); int32_t *buf = BSL_SAL_Malloc(st->bufSize); if (buf == NULL) { return BSL_MALLOC_FAIL; } st->bufAddr = buf; // Used to free memory. MLDSASetMatrixMem(k, l, st->matrix, buf); buf += k * l * MLDSA_N; for (uint8_t i = 0; i < k; i++) { MLDSA_SET_VECTOR_MEM(st->t1[i], buf); MLDSA_SET_VECTOR_MEM(st->h[i], buf); MLDSA_SET_VECTOR_MEM(st->w[i], buf); } for (uint8_t i = 0; i < l; i++) { MLDSA_SET_VECTOR_MEM(st->z[i], buf); } return CRYPT_SUCCESS; } // NIST.FIPS.204 Algorithm 14 CoeffFromThreeBytes(b0, b1, b2) static int32_t CoeffFromThreeBytes(uint8_t b0, uint8_t b1, uint8_t b2) { uint8_t b = b2; if (b > 0x7f) { b = b - 0x80; } // 𝑧 ← 2^16 ⋅ b2′ + 2^8 ⋅ b1 + b0 return (((int32_t)b << 16) | ((int32_t)b1 << 8)) | b0; } // NIST.FIPS.204 Algorithm 30 RejNTTPoly(ρ) static int32_t RejNTTPoly(int32_t a[MLDSA_N], uint8_t seed[MLDSA_SEED_EXTEND_BYTES_LEN]) { int32_t ret; unsigned int buflen = CRYPT_SHAKE128_BLOCKSIZE; uint8_t buf[CRYPT_SHAKE128_BLOCKSIZE]; const EAL_MdMethod *hashMethod = EAL_MdFindDefaultMethod(CRYPT_MD_SHAKE128); if (hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_NOT_SUPPORT); return CRYPT_EAL_ALG_NOT_SUPPORT; } void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, seed, MLDSA_SEED_EXTEND_BYTES_LEN), ret); GOTO_ERR_IF(hashMethod->squeeze(mdCtx, buf, buflen), ret); uint32_t j = 0; for (uint32_t i = 0; i < MLDSA_N;) { a[i] = CoeffFromThreeBytes(buf[j], buf[j + 1], buf[j + 2]); // Data from 3 uint8_t to int32_t. j += 3; if (a[i] < MLDSA_Q) { // a[i] is less than MLDSA_Q is an invalid value. i++; } if (j >= CRYPT_SHAKE128_BLOCKSIZE) { GOTO_ERR_IF(hashMethod->squeeze(mdCtx, buf, buflen), ret); j = 0; } } ERR: hashMethod->freeCtx(mdCtx); return ret; } // NIST.FIPS.204 Algorithm 32 ExpandA(ρ) static int32_t ExpandA(const CRYPT_ML_DSA_Ctx *ctx, const uint8_t *pubSeed, int32_t *matrix[MLDSA_K_MAX][MLDSA_L_MAX]) { uint8_t k = ctx->info->k; uint8_t l = ctx->info->l; uint8_t seed[MLDSA_SEED_EXTEND_BYTES_LEN]; (void)memcpy_s(seed, sizeof(seed), pubSeed, MLDSA_PUBLIC_SEED_LEN); for (uint8_t i = 0; i < k; i++) { for (uint8_t j = 0; j < l; j++) { seed[MLDSA_PUBLIC_SEED_LEN] = j; seed[MLDSA_PUBLIC_SEED_LEN + 1] = i; int32_t ret = RejNTTPoly(matrix[i][j], seed); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); } } return CRYPT_SUCCESS; } // NIST.FIPS.204 Algorithm 31 RejBoundedPoly(ρ) static int32_t RejBoundedPoly(const CRYPT_ML_DSA_Ctx *ctx, int32_t *a, uint8_t *s) { uint8_t buf[CRYPT_SHAKE256_BLOCKSIZE]; uint32_t bufLen = CRYPT_SHAKE256_BLOCKSIZE; int32_t ret = CRYPT_SUCCESS; const EAL_MdMethod *hashMethod = EAL_MdFindDefaultMethod(CRYPT_MD_SHAKE256); if (hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_NOT_SUPPORT); return CRYPT_EAL_ALG_NOT_SUPPORT; } void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, s, MLDSA_PRIVATE_SEED_LEN + 2), ret); // k and l used 2 bytes. GOTO_ERR_IF(hashMethod->squeeze(mdCtx, buf, bufLen), ret); for (uint32_t i = 0, j = 0; i < MLDSA_N; j++) { if (j == CRYPT_SHAKE256_BLOCKSIZE) { GOTO_ERR_IF(hashMethod->squeeze(mdCtx, buf, CRYPT_SHAKE256_BLOCKSIZE), ret); j = 0; } int32_t z0 = (int32_t)(buf[j] & 0x0F); int32_t z1 = (int32_t)(buf[j] >> 4u); // Algorithm 15 CoeffFromHalfByte(b) // if 𝜂 = 2 and b < 15 then return 2 − (b mod 5) if (ctx->info->eta == 2) { if (z0 < 0x0F) { // This is Barrett Modular Multiplication, 205 == 2^10 / 5 z0 = z0 - ((205 * z0) >> 10) * 5; // 2 − (b mod 5) a[i] = 2 - z0; i++; } if (z1 < 0x0F && i < MLDSA_N) { // Barrett Modular Multiplication, 205 == 2^10 / 5 z1 = z1 - ((205 * z1) >> 10) * 5; a[i] = 2 - z1; // 2 − (b mod 5) i++; } } else { if (z0 < 9) { // if 𝜂 = 4 and b < 9 then a[i] = 4 − b a[i] = 4 - z0; i++; } if (z1 < 9 && i < MLDSA_N) { // if 𝜂 = 4 and b < 9 then a[i + 1] = 4 − b a[i] = 4 - z1; i++; } } } ERR: hashMethod->freeCtx(mdCtx); return ret; } // Algorithm 33 ExpandS(ρ) static int32_t ExpandS(const CRYPT_ML_DSA_Ctx *ctx, const uint8_t *prvSeed, int32_t *s1[MLDSA_L_MAX], int32_t *s2[MLDSA_K_MAX]) { int32_t ret; uint8_t k = ctx->info->k; uint8_t l = ctx->info->l; uint8_t seed[MLDSA_PRIVATE_SEED_LEN + 2]; // 2 bytes are reserved. (void)memcpy_s(seed, sizeof(seed), prvSeed, MLDSA_PRIVATE_SEED_LEN); seed[MLDSA_PRIVATE_SEED_LEN + 1] = 0; for (uint8_t i = 0; i < l; i++) { seed[MLDSA_PRIVATE_SEED_LEN] = i; ret = RejBoundedPoly(ctx, s1[i], seed); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); } for (uint8_t i = 0; i < k; i++) { seed[MLDSA_PRIVATE_SEED_LEN] = l + i; ret = RejBoundedPoly(ctx, s2[i], seed); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); } return CRYPT_SUCCESS; } static void ComputesNTT(const CRYPT_ML_DSA_Ctx *ctx, int32_t *s[MLDSA_L_MAX], int32_t *sOut[MLDSA_L_MAX]) { for (uint8_t i = 0; i < ctx->info->l; i++) { (void)memcpy_s(sOut[i], sizeof(int32_t) * MLDSA_N, s[i], sizeof(int32_t) * MLDSA_N); MLDSA_ComputesNTT(sOut[i]); } return; } static void VectorsMul(int32_t *t, int32_t *matrix, int32_t *s) { for (uint32_t i = 0; i < MLDSA_N; i++) { t[i] = MLDSA_MontgomeryReduce((int64_t)matrix[i] * s[i]); } } static void MatrixMul(const CRYPT_ML_DSA_Ctx *ctx, int32_t *t, int32_t *matrix[MLDSA_L_MAX], int32_t *s[MLDSA_L_MAX]) { int32_t tmp[MLDSA_N] = { 0 }; VectorsMul(t, matrix[0], s[0]); for (uint32_t i = 1; i < ctx->info->l; i++) { VectorsMul(tmp, matrix[i], s[i]); for (uint32_t j = 0; j < MLDSA_N; j++) { t[j] = t[j] + tmp[j]; } } for (uint32_t j = 0; j < MLDSA_N; j++) { MLDSA_MOD_Q(t[j]); } } static void ComputesT(const CRYPT_ML_DSA_Ctx *ctx, int32_t *t[MLDSA_K_MAX], int32_t *matrix[MLDSA_K_MAX][MLDSA_L_MAX], int32_t *s1[MLDSA_L_MAX], int32_t *s2[MLDSA_K_MAX]) { for (uint8_t i = 0; i < ctx->info->k; i++) { MatrixMul(ctx, t[i], matrix[i], s1); MLDSA_ComputesINVNTT(t[i]); for (int32_t j = 0; j < MLDSA_N; j++) { t[i][j] = t[i][j] + s2[i][j]; t[i][j] = t[i][j] < 0 ? (t[i][j] + MLDSA_Q) : t[i][j]; } } } static void ComputesPower2Round(const CRYPT_ML_DSA_Ctx *ctx, int32_t *t0[MLDSA_K_MAX], int32_t *t1[MLDSA_K_MAX]) { for (uint32_t i = 0; i < ctx->info->k; i++) { for (int32_t j = 0; j < MLDSA_N; j++) { int32_t t = (t1[i][j] + (1 << (MLDSA_D - 1)) - 1) >> MLDSA_D; t0[i][j] = t1[i][j] - (t << MLDSA_D); t1[i][j] = t; } } } // The following encoding function encodes MLDSA_N int32_t data into the uint8_t array. static void ByteEncode(uint8_t *buf, uint32_t *t, uint32_t bits) { if (bits == 10u) { for (uint32_t i = 0; i < MLDSA_N / 4; i++) { buf[5 * i + 0] = (uint8_t)(t[4 * i + 0] >> 0); buf[5 * i + 1u] = (uint8_t)((t[4 * i + 0] >> 8u) | (t[4 * i + 1u] << 2u)); buf[5 * i + 2u] = (uint8_t)((t[4 * i + 1u] >> 6u) | (t[4 * i + 2u] << 4u)); buf[5 * i + 3u] = (uint8_t)((t[4 * i + 2u] >> 4u) | (t[4 * i + 3u] << 6u)); buf[5 * i + 4u] = (uint8_t)(t[4 * i + 3u] >> 2u); } } else if (bits == 6u) { for (uint32_t i = 0; i < MLDSA_N / 4; i++) { buf[3 * i + 0] = (uint8_t)(t[4 * i] | (t[4 * i + 1] << 6u)); buf[3 * i + 1u] = (uint8_t)(t[4 * i + 1u] >> 2 | (t[4 * i + 2u] << 4u)); buf[3 * i + 2u] = (uint8_t)(t[4 * i + 2u] >> 4 | (t[4 * i + 3u] << 2u)); } } else if (bits == 4u) { for (uint32_t i = 0; i < MLDSA_N / 2; i++) { buf[i] = (uint8_t)(t[2 * i] | (t[2 * i + 1] << 4u)); } } } static void ByteDecode(uint8_t *buf, uint32_t *t, uint32_t bits) { if (bits == 10u) { for (uint32_t i = 0; i < MLDSA_N / 4; i++) { t[4 * i + 0] = (buf[5 * i + 0] | ((uint32_t)buf[5 * i + 1] << 8)) & 0x03ff; t[4 * i + 1u] = ((buf[5 * i + 1u] >> 2u) | ((uint32_t)buf[5 * i + 2u] << 6u)) & 0x03ff; t[4 * i + 2u] = ((buf[5 * i + 2u] >> 4u) | ((uint32_t)buf[5 * i + 3u] << 4u)) & 0x03ff; t[4 * i + 3u] = ((buf[5 * i + 3u] >> 6u) | ((uint32_t)buf[5 * i + 4u] << 2u)) & 0x03ff; } } } static void BitPack(uint8_t *buf, uint32_t w[MLDSA_N], uint32_t bits, uint32_t b) { uint32_t t[8] = {0}; uint32_t i; uint32_t n; if (bits == 3u) { for (i = 0; i < MLDSA_N / 8; i++) { for (uint32_t j = 0; j < 8; j++) { t[j] = b - (uint32_t)w[i * 8 + j]; } n = bits * i; buf[n + 0] = (uint8_t)((t[0]) | (t[1] << 3u) | (t[2] << 6u)); buf[n + 1u] = (uint8_t)((t[2] >> 2u) | (t[3] << 1u) | (t[4] << 4u) | (t[5] << 7u)); buf[n + 2u] = (uint8_t)((t[5] >> 1u) | (t[6] << 2u) | (t[7] << 5u)); } } else if (bits == 4u) { for (i = 0; i < MLDSA_N / 2; i++) { t[0] = (int32_t)b - w[i * 2]; t[1] = (int32_t)b - w[i * 2 + 1]; buf[i] = (uint8_t)(t[0] | (t[1] << 4u)); } } else if (bits == MLDSA_D) { for (i = 0; i < MLDSA_N / 8; i++) { for (uint32_t j = 0; j < 8; j++) { t[j] = b - w[i * 8 + j]; } n = bits * i; buf[n + 0] = (uint8_t)t[0]; buf[n + 1] = (uint8_t)(t[0] >> 8u); buf[n + 1] |= (uint8_t)(t[1] << 5u); buf[n + 2] = (uint8_t)(t[1] >> 3u); buf[n + 3] = (uint8_t)(t[1] >> 11u); buf[n + 3] |= (uint8_t)(t[2] << 2u); buf[n + 4] = (uint8_t)(t[2] >> 6u); buf[n + 4] |= (uint8_t)(t[3] << 7u); buf[n + 5] = (uint8_t)(t[3] >> 1u); buf[n + 6] = (uint8_t)(t[3] >> 9u); buf[n + 6] |= (uint8_t)(t[4] << 4u); buf[n + 7] = (uint8_t)(t[4] >> 4u); buf[n + 8] = (uint8_t)(t[4] >> 12u); buf[n + 8] |= (uint8_t)(t[5] << 1u); buf[n + 9] = (uint8_t)(t[5] >> 7u); buf[n + 9] |= (uint8_t)(t[6] << 6u); buf[n + 10] = (uint8_t)(t[6] >> 2u); buf[n + 11] = (uint8_t)(t[6] >> 10u); buf[n + 11] |= (uint8_t)(t[7] << 3u); buf[n + 12] = (uint8_t)(t[7] >> 5u); } } // bits has only this three values. return; } static void BitUnPake(const uint8_t *v, uint32_t w[MLDSA_N], uint32_t bits, uint32_t b) { uint32_t t[8] = {0}; uint32_t i; uint32_t n; if (bits == 3u) { for (i = 0; i < MLDSA_N / 8; i++) { n = bits * i; t[0] = (v[n + 0]) & 0x07; t[1] = (v[n + 0] >> 3u) & 0x07; t[2] = ((v[n + 0] >> 6u) | (v[n + 1] << 2u)) & 0x07; t[3] = (v[n + 1u] >> 1u) & 0x07; t[4] = (v[n + 1u] >> 4u) & 0x07; t[5] = ((v[n + 1u] >> 7u) | (v[n + 2] << 1u)) & 0x07; t[6] = (v[n + 2u] >> 2u) & 0x07; t[7] = (v[n + 2u] >> 5u) & 0x07; for (uint32_t j = 0; j < 8; j++) { w[i * 8 + j] = b - t[j]; } } } else if (bits == 4u) { for (i = 0; i < MLDSA_N / 2; i++) { t[0] = v[i] & 0x0f; t[1] = (v[i] >> 4u) & 0x0f; w[i * 2] = b - t[0]; w[i * 2 + 1] = b - t[1]; } } else if (bits == MLDSA_D) { for (i = 0; i < MLDSA_N / 8; i++) { n = bits * i; t[0] = (v[n + 0] | ((uint32_t)v[n + 1] << 8u)) & 0x1fff; t[1] = (v[n + 1] >> 5u | ((uint32_t)v[n + 2u] << 3u) | ((uint32_t)v[n + 3u] << 11u)) & 0x1fff; t[2] = (v[n + 3u] >> 2u | ((uint32_t)v[n + 4u] << 6u)) & 0x1fff; t[3] = (v[n + 4u] >> 7u | ((uint32_t)v[n + 5u] << 1u) | ((uint32_t)v[n + 6u] << 9u)) & 0x1fff; t[4] = (v[n + 6u] >> 4u | ((uint32_t)v[n + 7u] << 4u) | ((uint32_t)v[n + 8u] << 12u)) & 0x1fff; t[5] = (v[n + 8u] >> 1u | ((uint32_t)v[n + 9u] << 7u)) & 0x1fff; t[6] = (v[n + 9u] >> 6u | ((uint32_t)v[n + 10u] << 2u) | ((uint32_t)v[n + 11u] << 10u)) & 0x1fff; t[7] = (v[n + 11u] >> 3u | ((uint32_t)v[n + 12u] << 5u)) & 0x1fff; for (uint32_t j = 0; j < 8; j++) { w[i * 8 + j] = b - t[j]; } } } // bits has only this three values. return; } static void SignBitPack(uint8_t *buf, uint32_t w[MLDSA_N], uint32_t bits, uint32_t b) { uint32_t t[4] = {0}; uint32_t i; uint32_t n; if (bits == GAMMA_BITS_OF_MLDSA_44) { for (i = 0; i < MLDSA_N / 4; i++) { for (uint32_t j = 0; j < 4; j++) { t[j] = b - w[i * 4 + j]; } n = 9 * i; buf[n + 0] = (uint8_t)t[0]; buf[n + 1u] = (uint8_t)(t[0] >> 8u); buf[n + 2u] = (uint8_t)(t[0] >> 16u | t[1] << 2u); buf[n + 3u] = (uint8_t)(t[1] >> 6u); buf[n + 4u] = (uint8_t)(t[1] >> 14u | t[2] << 4u); buf[n + 5u] = (uint8_t)(t[2] >> 4u); buf[n + 6u] = (uint8_t)(t[2] >> 12u | t[3] << 6u); buf[n + 7u] = (uint8_t)(t[3] >> 2u); buf[n + 8u] = (uint8_t)(t[3] >> 10u); } } else if (bits == GAMMA_BITS_OF_MLDSA_65_87) { for (i = 0; i < MLDSA_N / 2; i++) { t[0] = b - w[i * 2]; t[1] = b - w[i * 2 + 1u]; n = 5 * i; buf[n + 0] = (uint8_t)t[0]; buf[n + 1u] = (uint8_t)(t[0] >> 8u); buf[n + 2u] = (uint8_t)(t[0] >> 16u | t[1] << 4u); buf[n + 3u] = (uint8_t)(t[1] >> 4u); buf[n + 4u] = (uint8_t)(t[1] >> 12u); } } // bits has only this two values. return; } static void SignBitUnPake(const uint8_t *v, uint32_t w[MLDSA_N], uint32_t bits, uint32_t b) { uint32_t t[4] = {0}; uint32_t i; uint32_t n; if (bits == GAMMA_BITS_OF_MLDSA_44) { for (i = 0; i < MLDSA_N / 4; i++) { n = 9 * i; t[0] = (v[n + 0] | ((uint32_t)v[n + 1] << 8) | ((uint32_t)v[n + 2] << 16)) & 0x3ffff; t[1] = (v[n + 2u] >> 2u | ((uint32_t)v[n + 3u] << 6u) | ((uint32_t)v[n + 4u] << 14u)) & 0x3ffff; t[2] = (v[n + 4u] >> 4u | ((uint32_t)v[n + 5u] << 4u) | ((uint32_t)v[n + 6u] << 12u)) & 0x3ffff; t[3] = (v[n + 6u] >> 6u | ((uint32_t)v[n + 7u] << 2u) | ((uint32_t)v[n + 8u] << 10u)) & 0x3ffff; n = 4 * i; w[n] = b - t[0]; w[n + 1u] = b - t[1]; w[n + 2u] = b - t[2]; w[n + 3u] = b - t[3]; } } else if (bits == GAMMA_BITS_OF_MLDSA_65_87) { for (i = 0; i < MLDSA_N / 2; i++) { n = 5 * i; t[0] = (v[n + 0] | ((uint32_t)v[n + 1] << 8u) | ((uint32_t)v[n + 2u] << 16u)) & 0xfffff; t[1] = (v[n + 2u] >> 4u | ((uint32_t)v[n + 3u] << 4u) | ((uint32_t)v[n + 4u] << 12u)) & 0xfffff; w[i * 2] = b - t[0]; w[i * 2 + 1u] = b - t[1]; } } // bits has only this two values. return; } // Algorithm 22 pkEncode(ρ, t1) static void PkEncode(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *seed, int32_t *t[MLDSA_K_MAX]) { (void)memcpy_s(ctx->pubKey, ctx->pubLen, seed, MLDSA_PUBLIC_SEED_LEN); for (int32_t i = 0; i < ctx->info->k; i++) { // 10 is bitlen(𝑞−1) − d ByteEncode(ctx->pubKey + MLDSA_PUBLIC_SEED_LEN + i * MLDSA_PUBKEY_POLYT_PACKEDBYTES, (uint32_t *)t[i], 10); } } // Algorithm 23 pkDecode(pk) static void PkDecode(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *seed, int32_t *t[MLDSA_K_MAX]) { (void)memcpy_s(seed, MLDSA_PUBLIC_SEED_LEN, ctx->pubKey, MLDSA_PUBLIC_SEED_LEN); for (int32_t i = 0; i < ctx->info->k; i++) { // 10 is bitlen(𝑞−1) − d ByteDecode(ctx->pubKey + MLDSA_PUBLIC_SEED_LEN + i * MLDSA_PUBKEY_POLYT_PACKEDBYTES, (uint32_t *)t[i], 10); } } // Algorithm 24 skEncode(ρ, K,tr, 𝐬1, 𝐬2, t0) static void SkEncode(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *pubSeed, uint8_t *signSeed, uint8_t *tr, MLDSA_KeyGenMatrixSt *st) { uint32_t i; uint32_t bitLen = ctx->info->eta == 2 ? 3 : 4; // 3 and 4 is bitlen(2𝜂) uint32_t index = MLDSA_PUBLIC_SEED_LEN; (void)memcpy_s(ctx->prvKey, ctx->prvLen, pubSeed, MLDSA_PUBLIC_SEED_LEN); (void)memcpy_s(ctx->prvKey + index, ctx->prvLen - index, signSeed, MLDSA_SIGNING_SEED_LEN); index += MLDSA_SIGNING_SEED_LEN; (void)memcpy_s(ctx->prvKey + index, ctx->prvLen - index, tr, MLDSA_PRIVATE_SEED_LEN); index += MLDSA_PRIVATE_SEED_LEN; for (i = 0; i < ctx->info->l; i++) { BitPack(ctx->prvKey + index, (uint32_t *)st->s1[i], bitLen, ctx->info->eta); index += MLDSA_N_BYTE * bitLen; } for (i = 0; i < ctx->info->k; i++) { BitPack(ctx->prvKey + index, (uint32_t *)st->s2[i], bitLen, ctx->info->eta); index += MLDSA_N_BYTE * bitLen; } for (i = 0; i < ctx->info->k; i++) { BitPack(ctx->prvKey + index, (uint32_t *)st->t0[i], MLDSA_D, 4096); // 2^(𝑑−1) == 4096 index += MLDSA_N_BYTE * MLDSA_D; } } // Algorithm 25 skDecode(sk) static void SkDecode(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *pubSeed, uint8_t *signSeed, uint8_t *tr, MLDSA_SignMatrixSt *st) { uint32_t i; uint32_t bitLen = ctx->info->eta == 2 ? 3 : 4; // 3 and 4 is bitlen(2𝜂) uint32_t index = MLDSA_PUBLIC_SEED_LEN; (void)memcpy_s(pubSeed, MLDSA_PUBLIC_SEED_LEN, ctx->prvKey, MLDSA_PUBLIC_SEED_LEN); (void)memcpy_s(signSeed, MLDSA_SIGNING_SEED_LEN, ctx->prvKey + index, MLDSA_SIGNING_SEED_LEN); index += MLDSA_SIGNING_SEED_LEN; (void)memcpy_s(tr, MLDSA_PRIVATE_SEED_LEN, ctx->prvKey + index, MLDSA_PRIVATE_SEED_LEN); index += MLDSA_PRIVATE_SEED_LEN; for (i = 0; i < ctx->info->l; i++) { BitUnPake(ctx->prvKey + index, (uint32_t *)st->s1[i], bitLen, ctx->info->eta); index += MLDSA_N_BYTE * bitLen; } for (i = 0; i < ctx->info->k; i++) { BitUnPake(ctx->prvKey + index, (uint32_t *)st->s2[i], bitLen, ctx->info->eta); index += MLDSA_N_BYTE * bitLen; } for (i = 0; i < ctx->info->k; i++) { BitUnPake(ctx->prvKey + index, (uint32_t *)st->t0[i], MLDSA_D, 4096); // 2^(𝑑−1) == 4096 index += MLDSA_N_BYTE * MLDSA_D; } } static void SignCalNtt(const CRYPT_ML_DSA_Ctx *ctx, MLDSA_SignMatrixSt *st) { uint32_t i; for (i = 0; i < ctx->info->l; i++) { MLDSA_ComputesNTT(st->s1[i]); } for (i = 0; i < ctx->info->k; i++) { MLDSA_ComputesNTT(st->s2[i]); } for (i = 0; i < ctx->info->k; i++) { MLDSA_ComputesNTT(st->t0[i]); } } // Algorithm 34 ExpandMask(ρ, μ) static int32_t ExpandMask(const CRYPT_ML_DSA_Ctx *ctx, int32_t *y[MLDSA_L_MAX], uint8_t *p, uint16_t u) { uint16_t n = 0; uint8_t v[640]; // The maximum length is 20 * 32 == 640 byte. uint32_t bits = (ctx->info->k == K_VALUE_OF_MLDSA_44) ? GAMMA_BITS_OF_MLDSA_44 : GAMMA_BITS_OF_MLDSA_65_87; for (uint16_t i = 0; i < ctx->info->l; i++) { n = u + i; p[MLDSA_PRIVATE_SEED_LEN] = (uint8_t)n; p[MLDSA_PRIVATE_SEED_LEN + 1] = (uint8_t)(n >> BITS_OF_BYTE); // 𝑣 ← H(ρ′, 32𝑐) int32_t ret = HashFuncH(p, MLDSA_PRIVATE_SEED_LEN + 2, NULL, 0, v, 32 * bits); if (ret != CRYPT_SUCCESS) { return ret; } SignBitUnPake(v, (uint32_t *)y[i], bits, ctx->info->gamma1); } return CRYPT_SUCCESS; } // Algorithm 36 Decompose(r) static void Decompose(const CRYPT_ML_DSA_Ctx *ctx, int32_t r, int32_t *r1, int32_t *r0) { int32_t t = (int32_t)(((uint32_t)r + 0x7f) >> 7u); if (ctx->info->k == K_VALUE_OF_MLDSA_44) { // If is MLDSA44 // This is Barrett Modular Multiplication, mod is 2𝛾2. t = (t * 11275u + (1 << 23u)) >> 24u; t ^= ((43 - t) >> 31u) & t; } else { t = (t * 1025u + (1 << 21u)) >> 22u; t &= 0x0f; } *r0 = r - t * 2 * ctx->info->gamma2; // r1 ← (r+ − r0)/(2𝛾2) *r0 -= (((MLDSA_Q - 1) / 2 - *r0) >> 31u) & MLDSA_Q; *r1 = t; // high bits. return; } static void ComputesW(const CRYPT_ML_DSA_Ctx *ctx, int32_t *w[MLDSA_L_MAX], int32_t *w1[MLDSA_L_MAX], int32_t *matrix[MLDSA_K_MAX][MLDSA_L_MAX], int32_t *y[MLDSA_L_MAX]) { for (uint8_t i = 0; i < ctx->info->k; i++) { MatrixMul(ctx, w[i], matrix[i], y); MLDSA_ComputesINVNTT(w[i]); for (int32_t j = 0; j < MLDSA_N; j++) { w[i][j] = w[i][j] < 0 ? (w[i][j] + MLDSA_Q) : w[i][j]; Decompose(ctx, w[i][j], &w1[i][j], &w[i][j]); } } } // Algorithm 28 w1Encode(w1) static void W1Encode(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *buf, int32_t *w[MLDSA_K_MAX]) { uint32_t bitLen = ctx->info->k == K_VALUE_OF_MLDSA_44 ? 6 : 4; // Only the bitLen value of MLDSA44 is 6. uint32_t blockSize = ctx->info->k == K_VALUE_OF_MLDSA_44 ? 192 : 128; // MLDSA44 blockSize is 192, other is 128. for (uint32_t i = 0; i < ctx->info->k; i++) { ByteEncode(buf + i * blockSize, (uint32_t *)w[i], bitLen); } } // Algorithm 29 SampleInBall(ρ) static int32_t SampleInBall(const CRYPT_ML_DSA_Ctx *ctx, const uint8_t *p, uint32_t pLen, int32_t c[MLDSA_N]) { uint8_t s[CRYPT_SHAKE256_BLOCKSIZE] = {0}; uint32_t sLen = CRYPT_SHAKE256_BLOCKSIZE; uint64_t h = 0; uint32_t index = 0; uint8_t j = 0; int32_t ret; const EAL_MdMethod *hashMethod = EAL_MdFindDefaultMethod(CRYPT_MD_SHAKE256); if (hashMethod == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_NOT_SUPPORT); return CRYPT_EAL_ALG_NOT_SUPPORT; } void *mdCtx = hashMethod->newCtx(NULL, hashMethod->id); if (mdCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF(hashMethod->init(mdCtx, NULL), ret); GOTO_ERR_IF(hashMethod->update(mdCtx, p, pLen), ret); GOTO_ERR_IF(hashMethod->squeeze(mdCtx, s, sLen), ret); for (index = 0; index < 8; index++) { // 𝑠 ← H.Squeeze(ctx, 8) h = h | ((uint64_t)s[index] << (8 * index)); } for (uint32_t i = MLDSA_N - ctx->info->tau; i < MLDSA_N; i++) { do { if (index == CRYPT_SHAKE256_BLOCKSIZE) { GOTO_ERR_IF(hashMethod->squeeze(mdCtx, s, sLen), ret); index = 0; } j = s[index]; index++; } while (j > i); c[i] = c[j]; c[j] = 1 - ((h & 1) << 1); h >>= 1; } ERR: hashMethod->freeCtx(mdCtx); return ret; } static void MLDSA_VectorsAdd(int32_t *t, int32_t *a, int32_t *b) { for (uint32_t i = 0; i < MLDSA_N; i++) { t[i] = a[i] + b[i]; MLDSA_MOD_Q(t[i]); } } static void MLDSA_VectorsSub(int32_t *t, int32_t *a, int32_t *b) { for (uint32_t i = 0; i < MLDSA_N; i++) { t[i] = a[i] - b[i]; MLDSA_MOD_Q(t[i]); } } static void ComputesZ(const CRYPT_ML_DSA_Ctx *ctx, int32_t *y[MLDSA_L_MAX], int32_t *c, int32_t *s[MLDSA_L_MAX], int32_t *z[MLDSA_L_MAX]) { for (uint8_t i = 0; i < ctx->info->l; i++) { VectorsMul(z[i], c, s[i]); MLDSA_ComputesINVNTT(z[i]); MLDSA_VectorsAdd(z[i], y[i], z[i]); } } static bool ValidityChecks(int32_t *z, uint32_t t) { uint32_t n; for (uint32_t j = 0; j < MLDSA_N; j++) { n = z[j] >> 31; // Shift rightwards by 31 bits. n = z[j] - (n & ((uint32_t)z[j] << 1)); if (n >= t) { return false; } } return true; } static bool ValidityChecksL(const CRYPT_ML_DSA_Ctx *ctx, int32_t *z[MLDSA_L_MAX], uint32_t t) { for (uint8_t i = 0; i < ctx->info->l; i++) { if (ValidityChecks(z[i], t) == false) { return false; } } return true; } static bool ValidityChecksK(const CRYPT_ML_DSA_Ctx *ctx, int32_t *z[MLDSA_K_MAX], uint32_t t) { for (uint8_t i = 0; i < ctx->info->k; i++) { if (ValidityChecks(z[i], t) == false) { return false; } } return true; } static void ComputesR(const CRYPT_ML_DSA_Ctx *ctx, int32_t *c, MLDSA_SignMatrixSt *st) { for (uint8_t i = 0; i < ctx->info->k; i++) { VectorsMul(st->cs2[i], c, st->s2[i]); MLDSA_ComputesINVNTT(st->cs2[i]); MLDSA_VectorsSub(st->r0[i], st->w[i], st->cs2[i]); } } static void ComputesCT(const CRYPT_ML_DSA_Ctx *ctx, int32_t *c, int32_t *t[MLDSA_K_MAX], int32_t *ct[MLDSA_K_MAX]) { for (uint8_t i = 0; i < ctx->info->k; i++) { VectorsMul(ct[i], c, t[i]); MLDSA_ComputesINVNTT(ct[i]); for (uint32_t j = 0; j < MLDSA_N; j++) { int32_t m = (int32_t)(((uint32_t)ct[i][j] + (1 << 22)) >> 23); // m = (ct + 2^22) / 2^23 ct[i][j] = ct[i][j] - m * MLDSA_Q; } } } static uint32_t MakeHint(const CRYPT_ML_DSA_Ctx *ctx, MLDSA_SignMatrixSt *st) { uint32_t num = 0; for (uint32_t i = 0; i < ctx->info->k; i++) { MLDSA_VectorsAdd(st->w[i], st->w[i], st->ct0[i]); MLDSA_VectorsSub(st->w[i], st->w[i], st->cs2[i]); for (uint32_t j = 0; j < MLDSA_N; j++) { if (st->w[i][j] > (int32_t)ctx->info->gamma2 || st->w[i][j] < (0 - (int32_t)ctx->info->gamma2) || (st->w[i][j] == (0 - (int32_t)ctx->info->gamma2) && st->w1[i][j] != 0)) { st->h[i][j] = 1; num++; } else { st->h[i][j] = 0; } } } return num; } static void SigEncode(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *out, uint32_t outLen, int32_t *z[MLDSA_L_MAX], int32_t *h[MLDSA_K_MAX]) { // // 𝛾1 bits of MLDSA44 is 18,𝛾1 bits of MLDSA65 and MLDSA87 is 20. uint32_t bits = (ctx->info->k == K_VALUE_OF_MLDSA_44) ? GAMMA_BITS_OF_MLDSA_44 : GAMMA_BITS_OF_MLDSA_65_87; uint32_t blockSize = MLDSA_N / BITS_OF_BYTE * bits; uint8_t *ptr = out; uint32_t index = 0; for (uint32_t i = 0; i < ctx->info->l; i++) { SignBitPack(ptr, (uint32_t *)z[i], bits, ctx->info->gamma1); ptr += blockSize; } (void)memset_s(ptr, outLen - blockSize * ctx->info->l, 0, outLen - blockSize * ctx->info->l); for (uint32_t i = 0; i < ctx->info->k; i++) { for (uint32_t j = 0; j < MLDSA_N; j++) { if (h[i][j] != 0) { ptr[index] = j; index++; } } ptr[ctx->info->omega + i] = index; } } static int32_t SigDecode(const CRYPT_ML_DSA_Ctx *ctx, const uint8_t *in, int32_t *z[MLDSA_L_MAX], int32_t *h[MLDSA_K_MAX]) { uint32_t bits = (ctx->info->k == K_VALUE_OF_MLDSA_44) ? GAMMA_BITS_OF_MLDSA_44 : GAMMA_BITS_OF_MLDSA_65_87; uint32_t blockSize = MLDSA_N / BITS_OF_BYTE * bits; const uint8_t *ptr = in; uint32_t index = 0; for (int32_t i = 0; i < ctx->info->l; i++) { SignBitUnPake(ptr, (uint32_t *)z[i], bits, ctx->info->gamma1); ptr += blockSize; } for (int32_t i = 0; i < ctx->info->k; i++) { if (ptr[ctx->info->omega + i] < index || ptr[ctx->info->omega + i] > ctx->info->omega) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_SIGN_DATA_ERROR); return CRYPT_MLDSA_SIGN_DATA_ERROR; } uint32_t first = index; (void)memset_s(h[i], sizeof(int32_t) * MLDSA_N, 0, sizeof(int32_t) * MLDSA_N); while (index < ptr[ctx->info->omega + i]) { if (index > first && (ptr[index - 1] >= ptr[index])) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_SIGN_DATA_ERROR); return CRYPT_MLDSA_SIGN_DATA_ERROR; } h[i][ptr[index]] = 1; index++; } } for (int32_t i = index; i < (ctx->info->omega - 1); i++) { RETURN_RET_IF(ptr[i] != 0, CRYPT_MLDSA_SIGN_DATA_ERROR); } return CRYPT_SUCCESS; } static void ComputesApproxW(const CRYPT_ML_DSA_Ctx *ctx, MLDSA_VerifyMatrixSt *st, int32_t *c, int32_t *w[MLDSA_K_MAX]) { MLDSA_ComputesNTT(c); for (uint8_t i = 0; i < ctx->info->l; i++) { MLDSA_ComputesNTT(st->z[i]); } for (uint8_t i = 0; i < ctx->info->k; i++) { for (int32_t j = 0; j < MLDSA_N; j++) { // t1 ⋅ 2^𝑑 st->t1[i][j] = (int32_t)((uint32_t)st->t1[i][j] << MLDSA_D); } // NTT(t1 ⋅ 2^𝑑) MLDSA_ComputesNTT(st->t1[i]); // NTT(𝑐) ∘ NTT(t1 ⋅ 2^𝑑) VectorsMul(st->t1[i], st->t1[i], c); // A ∘ NTT(z) MatrixMul(ctx, w[i], st->matrix[i], st->z); MLDSA_VectorsSub(w[i], w[i], st->t1[i]); MLDSA_ComputesINVNTT(w[i]); } } static void UseHint(const CRYPT_ML_DSA_Ctx *ctx, int32_t *h[MLDSA_K_MAX], int32_t *w[MLDSA_K_MAX]) { int32_t r1; int32_t r0; for (uint8_t i = 0; i < ctx->info->k; i++) { for (uint32_t j = 0; j < MLDSA_N; j++) { if (w[i][j] < 0) { w[i][j] += MLDSA_Q; } Decompose(ctx, w[i][j], &r1, &r0); if (h[i][j] == 0) { w[i][j] = r1; continue; } if (ctx->info->gamma2 == 95232) { // 95232 is (MLDSA_Q-1) / 88; // 𝑚 ← (𝑞 − 1)/(2𝛾2) = 44 // If r0 > 0 return (r1 + 1) mod m else return (r1 − 1) mod m w[i][j] = (r0 > 0) ? ((r1 == 43) ? 0 : (r1 + 1)) : ((r1 == 0) ? 43 : (r1 - 1)); // 43 is (m - 1) continue; } w[i][j] = ((r0 > 0) ? (r1 + 1) : (r1 - 1)) & 0x0f; } } } // Referenced from NIST.FIPS.204 Algorithm 6 ML-DSA.KeyGen_internal(𝑑) int32_t MLDSA_KeyGenInternal(CRYPT_ML_DSA_Ctx *ctx, uint8_t *d) { uint8_t k = ctx->info->k; uint8_t l = ctx->info->l; uint8_t seed[MLDSA_SEED_EXTEND_BYTES_LEN] = { 0 }; uint8_t digest[MLDSA_EXPANDED_SEED_BYTES_LEN] = { 0 }; uint8_t tr[MLDSA_TR_MSG_LEN] = { 0 }; MLDSA_KeyGenMatrixSt st = { 0 }; int32_t ret; GOTO_ERR_IF(MLDSAKeyGenCreateMatrix(k, l, &st), ret); // 32-byte random seed + 1 byte 'k' + 1 byte 'l' (void)memcpy_s(seed, sizeof(seed), d, MLDSA_SEED_BYTES_LEN); seed[MLDSA_SEED_BYTES_LEN] = k; seed[MLDSA_SEED_BYTES_LEN + 1] = l; // (ρ, ρ′, K) ∈ B32 × B64 × B32 ← H(𝜉||IntegerToBytes(k, 1)||IntegerToBytes(ℓ, 1), 128) GOTO_ERR_IF(HashFuncH(seed, sizeof(seed), NULL, 0, digest, MLDSA_EXPANDED_SEED_BYTES_LEN), ret); uint8_t *pubSeed = digest; uint8_t *prvSeed = digest + MLDSA_PUBLIC_SEED_LEN; uint8_t *signSeed = digest + MLDSA_PUBLIC_SEED_LEN + MLDSA_PRIVATE_SEED_LEN; // A ← ExpandA(ρ) GOTO_ERR_IF(ExpandA(ctx, pubSeed, st.matrix), ret); // (𝐬1, 𝐬2) ← ExpandS(ρ′) GOTO_ERR_IF(ExpandS(ctx, prvSeed, st.s1, st.s2), ret); // t ← NTT^−1(A ∘ NTT(𝐬1)) + 𝐬2 ComputesNTT(ctx, st.s1, st.s1Ntt); ComputesT(ctx, st.t1, st.matrix, st.s1Ntt, st.s2); // t = As1 + s2 // (t1, t0) ← Power2Round(t) ComputesPower2Round(ctx, st.t0, st.t1); // pk ← pkEncode(ρ, t1) PkEncode(ctx, pubSeed, st.t1); // tr ← H(pk, 64) GOTO_ERR_IF(HashFuncH(ctx->pubKey, ctx->pubLen, NULL, 0, tr, MLDSA_TR_MSG_LEN), ret); // Step 9 // sk ← skEncode(ρ, K, tr, 𝐬1, 𝐬2, t0) SkEncode(ctx, pubSeed, signSeed, tr, &st); // Step 10 ERR: BSL_SAL_ClearFree(st.bufAddr, st.bufSize); BSL_SAL_CleanseData(seed, sizeof(seed)); BSL_SAL_CleanseData(digest, sizeof(digest)); return ret; } // Referenced from NIST.FIPS.204 Algorithm 7 ML-DSA.Sign_internal(sk, 𝑀′, r𝑛𝑑) int32_t MLDSA_SignInternal(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_Data *msg, uint8_t *out, uint32_t *outLen, uint8_t *rand) { int32_t ret = CRYPT_SUCCESS; uint8_t pubSeed[MLDSA_PUBLIC_SEED_LEN]; uint8_t uBuf[MLDSA_XOF_MSG_LEN]; uint8_t tr[MLDSA_TR_MSG_LEN]; uint8_t signSeed[MLDSA_SIGNING_SEED_LEN + MLDSA_SEED_BYTES_LEN]; (void)memcpy_s(signSeed + MLDSA_SIGNING_SEED_LEN, MLDSA_SEED_BYTES_LEN, rand, MLDSA_SEED_BYTES_LEN); // The w1Len length of MLDSA44 and MLDSA65 is 768, and the w1Len length of MLDSA87 is 1024. uint32_t w1Len = (ctx->info->k == 4 || ctx->info->k == 6) ? 768 : 1024; uint8_t *w1Buf = BSL_SAL_Malloc(w1Len); RETURN_RET_IF(w1Buf == NULL, CRYPT_MEM_ALLOC_FAIL); MLDSA_SignMatrixSt st = { 0 }; GOTO_ERR_IF(MLDSASignCreateMatrix(ctx->info->k, ctx->info->l, &st), ret); // (ρ, K, tr, 𝐬1, 𝐬2, t0) ← skDecode(sk) SkDecode(ctx, pubSeed, signSeed, tr, &st); // NTT(s1), NTT(s2), NTT(t0) SignCalNtt(ctx, &st); // A ← ExpandA(ρ) GOTO_ERR_IF(ExpandA(ctx, pubSeed, st.matrix), ret); if (ctx->isMuMsg) { (void)memcpy_s(uBuf, MLDSA_XOF_MSG_LEN, msg->data, msg->len); } else { // μ ← H(BytesToBits(tr)||𝑀′, 64) GOTO_ERR_IF(HashFuncH(tr, MLDSA_TR_MSG_LEN, msg->data, msg->len, uBuf, MLDSA_XOF_MSG_LEN), ret); } // ρ″ ← H(K||r𝑛𝑑||μ, 64) uint8_t p[MLDSA_XOF_MSG_LEN + 2]; // The counter used 2 bytes. GOTO_ERR_IF(HashFuncH(signSeed, sizeof(signSeed), uBuf, MLDSA_XOF_MSG_LEN, p, MLDSA_XOF_MSG_LEN), ret); uint16_t u = 0; // The length of c is λ/4. uint32_t cBufLen = ctx->info->secBits / 4; int32_t c[MLDSA_N]; do { // y ← ExpandMask(ρ″, 𝜅) GOTO_ERR_IF(ExpandMask(ctx, st.y, p, u), ret); u = u + ctx->info->l; ComputesNTT(ctx, st.y, st.z); // w ← NTT−1(A ∘ NTT(y)); w1 ← HighBits(w) ComputesW(ctx, st.w, st.w1, st.matrix, st.z); // 𝑐 ← H(μ||w1Encode(w1), 𝜆/4) W1Encode(ctx, w1Buf, st.w1); GOTO_ERR_IF(HashFuncH(uBuf, MLDSA_XOF_MSG_LEN, w1Buf, w1Len, out, cBufLen), ret); (void)memset_s(c, sizeof(c), 0, sizeof(c)); // 𝑐 ∈ 𝑅𝑞 ← SampleInBall(c) SampleInBall(ctx, out, cBufLen, c); // 𝑐 ← NTT(𝑐) MLDSA_ComputesNTT(c); // ⟨⟨𝑐𝐬1⟩⟩ ← NTT^−1(𝑐 ∘ 𝐬1); z ← y + ⟨⟨𝑐𝐬1⟩⟩ ComputesZ(ctx, st.y, c, st.s1, st.z); // if ||z||∞ ≥ 𝛾1 − β if (ValidityChecksL(ctx, st.z, ctx->info->gamma1 - ctx->info->beta) == false) { continue; } // ⟨⟨𝑐𝐬2⟩⟩ ← NTT^−1(𝑐 ∘ 𝐬2); 𝐫0 ← LowBits(w − ⟨⟨𝑐𝐬2⟩⟩) ComputesR(ctx, c, &st); // if ||𝐫0||∞ ≥ 𝛾2 − β if (ValidityChecksK(ctx, st.r0, ctx->info->gamma2 - ctx->info->beta) == false) { continue; } // ⟨⟨𝑐t0⟩⟩ ← NTT^−1(𝑐 ∘ t0) ComputesCT(ctx, c, st.t0, st.ct0); // if ||⟨⟨𝑐t0⟩⟩||∞ ≥ 𝛾2 if (ValidityChecksK(ctx, st.ct0, ctx->info->gamma2) == false) { continue; } // h ← MakeHint(−⟨⟨𝑐t0⟩⟩, w − ⟨⟨𝑐𝐬2⟩⟩ + ⟨⟨𝑐t0⟩⟩) if (MakeHint(ctx, &st) > ctx->info->omega) { continue; } break; } while (true); *outLen = ctx->info->signatureLen; // σ ← sigEncode(𝑐, z̃ mod±𝑞, h) SigEncode(ctx, out + cBufLen, *outLen - cBufLen, st.z, st.h); ERR: BSL_SAL_ClearFree(st.bufAddr, st.bufSize); BSL_SAL_ClearFree(w1Buf, w1Len); BSL_SAL_CleanseData(signSeed, sizeof(signSeed)); return ret; } // Referenced from NIST.FIPS.204 Algorithm 8 ML-DSA.Verify_internal(pk, 𝑀′, σ) int32_t MLDSA_VerifyInternal(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_Data *msg, const uint8_t *sign, uint32_t signLen) { (void)signLen; uint8_t k = ctx->info->k; uint8_t l = ctx->info->l; uint8_t pubSeed[MLDSA_PUBLIC_SEED_LEN]; uint8_t uBuf[MLDSA_XOF_MSG_LEN]; uint8_t cBuf[MLDSA_XOF_MSG_LEN]; uint8_t tr[MLDSA_TR_MSG_LEN]; uint32_t cBufLen = ctx->info->secBits / 4; MLDSA_VerifyMatrixSt st = { 0 }; int32_t c[MLDSA_N] = { 0 }; int32_t ret; // The w1Len length of MLDSA44 and MLDSA65 is 768, and the w1Len length of MLDSA87 is 1024. uint32_t w1Len = (k == 4 || k == 6) ? 768 : 1024; uint8_t *w1Buf = BSL_SAL_Malloc(w1Len); RETURN_RET_IF(w1Buf == NULL, CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF(MLDSAVerifyCreateMatrix(k, l, &st), ret); // (ρ, t1) ← pkDecode(pk) PkDecode(ctx, pubSeed, st.t1); // (c,z,h) ← sigDecode(σ) GOTO_ERR_IF(SigDecode(ctx, sign + cBufLen, st.z, st.h), ret); // if ||z||∞ < 𝛾1 − β if (ValidityChecksL(ctx, st.z, ctx->info->gamma1 - ctx->info->beta) == false) { ret = CRYPT_MLDSA_SIGN_DATA_ERROR; goto ERR; } // A ← ExpandA(ρ) GOTO_ERR_IF(ExpandA(ctx, pubSeed, st.matrix), ret); if (ctx->isMuMsg) { (void)memcpy_s(uBuf, MLDSA_XOF_MSG_LEN, msg->data, msg->len); } else { // tr ← H(pk, 64) GOTO_ERR_IF(HashFuncH(ctx->pubKey, ctx->pubLen, NULL, 0, tr, MLDSA_TR_MSG_LEN), ret); // μ ← (H(BytesToBits(tr)||𝑀′, 64)) GOTO_ERR_IF(HashFuncH(tr, MLDSA_TR_MSG_LEN, msg->data, msg->len, uBuf, MLDSA_XOF_MSG_LEN), ret); } // 𝑐 ∈ 𝑅𝑞 ← SampleInBall(𝑐) SampleInBall(ctx, sign, cBufLen, c); // w′ ← NTT−1(A ∘ NTT(z) − NTT(𝑐) ∘ NTT(t1 ⋅ 2𝑑)) ComputesApproxW(ctx, &st, c, st.w); // w1′ ← UseHint(h, w′) UseHint(ctx, st.h, st.w); // c′← H(μ||w1Encode(w1′), 𝜆/4) W1Encode(ctx, w1Buf, st.w); GOTO_ERR_IF(HashFuncH(uBuf, MLDSA_XOF_MSG_LEN, w1Buf, w1Len, cBuf, cBufLen), ret); // If c and c' are not equal, verify failed. if (memcmp(sign, cBuf, cBufLen) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_VERIFY_FAIL); ret = CRYPT_MLDSA_VERIFY_FAIL; goto ERR; } ERR: BSL_SAL_Free(st.bufAddr); BSL_SAL_Free(w1Buf); return ret; } #ifdef HITLS_CRYPTO_MLDSA_CHECK // calculate public key from private key int32_t MLDSA_CalPub(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *pub, uint32_t pubLen) { int32_t ret; MLDSA_SignMatrixSt st = { 0 }; uint8_t pubSeed[MLDSA_PUBLIC_SEED_LEN]; uint8_t kValue[MLDSA_SIGNING_SEED_LEN + MLDSA_SEED_BYTES_LEN]; int32_t tmp0[MLDSA_K_MAX][MLDSA_N]; int32_t tmp1[MLDSA_K_MAX][MLDSA_N]; int32_t tmp2[MLDSA_L_MAX][MLDSA_N]; uint8_t tr[MLDSA_TR_MSG_LEN]; int32_t *s1Ntt[MLDSA_L_MAX]; int32_t *t0[MLDSA_K_MAX]; int32_t *t1[MLDSA_K_MAX]; for (int32_t i = 0; i < ctx->info->k; i++) { t0[i] = tmp0[i]; t1[i] = tmp1[i]; } for (int32_t i = 0; i < ctx->info->l; i++) { s1Ntt[i] = tmp2[i]; } GOTO_ERR_IF(MLDSASignCreateMatrix(ctx->info->k, ctx->info->l, &st), ret); SkDecode(ctx, pubSeed, kValue, tr, &st); // get ρ, K, tr, s1, s2, t0 // A <- ExpandA(ρ) GOTO_ERR_IF(ExpandA(ctx, pubSeed, st.matrix), ret); // t <- NTT^−1(A ∘ NTT(s1)) + s2 ComputesNTT(ctx, st.s1, s1Ntt); ComputesT(ctx, t1, st.matrix, s1Ntt, st.s2); // t = As1 + s2 // (t1, t0) <- Power2Round(t) ComputesPower2Round(ctx, t0, t1); for (int32_t i = 0; i < ctx->info->k; i++) { if (memcmp(t0[i], st.t0[i], MLDSA_N) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_PAIRWISE_CHECK_FAIL); ret = CRYPT_MLDSA_PAIRWISE_CHECK_FAIL; goto ERR; } } // pk <- pkEncode(ρ, t1) if (memcpy_s(pub, pubLen, pubSeed, MLDSA_PUBLIC_SEED_LEN) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_MLDSA_LEN_NOT_ENOUGH); ret = CRYPT_MLDSA_LEN_NOT_ENOUGH; goto ERR; } for (int32_t i = 0; i < ctx->info->k; i++) { // 10 is bitlen(q − 1) − d ByteEncode(pub + MLDSA_PUBLIC_SEED_LEN + i * MLDSA_PUBKEY_POLYT_PACKEDBYTES, (uint32_t *)t1[i], 10); } ERR: BSL_SAL_ClearFree(st.bufAddr, st.bufSize); BSL_SAL_CleanseData(kValue, sizeof(kValue)); BSL_SAL_CleanseData(pubSeed, sizeof(pubSeed)); return ret; } #endif // HITLS_CRYPTO_MLDSA_CHECK #endif
2301_79861745/bench_create
crypto/mldsa/src/ml_dsa_core.c
C
unknown
46,137
/* * 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_ML_DSA_LOCAL_H #define CRYPT_ML_DSA_LOCAL_H #include "crypt_mldsa.h" #include "sal_atomic.h" #include "crypt_local_types.h" #define MLDSA_SEED_BYTES_LEN 32 #define MLDSA_PUBLIC_SEED_LEN 32 #define MLDSA_PRIVATE_SEED_LEN 64 #define MLDSA_SIGNING_SEED_LEN 32 #define MLDSA_EXPANDED_SEED_BYTES_LEN (MLDSA_PUBLIC_SEED_LEN + MLDSA_PRIVATE_SEED_LEN + MLDSA_SIGNING_SEED_LEN) #define MLDSA_SEED_EXTEND_BYTES_LEN (MLDSA_SEED_BYTES_LEN + 2) #define MLDSA_K_MAX 8 #define MLDSA_L_MAX 7 #define MLDSA_TR_MSG_LEN 64 #define MLDSA_XOF_MSG_LEN 64 #define MLDSA_N 256 #define MLDSA_N_BYTE 32 #define GAMMA_BITS_OF_MLDSA_44 18 #define GAMMA_BITS_OF_MLDSA_65_87 20 #define K_VALUE_OF_MLDSA_44 4 #define MLDSA_Q 8380417 #define MLDSA_QINV 58728449 // MLDSA_Q^(-1) mod 2^32 #define MLDSA_D 13 #define MLDSA_PUBKEY_POLYT_PACKEDBYTES 320 #define MLDSA_MAX_CTX_BYTES 255 #define MLDSA_SIGN_PREFIX_BYTES 2 // This is Barrett Modular Multiplication, mod is MLDSA_Q. #define MLDSA_MOD_Q(val) {int32_t m = ((val) + (1 << 22u)) >> 23u; (val) = (val) - m * MLDSA_Q;} typedef struct { uint8_t k; uint8_t l; uint8_t eta; uint8_t tau; uint32_t beta; uint32_t gamma1; uint32_t gamma2; uint8_t omega; uint32_t secBits; uint32_t publicKeyLen; uint32_t privateKeyLen; uint32_t signatureLen; } CRYPT_ML_DSA_Info; struct CryptMlDsaCtx { const CRYPT_ML_DSA_Info *info; uint8_t *pubKey; uint32_t pubLen; uint8_t *prvKey; uint32_t prvLen; uint8_t *ctxInfo; uint32_t ctxLen; bool isMuMsg; bool needEncodeCtx; bool needPreHash; bool deterministicSignFlag; BSL_SAL_RefCount references; void *libCtx; }; void MLDSA_ComputesNTT(int32_t w[MLDSA_N]); void MLDSA_ComputesINVNTT(int32_t w[MLDSA_N]); int32_t MLDSA_MontgomeryReduce(int64_t a); int32_t MLDSA_KeyGenInternal(CRYPT_ML_DSA_Ctx *ctx, uint8_t *d); int32_t MLDSA_SignInternal(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_Data *msg, uint8_t *out, uint32_t *outLen, uint8_t *rand); int32_t MLDSA_VerifyInternal(const CRYPT_ML_DSA_Ctx *ctx, CRYPT_Data *msg, const uint8_t *sign, uint32_t signLen); #ifdef HITLS_CRYPTO_MLDSA_CHECK // calculate public key from private key int32_t MLDSA_CalPub(const CRYPT_ML_DSA_Ctx *ctx, uint8_t *pub, uint32_t pubLen); #endif // HITLS_CRYPTO_MLDSA_CHECK #endif // ML_DSA_LOCAL_H
2301_79861745/bench_create
crypto/mldsa/src/ml_dsa_local.h
C
unknown
2,913
/* * 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_MLDSA #include "ml_dsa_local.h" // mont = 2^32 mod q = -4186625 // ZETAS is NIST.FIPS.204 Zetas * mont static const int32_t ZETAS[MLDSA_N] = { 0, 25847, -2608894, -518909, 237124, -777960, -876248, 466468, 1826347, 2353451, -359251, -2091905, 3119733, -2884855, 3111497, 2680103, 2725464, 1024112, -1079900, 3585928, -549488, -1119584, 2619752, -2108549, -2118186, -3859737, -1399561, -3277672, 1757237, -19422, 4010497, 280005, 2706023, 95776, 3077325, 3530437, -1661693, -3592148, -2537516, 3915439, -3861115, -3043716, 3574422, -2867647, 3539968, -300467, 2348700, -539299, -1699267, -1643818, 3505694, -3821735, 3507263, -2140649, -1600420, 3699596, 811944, 531354, 954230, 3881043, 3900724, -2556880, 2071892, -2797779, -3930395, -1528703, -3677745, -3041255, -1452451, 3475950, 2176455, -1585221, -1257611, 1939314, -4083598, -1000202, -3190144, -3157330, -3632928, 126922, 3412210, -983419, 2147896, 2715295, -2967645, -3693493, -411027, -2477047, -671102, -1228525, -22981, -1308169, -381987, 1349076, 1852771, -1430430, -3343383, 264944, 508951, 3097992, 44288, -1100098, 904516, 3958618, -3724342, -8578, 1653064, -3249728, 2389356, -210977, 759969, -1316856, 189548, -3553272, 3159746, -1851402, -2409325, -177440, 1315589, 1341330, 1285669, -1584928, -812732, -1439742, -3019102, -3881060, -3628969, 3839961, 2091667, 3407706, 2316500, 3817976, -3342478, 2244091, -2446433, -3562462, 266997, 2434439, -1235728, 3513181, -3520352, -3759364, -1197226, -3193378, 900702, 1859098, 909542, 819034, 495491, -1613174, -43260, -522500, -655327, -3122442, 2031748, 3207046, -3556995, -525098, -768622, -3595838, 342297, 286988, -2437823, 4108315, 3437287, -3342277, 1735879, 203044, 2842341, 2691481, -2590150, 1265009, 4055324, 1247620, 2486353, 1595974, -3767016, 1250494, 2635921, -3548272, -2994039, 1869119, 1903435, -1050970, -1333058, 1237275, -3318210, -1430225, -451100, 1312455, 3306115, -1962642, -1279661, 1917081, -2546312, -1374803, 1500165, 777191, 2235880, 3406031, -542412, -2831860, -1671176, -1846953, -2584293, -3724270, 594136, -3776993, -2013608, 2432395, 2454455, -164721, 1957272, 3369112, 185531, -1207385, -3183426, 162844, 1616392, 3014001, 810149, 1652634, -3694233, -1799107, -3038916, 3523897, 3866901, 269760, 2213111, -975884, 1717735, 472078, -426683, 1723600, -1803090, 1910376, -1667432, -1104333, -260646, -3833893, -2939036, -2235985, -420899, -2286327, 183443, -976891, 1612842, -3545687, -554416, 3919660, -48306, -1362209, 3937738, 1400424, -846154, 1976782 }; // Referenced from NIST.FIPS.204 Algorithm 49 MontgomeryReduce(a) int32_t MLDSA_MontgomeryReduce(int64_t a) { int32_t t = (int32_t)(a * MLDSA_QINV); t = (int32_t)((a - (int64_t)t * MLDSA_Q) >> 32); // (a - t * q)/2^32 return t; } // Algorithm 41 NTT(w) void MLDSA_ComputesNTT(int32_t w[MLDSA_N]) { uint32_t m = 0; for (uint32_t len = MLDSA_N / 2; len > 0; len >>= 1) { for (uint32_t start = 0; start < MLDSA_N;) { m++; int32_t z = ZETAS[m]; for (uint32_t j = start; j < start + len; ++j) { int32_t t = MLDSA_MontgomeryReduce((int64_t)z * w[j + len]); w[j + len] = w[j] - t; w[j] = w[j] + t; } start = start + 2 * len; } } } // Algorithm 42 NTT^−1(w) void MLDSA_ComputesINVNTT(int32_t w[MLDSA_N]) { const int64_t f = 41978; // 41978 is mont^2/256 uint32_t m = MLDSA_N; for (uint32_t len = 1; len < MLDSA_N; len <<= 1) { for (uint32_t start = 0; start < MLDSA_N;) { m--; int32_t zeta = -ZETAS[m]; for (uint32_t j = start; j < start + len; j++) { int32_t t = w[j]; w[j] = t + w[j + len]; w[j + len] = t - w[j + len]; w[j + len] = MLDSA_MontgomeryReduce((int64_t)zeta * w[j + len]); } start = start + 2 * len; } } for (uint32_t j = 0; j < MLDSA_N; j++) { w[j] = MLDSA_MontgomeryReduce((int64_t)f * w[j]); } } #endif
2301_79861745/bench_create
crypto/mldsa/src/ml_dsa_ntt.c
C
unknown
4,782
/* * 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_ML_KEM_H #define CRYPT_ML_KEM_H #include <stdint.h> #include "crypt_types.h" #include "bsl_params.h" typedef struct CryptMlKemCtx CRYPT_ML_KEM_Ctx; CRYPT_ML_KEM_Ctx *CRYPT_ML_KEM_NewCtx(void); CRYPT_ML_KEM_Ctx *CRYPT_ML_KEM_NewCtxEx(void *libCtx); void CRYPT_ML_KEM_FreeCtx(CRYPT_ML_KEM_Ctx *ctx); CRYPT_ML_KEM_Ctx *CRYPT_ML_KEM_DupCtx(CRYPT_ML_KEM_Ctx *ctx); int32_t CRYPT_ML_KEM_Ctrl(CRYPT_ML_KEM_Ctx *ctx, int32_t opt, void *val, uint32_t len); int32_t CRYPT_ML_KEM_GenKey(CRYPT_ML_KEM_Ctx *ctx); int32_t CRYPT_ML_KEM_SetEncapsKey(CRYPT_ML_KEM_Ctx *ctx, const CRYPT_KemEncapsKey *ek); int32_t CRYPT_ML_KEM_GetEncapsKey(const CRYPT_ML_KEM_Ctx *ctx, CRYPT_KemEncapsKey *ek); int32_t CRYPT_ML_KEM_SetDecapsKey(CRYPT_ML_KEM_Ctx *ctx, const CRYPT_KemDecapsKey *dk); int32_t CRYPT_ML_KEM_GetDecapsKey(const CRYPT_ML_KEM_Ctx *ctx, CRYPT_KemDecapsKey *dk); #ifdef HITLS_BSL_PARAMS int32_t CRYPT_ML_KEM_SetEncapsKeyEx(CRYPT_ML_KEM_Ctx *ctx, const BSL_Param *para); int32_t CRYPT_ML_KEM_GetEncapsKeyEx(const CRYPT_ML_KEM_Ctx *ctx, BSL_Param *para); int32_t CRYPT_ML_KEM_SetDecapsKeyEx(CRYPT_ML_KEM_Ctx *ctx, const BSL_Param *para); int32_t CRYPT_ML_KEM_GetDecapsKeyEx(const CRYPT_ML_KEM_Ctx *ctx, BSL_Param *para); #endif int32_t CRYPT_ML_KEM_Cmp(const CRYPT_ML_KEM_Ctx *a, const CRYPT_ML_KEM_Ctx *b); int32_t CRYPT_ML_KEM_GetSecBits(const CRYPT_ML_KEM_Ctx *ctx); int32_t CRYPT_ML_KEM_Encaps(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *cipher, uint32_t *cipherLen, uint8_t *share, uint32_t *shareLen); int32_t CRYPT_ML_KEM_Decaps(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *cipher, uint32_t cipherLen, uint8_t *share, uint32_t *shareLen); #ifdef HITLS_CRYPTO_MLKEM_CHECK /** * @ingroup mlkem * @brief check the key pair consistency * * @param checkType [IN] check type * @param pkey1 [IN] mlkem key context structure * @param pkey2 [IN] mlkem key context structure * * @retval CRYPT_SUCCESS check success. * Others. For details, see error code in errno. */ int32_t CRYPT_ML_KEM_Check(uint32_t checkType, const CRYPT_ML_KEM_Ctx *pkey1, const CRYPT_ML_KEM_Ctx *pkey2); #endif // HITLS_CRYPTO_MLKEM_CHECK #endif // CRYPT_ML_KEM_H
2301_79861745/bench_create
crypto/mlkem/include/crypt_mlkem.h
C
unknown
2,713
/* * 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_MLKEM #include "securec.h" #include "crypt_errno.h" #include "crypt_algid.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "eal_pkey_local.h" #include "crypt_util_rand.h" #include "crypt_utils.h" #include "ml_kem_local.h" static const CRYPT_MlKemInfo ML_KEM_INFO[] = { {2, 3, 2, 10, 4, 128, 800, 1632, 768, 32, 512}, {3, 2, 2, 10, 4, 192, 1184, 2400, 1088, 32, 768}, {4, 2, 2, 11, 5, 256, 1568, 3168, 1568, 32, 1024} }; static const CRYPT_MlKemInfo *MlKemGetInfo(uint32_t bits) { for (uint32_t i = 0; i < sizeof(ML_KEM_INFO) / sizeof(ML_KEM_INFO[0]); i++) { if (ML_KEM_INFO[i].bits == bits) { return &ML_KEM_INFO[i]; } } return NULL; } CRYPT_ML_KEM_Ctx *CRYPT_ML_KEM_NewCtx(void) { CRYPT_ML_KEM_Ctx *keyCtx = BSL_SAL_Malloc(sizeof(CRYPT_ML_KEM_Ctx)); if (keyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(keyCtx, sizeof(CRYPT_ML_KEM_Ctx), 0, sizeof(CRYPT_ML_KEM_Ctx)); BSL_SAL_ReferencesInit(&(keyCtx->references)); return keyCtx; } CRYPT_ML_KEM_Ctx *CRYPT_ML_KEM_NewCtxEx(void *libCtx) { CRYPT_ML_KEM_Ctx *ctx = CRYPT_ML_KEM_NewCtx(); if (ctx == NULL) { return NULL; } ctx->libCtx = libCtx; return ctx; } void CRYPT_ML_KEM_FreeCtx(CRYPT_ML_KEM_Ctx *ctx) { if (ctx == NULL) { return; } int ret = 0; BSL_SAL_AtomicDownReferences(&(ctx->references), &ret); if (ret > 0) { return; } BSL_SAL_CleanseData(ctx->dk, ctx->dkLen); BSL_SAL_FREE(ctx->dk); BSL_SAL_FREE(ctx->ek); BSL_SAL_ReferencesFree(&(ctx->references)); BSL_SAL_FREE(ctx); } static int32_t MlKemSetAlgInfo(CRYPT_ML_KEM_Ctx *ctx, void *val, uint32_t len) { if (len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (ctx->info != NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_CTRL_INIT_REPEATED); return CRYPT_MLKEM_CTRL_INIT_REPEATED; } uint32_t bits = 0; int32_t keyType = *(int32_t*)val; if (keyType == CRYPT_KEM_TYPE_MLKEM_512) { bits = 512; // MLKEM512 } else if (keyType == CRYPT_KEM_TYPE_MLKEM_768) { bits = 768; // MLKEM768 } else if (keyType == CRYPT_KEM_TYPE_MLKEM_1024) { bits = 1024; // MLKEM1024 } const CRYPT_MlKemInfo *info = MlKemGetInfo(bits); if (info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NOT_SUPPORT); return CRYPT_NOT_SUPPORT; } ctx->info = info; return CRYPT_SUCCESS; } CRYPT_ML_KEM_Ctx *CRYPT_ML_KEM_DupCtx(CRYPT_ML_KEM_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_ML_KEM_Ctx *newCtx = CRYPT_ML_KEM_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } if (ctx->info != NULL) { newCtx->info = ctx->info; } if (ctx->ek != NULL) { newCtx->ek = BSL_SAL_Dump(ctx->ek, ctx->ekLen); if (newCtx->ek == NULL) { CRYPT_ML_KEM_FreeCtx(newCtx); return NULL; } newCtx->ekLen = ctx->ekLen; } if (ctx->dk != NULL) { newCtx->dk = BSL_SAL_Dump(ctx->dk, ctx->dkLen); if (newCtx->dk == NULL) { CRYPT_ML_KEM_FreeCtx(newCtx); return NULL; } newCtx->dkLen = ctx->dkLen; } newCtx->libCtx = ctx->libCtx; return newCtx; } static int32_t MlKemGetEncapsKeyLen(CRYPT_ML_KEM_Ctx *ctx, void *val, uint32_t len) { if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYINFO_NOT_SET); return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(uint32_t*)val = ctx->info->encapsKeyLen; return CRYPT_SUCCESS; } static int32_t MlKemGetDecapsKeyLen(CRYPT_ML_KEM_Ctx *ctx, void *val, uint32_t len) { if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYINFO_NOT_SET); return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(uint32_t*)val = ctx->info->decapsKeyLen; return CRYPT_SUCCESS; } static int32_t MlKemGetCipherTextLen(CRYPT_ML_KEM_Ctx *ctx, void *val, uint32_t len) { if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYINFO_NOT_SET); return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(uint32_t*)val = ctx->info->cipherLen; return CRYPT_SUCCESS; } static int32_t MlKemGetSharedLen(CRYPT_ML_KEM_Ctx *ctx, void *val, uint32_t len) { if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYINFO_NOT_SET); return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(uint32_t*)val = ctx->info->sharedLen; return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_SetEncapsKey(CRYPT_ML_KEM_Ctx *ctx, const CRYPT_KemEncapsKey *ek) { if (ctx == NULL || ctx->info == NULL || ek == NULL || ek->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ek->len != ctx->info->encapsKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYLEN_ERROR); return CRYPT_MLKEM_KEYLEN_ERROR; } uint8_t *data = BSL_SAL_Dump(ek->data, ek->len); if (data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (ctx->ek != NULL) { BSL_SAL_Free(ctx->ek); } ctx->ek = data; ctx->ekLen = ek->len; return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_GetEncapsKey(const CRYPT_ML_KEM_Ctx *ctx, CRYPT_KemEncapsKey *ek) { if (ctx == NULL || ctx->info == NULL || ek == NULL || ek->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->ek == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEY_NOT_SET); return CRYPT_MLKEM_KEY_NOT_SET; } if (memcpy_s(ek->data, ek->len, ctx->ek, ctx->ekLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYLEN_ERROR); return CRYPT_MLKEM_KEYLEN_ERROR; } ek->len = ctx->ekLen; return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_SetDecapsKey(CRYPT_ML_KEM_Ctx *ctx, const CRYPT_KemDecapsKey *dk) { if (ctx == NULL || ctx->info == NULL || dk == NULL || dk->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (dk->len != ctx->info->decapsKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYLEN_ERROR); return CRYPT_MLKEM_KEYLEN_ERROR; } uint8_t *data = BSL_SAL_Dump(dk->data, dk->len); if (data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } if (ctx->dk != NULL) { BSL_SAL_CleanseData(ctx->dk, ctx->dkLen); BSL_SAL_Free(ctx->dk); } ctx->dk = data; ctx->dkLen = dk->len; return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_GetDecapsKey(const CRYPT_ML_KEM_Ctx *ctx, CRYPT_KemDecapsKey *dk) { if (ctx == NULL || ctx->info == NULL || dk == NULL || dk->data == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->dk == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEY_NOT_SET); return CRYPT_MLKEM_KEY_NOT_SET; } if (memcpy_s(dk->data, dk->len, ctx->dk, ctx->dkLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYLEN_ERROR); return CRYPT_MLKEM_KEYLEN_ERROR; } dk->len = ctx->dkLen; return CRYPT_SUCCESS; } #ifdef HITLS_BSL_PARAMS int32_t CRYPT_ML_KEM_SetEncapsKeyEx(CRYPT_ML_KEM_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_KemEncapsKey pub = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_ML_KEM_PUBKEY, &pub.data, &pub.len); return CRYPT_ML_KEM_SetEncapsKey(ctx, &pub); } int32_t CRYPT_ML_KEM_GetEncapsKeyEx(const CRYPT_ML_KEM_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_KemEncapsKey pub = {0}; BSL_Param *paramPub = GetParamValue(para, CRYPT_PARAM_ML_KEM_PUBKEY, &pub.data, &(pub.len)); int32_t ret = CRYPT_ML_KEM_GetEncapsKey(ctx, &pub); if (ret != CRYPT_SUCCESS) { return ret; } paramPub->useLen = pub.len; return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_SetDecapsKeyEx(CRYPT_ML_KEM_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_KemDecapsKey prv = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_ML_KEM_PRVKEY, &prv.data, &prv.len); return CRYPT_ML_KEM_SetDecapsKey(ctx, &prv); } int32_t CRYPT_ML_KEM_GetDecapsKeyEx(const CRYPT_ML_KEM_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_KemDecapsKey prv = {0}; BSL_Param *paramPrv = GetParamValue(para, CRYPT_PARAM_ML_KEM_PRVKEY, &prv.data, &(prv.len)); int32_t ret = CRYPT_ML_KEM_GetDecapsKey(ctx, &prv); if (ret != CRYPT_SUCCESS) { return ret; } paramPrv->useLen = prv.len; return CRYPT_SUCCESS; } #endif static int32_t MlKemCmpKey(uint8_t *a, uint32_t aLen, uint8_t *b, uint32_t bLen) { if (aLen != bLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEY_NOT_EQUAL); return CRYPT_MLKEM_KEY_NOT_EQUAL; } if (a != NULL && b != NULL) { if (memcmp(a, b, aLen) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEY_NOT_EQUAL); return CRYPT_MLKEM_KEY_NOT_EQUAL; } } if ((a != NULL) != (b != NULL)) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEY_NOT_EQUAL); return CRYPT_MLKEM_KEY_NOT_EQUAL; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_Cmp(const CRYPT_ML_KEM_Ctx *a, const CRYPT_ML_KEM_Ctx *b) { if (a == NULL || b == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (a->info != b->info) { // The value of info must be one of the ML_KEM_INFO arrays. BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEY_NOT_EQUAL); return CRYPT_MLKEM_KEY_NOT_EQUAL; } if (MlKemCmpKey(a->ek, a->ekLen, b->ek, b->ekLen) != CRYPT_SUCCESS) { return CRYPT_MLKEM_KEY_NOT_EQUAL; } if (MlKemCmpKey(a->dk, a->dkLen, b->dk, b->dkLen) != CRYPT_SUCCESS) { return CRYPT_MLKEM_KEY_NOT_EQUAL; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_GetSecBits(const CRYPT_ML_KEM_Ctx *ctx) { if (ctx == NULL || ctx->info == NULL) { return 0; } return (int32_t)ctx->info->secBits; } static int32_t CRYPT_ML_KEM_GetLen(const CRYPT_ML_KEM_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; } static int32_t MlKemCleanPubKey(CRYPT_ML_KEM_Ctx *ctx) { if (ctx->ek != NULL) { BSL_SAL_CleanseData(ctx->ek, ctx->ekLen); BSL_SAL_FREE(ctx->ek); ctx->ekLen = 0; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_Ctrl(CRYPT_ML_KEM_Ctx *ctx, int32_t opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (opt == CRYPT_CTRL_GET_SECBITS) { return CRYPT_ML_KEM_GetLen(ctx, (GetLenFunc)CRYPT_ML_KEM_GetSecBits, val, len); } if (opt == CRYPT_CTRL_CLEAN_PUB_KEY) { return MlKemCleanPubKey(ctx); } if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (opt) { case CRYPT_CTRL_SET_PARA_BY_ID: return MlKemSetAlgInfo(ctx, val, len); case CRYPT_CTRL_GET_PUBKEY_LEN: return MlKemGetEncapsKeyLen(ctx, val, len); case CRYPT_CTRL_GET_PRVKEY_LEN: return MlKemGetDecapsKeyLen(ctx, val, len); case CRYPT_CTRL_GET_CIPHERTEXT_LEN: return MlKemGetCipherTextLen(ctx, val, len); case CRYPT_CTRL_GET_SHARED_KEY_LEN: return MlKemGetSharedLen(ctx, val, len); default: BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_CTRL_NOT_SUPPORT); return CRYPT_MLKEM_CTRL_NOT_SUPPORT; } } static int32_t MlKemCreateKeyBuf(CRYPT_ML_KEM_Ctx *ctx) { if (ctx->dk == NULL) { uint8_t *dk = BSL_SAL_Malloc(ctx->info->decapsKeyLen); if (dk == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->dk = dk; ctx->dkLen = ctx->info->decapsKeyLen; } if (ctx->ek == NULL) { uint8_t *ek = BSL_SAL_Malloc(ctx->info->encapsKeyLen); if (ek == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->ek = ek; ctx->ekLen = ctx->info->encapsKeyLen; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_GenKey(CRYPT_ML_KEM_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYINFO_NOT_SET); return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (MlKemCreateKeyBuf(ctx) != CRYPT_SUCCESS) { return CRYPT_MEM_ALLOC_FAIL; } uint8_t d[MLKEM_SEED_LEN]; uint8_t z[MLKEM_SEED_LEN]; int32_t ret = CRYPT_RandEx(ctx->libCtx, d, MLKEM_SEED_LEN); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); ret = CRYPT_RandEx(ctx->libCtx, z, MLKEM_SEED_LEN); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); ret = MLKEM_KeyGenInternal(ctx, d, z); BSL_SAL_CleanseData(d, MLKEM_SEED_LEN); BSL_SAL_CleanseData(z, MLKEM_SEED_LEN); return ret; } static int32_t EncCapsInputCheck(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t *ctLen, uint8_t *sk, uint32_t *skLen) { if (ctx == NULL || ctx->ek == NULL || ct == NULL || ctLen == NULL || sk == NULL || skLen == NULL) { return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (*ctLen < ctx->info->cipherLen || *skLen < MLKEM_SHARED_KEY_LEN) { return CRYPT_MLKEM_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_Encaps(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *cipher, uint32_t *cipherLen, uint8_t *share, uint32_t *shareLen) { int32_t ret = EncCapsInputCheck(ctx, cipher, cipherLen, share, shareLen); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); uint8_t m[MLKEM_SEED_LEN]; ret = CRYPT_RandEx(ctx->libCtx, m, MLKEM_SEED_LEN); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); ret = MLKEM_EncapsInternal(ctx, cipher, cipherLen, share, shareLen, m); BSL_SAL_CleanseData(m, MLKEM_SEED_LEN); return ret; } static int32_t DecCapsInputCheck(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t ctLen, uint8_t *sk, uint32_t *skLen) { if (ctx == NULL || ctx->dk == NULL || ct == NULL || sk == NULL || skLen == NULL) { return CRYPT_NULL_INPUT; } if (ctx->info == NULL) { return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (ctLen != ctx->info->cipherLen || *skLen < MLKEM_SHARED_KEY_LEN) { return CRYPT_MLKEM_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_Decaps(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *cipher, uint32_t cipherLen, uint8_t *share, uint32_t *shareLen) { int32_t ret = DecCapsInputCheck(ctx, cipher, cipherLen, share, shareLen); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); return MLKEM_DecapsInternal(ctx, cipher, cipherLen, share, shareLen); } #ifdef HITLS_CRYPTO_MLKEM_CHECK static int32_t MlKemKeyPairCheck(const CRYPT_ML_KEM_Ctx *pubKey, const CRYPT_ML_KEM_Ctx *prvKey) { if (pubKey == NULL || prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (pubKey->info == NULL || prvKey->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYINFO_NOT_SET); return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (pubKey->info->bits != prvKey->info->bits) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_PAIRWISE_CHECK_FAIL); return CRYPT_MLKEM_PAIRWISE_CHECK_FAIL; } uint32_t cipherLen = pubKey->info->cipherLen; uint8_t *ciphertext = BSL_SAL_Malloc(pubKey->info->cipherLen); if (ciphertext == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret; uint32_t sharedLen1 = MLKEM_SHARED_KEY_LEN; uint8_t sharedKey1[MLKEM_SHARED_KEY_LEN]; uint32_t sharedLen2 = MLKEM_SHARED_KEY_LEN; uint8_t sharedKey2[MLKEM_SHARED_KEY_LEN]; GOTO_ERR_IF(CRYPT_ML_KEM_Encaps(pubKey, ciphertext, &cipherLen, sharedKey1, &sharedLen1), ret); GOTO_ERR_IF(CRYPT_ML_KEM_Decaps(prvKey, ciphertext, cipherLen, sharedKey2, &sharedLen2), ret); if (sharedLen1 != sharedLen2 || memcmp(sharedKey1, sharedKey2, sharedLen1) != 0) { ret = CRYPT_MLKEM_PAIRWISE_CHECK_FAIL; BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_PAIRWISE_CHECK_FAIL); } ERR: BSL_SAL_CleanseData(sharedKey1, MLKEM_SHARED_KEY_LEN); BSL_SAL_ClearFree(ciphertext, pubKey->info->cipherLen); return ret; } static int32_t MlKemPrvKeyCheck(const CRYPT_ML_KEM_Ctx *prvKey) { if (prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prvKey->info == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYINFO_NOT_SET); return CRYPT_MLKEM_KEYINFO_NOT_SET; } if (prvKey->dk == NULL || prvKey->dkLen != prvKey->info->decapsKeyLen) { BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_INVALID_PRVKEY); return CRYPT_MLKEM_INVALID_PRVKEY; } return CRYPT_SUCCESS; } int32_t CRYPT_ML_KEM_Check(uint32_t checkType, const CRYPT_ML_KEM_Ctx *pkey1, const CRYPT_ML_KEM_Ctx *pkey2) { switch (checkType) { case CRYPT_PKEY_CHECK_KEYPAIR: return MlKemKeyPairCheck(pkey1, pkey2); case CRYPT_PKEY_CHECK_PRVKEY: return MlKemPrvKeyCheck(pkey1); default: BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } } #endif // HITLS_CRYPTO_MLKEM_CHECK #endif
2301_79861745/bench_create
crypto/mlkem/src/ml_kem.c
C
unknown
19,202
/* * 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_ML_KEM_LOCAL_H #define CRYPT_ML_KEM_LOCAL_H #include "crypt_mlkem.h" #include "sal_atomic.h" #include "crypt_local_types.h" #define MLKEM_N 256 #define MLKEM_N_HALF 128 #define MLKEM_CIPHER_LEN 384 #define MLKEM_SEED_LEN 32 #define MLKEM_SHARED_KEY_LEN 32 #define MLKEM_PRF_BLOCKSIZE 64 #define MLKEM_ENCODE_BLOCKSIZE 32 // 9 = 8.38 = (((MLKEM_BITS_OF_Q * (MLKEM_N/8) * 2^MLKEM_BITS_OF_Q) / MLKEM_Q) + 64) / 64; // array_B_arbitrary_length = 9 * 64 + 2 = 578 #define MLKEM_XOF_OUTPUT_LENGTH 578 #define MLKEM_Q 3329 #define MLKEM_BITS_OF_Q 12 #define MLKEM_INVN 3303 // MLKEM_N_HALF * MLKEM_INVN = 1 mod MLKEM_Q typedef int32_t (*MlKemHashFunc)(uint32_t id, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); static inline void MlKemAddModQ(int16_t *val) { while (*val < 0) { *val += MLKEM_Q; } } typedef struct { uint8_t k; uint8_t eta1; uint8_t eta2; uint8_t du; uint8_t dv; uint32_t secBits; uint32_t encapsKeyLen; uint32_t decapsKeyLen; uint32_t cipherLen; uint32_t sharedLen; uint32_t bits; } CRYPT_MlKemInfo; struct CryptMlKemCtx { int32_t algId; const CRYPT_MlKemInfo *info; uint8_t *ek; uint32_t ekLen; uint8_t *dk; uint32_t dkLen; BSL_SAL_RefCount references; void *libCtx; }; void MLKEM_ComputNTT(int16_t *a, const int16_t *psi, uint32_t pruLength); void MLKEM_ComputINTT(int16_t *a, const int16_t *psiInv, uint32_t pruLength); void MLKEM_SamplePolyCBD(int16_t *polyF, uint8_t *buf, uint8_t eta); void MLKEM_MatrixMulAdd(uint8_t k, int16_t *matrix[], int16_t *vectorS[], int16_t *vectorE, int16_t *vectorT, const int16_t *factor); int32_t MLKEM_KeyGenInternal(CRYPT_ML_KEM_Ctx *ctx, uint8_t *d, uint8_t *z); int32_t MLKEM_EncapsInternal(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t *ctLen, uint8_t *sk, uint32_t *skLen, uint8_t *m); int32_t MLKEM_DecapsInternal(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t ctLen, uint8_t *sk, uint32_t *skLen); #endif // ML_KEM_LOCAL_H
2301_79861745/bench_create
crypto/mlkem/src/ml_kem_local.h
C
unknown
2,598
/* * 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_MLKEM #include "ml_kem_local.h" void MLKEM_ComputNTT(int16_t *a, const int16_t *psi, uint32_t pruLength) { uint32_t t = MLKEM_N; for (uint32_t m = 1; m < pruLength; m <<= 1) { t >>= 1; for (uint32_t i = 0; i < m; i++) { uint32_t j1 = (i << 1) * t; int16_t s = psi[m + i]; int16_t *x = a + j1; int16_t *y = x + (int16_t)t; for (uint32_t j = j1; j < j1 + t; j++) { int32_t ys = (*y) * s; *y = (*x - ys) % MLKEM_Q; *x = (*x + ys) % MLKEM_Q; MlKemAddModQ(y); MlKemAddModQ(x); y++; x++; } } } } void MLKEM_ComputINTT(int16_t *a, const int16_t *psiInv, uint32_t pruLength) { uint32_t t = MLKEM_N / pruLength; for (uint32_t m = pruLength; m > 1; m >>= 1) { uint32_t j1 = 0; uint32_t h = m >> 1; for (uint32_t i = 0; i < h; i++) { int16_t s = psiInv[h + i]; for (uint32_t j = j1; j < j1 + t; j++) { int16_t u = a[j]; int16_t v = a[j + t]; a[j] = (u + v) % MLKEM_Q; // Both u and v are smaller than MLKEM_Q, temp not overflow. int16_t temp = u - v; MlKemAddModQ(&a[j]); MlKemAddModQ(&temp); a[j + t] = ((int32_t)temp * s) % MLKEM_Q; } j1 += (t << 1); } t <<= 1; } for (uint32_t n = 0; n < MLKEM_N; n++) { a[n] = (a[n] * MLKEM_INVN) % MLKEM_Q; MlKemAddModQ(&a[n]); } } #endif
2301_79861745/bench_create
crypto/mlkem/src/ml_kem_ntt.c
C
unknown
2,299
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_MLKEM #include <stdlib.h> #include <string.h> #include <stdio.h> #include "securec.h" #include "bsl_errno.h" #include "bsl_sal.h" #include "crypt_utils.h" #include "crypt_sha3.h" #include "crypt_errno.h" #include "bsl_err_internal.h" #include "eal_md_local.h" #include "ml_kem_local.h" #define BITS_OF_BYTE 8 #define MLKEM_K_MAX 4 #define MLKEM_ETA1_MAX 3 #define MLKEM_ETA2_MAX 2 // A LUT of the primitive n-th roots of unity (psi) in bit-reversed order. static const int16_t PRE_COMPUT_TABLE_NTT[MLKEM_N_HALF] = { 1, 1729, 2580, 3289, 2642, 630, 1897, 848, 1062, 1919, 193, 797, 2786, 3260, 569, 1746, 296, 2447, 1339, 1476, 3046, 56, 2240, 1333, 1426, 2094, 535, 2882, 2393, 2879, 1974, 821, 289, 331, 3253, 1756, 1197, 2304, 2277, 2055, 650, 1977, 2513, 632, 2865, 33, 1320, 1915, 2319, 1435, 807, 452, 1438, 2868, 1534, 2402, 2647, 2617, 1481, 648, 2474, 3110, 1227, 910, 17, 2761, 583, 2649, 1637, 723, 2288, 1100, 1409, 2662, 3281, 233, 756, 2156, 3015, 3050, 1703, 1651, 2789, 1789, 1847, 952, 1461, 2687, 939, 2308, 2437, 2388, 733, 2337, 268, 641, 1584, 2298, 2037, 3220, 375, 2549, 2090, 1645, 1063, 319, 2773, 757, 2099, 561, 2466, 2594, 2804, 1092, 403, 1026, 1143, 2150, 2775, 886, 1722, 1212, 1874, 1029, 2110, 2935, 885, 2154 }; // A LUT of all powers of psi^{-1} in bit-reversed order. static const int16_t PRE_COMPUT_TABLE_INTT[MLKEM_N_HALF] = { 1, 1600, 40, 749, 2481, 1432, 2699, 687, 1583, 2760, 69, 543, 2532, 3136, 1410, 2267, 2508, 1355, 450, 936, 447, 2794, 1235, 1903, 1996, 1089, 3273, 283, 1853, 1990, 882, 3033, 2419, 2102, 219, 855, 2681, 1848, 712, 682, 927, 1795, 461, 1891, 2877, 2522, 1894, 1010, 1414, 2009, 3296, 464, 2697, 816, 1352, 2679, 1274, 1052, 1025, 2132, 1573, 76, 2998, 3040, 1175, 2444, 394, 1219, 2300, 1455, 2117, 1607, 2443, 554, 1179, 2186, 2303, 2926, 2237, 525, 735, 863, 2768, 1230, 2572, 556, 3010, 2266, 1684, 1239, 780, 2954, 109, 1292, 1031, 1745, 2688, 3061, 992, 2596, 941, 892, 1021, 2390, 642, 1868, 2377, 1482, 1540, 540, 1678, 1626, 279, 314, 1173, 2573, 3096, 48, 667, 1920, 2229, 1041, 2606, 1692, 680, 2746, 568, 3312 }; typedef struct { int16_t *bufAddr; int16_t *matrix[MLKEM_K_MAX][MLKEM_K_MAX]; int16_t *vectorS[MLKEM_K_MAX]; int16_t *vectorE[MLKEM_K_MAX]; int16_t *vectorT[MLKEM_K_MAX]; } MLKEM_MatrixSt; // Intermediate data of the key generation and encryption. typedef struct { int16_t *bufAddr; int16_t *vectorS[MLKEM_K_MAX]; int16_t *vectorC1[MLKEM_K_MAX]; int16_t *vectorC2; int16_t *polyM; } MLKEM_DecVectorSt; // Intermediate data of the decryption. static int32_t CreateMatrixBuf(uint8_t k, MLKEM_MatrixSt *st) { // A total of (k * k + 3 * k) data blocks are required. Each block has 512 bytes. int16_t *buf = BSL_SAL_Malloc((k * k + 3 * k) * MLKEM_N * sizeof(int16_t)); if (buf == NULL) { return BSL_MALLOC_FAIL; } st->bufAddr = buf; // Used to release memory. for (uint8_t i = 0; i < k; i++) { for (uint8_t j = 0; j < k; j++) { st->matrix[i][j] = buf + (i * k + j) * MLKEM_N; } // vectorS,vectorE,vectorT use 3 * k data blocks. st->vectorS[i] = buf + (k * k + i * 3) * MLKEM_N; st->vectorE[i] = buf + (k * k + i * 3 + 1) * MLKEM_N; st->vectorT[i] = buf + (k * k + i * 3 + 2) * MLKEM_N; } return CRYPT_SUCCESS; } static void MatrixBufFree(uint8_t k, MLKEM_MatrixSt *st) { // A total of (k * k + 3 * k) data blocks, each block has 512 bytes. BSL_SAL_ClearFree(st->bufAddr, (k * k + 3 * k) * MLKEM_N * sizeof(int16_t)); } static int32_t CreateDecVectorBuf(uint8_t k, MLKEM_DecVectorSt *st) { // A total of (k * 2 + 2) data blocks are required. Each block has 512 bytes. int16_t *buf = BSL_SAL_Malloc((k * 2 + 2) * MLKEM_N * sizeof(int16_t)); if (buf == NULL) { return BSL_MALLOC_FAIL; } st->bufAddr = buf; // Used to release memory. for (uint8_t i = 0; i < k; i++) { st->vectorS[i] = buf + (i) * MLKEM_N; st->vectorC1[i] = buf + (k + i) * MLKEM_N; } // vectorC2 and polyM use 2 * k data blocks. st->vectorC2 = buf + (k * 2) * MLKEM_N; st->polyM = buf + (k * 2 + 1) * MLKEM_N; return CRYPT_SUCCESS; } static void DecVectorBufFree(uint8_t k, MLKEM_DecVectorSt *st) { // A total of (k * 2 + 2) data blocks, each block has 512 bytes. BSL_SAL_ClearFree(st->bufAddr, (k * 2 + 2) * MLKEM_N * sizeof(int16_t)); } // Compress typedef struct { uint64_t barrettMultiplier; /* round(2 ^ barrettShift / MLKEM_Q) */ uint16_t barrettShift; uint16_t halfQ; /* rounded (MLKEM_Q / 2) down or up */ uint8_t bits; } MLKEM_BARRET_REDUCE; // The values of du and dv are from NIST.FIPS.203 Table 2. static const MLKEM_BARRET_REDUCE MLKEM_BARRETT_TABLE[] = { {80635 /* round(2^28/MLKEM_Q) */, 28, 1665 /* Ceil(MLKEM_Q/2) */, 1}, {1290167 /* round(2^32/MLKEM_Q) */, 32, 1665 /* Ceil(MLKEM_Q/2) */, 10}, // 10 is mlkem768 du {80635 /* round(2^28/MLKEM_Q) */, 28, 1665 /* Ceil(MLKEM_Q/2) */, 4}, // 4 is mlkem768 dv {40318 /* round(2^27/MLKEM_Q) */, 27, 1664 /* Floor(MLKEM_Q/2) */, 5}, // 5 is mlkem1024 dv {645084 /* round(2^31/MLKEM_Q) */, 31, 1664 /* Floor(MLKEM_Q/2) */, 11} // 11 is mlkem1024 du }; static int16_t DivMlKemQ(uint16_t x, uint8_t bits, uint16_t halfQ, uint16_t barrettShift, uint64_t barrettMultiplier) { uint64_t round = ((uint64_t)x << bits) + halfQ; round *= barrettMultiplier; round >>= barrettShift; return (int16_t)(round & ((1 << bits) - 1)); } static int16_t Compress(int16_t x, uint8_t d) { int16_t value = 0; uint16_t t = (uint16_t)(x + MLKEM_Q) % MLKEM_Q; /* Computing (x << d) / MLKEM_Q by Barret Reduce */ for (uint32_t i = 0; i < sizeof(MLKEM_BARRETT_TABLE) / sizeof(MLKEM_BARRET_REDUCE); i++) { if (d == MLKEM_BARRETT_TABLE[i].bits) { value = DivMlKemQ(t, MLKEM_BARRETT_TABLE[i].bits, MLKEM_BARRETT_TABLE[i].halfQ, MLKEM_BARRETT_TABLE[i].barrettShift, MLKEM_BARRETT_TABLE[i].barrettMultiplier); break; } } return value; } // DeCompress static int16_t DeCompress(int16_t x, uint8_t bits) { uint32_t product = (uint32_t)x * MLKEM_Q; uint32_t power = 1 << bits; return (int16_t)((product >> bits) + ((product & (power - 1)) >> (bits - 1))); } // hash functions static int32_t HashFuncH(void *libCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t outLen) { uint32_t len = outLen; return EAL_Md(CRYPT_MD_SHA3_256, libCtx, NULL, in, inLen, out, &len); } static int32_t HashFuncG(void *libCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t outLen) { uint32_t len = outLen; return EAL_Md(CRYPT_MD_SHA3_512, libCtx, NULL, in, inLen, out, &len); } static int32_t HashFuncXOF(void *libCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t outLen) { uint32_t len = outLen; return EAL_Md(CRYPT_MD_SHAKE128, libCtx, NULL, in, inLen, out, &len); } static int32_t HashFuncJ(void *libCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t outLen) { uint32_t len = outLen; return EAL_Md(CRYPT_MD_SHAKE256, libCtx, NULL, in, inLen, out, &len); } static int32_t PRF(void *libCtx, uint8_t *extSeed, uint32_t extSeedLen, uint8_t *outBuf, uint32_t bufLen) { uint32_t len = bufLen; return EAL_Md(CRYPT_MD_SHAKE256, libCtx, NULL, extSeed, extSeedLen, outBuf, &len); } static int32_t Parse(uint16_t *polyNtt, uint8_t *arrayB, uint32_t arrayLen, uint32_t n) { uint32_t i = 0; uint32_t j = 0; while (j < n) { if (i + 3 > arrayLen) { // 3 bytes of arrayB are read in each round. BSL_ERR_PUSH_ERROR(CRYPT_MLKEM_KEYLEN_ERROR); return CRYPT_MLKEM_KEYLEN_ERROR; } // The 4 bits of each byte are combined with the 8 bits of another byte into 12 bits. uint16_t d1 = ((uint16_t)arrayB[i]) + (((uint16_t)arrayB[i + 1] & 0x0f) << 8); // 4 bits. uint16_t d2 = (((uint16_t)arrayB[i + 1]) >> 4) + (((uint16_t)arrayB[i + 2]) << 4); if (d1 < MLKEM_Q) { polyNtt[j] = d1; j++; } if (d2 < MLKEM_Q && j < n) { polyNtt[j] = d2; j++; } i += 3; // 3 bytes are processed in each round. } return CRYPT_SUCCESS; } static void EncodeBits1(uint8_t *r, uint16_t *polyF) { for (uint32_t i = 0; i < MLKEM_N / BITS_OF_BYTE; i++) { r[i] = (uint8_t)polyF[BITS_OF_BYTE * i]; for (uint32_t j = 1; j < BITS_OF_BYTE; j++) { r[i] = (uint8_t)(polyF[BITS_OF_BYTE * i + j] << j) | r[i]; } } } static void EncodeBits4(uint8_t *r, uint16_t *polyF) { for (uint32_t i = 0; i < MLKEM_N / 2; i++) { // Two 4 bits are combined into 1 byte. r[i] = ((uint8_t)polyF[2 * i] | ((uint8_t)polyF[2 * i + 1] << 4)); } } static void EncodeBits5(uint8_t *r, uint16_t *polyF) { uint32_t indexR; uint32_t indexF; for (uint32_t i = 0; i < MLKEM_N / 8; i++) { indexR = 5 * i; // Each element in polyF has 5 bits. indexF = 8 * i; // Each element in r has 8 bits. // 8 polyF elements are padded to 5 bytes. r[indexR + 0] = (uint8_t)(polyF[indexF] | (polyF[indexF + 1] << 5)); r[indexR + 1] = (uint8_t)((polyF[indexF + 1] >> 3) | (polyF[indexF + 2] << 2) | (polyF[indexF + 3] << 7)); r[indexR + 2] = (uint8_t)((polyF[indexF + 3] >> 1) | (polyF[indexF + 4] << 4)); r[indexR + 3] = (uint8_t)((polyF[indexF + 4] >> 4) | (polyF[indexF + 5] << 1) | (polyF[indexF + 6] << 6)); r[indexR + 4] = (uint8_t)((polyF[indexF + 6] >> 2) | (polyF[indexF + 7] << 3)); } } static void EncodeBits10(uint8_t *r, uint16_t *polyF) { uint32_t indexR; uint32_t indexF; for (uint32_t i = 0; i < MLKEM_N / 4; i++) { // 4 polyF elements are padded to 5 bytes. indexR = 5 * i; indexF = 4 * i; r[indexR + 0] = (uint8_t)polyF[indexF]; r[indexR + 1] = (uint8_t)((polyF[indexF] >> 8) | (polyF[indexF + 1] << 2)); r[indexR + 2] = (uint8_t)((polyF[indexF + 1] >> 6) | (polyF[indexF + 2] << 4)); r[indexR + 3] = (uint8_t)((polyF[indexF + 2] >> 4) | (polyF[indexF + 3] << 6)); r[indexR + 4] = (uint8_t)(polyF[indexF + 3] >> 2); } } static void EncodeBits11(uint8_t *r, uint16_t *polyF) { uint32_t indexR; uint32_t indexF; for (uint32_t i = 0; i < MLKEM_N / 8; i++) { // 8 polyF elements are padded to 11 bytes. indexR = 11 * i; indexF = 8 * i; r[indexR + 0] = (uint8_t)polyF[indexF]; r[indexR + 1] = (uint8_t)((polyF[indexF] >> 8) | (polyF[indexF + 1] << 3)); r[indexR + 2] = (uint8_t)((polyF[indexF + 1] >> 5) | (polyF[indexF + 2] << 6)); r[indexR + 3] = (uint8_t)((polyF[indexF + 2] >> 2)); r[indexR + 4] = (uint8_t)((polyF[indexF + 2] >> 10) | (polyF[indexF + 3] << 1)); r[indexR + 5] = (uint8_t)((polyF[indexF + 3] >> 7) | (polyF[indexF + 4] << 4)); r[indexR + 6] = (uint8_t)((polyF[indexF + 4] >> 4) | (polyF[indexF + 5] << 7)); r[indexR + 7] = (uint8_t)((polyF[indexF + 5] >> 1)); r[indexR + 8] = (uint8_t)((polyF[indexF + 5] >> 9) | (polyF[indexF + 6] << 2)); r[indexR + 9] = (uint8_t)((polyF[indexF + 6] >> 6) | (polyF[indexF + 7] << 5)); r[indexR + 10] = (uint8_t)(polyF[indexF + 7] >> 3); } } static void EncodeBits12(uint8_t *r, uint16_t *polyF) { uint32_t i; uint16_t t0; uint16_t t1; for (i = 0; i < MLKEM_N / 2; i++) { // 2 polyF elements are padded to 3 bytes. t0 = polyF[2 * i]; t1 = polyF[2 * i + 1]; r[3 * i + 0] = (uint8_t)(t0 >> 0); r[3 * i + 1] = (uint8_t)((t0 >> 8) | (t1 << 4)); r[3 * i + 2] = (uint8_t)(t1 >> 4); } } // Encodes an array of d-bit integers into a byte array for 1 ≤ d ≤ 12. static void ByteEncode(uint8_t *r, int16_t *polyF, uint8_t bit) { switch (bit) { // Valid bits of each element in polyF. case 1: // 1 Used for K-PKE.Decrypt Step 7. EncodeBits1(r, (uint16_t *)polyF); break; case 4: // From FIPS 203 Table 2, dv = 4 EncodeBits4(r, (uint16_t *)polyF); break; case 5: // dv = 5 EncodeBits5(r, (uint16_t *)polyF); break; case 10: // du = 10 EncodeBits10(r, (uint16_t *)polyF); break; case 11: // du = 11 EncodeBits11(r, (uint16_t *)polyF); break; case 12: // 12 Used for K-PKE.KeyGen Step 19. EncodeBits12(r, (uint16_t *)polyF); break; default: break; } } static void DecodeBits1(int16_t *polyF, const uint8_t *a) { uint32_t i; uint32_t j; for (i = 0; i < MLKEM_N / BITS_OF_BYTE; i++) { // 1 byte data is decoded into 8 polyF elements. for (j = 0; j < BITS_OF_BYTE; j++) { polyF[BITS_OF_BYTE * i + j] = (a[i] >> j) & 0x01; } } } static void DecodeBits4(int16_t *polyF, const uint8_t *a) { uint32_t i; for (i = 0; i < MLKEM_N / 2; i++) { // 1 byte data is decoded into 2 polyF elements. polyF[2 * i] = a[i] & 0xF; polyF[2 * i + 1] = (a[i] >> 4) & 0xF; } } static void DecodeBits5(int16_t *polyF, const uint8_t *a) { uint32_t indexF; uint32_t indexA; for (uint32_t i = 0; i < MLKEM_N / 8; i++) { // 8 byte data is decoded into 5 polyF elements. indexF = 8 * i; indexA = 5 * i; // value & 0x1F is used to obtain 5 bits. polyF[indexF + 0] = ((a[indexA + 0] >> 0)) & 0x1F; polyF[indexF + 1] = ((a[indexA + 0] >> 5) | (a[indexA + 1] << 3)) & 0x1F; polyF[indexF + 2] = ((a[indexA + 1] >> 2)) & 0x1F; polyF[indexF + 3] = ((a[indexA + 1] >> 7) | (a[indexA + 2] << 1)) & 0x1F; polyF[indexF + 4] = ((a[indexA + 2] >> 4) | (a[indexA + 3] << 4)) & 0x1F; polyF[indexF + 5] = ((a[indexA + 3] >> 1)) & 0x1F; polyF[indexF + 6] = ((a[indexA + 3] >> 6) | (a[indexA + 4] << 2)) & 0x1F; polyF[indexF + 7] = ((a[indexA + 4] >> 3)) & 0x1F; } } static void DecodeBits10(int16_t *polyF, const uint8_t *a) { uint32_t indexF; uint32_t indexA; for (uint32_t i = 0; i < MLKEM_N / 4; i++) { // 5 byte data is decoded into 4 polyF elements. indexF = 4 * i; indexA = 5 * i; // value & 0x3FF is used to obtain 10 bits. polyF[indexF + 0] = ((a[indexA + 0] >> 0) | ((uint16_t)a[indexA + 1] << 8)) & 0x3FF; polyF[indexF + 1] = ((a[indexA + 1] >> 2) | ((uint16_t)a[indexA + 2] << 6)) & 0x3FF; polyF[indexF + 2] = ((a[indexA + 2] >> 4) | ((uint16_t)a[indexA + 3] << 4)) & 0x3FF; polyF[indexF + 3] = ((a[indexA + 3] >> 6) | ((uint16_t)a[indexA + 4] << 2)) & 0x3FF; } } static void DecodeBits11(int16_t *polyF, const uint8_t *a) { uint32_t indexF; uint32_t indexA; for (uint32_t i = 0; i < MLKEM_N / 8; i++) { // use type conversion because 11 > 8 indexF = 8 * i; indexA = 11 * i; // value & 0x7FF is used to obtain 11 bits. polyF[indexF + 0] = ((a[indexA + 0] >> 0) | ((uint16_t)a[indexA + 1] << 8)) & 0x7FF; polyF[indexF + 1] = ((a[indexA + 1] >> 3) | ((uint16_t)a[indexA + 2] << 5)) & 0x7FF; polyF[indexF + 2] = ((a[indexA + 2] >> 6) | ((uint16_t)a[indexA + 3] << 2) | ((uint16_t)a[indexA + 4] << 10)) & 0x7FF; polyF[indexF + 3] = ((a[indexA + 4] >> 1) | ((uint16_t)a[indexA + 5] << 7)) & 0x7FF; polyF[indexF + 4] = ((a[indexA + 5] >> 4) | ((uint16_t)a[indexA + 6] << 4)) & 0x7FF; polyF[indexF + 5] = ((a[indexA + 6] >> 7) | ((uint16_t)a[indexA + 7] << 1) | ((uint16_t)a[indexA + 8] << 9)) & 0x7FF; polyF[indexF + 6] = ((a[indexA + 8] >> 2) | ((uint16_t)a[indexA + 9] << 6)) & 0x7FF; polyF[indexF + 7] = ((a[indexA + 9] >> 5) | ((uint16_t)a[indexA + 10] << 3)) & 0x7FF; } } static void DecodeBits12(int16_t *polyF, const uint8_t *a) { uint32_t i; for (i = 0; i < MLKEM_N / 2; i++) { // 3 byte data is decoded into 2 polyF elements, value & 0xFFF is used to obtain 12 bits. polyF[2 * i] = ((a[3 * i + 0] >> 0) | ((uint16_t)a[3 * i + 1] << 8)) & 0xFFF; polyF[2 * i + 1] = ((a[3 * i + 1] >> 4) | ((uint16_t)a[3 * i + 2] << 4)) & 0xFFF; } } // Decodes a byte array into an array of d-bit integers for 1 ≤ d ≤ 12. static void ByteDecode(int16_t *polyF, const uint8_t *a, uint8_t bit) { switch (bit) { case 1: DecodeBits1(polyF, a); break; case 4: DecodeBits4(polyF, a); break; case 5: DecodeBits5(polyF, a); break; case 10: DecodeBits10(polyF, a); break; case 11: DecodeBits11(polyF, a); break; case 12: DecodeBits12(polyF, a); break; default: break; } } static int32_t GenMatrix(const CRYPT_ML_KEM_Ctx *ctx, const uint8_t *digest, int16_t *polyMatrix[MLKEM_K_MAX][MLKEM_K_MAX], bool isEnc) { uint8_t k = ctx->info->k; uint8_t p[MLKEM_SEED_LEN + 2]; // Reserved lengths of i and j is 2 byte. uint8_t xofOut[MLKEM_XOF_OUTPUT_LENGTH]; (void)memcpy_s(p, MLKEM_SEED_LEN, digest, MLKEM_SEED_LEN); for (uint8_t i = 0; i < k; i++) { for (uint8_t j = 0; j < k; j++) { if (isEnc) { p[MLKEM_SEED_LEN] = i; p[MLKEM_SEED_LEN + 1] = j; } else { p[MLKEM_SEED_LEN] = j; p[MLKEM_SEED_LEN + 1] = i; } int32_t ret = HashFuncXOF(ctx->libCtx, p, MLKEM_SEED_LEN + 2, xofOut, MLKEM_XOF_OUTPUT_LENGTH); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); ret = Parse((uint16_t *)polyMatrix[i][j], xofOut, MLKEM_XOF_OUTPUT_LENGTH, MLKEM_N); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); } } return CRYPT_SUCCESS; } static int32_t SampleEta1(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *digest, int16_t *polyS[], uint8_t *nonce) { uint8_t q[MLKEM_SEED_LEN + 1] = { 0 }; // Reserved lengths of nonce is 1 byte. uint8_t prfOut[MLKEM_PRF_BLOCKSIZE * MLKEM_ETA1_MAX] = { 0 }; (void)memcpy_s(q, MLKEM_SEED_LEN, digest, MLKEM_SEED_LEN); for (uint8_t i = 0; i < ctx->info->k; i++) { q[MLKEM_SEED_LEN] = *nonce; int32_t ret = PRF(ctx->libCtx, q, MLKEM_SEED_LEN + 1, prfOut, MLKEM_PRF_BLOCKSIZE * MLKEM_ETA1_MAX); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); MLKEM_SamplePolyCBD(polyS[i], prfOut, ctx->info->eta1); *nonce = *nonce + 1; MLKEM_ComputNTT(polyS[i], PRE_COMPUT_TABLE_NTT, MLKEM_N_HALF); } return CRYPT_SUCCESS; } static int32_t SampleEta2(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *digest, int16_t *polyS[], uint8_t *nonce) { uint8_t q[MLKEM_SEED_LEN + 1] = { 0 }; // Reserved lengths of nonce is 1 byte. uint8_t prfOut[MLKEM_PRF_BLOCKSIZE * MLKEM_ETA2_MAX] = { 0 }; (void)memcpy_s(q, MLKEM_SEED_LEN, digest, MLKEM_SEED_LEN); for (uint8_t i = 0; i < ctx->info->k; i++) { q[MLKEM_SEED_LEN] = *nonce; int32_t ret = PRF(ctx->libCtx, q, MLKEM_SEED_LEN + 1, prfOut, MLKEM_PRF_BLOCKSIZE * MLKEM_ETA2_MAX); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); MLKEM_SamplePolyCBD(polyS[i], prfOut, ctx->info->eta2); *nonce = *nonce + 1; } return CRYPT_SUCCESS; } // NIST.FIPS.203 Algorithm 13 K-PKE.KeyGen(𝑑) static int32_t PkeKeyGen(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *pk, uint8_t *dk, uint8_t *d) { uint8_t k = ctx->info->k; uint8_t nonce = 0; uint8_t seed[MLKEM_SEED_LEN + 1] = { 0 }; // Reserved lengths of k is 1 byte. uint8_t digest[CRYPT_SHA3_512_DIGESTSIZE] = { 0 }; // (p,q) = G(d || k) (void)memcpy_s(seed, MLKEM_SEED_LEN + 1, d, MLKEM_SEED_LEN); seed[MLKEM_SEED_LEN] = k; int32_t ret = HashFuncG(ctx->libCtx, seed, MLKEM_SEED_LEN + 1, digest, CRYPT_SHA3_512_DIGESTSIZE); // Step 1 RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); // expand 32+1 bytes to two pseudorandom 32-byte seeds uint8_t *p = digest; uint8_t *q = digest + CRYPT_SHA3_512_DIGESTSIZE / 2; MLKEM_MatrixSt st = { 0 }; ret = CreateMatrixBuf(k, &st); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); GOTO_ERR_IF(GenMatrix(ctx, p, st.matrix, false), ret); // Step 3 - 7 GOTO_ERR_IF(SampleEta1(ctx, q, st.vectorS, &nonce), ret); // Step 8 - 11 GOTO_ERR_IF(SampleEta1(ctx, q, st.vectorE, &nonce), ret); // Step 12 - 15 for (uint8_t i = 0; i < k; i++) { // Step 18 MLKEM_MatrixMulAdd(k, st.matrix[i], st.vectorS, st.vectorE[i], st.vectorT[i], PRE_COMPUT_TABLE_NTT); } // output: pk, dk, ekPKE ← ByteEncode12(𝐭)‖p. for (uint8_t i = 0; i < k; i++) { // Step 19 ByteEncode(pk + MLKEM_SEED_LEN * MLKEM_BITS_OF_Q * i, st.vectorT[i], MLKEM_BITS_OF_Q); // Step 20 ByteEncode(dk + MLKEM_SEED_LEN * MLKEM_BITS_OF_Q * i, st.vectorS[i], MLKEM_BITS_OF_Q); } // The buffer of pk is sufficient, check it before calling this function. (void)memcpy_s(pk + MLKEM_SEED_LEN * MLKEM_BITS_OF_Q * k, MLKEM_SEED_LEN, p, MLKEM_SEED_LEN); ERR: MatrixBufFree(k, &st); return ret; } // NIST.FIPS.203 Algorithm 14 K-PKE.Encrypt(ekPKE,𝑚,𝑟) static int32_t PkeEncrypt(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, const uint8_t *ek, uint8_t *m, uint8_t *r) { uint8_t i; uint32_t n; uint8_t k = ctx->info->k; uint8_t nonce = 0; // Step 1 uint8_t seedE[MLKEM_SEED_LEN + 1]; uint8_t bufEncE[MLKEM_PRF_BLOCKSIZE * MLKEM_ETA1_MAX]; int16_t polyVectorE2[MLKEM_N] = { 0 }; int16_t polyVectorC2[MLKEM_N] = { 0 }; int16_t polyVectorM[MLKEM_N] = { 0 }; MLKEM_MatrixSt st = { 0 }; int32_t ret = CreateMatrixBuf(k, &st); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); GOTO_ERR_IF(GenMatrix(ctx, ek + MLKEM_CIPHER_LEN * k, st.matrix, true), ret); // Step 3 - 8 GOTO_ERR_IF(SampleEta1(ctx, r, st.vectorS, &nonce), ret); // Step 9 - 12 GOTO_ERR_IF(SampleEta2(ctx, r, st.vectorE, &nonce), ret); // Step 13 - 16 // Step 17 (void)memcpy_s(seedE, MLKEM_SEED_LEN, r, MLKEM_SEED_LEN); seedE[MLKEM_SEED_LEN] = nonce; GOTO_ERR_IF(PRF(ctx->libCtx, seedE, MLKEM_SEED_LEN + 1, bufEncE, MLKEM_PRF_BLOCKSIZE * ctx->info->eta2), ret); MLKEM_SamplePolyCBD(polyVectorE2, bufEncE, ctx->info->eta2); // Step 18 for (i = 0; i < k; i++) { MLKEM_MatrixMulAdd(k, st.matrix[i], st.vectorS, NULL, st.vectorT[i], PRE_COMPUT_TABLE_NTT); } // Step 19 for (i = 0; i < k; i++) { MLKEM_ComputINTT(st.vectorT[i], PRE_COMPUT_TABLE_INTT, MLKEM_N_HALF); for (n = 0; n < MLKEM_N; n++) { st.vectorT[i][n] = Compress(st.vectorT[i][n] + st.vectorE[i][n], ctx->info->du); } } // Step 21 for (i = 0; i < k; i++) { ByteDecode(st.vectorE[i], ek + MLKEM_CIPHER_LEN * i, MLKEM_BITS_OF_Q); } MLKEM_MatrixMulAdd(k, st.vectorE, st.vectorS, NULL, polyVectorC2, PRE_COMPUT_TABLE_NTT); ByteDecode(polyVectorM, m, 1); MLKEM_ComputINTT(polyVectorC2, PRE_COMPUT_TABLE_INTT, MLKEM_N_HALF); for (n = 0; n < MLKEM_N; n++) { polyVectorM[n] = DeCompress(polyVectorM[n], 1); // Step 20 // Step 22 polyVectorC2[n] = Compress(polyVectorC2[n] + polyVectorE2[n] + polyVectorM[n], ctx->info->dv); } // Step 22 for (i = 0; i < k; i++) { ByteEncode(ct + MLKEM_ENCODE_BLOCKSIZE * ctx->info->du * i, st.vectorT[i], ctx->info->du); } // Step 23 ByteEncode(ct + MLKEM_ENCODE_BLOCKSIZE * ctx->info->du * k, polyVectorC2, ctx->info->dv); ERR: MatrixBufFree(k, &st); return ret; } // NIST.FIPS.203 Algorithm 15 K-PKE.Decrypt(dkPKE, 𝑐) static int32_t PkeDecrypt(const CRYPT_MlKemInfo *algInfo, uint8_t *result, const uint8_t *dk, const uint8_t *ciphertext) { uint8_t i; uint8_t k = algInfo->k; uint32_t n; MLKEM_DecVectorSt st = { 0 }; int32_t ret = CreateDecVectorBuf(k, &st); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); for (i = 0; i < k; i++) { ByteDecode(st.vectorC1[i], ciphertext + MLKEM_ENCODE_BLOCKSIZE * algInfo->du * i, algInfo->du); // Step 3 ByteDecode(st.vectorS[i], dk + MLKEM_ENCODE_BLOCKSIZE * MLKEM_BITS_OF_Q * i, MLKEM_BITS_OF_Q); // Step 5 } ByteDecode(st.vectorC2, ciphertext + MLKEM_ENCODE_BLOCKSIZE * algInfo->du * k, algInfo->dv); // Step 4 for (i = 0; i < k; i++) { for (n = 0; n < MLKEM_N; n++) { st.vectorC1[i][n] = DeCompress(st.vectorC1[i][n], algInfo->du); // Step 3 if (i == 0) { st.vectorC2[n] = DeCompress(st.vectorC2[n], algInfo->dv); // Step 4 } } MLKEM_ComputNTT(st.vectorC1[i], PRE_COMPUT_TABLE_NTT, MLKEM_N_HALF); } MLKEM_MatrixMulAdd(k, st.vectorS, st.vectorC1, NULL, st.polyM, PRE_COMPUT_TABLE_NTT); // Step 6 // polyM = intt(polyM) MLKEM_ComputINTT(st.polyM, PRE_COMPUT_TABLE_INTT, MLKEM_N_HALF); // c2 - polyM for (n = 0; n < MLKEM_N; n++) { st.polyM[n] = Compress(st.vectorC2[n] - st.polyM[n], 1); } ByteEncode(result, st.polyM, 1); // Step 7 DecVectorBufFree(k, &st); return CRYPT_SUCCESS; } // NIST.FIPS.203 Algorithm 16 ML-KEM.KeyGen_internal(𝑑,𝑧) int32_t MLKEM_KeyGenInternal(CRYPT_ML_KEM_Ctx *ctx, uint8_t *d, uint8_t *z) { const CRYPT_MlKemInfo *algInfo = ctx->info; uint32_t dkPkeLen = MLKEM_CIPHER_LEN * algInfo->k; // (ekPKE,dkPKE) ← K-PKE.KeyGen(𝑑) int32_t ret = PkeKeyGen(ctx, ctx->ek, ctx->dk, d); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); // dk ← (dkPKE‖ek‖H(ek)‖𝑧) if (memcpy_s(ctx->dk + dkPkeLen, ctx->dkLen - dkPkeLen, ctx->ek, ctx->ekLen) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } ret = HashFuncH(ctx->libCtx, ctx->ek, ctx->ekLen, ctx->dk + dkPkeLen + ctx->ekLen, CRYPT_SHA3_256_DIGESTSIZE); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); if (memcpy_s(ctx->dk + dkPkeLen + ctx->ekLen + CRYPT_SHA3_256_DIGESTSIZE, ctx->dkLen - (dkPkeLen + ctx->ekLen + CRYPT_SHA3_256_DIGESTSIZE), z, MLKEM_SEED_LEN) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } return CRYPT_SUCCESS; } // NIST.FIPS.203 Algorithm 17 ML-KEM.Encaps_internal(ek,𝑚) int32_t MLKEM_EncapsInternal(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t *ctLen, uint8_t *sk, uint32_t *skLen, uint8_t *m) { uint8_t mhek[MLKEM_SEED_LEN + CRYPT_SHA3_256_DIGESTSIZE]; // m and H(ek) uint8_t kr[CRYPT_SHA3_512_DIGESTSIZE]; // K and r // (K,r) = G(m || H(ek)) (void)memcpy_s(mhek, MLKEM_SEED_LEN, m, MLKEM_SEED_LEN); int32_t ret = HashFuncH(ctx->libCtx, ctx->ek, ctx->ekLen, mhek + MLKEM_SEED_LEN, CRYPT_SHA3_256_DIGESTSIZE); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); ret = HashFuncG(ctx->libCtx, mhek, MLKEM_SEED_LEN + CRYPT_SHA3_256_DIGESTSIZE, kr, CRYPT_SHA3_512_DIGESTSIZE); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); (void)memcpy_s(sk, *skLen, kr, MLKEM_SHARED_KEY_LEN); // 𝑐 ← K-PKE.Encrypt(ek,𝑚,𝑟) ret = PkeEncrypt(ctx, ct, ctx->ek, m, kr + MLKEM_SHARED_KEY_LEN); BSL_SAL_CleanseData(kr, CRYPT_SHA3_512_DIGESTSIZE); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); *ctLen = ctx->info->cipherLen; *skLen = ctx->info->sharedLen; return CRYPT_SUCCESS; } // NIST.FIPS.203 Algorithm 18 ML-KEM.Decaps_internal(dk, 𝑐) int32_t MLKEM_DecapsInternal(const CRYPT_ML_KEM_Ctx *ctx, uint8_t *ct, uint32_t ctLen, uint8_t *sk, uint32_t *skLen) { const CRYPT_MlKemInfo *algInfo = ctx->info; const uint8_t *dk = ctx->dk; // Step 1 dkPKE ← dk[0 : 384k] const uint8_t *ek = dk + MLKEM_CIPHER_LEN * algInfo->k; // Step 2 ekPKE ← dk[384k : 768k +32] const uint8_t *h = ek + algInfo->encapsKeyLen; // Step 3 h ← dk[768k +32 : 768k +64] const uint8_t *z = h + MLKEM_SEED_LEN; // Step 4 z ← dk[768k +64 : 768k +96] uint8_t mh[MLKEM_SEED_LEN + CRYPT_SHA3_256_DIGESTSIZE]; // m′ and h uint8_t kr[CRYPT_SHA3_512_DIGESTSIZE]; // K' and r' int32_t ret = PkeDecrypt(algInfo, mh, dk, ct); // Step 5: 𝑚′ ← K-PKE.Decrypt(dkPKE, 𝑐) RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); // Step 6: (K′,r′) ← G(m′ || h) (void)memcpy_s(mh + MLKEM_SEED_LEN, CRYPT_SHA3_256_DIGESTSIZE, h, CRYPT_SHA3_256_DIGESTSIZE); ret = HashFuncG(ctx->libCtx, mh, MLKEM_SEED_LEN + CRYPT_SHA3_256_DIGESTSIZE, kr, CRYPT_SHA3_512_DIGESTSIZE); RETURN_RET_IF(ret != CRYPT_SUCCESS, ret); // Step 8: 𝑐′ ← K-PKE.Encrypt(ekPKE,𝑚′,𝑟′) uint8_t *r = kr + MLKEM_SHARED_KEY_LEN; uint8_t *newCt = BSL_SAL_Malloc(ctLen + MLKEM_SEED_LEN); RETURN_RET_IF(newCt == NULL, BSL_MALLOC_FAIL); GOTO_ERR_IF(PkeEncrypt(ctx, newCt, ek, mh, r), ret); // Step 9: if c != c′ if (memcmp(ct, newCt, ctLen) == 0) { (void)memcpy_s(sk, *skLen, kr, MLKEM_SHARED_KEY_LEN); } else { // Step 7: K = J(z || c) (void)memcpy_s(newCt, ctLen + MLKEM_SEED_LEN, z, MLKEM_SEED_LEN); (void)memcpy_s(newCt + MLKEM_SEED_LEN, ctLen, ct, ctLen); GOTO_ERR_IF(HashFuncJ(ctx->libCtx, newCt, ctLen + MLKEM_SEED_LEN, sk, MLKEM_SHARED_KEY_LEN), ret); } *skLen = MLKEM_SHARED_KEY_LEN; ERR: BSL_SAL_CleanseData(kr, CRYPT_SHA3_512_DIGESTSIZE); BSL_SAL_Free(newCt); return ret; } #endif
2301_79861745/bench_create
crypto/mlkem/src/ml_kem_pke.c
C
unknown
30,365
/* * 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_MLKEM #include "ml_kem_local.h" // basecase multiplication static void BaseMul(int16_t polyH[2], int16_t f0, int16_t f1, int16_t g0, int16_t g1, int16_t factor) { polyH[0] = (int16_t)(((int32_t)(f0 * g0) + (int32_t)(((int32_t)(f1 * g1) % MLKEM_Q) * factor)) % MLKEM_Q); polyH[1] = (int16_t)(((int32_t)(f0 * g1) + (int32_t)(f1 * g0)) % MLKEM_Q); MlKemAddModQ(&polyH[0]); MlKemAddModQ(&polyH[1]); } // circle multiplication static void CircMul(int16_t dest[MLKEM_N], int16_t src1[MLKEM_N], int16_t src2[MLKEM_N], const int16_t *factor) { for (uint32_t i = 0; i < MLKEM_N / 4; i++) { // 4-byte data is calculated in each round. BaseMul(&dest[4 * i], src1[4 * i], src1[4 * i + 1], src2[4 * i], src2[4 * i + 1], factor[i]); BaseMul(&dest[4 * i + 2], src1[4 * i + 2], src1[4 * i + 3], src2[4 * i + 2], src2[4 * i + 3], -1 * factor[i]); } } void MLKEM_MatrixMulAdd(uint8_t k, int16_t *matrix[], int16_t *vectorS[], int16_t *vectorE, int16_t *vectorT, const int16_t *factor) { int16_t dest[MLKEM_N] = { 0 }; for (uint8_t j = 0; j < k; j++) { // factor is a half of the NTT table. CircMul(dest, matrix[j], vectorS[j], factor + MLKEM_N_HALF / 2); for (uint32_t n = 0; n < MLKEM_N; n++) { if (j == 0) { vectorT[n] = (vectorE == NULL) ? dest[n] : (vectorE[n] + dest[n]); } else if (j != 0 && j != (k - 1)) { vectorT[n] += dest[n]; } else if (j == (k - 1)) { vectorT[n] = (vectorT[n] + dest[n]) % MLKEM_Q; } } } } void MLKEM_SamplePolyCBD(int16_t *polyF, uint8_t *buf, uint8_t eta) { uint32_t i; uint32_t j; uint8_t a; uint8_t b; uint32_t t1; if (eta == 3) { // The value of eta can only be 2 or 3. for (i = 0; i < MLKEM_N / 4; i++) { uint32_t temp = (uint32_t)buf[eta * i]; temp |= (uint32_t)buf[eta * i + 1] << 8; temp |= (uint32_t)buf[eta * i + 2] << 16; t1 = temp & 0x00249249; // temp & 0x00249249 is used to obtain a specific bit in temp. t1 += (temp >> 1) & 0x00249249; t1 += (temp >> 2) & 0x00249249; for (j = 0; j < 4; j++) { a = (t1 >> (6 * j)) & 0x3; b = (t1 >> (6 * j + eta)) & 0x3; polyF[4 * i + j] = a - b; } } } else if (eta == 2) { for (i = 0; i < MLKEM_N / 4; i++) { uint16_t temp = (uint16_t)buf[eta * i]; temp |= (uint16_t)buf[eta * i + 1] << 0x8; t1 = temp & 0x5555; // temp & 0x5555 is used to obtain a specific bit in temp. t1 += (temp >> 1) & 0x5555; for (j = 0; j < 4; j++) { a = (t1 >> (4 * j)) & 0x3; b = (t1 >> (4 * j + eta)) & 0x3; polyF[4 * i + j] = a - b; } } } } #endif
2301_79861745/bench_create
crypto/mlkem/src/ml_kem_poly.c
C
unknown
3,603
/* * 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_MODES_H #define CRYPT_MODES_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_MODES #include <stdint.h> #include "crypt_local_types.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define MODES_MAX_IV_LENGTH 24 #define MODES_MAX_BUF_LENGTH 24 #define MODES_IV_LENGTH 16 #define EAL_MAX_BLOCK_LENGTH 32 typedef struct { void *ciphCtx; /* Context defined by each algorithm */ const EAL_SymMethod *ciphMeth; /* Corresponding to the related methods for each symmetric algorithm */ uint8_t iv[MODES_MAX_IV_LENGTH]; /* IV information */ uint8_t buf[MODES_MAX_BUF_LENGTH]; /* Cache the information of the previous block. */ uint8_t blockSize; /* Save the block size. */ uint8_t offset; uint8_t flag3Iv; /* Indicates whether three IVs are used. */ uint32_t ivIndex; /* Indicates the sequence number of the IV block to be used. TDES may have three IV. */ } MODES_CipherCommonCtx; struct ModesCipherCtx { MODES_CipherCommonCtx commonCtx; int32_t algId; uint8_t data[EAL_MAX_BLOCK_LENGTH]; /**< last data block that may not be processed */ uint8_t dataLen; /**< size of the last data block that may not be processed. */ CRYPT_PaddingType pad; /**< padding type */ bool enc; }; typedef struct ModesCipherCtx MODES_CipherCtx; typedef struct { const uint8_t *in; uint8_t *out; const uint8_t *ctr; uint8_t *tag; } XorCryptData; void MODES_Clean(MODES_CipherCommonCtx *ctx); int32_t MODES_SetIv(MODES_CipherCommonCtx *ctx, const uint8_t *val, uint32_t len); int32_t MODES_GetIv(MODES_CipherCommonCtx *ctx, uint8_t *val, uint32_t len); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_MODES #endif // CRYPT_MODES_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes.h
C
unknown
2,376
/* * 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_MODES_CBC_H #define CRYPT_MODES_CBC_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CBC #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus // CBC mode universal implementation MODES_CipherCtx *MODES_CBC_NewCtx(int32_t algId); int32_t MODES_CBC_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_CBC_DeInitCtx(MODES_CipherCtx *modeCtx); int32_t MODES_CBC_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen); void MODES_CBC_FreeCtx(MODES_CipherCtx *modeCtx); #ifdef HITLS_CRYPTO_AES // AES CBC optimization implementation int32_t AES_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t AES_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); #endif #ifdef HITLS_CRYPTO_SM4 // SM4 CBC optimization implementation int32_t SM4_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t SM4_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t SM4_CBC_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); #endif int32_t MODES_CBC_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CBC_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_CBC_FinalEx(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_CBC #endif // CRYPT_MODES_CBC_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_cbc.h
C
unknown
2,514
/* * 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_MODES_CCM_H #define CRYPT_MODES_CCM_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CCM #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define CCM_BLOCKSIZE 16 typedef struct { void *ciphCtx; /* Context defined by each algorithm */ const EAL_SymMethod *ciphMeth; /* Corresponding to the related methods for each symmetric algorithm */ uint8_t nonce[CCM_BLOCKSIZE]; /* Data nonce, ctr encrypted data */ uint8_t tag[CCM_BLOCKSIZE]; /* Data tag, intermediate data encrypted by the CBC */ uint8_t last[CCM_BLOCKSIZE]; /* Previous data block in ctr mode */ uint64_t msgLen; /* The message length */ uint8_t lastLen; /* Unused data length of the previous data block in ctr mode. */ uint8_t tagLen; /* The length of the tag is 16 by default. The tag is reset each time the key is set. */ uint8_t tagInit; /* Indicate whether the tag is initialized. */ } MODES_CipherCCMCtx; struct ModesCcmCtx { int32_t algId; MODES_CipherCCMCtx ccmCtx; bool enc; }; typedef struct ModesCcmCtx MODES_CCM_Ctx; // CCM mode universal implementation MODES_CCM_Ctx *MODES_CCM_NewCtx(int32_t algId); int32_t MODES_CCM_InitCtx(MODES_CCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_CCM_Update(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CCM_Final(MODES_CCM_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_CCM_DeInitCtx(MODES_CCM_Ctx *modeCtx); int32_t MODES_CCM_Ctrl(MODES_CCM_Ctx *modeCtx, int32_t opt, void *val, uint32_t len); void MODES_CCM_FreeCtx(MODES_CCM_Ctx *modeCtx); // AES CCM optimization implementation int32_t AES_CCM_Update(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CCM_UpdateEx(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_CCM #endif // CRYPT_MODES_CCM_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_ccm.h
C
unknown
2,671
/* * 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_MODES_CFB_H #define CRYPT_MODES_CFB_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CFB #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define DES_BLOCK_BYTE_NUM 8 typedef struct { MODES_CipherCommonCtx modeCtx; uint8_t feedbackBits; /* Save the FeedBack length. */ uint8_t cipherCache[3][DES_BLOCK_BYTE_NUM]; uint8_t cacheIndex; /* Used by the TDES that has 3IV. Indicate which cache is being used. */ } MODES_CipherCFBCtx; struct ModesCFBCtx { int32_t algId; MODES_CipherCFBCtx cfbCtx; bool enc; }; typedef struct ModesCFBCtx MODES_CFB_Ctx; // CFB mode universal implementation MODES_CFB_Ctx *MODES_CFB_NewCtx(int32_t algId); int32_t MODES_CFB_InitCtx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CFB_Final(MODES_CFB_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_CFB_DeInitCtx(MODES_CFB_Ctx *modeCtx); int32_t MODES_CFB_Ctrl(MODES_CFB_Ctx *modeCtx, int32_t opt, void *val, uint32_t len); void MODES_CFB_FreeCtx(MODES_CFB_Ctx *modeCtx); // AES CFB optimization implementation int32_t AES_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); // SM4 CFB optimization implementation int32_t SM4_CFB_InitCtx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t SM4_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CFB_InitCtxEx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_CFB_UpdateEx(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_CFB #endif // CRYPT_MODES_CFB_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_cfb.h
C
unknown
2,619
/* * 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_MODES_CHACHA20POLY1305_H #define CRYPT_MODES_CHACHA20POLY1305_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct { uint32_t acc[6]; // The intermediate data of the acc, must be greater than 130 bits. uint32_t r[4]; // Key information r, 16 bytes, that is, 4 * sizeof(uint32_t) uint32_t s[4]; // Key information s, 16 bytes, that is, 4 * sizeof(uint32_t) uint32_t table[36]; // Indicates the table used to accelerate the assembly calculation. uint8_t last[16]; // A block 16 bytes are cached for the last unprocessed data. uint32_t lastLen; // Indicates the remaining length of the last data. uint32_t flag; // Used to save the assembly status information. } Poly1305Ctx; typedef struct { void *key; // Handle for the method. const EAL_SymMethod *method; // algorithm method Poly1305Ctx polyCtx; uint64_t aadLen; // Status, indicating whether identification data is set. uint64_t cipherTextLen; // status, indicating whether the identification data is set. } MODES_CipherChaChaPolyCtx; struct ModesChaChaCtx { int32_t algId; MODES_CipherChaChaPolyCtx chachaCtx; bool enc; }; typedef struct ModesChaChaCtx MODES_CHACHAPOLY_Ctx; MODES_CHACHAPOLY_Ctx *MODES_CHACHA20POLY1305_NewCtx(int32_t algId); int32_t MODES_CHACHA20POLY1305_InitCtx(MODES_CHACHAPOLY_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_CHACHA20POLY1305_Update(MODES_CHACHAPOLY_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CHACHA20POLY1305_Final(MODES_CHACHAPOLY_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_CHACHA20POLY1305_DeInitCtx(MODES_CHACHAPOLY_Ctx *modeCtx); int32_t MODES_CHACHA20POLY1305_Ctrl(MODES_CHACHAPOLY_Ctx *modeCtx, int32_t cmd, void *val, uint32_t len); void MODES_CHACHA20POLY1305_FreeCtx(MODES_CHACHAPOLY_Ctx *modeCtx); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_CHACHA20POLY1305 #endif // CRYPT_MODES_CHACHA20POLY1305_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_chacha20poly1305.h
C
unknown
2,798
/* * 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_MODES_CTR_H #define CRYPT_MODES_CTR_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CTR #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus // CTR mode universal implementation MODES_CipherCtx *MODES_CTR_NewCtx(int32_t algId); int32_t MODES_CTR_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_CTR_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_CTR_DeInitCtx(MODES_CipherCtx *modeCtx); int32_t MODES_CTR_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen); void MODES_CTR_FreeCtx(MODES_CipherCtx *modeCtx); // AES CTR optimization implementation int32_t AES_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); // SM4 CTR optimization implementation int32_t SM4_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t SM4_CTR_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_CTR_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_CTR_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_CTR #endif // CRYPT_MODES_CTR_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_ctr.h
C
unknown
2,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. */ #ifndef CRYPT_MODES_ECB_H #define CRYPT_MODES_ECB_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_ECB #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus // ECB mode universal implementation MODES_CipherCtx *MODES_ECB_NewCtx(int32_t algId); int32_t MODES_ECB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_ECB_DeinitCtx(MODES_CipherCtx *modeCtx); int32_t MODES_ECB_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen); void MODES_ECB_FreeCtx(MODES_CipherCtx *modeCtx); // AES ECB optimization implementation int32_t AES_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t AES_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); // SM4 ECB optimization implementation int32_t SM4_ECB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t SM4_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t SM4_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_ECB_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_ECB_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_ECB_FinalEx(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif // __cplusplus #endif #endif
2301_79861745/bench_create
crypto/modes/include/crypt_modes_ecb.h
C
unknown
2,409
/* * 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_MODES_GCM_H #define CRYPT_MODES_GCM_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_GCM #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define GCM_MAX_COMBINED_LENGTH (((uint64_t)1 << 36) - 32) #define GCM_MAX_INVOCATIONS_TIMES ((uint32_t)(-1)) #define GCM_BLOCK_MASK (0xfffffff0) typedef struct { uint64_t h; uint64_t l; } MODES_GCM_GF128; #define GCM_BLOCKSIZE 16 typedef struct { uint8_t iv[GCM_BLOCKSIZE]; // Processed IV information. The length is 16 bytes. uint8_t ghash[GCM_BLOCKSIZE]; // Intermediate data for tag calculation. MODES_GCM_GF128 hTable[16]; // The window uses 4 bits, 2 ^ 4 = 16 entries need to be pre-calculated. void *ciphCtx; // Context defined by each symmetric algorithm. const EAL_SymMethod *ciphMeth; // algorithm method uint8_t tagLen; uint32_t cryptCnt; // Indicate the number of encryption times that the key can be used. uint8_t last[GCM_BLOCKSIZE]; // ctr mode last uint8_t remCt[GCM_BLOCKSIZE]; // Remaining ciphertext uint8_t ek0[GCM_BLOCKSIZE]; // ek0 uint64_t plaintextLen; // use for calc tag uint32_t aadLen; // use for calc tag uint32_t lastLen; // ctr mode lastLen } MODES_CipherGCMCtx; struct ModesGcmCtx { int32_t algId; MODES_CipherGCMCtx gcmCtx; bool enc; }; typedef struct ModesGcmCtx MODES_GCM_Ctx; // GCM mode universal implementation MODES_GCM_Ctx *MODES_GCM_NewCtx(int32_t algId); int32_t MODES_GCM_InitCtx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_GCM_Final(MODES_GCM_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_GCM_DeInitCtx(MODES_GCM_Ctx *modeCtx); int32_t MODES_GCM_Ctrl(MODES_GCM_Ctx *modeCtx, int32_t cmd, void *val, uint32_t len); void MODES_GCM_FreeCtx(MODES_GCM_Ctx *modeCtx); // AES GCM optimization implementation int32_t AES_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); // SM4 GCM optimization implementation int32_t SM4_GCM_InitCtx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t SM4_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_GCM_InitCtxEx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_GCM_UpdateEx(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_GCM_InitHashTable(MODES_CipherGCMCtx *ctx); int32_t MODES_GCM_SetKey(MODES_CipherGCMCtx *ctx, const uint8_t *key, uint32_t len); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_GCM #endif // CRYPT_MODES_GCM_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_gcm.h
C
unknown
3,545
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef CRYPT_MODES_OFB_H #define CRYPT_MODES_OFB_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_OFB #include "crypt_types.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus // OFB mode universal implementation MODES_CipherCtx *MODES_OFB_NewCtx(int32_t algId); int32_t MODES_OFB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_OFB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_OFB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_OFB_DeInitCtx(MODES_CipherCtx *modeCtx); int32_t MODES_OFB_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen); void MODES_OFB_FreeCtx(MODES_CipherCtx *modeCtx); // SM4 OFB optimization implementation int32_t SM4_OFB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t SM4_OFB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_OFB_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_OFB_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_OFB #endif // CRYPT_MODES_OFB_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_ofb.h
C
unknown
2,050
/* * 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_MODES_XTS_H #define CRYPT_MODES_XTS_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_XTS #include "crypt_types.h" #include "bsl_params.h" #include "crypt_modes.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct { void *ciphCtx; /* Key defined by each algorithm */ const EAL_SymMethod *ciphMeth; /* corresponding to the encrypt and decrypt in the bottom layer, operate keyctx */ uint8_t iv[MODES_MAX_IV_LENGTH]; /* The length is blocksize */ uint8_t tweak[MODES_MAX_IV_LENGTH]; /* The length is blocksize */ uint8_t blockSize; /* Save the block size. */ } MODES_CipherXTSCtx; struct ModesXTSCtx { int32_t algId; MODES_CipherXTSCtx xtsCtx; uint8_t data[EAL_MAX_BLOCK_LENGTH]; /**< last data block that may not be processed */ uint8_t dataLen; /**< size of the last data block that may not be processed. */ CRYPT_PaddingType pad; bool enc; }; typedef struct ModesXTSCtx MODES_XTS_Ctx; // XTS mode universal implementation MODES_XTS_Ctx *MODES_XTS_NewCtx(int32_t algId); int32_t MODES_XTS_InitCtx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODES_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_XTS_Final(MODES_XTS_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_XTS_DeInitCtx(MODES_XTS_Ctx *modeCtx); int32_t MODES_XTS_Ctrl(MODES_XTS_Ctx *modeCtx, int32_t cmd, void *val, uint32_t len); void MODES_XTS_FreeCtx(MODES_XTS_Ctx *modeCtx); // XTS mode universal implementation int32_t SM4_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t SM4_XTS_InitCtx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t SM4_XTS_Final(MODES_XTS_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); int32_t MODES_XTS_InitCtxEx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc); int32_t MODES_XTS_UpdateEx(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t AES_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t AES_XTS_Final(MODES_XTS_Ctx *modeCtx, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_XTS #endif // CRYPT_MODES_XTS_H
2301_79861745/bench_create
crypto/modes/include/crypt_modes_xts.h
C
unknown
3,133
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) .macro GCM_ENC128_LOOP ldp x6, x7, [INPUT], #16 // AES[0] - load plaintext rev64 OUT0.16b, OUT0.16b // GHASH blocl[0] rev64 OUT2.16b, OUT2.16b // GHASH block[2] ROUND CTR2.16b, KEY0.16b #ifdef HITLS_BIG_ENDIAN rev x6, x6 rev x7, x7 #endif fmov d3, x10 // CTR[3] ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 rev64 OUT1.16b, OUT1.16b // GHASH block[1] ROUND CTR1.16b, KEY0.16b add IV_W, IV_W, #1 // CTR3++ fmov CTR3.d[1], x9 // CTR[3]--OK ROUND CTR0.16b, KEY0.16b mov d31, OUT2.d[1] // GHASH block[2.1] ROUND CTR2.16b, KEY1.16b mov d30, OUT1.d[1] // GHASH block[1.1] ROUND CTR1.16b, KEY1.16b eor v4.16b, OUT0.16b, HASH0.16b // PRE 1 ROUND CTR3.16b, KEY0.16b eor x7, x7, KEND1 // AES[0] - round 10 high pmull2 v28.1q, OUT1.2d, HASH3.2d // GHASH block 4k+1 - high eor v31.8b, v31.8b, OUT2.8b // GHASH[2] - mid ldp x19, x20, [INPUT], #16 // AES[1] - load plaintext ROUND CTR0.16b, KEY1.16b rev w9, IV_W // CTR0--Start eor v30.8b, v30.8b, OUT1.8b // GHASH block 4k+1 - mid #ifdef HITLS_BIG_ENDIAN rev x19, x19 rev x20, x20 #endif mov d8, v4.d[1] // GHASH block 4k - mid orr x9, x11, x9, lsl #32 // CTR0 block 4k+8 pmull2 v9.1q, v4.2d, HASH4.2d // GHASH block 4k - high add IV_W, IV_W, #1 // CTR0++ mov d10, v17.d[1] // GHASH block 4k - mid ROUND CTR0.16b, KEY2.16b pmull HASH0.1q, v4.1d, HASH4.1d // GHASH block 4k - low eor v8.8b, v8.8b, v4.8b // GHASH block 4k - mid ROUND CTR1.16b, KEY2.16b ROUND CTR0.16b, KEY3.16b eor v9.16b, v9.16b, v28.16b // GHASH block 4k+1 - high pmull v28.1q, OUT2.1d, HASH2.1d // GHASH[2] - low pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid rev64 OUT3.16b, OUT3.16b // GHASH[0] (t0, t1, t2 and t3 free) pmull v30.1q, v30.1d, v17.1d // GHASH block 4k+1 - mid pmull v29.1q, OUT1.1d, HASH3.1d // GHASH block 4k+1 - low ins v31.d[1], v31.d[0] // GHASH[2] - mid pmull2 v8.1q, OUT2.2d, HASH2.2d // GHASH[2] - high eor x20, x20, KEND1 // AES[1] - round 10 high eor v10.16b, v10.16b, v30.16b // GHASH block 4k+1 - mid mov d30, OUT3.d[1] // GHASH[0] - mid ROUND CTR3.16b, v19.16b eor HASH0.16b, HASH0.16b, v29.16b // GHASH block 4k+1 - low ROUND CTR2.16b, KEY2.16b eor x6, x6, KEND0 // AES[0] - round 10 low ROUND CTR1.16b, KEY3.16b eor v30.8b, v30.8b, OUT3.8b // GHASH[0] - mid pmull2 v4.1q, OUT3.2d, HASH1.2d // GHASH[0] - high ROUND CTR2.16b, KEY3.16b eor v9.16b, v9.16b, v8.16b // GHASH[2] - high pmull2 v31.1q, v31.2d, v16.2d // GHASH[2] - mid pmull v29.1q, OUT3.1d, HASH1.1d // GHASH[0] - low movi v8.8b, #0xc2 pmull v30.1q, v30.1d, v16.1d // GHASH[0] - mid eor HASH0.16b, HASH0.16b, v28.16b // GHASH[2] - low ROUND CTR1.16b, KEY4.16b ROUND CTR3.16b, v20.16b shl d8, d8, #56 // mod_constant ROUND CTR0.16b, KEY4.16b eor v9.16b, v9.16b, v4.16b // GHASH[0] - high ROUND CTR1.16b, KEY5.16b ldp x21, x22, [INPUT], #16 // AES[2] - load plaintext ROUND CTR3.16b, v21.16b eor v10.16b, v10.16b, v31.16b // GHASH[2] - mid #ifdef HITLS_BIG_ENDIAN rev x21, x21 rev x22, x22 #endif ROUND CTR0.16b, KEY5.16b ldp x23, x24, [INPUT], #16 // AES[3] - load plaintext pmull v31.1q, v9.1d, v8.1d // MODULO - top 64b align with mid eor HASH0.16b, HASH0.16b, v29.16b // GHASH[0] - low #ifdef HITLS_BIG_ENDIAN rev x23, x23 rev x24, x24 #endif ROUND CTR2.16b, KEY4.16b eor x19, x19, KEND0 // AES[1] - round 10 low ROUND CTR3.16b, v22.16b eor v10.16b, v10.16b, v30.16b // GHASH[0] - mid ROUND CTR1.16b, KEY6.16b eor x23, x23, KEND0 // AES[3] - round 10 low ROUND CTR2.16b, KEY5.16b eor v30.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up fmov d4, x6 // AES[0] - mov low ROUND CTR0.16b, KEY6.16b fmov OUT0.d[1], x7 // AES[0] - mov high fmov d7, x23 // AES[3] - mov low ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment ROUND CTR3.16b, v23.16b fmov d5, x19 // AES[2] - mov low ROUND CTR0.16b, KEY7.16b eor v10.16b, v10.16b, v30.16b // MODULO - karatsuba tidy up ROUND CTR2.16b, KEY6.16b eor x24, x24, KEND1 // AES[3] - round 10 high ROUND CTR1.16b, KEY7.16b fmov OUT1.d[1], x20 // AES[1] - mov high ROUND CTR0.16b, KEY8.16b fmov OUT3.d[1], x24 // AES[3] - mov high ROUND CTR3.16b, v24.16b subs COUNT, COUNT, #1 // count-- ROUND CTR1.16b, KEY8.16b eor v10.16b, v10.16b, v31.16b // MODULO - fold into mid aese CTR0.16b, KEY9.16b eor x21, x21, KEND0 // AES[2] - round 10 low eor x22, x22, KEND1 // AES[2] - round 10 high ROUND CTR3.16b, v25.16b fmov d6, x21 // AES[2] - mov low aese CTR1.16b, KEY9.16b // AES[1] - round 9 fmov OUT2.d[1], x22 // AES[2] - mov high ROUND CTR2.16b, KEY7.16b eor OUT0.16b, OUT0.16b, CTR0.16b // AES[0] - result fmov d0, x10 // CTR0-0 ROUND CTR3.16b, KEY8.16b fmov CTR0.d[1], x9 // CTR0-1--OK rev w9, IV_W // CTR1--start eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid ROUND CTR2.16b, KEY8.16b eor OUT1.16b, OUT1.16b, CTR1.16b // AES[1] - result add IV_W, IV_W, #1 // CTR1++ orr x9, x11, x9, lsl #32 // CTR1 block 4k+9 fmov d1, x10 // CTR1-0 pmull v9.1q, v10.1d, v8.1d // MODULO - mid 64b align with low fmov CTR1.d[1], x9 // CTR1-1--OK rev w9, IV_W // CTR2--Start aese CTR2.16b, KEY9.16b st1 {OUT0.16b}, [OUT00], #16 // Write back - OUT0 eor OUT2.16b, OUT2.16b, CTR2.16b // AES[2]-result orr x9, x11, x9, lsl #32 // CTR2 block 4k+10 aese CTR3.16b, KEY9.16b add IV_W, IV_W, #1 // CTR2++ ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment fmov d2, x10 // CTR2-0 eor HASH0.16b, HASH0.16b, v9.16b // MODULO - fold into low st1 {OUT1.16b}, [OUT00], #16 // Write back - OUT1 fmov CTR2.d[1], x9 // CTR2-1--OK st1 {OUT2.16b}, [OUT00], #16 // Write back - OUT2 rev w9, IV_W // CTR3--start eor OUT3.16b, OUT3.16b, CTR3.16b // AES[3]-result orr x9, x11, x9, lsl #32 // CTR3 block 4k+11 eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low st1 {OUT3.16b}, [OUT00], #16 // Write back - OUT3 .endm .macro GCM_DEC128_LOOP eor CTR3.16b, OUT3.16b, CTR3.16b // AES[3] - result ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 mov x21, CTR2.d[0] // AES[2] - mov low pmull2 v28.1q, v5.2d, HASH3.2d // GHASH block 4k+1 - high mov x22, CTR2.d[1] // AES[2] - mov high ROUND CTR1.16b, KEY0.16b fmov d2, x10 // CTR[3] #ifdef HITLS_BIG_ENDIAN rev x21, x21 rev x22, x22 #endif rev64 OUT2.16b, OUT2.16b // GHASH[2] fmov v2.d[1], x9 // CTR[3] rev w9, IV_W // CTR[0] mov x23, CTR3.d[0] // AES[3] - mov low eor v4.16b, v4.16b, HASH0.16b // PRE 1 mov d30, v5.d[1] // GHASH block 4k+1 - mid ROUND CTR1.16b, KEY1.16b rev64 v7.16b, v7.16b // GHASH[0] pmull v29.1q, v5.1d, HASH3.1d // GHASH block 4k+1 - low mov x24, CTR3.d[1] // AES[3] - mov high orr x9, x11, x9, lsl #32 // CTR[0] pmull HASH0.1q, v4.1d, HASH4.1d // GHASH block 4k - low #ifdef HITLS_BIG_ENDIAN rev x23, x23 rev x24, x24 #endif fmov d3, x10 // CTR[0] eor v30.8b, v30.8b, v5.8b // GHASH block 4k+1 - mid ROUND CTR1.16b, KEY2.16b fmov v3.d[1], x9 // CTR[0] ROUND CTR2.16b, KEY0.16b mov d10, v17.d[1] // GHASH block 4k - mid pmull2 v9.1q, v4.2d, HASH4.2d // GHASH block 4k - high eor HASH0.16b, HASH0.16b, v29.16b // GHASH block 4k+1 - low pmull v29.1q, v7.1d, HASH1.1d // GHASH[0] - low ROUND CTR1.16b, KEY3.16b mov d8, v4.d[1] // GHASH block 4k - mid ROUND CTR3.16b, KEY0.16b eor v9.16b, v9.16b, v28.16b // GHASH block 4k+1 - high ROUND CTR0.16b, KEY0.16b pmull v28.1q, v6.1d, HASH2.1d // GHASH[2] - low eor v8.8b, v8.8b, v4.8b // GHASH block 4k - mid ROUND CTR3.16b, KEY1.16b eor x23, x23, KEND0 // AES[3] - round 10 low pmull v30.1q, v30.1d, v17.1d // GHASH block 4k+1 - mid eor x22, x22, KEND1 // AES[2] - round 10 high mov d31, v6.d[1] // GHASH[2] - mid ROUND CTR0.16b, KEY1.16b eor HASH0.16b, HASH0.16b, v28.16b // GHASH[2] - low pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid ROUND CTR3.16b, KEY2.16b eor v31.8b, v31.8b, v6.8b // GHASH[2] - mid ROUND CTR0.16b, KEY2.16b ROUND CTR1.16b, KEY4.16b eor v10.16b, v10.16b, v30.16b // GHASH block 4k+1 - mid pmull2 v8.1q, v6.2d, HASH2.2d // GHASH[2] - high ROUND CTR0.16b, KEY3.16b ins v31.d[1], v31.d[0] // GHASH[2] - mid pmull2 v4.1q, v7.2d, HASH1.2d // GHASH[0] - high ROUND CTR2.16b, KEY1.16b mov d30, v7.d[1] // GHASH[0] - mid ROUND CTR0.16b, KEY4.16b eor v9.16b, v9.16b, v8.16b // GHASH[2] - high pmull2 v31.1q, v31.2d, v16.2d // GHASH[2] - mid eor x24, x24, KEND1 // AES[3] - round 10 high ROUND CTR2.16b, KEY2.16b eor v30.8b, v30.8b, v7.8b // GHASH[0] - mid ROUND CTR1.16b, KEY5.16b eor x21, x21, KEND0 // AES[2] - round 10 low ROUND CTR0.16b, KEY5.16b movi v8.8b, #0xc2 ROUND CTR2.16b, KEY3.16b eor HASH0.16b, HASH0.16b, v29.16b // GHASH[0] - low ROUND CTR1.16b, KEY6.16b ROUND CTR0.16b, KEY6.16b eor v10.16b, v10.16b, v31.16b // GHASH[2] - mid ROUND CTR2.16b, KEY4.16b stp x21, x22, [OUT00], #16 // AES[2] - store result pmull v30.1q, v30.1d, v16.1d // GHASH[0] - mid eor v9.16b, v9.16b, v4.16b // GHASH[0] - high ld1 {OUT0.16b}, [INPUT], #16 // AES[0] - load ciphertext ROUND CTR1.16b, KEY7.16b add IV_W, IV_W, #1 // CTR++ ROUND CTR0.16b, KEY7.16b shl d8, d8, #56 // mod_constant ROUND CTR2.16b, KEY5.16b eor v10.16b, v10.16b, v30.16b // GHASH[0] - mid ROUND CTR1.16b, KEY8.16b stp x23, x24, [OUT00], #16 // AES[3] - store result ROUND CTR0.16b, KEY8.16b eor v30.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up ROUND CTR3.16b, KEY3.16b rev w9, IV_W // CTR block 4k+8 pmull v31.1q, v9.1d, v8.1d // MODULO - top 64b align with mid ld1 {OUT1.16b}, [INPUT], #16 // AES[1] - load ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment aese CTR0.16b, KEY9.16b // AES[0] - round 9 orr x9, x11, x9, lsl #32 // CTR block 4k+8 ROUND CTR3.16b, KEY4.16b eor v10.16b, v10.16b, v30.16b // MODULO - karatsuba tidy up aese CTR1.16b, KEY9.16b // AES[1] - round 9 ROUND CTR2.16b, KEY6.16b eor CTR0.16b, OUT0.16b, CTR0.16b // AES[0] - result ROUND CTR3.16b, KEY5.16b ld1 {OUT2.16b}, [INPUT], #16 // AES[2] - load add IV_W, IV_W, #1 // CTR++ eor v10.16b, v10.16b, v31.16b // MODULO - fold into mid eor CTR1.16b, OUT1.16b, CTR1.16b // AES[1] - result ROUND CTR2.16b, KEY7.16b ld1 {OUT3.16b}, [INPUT], #16 ROUND CTR3.16b, KEY6.16b rev64 OUT1.16b, OUT1.16b // GHASH block[1] eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid mov x7, CTR0.d[1] // AES[0] - mov high ROUND CTR2.16b, KEY8.16b mov x6, CTR0.d[0] // AES[0] - mov low ROUND CTR3.16b, KEY7.16b fmov d0, x10 // CTR[0] #ifdef HITLS_BIG_ENDIAN rev x7, x7 rev x6, x6 #endif pmull v8.1q, v10.1d, v8.1d // MODULO - mid 64b align with low fmov CTR0.d[1], x9 // CTR[0] - OK rev w9, IV_W // CTR block 4k+9 aese CTR2.16b, KEY9.16b orr x9, x11, x9, lsl #32 // CTR block 4k+9 ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment ROUND CTR3.16b, KEY8.16b eor x7, x7, KEND1 // AES[0] - round 10 high eor HASH0.16b, HASH0.16b, v8.16b // MODULO - fold into low mov x20, CTR1.d[1] // AES[1] - mov high eor x6, x6, KEND0 // AES[0] - round 10 low eor CTR2.16b, OUT2.16b, CTR2.16b // AES[2] - result mov x19, CTR1.d[0] // AES[1] - mov low add IV_W, IV_W, #1 // CTR++ aese CTR3.16b, KEY9.16b fmov d1, x10 // CTR[1] #ifdef HITLS_BIG_ENDIAN rev x20, x20 rev x19, x19 #endif subs COUNT, COUNT, #1 // COUNT-- rev64 OUT0.16b, OUT0.16b // GHASH block[0] eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low fmov v1.d[1], x9 // CTR[1] - OK rev w9, IV_W // CTR block 4k+10 add IV_W, IV_W, #1 // CTR block 4k+10 eor x20, x20, KEND1 // AES[1] - round 10 high stp x6, x7, [OUT00], #16 // AES[0] - store result eor x19, x19, KEND0 // AES[1] - round 10 low stp x19, x20, [OUT00], #16 // AES[1] - store result orr x9, x11, x9, lsl #32 // CTR block 4k+10 .endm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes128_gcm_aarch64.S
Unix Assembly
unknown
16,590
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) .macro GCM_ENC192_LOOP ROUND CTR2.16b, KEY0.16b rev64 OUT1.16b, OUT1.16b // GHASH block 4k+1 (t0 and t1 free) ROUND CTR1.16b, KEY0.16b ldp x6, x7, [INPUT], #16 // AES[0] - load plaintext ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 fmov d3, x10 // CTR[3] rev64 OUT0.16b, OUT0.16b // GHASH block 4k (only t0 is free) ROUND CTR2.16b, KEY1.16b fmov CTR3.d[1], x9 // CTR[3]--OK #ifdef HITLS_BIG_ENDIAN rev x6, x6 rev x7, x7 #endif pmull2 v30.1q, v5.2d, HASH3.2d // GHASH block 4k+1 - high rev64 OUT3.16b, OUT3.16b // GHASH[0] (t0, t1, t2 and t3 free) ldp x19, x20, [INPUT], #16 // AES[1] - load plaintext ROUND CTR0.16b, KEY0.16b ldp x21, x22, [INPUT], #16 // AES[2] - load plaintext pmull v31.1q, v5.1d, HASH3.1d // GHASH block 4k+1 - low eor v4.16b, v4.16b, HASH0.16b // PRE 1 #ifdef HITLS_BIG_ENDIAN rev x19, x19 rev x20, x20 rev x21, x21 rev x22, x22 #endif ROUND CTR1.16b, KEY1.16b ROUND CTR0.16b, KEY1.16b rev64 OUT2.16b, OUT2.16b // GHASH[2] (t0, t1, and t2 free) ROUND CTR3.16b, KEY0.16b eor x7, x7, KEND1 // AES[0] - round 12 high pmull HASH0.1q, v4.1d, HASH4.1d // GHASH block 4k - low mov d8, v4.d[1] // GHASH block 4k - mid ROUND CTR0.16b, KEY2.16b ROUND CTR3.16b, KEY1.16b eor x6, x6, KEND0 // AES[0] - round 12 low eor v8.8b, v8.8b, v4.8b // GHASH block 4k - mid eor HASH0.16b, HASH0.16b, v31.16b // GHASH block 4k+1 - low ROUND CTR0.16b, KEY3.16b eor x19, x19, KEND0 // AES[1] - round 12 low ROUND CTR1.16b, KEY2.16b mov d31, v6.d[1] // GHASH[2] - mid pmull2 v9.1q, v4.2d, HASH4.2d // GHASH block 4k - high mov d4, v5.d[1] // GHASH block 4k+1 - mid ROUND CTR2.16b, KEY2.16b ROUND CTR1.16b, KEY3.16b mov d10, v17.d[1] // GHASH block 4k - mid eor v9.16b, v9.16b, v30.16b // GHASH block 4k+1 - high ROUND CTR3.16b, KEY2.16b eor v31.8b, v31.8b, v6.8b // GHASH[2] - mid pmull2 v30.1q, v6.2d, HASH2.2d // GHASH[2] - high ROUND CTR0.16b, KEY4.16b eor v4.8b, v4.8b, v5.8b // GHASH block 4k+1 - mid ROUND CTR3.16b, KEY3.16b pmull2 v5.1q, v7.2d, HASH1.2d // GHASH[0] - high eor x20, x20, KEND1 // AES[1] - round 12 high ins v31.d[1], v31.d[0] // GHASH[2] - mid ROUND CTR0.16b, KEY5.16b add IV_W, IV_W, #1 // CTR++ ROUND CTR3.16b, KEY4.16b eor v9.16b, v9.16b, v30.16b // GHASH[2] - high pmull v4.1q, v4.1d, v17.1d // GHASH block 4k+1 - mid eor x22, x22, KEND1 // AES[2] - round 12 high pmull2 v31.1q, v31.2d, v16.2d // GHASH[2] - mid eor x21, x21, KEND0 // AES[2] - round 12 low mov d30, v7.d[1] // GHASH[0] - mid pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid rev w9, IV_W // CTR[0] pmull v8.1q, v6.1d, HASH2.1d // GHASH[2] - low orr x9, x11, x9, lsl #32 // CTR[0] ROUND CTR2.16b, KEY3.16b eor v30.8b, v30.8b, v7.8b // GHASH[0] - mid ROUND CTR1.16b, KEY4.16b ldp x23, x24, [INPUT], #16 // AES[3] - load plaintext ROUND CTR0.16b, KEY6.16b eor HASH0.16b, HASH0.16b, v8.16b // GHASH[2] - low ROUND CTR2.16b, KEY4.16b #ifdef HITLS_BIG_ENDIAN rev x23, x23 rev x24, x24 #endif ROUND CTR1.16b, KEY5.16b movi v8.8b, #0xc2 pmull v6.1q, v7.1d, HASH1.1d // GHASH[0] - low eor x24, x24, KEND1 // AES[3] - round 12 high eor v10.16b, v10.16b, v4.16b // GHASH block 4k+1 - mid ROUND CTR2.16b, KEY5.16b eor x23, x23, KEND0 // AES[3] - round 12 low ROUND CTR1.16b, KEY6.16b shl d8, d8, #56 // mod_constant ROUND CTR3.16b, KEY5.16b eor v9.16b, v9.16b, v5.16b // GHASH[0] - high ROUND CTR0.16b, KEY7.16b fmov d5, x19 // AES[1] - mov low ROUND CTR1.16b, KEY7.16b eor v10.16b, v10.16b, v31.16b // GHASH[2] - mid ROUND CTR3.16b, KEY6.16b fmov OUT1.d[1], x20 // AES[1] - mov high ROUND CTR0.16b, KEY8.16b eor HASH0.16b, HASH0.16b, v6.16b // GHASH[0] - low pmull v30.1q, v30.1d, v16.1d // GHASH[0] - mid subs COUNT, COUNT, #1 // count-- fmov d4, x6 // AES[0] - mov low ROUND CTR2.16b, KEY6.16b fmov OUT0.d[1], x7 // AES[0] - mov high ROUND CTR1.16b, KEY8.16b fmov d7, x23 // AES[0] - mov low eor v10.16b, v10.16b, v30.16b // GHASH[0] - mid eor v30.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up add IV_W, IV_W, #1 // CTR++ ROUND CTR2.16b, KEY7.16b fmov OUT3.d[1], x24 // AES[3] - mov high pmull v31.1q, v9.1d, v8.1d // MODULO - top 64b align with mid ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment fmov d6, x21 // AES[3] - mov low ROUND CTR3.16b, KEY7.16b ROUND CTR0.16b, KEY9.16b eor v10.16b, v10.16b, v30.16b // MODULO - karatsuba tidy up ROUND CTR2.16b, KEY8.16b ROUND CTR3.16b, KEY8.16b ROUND CTR1.16b, KEY9.16b ROUND CTR0.16b, KEY10.16b eor v10.16b, v10.16b, v31.16b // MODULO - fold into mid ROUND CTR3.16b, KEY9.16b ROUND CTR2.16b, KEY9.16b aese CTR0.16b, KEY11.16b // AES[1] - round 11 ROUND CTR1.16b, KEY10.16b eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid ROUND CTR2.16b, KEY10.16b eor OUT0.16b, OUT0.16b, CTR0.16b // AES[0] - result fmov d0, x10 // CTR[0] aese CTR1.16b, KEY11.16b // AES[2] - round 11 fmov CTR0.d[1], x9 // CTR[0]--OK rev w9, IV_W // CTR[1] pmull v9.1q, v10.1d, v8.1d // MODULO - mid 64b align with low fmov OUT2.d[1], x22 // AES[2] - mov high st1 {OUT0.16b}, [OUT00], #16 // AES[0] - store result ROUND CTR3.16b, KEY10.16b orr x9, x11, x9, lsl #32 // CTR[1] eor OUT1.16b, OUT1.16b, CTR1.16b // AES[1] - result add IV_W, IV_W, #1 // CTR++ fmov d1, x10 // CTR[1] aese CTR2.16b, KEY11.16b fmov v1.d[1], x9 // CTR[1]--OK rev w9, IV_W // CTR[2] add IV_W, IV_W, #1 // CTR++ ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment orr x9, x11, x9, lsl #32 // CTR[2] st1 {OUT1.16b}, [OUT00], #16 // AES[1] - store result eor HASH0.16b, HASH0.16b, v9.16b // MODULO - fold into low aese CTR3.16b, KEY11.16b // AES[2] - round 11 eor OUT2.16b, OUT2.16b, CTR2.16b // AES[2] - result fmov d2, x10 // CTR[2] st1 {OUT2.16b}, [OUT00], #16 // AES[2] - store result fmov CTR2.d[1], x9 // CTR[2]--OK rev w9, IV_W // CTR[3] eor OUT3.16b, OUT3.16b, CTR3.16b // AES[3] - result eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low orr x9, x11, x9, lsl #32 // CTR[3] st1 {OUT3.16b}, [OUT00], #16 // AES[3] - store result .endm .macro GCM_DEC192_LOOP ROUND CTR1.16b, KEY0.16b ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 pmull v31.1q, OUT1.1d, HASH3.1d // GHASH block 4k+1 - low mov x21, CTR2.d[0] // AES[2] block - mov low mov x22, CTR2.d[1] // AES[2] block - mov high eor CTR3.16b, OUT3.16b, CTR3.16b // AES[3] block - result rev64 v7.16b, v7.16b // GHASH[0] ROUND CTR1.16b, KEY1.16b fmov d2, x10 // CTR[2] block ROUND CTR0.16b, KEY0.16b #ifdef HITLS_BIG_ENDIAN rev x21, x21 rev x22, x22 #endif eor v4.16b, v4.16b, HASH0.16b // PRE 1 pmull2 v30.1q, v5.2d, HASH3.2d // GHASH block 4k+1 - high fmov CTR2.d[1], x9 // CTR[2]--OK ROUND CTR1.16b, KEY2.16b mov x24, CTR3.d[1] // AES[3] block - mov high ROUND CTR0.16b, KEY1.16b mov x23, CTR3.d[0] // AES[3] block - mov low pmull2 v9.1q, v4.2d, HASH4.2d // GHASH block 4k - high fmov d3, x10 // CTR[3] mov d8, v4.d[1] // GHASH block 4k - mid pmull HASH0.1q, v4.1d, HASH4.1d // GHASH block 4k - low #ifdef HITLS_BIG_ENDIAN rev x23, x23 rev x24, x24 #endif mov d10, v17.d[1] // GHASH block 4k - mid rev w9, IV_W // CTR[3] ROUND CTR2.16b, KEY0.16b orr x9, x11, x9, lsl #32 // CTR[3] fmov CTR3.d[1], x9 // CTR[3]--OK eor v8.8b, v8.8b, v4.8b // GHASH block 4k - mid mov d4, v5.d[1] // GHASH block 4k+1 - mid ROUND CTR1.16b, KEY3.16b ROUND CTR0.16b, KEY2.16b eor x22, x22, KEND1 // AES[2] block - round 12 high ROUND CTR2.16b, KEY1.16b eor v4.8b, v4.8b, v5.8b // GHASH block 4k+1 - mid pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid ROUND CTR3.16b, KEY0.16b rev64 v6.16b, v6.16b // GHASH[2] ROUND CTR2.16b, KEY2.16b pmull v4.1q, v4.1d, v17.1d // GHASH block 4k+1 - mid eor HASH0.16b, HASH0.16b, v31.16b // GHASH block 4k+1 - low eor x21, x21, KEND0 // AES[2] block - round 12 low ROUND CTR1.16b, KEY4.16b ROUND CTR0.16b, KEY3.16b eor v10.16b, v10.16b, v4.16b // GHASH block 4k+1 - mid mov d31, v6.d[1] // GHASH[2] - mid ROUND CTR3.16b, KEY1.16b eor v9.16b, v9.16b, v30.16b // GHASH block 4k+1 - high ROUND CTR0.16b, KEY4.16b pmull2 v30.1q, v6.2d, HASH2.2d // GHASH[2] - high eor v31.8b, v31.8b, v6.8b // GHASH[2] - mid pmull v8.1q, v6.1d, HASH2.1d // GHASH[2] - low ROUND CTR0.16b, KEY5.16b eor v9.16b, v9.16b, v30.16b // GHASH[2] - high mov d30, v7.d[1] // GHASH[0] - mid ROUND CTR1.16b, KEY5.16b pmull2 v5.1q, v7.2d, HASH1.2d // GHASH[0] - high ROUND CTR3.16b, KEY2.16b eor v30.8b, v30.8b, v7.8b // GHASH[0] - mid ROUND CTR1.16b, KEY6.16b ROUND CTR0.16b, KEY6.16b ins v31.d[1], v31.d[0] // GHASH[2] - mid ROUND CTR3.16b, KEY3.16b pmull v30.1q, v30.1d, v16.1d // GHASH[0] - mid eor HASH0.16b, HASH0.16b, v8.16b // GHASH[2] - low ROUND CTR0.16b, KEY7.16b pmull2 v31.1q, v31.2d, v16.2d // GHASH[2] - mid eor v9.16b, v9.16b, v5.16b // GHASH[0] - high ROUND CTR1.16b, KEY7.16b ROUND CTR0.16b, KEY8.16b movi v8.8b, #0xc2 pmull v6.1q, v7.1d, HASH1.1d // GHASH[0] - low ROUND CTR1.16b, KEY8.16b eor v10.16b, v10.16b, v31.16b // GHASH[2] - mid ROUND CTR2.16b, KEY3.16b ROUND CTR0.16b, KEY9.16b eor HASH0.16b, HASH0.16b, v6.16b // GHASH[0] - low ROUND CTR3.16b, KEY4.16b ROUND CTR2.16b, KEY4.16b eor v10.16b, v10.16b, v30.16b // GHASH[0] - mid ROUND CTR0.16b, KEY10.16b ROUND CTR1.16b, KEY9.16b eor v30.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up ROUND CTR2.16b, KEY5.16b ROUND CTR3.16b, KEY5.16b shl d8, d8, #56 // mod_constant ROUND CTR1.16b, KEY10.16b ROUND CTR2.16b, KEY6.16b ld1 {OUT0.16b}, [INPUT], #16 // AES load[0] ciphertext ROUND CTR3.16b, KEY6.16b eor v10.16b, v10.16b, v30.16b // MODULO - karatsuba tidy up pmull v31.1q, v9.1d, v8.1d // MODULO - top 64b align with mid ld1 {OUT1.16b}, [INPUT], #16 // AES load[1] ciphertext eor x23, x23, KEND0 // AES[3] block - round 12 low ROUND CTR2.16b, KEY7.16b ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment aese CTR0.16b, KEY11.16b add IV_W, IV_W, #1 // CTR++ ROUND CTR3.16b, KEY7.16b eor v10.16b, v10.16b, v31.16b // MODULO - fold into mid ld1 {OUT2.16b}, [INPUT], #16 // AES load[2] ciphertext ROUND CTR2.16b, KEY8.16b aese CTR1.16b, KEY11.16b ld1 {OUT3.16b}, [INPUT], #16 // AES load[3] ciphertext rev w9, IV_W // CTR block 4k+8 ROUND CTR3.16b, KEY8.16b stp x21, x22, [OUT00], #16 // AES[2] block - store result ROUND CTR2.16b, KEY9.16b eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid subs COUNT, COUNT, #1 // COUNT-- eor CTR0.16b, OUT0.16b, CTR0.16b // AES[0] block - result eor x24, x24, KEND1 // AES[3] block - round 12 high eor CTR1.16b, OUT1.16b, CTR1.16b // AES[1] block - result ROUND CTR2.16b, KEY10.16b orr x9, x11, x9, lsl #32 // CTR block 4k+8 ROUND CTR3.16b, KEY9.16b pmull v8.1q, v10.1d, v8.1d // MODULO - mid 64b align with low mov x19, CTR1.d[0] // AES[1] block - mov low mov x6, CTR0.d[0] // AES[0] block - mov low stp x23, x24, [OUT00], #16 // AES[3] - store result rev64 v5.16b, v5.16b // GHASH[2] aese CTR2.16b, KEY11.16b mov x7, CTR0.d[1] // AES[0] block - mov high ROUND CTR3.16b, KEY10.16b mov x20, CTR1.d[1] // AES[1] block - mov high #ifdef HITLS_BIG_ENDIAN rev x6, x6 rev x7, x7 rev x19, x19 rev x20, x20 #endif fmov d0, x10 // CTR[0] add IV_W, IV_W, #1 // CTR++ ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment eor CTR2.16b, OUT2.16b, CTR2.16b // AES[2] block - result fmov CTR0.d[1], x9 // CTR[0]--OK rev w9, IV_W // CTR block 4k+9 eor x6, x6, KEND0 // AES[0] block - round 12 low orr x9, x11, x9, lsl #32 // CTR block 4k+9 eor HASH0.16b, HASH0.16b, v8.16b // MODULO - fold into low fmov d1, x10 // CTR[1] add IV_W, IV_W, #1 // CTR++ eor x19, x19, KEND0 // AES[1] block - round 12 low fmov CTR1.d[1], x9 // CTR[1]--OK rev w9, IV_W // CTR block 4k+10 eor x20, x20, KEND1 // AES[2] - round 12 high eor x7, x7, KEND1 // AES[0] - round 12 high stp x6, x7, [OUT00], #16 // AES[0] block - store result eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low add IV_W, IV_W, #1 // CTR++ rev64 v4.16b, v4.16b // GHASH[1] orr x9, x11, x9, lsl #32 // CTR block 4k+10 aese CTR3.16b, KEY11.16b // AES[3] round 11 stp x19, x20, [OUT00], #16 // AES[1] block - store result .endm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes192_gcm_aarch64.S
Unix Assembly
unknown
17,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" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) .macro GCM_ENC256_LOOP ROUND CTR0.16b, KEY0.16b rev64 v4.16b, v4.16b // GHASH block 4k (only t0 is free) ROUND CTR1.16b, KEY0.16b fmov d3, x10 // CTR[3] ROUND CTR2.16b, KEY0.16b ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 ROUND CTR0.16b, KEY1.16b fmov CTR3.d[1], x9 // CTR[3] - OK ROUND CTR1.16b, KEY1.16b ldp x6, x7, [INPUT], #16 // AES[0] - load plaintext ROUND CTR2.16b, KEY1.16b ldp x19, x20, [INPUT], #16 // AES[1] - load plaintext ROUND CTR0.16b, KEY2.16b #ifdef HITLS_BIG_ENDIAN rev x6, x6 rev x7, x7 rev x19, x19 rev x20, x20 #endif eor v4.16b, v4.16b, HASH0.16b // PRE 1 ROUND CTR1.16b, KEY2.16b ROUND CTR3.16b, KEY0.16b eor x6, x6, KEND0 // AES[0] - round 14 low ROUND CTR0.16b, KEY3.16b mov d10, v17.d[1] // GHASH block 4k - mid pmull2 v9.1q, v4.2d, HASH4.2d // GHASH block 4k - high eor x7, x7, KEND1 // AES[0] - round 14 high mov d8, v4.d[1] // GHASH block 4k - mid ROUND CTR3.16b, KEY1.16b rev64 v5.16b, v5.16b // GHASH block 4k+1 (t0 and t1 free) ROUND CTR0.16b, KEY4.16b pmull HASH0.1q, v4.1d, HASH4.1d // GHASH block 4k - low eor v8.8b, v8.8b, v4.8b // GHASH block 4k - mid ROUND CTR2.16b, KEY2.16b ROUND CTR0.16b, KEY5.16b rev64 v7.16b, v7.16b // GHASH[0] (t0, t1, t2 and t3 free) pmull2 v4.1q, v5.2d, HASH3.2d // GHASH block 4k+1 - high pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid rev64 v6.16b, v6.16b // GHASH[2] (t0, t1, and t2 free) pmull v8.1q, v5.1d, HASH3.1d // GHASH block 4k+1 - low eor v9.16b, v9.16b, v4.16b // GHASH block 4k+1 - high mov d4, v5.d[1] // GHASH block 4k+1 - mid ROUND CTR1.16b, KEY3.16b ROUND CTR3.16b, KEY2.16b eor HASH0.16b, HASH0.16b, v8.16b // GHASH block 4k+1 - low ROUND CTR2.16b, KEY3.16b ROUND CTR1.16b, KEY4.16b mov d8, v6.d[1] // GHASH[2] - mid ROUND CTR3.16b, KEY3.16b eor v4.8b, v4.8b, v5.8b // GHASH block 4k+1 - mid ROUND CTR2.16b, KEY4.16b ROUND CTR0.16b, KEY6.16b eor v8.8b, v8.8b, v6.8b // GHASH[2] - mid ROUND CTR3.16b, KEY4.16b pmull v4.1q, v4.1d, v17.1d // GHASH block 4k+1 - mid ROUND CTR0.16b, KEY7.16b ROUND CTR3.16b, KEY5.16b ins v8.d[1], v8.d[0] // GHASH[2] - mid ROUND CTR1.16b, KEY5.16b ROUND CTR0.16b, KEY8.16b ROUND CTR2.16b, KEY5.16b ROUND CTR1.16b, KEY6.16b eor v10.16b, v10.16b, v4.16b // GHASH block 4k+1 - mid pmull2 v4.1q, v6.2d, HASH2.2d // GHASH[2] - high pmull v5.1q, v6.1d, HASH2.1d // GHASH[2] - low ROUND CTR1.16b, KEY7.16b pmull v6.1q, v7.1d, HASH1.1d // GHASH[0] - low eor v9.16b, v9.16b, v4.16b // GHASH[2] - high ROUND CTR3.16b, KEY6.16b ldp x21, x22, [INPUT], #16 // AES[2] - load plaintext ROUND CTR1.16b, KEY8.16b mov d4, v7.d[1] // GHASH[0] - mid #ifdef HITLS_BIG_ENDIAN rev x21, x21 rev x22, x22 #endif ROUND CTR2.16b, KEY6.16b eor HASH0.16b, HASH0.16b, v5.16b // GHASH[2] - low pmull2 v8.1q, v8.2d, v16.2d // GHASH[2] - mid pmull2 v5.1q, v7.2d, HASH1.2d // GHASH[0] - high eor v4.8b, v4.8b, v7.8b // GHASH[0] - mid ROUND CTR2.16b, KEY7.16b eor x19, x19, KEND0 // AES[1] - round 14 low ROUND CTR1.16b, KEY9.16b eor v10.16b, v10.16b, v8.16b // GHASH[2] - mid ROUND CTR3.16b, KEY7.16b eor x21, x21, KEND0 // AES[2] - round 14 low ROUND CTR0.16b, KEY9.16b movi v8.8b, #0xc2 pmull v4.1q, v4.1d, v16.1d // GHASH[0] - mid eor v9.16b, v9.16b, v5.16b // GHASH[0] - high fmov d5, x19 // AES[1] - mov low ROUND CTR2.16b, KEY8.16b ldp x23, x24, [INPUT], #16 // AES[3] - load plaintext ROUND CTR0.16b, KEY10.16b shl d8, d8, #56 // mod_constant #ifdef HITLS_BIG_ENDIAN rev x23, x23 rev x24, x24 #endif ROUND CTR3.16b, KEY8.16b eor HASH0.16b, HASH0.16b, v6.16b // GHASH[0] - low ROUND CTR2.16b, KEY9.16b ROUND CTR1.16b, KEY10.16b eor v10.16b, v10.16b, v4.16b // GHASH[0] - mid ROUND CTR3.16b, KEY9.16b add IV_W, IV_W, #1 // CTR++ ROUND CTR0.16b, KEY11.16b eor v4.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up ROUND CTR1.16b, KEY11.16b pmull v7.1q, v9.1d, v8.1d // MODULO - top 64b align with mid rev w9, IV_W // CTR block 4k+8 ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment ROUND CTR2.16b, KEY10.16b eor x23, x23, KEND0 // AES[3] - round 14 low ROUND CTR1.16b, KEY12.16b eor v10.16b, v10.16b, v4.16b // MODULO - karatsuba tidy up ROUND CTR3.16b, KEY10.16b eor x20, x20, KEND1 // AES[1] - round 14 high fmov d4, x6 // AES[0] - mov low orr x9, x11, x9, lsl #32 // CTR block 4k+8 eor v7.16b, v9.16b, v7.16b // MODULO - fold into mid ROUND CTR0.16b, KEY12.16b eor x22, x22, KEND1 // AES[2] - round 14 high ROUND CTR2.16b, KEY11.16b eor x24, x24, KEND1 // AES[3] - round 14 high ROUND CTR3.16b, KEY11.16b add IV_W, IV_W, #1 // CTR++ aese CTR0.16b, KEY13.16b // AES[0] - round 13 fmov OUT0.d[1], x7 // AES[0] - mov high eor v10.16b, v10.16b, v7.16b // MODULO - fold into mid ROUND CTR2.16b, KEY12.16b fmov d7, x23 // AES[3] - mov low aese CTR1.16b, KEY13.16b // AES[2] - round 13 fmov OUT1.d[1], x20 // AES[1] - mov high fmov d6, x21 // AES[2] - mov low subs COUNT, COUNT, #1 // COUNT-- fmov OUT2.d[1], x22 // AES[2] - mov high pmull v9.1q, v10.1d, v8.1d // MODULO - mid 64b align with low eor OUT0.16b, OUT0.16b, CTR0.16b // AES[0] - result fmov d0, x10 // CTR[0] fmov CTR0.d[1], x9 // CTR[0]--OK rev w9, IV_W // CTR[1] add IV_W, IV_W, #1 // CTR++ eor OUT1.16b, OUT1.16b, CTR1.16b // AES[1] - result fmov d1, x10 // CTR[1] orr x9, x11, x9, lsl #32 // CTR[1] ROUND CTR3.16b, KEY12.16b fmov v1.d[1], x9 // CTR[1]--OK aese CTR2.16b, KEY13.16b // AES[3] - round 13 rev w9, IV_W // CTR block 4k+10 st1 {OUT0.16b}, [OUT00], #16 // AES[0] - store result orr x9, x11, x9, lsl #32 // CTR block 4k+10 eor HASH0.16b, HASH0.16b, v9.16b // MODULO - fold into low fmov OUT3.d[1], x24 // AES[3] - mov high ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment st1 {OUT1.16b}, [OUT00], #16 // AES[1] - store result add IV_W, IV_W, #1 // CTR++ aese CTR3.16b, KEY13.16b // AES[0] - round 13 eor OUT2.16b, OUT2.16b, CTR2.16b // AES[2] - result fmov d2, x10 // CTR[2] st1 {OUT2.16b}, [OUT00], #16 // AES[2] - store result fmov v2.d[1], x9 // CTR[2]--OK rev w9, IV_W // CTR block 4k+11 eor OUT3.16b, OUT3.16b, CTR3.16b // AES[3] - result eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low orr x9, x11, x9, lsl #32 // CTR block 4k+11 st1 {OUT3.16b}, [OUT00], #16 // AES[3] - store result .endm .macro GCM_DEC256_LOOP mov x21, CTR2.d[0] // AES[2] block - mov low ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 eor CTR3.16b, OUT3.16b, CTR3.16b // AES[3] block - result ROUND CTR0.16b, KEY0.16b mov x22, CTR2.d[1] // AES[2] block - mov high ROUND CTR1.16b, KEY0.16b fmov d2, x10 // CTR[2] fmov v2.d[1], x9 // CTR[2] eor v4.16b, v4.16b, HASH0.16b // PRE 1 #ifdef HITLS_BIG_ENDIAN rev x21, x21 rev x22, x22 #endif rev w9, IV_W // CTR[0] ROUND CTR0.16b, KEY1.16b mov x24, CTR3.d[1] // AES[3] block - mov high ROUND CTR1.16b, KEY1.16b mov x23, CTR3.d[0] // AES[3] block - mov low pmull2 v9.1q, v4.2d, HASH4.2d // GHASH block 4k - high mov d8, v4.d[1] // GHASH block 4k - mid fmov d3, x10 // CTR[0] #ifdef HITLS_BIG_ENDIAN rev x23, x23 rev x24, x24 #endif ROUND CTR0.16b, KEY2.16b orr x9, x11, x9, lsl #32 // CTR[0] ROUND CTR2.16b, KEY0.16b fmov v3.d[1], x9 // CTR[0] ROUND CTR1.16b, KEY2.16b eor v8.8b, v8.8b, v4.8b // GHASH block 4k - mid ROUND CTR0.16b, KEY3.16b eor x22, x22, KEND1 // AES[2] - round 14 high ROUND CTR2.16b, KEY1.16b mov d10, v17.d[1] // GHASH block 4k - mid ROUND CTR1.16b, KEY3.16b rev64 v6.16b, v6.16b // GHASH[2] ROUND CTR3.16b, KEY0.16b eor x21, x21, KEND0 // AES[2] - round 14 low ROUND CTR2.16b, KEY2.16b stp x21, x22, [OUT00], #16 // AES[2] - store result pmull HASH0.1q, v4.1d, HASH4.1d // GHASH block 4k - low pmull2 v4.1q, v5.2d, HASH3.2d // GHASH block 4k+1 - high ROUND CTR2.16b, KEY3.16b rev64 v7.16b, v7.16b // GHASH[0] pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid eor x23, x23, KEND0 // AES[3] - round 14 low pmull v8.1q, v5.1d, HASH3.1d // GHASH block 4k+1 - low eor x24, x24, KEND1 // AES[3] - round 14 high eor v9.16b, v9.16b, v4.16b // GHASH block 4k+1 - high ROUND CTR2.16b, KEY4.16b ROUND CTR3.16b, KEY1.16b mov d4, v5.d[1] // GHASH block 4k+1 - mid ROUND CTR0.16b, KEY4.16b eor HASH0.16b, HASH0.16b, v8.16b // GHASH block 4k+1 - low ROUND CTR2.16b, KEY5.16b add IV_W, IV_W, #1 // CTR[0] ROUND CTR3.16b, KEY2.16b mov d8, v6.d[1] // GHASH[2] - mid ROUND CTR1.16b, KEY4.16b eor v4.8b, v4.8b, v5.8b // GHASH block 4k+1 - mid pmull v5.1q, v6.1d, HASH2.1d // GHASH[2] - low ROUND CTR3.16b, KEY3.16b eor v8.8b, v8.8b, v6.8b // GHASH[2] - mid ROUND CTR1.16b, KEY5.16b ROUND CTR0.16b, KEY5.16b eor HASH0.16b, HASH0.16b, v5.16b // GHASH[2] - low pmull v4.1q, v4.1d, v17.1d // GHASH block 4k+1 - mid rev w9, IV_W // CTR block 4k+8 ROUND CTR1.16b, KEY6.16b ins v8.d[1], v8.d[0] // GHASH[2] - mid ROUND CTR0.16b, KEY6.16b add IV_W, IV_W, #1 // CTR block 4k+8 ROUND CTR3.16b, KEY4.16b ROUND CTR1.16b, KEY7.16b eor v10.16b, v10.16b, v4.16b // GHASH block 4k+1 - mid ROUND CTR0.16b, KEY7.16b pmull2 v4.1q, v6.2d, HASH2.2d // GHASH[2] - high mov d6, v7.d[1] // GHASH[0] - mid ROUND CTR3.16b, KEY5.16b pmull2 v8.1q, v8.2d, v16.2d // GHASH[2] - mid ROUND CTR0.16b, KEY8.16b eor v9.16b, v9.16b, v4.16b // GHASH[2] - high ROUND CTR3.16b, KEY6.16b pmull v4.1q, v7.1d, HASH1.1d // GHASH[0] - low orr x9, x11, x9, lsl #32 // CTR block 4k+8 eor v10.16b, v10.16b, v8.16b // GHASH[2] - mid pmull2 v5.1q, v7.2d, HASH1.2d // GHASH[0] - high ROUND CTR0.16b, KEY9.16b eor v6.8b, v6.8b, v7.8b // GHASH[0] - mid ROUND CTR1.16b, KEY8.16b ROUND CTR2.16b, KEY6.16b eor v9.16b, v9.16b, v5.16b // GHASH[0] - high ROUND CTR0.16b, KEY10.16b pmull v6.1q, v6.1d, v16.1d // GHASH[0] - mid movi v8.8b, #0xc2 ROUND CTR2.16b, KEY7.16b eor HASH0.16b, HASH0.16b, v4.16b // GHASH[0] - low ROUND CTR0.16b, KEY11.16b ROUND CTR3.16b, KEY7.16b shl d8, d8, #56 // mod_constant ROUND CTR2.16b, KEY8.16b eor v10.16b, v10.16b, v6.16b // GHASH[0] - mid ROUND CTR0.16b, KEY12.16b pmull v7.1q, v9.1d, v8.1d // MODULO - top 64b align with mid eor v6.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up ROUND CTR1.16b, KEY9.16b ld1 {OUT0.16b}, [INPUT], #16 // AES load[0] ciphertext aese CTR0.16b, KEY13.16b ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment ROUND CTR1.16b, KEY10.16b eor v10.16b, v10.16b, v6.16b // MODULO - karatsuba tidy up ROUND CTR2.16b, KEY9.16b ld1 {OUT1.16b}, [INPUT], #16 // AES load[1] ciphertext ROUND CTR3.16b, KEY8.16b eor CTR0.16b, OUT0.16b, CTR0.16b // AES[0] block - result ROUND CTR1.16b, KEY11.16b stp x23, x24, [OUT00], #16 // AES[3] block - store result ROUND CTR2.16b, KEY10.16b eor v10.16b, v10.16b, v7.16b // MODULO - fold into mid ROUND CTR3.16b, KEY9.16b ld1 {OUT2.16b}, [INPUT], #16 // AES load[1] ciphertext ROUND CTR1.16b, KEY12.16b ld1 {OUT3.16b}, [INPUT], #16 // AES load[1] ciphertext ROUND CTR2.16b, KEY11.16b mov x7, CTR0.d[1] // AES[0] block - mov high ROUND CTR3.16b, KEY10.16b eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid aese CTR1.16b, KEY13.16b // AES[2] - round 13 mov x6, CTR0.d[0] // AES[0] block - mov low ROUND CTR2.16b, KEY12.16b fmov d0, x10 // CTR[0] ROUND CTR3.16b, KEY11.16b #ifdef HITLS_BIG_ENDIAN rev x6, x6 rev x7, x7 #endif fmov CTR0.d[1], x9 // CTR[0]--OK pmull v8.1q, v10.1d, v8.1d // MODULO - mid 64b align with low eor CTR1.16b, OUT1.16b, CTR1.16b // AES[1] block - result rev w9, IV_W // CTR block 4k+9 aese CTR2.16b, KEY13.16b orr x9, x11, x9, lsl #32 // CTR block 4k+9 subs COUNT, COUNT, #1 // COUNT-- add IV_W, IV_W, #1 // CTR++ eor x6, x6, KEND0 // AES[0] block - round 14 low eor x7, x7, KEND1 // AES[0] block - round 14 high mov x20, v1.d[1] // AES[1] block - mov high eor CTR2.16b, OUT2.16b, CTR2.16b // AES[2] block - result eor HASH0.16b, HASH0.16b, v8.16b // MODULO - fold into low ROUND CTR3.16b, KEY12.16b mov x19, CTR1.d[0] // AES[1] block - mov low fmov d1, x10 // CTR[1] ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment #ifdef HITLS_BIG_ENDIAN rev x20, x20 rev x19, x19 #endif fmov CTR1.d[1], x9 // CTR[1]--OK rev w9, IV_W // CTR block 4k+10 add IV_W, IV_W, #1 // CTR++ aese CTR3.16b, KEY13.16b orr x9, x11, x9, lsl #32 // CTR block 4k+10 rev64 v5.16b, v5.16b // GHASH[2] eor x20, x20, KEND1 // AES[1] block - round 14 high stp x6, x7, [OUT00], #16 // AES[0] block - store result eor x19, x19, KEND0 // AES[1] block - round 14 low stp x19, x20, [OUT00], #16 // AES[1] block - store result rev64 OUT0.16b, OUT0.16b // GHASH block[0] eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low .endm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes256_gcm_aarch64.S
Unix Assembly
unknown
18,230
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CCM) .text .balign 16 g_byteSwapMask: .byte 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08 .byte 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 .size g_byteSwapMask, .-g_byteSwapMask .balign 16 g_one: .byte 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .size g_one, .-g_one /* * void AesCcmEncryptAsm(void *key, uint8_t *nonce, const uint8_t *in, uint8_t *out, uint32_t len) * rdi *key * rsi *nonce * rdx *in * rcx *out * r8 len */ .globl AesCcmEncryptAsm .type AesCcmEncryptAsm, @function .balign 16 AesCcmEncryptAsm: .cfi_startproc shr $4, %r8d // loop times jz .Lenc_ret lea g_byteSwapMask(%rip), %r11 mov 0xf0(%rdi), %r9d // key->rounds vmovdqa (%r11), %xmm15 // g_byteSwapMask sub $1, %r9d vmovdqa 0x10(%r11), %xmm14 // g_one vmovdqu (%rsi), %xmm0 // nonce(counter) vmovdqu 0x10(%rsi), %xmm8 // tag vmovdqu 0x20(%rsi), %xmm9 // last .balign 16 .Lenc_outer_loop: mov %r9d, %r10d vpxor (%rdx), %xmm8, %xmm8 // in ^ tag = tag vmovdqu (%rdi), %xmm1 // key0 lea 0x10(%rdi), %r11 // &key + 1 vpxor %xmm0, %xmm1, %xmm2 // first round xor(aes-ctr) vpshufb %xmm15, %xmm0, %xmm0 // reverse byte order of nonce => nonce' vpxor %xmm8, %xmm1, %xmm3 // first round xor(aes-cmac) .balign 16 .Lenc_aes_loop: vmovdqu (%r11), %xmm1 vaesenc %xmm1, %xmm2, %xmm2 vaesenc %xmm1, %xmm3, %xmm3 lea 0x10(%r11), %r11 // to next key ptr dec %r10d jnz .Lenc_aes_loop vmovdqu (%r11), %xmm1 // get the last key vpaddq %xmm14, %xmm0, %xmm0 // nonce' + 1 vaesenclast %xmm1, %xmm2, %xmm9 vaesenclast %xmm1, %xmm3, %xmm8 vpxor (%rdx), %xmm9, %xmm2 // in ^ last = out vpshufb %xmm15, %xmm0, %xmm0 // reverse byte order of nonce' => nonce lea 0x10(%rdx), %rdx // go to next ptr vmovdqu %xmm2, (%rcx) // out out lea 0x10(%rcx), %rcx // go to next ptr dec %r8d jnz .Lenc_outer_loop vpxor %xmm1, %xmm1, %xmm1 vpxor %xmm2, %xmm2, %xmm2 vpxor %xmm3, %xmm3, %xmm3 vmovdqu %xmm0, (%rsi) // out nonce vpxor %xmm0, %xmm0, %xmm0 vmovdqu %xmm8, 0x10(%rsi) // out tag vpxor %xmm8, %xmm8, %xmm8 vmovdqu %xmm9, 0x20(%rsi) // out last vpxor %xmm9, %xmm9, %xmm9 .Lenc_ret: ret .cfi_endproc .size AesCcmEncryptAsm, .-AesCcmEncryptAsm /* * void AesCcmDecryptAsm(void *key, uint8_t *nonce, const uint8_t *in, uint8_t *out, uint32_t len) * rdi *key * rsi *nonce * rdx *in * rcx *out * r8 len */ .globl AesCcmDecryptAsm .type AesCcmDecryptAsm, @function .balign 16 AesCcmDecryptAsm: .cfi_startproc shr $4, %r8d // loop times jz .Ldec_ret lea g_byteSwapMask(%rip), %r11 mov 0xf0(%rdi), %r9d // key->rounds vmovdqa (%r11), %xmm15 // g_byteSwapMask sub $1, %r9d vmovdqa 0x10(%r11), %xmm14 // g_one vmovdqu (%rsi), %xmm0 // nonce(counter) vmovdqu 0x10(%rsi), %xmm8 // tag .balign 16 .Ldec_outer_loop: mov %r9d, %r10d lea 0x10(%rdi), %r11 // &key vmovdqu (%rdi), %xmm1 // key0 vpxor %xmm0, %xmm1, %xmm2 // first round xor(aes-ctr) .Ldec_aes_loop: vmovdqu (%r11), %xmm1 vaesenc %xmm1, %xmm2, %xmm2 lea 0x10(%r11), %r11 dec %r10d jnz .Ldec_aes_loop vmovdqu (%r11), %xmm1 vaesenclast %xmm1, %xmm2, %xmm4 vmovdqu %xmm4, 0x20(%rsi) // out last vpxor (%rdx), %xmm4, %xmm2 // in ^ last = out vpxor %xmm2, %xmm8, %xmm8 // out ^ tag = tag vmovdqu %xmm2, (%rcx) // out out lea 0x10(%rdx), %rdx lea 0x10(%rcx), %rcx vpshufb %xmm15, %xmm0, %xmm0 // reverse byte order of nonce => nonce' vpaddq %xmm14, %xmm0, %xmm0 // nonce' + 1 vpshufb %xmm15, %xmm0, %xmm0 // reverse byte order of nonce' => nonce cmp $2, %r8d jb .Ldec_parallel_out .Ldec_parallel_loop: mov %r9d, %r10d lea 0x10(%rdi), %r11 // &key vmovdqu (%rdi), %xmm1 // key0 vpxor %xmm0, %xmm1, %xmm2 // first round xor(aes-ctr) vpxor %xmm8, %xmm1, %xmm3 // first round xor(aes-cmac) .Ldec_parallel_inner_loop: vmovdqu (%r11), %xmm1 vaesenc %xmm1, %xmm2, %xmm2 lea 0x10(%r11), %r11 vaesenc %xmm1, %xmm3, %xmm3 dec %r10d jnz .Ldec_parallel_inner_loop vmovdqu (%r11), %xmm1 vaesenclast %xmm1, %xmm2, %xmm4 vaesenclast %xmm1, %xmm3, %xmm8 vmovdqu %xmm4, 0x20(%rsi) // out last vpxor (%rdx), %xmm4, %xmm2 // in ^ last = out vpxor %xmm2, %xmm8, %xmm8 // out ^ tag = tag vmovdqu %xmm2, (%rcx) // out out lea 0x10(%rdx), %rdx lea 0x10(%rcx), %rcx vpshufb %xmm15, %xmm0, %xmm0 // reverse byte order of nonce => nonce' vpaddq %xmm14, %xmm0, %xmm0 // nonce' + 1 vpshufb %xmm15, %xmm0, %xmm0 // reverse byte order of nonce' => nonce dec %r8d cmp $2, %r8d jae .Ldec_parallel_loop .Ldec_parallel_out: mov %r9d, %r10d lea 0x10(%rdi), %r11 // &key vmovdqu (%rdi), %xmm1 // key0 vpxor %xmm8, %xmm1, %xmm3 // first round xor(aes-cmac) .Ldec_aes_loop_1: vmovdqu (%r11), %xmm1 vaesenc %xmm1, %xmm3, %xmm3 lea 0x10(%r11), %r11 dec %r10d jnz .Ldec_aes_loop_1 vmovdqu (%r11), %xmm1 vaesenclast %xmm1, %xmm3, %xmm8 dec %r8d jnz .Ldec_outer_loop vmovdqu %xmm0, (%rsi) // out nonce vpxor %xmm0, %xmm0, %xmm0 vpxor %xmm1, %xmm1, %xmm1 vpxor %xmm2, %xmm2, %xmm2 vmovdqu %xmm8, 0x10(%rsi) // out tag vpxor %xmm8, %xmm8, %xmm8 vpxor %xmm3, %xmm3, %xmm3 vpxor %xmm4, %xmm4, %xmm4 .Ldec_ret: ret .cfi_endproc .size AesCcmDecryptAsm, .-AesCcmDecryptAsm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes_ccm_x86_64.S
Unix Assembly
unknown
6,559
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) #include "crypt_arm.h" #include "aes_gcm_common_aarch64.S" .text .arch armv8-a+crypto .globl AES_GCM_Encrypt16BlockAsm .type AES_GCM_Encrypt16BlockAsm,%function .align 4 AES_GCM_Encrypt16BlockAsm: AARCH64_PACIASP IN_STP // register Protection ldr ROUNDS, [KEY00, #240] // load number of rounds add HTABLE, IVEC0, #16 // obtains the start address of the ghash table lsr COUNT, INLEN, #4 // length divided by 16 LOAD_GHASH_TABLE // load the ghash table cmp ROUNDS, #10 // number of comparison rounds 10 LOAD_KEY // load AES KEY b.eq .LEnc16_128_process // go to the AES128 part cmp ROUNDS, #12 // number of comparison rounds 12 ld1 {KEY10.4s, KEY11.4s}, [KEY00], #32 b.eq .LEnc16_192_process // go to the AES192 part ld1 {KEY12.4s, KEY13.4s}, [KEY00], #32 b .LEnc16_256_process // go to the AES256 part .LEnc16_128_process: BEFORE16_ROUND // data preprocessing .LEnc16_128_loop: FIRST16_ROUND // data preprocessing ldp x6, x7, [INPUT], #16 // load INPUT 0 #ifdef HITLS_BIG_ENDIAN REV_2S x6, x7 #endif aese CTR0.16b, KEY9.16b subs COUNT, COUNT, #1 // COUNT-- ENC16_BLOCK // processes 16-byte data b.le .LEnc16_end b .LEnc16_128_loop .LEnc16_192_process: BEFORE16_ROUND .LEnc16_192_loop: FIRST16_ROUND ldp x6, x7, [INPUT], #16 // load INPUT 0 #ifdef HITLS_BIG_ENDIAN REV_2S x6, x7 #endif ROUND CTR0.16b, KEY9.16b ROUND CTR0.16b, KEY10.16b aese CTR0.16b, KEY11.16b subs COUNT, COUNT, #1 // COUNT-- ENC16_BLOCK b.le .LEnc16_end b .LEnc16_192_loop .LEnc16_256_process: BEFORE16_ROUND .LEnc16_256_loop: FIRST16_ROUND ldp x6, x7, [INPUT], #16 // load INPUT 0 #ifdef HITLS_BIG_ENDIAN REV_2S x6, x7 #endif ROUND CTR0.16b, KEY9.16b ROUND CTR0.16b, KEY10.16b ROUND CTR0.16b, KEY11.16b ROUND CTR0.16b, KEY12.16b aese CTR0.16b, KEY13.16b subs COUNT, COUNT, #1 // COUNT-- ENC16_BLOCK b.le .LEnc16_end b .LEnc16_256_loop .LEnc16_end: ext HASH0.16b, HASH0.16b, HASH0.16b, #8 add x6, IVEC0, #16 rev64 HASH0.16b, HASH0.16b st1 {CTR1.16b }, [IVEC0] // out counter st1 {HASH0.16b }, [x6] // out hash OUT_STP // restore protection register .LEnc_ret: mov w0, #0x0 AARCH64_AUTIASP ret .size AES_GCM_Encrypt16BlockAsm,.-AES_GCM_Encrypt16BlockAsm .globl AES_GCM_Decrypt16BlockAsm .type AES_GCM_Decrypt16BlockAsm,%function .align 4 AES_GCM_Decrypt16BlockAsm: AARCH64_PACIASP IN_STP // stp ldr ROUNDS, [KEY00, #240] // pull rounds mov IVEC0, x0 // ctr0 add HTABLE, IVEC0, #16 // htable lsr COUNT, INLEN, #4 // 2*2 2*2 = 16 LOAD_GHASH_TABLE cmp ROUNDS, #10 LOAD_KEY b.eq .LDec16_128_process cmp ROUNDS, #12 ld1 {KEY10.4s, KEY11.4s}, [KEY00], #32 b.eq .LDec16_192_process ld1 {KEY12.4s, KEY13.4s}, [KEY00], #32 b .LDec16_256_process .LDec16_128_process: BEFORE16_ROUND #ifdef HITLS_BIG_ENDIAN REV_2S KEND0, KEND1 #endif .LDec16_128_loop: FIRST16_ROUND aese CTR0.16b, KEY9.16b DEC16_BLOCK b.le .LDec16_end b .LDec16_128_loop .LDec16_192_process: BEFORE16_ROUND #ifdef HITLS_BIG_ENDIAN REV_2S KEND0, KEND1 #endif .LDec16_192_loop: FIRST16_ROUND ROUND CTR0.16b, KEY9.16b ROUND CTR0.16b, KEY10.16b aese CTR0.16b, KEY11.16b DEC16_BLOCK b.le .LDec16_end b .LDec16_192_loop .LDec16_256_process: BEFORE16_ROUND #ifdef HITLS_BIG_ENDIAN REV_2S KEND0, KEND1 #endif .LDec16_256_loop: FIRST16_ROUND ROUND CTR0.16b, KEY9.16b ROUND CTR0.16b, KEY10.16b ROUND CTR0.16b, KEY11.16b ROUND CTR0.16b, KEY12.16b aese CTR0.16b, KEY13.16b DEC16_BLOCK b.le .LDec16_end b .LDec16_256_loop .LDec16_end: ext HASH0.16b, HASH0.16b, HASH0.16b, #8 add x6, IVEC0, #16 rev64 HASH0.16b, HASH0.16b st1 {CTR1.16b }, [IVEC0] // out counter st1 {HASH0.16b }, [x6] // out hash OUT_STP .LDec_ret: mov w0, #0x0 AARCH64_AUTIASP ret .size AES_GCM_Decrypt16BlockAsm,.-AES_GCM_Decrypt16BlockAsm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes_gcm_16block_aarch64.S
Unix Assembly
unknown
5,393
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) .text .balign 16 g_byteSwapMask: .byte 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08 .byte 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 .size g_byteSwapMask, .-g_byteSwapMask .balign 16 g_poly: .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2 .size g_poly, .-g_poly /* * void AES_GCM_Encrypt16BlockAsm(MODES_GCM_Ctx *ctx, const uint8_t *in, * uint8_t *out, uint32_t len, void *key); * ctx %rdi * in %rsi * out %rdx * len %rcx * key %r8 */ .globl AES_GCM_Encrypt16BlockAsm .type AES_GCM_Encrypt16BlockAsm, @function .align 32 AES_GCM_Encrypt16BlockAsm: .cfi_startproc leaq g_byteSwapMask(%rip), %r11 shrl $4, %ecx // blocks number = loop times mov 240(%r8), %r9d // rounds .Lenc_loop: mov 12(%rdi), %eax // counter eax(32bit) addl $0x1000000, %eax // ctr inc vmovdqu (%rdi), %xmm0 // iv jc .Lenc_ctr_carry jmp .Lenc_aes_cipher .Lenc_ctr_carry: bswap %eax addl $0x100, %eax // add carry bit bswap %eax jmp .Lenc_aes_cipher .Lenc_aes_cipher: mov %eax, 12(%rdi) // out iv vmovdqu (%r8), %xmm1 // key0 vpxor %xmm1, %xmm0, %xmm0 vmovdqu 0x10(%r8), %xmm2 // key1 lea 0xa0(%r8), %r10 // point to the last key in 128-bit encryption vmovdqu 0x20(%r8), %xmm3 // key2 vaesenc %xmm2, %xmm0, %xmm0 vmovdqu 0x30(%r8), %xmm4 // key3 vaesenc %xmm3, %xmm0, %xmm0 vmovdqu 0x40(%r8), %xmm5 // key4 vaesenc %xmm4, %xmm0, %xmm0 vmovdqu 0x50(%r8), %xmm6 // key5 vaesenc %xmm5, %xmm0, %xmm0 vmovdqu 0x60(%r8), %xmm7 // key6 vaesenc %xmm6, %xmm0, %xmm0 vmovdqu 0x70(%r8), %xmm8 // key7 vaesenc %xmm7, %xmm0, %xmm0 vmovdqu 0x80(%r8), %xmm9 // key8 vaesenc %xmm8, %xmm0, %xmm0 vmovdqu 0x90(%r8), %xmm10 // key9 vaesenc %xmm9, %xmm0, %xmm0 vaesenc %xmm10, %xmm0, %xmm0 cmp $12, %r9d // compare the number of rounds to determine // when to jump to the next processing part jb .Lenc_aes_end vmovdqu (%r10), %xmm1 // key10 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x10(%r10), %xmm2 // key11 vaesenc %xmm2, %xmm0, %xmm0 lea 0x20(%r10), %r10 je .Lenc_aes_end vmovdqu (%r10), %xmm1 // key12 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x10(%r10), %xmm2 // key13 vaesenc %xmm2, %xmm0, %xmm0 lea 0x20(%r10), %r10 jmp .Lenc_aes_end .Lenc_aes_end: vmovdqu (%r10), %xmm1 // key last vpxor (%rsi), %xmm1, %xmm1 // Advance ciphertext XOR in vaesenclast %xmm1, %xmm0, %xmm0 vmovdqu %xmm0, (%rdx) // out vmovdqu 16(%rdi), %xmm1 // ghash vmovdqa (%r11), %xmm15 // .LByte_Swap_Mask vpxor %xmm1, %xmm0, %xmm0 // input for ghash operation vmovdqu 32(%rdi), %xmm1 // Hash key H^1 vpshufb %xmm15, %xmm0, %xmm0 // data transform vmovdqu 32+32(%rdi), %xmm2 // Hash key H^1_2 vpalignr $8, %xmm0, %xmm0, %xmm3 // data transform vpclmulqdq $0x11, %xmm1, %xmm0, %xmm5 // Karatsuba Multiply vpxor %xmm0, %xmm3, %xmm3 vpclmulqdq $0x00, %xmm1, %xmm0, %xmm0 vpxor %xmm0, %xmm5, %xmm1 vpclmulqdq $0x00, %xmm2, %xmm3, %xmm3 vpxor %xmm1, %xmm3, %xmm3 vpslldq $8, %xmm3, %xmm4 vpsrldq $8, %xmm3, %xmm3 vpxor %xmm4, %xmm0, %xmm0 vpxor %xmm3, %xmm5, %xmm5 vmovdqa 0x10(%r11), %xmm14 // g_poly vpalignr $8, %xmm0, %xmm0, %xmm2 // 1st phase of reduction vpclmulqdq $0x10, %xmm14, %xmm0, %xmm0 vpxor %xmm2, %xmm0, %xmm0 vpalignr $8, %xmm0, %xmm0, %xmm2 // 2nd phase of reduction vpclmulqdq $0x10, %xmm14, %xmm0, %xmm0 vpxor %xmm5, %xmm2, %xmm2 vpxor %xmm2, %xmm0, %xmm0 vpshufb %xmm15, %xmm0, %xmm0 lea 0x10(%rsi), %rsi vmovdqu %xmm0, 16(%rdi) // out lea 0x10(%rdx), %rdx dec %ecx jnz .Lenc_loop ret .cfi_endproc .size AES_GCM_Encrypt16BlockAsm, .-AES_GCM_Encrypt16BlockAsm /* * void AES_GCM_Decrypt16BlockAsm(MODES_GCM_Ctx *ctx, const uint8_t *in, * uint8_t *out, uint32_t len, void *key); * ctx %rdi * in %rsi * out %rdx * len %rcx * key %r8 */ .globl AES_GCM_Decrypt16BlockAsm .type AES_GCM_Decrypt16BlockAsm, @function .balign 32 AES_GCM_Decrypt16BlockAsm: .cfi_startproc leaq g_byteSwapMask(%rip), %r11 vmovdqu 16(%rdi), %xmm10 // ghash shrl $4, %ecx // blocks number = loop times vmovdqa (%r11), %xmm15 // g_byteSwapMask .Ldec_loop: mov 12(%rdi), %eax // counter eax(32bit) addl $0x1000000, %eax // ctr inc mov 240(%r8), %r9d // rounds vmovdqu (%rdi), %xmm0 // iv jc .Ldec_ctr_carry jmp .Ldec_aes_cipher .Ldec_ctr_carry: bswap %eax addl $0x100, %eax // add carry bit bswap %eax jmp .Ldec_aes_cipher .balign 32 .Ldec_aes_cipher: mov %eax, 12(%rdi) // out iv cmp $12, %r9d // Compare the number of rounds to determine // when to jump to the next processing part vmovdqu (%r8), %xmm1 // key 0 vpxor (%rsi), %xmm10, %xmm10 // input for ghash operation lea 0xa0(%r8), %r10 // Point to the last key in 128-bit encryption vpxor %xmm1, %xmm0, %xmm0 vmovdqu 0x10(%r8), %xmm1 // key 1 vmovdqu 32(%rdi), %xmm11 // Hash key H^1 vmovdqu 32+32(%rdi), %xmm12 // Hash key H^1_2 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x20(%r8), %xmm1 // key 2 vpshufb %xmm15, %xmm10, %xmm10 // data transform vpshufd $0x4e, %xmm10, %xmm13 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x30(%r8), %xmm1 // key 3 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14// Karatsuba Multiply vpxor %xmm10, %xmm13, %xmm13 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x40(%r8), %xmm1 // key 4 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vpxor %xmm10, %xmm14, %xmm11 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x50(%r8), %xmm1 // key 5 vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 vpxor %xmm11, %xmm13, %xmm13 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x60(%r8), %xmm1 // key 6 vpslldq $8, %xmm13, %xmm11 vpsrldq $8, %xmm13, %xmm13 vpxor %xmm11, %xmm10, %xmm10 vpxor %xmm13, %xmm14, %xmm14 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x70(%r8), %xmm1 // key 7 vmovdqa 0x10(%r11), %xmm13 // g_poly vpalignr $8, %xmm10, %xmm10, %xmm12 // 1st phase of reduction vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x80(%r8), %xmm1 // key 8 vpclmulqdq $0x10, %xmm13, %xmm10, %xmm10 vpxor %xmm12, %xmm10, %xmm10 vaesenc %xmm1, %xmm0, %xmm0 vmovdqu 0x90(%r8), %xmm1 // key 9 vpalignr $8, %xmm10, %xmm10, %xmm12 // 2nd phase of reduction vpclmulqdq $0x10, %xmm13, %xmm10, %xmm10 vaesenc %xmm1, %xmm0, %xmm0 vpxor %xmm14, %xmm12, %xmm12 jb .Ldec_ending vmovdqu (%r10), %xmm1 // key 10 vmovdqu 0x10(%r10), %xmm2 // key 11 lea 0x20(%r10), %r10 vaesenc %xmm1, %xmm0, %xmm0 vaesenc %xmm2, %xmm0, %xmm0 je .Ldec_ending vmovdqu (%r10), %xmm1 // key 12 vmovdqu 0x10(%r10), %xmm2 // key 13 lea 0x20(%r10), %r10 vaesenc %xmm1, %xmm0, %xmm0 vaesenc %xmm2, %xmm0, %xmm0 jmp .Ldec_ending .Ldec_ending: vmovdqu (%r10), %xmm1 // key last vpxor %xmm12, %xmm10, %xmm10 vpxor (%rsi), %xmm1, %xmm1 vaesenclast %xmm1, %xmm0, %xmm0 vpshufb %xmm15, %xmm10, %xmm10 vmovdqu %xmm0, (%rdx) // out lea 0x10(%rsi), %rsi lea 0x10(%rdx), %rdx dec %ecx jnz .Ldec_loop vmovdqu %xmm10, 16(%rdi) // out ret .cfi_endproc .size AES_GCM_Decrypt16BlockAsm, .-AES_GCM_Decrypt16BlockAsm .globl AES_GCM_ClearAsm .type AES_GCM_ClearAsm, @function .balign 32 AES_GCM_ClearAsm: .cfi_startproc vpxor %xmm1, %xmm1, %xmm1 vpxor %xmm2, %xmm2, %xmm2 vpxor %xmm3, %xmm3, %xmm3 vpxor %xmm4, %xmm4, %xmm4 vpxor %xmm5, %xmm5, %xmm5 vpxor %xmm6, %xmm6, %xmm6 vpxor %xmm7, %xmm7, %xmm7 vpxor %xmm8, %xmm8, %xmm8 vpxor %xmm9, %xmm9, %xmm9 vpxor %xmm10, %xmm10, %xmm10 vpxor %xmm11, %xmm11, %xmm11 vpxor %xmm12, %xmm12, %xmm12 ret .cfi_endproc .size AES_GCM_ClearAsm, .-AES_GCM_ClearAsm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes_gcm_16block_x86_64.S
Unix Assembly
unknown
9,868
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) #include "crypt_arm.h" #include "aes_gcm_common_aarch64.S" #include "aes128_gcm_aarch64.S" #include "aes192_gcm_aarch64.S" #include "aes256_gcm_aarch64.S" .text .arch armv8-a+crypto .globl AES_GCM_ClearAsm .type AES_GCM_ClearAsm,%function .align 4 AES_GCM_ClearAsm: AARCH64_PACIASP eor KEY0.16b, KEY0.16b, KEY0.16b eor KEY1.16b, KEY1.16b, KEY1.16b eor KEY2.16b, KEY2.16b, KEY2.16b eor KEY3.16b, KEY3.16b, KEY3.16b eor KEY4.16b, KEY4.16b, KEY4.16b eor KEY5.16b, KEY5.16b, KEY5.16b eor KEY6.16b, KEY6.16b, KEY6.16b eor KEY7.16b, KEY7.16b, KEY7.16b eor KEY8.16b, KEY8.16b, KEY8.16b eor KEY9.16b, KEY9.16b, KEY9.16b eor KEY10.16b, KEY10.16b, KEY10.16b AARCH64_AUTIASP ret .size AES_GCM_ClearAsm,.-AES_GCM_ClearAsm .globl AES_GCM_EncryptBlockAsm .type AES_GCM_EncryptBlockAsm,%function .align 4 AES_GCM_EncryptBlockAsm: AARCH64_PACIASP IN_STP // Register Protection ldr ROUNDS, [KEY00, #240] // Number of loading rounds add HTABLE, IVEC0, #16 // Sets the gHash start address. lsr COUNT, INLEN, #6 // Divided by 64, count the number of times cmp ROUNDS, #10 // Number of comparison rounds 10 LOAD_KEY // load AES KEY b.eq .LEnc_128_process // go to AES128 processing part cmp ROUNDS, #12 // Number of comparison rounds 12 ld1 {KEY10.4s, KEY11.4s}, [KEY00], #32 b.eq .LEnc_192_process // go to AES192 processing part ld1 {KEY12.4s, KEY13.4s}, [KEY00], #32 b .LEnc_256_process // go to AES256 processing part .LEnc_128_process: ldp KEND0, KEND1, [KEY00] // load key-10 ldp IV_H, IV_L, [IVEC0] // load IV #ifdef HITLS_BIG_ENDIAN ror KEND0, KEND0, #32 ror KEND1, KEND1, #32 REV_2S IV_H, IV_L #endif lsr IV_C, IV_L, #32 ld1 {CTR0.16b}, [IVEC0] // CTR bolck 0 lsl IVCTR, COUNTW, #2 // <<16 LOAD_GHASH_TABLE // load gHashTable BEFORE_ROUND FIRST_ROUND // data preprocessing ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY7.16b // round 7 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY8.16b // round 8 rev w9, IV_W // CTR0--Start ROUND4_END CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY9.16b // round 9 orr x9, x11, x9, lsl #32 // CTR0 block 4k+8 add IV_W, IV_W, #1 // CTR0++ eor v17.16b, v17.16b, v9.16b // h4k | h3k eor v16.16b, v16.16b, v8.16b // h2k | h1k STORE_RESULT // data preprocessing b.le .LEnc_end // After the first 64-byte processing is complete, // check the remaining length. b .LEnc_128_loop // Enter the cyclic processing flow. .LEnc_192_process: ldp KEND0, KEND1, [KEY00] // load key-10 ldp IV_H, IV_L, [IVEC0] // load IV #ifdef HITLS_BIG_ENDIAN ror KEND0, KEND0, #32 ror KEND1, KEND1, #32 REV_2S IV_H, IV_L #endif lsr IV_C, IV_L, #32 // IV-l ld1 {CTR0.16b}, [IVEC0] // CTR bolck 0 lsl IVCTR, COUNTW, #2 // <<16 LOAD_GHASH_TABLE // load hash table BEFORE_ROUND FIRST_ROUND // aes round ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY7.16b // round 7 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY8.16b // round 8 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY9.16b // round 9 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY10.16b // round 10 rev w9, IV_W // CTR0--Start ROUND4_END CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY11.16b // round 11 orr x9, x11, x9, lsl #32 // CTR0 block 4k+8 add IV_W, IV_W, #1 // CTR0++ eor v17.16b, v17.16b, v9.16b // h4k | h3k eor v16.16b, v16.16b, v8.16b // h2k | h1k STORE_RESULT b.le .LEnc_end b .LEnc_192_loop .LEnc_256_process: ldp KEND0, KEND1, [KEY00] // load key-10 ldp IV_H, IV_L, [IVEC0] // load IV #ifdef HITLS_BIG_ENDIAN ror KEND0, KEND0, #32 ror KEND1, KEND1, #32 REV_2S IV_H, IV_L #endif lsr IV_C, IV_L, #32 ld1 {CTR0.16b}, [IVEC0] // CTR bolck 0 lsl IVCTR, COUNTW, #2 // <<16 LOAD_GHASH_TABLE BEFORE_ROUND FIRST_ROUND ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY7.16b // round 7 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY8.16b // round 8 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY9.16b // round 9 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY10.16b // round 10 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY11.16b // round 11 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY12.16b // round 12 rev w9, IV_W // CTR0--Start ROUND4_END CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY13.16b // round 13 orr x9, x11, x9, lsl #32 // CTR0 block 4k+8 add IV_W, IV_W, #1 // CTR0++ eor v17.16b, v17.16b, v9.16b // h4k | h3k eor v16.16b, v16.16b, v8.16b // h2k | h1k STORE_RESULT b.le .LEnc_end b .LEnc_256_loop .LEnc_128_loop: GCM_ENC128_LOOP // Processes 64 bytes. b.le .LEnc_end // If the number of remaining blocks is 0, exit the loop. b .LEnc_128_loop // Continue the loop .LEnc_192_loop: GCM_ENC192_LOOP b.le .LEnc_end // <= 0 b .LEnc_192_loop .LEnc_256_loop: GCM_ENC256_LOOP b.le .LEnc_end // <= 0 b .LEnc_256_loop .LEnc_end: rev64 OUT0.16b, OUT0.16b // GHASH block 4k (only t0 is free) rev64 OUT1.16b, OUT1.16b // GHASH block 4k+1 (t0 and t1 free) rev64 OUT2.16b, OUT2.16b // GHASH[2] (t0, t1, and t2 free) rev64 OUT3.16b, OUT3.16b // GHASH[0] (t0, t1, t2 and t3 free) GHASH_BLOCK // Ghash calculation and encryption/decryption processing rev w9, IVCTR // CTR[0] ext HASH0.16b, HASH0.16b, HASH0.16b, #8 add x6, IVEC0, #16 orr x9, x11, x9, lsl #32 // CTR[0] fmov d0, x10 // CTR[0] fmov CTR0.d[1], x9 // CTR[0]--OK st1 {CTR0.16b }, [IVEC0] // out hash rev64 HASH0.16b, HASH0.16b st1 {HASH0.16b }, [x6] // out hash OUT_STP .LEnc_ret: and x0, INLEN, #-64 // length of processed data AARCH64_AUTIASP ret .size AES_GCM_EncryptBlockAsm,.-AES_GCM_EncryptBlockAsm .globl AES_GCM_DecryptBlockAsm .type AES_GCM_DecryptBlockAsm,%function .align 4 AES_GCM_DecryptBlockAsm: AARCH64_PACIASP IN_STP // stp ldr ROUNDS, [KEY00, #240] // pull rounds mov IVEC0, x0 // ctr0 add HTABLE, IVEC0, #16 // htable lsr COUNT, INLEN, #6 // divided by 64 cmp ROUNDS, #10 LOAD_KEY b.eq .LDec_128_process cmp ROUNDS, #12 ld1 {KEY10.4s, KEY11.4s}, [KEY00], #32 b.eq .LDec_192_process ld1 {KEY12.4s, KEY13.4s}, [KEY00], #32 b .LDec_256_process .LDec_128_process: ldp KEND0, KEND1, [KEY00] // load key-10 ldp IV_H, IV_L, [IVEC0] // load IV #ifdef HITLS_BIG_ENDIAN ror KEND0, KEND0, #32 ror KEND1, KEND1, #32 REV_2S IV_H, IV_L #endif lsr IV_C, IV_L, #32 ld1 {CTR0.16b}, [IVEC0] // CTR[0] #ifdef HITLS_BIG_ENDIAN REV_2S KEND0, KEND1 #endif lsl IVCTR, COUNTW, #2 // <<16 LOAD_GHASH_TABLE BEFORE_ROUND ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY0.16b // round 0 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY1.16b // round 1 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY2.16b // round 2 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY3.16b // round 3 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY4.16b // round 4 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY5.16b // round 5 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY6.16b // round 6 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY7.16b // round 7 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY8.16b // round 8 ROUND4_END CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY9.16b // round 9 eor v17.16b, v17.16b, v9.16b // h4k | h3k eor v16.16b, v16.16b, v8.16b // h2k | h1k STORE_DEC_RESULT b.le .LDec_end b .LDec_128_loop .LDec_192_process: ldp KEND0, KEND1, [KEY00] // load key-10 ldp IV_H, IV_L, [IVEC0] // load IV #ifdef HITLS_BIG_ENDIAN ror KEND0, KEND0, #32 ror KEND1, KEND1, #32 REV_2S IV_H, IV_L #endif lsr IV_C, IV_L, #32 ld1 {CTR0.16b}, [IVEC0] // CTR[0] #ifdef HITLS_BIG_ENDIAN REV_2S KEND0, KEND1 #endif lsl IVCTR, COUNTW, #2 // <<16 LOAD_GHASH_TABLE BEFORE_ROUND ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY0.16b // round 0 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY1.16b // round 1 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY2.16b // round 2 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY3.16b // round 3 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY4.16b // round 4 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY5.16b // round 5 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY6.16b // round 6 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY7.16b // round 7 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY8.16b // round 8 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY9.16b // round 9 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY10.16b // round 10 ROUND4_END CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY11.16b // round 11 eor v17.16b, v17.16b, v9.16b // h4k | h3k eor v16.16b, v16.16b, v8.16b // h2k | h1k STORE_DEC_RESULT b.le .LDec_end b .LDec_192_loop .LDec_256_process: ldp KEND0, KEND1, [KEY00] // load key-10 ldp IV_H, IV_L, [IVEC0] // load IV #ifdef HITLS_BIG_ENDIAN ror KEND0, KEND0, #32 ror KEND1, KEND1, #32 REV_2S IV_H, IV_L #endif lsr IV_C, IV_L, #32 ld1 {CTR0.16b}, [IVEC0] // CTR[0] #ifdef HITLS_BIG_ENDIAN REV_2S KEND0, KEND1 #endif lsl IVCTR, COUNTW, #2 // <<16 LOAD_GHASH_TABLE BEFORE_ROUND ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY0.16b // round 0 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY1.16b // round 1 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY2.16b // round 2 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY3.16b // round 3 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY4.16b // round 4 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY5.16b // round 5 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY6.16b // round 6 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY7.16b // round 7 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY8.16b // round 8 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY9.16b // round 9 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY10.16b // round 10 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY11.16b // round 11 ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY12.16b // round 12 ROUND4_END CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY13.16b // round 13 eor v17.16b, v17.16b, v9.16b // h4k | h3k eor v16.16b, v16.16b, v8.16b // h2k | h1k STORE_DEC_RESULT b.le .LDec_end b .LDec_256_loop .LDec_128_loop: GCM_DEC128_LOOP b.le .LDec_end // <=0 b .LDec_128_loop .LDec_192_loop: GCM_DEC192_LOOP b.le .LDec_end // <=0 b .LDec_192_loop .LDec_256_loop: GCM_DEC256_LOOP b.le .LDec_end // <=0 b .LDec_256_loop .LDec_end: GHASH_DEC_BLOCK rev w9, IVCTR // CTR[0] ext HASH0.16b, HASH0.16b, HASH0.16b, #8 add x6, IVEC0, #16 orr x9, x11, x9, lsl #32 // CTR[0] fmov d0, x10 // CTR[0] rev64 HASH0.16b, HASH0.16b fmov CTR0.d[1], x9 // CTR[0]--OK st1 {CTR0.16b }, [IVEC0] // out hash st1 {HASH0.16b }, [x6] // out hash OUT_STP .LDec_ret: and x0, INLEN, #-64 // length of processed data AARCH64_AUTIASP ret .size AES_GCM_DecryptBlockAsm,.-AES_GCM_DecryptBlockAsm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes_gcm_64block_aarch64.S
Unix Assembly
unknown
15,484
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) #include "aes_gcm_96block_x86_64.S" .text .balign 16 g_byteSwapMask: .byte 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08 .byte 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 .size g_byteSwapMask, .-g_byteSwapMask .balign 16 g_oneHigh: .byte 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .size g_oneHigh, .-g_oneHigh .balign 16 g_oneLow: .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 .size g_oneLow, .-g_oneLow .balign 16 g_poly: .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2 .size g_poly, .-g_poly /* * uint32_t AES_GCM_EncryptBlockAsm(MODES_GCM_Ctx *ctx, const uint8_t *in, * uint8_t *out, uint32_t len, void *key); * ctx %rdi * in %rsi * out %rdx * len %rcx * key %r8 */ .globl AES_GCM_EncryptBlockAsm .type AES_GCM_EncryptBlockAsm, @function .balign 32 AES_GCM_EncryptBlockAsm: .cfi_startproc push %r12 push %r13 leaq g_byteSwapMask(%rip), %r11 mov 240(%r8), %r9d // rounds shrl $4, %ecx movl 12(%rdi), %r12d // counter r12d(32bit) mov %ecx, %eax lea 0x80(%r8), %r8 shl $4, %eax cmp $6, %ecx jb .Lm64_enc_pass // If the number of data blocks is less than six, the data is skipped. call AES_GCM_Encrypt96BlockAsm // Invoke six parallel processing parts. .Lm64_enc_pass: cmp $4, %ecx // If the remaining data is less than four blocks, // the function is returned. jb .Lm64_enc_return vmovdqu (%rdi), %xmm0 // iv and $0b11, %ecx lea -2(%r9d), %r13d addl $0x4000000, %r12d // ctr inc jc .Lm64_enc_ctr_carry mov %r12d, 12(%rdi) // out iv vmovdqa 0x20(%r11), %xmm14 // Lone_low lea -0x70(%r8), %r10 vpaddb %xmm14, %xmm0, %xmm1 vmovdqu -0x80(%r8), %xmm4 // key0 vpaddb %xmm14, %xmm1, %xmm2 vpxor %xmm4, %xmm0, %xmm0 vpxor %xmm4, %xmm1, %xmm1 vpaddb %xmm14, %xmm2, %xmm3 vpxor %xmm4, %xmm2, %xmm2 vpxor %xmm4, %xmm3, %xmm3 jmp .Lm64_enc_aes .Lm64_enc_ctr_carry: vmovdqa 0x10(%r11), %xmm14 // Lone_high bswap %r12d vmovdqa (%r11), %xmm15 addl $0x100, %r12d // add carry bit vpshufb %xmm15, %xmm0, %xmm0 bswap %r12d vpaddd %xmm14, %xmm0, %xmm1 vpshufb %xmm15, %xmm0, %xmm0 mov %r12d, 12(%rdi) // out iv vpaddd %xmm14, %xmm1, %xmm2 lea -0x70(%r8), %r10 vpshufb %xmm15, %xmm1, %xmm1 vmovdqu -0x80(%r8), %xmm4 // key0 vpaddd %xmm14, %xmm2, %xmm3 vpxor %xmm4, %xmm0, %xmm0 vpshufb %xmm15, %xmm2, %xmm2 vpxor %xmm4, %xmm1, %xmm1 vpshufb %xmm15, %xmm3, %xmm3 vpxor %xmm4, %xmm2, %xmm2 vpxor %xmm4, %xmm3, %xmm3 jmp .Lm64_enc_aes .balign 16 .Lm64_enc_aes: vmovdqu (%r10), %xmm4 // key1-8/10/12 vaesenc %xmm4, %xmm0, %xmm0 vaesenc %xmm4, %xmm1, %xmm1 vaesenc %xmm4, %xmm2, %xmm2 vaesenc %xmm4, %xmm3, %xmm3 lea 0x10(%r10), %r10 dec %r13d jnz .Lm64_enc_aes vmovdqu (%r10), %xmm4 // key9/11/13 vmovdqu 0x10(%r10), %xmm5 // key10/12/14 vaesenc %xmm4, %xmm0, %xmm0 vpxor (%rsi), %xmm5, %xmm6 // last key xor plaintext vaesenc %xmm4, %xmm1, %xmm1 vpxor 0x10(%rsi), %xmm5, %xmm7 vaesenc %xmm4, %xmm2, %xmm2 vpxor 0x20(%rsi), %xmm5, %xmm8 vaesenc %xmm4, %xmm3, %xmm3 vpxor 0x30(%rsi), %xmm5, %xmm9 vaesenclast %xmm6, %xmm0, %xmm10 vmovdqu 16(%rdi), %xmm5 // ghash vaesenclast %xmm7, %xmm1, %xmm11 vmovdqu %xmm10, (%rdx) // out ciphertext vaesenclast %xmm8, %xmm2, %xmm12 vmovdqu %xmm11, 0x10(%rdx) vaesenclast %xmm9, %xmm3, %xmm13 vmovdqu %xmm12, 0x20(%rdx) // +++++++++++++++++ ghash process +++++++++++++++++++++++++++++++++ vmovdqu %xmm13, 0x30(%rdx) vpxor %xmm5, %xmm10, %xmm0 // input for ghash operation vmovdqu 0x20+0x40(%rdi), %xmm1 // hash key h^4 vpshufb (%r11), %xmm0, %xmm0 // data transform vmovdqu 0x20+0x50(%rdi), %xmm2 // hash key h^3_4 vpalignr $8, %xmm0, %xmm0, %xmm3 vpclmulqdq $0x11, %xmm1, %xmm0, %xmm9 // Karatsuba Multiply vpxor %xmm0, %xmm3, %xmm3 vpclmulqdq $0x00, %xmm1, %xmm0, %xmm7 vmovdqu 0x20+0x30(%rdi), %xmm1 // hash key h^3 vpclmulqdq $0x11, %xmm2, %xmm3, %xmm8 vpshufb (%r11), %xmm11, %xmm0 // data transform vpalignr $8, %xmm0, %xmm0, %xmm3 vpclmulqdq $0x11, %xmm1, %xmm0, %xmm5 // Karatsuba Multiply vpxor %xmm0, %xmm3, %xmm3 vpclmulqdq $0x00, %xmm1, %xmm0, %xmm0 vpxor %xmm5, %xmm9, %xmm9 vmovdqu 0x20+0x10(%rdi), %xmm1 // hash key h^2 vpclmulqdq $0x00, %xmm2, %xmm3, %xmm3 vpxor %xmm0, %xmm7, %xmm7 vpshufb (%r11), %xmm12, %xmm0 // data transform vpxor %xmm3, %xmm8, %xmm8 vmovdqu 0x20+0x20(%rdi), %xmm2 // hash key h^1_2 vpalignr $8, %xmm0, %xmm0, %xmm3 vpclmulqdq $0x11, %xmm1, %xmm0, %xmm5 // Karatsuba Multiply vpxor %xmm0, %xmm3, %xmm3 vpclmulqdq $0x00, %xmm1, %xmm0, %xmm0 vmovdqu 0x20(%rdi), %xmm1 // hash key h^1 vpxor %xmm5, %xmm9, %xmm9 vpclmulqdq $0x11, %xmm2, %xmm3, %xmm3 vpxor %xmm0, %xmm7, %xmm7 vpshufb (%r11), %xmm13, %xmm0 // data transform vpxor %xmm3, %xmm8, %xmm8 vpalignr $8, %xmm0, %xmm0, %xmm3 vpclmulqdq $0x11, %xmm1, %xmm0, %xmm5 // Karatsuba Multiply vpxor %xmm0, %xmm3, %xmm3 vpclmulqdq $0x00, %xmm1, %xmm0, %xmm0 vpxor %xmm5, %xmm9, %xmm5 vpclmulqdq $0x00, %xmm2, %xmm3, %xmm3 vpxor %xmm0, %xmm7, %xmm0 vpxor %xmm3, %xmm8, %xmm3 vpxor %xmm0, %xmm5, %xmm1 vpxor %xmm1, %xmm3, %xmm3 vpslldq $8, %xmm3, %xmm4 vmovdqa 0x30(%r11), %xmm14 // Lpoly vpxor %xmm4, %xmm0, %xmm0 vpalignr $8, %xmm0, %xmm0, %xmm2 // 1st phase of reduction vpclmulqdq $0x10, %xmm14, %xmm0, %xmm0 vpsrldq $8, %xmm3, %xmm3 vpxor %xmm2, %xmm0, %xmm0 vpalignr $8, %xmm0, %xmm0, %xmm2 // 2nd phase of reduction vpxor %xmm3, %xmm5, %xmm5 vpclmulqdq $0x10, %xmm14, %xmm0, %xmm0 vpxor %xmm5, %xmm2, %xmm2 vpxor %xmm2, %xmm0, %xmm0 vpshufb (%r11), %xmm0, %xmm0 // results for ghash // ------------------- ghash complete --------------------------------- vmovdqu %xmm0, 16(%rdi) // out ghash .Lm64_enc_return: shl $4, %ecx sub %ecx, %eax pop %r13 pop %r12 ret .cfi_endproc .size AES_GCM_EncryptBlockAsm, .-AES_GCM_EncryptBlockAsm /* * uint32_t AES_GCM_DecryptBlockAsm(MODES_GCM_Ctx *ctx, const uint8_t *in, * uint8_t *out, uint32_t len, void *key); * ctx %rdi * in %rsi * out %rdx * len %rcx * key %r8 */ .globl AES_GCM_DecryptBlockAsm .type AES_GCM_DecryptBlockAsm, @function .balign 32 AES_GCM_DecryptBlockAsm: .cfi_startproc leaq g_byteSwapMask(%rip), %r11 shrl $4, %ecx mov %ecx, %eax shll $4, %eax vmovdqa (%r11), %xmm15 // g_byteSwapMask mov 240(%r8), %r9d // rounds cmp $6, %ecx // invoke six parallel processing parts. jb .Lm64_dec_pass // if the number of data blocks is less than six, the data is skipped. call AES_GCM_Decrypt96BlockAsm .Lm64_dec_pass: cmp $4, %ecx // If the remaining data is less than four blocks, // the function is returned. jb .Lm64_dec_return .balign 16 .Lm64_dec_loop: vmovdqu (%rdi), %xmm0 // iv mov 12(%rdi), %r10d // counter r10d(32bit) addl $0x4000000, %r10d // ctr inc jc .Lm64_dec_ctr_carry vmovdqa 0x20(%r11), %xmm14 // Lone_low vpaddb %xmm14, %xmm0, %xmm1 vpaddb %xmm14, %xmm1, %xmm2 vpaddb %xmm14, %xmm2, %xmm3 jmp .Lm64_dec_aes_cipher .Lm64_dec_ctr_carry: vmovdqa (%r11), %xmm15 bswap %r10d vpshufb %xmm15, %xmm0, %xmm0 vmovdqa 0x10(%r11), %xmm14 // Lone_high addl $0x100, %r10d // add carry bit vpaddd %xmm14, %xmm0, %xmm1 vpshufb %xmm15, %xmm0, %xmm0 vpaddd %xmm14, %xmm1, %xmm2 vpshufb %xmm15, %xmm1, %xmm1 vpaddd %xmm14, %xmm2, %xmm3 vpshufb %xmm15, %xmm2, %xmm2 bswap %r10d vpshufb %xmm15, %xmm3, %xmm3 jmp .Lm64_dec_aes_cipher .balign 32 .Lm64_dec_aes_cipher: vmovdqu (%r8), %xmm7 mov %r10d, 12(%rdi) // out iv lea 0x10(%r8), %r10 vmovdqu 0x10(%rsi), %xmm5 vpxor %xmm7, %xmm0, %xmm0 // key 0 vmovdqu 0x20(%rsi), %xmm6 vpxor %xmm7, %xmm1, %xmm1 vmovdqu 0x30(%rsi), %xmm9 vpxor %xmm7, %xmm2, %xmm2 vmovdqu (%r10), %xmm4 vpxor %xmm7, %xmm3, %xmm3 // +++++++++++++++++ ghash process +++++++++++++++++++++++++++++++++ vmovdqu 16(%rdi), %xmm11 // ghash lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 1 vpxor (%rsi), %xmm11, %xmm10 // input for ghash operation vmovdqu 0x20+0x40(%rdi), %xmm11 // hash key h^4 vaesenc %xmm4, %xmm1, %xmm1 vmovdqu 0x20+0x50(%rdi), %xmm12 // hash key h^3_4 vpshufb (%r11), %xmm10, %xmm10 // data transform vaesenc %xmm4, %xmm2, %xmm2 vpalignr $8, %xmm10, %xmm10, %xmm13 vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm15 // Karatsuba Multiply lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 2 vpxor %xmm10, %xmm13, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm7 vaesenc %xmm4, %xmm1, %xmm1 vmovdqu 0x20+0x30(%rdi), %xmm11 // hash key h^3 vpclmulqdq $0x11, %xmm12, %xmm13, %xmm8 vaesenc %xmm4, %xmm2, %xmm2 vpshufb (%r11), %xmm5, %xmm10 // data transform vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpalignr $8, %xmm10, %xmm10, %xmm13 lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 3 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vaesenc %xmm4, %xmm1, %xmm1 vpxor %xmm10, %xmm13, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vaesenc %xmm4, %xmm2, %xmm2 vpxor %xmm14, %xmm15, %xmm15 vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpxor %xmm10, %xmm7, %xmm7 lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 4 vpxor %xmm13, %xmm8, %xmm8 vaesenc %xmm4, %xmm1, %xmm1 vmovdqu 0x20+0x10(%rdi), %xmm11 // hash key h^2 vmovdqu 0x20+0x20(%rdi), %xmm12 // hash key h^1_2 vaesenc %xmm4, %xmm2, %xmm2 vpshufb (%r11), %xmm6, %xmm10 // data transform vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpalignr $8, %xmm10, %xmm10, %xmm13 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 5 vpxor %xmm10, %xmm13, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vaesenc %xmm4, %xmm1, %xmm1 vpxor %xmm14, %xmm15, %xmm15 vpclmulqdq $0x11, %xmm12, %xmm13, %xmm13 vaesenc %xmm4, %xmm2, %xmm2 vpxor %xmm10, %xmm7, %xmm7 vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpxor %xmm13, %xmm8, %xmm8 lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 6 vmovdqu 0x20(%rdi), %xmm11 // hash key h^1 vpshufb (%r11), %xmm9, %xmm10 // data transform vpalignr $8, %xmm10, %xmm10, %xmm13 vaesenc %xmm4, %xmm1, %xmm1 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vpxor %xmm10, %xmm13, %xmm13 vaesenc %xmm4, %xmm2, %xmm2 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vpxor %xmm14, %xmm15, %xmm14 vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 vpxor %xmm10, %xmm7, %xmm10 vpxor %xmm13, %xmm8, %xmm13 lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 7 vpxor %xmm10, %xmm14, %xmm11 vaesenc %xmm4, %xmm1, %xmm1 vpxor %xmm11, %xmm13, %xmm13 vpslldq $8, %xmm13, %xmm11 vaesenc %xmm4, %xmm2, %xmm2 vpsrldq $8, %xmm13, %xmm13 vpxor %xmm13, %xmm14, %xmm14 vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpxor %xmm11, %xmm10, %xmm10 lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm0, %xmm0 // key 8 vmovdqa 0x30(%r11), %xmm13 // Lpoly vaesenc %xmm4, %xmm1, %xmm1 vpalignr $8, %xmm10, %xmm10, %xmm12 // 1st phase of reduction vpclmulqdq $0x10, %xmm13, %xmm10, %xmm10 vpxor %xmm12, %xmm10, %xmm10 vaesenc %xmm4, %xmm2, %xmm2 vpalignr $8, %xmm10, %xmm10, %xmm12 // 2nd phase of reduction vaesenc %xmm4, %xmm3, %xmm3 vmovdqu (%r10), %xmm4 vpclmulqdq $0x10, %xmm13, %xmm10, %xmm10 vpxor %xmm14, %xmm12, %xmm12 vaesenc %xmm4, %xmm0, %xmm0 // key 9 vpxor %xmm12, %xmm10, %xmm10 vaesenc %xmm4, %xmm1, %xmm1 lea 0x10(%r10), %r10 vaesenc %xmm4, %xmm2, %xmm2 vpshufb (%r11), %xmm10, %xmm10 // ------------------- ghash complete --------------------------------- vaesenc %xmm4, %xmm3, %xmm3 vmovdqu %xmm10, 16(%rdi) // out ghash cmp $12, %r9d jb .Lm64_dec_ending vmovdqu (%r10), %xmm4 vmovdqu 0x10(%r10), %xmm5 vaesenc %xmm4, %xmm0, %xmm0 // key 10 vaesenc %xmm4, %xmm1, %xmm1 vaesenc %xmm4, %xmm2, %xmm2 vaesenc %xmm4, %xmm3, %xmm3 lea 0x20(%r10), %r10 vaesenc %xmm5, %xmm0, %xmm0 // key 11 vaesenc %xmm5, %xmm1, %xmm1 vaesenc %xmm5, %xmm2, %xmm2 vaesenc %xmm5, %xmm3, %xmm3 je .Lm64_dec_ending vmovdqu (%r10), %xmm4 vmovdqu 0x10(%r10), %xmm5 vaesenc %xmm4, %xmm0, %xmm0 // key 12 vaesenc %xmm4, %xmm1, %xmm1 vaesenc %xmm4, %xmm2, %xmm2 vaesenc %xmm4, %xmm3, %xmm3 lea 0x20(%r10), %r10 vaesenc %xmm5, %xmm0, %xmm0 // key 13 vaesenc %xmm5, %xmm1, %xmm1 vaesenc %xmm5, %xmm2, %xmm2 vaesenc %xmm5, %xmm3, %xmm3 jmp .Lm64_dec_ending .Lm64_dec_ending: vmovdqu (%r10), %xmm4 // key10/12/14 vpxor (%rsi), %xmm4, %xmm5 // last key xor plaintext vpxor 0x10(%rsi), %xmm4, %xmm6 vaesenclast %xmm5, %xmm0, %xmm0 vpxor 0x20(%rsi), %xmm4, %xmm7 vaesenclast %xmm6, %xmm1, %xmm1 vpxor 0x30(%rsi), %xmm4, %xmm8 vmovdqu %xmm0, (%rdx) // out ciphertext vaesenclast %xmm7, %xmm2, %xmm2 vmovdqu %xmm1, 0x10(%rdx) vaesenclast %xmm8, %xmm3, %xmm3 vmovdqu %xmm2, 0x20(%rdx) and $0b11, %ecx vmovdqu %xmm3, 0x30(%rdx) .Lm64_dec_return: shl $4, %ecx sub %ecx, %eax ret .cfi_endproc .size AES_GCM_DecryptBlockAsm, .-AES_GCM_DecryptBlockAsm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes_gcm_64block_x86_64.S
Unix Assembly
unknown
16,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" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) .text /* * MODES_GCM_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, void *key * ctx %rdi * in %rsi * out %rdx * len %rcx * key %r8 * The %r12 register is shared with AES_GCM_EncryptBlockAsm. Pay attention to its use. */ .type AES_GCM_Encrypt96BlockAsm, @function .balign 16 AES_GCM_Encrypt96BlockAsm: .cfi_startproc push %rbp mov %rsp, %rbp vmovdqu (%rdi), %xmm0 // iv lea -2(%r9d), %r13d addl $0x6000000, %r12d // ctr inc vmovdqu -0x80(%r8), %xmm6 // key0 leaq -0x70(%r8), %r10 jc .Lm96_enc_ctr_carry vmovdqa 0x20(%r11), %xmm7 movl %r12d, 12(%rdi) // out iv vpaddb %xmm7, %xmm0, %xmm1 // g_oneLow vpxor %xmm6, %xmm0, %xmm0 vpaddb %xmm7, %xmm1, %xmm2 vpxor %xmm6, %xmm1, %xmm1 vpaddb %xmm7, %xmm2, %xmm3 vpxor %xmm6, %xmm2, %xmm2 vpaddb %xmm7, %xmm3, %xmm4 vpxor %xmm6, %xmm3, %xmm3 vpaddb %xmm7, %xmm4, %xmm5 vpxor %xmm6, %xmm4, %xmm4 vpxor %xmm6, %xmm5, %xmm5 vmovdqu (%r10), %xmm6 // key1 .balign 16 .Lm96_enc_aes: vaesenc %xmm6, %xmm0, %xmm0 vaesenc %xmm6, %xmm1, %xmm1 vaesenc %xmm6, %xmm2, %xmm2 vaesenc %xmm6, %xmm3, %xmm3 vaesenc %xmm6, %xmm4, %xmm4 leaq 0x10(%r10), %r10 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu (%r10), %xmm6 // key2-9/11/13 decl %r13d jnz .Lm96_enc_aes vaesenc %xmm6, %xmm0, %xmm0 vmovdqu 0x10(%r10), %xmm10 // key10/12/14 sub $112, %rsp vaesenc %xmm6, %xmm1, %xmm1 vpxor (%rsi), %xmm10, %xmm8 // last key xor plaintext and $-16, %rsp vaesenc %xmm6, %xmm2, %xmm2 vpxor 0x10(%rsi), %xmm10, %xmm7 vaesenc %xmm6, %xmm3, %xmm3 vpxor 0x20(%rsi), %xmm10, %xmm9 vaesenc %xmm6, %xmm4, %xmm4 vpxor 0x30(%rsi), %xmm10, %xmm11 vaesenc %xmm6, %xmm5, %xmm5 vpxor 0x40(%rsi), %xmm10, %xmm12 vaesenclast %xmm8, %xmm0, %xmm0 vpxor 0x50(%rsi), %xmm10, %xmm13 vmovdqa (%r11), %xmm15 // g_byteSwapMask vaesenclast %xmm7, %xmm1, %xmm1 vmovdqu %xmm0, (%rdx) // out ciphertext vpxor 16(%rdi), %xmm0, %xmm0 // Do it in advance. input for ghash operation vmovdqa 0x20(%r11), %xmm6 vpshufb %xmm15, %xmm0, %xmm8 vmovdqu (%rdi), %xmm0 // iv vaesenclast %xmm9, %xmm2, %xmm2 vmovdqa %xmm8, (%rsp) vmovdqu -0x80(%r8), %xmm7 vmovdqu %xmm1, 0x10(%rdx) vpshufb %xmm15, %xmm1, %xmm9 vpaddb %xmm6, %xmm0, %xmm1 // g_oneLow vpxor %xmm7, %xmm0, %xmm0 // key 0 vaesenclast %xmm11, %xmm3, %xmm3 vmovdqa %xmm9, 0x10(%rsp) vmovdqu %xmm2, 0x20(%rdx) vpshufb %xmm15, %xmm2, %xmm11 vpaddb %xmm6, %xmm1, %xmm2 vpxor %xmm7, %xmm1, %xmm1 vaesenclast %xmm12, %xmm4, %xmm4 vmovdqa %xmm11, 0x20(%rsp) vmovdqu %xmm3, 0x30(%rdx) vpshufb %xmm15, %xmm3, %xmm12 vpaddb %xmm6, %xmm2, %xmm3 vpxor %xmm7, %xmm2, %xmm2 vaesenclast %xmm13, %xmm5, %xmm5 vmovdqa %xmm12, 0x30(%rsp) vmovdqu %xmm4, 0x40(%rdx) vpshufb %xmm15, %xmm4, %xmm13 vpaddb %xmm6, %xmm3, %xmm4 vpxor %xmm7, %xmm3, %xmm3 leaq 0x60(%rsi), %rsi vmovdqu %xmm5, 0x50(%rdx) vpshufb %xmm15, %xmm5, %xmm14 vpaddb %xmm6, %xmm4, %xmm5 vmovdqa %xmm13, 0x40(%rsp) vmovdqa %xmm14, 0x50(%rsp) cmpl $12, %ecx // If the remaining length is less than 12, // the loop is not performed. jb .Lm96_inner_out addl $0x6000000, %r12d jc .Lm96_inner_ctr_carry .balign 16 .Lm96_inner_loop: vmovdqu -0x70(%r8), %xmm6 // rk0 // function stitch second 6x-blocks encryption and ghash of first 6x-blocks. vaesenc %xmm6, %xmm0, %xmm0 // encryption of the 'inout0' of second 6x-blocks. vmovdqu 0x20+0x70(%rdi), %xmm11 // H^6 vmovdqa (%rsp), %xmm10 // xmm10 = inout0, the first encrypted 6x-blocks vaesenc %xmm6, %xmm1, %xmm1 movl %r12d, 12(%rdi) // out iv vmovdqu 0x20+0x80(%rdi), %xmm12 // xmm12 = (H^5.h + H^5.l, H^6.h + H^6.l) vpclmulqdq $0x11, %xmm11, %xmm10, %xmm15 // Karatsuba Multiply. xmm15 = H^6.h·inout0.h, where '·' represent multiply in GF(2^128) of GHASH. vaesenc %xmm6, %xmm2, %xmm2 vpxor %xmm7, %xmm4, %xmm4 vpxor %xmm7, %xmm5, %xmm5 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm14 // xmm14 = H^6.l·inout0.l vaesenc %xmm6, %xmm3, %xmm3 vpunpckhqdq %xmm10, %xmm10, %xmm13 // xmm13= (inout0.h, inout0.h) leaq 0x10(%r8), %r10 vpxor %xmm10, %xmm13, %xmm13 // xmm13 = (inout0.h + inout0.l, inout0.h + inout0.h) vmovdqu 0x20+0x60(%rdi), %xmm11 // H^5 vpclmulqdq $0x10, %xmm12, %xmm13, %xmm13 // xmm13 = (H^6.h + H^6.l)·(inout0.h + inout0.l) vaesenc %xmm6, %xmm4, %xmm4 vmovdqa 0x10(%rsp), %xmm10 // xmm10 = inout1 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm8 // xmm8 = H^5.h·inout1.h vaesenc %xmm6, %xmm5, %xmm5 vmovdqu -0x60(%r8), %xmm7 // rk1 vpxor %xmm8, %xmm15, %xmm15 // xmm15 += xmm8 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm11 // xmm11 = H^5.l·inout1.l vaesenc %xmm7, %xmm0, %xmm0 vpunpckhqdq %xmm10, %xmm10, %xmm8 // xmm8 = (inout1.h, inout1.h) vpxor %xmm11, %xmm14, %xmm14 // xmm14 += xmm11 vpxor %xmm10, %xmm8, %xmm8 // xmm8 = (inout1.h + inout1.l, inout1.h + inout1.h) vmovdqu 0x20+0x40(%rdi), %xmm11 // xmm11 = H^4 vpclmulqdq $0x00, %xmm12, %xmm8, %xmm9 // xmm9 = (H^5.h + H^5.l)·(inout1.h + inout1.l) vaesenc %xmm7, %xmm1, %xmm1 vmovdqa 0x20(%rsp), %xmm10 // xmm10 = inout2 vpxor %xmm9, %xmm13, %xmm13 // xmm13 += xmm9 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm8 // xmm8 = H^4.h·inout2.h vaesenc %xmm7, %xmm2, %xmm2 vmovdqu 0x20+0x50(%rdi), %xmm12 // xmm12 = (H^3.h + H^3.l, H^4.h + H^4.l) vpclmulqdq $0x00, %xmm11, %xmm10, %xmm9 // xmm9 = H^4.l·inout2.l vaesenc %xmm7, %xmm3, %xmm3 vpxor %xmm8, %xmm15, %xmm15 // xmm15 += xmm8 vpxor %xmm9, %xmm14, %xmm14 // xmm14 += xmm9 vpunpckhqdq %xmm10, %xmm10, %xmm6 // xmm6 = (inout2.h, inout2.h) vaesenc %xmm7, %xmm4, %xmm4 vpxor %xmm10, %xmm6, %xmm6 // xmm6 = (inout2.h + inout2.l, inout2.h + inout2.h) vmovdqu 0x20+0x30(%rdi), %xmm11 // xmm11 = H^3 vpclmulqdq $0x10, %xmm12, %xmm6, %xmm8 // xmm8 = (H^4.h + H^4.l)·(inout2.h + inout2.h) vaesenc %xmm7, %xmm5, %xmm5 vmovdqa 0x30(%rsp), %xmm10 // xmm10 = inout3 vmovdqu -0x50(%r8), %xmm6 // rk2 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm9 // xmm9 = H^3.h·inout3.h vaesenc %xmm6, %xmm0, %xmm0 vpxor %xmm8, %xmm13, %xmm13 // xmm13 += xmm8 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm7 // xmm7 = H^3.l·inout3.l vaesenc %xmm6, %xmm1, %xmm1 vpxor %xmm9, %xmm15, %xmm15 // xmm15 += xmm9 vpunpckhqdq %xmm10, %xmm10, %xmm8 // xmm8 = (inout3.h, inout3,h) vaesenc %xmm6, %xmm2, %xmm2 vpxor %xmm10, %xmm8, %xmm8 // xmm8 = (inout3.h + inout3.l, inout3.h + inout3.h) vpxor %xmm7, %xmm14, %xmm14 // xmm14 += xmm7 vpclmulqdq $0x00, %xmm12, %xmm8, %xmm9 // xmm9 = (H^3.h + H^3.l)·(inout3.h + inout3.l) vaesenc %xmm6, %xmm3, %xmm3 vmovdqa 0x40(%rsp), %xmm10 // xmm10 = inout4 vmovdqu 0x20+0x10(%rdi), %xmm11 // xmm11 = H^2 vaesenc %xmm6, %xmm4, %xmm4 vpxor %xmm9, %xmm13, %xmm13 // xmm13 += xmm9 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu -0x40(%r8), %xmm7 // rk3 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm8 // xmm8 = H^2.h·inout4.h vaesenc %xmm7, %xmm0, %xmm0 vmovdqu 0x20+0x20(%rdi), %xmm12 // xmm12 = (H^1.h + H^1.l, H^2.h + H^2.l) vpclmulqdq $0x00, %xmm11, %xmm10, %xmm9 // xmm9 = H^2.h·inout4.h vaesenc %xmm7, %xmm1, %xmm1 vpunpckhqdq %xmm10, %xmm10, %xmm6 // xmm6 = (inout4.h, inout4.h) vpxor %xmm8, %xmm15, %xmm15 // xmm15 += xmm8 vaesenc %xmm7, %xmm2, %xmm2 vpxor %xmm10, %xmm6, %xmm6 // xmm6 = (inout4.h + inout4.l, inout4.h + inout4.h) vpxor %xmm9, %xmm14, %xmm14 // xmm14 += xmm9 vpclmulqdq $0x10, %xmm12, %xmm6, %xmm8 // xmm8 = (H^2.h + H^2.l)·(inout4.h + inout4.l) vaesenc %xmm7, %xmm3, %xmm3 vmovdqa 0x50(%rsp), %xmm10 // xmm10 = inout5 vmovdqu 0x20(%rdi), %xmm11 // xmm11 = H^1 vaesenc %xmm7, %xmm4, %xmm4 vpxor %xmm8, %xmm13, %xmm13 // xmm13 += xmm8 vmovdqu -0x30(%r8), %xmm6 // rk4 vaesenc %xmm7, %xmm5, %xmm5 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm9 // xmm9 = H^1.h·inout5.h vaesenc %xmm6, %xmm0, %xmm0 vpunpckhqdq %xmm10, %xmm10, %xmm7 // xmm7 = (inout5.h, inout5.h) vpclmulqdq $0x00, %xmm11, %xmm10, %xmm8 // xmm8 = H^1.l·inout5.l vaesenc %xmm6, %xmm1, %xmm1 vpxor %xmm10, %xmm7, %xmm7 // xmm7 = (inout5.h + inout5.l, inout5.h + inout5.h) vpxor %xmm9, %xmm15, %xmm15 // xmm15 += xmm9 vpclmulqdq $0x00, %xmm12, %xmm7, %xmm12 // xmm12 = (H^1.h + H^1.l)·(inout5.h + inout5.l), that's all the (l+h)·(l+h) vaesenc %xmm6, %xmm2, %xmm2 vpxor %xmm12, %xmm13, %xmm13 // xmm13 += xmm12 vpxor %xmm8, %xmm14, %xmm14 // xmm14 += xmm8 vaesenc %xmm6, %xmm3, %xmm3 vpxor %xmm15, %xmm13, %xmm13 // Karatsuba, middle coefficient + higher coefficient vpxor %xmm14, %xmm13, %xmm13 // Karatsuba, middle coefficient + lower coefficient vpslldq $8, %xmm13, %xmm9 // xmm9 = (0, xmm13.l) vaesenc %xmm6, %xmm4, %xmm4 vpxor %xmm9, %xmm14, %xmm14 // xmm14 is the lower 128-bits of ghash of first 6x-blocks vaesenc %xmm6, %xmm5, %xmm5 // montgomery reduction method from https://link.springer.com/chapter/10.1007/978-3-031-34671-2_30 // T = [D:C:B:A]. here, xmm14 = [B:A], xmm15 = [D:C] // WA = W·A, U = C + A + WA.h, V = B + WA.l vmovdqu -0x20(%r8), %xmm7 // rk5 vpalignr $8, %xmm14, %xmm14, %xmm9 // xmm9 = [A:B] vaesenc %xmm7, %xmm0, %xmm0 vmovdqu 0x30(%r11), %xmm12 // poly vaesenc %xmm7, %xmm1, %xmm1 vpsrldq $8, %xmm13, %xmm8 // xmm8 = (xmm13.h, 0) vpxor %xmm8, %xmm15, %xmm15 // xmm15 is the higher 128-bits of ghash of first 6x-blocks vaesenc %xmm7, %xmm2, %xmm2 vpclmulqdq $0x10, %xmm12, %xmm14, %xmm14 // xmm14 = W·A, where W = 0xC200000000000000. vaesenc %xmm7, %xmm3, %xmm3 vmovdqu -0x10(%r8), %xmm6 // rk6 vpxor %xmm9, %xmm14, %xmm14 // xmm14 = W·A + [A:B] = [A+WA.h:B+WA.l] = [U':V], where U' = A + WA.h vaesenc %xmm7, %xmm4, %xmm4 vpalignr $8, %xmm14, %xmm14, %xmm8 // xmm8 = [V:U'] vpclmulqdq $0x10, %xmm12, %xmm14, %xmm14 // xmm14 = W·V vaesenc %xmm7, %xmm5, %xmm5 vpxor %xmm8, %xmm15, %xmm15 // xmm15 = [D+V:C+U'] vpxor %xmm14, %xmm15, %xmm15 // xmm15 = [D+V:C+U'] + W·V = [D+V+WV.h:C+U'+WV.l] = [D+V+WV.h:U+WV.l] = [D+V:U+WV] // reduction finished. vaesenc %xmm6, %xmm0, %xmm0 vaesenc %xmm6, %xmm1, %xmm1 vaesenc %xmm6, %xmm2, %xmm2 vaesenc %xmm6, %xmm3, %xmm3 vaesenc %xmm6, %xmm4, %xmm4 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x00(%r8), %xmm7 // rk7 vaesenc %xmm7, %xmm0, %xmm0 vaesenc %xmm7, %xmm1, %xmm1 vmovdqa %xmm15, %xmm10 vaesenc %xmm7, %xmm2, %xmm2 vaesenc %xmm7, %xmm3, %xmm3 vaesenc %xmm7, %xmm4, %xmm4 cmpl $12, %r9d vaesenc %xmm7, %xmm5, %xmm5 vmovdqu (%r10), %xmm6 vmovdqu 0x10(%r10), %xmm11 jb .Lm96_inner_ending vaesenc %xmm6, %xmm0, %xmm0 // key 10 vaesenc %xmm6, %xmm1, %xmm1 vaesenc %xmm6, %xmm2, %xmm2 vaesenc %xmm6, %xmm3, %xmm3 vaesenc %xmm6, %xmm4, %xmm4 vaesenc %xmm6, %xmm5, %xmm5 vaesenc %xmm11, %xmm0, %xmm0 // key 11 vaesenc %xmm11, %xmm1, %xmm1 vaesenc %xmm11, %xmm2, %xmm2 vaesenc %xmm11, %xmm3, %xmm3 leaq 0x20(%r10), %r10 vaesenc %xmm11, %xmm4, %xmm4 vmovdqu (%r10), %xmm6 vaesenc %xmm11, %xmm5, %xmm5 vmovdqu 0x10(%r10), %xmm11 je .Lm96_inner_ending vaesenc %xmm6, %xmm0, %xmm0 // key 12 vaesenc %xmm6, %xmm1, %xmm1 vaesenc %xmm6, %xmm2, %xmm2 vaesenc %xmm6, %xmm3, %xmm3 vaesenc %xmm6, %xmm4, %xmm4 vaesenc %xmm6, %xmm5, %xmm5 vaesenc %xmm11, %xmm0, %xmm0 // key 13 vaesenc %xmm11, %xmm1, %xmm1 vaesenc %xmm11, %xmm2, %xmm2 vaesenc %xmm11, %xmm3, %xmm3 leaq 0x20(%r10), %r10 vaesenc %xmm11, %xmm4, %xmm4 vmovdqu (%r10), %xmm6 vaesenc %xmm11, %xmm5, %xmm5 vmovdqu 0x10(%r10), %xmm11 jmp .Lm96_inner_ending .balign 16 .Lm96_inner_ctr_carry: vmovdqu (%rdi), %xmm0 // iv bswap %r12d vmovdqa (%r11), %xmm15 addl $0x100, %r12d // add carry bit vmovdqa 0x10(%r11), %xmm14 // lone_high vpshufb %xmm15, %xmm0, %xmm0 vmovdqu -0x80(%r8), %xmm7 vpaddd %xmm14, %xmm0, %xmm1 vpshufb %xmm15, %xmm0, %xmm0 vpaddd %xmm14, %xmm1, %xmm2 vpxor %xmm7, %xmm0, %xmm0 // key 0 vpshufb %xmm15, %xmm1, %xmm1 vpaddd %xmm14, %xmm2, %xmm3 vpxor %xmm7, %xmm1, %xmm1 vpshufb %xmm15, %xmm2, %xmm2 vpaddd %xmm14, %xmm3, %xmm4 vpxor %xmm7, %xmm2, %xmm2 vpshufb %xmm15, %xmm3, %xmm3 vpaddd %xmm14, %xmm4, %xmm5 vpxor %xmm7, %xmm3, %xmm3 vpshufb %xmm15, %xmm4, %xmm4 bswap %r12d vpshufb %xmm15, %xmm5, %xmm5 jmp .Lm96_inner_loop .balign 16 .Lm96_inner_ending: vaesenc %xmm6, %xmm0, %xmm0 // key 9 vpxor (%rsi), %xmm11, %xmm13 // last key xor plaintext vpxor %xmm14, %xmm12, %xmm12 vaesenc %xmm6, %xmm1, %xmm1 vpxor 0x10(%rsi), %xmm11, %xmm8 vaesenclast %xmm13, %xmm0, %xmm0 // key 10/12/14 vmovdqa (%r11), %xmm15 leaq 0x60(%rdx), %rdx vaesenc %xmm6, %xmm2, %xmm2 vpxor 0x20(%rsi), %xmm11, %xmm9 sub $6, %ecx vaesenclast %xmm8, %xmm1, %xmm1 vmovdqu %xmm0, (%rdx) // out result text vaesenc %xmm6, %xmm3, %xmm3 vpshufb %xmm15, %xmm0, %xmm0 vmovdqa 0x20(%r11), %xmm12 vpxor %xmm10, %xmm0, %xmm0 // Do it in advance. input for ghash operation vpxor 0x30(%rsi), %xmm11, %xmm8 vmovdqa %xmm0, (%rsp) vaesenclast %xmm9, %xmm2, %xmm2 vmovdqu -0x80(%r8), %xmm7 vmovdqu %xmm1, 0x10(%rdx) vmovdqu (%rdi), %xmm0 // iv vpshufb %xmm15, %xmm1, %xmm1 vaesenc %xmm6, %xmm4, %xmm4 vmovdqa %xmm1, 0x10(%rsp) vpaddb %xmm12, %xmm0, %xmm1 // g_oneLow vaesenc %xmm6, %xmm5, %xmm5 vpxor %xmm7, %xmm0, %xmm0 // key 0 vmovdqu %xmm2, 0x20(%rdx) vpxor 0x40(%rsi), %xmm11, %xmm9 vpshufb %xmm15, %xmm2, %xmm2 vaesenclast %xmm8, %xmm3, %xmm3 vmovdqa %xmm2, 0x20(%rsp) vpaddb %xmm12, %xmm1, %xmm2 vpxor %xmm7, %xmm1, %xmm1 vpxor 0x50(%rsi), %xmm11, %xmm8 vmovdqu %xmm3, 0x30(%rdx) vpshufb %xmm15, %xmm3, %xmm3 vaesenclast %xmm9, %xmm4, %xmm4 vmovdqa %xmm3, 0x30(%rsp) vpaddb %xmm12, %xmm2, %xmm3 vpxor %xmm7, %xmm2, %xmm2 leaq 0x60(%rsi), %rsi vmovdqu %xmm4, 0x40(%rdx) vpshufb %xmm15, %xmm4, %xmm4 vaesenclast %xmm8, %xmm5, %xmm5 vmovdqa %xmm4, 0x40(%rsp) vpaddb %xmm12, %xmm3, %xmm4 vpxor %xmm7, %xmm3, %xmm3 vmovdqu %xmm5, 0x50(%rdx) vpshufb %xmm15, %xmm5, %xmm5 cmpl $12, %ecx // If the remaining length is greater than or equal to 12, // the loop continues. vmovdqa %xmm5, 0x50(%rsp) vpaddb %xmm12, %xmm4, %xmm5 jb .Lm96_inner_out addl $0x6000000, %r12d jc .Lm96_inner_ctr_carry jmp .Lm96_inner_loop .balign 16 .Lm96_enc_ctr_carry: vmovdqa (%r11), %xmm15 bswap %r12d vmovdqa 0x10(%r11), %xmm14 vpshufb %xmm15, %xmm0, %xmm0 addl $0x100, %r12d // add carry bit vpaddd %xmm14, %xmm0, %xmm1 // g_oneHigh vpshufb %xmm15, %xmm0, %xmm0 vpaddd %xmm14, %xmm1, %xmm2 vpxor %xmm6, %xmm0, %xmm0 vpshufb %xmm15, %xmm1, %xmm1 vpaddd %xmm14, %xmm2, %xmm3 vpxor %xmm6, %xmm1, %xmm1 vpshufb %xmm15, %xmm2, %xmm2 vpaddd %xmm14, %xmm3, %xmm4 vpxor %xmm6, %xmm2, %xmm2 vpshufb %xmm15, %xmm3, %xmm3 vpaddd %xmm14, %xmm4, %xmm5 vpxor %xmm6, %xmm3, %xmm3 vpshufb %xmm15, %xmm4, %xmm4 bswap %r12d vpshufb %xmm15, %xmm5, %xmm5 vpxor %xmm6, %xmm4, %xmm4 movl %r12d, 12(%rdi) // out iv vpxor %xmm6, %xmm5, %xmm5 vmovdqu (%r10), %xmm6 // key1 jmp .Lm96_enc_aes .balign 16 .Lm96_inner_out: // +++++++++++++++++ ghash process +++++++++++++++++++++++++++++++++ vmovdqu 0x20+0x70(%rdi), %xmm11 // hash key h^6 vmovdqa (%rsp), %xmm10 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm9 // Karatsuba Multiply vmovdqu 0x20+0x80(%rdi), %xmm12 // hash key h^5_6 vpxor 0x08(%rsp), %xmm10, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm7 vmovdqu 0x20+0x60(%rdi), %xmm11 // hash key h^5 vmovdqa 0x10(%rsp), %xmm10 vpclmulqdq $0x10, %xmm12, %xmm13, %xmm8 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vmovdqu 0x20+0x50(%rdi), %xmm6 // hash key h^3_4 vpxor 0x18(%rsp), %xmm10, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vmovdqu 0x20+0x40(%rdi), %xmm11 // hash key h^4 vpxor %xmm14, %xmm9, %xmm9 vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 vpxor %xmm10, %xmm7, %xmm7 vmovdqa 0x20(%rsp), %xmm10 vpxor %xmm13, %xmm8, %xmm8 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vpxor 0x28(%rsp), %xmm10, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vmovdqu 0x20+0x30(%rdi), %xmm11 // hash key h^3 vpxor %xmm14, %xmm9, %xmm9 vpclmulqdq $0x10, %xmm6, %xmm13, %xmm13 vpxor %xmm10, %xmm7, %xmm7 vmovdqa 0x30(%rsp), %xmm10 vpxor %xmm13, %xmm8, %xmm8 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vmovdqu 0x20+0x20(%rdi), %xmm12 // hash key h^1_2 vpxor 0x38(%rsp), %xmm10, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vmovdqu 0x20+0x10(%rdi), %xmm11 // hash key h^2 vpxor %xmm14, %xmm9, %xmm9 vpclmulqdq $0x00, %xmm6, %xmm13, %xmm13 vpxor %xmm10, %xmm7, %xmm7 vmovdqa 0x40(%rsp), %xmm10 vpxor %xmm13, %xmm8, %xmm8 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vpxor 0x48(%rsp), %xmm10, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vmovdqu 0x20(%rdi), %xmm11 // hash key h^1 vpxor %xmm14, %xmm9, %xmm9 vpclmulqdq $0x10, %xmm12, %xmm13, %xmm13 vpxor %xmm10, %xmm7, %xmm7 vmovdqa 0x50(%rsp), %xmm10 vpxor %xmm13, %xmm8, %xmm8 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vpxor 0x58(%rsp), %xmm10, %xmm13 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vpxor %xmm14, %xmm9, %xmm14 vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 vpxor %xmm10, %xmm7, %xmm10 vpxor %xmm13, %xmm8, %xmm13 vpxor %xmm10, %xmm14, %xmm11 vpxor %xmm11, %xmm13, %xmm13 vpslldq $8, %xmm13, %xmm11 vmovdqa 0x30(%r11), %xmm15 // g_poly vpsrldq $8, %xmm13, %xmm13 vpxor %xmm11, %xmm10, %xmm10 vpxor %xmm13, %xmm14, %xmm14 vpalignr $8, %xmm10, %xmm10, %xmm12 // 1st phase of reduction vpclmulqdq $0x10, %xmm15, %xmm10, %xmm10 sub $6, %ecx vpxor %xmm12, %xmm10, %xmm10 vpalignr $8, %xmm10, %xmm10, %xmm12 // 2nd phase of reduction vpclmulqdq $0x10, %xmm15, %xmm10, %xmm10 vpxor %xmm14, %xmm12, %xmm12 leaq 0x60(%rdx), %rdx vpxor %xmm12, %xmm10, %xmm10 vpxor %xmm0, %xmm0, %xmm0 vpshufb (%r11), %xmm10, %xmm10 // ------------------- ghash complete --------------------------------- vmovdqa %xmm0, (%rsp) vmovdqu %xmm10, 16(%rdi) // out ghash vmovdqa %xmm0, 0x10(%rsp) vmovdqa %xmm0, 0x20(%rsp) vmovdqa %xmm0, 0x30(%rsp) vmovdqa %xmm0, 0x40(%rsp) vmovdqa %xmm0, 0x50(%rsp) mov %rbp, %rsp pop %rbp ret .cfi_endproc .size AES_GCM_Encrypt96BlockAsm, .-AES_GCM_Encrypt96BlockAsm /* * MODES_GCM_Ctx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, void *key * ctx %rdi * in %rsi * out %rdx * len %rcx * key %r8 */ .type AES_GCM_Decrypt96BlockAsm, @function .balign 32 AES_GCM_Decrypt96BlockAsm: .cfi_startproc push %rbp push %r12 push %r13 push %r14 mov %rsp, %rbp sub $112, %rsp vmovdqu (%rsi), %xmm0 // in0 vmovdqa (%r11), %xmm6 and $-16, %rsp vpshufb %xmm6, %xmm0, %xmm0 vmovdqu 0x10(%rsi), %xmm1 // in1 vmovdqa %xmm0, (%rsp) vpshufb %xmm6, %xmm1, %xmm1 vmovdqu 0x20(%rsi), %xmm2 // in2 vmovdqa %xmm1, 0x10(%rsp) vpshufb %xmm6, %xmm2, %xmm2 vmovdqu 0x30(%rsi), %xmm3 // in3 vmovdqa %xmm2, 0x20(%rsp) vpshufb %xmm6, %xmm3, %xmm3 vmovdqu 0x40(%rsi), %xmm4 // in4 vmovdqa %xmm3, 0x30(%rsp) vpshufb %xmm6, %xmm4, %xmm4 vmovdqu 0x50(%rsi), %xmm5 // in5 vmovdqa %xmm4, 0x40(%rsp) vpshufb %xmm6, %xmm5, %xmm5 vmovdqu 16(%rdi), %xmm10 // ghash vmovdqa %xmm5, 0x50(%rsp) vpshufb %xmm6, %xmm10, %xmm10 .balign 16 .Lm96_dec_loop: mov 12(%rdi), %r10d // counter r10d(32bit) vmovdqu (%rdi), %xmm0 // iv addl $0x6000000, %r10d // ctr inc jc .Lm96_dec_ctr_carry vmovdqa 0x20(%r11), %xmm6 vmovdqu (%r8), %xmm7 vpaddb %xmm6, %xmm0, %xmm1 // g_oneLow vpxor %xmm7, %xmm0, %xmm0 // key 0 vpaddb %xmm6, %xmm1, %xmm2 vpxor %xmm7, %xmm1, %xmm1 vpaddb %xmm6, %xmm2, %xmm3 vpxor %xmm7, %xmm2, %xmm2 vpaddb %xmm6, %xmm3, %xmm4 vpxor %xmm7, %xmm3, %xmm3 vpaddb %xmm6, %xmm4, %xmm5 vmovdqu 0x10(%r8), %xmm6 .Lm96_dec_aes_cipher: vaesenc %xmm6, %xmm0, %xmm0 // key 1 xor %r14, %r14 cmp $12, %ecx mov %r10d, 12(%rdi) // out iv setae %r14b vaesenc %xmm6, %xmm1, %xmm1 vpxor %xmm7, %xmm4, %xmm4 neg %r14 lea 0x90(%r8), %r10 // +++++++++++++++++ ghash process +++++++++++++++++++++++++++++++++ vmovdqu 0x20+0x70(%rdi), %xmm11 // hash key h^6 and $0x60, %r14 vpxor %xmm7, %xmm5, %xmm5 vmovdqu 0x20+0x80(%rdi), %xmm12 // hash key h^5_6 lea (%rsi, %r14), %r14 vaesenc %xmm6, %xmm2, %xmm2 vpxor (%rsp), %xmm10, %xmm10 // xor ghash vaesenc %xmm6, %xmm3, %xmm3 vpalignr $8, %xmm10, %xmm10, %xmm13 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm15 // Karatsuba Multiply vpxor %xmm10, %xmm13, %xmm13 vaesenc %xmm6, %xmm4, %xmm4 movbe (%r14), %r12 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm7 movbe 0x08(%r14), %r13 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x20(%r8), %xmm6 mov %r13, (%rsp) vpclmulqdq $0x10, %xmm12, %xmm13, %xmm8 vmovdqu 0x20+0x60(%rdi), %xmm11 // hash key h^5 mov %r12, 0x08(%rsp) vaesenc %xmm6, %xmm0, %xmm0 // key 2 vmovdqa 0x10(%rsp), %xmm10 vaesenc %xmm6, %xmm1, %xmm1 vpxor 0x18(%rsp), %xmm10, %xmm13 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply movbe 0x10(%r14), %r12 vaesenc %xmm6, %xmm2, %xmm2 vpxor %xmm14, %xmm15, %xmm15 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 movbe 0x18(%r14), %r13 vaesenc %xmm6, %xmm3, %xmm3 mov %r13, 0x10(%rsp) vmovdqu 0x20+0x40(%rdi), %xmm11 // hash key h^4 vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 mov %r12, 0x18(%rsp) vpxor %xmm10, %xmm7, %xmm7 vaesenc %xmm6, %xmm4, %xmm4 vpxor %xmm13, %xmm8, %xmm8 vmovdqu 0x20+0x50(%rdi), %xmm12 // hash key h^3_4 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x30(%r8), %xmm6 vmovdqa 0x20(%rsp), %xmm10 vaesenc %xmm6, %xmm0, %xmm0 // key 3 vpxor 0x28(%rsp), %xmm10, %xmm13 movbe 0x20(%r14), %r12 vaesenc %xmm6, %xmm1, %xmm1 movbe 0x28(%r14), %r13 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply mov %r13, 0x20(%rsp) vaesenc %xmm6, %xmm2, %xmm2 vpxor %xmm14, %xmm15, %xmm15 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 mov %r12, 0x28(%rsp) vmovdqu 0x20+0x30(%rdi), %xmm11 // hash key h^3 vaesenc %xmm6, %xmm3, %xmm3 vpxor %xmm10, %xmm7, %xmm7 vpclmulqdq $0x10, %xmm12, %xmm13, %xmm13 vmovdqa 0x30(%rsp), %xmm10 vaesenc %xmm6, %xmm4, %xmm4 vpxor %xmm13, %xmm8, %xmm8 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x40(%r8), %xmm6 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply vpxor 0x38(%rsp), %xmm10, %xmm13 vaesenc %xmm6, %xmm0, %xmm0 // key 4 movbe 0x30(%r14), %r12 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 vpxor %xmm14, %xmm15, %xmm15 vaesenc %xmm6, %xmm1, %xmm1 movbe 0x38(%r14), %r13 vpxor %xmm10, %xmm7, %xmm7 vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 mov %r13, 0x30(%rsp) vpxor %xmm13, %xmm8, %xmm8 vaesenc %xmm6, %xmm2, %xmm2 mov %r12, 0x38(%rsp) vaesenc %xmm6, %xmm3, %xmm3 vmovdqu 0x20+0x10(%rdi), %xmm11 // hash key h^2 vaesenc %xmm6, %xmm4, %xmm4 vmovdqu 0x20+0x20(%rdi), %xmm12 // hash key h^1_2 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x50(%r8), %xmm6 vmovdqa 0x40(%rsp), %xmm10 vaesenc %xmm6, %xmm0, %xmm0 // key 5 vpxor 0x48(%rsp), %xmm10, %xmm13 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply movbe 0x40(%r14), %r12 vaesenc %xmm6, %xmm1, %xmm1 movbe 0x48(%r14), %r13 vpxor %xmm14, %xmm15, %xmm15 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 mov %r13, 0x40(%rsp) vaesenc %xmm6, %xmm2, %xmm2 vpxor %xmm10, %xmm7, %xmm7 vpclmulqdq $0x10, %xmm12, %xmm13, %xmm13 mov %r12, 0x48(%rsp) vpxor %xmm13, %xmm8, %xmm8 vaesenc %xmm6, %xmm3, %xmm3 vmovdqu 0x20(%rdi), %xmm11 // hash key h^1 vaesenc %xmm6, %xmm4, %xmm4 vmovdqa 0x50(%rsp), %xmm10 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x60(%r8), %xmm6 vaesenc %xmm6, %xmm0, %xmm0 // key 6 vpxor 0x58(%rsp), %xmm10, %xmm13 vpclmulqdq $0x11, %xmm11, %xmm10, %xmm14 // Karatsuba Multiply movbe 0x50(%r14), %r12 vaesenc %xmm6, %xmm1, %xmm1 vpxor %xmm14, %xmm15, %xmm14 vpclmulqdq $0x00, %xmm11, %xmm10, %xmm10 movbe 0x58(%r14), %r13 vaesenc %xmm6, %xmm2, %xmm2 mov %r13, 0x50(%rsp) vpclmulqdq $0x00, %xmm12, %xmm13, %xmm13 mov %r12, 0x58(%rsp) vpxor %xmm10, %xmm7, %xmm10 vaesenc %xmm6, %xmm3, %xmm3 vpxor %xmm13, %xmm8, %xmm13 vaesenc %xmm6, %xmm4, %xmm4 vpxor %xmm10, %xmm14, %xmm11 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x70(%r8), %xmm6 vpxor %xmm11, %xmm13, %xmm13 vaesenc %xmm6, %xmm0, %xmm0 // key 7 vpslldq $8, %xmm13, %xmm11 vaesenc %xmm6, %xmm1, %xmm1 vpsrldq $8, %xmm13, %xmm13 vaesenc %xmm6, %xmm2, %xmm2 vpxor %xmm13, %xmm14, %xmm14 vaesenc %xmm6, %xmm3, %xmm3 vpxor %xmm11, %xmm10, %xmm10 vaesenc %xmm6, %xmm4, %xmm4 vmovdqa 0x30(%r11), %xmm13 // g_poly vaesenc %xmm6, %xmm5, %xmm5 vmovdqu 0x80(%r8), %xmm6 vpalignr $8, %xmm10, %xmm10, %xmm12 // 1st phase of reduction vaesenc %xmm6, %xmm0, %xmm0 // key 8 vpclmulqdq $0x10, %xmm13, %xmm10, %xmm10 vaesenc %xmm6, %xmm1, %xmm1 vpxor %xmm12, %xmm10, %xmm10 vaesenc %xmm6, %xmm2, %xmm2 vpalignr $8, %xmm10, %xmm10, %xmm12 // 2nd phase of reduction vaesenc %xmm6, %xmm3, %xmm3 vpclmulqdq $0x10, %xmm13, %xmm10, %xmm10 vpxor %xmm14, %xmm12, %xmm12 vaesenc %xmm6, %xmm4, %xmm4 vpxor %xmm12, %xmm10, %xmm10 vmovdqu 0x10(%r10), %xmm7 vaesenc %xmm6, %xmm5, %xmm5 vmovdqu (%r10), %xmm6 // ------------------- ghash complete --------------------------------- cmp $12, %r9d jb .Lm96_dec_ending vaesenc %xmm6, %xmm0, %xmm0 // key 9 vaesenc %xmm6, %xmm1, %xmm1 vaesenc %xmm6, %xmm2, %xmm2 vaesenc %xmm6, %xmm3, %xmm3 vaesenc %xmm6, %xmm4, %xmm4 vaesenc %xmm6, %xmm5, %xmm5 vaesenc %xmm7, %xmm0, %xmm0 // key 10 vaesenc %xmm7, %xmm1, %xmm1 vaesenc %xmm7, %xmm2, %xmm2 vaesenc %xmm7, %xmm3, %xmm3 lea 0x20(%r10), %r10 vaesenc %xmm7, %xmm4, %xmm4 vmovdqu (%r10), %xmm6 vaesenc %xmm7, %xmm5, %xmm5 vmovdqu 0x10(%r10), %xmm7 je .Lm96_dec_ending vaesenc %xmm6, %xmm0, %xmm0 // key 11 vaesenc %xmm6, %xmm1, %xmm1 vaesenc %xmm6, %xmm2, %xmm2 vaesenc %xmm6, %xmm3, %xmm3 vaesenc %xmm6, %xmm4, %xmm4 vaesenc %xmm6, %xmm5, %xmm5 vaesenc %xmm7, %xmm0, %xmm0 // key 12 vaesenc %xmm7, %xmm1, %xmm1 vaesenc %xmm7, %xmm2, %xmm2 vaesenc %xmm7, %xmm3, %xmm3 lea 0x20(%r10), %r10 vaesenc %xmm7, %xmm4, %xmm4 vmovdqu (%r10), %xmm6 vaesenc %xmm7, %xmm5, %xmm5 vmovdqu 0x10(%r10), %xmm7 jmp .Lm96_dec_ending .balign 16 .Lm96_dec_ctr_carry: vmovdqa (%r11), %xmm8 vmovdqu (%r8), %xmm7 bswap %r10d vpshufb %xmm8, %xmm0, %xmm0 vmovdqa 0x10(%r11), %xmm6 addl $0x100, %r10d // add carry bit vpaddd %xmm6, %xmm0, %xmm1 vpshufb %xmm8, %xmm0, %xmm0 vpaddd %xmm6, %xmm1, %xmm2 vpxor %xmm7, %xmm0, %xmm0 // key 0 vpshufb %xmm8, %xmm1, %xmm1 vpaddd %xmm6, %xmm2, %xmm3 vpxor %xmm7, %xmm1, %xmm1 vpshufb %xmm8, %xmm2, %xmm2 vpaddd %xmm6, %xmm3, %xmm4 vpxor %xmm7, %xmm2, %xmm2 vpshufb %xmm8, %xmm3, %xmm3 vpaddd %xmm6, %xmm4, %xmm5 vpxor %xmm7, %xmm3, %xmm3 vpshufb %xmm8, %xmm4, %xmm4 bswap %r10d vpshufb %xmm8, %xmm5, %xmm5 vmovdqu 0x10(%r8), %xmm6 jmp .Lm96_dec_aes_cipher .balign 16 .Lm96_dec_ending: vaesenc %xmm6, %xmm0, %xmm0 sub $6, %ecx vpxor (%rsi), %xmm7, %xmm8 // last key xor plaintext vaesenc %xmm6, %xmm1, %xmm1 vpxor 0x10(%rsi), %xmm7, %xmm9 // last key xor plaintext vaesenc %xmm6, %xmm2, %xmm2 vpxor 0x20(%rsi), %xmm7, %xmm11 // last key xor plaintext vaesenc %xmm6, %xmm3, %xmm3 vpxor 0x30(%rsi), %xmm7, %xmm12 // last key xor plaintext vaesenc %xmm6, %xmm4, %xmm4 vpxor 0x40(%rsi), %xmm7, %xmm13 // last key xor plaintext vaesenc %xmm6, %xmm5, %xmm5 vpxor 0x50(%rsi), %xmm7, %xmm14 // last key xor plaintext vaesenclast %xmm8, %xmm0, %xmm0 // last key lea 0x60(%rsi), %rsi vaesenclast %xmm9, %xmm1, %xmm1 vmovdqu %xmm0, (%rdx) // out result text vaesenclast %xmm11, %xmm2, %xmm2 vmovdqu %xmm1, 0x10(%rdx) vaesenclast %xmm12, %xmm3, %xmm3 vmovdqu %xmm2, 0x20(%rdx) vaesenclast %xmm13, %xmm4, %xmm4 vmovdqu %xmm3, 0x30(%rdx) vaesenclast %xmm14, %xmm5, %xmm5 vmovdqu %xmm4, 0x40(%rdx) cmp $6, %ecx vmovdqu %xmm5, 0x50(%rdx) lea 0x60(%rdx), %rdx jae .Lm96_dec_loop vpxor %xmm0, %xmm0, %xmm0 vpshufb (%r11), %xmm10, %xmm10 vmovdqa %xmm0, (%rsp) vmovdqa %xmm0, 0x10(%rsp) vmovdqa %xmm0, 0x20(%rsp) vmovdqa %xmm0, 0x30(%rsp) vmovdqa %xmm0, 0x40(%rsp) vmovdqa %xmm0, 0x50(%rsp) mov %rbp, %rsp vmovdqu %xmm10, 16(%rdi) // out ghash pop %r14 pop %r13 pop %r12 pop %rbp ret .cfi_endproc .size AES_GCM_Decrypt96BlockAsm, .-AES_GCM_Decrypt96BlockAsm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes_gcm_96block_x86_64.S
Unix Assembly
unknown
33,413
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) .text INPUT .req x1 OUT00 .req x2 INLEN .req x3 KEY00 .req x4 IVCTR .req w4 HTABLE .req x5 IVEC0 .req x0 ROUNDS .req w8 COUNT .req x15 COUNTW .req w15 IV_H .req x10 // high 64 bits IV_L .req x11 // lower 64 bits IV_C .req x12 IV_W .req w12 IV_CW .req w9 IV_CX .req x9 CTR0 .req v0 CTR1 .req v1 CTR2 .req v2 CTR3 .req v3 OUT0 .req v4 OUT1 .req v5 OUT2 .req v6 OUT3 .req v7 KEY0 .req v18 KEY1 .req v19 KEY2 .req v20 KEY3 .req v21 KEY4 .req v22 KEY5 .req v23 KEY6 .req v24 KEY7 .req v25 KEY8 .req v26 KEY9 .req v27 KEY10 .req v28 KEY11 .req v29 KEY12 .req v30 KEY13 .req v31 KEND0 .req x13 KEND1 .req x14 HASH0 .req v11 HASH1 .req v12 HASH2 .req v13 HASH3 .req v14 HASH4 .req v15 MULL_C2 .req v13 HASH1_2 .req v12 .macro IN_STP stp x19, x20, [sp, #-112]! stp x21, x22, [sp, #16] stp x23, x24, [sp, #32] stp d8, d9, [sp, #48] stp d10, d11, [sp, #64] stp d12, d13, [sp, #80] stp d14, d15, [sp, #96] .endm .macro OUT_STP ldp x21, x22, [sp, #16] ldp x23, x24, [sp, #32] ldp d8, d9, [sp, #48] ldp d10, d11, [sp, #64] ldp d12, d13, [sp, #80] ldp d14, d15, [sp, #96] ldp x19, x20, [sp], #112 .endm .macro REV_2S REG0, REG1 rev \REG0, \REG0 rev \REG1, \REG1 .endm .macro LOAD_KEY ld1 {KEY0.4s, KEY1.4s}, [KEY00], #32 // load key-0-1 ld1 {KEY2.4s, KEY3.4s}, [KEY00], #32 // load key-2-3 ld1 {KEY4.4s, KEY5.4s}, [KEY00], #32 // load key-4-5 ld1 {KEY6.4s, KEY7.4s}, [KEY00], #32 // load key-6-7 ld1 {KEY8.4s, KEY9.4s}, [KEY00], #32 // load key-8-9 .endm .macro LOAD_GHASH_TABLE ld1 {HASH0.16b}, [HTABLE], #16 // load ghash ld1 {HASH1.2d}, [HTABLE], #16 // load h^1 add HTABLE, HTABLE, #16 ld1 {HASH2.2d}, [HTABLE], #16 // load h^2 ld1 {HASH3.2d}, [HTABLE], #16 // load h^3 add HTABLE, HTABLE, #16 ld1 {HASH4.2d}, [HTABLE] // load h^4 .endm .macro ROUND4 BLOCK0, BLOCK1, BLOCK2, BLOCK3, KEY aese \BLOCK0, \KEY aesmc \BLOCK0, \BLOCK0 aese \BLOCK1, \KEY aesmc \BLOCK1, \BLOCK1 aese \BLOCK2, \KEY aesmc \BLOCK2, \BLOCK2 aese \BLOCK3, \KEY aesmc \BLOCK3, \BLOCK3 .endm .macro ROUND4_END BLOCK0, BLOCK1, BLOCK2, BLOCK3, KEY aese \BLOCK0, \KEY aese \BLOCK1, \KEY aese \BLOCK2, \KEY aese \BLOCK3, \KEY .endm .macro ROUND BLOCK, KEY aese \BLOCK, \KEY aesmc \BLOCK, \BLOCK .endm .macro LOAD_CTR DI, VI rev IV_CW, IV_W fmov \DI, IV_H // set h64 orr IV_CX, IV_L, IV_CX, lsl #32 add IV_W, IV_W, #1 // CTR++ fmov \VI, IV_CX // set l64 .endm .macro BEFORE_ROUND ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // xi ext HASH1.16b, HASH1.16b, HASH1.16b, #8 // h^1 rev IV_W, IV_W // rev_ctr32 ext HASH2.16b, HASH2.16b, HASH2.16b, #8 // h^2 ext HASH3.16b, HASH3.16b, HASH3.16b, #8 // h^3 add IVCTR, IV_W, IVCTR ext HASH4.16b, HASH4.16b, HASH4.16b, #8 // h^4 add IV_W, IV_W, #1 // ctr++ rev64 HASH0.16b, HASH0.16b // orr w11, w11, w11 // trn2 v17.2d, HASH3.2d, HASH4.2d // h4l | h3l LOAD_CTR d1, CTR1.d[1] // CTR bolck 1 trn1 v9.2d, HASH3.2d, HASH4.2d // h4h | h3h LOAD_CTR d2, CTR2.d[1] // CTR bolck 2 trn2 v16.2d, HASH1.2d, HASH2.2d // h2l | h1l LOAD_CTR d3, CTR3.d[1] // CTR bolck 3 trn1 v8.2d, HASH1.2d, HASH2.2d // h2h | h1h .endm .macro FIRST_ROUND ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY0.16b // round 0 ldp x6, x7, [INPUT, #0] // load INPUT 0 #ifdef HITLS_BIG_ENDIAN REV_2S x6, x7 #endif ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY1.16b // round 1 ldp x19, x20, [INPUT, #16] // AES[1] - load plaintext #ifdef HITLS_BIG_ENDIAN REV_2S x19, x20 #endif eor x6, x6, KEND0 // round 10 low eor x7, x7, KEND1 // round 10 high ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY2.16b // round 2 ldp x21, x22, [INPUT, #32] // AES[2] - load plaintext #ifdef HITLS_BIG_ENDIAN REV_2S x21, x22 #endif eor x19, x19, KEND0 // AES[1] - round 10 low eor x20, x20, KEND1 // AES[1] - round 10 high ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY3.16b // round 3 ldp x23, x24, [INPUT, #48] // AES[3] - load plaintext #ifdef HITLS_BIG_ENDIAN REV_2S x23, x24 #endif eor x21, x21, KEND0 // AES[2] - round 10 low eor x22, x22, KEND1 // AES[2] - round 10 high ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY4.16b // round 4 eor x23, x23, KEND0 // AES[3] - round 10 low eor x24, x24, KEND1 // AES[3] - round 10 high ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY5.16b // round 5 fmov d4, x6 // INPUT 0 - mov low fmov d5, x19 // AES[1] - mov low fmov d6, x21 // AES[2] - mov low fmov d7, x23 // AES[3] - mov low ROUND4 CTR2.16b, CTR1.16b, CTR3.16b, CTR0.16b, KEY6.16b // round 6 fmov OUT0.d[1], x7 // AES[0] - mov high fmov OUT1.d[1], x20 // AES[1] - mov high fmov OUT2.d[1], x22 // AES[2] - mov high fmov OUT3.d[1], x24 // AES[3] - mov high .endm .macro STORE_RESULT add INPUT, INPUT, #64 // AES input_ptr update eor OUT0.16b, OUT0.16b, CTR0.16b // AES[0] - result eor OUT1.16b, OUT1.16b, CTR1.16b // AES[1] - result eor OUT2.16b, OUT2.16b, CTR2.16b // AES[2] - result fmov d0, x10 // CTR[0] eor OUT3.16b, OUT3.16b, CTR3.16b // AES[3] - result subs COUNT, COUNT, #1 // count-- fmov CTR0.d[1], x9 // CTR[0]--OK rev w9, IV_W // CTR[1]--Start st1 {OUT0.16b}, [OUT00], #16 // AES[0] - store result orr x9, x11, x9, lsl #32 // CTR[1] st1 {OUT1.16b}, [OUT00], #16 // AES[1] - store result add IV_W, IV_W, #1 // CTR++ fmov d1, x10 // CTR[1] st1 {OUT2.16b}, [OUT00], #16 // AES[2] - store result fmov v1.d[1], x9 // CTR[1]--OK rev w9, IV_W // CTR[2]--Start st1 {OUT3.16b}, [OUT00], #16 // AES[3] - store result orr x9, x11, x9, lsl #32 // CTR[2] add IV_W, IV_W, #1 // CTR++ fmov d2, x10 // CTR2-0 fmov v2.d[1], x9 // CTR[2]--OK rev w9, IV_W // CTR[3]--Start orr x9, x11, x9, lsl #32 // CTR[3] // <= 0 .endm .macro STORE_DEC_RESULT ld1 {OUT0.16b}, [INPUT], #16 ld1 {OUT1.16b}, [INPUT], #16 ld1 {OUT2.16b}, [INPUT], #16 eor CTR0.16b, CTR0.16b, OUT0.16b ld1 {OUT3.16b}, [INPUT], #16 eor CTR1.16b, CTR1.16b, OUT1.16b eor CTR2.16b, CTR2.16b, OUT2.16b mov x6, CTR0.d[0] mov x7, CTR0.d[1] mov x19, CTR1.d[0] mov x20, CTR1.d[1] #ifdef HITLS_BIG_ENDIAN REV_2S x6, x7 REV_2S x19, x20 #endif rev w9, IV_W // CTR[0] eor x6, x6, KEND0 orr x9, x11, x9, lsl #32 // CTR[0] eor x7, x7, KEND1 add IV_W, IV_W, #1 // CTR++ fmov d0, x10 // CTR[0] eor x19, x19, KEND0 fmov CTR0.d[1], x9 // CTR[0]--OK rev w9, IV_W // CTR[1] eor x20, x20, KEND1 orr x9, x11, x9, lsl #32 // CTR[1] subs COUNT, COUNT, #1 // count-- add IV_W, IV_W, #1 // CTR++ fmov d1, x10 // CTR[1] stp x6, x7, [OUT00], #16 fmov v1.d[1], x9 // CTR[1]--OK stp x19, x20, [OUT00], #16 rev w9, IV_W // CTR[2] rev64 OUT0.16b, OUT0.16b add IV_W, IV_W, #1 // CTR++ rev64 OUT1.16b, OUT1.16b orr x9, x11, x9, lsl #32 // CTR[2] .endm .macro GHASH_BLOCK ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 mov d30, OUT1.d[1] // GHASH block 4k+1 - mid mov d31, OUT2.d[1] // GHASH[2] - mid eor OUT0.16b, OUT0.16b, HASH0.16b // PRE 1 tag ^ out pmull2 v28.1q, OUT1.2d, HASH3.2d // GHASH block 4k+1 - high eor v30.8b, v30.8b, OUT1.8b // GHASH block 4k+1 - mid eor v31.8b, v31.8b, OUT2.8b // GHASH[2] - mid mov d8, OUT0.d[1] // GHASH block 4k - mid mov d10, v17.d[1] // GHASH block 4k - mid pmull2 v9.1q, OUT0.2d, HASH4.2d // GHASH block 4k - high pmull HASH0.1q, OUT0.1d, HASH4.1d // GHASH block 4k - low eor v8.8b, v8.8b, OUT0.8b // GHASH block 4k - mid eor v9.16b, v9.16b, v28.16b // GHASH block 4k+1 - high pmull v29.1q, OUT1.1d, HASH3.1d // GHASH block 4k+1 - low pmull v28.1q, OUT2.1d, HASH2.1d // GHASH[2] - low pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid pmull v30.1q, v30.1d, v17.1d // GHASH block 4k+1 - mid ins v31.d[1], v31.d[0] // GHASH[2] - mid pmull2 v8.1q, OUT2.2d, HASH2.2d // GHASH[2] - high eor v10.16b, v10.16b, v30.16b // GHASH block 4k+1 - mid mov d30, OUT3.d[1] // GHASH[0] - mid eor HASH0.16b, HASH0.16b, v29.16b // GHASH block 4k+1 - low eor v30.8b, v30.8b, OUT3.8b // GHASH[0] - mid pmull2 OUT0.1q, OUT3.2d, HASH1.2d // GHASH[0] - high eor v9.16b, v9.16b, v8.16b // GHASH[2] - high pmull2 v31.1q, v31.2d, v16.2d // GHASH[2] - mid pmull v29.1q, OUT3.1d, HASH1.1d // GHASH[0] - low movi v8.8b, #0xc2 pmull v30.1q, v30.1d, v16.1d // GHASH[0] - mid eor HASH0.16b, HASH0.16b, v28.16b // GHASH[2] - low shl d8, d8, #56 // mod_constant eor v9.16b, v9.16b, OUT0.16b // GHASH[0] - high eor v10.16b, v10.16b, v31.16b // GHASH[2] - mid pmull v31.1q, v9.1d, v8.1d // MODULO - top 64b align with mid eor HASH0.16b, HASH0.16b, v29.16b // GHASH[0] - low eor v10.16b, v10.16b, v30.16b // GHASH[0] - mid eor v30.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment eor v10.16b, v10.16b, v30.16b // MODULO - karatsuba tidy up eor v10.16b, v10.16b, v31.16b // MODULO - fold into mid eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid pmull v9.1q, v10.1d, v8.1d // MODULO - mid 64b align with low ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment eor HASH0.16b, HASH0.16b, v9.16b // MODULO - fold into low eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low .endm .macro GHASH_DEC_BLOCK ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // PRE 0 mov x21, v2.d[0] // AES[2] block - mov low mov x22, v2.d[1] // AES[2] block - mov high rev64 v6.16b, v6.16b // GHASH[2] #ifdef HITLS_BIG_ENDIAN REV_2S x21, x22 #endif eor v4.16b, v4.16b, HASH0.16b // PRE 1 eor CTR3.16b, OUT3.16b, CTR3.16b // AES[3] block - result eor x21, x21, KEND0 // AES[2] - round 14 low eor x22, x22, KEND1 // AES[2] - round 14 high pmull2 v9.1q, v4.2d, HASH4.2d // GHASH block 4k - high mov d8, v4.d[1] // GHASH block 4k - mid mov d10, v17.d[1] // GHASH block 4k - mid mov x24, CTR3.d[1] // AES[3] block - mov high pmull HASH0.1q, v4.1d, HASH4.1d // GHASH block 4k - low eor v8.8b, v8.8b, v4.8b // GHASH block 4k - mid pmull2 v4.1q, v5.2d, HASH3.2d // GHASH block 4k+1 - high mov x23, CTR3.d[0] // AES[3] block - mov low rev64 v7.16b, v7.16b // GHASH[0] #ifdef HITLS_BIG_ENDIAN REV_2S x24, x23 #endif pmull v10.1q, v8.1d, v10.1d // GHASH block 4k - mid eor x23, x23, KEND0 // AES[3] block - round 14 low pmull v8.1q, v5.1d, HASH3.1d // GHASH block 4k+1 - low eor x24, x24, KEND1 // AES[3] block - round 14 high eor v9.16b, v9.16b, v4.16b // GHASH block 4k+1 - high mov d4, v5.d[1] // GHASH block 4k+1 - mid eor HASH0.16b, HASH0.16b, v8.16b // GHASH block 4k+1 - low mov d8, v6.d[1] // GHASH[2] - mid eor v4.8b, v4.8b, v5.8b // GHASH block 4k+1 - mid pmull v5.1q, v6.1d, HASH2.1d // GHASH[2] - low eor v8.8b, v8.8b, v6.8b // GHASH[2] - mid eor HASH0.16b, HASH0.16b, v5.16b // GHASH[2] - low pmull v4.1q, v4.1d, v17.1d // GHASH block 4k+1 - mid ins v8.d[1], v8.d[0] // GHASH[2] - mid eor v10.16b, v10.16b, v4.16b // GHASH block 4k+1 - mid pmull2 v4.1q, v6.2d, HASH2.2d // GHASH[2] - high mov d6, v7.d[1] // GHASH[0] - mid pmull2 v8.1q, v8.2d, v16.2d // GHASH[2] - mid eor v9.16b, v9.16b, v4.16b // GHASH[2] - high pmull v4.1q, v7.1d, HASH1.1d // GHASH[0] - low eor v10.16b, v10.16b, v8.16b // GHASH[2] - mid pmull2 v5.1q, v7.2d, HASH1.2d // GHASH[0] - high eor v6.8b, v6.8b, v7.8b // GHASH[0] - mid eor v9.16b, v9.16b, v5.16b // GHASH[0] - high pmull v6.1q, v6.1d, v16.1d // GHASH[0] - mid movi v8.8b, #0xc2 eor HASH0.16b, HASH0.16b, v4.16b // GHASH[0] - low shl d8, d8, #56 // mod_constant eor v10.16b, v10.16b, v6.16b // GHASH[0] - mid pmull v7.1q, v9.1d, v8.1d // MODULO - top 64b align with mid eor v6.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment eor v10.16b, v10.16b, v6.16b // MODULO - karatsuba tidy up eor v10.16b, v10.16b, v7.16b // MODULO - fold into mid eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid pmull v8.1q, v10.1d, v8.1d // MODULO - mid 64b align with low eor HASH0.16b, HASH0.16b, v8.16b // MODULO - fold into low stp x21, x22, [OUT00], #16 // AES[2] block - store result ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment stp x23, x24, [OUT00], #16 // AES[3] block - store result eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low .endm .macro FIRST16_ROUND ROUND CTR0.16b, KEY0.16b ROUND CTR0.16b, KEY1.16b ROUND CTR0.16b, KEY2.16b ROUND CTR0.16b, KEY3.16b ROUND CTR0.16b, KEY4.16b ROUND CTR0.16b, KEY5.16b ROUND CTR0.16b, KEY6.16b ROUND CTR0.16b, KEY7.16b ROUND CTR0.16b, KEY8.16b .endm .macro DEC16_BLOCK ld1 {OUT0.16b}, [INPUT], #16 eor CTR0.16b, CTR0.16b, OUT0.16b // data->out[i] = data->in[i] ^ data->ctr[i]; subs COUNT, COUNT, #1 // COUNT-- mov x6, CTR0.d[0] mov x7, CTR0.d[1] #ifdef HITLS_BIG_ENDIAN REV_2S x6, x7 #endif rev w9, IV_W // CTR[0] eor x6, x6, KEND0 orr x9, x11, x9, lsl #32 // CTR[0] eor x7, x7, KEND1 stp x6, x7, [OUT00], #16 // OUT OK add IV_W, IV_W, #1 // CTR++ fmov d0, x10 // CTR[0] fmov CTR0.d[1], x9 // CTR[0]--OK ext v8.16b, HASH0.16b, HASH0.16b, #8 // prepare final partial tag movi v11.8b, #0 movi v9.8b, #0 movi v10.8b, #0 rev64 v4.16b, OUT0.16b // GHASH final block mov CTR1.16b, CTR0.16b eor v4.16b, v4.16b, v8.16b // feed in partial tag mov d8, v4.d[1] // GHASH final block - mid pmull v6.1q, v4.1d, HASH1_2.1d // GHASH final block - low eor v8.8b, v8.8b, v4.8b // GHASH final block - mid pmull2 v5.1q, v4.2d, HASH1_2.2d // GHASH final block - high pmull v8.1q, v8.1d, v16.1d // GHASH final block - mid eor HASH0.16b, HASH0.16b, v6.16b // GHASH final block - low eor v9.16b, v9.16b, v5.16b // GHASH final block - high eor v10.16b, v10.16b, v8.16b // GHASH final block - mid movi v8.8b, #0xc2 eor v7.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up shl d8, d8, #56 // mod_constant eor v10.16b, v10.16b, v7.16b // MODULO - karatsuba tidy up pmull v5.1q, v9.1d, v8.1d // MODULO - top 64b align with mid ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment eor v10.16b, v10.16b, v5.16b // MODULO - fold into mid eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid pmull v9.1q, v10.1d, v8.1d // MODULO - mid 64b align with low ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment eor HASH0.16b, HASH0.16b, v9.16b // MODULO - fold into low eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low .endm .macro ENC16_BLOCK eor x6, x6, KEND0 // round 10 low eor x7, x7, KEND1 // round 10 high rev w9, IV_W // CTR[0] fmov d4, x6 // INPUT 0 - mov low fmov OUT0.d[1], x7 // AES[0] - mov high orr x9, x11, x9, lsl #32 // CTR[0] add IV_W, IV_W, #1 // CTR++ eor OUT0.16b, OUT0.16b, CTR0.16b // AES[0] - result st1 {OUT0.16b}, [OUT00], #16 // AES[0] - store result fmov d0, x10 // CTR[0] fmov CTR0.d[1], x9 // CTR[0]--OK ext v8.16b, HASH0.16b, HASH0.16b, #8 // prepare final partial tag movi v11.8b, #0 movi v9.8b, #0 movi v10.8b, #0 rev64 v4.16b, OUT0.16b // GHASH final block mov CTR1.16b, CTR0.16b eor v4.16b, v4.16b, v8.16b // feed in partial tag mov d8, v4.d[1] // GHASH final block - mid pmull v6.1q, v4.1d, HASH1_2.1d // GHASH final block - low eor v8.8b, v8.8b, v4.8b // GHASH final block - mid pmull2 v5.1q, v4.2d, HASH1_2.2d // GHASH final block - high pmull v8.1q, v8.1d, v16.1d // GHASH final block - mid eor HASH0.16b, HASH0.16b, v6.16b // GHASH final block - low eor v9.16b, v9.16b, v5.16b // GHASH final block - high eor v10.16b, v10.16b, v8.16b // GHASH final block - mid movi v8.8b, #0xc2 eor v7.16b, HASH0.16b, v9.16b // MODULO - karatsuba tidy up shl d8, d8, #56 // mod_constant eor v10.16b, v10.16b, v7.16b // MODULO - karatsuba tidy up pmull v5.1q, v9.1d, v8.1d // MODULO - top 64b align with mid ext v9.16b, v9.16b, v9.16b, #8 // MODULO - other top alignment eor v10.16b, v10.16b, v5.16b // MODULO - fold into mid eor v10.16b, v10.16b, v9.16b // MODULO - fold into mid pmull v9.1q, v10.1d, v8.1d // MODULO - mid 64b align with low ext v10.16b, v10.16b, v10.16b, #8 // MODULO - other mid alignment eor HASH0.16b, HASH0.16b, v9.16b // MODULO - fold into low eor HASH0.16b, HASH0.16b, v10.16b // MODULO - fold into low .endm .macro BEFORE16_ROUND ext HASH0.16b, HASH0.16b, HASH0.16b, #8 // xi ext HASH1.16b, HASH1.16b, HASH1.16b, #8 // h^1 // rev_ctr32 ext HASH2.16b, HASH2.16b, HASH2.16b, #8 // h^2 ldp KEND0, KEND1, [KEY00] // load key-10 #ifdef HITLS_BIG_ENDIAN ror KEND0, KEND0, #32 ror KEND1, KEND1, #32 #endif ldp IV_H, IV_L, [IVEC0] // load IV #ifdef HITLS_BIG_ENDIAN rev IV_H, IV_H rev IV_L, IV_L #endif lsr IV_C, IV_L, #32 ld1 {CTR0.16b}, [IVEC0] // CTR[0] rev IV_W, IV_W // rev_ctr32 trn1 v8.2d, HASH1.2d, HASH2.2d // h2h | h1h trn2 v16.2d, HASH1.2d, HASH2.2d // h2l | h1l orr w11, w11, w11 // rev64 HASH0.16b, HASH0.16b // add IV_W, IV_W, #1 // ctr++ eor v16.16b, v16.16b, v8.16b //h2k | h1k .endm #endif
2301_79861745/bench_create
crypto/modes/src/asm/aes_gcm_common_aarch64.S
Unix Assembly
unknown
25,230
/* * 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_GCM #include "crypt_arm.h" .arch armv8-a+crypto .text INPUT_H .req x0 OUT_TB .req x1 MULL_C2 .req v31 MULL_H .req v24 OUT_H0 .req v25 OUT_H1_2 .req v26 OUT_H2 .req v27 OUT_H3 .req v28 OUT_H3_4 .req v29 OUT_H4 .req v30 INPUT_X .req v0 OUT_X .req v23 .macro GEN_H ext v1.16b, MULL_H.16b, MULL_H.16b, #8 // ushr v2.2d, MULL_C2.2d, #63 dup MULL_H.4s, MULL_H.s[1] ext v3.16b, v2.16b, MULL_C2.16b, #8 // t0 = 0xc2....01 ushr v2.2d, v1.2d, #63 sshr MULL_H.4s, MULL_H.4s, #31 // broadcast carry bit and v2.16b, v2.16b, v3.16b shl v1.2d, v1.2d, #1 ext v2.16b, v2.16b, v2.16b, #8 and v3.16b, v3.16b, MULL_H.16b orr v1.16b, v1.16b, v2.16b // H<<<=1 eor OUT_H0.16b, v1.16b, v3.16b // twisted H st1 {OUT_H0.2d}, [OUT_TB], #16 // store H0 .endm .macro GEN_H2 //(ah + al) * (ah + al) = ah * ah + 2 * ah * al + al * al ext v10.16b, OUT_H0.16b, OUT_H0.16b, #8 // A pmull v12.1q, OUT_H0.1d, OUT_H0.1d // aL * aL eor v10.16b, v10.16b, OUT_H0.16b // A + h pmull2 v11.1q, OUT_H0.2d, OUT_H0.2d // ah * ah pmull v13.1q, v10.1d, v10.1d // (A + h) * (A + h) ext v14.16b, v12.16b, v11.16b, #8 // B eor v15.16b, v12.16b, v11.16b // aL * aL + ah * ah eor v13.16b, v13.16b, v14.16b // ah * al + B eor v13.16b, v13.16b, v15.16b // pmull v15.1q, v12.1d, MULL_C2.1d // 1st phase ins v11.d[0], v13.d[1] ins v13.d[1], v12.d[0] eor v12.16b, v13.16b, v15.16b ext v15.16b, v12.16b, v12.16b, #8 pmull v12.1q, v12.1d, MULL_C2.1d eor v15.16b, v15.16b, v11.16b eor OUT_H2.16b, v12.16b, v15.16b // H^2 ext v16.16b, OUT_H2.16b, OUT_H2.16b, #8 eor v16.16b, v16.16b, OUT_H2.16b ext OUT_H1_2.16b, v10.16b, v16.16b, #8 st1 {OUT_H1_2.2d, OUT_H2.2d}, [OUT_TB], #32 // store H^2h H^2 .endm .macro GEN_H3_4 //calculate H^3 and H^4 pmull v0.1q, OUT_H0.1d, OUT_H2.1d pmull v1.1q, OUT_H2.1d, OUT_H2.1d pmull2 v2.1q, OUT_H0.2d, OUT_H2.2d pmull2 v3.1q, OUT_H2.2d, OUT_H2.2d pmull v4.1q, v10.1d, v16.1d pmull v5.1q, v16.1d, v16.1d ext v6.16b, v0.16b, v2.16b, #8 // Karatsuba post-processing ext v7.16b, v1.16b, v3.16b, #8 eor v8.16b, v0.16b, v2.16b eor v4.16b, v4.16b, v6.16b eor v9.16b, v1.16b, v3.16b eor v5.16b, v5.16b, v7.16b eor v4.16b, v4.16b, v8.16b pmull v8.1q, v0.1d, MULL_C2.1d // 1st phase eor v5.16b, v5.16b, v9.16b pmull v9.1q, v1.1d, MULL_C2.1d ins v2.d[0], v4.d[1] ins v3.d[0], v5.d[1] ins v4.d[1], v0.d[0] ins v5.d[1], v1.d[0] eor v0.16b, v4.16b, v8.16b eor v1.16b, v5.16b, v9.16b ext v8.16b, v0.16b, v0.16b,#8 // 2nd phase ext v9.16b, v1.16b, v1.16b,#8 pmull v0.1q, v0.1d, MULL_C2.1d pmull v1.1q, v1.1d, MULL_C2.1d eor v8.16b, v8.16b, v2.16b eor v9.16b, v9.16b, v3.16b eor OUT_H3.16b, v0.16b, v8.16b // H^3 eor OUT_H4.16b, v1.16b, v9.16b // H^4 ext v20.16b, OUT_H3.16b, OUT_H3.16b, #8 // Karatsuba pre-processing ext v21.16b, OUT_H4.16b, OUT_H4.16b, #8 eor v20.16b, v20.16b, OUT_H4.16b eor v21.16b, v21.16b, OUT_H4.16b ext OUT_H3_4.16b, v20.16b, v21.16b, #8 // h st1 {OUT_H3.2d, OUT_H3_4.2d, OUT_H4.2d}, [OUT_TB] // store h^3 h^3+h^4 h^4 .endm .globl GcmTableGen4bit .type GcmTableGen4bit, %function .align 4 GcmTableGen4bit: AARCH64_PACIASP stp x29, x30, [sp, #-0x50]! add x29, sp, #0 stp d8, d9, [sp, #0x10] stp d10, d11, [sp, #0x20] stp d12, d13, [sp, #0x30] stp d14, d15, [sp, #0x40] movi MULL_C2.16b, #0xe1 // set 0xc2 ld1 {MULL_H.16b}, [INPUT_H] // load input H shl MULL_C2.2d, MULL_C2.2d, #57 // 0xc20000000000000 rev64 MULL_H.16b, MULL_H.16b GEN_H GEN_H2 GEN_H3_4 ldp d8, d9, [sp, #0x10] ldp d10, d11, [sp, #0x20] ldp d12, d13, [sp, #0x30] ldp d14, d15, [sp, #0x40] ldp x29, x30, [sp], #0x50 AARCH64_AUTIASP ret .size GcmTableGen4bit,.-GcmTableGen4bit // void GcmHashMultiBlock(uint8_t t[GCM_BLOCKSIZE], const MODES_GCM_GF128 hTable[16], const uint8_t *in, uint32_t inLen) .globl GcmHashMultiBlock .type GcmHashMultiBlock, %function .align 4 GcmHashMultiBlock: AARCH64_PACIASP lsr x3, x3, #4 // Divided by 64 16*2*2 ld1 {INPUT_X.16b}, [INPUT_H] // load Xi movi MULL_C2.16b, #0xe1 // set 0xc2 ld1 {OUT_H0.2d, OUT_H1_2.2d}, [OUT_TB] // load twisted H, ... shl MULL_C2.2d, MULL_C2.2d, #57 // 0xc20000000000000 .LGcmLoop: subs x3, x3, #1 ld1 {v20.16b}, [x2], #16 // load in eor INPUT_X.16b, INPUT_X.16b, v20.16b // t ^ in rev64 INPUT_X.16b, INPUT_X.16b // Vectors are reversed in doublewords ext v3.16b, INPUT_X.16b, INPUT_X.16b, #8 // {Xi.hi, Xi.lo} => {Xi.lo, Xi.hi} pmull OUT_X.1q, OUT_H0.1d, v3.1d // (H.lo * Xi.hi) eor INPUT_X.16b, INPUT_X.16b, v3.16b // (Xi.lo + Xi.hi) pmull2 v2.1q, OUT_H0.2d, v3.2d // (H.hi * Xi.lo) pmull v1.1q, OUT_H1_2.1d, INPUT_X.1d // (H.lo + H.hi) * (Xi.lo + Xi.hi) ext v7.16b, OUT_X.16b, v2.16b, #8 // M eor v6.16b, OUT_X.16b, v2.16b // (H.lo * Xi.hi) + (H.hi * Xi.lo) eor v1.16b, v1.16b, v7.16b // (H.lo + H.hi) * (Xi.lo + Xi.hi) + M eor v1.16b, v1.16b, v6.16b // (H.lo * Xi.hi) + (H.hi * Xi.lo) + (H.lo + H.hi) * (Xi.lo + Xi.hi) + M pmull v18.1q, OUT_X.1d, MULL_C2.1d // 1st phase of reduction ins v2.d[0], v1.d[1] ins v1.d[1], OUT_X.d[0] eor OUT_X.16b, v1.16b, v18.16b ext v18.16b, OUT_X.16b, OUT_X.16b, #8 // 2nd phase of reduction pmull OUT_X.1q, OUT_X.1d, MULL_C2.1d eor v18.16b, v18.16b, v2.16b eor OUT_X.16b, OUT_X.16b, v18.16b rev64 OUT_X.16b, OUT_X.16b ext INPUT_X.16b, OUT_X.16b, OUT_X.16b, #8 b.gt .LGcmLoop // > 0 st1 {INPUT_X.16b}, [INPUT_H] // write out Xi .LhashEnd: AARCH64_AUTIASP ret .size GcmHashMultiBlock,.-GcmHashMultiBlock #endif
2301_79861745/bench_create
crypto/modes/src/asm/ghash_armv8.S
Unix Assembly
unknown
7,061
/* * 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_GCM .file "ghash_x86_64.S" .text .set INL, %xmm11 .set INH, %xmm12 .set INM, %xmm13 .set HKEY3, %xmm14 .set HKEY4, %xmm15 .set INPUT_XI, %rdi .set HTABLE, %rsi .set INPUT_IN, %rdx .set LEN, %rcx .set XI_L, %xmm0 .set XI_H, %xmm1 .set HKEY, %xmm2 .set IN_L, %xmm3 .set IN_H, %xmm4 .set IN_M, %xmm5 .set HKEY2, %xmm6 .set HKEY1_2, %xmm7 .set TEMP1, %xmm8 .set TEMP2, %xmm9 .set MASK, %xmm10 .balign 16 g_bswapMask: .byte 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 .size g_bswapMask, .-g_bswapMask .balign 16 g_polynomial: .byte 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xc2 .size g_polynomial, .-g_polynomial .balign 16 g_64swapMask: .byte 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8 .size g_64swapMask, .-g_64swapMask .balign 16 g_poly: .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2 .size g_poly, .-g_poly /** * Macro description: one block * H (128bit * 128bit) * Input registers: xl, hKey, hKey12 * Change registers: temp1 and temp2 * Result register: xh, xl */ .macro GHASH_MUL128X128 xh, xl, hKey, hKey12, temp1, temp2 vpshufd $0x4e, \xl, \temp1 vpclmulqdq $0x11, \hKey, \xl, \xh vpxor \xl, \temp1, \temp1 vpclmulqdq $0x00, \hKey, \xl, \xl vpxor \xl, \xh, \temp2 vpclmulqdq $0x00, \hKey12, \temp1, \temp1 vpxor \temp2, \temp1, \temp1 vpslldq $8, \temp1, \temp2 vpsrldq $8, \temp1, \temp1 vpxor \temp1, \xh, \xh vpxor \temp2, \xl, \xl .endm /** * Macro description: 256-bit large number reduction modulo g(x) * Input register: xh, xl * Change registers: temp1 and temp2 * Result register: xl */ .macro REDUCTION_256BIT xh, xl, temp1, temp2, reducMask vmovdqa \reducMask(%rip), \temp1 // g_poly vpalignr $8, \xl, \xl, \temp2 // 1st phase of reduction vpclmulqdq $0x10, \temp1, \xl, \xl vpxor \temp2, \xl, \xl vpalignr $8, \xl, \xl, \temp2 // 2nd phase of reduction vpclmulqdq $0x10, \temp1, \xl, \xl vpxor \xh, \temp2, \temp2 vpxor \temp2, \xl, \xl .endm /** * Function description: x86_64 hTable pre-computation table implementation (H has been transformed) * Function prototype: void GcmTableGen4bit(uint8_t key[GCM_BLOCKSIZE], MODES_GCM_GF128 hTable[16]); * Input register: * rdi: uint8_t key[GCM_BLOCKSIZE] * rsi: MODES_GCM_GF128 hTable[16] * Change register: xmm0-xmm15 * Function/Macro Call: * GHASH_MUL128X128 * REDUCTION_256BIT */ .align 32 .globl GcmTableGen4bit .type GcmTableGen4bit, %function GcmTableGen4bit: .cfi_startproc vmovdqu (INPUT_XI), HKEY vpshufb g_64swapMask(%rip), HKEY, HKEY vpshufd $0x4e, HKEY, IN_L vpshufd $0x55, HKEY, HKEY // broadcast carry bit vmovdqa g_polynomial(%rip), IN_H vpsrlq $63, IN_L, IN_M vpxor MASK, MASK, MASK vpcmpgtd HKEY, MASK, HKEY vpand IN_H, IN_M, IN_M vpsllq $1, IN_L, IN_L vpshufd $0x4e, IN_M, IN_M vpand HKEY, IN_H, IN_H vpor IN_M, IN_L, IN_L // H<<<=1 vpxor IN_L, IN_H, HKEY // twisted H vmovdqu HKEY, (HTABLE) // store in H[0] vpshufd $0x4e, HKEY, HKEY1_2 vpxor HKEY, HKEY1_2, HKEY1_2 vmovdqa HKEY, XI_L /* xh, xl, hKey, hKey12, temp1, temp2 */ GHASH_MUL128X128 XI_H, XI_L, HKEY, HKEY1_2, TEMP1, TEMP2 // calculate H^2 /* xh, xl, temp1, temp2, reducMask */ REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly vmovdqa XI_L, HKEY2 GHASH_MUL128X128 XI_H, XI_L, HKEY, HKEY1_2, TEMP1, TEMP2 // calculate H^3 REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly vmovdqa XI_L, HKEY3 GHASH_MUL128X128 XI_H, XI_L, HKEY, HKEY1_2, TEMP1, TEMP2 // calculate H^4 REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly vmovdqa XI_L, HKEY4 vmovdqu HKEY2, 0x10(HTABLE) // store H^2 in H[1] vmovdqu HKEY3, 0x30(HTABLE) // store H^3 in H[3] vmovdqu HKEY4, 0x40(HTABLE) // store H^4 in H[4] vpshufd $0x4e, HKEY2, TEMP1 vpxor HKEY2, TEMP1, TEMP1 vshufps $0x44, TEMP1, HKEY1_2, HKEY1_2 vmovdqu HKEY1_2, 0x20(HTABLE) // store [H^2.h + H^2.l, H.h + H.l] in H[2] vpshufd $0x4e, HKEY3, TEMP1 vpshufd $0x4e, HKEY4, TEMP2 vpxor HKEY3, TEMP1, TEMP1 vpxor HKEY4, TEMP2, TEMP2 vshufps $0x44, TEMP2, TEMP1, HKEY1_2 vmovdqu HKEY1_2, 0x50(HTABLE) // store [H^4.h + H^4.l, H^3.h + H^3.l] in H[5] vmovdqu 0x20(HTABLE), HKEY1_2 // reload [H^2.h + H^2.l, H.h + H.l] GHASH_MUL128X128 XI_H, XI_L, HKEY, HKEY1_2, TEMP1, TEMP2 // calculate H^5, for aes-gcm REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly vmovdqa XI_L, HKEY3 GHASH_MUL128X128 XI_H, XI_L, HKEY, HKEY1_2, TEMP1, TEMP2 // calculate H^6, for aes-gcm REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly vmovdqa XI_L, HKEY4 vmovdqu HKEY3, 0x60(HTABLE) // store H^5 in H[6] vmovdqu HKEY4, 0x70(HTABLE) // store H^6 in H[7] vpshufd $0x4e, HKEY3, TEMP1 vpshufd $0x4e, HKEY4, TEMP2 vpxor HKEY3, TEMP1, TEMP1 vpxor HKEY4, TEMP2, TEMP2 vshufps $0x44, TEMP2, TEMP1, HKEY1_2 vmovdqu HKEY1_2, 0x80(HTABLE) // store [H^6.h + H^6.l, H^5.h + H^5.l] in H[8] vpxor HKEY, HKEY, HKEY // clear hTable vpxor HKEY1_2, HKEY1_2, HKEY1_2 vpxor HKEY2, HKEY2, HKEY2 vpxor HKEY3, HKEY3, HKEY3 vpxor HKEY4, HKEY4, HKEY4 ret .cfi_endproc .size GcmTableGen4bit, .-GcmTableGen4bit /** * Function description: x86_64 ghash assembly acceleration implementation * Function prototype: void GcmHashMultiBlock(uint8_t t[GCM_BLOCKSIZE], const MODES_GCM_GF128 hTable[16], * const uint8_t *in, uint32_t inLen); * Input register: * rdi: uint8_t t[GCM_BLOCKSIZE] * rsi: const MODES_GCM_GF128 hTable[16] * rdx: const uint8_t *in * rcx: uint32_t inLen * Change register: xmm0-xmm15 * Function/Macro Call: * GHASH_MUL128X128 * REDUCTION_256BIT // reduction modulo g(x) */ .align 32 .globl GcmHashMultiBlock .type GcmHashMultiBlock, %function GcmHashMultiBlock: .cfi_startproc vmovdqa g_bswapMask(%rip), MASK vmovdqu (INPUT_XI), XI_L vmovdqu (HTABLE), HKEY vmovdqu 0x20(HTABLE), HKEY1_2 vpshufb MASK, XI_L, XI_L cmp $0x10, LEN je .Lremain_1block vmovdqu 0x10(HTABLE), HKEY2 cmp $0x40, LEN jae .Lmul_4blocks jmp .Lremain_Least_2blocks .align 32 .Lmul_4blocks: subq $0x40, LEN vmovdqu 0x30(INPUT_IN), IN_L // load In_3, In_2 vmovdqu 0x20(INPUT_IN), INL vpshufb MASK, IN_L, IN_L vpshufb MASK, INL, INL vmovdqa IN_L, IN_H // H * In_3 vpshufd $0x4e, IN_L, IN_M vpxor IN_L, IN_M, IN_M vpclmulqdq $0x00, HKEY, IN_L, IN_L vpclmulqdq $0x11, HKEY, IN_H, IN_H vpclmulqdq $0x00, HKEY1_2, IN_M, IN_M vmovdqa INL, INH // H^2 * In_2 vpshufd $0x4e, INL, INM vpxor INL, INM, INM vpclmulqdq $0x00, HKEY2, INL, INL vpclmulqdq $0x11, HKEY2, INH, INH vpclmulqdq $0x10, HKEY1_2, INM, INM vxorps INL, IN_L, IN_L // H * In_3 + H^2 * In_2 vxorps INH, IN_H, IN_H vxorps INM, IN_M, IN_M vmovdqu 0x30(HTABLE), HKEY3 vmovdqu 0x40(HTABLE), HKEY4 vmovdqu 0x50(HTABLE), HKEY1_2 vmovdqu 0x10(INPUT_IN), INL // load In_1, In_0 vmovdqu (INPUT_IN), TEMP1 vpshufb MASK, INL, INL vpshufb MASK, TEMP1, TEMP1 vmovdqa INL, INH // H^3 * In_1 vpshufd $0x4e, INL, INM vpxor INL, INM, INM vpclmulqdq $0x00, HKEY3, INL, INL vpclmulqdq $0x11, HKEY3, INH, INH vpclmulqdq $0x00, HKEY1_2, INM, INM vxorps INL, IN_L, IN_L // H * In_3 + H^2 * In_2 + H^3 * In_1 vxorps INH, IN_H, IN_H vxorps INM, IN_M, IN_M vpxor TEMP1, XI_L, XI_L // (In_1 + Xi) vmovdqa XI_L, XI_H vpshufd $0x4e, XI_L, TEMP1 vpxor XI_L, TEMP1, TEMP1 vpclmulqdq $0x00, HKEY4, XI_L, XI_L // H^4 * (In_1 + Xi) vpclmulqdq $0x11, HKEY4, XI_H, XI_H vpclmulqdq $0x10, HKEY1_2, TEMP1, TEMP1 vxorps IN_L, XI_L, XI_L // H * In_3 + H^2 * In_2 + H^3 * In_1 + H^4 * (In_1 + Xi) vxorps IN_H, XI_H, XI_H vxorps IN_M, TEMP1, TEMP1 vpxor XI_L, TEMP1, TEMP1 vpxor XI_H, TEMP1, TEMP1 vmovdqa TEMP1, TEMP2 vpslldq $8, TEMP1, TEMP1 vpsrldq $8, TEMP2, TEMP2 vpxor TEMP1, XI_L, XI_L vpxor TEMP2, XI_H, XI_H REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly cmp $0x00, LEN jz .Lend // finshed all blocks leaq 0x40(INPUT_IN), INPUT_IN vmovdqu 0x20(HTABLE), HKEY1_2 cmp $0x40, LEN jae .Lmul_4blocks cmp $0x20, LEN jae .Lremain_Least_2blocks jmp .Lremain_1block .align 32 .Lremain_Least_2blocks: subq $0x20, LEN vmovdqu 0x10(INPUT_IN), IN_L // loda (4 * i) + 1 or 2 block vmovdqu (INPUT_IN), TEMP1 vpshufb MASK, IN_L, IN_L vpshufb MASK, TEMP1, TEMP1 vpxor TEMP1, XI_L, XI_L vmovdqa IN_L, IN_H vpshufd $0x4e, IN_L, IN_M vpxor IN_L, IN_M, IN_M vpclmulqdq $0x00, HKEY, IN_L, IN_L vpclmulqdq $0x11, HKEY, IN_H, IN_H vpclmulqdq $0x00, HKEY1_2, IN_M, IN_M vmovdqa XI_L, XI_H vpshufd $0x4e, XI_L, TEMP1 vpxor XI_L, TEMP1, TEMP1 vpclmulqdq $0x00, HKEY2, XI_L, XI_L vpclmulqdq $0x11, HKEY2, XI_H, XI_H vpclmulqdq $0x10, HKEY1_2, TEMP1, TEMP1 vxorps IN_L, XI_L, XI_L vxorps IN_H, XI_H, XI_H vxorps IN_M, TEMP1, TEMP1 vpxor XI_L, TEMP1, TEMP1 vpxor XI_H, TEMP1, TEMP1 vmovdqa TEMP1, TEMP2 vpslldq $8, TEMP1, TEMP1 vpsrldq $8, TEMP2, TEMP2 vpxor TEMP1, XI_L, XI_L vpxor TEMP2, XI_H, XI_H REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly cmp $0x00, LEN jz .Lend leaq 0x20(INPUT_IN), INPUT_IN .align 32 .Lremain_1block: subq $0x10, LEN vmovdqu (INPUT_IN), TEMP1 vpshufb MASK, TEMP1, TEMP1 vpxor TEMP1, XI_L, XI_L GHASH_MUL128X128 XI_H, XI_L, HKEY, HKEY1_2, TEMP1, TEMP2 REDUCTION_256BIT XI_H, XI_L, TEMP1, TEMP2, g_poly .Lend: vpshufb MASK, XI_L, XI_L vmovdqu XI_L, (INPUT_XI) vpxor HKEY, HKEY, HKEY // clear hTable vpxor HKEY1_2, HKEY1_2, HKEY1_2 vpxor HKEY2, HKEY2, HKEY2 vpxor HKEY3, HKEY3, HKEY3 vpxor HKEY4, HKEY4, HKEY4 ret .cfi_endproc .size GcmHashMultiBlock, .-GcmHashMultiBlock #endif
2301_79861745/bench_create
crypto/modes/src/asm/ghash_x86_64.S
Unix Assembly
unknown
11,593
/* * 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_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "crypt_arm.h" .arch armv8-a /** * c structure:Poly1305Ctx */ .set CTX_acc, 0 .set CTX_r, 24 .set CTX_s, 40 .set CTX_table, 56 .set CTX_data, 200 .set CTX_lastLen, 216 .set CTX_flag, 220 .set CTX_size, 224 .equ FLAG_BASE2_26, 1 /* 104 78 52 26 0 * out4 out3 out2 out1 out0 * in0[63:52] in0[51:26] in0[25:0] * in1[63:40] in1[39:14] in1[13:0]<<12 * in2[39:0]<<24 */ /** * Macro description: converts the large number format. Three pieces of base 2^64 data are transferred, * and five pieces of base 2^26 data are transferred. * Input register: * in0: digits 0 to 63 of the large number in the original format * in1:64 to 127 characters in the original format * in2:128 or more digits of the large number in the original format * Modified register: None * Output register: * out0: 0 to 25 bits of the converted large number * out1:26 to 51 digits of the converted large number * out2:52 to 77 bits of the converted large number * out3:78 to 103 bits of the converted large number * out4:104 or more digits of the converted large number * Function/Macro Call: None * Restriction: Note that the valid bits of in2 cannot exceed 40 bits. * Otherwise, data will be lost. */ .macro CONVERT_64TO26 out0 out1 out2 out3 out4 in0 in1 in2 and \out0, \in0, #0x03ffffff ubfx \out1, \in0, #26, #26 extr \out2, \in1, \in0, #52 and \out2, \out2, #0x03ffffff ubfx \out3, \in1, #14, #26 extr \out4, \in2, \in1, #40 .endm /* 128 64 0 * out2 out1 out0 * in0 * (in1>>38) in1<<26 * in2>>12 in2<<52 * (in3>>50) in3<<14 * in4>>24 in4<<40 */ /** * Macro description: converts the large number format. Five pieces of base2^26 data are transferred, * and three pieces of base2^64 data are transferred. * Input register: * in0: large data block 0 in the original format * in1: large data block 1 in the original format * in2: large data block 2 in the original format * in3: large data block 3 in the original format * in4: large data block 4 in the original format * Modified register: None * Output register: * out0: bits 0 to 63 of the converted large number * out1: 64-127 bits of the converted large number * out2: 128 or more digits of the converted large number * Function/Macro Call: None * Restriction: Ensure that the valid bits in0-in4 of the input data do not exceed 38 bits. Otherwise, * data will be lost. */ .macro CONVERT_26TO64 out0 out1 out2 in0 in1 in2 in3 in4 add \out0, \in0, \in1, lsl#26 adds \out0, \out0, \in2, lsl#52 lsr \out1, \in2, #12 add \out1, \out1, \in3, lsl#14 adc \out1, \out1, xzr adds \out1, \out1, \in4, lsl#40 lsr \out2, \in4, #24 adc \out2, \out2, xzr .endm /* register | t_0 t_1 | t_2 | * bits | 128 bits | 64 bits | * 1 | r0*a0(lo) r0*a1(lo) | r0*a2(lo) | * 2 | r0*a0(hi) | r0*a1(hi) | * 3 | s1*a1(lo) r1*a0(lo) | | * 4 | s1*a1(hi) | r1*a0(hi) | * 5 | s1*a2(lo) | | */ /** * Macro description: Multiply large numbers and perform modulo * (a0|a1|a2) = (a0|a1|a2) * (r0|r1) mod P * Input register: * a_0: digits 0 to 63 of the large number a * a_1: 64 to 127 digits of the major number a * a_2: 128 or more digits of the major number a * r_0: bits 0 to 63 of the large number r * r_1: 64-127 bits of the large number r * s_1: 5/4 times the large number r_1 * Change register: x11-x15 * Output register: * a_0: bits 0 to 63 of the multiplication result * a_1: 64-127 bits of the multiplication result * a_2: 128 or more bits of the multiplication result * Function/Macro Call: None * Restriction: The relationship between s1 and r1 is s1 = r1 + r1 >> 2. */ .macro POLY1305_MOD_MUL a_0, a_1, a_2, r_0, r_1, s_1 /* 1 */ mul x11, \r_0, \a_0 mul x12, \r_0, \a_1 mul x13, \r_0, \a_2 /* 2 */ umulh x14, \r_0, \a_0 umulh x15, \r_0, \a_1 adds x12, x12, x14 adc x13, x13, x15 /* 3 */ mul x14, \s_1, \a_1 mul x15, \r_1, \a_0 adds x11, x11, x14 adcs x12, x12, x15 adc x13, x13, xzr /* 4 */ umulh x14, \s_1, \a_1 umulh x15, \r_1, \a_0 adds x12, x12, x14 adc x13, x13, x15 /* 5 */ mul x15, \s_1, \a_2 adds x12, x12, x15 adc x13, x13, xzr /* Split x13 and add 5/4 of the high-order part to x11. */ bic x15, x13, #3 and x13, x13, #3 add x15, x15, x15, lsr#2 adds \a_0, x11, x15 adcs \a_1, x12, xzr adc \a_2, x13, xzr .endm /** * Macro description: Convert the content of a large number (r_0|r_1|r_2) into the format of 2 ^ 26, * and then fill the memory pointed to by ptr at intervals. * Input register: * r_0: digits 0 to 63 of a large number * r_1: indicates the 64th to 127th digits of the large number. * r_2: 128th to 191th digits of a large number * ptr: start address of the memory to be filled * Change register: x11-x15 * Output register: None * Function/Macro call: TRANSFER_64TO26 * */ .macro Fill_TABLE r_0, r_1, r_2, ptr /* base 2^64 -> base 2^26 */ /* r_0 r_1 r_2 --> x11 x12 x13 x14 x15 */ CONVERT_64TO26 x11, x12, x13, x14, x15, \r_0, \r_1, \r_2 /* Stores the converted value. */ str w11, [\ptr, #16*0] str w12, [\ptr, #16*1] str w13, [\ptr, #16*2] str w14, [\ptr, #16*3] str w15, [\ptr, #16*4] /* Multiply 5 times and continue to store */ add w12, w12, w12, lsl#2 add w13, w13, w13, lsl#2 add w14, w14, w14, lsl#2 add w15, w15, w15, lsl#2 str w12, [\ptr, #16*5] str w13, [\ptr, #16*6] str w14, [\ptr, #16*7] str w15, [\ptr, #16*8] .endm /** * Function description: This function is used to initialize the pre-computation table. * Function prototype: void Poly1305InitForAsm(Poly1305Ctx *ctx); * Input register: * x0: address of the context structure * Change register x0 and x5-x15. * Output register: None * Function/Macro Call: Poly1305_MOD_MUL Fill_TABLE */ .text .balign 64 .global Poly1305InitForAsm .type Poly1305InitForAsm, %function Poly1305InitForAsm: AARCH64_PACIASP stp x29, x30, [sp, #-16]! add x29, sp, #0 /* Clearing the member flag */ str wzr, [x0, #CTX_flag] /* Initialize the r table. */ ldp x8, x9, [x0, #CTX_r] #ifdef HITLS_BIG_ENDIAN /* The r value needs to be reversed in the big-endian case. */ ror x8, x8, #32 ror x9, x9, #32 #endif add x10, x9, x9, lsr#2 /* padding r^1 */ add x0, x0, #CTX_table + 12 mov x5, x8 mov x6, x9 mov x7, xzr Fill_TABLE x5, x6, x7, x0 /* Calculate and populate r^2 */ sub x0, x0, #4 POLY1305_MOD_MUL x5, x6, x7, x8, x9, x10 Fill_TABLE x5, x6, x7, x0 /* Calculate and populate r^3 */ sub x0, x0, #4 POLY1305_MOD_MUL x5, x6, x7, x8, x9, x10 Fill_TABLE x5, x6, x7, x0 /* Calculate and populate r^4 */ sub x0, x0, #4 POLY1305_MOD_MUL x5, x6, x7, x8, x9, x10 Fill_TABLE x5, x6, x7, x0 eor x5, x5, x5 eor x6, x6, x6 eor x7, x7, x7 eor x8, x8, x8 eor x9, x9, x9 eor x10, x10, x10 ldp x29, x30, [sp], #16 AARCH64_AUTIASP ret .size Poly1305InitForAsm, .-Poly1305InitForAsm /** * Function description: Outputs the final result value to the specified memory. * Function prototype: void Poly1305Last(Poly1305Ctx *ctx, uint8_t mac[POLY1305_TAGSIZE]); * Input register: * x0: address of the context structure * x1: pointer to the output buffer * Change register: x3-x15 * Output register: None * Function/Macro Call: Poly1305LastNeon */ .text .balign 64 .global Poly1305Last .type Poly1305Last, %function Poly1305Last: AARCH64_PACIASP ldr w15, [x0, #CTX_flag] and w15, w15, #FLAG_BASE2_26 cbnz w15, Poly1305LastNeon ldp x3, x4, [x0, #CTX_acc] ldr x5, [x0, #CTX_acc + 16] ldp x12, x13, [x0, #CTX_s] adds x9, x3, #5 // Compute acc + 5 adcs x10, x4, xzr adc x11, x5, xzr /* Test for more than 2 ^ 130 */ cmp x11, #3 /* If yes, use the value after adding 5 (equal to the value after modulo operation). If no, use the original value. */ csel x3, x3, x9, le csel x4, x4, x10, le /* Plus the s value */ #ifdef HITLS_BIG_ENDIAN /* In the big-endian scenario, the s value needs to be reversed. */ ror x12, x12, #32 ror x13, x13, #32 #endif adds x3, x3, x12 adc x4, x4, x13 mov x12, xzr // zero out. mov x13, xzr #ifdef HITLS_BIG_ENDIAN /* In big-endian mode, the data is converted to little-endian and then output to the memory. */ rev x3, x3 rev x4, x4 #endif stp x3, x4, [x1] AARCH64_AUTIASP ret .size Poly1305Last, .-Poly1305Last /** * Function description: Outputs the final result value to the specified memory. * Function prototype: void Poly1305LastNeon(Poly1305Ctx *ctx, uint8_t mac[POLY1305_TAGSIZE]); * Input register: * x0: address of the context structure * x1: pointer to the output buffer * Change register: x2-x15 * Output register: None * Function/Macro Call: None */ .text .balign 64 .type Poly1305LastNeon, %function Poly1305LastNeon: AARCH64_PACIASP /* Load the value of base 2^26. */ ldp w11, w12, [x0, #CTX_acc] ldp w13, w14, [x0, #CTX_acc + 8] ldr w15, [x0, #CTX_acc + 16] /* Converted to base 2^64, x11 to x15 are within 30 bits. */ CONVERT_26TO64 x5, x6, x7, x11, x12, x13, x14, x15 /* Load the s value. */ ldp x2, x3, [x0, #CTX_s] /* Add more than 130 bits by 5 to the lower bits. */ bic x15, x7, #3 and x7, x7, #3 add x15, x15, x15, lsr#2 adds x5, x5, x15 adcs x6, x6, xzr adc x7, x7, xzr /* Modulo P, subtract directly */ /* subtraction:acc - (2^130 - 5) = acc + 5 - 2^130 */ adds x11, x5, #5 adcs x12, x6, xzr adc x13, x7, xzr /* Test for more than 2 ^ 130 */ cmp x13, #4 /* If P is greater than or equal to P, the new value is used. */ csel x5, x11, x5, ge csel x6, x12, x6, ge /* Value of s plus acc */ #ifdef HITLS_BIG_ENDIAN /* In the big-endian scenario, the s value needs to be reversed. */ ror x2, x2, #32 ror x3, x3, #32 #endif adds x2, x2, x5 adc x3, x3, x6 #ifdef HITLS_BIG_ENDIAN /* In big-endian mode, the data is converted to little-endian and then output to the memory. */ rev x2, x2 rev x3, x3 #endif stp x2, x3, [x1] AARCH64_AUTIASP ret .size Poly1305LastNeon, .-Poly1305LastNeon /** * Function description: Compresses the input data and stores it in the context structure. * Function prototype: uint32_t Poly1305Block(Poly1305Ctx *ctx, const uint8_t *data, * uint32_t dataLen, uint32_t padbit); * Input register: * x0: address of the context structure * x1: pointer to the input data * x2: length of the input data * x3: padded bits, 0 or 1. * Change register: x4-x15 * Output register: * x0: length of the remaining data to be processed * Function/Macro Call: CONVERT_26TO64 POLY1305_MOD_MUL Poly1305BlockNeon */ .text .balign 64 .global Poly1305Block .type Poly1305Block, %function Poly1305Block: AARCH64_PACIASP /* x4 indicates the length of the basic instruction set to be processed, and x2 indicates the remaining length of the instruction set to be processed. */ /* If the value is less than 16, no processing is required. If NEON is supported, the part that is greater than or equal to 256 is reserved for NEON. */ and x4, x2, #0xF0 // x4 is the processing length of the basic instruction set. bic x2, x2, #0xF0 // x2 is the remaining length after the basic instruction set is processed. cbz x4, .Lskip_process /* Load the ACC value. */ ldr w15, [x0, #CTX_flag] and w14, w15, #FLAG_BASE2_26 cbz w14, .Lload_acc_64 bic w15, w15, #FLAG_BASE2_26 str w15, [x0, #CTX_flag] ldp w10, w11, [x0, #CTX_acc] ldp w12, w13, [x0, #CTX_acc + 8] ldr w14, [x0, #CTX_acc + 16] CONVERT_26TO64 x5, x6, x7, x10, x11, x12, x13, x14 b .Lend_load_acc_64 .Lload_acc_64: ldp x5, x6, [x0, #CTX_acc] ldr x7, [x0, #CTX_acc + 16] .Lend_load_acc_64: /* Load the r value. */ ldp x8, x9, [x0, #CTX_r] #ifdef HITLS_BIG_ENDIAN /* The r value needs to be reversed in the big-endian case. */ ror x8, x8, #32 ror x9, x9, #32 #endif add x10, x9, x9, lsr#2 .Lloop_64: /* Accumulator acc plus plaintext block with padding x3 */ ldp x11, x12, [x1], #16 #ifdef HITLS_BIG_ENDIAN rev x11, x11 rev x12, x12 #endif adds x5, x5, x11 adcs x6, x6, x12 adc x7, x7, x3 /* Multiply large numbers and take modulo (x5|x6|x7) = (x5|x6|x7) * (x8|x9) mod P */ /* x10 = x9 + x9 >> 2 */ POLY1305_MOD_MUL x5, x6, x7, x8, x9, x10 /* End of loop, update iteration information */ sub x4, x4, #16 cbnz x4, .Lloop_64 stp x5, x6, [x0, #CTX_acc] str x7, [x0, #CTX_acc + 16] .Lskip_process: /* If the remaining length is 256 bytes or more, the NEON processes the remaining length. */ bic x4, x2, #0xFF cbnz x4, Poly1305BlockNeon /* function returns */ and x0, x2, #15 // The return value is the unprocessed length. eor x8, x8, x8 eor x9, x9, x9 AARCH64_AUTIASP ret .size Poly1305Block, .-Poly1305Block /** * Function description: Compresses the input data, stores the data in the context structure, and uses the NEON register. * Function prototype: uint32_t Poly1305BlockNeon(Poly1305Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint32_t padbit); * Input register: * x0: context structure address * x1: pointer to the input data * x2: length of the input data * x3: padding bit, 0 or 1. * Modify the register x0-x15,v0-v7,v16-v31. * Output register: * x0: length of the remaining data to be processed * Function/Macro call: CONVERT_64TO26 */ .text .balign 64 .type Poly1305BlockNeon, %function Poly1305BlockNeon: stp x29, x30, [sp, #-16]! stp d8, d9, [sp, #-16]! stp d10, d11, [sp, #-16]! stp d12, d13, [sp, #-16]! stp d14, d15, [sp, #-16]! /* Load the acc value, which is stored in v24-v28. */ ldr w15, [x0, #CTX_flag] and w14, w15, #FLAG_BASE2_26 cbnz w14, .Lload_acc_26 orr w15, w15, #FLAG_BASE2_26 str w15, [x0, #CTX_flag] ldp x5, x6, [x0, #CTX_acc] ldr x7, [x0, #CTX_acc + 16] CONVERT_64TO26 x11, x12, x13, x14, x15, x5, x6, x7 fmov s24, w11 fmov s25, w12 fmov s26, w13 fmov s27, w14 fmov s28, w15 b .Lend_load_acc_26 .Lload_acc_26: ldp s24, s25, [x0, #CTX_acc] ldp s26, s27, [x0, #CTX_acc + 8] ldr s28, [x0, #CTX_acc + 16] .Lend_load_acc_26: /* Load r-value table */ add x15, x0, #CTX_table ld1 {v0.4s}, [x15], #16 // r^n[0] mod P, n = 1, 2, 3, 4 ld1 {v1.4s, v2.4s, v3.4s, v4.4s}, [x15], #64 // r^n[1:4] mod P ld1 {v5.4s, v6.4s, v7.4s, v8.4s}, [x15], #64 // 5 * r^n[1:4] mod P /* Pre-treatment before start of cycle */ add x1, x1, #64 sub x4, x4, #64 /* v31.2d is {0x3ffffff, 0x3ffffff} */ movi v31.16b, #0xFF ushr v31.2d, v31.2d, #38 /* Load (m[2], m[3]), convert the format, and save it to v14-v18. */ ldp x9, x10, [x1, #-32] ldp x14, x15, [x1, #-16] #ifdef HITLS_BIG_ENDIAN rev x9, x9 rev x10, x10 rev x14, x14 rev x15, x15 #endif and x6, x9, #0x03ffffff ubfx x7, x9, #26, #26 extr x8, x10, x9, #52 and x8, x8, #0x03ffffff ubfx x9, x10, #14, #26 extr x10, x3, x10, #40 and x11, x14, #0x03ffffff ubfx x12, x14, #26, #26 extr x13, x15, x14, #52 and x13, x13, #0x03ffffff ubfx x14, x15, #14, #26 extr x15, x3, x15, #40 add x6, x6, x11, lsl#32 add x7, x7, x12, lsl#32 add x8, x8, x13, lsl#32 add x9, x9, x14, lsl#32 add x10, x10, x15, lsl#32 fmov d14, x6 fmov d15, x7 fmov d16, x8 fmov d17, x9 fmov d18, x10 /* Load (m[0], m[1]) and save the converted format in v9-v13. */ ldp x9, x10, [x1, #-64] ldp x14, x15, [x1, #-48] #ifdef HITLS_BIG_ENDIAN rev x9, x9 rev x10, x10 rev x14, x14 rev x15, x15 #endif and x6, x9, #0x03ffffff ubfx x7, x9, #26, #26 extr x8, x10, x9, #52 and x8, x8, #0x03ffffff ubfx x9, x10, #14, #26 extr x10, x3, x10, #40 and x11, x14, #0x03ffffff ubfx x12, x14, #26, #26 extr x13, x15, x14, #52 and x13, x13, #0x03ffffff ubfx x14, x15, #14, #26 extr x15, x3, x15, #40 add x6, x6, x11, lsl#32 add x7, x7, x12, lsl#32 add x8, x8, x13, lsl#32 add x9, x9, x14, lsl#32 add x10, x10, x15, lsl#32 fmov d9, x6 fmov d10, x7 fmov d11, x8 fmov d12, x9 fmov d13, x10 /* See NEON Crypto by Daniel J. Bernstein and Peter Schwabe Use base 2^26 to represent a large number: f = f[0] + f[1]<<26 + f[2]<<52 + f[3]<<78 + f[4]<<104 Calculate h = (f * g) mod (2^130 - 5), using the NEON register h[0] = f[0]g[0] + 5f[1]g[4] + 5f[2]g[3] + 5f[3]g[2] + 5f[4]g[1] h[1] = f[0]g[1] + f[1]g[0] + 5f[2]g[4] + 5f[3]g[3] + 5f[4]g[2] h[2] = f[0]g[2] + f[1]g[1] + f[2]g[0] + 5f[3]g[4] + 5f[4]g[3] h[3] = f[0]g[3] + f[1]g[2] + f[2]g[1] + f[3]g[0] + 5f[4]g[4] h[4] = f[0]g[4] + f[1]g[3] + f[2]g[2] + f[3]g[1] + f[4]g[0] NEON Polynomial Calculation Process: ((m[0]r^4 + m[2]r^2 + m[4])*r^4 + m[6]r^2 + m[8])*r^4 + m[10]r^2 + ((m[1]r^4 + m[3]r^2 + m[5])*r^4 + m[7]r^2 + m[9])*r^3 + m[11]r^1 Calculated inside the loop: (x[0],y[0]) = (acc, 0) (x[1],y[1]) = (m[2],m[3])*(r^2,r^2) + ((m[0],m[1]) + (x[0],y[0]))*(r^4,r^4) (x[2],y[2]) = (m[6],m[7])*(r^2,r^2) + ((m[4],m[5]) + (x[1],y[1]))*(r^4,r^4) */ /* Start loop, vector register has used v0-v8 to hold r value precalculated table, v24-v28 to hold ACC value */ .Lloop_neon: add x1, x1, #64 sub x4, x4, #64 /* Compute (m[2 + 4i], m[3 + 4i])*(r^2, r^2), stored in v19-v23 */ /* Load the (m[6 + 4i], m[7 + 4i]) file and save it in v14-v18. */ ldp x9, x10, [x1, #-32] umull v19.2d, v14.2s, v0.s[2] umull v20.2d, v14.2s, v1.s[2] umull v21.2d, v14.2s, v2.s[2] umull v22.2d, v14.2s, v3.s[2] umull v23.2d, v14.2s, v4.s[2] ldp x14, x15, [x1, #-16] umlal v19.2d, v15.2s, v8.s[2] umlal v20.2d, v15.2s, v0.s[2] umlal v21.2d, v15.2s, v1.s[2] umlal v22.2d, v15.2s, v2.s[2] umlal v23.2d, v15.2s, v3.s[2] #ifdef HITLS_BIG_ENDIAN rev x9, x9 rev x10, x10 rev x14, x14 rev x15, x15 #endif and x6, x9, #0x03ffffff and x11, x14, #0x03ffffff ubfx x7, x9, #26, #26 ubfx x12, x14, #26, #26 extr x8, x10, x9, #52 extr x13, x15, x14, #52 umlal v19.2d, v16.2s, v7.s[2] umlal v20.2d, v16.2s, v8.s[2] umlal v21.2d, v16.2s, v0.s[2] umlal v22.2d, v16.2s, v1.s[2] umlal v23.2d, v16.2s, v2.s[2] and x8, x8, #0x03ffffff and x13, x13, #0x03ffffff ubfx x9, x10, #14, #26 ubfx x14, x15, #14, #26 extr x10, x3, x10, #40 extr x15, x3, x15, #40 umlal v19.2d, v17.2s, v6.s[2] umlal v20.2d, v17.2s, v7.s[2] umlal v21.2d, v17.2s, v8.s[2] umlal v22.2d, v17.2s, v0.s[2] umlal v23.2d, v17.2s, v1.s[2] add x6, x6, x11, lsl#32 add x7, x7, x12, lsl#32 add x8, x8, x13, lsl#32 add x9, x9, x14, lsl#32 add x10, x10, x15, lsl#32 umlal v19.2d, v18.2s, v5.s[2] umlal v20.2d, v18.2s, v6.s[2] umlal v21.2d, v18.2s, v7.s[2] umlal v22.2d, v18.2s, v8.s[2] umlal v23.2d, v18.2s, v0.s[2] fmov d14, x6 fmov d15, x7 fmov d16, x8 fmov d17, x9 fmov d18, x10 /* It is not placed at the beginning of the loop because it depends on v24 to v28. */ /* Compute ((m[0 + 4i], m[1 + 4i]) + (x[i], y[i]))*(r^4, r^4), stored in v19-v23 */ /* Load the (m[4 + 4i], m[5 + 4i]) file and save it in v9-v13. */ add v9.2s, v9.2s, v24.2s add v10.2s, v10.2s, v25.2s add v11.2s, v11.2s, v26.2s add v12.2s, v12.2s, v27.2s add v13.2s, v13.2s, v28.2s ldp x9, x10, [x1, #-64] umlal v19.2d, v9.2s, v0.s[0] umlal v20.2d, v9.2s, v1.s[0] umlal v21.2d, v9.2s, v2.s[0] umlal v22.2d, v9.2s, v3.s[0] umlal v23.2d, v9.2s, v4.s[0] ldp x14, x15, [x1, #-48] umlal v19.2d, v10.2s, v8.s[0] umlal v20.2d, v10.2s, v0.s[0] umlal v21.2d, v10.2s, v1.s[0] umlal v22.2d, v10.2s, v2.s[0] umlal v23.2d, v10.2s, v3.s[0] #ifdef HITLS_BIG_ENDIAN rev x9, x9 rev x10, x10 rev x14, x14 rev x15, x15 #endif and x6, x9, #0x03ffffff and x11, x14, #0x03ffffff ubfx x7, x9, #26, #26 ubfx x12, x14, #26, #26 extr x8, x10, x9, #52 extr x13, x15, x14, #52 umlal v19.2d, v11.2s, v7.s[0] umlal v20.2d, v11.2s, v8.s[0] umlal v21.2d, v11.2s, v0.s[0] umlal v22.2d, v11.2s, v1.s[0] umlal v23.2d, v11.2s, v2.s[0] and x8, x8, #0x03ffffff and x13, x13, #0x03ffffff ubfx x9, x10, #14, #26 ubfx x14, x15, #14, #26 extr x10, x3, x10, #40 extr x15, x3, x15, #40 umlal v19.2d, v12.2s, v6.s[0] umlal v20.2d, v12.2s, v7.s[0] umlal v21.2d, v12.2s, v8.s[0] umlal v22.2d, v12.2s, v0.s[0] umlal v23.2d, v12.2s, v1.s[0] add x6, x6, x11, lsl#32 add x7, x7, x12, lsl#32 add x8, x8, x13, lsl#32 add x9, x9, x14, lsl#32 add x10, x10, x15, lsl#32 umlal v19.2d, v13.2s, v5.s[0] umlal v20.2d, v13.2s, v6.s[0] umlal v21.2d, v13.2s, v7.s[0] umlal v22.2d, v13.2s, v8.s[0] umlal v23.2d, v13.2s, v0.s[0] fmov d9, x6 fmov d10, x7 fmov d11, x8 fmov d12, x9 fmov d13, x10 /* Because v19-v23 significant bits may exceed 56 bits, to ensure that subsequent multiplication does not overflow, two carry is processed. */ ushr v24.2d, v19.2d, #26 ushr v25.2d, v20.2d, #26 ushr v26.2d, v21.2d, #26 ushr v27.2d, v22.2d, #26 ushr v28.2d, v23.2d, #26 /* More than 130 digits multiplied by 5 to the lower bits */ shl v29.2d, v28.2d, #2 add v28.2d, v28.2d, v29.2d /* Use the AND operation to truncate the lower 26 bits. */ and v19.16b, v19.16b, v31.16b and v20.16b, v20.16b, v31.16b and v21.16b, v21.16b, v31.16b and v22.16b, v22.16b, v31.16b and v23.16b, v23.16b, v31.16b /* Add the part of the low carry */ add v19.2d, v19.2d, v28.2d add v20.2d, v20.2d, v24.2d add v21.2d, v21.2d, v25.2d add v22.2d, v22.2d, v26.2d add v23.2d, v23.2d, v27.2d /* Continue carry processing */ ushr v24.2d, v19.2d, #26 ushr v25.2d, v20.2d, #26 ushr v26.2d, v21.2d, #26 ushr v27.2d, v22.2d, #26 ushr v28.2d, v23.2d, #26 shl v29.2d, v28.2d, #2 add v28.2d, v28.2d, v29.2d and v19.16b, v19.16b, v31.16b and v20.16b, v20.16b, v31.16b and v21.16b, v21.16b, v31.16b and v22.16b, v22.16b, v31.16b and v23.16b, v23.16b, v31.16b add v19.2d, v19.2d, v28.2d add v20.2d, v20.2d, v24.2d add v21.2d, v21.2d, v25.2d add v22.2d, v22.2d, v26.2d add v23.2d, v23.2d, v27.2d /* The calculated (x[i + 1], y[i + 1]) is stored in v24-v28 and is reserved for the next cycle. */ xtn v24.2s, v19.2d xtn v25.2s, v20.2d xtn v26.2s, v21.2d xtn v27.2s, v22.2d xtn v28.2s, v23.2d /* End of loop, skip */ cbnz x4, .Lloop_neon /* Dealing with the tail */ /* Compute (m[6 + 4i], m[7 + 4i])*(r^2, r^1), stored in v19-v23 */ dup v14.2d, v14.d[0] dup v15.2d, v15.d[0] dup v16.2d, v16.d[0] dup v17.2d, v17.d[0] dup v18.2d, v18.d[0] umull2 v19.2d, v14.4s, v0.4s umull2 v20.2d, v14.4s, v1.4s umull2 v21.2d, v14.4s, v2.4s umull2 v22.2d, v14.4s, v3.4s umull2 v23.2d, v14.4s, v4.4s umlal2 v19.2d, v15.4s, v8.4s umlal2 v20.2d, v15.4s, v0.4s umlal2 v21.2d, v15.4s, v1.4s umlal2 v22.2d, v15.4s, v2.4s umlal2 v23.2d, v15.4s, v3.4s umlal2 v19.2d, v16.4s, v7.4s umlal2 v20.2d, v16.4s, v8.4s umlal2 v21.2d, v16.4s, v0.4s umlal2 v22.2d, v16.4s, v1.4s umlal2 v23.2d, v16.4s, v2.4s umlal2 v19.2d, v17.4s, v6.4s umlal2 v20.2d, v17.4s, v7.4s umlal2 v21.2d, v17.4s, v8.4s umlal2 v22.2d, v17.4s, v0.4s umlal2 v23.2d, v17.4s, v1.4s umlal2 v19.2d, v18.4s, v5.4s umlal2 v20.2d, v18.4s, v6.4s umlal2 v21.2d, v18.4s, v7.4s umlal2 v22.2d, v18.4s, v8.4s umlal2 v23.2d, v18.4s, v0.4s /* Compute (m[4 + 4i], m[5 + 4i])*(r^4, r^3), stored in v19-v23 */ add v9.2s, v9.2s, v24.2s add v10.2s, v10.2s, v25.2s add v11.2s, v11.2s, v26.2s add v12.2s, v12.2s, v27.2s add v13.2s, v13.2s, v28.2s umlal v19.2d, v9.2s, v0.2s umlal v20.2d, v9.2s, v1.2s umlal v21.2d, v9.2s, v2.2s umlal v22.2d, v9.2s, v3.2s umlal v23.2d, v9.2s, v4.2s umlal v19.2d, v10.2s, v8.2s umlal v20.2d, v10.2s, v0.2s umlal v21.2d, v10.2s, v1.2s umlal v22.2d, v10.2s, v2.2s umlal v23.2d, v10.2s, v3.2s umlal v19.2d, v11.2s, v7.2s umlal v20.2d, v11.2s, v8.2s umlal v21.2d, v11.2s, v0.2s umlal v22.2d, v11.2s, v1.2s umlal v23.2d, v11.2s, v2.2s umlal v19.2d, v12.2s, v6.2s umlal v20.2d, v12.2s, v7.2s umlal v21.2d, v12.2s, v8.2s umlal v22.2d, v12.2s, v0.2s umlal v23.2d, v12.2s, v1.2s umlal v19.2d, v13.2s, v5.2s umlal v20.2d, v13.2s, v6.2s umlal v21.2d, v13.2s, v7.2s umlal v22.2d, v13.2s, v8.2s umlal v23.2d, v13.2s, v0.2s /* The results are added, stored in v24-v28, and base 2^26 carry. */ ushr v24.2d, v19.2d, #26 ushr v25.2d, v20.2d, #26 ushr v26.2d, v21.2d, #26 ushr v27.2d, v22.2d, #26 ushr v28.2d, v23.2d, #26 shl v29.2d, v28.2d, #2 add v28.2d, v28.2d, v29.2d and v19.16b, v19.16b, v31.16b and v20.16b, v20.16b, v31.16b and v21.16b, v21.16b, v31.16b and v22.16b, v22.16b, v31.16b and v23.16b, v23.16b, v31.16b add v19.2d, v19.2d, v28.2d add v20.2d, v20.2d, v24.2d add v21.2d, v21.2d, v25.2d add v22.2d, v22.2d, v26.2d add v23.2d, v23.2d, v27.2d /* Continue carry processing */ ushr v24.2d, v19.2d, #26 ushr v25.2d, v20.2d, #26 ushr v26.2d, v21.2d, #26 ushr v27.2d, v22.2d, #26 ushr v28.2d, v23.2d, #26 shl v29.2d, v28.2d, #2 add v28.2d, v28.2d, v29.2d and v19.16b, v19.16b, v31.16b and v20.16b, v20.16b, v31.16b and v21.16b, v21.16b, v31.16b and v22.16b, v22.16b, v31.16b and v23.16b, v23.16b, v31.16b add v19.2d, v19.2d, v28.2d add v20.2d, v20.2d, v24.2d add v21.2d, v21.2d, v25.2d add v22.2d, v22.2d, v26.2d add v23.2d, v23.2d, v27.2d addp v24.2d, v19.2d, v19.2d addp v25.2d, v20.2d, v20.2d addp v26.2d, v21.2d, v21.2d addp v27.2d, v22.2d, v22.2d addp v28.2d, v23.2d, v23.2d /* After the processing is complete, save the data. Note that the carry may not be completely processed. */ stp s24, s25, [x0, #CTX_acc] stp s26, s27, [x0, #CTX_acc + 8] str s28, [x0, #CTX_acc + 16] /* return */ mov x5, xzr ldp d14, d15, [sp], #16 ldp d12, d13, [sp], #16 ldp d10, d11, [sp], #16 ldp d8, d9, [sp], #16 ldp x29, x30, [sp], #16 and x0, x2, #15 // The return value is the unprocessed length. AARCH64_AUTIASP ret .size Poly1305BlockNeon, .-Poly1305BlockNeon /** * Function description: This function is used to clear residual sensitive information in registers. * Function prototype: void Poly1305CleanRegister(); * Input register: None * Modify the registers v0-v7, v16-v31. * Output register: None * Function/Macro Call: None */ .text .balign 64 .global Poly1305CleanRegister .type Poly1305CleanRegister, %function Poly1305CleanRegister: AARCH64_PACIASP movi v0.16b, #0 and v1.16b, v1.16b, v0.16b and v2.16b, v2.16b, v0.16b and v3.16b, v3.16b, v0.16b and v4.16b, v4.16b, v0.16b and v5.16b, v5.16b, v0.16b and v6.16b, v6.16b, v0.16b and v7.16b, v7.16b, v0.16b /* V8 to V15 are overwritten during register recovery and do not need to be cleared. */ and v16.16b, v16.16b, v0.16b and v17.16b, v17.16b, v0.16b and v18.16b, v18.16b, v0.16b and v19.16b, v19.16b, v0.16b and v20.16b, v20.16b, v0.16b and v21.16b, v21.16b, v0.16b and v22.16b, v22.16b, v0.16b and v23.16b, v23.16b, v0.16b and v24.16b, v24.16b, v0.16b and v25.16b, v25.16b, v0.16b and v26.16b, v26.16b, v0.16b and v27.16b, v27.16b, v0.16b and v28.16b, v28.16b, v0.16b and v29.16b, v29.16b, v0.16b and v30.16b, v30.16b, v0.16b and v31.16b, v31.16b, v0.16b AARCH64_AUTIASP ret .size Poly1305CleanRegister, .-Poly1305CleanRegister #endif
2301_79861745/bench_create
crypto/modes/src/asm/poly1305_armv8.S
Unix Assembly
unknown
31,825
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "poly1305_x86_64_macro.s" .file "poly1305_x86_64.S" .text /** * Function description: Initializes the pre-computation table and clears the flag. * Function prototype: void Poly1305InitForAsm(Poly1305_Ctx *ctx); * Input register: * CTX: address of the Poly305_Ctx structure * Modify the register: rax, rdx, rbx, rbp, r8, r9, r11-r14. * Output register: None * Function/Macro Call: Poly1305_MOD_MUL */ .globl Poly1305InitForAsm .type Poly1305InitForAsm, @function .align 32 Poly1305InitForAsm: .cfi_startproc push %rbx push %rbp push %r12 push %r13 push %r14 movl $0, 220(CTX) // flag bit Clear movq 24(CTX), R0 movq 32(CTX), R1 movq R1, R2 shrq $2, R2 addq R1, R2 lea 56(CTX), CTX movq R0, ACC1 movq R1, ACC2 xorq ACC3, ACC3 movq R1, %rax POLY1305_MOD_MUL ACC1, ACC2, ACC3, R0, R1, R2 // r^2 movl $0x3ffffff, %eax movl $0x3ffffff, %edx movq ACC1, D1 andl %r14d, %eax movq R0, D2 andl %r11d, %edx movl %eax, (CTX) // r0^2 shrq $26, D1 movl %edx, 4(CTX) // r0 shrq $26, D2 movl $0x3ffffff, %eax movl $0x3ffffff, %edx andl %r8d, %eax andl %r9d, %edx movl %eax, 16(CTX) // r1^2 lea (%rax, %rax, 4), %eax movl %edx, 20(CTX) // r1 lea (%rdx, %rdx, 4), %edx movl %eax, 32(CTX) // s1^2 shrq $26, D1 movl %edx, 36(CTX) // s1 shrq $26, D2 movq ACC2, %rax movq R1, %rdx shlq $12, %rax shlq $12, %rdx orq D1, %rax orq D2, %rdx andl $0x3ffffff, %eax andl $0x3ffffff, %edx movl %eax, 48(CTX) // r2^2 lea (%rax, %rax, 4), %eax movl %edx, 52(CTX) // r2 lea (%rdx, %rdx, 4), %edx movl %eax, 64(CTX) // s2^2 movq ACC2, D1 movl %edx, 68(CTX) // s2 movq R1, D2 shrq $14, D1 movl $0x3ffffff, %eax shrq $14, D2 movl $0x3ffffff, %edx andl %r8d, %eax andl %r9d, %edx movl %eax, 80(CTX) // r3^2 lea (%rax, %rax, 4), %eax movl %edx, 84(CTX) // r3 lea (%rdx, %rdx, 4), %edx movl %eax, 96(CTX) // s3^2 shrq $26, D1 movl %edx, 100(CTX) // s3 shrq $26, D2 movq ACC3, %rax shlq $24, %rax orq %rax, D1 movl %r8d, 112(CTX) // r4^2 lea (D1, D1, 4), D1 movl %r9d, 116(CTX) // r4 lea (D2, D2, 4), D2 movl %r8d, 128(CTX) // s4^2 movl %r9d, 132(CTX) // s4 movq R1, %rax POLY1305_MOD_MUL ACC1, ACC2, ACC3, R0, R1, R2 // r^3 movq ACC1, D1 movl $0x3ffffff, %edx andl %r8d, %edx movl %edx, 12(CTX) // r0^3 shrq $26, D1 movl $0x3ffffff, %edx andl %r8d, %edx movl %edx, 28(CTX) // r1^3 lea (%rdx, %rdx, 4), %edx shrq $26, D1 movl %edx, 44(CTX) // s1^3 movq ACC2, %rax shlq $12, %rax orq D1, %rax andl $0x3ffffff, %eax movl %eax, 60(CTX) // r2^3 lea (%rax, %rax, 4), %eax movq ACC2, D1 movl %eax, 76(CTX) // s2^3 shrq $14, D1 movl $0x3ffffff, %eax andl %r8d, %eax movl %eax, 92(CTX) // r3^3 lea (%rax, %rax, 4), %eax shrq $26, D1 movl %eax, 108(CTX) // s3^3 movq ACC3, %rdx shlq $24, %rdx orq %rdx, D1 movl %r8d, 124(CTX) // r4^3 lea (D1, D1, 4), D1 movl %r8d, 140(CTX) // s4^3 movq R1, %rax POLY1305_MOD_MUL ACC1, ACC2, ACC3, R0, R1, R2 // r^4 movq ACC1, D1 movl $0x3ffffff, %edx andl %r8d, %edx movl %edx, 8(CTX) // r0^4 shrq $26, D1 movl $0x3ffffff, %edx andl %r8d, %edx movl %edx, 24(CTX) // r1^4 lea (%rdx, %rdx, 4), %edx shrq $26, D1 movl %edx, 40(CTX) // s1^4 movq ACC2, %rax shlq $12, %rax orq D1, %rax andl $0x3ffffff, %eax movl %eax, 56(CTX) // r2^4 lea (%rax, %rax, 4), %eax movq ACC2, D1 movl %eax, 72(CTX) // s2^4 shrq $14, D1 movl $0x3ffffff, %eax andl %r8d, %eax movl %eax, 88(CTX) // r3^4 lea (%rax, %rax, 4), %eax shrq $26, D1 movl %eax, 104(CTX) // s3^4 movq ACC3, %rdx shlq $24, %rdx orq %rdx, D1 movl %r8d, 120(CTX) // r4^4 lea (D1, D1, 4), D1 movl %r8d, 136(CTX) // s4^4 lea -56(CTX), CTX pop %r14 pop %r13 pop %r12 pop %rbp pop %rbx ret .cfi_endproc .size Poly1305InitForAsm, .-Poly1305InitForAsm /** * Function description: x86_64 poly1305 64-bit basic instruction implementation * Input register: * CTX: address of the Poly305_Ctx structure * INP: data pointer * LEN: data length * PADBIT: padding data * Change register: r8-r15, rax, rbx, rdx, rbp * Output register: * rax: length of the remaining data to be processed * Macro invoking:Poly1305_MOD_MUL */ .globl Poly1305Block64Bit .type Poly1305Block64Bit, @function Poly1305Block64Bit: .cfi_startproc .align 32 .Lblock_start: push %rbx push %rbp push %r12 push %r13 push %r14 push %r15 movq LEN, %r15 LOAD_ACC_R CTX, R0, R1, R2, ACC1, ACC2, ACC3, %r8d, %rax test %r8d, %r8d jz .Lblock64_loop CONVERT_26TO64_PRE ACC1, ACC2, D1, D2, D3 CONVERT_26TO64 ACC1, D1, ACC2, D2, D3, ACC3 movl $0, 220(CTX) .align 32 .Lblock64_loop: addq (INP), ACC1 adcq 8(INP), ACC2 adcq PADBIT, ACC3 lea 16(INP), INP POLY1305_MOD_MUL ACC1, ACC2, ACC3, R0, R1, R2 subq $16, %r15 movq R1, %rax jnz .Lblock64_loop movq ACC1, (CTX) movq ACC2, 8(CTX) movq ACC3, 16(CTX) movq %r15, %rax pop %r15 pop %r14 pop %r13 pop %r12 pop %rbp pop %rbx ret .cfi_endproc .size Poly1305Block64Bit, .-Poly1305Block64Bit /** * Function description: Calculates (acc + s) mod 2^128 and outputs the final result to the specified memory. * Function prototype: void Poly1305Last(Poly1305_Ctx *ctx, uint8_t mac[POLY1305_TAGSIZE]); * Input register: * rdi: address of the Poly305_Ctx structure * rsi: pointer to the output buffer * Modify the register: rax, rcx, r14, rbx, rbp, r8-r10. * Output register: None * Function/Macro Call: * CONVERT_26TO64 */ .globl Poly1305Last .type Poly1305Last, @function .align 32 Poly1305Last: .cfi_startproc push %rbx push %rbp push %r14 movl 220(CTX), %r8d movq (CTX), ACC1 movq 8(CTX), ACC2 movq 16(CTX), ACC3 test %r8d, %r8d jz .Lblock_last_body CONVERT_26TO64_PRE ACC1, ACC2, D1, D2, D3 CONVERT_26TO64 ACC1, D1, ACC2, D2, D3, ACC3 movl $0, 220(CTX) .Lblock_last_body: movq ACC1, %rax addq $5, ACC1 movq ACC2, %rcx adcq $0, ACC2 adcq $0, ACC3 shrq $2, ACC3 cmovnz ACC1, %rax cmovnz ACC2, %rcx addq 40(CTX), %rax adcq 48(CTX), %rcx movq %rax, (%rsi) movq %rcx, 8(%rsi) pop %r14 pop %rbp pop %rbx ret .cfi_endproc .size Poly1305Last, .-Poly1305Last #endif
2301_79861745/bench_create
crypto/modes/src/asm/poly1305_x86_64.S
Unix Assembly
unknown
8,997
/* * 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_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "poly1305_x86_64.S" .file "poly1305_x86_64_avx2.S" .text /** * Function description: This function is implemented by x86_64 poly1305. The result is stored in ctx->acc. * Function prototype: uint32_t Poly1305Block(Poly1305_Ctx *ctx, const uint8_t *data, * uint32_t dataLen, uint32_t padbit); * Input register: * CTX: address of the Poly305_Ctx structure * INP: pointer to the input data * LEN: length of the input data * PADBIT: padding bit, 0 or 1. * Change registers: r8-r14, rbx, rbp * Output register: * %rax: length of the remaining data to be processed * Function/Macro Call:Poly1305_MOD_MUL */ .globl Poly1305Block .type Poly1305Block,@function Poly1305Block: .cfi_startproc .align 32 cmp $256, LEN jae .Lblock_avx_pre jmp Poly1305Block64Bit .Lblock_avx_pre: andq $-16, LEN test $63, LEN jz Poly1305BlockAVX2 .Lbase2_64_avx_body: push %rbx push %rbp push %r12 push %r13 push %r14 push %r15 movq LEN, %r15 LOAD_ACC_R CTX, R0, R1, R2, ACC1, ACC2, ACC3, %r8d, %rax test %r8d, %r8d jz .Lbase2_64_avx_loop CONVERT_26TO64_PRE ACC1, ACC2, D1, D2, D3 CONVERT_26TO64 ACC1, D1, ACC2, D2, D3, ACC3 movl $0, 220(CTX) .align 32 .Lbase2_64_avx_loop: addq (INP), ACC1 adcq 8(INP), ACC2 adcq PADBIT, ACC3 lea 16(INP), INP POLY1305_MOD_MUL ACC1, ACC2, ACC3, R0, R1, R2 subq $16, %r15 movq R1, %rax test $63, %r15 jnz .Lbase2_64_avx_loop movq ACC1, (CTX) movq ACC2, 8(CTX) movq ACC3, 16(CTX) movq %r15, LEN pop %r15 pop %r14 pop %r13 pop %r12 pop %rbp pop %rbx jmp Poly1305BlockAVX2 ret .cfi_endproc .size Poly1305Block, .-Poly1305Block /** * Function description: x86_64 poly1305 AVX2 implementation * Input register: * CTX: address of the Poly305_Ctx structure * INP: pointer to the input data * LEN: length of the input data * PADBIT: padding bit, 0 or 1. * Change register: ymm0-15, r8, r9, r14, r15, rax, rbx, rdx, rbp * Output register: * rax: length of the remaining data to be processed * Function/Macro Call: * CONVERT_64TO26 */ .globl Poly1305BlockAVX2 .type Poly1305BlockAVX2, @function .align 32 Poly1305BlockAVX2: .cfi_startproc push %rbx push %rbp push %r14 push %r15 vzeroupper movq (CTX), ACC1 // load acc movq 8(CTX), ACC2 movq 16(CTX), ACC3 movl 220(CTX), %r8d test %r8d, %r8d jnz .Lblock_avx2_pre movq LEN, %r15 CONVERT_64TO26 ACC1, ACC2, ACC3, %rax, %rdx // base2_64 --> base2_26 movq %r15, LEN jmp .Lblock_avx2_body .Lblock_avx2_pre: movd %r14, %xmm0 movd %rbx, %xmm2 movd %rbp, %xmm4 shrq $32, %r14 shrq $32, %rbx movd %r14, %xmm1 movd %rbx, %xmm3 .align 32 .Lblock_avx2_body: leaq 56(CTX), CTX // 56(CTX) vmovdqu g_permd_avx2(%rip), YT0 // g_permd_avx2 leaq -8(%rsp), %r11 /* Transform the content in the precomputation table into a computable form and put it into the stack. */ vmovdqu (CTX), %xmm7 vmovdqu 16(CTX), %xmm8 subq $0x128, %rsp vmovdqu 32(CTX), %xmm9 vmovdqu 48(CTX), %xmm11 andq $-512, %rsp vmovdqu 64(CTX), %xmm12 vmovdqu 80(CTX), %xmm13 vpermd YT2, YT0, YT2 // 00 00 34 12 --> 14 24 34 44 vmovdqu 96(CTX), %xmm14 vpermd YT3, YT0, YT3 vmovdqu 112(CTX), %xmm15 vpermd YT4, YT0, YT4 vmovdqu 128(CTX), %xmm10 vpermd YB0, YT0, YB0 vmovdqa YT2, (%rsp) // r0 vpermd YB1, YT0, YB1 vmovdqa YT3, 0x20(%rsp) // r1 vpermd YB2, YT0, YB2 vmovdqa YT4, 0x40(%rsp) // s1 vpermd YB3, YT0, YB3 vmovdqa YB0, 0x60(%rsp) // r2 vpermd YB4, YT0, YB4 vmovdqa YB1, 0x80(%rsp) // s2 vpermd YMASK, YT0, YMASK vmovdqa YB2, 0xa0(%rsp) // r3 vmovdqa YB3, 0xc0(%rsp) // s3 vmovdqa YB4, 0xe0(%rsp) // r4 vmovdqa YMASK, 0x100(%rsp) // s4 /* Load 4 blocks of data and convert them to base2_26 */ vmovdqu g_mask26(%rip), YMASK // g_mask26 vmovdqu (INP), %xmm5 vmovdqu 16(INP), %xmm6 vinserti128 $1, 32(INP), YT0, YT0 vinserti128 $1, 48(INP), YT1, YT1 leaq 64(INP), INP vpsrldq $6, YT0, YT2 vpsrldq $6, YT1, YT3 vpunpckhqdq YT1, YT0, YT4 vpunpcklqdq YT1, YT0, YT0 vpunpcklqdq YT3, YT2, YT2 vpsrlq $26, YT0, YT1 vpsrlq $30, YT2, YT3 vpsrlq $4, YT2, YT2 vpsrlq $40, YT4, YT4 // 4 vpand YMASK, YT3, YT3 // 3 vpand YMASK, YT2, YT2 // 2 vpor g_129(%rip), YT4, YT4 // padbit vpand YMASK, YT1, YT1 // 1 vpand YMASK, YT0, YT0 // 0 vpaddq YH2, YT2, YH2 sub $64, LEN jz .Lblock_avx2_tail jmp .Lblock_avx2_loop .align 32 .Lblock_avx2_loop: // ((inp[0]*r^4 + inp[4])*r^4 + inp[ 8])*r^4 // ((inp[1]*r^4 + inp[5])*r^4 + inp[ 9])*r^3 // ((inp[2]*r^4 + inp[6])*r^4 + inp[10])*r^2 // ((inp[3]*r^4 + inp[7])*r^4 + inp[11])*r^1 vpaddq YH0, YT0, YH0 vpaddq YH1, YT1, YH1 vpaddq YH3, YT3, YH3 vpaddq YH4, YT4, YH4 vmovdqa (%rsp), YT0 // r0^4 vmovdqa 0x20(%rsp), YT1 // r1^4 vmovdqa 0x60(%rsp), YT2 // r2^4 vmovdqa 0xc0(%rsp), YT3 // s3^4 vmovdqa 0x100(%rsp), YMASK // s4^4 // b4 = h4*r0^4 + h3*r1^4 + h2*r2^4 + h1*r3^4 + h0*r4^4 // b3 = h3*r0^4 + h2*r1^4 + h1*r2^4 + h0*r3^4 + h4*s4^4 // b2 = h2*r0^4 + h1*r1^4 + h0*r2^4 + h4*s3^4 + h3*s4^4 // b1 = h1*r0^4 + h0*r1^4 + h4*s2^4 + h3*s3^4 + h2*s4^4 // b0 = h0*r0^4 + h4*s1^4 + h3*s2^4 + h2*s3^4 + h1*s4^4 // // First calculate h2, the above formula can be deformed as // // b4 = h2*r2^4 + h4*r0^4 + h3*r1^4 + + h1*r3^4 + h0*r4^4 // b3 = h2*r1^4 + h3*r0^4 + + h1*r2^4 + h0*r3^4 + h4*s4^4 // b2 = h2*r0^4 + + h1*r1^4 + h0*r2^4 + h4*s3^4 + h3*s4^4 // b1 = h2*s4^4 + h1*r0^4 + h0*r1^4 + h4*s2^4 + h3*s3^4 + // b0 = h2*s3^4 + h0*r0^4 + h4*s1^4 + h3*s2^4 + + h1*s4^4 vpmuludq YH2, YT0, YB2 // b2 = h2 * r0^4 vpmuludq YH2, YT1, YB3 // b3 = h2 * r1^4 vpmuludq YH2, YT2, YB4 // b4 = h2 * r2^4 vpmuludq YH2, YT3, YB0 // b0 = h2 * s3^4 vpmuludq YH2, YMASK, YB1 // b1 = h2 * s4^4 vpmuludq YH1, YT1, YT4 // h1 * r1^4 (Available Scratch Registers:T4、H2) vpmuludq YH0, YT1, YH2 // h0 * r1^4 vpaddq YT4, YB2, YB2 // b2 += h1 * r1^4 vpaddq YH2, YB1, YB1 // b1 += h0 * r1^4 vpmuludq YH3, YT1, YT4 // h3 * r1^4 vpmuludq 0x40(%rsp), YH4, YH2 // h4 * s1^4 vpaddq YT4 ,YB4, YB4 // b4 += h3 * r1^4 vpaddq YH2, YB0, YB0 // b0 += h4 * s1^4 vmovdqa 0x80(%rsp), YT1 // load s2^4 vpmuludq YH4, YT0, YT4 // h4 * r0^4 (Available Scratch Registers:T4、H2) vpmuludq YH3, YT0, YH2 // h3 * r0^4 vpaddq YT4, YB4, YB4 // b4 += h4 * r0^4 vpaddq YH2, YB3, YB3 // b3 += h3 * r0^4 vpmuludq YH0, YT0, YT4 // h0 * r0^4 vpmuludq YH1, YT0, YH2 // h1 * r0^4 vpaddq YT4, YB0, YB0 // b0 += h0 * r0^4 vpaddq YH2, YB1, YB1 // b1 += h1 * r0^4 vmovdqu (INP), %xmm5 // load input (YT0) vpmuludq YH4, YT1, YT4 // h4 * s2^4 vpmuludq YH3, YT1, YH2 // h3 * s2^4 vinserti128 $1, 32(INP), YT0, YT0 vpaddq YT4, YB1, YB1 // b1 += h4 * s2^4 vpaddq YH2, YB0, YB0 // b0 += h3 * s2^4 vpmuludq YH1, YT2, YT4 // h1 * r2^4 (Available Scratch Registers:T4、H2) vpmuludq YH0, YT2, YH2 // h0 * r2^4 vmovdqu 16(INP), %xmm6 // load input (YT1) vpaddq YT4, YB3, YB3 // b3 += h1 * r2^4 vpaddq YH2, YB2, YB2 // b2 += h0 * r2^4 vinserti128 $1, 48(INP), YT1, YT1 vmovdqa 0xa0(%rsp), YH2 // load r3^4 leaq 64(INP), INP vpmuludq YH1, YH2, YT4 // h1 * r3^4 (Available Scratch Registers:T4、H2) vpmuludq YH0, YH2, YH2 // h0 * r3^4 vpsrldq $6, YT0, YT2 vpaddq YT4, YB4, YB4 // b4 += h1 * r3^4 vpaddq YH2, YB3, YB3 // b3 += h0 * r3^4 vpmuludq YH4, YT3, YT4 // h4 * s3^4 vpmuludq YH3, YT3, YH2 // h3 * s3^4 vpsrldq $6, YT1, YT3 vpaddq YT4, YB2, YB2 // b2 += h4 * s3^4 vpaddq YH2, YB1, YB1 // b1 += h3 * s3^4 (finish) vpunpckhqdq YT1, YT0, YT4 vpmuludq YH3, YMASK, YH3 // h3 * s4^4 vpmuludq YH4, YMASK, YH4 // h4 * s4^4 vpunpcklqdq YT1, YT0, YT0 vpaddq YB2, YH3, YH2 // h2 += h3 * s4^4 (finish) vpaddq YB3, YH4, YH3 // h3 += h4 * s4^4 (finish) vpunpcklqdq YT3, YT2, YT3 vpmuludq 0xe0(%rsp), YH0, YH4 // h0 * r4^4 vpmuludq YH1, YMASK, YH0 // h1 * s4^4 vmovdqu g_mask26(%rip), YMASK vpaddq YH4, YB4, YH4 // h4 += h0 * r4^4 (finish) vpaddq YH0, YB0, YH0 // h0 += h1 * s4^4 (finish) // reduction vpsrlq $26, YH3, YB3 vpand YMASK, YH3, YH3 vpaddq YB3, YH4, YH4 // h3 -> h4 vpsrlq $26, YH0, YB0 vpand YMASK, YH0, YH0 vpaddq YB0, YB1, YH1 // h0 -> h1 vpsrlq $26, YH4, YB4 vpand YMASK, YH4, YH4 vpsrlq $4, YT3, YT2 vpsrlq $26, YH1, YB1 vpand YMASK, YH1, YH1 vpaddq YB1, YH2, YH2 // h1 -> h2 vpaddq YB4, YH0, YH0 vpsllq $2, YB4, YB4 vpaddq YB4, YH0, YH0 // h4 -> h0 vpand YMASK, YT2, YT2 vpsrlq $26, YT0, YT1 vpsrlq $26, YH2, YB2 vpand YMASK, YH2, YH2 vpaddq YB2, YH3, YH3 // h2 -> h3 vpaddq YT2, YH2, YH2 // prepare next 4 block vpsrlq $30, YT3, YT3 vpsrlq $26, YH0, YB0 vpand YMASK, YH0, YH0 vpaddq YB0, YH1, YH1 // h0 -> h1 vpsrlq $40, YT4, YT4 vpsrlq $26, YH3, YB3 vpand YMASK, YH3, YH3 vpaddq YB3, YH4, YH4 // h3 -> h4 vpand YMASK, YT0, YT0 // new input 0 vpand YMASK, YT1, YT1 // new input 1 vpand YMASK, YT3, YT3 // new input 3 vpor g_129(%rip), YT4, YT4 // new input 4, padbit subq $64, LEN jnz .Lblock_avx2_loop .Lblock_avx2_tail: BLOCK4_AVX2_TAIL YT0, YT1, YT2, YT3, YT4, YH0, YH1, YH2, YH3, YH4, YB0, YB1, YB2, YB3, YB4, YMASK, %rsp vmovd %xmm0, -56(CTX) vmovd %xmm1, -52(CTX) vmovd %xmm2, -48(CTX) vmovd %xmm3, -44(CTX) vmovd %xmm4, -40(CTX) vzeroupper leaq 8(%r11), %rsp pop %r15 pop %r14 pop %rbp pop %rbx movq LEN, %rax ret .cfi_endproc .size Poly1305BlockAVX2, .-Poly1305BlockAVX2 /** * Function description: This function is used to clear residual sensitive information in a register. * Function prototype: void Poly1305CleanRegister(); */ .globl Poly1305CleanRegister .type Poly1305CleanRegister, @function Poly1305CleanRegister: .cfi_startproc vzeroall ret .cfi_endproc .size Poly1305CleanRegister, .-Poly1305CleanRegister #endif
2301_79861745/bench_create
crypto/modes/src/asm/poly1305_x86_64_avx2.S
Unix Assembly
unknown
13,801
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "poly1305_x86_64.S" .file "poly1305_x86_64_avx512.S" .text .set ZH0, %zmm0 .set ZH1, %zmm1 .set ZH2, %zmm2 .set ZH3, %zmm3 .set ZH4, %zmm4 .set ZT0, %zmm5 .set ZT1, %zmm6 .set ZT2, %zmm7 .set ZT3, %zmm8 .set ZT4, %zmm9 .set ZMASK, %zmm10 .set ZB0, %zmm11 .set ZB1, %zmm12 .set ZB2, %zmm13 .set ZB3, %zmm14 .set ZB4, %zmm15 .set ZR0, %zmm16 .set ZR1, %zmm17 .set ZR2, %zmm18 .set ZR3, %zmm19 .set ZR4, %zmm20 .set ZS1, %zmm21 .set ZS2, %zmm22 .set ZS3, %zmm23 .set ZS4, %zmm24 .set ZM0, %zmm25 .set ZM1, %zmm26 .set ZM2, %zmm27 .set ZM3, %zmm28 .set ZM4, %zmm29 .set PADBIT_ZMM, %zmm30 .align 64 g_permd_avx512: .long 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7 .size g_permd_avx512, .-g_permd_avx512 /** * Function description: This function is implemented by x86_64 poly1305. The result is stored in ctx->acc. * Function prototype: uint32_t Poly1305Block(Poly1305_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint32_t padbit); * Input register: * CTX: address of the Poly305_Ctx structure * INP: pointer to the input data * LEN: length of the input data * PADBIT: padding bit, 0 or 1. * Change register: r8-r15, rbx, rbp, rdx, rax * Output register: * %rax: length of the remaining data to be processed * Function/Macro Call: Poly1305_MOD_MUL */ .globl Poly1305Block .type Poly1305Block, @function Poly1305Block: .cfi_startproc .align 32 cmp $256, LEN jae .Lblock_avx_pre call Poly1305Block64Bit ret .Lblock_avx_pre: andq $-16, LEN test $63, LEN jz Poly1305BlockAVX512 .Lbase2_64_avx_body: push %rbx push %rbp push %r12 push %r13 push %r14 push %r15 movq LEN, %r15 movq (CTX), ACC1 // load acc LOAD_ACC_R CTX, R0, R1, R2, ACC1, ACC2, ACC3, %r8d, %rax test %r8d, %r8d jz .Lbase2_64_avx_loop CONVERT_26TO64_PRE ACC1, ACC2, D1, D2, D3 CONVERT_26TO64 ACC1 D1, ACC2, D2, D3, ACC3 movl $0, 220(CTX) .align 32 .Lbase2_64_avx_loop: addq (INP), ACC1 adcq 8(INP), ACC2 adcq PADBIT, ACC3 lea 16(INP), INP POLY1305_MOD_MUL ACC1, ACC2, ACC3, R0, R1, R2 subq $16, %r15 test $63, %r15 movq R1, %rax jnz .Lbase2_64_avx_loop movq ACC1, (CTX) movq ACC2, 8(CTX) movq ACC3, 16(CTX) movq %r15, LEN pop %r15 pop %r14 pop %r13 pop %r12 pop %rbp pop %rbx jmp Poly1305BlockAVX512 ret .cfi_endproc .size Poly1305Block, .-Poly1305Block /** * Function description: x86_64 poly1305 AVX512 assembly acceleration implementation * Input register: * CTX: address of the Poly305_Ctx structure * INP: pointer to the input data * LEN: length of the input data * PADBIT: padding bit, 0 or 1. * Change register: zmm0-31, rax, rsp, r11, rcx, rdi, k1-k3 * Output register: * rax: length of the remaining data to be processed * Function/Macro Call: * CONVERT_64TO26 */ .globl Poly1305BlockAVX512 .type Poly1305BlockAVX512, @function .align 32 Poly1305BlockAVX512: .cfi_startproc push %rbx push %rbp push %r12 push %r13 push %r14 push %r15 vzeroupper movq (CTX), ACC1 movq 8(CTX), ACC2 movq 16(CTX), ACC3 movl 220(CTX), %r8d test %r8d, %r8d jnz .Lblock_avx512_pre movq LEN, %r15 CONVERT_64TO26 ACC1, ACC2, ACC3, %rax, %rdx movq %r15, LEN jmp .Lblock_avx512_body .Lblock_avx512_pre: movd %r14, %xmm0 movd %rbx, %xmm2 shrq $32, %r14 shrq $32, %rbx movd %r14, %xmm1 movd %rbx, %xmm3 movd %rbp, %xmm4 .Lblock_avx512_body: movl $15, %eax kmovw %eax, %k2 leaq -8(%rsp), %r11 subq $0x128, %rsp leaq 56(CTX), CTX vmovdqa g_permd_avx2(%rip), YT2 // g_permd_avx2 // Extend the precomputation table to the power of 8 andq $-512, %rsp movq $0x20, %rax vmovdqu (CTX), %xmm11 vmovdqu 16(CTX), %xmm12 vmovdqu 32(CTX), %xmm5 vmovdqu 48(CTX), %xmm13 vmovdqu 64(CTX), %xmm6 vmovdqu 80(CTX), %xmm14 vpermd ZB0, ZT2, ZR0 // 00 00 34 12 -> 14 24 34 44 vmovdqu 96(CTX), %xmm8 vpbroadcastq g_mask26(%rip), ZMASK // g_mask26 vmovdqu 112(CTX), %xmm15 vpermd ZB1, ZT2, ZR1 vmovdqu 128(CTX), %xmm9 vpermd ZT0, ZT2, ZS1 vpermd ZB2, ZT2, ZR2 vmovdqa64 ZR0, (%rsp){%k2} vpsrlq $32, ZR0, ZT0 // 14 24 34 44 -> 01 02 03 04 vpermd ZT1, ZT2, ZS2 vmovdqu64 ZR1, (%rsp, %rax){%k2} vpsrlq $32, ZR1, ZT1 vpermd ZB3, ZT2, ZR3 vmovdqa64 ZS1, 0x40(%rsp){%k2} vpermd ZT3, ZT2, ZS3 vmovdqu64 ZR2, 0x40(%rsp, %rax){%k2} vpermd ZB4, ZT2, ZR4 vmovdqa64 ZS2, 0x80(%rsp){%k2} vpermd ZT4, ZT2, ZS4 vmovdqu64 ZR3, 0x80(%rsp, %rax){%k2} vmovdqa64 ZS3, 0xc0(%rsp){%k2} vmovdqu64 ZR4, 0xc0(%rsp, %rax){%k2} vmovdqa64 ZS4, 0x100(%rsp){%k2} vpmuludq ZT0, ZR0, ZB0 vpmuludq ZT0, ZR1, ZB1 vpmuludq ZT0, ZR2, ZB2 vpmuludq ZT0, ZR3, ZB3 vpmuludq ZT0, ZR4, ZB4 vpsrlq $32, ZR2, ZT2 vpmuludq ZT1, ZS4, ZM0 vpmuludq ZT1, ZR0, ZM1 vpmuludq ZT1, ZR1, ZM2 vpmuludq ZT1, ZR2, ZM3 vpmuludq ZT1, ZR3, ZM4 vpsrlq $32, ZR3, ZT3 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpmuludq ZT2, ZS3, ZM0 vpmuludq ZT2, ZS4, ZM1 vpmuludq ZT2, ZR0, ZM2 vpmuludq ZT2, ZR1, ZM3 vpmuludq ZT2, ZR2, ZM4 vpsrlq $32, ZR4, ZT4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpmuludq ZT3, ZS2, ZM0 vpmuludq ZT3, ZS3, ZM1 vpmuludq ZT3, ZS4, ZM2 vpmuludq ZT3, ZR0, ZM3 vpmuludq ZT3, ZR1, ZM4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpmuludq ZT4, ZS1, ZM0 vpmuludq ZT4, ZS2, ZM1 vpmuludq ZT4, ZS3, ZM2 vpmuludq ZT4, ZS4, ZM3 vpmuludq ZT4, ZR0, ZM4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 // reduction vpsrlq $26, ZB3, ZM3 vpandq ZMASK, ZB3, ZB3 vpaddq ZM3, ZB4, ZB4 // d3 -> d4 vpsrlq $26, ZB0, ZM0 vpandq ZMASK, ZB0, ZB0 vpaddq ZM0, ZB1, ZB1 // d0 -> d1 vpsrlq $26, ZB4, ZM4 vpandq ZMASK, ZB4, ZB4 vmovdqu64 (INP), ZT3 vmovdqu64 64(INP), ZT4 leaq 128(INP), INP vpsrlq $26, ZB1, ZM1 vpandq ZMASK, ZB1, ZB1 vpaddq ZM1, ZB2, ZB2 // d1 -> d2 vpaddq ZM4, ZB0, ZB0 vpsllq $2, ZM4, ZM4 vpaddq ZM4, ZB0, ZB0 // d4 -> d0 vpsrlq $26, ZB2, ZM2 vpandq ZMASK, ZB2, ZB2 vpaddq ZM2, ZB3, ZB3 // d2 -> d3 vpsrlq $26, ZB0, ZM0 vpandq ZMASK, ZB0, ZB0 vpaddq ZM0, ZB1, ZB1 // d0 -> d1 vpsrlq $26, ZB3, ZM3 vpandq ZMASK, ZB3, ZB3 vpaddq ZM3, ZB4, ZB4 // d3 -> d4 vpunpcklqdq ZT4, ZT3, ZT0 vpunpckhqdq ZT4, ZT3, ZT4 // Construct R and S to make them in operable form. vmovdqu32 g_permd_avx512(%rip), ZM0 // g_permd_avx512 movl $0x7777, %eax kmovw %eax, %k1 vpermd ZR0, ZM0, ZR0 // 14 24 34 44 -> 1444 2444 3444 4444 vpermd ZR1, ZM0, ZR1 vpermd ZR2, ZM0, ZR2 vpermd ZR3, ZM0, ZR3 vpermd ZR4, ZM0, ZR4 vpermd ZB0, ZM0, ZR0{%k1} // 05 06 07 08 and 1444 2444 3444 4444 -> 1858 2868 3878 4888 vpermd ZB1, ZM0, ZR1{%k1} vpermd ZB2, ZM0, ZR2{%k1} vpermd ZB3, ZM0, ZR3{%k1} vpermd ZB4, ZM0, ZR4{%k1} vpslld $2, ZR1, ZS1 vpslld $2, ZR2, ZS2 vpslld $2, ZR3, ZS3 vpslld $2, ZR4, ZS4 vpaddd ZR1, ZS1, ZS1 vpaddd ZR2, ZS2, ZS2 vpaddd ZR3, ZS3, ZS3 vpaddd ZR4, ZS4, ZS4 // Processes the input message block and constructs the operation form. vpbroadcastq g_129(%rip), PADBIT_ZMM // g_129 vpsrlq $52, ZT0, ZT2 vpsllq $12, ZT4, ZT3 vporq ZT3, ZT2, ZT2 vpsrlq $26, ZT0, ZT1 vpsrlq $14, ZT4, ZT3 vpsrlq $40, ZT4, ZT4 // 4 vpandq ZMASK, ZT0, ZT0 // 0 vpandq ZMASK, ZT2, ZT2 // 2 vpaddq ZH2, ZT2, ZH2 subq $192, LEN jbe .Lblock_avx512_tail jmp .Lblock_avx512_loop .align 32 .Lblock_avx512_loop: // ((inp[0] * r^8 + inp[ 8]) * r^8 + inp[16]) * r^8 // ((inp[1] * r^8 + inp[ 9]) * r^8 + inp[17]) * r^7 // ((inp[2] * r^8 + inp[10]) * r^8 + inp[18]) * r^6 // ((inp[3] * r^8 + inp[11]) * r^8 + inp[19]) * r^5 // ((inp[4] * r^8 + inp[12]) * r^8 + inp[20]) * r^4 // ((inp[5] * r^8 + inp[13]) * r^8 + inp[21]) * r^3 // ((inp[6] * r^8 + inp[14]) * r^8 + inp[22]) * r^2 // ((inp[7] * r^8 + inp[15]) * r^8 + inp[23]) * r^1 // b3 = h2*r1 + h0*r3 + h1*r2 + h3*r0 + h4*5*r4 // b4 = h2*r2 + h0*r4 + h1*r3 + h3*r1 + h4*r0 // b0 = h2*5*r3 + h0*r0 + h1*5*r4 + h3*5*r2 + h4*5*r1 // b1 = h2*5*r4 + h0*r1 + h1*r0 + h3*5*r3 + h4*5*r2 // b2 = h2*r0 + h0*r2 + h1*r1 + h3*5*r4 + h4*5*r3 vpmuludq ZH2, ZR1, ZB3 vpandq ZMASK, ZT1, ZT1 // 1 vpmuludq ZH2, ZR2, ZB4 vpandq ZMASK, ZT3, ZT3 // 3 vpmuludq ZH2, ZS3, ZB0 vporq PADBIT_ZMM, ZT4, ZT4 vpmuludq ZH2, ZS4, ZB1 vpaddq ZH0, ZT0, ZH0 vpmuludq ZH2, ZR0, ZB2 vpaddq ZH1, ZT1, ZH1 vpaddq ZH3, ZT3, ZH3 vpaddq ZH4, ZT4, ZH4 vmovdqu64 (INP), ZT3 vmovdqu64 64(INP), ZT4 lea 128(INP), INP vpmuludq ZH0, ZR3, ZM3 vpmuludq ZH0, ZR4, ZM4 vpmuludq ZH0, ZR0, ZM0 vpmuludq ZH0, ZR1, ZM1 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vpmuludq ZH1, ZR2, ZM3 vpmuludq ZH1, ZR3, ZM4 vpmuludq ZH1, ZS4, ZM0 vpmuludq ZH0, ZR2, ZM2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM2, ZB2, ZB2 vpunpcklqdq ZT4, ZT3, ZT0 vpunpckhqdq ZT4, ZT3, ZT4 vpmuludq ZH3, ZR0, ZM3 vpmuludq ZH3, ZR1, ZM4 vpmuludq ZH1, ZR0, ZM1 vpmuludq ZH1, ZR1, ZM2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpmuludq ZH4, ZS4, ZM3 vpmuludq ZH4, ZR0, ZM4 vpmuludq ZH3, ZS2, ZM0 vpmuludq ZH3, ZS3, ZM1 vpmuludq ZH3, ZS4, ZM2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpmuludq ZH4, ZS1, ZM0 vpmuludq ZH4, ZS2, ZM1 vpmuludq ZH4, ZS3, ZM2 vpaddq ZM0, ZB0, ZH0 vpaddq ZM1, ZB1, ZH1 vpaddq ZM2, ZB2, ZH2 vpsrlq $52, ZT0, ZT2 vpsllq $12, ZT4, ZT3 // reduction vpsrlq $26, ZB3, ZH3 vpandq ZMASK, ZB3, ZB3 vpaddq ZH3, ZB4, ZH4 vporq ZT3, ZT2, ZT2 vpsrlq $26, ZH0, ZB0 vpandq ZMASK, ZH0, ZH0 vpaddq ZB0, ZH1, ZH1 vpandq ZMASK, ZT2, ZT2 vpsrlq $26, ZH4, ZB4 vpandq ZMASK, ZH4, ZH4 vpsrlq $26, ZH1, ZB1 vpandq ZMASK, ZH1, ZH1 vpaddq ZB1, ZH2, ZH2 vpaddq ZB4, ZH0, ZH0 vpsllq $2, ZB4, ZB4 vpaddq ZB4, ZH0, ZH0 vpaddq ZT2, ZH2, ZH2 vpsrlq $26, ZT0, ZT1 vpsrlq $26, ZH2, ZB2 vpandq ZMASK, ZH2, ZH2 vpaddq ZB2, ZB3, ZH3 vpsrlq $14, ZT4, ZT3 vpsrlq $40, ZT4, ZT4 vpandq ZMASK, ZT0, ZT0 vpsrlq $26, ZH0, ZB0 vpandq ZMASK, ZH0, ZH0 vpaddq ZB0, ZH1, ZH1 vpsrlq $26, ZH3, ZB3 vpandq ZMASK, ZH3, ZH3 vpaddq ZB3, ZH4, ZH4 subq $128, LEN ja .Lblock_avx512_loop .align 32 .Lblock_avx512_tail: vpsrlq $32, ZR0, ZR0 // 1858286838784888 -> 0105020603070408 vpsrlq $32, ZR1, ZR1 vpsrlq $32, ZS1, ZS1 vpsrlq $32, ZR2, ZR2 vpsrlq $32, ZS2, ZS2 vpsrlq $32, ZR3, ZR3 vpsrlq $32, ZS3, ZS3 vpsrlq $32, ZR4, ZR4 vpsrlq $32, ZS4, ZS4 lea (INP, LEN), INP vpaddq ZH0, ZT0, ZH0 vpmuludq ZH2, ZR1, ZB3 vpandq ZMASK, ZT1, ZT1 vpmuludq ZH2, ZR2, ZB4 vpandq ZMASK, ZT3, ZT3 vpmuludq ZH2, ZS3, ZB0 vporq PADBIT_ZMM, ZT4, ZT4 vpmuludq ZH2, ZS4, ZB1 vpaddq ZH1, ZT1, ZH1 vpmuludq ZH2, ZR0, ZB2 vpaddq ZH3, ZT3, ZH3 vpaddq ZH4, ZT4, ZH4 vmovdqu (INP), %xmm5 vmovdqu 16(INP), %xmm6 vpmuludq ZH0, ZR3, ZM3 vpmuludq ZH0, ZR4, ZM4 vpmuludq ZH0, ZR0, ZM0 vpmuludq ZH0, ZR1, ZM1 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vinserti128 $1, 32(INP), YT0, YT0 vinserti128 $1, 48(INP), YT1, YT1 vpmuludq ZH1, ZR2, ZM3 vpmuludq ZH1, ZR3, ZM4 vpmuludq ZH1, ZS4, ZM0 vpmuludq ZH0, ZR2, ZM2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM2, ZB2, ZB2 vpmuludq ZH3, ZR0, ZM3 vpmuludq ZH3, ZR1, ZM4 vpmuludq ZH1, ZR0, ZM1 vpmuludq ZH1, ZR1, ZM2 vpaddq ZM3, ZB3, ZB3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpmuludq ZH4, ZS4, ZM3 vpmuludq ZH4, ZR0, ZM4 vpmuludq ZH3, ZS2, ZM0 vpmuludq ZH3, ZS3, ZM1 vpmuludq ZH3, ZS4, ZM2 vpaddq ZM3, ZB3, ZH3 vpaddq ZM4, ZB4, ZB4 vpaddq ZM0, ZB0, ZB0 vpaddq ZM1, ZB1, ZB1 vpaddq ZM2, ZB2, ZB2 vpmuludq ZH4, ZS1, ZM0 vpmuludq ZH4, ZS2, ZM1 vpmuludq ZH4, ZS3, ZM2 vpaddq ZM0, ZB0, ZH0 vpaddq ZM1, ZB1, ZH1 vpaddq ZM2, ZB2, ZH2 // Summary of calculation results of different blocks movl $1, %eax kmovw %eax, %k3 vpermq $0xb1, ZH0, ZB0 vpermq $0xb1, ZH1, ZB1 vpermq $0xb1, ZH2, ZB2 vpermq $0xb1, ZH3, ZB3 vpermq $0xb1, ZB4, ZH4 vpaddq ZB0, ZH0, ZH0 vpaddq ZB1, ZH1, ZH1 vpaddq ZB2, ZH2, ZH2 vpaddq ZB3, ZH3, ZH3 vpaddq ZB4, ZH4, ZH4 vpermq $0x2, ZH0, ZB0 vpermq $0x2, ZH1, ZB1 vpermq $0x2, ZH2, ZB2 vpermq $0x2, ZH3, ZB3 vpermq $0x2, ZH4, ZB4 vpaddq ZB0, ZH0, ZH0 vpaddq ZB1, ZH1, ZH1 vpaddq ZB2, ZH2, ZH2 vpaddq ZB3, ZH3, ZH3 vpaddq ZB4, ZH4, ZH4 vextracti64x4 $0x1, ZH0, YB0 vextracti64x4 $0x1, ZH1, YB1 vextracti64x4 $0x1, ZH2, YB2 vextracti64x4 $0x1, ZH3, YB3 vextracti64x4 $0x1, ZH4, YB4 vpaddq ZB0, ZH0, ZH0{%k3}{z} vpaddq ZB1, ZH1, ZH1{%k3}{z} vpaddq ZB2, ZH2, ZH2{%k3}{z} vpaddq ZB3, ZH3, ZH3{%k3}{z} vpaddq ZB4, ZH4, ZH4{%k3}{z} // reduction vpsrlq $26, YH3, YB3 vpandq YMASK, YH3, YH3 vpaddq YB3, YH4, YH4 vpsrldq $6, YT0, YT2 vpsrldq $6, YT1, YT3 vpsrlq $26, YH0, YB0 vpandq YMASK, YH0, YH0 vpaddq YB0, YH1, YH1 vpunpckhqdq YT1, YT0, YT4 vpunpcklqdq YT1, YT0, YT0 vpunpcklqdq YT3, YT2, YT2 vpsrlq $26, YH4, YB4 vpandq YMASK, YH4, YH4 vpsrlq $26, YH1, YB1 vpandq YMASK, YH1, YH1 vpaddq YB1, YH2, YH2 vpsrlq $30, YT2, YT3 vpsrlq $4, YT2, YT2 vpaddq YB4, YH0, YH0 vpsllq $2, YB4, YB4 vpaddq YB4, YH0, YH0 vpsrlq $26, YT0, YT1 vpsrlq $40, YT4, YT4 vpsrlq $26, YH2, YB2 vpandq YMASK, YH2, YH2 vpaddq YB2, YH3, YH3 vpand YMASK, YT2, YT2 vpand YMASK, YT3, YT3 vpsrlq $26, YH0, YB0 vpandq YMASK, YH0, YH0 vpaddq YB0, YH1, YH1 vpaddq YH2, YT2, YH2 vpand YMASK, YT1, YT1 vpsrlq $26, YH3, YB3 vpand YMASK, YH3, YH3 vpaddq YB3, YH4, YH4 vpand YMASK, YT0, YT0 vpor g_129(%rip), YT4, YT4 addq $64, LEN jnz .Lblock_4_tail vpsubq YT2, YH2, YH2 jmp .Lblock_avx512_end .align 32 .Lblock_4_tail: BLOCK4_AVX2_TAIL YT0, YT1, YT2, YT3, YT4, YH0, YH1, YH2, YH3, YH4, YB0, YB1, YB2, YB3, YB4, YMASK, %rsp .Lblock_avx512_end: vmovd %xmm0, -56(CTX) vmovd %xmm1, -52(CTX) vmovd %xmm2, -48(CTX) vmovd %xmm3, -44(CTX) vmovd %xmm4, -40(CTX) vzeroall lea 8(%r11),%rsp pop %r15 pop %r14 pop %r13 pop %r12 pop %rbp pop %rbx movq LEN, %rax ret .cfi_endproc .size Poly1305BlockAVX512, .-Poly1305BlockAVX512 /** * Function description: This function is used to clear residual sensitive information in a register. * Function prototype: void Poly1305CleanRegister(); * Input register: None * Modify the register: * Output register: None * Function/Macro Call: None */ .globl Poly1305CleanRegister .type Poly1305CleanRegister,@function Poly1305CleanRegister: .cfi_startproc vzeroall vpxorq ZR0, ZR0, ZR0 vpxorq ZR1, ZR1, ZR1 vpxorq ZR2, ZR2, ZR2 vpxorq ZR3, ZR3, ZR3 vpxorq ZR4, ZR4, ZR4 vpxorq ZS1, ZS1, ZS1 vpxorq ZS2, ZS2, ZS2 vpxorq ZS3, ZS3, ZS3 vpxorq ZS4, ZS4, ZS4 vpxorq ZM0, ZM0, ZM0 vpxorq ZM1, ZM1, ZM1 vpxorq ZM2, ZM2, ZM2 vpxorq ZM3, ZM3, ZM3 vpxorq ZM4, ZM4, ZM4 ret .cfi_endproc .size Poly1305CleanRegister, .-Poly1305CleanRegister #endif
2301_79861745/bench_create
crypto/modes/src/asm/poly1305_x86_64_avx512.S
Unix Assembly
unknown
19,352
/* * 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_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) .file "poly1305_x86_64_macro.s" .text .align 32 g_129: .long 1<<24, 0, 1<<24, 0, 1<<24, 0, 1<<24, 0 .size g_129, .-g_129 .align 32 g_mask26: .long 0x3ffffff, 0, 0x3ffffff, 0, 0x3ffffff, 0, 0x3ffffff, 0 .size g_mask26, .-g_mask26 .align 32 g_permd_avx2: .long 2, 2, 2, 3, 2, 0, 2, 1 .size g_permd_avx2, .-g_permd_avx2 .set CTX, %rdi .set INP, %rsi .set LEN, %rdx .set PADBIT, %rcx .set ACC1, %r14 .set ACC2, %rbx .set ACC3, %rbp .set D1, %r8 .set D2, %r9 .set D3, %r10 .set R0, %r11 .set R1, %r12 .set R2, %r13 .set YH0, %ymm0 .set YH1, %ymm1 .set YH2, %ymm2 .set YH3, %ymm3 .set YH4, %ymm4 .set YT0, %ymm5 .set YT1, %ymm6 .set YT2, %ymm7 .set YT3, %ymm8 .set YT4, %ymm9 .set YMASK, %ymm10 .set YB0, %ymm11 .set YB1, %ymm12 .set YB2, %ymm13 .set YB3, %ymm14 .set YB4, %ymm15 /** * Macro description: x86_64 poly1305 big number multiplication modulo basic instruction implementation (acc1|acc2|acc3) = (acc1|acc2|acc3) * (r0|r1) mod P * Input register: * acc1-3: accumulator * r0-1: key r * r2: r1 + (r1 >> 2) * Change register: r8-r14, rbx, rbp, rax * Output register: * acc1-3: result of the one block operation */ .macro POLY1305_MOD_MUL acc1 acc2 acc3 r0 r1 r2 mulq \acc1 // acc1 * r1 movq %rax, D2 movq \r0, %rax movq %rdx, D3 mulq \acc1 // acc1 * r0 movq %rax, \acc1 movq \r0, %rax movq %rdx, D1 mulq \acc2 // acc2 * r0 addq %rax, D2 movq \r2, %rax adcq %rdx, D3 mulq \acc2 // acc2 * (r1 + (r1 >> 2)) movq \acc3, \acc2 addq %rax, \acc1 adcq %rdx, D1 imulq \r2, \acc2 // acc3 * (r1 + (r1 >> 2)) addq \acc2, D2 movq D1, \acc2 adcq $0, D3 imulq \r0, \acc3 // acc3 * r0 mov $-4, %rax addq D2, \acc2 adcq \acc3, D3 andq D3, %rax // reduction movq D3, \acc3 shrq $2, D3 andq $3, \acc3 addq D3, %rax addq %rax, \acc1 adcq $0, \acc2 adcq $0, \acc3 .endm /** * Macro description: converts 130-bit base2^26 data into base 2^64 data. * Input register: * a1: large data block 0 in the original format * d1: large data block 1 in the original format * a2: large data block 2 in the original format * d2: large data block 3 in the original format * r2: big number of data blocks 2 and 3 in the original format * a3: large data block 4 in the original format * Modify the register r8, r9, r13, r14, rbx, rbp. * Output register: * a1: bits 0 to 63 of the converted big number * a2: 64-127 bits of the converted big number * a3: 128-130 bits of the converted big number * Function/Macro Call: None */ .macro CONVERT_26TO64 a1 d1 a2 d2 r2 a3 shrq $6, \d1 shlq $52, \r2 shrq $12, \a2 addq \d1, \a1 shrq $18, \d2 addq \r2, \a1 // 1st 64bit adcq \d2, \a2 movq \a3, \d1 shlq $40, \d1 shrq $24, \a3 addq \d1, \a2 // 2nd 64bit adcq $0, \a3 // 3rd 64bit .endm /** * Macro description: converts 130-bit base2^64 data to base 2^26 data. * Input register: * a1: large data block 0 in the original format * a2: large data block 1 in the original format * a3: large data block 2 in the original format * Modify the register: r8, r9, r14, rax, rdx, rbp, rbx. * Output register: * a4: 0 to 25 digits of the converted big number * a5: 26 to 51 digits of the converted big number * a1: 52 to 77 digits of the converted big number * a2: 78 to 103 bits of the converted big number * a3: 104-130 bits of the converted big number * Function/Macro Call: None */ .macro CONVERT_64TO26 a1 a2 a3 a4 a5 movq \a1, \a4 movq \a1, \a5 andq $0x3ffffff, \a4 // 1st 26bit shrq $26, \a5 movd \a4, %xmm0 andq $0x3ffffff, \a5 // 2nd 26bit shrq $52, \a1 movd \a5, %xmm1 movq \a2, D1 movq \a2, D2 shlq $12, D1 orq D1, \a1 andq $0x3ffffff, \a1 // 3rd 26bit shrq $14, \a2 movd \a1, %xmm2 shlq $24, \a3 andq $0x3ffffff, \a2 // 4th 26bit shrq $40, D2 movd \a2, %xmm3 orq D2, \a3 // 5th 26bit movl $1, 220(CTX) movd \a3, %xmm4 .endm /** * Macro description: preprocessing of converting base2^26 data to base 2^64 * Input register: 128 bits of acc1 and acc2 data * Change register: r8-r10, r14, and rbx. * Output register: acc1, acc2, d1, d2, d3 */ .macro CONVERT_26TO64_PRE acc1 acc2 d1 d2 d3 movq $0xffffffff, \d3 // base2_26 --> base2_64 movq \acc1, \d1 movq \acc2, \d2 andq \d3, \acc1 andq \d3, \acc2 andq $-1*(1<<31), \d1 movq \d2, \d3 andq $-1*(1<<31), \d2 .endm /** * Macro description: load accumulator data and key r * Input register: in_ctx context * Modify the register: r8, r11-r14, rax, rbp, rbx. * Output register: * r0 - r2: key r * acc1 - acc3: accumulator data * flag: indicates the data organization flag of the current accumulator. * mul: r1 */ .macro LOAD_ACC_R inctx r0 r1 r2 acc1 acc2 acc3 flag mul movq 24(\inctx), \r0 // load r movq 32(\inctx), \r1 movl 220(\inctx), \flag // judge the ACC organization form. movq \r1, \r2 movq (\inctx), \acc1 // load acc shrq $2, \r2 movq 8(\inctx), \acc2 addq \r1, \r2 // R2 = R1 + (R1 >> 2) movq 16(\inctx), \acc3 movq \r1, \mul .endm /** * Macro description: The avx2 instruction set implements parallel operation of the last four blocks. * Input register: * yh0 - yh4: stores messages. * yt0 - yt4: stores keys. * yb0 - yb4: temporary storage of intermediate results * addr: stack address * Output register: * yh0 - yh4: store operation results. */ .macro BLOCK4_AVX2_TAIL yt0 yt1 yt2 yt3 yt4 yh0 yh1 yh2 yh3 yh4 yb0 yb1 yb2 yb3 yb4 ymask addr vpaddq \yt0, \yh0, \yh0 vpaddq \yt1, \yh1, \yh1 vpaddq \yt3, \yh3, \yh3 vpaddq \yt4, \yh4, \yh4 vmovdqu 0x4(\addr), \yt0 // r0^i vmovdqu 0x24(\addr), \yt1 // r1^i vmovdqu 0x64(\addr), \yt2 // r2^i vmovdqu 0xc4(\addr), \yt3 // s3^i vmovdqu 0x104(\addr), \ymask // s4^i vpmuludq \yh2, \yt0, \yb2 // b2 = h2 * r0^i vpmuludq \yh2, \yt1, \yb3 // b3 = h2 * r1^i vpmuludq \yh2, \yt2, \yb4 // b4 = h2 * r2^i vpmuludq \yh2, \yt3, \yb0 // b0 = h2 * s3^i vpmuludq \yh2, \ymask, \yb1 // b1 = h2 * s4^i vpmuludq \yh1, \yt1, \yt4 // h1 * r1^i vpmuludq \yh0, \yt1, \yh2 // h0 * r1^i vpaddq \yt4, \yb2, \yb2 // b2 += h1 * r1^i vpaddq \yh2, \yb1, \yb1 // b1 += h0 * r1^i vpmuludq \yh3, \yt1, \yt4 // h3 * r1^i vpmuludq 0x44(\addr), \yh4, \yh2 // h4 * s1^i vpaddq \yt4, \yb4, \yb4 // b4 += h3 * r1^i vpaddq \yh2, \yb0, \yb0 // b0 += h4 * s1^i vmovdqu 0x84(\addr), \yt1 // load s2^i vpmuludq \yh4, \yt0, \yt4 // h4 * r0^i vpmuludq \yh3, \yt0, \yh2 // h3 * r0^i vpaddq \yt4, \yb4, \yb4 // b4 += h4 * r0^i vpaddq \yh2, \yb3, \yb3 // b3 += h3 * r0^i vpmuludq \yh0, \yt0, \yt4 // h0 * r0^i vpmuludq \yh1, \yt0, \yh2 // h1 * r0^i vpaddq \yt4, \yb0, \yb0 // b0 += h0 * r0^i vpaddq \yh2, \yb1, \yb1 // b1 += h1 * r0^i vpmuludq \yh1, \yt2, \yt4 // h1 * r2^i vpmuludq \yh0, \yt2, \yh2 // h0 * r2^i vpaddq \yt4, \yb3, \yb3 // b3 += h1 * r2^i vpaddq \yh2, \yb2, \yb2 // b2 += h0 * r2^i vpmuludq \yh4, \yt1, \yt4 // h4 * s2^i vpmuludq \yh3, \yt1, \yh2 // h3 * s2^i vpaddq \yt4, \yb1, \yb1 // b1 += h4 * s2^i vpaddq \yh2, \yb0, \yb0 // b0 += h3 * s2^i vmovdqu 0xa4(\addr), \yh2 // load r3^i vpmuludq \yh1, \yh2, \yt4 // h1 * r3^i vpmuludq \yh0, \yh2, \yh2 // h0 * r3^i vpaddq \yt4, \yb4, \yb4 // b4 += h1 * r3^i vpaddq \yh2, \yb3, \yb3 // b3 += h0 * r3^i vpmuludq \yh4, \yt3, \yt4 // h4 * s3^i vpmuludq \yh3, \yt3, \yh2 // h3 * s3^i vpaddq \yt4, \yb2, \yb2 // b2 += h4 * s3^i vpaddq \yh2, \yb1, \yb1 // b1 += h3 * s3^i (finish) vpmuludq \yh3, \ymask, \yh3 // h3 * s4^i vpmuludq \yh4, \ymask, \yh4 // h4 * s4^i vpaddq \yb2, \yh3, \yh2 // h2 += h3 * s4^i (finish) vpaddq \yb3, \yh4, \yh3 // h3 += h4 * s4^i (finish) vpmuludq 0xe4(\addr), \yh0, \yh4 // h0 * r4^i vpmuludq \yh1, \ymask, \yh0 // h1 * s4^i vmovdqu g_mask26(%rip), \ymask vpaddq \yh4, \yb4, \yh4 // h4 += h0 * r4^i (finish) vpaddq \yh0, \yb0, \yh0 // h0 += h1 * s4^i (finish) // Summary of calculation results of different blocks vpsrldq $8, \yh0, \yt0 vpsrldq $8, \yb1, \yt1 vpaddq \yt0, \yh0, \yh0 vpsrldq $8, \yh2, \yt2 vpaddq \yt1, \yb1, \yb1 vpsrldq $8, \yh3, \yt3 vpaddq \yt2, \yh2, \yh2 vpsrldq $8, \yh4, \yt4 vpaddq \yt3, \yh3, \yh3 vpaddq \yt4, \yh4, \yh4 vpermq $0x2, \yh0, \yt0 vpermq $0x2, \yb1, \yt1 vpaddq \yt0, \yh0, \yh0 vpermq $0x2, \yh2, \yt2 vpaddq \yt1, \yb1, \yb1 vpermq $0x2, \yh3, \yt3 vpaddq \yt2, \yh2, \yh2 vpermq $0x2, \yh4, \yt4 vpaddq \yt3, \yh3, \yh3 vpaddq \yt4, \yh4, \yh4 // reduction vpsrlq $26, \yh3, \yb3 vpand \ymask, \yh3, \yh3 vpaddq \yb3, \yh4, \yh4 // h3 -> h4 vpsrlq $26, \yh0, \yb0 vpand \ymask, \yh0, \yh0 vpaddq \yb0, \yb1, \yh1 // h0 -> h1 vpsrlq $26, \yh4, \yb4 vpand \ymask, \yh4, \yh4 vpsrlq $26, \yh1, \yb1 vpand \ymask, \yh1, \yh1 vpaddq \yb1, \yh2, \yh2 // h1 -> h2 vpaddq \yb4, \yh0, \yh0 vpsllq $2, \yb4, \yb4 vpaddq \yb4, \yh0, \yh0 // h4 -> h0 vpsrlq $26, \yh2, \yb2 vpand \ymask, \yh2, \yh2 vpaddq \yb2, \yh3, \yh3 // h2 -> h3 vpsrlq $26, \yh0, \yb0 vpand \ymask, \yh0, \yh0 vpaddq \yb0, \yh1, \yh1 // h0 -> h1 vpsrlq $26, \yh3, \yb3 vpand \ymask, \yh3, \yh3 vpaddq \yb3, \yh4, \yh4 // h3 -> h4 .endm #endif
2301_79861745/bench_create
crypto/modes/src/asm/poly1305_x86_64_macro.s
Unix Assembly
unknown
12,863
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CBC) #include "bsl_err_internal.h" #include "crypt_aes.h" #include "modes_local.h" #include "crypt_errno.h" #include "crypt_modes_cbc.h" int32_t AES_CBC_EncryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx->ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((len & 0x0f) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } (void)CRYPT_AES_CBC_Encrypt(ctx->ciphCtx, in, out, len, ctx->iv); return CRYPT_SUCCESS; } int32_t AES_CBC_DecryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx->ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((len & 0x0f) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } (void)CRYPT_AES_CBC_Decrypt(ctx->ciphCtx, in, out, len, ctx->iv); return CRYPT_SUCCESS; } int32_t AES_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherUpdate(modeCtx, modeCtx->enc ? AES_CBC_EncryptBlock : AES_CBC_DecryptBlock, in, inLen, out, outLen); } int32_t AES_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CipherFinal(modeCtx, modeCtx->enc ? AES_CBC_EncryptBlock : AES_CBC_DecryptBlock, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_cbc.c
C
unknown
2,185
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CCM) #include "bsl_err_internal.h" #include "crypt_errno.h" #include "asm_aes_ccm.h" #include "ccm_core.h" #include "crypt_modes_ccm.h" #include "modes_local.h" static int32_t AesCcmBlocks(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { if (ctx->ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } XorCryptData data; data.in = in; data.out = out; data.ctr = ctx->last; data.tag = ctx->tag; uint8_t countLen = (ctx->nonce[0] & 0x07) + 1; uint32_t dataLen = len; void (*xor)(XorCryptData *data, uint32_t len) = enc ? XorInEncrypt : XorInDecrypt; void (*crypt_asm)(void *key, uint8_t *nonce, const uint8_t *in, uint8_t *out, uint32_t len) = enc ? AesCcmEncryptAsm : AesCcmDecryptAsm; crypt_asm(ctx->ciphCtx, ctx->nonce, data.in, data.out, dataLen); uint32_t tmpOffset = dataLen & 0xfffffff0; dataLen &= 0x0fU; data.in += tmpOffset; data.out += tmpOffset; if (dataLen > 0) { // data processing with less than 16 bytes (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->nonce, ctx->last, CCM_BLOCKSIZE); xor(&data, dataLen); MODE_IncCounter(ctx->nonce + CCM_BLOCKSIZE - countLen, countLen); // counter +1 } return CRYPT_SUCCESS; } int32_t MODES_AES_CCM_Encrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return CcmCrypt(ctx, in, out, len, true, AesCcmBlocks); } int32_t MODES_AES_CCM_Decrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return CcmCrypt(ctx, in, out, len, false, AesCcmBlocks); } int32_t AES_CCM_Update(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_AES_CCM_Encrypt : MODES_AES_CCM_Decrypt, &modeCtx->ccmCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_ccm.c
C
unknown
2,558
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #ifndef ASM_AES_CCM_H #define ASM_AES_CCM_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CCM) #include "crypt_utils.h" #include "modes_local.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus void AesCcmEncryptAsm(void *key, uint8_t *nonce, const uint8_t *in, uint8_t *out, uint32_t len); void AesCcmDecryptAsm(void *key, uint8_t *nonce, const uint8_t *in, uint8_t *out, uint32_t len); void XorInDecrypt(XorCryptData *data, uint32_t len); void XorInEncrypt(XorCryptData *data, uint32_t len); void XorInEncryptBlock(XorCryptData *data); void XorInDecryptBlock(XorCryptData *data); #ifdef __cplusplus } #endif // __cplusplus #endif #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_ccm.h
C
unknown
1,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" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CFB) #include "bsl_err_internal.h" #include "crypt_aes.h" #include "crypt_errno.h" #include "crypt_modes_cfb.h" #include "modes_local.h" /* Decrypt the 128-bit CFB. Here, len indicates the number of bytes to be processed. */ static int32_t CRYPT_AES_CFB16_Decrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx->modeCtx.ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } const uint8_t *input = in; uint8_t *output = out; uint8_t *tmp = ctx->modeCtx.buf; uint32_t blockSize = ctx->modeCtx.blockSize; uint32_t left = len; uint32_t i, k; // If the remaining encryption iv is not used up last time, use the part to perform exclusive OR. while (left > 0 && ctx->modeCtx.offset > 0) { uint8_t tmpInput = *input; // To support the same address in and out *(output++) = ctx->modeCtx.iv[ctx->modeCtx.offset] ^ *(input++); // Write the iv to ciphertext to prepare for the next round of encryption. ctx->modeCtx.iv[ctx->modeCtx.offset] = tmpInput; ctx->modeCtx.offset = (ctx->modeCtx.offset + 1) % blockSize; left--; } if (left >= blockSize) { uint32_t processedLen = left - (left % blockSize); (void)CRYPT_AES_CFB_Decrypt(ctx->modeCtx.ciphCtx, input, output, processedLen, ctx->modeCtx.iv); UPDATE_VALUES(left, input, output, processedLen); } if (left > 0) { // encrypt the IV int32_t ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, ctx->modeCtx.iv, tmp, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } for (i = 0, k = 0; k < left; k++, i++) { // Write the iv to ciphertext to prepare for the next round of encryption. ctx->modeCtx.iv[i] = input[k]; output[k] = input[k] ^ tmp[k]; } while (i < blockSize) { ctx->modeCtx.iv[i++] = tmp[k++]; } ctx->modeCtx.offset = (uint8_t)left; } return CRYPT_SUCCESS; } int32_t MODE_AES_CFB_Decrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->feedbackBits == 128) { // feedbackBits 128 has assembly optimization return CRYPT_AES_CFB16_Decrypt(ctx, in, out, len); } else { // no optimization return MODES_CFB_Decrypt(ctx, in, out, len); } } int32_t AES_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_CFB_Encrypt : MODE_AES_CFB_Decrypt, &modeCtx->cfbCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_cfb.c
C
unknown
3,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. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CTR) #include "bsl_err_internal.h" #include "crypt_aes.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "crypt_modes_ctr.h" #include "modes_local.h" int32_t AES_CTR_EncryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { // The ctx, in, and out pointers have been determined at the EAL layer and are not determined again. if (ctx->ciphCtx == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t left = len; const uint8_t *tmpIn = in; uint8_t *tmpOut = out; while ((ctx->offset != 0) && (left > 0)) { *(tmpOut++) = ((*(tmpIn++)) ^ (ctx->buf[ctx->offset++])); --left; ctx->offset &= (uint8_t)(ctx->blockSize - 1); } uint32_t blockSize = ctx->blockSize; // ctr supports only 16-byte block size uint32_t blocks, beCtr32; while (left >= blockSize) { blocks = left >> 4; // Shift rightwards by 4 bytes to obtain the number of blocks. beCtr32 = GET_UINT32_BE(ctx->iv, 12); // offset of 12 bytes, it is used to obtain the lower 32 bits of IV beCtr32 += blocks; if (beCtr32 < blocks) { blocks -= beCtr32; beCtr32 = 0; } // Shift leftwards by 4 bytes to obtain the length of the data involved in the calculation. uint32_t calLen = blocks << 4; (void)CRYPT_AES_CTR_Encrypt(ctx->ciphCtx, tmpIn, tmpOut, calLen, ctx->iv); left -= calLen; tmpIn += calLen; tmpOut += calLen; if (beCtr32 == 0) { // 16 - 4, the lower 32 bits are carried, and the upper 12 bytes are increased by 1. MODE_IncCounter(ctx->iv, blockSize - 4); } } if (left > 0) { (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->buf, blockSize); MODE_IncCounter(ctx->iv, ctx->blockSize); ctx->offset = 0; while ((left) > 0) { tmpOut[ctx->offset] = (tmpIn[ctx->offset]) ^ (ctx->buf[ctx->offset]); --left; ++ctx->offset; } } return CRYPT_SUCCESS; } int32_t AES_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(AES_CTR_EncryptBlock, &modeCtx->commonCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_ctr.c
C
unknown
3,061
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_ECB) #include "bsl_err_internal.h" #include "crypt_aes.h" #include "crypt_errno.h" #include "crypt_modes_ecb.h" #include "modes_local.h" int32_t AES_ECB_EncryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx->ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((len & 0x0f) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } (void)CRYPT_AES_ECB_Encrypt(ctx->ciphCtx, in, out, len); return CRYPT_SUCCESS; } int32_t AES_ECB_DecryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx->ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((len & 0x0f) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } (void)CRYPT_AES_ECB_Decrypt(ctx->ciphCtx, in, out, len); return CRYPT_SUCCESS; } int32_t AES_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherUpdate(modeCtx, modeCtx->enc ? AES_ECB_EncryptBlock : AES_ECB_DecryptBlock, in, inLen, out, outLen); } int32_t AES_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CipherFinal(modeCtx, modeCtx->enc ? AES_ECB_EncryptBlock : AES_ECB_DecryptBlock, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_ecb.c
C
unknown
2,167
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) #include "crypt_aes.h" #include "asm_aes_gcm.h" #include "modes_local.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_modes_gcm.h" int32_t AES_GCM_EncryptBlock(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx->ciphCtx == NULL) { return CRYPT_NULL_INPUT; } int32_t ret = CryptLenCheckAndRefresh(ctx, len); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t lastLen = MODES_GCM_LastHandle(ctx, in, out, len, true); // Data processing is complete. Exit. if (lastLen == len) { return CRYPT_SUCCESS; } uint32_t clen = len - lastLen; if (clen >= 64) { // If the value is greater than 64, the logic for processing large blocks is used. // invoke the assembly API uint32_t finishedLen = AES_GCM_EncryptBlockAsm(ctx, in + lastLen, out + lastLen, clen, ctx->ciphCtx); lastLen += finishedLen; // add the processed length clen -= finishedLen; // subtract the processed length } if (clen >= 16) { // Remaining 16, use small block processing logic AES_GCM_Encrypt16BlockAsm(ctx, in + lastLen, out + lastLen, clen, ctx->ciphCtx); // call the assembly API lastLen += clen & 0xfffffff0; clen = clen & 0x0f; // take the remainder of 16 } AES_GCM_ClearAsm(); // clear the Neon register if (clen > 0) { // tail processing uint32_t ctr = GET_UINT32_BE(ctx->iv, 12); ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->last, GCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t i; // encryption const uint8_t *cin = (const uint8_t *)(in + lastLen); uint8_t *cout = out + lastLen; for (i = 0; i < clen; i++) { cout[i] = cin[i] ^ ctx->last[i]; ctx->remCt[i] = cout[i]; } ctr++; PUT_UINT32_BE(ctr, ctx->iv, 12); // offset of 12 bytes, the last four bytes are used ctx->lastLen = GCM_BLOCKSIZE - clen; } return CRYPT_SUCCESS; } int32_t AES_GCM_DecryptBlock(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx->ciphCtx == NULL) { return CRYPT_NULL_INPUT; } int32_t ret = CryptLenCheckAndRefresh(ctx, len); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t lastLen = MODES_GCM_LastHandle(ctx, in, out, len, false); // Data processing is complete. Exit. if (lastLen == len) { return CRYPT_SUCCESS; } uint32_t clen = len - lastLen; if (clen >= 64) { // If the value is greater than 64, the logic for processing large blocks is used. // invoke the assembly API uint32_t finishedLen = AES_GCM_DecryptBlockAsm(ctx, in + lastLen, out + lastLen, clen, ctx->ciphCtx); lastLen += finishedLen; // add the processed length clen -= finishedLen; // subtract the processed length } if (clen >= 16) { // Remaining 16, use small block processing logic AES_GCM_Decrypt16BlockAsm(ctx, in + lastLen, out + lastLen, clen, ctx->ciphCtx); // call the assembly API lastLen += clen & 0xfffffff0; clen = clen & 0x0f; // take the remainder of 16 } AES_GCM_ClearAsm(); // clear the Neon register if (clen > 0) { // tail processing uint32_t ctr = GET_UINT32_BE(ctx->iv, 12); ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->last, GCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t i; // encryption const uint8_t *cin = (const uint8_t *)(in + lastLen); uint8_t *cout = out + lastLen; for (i = 0; i < clen; i++) { ctx->remCt[i] = cin[i]; cout[i] = cin[i] ^ ctx->last[i]; } ctr++; PUT_UINT32_BE(ctr, ctx->iv, 12); // offset of 12 bytes, the last four bytes are used ctx->lastLen = GCM_BLOCKSIZE - clen; } return CRYPT_SUCCESS; } int32_t AES_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? AES_GCM_EncryptBlock : AES_GCM_DecryptBlock, &modeCtx->gcmCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_gcm.c
C
unknown
4,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. */ #ifndef ASM_AES_GCM_H #define ASM_AES_GCM_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) #include "crypt_modes_gcm.h" #include "modes_local.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus uint32_t AES_GCM_EncryptBlockAsm(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, void *key); uint32_t AES_GCM_DecryptBlockAsm(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, void *key); void AES_GCM_Encrypt16BlockAsm(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, void *key); void AES_GCM_Decrypt16BlockAsm(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, void *key); void AES_GCM_ClearAsm(void); #ifdef __cplusplus } #endif // __cplusplus #endif #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_gcm.h
C
unknown
1,334
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_XTS) #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_aes.h" #include "crypt_modes_xts.h" #include "modes_local.h" int32_t MODES_AES_XTS_Encrypt(MODES_CipherXTSCtx *xtsCtx, const uint8_t *in, uint8_t *out, uint32_t len) { if (xtsCtx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < xtsCtx->blockSize) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } (void)CRYPT_AES_XTS_Encrypt(xtsCtx->ciphCtx, in, out, len, xtsCtx->tweak); return CRYPT_SUCCESS; } int32_t MODES_AES_XTS_Decrypt(MODES_CipherXTSCtx *xtsCtx, const uint8_t *in, uint8_t *out, uint32_t len) { if (xtsCtx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len < xtsCtx->blockSize) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_BUFF_LEN_NOT_ENOUGH); return CRYPT_MODE_BUFF_LEN_NOT_ENOUGH; } (void)CRYPT_AES_XTS_Decrypt(xtsCtx->ciphCtx, in, out, len, xtsCtx->tweak); return CRYPT_SUCCESS; } int32_t AES_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_AES_XTS_Encrypt : MODES_AES_XTS_Decrypt, &modeCtx->xtsCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_aes_xts.c
C
unknown
2,099
/* * 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_SM4) && defined(HITLS_CRYPTO_CBC) #include "bsl_err_internal.h" #include "crypt_sm4.h" #include "crypt_errno.h" #include "crypt_modes_cbc.h" #include "modes_local.h" int32_t MODE_SM4_CBC_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SM4_CBC_Encrypt(ctx->ciphCtx, in, out, len, ctx->iv); } int32_t MODE_SM4_CBC_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SM4_CBC_Decrypt(ctx->ciphCtx, in, out, len, ctx->iv); } int32_t SM4_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherUpdate(modeCtx, modeCtx->enc ? MODE_SM4_CBC_Encrypt : MODE_SM4_CBC_Decrypt, in, inLen, out, outLen); } int32_t SM4_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CipherFinal(modeCtx, modeCtx->enc ? MODE_SM4_CBC_Encrypt : MODE_SM4_CBC_Decrypt, out, outLen); } int32_t SM4_CBC_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { void *setKey = enc ? MODES_SM4_SetEncryptKey : MODES_SM4_SetDecryptKey; return MODES_CipherInitCtx(modeCtx, setKey, &modeCtx->commonCtx, key, keyLen, iv, ivLen, enc); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_cbc.c
C
unknown
2,168
/* * 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_SM4) && defined(HITLS_CRYPTO_CFB) #include "bsl_err_internal.h" #include "crypt_sm4.h" #include "crypt_errno.h" #include "crypt_modes_cfb.h" #include "modes_local.h" #include "securec.h" int32_t MODE_SM4_CFB_Encrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->feedbackBits == 128) { // feedbackBits value of 128 has assembly optimizations return CRYPT_SM4_CFB_Encrypt(ctx->modeCtx.ciphCtx, in, out, len, ctx->modeCtx.iv, &ctx->modeCtx.offset); } else { // no assembly optimization return MODES_CFB_Encrypt(ctx, in, out, len); } } int32_t MODE_SM4_CFB_Decrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->feedbackBits == 128) { // feedbackBits value of 128 has assembly optimizations return CRYPT_SM4_CFB_Decrypt(ctx->modeCtx.ciphCtx, in, out, len, ctx->modeCtx.iv, &ctx->modeCtx.offset); } else { // no assembly optimization return MODES_CFB_Decrypt(ctx, in, out, len); } } int32_t SM4_CFB_InitCtx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { int32_t ret; if (ivLen != modeCtx->cfbCtx.modeCtx.blockSize) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_IVLEN_ERROR); return CRYPT_MODES_IVLEN_ERROR; } ret = CRYPT_SM4_SetEncryptKey(modeCtx->cfbCtx.modeCtx.ciphCtx, key, keyLen); if (ret != CRYPT_SUCCESS) { return ret; } (void)memcpy_s(modeCtx->cfbCtx.modeCtx.iv, MODES_MAX_IV_LENGTH, iv, ivLen); modeCtx->enc = enc; return ret; } int32_t SM4_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODE_SM4_CFB_Encrypt : MODE_SM4_CFB_Decrypt, &modeCtx->cfbCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_cfb.c
C
unknown
2,720
/* * 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_SM4) && defined(HITLS_CRYPTO_CTR) #include "bsl_err_internal.h" #include "crypt_sm4.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_modes_ctr.h" #include "modes_local.h" int32_t MODE_SM4_CTR_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } // ctx, in, and out pointers have been determined at the EAL layer and will not be determined again if (len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t offset = MODES_CTR_LastHandle(ctx, in, out, len); uint32_t left = len - offset; const uint8_t *tmpIn = in + offset; uint8_t *tmpOut = out + offset; uint32_t blockSize = ctx->blockSize; // ctr supports only 16-byte block size uint32_t blocks, beCtr32; while (left >= blockSize) { blocks = left >> 4; // Shift rightwards by 4 bytes to obtain the number of blocks. beCtr32 = GET_UINT32_BE(ctx->iv, 12); // offset of 12 bytes to obtain the IV in lower 32 bits beCtr32 += blocks; if (beCtr32 < blocks) { blocks -= beCtr32; beCtr32 = 0; } // Shift leftwards by 4 bytes to obtain the length of the data involved in the calculation. uint32_t calLen = blocks << 4; (void)CRYPT_SM4_CTR_Encrypt(ctx->ciphCtx, tmpIn, tmpOut, calLen / ctx->blockSize, ctx->iv); left -= calLen; tmpIn += calLen; tmpOut += calLen; if (beCtr32 == 0) { // 16 - 4, The lower 32 bits are carried, and the upper 12 bytes are increased by 1. MODE_IncCounter(ctx->iv, blockSize - 4); } } MODES_CTR_RemHandle(ctx, tmpIn, tmpOut, left); return CRYPT_SUCCESS; } int32_t SM4_CTR_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_CipherInitCtx(modeCtx, MODES_SM4_SetEncryptKey, &modeCtx->commonCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(MODE_SM4_CTR_Encrypt, &modeCtx->commonCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_ctr.c
C
unknown
2,951
/* * 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_SM4) && defined(HITLS_CRYPTO_ECB) #include "bsl_err_internal.h" #include "crypt_sm4.h" #include "crypt_errno.h" #include "crypt_modes_ecb.h" #include "modes_local.h" int32_t MODE_SM4_ECB_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SM4_ECB_Encrypt(ctx->ciphCtx, in, out, len); } int32_t MODE_SM4_ECB_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SM4_ECB_Decrypt(ctx->ciphCtx, in, out, len); } int32_t SM4_ECB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { (void) iv; (void) ivLen; int32_t ret = enc ? MODES_SM4_SetEncryptKey(&modeCtx->commonCtx, key, keyLen) : MODES_SM4_SetDecryptKey(&modeCtx->commonCtx, key, keyLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } modeCtx->enc = enc; return ret; } int32_t SM4_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherUpdate(modeCtx, modeCtx->enc ? MODE_SM4_ECB_Encrypt : MODE_SM4_ECB_Decrypt, in, inLen, out, outLen); } int32_t SM4_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CipherFinal(modeCtx, modeCtx->enc ? MODE_SM4_ECB_Encrypt : MODE_SM4_ECB_Decrypt, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_ecb.c
C
unknown
2,290
/* * 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_SM4) && defined(HITLS_CRYPTO_GCM) #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_sm4.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "modes_local.h" #include "crypt_modes_gcm.h" int32_t MODES_SM4_GCM_SetKey(MODES_CipherGCMCtx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint8_t gcmKey[GCM_BLOCKSIZE] = { 0 }; int32_t ret = CRYPT_SM4_SetEncryptKey(ctx->ciphCtx, key, len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, gcmKey, gcmKey, GCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } GcmTableGen4bit(gcmKey, ctx->hTable); ctx->tagLen = 16; // The default tag length is 128 bits, that is, 16 bytes. BSL_SAL_CleanseData(gcmKey, sizeof(gcmKey)); return CRYPT_SUCCESS; } static void GcmRemHandle(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->last, GCM_BLOCKSIZE); uint32_t i; if (enc) { for (i = 0; i < len; i++) { out[i] = in[i] ^ ctx->last[i]; ctx->remCt[i] = out[i]; } } else { for (i = 0; i < len; i++) { ctx->remCt[i] = in[i]; out[i] = in[i] ^ ctx->last[i]; } } uint32_t ctr = GET_UINT32_BE(ctx->iv, 12); // offset 12 bytes and obtain the last four bytes ctr++; PUT_UINT32_BE(ctr, ctx->iv, 12); // offset 12 bytes and obtain the last four bytes ctx->lastLen = GCM_BLOCKSIZE - len; } int32_t MODES_SM4_GCM_EncryptBlock(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { ctx->plaintextLen += len; uint32_t lastLen = MODES_GCM_LastHandle(ctx, in, out, len, true); // Data processing is complete. Exit. if (lastLen == len) { return CRYPT_SUCCESS; } const uint8_t *tmpIn = in + lastLen; uint8_t *tmpOut = out + lastLen; uint32_t clen = len - lastLen; if (clen >= GCM_BLOCKSIZE) { uint32_t calLen = clen & 0xfffffff0; (void)CRYPT_SM4_CTR_Encrypt(ctx->ciphCtx, tmpIn, tmpOut, calLen / GCM_BLOCKSIZE, ctx->iv); GcmHashMultiBlock(ctx->ghash, ctx->hTable, tmpOut, calLen); clen -= calLen; tmpIn += calLen; tmpOut += calLen; } if (clen > 0) { // tail processing GcmRemHandle(ctx, tmpIn, tmpOut, clen, true); } return CRYPT_SUCCESS; } int32_t MODES_SM4_GCM_DecryptBlock(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { ctx->plaintextLen += len; uint32_t lastLen = MODES_GCM_LastHandle(ctx, in, out, len, false); // Data processing is complete. Exit. if (lastLen == len) { return CRYPT_SUCCESS; } const uint8_t *tmpIn = in + lastLen; uint8_t *tmpOut = out + lastLen; uint32_t clen = len - lastLen; if (clen >= GCM_BLOCKSIZE) { uint32_t calLen = clen & 0xfffffff0; // Obtains the length that is an integer multiple of 16 bytes. GcmHashMultiBlock(ctx->ghash, ctx->hTable, tmpIn, calLen); (void)CRYPT_SM4_CTR_Encrypt(ctx->ciphCtx, tmpIn, tmpOut, calLen / GCM_BLOCKSIZE, ctx->iv); tmpIn += calLen; tmpOut += calLen; clen -= calLen; } if (clen > 0) { // tail processing GcmRemHandle(ctx, tmpIn, tmpOut, clen, false); } return CRYPT_SUCCESS; } int32_t SM4_GCM_InitCtx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { int32_t ret = MODES_SM4_GCM_SetKey(&modeCtx->gcmCtx, key, keyLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = MODES_GCM_SetIv(&modeCtx->gcmCtx, iv, ivLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); (void)MODES_GCM_DeInitCtx(modeCtx); return ret; } modeCtx->enc = enc; return ret; } int32_t SM4_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_SM4_GCM_EncryptBlock : MODES_SM4_GCM_DecryptBlock, &modeCtx->gcmCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_gcm.c
C
unknown
4,963
/* * 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_SM4) && defined(HITLS_CRYPTO_OFB) #include "crypt_modes_ofb.h" int32_t SM4_OFB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_OFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_OFB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_OFB_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_ofb_armv8.c
C
unknown
1,058
/* * 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_SM4) && defined(HITLS_CRYPTO_OFB) #include "bsl_err_internal.h" #include "crypt_sm4.h" #include "crypt_errno.h" #include "crypt_modes_ofb.h" #include "modes_local.h" int32_t MODE_SM4_OFB_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SM4_OFB_Encrypt(ctx->ciphCtx, in, out, len, ctx->iv, &ctx->offset); } int32_t MODE_SM4_OFB_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SM4_OFB_Decrypt(ctx->ciphCtx, in, out, len, ctx->iv, &ctx->offset); } int32_t SM4_OFB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { void *setKey = MODES_SM4_SetEncryptKey; return MODES_CipherInitCtx(modeCtx, setKey, &modeCtx->commonCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_OFB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODE_SM4_OFB_Encrypt : MODE_SM4_OFB_Decrypt, &modeCtx->commonCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_ofb_x86_64.c
C
unknown
1,985
/* * 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 "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_sm4.h" #include "modes_local.h" int32_t MODES_SM4_SetEncryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len) { return CRYPT_SM4_SetEncryptKey(ctx->ciphCtx, key, len); } int32_t MODES_SM4_SetDecryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len) { return CRYPT_SM4_SetDecryptKey(ctx->ciphCtx, key, len); } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_setkey.c
C
unknown
1,046
/* * 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_SM4) && defined(HITLS_CRYPTO_XTS) #include "bsl_err_internal.h" #include "crypt_sm4.h" #include "crypt_errno.h" #include "modes_local.h" #include "crypt_modes_xts.h" int32_t MODES_SM4_XTS_Encrypt(MODES_CipherXTSCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return CRYPT_SM4_XTS_Encrypt(ctx->ciphCtx, in, out, len, ctx->tweak); } int32_t MODES_SM4_XTS_Decrypt(MODES_CipherXTSCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SM4_XTS_Decrypt(ctx->ciphCtx, in, out, len, ctx->tweak); } int32_t SM4_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_SM4_XTS_Encrypt : MODES_SM4_XTS_Decrypt, &modeCtx->xtsCtx, in, inLen, out, outLen); } int32_t SM4_XTS_InitCtx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { int32_t ret; if (key == NULL || iv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (enc) { ret = CRYPT_SM4_XTS_SetEncryptKey(modeCtx->xtsCtx.ciphCtx, key, keyLen); } else { ret = CRYPT_SM4_XTS_SetDecryptKey(modeCtx->xtsCtx.ciphCtx, key, keyLen); } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = MODES_XTS_SetIv(&modeCtx->xtsCtx, iv, ivLen); if (ret != CRYPT_SUCCESS) { (void)MODES_XTS_DeInitCtx(modeCtx); return ret; } modeCtx->enc = enc; return ret; } #endif
2301_79861745/bench_create
crypto/modes/src/asm_sm4_xts.c
C
unknown
2,373
/* * 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 CCM_CORE_H #define CCM_CORE_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_CCM #include "crypt_modes_ccm.h" #include "modes_local.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef int32_t (*CcmCore)(MODES_CipherCCMCtx *, const uint8_t *, uint8_t *, uint32_t, bool); int32_t CcmCrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc, const CcmCore func); #ifdef __cplusplus } #endif // __cplusplus #endif #endif
2301_79861745/bench_create
crypto/modes/src/ccm_core.h
C
unknown
1,018
/* * 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 GHASH_CORE_H #define GHASH_CORE_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_GCM #include "crypt_modes_gcm.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus void GcmTableGen4bitAsm(const MODES_GCM_GF128 *H, MODES_GCM_GF128 hTable[16]); void GcmMultH4bitAsm(uint8_t t[GCM_BLOCKSIZE], const MODES_GCM_GF128 hTable[16]); #ifdef __cplusplus } #endif // __cplusplus #endif #endif
2301_79861745/bench_create
crypto/modes/src/ghash_core.h
C
unknown
940
/* * 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_MODES #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_errno.h" #include "modes_local.h" #ifdef HITLS_CRYPTO_CTR #include "crypt_modes_ctr.h" #endif #ifdef HITLS_CRYPTO_CBC #include "crypt_modes_cbc.h" #endif #ifdef HITLS_CRYPTO_ECB #include "crypt_modes_ecb.h" #endif #ifdef HITLS_CRYPTO_GCM #include "crypt_modes_gcm.h" #endif #ifdef HITLS_CRYPTO_CCM #include "crypt_modes_ccm.h" #endif #ifdef HITLS_CRYPTO_XTS #include "crypt_modes_xts.h" #endif #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "crypt_modes_chacha20poly1305.h" #endif #ifdef HITLS_CRYPTO_CFB #include "crypt_modes_cfb.h" #endif #ifdef HITLS_CRYPTO_OFB #include "crypt_modes_ofb.h" #endif #ifdef HITLS_CRYPTO_WRAP #include "crypt_modes_aes_wrap.h" #endif int32_t MODE_NewCtxInternal(MODES_CipherCtx *ctx, const EAL_SymMethod *method) { ctx->commonCtx.ciphCtx = BSL_SAL_Calloc(1, method->ctxSize); if (ctx->commonCtx.ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } // block mode blockSize equal symm blockSize ctx->commonCtx.blockSize = method->blockSize; ctx->commonCtx.ciphMeth = method; ctx->commonCtx.offset = 0; return CRYPT_SUCCESS; } MODES_CipherCtx *MODES_CipherNewCtx(int32_t algId) { const EAL_SymMethod *method = EAL_GetSymMethod(algId); if (method == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } MODES_CipherCtx *ctx = BSL_SAL_Calloc(1, sizeof(MODES_CipherCtx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return ctx; } ctx->algId = algId; int32_t ret = MODE_NewCtxInternal(ctx, method); if (ret != CRYPT_SUCCESS) { BSL_SAL_Free(ctx); return NULL; } return ctx; } int32_t MODES_CipherInitCommonCtx(MODES_CipherCommonCtx *modeCtx, void *setSymKey, void *keyCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen) { int32_t ret; if (modeCtx->ciphMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (iv == NULL && ivLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ret = ((SetKey)setSymKey)(keyCtx, key, keyLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = MODES_SetIv(modeCtx, iv, ivLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } int32_t MODES_CipherInitCtx(MODES_CipherCtx *ctx, void *setSymKey, void *keyCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { ctx->enc = enc; return MODES_CipherInitCommonCtx(&ctx->commonCtx, setSymKey, keyCtx, key, keyLen, iv, ivLen); } int32_t MODE_CheckUpdateParam(uint8_t blockSize, uint32_t cacheLen, uint32_t inLen, uint32_t *outLen) { if (inLen + cacheLen < inLen) { // Integer flipping BSL_ERR_PUSH_ERROR(CRYPT_EAL_BUFF_LEN_TOO_LONG); return CRYPT_EAL_BUFF_LEN_TOO_LONG; } if ((*outLen) < ((inLen + cacheLen) / blockSize * blockSize)) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_BUFF_LEN_NOT_ENOUGH); return CRYPT_EAL_BUFF_LEN_NOT_ENOUGH; } return CRYPT_SUCCESS; } static bool IfXts(CRYPT_CIPHER_AlgId id) { return id == CRYPT_CIPHER_AES128_XTS || id == CRYPT_CIPHER_AES256_XTS || id == CRYPT_CIPHER_SM4_XTS; } static int32_t UnPaddingISO7816(const uint8_t *pad, uint32_t padLen, uint32_t *finLen) { const uint8_t *p = pad; uint32_t len = padLen - 1; while (*(p + len) == 0 && len > 0) { len--; } len = (*(p + len) == 0x80) ? len : padLen; if (len == padLen) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_CIPHER_DATA_ERROR); return CRYPT_EAL_CIPHER_DATA_ERROR; } (*finLen) = len; return CRYPT_SUCCESS; } static int32_t UnPaddingX923(const uint8_t *pad, uint32_t padLen, uint32_t *finLen) { uint32_t len, pos, i; uint32_t check = 0; len = pad[padLen - 1]; check |= (uint32_t)(len > padLen); pos = padLen - len; for (i = 0; i < padLen - 1; i++) { check |= (pad[i] * (uint32_t)(i >= pos)); } if (check != 0) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_CIPHER_DATA_ERROR); return CRYPT_EAL_CIPHER_DATA_ERROR; } (*finLen) = padLen - len; return CRYPT_SUCCESS; } static int32_t UnPaddingPkcs(const uint8_t *pad, uint32_t padLen, uint32_t *finLen) { uint32_t len, pos, i; uint32_t check = 0; len = pad[padLen - 1]; check |= (uint32_t)((len == 0) || (len > padLen)); pos = padLen - len; for (i = 0; i < padLen; i++) { check |= ((pad[i] ^ len) * (uint32_t)(i >= pos)); } if (check != 0) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_CIPHER_DATA_ERROR); return CRYPT_EAL_CIPHER_DATA_ERROR; } (*finLen) = padLen - len; return CRYPT_SUCCESS; } int32_t MODES_BlockUnPadding(int32_t padding, const uint8_t *pad, uint32_t padLen, uint32_t *dataLen) { int32_t ret = 0; uint32_t len = *dataLen; switch (padding) { case CRYPT_PADDING_ISO7816: ret = UnPaddingISO7816(pad, padLen, &len); break; case CRYPT_PADDING_X923: ret = UnPaddingX923(pad, padLen, &len); break; case CRYPT_PADDING_PKCS5: case CRYPT_PADDING_PKCS7: ret = UnPaddingPkcs(pad, padLen, &len); break; default: break; } *dataLen = len; return ret; } int32_t MODES_BlockPadding(int32_t algId, int32_t padding, uint8_t blockSize, uint8_t *data, uint8_t *dataLen) { uint8_t tempLen = *dataLen; uint8_t *pad = data + tempLen; uint8_t padLen = blockSize - tempLen; uint8_t i; *dataLen += padLen; switch (padding) { case CRYPT_PADDING_NONE: *dataLen = tempLen; if (tempLen % blockSize != 0) { return IfXts(algId) ? CRYPT_SUCCESS : CRYPT_MODE_ERR_INPUT_LEN; } break; case CRYPT_PADDING_ZEROS: for (i = 0; i < padLen; i++) { pad[i] = 0x00L; } break; case CRYPT_PADDING_ISO7816: pad[0] = 0x80; for (i = 1; i < padLen; i++) { pad[i] = 0x00L; } break; case CRYPT_PADDING_X923: for (i = 0; i < padLen - 1; i++) { pad[i] = 0x00L; } pad[padLen - 1] = padLen; break; case CRYPT_PADDING_PKCS5: case CRYPT_PADDING_PKCS7: for (i = 0; i < padLen; i++) { pad[i] = padLen; } break; default: *dataLen = tempLen; return CRYPT_INVALID_ARG; } return CRYPT_SUCCESS; } int32_t MODES_CipherUpdateCache(MODES_CipherCtx *ctx, void *blockUpdate, const uint8_t **in, uint32_t *inLen, uint8_t **out, uint32_t *outLen) { int32_t ret; uint8_t blockSize = ctx->commonCtx.blockSize; // Process the cache. If there is cached data, the cache data is padded into a block first. if (ctx->dataLen > 0) { uint8_t padding = blockSize - ctx->dataLen; padding = (*inLen) > (padding) ? padding : (uint8_t)(*inLen); if (padding != 0) { (void)memcpy_s(ctx->data + ctx->dataLen, (EAL_MAX_BLOCK_LENGTH - ctx->dataLen), (*in), padding); (*inLen) -= padding; (*in) += padding; ctx->dataLen += padding; } } // No block is formed, return. if (ctx->dataLen != blockSize) { return CRYPT_SUCCESS; } // If the block is padded, perform operations on this block first. if (ctx->enc) { ret = ((EncryptBlock)blockUpdate)(&ctx->commonCtx, ctx->data, *out, blockSize); } else { // If it's decryption and the cached data + input data is equal to blockSize, // may be it's the last piece of data with padding, the data is cached in the ctx and left for final processing. if ((*inLen) == 0 && (ctx->pad != CRYPT_PADDING_NONE)) { (*outLen) = 0; return CRYPT_SUCCESS; } ret = ((DecryptBlock)blockUpdate)(&ctx->commonCtx, ctx->data, *out, blockSize); } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ctx->dataLen = 0; (*outLen) = blockSize; (*out) += blockSize; return CRYPT_SUCCESS; } int32_t MODES_CipherFinal(MODES_CipherCtx *modeCtx, void *blockUpdate, uint8_t *out, uint32_t *outLen) { int32_t ret; uint32_t outLenTemp; if (out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (modeCtx->pad != CRYPT_PADDING_NONE && (*outLen) < modeCtx->commonCtx.blockSize) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_BUFF_LEN_NOT_ENOUGH); return CRYPT_EAL_BUFF_LEN_NOT_ENOUGH; } if (modeCtx->enc) { ret = MODES_BlockPadding(modeCtx->algId, modeCtx->pad, modeCtx->commonCtx.blockSize, modeCtx->data, &modeCtx->dataLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (modeCtx->dataLen == 0) { *outLen = modeCtx->dataLen; return CRYPT_SUCCESS; } ret = ((EncryptBlock)blockUpdate)(&modeCtx->commonCtx, modeCtx->data, out, modeCtx->dataLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *outLen = modeCtx->dataLen; } else { if (modeCtx->dataLen == 0) { *outLen = modeCtx->dataLen; return CRYPT_SUCCESS; } ret = ((DecryptBlock)blockUpdate)(&modeCtx->commonCtx, modeCtx->data, out, modeCtx->dataLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } outLenTemp = modeCtx->dataLen; ret = MODES_BlockUnPadding(modeCtx->pad, out, modeCtx->commonCtx.blockSize, &outLenTemp); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *outLen = outLenTemp; } modeCtx->dataLen = 0; return ret; } int32_t MODES_CipherUpdate(MODES_CipherCtx *modeCtx, void *blockUpdate, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { int32_t ret; uint32_t tmpLen = inLen; const uint8_t *tmpIn = in; uint8_t *tmpOut = (uint8_t *)out; ret = MODE_CheckUpdateParam(modeCtx->commonCtx.blockSize, modeCtx->dataLen, inLen, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *outLen = 0; ret = MODES_CipherUpdateCache(modeCtx, blockUpdate, &tmpIn, &tmpLen, &tmpOut, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (tmpLen == 0) { return CRYPT_SUCCESS; } uint8_t left = tmpLen % modeCtx->commonCtx.blockSize; uint32_t len = tmpLen - left; if (len > 0) { if (modeCtx->enc) { ret = ((EncryptBlock)blockUpdate)(&modeCtx->commonCtx, tmpIn, tmpOut, len); } else { // If it's decryption and the cached data + input data is equal to blockSize, // may be it's the last piece of data with padding, the data is cached in the ctx and left for final processing. if ((modeCtx->pad != CRYPT_PADDING_NONE) && left == 0) { left = modeCtx->commonCtx.blockSize; len -= modeCtx->commonCtx.blockSize; } if (len > 0) { ret = ((DecryptBlock)blockUpdate)(&modeCtx->commonCtx, tmpIn, tmpOut, len); } } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } // Process the new cache. if (left > 0) { (void)memcpy_s(modeCtx->data, modeCtx->commonCtx.blockSize, tmpIn + len, left); modeCtx->dataLen = left; } // The encryption/decryption is successful. OutLen is updated. (*outLen) += len; return CRYPT_SUCCESS; } void MODES_Clean(MODES_CipherCommonCtx *ctx) { if (ctx == NULL) { return; } BSL_SAL_CleanseData((void *)(ctx->buf), MODES_MAX_BUF_LENGTH); BSL_SAL_CleanseData((void *)(ctx->iv), MODES_MAX_IV_LENGTH); ctx->ciphMeth->cipherDeInitCtx(ctx->ciphCtx); ctx->offset = 0; ctx->ivIndex = 0; } int32_t MODES_CipherDeInitCtx(MODES_CipherCtx *modeCtx) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } (void)memset_s(modeCtx->data, EAL_MAX_BLOCK_LENGTH, 0, EAL_MAX_BLOCK_LENGTH); modeCtx->dataLen = 0; modeCtx->pad = CRYPT_PADDING_NONE; MODES_Clean(&modeCtx->commonCtx); return CRYPT_SUCCESS; } void MODES_CipherFreeCtx(MODES_CipherCtx *modeCtx) { if (modeCtx == NULL) { return; } (void)MODES_CipherDeInitCtx(modeCtx); BSL_SAL_FREE(modeCtx->commonCtx.ciphCtx); BSL_SAL_Free(modeCtx); } int32_t MODES_SetIv(MODES_CipherCommonCtx *ctx, const uint8_t *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != ctx->blockSize) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_IVLEN_ERROR); return CRYPT_MODES_IVLEN_ERROR; } if (memcpy_s(ctx->iv, MODES_MAX_IV_LENGTH, val, len) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } ctx->offset = 0; // If the IV value is changed, the original offset is useless. ctx->ivIndex = 0; return CRYPT_SUCCESS; } int32_t MODES_GetIv(MODES_CipherCommonCtx *ctx, uint8_t *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t ivLen = ctx->blockSize; if (len != ivLen) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } (void)memcpy_s(val, len, ctx->iv, ivLen); return CRYPT_SUCCESS; } int32_t MODES_CipherCtrl(MODES_CipherCtx *ctx, int32_t opt, void *val, uint32_t len) { switch (opt) { case CRYPT_CTRL_REINIT_STATUS: (void)memset_s(ctx->data, EAL_MAX_BLOCK_LENGTH, 0, EAL_MAX_BLOCK_LENGTH); ctx->dataLen = 0; ctx->pad = CRYPT_PADDING_NONE; return MODES_SetIv(&ctx->commonCtx, val, len); case CRYPT_CTRL_GET_IV: return MODES_GetIv(&ctx->commonCtx, (uint8_t *)val, len); default: if (ctx->commonCtx.ciphMeth == NULL || ctx->commonCtx.ciphMeth->cipherCtrl == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_TYPE_ERROR); return CRYPT_MODES_CTRL_TYPE_ERROR; } return ctx->commonCtx.ciphMeth->cipherCtrl(ctx->commonCtx.ciphCtx, opt, val, len); } } int32_t MODES_CipherStreamProcess(void *processFuncs, void *ctx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { int32_t ret; if (inLen == 0) { *outLen = 0; return CRYPT_SUCCESS; } if (inLen > *outLen) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_BUFF_LEN_NOT_ENOUGH); return CRYPT_MODE_BUFF_LEN_NOT_ENOUGH; } ret = ((CipherStreamProcess)(processFuncs))(ctx, in, out, inLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } *outLen = inLen; return CRYPT_SUCCESS; } // Note that CRYPT_PADDING_ZEROS cannot restore the plaintext length. // If uses it, need to maintain the length themselves int32_t MODES_SetPaddingCheck(int32_t pad) { if (pad >= CRYPT_PADDING_MAX_COUNT || pad < CRYPT_PADDING_NONE) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_PADDING_NOT_SUPPORT); return CRYPT_MODES_PADDING_NOT_SUPPORT; } return CRYPT_SUCCESS; } int32_t MODES_SetEncryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len) { // The ctx and key have been checked at the EAL layer and will not be checked again here. // The keyMethod will support registration in the future. Therefore, this check is added. if (ctx == NULL || ctx->ciphMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ctx->ciphMeth->setEncryptKey(ctx->ciphCtx, key, len); } int32_t MODES_SetDecryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len) { // The ctx and key have been checked at the EAL layer and will not be checked again here. // The keyMethod will support registration in the future. Therefore, this check is added. if (ctx == NULL || ctx->ciphMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ctx->ciphMeth->setDecryptKey(ctx->ciphCtx, key, len); } #endif // HITLS_CRYPTO_MODES
2301_79861745/bench_create
crypto/modes/src/modes.c
C
unknown
17,606
/* * 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_CBC #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_modes_cbc.h" #include "modes_local.h" #define CBC_UPDATE_VALUES(l, i, o, len) \ do { \ (l) -= (len); \ (i) += (len); \ (o) += (len); \ } while (false) int32_t MODES_CBC_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { uint32_t blockSize = ctx->blockSize; int32_t ret; uint8_t *iv = ctx->iv; uint8_t *tmp = ctx->buf; uint32_t left = len; const uint8_t *input = in; uint8_t *output = out; // The ctx, in, and out pointers have been determined at the EAL layer and are not determined again. if ((left % blockSize) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } while (left >= blockSize) { /* Plaintext XOR IV. BlockSize must be an integer multiple of 4 bytes. */ DATA32_XOR(input, iv, tmp, blockSize); ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, tmp, output, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } /* The current encryption result is used as the next IV value. */ iv = output; /* Offset length is the size of integer multiple blocks */ CBC_UPDATE_VALUES(left, input, output, blockSize); } if (memcpy_s(ctx->iv, MODES_MAX_IV_LENGTH, iv, blockSize) != EOK) { BSL_ERR_PUSH_ERROR(CRYPT_SECUREC_FAIL); return CRYPT_SECUREC_FAIL; } return CRYPT_SUCCESS; } int32_t MODES_CBC_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { const uint8_t *iv = ctx->iv; uint8_t *tmp = ctx->buf; uint32_t blockSize = ctx->blockSize; uint32_t left = len; uint8_t *output = out; uint8_t tmpChar; const uint8_t *input = in; // The ctx, in, and out pointers have been determined at the EAL layer and are not determined again. if ((left % blockSize) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } // In the case where the input and output are at the same address, // the judgment should be placed outside the while loop. Otherwise, the performance will be affected. if (in != out) { while (left >= blockSize) { int32_t ret = ctx->ciphMeth->decryptBlock(ctx->ciphCtx, input, tmp, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } /* The ciphertext is used as the next IV value. BlockSize must be an integer multiple of 4 bytes. */ DATA32_XOR(iv, tmp, output, blockSize); iv = input; CBC_UPDATE_VALUES(left, input, output, blockSize); } if (iv != ctx->iv) { (void)memcpy_s(ctx->iv, MODES_MAX_IV_LENGTH, iv, blockSize); } } else { while (left >= blockSize) { int32_t ret = ctx->ciphMeth->decryptBlock(ctx->ciphCtx, input, tmp, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } for (uint32_t i = 0; i < blockSize; i++) { tmpChar = input[i]; output[i] = tmp[i] ^ ctx->iv[i]; ctx->iv[i] = tmpChar; } CBC_UPDATE_VALUES(left, input, output, blockSize); } } return CRYPT_SUCCESS; } MODES_CipherCtx *MODES_CBC_NewCtx(int32_t algId) { return MODES_CipherNewCtx(algId); } int32_t MODES_CBC_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { void *setKeyFuncs = enc ? modeCtx->commonCtx.ciphMeth->setEncryptKey : modeCtx->commonCtx.ciphMeth->setDecryptKey; return MODES_CipherInitCtx(modeCtx, setKeyFuncs, modeCtx->commonCtx.ciphCtx, key, keyLen, iv, ivLen, enc); } int32_t MODES_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherUpdate(modeCtx, modeCtx->enc ? MODES_CBC_Encrypt : MODES_CBC_Decrypt, in, inLen, out, outLen); } int32_t MODES_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CipherFinal(modeCtx, modeCtx->enc ? MODES_CBC_Encrypt : MODES_CBC_Decrypt, out, outLen); } int32_t MODES_CBC_DeInitCtx(MODES_CipherCtx *modeCtx) { return MODES_CipherDeInitCtx(modeCtx); } int32_t MODES_CBC_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen) { int32_t ret; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (cmd) { case CRYPT_CTRL_SET_PADDING: if (val == NULL || valLen != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } ret = MODES_SetPaddingCheck(*(int32_t *)val); if (ret != CRYPT_SUCCESS) { return ret; } modeCtx->pad = *(int32_t *)val; return CRYPT_SUCCESS; case CRYPT_CTRL_GET_PADDING: if (val == NULL || valLen != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(int32_t *)val = modeCtx->pad; return CRYPT_SUCCESS; case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || valLen != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } *(int32_t *)val = modeCtx->commonCtx.ciphMeth->blockSize; return CRYPT_SUCCESS; default: return MODES_CipherCtrl(modeCtx, cmd, val, valLen); } } void MODES_CBC_FreeCtx(MODES_CipherCtx *modeCtx) { MODES_CipherFreeCtx(modeCtx); } int32_t MODES_CBC_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_CBC: case CRYPT_CIPHER_AES192_CBC: case CRYPT_CIPHER_AES256_CBC: #ifdef HITLS_CRYPTO_AES return AES_CBC_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif case CRYPT_CIPHER_SM4_CBC: #ifdef HITLS_CRYPTO_SM4 return SM4_CBC_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CBC_Update(modeCtx, in, inLen, out, outLen); } } int32_t MODES_CBC_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void)param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_SM4_CBC: #ifdef HITLS_CRYPTO_SM4 return SM4_CBC_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CBC_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } } int32_t MODES_CBC_FinalEx(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_CBC: case CRYPT_CIPHER_AES192_CBC: case CRYPT_CIPHER_AES256_CBC: #ifdef HITLS_CRYPTO_AES return AES_CBC_Final(modeCtx, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif case CRYPT_CIPHER_SM4_CBC: #ifdef HITLS_CRYPTO_SM4 return SM4_CBC_Final(modeCtx, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CBC_Final(modeCtx, out, outLen); } } #endif // HITLS_CRYPTO_CBC
2301_79861745/bench_create
crypto/modes/src/modes_cbc.c
C
unknown
8,810
/* * 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_CCM #include <stdint.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "modes_local.h" #include "ccm_core.h" #include "crypt_modes_ccm.h" #include "crypt_modes.h" void XorInEncrypt(XorCryptData *data, uint32_t len) { uint32_t i; for (i = 0; i < len; i++) { data->tag[i] ^= data->in[i]; data->out[i] = data->in[i] ^ data->ctr[i]; } } void XorInEncryptBlock(XorCryptData *data) { DATA64_XOR(data->in, data->tag, data->tag, CCM_BLOCKSIZE); DATA64_XOR(data->in, data->ctr, data->out, CCM_BLOCKSIZE); } void XorInDecrypt(XorCryptData *data, uint32_t len) { uint32_t i; // Decryption for (i = 0; i < len; i++) { data->out[i] = data->in[i] ^ data->ctr[i]; data->tag[i] ^= data->out[i]; } } void XorInDecryptBlock(XorCryptData *data) { DATA64_XOR(data->in, data->ctr, data->out, CCM_BLOCKSIZE); DATA64_XOR(data->out, data->tag, data->tag, CCM_BLOCKSIZE); } // Process the remaining data in the last update. static uint32_t CcmLastHandle(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { uint32_t lastLen = (ctx->lastLen < len) ? ctx->lastLen : len; if (ctx->lastLen > 0) { XorCryptData data; data.in = in; data.out = out; data.ctr = &(ctx->last[CCM_BLOCKSIZE - ctx->lastLen]); data.tag = &(ctx->tag[CCM_BLOCKSIZE - ctx->lastLen]); if (enc) { XorInEncrypt(&data, lastLen); } else { XorInDecrypt(&data, lastLen); } // Refresh the remaining length. // The judgment of the function entry ensures that lastLen does not exceed ctx->lastLen, // and this forcible transition does not occur truncation. ctx->lastLen -= (uint8_t)lastLen; } return lastLen; } static void RefreshNonce(MODES_CipherCCMCtx *ctx) { if ((ctx->nonce[0] & (~0x07)) != 0) { /** * RFC_3610-2.3 * Bit Number Contents * ---------- ---------------------- * 7 Reserved (always zero) * 6 Reserved (always zero) * 5 ... 3 Zero * 2 ... 0 L' */ ctx->nonce[0] &= 0x07; /** * RFC_3610-2.3 * Octet Number Contents * ------------ --------- * 0 Flags * 1 ... 15-L Nonce N * 16-L ... 15 Counter i */ uint8_t i; uint8_t l = ctx->nonce[0] + 1; for (i = 1; i < l; i++) { ctx->nonce[CCM_BLOCKSIZE - 1 - i] = 0; } /** * RFC_3610-2.3 * The message is encrypted by XORing the octets of message m with the * first l(m) octets of the concatenation of S_1, S_2, S_3, ... . Note * that S_0 is not used to encrypt the message. */ ctx->nonce[CCM_BLOCKSIZE - 1] = 1; } } static int32_t TagInit(MODES_CipherCCMCtx *ctx) { if (ctx->tagInit == 0) { int32_t ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->nonce, ctx->tag, CCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ctx->tagInit = 1; } return CRYPT_SUCCESS; } static int32_t CcmBlocks(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { XorCryptData data; data.in = in; data.out = out; data.ctr = ctx->last; data.tag = ctx->tag; uint8_t countLen = (ctx->nonce[0] & 0x07) + 1; uint32_t dataLen = len; void (*xorBlock)(XorCryptData *data) = enc ? XorInEncryptBlock : XorInDecryptBlock; void (*xor)(XorCryptData *data, uint32_t len) = enc ? XorInEncrypt : XorInDecrypt; while (dataLen >= CCM_BLOCKSIZE) { // process the integer multiple of 16bytes data (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->nonce, ctx->last, CCM_BLOCKSIZE); xorBlock(&data); (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->tag, ctx->tag, CCM_BLOCKSIZE); MODE_IncCounter(ctx->nonce + CCM_BLOCKSIZE - countLen, countLen); // counter +1 dataLen -= CCM_BLOCKSIZE; data.in += CCM_BLOCKSIZE; data.out += CCM_BLOCKSIZE; } if (dataLen > 0) { // process the integer multiple of 16bytes data (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->nonce, ctx->last, CCM_BLOCKSIZE); xor(&data, dataLen); MODE_IncCounter(ctx->nonce + CCM_BLOCKSIZE - countLen, countLen); // counter +1 } return CRYPT_SUCCESS; } // Enc: true for encryption and false for decryption. int32_t CcmCrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc, const CcmCore func) { if (ctx == NULL || ctx->ciphCtx == NULL || in == NULL || out == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len > ctx->msgLen) { // The message length is exceeded. BSL_ERR_PUSH_ERROR(CRYPT_MODES_MSGLEN_OVERFLOW); return CRYPT_MODES_MSGLEN_OVERFLOW; } int32_t ret = TagInit(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // Determine whether to start encryption and update the nonce information. RefreshNonce(ctx); uint32_t lastLen = CcmLastHandle(ctx, in, out, len, enc); if (lastLen != 0 && ctx->lastLen == 0) { ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->tag, ctx->tag, CCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } // Data processing is complete and exits in advance. if (lastLen == len) { ctx->msgLen -= len; // Refresh the remaining length. return CRYPT_SUCCESS; } uint32_t tmpLen = len - lastLen; ret = func(ctx, in + lastLen, out + lastLen, tmpLen, enc); if (ret != CRYPT_SUCCESS) { // Returned by the internal function. No redundant push err is required. return ret; } ctx->lastLen = (CCM_BLOCKSIZE - (tmpLen % CCM_BLOCKSIZE)) % CCM_BLOCKSIZE; ctx->msgLen -= len; // Refresh the remaining length. return CRYPT_SUCCESS; } int32_t MODES_CCM_Encrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return CcmCrypt(ctx, in, out, len, true, CcmBlocks); } int32_t MODES_CCM_Decrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return CcmCrypt(ctx, in, out, len, false, CcmBlocks); } // 7 <= ivLen <= 13 static int32_t SetIv(MODES_CipherCCMCtx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } /** * RFC_3610-2 * Valid values of L range between 2 octets and 8 octets */ // L = 15 - ivLen that is 7 <= ivLen <= 13 if (len < 7 || len > 13) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_IVLEN_ERROR); return CRYPT_MODES_IVLEN_ERROR; } // The previous judgment limits the size of iv to [7, 13]. Therefore, forcible conversion does not cause truncation. uint8_t l = CCM_BLOCKSIZE - 1 - (uint8_t)len; // Clear data. void *ciphCtx = ctx->ciphCtx; // Handle used by the method const EAL_SymMethod *ciphMeth = ctx->ciphMeth; // algorithm method uint8_t tagLen = ctx->tagLen; (void)memset_s(ctx, sizeof(MODES_CipherCCMCtx), 0, sizeof(MODES_CipherCCMCtx)); ctx->ciphCtx = ciphCtx; ctx->ciphMeth = ciphMeth; ctx->tagLen = tagLen; uint8_t m = (ctx->tagLen - 2) / 2; // M' = (M - 2)/2 ctx->nonce[0] = (uint8_t)((l - 1) & 0x7); // set L ctx->nonce[0] |= (m << 3); // set M. The default value of TagLen is 16bytes. (bit2 bit3 bit4) indicating the tagLen (void)memcpy_s(ctx->nonce + 1, CCM_BLOCKSIZE - 1, val, len); return CRYPT_SUCCESS; } // The input data is the uint64_t. static int32_t SetMsgLen(MODES_CipherCCMCtx *ctx, const void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != sizeof(uint64_t)) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_MSGLEN_ERROR); return CRYPT_MODES_CTRL_MSGLEN_ERROR; } if ((ctx->nonce[0] & 0x40) != 0) { // If aad has been set, msgLen cannot be set. BSL_ERR_PUSH_ERROR(CRYPT_MODES_AAD_IS_SET_ERROR); return CRYPT_MODES_AAD_IS_SET_ERROR; } const uint64_t msgLen = *(const uint64_t *)val; uint8_t l = (ctx->nonce[0] & 0x7) + 1; /** * RFC_3610-7 * octet aligned message of arbitrary length, up to 2^(8*L) octets, * and octet aligned arbitrary additional authenticated data, up to * 2^64 octets */ if (l < 8 && msgLen >= ((uint64_t)1 << (8 * l))) { // When l is 8, the condition must be met. BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_MSGLEN_ERROR); return CRYPT_MODES_CTRL_MSGLEN_ERROR; } uint8_t i; /** * RFC_3610-2.3 * Octet Number Contents * ------------ --------- * 0 Flags * 1 ... 15-L Nonce N * 16-L ... 15 Counter i */ uint8_t bytes[sizeof(uint64_t)]; Uint64ToBeBytes(msgLen, bytes); for (i = 0; i < l; i++) { ctx->nonce[CCM_BLOCKSIZE - 1 - i] = bytes[8 - 1 - i]; // 8 bit msgLen information } ctx->msgLen = msgLen; return CRYPT_SUCCESS; } static int32_t SetTagLen(MODES_CipherCCMCtx *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_MODES_CTRL_TAGLEN_ERROR); return CRYPT_MODES_CTRL_TAGLEN_ERROR; } if ((ctx->nonce[0] & 0x40) != 0) { // If aad has been set, tagLen cannot be set. BSL_ERR_PUSH_ERROR(CRYPT_MODES_AAD_IS_SET_ERROR); return CRYPT_MODES_AAD_IS_SET_ERROR; } /** * RFC_3610-2 * Valid values are 4, 6, 8, 10, 12, 14, and 16 octets */ uint32_t tagLen = *((const uint32_t *)val); // 4 <= tagLen <= 16 and tagLen is an even number. if (tagLen > 16 || tagLen < 4 || ((tagLen & 0x01) != 0)) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_TAGLEN_ERROR); return CRYPT_MODES_CTRL_TAGLEN_ERROR; } ctx->tagLen = (uint8_t)tagLen; uint8_t m = (ctx->tagLen - 2) / 2; // M' = (M - 2)/2 ctx->nonce[0] &= ~(0x7 << 3); // Clear 3|4|5 three bits ctx->nonce[0] |= (m << 3); // Set M return CRYPT_SUCCESS; } static uint32_t XorAadLen(MODES_CipherCCMCtx *ctx, uint32_t aadLen) { /** * RFC_3610-2.2 * First two octets Followed by Comment * ----------------- ---------------- ------------------------------- * 0x0000 Nothing Reserved * 0x0001 ... 0xFEFF Nothing For 0 < l(a) < (2^16 - 2^8) * 0xFF00 ... 0xFFFD Nothing Reserved * 0xFFFE 4 octets of l(a) For (2^16 - 2^8) <= l(a) < 2^32 * 0xFFFF 8 octets of l(a) For 2^32 <= l(a) < 2^64 */ uint32_t record; /* In order to record aadlen */ // For 0 < l(a) < (2^16 - 2^8) if (aadLen < (((size_t)1 << 16) - ((size_t)1 << 8))) { /* 0 < l(a) < (2^16 - 2^8) */ record = 2; /* 2 octets */ ctx->tag[1] ^= (uint8_t)aadLen; ctx->tag[0] ^= (uint8_t)(aadLen >> 8); // 1byte = 8bit } else { /* (2^16 - 2^8) <= l(a) < 2^32 */ record = 6; /* 6 octets */ ctx->tag[5] ^= (uint8_t)aadLen; // base offset = 5 ctx->tag[4] ^= (uint8_t)(aadLen >> 8); // 1byte(off 5 -> 4) == 8bit ctx->tag[3] ^= (uint8_t)(aadLen >> 16); // 2byte(off 5 -> 3) == 16bit ctx->tag[2] ^= (uint8_t)(aadLen >> 24); // 3byte(off 5 -> 2) == 24bit ctx->tag[1] ^= 0xfe; ctx->tag[0] ^= 0xff; } return record; } // 0 < aadLen < 2^32 static int32_t SetAad(MODES_CipherCCMCtx *ctx, const void *val, uint32_t len) { if ((ctx->nonce[0] & 0x40) != 0 || ctx->tagInit != 0) { // If aad has been set, the setting cannot be repeated. BSL_ERR_PUSH_ERROR(CRYPT_MODES_AAD_REPEAT_SET_ERROR); return CRYPT_MODES_AAD_REPEAT_SET_ERROR; } if (len == 0) { // If AAD is 0, returned directly. return CRYPT_SUCCESS; } if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } // bit6 Adata ctx->nonce[0] |= 0x40; // X_1 := E( K, B_0 ) int32_t ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->nonce, ctx->tag, CCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ctx->tagInit = 1; uint32_t i; uint32_t aadLen = len; uint32_t record = XorAadLen(ctx, aadLen); const uint8_t *aad = val; uint32_t use = CCM_BLOCKSIZE - record; use = (use < aadLen) ? use : aadLen; for (i = 0; i < use; i++) { ctx->tag[i + record] ^= aad[i]; } aad += use; aadLen -= use; ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->tag, ctx->tag, CCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } while (aadLen > 0) { uint32_t blockLen = (aadLen < CCM_BLOCKSIZE) ? aadLen : CCM_BLOCKSIZE; for (i = 0; i < blockLen; i++) { ctx->tag[i] ^= aad[i]; } aad += blockLen; aadLen -= blockLen; ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->tag, ctx->tag, CCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } return CRYPT_SUCCESS; } static int32_t CtrTagCalc(MODES_CipherCCMCtx *ctx) { /** * RFC_3610-2.3 * The authentication value U is computed by encrypting T with the ciphCtx * stream block S_0 and truncating it to the desired length. */ ctx->nonce[0] &= 0x07; // update the nonce uint8_t l = (ctx->nonce[0] & 0x07) + 1; (void)memset_s(ctx->nonce + CCM_BLOCKSIZE - l, l, 0, l); int32_t ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->nonce, ctx->nonce, CCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static int32_t GetTag(MODES_CipherCCMCtx *ctx, void *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != ctx->tagLen) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_TAGLEN_ERROR); return CRYPT_MODES_TAGLEN_ERROR; } if (ctx->msgLen != 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_MSGLEN_LEFT_ERROR); return CRYPT_MODES_MSGLEN_LEFT_ERROR; } int32_t ret = TagInit(ctx); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = CtrTagCalc(ctx); if (ret != CRYPT_SUCCESS) { // An error is reported by the internal function, and no redundant pushErr is required. return ret; } if (ctx->lastLen != 0) { ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->tag, ctx->tag, CCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } uint32_t i; uint8_t *tag = val; /** * RFC_3610-2.3 * U := T XOR first-M-bytes( S_0 ) */ for (i = 0; i < len; i++) { tag[i] = ctx->tag[i] ^ ctx->nonce[i]; } return CRYPT_SUCCESS; } int32_t MODES_CCM_Ctrl(MODES_CCM_Ctx *modeCtx, int32_t opt, void *val, uint32_t len) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (opt) { case CRYPT_CTRL_SET_IV: case CRYPT_CTRL_REINIT_STATUS: return SetIv(&modeCtx->ccmCtx, val, len); case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || len != sizeof(uint32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } *(int32_t *)val = 1; return CRYPT_SUCCESS; case CRYPT_CTRL_SET_TAGLEN: return SetTagLen(&modeCtx->ccmCtx, val, len); case CRYPT_CTRL_SET_MSGLEN: return SetMsgLen(&modeCtx->ccmCtx, val, len); case CRYPT_CTRL_SET_AAD: return SetAad(&modeCtx->ccmCtx, val, len); case CRYPT_CTRL_GET_TAG: return GetTag(&modeCtx->ccmCtx, val, len); default: BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_TYPE_ERROR); return CRYPT_MODES_CTRL_TYPE_ERROR; } } MODES_CCM_Ctx *MODES_CCM_NewCtx(int32_t algId) { const EAL_SymMethod *method = EAL_GetSymMethod(algId); if (method == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } MODES_CCM_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(MODES_CCM_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return ctx; } ctx->algId = algId; ctx->ccmCtx.ciphCtx = BSL_SAL_Calloc(1, method->ctxSize); if (ctx->ccmCtx.ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_Free(ctx); return NULL; } ctx->ccmCtx.ciphMeth = method; return ctx; } int32_t MODES_CCM_InitCtx(MODES_CCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void)param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = modeCtx->ccmCtx.ciphMeth->setEncryptKey(modeCtx->ccmCtx.ciphCtx, key, keyLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } modeCtx->ccmCtx.tagLen = 16; // 16 default tag len, set iv need ret = SetIv(&modeCtx->ccmCtx, iv, ivLen); if (ret != CRYPT_SUCCESS) { modeCtx->ccmCtx.ciphMeth->cipherDeInitCtx(modeCtx->ccmCtx.ciphCtx); BSL_ERR_PUSH_ERROR(ret); return ret; } modeCtx->enc = enc; return ret; } int32_t MODES_CCM_Update(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_CCM_Encrypt : MODES_CCM_Decrypt, &modeCtx->ccmCtx, in, inLen, out, outLen); } int32_t MODES_CCM_Final(MODES_CCM_Ctx *modeCtx, uint8_t *out, uint32_t *outLen) { (void) modeCtx; (void) out; (void) outLen; return CRYPT_EAL_CIPHER_FINAL_WITH_AEAD_ERROR; } int32_t MODES_CCM_DeInitCtx(MODES_CCM_Ctx *modeCtx) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } const EAL_SymMethod *ciphMeth = modeCtx->ccmCtx.ciphMeth; void *ciphCtx = modeCtx->ccmCtx.ciphCtx; modeCtx->ccmCtx.ciphMeth->cipherDeInitCtx(modeCtx->ccmCtx.ciphCtx); BSL_SAL_CleanseData(&modeCtx->ccmCtx, sizeof(MODES_CipherCCMCtx)); modeCtx->ccmCtx.ciphMeth = ciphMeth; modeCtx->ccmCtx.ciphCtx = ciphCtx; return CRYPT_SUCCESS; } void MODES_CCM_FreeCtx(MODES_CCM_Ctx *modeCtx) { if (modeCtx == NULL) { return; } modeCtx->ccmCtx.ciphMeth->cipherDeInitCtx(modeCtx->ccmCtx.ciphCtx); BSL_SAL_FREE(modeCtx->ccmCtx.ciphCtx); BSL_SAL_CleanseData(&modeCtx->ccmCtx, sizeof(MODES_CipherCCMCtx)); BSL_SAL_FREE(modeCtx); } int32_t MODES_CCM_UpdateEx(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_CCM: case CRYPT_CIPHER_AES192_CCM: case CRYPT_CIPHER_AES256_CCM: #ifdef HITLS_CRYPTO_AES return AES_CCM_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CCM_Update(modeCtx, in, inLen, out, outLen); } } #endif
2301_79861745/bench_create
crypto/modes/src/modes_ccm.c
C
unknown
20,629
/* * 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_CFB #include <stdbool.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_modes_cfb.h" #include "modes_local.h" #include "crypt_errno.h" #include "crypt_local_types.h" #include "crypt_modes.h" /* 8-bit | 64-bit | 128-bit CFB encryption. Here, len indicates the number of bytes to be processed. */ static int32_t MODES_CFB_BytesEncrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { const uint8_t *input = in; uint8_t *output = out; uint8_t *tmp = ctx->modeCtx.buf; uint32_t blockSize = ctx->modeCtx.blockSize; uint32_t feedbackBytes = ctx->feedbackBits >> 3; // right shifting by 3 to obtain the number of bytes. uint32_t left = len; uint32_t i, k; // If the remaining encryption iv is not used up last time, use that part to perform XOR. while (left > 0 && ctx->modeCtx.offset > 0) { ctx->modeCtx.iv[ctx->modeCtx.offset] ^= *(input++); *(output++) = ctx->modeCtx.iv[ctx->modeCtx.offset]; left--; ctx->modeCtx.offset = (ctx->modeCtx.offset + 1) % blockSize; } while (left > 0) { // Encrypt the IV. int32_t ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, ctx->modeCtx.iv, tmp, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } i = 0; // The first (blockSize - feedbackBytes) bytes are filled with the least significant bytes of the previous IV. if (blockSize - feedbackBytes > 0) { (void)memmove_s(&ctx->modeCtx.iv[0], blockSize, &ctx->modeCtx.iv[feedbackBytes], blockSize - feedbackBytes); i = blockSize - feedbackBytes; } // The input data is XORed with the encrypted IV, and the current ciphertext is sent to the next IV. if (left >= feedbackBytes) { // Enter the last feedbackBytes in ciphertext. for (k = 0; i < blockSize; i++, k++) { output[k] = input[k] ^ tmp[k]; ctx->modeCtx.iv[i] = output[k]; } UPDATE_VALUES(left, input, output, feedbackBytes); } else { // Enter the last feedbackBytes in ciphertext. // The cache with insufficient feedbackBytes is used to encrypt the IV. for (k = 0; k < left; k++) { output[k] = input[k] ^ tmp[k]; ctx->modeCtx.iv[i++] = output[k]; } while (i < blockSize) { ctx->modeCtx.iv[i++] = tmp[k++]; } ctx->modeCtx.offset = (uint8_t)(blockSize - feedbackBytes + left); left = 0; } } return CRYPT_SUCCESS; } static int32_t MODES_CFB128_BytesEncrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { const uint8_t *inp = in; uint8_t *outp = out; uint32_t blockSize = ctx->modeCtx.blockSize; uint8_t *iv = ctx->modeCtx.iv; uint32_t left = len; int32_t ret; while (left > 0 && ctx->modeCtx.offset > 0) { iv[ctx->modeCtx.offset] ^= *(inp++); *(outp++) = iv[ctx->modeCtx.offset]; left--; ctx->modeCtx.offset = (ctx->modeCtx.offset + 1) % blockSize; } while (left >= blockSize) { ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, iv, iv, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } #ifdef FORCE_ADDR_ALIGN for (uint32_t i = 0; i < blockSize; i++) { iv[i] ^= inp[i]; out[i] = iv[i]; } #else for (uint32_t i = 0; i < blockSize; i += sizeof(uint64_t)) { *((uint64_t *)(iv + i)) ^= *((const uint64_t *)(inp + i)); *((uint64_t *)(outp + i)) = *((uint64_t *)(iv + i)); } #endif UPDATE_VALUES(left, inp, outp, blockSize); } if (left > 0) { ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, iv, iv, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t i = 0; for (; i < left; i++) { iv[i] ^= inp[i]; outp[i] = iv[i]; } ctx->modeCtx.offset = (uint8_t)left; } return CRYPT_SUCCESS; } /* 8-bit | 64-bit | 128-bit CFB decryption. Here, len indicates the number of bytes to be processed. */ static int32_t MODES_CFB_BytesDecrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { const uint8_t *input = in; uint8_t *output = out; uint8_t *tmp = ctx->modeCtx.buf; uint32_t blockSize = ctx->modeCtx.blockSize; uint32_t feedbackBytes = ctx->feedbackBits >> 3; uint32_t left = len; uint32_t i, k; // If the remaining encryption iv is not used up last time, use that part to perform XOR. while (left > 0 && ctx->modeCtx.offset > 0) { uint8_t tmpInput = *input; // To support the same address in and out *(output++) = ctx->modeCtx.iv[ctx->modeCtx.offset] ^ *(input++); ctx->modeCtx.iv[ctx->modeCtx.offset] = tmpInput; left--; ctx->modeCtx.offset = (ctx->modeCtx.offset + 1) % blockSize; } while (left > 0) { // Encrypt the IV. int32_t ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, ctx->modeCtx.iv, tmp, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } i = 0; // The first (blockSize - feedbackBytes) bytes are filled with the least significant bytes of the previous IV. if (blockSize - feedbackBytes > 0) { (void)memmove_s(&ctx->modeCtx.iv[0], blockSize, &ctx->modeCtx.iv[feedbackBytes], blockSize - feedbackBytes); i = blockSize - feedbackBytes; } // The input data is XORed with the encrypted IV, and the current ciphertext is sent to the next IV. if (left >= feedbackBytes) { // Enter the last feedbackBytes in ciphertext. for (k = 0; i < blockSize; i++, k++) { ctx->modeCtx.iv[i] = input[k]; output[k] = input[k] ^ tmp[k]; } UPDATE_VALUES(left, input, output, feedbackBytes); } else { // Enter the last feedbackBytes in ciphertext. // The cache with insufficient feedbackBytes is used to encrypt the IV. for (k = 0; k < left; k++) { ctx->modeCtx.iv[i++] = input[k]; output[k] = input[k] ^ tmp[k]; } while (i < blockSize) { ctx->modeCtx.iv[i++] = tmp[k++]; } ctx->modeCtx.offset = (uint8_t)(blockSize - feedbackBytes + left); left = 0; } } return CRYPT_SUCCESS; } static int32_t MODES_CFB128_BytesDecrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { const uint8_t *inp = in; uint8_t *outp = out; uint8_t *iv = ctx->modeCtx.iv; uint32_t blockSize = ctx->modeCtx.blockSize; uint32_t left = len; int32_t ret; while (left > 0 && ctx->modeCtx.offset > 0) { uint8_t tmpInput = *inp; // To support the same address in and out *(outp++) = iv[ctx->modeCtx.offset] ^ *(inp++); iv[ctx->modeCtx.offset] = tmpInput; left--; ctx->modeCtx.offset = (ctx->modeCtx.offset + 1) % blockSize; } while (left >= blockSize) { ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, iv, iv, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } #ifdef FORCE_ADDR_ALIGN for (uint32_t i = 0; i < blockSize; i++) { uint8_t c = inp[i]; outp[i] = iv[i] ^ c; iv[i] = c; } #else for (uint32_t i = 0; i < blockSize; i += sizeof(uint64_t)) { uint64_t ti = *((const uint64_t *)(inp + i)); *((uint64_t *)(outp + i)) = *((uint64_t *)(iv + i)) ^ ti; *((uint64_t *)(iv + i)) = ti; } #endif UPDATE_VALUES(left, inp, outp, blockSize); } if (left > 0) { ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, iv, iv, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t i = 0; for (; i < left; i++) { uint8_t b = inp[i]; outp[i] = iv[i] ^ b; iv[i] = b; } ctx->modeCtx.offset = (uint8_t)left; } return CRYPT_SUCCESS; } static int32_t Cfb1Crypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, bool enc) { int32_t ret; uint8_t *tmp = ctx->modeCtx.buf; uint32_t blockSize = ctx->modeCtx.blockSize; uint32_t i; // Encrypt the IV. ret = ctx->modeCtx.ciphMeth->encryptBlock(ctx->modeCtx.ciphCtx, ctx->modeCtx.iv, tmp, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } for (i = 0; i < blockSize - 1; i++) { // All bytes are shifted left by one bit, // and the least significant bits are obtained by shifting right by 7 bits from the next byte. ctx->modeCtx.iv[i] = (ctx->modeCtx.iv[i] << 1) | (ctx->modeCtx.iv[i + 1] >> 7); } if (enc) { *out = tmp[0] ^ *in; // The last byte is shifted to the left by one bit and then filled in the ciphertext. // Shifted to the right by 7 bits to obtain the first bit of the byte. ctx->modeCtx.iv[i] = (ctx->modeCtx.iv[i] << 1) | (*out >> 7); } else { // The last byte is shifted to the left by one bit and then filled in the ciphertext. // Shifted to the right by 7 bits to obtain the first bit of the byte. ctx->modeCtx.iv[i] = (ctx->modeCtx.iv[i] << 1) | (*in >> 7); *out = tmp[0] ^ *in; } return CRYPT_SUCCESS; } /* 1-bit CFB. Here, len indicates the number of bits to be processed. */ int32_t MODES_CFB_BitCrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { int32_t ret; uint8_t tmp[2]; uint32_t pos; for (uint32_t i = 0; i < len; i++) { // 7 - i % 8 is used to obtain the number of bits in the byte stream (high bit -> low bit). pos = 7 - i % 8; // Obtain the bits to be encrypted. 0x80 indicates a byte whose most significant bit is 1. tmp[0] = ((in[i / 8] & (1 << pos)) > 0) ? 0x80 : 0; ret = Cfb1Crypt(ctx, &tmp[0], &tmp[1], enc); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } // Divide by 8 to obtain the current byte position. Assign the out encryption bit to 0. out[i / 8] = out[i / 8] & ~(1u << pos); // Divide by 8 to obtain the current byte position. tmpOut[0] >> 7 to obtain the most significant bit. out[i / 8] |= (tmp[1] >> 7) << pos; // Assign the out encryption bit to the encrypted/decrypted value. } (void)memset_s(tmp, sizeof(tmp), 0, sizeof(tmp)); return CRYPT_SUCCESS; } int32_t MODES_CFB_Encrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (ctx->feedbackBits) { case 1: return MODES_CFB_BitCrypt(ctx, in, out, len * 8, true); // Each byte occupies 8 bits. case 8: case 64: return MODES_CFB_BytesEncrypt(ctx, in, out, len); case 128: return MODES_CFB128_BytesEncrypt(ctx, in, out, len); default: BSL_ERR_PUSH_ERROR(CRYPT_MODES_ERR_FEEDBACKSIZE); return CRYPT_MODES_ERR_FEEDBACKSIZE; } } int32_t MODES_CFB_Decrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (ctx->feedbackBits) { case 1: // 1-bit cfb. Convert the length of bytes to the length of bits. return MODES_CFB_BitCrypt(ctx, in, out, len * 8, false); // Each byte occupies 8 bits. case 8: // 8-bit cfb case 64: // 64-bit cfb return MODES_CFB_BytesDecrypt(ctx, in, out, len); case 128: // 128-bit cfb return MODES_CFB128_BytesDecrypt(ctx, in, out, len); default: BSL_ERR_PUSH_ERROR(CRYPT_MODES_ERR_FEEDBACKSIZE); return CRYPT_MODES_ERR_FEEDBACKSIZE; } } static int32_t SetFeedbackSize(MODES_CipherCFBCtx *ctx, const uint32_t *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_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } if (*val != 1 && *val != 8 && *val != 64 && *val != 128) { // set 1|8|64|128 feedbackbits only BSL_ERR_PUSH_ERROR(CRYPT_MODES_ERR_FEEDBACKSIZE); return CRYPT_MODES_ERR_FEEDBACKSIZE; } if (*val > (uint32_t)(ctx->modeCtx.blockSize * 8)) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_ERR_FEEDBACKSIZE); return CRYPT_MODES_ERR_FEEDBACKSIZE; } ctx->feedbackBits = (uint8_t)*val; return CRYPT_SUCCESS; } static int32_t GetFeedbackSize(MODES_CipherCFBCtx *ctx, uint32_t *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_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } *val = ctx->feedbackBits; return CRYPT_SUCCESS; } int32_t MODES_CFB_Ctrl(MODES_CFB_Ctx *modeCtx, int32_t opt, void *val, uint32_t len) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (opt) { case CRYPT_CTRL_REINIT_STATUS: modeCtx->cfbCtx.cacheIndex = 0; return MODES_SetIv(&modeCtx->cfbCtx.modeCtx, val, len); case CRYPT_CTRL_GET_IV: return MODES_GetIv(&modeCtx->cfbCtx.modeCtx, (uint8_t *)val, len); case CRYPT_CTRL_SET_FEEDBACKSIZE: return SetFeedbackSize(&modeCtx->cfbCtx, (uint32_t *)val, len); case CRYPT_CTRL_GET_FEEDBACKSIZE: return GetFeedbackSize(&modeCtx->cfbCtx, (uint32_t *)val, len); case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || len != sizeof(uint32_t)) { return CRYPT_INVALID_ARG; } *(int32_t *)val = 1; return CRYPT_SUCCESS; default: if (modeCtx->cfbCtx.modeCtx.ciphMeth == NULL || modeCtx->cfbCtx.modeCtx.ciphMeth->cipherCtrl == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_TYPE_ERROR); return CRYPT_MODES_CTRL_TYPE_ERROR; } return modeCtx->cfbCtx.modeCtx.ciphMeth->cipherCtrl(modeCtx->cfbCtx.modeCtx.ciphCtx, opt, val, len); } } MODES_CFB_Ctx *MODES_CFB_NewCtx(int32_t algId) { const EAL_SymMethod *method = EAL_GetSymMethod(algId); if (method == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } MODES_CFB_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(MODES_CFB_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return ctx; } ctx->algId = algId; ctx->cfbCtx.modeCtx.ciphCtx = BSL_SAL_Calloc(1, method->ctxSize); if (ctx->cfbCtx.modeCtx.ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_Free(ctx); return NULL; } ctx->cfbCtx.cacheIndex = 0; uint8_t blockBits = method->blockSize * 8; if (blockBits <= 128) { ctx->cfbCtx.feedbackBits = blockBits; } else { ctx->cfbCtx.feedbackBits = 128; } ctx->cfbCtx.modeCtx.blockSize = method->blockSize; ctx->cfbCtx.modeCtx.ciphMeth = method; ctx->cfbCtx.modeCtx.offset = 0; return ctx; } int32_t MODES_CFB_InitCtx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { modeCtx->cfbCtx.cacheIndex = 0; modeCtx->enc = enc; return MODES_CipherInitCommonCtx(&modeCtx->cfbCtx.modeCtx, modeCtx->cfbCtx.modeCtx.ciphMeth->setEncryptKey, modeCtx->cfbCtx.modeCtx.ciphCtx, key, keyLen, iv, ivLen); } int32_t MODES_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_CFB_Encrypt : MODES_CFB_Decrypt, &modeCtx->cfbCtx, in, inLen, out, outLen); } int32_t MODES_CFB_Final(MODES_CFB_Ctx *modeCtx, uint8_t *out, uint32_t *outLen) { (void) modeCtx; (void) out; *outLen = 0; return CRYPT_SUCCESS; } int32_t MODES_CFB_DeInitCtx(MODES_CFB_Ctx *modeCtx) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } MODES_Clean(&modeCtx->cfbCtx.modeCtx); BSL_SAL_CleanseData((void *)(modeCtx->cfbCtx.cipherCache[0]), DES_BLOCK_BYTE_NUM * 3); // 3 ciphertext caches return CRYPT_SUCCESS; } void MODES_CFB_FreeCtx(MODES_CFB_Ctx *modeCtx) { if (modeCtx == NULL) { return ; } (void)MODES_CFB_DeInitCtx(modeCtx); BSL_SAL_Free(modeCtx->cfbCtx.modeCtx.ciphCtx); BSL_SAL_Free(modeCtx); } int32_t MODES_CFB_InitCtxEx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void) param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ivLen != modeCtx->cfbCtx.modeCtx.blockSize) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_IVLEN_ERROR); return CRYPT_MODES_IVLEN_ERROR; } switch (modeCtx->algId) { case CRYPT_CIPHER_SM4_CFB: #ifdef HITLS_CRYPTO_SM4 return SM4_CFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } } int32_t MODES_CFB_UpdateEx(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_CFB: case CRYPT_CIPHER_AES192_CFB: case CRYPT_CIPHER_AES256_CFB: #ifdef HITLS_CRYPTO_AES return AES_CFB_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif case CRYPT_CIPHER_SM4_CFB: #ifdef HITLS_CRYPTO_SM4 return SM4_CFB_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CFB_Update(modeCtx, in, inLen, out, outLen); } } #endif
2301_79861745/bench_create
crypto/modes/src/modes_cfb.c
C
unknown
19,758
/* * 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_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include <stdint.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "modes_local.h" #include "poly1305_core.h" #include "crypt_modes.h" void Poly1305SetKey(Poly1305Ctx *ctx, const uint8_t key[POLY1305_KEYSIZE]) { // RFC_7539-2.5 ctx->r[0] = GET_UINT32_LE(key, 0); ctx->r[1] = GET_UINT32_LE(key, 4); ctx->r[2] = GET_UINT32_LE(key, 8); ctx->r[3] = GET_UINT32_LE(key, 12); ctx->s[0] = GET_UINT32_LE(key, 16); ctx->s[1] = GET_UINT32_LE(key, 20); ctx->s[2] = GET_UINT32_LE(key, 24); ctx->s[3] = GET_UINT32_LE(key, 28); /** * r[3], r[7], r[11], and r[15] are required to have their top four * bits clear (be smaller than 16) * r[4], r[8], and r[12] are required to have their bottom two bits * clear (be divisible by 4) */ ctx->r[0] &= 0x0FFFFFFF; ctx->r[1] &= 0x0FFFFFFC; ctx->r[2] &= 0x0FFFFFFC; ctx->r[3] &= 0x0FFFFFFC; ctx->acc[0] = 0; ctx->acc[1] = 0; ctx->acc[2] = 0; ctx->acc[3] = 0; ctx->acc[4] = 0; ctx->acc[5] = 0; (void)memset_s(ctx->last, sizeof(ctx->last), 0, sizeof(ctx->last)); ctx->lastLen = 0; Poly1305InitForAsm(ctx); // Information such as tables required for initializing the assembly } void Poly1305Update(Poly1305Ctx *ctx, const uint8_t *data, uint32_t dataLen) { uint32_t i; uint32_t len = dataLen; const uint8_t *off = data; if (ctx->lastLen != 0) { uint64_t end = (uint64_t)len + ctx->lastLen; if (end > POLY1305_BLOCKSIZE) { end = POLY1305_BLOCKSIZE; } for (i = ctx->lastLen; i < end; i++) { ctx->last[i] = *off; off++; } len -= (uint32_t)(end - ctx->lastLen); if (end == POLY1305_BLOCKSIZE) { (void)Poly1305Block(ctx, ctx->last, POLY1305_BLOCKSIZE, 1); } else { ctx->lastLen = (uint32_t)end; return; } } /** * Add one bit beyond the number of octets. For a 16-byte block, * this is equivalent to adding 2^128 to the number. */ if (len >= POLY1305_BLOCKSIZE) { (void)Poly1305Block(ctx, off, len & 0xfffffff0, 1); } ctx->lastLen = len & 0x0f; // mod 16; off += len - ctx->lastLen; for (i = 0; i < ctx->lastLen; i++) { ctx->last[i] = off[i]; } return; } void Poly1305Final(Poly1305Ctx *ctx, uint8_t mac[POLY1305_TAGSIZE]) { uint32_t len = ctx->lastLen; if (len > 0) { /** * For the shorter block, it can be 2^120, 2^112, or any power of two that is * evenly divisible by 8, all the way down to 2^8 */ ctx->last[len++] = 1; /** * If the block is not 17 bytes long (the last block), pad it with * zeros. This is meaningless if you are treating the blocks as * numbers. */ while (len < POLY1305_BLOCKSIZE) { ctx->last[len++] = 0; } Poly1305Block(ctx, ctx->last, POLY1305_BLOCKSIZE, 0); ctx->lastLen = 0; } Poly1305Last(ctx, mac); } int32_t MODES_CHACHA20POLY1305_Encrypt(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = ctx->method->encryptBlock(ctx->key, in, out, len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } Poly1305Update(&(ctx->polyCtx), out, len); ctx->cipherTextLen += (uint64_t)len; return CRYPT_SUCCESS; } int32_t MODES_CHACHA20POLY1305_Decrypt(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } Poly1305Update(&(ctx->polyCtx), in, len); ctx->cipherTextLen += (uint64_t)len; int32_t ret = ctx->method->decryptBlock(ctx->key, in, out, len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static void CipherTextPad(MODES_CipherChaChaPolyCtx *ctx) { /** * The ciphertext * padding2 -- the padding is up to 15 zero bytes, and it brings * the total length so far to an integral multiple of 16. If the * length of the ciphertext was already an integral multiple of 16 * bytes, this field is zero-length. */ Poly1305Ctx *polyCtx = &(ctx->polyCtx); uint32_t len = polyCtx->lastLen; if (len != 0) { const uint8_t pad2[POLY1305_BLOCKSIZE] = {0}; Poly1305Update(polyCtx, pad2, POLY1305_BLOCKSIZE - len); } uint8_t pad[POLY1305_BLOCKSIZE]; /** * The length of the additional data in octets (as a 64-bit * little-endian integer). */ PUT_UINT64_LE(ctx->aadLen, pad, 0); // The first eight bytes are padded with the length of the AAD. /** * The length of the ciphertext in octets (as a 64-bit littleendian * integer). */ PUT_UINT64_LE(ctx->cipherTextLen, pad, 8); // The last 8 bytes are padded the length of the ciphertext. Poly1305Update(polyCtx, pad, POLY1305_BLOCKSIZE); } static int32_t GetTag(MODES_CipherChaChaPolyCtx *ctx, uint8_t *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != POLY1305_TAGSIZE) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_TAGLEN_ERROR); return CRYPT_MODES_TAGLEN_ERROR; } CipherTextPad(ctx); Poly1305Final(&(ctx->polyCtx), val); return CRYPT_SUCCESS; } static int32_t SetIv(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *iv, uint32_t ivLen) { /** * RFC_7539-2.6 * ChaCha20 as specified here requires a 96-bit nonce. * So if the provided nonce is only 64-bit, then the first 32 * bits of the nonce will be set to a constant number. This will * usually be zero, but for protocols with multiple senders it may be * different for each sender, but should be the same for all * invocations of the function with the same key by a particular * sender. */ if (iv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } // 96 bits or 64 bits, that is, 12 bytes or 8 bytes. if (ivLen != 12 && ivLen != 8) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_IVLEN_ERROR); return CRYPT_MODES_IVLEN_ERROR; } int32_t ret; uint8_t block[POLY1305_KEYSIZE] = { 0 }; if (ivLen == 8) { // If the length of the IV is 8, 0 data must be padded before. uint8_t tmpBuff[12] = { 0 }; (void)memcpy_s(tmpBuff + 4, sizeof(tmpBuff) - 4, iv, ivLen); // // 4 bytes 0 data must be padded before. ret = ctx->method->cipherCtrl(ctx->key, CRYPT_CTRL_SET_IV, tmpBuff, sizeof(tmpBuff)); // Clear sensitive data. (void)BSL_SAL_CleanseData(tmpBuff, sizeof(tmpBuff)); } else { ret = ctx->method->cipherCtrl(ctx->key, CRYPT_CTRL_SET_IV, (void *)(uintptr_t)iv, ivLen); } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } /** * RFC_7539-2.6 * The block counter is set to zero */ uint8_t initCount[4] = { 0 }; ret = ctx->method->cipherCtrl(ctx->key, CRYPT_CTRL_SET_COUNT, initCount, sizeof(initCount)); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = ctx->method->encryptBlock(ctx->key, block, block, POLY1305_KEYSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } Poly1305SetKey(&(ctx->polyCtx), block); /** * RFC_7539-2.8 * the ChaCha20 encryption function is called to encrypt the * plaintext, using the same key and nonce, and with the initial * counter set to 1 */ initCount[0] = 0x01; ret = ctx->method->cipherCtrl(ctx->key, CRYPT_CTRL_SET_COUNT, initCount, sizeof(initCount)); // 4bytes count if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } // Information that needs to be reset. ctx->aadLen = 0; ctx->cipherTextLen = 0; return ret; } // Set the AAD information. The AAD information can be set only once. static int32_t SetAad(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *aad, uint32_t aadLen) { /** * RFC_7539-2.8 * The AAD * padding1 -- the padding is up to 15 zero bytes, and it brings * the total length so far to an integral multiple of 16. If the * length of the AAD was already an integral multiple of 16 bytes, * this field is zero-length. */ if (ctx->aadLen != 0 || ctx->cipherTextLen != 0) { // aad is set BSL_ERR_PUSH_ERROR(CRYPT_MODES_AAD_REPEAT_SET_ERROR); return CRYPT_MODES_AAD_REPEAT_SET_ERROR; } if (aadLen == 0) { return CRYPT_SUCCESS; } if (aad == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } const uint8_t pad[16] = { 0 }; ctx->aadLen = aadLen; Poly1305Update(&(ctx->polyCtx), aad, aadLen); uint32_t padLen = (16 - aadLen % 16) % 16; Poly1305Update(&(ctx->polyCtx), pad, padLen); return CRYPT_SUCCESS; } int32_t MODES_CHACHA20POLY1305_SetEncryptKey(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ctx->method->setEncryptKey(ctx->key, key, len); } int32_t MODES_CHACHA20POLY1305_SetDecryptKey(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return ctx->method->setDecryptKey(ctx->key, key, len); } int32_t MODES_CHACHA20POLY1305_Ctrl(MODES_CHACHAPOLY_Ctx *modeCtx, int32_t opt, void *val, uint32_t len) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } switch (opt) { case CRYPT_CTRL_REINIT_STATUS: return SetIv(&modeCtx->chachaCtx, val, len); case CRYPT_CTRL_GET_TAG: return GetTag(&modeCtx->chachaCtx, val, len); case CRYPT_CTRL_SET_AAD: return SetAad(&modeCtx->chachaCtx, val, len); case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || len != sizeof(uint32_t)) { return CRYPT_INVALID_ARG; } *(int32_t *)val = 1; return CRYPT_SUCCESS; default: if (modeCtx->chachaCtx.method == NULL || modeCtx->chachaCtx.method->cipherCtrl == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_TYPE_ERROR); return CRYPT_MODES_CTRL_TYPE_ERROR; } return modeCtx->chachaCtx.method->cipherCtrl(modeCtx->chachaCtx.key, opt, val, len); } } MODES_CHACHAPOLY_Ctx *MODES_CHACHA20POLY1305_NewCtx(int32_t algId) { const EAL_SymMethod *method = EAL_GetSymMethod(algId); if (method == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } MODES_CHACHAPOLY_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(MODES_CHACHAPOLY_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return ctx; } ctx->algId = algId; ctx->chachaCtx.key = BSL_SAL_Calloc(1, method->ctxSize); if (ctx->chachaCtx.key == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_Free(ctx); return NULL; } ctx->chachaCtx.method = method; return ctx; } int32_t MODES_CHACHA20POLY1305_InitCtx(MODES_CHACHAPOLY_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void)param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = enc ? modeCtx->chachaCtx.method->setEncryptKey(modeCtx->chachaCtx.key, key, keyLen) : modeCtx->chachaCtx.method->setDecryptKey(modeCtx->chachaCtx.key, key, keyLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = SetIv(&modeCtx->chachaCtx, iv, ivLen); if (ret != CRYPT_SUCCESS) { modeCtx->chachaCtx.method->cipherDeInitCtx(modeCtx->chachaCtx.key); BSL_ERR_PUSH_ERROR(ret); return ret; } modeCtx->enc = enc; return ret; } int32_t MODES_CHACHA20POLY1305_Update(MODES_CHACHAPOLY_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return MODES_CipherStreamProcess(modeCtx->enc ? MODES_CHACHA20POLY1305_Encrypt : MODES_CHACHA20POLY1305_Decrypt, &modeCtx->chachaCtx, in, inLen, out, outLen); } int32_t MODES_CHACHA20POLY1305_Final(MODES_CHACHAPOLY_Ctx *modeCtx, uint8_t *out, uint32_t *outLen) { (void) modeCtx; (void) out; (void) outLen; return CRYPT_EAL_CIPHER_FINAL_WITH_AEAD_ERROR; } int32_t MODES_CHACHA20POLY1305_DeInitCtx(MODES_CHACHAPOLY_Ctx *modeCtx) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } modeCtx->chachaCtx.method->cipherDeInitCtx(modeCtx->chachaCtx.key); BSL_SAL_CleanseData((void *)(&(modeCtx->chachaCtx.polyCtx)), sizeof(Poly1305Ctx)); modeCtx->chachaCtx.aadLen = 0; modeCtx->chachaCtx.cipherTextLen = 0; Poly1305CleanRegister(); return CRYPT_SUCCESS; } void MODES_CHACHA20POLY1305_FreeCtx(MODES_CHACHAPOLY_Ctx *modeCtx) { if (modeCtx == NULL) { return; } modeCtx->chachaCtx.method->cipherDeInitCtx(modeCtx->chachaCtx.key); BSL_SAL_FREE(modeCtx->chachaCtx.key); BSL_SAL_ClearFree(modeCtx, sizeof(MODES_CHACHAPOLY_Ctx)); } #endif
2301_79861745/bench_create
crypto/modes/src/modes_chacha20_poly1305.c
C
unknown
14,601
/* * 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_CTR #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_modes_ctr.h" #include "modes_local.h" uint32_t MODES_CTR_LastHandle(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { uint32_t left = len; uint32_t blockSize = ctx->blockSize; const uint8_t *tmpIn = in; uint8_t *tmpOut = out; // buf[0, ctx->offset, blockSize) // The data from st to blockSize - 1 is the data obtained after the last encryption and is not used up. while ((ctx->offset != 0) && (left > 0)) { *(tmpOut++) = ((*(tmpIn++)) ^ (ctx->buf[ctx->offset++])); --left; // & (blockSize - 1) is equivalent to mod blockSize. ctx->offset &= (uint8_t)(blockSize - 1); } // Return the calculated length. return (len - left); } void MODES_CTR_RemHandle(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (len == 0) { return; } uint32_t left = len; uint32_t blockSize = ctx->blockSize; const uint8_t *tmpIn = in; uint8_t *tmpOut = out; // Ensure that the length of IV is 16 when setting it, which will not cause encryption failures. // To optimize performance, the function does not determine the length of the IV. (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->buf, blockSize); MODE_IncCounter(ctx->iv, ctx->blockSize); ctx->offset = 0; while ((left) > 0) { tmpOut[ctx->offset] = (tmpIn[ctx->offset]) ^ (ctx->buf[ctx->offset]); --left; ++ctx->offset; } } int32_t MODES_CTR_Crypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { uint32_t offset = MODES_CTR_LastHandle(ctx, in, out, len); uint32_t left = len - offset; const uint8_t *tmpIn = in + offset; uint8_t *tmpOut = out + offset; uint32_t blockSize = ctx->blockSize; while (left >= blockSize) { // Ensure that the length of IV is 16 when setting it, which will not cause encryption failures. // To optimize performance, the function does not determine the length of the IV. (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->buf, blockSize); MODE_IncCounter(ctx->iv, ctx->blockSize); DATA64_XOR(tmpIn, ctx->buf, tmpOut, blockSize); left -= blockSize; tmpOut += blockSize; tmpIn += blockSize; } MODES_CTR_RemHandle(ctx, tmpIn, tmpOut, left); return CRYPT_SUCCESS; } MODES_CipherCtx *MODES_CTR_NewCtx(int32_t algId) { return MODES_CipherNewCtx(algId); } int32_t MODES_CTR_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_CipherInitCtx(modeCtx, modeCtx->commonCtx.ciphMeth->setEncryptKey, modeCtx->commonCtx.ciphCtx, key, keyLen, iv, ivLen, enc); } int32_t MODES_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(MODES_CTR_Crypt, &modeCtx->commonCtx, in, inLen, out, outLen); } int32_t MODES_CTR_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { (void) modeCtx; (void) out; *outLen = 0; return CRYPT_SUCCESS; } int32_t MODES_CTR_DeInitCtx(MODES_CipherCtx *modeCtx) { return MODES_CipherDeInitCtx(modeCtx); } int32_t MODES_CTR_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (cmd) { case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || valLen != sizeof(uint32_t)) { return CRYPT_INVALID_ARG; } *(int32_t *)val = 1; return CRYPT_SUCCESS; default: return MODES_CipherCtrl(modeCtx, cmd, val, valLen);; } } void MODES_CTR_FreeCtx(MODES_CipherCtx *modeCtx) { MODES_CipherFreeCtx(modeCtx); } int32_t MODES_CTR_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void)param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_SM4_CTR: #ifdef HITLS_CRYPTO_SM4 return SM4_CTR_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CTR_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } } int32_t MODES_CTR_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_CTR: case CRYPT_CIPHER_AES192_CTR: case CRYPT_CIPHER_AES256_CTR: #ifdef HITLS_CRYPTO_AES return AES_CTR_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif case CRYPT_CIPHER_SM4_CTR: #ifdef HITLS_CRYPTO_SM4 return SM4_CTR_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_CTR_Update(modeCtx, in, inLen, out, outLen); } } #endif
2301_79861745/bench_create
crypto/modes/src/modes_ctr.c
C
unknown
6,028
/* * 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_ECB #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_modes_ecb.h" #include "modes_local.h" #include "securec.h" int32_t MODES_ECB_Crypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { // ctx, in, out, these pointer have been judged at the EAL layer and is not judged again here. if (ctx->ciphCtx == NULL || len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; uint32_t left = len; const uint8_t *input = in; uint8_t *output = out; uint32_t blockSize = ctx->blockSize; while (left >= blockSize) { if (enc) { ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, input, output, blockSize); } else { ret = ctx->ciphMeth->decryptBlock(ctx->ciphCtx, input, output, blockSize); } if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } input += blockSize; output += blockSize; left -= blockSize; } if (left > 0) { BSL_ERR_PUSH_ERROR(CRYPT_MODE_ERR_INPUT_LEN); return CRYPT_MODE_ERR_INPUT_LEN; } return CRYPT_SUCCESS; } int32_t MODES_ECB_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return MODES_ECB_Crypt(ctx, in, out, len, true); } int32_t MODES_ECB_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return MODES_ECB_Crypt(ctx, in, out, len, false); } MODES_CipherCtx *MODES_ECB_NewCtx(int32_t algId) { return MODES_CipherNewCtx(algId); } int32_t MODES_ECB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { (void)iv; (void)ivLen; int32_t ret; ret = enc ? modeCtx->commonCtx.ciphMeth->setEncryptKey(modeCtx->commonCtx.ciphCtx, key, keyLen) : modeCtx->commonCtx.ciphMeth->setDecryptKey(modeCtx->commonCtx.ciphCtx, key, keyLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } modeCtx->enc = enc; return ret; } int32_t MODES_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherUpdate(modeCtx, modeCtx->enc ? MODES_ECB_Encrypt : MODES_ECB_Decrypt, in, inLen, out, outLen); } int32_t MODES_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CipherFinal(modeCtx, modeCtx->enc ? MODES_ECB_Encrypt : MODES_ECB_Decrypt, out, outLen); } int32_t MODES_ECB_DeinitCtx(MODES_CipherCtx *modeCtx) { return MODES_CipherDeInitCtx(modeCtx); } int32_t MODES_ECB_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen) { int ret; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (cmd) { case CRYPT_CTRL_REINIT_STATUS: (void)memset_s(modeCtx->data, EAL_MAX_BLOCK_LENGTH, 0, EAL_MAX_BLOCK_LENGTH); modeCtx->dataLen = 0; modeCtx->pad = CRYPT_PADDING_NONE; return CRYPT_SUCCESS; case CRYPT_CTRL_SET_PADDING: if (val == NULL || valLen != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } if (modeCtx->commonCtx.blockSize == 1) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_PADDING_NOT_SUPPORT); return CRYPT_EAL_PADDING_NOT_SUPPORT; } ret = MODES_SetPaddingCheck(*(int32_t *)val); if (ret != CRYPT_SUCCESS) { return ret; } modeCtx->pad = *(int32_t *)val; return CRYPT_SUCCESS; case CRYPT_CTRL_GET_PADDING: if (val == NULL || valLen != sizeof(int32_t)) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } *(int32_t *)val = modeCtx->pad; return CRYPT_SUCCESS; case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || valLen != sizeof(uint32_t)) { return CRYPT_INVALID_ARG; } *(int32_t *)val = modeCtx->commonCtx.ciphMeth->blockSize; return CRYPT_SUCCESS; default: return MODES_CipherCtrl(modeCtx, cmd, val, valLen); } } void MODES_ECB_FreeCtx(MODES_CipherCtx *modeCtx) { MODES_CipherFreeCtx(modeCtx); } int32_t MODES_ECB_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void)param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_SM4_ECB: #ifdef HITLS_CRYPTO_SM4 return SM4_ECB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_ECB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } } int32_t MODES_ECB_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_ECB: case CRYPT_CIPHER_AES192_ECB: case CRYPT_CIPHER_AES256_ECB: #ifdef HITLS_CRYPTO_AES return AES_ECB_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif case CRYPT_CIPHER_SM4_ECB: #ifdef HITLS_CRYPTO_SM4 return SM4_ECB_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_ECB_Update(modeCtx, in, inLen, out, outLen); } } int32_t MODES_ECB_FinalEx(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_ECB: case CRYPT_CIPHER_AES192_ECB: case CRYPT_CIPHER_AES256_ECB: #ifdef HITLS_CRYPTO_AES return AES_ECB_Final(modeCtx, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif case CRYPT_CIPHER_SM4_ECB: #ifdef HITLS_CRYPTO_SM4 return SM4_ECB_Final(modeCtx, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_ECB_Final(modeCtx, out, outLen); } } #endif // end HITLS_CRYPTO_ECB
2301_79861745/bench_create
crypto/modes/src/modes_ecb.c
C
unknown
7,331
/* * 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_GCM #include <stdint.h> #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "crypt_modes_gcm.h" #include "modes_local.h" #include "crypt_modes.h" int32_t MODES_GCM_SetKey(MODES_CipherGCMCtx *ctx, const uint8_t *key, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = ctx->ciphMeth->setEncryptKey(ctx->ciphCtx, key, len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return MODES_GCM_InitHashTable(ctx); } int32_t MODES_GCM_InitHashTable(MODES_CipherGCMCtx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint8_t gcmKey[GCM_BLOCKSIZE] = { 0 }; int32_t ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, gcmKey, gcmKey, GCM_BLOCKSIZE); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } GcmTableGen4bit(gcmKey, ctx->hTable); ctx->tagLen = GCM_BLOCKSIZE; BSL_SAL_CleanseData(gcmKey, sizeof(gcmKey)); return CRYPT_SUCCESS; } // Update the number of usage times. static int32_t CheckUseCnt(const MODES_CipherGCMCtx *ctx) { // 128, 120, 112, 104, or 96 that is 12 byte - 16 byte if (ctx->cryptCnt == GCM_MAX_INVOCATIONS_TIMES) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_KEYUSE_TOOMANY_TIME); return CRYPT_MODES_KEYUSE_TOOMANY_TIME; } return CRYPT_SUCCESS; } /** * NIST_800-38D-5.2 * 1 ≤ len(IV) ≤ 2^64 - 1 (bit) * It is currently restricted to no more than 2^32 - 1 bytes */ int32_t MODES_GCM_SetIv(MODES_CipherGCMCtx *ctx, const uint8_t *iv, uint32_t ivLen) { if (iv == NULL || ivLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CheckUseCnt(ctx); // Check the number of usage times. if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t i; uint64_t len = (uint64_t)ivLen; // when ivLen == 0, do reinit, no need to refersh iv if (len == 12) { // len(IV ) = 96bit = 12byte const uint8_t ivPad[4] = {0x00, 0x00, 0x00, 0x01}; /* Y0 = IV || 0^31 || 1 if len(IV ) = 96 = 12byte */ (void)memcpy_s(ctx->iv, GCM_BLOCKSIZE, iv, 12); (void)memcpy_s(ctx->iv + 12, GCM_BLOCKSIZE - 12, ivPad, sizeof(ivPad)); // pad last 4bit(base = 12) } else { /* Y0 = GHASH(H, {}, IV ) otherwise */ (void)memset_s(ctx->iv, GCM_BLOCKSIZE, 0, GCM_BLOCKSIZE); const uint8_t *off = iv; uint32_t blockLen = ivLen & GCM_BLOCK_MASK; uint32_t lastLen = ivLen - blockLen; uint8_t tmp[GCM_BLOCKSIZE] = {0}; if (blockLen > 0) { GcmHashMultiBlock(ctx->iv, ctx->hTable, off, blockLen); off += blockLen; } if (lastLen > 0) { for (i = 0; i < lastLen; i++) { tmp[i] = off[i]; } GcmHashMultiBlock(ctx->iv, ctx->hTable, tmp, GCM_BLOCKSIZE); } len = (uint64_t)ivLen << 3; // bitLen = byteLen << 3 (void)BSL_SAL_CleanseData(tmp, GCM_BLOCKSIZE); Uint64ToBeBytes(len, tmp + 8); // The last 8 bytes store the length of the IV. GcmHashMultiBlock(ctx->iv, ctx->hTable, tmp, GCM_BLOCKSIZE); } /** * NIST_800-38D-7.1 * GCTR(J0) */ ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->ek0, GCM_BLOCKSIZE); /** * NIST_800-38D-7.1 * INC32 * the 32-bit incrementing function is applied to the pre-counter block * to produce the initial counter block for an invocation of the GCTR * function on the plaintext */ uint32_t ctr = GET_UINT32_BE(ctx->iv, 12); // Offset of 12 bytes. Use the last four bytes. ctr++; PUT_UINT32_BE(ctr, ctx->iv, 12); // Writeback of offset 12 bytes // Reset information. (void)memset_s(ctx->ghash, GCM_BLOCKSIZE, 0, GCM_BLOCKSIZE); ctx->aadLen = 0; (void)memset_s(ctx->last, GCM_BLOCKSIZE, 0, GCM_BLOCKSIZE); ctx->lastLen = 0; ctx->plaintextLen = 0; (void)memset_s(ctx->remCt, GCM_BLOCKSIZE, 0, GCM_BLOCKSIZE); // Clear sensitive information. BSL_SAL_CleanseData(&ctr, sizeof(uint32_t)); return CRYPT_SUCCESS; } /** * NIST_800-38D-5.2 * len(AAD) ≤ 2^64 - 1 (bit) * Currently, it is restricted to no more than 2^32 - 1 bytes. */ static int32_t SetAad(MODES_CipherGCMCtx *ctx, const uint8_t *aad, uint32_t aadLen) { if (aad == NULL && aadLen != 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } const uint8_t *off = aad; uint32_t i; if (ctx->aadLen != 0 || ctx->plaintextLen != 0) { // aad is set BSL_ERR_PUSH_ERROR(CRYPT_MODES_AAD_REPEAT_SET_ERROR); return CRYPT_MODES_AAD_REPEAT_SET_ERROR; } uint32_t blockLen = aadLen & GCM_BLOCK_MASK; uint32_t lastLen = aadLen - blockLen; if (blockLen > 0) { GcmHashMultiBlock(ctx->ghash, ctx->hTable, off, blockLen); off += blockLen; } if (lastLen > 0) { uint8_t temp[GCM_BLOCKSIZE] = {0}; for (i = 0; i < lastLen; i++) { temp[i] = off[i]; } GcmHashMultiBlock(ctx->ghash, ctx->hTable, temp, GCM_BLOCKSIZE); } ctx->aadLen = aadLen; return CRYPT_SUCCESS; } // Overflow occurs when the encryption length is determined and the encrypted length information is updated. int32_t CryptLenCheckAndRefresh(MODES_CipherGCMCtx *ctx, uint32_t len) { // The length of len is only 32 bits. This calculation does not cause overflow. uint64_t plaintextLen = ctx->plaintextLen + len; if (plaintextLen > GCM_MAX_COMBINED_LENGTH) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_CRYPTLEN_OVERFLOW); return CRYPT_MODES_CRYPTLEN_OVERFLOW; } ctx->plaintextLen = plaintextLen; return CRYPT_SUCCESS; } static void GcmXorInEncrypt(XorCryptData *data, uint32_t len) { uint32_t i; for (i = 0; i < len; i++) { data->out[i] = data->in[i] ^ data->ctr[i]; data->tag[i] = data->out[i]; } } static void GcmXorInDecrypt(XorCryptData *data, uint32_t len) { uint32_t i; for (i = 0; i < len; i++) { data->tag[i] = data->in[i]; data->out[i] = data->in[i] ^ data->ctr[i]; } } // Process the remaining data in the last update. uint32_t MODES_GCM_LastHandle(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { uint32_t lastLen = 0; if (ctx->lastLen > 0) { XorCryptData data; lastLen = (ctx->lastLen < len) ? ctx->lastLen : len; data.in = in; data.out = out; data.ctr = &(ctx->last[GCM_BLOCKSIZE - ctx->lastLen]); data.tag = &(ctx->remCt[GCM_BLOCKSIZE - ctx->lastLen]); if (enc) { // ctx->lastLen must be smaller than the GCM_BLOCKSIZE GcmXorInEncrypt(&data, lastLen); } else { GcmXorInDecrypt(&data, lastLen); } // Refresh the remaining length. ctx->lastLen -= lastLen; if (ctx->lastLen == 0) { GcmHashMultiBlock(ctx->ghash, ctx->hTable, ctx->remCt, GCM_BLOCKSIZE); } } return lastLen; } static void GcmMultiBlockCrypt(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { uint32_t blockLen = len; const uint8_t *dataIn = in; uint8_t *dataOut = out; // count information, last 32 bits of the IV, with an offset of 12 bytes (16-4 = 12) uint32_t ctr = GET_UINT32_BE(ctx->iv, 12); if (enc == false) { GcmHashMultiBlock(ctx->ghash, ctx->hTable, in, len); } while (blockLen > 0) { ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->last, GCM_BLOCKSIZE); DATA64_XOR(dataIn, ctx->last, dataOut, GCM_BLOCKSIZE); /** * NIST_800-38D-7.1 * INC32 */ ctr++; PUT_UINT32_BE(ctr, ctx->iv, 12); // Offset of 12 bytes. Use the last four bytes. // Refresh the remaining length. blockLen -= GCM_BLOCKSIZE; // offset dataIn += GCM_BLOCKSIZE; dataOut += GCM_BLOCKSIZE; } if (enc) { GcmHashMultiBlock(ctx->ghash, ctx->hTable, out, len); } // Clear sensitive information. BSL_SAL_CleanseData(&ctr, sizeof(uint32_t)); } // enc: true: the encryption operation, false: the decryption operation static int32_t MODES_GCM_Crypt(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc) { if (ctx == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != 0 && in == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CryptLenCheckAndRefresh(ctx, len); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t lastLen = MODES_GCM_LastHandle(ctx, in, out, len, enc); // Data processing is complete. Exit. if (lastLen == len) { return CRYPT_SUCCESS; } XorCryptData data; data.in = in + lastLen; data.out = out + lastLen; data.ctr = ctx->last; data.tag = ctx->remCt; uint32_t multiBlockLen = (len - lastLen) & GCM_BLOCK_MASK; if (multiBlockLen > 0) { GcmMultiBlockCrypt(ctx, data.in, data.out, multiBlockLen, enc); data.in += multiBlockLen; data.out += multiBlockLen; } uint32_t remLen = len - lastLen - multiBlockLen; if (remLen > 0) { // count information, last 32 bits of the IV, with an offset of 12 bytes (16-4 = 12) uint32_t ctr = GET_UINT32_BE(ctx->iv, 12); (void)ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->last, GCM_BLOCKSIZE); if (enc) { GcmXorInEncrypt(&data, remLen); } else { GcmXorInDecrypt(&data, remLen); } /** * NIST_800-38D-7.1 * INC32 */ ctr++; PUT_UINT32_BE(ctr, ctx->iv, 12); // Offset of 12 bytes. Use the last four bytes. // Clear sensitive information. BSL_SAL_CleanseData(&ctr, sizeof(uint32_t)); } ctx->lastLen = (remLen > 0) ? (GCM_BLOCKSIZE - remLen) : 0; return CRYPT_SUCCESS; } static void GcmPad(MODES_CipherGCMCtx *ctx) { // S = GHASHH (A || 0v || C || 0u || [len(A)]64 || [len(C)]64). if (ctx->lastLen != 0) { uint32_t offset = GCM_BLOCKSIZE - ctx->lastLen; (void)memset_s(ctx->remCt + offset, GCM_BLOCKSIZE - offset, 0, ctx->lastLen); GcmHashMultiBlock(ctx->ghash, ctx->hTable, ctx->remCt, GCM_BLOCKSIZE); } uint64_t aadLen = (uint64_t)(ctx->aadLen) << 3; // bitLen = byteLen << 3 uint64_t plaintextLen = ctx->plaintextLen << 3; // bitLen = byteLen << 3 uint8_t padBuf[GCM_BLOCKSIZE]; Uint64ToBeBytes(aadLen, padBuf); Uint64ToBeBytes(plaintextLen, padBuf + 8); // The last 64 bits (8 bytes) is the length of the ciphertext. GcmHashMultiBlock(ctx->ghash, ctx->hTable, padBuf, GCM_BLOCKSIZE); } static int32_t SetTagLen(MODES_CipherGCMCtx *ctx, const uint8_t *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_MODES_CTRL_TAGLEN_ERROR); return CRYPT_MODES_CTRL_TAGLEN_ERROR; } /** * NIST_800-38D-5.2.1.2 * The bit length of the tag, denoted t, is a security parameter, as discussed in Appendix B. * In general, t may be any one of the following five values: 128, 120, 112, 104, or 96. For certain * applications, t may be 64 or 32; guidance for the use of these two tag lengths, including * requirements on the length of the input data and the lifetime of the ciphCtx in these cases, * is given in Appendix C */ uint32_t tagLen = *((const uint32_t *)val); // 32bit is 4 bytes, 64bit is 8 bytes, 128, 120, 112, 104, or 96 is 12byte - 16byte if (tagLen == 4 || tagLen == 8 || (tagLen >= 12 && tagLen <= 16)) { ctx->tagLen = (uint8_t)tagLen; return CRYPT_SUCCESS; } BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_TAGLEN_ERROR); return CRYPT_MODES_CTRL_TAGLEN_ERROR; } static int32_t GetTag(MODES_CipherGCMCtx *ctx, uint8_t *val, uint32_t len) { if (val == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (len != ctx->tagLen) { BSL_ERR_PUSH_ERROR(CRYPT_MODES_TAGLEN_ERROR); return CRYPT_MODES_TAGLEN_ERROR; } ctx->cryptCnt++; // The encryption/decryption process ends. Key usage times + 1 GcmPad(ctx); uint32_t i; for (i = 0; i < len; i++) { val[i] = ctx->ghash[i] ^ ctx->ek0[i]; } return CRYPT_SUCCESS; } int32_t MODES_GCM_Encrypt(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return MODES_GCM_Crypt(ctx, in, out, len, true); } int32_t MODES_GCM_Decrypt(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { return MODES_GCM_Crypt(ctx, in, out, len, false); } int32_t MODES_GCM_Ctrl(MODES_GCM_Ctx *modeCtx, int32_t opt, void *val, uint32_t len) { if (modeCtx == NULL) { return CRYPT_NULL_INPUT; } switch (opt) { case CRYPT_CTRL_SET_IV: case CRYPT_CTRL_REINIT_STATUS: return MODES_GCM_SetIv(&modeCtx->gcmCtx, val, len); case CRYPT_CTRL_SET_TAGLEN: return SetTagLen(&modeCtx->gcmCtx, val, len); case CRYPT_CTRL_SET_AAD: return SetAad(&modeCtx->gcmCtx, val, len); case CRYPT_CTRL_GET_TAG: return GetTag(&modeCtx->gcmCtx, val, len); case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || len != sizeof(uint32_t)) { return CRYPT_INVALID_ARG; } *(int32_t *)val = 1; return CRYPT_SUCCESS; default: BSL_ERR_PUSH_ERROR(CRYPT_MODES_CTRL_TYPE_ERROR); return CRYPT_MODES_CTRL_TYPE_ERROR; } } MODES_GCM_Ctx *MODES_GCM_NewCtx(int32_t algId) { const EAL_SymMethod *method = EAL_GetSymMethod(algId); if (method == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return NULL; } MODES_GCM_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(MODES_GCM_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return ctx; } ctx->algId = algId; ctx->gcmCtx.ciphCtx = BSL_SAL_Calloc(1, method->ctxSize); if (ctx->gcmCtx.ciphCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_Free(ctx); return NULL; } ctx->gcmCtx.ciphMeth = method; return ctx; } int32_t MODES_GCM_InitCtx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = MODES_GCM_SetKey(&modeCtx->gcmCtx, key, keyLen); if (ret != CRYPT_SUCCESS) { return ret; } ret = MODES_GCM_SetIv(&modeCtx->gcmCtx, iv, ivLen); if (ret != CRYPT_SUCCESS) { (void)MODES_GCM_DeInitCtx(modeCtx); return ret; } modeCtx->enc = enc; return ret; } int32_t MODES_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(modeCtx->enc ? MODES_GCM_Encrypt : MODES_GCM_Decrypt, &modeCtx->gcmCtx, in, inLen, out, outLen); } int32_t MODES_GCM_Final(MODES_GCM_Ctx *modeCtx, uint8_t *out, uint32_t *outLen) { (void) modeCtx; (void) out; (void) outLen; return CRYPT_EAL_CIPHER_FINAL_WITH_AEAD_ERROR; } int32_t MODES_GCM_DeInitCtx(MODES_GCM_Ctx *modeCtx) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t algId = modeCtx->algId; void *ciphCtx = modeCtx->gcmCtx.ciphCtx; const EAL_SymMethod *ciphMeth = modeCtx->gcmCtx.ciphMeth; modeCtx->gcmCtx.ciphMeth->cipherDeInitCtx(ciphCtx); BSL_SAL_CleanseData((void *)(&(modeCtx->gcmCtx)), sizeof(MODES_CipherGCMCtx)); modeCtx->gcmCtx.ciphCtx = ciphCtx; modeCtx->gcmCtx.ciphMeth = ciphMeth; modeCtx->algId = algId; return CRYPT_SUCCESS; } void MODES_GCM_FreeCtx(MODES_GCM_Ctx *modeCtx) { if (modeCtx == NULL) { return; } (void)BSL_SAL_ClearFree(modeCtx->gcmCtx.ciphCtx, modeCtx->gcmCtx.ciphMeth->ctxSize); (void)BSL_SAL_CleanseData(modeCtx, sizeof(MODES_GCM_Ctx)); BSL_SAL_Free(modeCtx); } int32_t MODES_GCM_InitCtxEx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void)param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_SM4_GCM: #ifdef HITLS_CRYPTO_SM4 return SM4_GCM_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_GCM_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } } int32_t MODES_GCM_UpdateEx(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_AES128_GCM: case CRYPT_CIPHER_AES192_GCM: case CRYPT_CIPHER_AES256_GCM: #ifdef HITLS_CRYPTO_AES return AES_GCM_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif case CRYPT_CIPHER_SM4_GCM: #ifdef HITLS_CRYPTO_SM4 return SM4_GCM_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_GCM_Update(modeCtx, in, inLen, out, outLen); } } #endif
2301_79861745/bench_create
crypto/modes/src/modes_gcm.c
C
unknown
18,606
/* * 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 MODES_LOCAL_H #define MODES_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_MODES #include <stdint.h> #include <stdbool.h> #include "crypt_local_types.h" #include "crypt_modes_xts.h" #include "crypt_modes_cbc.h" #include "crypt_modes_ccm.h" #include "crypt_modes_cfb.h" #include "crypt_modes_chacha20poly1305.h" #include "crypt_modes_ctr.h" #include "crypt_modes_ecb.h" #include "crypt_modes_gcm.h" #include "crypt_modes_ofb.h" #include "crypt_modes.h" #include "eal_cipher_local.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define UPDATE_VALUES(l, i, o, len) \ do { \ (l) -= (len); \ (i) += (len); \ (o) += (len); \ } while (false) MODES_CipherCtx *MODES_CipherNewCtx(int32_t algId); int32_t MODES_CipherInitCommonCtx(MODES_CipherCommonCtx *modeCtx, void *setSymKey, void *keyCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen); int32_t MODES_CipherInitCtx(MODES_CipherCtx *ctx, void *setSymKey, void *keyCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc); int32_t MODE_CheckUpdateParam(uint8_t blockSize, uint32_t cacheLen, uint32_t inLen, uint32_t *outLen); /* Block cipher processing */ int32_t MODES_CipherUpdate(MODES_CipherCtx *modeCtx, void *blockUpdate, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); int32_t MODES_BlockPadding(int32_t algId, int32_t padding, uint8_t blockSize, uint8_t *data, uint8_t *dataLen); int32_t MODES_BlockUnPadding(int32_t padding, const uint8_t *pad, uint32_t padLen, uint32_t *dataLen); /* Block cipher processing */ int32_t MODES_CipherFinal(MODES_CipherCtx *modeCtx, void *blockUpdate, uint8_t *out, uint32_t *outLen); int32_t MODES_CipherDeInitCtx(MODES_CipherCtx *modeCtx); void MODES_CipherFreeCtx(MODES_CipherCtx *modeCtx); int32_t MODES_CipherCtrl(MODES_CipherCtx *ctx, int32_t opt, void *val, uint32_t len); int32_t MODES_CipherStreamProcess(void *processFuncs, void *ctx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); static inline void MODE_IncCounter(uint8_t *counter, uint32_t counterLen) { uint32_t i = counterLen; uint16_t carry = 1; while (i > 0) { i--; carry += counter[i]; counter[i] = carry & (0xFFu); carry >>= 8; // Take the upper 8 bits. } } int32_t MODES_SetEncryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len); int32_t MODES_SetDecryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len); int32_t MODES_SetPaddingCheck(int32_t pad); #ifdef HITLS_CRYPTO_SM4 int32_t MODES_SM4_SetEncryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len); int32_t MODES_SM4_SetDecryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len); #endif // cfb #ifdef HITLS_CRYPTO_CFB int32_t MODES_CFB_Encrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_CFB_Decrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif // ctr #ifdef HITLS_CRYPTO_CTR uint32_t MODES_CTR_LastHandle(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); void MODES_CTR_RemHandle(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif // gcm #ifdef HITLS_CRYPTO_GCM void GcmTableGen4bit(uint8_t key[GCM_BLOCKSIZE], MODES_GCM_GF128 hTable[16]); void GcmHashMultiBlock(uint8_t t[GCM_BLOCKSIZE], const MODES_GCM_GF128 hTable[16], const uint8_t *in, uint32_t inLen); uint32_t MODES_GCM_LastHandle(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc); int32_t MODES_GCM_SetIv(MODES_CipherGCMCtx *ctx, const uint8_t *iv, uint32_t ivLen); int32_t CryptLenCheckAndRefresh(MODES_CipherGCMCtx *ctx, uint32_t len); #endif // xts #ifdef HITLS_CRYPTO_XTS int32_t MODES_XTS_CheckPara(const uint8_t *key, uint32_t len, const uint8_t *iv); int32_t MODES_XTS_SetIv(MODES_CipherXTSCtx *ctx, const uint8_t *val, uint32_t len); int32_t MODES_XTS_SetEncryptKey(MODES_CipherXTSCtx *ctx, const uint8_t *key, uint32_t len); int32_t MODES_XTS_SetDecryptKey(MODES_CipherXTSCtx *ctx, const uint8_t *key, uint32_t len); #endif #ifdef HITLS_CRYPTO_CBC int32_t AES_CBC_EncryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t AES_CBC_DecryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_CBC_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_CBC_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODE_SM4_CBC_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODE_SM4_CBC_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_CCM int32_t MODES_CCM_Encrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_CCM_Decrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_AES_CCM_Encrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_AES_CCM_Decrypt(MODES_CipherCCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_CFB int32_t MODES_CFB_BitCrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len, bool enc); int32_t MODE_AES_CFB_Decrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODE_SM4_CFB_Encrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODE_SM4_CFB_Decrypt(MODES_CipherCFBCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) int32_t MODES_CHACHA20POLY1305_Encrypt(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_CHACHA20POLY1305_Decrypt(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_CHACHA20POLY1305_SetEncryptKey(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *key, uint32_t len); int32_t MODES_CHACHA20POLY1305_SetDecryptKey(MODES_CipherChaChaPolyCtx *ctx, const uint8_t *key, uint32_t len); #endif #ifdef HITLS_CRYPTO_CTR int32_t MODES_CTR_Crypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t AES_CTR_EncryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODE_SM4_CTR_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_ECB int32_t MODES_ECB_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_ECB_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t AES_ECB_EncryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t AES_ECB_DecryptBlock(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODE_SM4_ECB_Encrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODE_SM4_ECB_Decrypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_GCM int32_t MODES_GCM_Encrypt(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_GCM_Decrypt(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t AES_GCM_EncryptBlock(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t AES_GCM_DecryptBlock(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_SM4_GCM_DecryptBlock(MODES_CipherGCMCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_OFB int32_t MODES_OFB_Crypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef HITLS_CRYPTO_XTS int32_t MODES_XTS_Encrypt(MODES_CipherXTSCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_XTS_Decrypt(MODES_CipherXTSCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_AES_XTS_Encrypt(MODES_CipherXTSCtx *xtsCtx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_AES_XTS_Decrypt(MODES_CipherXTSCtx *xtsCtx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_SM4_XTS_Encrypt(MODES_CipherXTSCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); int32_t MODES_SM4_XTS_Decrypt(MODES_CipherXTSCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len); #endif #ifdef __cplusplus } #endif // __cplusplus #endif #endif
2301_79861745/bench_create
crypto/modes/src/modes_local.h
C
unknown
9,184
/* * 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_OFB #include "securec.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_errno.h" #include "modes_local.h" #include "crypt_modes_ofb.h" int32_t MODES_OFB_Crypt(MODES_CipherCommonCtx *ctx, const uint8_t *in, uint8_t *out, uint32_t len) { if (ctx == NULL || in == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret; const uint8_t *input = in; uint32_t blockSize = ctx->blockSize; uint32_t left = len; uint8_t *output = out; uint32_t i; // If the remaining encrypted iv is not used up last time, use that part to perform XOR. while (left > 0 && ctx->offset > 0) { *(output++) = ctx->iv[ctx->offset] ^ *(input++); left--; ctx->offset = (ctx->offset + 1) % blockSize; } while (left > 0) { // Encrypt the IV. ret = ctx->ciphMeth->encryptBlock(ctx->ciphCtx, ctx->iv, ctx->iv, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (left >= blockSize) { DATA32_XOR(input, ctx->iv, output, blockSize); UPDATE_VALUES(left, input, output, blockSize); } else { for (i = 0; i < left; i++) { output[i] = input[i] ^ ctx->iv[i]; } ctx->offset = (uint8_t)left; left = 0; } } return CRYPT_SUCCESS; } MODES_CipherCtx *MODES_OFB_NewCtx(int32_t algId) { return MODES_CipherNewCtx(algId); } int32_t MODES_OFB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_CipherInitCtx(modeCtx, modeCtx->commonCtx.ciphMeth->setEncryptKey, modeCtx->commonCtx.ciphCtx, key, keyLen, iv, ivLen, enc); } int32_t MODES_OFB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CipherStreamProcess(MODES_OFB_Crypt, &modeCtx->commonCtx, in, inLen, out, outLen); } int32_t MODES_OFB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { (void) modeCtx; (void) out; *outLen = 0; return CRYPT_SUCCESS; } int32_t MODES_OFB_DeInitCtx(MODES_CipherCtx *modeCtx) { return MODES_CipherDeInitCtx(modeCtx); } int32_t MODES_OFB_Ctrl(MODES_CipherCtx *modeCtx, int32_t cmd, void *val, uint32_t valLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (cmd) { case CRYPT_CTRL_GET_BLOCKSIZE: if (val == NULL || valLen != sizeof(uint32_t)) { return CRYPT_INVALID_ARG; } *(int32_t *)val = 1; return CRYPT_SUCCESS; default: return MODES_CipherCtrl(modeCtx, cmd, val, valLen);; } } void MODES_OFB_FreeCtx(MODES_CipherCtx *modeCtx) { MODES_CipherFreeCtx(modeCtx); } int32_t MODES_OFB_InitCtxEx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, void *param, bool enc) { (void) param; if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_SM4_OFB: #ifdef HITLS_CRYPTO_SM4 return SM4_OFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_OFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } } int32_t MODES_OFB_UpdateEx(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { if (modeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (modeCtx->algId) { case CRYPT_CIPHER_SM4_OFB: #ifdef HITLS_CRYPTO_SM4 return SM4_OFB_Update(modeCtx, in, inLen, out, outLen); #else return CRYPT_EAL_ALG_NOT_SUPPORT; #endif default: return MODES_OFB_Update(modeCtx, in, inLen, out, outLen); } } #endif // HITLS_CRYPTO_OFB
2301_79861745/bench_create
crypto/modes/src/modes_ofb.c
C
unknown
4,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. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CBC) #include "crypt_modes_cbc.h" #include "modes_local.h" int32_t AES_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CBC_Update(modeCtx, in, inLen, out, outLen); } int32_t AES_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CBC_Final(modeCtx, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_aes_cbc.c
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. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CCM) #include "modes_local.h" #include "crypt_modes_ccm.h" int32_t AES_CCM_Update(MODES_CCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CCM_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_aes_ccm.c
C
unknown
870
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CFB) #include "crypt_modes_cfb.h" int32_t AES_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CFB_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_aes_cfb.c
C
unknown
845
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_CTR) #include "crypt_modes_ctr.h" #include "modes_local.h" int32_t AES_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CTR_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_aes_ctr.c
C
unknown
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" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_ECB) #include "crypt_modes_ecb.h" #include "modes_local.h" int32_t AES_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_ECB_Update(modeCtx, in, inLen, out, outLen); } int32_t AES_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_ECB_Final(modeCtx, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_aes_ecb.c
C
unknown
1,041
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_GCM) #include "crypt_modes_gcm.h" int32_t AES_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_GCM_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_aes_gcm.c
C
unknown
845
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #if defined(HITLS_CRYPTO_AES) && defined(HITLS_CRYPTO_XTS) #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_aes.h" #include "crypt_modes_xts.h" #include "modes_local.h" int32_t AES_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_XTS_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_aes_xts.c
C
unknown
950
/* * 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_GCM #include "bsl_sal.h" #include "crypt_utils.h" #include "modes_local.h" /* table[i] = (P^4)*i, P = 0x4000000000000000, i = 0...16 */ static const uint64_t TABLE_P4_BITS[16] = { 0x0000000000000000, 0x1c20000000000000, 0x3840000000000000, 0x2460000000000000, 0x7080000000000000, 0x6ca0000000000000, 0x48c0000000000000, 0x54e0000000000000, 0xe100000000000000, 0xfd20000000000000, 0xd940000000000000, 0xc560000000000000, 0x9180000000000000, 0x8da0000000000000, 0xa9c0000000000000, 0xb5e0000000000000 }; // Calculate n*H, n is in 0 .... 16 void GcmTableGen4bit(uint8_t key[GCM_BLOCKSIZE], MODES_GCM_GF128 hTable[16]) { uint32_t i; uint32_t j; const uint64_t r = 0xE100000000000000; hTable[0].h = 0; hTable[0].l = 0; // The intermediate term of the table (16 / 2 = = 8) is H itself. hTable[8].h = Uint64FromBeBytes(key); // The intermediate term of the table (16 / 2 = = 8) is H itself. hTable[8].l = Uint64FromBeBytes(key + sizeof(uint64_t)); for (i = 4; i > 0; i >>= 1) { // 4-bit table, the value of the 2^n item is calculated first. // cyclically shift to right by 1bit. The upper 1 bit of h is combined with the lower 63 bits of l. hTable[i].l = (hTable[ i * 2].h << 63) | (hTable[ i * 2].l >> 1); hTable[i].h = (hTable[ i * 2].h >> 1) ^ ((hTable[ i * 2].l & 1) * r); // the value of the 2^n item } for (i = 1; i < 16; i <<= 1) { for (j = 1; j < i; j++) { hTable[i + j].h = hTable[i].h ^ hTable[j].h; hTable[i + j].l = hTable[i].l ^ hTable[j].l; } } } // Calculate t = t * H void GcmHashMultiBlock(uint8_t t[GCM_BLOCKSIZE], const MODES_GCM_GF128 hTable[16], const uint8_t *in, uint32_t inLen) { MODES_GCM_GF128 x; uint8_t r; uint8_t h, l, tag; // Ciphertext information, digest information, and non-sensitive information. const uint8_t *tempIn = in; for (uint32_t i = 0; i < inLen; i += GCM_BLOCKSIZE) { uint8_t cnt = GCM_BLOCKSIZE - 1; x.h = 0; x.l = 0; while (1) { tag = t[cnt] ^ tempIn[cnt]; l = tag & 0xf; h = (tag >> 4) & 0xf; x.h ^= hTable[l].h; x.l ^= hTable[l].l; r = (x.l & 0xf); // Cyclically shift to right by 4 bits. The upper 4 bits of h is combined with the lower 60 bits of l. x.l = (x.h << 60) | (x.l >> 4); x.h = (x.h >> 4); // Cyclically shift to right by 4 bits. x.h ^= TABLE_P4_BITS[r]; x.h ^= hTable[h].h; x.l ^= hTable[h].l; if (cnt == 0) { break; } cnt--; r = (x.l & 0xf); // Cyclically shift to right by 4 bits. The upper 4 bits of h is combined with the lower 60 bits of l. x.l = (x.h << 60) | (x.l >> 4); x.h = (x.h >> 4); // Cyclically shift to right by 4 bits. x.h ^= TABLE_P4_BITS[r]; } tempIn += GCM_BLOCKSIZE; Uint64ToBeBytes(x.h, t); Uint64ToBeBytes(x.l, t + 8); } // Clear sensitive information. BSL_SAL_CleanseData(&x, sizeof(MODES_GCM_GF128)); BSL_SAL_CleanseData(&r, sizeof(uint8_t)); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_ghash.c
C
unknown
3,822
/* * 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_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "bsl_sal.h" #include "crypt_utils.h" #include "poly1305_core.h" // Information required by initializing the assembly, // for example, ctx->table. However, the C language does not calculate the table. void Poly1305InitForAsm(Poly1305Ctx *ctx) { (void)ctx; return; } // Operation for blocks. The return value is the length of the remaining unprocessed data. uint32_t Poly1305Block(Poly1305Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint32_t padbit) { uint32_t a[5], r[4]; uint64_t b[8]; // RFC_7539-2.5.1 for loop internal operation a[0] = ctx->acc[0]; a[1] = ctx->acc[1]; a[2] = ctx->acc[2]; a[3] = ctx->acc[3]; a[4] = ctx->acc[4]; r[0] = ctx->r[0]; r[1] = ctx->r[1]; r[2] = ctx->r[2]; r[3] = ctx->r[3]; const uint8_t *off = data; uint32_t len = dataLen; while (len >= POLY1305_BLOCKSIZE) { // a = acc + inputret b[0] = (uint64_t)a[0] + GET_UINT32_LE(off, 0); b[1] = (uint64_t)a[1] + GET_UINT32_LE(off, 4) + (b[0] >> 32); b[2] = (uint64_t)a[2] + GET_UINT32_LE(off, 8) + (b[1] >> 32); b[3] = (uint64_t)a[3] + GET_UINT32_LE(off, 12) + (b[2] >> 32); a[0] = (uint32_t)b[0]; a[1] = (uint32_t)b[1]; a[2] = (uint32_t)b[2]; a[3] = (uint32_t)b[3]; // Upper 32 bits of b[3] carry to a[4]. Because a[4] <= 4, this processing can never overflow a[4] += (uint32_t)(b[3] >> 32) + padbit; /* Lower bits of the data product. Because the high bits of each term of r are processed, there is no carry in the following polynomial multiplication and addition. */ b[0] = (uint64_t)a[0] * r[0]; b[1] = (uint64_t)a[0] * r[1] + (uint64_t)a[1] * r[0]; b[2] = (uint64_t)a[0] * r[2] + (uint64_t)a[1] * r[1] + (uint64_t)a[2] * r[0]; b[3] = (uint64_t)a[0] * r[3] + (uint64_t)a[1] * r[2] + (uint64_t)a[2] * r[1] + (uint64_t)a[3] * r[0]; /** * Higher bits of the data product. Because the high bits of each term of r are processed, * there is no carry in the following polynomial multiplication and addition. */ // (Ensure that the calculation (b[4] * 5) does not overflow, calculate (a[4] * r[0]) items later.) b[4] = (uint64_t)a[1] * r[3] + (uint64_t)a[2] * r[2] + (uint64_t)a[3] * r[1]; b[5] = (uint64_t)a[2] * r[3] + (uint64_t)a[3] * r[2] + (uint64_t)a[4] * r[1]; b[6] = (uint64_t)a[3] * r[3] + (uint64_t)a[4] * r[2]; b[7] = (uint64_t)a[4] * r[3]; /** * The upper bits are multiplied by 5/4, because r1, r[2], r3 is processed, * so the above values are divisible by 4. Because the high bits of each term of r are processed, * there is no carry in the following polynomial multiplication and addition: (3 * 5) < 0xF */ b[4] = (b[4] >> 2) + b[4]; b[5] = (b[5] >> 2) + b[5]; b[6] = (b[6] >> 2) + b[6]; b[7] = (b[7] >> 2) + b[7]; /* After offset 130 bits, the combination is obtained a0 = b[4] * 5 + b[0].... Because the high bits of each term of r are processed, there is no carry in the following polynomial multiplication and addition. */ b[0] += (b[4] & 0xFFFFFFFF); b[1] += (b[0] >> 32) + (b[4] >> 32) + (b[5] & 0xFFFFFFFF); b[2] += (b[1] >> 32) + (b[5] >> 32) + (b[6] & 0xFFFFFFFF); b[3] += (b[2] >> 32) + (b[6] >> 32) + (b[7] & 0xFFFFFFFF); a[4] = a[4] * r[0] + (uint32_t)(b[3] >> 32) + (uint32_t)(b[7] >> 32); b[0] = (uint32_t)b[0]; b[1] = (uint32_t)b[1]; b[2] = (uint32_t)b[2]; b[3] = (uint32_t)b[3]; // Shift the upper bits of a4 by 130 bits and then multiply it by 5. // The amount of a4 data is small and carry cannot be occurred. b[0] += (a[4] >> 2) + (a[4] & 0xFFFFFFFC); a[4] &= 0x3; /* Process carry */ b[1] += (b[0] >> 32); b[2] += (b[1] >> 32); b[3] += (b[2] >> 32); a[4] += (uint32_t)(b[3] >> 32); a[0] = (uint32_t)b[0]; a[1] = (uint32_t)b[1]; a[2] = (uint32_t)b[2]; a[3] = (uint32_t)b[3]; len -= POLY1305_BLOCKSIZE; off += POLY1305_BLOCKSIZE; } ctx->acc[0] = a[0]; ctx->acc[1] = a[1]; ctx->acc[2] = a[2]; ctx->acc[3] = a[3]; ctx->acc[4] = a[4]; // Clear sensitive information. BSL_SAL_CleanseData(a, sizeof(a)); BSL_SAL_CleanseData(r, sizeof(r)); BSL_SAL_CleanseData(b, sizeof(b)); return len; } void Poly1305Last(Poly1305Ctx *ctx, uint8_t mac[POLY1305_TAGSIZE]) { uint32_t a[5]; uint64_t b[5]; a[0] = ctx->acc[0]; a[1] = ctx->acc[1]; a[2] = ctx->acc[2]; a[3] = ctx->acc[3]; a[4] = ctx->acc[4]; /* Check whether it is greater than p. */ b[0] = (uint64_t)(a[0]) + 5; b[1] = a[1] + (b[0] >> 32); b[2] = a[2] + (b[1] >> 32); b[3] = a[3] + (b[2] >> 32); b[4] = a[4] + (b[3] >> 32); /* Obtain the mask. If there is a carry, the number is greater than p. */ if ((b[4] & 0x4) == 0) { // b[4] & 0x4 is bit131. b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3]; } // Adding s at the end does not require modulo processing. b[0] = ctx->s[0] + (b[0] & 0xffffffff); b[1] = ctx->s[1] + (b[1] & 0xffffffff) + (b[0] >> 32); b[2] = ctx->s[2] + (b[2] & 0xffffffff) + (b[1] >> 32); b[3] = ctx->s[3] + (b[3] & 0xffffffff) + (b[2] >> 32); PUT_UINT32_LE(b[0], mac, 0); PUT_UINT32_LE(b[1], mac, 4); PUT_UINT32_LE(b[2], mac, 8); PUT_UINT32_LE(b[3], mac, 12); // Clear sensitive information. BSL_SAL_CleanseData(a, sizeof(a)); BSL_SAL_CleanseData(b, sizeof(b)); } // Clear the residual sensitive information in the register. // This function is implemented only when the assembly function is enabled. void Poly1305CleanRegister(void) { return; } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_poly1305.c
C
unknown
6,568
/* * 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_SM4) && defined(HITLS_CRYPTO_CBC) #include "crypt_modes_cbc.h" #include "modes_local.h" int32_t SM4_CBC_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CBC_Update(modeCtx, in, inLen, out, outLen); } int32_t SM4_CBC_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_CBC_Final(modeCtx, out, outLen); } int32_t SM4_CBC_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_CBC_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_cbc.c
C
unknown
1,219
/* * 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_SM4) && defined(HITLS_CRYPTO_CFB) #include "crypt_modes_cfb.h" #include "modes_local.h" int32_t SM4_CFB_InitCtx(MODES_CFB_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_CFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_CFB_Update(MODES_CFB_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CFB_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_cfb.c
C
unknown
1,078
/* * 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_SM4) && defined(HITLS_CRYPTO_CTR) #include "crypt_modes_ctr.h" #include "modes_local.h" int32_t SM4_CTR_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_CTR_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_CTR_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_CTR_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_ctr.c
C
unknown
1,082
/* * 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_SM4) && defined(HITLS_CRYPTO_ECB) #include "crypt_modes_ecb.h" #include "modes_local.h" int32_t SM4_ECB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_ECB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_ECB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_ECB_Update(modeCtx, in, inLen, out, outLen); } int32_t SM4_ECB_Final(MODES_CipherCtx *modeCtx, uint8_t *out, uint32_t *outLen) { return MODES_ECB_Final(modeCtx, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_ecb.c
C
unknown
1,219
/* * 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_SM4) && defined(HITLS_CRYPTO_GCM) #include "crypt_modes_gcm.h" #include "modes_local.h" int32_t SM4_GCM_InitCtx(MODES_GCM_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_GCM_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_GCM_Update(MODES_GCM_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_GCM_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_gcm.c
C
unknown
1,078
/* * 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_SM4) && defined(HITLS_CRYPTO_OFB) #include "crypt_modes_ofb.h" #include "modes_local.h" int32_t SM4_OFB_InitCtx(MODES_CipherCtx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_OFB_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } int32_t SM4_OFB_Update(MODES_CipherCtx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_OFB_Update(modeCtx, in, inLen, out, outLen); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_ofb.c
C
unknown
1,083
/* * 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 "bsl_err_internal.h" #include "crypt_sm4.h" #include "modes_local.h" /** * @brief Set the encryption key. * * @param ctx [IN] Mode handle * @param key [IN] Encrypt key * @param len [IN] Encrypt key length. Only 16 bytes (128 bits) are supported. * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t MODES_SM4_SetEncryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len) { return MODES_SetEncryptKey(ctx, key, len); } /** * @brief Set the decryption key. * * @param ctx [IN] Mode handle * @param key [IN] Decrypt key * @param len [IN] Decrypt key length. Only 16 bytes (128 bits) are supported. * @return Success: CRYPT_SUCCESS * Other error codes are returned if the operation fails. */ int32_t MODES_SM4_SetDecryptKey(MODES_CipherCommonCtx *ctx, const uint8_t *key, uint32_t len) { return MODES_SetDecryptKey(ctx, key, len); } #endif // HITLS_CRYPTO_SM4
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_setkey.c
C
unknown
1,564
/* * 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_SM4) && defined(HITLS_CRYPTO_XTS) #include "bsl_err_internal.h" #include "crypt_errno.h" #include "crypt_sm4.h" #include "crypt_modes_xts.h" #include "modes_local.h" int32_t SM4_XTS_Update(MODES_XTS_Ctx *modeCtx, const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen) { return MODES_XTS_Update(modeCtx, in, inLen, out, outLen); } int32_t SM4_XTS_InitCtx(MODES_XTS_Ctx *modeCtx, const uint8_t *key, uint32_t keyLen, const uint8_t *iv, uint32_t ivLen, bool enc) { return MODES_XTS_InitCtx(modeCtx, key, keyLen, iv, ivLen, enc); } #endif
2301_79861745/bench_create
crypto/modes/src/noasm_sm4_xts.c
C
unknown
1,155
/* * 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 POLY1305_CORE_H #define POLY1305_CORE_H #include "hitls_build.h" #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) #include "crypt_modes_chacha20poly1305.h" #include "modes_local.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #define POLY1305_BLOCKSIZE 16 #define POLY1305_TAGSIZE 16 #define POLY1305_KEYSIZE 32 void Poly1305InitForAsm(Poly1305Ctx *ctx); uint32_t Poly1305Block(Poly1305Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint32_t padbit); void Poly1305Last(Poly1305Ctx *ctx, uint8_t mac[POLY1305_TAGSIZE]); void Poly1305CleanRegister(void); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_CHACHA20POLY1305 #endif // POLY1305_CORE_H
2301_79861745/bench_create
crypto/modes/src/poly1305_core.h
C
unknown
1,266
/* * 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_PAILLIER_H #define CRYPT_PAILLIER_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_PAILLIER #include <stdlib.h> #include <stdint.h> #include "crypt_bn.h" #include "crypt_local_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ #define PAILLIER_MAX_MODULUS_BITS 16384 /* Paillier*/ typedef struct PAILLIER_Ctx CRYPT_PAILLIER_Ctx; typedef struct PAILLIER_Para CRYPT_PAILLIER_Para; /* Paillier method*/ /** * @ingroup paillier * @brief Allocate paillier context memory space. * * @retval (CRYPT_PAILLIER_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_PAILLIER_Ctx *CRYPT_PAILLIER_NewCtx(void); /** * @ingroup paillier * @brief Allocate paillier context memory space. * @param libCtx [IN] Library context * * @retval (CRYPT_PAILLIER_Ctx *) Pointer to the memory space of the allocated context * @retval NULL Invalid null pointer. */ CRYPT_PAILLIER_Ctx *CRYPT_PAILLIER_NewCtxEx(void *libCtx); /** * @ingroup paillier * @brief Copy the Paillier context. After the duplication is complete, call the CRYPT_PAILLIER_FreeCtx to release the memory. * * @param ctx [IN] PAILLIER context * * @return CRYPT_PAILLIER_Ctx Paillier context pointer * If the operation fails, a null value is returned. */ CRYPT_PAILLIER_Ctx *CRYPT_PAILLIER_DupCtx(CRYPT_PAILLIER_Ctx *keyCtx); /** * @ingroup paillier * @brief Create paillier key parameter structure * * @param para [IN] PAILLIER External parameter * * @retval (CRYPT_PAILLIER_Para *) Pointer to the allocated memory space of the structure * @retval NULL Invalid null pointer. */ CRYPT_PAILLIER_Para *CRYPT_PAILLIER_NewPara(const CRYPT_PaillierPara *para); /** * @ingroup paillier * @brief release paillier key context structure * * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker. */ void CRYPT_PAILLIER_FreeCtx(CRYPT_PAILLIER_Ctx *ctx); /** * @ingroup paillier * @brief Release paillier key parameter structure * * @param para [IN] Storage pointer in the parameter structure to be released. The parameter is set NULL by the invoker. */ void CRYPT_PAILLIER_FreePara(CRYPT_PAILLIER_Para *para); /** * @ingroup paillier * @brief Set the data of the key parameter structure to the key structure. * * @param ctx [OUT] Paillier context structure for which related parameters need to be set * @param para [IN] Key parameter structure * * @retval CRYPT_NULL_INPUT Invalid null pointer input. * @retval CRYPT_PAILLIER_ERR_KEY_BITS The expected key length does not meet the requirements. * @retval CRYPT_PAILLIER_ERR_E_VALUE The expected value of e does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL internal memory allocation error * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_PAILLIER_SetPara(CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPara *para); /** * @ingroup paillier * @brief Obtain the valid length of the key. * * @param ctx [IN] Structure from which the key length is expected to be obtained * * @retval 0: The input is incorrect or the corresponding key structure does not have a valid key length. * @retval uint32_t: Valid key length */ uint32_t CRYPT_PAILLIER_GetBits(const CRYPT_PAILLIER_Ctx *ctx); /** * @ingroup paillier * @brief Generate the Paillier key pair. * * @param ctx [IN/OUT] paillier context structure * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_PAILLIER_ERR_KEY_BITS The value of e in the context structure does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The key pair is successfully generated. */ int32_t CRYPT_PAILLIER_Gen(CRYPT_PAILLIER_Ctx *ctx); /** * @ingroup paillier * @brief Paillier public key encryption * * @param ctx [IN] Paillier context structure * @param input [IN] Information to be encrypted * @param inputLen [IN] Length of the information to be encrypted * @param out [OUT] Pointer to the encrypted information output. * @param outLen [IN/OUT] Pointer to the length of the encrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_PAILLIER_NO_KEY_INFO does not contain the key information. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS encryption succeeded. */ int32_t CRYPT_PAILLIER_PubEnc(const CRYPT_PAILLIER_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen); /** * @ingroup paillier * @brief Paillier private key decryption * * @param ctx [IN] Paillier context structure * @param ciphertext [IN] Information to be decrypted * @param bits [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the decrypted information output. * @param outLen [IN/OUT] Pointer to the length of the decrypted information. * Before being transferred, the value must be set to the maximum length of the array. * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_PAILLIER_ERR_DEC_BITS Incorrect length of the encrypted private key. * @retval CRYPT_PAILLIER_NO_KEY_INFO does not contain the key information. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS Decrypted Successfully */ int32_t CRYPT_PAILLIER_PrvDec(const CRYPT_PAILLIER_Ctx *ctx, const BN_BigNum *ciphertext, uint32_t bits, uint8_t *out, uint32_t *outLen); /** * @ingroup paillier * @brief Paillier Set the private key information. * * @param ctx [OUT] paillier context structure * @param prv [IN] Private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_PAILLIER_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_PAILLIER_NO_KEY_INFO does not contain the key information. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is successfully set. */ int32_t CRYPT_PAILLIER_SetPrvKey(CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPrv *prv); /** * @ingroup paillier * @brief Paillier Set the public key information. * * @param ctx [OUT] Paillier context structure * @param pub [IN] Public key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_PAILLIER_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is successfully set. */ int32_t CRYPT_PAILLIER_SetPubKey(CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPub *pub); /** * @ingroup paillier * @brief Paillier Obtain the private key information. * * @param ctx [IN] Paillier context structure * @param prv [OUT] Private key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is obtained successfully. */ int32_t CRYPT_PAILLIER_GetPrvKey(const CRYPT_PAILLIER_Ctx *ctx, CRYPT_PaillierPrv *prv); /** * @ingroup paillier * @brief Paillier Obtain the public key information. * * @param ctx [IN] Paillier context structure * @param pub [OUT] Public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is obtained successfully. */ int32_t CRYPT_PAILLIER_GetPubKey(const CRYPT_PAILLIER_Ctx *ctx, CRYPT_PaillierPub *pub); #ifdef HITLS_BSL_PARAMS /** * @ingroup paillier * @brief Set the data of the key parameter structure to the key structure. * * @param ctx [OUT] Paillier context structure for which related parameters need to be set * @param para [IN] Key parameter structure * * @retval CRYPT_NULL_INPUT Invalid null pointer input. * @retval CRYPT_PAILLIER_ERR_KEY_BITS The expected key length does not meet the requirements. * @retval CRYPT_PAILLIER_ERR_E_VALUE The expected value of e does not meet the requirements. * @retval CRYPT_MEM_ALLOC_FAIL internal memory allocation error * @retval CRYPT_SUCCESS set successfully. */ int32_t CRYPT_PAILLIER_SetParaEx(CRYPT_PAILLIER_Ctx *ctx, const BSL_Param *para); /** * @ingroup paillier * @brief Paillier Set the private key information. * * @param ctx [OUT] paillier context structure * @param prv [IN] Private key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_PAILLIER_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_PAILLIER_NO_KEY_INFO does not contain the key information. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is successfully set. */ int32_t CRYPT_PAILLIER_SetPrvKeyEx(CRYPT_PAILLIER_Ctx *ctx, const BSL_Param *para); /** * @ingroup paillier * @brief Paillier Set the public key information. * * @param ctx [OUT] Paillier context structure * @param pub [IN] Public key data * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_PAILLIER_ERR_KEY_BITS The key length does not meet the requirements. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is successfully set. */ int32_t CRYPT_PAILLIER_SetPubKeyEx(CRYPT_PAILLIER_Ctx *ctx, const BSL_Param *para); /** * @ingroup paillier * @brief Paillier Obtain the private key information. * * @param ctx [IN] Paillier context structure * @param prv [OUT] Private key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The private key is obtained successfully. */ int32_t CRYPT_PAILLIER_GetPrvKeyEx(const CRYPT_PAILLIER_Ctx *ctx, BSL_Param *para); /** * @ingroup paillier * @brief Paillier Obtain the public key information. * * @param ctx [IN] Paillier context structure * @param pub [OUT] Public key data * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval BN error An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS The public key is obtained successfully. */ int32_t CRYPT_PAILLIER_GetPubKeyEx(const CRYPT_PAILLIER_Ctx *ctx, BSL_Param *para); #endif /** * @ingroup paillier * @brief PAILLIER public key encryption * * @param ctx [IN] PAILLIER context structure * @param data [IN] Information to be encrypted * @param dataLen [IN] Length of the information to be encrypted * @param out [OUT] Pointer to the encrypted information output. * @param outLen [OUT] Pointer to the length of the encrypted information * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_PAILLIER_NO_KEY_INFO does not contain the key information. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A safe function error occurs. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the encryption method. * @retval CRYPT_SUCCESS encryption succeeded. */ int32_t CRYPT_PAILLIER_Encrypt(CRYPT_PAILLIER_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); /** * @ingroup paillier * @brief PAILLIER private key decryption * * @param ctx [IN] PAILLIER context structure * @param data [IN] Information to be decrypted * @param dataLen [IN] Length of the information to be decrypted * @param out [OUT] Pointer to the output information after decryption. * @param outLen [OUT] Pointer to the length of the decrypted information * * @retval CRYPT_NULL_INPUT Error null pointer input * @retval CRYPT_PAILLIER_NO_KEY_INFO does not contain the key information. * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. * @retval CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SECUREC_FAIL A security function error occurs. * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the decryption method. * @retval BN error. An error occurs in the internal BigNum operation. * @retval CRYPT_SUCCESS Decryption succeeded. */ int32_t CRYPT_PAILLIER_Decrypt(CRYPT_PAILLIER_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); /** * @ingroup paillier * @brief PAILLIER get security bits * * @param ctx [IN] PAILLIER Context structure * * @retval security bits */ int32_t CRYPT_PAILLIER_GetSecBits(const CRYPT_PAILLIER_Ctx *ctx); /** * @ingroup paillier * @brief PAILLIER control function for various operations * * @param ctx [IN/OUT] PAILLIER context structure * @param opt [IN] Control operation type * @param val [IN/OUT] Parameter value for the operation * @param len [IN] Length of the parameter value * * @retval CRYPT_NULL_INPUT Invalid null pointer input * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE The entered value does not meet the calculation conditions * @retval CRYPT_PAILLIER_NO_KEY_INFO Does not contain the key information * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_EAL_ALG_NOT_SUPPORT Operation not supported * @retval CRYPT_SUCCESS Operation succeeded */ int32_t CRYPT_PAILLIER_Ctrl(CRYPT_PAILLIER_Ctx *ctx, int32_t opt, void *val, uint32_t len); /** * @ingroup paillier * @brief PAILLIER homomorphic addition operation (c_res = c1 * c2 mod n^2) * * @param ctx [IN] Paillier context structure * @param input [IN] Addition operands, must include two ciphertext parameters * @param out [OUT] The result of the addition operation * @param outLen [IN/OUT] Pointer to the length of the output buffer * * @retval CRYPT_NULL_INPUT Any input pointer is NULL * @retval CRYPT_PAILLIER_NO_KEY_INFO Context does not contain valid public key information * @retval CRYPT_PAILLIER_ERR_INPUT_VALUE Input ciphertext value is incorrect * @retval CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH Output buffer length is insufficient * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure * @retval CRYPT_SUCCESS Operation succeeded, result is written to out */ int32_t CRYPT_PAILLIER_Add(const void *ctx, const BSL_Param *input, uint8_t *out, uint32_t *outLen); #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_PAILLIER #endif // CRYPT_PAILLIER_H
2301_79861745/bench_create
crypto/paillier/include/crypt_paillier.h
C
unknown
17,517
/* * 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_PAILLIER #include "crypt_utils.h" #include "crypt_paillier.h" #include "paillier_local.h" #include "crypt_errno.h" #include "bsl_sal.h" #include "securec.h" #include "bsl_err_internal.h" int32_t CRYPT_PAILLIER_PubEnc(const CRYPT_PAILLIER_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out, uint32_t *outLen) { int32_t ret; CRYPT_PAILLIER_PubKey *pubKey = ctx->pubKey; if (pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } uint32_t bits = CRYPT_PAILLIER_GetBits(ctx); BN_Optimizer *optimizer = BN_OptimizerCreate(); if (optimizer == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } BN_BigNum *m = BN_Create(bits); BN_BigNum *r = BN_Create(bits); BN_BigNum *gm = BN_Create(bits); BN_BigNum *rn = BN_Create(bits); BN_BigNum *result = BN_Create(bits); BN_BigNum *gcd_result = BN_Create(bits); bool createFailed = (m == NULL || r == NULL || gm == NULL || rn == NULL || result == NULL || gcd_result == NULL); if (createFailed) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto EXIT; } ret = BN_Bin2Bn(m, input, inputLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // Check whether m is less than n and non-negative if (BN_Cmp(m, pubKey->n) >= 0 || BN_IsNegative(m)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); ret = CRYPT_PAILLIER_ERR_INPUT_VALUE; goto EXIT; } ret = BN_RandRangeEx(ctx->libCtx, r, pubKey->n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } while (true) { // Check whether r is relatively prime to n, if not, regenerate r ret = BN_Gcd(gcd_result, r, pubKey->n, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if (BN_IsOne(gcd_result)) { break; } ret = BN_RandRangeEx(ctx->libCtx, r, pubKey->n); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } } ret = BN_ModExp(gm, pubKey->g, m, pubKey->n2, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_ModExp(rn, r, pubKey->n, pubKey->n2, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_ModMul(result, gm, rn, pubKey->n2, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Bn2Bin(result, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: BN_Destroy(m); BN_Destroy(r); BN_Destroy(gm); BN_Destroy(rn); BN_Destroy(result); BN_Destroy(gcd_result); BN_OptimizerDestroy(optimizer); return ret; } int32_t CRYPT_PAILLIER_PrvDec(const CRYPT_PAILLIER_Ctx *ctx, const BN_BigNum *ciphertext, uint32_t bits, uint8_t *out, uint32_t *outLen) { int32_t ret; CRYPT_PAILLIER_PrvKey *prvKey = ctx->prvKey; if (prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BN_Optimizer *optimizer = BN_OptimizerCreate(); if (optimizer == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } BN_BigNum *m = BN_Create(bits); BN_BigNum *result = BN_Create(bits); bool createFailed = (m == NULL || result == NULL); if (createFailed) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto EXIT; } ret = BN_ModExp(m, ciphertext, prvKey->lambda, prvKey->n2, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_SubLimb(result, m, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Div(result, NULL, result, prvKey->n, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_ModMul(result, result, prvKey->mu, prvKey->n, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Bn2Bin(result, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: BN_Destroy(m); BN_Destroy(result); BN_OptimizerDestroy(optimizer); return ret; } static int32_t EncryptInputCheck(const CRYPT_PAILLIER_Ctx *ctx, const uint8_t *input, uint32_t inputLen, const uint8_t *out, const uint32_t *outLen) { if (ctx == NULL || (input == NULL && inputLen != 0) || out == NULL || outLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->pubKey == NULL) { // Check whether the public key information exists. BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_NO_KEY_INFO); return CRYPT_PAILLIER_NO_KEY_INFO; } // Check whether the length of the out is sufficient to place the encryption information. uint32_t bits = CRYPT_PAILLIER_GetBits(ctx); if ((*outLen) < BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH); return CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH; } if (inputLen > BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_ENC_BITS); return CRYPT_PAILLIER_ERR_ENC_BITS; } return CRYPT_SUCCESS; } int32_t CRYPT_PAILLIER_Encrypt(CRYPT_PAILLIER_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; } ret = CRYPT_PAILLIER_PubEnc(ctx, data, dataLen, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static int32_t DecryptInputCheck(const CRYPT_PAILLIER_Ctx *ctx, const uint8_t *data, uint32_t dataLen, const uint8_t *out, const uint32_t *outLen) { if (ctx == NULL || data == NULL || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->prvKey == NULL) { // Check whether the private key information exists. BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_NO_KEY_INFO); return CRYPT_PAILLIER_NO_KEY_INFO; } // Check whether the length of the out is sufficient to place the decryption information. uint32_t bits = CRYPT_PAILLIER_GetBits(ctx); if ((*outLen) < BN_BITS_TO_BYTES(bits)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH); return CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH; } if (dataLen != BN_BITS_TO_BYTES(bits) * 2) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_DEC_BITS); return CRYPT_PAILLIER_ERR_DEC_BITS; } return CRYPT_SUCCESS; } static int32_t CRYPT_PAILLIER_CheckCiphertext(const BN_BigNum* ciphertext, const CRYPT_PAILLIER_PrvKey* prvKey) { if (BN_Cmp(ciphertext, prvKey->n2) >= 0 || BN_IsNegative(ciphertext)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); return CRYPT_PAILLIER_ERR_INPUT_VALUE; } int32_t ret = CRYPT_SUCCESS; BN_BigNum *gcd_result = BN_Create(BN_Bits(ciphertext)); BN_Optimizer *optimizer = BN_OptimizerCreate(); if (gcd_result == NULL || optimizer == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto EXIT; } ret = BN_Gcd(gcd_result, ciphertext, prvKey->n2, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if (BN_IsOne(gcd_result) == false) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); ret = CRYPT_PAILLIER_ERR_INPUT_VALUE; } EXIT: BN_Destroy(gcd_result); BN_OptimizerDestroy(optimizer); return ret; } int32_t CRYPT_PAILLIER_Decrypt(CRYPT_PAILLIER_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen) { int32_t ret = DecryptInputCheck(ctx, data, dataLen, out, outLen); if (ret != CRYPT_SUCCESS) { return ret; } uint32_t bits = CRYPT_PAILLIER_GetBits(ctx); BN_BigNum *c = BN_Create(bits); if (c == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ret = BN_Bin2Bn(c, data, dataLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // check whether c is in Zn2* ret = CRYPT_PAILLIER_CheckCiphertext(c, ctx->prvKey); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = CRYPT_PAILLIER_PrvDec(ctx, c, bits, out, outLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: BN_Destroy(c); return ret; } static int32_t CRYPT_PAILLIER_GetLen(const CRYPT_PAILLIER_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_PAILLIER_Ctrl(CRYPT_PAILLIER_Ctx *ctx, int32_t opt, void *val, uint32_t len) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } switch (opt) { case CRYPT_CTRL_GET_BITS: return CRYPT_PAILLIER_GetLen(ctx, (GetLenFunc)CRYPT_PAILLIER_GetBits, val, len); case CRYPT_CTRL_GET_SECBITS: return CRYPT_PAILLIER_GetLen(ctx, (GetLenFunc)CRYPT_PAILLIER_GetSecBits, val, len); default: BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_CTRL_NOT_SUPPORT_ERROR); return CRYPT_PAILLIER_CTRL_NOT_SUPPORT_ERROR; } } static int32_t CRYPT_PAILLIER_CheckHECiphertext(const BN_BigNum* ciphertext, const CRYPT_PAILLIER_PubKey* pubKey, BN_Optimizer *optimizer) { if (BN_Cmp(ciphertext, pubKey->n2) >= 0 || BN_IsNegative(ciphertext)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); return CRYPT_PAILLIER_ERR_INPUT_VALUE; } int32_t ret = CRYPT_SUCCESS; BN_BigNum *gcd_result = BN_Create(BN_Bits(ciphertext)); if (gcd_result == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto EXIT; } ret = BN_Gcd(gcd_result, ciphertext, pubKey->n2, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if (BN_IsOne(gcd_result) == false) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); ret = CRYPT_PAILLIER_ERR_INPUT_VALUE; } EXIT: BN_Destroy(gcd_result); return ret; } int32_t CRYPT_PAILLIER_Add(const void *ctx, const BSL_Param *input, uint8_t *out, uint32_t *outLen) { if (ctx == NULL || input == NULL || out == NULL || outLen == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } const CRYPT_PAILLIER_Ctx *paillierCtx = ctx; if (paillierCtx == NULL || paillierCtx->pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_NO_KEY_INFO); return CRYPT_PAILLIER_NO_KEY_INFO; } const BSL_Param *ciphertext1Param = BSL_PARAM_FindConstParam(input, CRYPT_PARAM_PKEY_HE_CIPHERTEXT1); const BSL_Param *ciphertext2Param = BSL_PARAM_FindConstParam(input, CRYPT_PARAM_PKEY_HE_CIPHERTEXT2); if (ciphertext1Param == NULL || ciphertext2Param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); return CRYPT_PAILLIER_ERR_INPUT_VALUE; } uint32_t bits = CRYPT_PAILLIER_GetBits(paillierCtx); uint32_t n2Bytes = BN_BITS_TO_BYTES(bits) * 2; int32_t ret = CRYPT_SUCCESS; uint8_t *ciphertext1 = (uint8_t *)BSL_SAL_Malloc(n2Bytes); uint8_t *ciphertext2 = (uint8_t *)BSL_SAL_Malloc(n2Bytes); uint32_t ciphertext1Len = n2Bytes; uint32_t ciphertext2Len = n2Bytes; if (ciphertext1 == NULL || ciphertext2 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); BSL_SAL_Free(ciphertext1); BSL_SAL_Free(ciphertext2); return CRYPT_MEM_ALLOC_FAIL; } BN_BigNum *c1 = BN_Create(bits); BN_BigNum *c2 = BN_Create(bits); BN_BigNum *result = BN_Create(bits); if (c1 == NULL || c2 == NULL || result == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BN_Destroy(c1); BN_Destroy(c2); BN_Destroy(result); BSL_SAL_Free(ciphertext1); BSL_SAL_Free(ciphertext2); return ret; } BN_Optimizer *optimizer = BN_OptimizerCreate(); if (optimizer == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto EXIT; } if (BSL_PARAM_GetValue(ciphertext1Param, CRYPT_PARAM_PKEY_HE_CIPHERTEXT1, BSL_PARAM_TYPE_OCTETS, ciphertext1, &ciphertext1Len) != BSL_SUCCESS || BSL_PARAM_GetValue(ciphertext2Param, CRYPT_PARAM_PKEY_HE_CIPHERTEXT2, BSL_PARAM_TYPE_OCTETS, ciphertext2, &ciphertext2Len) != BSL_SUCCESS) { ret = CRYPT_PAILLIER_ERR_INPUT_VALUE; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // Check whether the input ciphertext meets the specifications if (ciphertext1Len != n2Bytes || ciphertext2Len != n2Bytes) { ret = CRYPT_PAILLIER_ERR_INPUT_VALUE; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // Check whether the length of the out is sufficient to place the result if (*outLen < n2Bytes) { ret = CRYPT_PAILLIER_BUFF_LEN_NOT_ENOUGH; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if ((ret = BN_Bin2Bn(c1, ciphertext1, ciphertext1Len)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if ((ret = BN_Bin2Bn(c2, ciphertext2, ciphertext2Len)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // Verify that the ciphertext is within the valid range [0, n^2) if ((ret = CRYPT_PAILLIER_CheckHECiphertext(c1, paillierCtx->pubKey, optimizer))!= CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if ((ret = CRYPT_PAILLIER_CheckHECiphertext(c2, paillierCtx->pubKey, optimizer))!= CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // Add: c_res = c1 * c2 mod n^2 if ((ret = BN_ModMul(result, c1, c2, paillierCtx->pubKey->n2, optimizer)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } // Export result as fixed-length n^2 bytes with leading-zero padding uint8_t *tmpBuf = (uint8_t *)BSL_SAL_Malloc(n2Bytes); if (tmpBuf == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } uint32_t actualLen = n2Bytes; ret = BN_Bn2Bin(result, tmpBuf, &actualLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); BSL_SAL_Free(tmpBuf); goto EXIT; } (void)memset_s(out, n2Bytes, 0, n2Bytes); if (actualLen > 0) { (void)memcpy_s(out + (n2Bytes - actualLen), actualLen, tmpBuf, actualLen); } *outLen = n2Bytes; BSL_SAL_Free(tmpBuf); EXIT: BN_Destroy(c1); BN_Destroy(c2); BN_Destroy(result); BN_OptimizerDestroy(optimizer); BSL_SAL_Free(ciphertext1); BSL_SAL_Free(ciphertext2); return ret; } #endif // HITLS_CRYPTO_PAILLIER
2301_79861745/bench_create
crypto/paillier/src/paillier_encdec.c
C
unknown
16,085
/* * 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_PAILLIER #include "crypt_paillier.h" #include "paillier_local.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "securec.h" #include "bsl_sal.h" #include "bsl_err_internal.h" #include "crypt_utils.h" #include "crypt_params_key.h" CRYPT_PAILLIER_Ctx *CRYPT_PAILLIER_NewCtx(void) { CRYPT_PAILLIER_Ctx *ctx = NULL; ctx = (CRYPT_PAILLIER_Ctx *)BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(ctx, sizeof(CRYPT_PAILLIER_Ctx), 0, sizeof(CRYPT_PAILLIER_Ctx)); BSL_SAL_ReferencesInit(&(ctx->references)); return ctx; } CRYPT_PAILLIER_Ctx *CRYPT_PAILLIER_NewCtxEx(void *libCtx) { CRYPT_PAILLIER_Ctx *ctx = CRYPT_PAILLIER_NewCtx(); if (ctx == NULL) { return NULL; } ctx->libCtx = libCtx; return ctx; } static CRYPT_PAILLIER_PubKey *PaillierPubKeyDupCtx(CRYPT_PAILLIER_PubKey *pubKey) { CRYPT_PAILLIER_PubKey *newPubKey = (CRYPT_PAILLIER_PubKey *)BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_PubKey)); if (newPubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newPubKey, sizeof(CRYPT_PAILLIER_PubKey), 0, sizeof(CRYPT_PAILLIER_PubKey)); GOTO_ERR_IF_SRC_NOT_NULL(newPubKey->n, pubKey->n, BN_Dup(pubKey->n), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPubKey->g, pubKey->g, BN_Dup(pubKey->g), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPubKey->n2, pubKey->n2, BN_Dup(pubKey->n2), CRYPT_MEM_ALLOC_FAIL); return newPubKey; ERR: PAILLIER_FREE_PUB_KEY(newPubKey); return NULL; } static CRYPT_PAILLIER_PrvKey *PaillierPrvKeyDupCtx(CRYPT_PAILLIER_PrvKey *prvKey) { CRYPT_PAILLIER_PrvKey *newPrvKey = (CRYPT_PAILLIER_PrvKey *)BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_PrvKey)); if (newPrvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newPrvKey, sizeof(CRYPT_PAILLIER_PrvKey), 0, sizeof(CRYPT_PAILLIER_PrvKey)); GOTO_ERR_IF_SRC_NOT_NULL(newPrvKey->n, prvKey->n, BN_Dup(prvKey->n), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPrvKey->lambda, prvKey->lambda, BN_Dup(prvKey->lambda), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPrvKey->mu, prvKey->mu, BN_Dup(prvKey->mu), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPrvKey->n2, prvKey->n2, BN_Dup(prvKey->n2), CRYPT_MEM_ALLOC_FAIL); return newPrvKey; ERR: PAILLIER_FREE_PRV_KEY(newPrvKey); return NULL; } static CRYPT_PAILLIER_Para *PaillierParaDupCtx(CRYPT_PAILLIER_Para *para) { CRYPT_PAILLIER_Para *newPara = (CRYPT_PAILLIER_Para *)BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_Para)); if (newPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newPara, sizeof(CRYPT_PAILLIER_Para), 0, sizeof(CRYPT_PAILLIER_Para)); newPara->bits = para->bits; GOTO_ERR_IF_SRC_NOT_NULL(newPara->p, para->p, BN_Dup(para->p), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newPara->q, para->q, BN_Dup(para->q), CRYPT_MEM_ALLOC_FAIL); return newPara; ERR: PAILLIER_FREE_PARA(newPara); return NULL; } CRYPT_PAILLIER_Ctx *CRYPT_PAILLIER_DupCtx(CRYPT_PAILLIER_Ctx *keyCtx) { if (keyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return NULL; } CRYPT_PAILLIER_Ctx *newKeyCtx = NULL; newKeyCtx = BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_Ctx)); if (newKeyCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } (void)memset_s(newKeyCtx, sizeof(CRYPT_PAILLIER_Ctx), 0, sizeof(CRYPT_PAILLIER_Ctx)); GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->prvKey, keyCtx->prvKey, PaillierPrvKeyDupCtx(keyCtx->prvKey), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->pubKey, keyCtx->pubKey, PaillierPubKeyDupCtx(keyCtx->pubKey), CRYPT_MEM_ALLOC_FAIL); GOTO_ERR_IF_SRC_NOT_NULL(newKeyCtx->para, keyCtx->para, PaillierParaDupCtx(keyCtx->para), CRYPT_MEM_ALLOC_FAIL); newKeyCtx->libCtx = keyCtx->libCtx; BSL_SAL_ReferencesInit(&(newKeyCtx->references)); return newKeyCtx; ERR: CRYPT_PAILLIER_FreeCtx(newKeyCtx); return NULL; } static int32_t PaillierNewParaBasicCheck(const CRYPT_PaillierPara *para) { if (para == NULL || para->p == NULL || para->pLen == 0 || para->q == NULL || para->qLen == 0 || para->bits <= 0 || para->bits > PAILLIER_MAX_MODULUS_BITS) { BSL_ERR_PUSH_ERROR(CRYPT_INVALID_ARG); return CRYPT_INVALID_ARG; } /* the length of p and q should be equal to bits */ if (para->pLen != BN_BITS_TO_BYTES(para->bits) || para->qLen != BN_BITS_TO_BYTES(para->bits)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_KEY_BITS); return CRYPT_PAILLIER_ERR_KEY_BITS; } return CRYPT_SUCCESS; } CRYPT_PAILLIER_Para *CRYPT_PAILLIER_NewPara(const CRYPT_PaillierPara *para) { int32_t ret = PaillierNewParaBasicCheck(para); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return NULL; } CRYPT_PAILLIER_Para *retPara = BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_Para)); if (retPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } retPara->bits = para->bits; retPara->p = BN_Create(para->bits); retPara->q = BN_Create(para->bits); if (retPara->p == NULL || retPara->q == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } return retPara; ERR: CRYPT_PAILLIER_FreePara(retPara); return NULL; } void CRYPT_PAILLIER_FreeCtx(CRYPT_PAILLIER_Ctx *ctx) { if (ctx == NULL) { return; } int i = 0; BSL_SAL_AtomicDownReferences(&(ctx->references), &i); if (i > 0) { return; } BSL_SAL_ReferencesFree(&(ctx->references)); PAILLIER_FREE_PRV_KEY(ctx->prvKey); PAILLIER_FREE_PUB_KEY(ctx->pubKey); PAILLIER_FREE_PARA(ctx->para); BSL_SAL_Free(ctx); } void CRYPT_PAILLIER_FreePara(CRYPT_PAILLIER_Para *para) { if (para == NULL) { return; } BN_Destroy(para->p); BN_Destroy(para->q); BSL_SAL_Free(para); } void PAILLIER_FreePrvKey(CRYPT_PAILLIER_PrvKey *prvKey) { if (prvKey == NULL) { return; } BN_Destroy(prvKey->n); BN_Destroy(prvKey->lambda); BN_Destroy(prvKey->mu); BN_Destroy(prvKey->n2); BSL_SAL_Free(prvKey); } void PAILLIER_FreePubKey(CRYPT_PAILLIER_PubKey *pubKey) { if (pubKey == NULL) { return; } BN_Destroy(pubKey->n); BN_Destroy(pubKey->g); BN_Destroy(pubKey->n2); BSL_SAL_Free(pubKey); } static int32_t IsPAILLIERSetParaVaild(const CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PAILLIER_Para *para) { if (ctx == NULL || para == NULL || para->p == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (para->bits > PAILLIER_MAX_MODULUS_BITS || para->bits <= 0) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_KEY_BITS); return CRYPT_PAILLIER_ERR_KEY_BITS; } return CRYPT_SUCCESS; } CRYPT_PAILLIER_Para *CRYPT_Paillier_DupPara(const CRYPT_PAILLIER_Para *para) { CRYPT_PAILLIER_Para *paraCopy = BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_Para)); if (paraCopy == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } paraCopy->bits = para->bits; paraCopy->p = BN_Dup(para->p); paraCopy->q = BN_Dup(para->q); if (paraCopy->p == NULL || paraCopy->q == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); PAILLIER_FREE_PARA(paraCopy); return NULL; } return paraCopy; } int32_t CRYPT_PAILLIER_SetPara(CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPara *para) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_PAILLIER_Para *paillierPara = CRYPT_PAILLIER_NewPara(para); if (paillierPara == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } int32_t ret = IsPAILLIERSetParaVaild(ctx, paillierPara); if (ret != CRYPT_SUCCESS) { CRYPT_PAILLIER_FreePara(paillierPara); return ret; } PAILLIER_FREE_PARA(ctx->para); PAILLIER_FREE_PUB_KEY(ctx->pubKey); PAILLIER_FREE_PRV_KEY(ctx->prvKey); ctx->para = paillierPara; return CRYPT_SUCCESS; } #ifdef HITLS_BSL_PARAMS int32_t CRYPT_PAILLIER_SetParaEx(CRYPT_PAILLIER_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_PaillierPara paillierPara = {0}; uint32_t bitsLen = sizeof(uint32_t); int32_t ret = 0; (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_P, &(paillierPara.p), &(paillierPara.pLen)); (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_Q, &(paillierPara.q), &(paillierPara.qLen)); const BSL_Param *temp = BSL_PARAM_FindConstParam(para, CRYPT_PARAM_PAILLIER_BITS); if (temp != NULL) { RETURN_RET_IF_ERR(BSL_PARAM_GetValue(temp, CRYPT_PARAM_PAILLIER_BITS, BSL_PARAM_TYPE_UINT32, &paillierPara.bits, &bitsLen), ret); } return CRYPT_PAILLIER_SetPara(ctx, &paillierPara); } #endif uint32_t CRYPT_PAILLIER_GetBits(const CRYPT_PAILLIER_Ctx *ctx) { if (ctx == NULL) { return 0; } if (ctx->para != NULL) { return ctx->para->bits; } if (ctx->prvKey != NULL) { return BN_Bits(ctx->prvKey->lambda); } if (ctx->pubKey != NULL) { return BN_Bits(ctx->pubKey->n); } return 0; } CRYPT_PAILLIER_PrvKey *Paillier_NewPrvKey(uint32_t bits) { CRYPT_PAILLIER_PrvKey *prvKey = (CRYPT_PAILLIER_PrvKey *)BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_PrvKey)); if (prvKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } prvKey->n = BN_Create(bits); prvKey->lambda = BN_Create(bits); prvKey->mu = BN_Create(bits); prvKey->n2 = BN_Create(bits); if (prvKey->n == NULL || prvKey->lambda == NULL || prvKey->mu == NULL || prvKey->n2 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); PAILLIER_FREE_PRV_KEY(prvKey); } return prvKey; } CRYPT_PAILLIER_PubKey *Paillier_NewPubKey(uint32_t bits) { CRYPT_PAILLIER_PubKey *pubKey = (CRYPT_PAILLIER_PubKey *)BSL_SAL_Malloc(sizeof(CRYPT_PAILLIER_PubKey)); if (pubKey == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } pubKey->n = BN_Create(bits); pubKey->g = BN_Create(bits); pubKey->n2 = BN_Create(bits); if (pubKey->n == NULL || pubKey->g == NULL || pubKey->n2 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); PAILLIER_FREE_PUB_KEY(pubKey); } return pubKey; } static int32_t Paillier_GenPQ(CRYPT_PAILLIER_Para *para, BN_Optimizer *optimizer) { uint32_t bits = para->bits; int32_t ret = BN_GenPrime(para->p, NULL, bits, true, optimizer, NULL); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_GenPrime(para->q, NULL, bits, true, optimizer, NULL); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static int32_t Paillier_CalcPubKey(CRYPT_PAILLIER_PubKey *pubKey, CRYPT_PAILLIER_Para *para, BN_Optimizer *optimizer) { int32_t ret = BN_Mul(pubKey->n, para->p, para->q, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_AddLimb(pubKey->g, pubKey->n, 1); // g = n + 1 if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_Sqr(pubKey->n2, pubKey->n, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } static int32_t Paillier_CalcLambda(BN_BigNum *lambda, CRYPT_PAILLIER_Para *para, BN_Optimizer *optimizer) { uint32_t bits = para->bits; BN_BigNum *pMinus1 = BN_Create(bits); BN_BigNum *qMinus1 = BN_Create(bits); int32_t ret = CRYPT_MEM_ALLOC_FAIL; if (pMinus1 == NULL || qMinus1 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto EXIT; } ret = BN_SubLimb(pMinus1, para->p, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_SubLimb(qMinus1, para->q, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Lcm(lambda, pMinus1, qMinus1, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: BN_Destroy(pMinus1); BN_Destroy(qMinus1); return ret; } static int32_t Paillier_CalcMu(BN_BigNum *mu, const BN_BigNum *lambda, CRYPT_PAILLIER_PubKey *pubKey, uint32_t bits, BN_Optimizer *optimizer) { BN_BigNum *x = BN_Create(bits); BN_BigNum *xMinus1 = BN_Create(bits); BN_BigNum *Lx = BN_Create(bits); int32_t ret; if (x == NULL || xMinus1 == NULL || Lx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); ret = CRYPT_MEM_ALLOC_FAIL; goto EXIT; } ret = BN_ModExp(x, pubKey->g, lambda, pubKey->n2, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_SubLimb(xMinus1, x, 1); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Div(Lx, NULL, xMinus1, pubKey->n, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_ModInv(mu, Lx, pubKey->n, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } EXIT: BN_Destroy(x); BN_Destroy(xMinus1); BN_Destroy(Lx); return ret; } int32_t Paillier_CalcPrvKey(CRYPT_PAILLIER_Ctx *ctx, BN_Optimizer *optimizer) { int32_t ret = Paillier_CalcLambda(ctx->prvKey->lambda, ctx->para, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = Paillier_CalcMu(ctx->prvKey->mu, ctx->prvKey->lambda, ctx->pubKey, ctx->para->bits, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); } return ret; } int32_t CRYPT_PAILLIER_Gen(CRYPT_PAILLIER_Ctx *ctx) { if (ctx == NULL || ctx->para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = CRYPT_MEM_ALLOC_FAIL; BN_Optimizer *optimizer = NULL; CRYPT_PAILLIER_Ctx *newCtx = CRYPT_PAILLIER_NewCtx(); if (newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } newCtx->para = CRYPT_Paillier_DupPara(ctx->para); if (newCtx->para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); goto ERR; } newCtx->prvKey = Paillier_NewPrvKey(newCtx->para->bits); newCtx->pubKey = Paillier_NewPubKey(newCtx->para->bits); optimizer = BN_OptimizerCreate(); if (optimizer == NULL || newCtx->prvKey == NULL || newCtx->pubKey == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } BN_OptimizerSetLibCtx(ctx->libCtx, optimizer); ret = Paillier_GenPQ(newCtx->para, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = Paillier_CalcPubKey(newCtx->pubKey, newCtx->para, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = Paillier_CalcPrvKey(newCtx, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(BN_Copy(newCtx->prvKey->n, newCtx->pubKey->n), ret); GOTO_ERR_IF(BN_Copy(newCtx->prvKey->n2, newCtx->pubKey->n2), ret); PAILLIER_FREE_PARA(ctx->para); PAILLIER_FREE_PRV_KEY(ctx->prvKey); PAILLIER_FREE_PUB_KEY(ctx->pubKey); BSL_SAL_ReferencesFree(&(newCtx->references)); ctx->prvKey = newCtx->prvKey; ctx->pubKey = newCtx->pubKey; ctx->para = newCtx->para; BSL_SAL_FREE(newCtx); BN_OptimizerDestroy(optimizer); return ret; ERR: CRYPT_PAILLIER_FreeCtx(newCtx); BN_OptimizerDestroy(optimizer); return ret; } #endif // HITLS_CRYPTO_PAILLIER
2301_79861745/bench_create
crypto/paillier/src/paillier_keygen.c
C
unknown
16,825
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_PAILLIER #include "crypt_types.h" #include "crypt_paillier.h" #include "crypt_utils.h" #include "bsl_err_internal.h" #include "paillier_local.h" #include "crypt_errno.h" #include "securec.h" #include "bsl_sal.h" #include "crypt_params_key.h" typedef struct { BSL_Param *n; /**< Paillier private key parameter marked as n */ BSL_Param *lambda; /**< Paillier private key parameter marked as lambda */ BSL_Param *mu; /**< Paillier private key parameter marked as mu */ BSL_Param *n2; /**< Paillier private key parameter marked as n2 */ } CRYPT_PaillierPrvParam; typedef struct { BSL_Param *n; /**< Paillier public key parameter marked as n */ BSL_Param *g; /**< Paillier public key parameter marked as g */ BSL_Param *n2; /**< Paillier public key parameter marked as n2 */ } CRYPT_PaillierPubParam; #define PARAMISNULL(a) (a == NULL || a->value == NULL) static int32_t CheckSquare(const BN_BigNum *n2, const BN_BigNum *n, uint32_t bits) { BN_BigNum *tmp = BN_Create(bits); BN_Optimizer *optimizer = BN_OptimizerCreate(); int32_t ret; if (optimizer == NULL || tmp == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto EXIT; } ret = BN_Sqr(tmp, n, optimizer); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto EXIT; } if (BN_Cmp(tmp, n2) != 0) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); ret = CRYPT_PAILLIER_ERR_INPUT_VALUE; } EXIT: BN_Destroy(tmp); BN_OptimizerDestroy(optimizer); return ret; } static int32_t SetPrvPara(const CRYPT_PAILLIER_PrvKey *prvKey, const CRYPT_PaillierPrv *prv) { int32_t ret = BN_Bin2Bn(prvKey->n, prv->n, prv->nLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } uint32_t bnBits = BN_Bits(prvKey->n); if (bnBits > PAILLIER_MAX_MODULUS_BITS || bnBits <= 0) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_KEY_BITS); return CRYPT_PAILLIER_ERR_KEY_BITS; } ret = BN_Bin2Bn(prvKey->lambda, prv->lambda, prv->lambdaLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_Bin2Bn(prvKey->mu, prv->mu, prv->muLen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = BN_Bin2Bn(prvKey->n2, prv->n2, prv->n2Len); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ret = CheckSquare(prvKey->n2, prvKey->n, prv->n2Len * 8); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if (BN_IsZero(prvKey->mu) || BN_IsOne(prvKey->mu)) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); return CRYPT_PAILLIER_ERR_INPUT_VALUE; } return ret; } static int32_t SetPrvBasicCheck(const CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPrv *prv) { if (ctx == NULL || prv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prv->n == NULL || prv->lambda == NULL || prv->mu == NULL || prv->n2 == NULL || prv->lambdaLen == 0 || prv->muLen == 0 || prv->nLen == 0 || prv->n2Len == 0) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); return CRYPT_PAILLIER_ERR_INPUT_VALUE; } return CRYPT_SUCCESS; } int32_t CRYPT_PAILLIER_SetPrvKey(CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPrv *prv) { int32_t ret = SetPrvBasicCheck(ctx, prv); if (ret != CRYPT_SUCCESS) { return ret; } CRYPT_PAILLIER_Ctx *newCtx = CRYPT_PAILLIER_NewCtx(); if (newCtx == NULL) { return CRYPT_MEM_ALLOC_FAIL; } newCtx->prvKey = Paillier_NewPrvKey(prv->lambdaLen * 8); // Bit length is obtained by multiplying byte length by 8. if (newCtx->prvKey == NULL) { ret = CRYPT_MEM_ALLOC_FAIL; BSL_ERR_PUSH_ERROR(ret); goto ERR; } ret = SetPrvPara(newCtx->prvKey, prv); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); goto ERR; } PAILLIER_FREE_PRV_KEY(ctx->prvKey); ctx->prvKey = newCtx->prvKey; BSL_SAL_ReferencesFree(&(newCtx->references)); BSL_SAL_FREE(newCtx); return ret; ERR: CRYPT_PAILLIER_FreeCtx(newCtx); return ret; } static int32_t SetPubBasicCheck(const CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPub *pub) { if (ctx == NULL || pub == NULL || pub->n == NULL || pub->g == NULL || pub->n2 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } return CRYPT_SUCCESS; } int32_t CRYPT_PAILLIER_SetPubKey(CRYPT_PAILLIER_Ctx *ctx, const CRYPT_PaillierPub *pub) { int32_t ret = SetPubBasicCheck(ctx, pub); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } CRYPT_PAILLIER_PubKey *newPub = NULL; /* Bit length is obtained by multiplying byte length by 8. */ newPub = Paillier_NewPubKey(pub->nLen * 8); if (newPub == NULL) { return CRYPT_MEM_ALLOC_FAIL; } GOTO_ERR_IF(BN_Bin2Bn(newPub->n, pub->n, pub->nLen), ret); uint32_t bnBits = BN_Bits(newPub->n); if (bnBits > PAILLIER_MAX_MODULUS_BITS || bnBits <= 0) { ret = CRYPT_PAILLIER_ERR_KEY_BITS; BSL_ERR_PUSH_ERROR(ret); goto ERR; } GOTO_ERR_IF(BN_Bin2Bn(newPub->g, pub->g, pub->gLen), ret); GOTO_ERR_IF(BN_Bin2Bn(newPub->n2, pub->n2, pub->n2Len), ret); GOTO_ERR_IF(CheckSquare(newPub->n2, newPub->n, pub->nLen * 8), ret); PAILLIER_FREE_PUB_KEY(ctx->pubKey); ctx->pubKey = newPub; return ret; ERR: PAILLIER_FREE_PUB_KEY(newPub); return ret; } int32_t CRYPT_PAILLIER_GetPrvKey(const CRYPT_PAILLIER_Ctx *ctx, CRYPT_PaillierPrv *prv) { if (ctx == NULL || ctx->prvKey == NULL || prv == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (prv->lambda == NULL || prv->mu == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PAILLIER_ERR_INPUT_VALUE); return CRYPT_PAILLIER_ERR_INPUT_VALUE; } int32_t ret = 0; GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->lambda, prv->lambda, &prv->lambdaLen), ret); GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->mu, prv->mu, &prv->muLen), ret); if (prv->n != NULL) { GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->n, prv->n, &prv->nLen), ret); } if (prv->n2 != NULL) { GOTO_ERR_IF(BN_Bn2Bin(ctx->prvKey->n2, prv->n2, &prv->n2Len), ret); } return CRYPT_SUCCESS; ERR: BSL_SAL_CleanseData(prv->lambda, prv->lambdaLen); BSL_SAL_CleanseData(prv->mu, prv->muLen); BSL_SAL_CleanseData(prv->n, prv->nLen); BSL_SAL_CleanseData(prv->n2, prv->n2Len); prv->lambdaLen = 0; prv->muLen = 0; prv->nLen = 0; prv->n2Len = 0; return ret; } int32_t CRYPT_PAILLIER_GetPubKey(const CRYPT_PAILLIER_Ctx *ctx, CRYPT_PaillierPub *pub) { if (ctx == NULL || ctx->pubKey == NULL || pub == NULL || pub->n == NULL || pub->g == NULL || pub->n2 == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = 0; GOTO_ERR_IF(BN_Bn2Bin(ctx->pubKey->g, pub->g, &pub->gLen), ret); GOTO_ERR_IF(BN_Bn2Bin(ctx->pubKey->n, pub->n, &pub->nLen), ret); GOTO_ERR_IF(BN_Bn2Bin(ctx->pubKey->n2, pub->n2, &pub->n2Len), ret); return CRYPT_SUCCESS; ERR: BSL_SAL_CleanseData(pub->g, pub->gLen); BSL_SAL_CleanseData(pub->n, pub->nLen); BSL_SAL_CleanseData(pub->n2, pub->n2Len); pub->gLen = 0; pub->nLen = 0; pub->n2Len = 0; return ret; } #ifdef HITLS_BSL_PARAMS int32_t CRYPT_PAILLIER_SetPrvKeyEx(CRYPT_PAILLIER_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_PaillierPrv prv = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_N, &prv.n, &prv.nLen); (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_LAMBDA, &prv.lambda, &prv.lambdaLen); (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_MU, &prv.mu, &prv.muLen); (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_N2, &prv.n2, &prv.n2Len); return CRYPT_PAILLIER_SetPrvKey(ctx, &prv); } int32_t CRYPT_PAILLIER_SetPubKeyEx(CRYPT_PAILLIER_Ctx *ctx, const BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_PaillierPub pub = {0}; (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_N, &pub.n, &pub.nLen); (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_G, &pub.g, &pub.gLen); (void)GetConstParamValue(para, CRYPT_PARAM_PAILLIER_N2, &pub.n2, &pub.n2Len); return CRYPT_PAILLIER_SetPubKey(ctx, &pub); } int32_t CRYPT_PAILLIER_GetPrvKeyEx(const CRYPT_PAILLIER_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_PaillierPrv prv = {0}; BSL_Param *paramN = GetParamValue(para, CRYPT_PARAM_PAILLIER_N, &prv.n, &(prv.nLen)); BSL_Param *paramLambda = GetParamValue(para, CRYPT_PARAM_PAILLIER_LAMBDA, &prv.lambda, &(prv.lambdaLen)); BSL_Param *paramMu = GetParamValue(para, CRYPT_PARAM_PAILLIER_MU, &prv.mu, &(prv.muLen)); BSL_Param *paramN2 = GetParamValue(para, CRYPT_PARAM_PAILLIER_N2, &prv.n2, &(prv.n2Len)); int32_t ret = CRYPT_PAILLIER_GetPrvKey(ctx, &prv); if (ret != CRYPT_SUCCESS) { return ret; } paramLambda->useLen = prv.lambdaLen; paramMu->useLen = prv.muLen; if (paramN != NULL) { paramN->useLen = prv.nLen; } if (paramN2 != NULL) { paramN2->useLen = prv.n2Len; } return CRYPT_SUCCESS; } int32_t CRYPT_PAILLIER_GetPubKeyEx(const CRYPT_PAILLIER_Ctx *ctx, BSL_Param *para) { if (para == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } CRYPT_PaillierPub pub = {0}; BSL_Param *paramN = GetParamValue(para, CRYPT_PARAM_PAILLIER_N, &pub.n, &(pub.nLen)); BSL_Param *paramG = GetParamValue(para, CRYPT_PARAM_PAILLIER_G, &pub.g, &(pub.gLen)); BSL_Param *paramN2 = GetParamValue(para, CRYPT_PARAM_PAILLIER_N2, &pub.n2, &(pub.n2Len)); int32_t ret = CRYPT_PAILLIER_GetPubKey(ctx, &pub); if (ret != CRYPT_SUCCESS) { return ret; } paramN->useLen = pub.nLen; paramG->useLen = pub.gLen; paramN2->useLen = pub.n2Len; return CRYPT_SUCCESS; } #endif int32_t CRYPT_PAILLIER_GetSecBits(const CRYPT_PAILLIER_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return 0; } int32_t bits = (int32_t)CRYPT_PAILLIER_GetBits(ctx); return BN_SecBits(bits, -1); } #endif /* HITLS_CRYPTO_PAILLIER */
2301_79861745/bench_create
crypto/paillier/src/paillier_keyop.c
C
unknown
11,292
/* * 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 PAILLIER_LOCAL_H #define PAILLIER_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_PAILLIER #include "crypt_paillier.h" #include "crypt_bn.h" #include "crypt_local_types.h" #include "crypt_types.h" #include "sal_atomic.h" #ifdef __cplusplus extern "C" { #endif /* __cpluscplus */ typedef struct { BN_BigNum *n; // modulo Value - converted.Not in char BN_BigNum *g; // modulo Value -converted.Not in char BN_BigNum *n2; // square of n } CRYPT_PAILLIER_PubKey; typedef struct { BN_BigNum *n; // pub key n needed for decryption BN_BigNum *lambda; // modulo Value - converted.Not in char BN_BigNum *mu; // modulo Value -converted.Not in char BN_BigNum *n2; // pub key n2 needed for decryption } CRYPT_PAILLIER_PrvKey; struct PAILLIER_Para { BN_BigNum *p; // prime factor p BN_BigNum *q; // prime factor q uint32_t bits; // length in bits of modulus }; struct PAILLIER_Ctx { CRYPT_PAILLIER_PubKey *pubKey; CRYPT_PAILLIER_PrvKey *prvKey; CRYPT_PAILLIER_Para *para; BSL_SAL_RefCount references; void *libCtx; }; CRYPT_PAILLIER_PrvKey *Paillier_NewPrvKey(uint32_t bits); CRYPT_PAILLIER_PubKey *Paillier_NewPubKey(uint32_t bits); void PAILLIER_FreePrvKey(CRYPT_PAILLIER_PrvKey *prvKey); void PAILLIER_FreePubKey(CRYPT_PAILLIER_PubKey *pubKey); CRYPT_PAILLIER_Para *CRYPT_Paillier_DupPara(const CRYPT_PAILLIER_Para *para); #define PAILLIER_FREE_PRV_KEY(prvKey_) \ do { \ PAILLIER_FreePrvKey((prvKey_)); \ (prvKey_) = NULL; \ } while (0) #define PAILLIER_FREE_PUB_KEY(pubKey_) \ do { \ PAILLIER_FreePubKey((pubKey_)); \ (pubKey_) = NULL; \ } while (0) #define PAILLIER_FREE_PARA(para_) \ do { \ CRYPT_PAILLIER_FreePara((para_)); \ (para_) = NULL; \ } while (0) #ifdef __cplusplus } #endif #endif // HITLS_CRYPTO_PAILLIER #endif // PAILLIER_LOCAL_H
2301_79861745/bench_create
crypto/paillier/src/paillier_local.h
C
unknown
2,729
/* * 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_PBKDF2_H #define CRYPT_PBKDF2_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_PBKDF2 #include <stdint.h> #include "crypt_local_types.h" #include "bsl_params.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus typedef struct CryptPbkdf2Ctx CRYPT_PBKDF2_Ctx; /** * @ingroup PBKDF2 * @brief Generate PBKDF2 context. * * @retval Success: PBKDF2 ctx. * Fails: NULL. */ CRYPT_PBKDF2_Ctx* CRYPT_PBKDF2_NewCtx(void); /** * @ingroup PBKDF2 * @brief Generate PBKDF2 context. * * @param libCtx [in] Library context. * * @retval Success: PBKDF2 ctx. * Fails: NULL. */ CRYPT_PBKDF2_Ctx *CRYPT_PBKDF2_NewCtxEx(void *libCtx); /** * @ingroup PBKDF2 * @brief Set parameters for the PBKDF2 context. * * @param ctx [in, out] Pointer to the PBKDF2 context. * @param param [in] Either a MAC algorithm ID, a salt, a password, or an iteration count. * * @retval Success: CRYPT_SUCCESS * For other error codes, see crypt_errno.h. */ int32_t CRYPT_PBKDF2_SetParam(CRYPT_PBKDF2_Ctx *ctx, const BSL_Param *param); /** * @ingroup PBKDF2 * @brief Obtain the derived key based on the passed PBKDF2 context.. * * @param ctx [in, out] Pointer to the PBKDF2 context. * @param out [out] Derived key buffer. * @param out [out] Derived key buffer size. * * @retval Success: CRYPT_SUCCESS * For other error codes, see crypt_errno.h. */ int32_t CRYPT_PBKDF2_Derive(CRYPT_PBKDF2_Ctx *ctx, uint8_t *out, uint32_t len); /** * @ingroup PBKDF2 * @brief PBKDF2 deinitialization API * * @param ctx [in, out] Pointer to the PBKDF2 context. * * @retval #CRYPT_SUCCESS Deinitialization succeeded. * @retval #CRYPT_NULL_INPUT Pointer ctx is NULL */ int32_t CRYPT_PBKDF2_Deinit(CRYPT_PBKDF2_Ctx *ctx); /** * @ingroup PBKDF2 * @brief free PBKDF2 context. * * @param ctx [IN] PBKDF2 handle */ void CRYPT_PBKDF2_FreeCtx(CRYPT_PBKDF2_Ctx *ctx); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_PBKDF2 #endif // CRYPT_PBKDF2_H
2301_79861745/bench_create
crypto/pbkdf2/include/crypt_pbkdf2.h
C
unknown
2,561
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ #include "hitls_build.h" #ifdef HITLS_CRYPTO_PBKDF2 #include <stdint.h> #include "securec.h" #include "bsl_err_internal.h" #include "bsl_sal.h" #include "crypt_local_types.h" #include "crypt_errno.h" #include "crypt_utils.h" #include "crypt_algid.h" #include "eal_mac_local.h" #include "crypt_ealinit.h" #include "pbkdf2_local.h" #include "bsl_params.h" #include "crypt_params_key.h" #include "crypt_pbkdf2.h" #define PBKDF2_MAX_BLOCKSIZE 64 #define PBKDF2_MAX_KEYLEN 0xFFFFFFFF static const uint32_t PBKDF_ID_LIST[] = { CRYPT_MAC_HMAC_MD5, CRYPT_MAC_HMAC_SHA1, CRYPT_MAC_HMAC_SHA224, CRYPT_MAC_HMAC_SHA256, CRYPT_MAC_HMAC_SHA384, CRYPT_MAC_HMAC_SHA512, CRYPT_MAC_HMAC_SM3, CRYPT_MAC_HMAC_SHA3_224, CRYPT_MAC_HMAC_SHA3_256, CRYPT_MAC_HMAC_SHA3_384, CRYPT_MAC_HMAC_SHA3_512, }; struct CryptPbkdf2Ctx { CRYPT_MAC_AlgId macId; EAL_MacMethod macMeth; uint32_t mdSize; void *macCtx; uint8_t *password; uint32_t passLen; uint8_t *salt; uint32_t saltLen; uint32_t iterCnt; #ifdef HITLS_CRYPTO_PROVIDER void *libCtx; #endif bool hasGetMdSize; }; bool CRYPT_PBKDF2_IsValidAlgId(CRYPT_MAC_AlgId id) { return ParamIdIsValid(id, PBKDF_ID_LIST, sizeof(PBKDF_ID_LIST) / sizeof(PBKDF_ID_LIST[0])); } int32_t CRYPT_PBKDF2_U1(const CRYPT_PBKDF2_Ctx *pCtx, uint32_t blockCount, uint8_t *u, uint32_t *blockSize) { int32_t ret; const EAL_MacMethod *macMeth = &pCtx->macMeth; void *macCtx = pCtx->macCtx; (void)macMeth->reinit(macCtx); if ((ret = macMeth->update(macCtx, pCtx->salt, pCtx->saltLen)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } /* processing the big endian */ uint32_t blockCnt = CRYPT_HTONL(blockCount); if ((ret = macMeth->update(macCtx, (uint8_t *)&blockCnt, sizeof(blockCnt))) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if ((ret = macMeth->final(macCtx, u, blockSize)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return CRYPT_SUCCESS; } int32_t CRYPT_PBKDF2_Un(const CRYPT_PBKDF2_Ctx *pCtx, uint8_t *u, uint32_t *blockSize, uint8_t *t, uint32_t tLen) { int32_t ret; const EAL_MacMethod *macMeth = &pCtx->macMeth; void *macCtx = pCtx->macCtx; macMeth->reinit(macCtx); if ((ret = macMeth->update(macCtx, u, *blockSize)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } if ((ret = macMeth->final(macCtx, u, blockSize)) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } DATA_XOR(t, u, t, tLen); return CRYPT_SUCCESS; } int32_t CRYPT_PBKDF2_CalcT(const CRYPT_PBKDF2_Ctx *pCtx, uint32_t blockCount, uint8_t *t, uint32_t *tlen) { uint8_t u[PBKDF2_MAX_BLOCKSIZE] = {0}; uint8_t tmpT[PBKDF2_MAX_BLOCKSIZE] = {0}; uint32_t blockSize = PBKDF2_MAX_BLOCKSIZE; int32_t ret; uint32_t iterCnt = pCtx->iterCnt; /* U1 = PRF(Password, Salt + INT_32_BE(i)) tmpT = U1 */ ret = CRYPT_PBKDF2_U1(pCtx, blockCount, u, &blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } (void)memcpy_s(tmpT, PBKDF2_MAX_BLOCKSIZE, u, blockSize); for (uint32_t un = 1; un < iterCnt; un++) { /* t = t ^ Un */ ret = CRYPT_PBKDF2_Un(pCtx, u, &blockSize, tmpT, blockSize); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } } uint32_t len = (*tlen > blockSize) ? blockSize : (*tlen); (void)memcpy_s(t, *tlen, tmpT, len); *tlen = len; BSL_SAL_CleanseData(u, PBKDF2_MAX_BLOCKSIZE); BSL_SAL_CleanseData(tmpT, PBKDF2_MAX_BLOCKSIZE); return CRYPT_SUCCESS; } int32_t CRYPT_PBKDF2_GenDk(const CRYPT_PBKDF2_Ctx *pCtx, uint8_t *dk, uint32_t dkLen) { uint32_t curLen; uint8_t *t = dk; uint32_t tlen; uint32_t i; int32_t ret; ret = pCtx->macMeth.init(pCtx->macCtx, pCtx->password, pCtx->passLen, NULL); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } /* DK = T1 + T2 + ⋯ + Tdklen/hlen */ for (i = 1, curLen = dkLen; curLen > 0; i++) { tlen = curLen; ret = CRYPT_PBKDF2_CalcT(pCtx, i, t, &tlen); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } curLen -= tlen; t += tlen; } return CRYPT_SUCCESS; } int32_t CRYPT_PBKDF2_HMAC(void *libCtx, const EAL_MacMethod *macMeth, CRYPT_MAC_AlgId macId, const EAL_MdMethod *mdMeth, const uint8_t *key, uint32_t keyLen, const uint8_t *salt, uint32_t saltLen, uint32_t iterCnt, uint8_t *out, uint32_t len) { int32_t ret; CRYPT_PBKDF2_Ctx pCtx; if (macMeth == NULL || mdMeth == NULL || out == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (key == NULL && keyLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } // add keyLen limit based on rfc2898 if (mdMeth->mdSize == 0 || (keyLen / mdMeth->mdSize) >= PBKDF2_MAX_KEYLEN) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_PARAM_ERROR); return CRYPT_PBKDF2_PARAM_ERROR; } if (salt == NULL && saltLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((len == 0) || (iterCnt == 0)) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_PARAM_ERROR); return CRYPT_PBKDF2_PARAM_ERROR; } void *macCtx = macMeth->newCtx(libCtx, macId); if (macCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } pCtx.macMeth = *macMeth; pCtx.macCtx = macCtx; pCtx.password = (uint8_t *)(uintptr_t)key; pCtx.passLen = keyLen; pCtx.salt = (uint8_t *)(uintptr_t)salt; pCtx.saltLen = saltLen; pCtx.iterCnt = iterCnt; ret = CRYPT_PBKDF2_GenDk(&pCtx, out, len); macMeth->deinit(macCtx); macMeth->freeCtx(macCtx); macCtx = NULL; return ret; } CRYPT_PBKDF2_Ctx *CRYPT_PBKDF2_NewCtx(void) { CRYPT_PBKDF2_Ctx *ctx = BSL_SAL_Calloc(1, sizeof(CRYPT_PBKDF2_Ctx)); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } return ctx; } CRYPT_PBKDF2_Ctx *CRYPT_PBKDF2_NewCtxEx(void *libCtx) { (void)libCtx; CRYPT_PBKDF2_Ctx *ctx = CRYPT_PBKDF2_NewCtx(); if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return NULL; } #ifdef HITLS_CRYPTO_PROVIDER ctx->libCtx = libCtx; #endif return ctx; } int32_t CRYPT_PBKDF2_SetMacMethod(CRYPT_PBKDF2_Ctx *ctx, const CRYPT_MAC_AlgId id) { #ifdef HITLS_CRYPTO_ASM_CHECK if (CRYPT_ASMCAP_Mac(id) != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ALG_ASM_NOT_SUPPORT); return CRYPT_EAL_ALG_ASM_NOT_SUPPORT; } #endif if (!CRYPT_PBKDF2_IsValidAlgId(id)) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_PARAM_ERROR); return CRYPT_PBKDF2_PARAM_ERROR; } // free the old macCtx if (ctx->macCtx != NULL) { if (ctx->macMeth.freeCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_ERR_MAC_METH); return CRYPT_PBKDF2_ERR_MAC_METH; } ctx->macMeth.freeCtx(ctx->macCtx); ctx->macCtx = NULL; (void)memset_s(&ctx->macMeth, sizeof(EAL_MacMethod), 0, sizeof(EAL_MacMethod)); } EAL_MacMethod *macMeth = EAL_MacFindMethod(id, &ctx->macMeth); if (macMeth == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_EAL_ERR_METH_NULL_MEMBER); return CRYPT_EAL_ERR_METH_NULL_MEMBER; } if (macMeth->newCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_ERR_MAC_METH); return CRYPT_PBKDF2_ERR_MAC_METH; } #ifdef HITLS_CRYPTO_PROVIDER ctx->macCtx = macMeth->newCtx(ctx->libCtx, id); #else ctx->macCtx = macMeth->newCtx(NULL, id); #endif if (ctx->macCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->macId = id; return CRYPT_SUCCESS; } int32_t CRYPT_PBKDF2_SetPassWord(CRYPT_PBKDF2_Ctx *ctx, const uint8_t *password, uint32_t passLen) { if (password == NULL && passLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_ClearFree(ctx->password, ctx->passLen); ctx->password = BSL_SAL_Dump(password, passLen); if (ctx->password == NULL && passLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->passLen = passLen; return CRYPT_SUCCESS; } int32_t CRYPT_PBKDF2_SetSalt(CRYPT_PBKDF2_Ctx *ctx, const uint8_t *salt, uint32_t saltLen) { if (salt == NULL && saltLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } BSL_SAL_FREE(ctx->salt); ctx->salt = BSL_SAL_Dump(salt, saltLen); if (ctx->salt == NULL && saltLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_MEM_ALLOC_FAIL); return CRYPT_MEM_ALLOC_FAIL; } ctx->saltLen = saltLen; return CRYPT_SUCCESS; } int32_t CRYPT_PBKDF2_SetCnt(CRYPT_PBKDF2_Ctx *ctx, const uint32_t iterCnt) { if (iterCnt == 0) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_PARAM_ERROR); return CRYPT_PBKDF2_PARAM_ERROR; } ctx->iterCnt = iterCnt; return CRYPT_SUCCESS; } static int32_t Pbkdf2GetMdSize(CRYPT_PBKDF2_Ctx *ctx, const char *mdAttr) { if (ctx->hasGetMdSize) { return CRYPT_SUCCESS; } void *libCtx = NULL; #ifdef HITLS_CRYPTO_PROVIDER libCtx = ctx->libCtx; #endif EAL_MdMethod mdMeth = {0}; EAL_MacDepMethod depMeth = {.method = {.md = &mdMeth}}; int32_t ret = EAL_MacFindDepMethod(ctx->macId, libCtx, mdAttr, &depMeth, NULL, true); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } ctx->mdSize = mdMeth.mdSize; ctx->hasGetMdSize = true; return CRYPT_SUCCESS; } #ifdef HITLS_CRYPTO_PROVIDER static int32_t CRYPT_PBKDF2_SetMdAttr(CRYPT_PBKDF2_Ctx *ctx, const char *mdAttr, uint32_t valLen) { if (mdAttr == NULL || valLen == 0) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_PARAM_ERROR); return CRYPT_PBKDF2_PARAM_ERROR; } if (ctx->macCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_ERR_MAC_ID_NOT_SET); return CRYPT_PBKDF2_ERR_MAC_ID_NOT_SET; } // Set mdAttr for macCtx if (ctx->macMeth.setParam == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_ERR_MAC_METH); return CRYPT_PBKDF2_ERR_MAC_METH; } BSL_Param param[] = { {.key = CRYPT_PARAM_MD_ATTR, .valueType = BSL_PARAM_TYPE_UTF8_STR, .value = (void *)(uintptr_t)mdAttr, .valueLen = valLen, .useLen = 0}, BSL_PARAM_END }; int32_t ret = ctx->macMeth.setParam(ctx->macCtx, param); if (ret != CRYPT_SUCCESS) { BSL_ERR_PUSH_ERROR(ret); return ret; } return Pbkdf2GetMdSize(ctx, mdAttr); } #endif int32_t CRYPT_PBKDF2_SetParam(CRYPT_PBKDF2_Ctx *ctx, const BSL_Param *param) { uint32_t val = 0; uint32_t len = 0; const BSL_Param *temp = NULL; int32_t ret = CRYPT_PBKDF2_PARAM_ERROR; if (ctx == NULL || param == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_MAC_ID)) != NULL) { len = sizeof(val); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_MAC_ID, BSL_PARAM_TYPE_UINT32, &val, &len), ret); GOTO_ERR_IF(CRYPT_PBKDF2_SetMacMethod(ctx, val), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_PASSWORD)) != NULL) { GOTO_ERR_IF(CRYPT_PBKDF2_SetPassWord(ctx, temp->value, temp->valueLen), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_SALT)) != NULL) { GOTO_ERR_IF(CRYPT_PBKDF2_SetSalt(ctx, temp->value, temp->valueLen), ret); } if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_KDF_ITER)) != NULL) { len = sizeof(val); GOTO_ERR_IF(BSL_PARAM_GetValue(temp, CRYPT_PARAM_KDF_ITER, BSL_PARAM_TYPE_UINT32, &val, &len), ret); GOTO_ERR_IF(CRYPT_PBKDF2_SetCnt(ctx, val), ret); } #ifdef HITLS_CRYPTO_PROVIDER if ((temp = BSL_PARAM_FindConstParam(param, CRYPT_PARAM_MD_ATTR)) != NULL) { GOTO_ERR_IF(CRYPT_PBKDF2_SetMdAttr(ctx, temp->value, temp->valueLen), ret); } #endif ERR: return ret; } int32_t CRYPT_PBKDF2_Derive(CRYPT_PBKDF2_Ctx *ctx, uint8_t *out, uint32_t len) { if (ctx == NULL || out == NULL || ctx->macCtx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->macMeth.deinit == NULL || ctx->macMeth.freeCtx == NULL || ctx->macMeth.init == NULL || ctx->macMeth.reinit == NULL || ctx->macMeth.update == NULL || ctx->macMeth.final == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_ERR_MAC_METH); return CRYPT_PBKDF2_ERR_MAC_METH; } if (ctx->password == NULL && ctx->passLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } int32_t ret = Pbkdf2GetMdSize(ctx, NULL); if (ret != CRYPT_SUCCESS) { return ret; } // add keyLen limit based on rfc2898 if (ctx->mdSize == 0 || (ctx->passLen / ctx->mdSize) >= PBKDF2_MAX_KEYLEN) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_PARAM_ERROR); return CRYPT_PBKDF2_PARAM_ERROR; } if (ctx->salt == NULL && ctx->saltLen > 0) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if ((len == 0) || (ctx->iterCnt == 0)) { BSL_ERR_PUSH_ERROR(CRYPT_PBKDF2_PARAM_ERROR); return CRYPT_PBKDF2_PARAM_ERROR; } return CRYPT_PBKDF2_GenDk(ctx, out, len); } int32_t CRYPT_PBKDF2_Deinit(CRYPT_PBKDF2_Ctx *ctx) { if (ctx == NULL) { BSL_ERR_PUSH_ERROR(CRYPT_NULL_INPUT); return CRYPT_NULL_INPUT; } if (ctx->macMeth.freeCtx != NULL) { ctx->macMeth.freeCtx(ctx->macCtx); ctx->macCtx = NULL; } BSL_SAL_ClearFree((void *)ctx->password, ctx->passLen); BSL_SAL_FREE(ctx->salt); (void)memset_s(ctx, sizeof(CRYPT_PBKDF2_Ctx), 0, sizeof(CRYPT_PBKDF2_Ctx)); return CRYPT_SUCCESS; } void CRYPT_PBKDF2_FreeCtx(CRYPT_PBKDF2_Ctx *ctx) { if (ctx == NULL) { return; } if (ctx->macMeth.freeCtx != NULL) { ctx->macMeth.freeCtx(ctx->macCtx); } BSL_SAL_ClearFree((void *)ctx->password, ctx->passLen); BSL_SAL_FREE(ctx->salt); BSL_SAL_Free(ctx); } #endif // HITLS_CRYPTO_PBKDF2
2301_79861745/bench_create
crypto/pbkdf2/src/pbkdf2.c
C
unknown
15,120
/* * 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 PBKDF2_LOCAL_H #define PBKDF2_LOCAL_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_PBKDF2 #include <stdint.h> #include "crypt_local_types.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus /** * @brief PBKDF Password-based key derivation function * * @param libCtx [IN] Library context * @param macMeth [IN] Pointer to the HMAC algorithm method * @param mdMeth [IN] MD algorithm method pointer * @param key [IN] Password, a string entered by the user. * @param keyLen [IN] Password length, which can be any length, including 0. * @param salt [IN] Salt value, a string entered by the user. * @param saltLen [IN] Salt value length, which can be any length, including 0. * @param iterCnt [IN] Iteration times. The value can be any positive integer that is not 0. * The value can be 1000 in special performance cases. The default value is 10000, * 10000000 is recommended in cases where performance is insensitive or security requirements are high. * @param out [OUT] Derived key. * @param len [IN] Length of the derived key. The value range is [1, 0xFFFFFFFF]. * * @return Success: CRYPT_SUCCESS * For other error codes, see crypt_errno.h */ int32_t CRYPT_PBKDF2_HMAC(void *libCtx, const EAL_MacMethod *macMeth, CRYPT_MAC_AlgId macId, const EAL_MdMethod *mdMeth, const uint8_t *key, uint32_t keyLen, const uint8_t *salt, uint32_t saltLen, uint32_t iterCnt, uint8_t *out, uint32_t len); #ifdef __cplusplus } #endif // __cplusplus #endif // HITLS_CRYPTO_PBKDF2 #endif // PBKDF2_LOCAL_H
2301_79861745/bench_create
crypto/pbkdf2/src/pbkdf2_local.h
C
unknown
2,115
/* * This file is part of the openHiTLS project. * * openHiTLS is licensed under the Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. * You may obtain a copy of Mulan PSL v2 at: * * http://license.coscl.org.cn/MulanPSL2 * * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PSL v2 for more details. */ /** * @defgroup crypt_eal_provider * @ingroup crypt * @brief default provider impl */ #ifndef CRYPT_EAL_DEFAULT_PROVIDERIMPL_H #define CRYPT_EAL_DEFAULT_PROVIDERIMPL_H #ifdef HITLS_CRYPTO_PROVIDER #include "crypt_eal_implprovider.h" #ifdef __cplusplus extern "C" { #endif // __cplusplus #ifdef HITLS_CRYPTO_MD #ifdef HITLS_CRYPTO_MD5 extern const CRYPT_EAL_Func g_defEalMdMd5[]; #endif // HITLS_CRYPTO_MD5 #ifdef HITLS_CRYPTO_SHA1 extern const CRYPT_EAL_Func g_defEalMdSha1[]; #endif // HITLS_CRYPTO_SHA1 #ifdef HITLS_CRYPTO_SHA224 extern const CRYPT_EAL_Func g_defEalMdSha224[]; #endif // HITLS_CRYPTO_SHA224 #ifdef HITLS_CRYPTO_SHA256 extern const CRYPT_EAL_Func g_defEalMdSha256[]; #endif // HITLS_CRYPTO_SHA256 #ifdef HITLS_CRYPTO_SHA384 extern const CRYPT_EAL_Func g_defEalMdSha384[]; #endif // HITLS_CRYPTO_SHA384 #ifdef HITLS_CRYPTO_SHA512 extern const CRYPT_EAL_Func g_defEalMdSha512[]; #endif // HITLS_CRYPTO_SHA512 #ifdef HITLS_CRYPTO_SHA3 extern const CRYPT_EAL_Func g_defEalMdSha3224[]; extern const CRYPT_EAL_Func g_defEalMdSha3256[]; extern const CRYPT_EAL_Func g_defEalMdSha3384[]; extern const CRYPT_EAL_Func g_defEalMdSha3512[]; extern const CRYPT_EAL_Func g_defEalMdShake512[]; extern const CRYPT_EAL_Func g_defEalMdShake128[]; extern const CRYPT_EAL_Func g_defEalMdShake256[]; #endif // HITLS_CRYPTO_SHA3 #ifdef HITLS_CRYPTO_SM3 extern const CRYPT_EAL_Func g_defEalMdSm3[]; #endif // HITLS_CRYPTO_SM3 #endif // HITLS_CRYPTO_MD #ifdef HITLS_CRYPTO_MAC #ifdef HITLS_CRYPTO_HMAC extern const CRYPT_EAL_Func g_defEalMacHmac[]; #endif #ifdef HITLS_CRYPTO_CMAC extern const CRYPT_EAL_Func g_defEalMacCmac[]; #endif #ifdef HITLS_CRYPTO_CBC_MAC extern const CRYPT_EAL_Func g_defEalMacCbcMac[]; #endif #ifdef HITLS_CRYPTO_GMAC extern const CRYPT_EAL_Func g_defEalMacGmac[]; #endif #ifdef HITLS_CRYPTO_SIPHASH extern const CRYPT_EAL_Func g_defEalMacSiphash[]; #endif #endif // HITLS_CRYPTO_MAC #ifdef HITLS_CRYPTO_KDF #ifdef HITLS_CRYPTO_SCRYPT extern const CRYPT_EAL_Func g_defEalKdfScrypt[]; #endif #ifdef HITLS_CRYPTO_PBKDF2 extern const CRYPT_EAL_Func g_defEalKdfPBKdf2[]; #endif #ifdef HITLS_CRYPTO_KDFTLS12 extern const CRYPT_EAL_Func g_defEalKdfKdfTLS12[]; #endif #ifdef HITLS_CRYPTO_HKDF extern const CRYPT_EAL_Func g_defEalKdfHkdf[]; #endif #endif // HITLS_CRYPTO_KDF #ifdef HITLS_CRYPTO_CIPHER #ifdef HITLS_CRYPTO_CBC extern const CRYPT_EAL_Func g_defEalCbc[]; #endif #ifdef HITLS_CRYPTO_CCM extern const CRYPT_EAL_Func g_defEalCcm[]; #endif #ifdef HITLS_CRYPTO_CFB extern const CRYPT_EAL_Func g_defEalCfb[]; #endif #if defined(HITLS_CRYPTO_CHACHA20) && defined(HITLS_CRYPTO_CHACHA20POLY1305) extern const CRYPT_EAL_Func g_defEalChaCha[]; #endif #ifdef HITLS_CRYPTO_CTR extern const CRYPT_EAL_Func g_defEalCtr[]; #endif #ifdef HITLS_CRYPTO_ECB extern const CRYPT_EAL_Func g_defEalEcb[]; #endif #ifdef HITLS_CRYPTO_GCM extern const CRYPT_EAL_Func g_defEalGcm[]; #endif #ifdef HITLS_CRYPTO_OFB extern const CRYPT_EAL_Func g_defEalOfb[]; #endif #ifdef HITLS_CRYPTO_XTS extern const CRYPT_EAL_Func g_defEalXts[]; #endif #endif // HITLS_CRYPTO_CIPHER #ifdef HITLS_CRYPTO_DRBG extern const CRYPT_EAL_Func g_defEalRand[]; #endif // HITLS_CRYPTO_DRBG #ifdef HITLS_CRYPTO_PKEY #ifdef HITLS_CRYPTO_DSA extern const CRYPT_EAL_Func g_defEalKeyMgmtDsa[]; #endif #ifdef HITLS_CRYPTO_ED25519 extern const CRYPT_EAL_Func g_defEalKeyMgmtEd25519[]; #endif #ifdef HITLS_CRYPTO_X25519 extern const CRYPT_EAL_Func g_defEalKeyMgmtX25519[]; #endif #ifdef HITLS_CRYPTO_RSA extern const CRYPT_EAL_Func g_defEalKeyMgmtRsa[]; #endif #ifdef HITLS_CRYPTO_DH extern const CRYPT_EAL_Func g_defEalKeyMgmtDh[]; #endif #ifdef HITLS_CRYPTO_ECDSA extern const CRYPT_EAL_Func g_defEalKeyMgmtEcdsa[]; #endif #ifdef HITLS_CRYPTO_ECDH extern const CRYPT_EAL_Func g_defEalKeyMgmtEcdh[]; #endif #ifdef HITLS_CRYPTO_SM2 extern const CRYPT_EAL_Func g_defEalKeyMgmtSm2[]; #endif #ifdef HITLS_CRYPTO_PAILLIER extern const CRYPT_EAL_Func g_defEalKeyMgmtPaillier[]; #endif #ifdef HITLS_CRYPTO_ELGAMAL extern const CRYPT_EAL_Func g_defEalKeyMgmtElGamal[]; #endif #ifdef HITLS_CRYPTO_MLDSA extern const CRYPT_EAL_Func g_defEalKeyMgmtMlDsa[]; #endif #ifdef HITLS_CRYPTO_SLH_DSA extern const CRYPT_EAL_Func g_defEalKeyMgmtSlhDsa[]; #endif #ifdef HITLS_CRYPTO_MLKEM extern const CRYPT_EAL_Func g_defEalKeyMgmtMlKem[]; #endif #ifdef HITLS_CRYPTO_HYBRIDKEM extern const CRYPT_EAL_Func g_defEalKeyMgmtHybridKem[]; #endif #ifdef HITLS_CRYPTO_X25519 extern const CRYPT_EAL_Func g_defEalExchX25519[]; #endif #ifdef HITLS_CRYPTO_DH extern const CRYPT_EAL_Func g_defEalExchDh[]; #endif #ifdef HITLS_CRYPTO_ECDH extern const CRYPT_EAL_Func g_defEalExchEcdh[]; #endif #ifdef HITLS_CRYPTO_SM2_EXCH extern const CRYPT_EAL_Func g_defEalExchSm2[]; #else #define g_defEalExchSm2 NULL #endif #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_DECRYPT) extern const CRYPT_EAL_Func g_defEalAsymCipherRsa[]; #else #define g_defEalAsymCipherRsa NULL #endif #ifdef HITLS_CRYPTO_SM2_CRYPT extern const CRYPT_EAL_Func g_defEalAsymCipherSm2[]; #else #define g_defEalAsymCipherSm2 NULL #endif #ifdef HITLS_CRYPTO_PAILLIER extern const CRYPT_EAL_Func g_defEalAsymCipherPaillier[]; #endif #ifdef HITLS_CRYPTO_ELGAMAL extern const CRYPT_EAL_Func g_defEalAsymCipherElGamal[]; #endif #ifdef HITLS_CRYPTO_DSA extern const CRYPT_EAL_Func g_defEalSignDsa[]; #endif #ifdef HITLS_CRYPTO_ED25519 extern const CRYPT_EAL_Func g_defEalSignEd25519[]; #endif #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) extern const CRYPT_EAL_Func g_defEalSignRsa[]; #else #define g_defEalSignRsa NULL #endif #ifdef HITLS_CRYPTO_ECDSA extern const CRYPT_EAL_Func g_defEalSignEcdsa[]; #endif #ifdef HITLS_CRYPTO_SM2_SIGN extern const CRYPT_EAL_Func g_defEalSignSm2[]; #else #define g_defEalSignSm2 NULL #endif #ifdef HITLS_CRYPTO_MLDSA extern const CRYPT_EAL_Func g_defEalSignMlDsa[]; #endif #ifdef HITLS_CRYPTO_SLH_DSA extern const CRYPT_EAL_Func g_defEalSignSlhDsa[]; #endif #ifdef HITLS_CRYPTO_MLKEM extern const CRYPT_EAL_Func g_defEalMlKem[]; #endif #ifdef HITLS_CRYPTO_HYBRIDKEM extern const CRYPT_EAL_Func g_defEalHybridKeyKem[]; #endif #endif // HITLS_CRYPTO_PKEY #ifdef HITLS_CRYPTO_CODECSKEY #ifdef HITLS_CRYPTO_KEY_EPKI extern const CRYPT_EAL_Func g_defEalPrvP8Enc2P8[]; #endif #ifdef HITLS_BSL_PEM extern const CRYPT_EAL_Func g_defEalPem2Der[]; #endif #ifdef HITLS_CRYPTO_RSA extern const CRYPT_EAL_Func g_defEalRsaPrvDer2Key[]; #endif #ifdef HITLS_CRYPTO_ECDSA extern const CRYPT_EAL_Func g_defEalEcdsaPrvDer2Key[]; #endif #ifdef HITLS_CRYPTO_SM2 extern const CRYPT_EAL_Func g_defEalSm2PrvDer2Key[]; #endif #ifdef HITLS_CRYPTO_RSA extern const CRYPT_EAL_Func g_defEalP8Der2RsaKey[]; #endif #ifdef HITLS_CRYPTO_ECDSA extern const CRYPT_EAL_Func g_defEalP8Der2EcdsaKey[]; #endif #ifdef HITLS_CRYPTO_SM2 extern const CRYPT_EAL_Func g_defEalP8Der2Sm2Key[]; #endif #ifdef HITLS_CRYPTO_ED25519 extern const CRYPT_EAL_Func g_defEalP8Der2Ed25519Key[]; #endif #ifdef HITLS_CRYPTO_RSA extern const CRYPT_EAL_Func g_defEalSubPubKeyDer2RsaKey[]; #endif #ifdef HITLS_CRYPTO_ECDSA extern const CRYPT_EAL_Func g_defEalSubPubKeyDer2EcdsaKey[]; #endif #ifdef HITLS_CRYPTO_SM2 extern const CRYPT_EAL_Func g_defEalSubPubKeyDer2Sm2Key[]; #endif #ifdef HITLS_CRYPTO_ED25519 extern const CRYPT_EAL_Func g_defEalSubPubKeyDer2Ed25519Key[]; #endif #ifdef HITLS_CRYPTO_RSA extern const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2RsaKey[]; #endif #ifdef HITLS_CRYPTO_ECDSA extern const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2EcdsaKey[]; #endif #ifdef HITLS_CRYPTO_SM2 extern const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2Sm2Key[]; #endif #ifdef HITLS_CRYPTO_ED25519 extern const CRYPT_EAL_Func g_defEalSubPubKeyWithoutSeqDer2Ed25519Key[]; #endif #ifdef HITLS_CRYPTO_RSA extern const CRYPT_EAL_Func g_defEalLowKeyObject2PkeyObject[]; #endif #ifdef HITLS_CRYPTO_RSA extern const CRYPT_EAL_Func g_defEalRsaPubDer2Key[]; #endif #endif // HITLS_CRYPTO_CODECSKEY #ifdef __cplusplus } #endif // __cplusplus #endif /* HITLS_CRYPTO_PROVIDER */ #endif // CRYPT_EAL_DEFAULT_PROVIDERIMPL_H
2301_79861745/bench_create
crypto/provider/include/crypt_default_provderimpl.h
C
unknown
8,594
/* * 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_PROVIDER_H #define CRYPT_PROVIDER_H #include "hitls_build.h" #ifdef HITLS_CRYPTO_PROVIDER #include "crypt_eal_provider.h" #include "crypt_eal_implprovider.h" #include "bsl_list.h" #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ #define CRYPT_EAL_DEFAULT_PROVIDER "default" // Maximum length of provider name #define DEFAULT_PROVIDER_NAME_LEN_MAX 255 typedef enum { CRYPT_PROVIDER_GET_USER_CTX = 1, CRYPT_PROVIDER_CTRL_MAX, } CRYPT_ProviderCtrlCmd; struct EAL_LibCtx { BslList *providers; // managing providers BSL_SAL_ThreadLockHandle lock; char *searchProviderPath; #ifdef HITLS_CRYPTO_DRBG void *drbg; #endif }; #if defined(HITLS_CRYPTO_ENTROPY) && \ (defined(HITLS_CRYPTO_ENTROPY_GETENTROPY) || defined(HITLS_CRYPTO_ENTROPY_DEVRANDOM) || \ defined(HITLS_CRYPTO_ENTROPY_SYS) || defined(HITLS_CRYPTO_ENTROPY_HARDWARE)) #define HITLS_CRYPTO_ENTROPY_DEFAULT #endif int32_t CRYPT_EAL_InitPreDefinedProviders(void); void CRYPT_EAL_FreePreDefinedProviders(void); int32_t CRYPT_EAL_DefaultProvInit(CRYPT_EAL_ProvMgrCtx *mgrCtx, BSL_Param *param, CRYPT_EAL_Func *capFuncs, CRYPT_EAL_Func **outFuncs, void **provCtx); int32_t CRYPT_EAL_AddNewProvMgrCtx(CRYPT_EAL_LibCtx *libCtx, const char *providerName, const char *providerPath, CRYPT_EAL_ImplProviderInit init, BSL_Param *param, CRYPT_EAL_ProvMgrCtx **ctx); int32_t CRYPT_EAL_ProviderGetFuncsAndMgrCtx(CRYPT_EAL_LibCtx *libCtx, int32_t operaId, int32_t algId, const char *attribute, const CRYPT_EAL_Func **funcs, CRYPT_EAL_ProvMgrCtx **mgrCtx); CRYPT_EAL_LibCtx* CRYPT_EAL_GetGlobalLibCtx(void); int32_t CRYPT_EAL_ProviderQuery(CRYPT_EAL_ProvMgrCtx *ctx, int32_t operaId, CRYPT_EAL_AlgInfo **algInfos); #ifdef __cplusplus } #endif /* __cpluscplus */ #endif /* HITLS_CRYPTO_PROVIDER */ #endif /* CRYPT_PROVIDER_H */
2301_79861745/bench_create
crypto/provider/include/crypt_provider.h
C
unknown
2,442