/src/openssl/crypto/sha/sha3.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2017-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <string.h> |
11 | | #include "internal/sha3.h" |
12 | | #include "internal/common.h" |
13 | | |
14 | | #if defined(__aarch64__) && defined(KECCAK1600_ASM) |
15 | | #include "arch/arm_arch.h" |
16 | | #endif |
17 | | |
18 | | #if defined(__s390x__) && defined(OPENSSL_CPUID_OBJ) |
19 | | #include "arch/s390x_arch.h" |
20 | | #if defined(KECCAK1600_ASM) |
21 | | #define S390_SHA3 1 |
22 | | #define S390_SHA3_CAPABLE(name) \ |
23 | | ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(name)) && (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(name))) |
24 | | #endif |
25 | | #endif |
26 | | |
27 | | void SHA3_squeeze(uint64_t A[5][5], unsigned char *out, size_t len, size_t r, int next); |
28 | | |
29 | | void ossl_sha3_reset(KECCAK1600_CTX *ctx) |
30 | 0 | { |
31 | | #if defined(__s390x__) && defined(OPENSSL_CPUID_OBJ) |
32 | | if (!(OPENSSL_s390xcap_P.stfle[1] & S390X_CAPBIT(S390X_MSA12))) |
33 | | #endif |
34 | 0 | memset(ctx->A, 0, sizeof(ctx->A)); |
35 | 0 | ctx->bufsz = 0; |
36 | 0 | ctx->xof_state = XOF_STATE_INIT; |
37 | 0 | } |
38 | | |
39 | | int ossl_sha3_init(KECCAK1600_CTX *ctx, unsigned char pad, size_t bitlen) |
40 | 0 | { |
41 | 0 | size_t bsz = SHA3_BLOCKSIZE(bitlen); |
42 | |
|
43 | 0 | if (bsz <= sizeof(ctx->buf)) { |
44 | 0 | ossl_sha3_reset(ctx); |
45 | 0 | ctx->block_size = bsz; |
46 | 0 | ctx->md_size = bitlen / 8; |
47 | 0 | ctx->pad = pad; |
48 | 0 | return 1; |
49 | 0 | } |
50 | | |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | | int ossl_keccak_init(KECCAK1600_CTX *ctx, unsigned char pad, size_t bitlen, size_t mdlen) |
55 | 0 | { |
56 | 0 | int ret = ossl_sha3_init(ctx, pad, bitlen); |
57 | |
|
58 | 0 | if (ret) |
59 | 0 | ctx->md_size = mdlen / 8; |
60 | 0 | return ret; |
61 | 0 | } |
62 | | |
63 | | /* |
64 | | * A buffered absorb function that calls a platform specific absorb |
65 | | * method. |
66 | | */ |
67 | | int ossl_sha3_absorb(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len) |
68 | 0 | { |
69 | 0 | const size_t bsz = ctx->block_size; |
70 | 0 | size_t num, rem; |
71 | |
|
72 | 0 | if (ossl_unlikely(len == 0)) |
73 | 0 | return 1; |
74 | | |
75 | 0 | if (!(ctx->xof_state == XOF_STATE_INIT || ctx->xof_state == XOF_STATE_ABSORB)) |
76 | 0 | return 0; |
77 | | |
78 | | /* Is there anything in the buffer already ? */ |
79 | 0 | if ((num = ctx->bufsz) != 0) { |
80 | | /* Calculate how much space is left in the buffer */ |
81 | 0 | rem = bsz - num; |
82 | | /* If the new input does not fill the buffer then just add it */ |
83 | 0 | if (len < rem) { |
84 | 0 | memcpy(ctx->buf + num, inp, len); |
85 | 0 | ctx->bufsz += len; |
86 | 0 | return 1; |
87 | 0 | } |
88 | | /* otherwise fill up the buffer and absorb the buffer */ |
89 | 0 | memcpy(ctx->buf + num, inp, rem); |
90 | | /* Update the input pointer */ |
91 | 0 | inp += rem; |
92 | 0 | len -= rem; |
93 | 0 | ctx->meth.absorb(ctx, ctx->buf, bsz); |
94 | 0 | ctx->bufsz = 0; |
95 | 0 | ctx->xof_state = XOF_STATE_ABSORB; |
96 | 0 | } |
97 | | /* Absorb the input - rem = leftover part of the input < blocksize) */ |
98 | 0 | rem = ctx->meth.absorb(ctx, inp, len); |
99 | 0 | if (len >= bsz) |
100 | 0 | ctx->xof_state = XOF_STATE_ABSORB; |
101 | | /* Copy the leftover bit of the input into the buffer */ |
102 | 0 | if (ossl_likely(rem > 0)) { |
103 | 0 | memcpy(ctx->buf, inp + len - rem, rem); |
104 | 0 | ctx->bufsz = rem; |
105 | 0 | } |
106 | 0 | return 1; |
107 | 0 | } |
108 | | |
109 | | /* |
110 | | * Call a platform specific final method. |
111 | | * In most cases outlen should be set to ctx->mdlen. |
112 | | * This function assumes the caller has checked outlen is bounded. |
113 | | */ |
114 | | int ossl_sha3_final(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
115 | 0 | { |
116 | 0 | int ret; |
117 | |
|
118 | 0 | if (ctx->xof_state == XOF_STATE_SQUEEZE |
119 | 0 | || ctx->xof_state == XOF_STATE_FINAL) |
120 | 0 | return 0; |
121 | 0 | if (outlen == 0) |
122 | 0 | return 1; |
123 | | |
124 | 0 | ret = ctx->meth.final(ctx, out, outlen); |
125 | 0 | ctx->xof_state = XOF_STATE_FINAL; |
126 | 0 | return ret; |
127 | 0 | } |
128 | | |
129 | | /* Calls a platform specific squeeze method */ |
130 | | int ossl_sha3_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
131 | 0 | { |
132 | 0 | int ret = 0; |
133 | |
|
134 | 0 | if (ctx->xof_state == XOF_STATE_FINAL) |
135 | 0 | return 0; |
136 | 0 | ret = ctx->meth.squeeze(ctx, out, outlen); |
137 | 0 | ctx->xof_state = XOF_STATE_SQUEEZE; |
138 | 0 | return ret; |
139 | 0 | } |
140 | | |
141 | | /* Default version of the absorb() */ |
142 | | size_t ossl_sha3_absorb_default(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len) |
143 | 0 | { |
144 | 0 | return SHA3_absorb(ctx->A, inp, len, ctx->block_size); |
145 | 0 | } |
146 | | |
147 | | /* |
148 | | * Default version of the final() is a single shot method |
149 | | * (Use ossl_sha3_default_squeeze() for multiple calls). |
150 | | */ |
151 | | int ossl_sha3_final_default(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
152 | 0 | { |
153 | 0 | size_t bsz = ctx->block_size; |
154 | 0 | size_t num = ctx->bufsz; |
155 | | |
156 | | /* |
157 | | * Pad the data with 10*1. Note that |num| can be |bsz - 1| |
158 | | * in which case both byte operations below are performed on |
159 | | * same byte... |
160 | | */ |
161 | 0 | memset(ctx->buf + num, 0, bsz - num); |
162 | 0 | ctx->buf[num] = ctx->pad; |
163 | 0 | ctx->buf[bsz - 1] |= 0x80; |
164 | |
|
165 | 0 | (void)SHA3_absorb(ctx->A, ctx->buf, bsz, bsz); |
166 | |
|
167 | 0 | SHA3_squeeze(ctx->A, out, outlen, bsz, 0); |
168 | 0 | return 1; |
169 | 0 | } |
170 | | |
171 | | /* |
172 | | * This method can be called multiple times. |
173 | | * Rather than requiring low-level squeeze functions to buffer partial |
174 | | * blocks, use their existing block-oriented interface. |
175 | | * i.e. Only request multiples of the ctx->block_size from the squeeze |
176 | | * callback. For output length requests smaller than the |
177 | | * ctx->block_size just request a single ctx->block_size bytes and |
178 | | * buffer the results. The next request will use the buffer first |
179 | | * to grab output bytes. |
180 | | */ |
181 | | int ossl_keccak_block_squeeze(KECCAK1600_CTX *ctx, unsigned char *out, |
182 | | size_t outlen, keccak_absorb_fn *absorb, keccak_squeeze_fn *squeeze) |
183 | 0 | { |
184 | 0 | size_t bsz = ctx->block_size; |
185 | 0 | size_t num = ctx->bufsz; |
186 | 0 | size_t len; |
187 | 0 | int next = 1; |
188 | | |
189 | | /* |
190 | | * On the first squeeze call, finish the absorb process, |
191 | | * by adding the trailing padding and then doing |
192 | | * a final absorb. |
193 | | */ |
194 | 0 | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
195 | | /* |
196 | | * Pad the data with 10*1. Note that |num| can be |bsz - 1| |
197 | | * in which case both byte operations below are performed on |
198 | | * same byte... |
199 | | */ |
200 | 0 | memset(ctx->buf + num, 0, bsz - num); |
201 | 0 | ctx->buf[num] = ctx->pad; |
202 | 0 | ctx->buf[bsz - 1] |= 0x80; |
203 | 0 | (void)absorb(ctx->A, ctx->buf, bsz, bsz); |
204 | 0 | num = ctx->bufsz = 0; |
205 | 0 | next = 0; |
206 | 0 | } |
207 | | |
208 | | /* |
209 | | * Step 1. Consume any bytes left over from a previous squeeze |
210 | | * (See Step 4 below). |
211 | | */ |
212 | 0 | if (num != 0) { |
213 | 0 | if (outlen > ctx->bufsz) |
214 | 0 | len = ctx->bufsz; |
215 | 0 | else |
216 | 0 | len = outlen; |
217 | 0 | memcpy(out, ctx->buf + bsz - ctx->bufsz, len); |
218 | 0 | out += len; |
219 | 0 | outlen -= len; |
220 | 0 | ctx->bufsz -= len; |
221 | 0 | } |
222 | 0 | if (outlen == 0) |
223 | 0 | return 1; |
224 | | |
225 | | /* Step 2. Copy full sized squeezed blocks to the output buffer directly */ |
226 | 0 | if (outlen >= bsz) { |
227 | 0 | len = bsz * (outlen / bsz); |
228 | 0 | squeeze(ctx->A, out, len, bsz, next); |
229 | 0 | next = 1; |
230 | 0 | out += len; |
231 | 0 | outlen -= len; |
232 | 0 | } |
233 | 0 | if (outlen > 0) { |
234 | | /* Step 3. Squeeze one more block into a buffer */ |
235 | 0 | squeeze(ctx->A, ctx->buf, bsz, bsz, next); |
236 | 0 | memcpy(out, ctx->buf, outlen); |
237 | | /* Step 4. Remember the leftover part of the squeezed block */ |
238 | 0 | ctx->bufsz = bsz - outlen; |
239 | 0 | } |
240 | 0 | return 1; |
241 | 0 | } |
242 | | |
243 | | int ossl_shake_squeeze_default(KECCAK1600_CTX *ctx, unsigned char *out, |
244 | | size_t outlen) |
245 | 0 | { |
246 | 0 | return ossl_keccak_block_squeeze(ctx, out, outlen, SHA3_absorb, |
247 | 0 | SHA3_squeeze); |
248 | 0 | } |
249 | | |
250 | | static PROV_SHA3_METHOD shake_generic_meth = { |
251 | | ossl_sha3_absorb_default, |
252 | | ossl_sha3_final_default, |
253 | | ossl_shake_squeeze_default |
254 | | }; |
255 | | |
256 | | #if defined(S390_SHA3) |
257 | | |
258 | | /*- |
259 | | * The platform specific parts of the absorb() and final() for S390X. |
260 | | */ |
261 | | static size_t sha3_absorb_s390x(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len) |
262 | | { |
263 | | size_t rem = len % ctx->block_size; |
264 | | unsigned int fc; |
265 | | |
266 | | if (len - rem > 0) { |
267 | | fc = ctx->pad; |
268 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KIMD_NIP : 0; |
269 | | s390x_kimd(inp, len - rem, fc, ctx->A); |
270 | | } |
271 | | return rem; |
272 | | } |
273 | | |
274 | | static int shake_final_s390x(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
275 | | { |
276 | | unsigned int fc; |
277 | | |
278 | | fc = ctx->pad | S390X_KLMD_DUFOP; |
279 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
280 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, fc, ctx->A); |
281 | | return 1; |
282 | | } |
283 | | |
284 | | static int shake_squeeze_s390x(KECCAK1600_CTX *ctx, unsigned char *out, size_t outlen) |
285 | | { |
286 | | unsigned int fc; |
287 | | size_t len; |
288 | | |
289 | | /* |
290 | | * On the first squeeze call, finish the absorb process (incl. padding). |
291 | | */ |
292 | | if (ctx->xof_state != XOF_STATE_SQUEEZE) { |
293 | | fc = ctx->pad; |
294 | | fc |= ctx->xof_state == XOF_STATE_INIT ? S390X_KLMD_NIP : 0; |
295 | | s390x_klmd(ctx->buf, ctx->bufsz, out, outlen, fc, ctx->A); |
296 | | ctx->bufsz = outlen % ctx->block_size; |
297 | | /* reuse ctx->bufsz to count bytes squeezed from current sponge */ |
298 | | return 1; |
299 | | } |
300 | | if (ctx->bufsz != 0) { |
301 | | len = ctx->block_size - ctx->bufsz; |
302 | | if (outlen < len) |
303 | | len = outlen; |
304 | | memcpy(out, (char *)ctx->A + ctx->bufsz, len); |
305 | | out += len; |
306 | | outlen -= len; |
307 | | ctx->bufsz += len; |
308 | | if (ctx->bufsz == ctx->block_size) |
309 | | ctx->bufsz = 0; |
310 | | } |
311 | | if (outlen == 0) |
312 | | return 1; |
313 | | s390x_klmd(NULL, 0, out, outlen, ctx->pad | S390X_KLMD_PS, ctx->A); |
314 | | ctx->bufsz = outlen % ctx->block_size; |
315 | | |
316 | | return 1; |
317 | | } |
318 | | |
319 | | static PROV_SHA3_METHOD shake_s390x_meth = { |
320 | | sha3_absorb_s390x, |
321 | | shake_final_s390x, |
322 | | shake_squeeze_s390x |
323 | | }; |
324 | | #elif defined(__aarch64__) && defined(KECCAK1600_ASM) |
325 | | |
326 | | size_t SHA3_absorb_cext(uint64_t A[5][5], const unsigned char *inp, size_t len, |
327 | | size_t r); |
328 | | /*- |
329 | | * Hardware-assisted ARMv8.2 SHA3 extension version of the absorb() |
330 | | */ |
331 | | static size_t sha3_absorb_arm(KECCAK1600_CTX *ctx, const unsigned char *inp, size_t len) |
332 | | { |
333 | | return SHA3_absorb_cext(ctx->A, inp, len, ctx->block_size); |
334 | | } |
335 | | |
336 | | static PROV_SHA3_METHOD shake_ARMSHA3_meth = { |
337 | | sha3_absorb_arm, |
338 | | ossl_sha3_final_default, |
339 | | ossl_shake_squeeze_default |
340 | | }; |
341 | | #endif |
342 | | |
343 | | KECCAK1600_CTX *ossl_shake256_new(void) |
344 | 0 | { |
345 | 0 | KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
346 | |
|
347 | 0 | if (ctx == NULL) |
348 | 0 | return NULL; |
349 | 0 | ossl_keccak_init(ctx, '\x1f', 256, 0); |
350 | 0 | ctx->md_size = SIZE_MAX; |
351 | 0 | ctx->meth = shake_generic_meth; |
352 | | #if defined(S390_SHA3) |
353 | | if (S390_SHA3_CAPABLE(S390X_SHAKE_256)) { |
354 | | ctx->pad = S390X_SHAKE_256; |
355 | | ctx->meth = shake_s390x_meth; |
356 | | } |
357 | | #elif defined(__aarch64__) && defined(KECCAK1600_ASM) |
358 | | if (OPENSSL_armcap_P & ARMV8_HAVE_SHA3_AND_WORTH_USING) |
359 | | ctx->meth = shake_ARMSHA3_meth; |
360 | | #endif |
361 | 0 | return ctx; |
362 | 0 | } |