Debian Codegraph @

lib/alg-sm3.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2025 Björn Esser <besser82 at fedoraproject.org>
3 * All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted.
7 *
8 * THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include "crypt-port.h"cxx . . $ `<file>/lib/crypt-port.h`/
18
19 #if INCLUDE_sm3cryptcxx . . $ `crypt-hashes.h:21:9`! || INCLUDE_sm3_yescryptcxx . . $ `crypt-hashes.h:20:9`!
20
21 #include "alg-sm3.h"cxx . . $ `<file>/lib/alg-sm3.h`/
22 #include "byteorder.h"cxx . . $ `<file>/lib/byteorder.h`/
23
24 #define ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
25
26 #define P0cxx . . $ `lib/alg-sm3.c:26:9`!(X) (X ^ ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(X, 9) ^ ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(X, 17))
27 #define P1cxx . . $ `lib/alg-sm3.c:27:9`!(X) (X ^ ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(X, 15) ^ ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(X, 23))
28
29 #define FF0cxx . . $ `lib/alg-sm3.c:29:9`!(X,Y,Z) (X ^ Y ^ Z)
30 #define GG0cxx . . $ `lib/alg-sm3.c:30:9`!(X,Y,Z) (X ^ Y ^ Z)
31
32 #define FF1cxx . . $ `lib/alg-sm3.c:32:9`!(X,Y,Z) ((X & Y) | ((X | Y) & Z))
33 #define GG1cxx . . $ `lib/alg-sm3.c:33:9`!(X,Y,Z) ((Z ^ (X & (Y ^ Z))))
34
35 #define EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W0,W7,W13,W3,W10) \
36 (P1cxx . . $ `lib/alg-sm3.c:27:9`!(W0 ^ W7 ^ ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(W13, 15)) ^ ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(W3, 7) ^ W10)
37
38 #define RNDcxx . . $ `lib/alg-sm3.c:38:9`!(A, B, C, D, E, F, G, H, TJ, Wi, Wj, FF, GG) \
39 do \
40 { \
41 const uint32_t A12 = ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(A, 12); \
42 const uint32_t A12_SM = A12 + E + TJ; \
43 const uint32_t SS1 = ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(A12_SM, 7); \
44 const uint32_t TT1 = FF(A, B, C) + D + (SS1 ^ A12) + (Wj); \
45 const uint32_t TT2 = GG(E, F, G) + H + SS1 + Wi; \
46 B = ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(B, 9); \
47 D = TT1; \
48 F = ROTATEcxx . . $ `lib/alg-sm3.c:24:9`!(F, 19); \
49 H = P0cxx . . $ `lib/alg-sm3.c:26:9`!(TT2); \
50 } \
51 while(0)
52
53 #define R1cxx . . $ `lib/alg-sm3.c:53:9`!(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
54 RNDcxx . . $ `lib/alg-sm3.c:38:9`!(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF0cxx . . $ `lib/alg-sm3.c:29:9`!,GG0cxx . . $ `lib/alg-sm3.c:30:9`!)
55
56 #define R2cxx . . $ `lib/alg-sm3.c:56:9`!(A,B,C,D,E,F,G,H,TJ,Wi,Wj) \
57 RNDcxx . . $ `lib/alg-sm3.c:38:9`!(A,B,C,D,E,F,G,H,TJ,Wi,Wj,FF1cxx . . $ `lib/alg-sm3.c:32:9`!,GG1cxx . . $ `lib/alg-sm3.c:33:9`!)
58
59 /*
60 * Encode a length len*2 vector of (uint32_t) into a length len*8 vector of
61 * (uint8_t) in big-endian form.
62 */
63 static void
64 sm3_be32enc_vectcxx . . $ sm3_be32enc_vect(63c3a6f0d52c79b1).(uint8_tcxx . . $ uint8_t# * dst, const uint32_tcxx . . $ uint32_t# * src, size_t len)
65 {
66
67 /* Encode vector, two words at a time. */
68 do
69 {
70 be32enccxx . . $ `lib/byteorder.h:154:9`!(&dst[0], src[0]);
71 be32enccxx . . $ `lib/byteorder.h:154:9`!(&dst[4], src[1]);
72 src += 2;
73 dst += 8;
74 }
75 while (--len);
76 }
77
78 /*
79 * Decode a big-endian length len*8 vector of (uint8_t) into a length
80 * len*2 vector of (uint32_t).
81 */
82 static void
83 sm3_be32dec_vectcxx . . $ sm3_be32dec_vect(3cd0bd0ee96534e2).(uint32_tcxx . . $ uint32_t# * dst, const uint8_tcxx . . $ uint8_t# * src, size_t len)
84 {
85
86 /* Decode vector, two words at a time. */
87 do
88 {
89 dst[0] = be32deccxx . . $ `lib/byteorder.h:155:9`!(&src[0]);
90 dst[1] = be32deccxx . . $ `lib/byteorder.h:155:9`!(&src[4]);
91 src += 8;
92 dst += 2;
93 }
94 while (--len);
95 }
96
97 static void
98 sm3_transformcxx . . $ sm3_transform(a3b8e8900f00030a).(uint32_tcxx . . $ uint32_t# state[static restrict 8],
99 const uint8_tcxx . . $ uint8_t# block[static restrict 64],
100 uint32_tcxx . . $ uint32_t# W[static restrict 64])
101 {
102 register uint32_tcxx . . $ uint32_t# A, B, C, D, E, F, G, H;
103 uint32_tcxx . . $ uint32_t# W00, W01, W02, W03, W04, W05, W06, W07,
104 W08, W09, W10, W11, W12, W13, W14, W15;
105
106 /* 1. Prepare the first part of the message schedule W. */
107 sm3_be32dec_vectcxx . . $ sm3_be32dec_vect(3cd0bd0ee96534e2).(W, block, 8);
108
109 A = state[0];
110 B = state[1];
111 C = state[2];
112 D = state[3];
113 E = state[4];
114 F = state[5];
115 G = state[6];
116 H = state[7];
117
118 W00 = W[0];
119 W01 = W[1];
120 W02 = W[2];
121 W03 = W[3];
122 W04 = W[4];
123 W05 = W[5];
124 W06 = W[6];
125 W07 = W[7];
126 W08 = W[8];
127 W09 = W[9];
128 W10 = W[10];
129 W11 = W[11];
130 W12 = W[12];
131 W13 = W[13];
132 W14 = W[14];
133 W15 = W[15];
134
135 R1cxx . . $ `lib/alg-sm3.c:53:9`!(A, B, C, D, E, F, G, H, 0x79CC4519, W00, W00 ^ W04);
136 W00 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W00, W07, W13, W03, W10);
137 R1cxx . . $ `lib/alg-sm3.c:53:9`!(D, A, B, C, H, E, F, G, 0xF3988A32, W01, W01 ^ W05);
138 W01 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W01, W08, W14, W04, W11);
139 R1cxx . . $ `lib/alg-sm3.c:53:9`!(C, D, A, B, G, H, E, F, 0xE7311465, W02, W02 ^ W06);
140 W02 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W02, W09, W15, W05, W12);
141 R1cxx . . $ `lib/alg-sm3.c:53:9`!(B, C, D, A, F, G, H, E, 0xCE6228CB, W03, W03 ^ W07);
142 W03 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W03, W10, W00, W06, W13);
143 R1cxx . . $ `lib/alg-sm3.c:53:9`!(A, B, C, D, E, F, G, H, 0x9CC45197, W04, W04 ^ W08);
144 W04 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W04, W11, W01, W07, W14);
145 R1cxx . . $ `lib/alg-sm3.c:53:9`!(D, A, B, C, H, E, F, G, 0x3988A32F, W05, W05 ^ W09);
146 W05 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W05, W12, W02, W08, W15);
147 R1cxx . . $ `lib/alg-sm3.c:53:9`!(C, D, A, B, G, H, E, F, 0x7311465E, W06, W06 ^ W10);
148 W06 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W06, W13, W03, W09, W00);
149 R1cxx . . $ `lib/alg-sm3.c:53:9`!(B, C, D, A, F, G, H, E, 0xE6228CBC, W07, W07 ^ W11);
150 W07 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W07, W14, W04, W10, W01);
151 R1cxx . . $ `lib/alg-sm3.c:53:9`!(A, B, C, D, E, F, G, H, 0xCC451979, W08, W08 ^ W12);
152 W08 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W08, W15, W05, W11, W02);
153 R1cxx . . $ `lib/alg-sm3.c:53:9`!(D, A, B, C, H, E, F, G, 0x988A32F3, W09, W09 ^ W13);
154 W09 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W09, W00, W06, W12, W03);
155 R1cxx . . $ `lib/alg-sm3.c:53:9`!(C, D, A, B, G, H, E, F, 0x311465E7, W10, W10 ^ W14);
156 W10 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W10, W01, W07, W13, W04);
157 R1cxx . . $ `lib/alg-sm3.c:53:9`!(B, C, D, A, F, G, H, E, 0x6228CBCE, W11, W11 ^ W15);
158 W11 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W11, W02, W08, W14, W05);
159 R1cxx . . $ `lib/alg-sm3.c:53:9`!(A, B, C, D, E, F, G, H, 0xC451979C, W12, W12 ^ W00);
160 W12 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W12, W03, W09, W15, W06);
161 R1cxx . . $ `lib/alg-sm3.c:53:9`!(D, A, B, C, H, E, F, G, 0x88A32F39, W13, W13 ^ W01);
162 W13 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W13, W04, W10, W00, W07);
163 R1cxx . . $ `lib/alg-sm3.c:53:9`!(C, D, A, B, G, H, E, F, 0x11465E73, W14, W14 ^ W02);
164 W14 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W14, W05, W11, W01, W08);
165 R1cxx . . $ `lib/alg-sm3.c:53:9`!(B, C, D, A, F, G, H, E, 0x228CBCE6, W15, W15 ^ W03);
166 W15 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W15, W06, W12, W02, W09);
167 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0x9D8A7A87, W00, W00 ^ W04);
168 W00 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W00, W07, W13, W03, W10);
169 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x3B14F50F, W01, W01 ^ W05);
170 W01 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W01, W08, W14, W04, W11);
171 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x7629EA1E, W02, W02 ^ W06);
172 W02 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W02, W09, W15, W05, W12);
173 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0xEC53D43C, W03, W03 ^ W07);
174 W03 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W03, W10, W00, W06, W13);
175 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0xD8A7A879, W04, W04 ^ W08);
176 W04 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W04, W11, W01, W07, W14);
177 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0xB14F50F3, W05, W05 ^ W09);
178 W05 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W05, W12, W02, W08, W15);
179 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x629EA1E7, W06, W06 ^ W10);
180 W06 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W06, W13, W03, W09, W00);
181 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0xC53D43CE, W07, W07 ^ W11);
182 W07 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W07, W14, W04, W10, W01);
183 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0x8A7A879D, W08, W08 ^ W12);
184 W08 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W08, W15, W05, W11, W02);
185 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x14F50F3B, W09, W09 ^ W13);
186 W09 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W09, W00, W06, W12, W03);
187 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x29EA1E76, W10, W10 ^ W14);
188 W10 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W10, W01, W07, W13, W04);
189 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0x53D43CEC, W11, W11 ^ W15);
190 W11 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W11, W02, W08, W14, W05);
191 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0xA7A879D8, W12, W12 ^ W00);
192 W12 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W12, W03, W09, W15, W06);
193 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x4F50F3B1, W13, W13 ^ W01);
194 W13 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W13, W04, W10, W00, W07);
195 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x9EA1E762, W14, W14 ^ W02);
196 W14 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W14, W05, W11, W01, W08);
197 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0x3D43CEC5, W15, W15 ^ W03);
198 W15 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W15, W06, W12, W02, W09);
199 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0x7A879D8A, W00, W00 ^ W04);
200 W00 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W00, W07, W13, W03, W10);
201 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0xF50F3B14, W01, W01 ^ W05);
202 W01 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W01, W08, W14, W04, W11);
203 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0xEA1E7629, W02, W02 ^ W06);
204 W02 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W02, W09, W15, W05, W12);
205 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0xD43CEC53, W03, W03 ^ W07);
206 W03 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W03, W10, W00, W06, W13);
207 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0xA879D8A7, W04, W04 ^ W08);
208 W04 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W04, W11, W01, W07, W14);
209 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x50F3B14F, W05, W05 ^ W09);
210 W05 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W05, W12, W02, W08, W15);
211 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0xA1E7629E, W06, W06 ^ W10);
212 W06 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W06, W13, W03, W09, W00);
213 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0x43CEC53D, W07, W07 ^ W11);
214 W07 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W07, W14, W04, W10, W01);
215 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0x879D8A7A, W08, W08 ^ W12);
216 W08 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W08, W15, W05, W11, W02);
217 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x0F3B14F5, W09, W09 ^ W13);
218 W09 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W09, W00, W06, W12, W03);
219 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x1E7629EA, W10, W10 ^ W14);
220 W10 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W10, W01, W07, W13, W04);
221 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0x3CEC53D4, W11, W11 ^ W15);
222 W11 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W11, W02, W08, W14, W05);
223 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0x79D8A7A8, W12, W12 ^ W00);
224 W12 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W12, W03, W09, W15, W06);
225 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0xF3B14F50, W13, W13 ^ W01);
226 W13 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W13, W04, W10, W00, W07);
227 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0xE7629EA1, W14, W14 ^ W02);
228 W14 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W14, W05, W11, W01, W08);
229 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0xCEC53D43, W15, W15 ^ W03);
230 W15 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W15, W06, W12, W02, W09);
231 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0x9D8A7A87, W00, W00 ^ W04);
232 W00 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W00, W07, W13, W03, W10);
233 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x3B14F50F, W01, W01 ^ W05);
234 W01 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W01, W08, W14, W04, W11);
235 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x7629EA1E, W02, W02 ^ W06);
236 W02 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W02, W09, W15, W05, W12);
237 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0xEC53D43C, W03, W03 ^ W07);
238 W03 = EXPANDcxx . . $ `lib/alg-sm3.c:35:9`!(W03, W10, W00, W06, W13);
239 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0xD8A7A879, W04, W04 ^ W08);
240 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0xB14F50F3, W05, W05 ^ W09);
241 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x629EA1E7, W06, W06 ^ W10);
242 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0xC53D43CE, W07, W07 ^ W11);
243 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0x8A7A879D, W08, W08 ^ W12);
244 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x14F50F3B, W09, W09 ^ W13);
245 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x29EA1E76, W10, W10 ^ W14);
246 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0x53D43CEC, W11, W11 ^ W15);
247 R2cxx . . $ `lib/alg-sm3.c:56:9`!(A, B, C, D, E, F, G, H, 0xA7A879D8, W12, W12 ^ W00);
248 R2cxx . . $ `lib/alg-sm3.c:56:9`!(D, A, B, C, H, E, F, G, 0x4F50F3B1, W13, W13 ^ W01);
249 R2cxx . . $ `lib/alg-sm3.c:56:9`!(C, D, A, B, G, H, E, F, 0x9EA1E762, W14, W14 ^ W02);
250 R2cxx . . $ `lib/alg-sm3.c:56:9`!(B, C, D, A, F, G, H, E, 0x3D43CEC5, W15, W15 ^ W03);
251
252 state[0] ^= A;
253 state[1] ^= B;
254 state[2] ^= C;
255 state[3] ^= D;
256 state[4] ^= E;
257 state[5] ^= F;
258 state[6] ^= G;
259 state[7] ^= H;
260 }
261
262 /* Magic initialization constants. */
263 static const uint32_tcxx . . $ uint32_t# initial_statecxx . . $ initial_state.[8] =
264 {
265 0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600,
266 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e
267 };
268
269 /**
270 * sm3_init(ctx):
271 * Initialize the SM3 context ${ctx}.
272 */
273 void
274 sm3_initcxx . . $ `lib/crypt-port.h:407:9`!(sm3_ctxcxx . . $ sm3_ctx# * ctx)
275 {
276
277 /* Zero bits processed so far. */
278 ctx->countcxx . . $ $anonymous_type_50e5df6d771a4f96_0#count. = 0;
279
280 /* Initialize state. */
281 memcpycxx . . $ memcpy(cc817cb175f282d).(ctx->statecxx . . $ $anonymous_type_50e5df6d771a4f96_0#state., initial_statecxx . . $ initial_state., sizeof(initial_statecxx . . $ initial_state.));
282 }
283
284
285 /**
286 * sm3_update(ctx, in, len):
287 * Input ${len} bytes from ${in} into the SM3 context ${ctx}.
288 */
289 static void
290 _sm3_updatecxx . . $ _sm3_update(6625ad82bfa28d72).(sm3_ctxcxx . . $ sm3_ctx# * ctx, const void * in, size_t len,
291 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
292 {
293 uint32_tcxx . . $ uint32_t# r;
294 const uint8_tcxx . . $ uint8_t# * src = in;
295
296 /* Return immediately if we have nothing to do. */
297 if (len == 0)
298 return;
299
300 /* Number of bytes left in the buffer from previous updates. */
301 r = (ctx->countcxx . . $ $anonymous_type_50e5df6d771a4f96_0#count. >> 3) & 0x3f;
302
303 /* Update number of bits. */
304 ctx->countcxx . . $ $anonymous_type_50e5df6d771a4f96_0#count. += (uint64_tcxx . . $ uint64_t#)(len) << 3;
305
306 /* Handle the case where we don't need to perform any transforms. */
307 if (len < 64 - r)
308 {
309 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf.[r], src, len);
310 return;
311 }
312
313 /* Finish the current block. */
314 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf.[r], src, 64 - r);
315 sm3_transformcxx . . $ sm3_transform(a3b8e8900f00030a).(ctx->statecxx . . $ $anonymous_type_50e5df6d771a4f96_0#state., ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf., &tmp32[0]);
316 src += 64 - r;
317 len -= 64 - r;
318
319 /* Perform complete blocks. */
320 while (len >= 64)
321 {
322 sm3_transformcxx . . $ sm3_transform(a3b8e8900f00030a).(ctx->statecxx . . $ $anonymous_type_50e5df6d771a4f96_0#state., src, &tmp32[0]);
323 src += 64;
324 len -= 64;
325 }
326
327 /* Copy left over data into buffer. */
328 memcpycxx . . $ memcpy(cc817cb175f282d).(ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf., src, len);
329 }
330
331 /* Wrapper function for intermediate-values sanitization. */
332 void
333 sm3_updatecxx . . $ `lib/crypt-port.h:408:9`!(sm3_ctxcxx . . $ sm3_ctx# * ctx, const void * in, size_t len)
334 {
335 uint32_tcxx . . $ uint32_t# tmp32[72];
336
337 /* Call the real function. */
338 _sm3_updatecxx . . $ _sm3_update(6625ad82bfa28d72).(ctx, in, len, tmp32);
339
340 /* Clean the stack. */
341 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
342 }
343
344 static const uint8_tcxx . . $ uint8_t# PADcxx . . $ PAD.[64] =
345 {
346 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
347 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
348 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
349 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
350 };
351
352 /* Add padding and terminating bit-count. */
353 static void
354 sm3_padcxx . . $ sm3_pad(d75fe0d21f392c4b).(sm3_ctxcxx . . $ sm3_ctx# * ctx, uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
355 {
356 size_t r;
357
358 /* Figure out how many bytes we have buffered. */
359 r = (ctx->countcxx . . $ $anonymous_type_50e5df6d771a4f96_0#count. >> 3) & 0x3f;
360
361 /* Pad to 56 mod 64, transforming if we finish a block en route. */
362 if (r < 56)
363 {
364 /* Pad to 56 mod 64. */
365 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf.[r], PADcxx . . $ PAD., 56 - r);
366 }
367 else
368 {
369 /* Finish the current block and mix. */
370 memcpycxx . . $ memcpy(cc817cb175f282d).(&ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf.[r], PADcxx . . $ PAD., 64 - r);
371 sm3_transformcxx . . $ sm3_transform(a3b8e8900f00030a).(ctx->statecxx . . $ $anonymous_type_50e5df6d771a4f96_0#state., ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf., &tmp32[0]);
372
373 /* The start of the final block is all zeroes. */
374 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf.[0], 0, 56);
375 }
376
377 /* Add the terminating bit-count. */
378 be64enccxx . . $ `lib/byteorder.h:156:9`!(&ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf.[56], ctx->countcxx . . $ $anonymous_type_50e5df6d771a4f96_0#count.);
379
380 /* Mix in the final block. */
381 sm3_transformcxx . . $ sm3_transform(a3b8e8900f00030a).(ctx->statecxx . . $ $anonymous_type_50e5df6d771a4f96_0#state., ctx->bufcxx . . $ $anonymous_type_50e5df6d771a4f96_0#buf., &tmp32[0]);
382 }
383 /**
384 * sm3_final(digest, ctx):
385 * Output the SM3 hash of the data input to the context ${ctx} into the
386 * buffer ${digest}.
387 */
388 static void
389 _sm3_finalcxx . . $ _sm3_final(f804ca132eb9f60c).(uint8_tcxx . . $ uint8_t# digest[32], sm3_ctxcxx . . $ sm3_ctx# * ctx,
390 uint32_tcxx . . $ uint32_t# tmp32[static restrict 72])
391 {
392
393 /* Add padding. */
394 sm3_padcxx . . $ sm3_pad(d75fe0d21f392c4b).(ctx, tmp32);
395
396 /* Write the hash. */
397 sm3_be32enc_vectcxx . . $ sm3_be32enc_vect(63c3a6f0d52c79b1).(digest, ctx->statecxx . . $ $anonymous_type_50e5df6d771a4f96_0#state., 4);
398 }
399
400 /* Wrapper function for intermediate-values sanitization. */
401 void
402 sm3_finalcxx . . $ `lib/crypt-port.h:409:9`!(uint8_tcxx . . $ uint8_t# digest[32], sm3_ctxcxx . . $ sm3_ctx# * ctx)
403 {
404 uint32_tcxx . . $ uint32_t# tmp32[72];
405
406 /* Call the real function. */
407 _sm3_finalcxx . . $ _sm3_final(f804ca132eb9f60c).(digest, ctx, tmp32);
408
409 /* Clear the context state. */
410 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(ctx, sizeof(sm3_ctx));
411
412 /* Clean the stack. */
413 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
414 }
415
416 /**
417 * sm3_hash(in, len, digest, ctx):
418 * Compute the SM3 hash of ${len} bytes from ${in} and write it to ${digest},
419 * using the prepared context ${ctx}.
420 */
421 void
422 sm3_hashcxx . . $ `lib/crypt-port.h:410:9`!(const void * in, size_t len, uint8_tcxx . . $ uint8_t# digest[32], sm3_ctxcxx . . $ sm3_ctx# * ctx)
423 {
424 uint32_tcxx . . $ uint32_t# tmp32[72];
425
426 sm3_initcxx . . $ `lib/crypt-port.h:407:9`!(ctx);
427 _sm3_updatecxx . . $ _sm3_update(6625ad82bfa28d72).(ctx, in, len, tmp32);
428 _sm3_finalcxx . . $ _sm3_final(f804ca132eb9f60c).(digest, ctx, tmp32);
429
430 /* Clean the stack. */
431 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(tmp32, 288);
432 }
433
434 /**
435 * sm3_buf(in, len, digest):
436 * Compute the SM3 hash of ${len} bytes from ${in} and write it to ${digest}.
437 */
438 void
439 sm3_bufcxx . . $ `lib/crypt-port.h:411:9`!(const void * in, size_t len, uint8_tcxx . . $ uint8_t# digest[32])
440 {
441 sm3_ctxcxx . . $ sm3_ctx# ctx;
442
443 sm3_hashcxx . . $ `lib/crypt-port.h:410:9`!(in, len, digest, &ctx);
444
445 /* Clean the stack. */
446 explicit_bzerocxx . . $ `lib/crypt-port.h:166:9`!(&ctx, sizeof(sm3_ctx));
447 }
448
449 #endif /* INCLUDE_sm3crypt || INCLUDE_sm3_yescrypt */