diff options
author | tb <> | 2018-11-11 06:53:31 +0000 |
---|---|---|
committer | tb <> | 2018-11-11 06:53:31 +0000 |
commit | 1d52a77751bd718c4bc8afa60ff78c4306636cd9 (patch) | |
tree | c825763485b682e2b4d62f2c543f52d67260398e /src/lib/libcrypto/sm3 | |
parent | 02538eda2e860ce41169bbc6f0d2519d31eda733 (diff) | |
download | openbsd-1d52a77751bd718c4bc8afa60ff78c4306636cd9.tar.gz openbsd-1d52a77751bd718c4bc8afa60ff78c4306636cd9.tar.bz2 openbsd-1d52a77751bd718c4bc8afa60ff78c4306636cd9.zip |
Add Ribose Inc's implementation of the SM3 hashing function with
tweaks from jsing and myself. The SM2/SM3/SM4 algorithms are mandatory
for legal use of cryptography within China and [are] widely applied in
the country, covering identification/financial cards, contactless,
TPM 2.0 and PKI.
ok beck inoguchi jsing
Diffstat (limited to 'src/lib/libcrypto/sm3')
-rw-r--r-- | src/lib/libcrypto/sm3/sm3.c | 206 | ||||
-rw-r--r-- | src/lib/libcrypto/sm3/sm3.h | 53 | ||||
-rw-r--r-- | src/lib/libcrypto/sm3/sm3_locl.h | 85 |
3 files changed, 344 insertions, 0 deletions
diff --git a/src/lib/libcrypto/sm3/sm3.c b/src/lib/libcrypto/sm3/sm3.c new file mode 100644 index 0000000000..ff6240a0bb --- /dev/null +++ b/src/lib/libcrypto/sm3/sm3.c | |||
@@ -0,0 +1,206 @@ | |||
1 | /* $OpenBSD: sm3.c,v 1.1 2018/11/11 06:53:31 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018, Ribose Inc | ||
4 | * | ||
5 | * Permission to use, copy, modify, and/or distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #ifndef OPENSSL_NO_SM3 | ||
19 | |||
20 | #include <openssl/sm3.h> | ||
21 | |||
22 | #include "sm3_locl.h" | ||
23 | |||
24 | int | ||
25 | SM3_Init(SM3_CTX *c) | ||
26 | { | ||
27 | memset(c, 0, sizeof(*c)); | ||
28 | c->A = SM3_A; | ||
29 | c->B = SM3_B; | ||
30 | c->C = SM3_C; | ||
31 | c->D = SM3_D; | ||
32 | c->E = SM3_E; | ||
33 | c->F = SM3_F; | ||
34 | c->G = SM3_G; | ||
35 | c->H = SM3_H; | ||
36 | return 1; | ||
37 | } | ||
38 | |||
39 | void | ||
40 | SM3_block_data_order(SM3_CTX *ctx, const void *p, size_t num) | ||
41 | { | ||
42 | const unsigned char *data = p; | ||
43 | SM3_WORD A, B, C, D, E, F, G, H; | ||
44 | SM3_WORD W00, W01, W02, W03, W04, W05, W06, W07; | ||
45 | SM3_WORD W08, W09, W10, W11, W12, W13, W14, W15; | ||
46 | |||
47 | while (num-- != 0) { | ||
48 | A = ctx->A; | ||
49 | B = ctx->B; | ||
50 | C = ctx->C; | ||
51 | D = ctx->D; | ||
52 | E = ctx->E; | ||
53 | F = ctx->F; | ||
54 | G = ctx->G; | ||
55 | H = ctx->H; | ||
56 | |||
57 | /* | ||
58 | * We have to load all message bytes immediately since SM3 reads | ||
59 | * them slightly out of order. | ||
60 | */ | ||
61 | HOST_c2l(data, W00); | ||
62 | HOST_c2l(data, W01); | ||
63 | HOST_c2l(data, W02); | ||
64 | HOST_c2l(data, W03); | ||
65 | HOST_c2l(data, W04); | ||
66 | HOST_c2l(data, W05); | ||
67 | HOST_c2l(data, W06); | ||
68 | HOST_c2l(data, W07); | ||
69 | HOST_c2l(data, W08); | ||
70 | HOST_c2l(data, W09); | ||
71 | HOST_c2l(data, W10); | ||
72 | HOST_c2l(data, W11); | ||
73 | HOST_c2l(data, W12); | ||
74 | HOST_c2l(data, W13); | ||
75 | HOST_c2l(data, W14); | ||
76 | HOST_c2l(data, W15); | ||
77 | |||
78 | R1(A, B, C, D, E, F, G, H, 0x79cc4519, W00, W00 ^ W04); | ||
79 | W00 = EXPAND(W00, W07, W13, W03, W10); | ||
80 | R1(D, A, B, C, H, E, F, G, 0xf3988a32, W01, W01 ^ W05); | ||
81 | W01 = EXPAND(W01, W08, W14, W04, W11); | ||
82 | R1(C, D, A, B, G, H, E, F, 0xe7311465, W02, W02 ^ W06); | ||
83 | W02 = EXPAND(W02, W09, W15, W05, W12); | ||
84 | R1(B, C, D, A, F, G, H, E, 0xce6228cb, W03, W03 ^ W07); | ||
85 | W03 = EXPAND(W03, W10, W00, W06, W13); | ||
86 | R1(A, B, C, D, E, F, G, H, 0x9cc45197, W04, W04 ^ W08); | ||
87 | W04 = EXPAND(W04, W11, W01, W07, W14); | ||
88 | R1(D, A, B, C, H, E, F, G, 0x3988a32f, W05, W05 ^ W09); | ||
89 | W05 = EXPAND(W05, W12, W02, W08, W15); | ||
90 | R1(C, D, A, B, G, H, E, F, 0x7311465e, W06, W06 ^ W10); | ||
91 | W06 = EXPAND(W06, W13, W03, W09, W00); | ||
92 | R1(B, C, D, A, F, G, H, E, 0xe6228cbc, W07, W07 ^ W11); | ||
93 | W07 = EXPAND(W07, W14, W04, W10, W01); | ||
94 | R1(A, B, C, D, E, F, G, H, 0xcc451979, W08, W08 ^ W12); | ||
95 | W08 = EXPAND(W08, W15, W05, W11, W02); | ||
96 | R1(D, A, B, C, H, E, F, G, 0x988a32f3, W09, W09 ^ W13); | ||
97 | W09 = EXPAND(W09, W00, W06, W12, W03); | ||
98 | R1(C, D, A, B, G, H, E, F, 0x311465e7, W10, W10 ^ W14); | ||
99 | W10 = EXPAND(W10, W01, W07, W13, W04); | ||
100 | R1(B, C, D, A, F, G, H, E, 0x6228cbce, W11, W11 ^ W15); | ||
101 | W11 = EXPAND(W11, W02, W08, W14, W05); | ||
102 | R1(A, B, C, D, E, F, G, H, 0xc451979c, W12, W12 ^ W00); | ||
103 | W12 = EXPAND(W12, W03, W09, W15, W06); | ||
104 | R1(D, A, B, C, H, E, F, G, 0x88a32f39, W13, W13 ^ W01); | ||
105 | W13 = EXPAND(W13, W04, W10, W00, W07); | ||
106 | R1(C, D, A, B, G, H, E, F, 0x11465e73, W14, W14 ^ W02); | ||
107 | W14 = EXPAND(W14, W05, W11, W01, W08); | ||
108 | R1(B, C, D, A, F, G, H, E, 0x228cbce6, W15, W15 ^ W03); | ||
109 | W15 = EXPAND(W15, W06, W12, W02, W09); | ||
110 | R2(A, B, C, D, E, F, G, H, 0x9d8a7a87, W00, W00 ^ W04); | ||
111 | W00 = EXPAND(W00, W07, W13, W03, W10); | ||
112 | R2(D, A, B, C, H, E, F, G, 0x3b14f50f, W01, W01 ^ W05); | ||
113 | W01 = EXPAND(W01, W08, W14, W04, W11); | ||
114 | R2(C, D, A, B, G, H, E, F, 0x7629ea1e, W02, W02 ^ W06); | ||
115 | W02 = EXPAND(W02, W09, W15, W05, W12); | ||
116 | R2(B, C, D, A, F, G, H, E, 0xec53d43c, W03, W03 ^ W07); | ||
117 | W03 = EXPAND(W03, W10, W00, W06, W13); | ||
118 | R2(A, B, C, D, E, F, G, H, 0xd8a7a879, W04, W04 ^ W08); | ||
119 | W04 = EXPAND(W04, W11, W01, W07, W14); | ||
120 | R2(D, A, B, C, H, E, F, G, 0xb14f50f3, W05, W05 ^ W09); | ||
121 | W05 = EXPAND(W05, W12, W02, W08, W15); | ||
122 | R2(C, D, A, B, G, H, E, F, 0x629ea1e7, W06, W06 ^ W10); | ||
123 | W06 = EXPAND(W06, W13, W03, W09, W00); | ||
124 | R2(B, C, D, A, F, G, H, E, 0xc53d43ce, W07, W07 ^ W11); | ||
125 | W07 = EXPAND(W07, W14, W04, W10, W01); | ||
126 | R2(A, B, C, D, E, F, G, H, 0x8a7a879d, W08, W08 ^ W12); | ||
127 | W08 = EXPAND(W08, W15, W05, W11, W02); | ||
128 | R2(D, A, B, C, H, E, F, G, 0x14f50f3b, W09, W09 ^ W13); | ||
129 | W09 = EXPAND(W09, W00, W06, W12, W03); | ||
130 | R2(C, D, A, B, G, H, E, F, 0x29ea1e76, W10, W10 ^ W14); | ||
131 | W10 = EXPAND(W10, W01, W07, W13, W04); | ||
132 | R2(B, C, D, A, F, G, H, E, 0x53d43cec, W11, W11 ^ W15); | ||
133 | W11 = EXPAND(W11, W02, W08, W14, W05); | ||
134 | R2(A, B, C, D, E, F, G, H, 0xa7a879d8, W12, W12 ^ W00); | ||
135 | W12 = EXPAND(W12, W03, W09, W15, W06); | ||
136 | R2(D, A, B, C, H, E, F, G, 0x4f50f3b1, W13, W13 ^ W01); | ||
137 | W13 = EXPAND(W13, W04, W10, W00, W07); | ||
138 | R2(C, D, A, B, G, H, E, F, 0x9ea1e762, W14, W14 ^ W02); | ||
139 | W14 = EXPAND(W14, W05, W11, W01, W08); | ||
140 | R2(B, C, D, A, F, G, H, E, 0x3d43cec5, W15, W15 ^ W03); | ||
141 | W15 = EXPAND(W15, W06, W12, W02, W09); | ||
142 | R2(A, B, C, D, E, F, G, H, 0x7a879d8a, W00, W00 ^ W04); | ||
143 | W00 = EXPAND(W00, W07, W13, W03, W10); | ||
144 | R2(D, A, B, C, H, E, F, G, 0xf50f3b14, W01, W01 ^ W05); | ||
145 | W01 = EXPAND(W01, W08, W14, W04, W11); | ||
146 | R2(C, D, A, B, G, H, E, F, 0xea1e7629, W02, W02 ^ W06); | ||
147 | W02 = EXPAND(W02, W09, W15, W05, W12); | ||
148 | R2(B, C, D, A, F, G, H, E, 0xd43cec53, W03, W03 ^ W07); | ||
149 | W03 = EXPAND(W03, W10, W00, W06, W13); | ||
150 | R2(A, B, C, D, E, F, G, H, 0xa879d8a7, W04, W04 ^ W08); | ||
151 | W04 = EXPAND(W04, W11, W01, W07, W14); | ||
152 | R2(D, A, B, C, H, E, F, G, 0x50f3b14f, W05, W05 ^ W09); | ||
153 | W05 = EXPAND(W05, W12, W02, W08, W15); | ||
154 | R2(C, D, A, B, G, H, E, F, 0xa1e7629e, W06, W06 ^ W10); | ||
155 | W06 = EXPAND(W06, W13, W03, W09, W00); | ||
156 | R2(B, C, D, A, F, G, H, E, 0x43cec53d, W07, W07 ^ W11); | ||
157 | W07 = EXPAND(W07, W14, W04, W10, W01); | ||
158 | R2(A, B, C, D, E, F, G, H, 0x879d8a7a, W08, W08 ^ W12); | ||
159 | W08 = EXPAND(W08, W15, W05, W11, W02); | ||
160 | R2(D, A, B, C, H, E, F, G, 0x0f3b14f5, W09, W09 ^ W13); | ||
161 | W09 = EXPAND(W09, W00, W06, W12, W03); | ||
162 | R2(C, D, A, B, G, H, E, F, 0x1e7629ea, W10, W10 ^ W14); | ||
163 | W10 = EXPAND(W10, W01, W07, W13, W04); | ||
164 | R2(B, C, D, A, F, G, H, E, 0x3cec53d4, W11, W11 ^ W15); | ||
165 | W11 = EXPAND(W11, W02, W08, W14, W05); | ||
166 | R2(A, B, C, D, E, F, G, H, 0x79d8a7a8, W12, W12 ^ W00); | ||
167 | W12 = EXPAND(W12, W03, W09, W15, W06); | ||
168 | R2(D, A, B, C, H, E, F, G, 0xf3b14f50, W13, W13 ^ W01); | ||
169 | W13 = EXPAND(W13, W04, W10, W00, W07); | ||
170 | R2(C, D, A, B, G, H, E, F, 0xe7629ea1, W14, W14 ^ W02); | ||
171 | W14 = EXPAND(W14, W05, W11, W01, W08); | ||
172 | R2(B, C, D, A, F, G, H, E, 0xcec53d43, W15, W15 ^ W03); | ||
173 | W15 = EXPAND(W15, W06, W12, W02, W09); | ||
174 | R2(A, B, C, D, E, F, G, H, 0x9d8a7a87, W00, W00 ^ W04); | ||
175 | W00 = EXPAND(W00, W07, W13, W03, W10); | ||
176 | R2(D, A, B, C, H, E, F, G, 0x3b14f50f, W01, W01 ^ W05); | ||
177 | W01 = EXPAND(W01, W08, W14, W04, W11); | ||
178 | R2(C, D, A, B, G, H, E, F, 0x7629ea1e, W02, W02 ^ W06); | ||
179 | W02 = EXPAND(W02, W09, W15, W05, W12); | ||
180 | R2(B, C, D, A, F, G, H, E, 0xec53d43c, W03, W03 ^ W07); | ||
181 | W03 = EXPAND(W03, W10, W00, W06, W13); | ||
182 | R2(A, B, C, D, E, F, G, H, 0xd8a7a879, W04, W04 ^ W08); | ||
183 | R2(D, A, B, C, H, E, F, G, 0xb14f50f3, W05, W05 ^ W09); | ||
184 | R2(C, D, A, B, G, H, E, F, 0x629ea1e7, W06, W06 ^ W10); | ||
185 | R2(B, C, D, A, F, G, H, E, 0xc53d43ce, W07, W07 ^ W11); | ||
186 | R2(A, B, C, D, E, F, G, H, 0x8a7a879d, W08, W08 ^ W12); | ||
187 | R2(D, A, B, C, H, E, F, G, 0x14f50f3b, W09, W09 ^ W13); | ||
188 | R2(C, D, A, B, G, H, E, F, 0x29ea1e76, W10, W10 ^ W14); | ||
189 | R2(B, C, D, A, F, G, H, E, 0x53d43cec, W11, W11 ^ W15); | ||
190 | R2(A, B, C, D, E, F, G, H, 0xa7a879d8, W12, W12 ^ W00); | ||
191 | R2(D, A, B, C, H, E, F, G, 0x4f50f3b1, W13, W13 ^ W01); | ||
192 | R2(C, D, A, B, G, H, E, F, 0x9ea1e762, W14, W14 ^ W02); | ||
193 | R2(B, C, D, A, F, G, H, E, 0x3d43cec5, W15, W15 ^ W03); | ||
194 | |||
195 | ctx->A ^= A; | ||
196 | ctx->B ^= B; | ||
197 | ctx->C ^= C; | ||
198 | ctx->D ^= D; | ||
199 | ctx->E ^= E; | ||
200 | ctx->F ^= F; | ||
201 | ctx->G ^= G; | ||
202 | ctx->H ^= H; | ||
203 | } | ||
204 | } | ||
205 | |||
206 | #endif /* !OPENSSL_NO_SM3 */ | ||
diff --git a/src/lib/libcrypto/sm3/sm3.h b/src/lib/libcrypto/sm3/sm3.h new file mode 100644 index 0000000000..553c64dcdb --- /dev/null +++ b/src/lib/libcrypto/sm3/sm3.h | |||
@@ -0,0 +1,53 @@ | |||
1 | /* $OpenBSD: sm3.h,v 1.1 2018/11/11 06:53:31 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018, Ribose Inc | ||
4 | * | ||
5 | * Permission to use, copy, modify, and/or distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #ifndef HEADER_SM3_H | ||
19 | #define HEADER_SM3_H | ||
20 | |||
21 | #include <stddef.h> | ||
22 | #include <openssl/opensslconf.h> | ||
23 | |||
24 | #ifdef __cplusplus | ||
25 | extern "C" { | ||
26 | #endif | ||
27 | |||
28 | #ifdef OPENSSL_NO_SM3 | ||
29 | #error SM3 is disabled. | ||
30 | #endif | ||
31 | |||
32 | #define SM3_DIGEST_LENGTH 32 | ||
33 | #define SM3_WORD unsigned int | ||
34 | |||
35 | #define SM3_CBLOCK 64 | ||
36 | #define SM3_LBLOCK (SM3_CBLOCK / 4) | ||
37 | |||
38 | typedef struct SM3state_st { | ||
39 | SM3_WORD A, B, C, D, E, F, G, H; | ||
40 | SM3_WORD Nl, Nh; | ||
41 | SM3_WORD data[SM3_LBLOCK]; | ||
42 | unsigned int num; | ||
43 | } SM3_CTX; | ||
44 | |||
45 | int SM3_Init(SM3_CTX *c); | ||
46 | int SM3_Update(SM3_CTX *c, const void *data, size_t len); | ||
47 | int SM3_Final(unsigned char *md, SM3_CTX *c); | ||
48 | |||
49 | #ifdef __cplusplus | ||
50 | } | ||
51 | #endif | ||
52 | |||
53 | #endif /* HEADER_SM3_H */ | ||
diff --git a/src/lib/libcrypto/sm3/sm3_locl.h b/src/lib/libcrypto/sm3/sm3_locl.h new file mode 100644 index 0000000000..6ecf8094f9 --- /dev/null +++ b/src/lib/libcrypto/sm3/sm3_locl.h | |||
@@ -0,0 +1,85 @@ | |||
1 | /* $OpenBSD: sm3_locl.h,v 1.1 2018/11/11 06:53:31 tb Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2018, Ribose Inc | ||
4 | * | ||
5 | * Permission to use, copy, modify, and/or distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | |||
18 | #include <string.h> | ||
19 | |||
20 | #include <openssl/opensslconf.h> | ||
21 | |||
22 | #define DATA_ORDER_IS_BIG_ENDIAN | ||
23 | |||
24 | #define HASH_LONG SM3_WORD | ||
25 | #define HASH_CTX SM3_CTX | ||
26 | #define HASH_CBLOCK SM3_CBLOCK | ||
27 | #define HASH_UPDATE SM3_Update | ||
28 | #define HASH_TRANSFORM SM3_Transform | ||
29 | #define HASH_FINAL SM3_Final | ||
30 | #define HASH_MAKE_STRING(c, s) do { \ | ||
31 | unsigned long ll; \ | ||
32 | ll = (c)->A; HOST_l2c(ll, (s)); \ | ||
33 | ll = (c)->B; HOST_l2c(ll, (s)); \ | ||
34 | ll = (c)->C; HOST_l2c(ll, (s)); \ | ||
35 | ll = (c)->D; HOST_l2c(ll, (s)); \ | ||
36 | ll = (c)->E; HOST_l2c(ll, (s)); \ | ||
37 | ll = (c)->F; HOST_l2c(ll, (s)); \ | ||
38 | ll = (c)->G; HOST_l2c(ll, (s)); \ | ||
39 | ll = (c)->H; HOST_l2c(ll, (s)); \ | ||
40 | } while (0) | ||
41 | #define HASH_BLOCK_DATA_ORDER SM3_block_data_order | ||
42 | |||
43 | void SM3_block_data_order(SM3_CTX *c, const void *p, size_t num); | ||
44 | void SM3_transform(SM3_CTX *c, const unsigned char *data); | ||
45 | |||
46 | #include "md32_common.h" | ||
47 | |||
48 | #define P0(X) (X ^ ROTATE(X, 9) ^ ROTATE(X, 17)) | ||
49 | #define P1(X) (X ^ ROTATE(X, 15) ^ ROTATE(X, 23)) | ||
50 | |||
51 | #define FF0(X, Y, Z) (X ^ Y ^ Z) | ||
52 | #define GG0(X, Y, Z) (X ^ Y ^ Z) | ||
53 | |||
54 | #define FF1(X, Y, Z) ((X & Y) | ((X | Y) & Z)) | ||
55 | #define GG1(X, Y, Z) ((Z ^ (X & (Y ^ Z)))) | ||
56 | |||
57 | #define EXPAND(W0, W7, W13, W3, W10) \ | ||
58 | (P1(W0 ^ W7 ^ ROTATE(W13, 15)) ^ ROTATE(W3, 7) ^ W10) | ||
59 | |||
60 | #define ROUND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) do { \ | ||
61 | const SM3_WORD A12 = ROTATE(A, 12); \ | ||
62 | const SM3_WORD A12_SM = A12 + E + TJ; \ | ||
63 | const SM3_WORD SS1 = ROTATE(A12_SM, 7); \ | ||
64 | const SM3_WORD TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \ | ||
65 | const SM3_WORD TT2 = GG(E, F, G) + H + SS1 + Wi; \ | ||
66 | B = ROTATE(B, 9); \ | ||
67 | D = TT1; \ | ||
68 | F = ROTATE(F, 19); \ | ||
69 | H = P0(TT2); \ | ||
70 | } while(0) | ||
71 | |||
72 | #define R1(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \ | ||
73 | ROUND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF0, GG0) | ||
74 | |||
75 | #define R2(A, B, C, D, E, F, G, H, TJ, Wi, Wj) \ | ||
76 | ROUND(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF1, GG1) | ||
77 | |||
78 | #define SM3_A 0x7380166fUL | ||
79 | #define SM3_B 0x4914b2b9UL | ||
80 | #define SM3_C 0x172442d7UL | ||
81 | #define SM3_D 0xda8a0600UL | ||
82 | #define SM3_E 0xa96f30bcUL | ||
83 | #define SM3_F 0x163138aaUL | ||
84 | #define SM3_G 0xe38dee4dUL | ||
85 | #define SM3_H 0xb0fb0e4eUL | ||