Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/sm3/sm3_local.h

raw
1 /*
2 * Copyright 2017-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2017 Ribose Inc. All Rights Reserved.
4 * Ported from Ribose contributions from Botan.
5 *
6 * Licensed under the Apache License 2.0 (the "License"). You may not use
7 * this file except in compliance with the License. You can obtain a copy
8 * in the file LICENSE in the source distribution or at
9 * https://www.openssl.org/source/license.html
10 */
11
12 #include <string.h>
13 #include "internal/cryptlib.h"
14 #include "internal/sm3.h"
15
16 #define DATA_ORDER_IS_BIG_ENDIAN
17
18 #define HASH_LONG SM3_WORD
19 #define HASH_CTX SM3_CTX
20 #define HASH_CBLOCK SM3_CBLOCK
21 #define HASH_UPDATE ossl_sm3_update
22 #define HASH_TRANSFORM ossl_sm3_transform
23 #define HASH_FINAL ossl_sm3_final
24 #define HASH_MAKE_STRING(c, s) \
25 do { \
26 unsigned long ll; \
27 ll = (c)->A; \
28 (void)HOST_l2c(ll, (s)); \
29 ll = (c)->B; \
30 (void)HOST_l2c(ll, (s)); \
31 ll = (c)->C; \
32 (void)HOST_l2c(ll, (s)); \
33 ll = (c)->D; \
34 (void)HOST_l2c(ll, (s)); \
35 ll = (c)->E; \
36 (void)HOST_l2c(ll, (s)); \
37 ll = (c)->F; \
38 (void)HOST_l2c(ll, (s)); \
39 ll = (c)->G; \
40 (void)HOST_l2c(ll, (s)); \
41 ll = (c)->H; \
42 (void)HOST_l2c(ll, (s)); \
43 } while (0)
44
45 #if defined(OPENSSL_SM3_ASM)
46 #if defined(__aarch64__) || defined(_M_ARM64)
47 #include "crypto/arm_arch.h"
48 #define HWSM3_CAPABLE (OPENSSL_armcap_P & ARMV8_SM3)
49 void ossl_hwsm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
50 #endif
51 #if defined(OPENSSL_CPUID_OBJ) && defined(__riscv) && __riscv_xlen == 64
52 #include "crypto/riscv_arch.h"
53 #define HWSM3_CAPABLE 1
54 void ossl_hwsm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
55 #endif
56 #if (defined(__x86_64) || defined(__x86_64__) || defined(_M_X64))
57 #define HWSM3_CAPABLE ((OPENSSL_ia32cap_P[2] & (1 << 5)) && (OPENSSL_ia32cap_P[5] & (1 << 1)))
58 void ossl_hwsm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
59 #endif
60 #endif
61
62 #if defined(HWSM3_CAPABLE)
63 #define HASH_BLOCK_DATA_ORDER (HWSM3_CAPABLE ? ossl_hwsm3_block_data_order \
64 : ossl_sm3_block_data_order)
65 #else
66 #define HASH_BLOCK_DATA_ORDER ossl_sm3_block_data_order
67 #endif
68
69 void ossl_sm3_block_data_order(SM3_CTX *c, const void *p, size_t num);
70 void ossl_sm3_transform(SM3_CTX *c, const unsigned char *data);
71
72 #include "crypto/md32_common.h"
73
74 #ifndef PEDANTIC
75 #if defined(__GNUC__) && __GNUC__ >= 2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
76 #if defined(__riscv_zksh)
77 #define P0(x) ({ MD32_REG_T ret; \
78 asm ("sm3p0 %0, %1" \
79 : "=r"(ret) \
80 : "r"(x)); ret; })
81 #define P1(x) ({ MD32_REG_T ret; \
82 asm ("sm3p1 %0, %1" \
83 : "=r"(ret) \
84 : "r"(x)); ret; })
85 #endif
86 #endif
87 #endif
88
89 #ifndef P0
90 #define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17))
91 #endif
92 #ifndef P1
93 #define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23))
94 #endif
95
96 #define FF0(X, Y, Z) (X ^ Y ^ Z)
97 #define GG0(X, Y, Z) (X ^ Y ^ Z)
98
99 #define FF1(X, Y, Z) ((X & Y) | ((X | Y) & Z))
100 #define GG1(X, Y, Z) ((Z ^ (X & (Y ^ Z))))
101
102 #define EXPAND(W0, W7, W13, W3, W10) \
103 (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10)
104
105 #define RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) \
106 do { \
107 const SM3_WORD A12 = ROTATE(A, 12); \
108 const SM3_WORD A12_SM = A12 + E + TJ; \
109 const SM3_WORD SS1 = ROTATE(A12_SM, 7); \
110 const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \
111 const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi; \
112 B = ROTATE(B, 9); \
113 D = TT1; \
114 F = ROTATE(F, 19); \
115 H = P0(TT2); \
116 } while (0)
117
118 #define R1(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \
119 RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF0, GG0)
120
121 #define R2(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \
122 RND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF1, GG1)
123
124 #define SM3_A 0x7380166fUL
125 #define SM3_B 0x4914b2b9UL
126 #define SM3_C 0x172442d7UL
127 #define SM3_D 0xda8a0600UL
128 #define SM3_E 0xa96f30bcUL
129 #define SM3_F 0x163138aaUL
130 #define SM3_G 0xe38dee4dUL
131 #define SM3_H 0xb0fb0e4eUL