/src/openssl/crypto/sha/sha3_x4_avx512vl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2026 Intel Corporation. All Rights Reserved. |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | /* |
12 | | * SHAKE x4 multi-buffer implementation for AVX-512VL |
13 | | * |
14 | | * This file provides incremental API wrappers around the AVX-512VL |
15 | | * assembly implementations for processing 4 SHAKE instances in parallel. |
16 | | * |
17 | | * Callers should check SHA3_avx512vl_capable() before calling. |
18 | | */ |
19 | | |
20 | | #include "internal/sha3.h" |
21 | | #include <openssl/crypto.h> |
22 | | #include <string.h> |
23 | | |
24 | | #if defined(KECCAK1600_ASM) \ |
25 | | && (defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)) \ |
26 | | && !defined(OPENSSL_NO_ASM) |
27 | | |
28 | | /* External assembly function declarations */ |
29 | | extern void SHA3_shake128_x4_inc_absorb_avx512vl( |
30 | | uint64_t *state, |
31 | | const void *in0, const void *in1, |
32 | | const void *in2, const void *in3, |
33 | | size_t inlen); |
34 | | |
35 | | extern void SHA3_shake256_x4_inc_absorb_avx512vl( |
36 | | uint64_t *state, |
37 | | const void *in0, const void *in1, |
38 | | const void *in2, const void *in3, |
39 | | size_t inlen); |
40 | | |
41 | | extern void SHA3_shake128_x4_inc_finalize_avx512vl(uint64_t *state); |
42 | | extern void SHA3_shake256_x4_inc_finalize_avx512vl(uint64_t *state); |
43 | | |
44 | | extern void SHA3_shake128_x4_inc_squeeze_avx512vl( |
45 | | void *out0, void *out1, |
46 | | void *out2, void *out3, |
47 | | size_t outlen, |
48 | | uint64_t *state); |
49 | | |
50 | | extern void SHA3_shake256_x4_inc_squeeze_avx512vl( |
51 | | void *out0, void *out1, |
52 | | void *out2, void *out3, |
53 | | size_t outlen, |
54 | | uint64_t *state); |
55 | | |
56 | | /* One-shot assembly function declarations */ |
57 | | extern void SHA3_shake128_x4_avx512vl( |
58 | | void *out0, void *out1, |
59 | | void *out2, void *out3, |
60 | | size_t outlen, |
61 | | const void *in0, const void *in1, |
62 | | const void *in2, const void *in3, |
63 | | size_t inlen); |
64 | | |
65 | | extern void SHA3_shake256_x4_avx512vl( |
66 | | void *out0, void *out1, |
67 | | void *out2, void *out3, |
68 | | size_t outlen, |
69 | | const void *in0, const void *in1, |
70 | | const void *in2, const void *in3, |
71 | | size_t inlen); |
72 | | |
73 | | /* |
74 | | * SHAKE-128 x4 Implementation |
75 | | */ |
76 | | |
77 | | void ossl_sha3_shake128_x4_inc_init_avx512vl(KECCAK1600_X4_AVX512VL_CTX *ctx) |
78 | 0 | { |
79 | 0 | memset(ctx->A, 0, sizeof(ctx->A)); |
80 | 0 | ctx->rate = SHA3_BLOCKSIZE(128); |
81 | 0 | ctx->finalized = 0; |
82 | 0 | } |
83 | | |
84 | | void ossl_sha3_shake128_x4_inc_absorb_avx512vl( |
85 | | KECCAK1600_X4_AVX512VL_CTX *ctx, |
86 | | const void *in0, const void *in1, |
87 | | const void *in2, const void *in3, |
88 | | size_t inlen) |
89 | 0 | { |
90 | 0 | if (ctx->finalized) { |
91 | | /* Error: cannot absorb after finalize */ |
92 | 0 | return; |
93 | 0 | } |
94 | | |
95 | 0 | SHA3_shake128_x4_inc_absorb_avx512vl( |
96 | 0 | ctx->A, in0, in1, in2, in3, inlen); |
97 | 0 | } |
98 | | |
99 | | void ossl_sha3_shake128_x4_inc_cleanup_avx512vl(KECCAK1600_X4_AVX512VL_CTX *ctx) |
100 | 0 | { |
101 | 0 | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
102 | 0 | } |
103 | | |
104 | | static void ossl_sha3_shake128_x4_inc_finalize_avx512vl(KECCAK1600_X4_AVX512VL_CTX *ctx) |
105 | 0 | { |
106 | 0 | if (ctx->finalized) { |
107 | 0 | return; /* Already finalized */ |
108 | 0 | } |
109 | | |
110 | 0 | SHA3_shake128_x4_inc_finalize_avx512vl(ctx->A); |
111 | 0 | ctx->finalized = 1; |
112 | 0 | } |
113 | | |
114 | | void ossl_sha3_shake128_x4_inc_squeeze_avx512vl( |
115 | | void *out0, void *out1, |
116 | | void *out2, void *out3, |
117 | | size_t outlen, |
118 | | KECCAK1600_X4_AVX512VL_CTX *ctx) |
119 | 0 | { |
120 | 0 | if (!ctx->finalized) { |
121 | | /* Auto-finalize on first squeeze */ |
122 | 0 | ossl_sha3_shake128_x4_inc_finalize_avx512vl(ctx); |
123 | 0 | } |
124 | |
|
125 | 0 | SHA3_shake128_x4_inc_squeeze_avx512vl( |
126 | 0 | out0, out1, out2, out3, outlen, ctx->A); |
127 | 0 | } |
128 | | |
129 | | /* |
130 | | * SHAKE-256 x4 Implementation |
131 | | */ |
132 | | |
133 | | void ossl_sha3_shake256_x4_inc_init_avx512vl(KECCAK1600_X4_AVX512VL_CTX *ctx) |
134 | 0 | { |
135 | 0 | memset(ctx->A, 0, sizeof(ctx->A)); |
136 | 0 | ctx->rate = SHA3_BLOCKSIZE(256); |
137 | 0 | ctx->finalized = 0; |
138 | 0 | } |
139 | | |
140 | | void ossl_sha3_shake256_x4_inc_absorb_avx512vl( |
141 | | KECCAK1600_X4_AVX512VL_CTX *ctx, |
142 | | const void *in0, const void *in1, |
143 | | const void *in2, const void *in3, |
144 | | size_t inlen) |
145 | 0 | { |
146 | 0 | if (ctx->finalized) { |
147 | | /* Error: cannot absorb after finalize */ |
148 | 0 | return; |
149 | 0 | } |
150 | | |
151 | 0 | SHA3_shake256_x4_inc_absorb_avx512vl( |
152 | 0 | ctx->A, in0, in1, in2, in3, inlen); |
153 | 0 | } |
154 | | |
155 | | void ossl_sha3_shake256_x4_inc_cleanup_avx512vl(KECCAK1600_X4_AVX512VL_CTX *ctx) |
156 | 0 | { |
157 | 0 | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
158 | 0 | } |
159 | | |
160 | | static void ossl_sha3_shake256_x4_inc_finalize_avx512vl(KECCAK1600_X4_AVX512VL_CTX *ctx) |
161 | 0 | { |
162 | 0 | if (ctx->finalized) { |
163 | 0 | return; /* Already finalized */ |
164 | 0 | } |
165 | | |
166 | 0 | SHA3_shake256_x4_inc_finalize_avx512vl(ctx->A); |
167 | 0 | ctx->finalized = 1; |
168 | 0 | } |
169 | | |
170 | | void ossl_sha3_shake256_x4_inc_squeeze_avx512vl( |
171 | | void *out0, void *out1, |
172 | | void *out2, void *out3, |
173 | | size_t outlen, |
174 | | KECCAK1600_X4_AVX512VL_CTX *ctx) |
175 | 0 | { |
176 | 0 | if (!ctx->finalized) { |
177 | | /* Auto-finalize on first squeeze */ |
178 | 0 | ossl_sha3_shake256_x4_inc_finalize_avx512vl(ctx); |
179 | 0 | } |
180 | |
|
181 | 0 | SHA3_shake256_x4_inc_squeeze_avx512vl( |
182 | 0 | out0, out1, out2, out3, outlen, ctx->A); |
183 | 0 | } |
184 | | |
185 | | /* |
186 | | * Single-call wrapper APIs |
187 | | */ |
188 | | |
189 | | void ossl_sha3_shake128_x4_avx512vl( |
190 | | void *out0, void *out1, |
191 | | void *out2, void *out3, |
192 | | size_t outlen, |
193 | | const void *in0, const void *in1, |
194 | | const void *in2, const void *in3, |
195 | | size_t inlen) |
196 | 0 | { |
197 | 0 | SHA3_shake128_x4_avx512vl(out0, out1, out2, out3, outlen, |
198 | 0 | in0, in1, in2, in3, inlen); |
199 | 0 | } |
200 | | |
201 | | void ossl_sha3_shake256_x4_avx512vl( |
202 | | void *out0, void *out1, |
203 | | void *out2, void *out3, |
204 | | size_t outlen, |
205 | | const void *in0, const void *in1, |
206 | | const void *in2, const void *in3, |
207 | | size_t inlen) |
208 | 0 | { |
209 | 0 | SHA3_shake256_x4_avx512vl(out0, out1, out2, out3, outlen, |
210 | 0 | in0, in1, in2, in3, inlen); |
211 | 0 | } |
212 | | |
213 | | #endif /* KECCAK1600_ASM && x86_64 && !OPENSSL_NO_ASM */ |