Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/hmac/hmac_s390x.c

raw
1 /*
2 * Copyright 2024-2025 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 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include "crypto/s390x_arch.h"
14 #include "hmac_local.h"
15 #include "openssl/obj_mac.h"
16 #include "openssl/evp.h"
17 #include "openssl/err.h"
18 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
19 #include <openssl/engine.h>
20 #endif
21
22 #ifdef OPENSSL_HMAC_S390X
23
24 static int s390x_fc_from_md(const EVP_MD *md)
25 {
26 int fc;
27
28 if (EVP_MD_is_a(md, "SHA2-224"))
29 fc = S390X_HMAC_SHA_224;
30 else if (EVP_MD_is_a(md, "SHA2-256"))
31 fc = S390X_HMAC_SHA_256;
32 else if (EVP_MD_is_a(md, "SHA2-384"))
33 fc = S390X_HMAC_SHA_384;
34 else if (EVP_MD_is_a(md, "SHA2-512"))
35 fc = S390X_HMAC_SHA_512;
36 else
37 return 0;
38
39 if ((OPENSSL_s390xcap_P.kmac[1] & S390X_CAPBIT(fc)) == 0)
40 return 0;
41
42 return fc;
43 }
44
45 static void s390x_call_kmac(HMAC_CTX *ctx, const unsigned char *in, size_t len)
46 {
47 unsigned int fc = ctx->plat.s390x.fc;
48
49 if (ctx->plat.s390x.ikp)
50 fc |= S390X_KMAC_IKP;
51
52 if (ctx->plat.s390x.iimp)
53 fc |= S390X_KMAC_IIMP;
54
55 switch (ctx->plat.s390x.fc) {
56 case S390X_HMAC_SHA_224:
57 case S390X_HMAC_SHA_256:
58 ctx->plat.s390x.param.hmac_224_256.imbl += ((uint64_t)len * 8);
59 break;
60 case S390X_HMAC_SHA_384:
61 case S390X_HMAC_SHA_512:
62 ctx->plat.s390x.param.hmac_384_512.imbl += ((uint128_t)len * 8);
63 break;
64 default:
65 break;
66 }
67
68 s390x_kmac(in, len, fc, &ctx->plat.s390x.param);
69
70 ctx->plat.s390x.ikp = 1;
71 }
72
73 static int s390x_check_engine_used(const EVP_MD *md, ENGINE *impl)
74 {
75 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
76 const EVP_MD *d;
77
78 if (impl != NULL) {
79 if (!ENGINE_init(impl))
80 return 0;
81 } else {
82 impl = ENGINE_get_digest_engine(EVP_MD_get_type(md));
83 }
84
85 if (impl == NULL)
86 return 0;
87
88 d = ENGINE_get_digest(impl, EVP_MD_get_type(md));
89 ENGINE_finish(impl);
90
91 if (d != NULL)
92 return 1;
93 #endif
94
95 return 0;
96 }
97
98 int s390x_HMAC_init(HMAC_CTX *ctx, const void *key, int key_len, ENGINE *impl)
99 {
100 unsigned char *key_param;
101 unsigned int key_param_len;
102
103 ctx->plat.s390x.fc = s390x_fc_from_md(ctx->md);
104 if (ctx->plat.s390x.fc == 0)
105 return -1; /* Not supported by kmac instruction */
106
107 if (s390x_check_engine_used(ctx->md, impl)) {
108 ctx->plat.s390x.fc = 0;
109 return -1; /* An engine handles the digest, disable acceleration */
110 }
111
112 ctx->plat.s390x.blk_size = EVP_MD_get_block_size(ctx->md);
113 if (ctx->plat.s390x.blk_size < 0)
114 return 0;
115
116 if (ctx->plat.s390x.size != (size_t)(ctx->plat.s390x.blk_size * HMAC_S390X_BUF_NUM_BLOCKS)) {
117 OPENSSL_clear_free(ctx->plat.s390x.buf, ctx->plat.s390x.size);
118 ctx->plat.s390x.size = 0;
119 ctx->plat.s390x.buf = OPENSSL_calloc(HMAC_S390X_BUF_NUM_BLOCKS,
120 ctx->plat.s390x.blk_size);
121 if (ctx->plat.s390x.buf == NULL)
122 return 0;
123 ctx->plat.s390x.size = ctx->plat.s390x.blk_size * HMAC_S390X_BUF_NUM_BLOCKS;
124 }
125 ctx->plat.s390x.num = 0;
126
127 ctx->plat.s390x.ikp = 0;
128 ctx->plat.s390x.iimp = 1;
129
130 switch (ctx->plat.s390x.fc) {
131 case S390X_HMAC_SHA_224:
132 case S390X_HMAC_SHA_256:
133 ctx->plat.s390x.param.hmac_224_256.imbl = 0;
134 OPENSSL_cleanse(ctx->plat.s390x.param.hmac_224_256.h,
135 sizeof(ctx->plat.s390x.param.hmac_224_256.h));
136 break;
137 case S390X_HMAC_SHA_384:
138 case S390X_HMAC_SHA_512:
139 ctx->plat.s390x.param.hmac_384_512.imbl = 0;
140 OPENSSL_cleanse(ctx->plat.s390x.param.hmac_384_512.h,
141 sizeof(ctx->plat.s390x.param.hmac_384_512.h));
142 break;
143 default:
144 return 0;
145 }
146
147 if (key != NULL) {
148 switch (ctx->plat.s390x.fc) {
149 case S390X_HMAC_SHA_224:
150 case S390X_HMAC_SHA_256:
151 OPENSSL_cleanse(&ctx->plat.s390x.param.hmac_224_256.key,
152 sizeof(ctx->plat.s390x.param.hmac_224_256.key));
153 key_param = ctx->plat.s390x.param.hmac_224_256.key;
154 key_param_len = sizeof(ctx->plat.s390x.param.hmac_224_256.key);
155 break;
156 case S390X_HMAC_SHA_384:
157 case S390X_HMAC_SHA_512:
158 OPENSSL_cleanse(&ctx->plat.s390x.param.hmac_384_512.key,
159 sizeof(ctx->plat.s390x.param.hmac_384_512.key));
160 key_param = ctx->plat.s390x.param.hmac_384_512.key;
161 key_param_len = sizeof(ctx->plat.s390x.param.hmac_384_512.key);
162 break;
163 default:
164 return 0;
165 }
166
167 if (!ossl_assert(ctx->plat.s390x.blk_size <= (int)key_param_len))
168 return 0;
169
170 if (key_len > ctx->plat.s390x.blk_size) {
171 if (!EVP_DigestInit_ex(ctx->md_ctx, ctx->md, impl)
172 || !EVP_DigestUpdate(ctx->md_ctx, key, key_len)
173 || !EVP_DigestFinal_ex(ctx->md_ctx, key_param,
174 &key_param_len))
175 return 0;
176 } else {
177 if (key_len < 0 || key_len > (int)key_param_len)
178 return 0;
179 memcpy(key_param, key, key_len);
180 /* remaining key bytes already zeroed out above */
181 }
182 }
183
184 return 1;
185 }
186
187 int s390x_HMAC_update(HMAC_CTX *ctx, const unsigned char *data, size_t len)
188 {
189 size_t remain, num;
190
191 if (ctx->plat.s390x.iimp != 1) {
192 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
193 return 0;
194 }
195
196 if (len == 0)
197 return 1;
198
199 /* buffer is full, process it now */
200 if (ctx->plat.s390x.num == ctx->plat.s390x.size) {
201 s390x_call_kmac(ctx, ctx->plat.s390x.buf, ctx->plat.s390x.num);
202
203 ctx->plat.s390x.num = 0;
204 }
205
206 remain = ctx->plat.s390x.size - ctx->plat.s390x.num;
207 if (len > remain) {
208 /* data does not fit into buffer */
209 if (ctx->plat.s390x.num > 0) {
210 /* first fill buffer and process it */
211 memcpy(&ctx->plat.s390x.buf[ctx->plat.s390x.num], data, remain);
212 ctx->plat.s390x.num += remain;
213
214 s390x_call_kmac(ctx, ctx->plat.s390x.buf, ctx->plat.s390x.num);
215
216 ctx->plat.s390x.num = 0;
217
218 data += remain;
219 len -= remain;
220 }
221
222 if (!ossl_assert(ctx->plat.s390x.num == 0))
223 return 0;
224
225 if (len > ctx->plat.s390x.size) {
226 /*
227 * remaining data is still larger than buffer, process remaining
228 * full blocks of input directly
229 */
230 remain = len % ctx->plat.s390x.blk_size;
231 num = len - remain;
232
233 s390x_call_kmac(ctx, data, num);
234
235 data += num;
236 len -= num;
237 }
238 }
239
240 /* add remaining input data (which is < buffer size) to buffer */
241 if (!ossl_assert(len <= ctx->plat.s390x.size))
242 return 0;
243
244 if (len > 0) {
245 memcpy(&ctx->plat.s390x.buf[ctx->plat.s390x.num], data, len);
246 ctx->plat.s390x.num += len;
247 }
248
249 return 1;
250 }
251
252 int s390x_HMAC_final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len)
253 {
254 void *result;
255 unsigned int res_len;
256
257 if (ctx->plat.s390x.iimp != 1) {
258 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
259 return 0;
260 }
261
262 ctx->plat.s390x.iimp = 0; /* last block */
263 s390x_call_kmac(ctx, ctx->plat.s390x.buf, ctx->plat.s390x.num);
264
265 ctx->plat.s390x.num = 0;
266
267 switch (ctx->plat.s390x.fc) {
268 case S390X_HMAC_SHA_224:
269 result = &ctx->plat.s390x.param.hmac_224_256.h[0];
270 res_len = SHA224_DIGEST_LENGTH;
271 break;
272 case S390X_HMAC_SHA_256:
273 result = &ctx->plat.s390x.param.hmac_224_256.h[0];
274 res_len = SHA256_DIGEST_LENGTH;
275 break;
276 case S390X_HMAC_SHA_384:
277 result = &ctx->plat.s390x.param.hmac_384_512.h[0];
278 res_len = SHA384_DIGEST_LENGTH;
279 break;
280 case S390X_HMAC_SHA_512:
281 result = &ctx->plat.s390x.param.hmac_384_512.h[0];
282 res_len = SHA512_DIGEST_LENGTH;
283 break;
284 default:
285 return 0;
286 }
287
288 memcpy(md, result, res_len);
289 if (len != NULL)
290 *len = res_len;
291
292 return 1;
293 }
294
295 int s390x_HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx)
296 {
297 dctx->plat.s390x.fc = sctx->plat.s390x.fc;
298 dctx->plat.s390x.blk_size = sctx->plat.s390x.blk_size;
299 dctx->plat.s390x.ikp = sctx->plat.s390x.ikp;
300 dctx->plat.s390x.iimp = sctx->plat.s390x.iimp;
301
302 memcpy(&dctx->plat.s390x.param, &sctx->plat.s390x.param,
303 sizeof(dctx->plat.s390x.param));
304
305 OPENSSL_clear_free(dctx->plat.s390x.buf, dctx->plat.s390x.size);
306 dctx->plat.s390x.buf = NULL;
307 if (sctx->plat.s390x.buf != NULL) {
308 dctx->plat.s390x.buf = OPENSSL_memdup(sctx->plat.s390x.buf,
309 sctx->plat.s390x.size);
310 if (dctx->plat.s390x.buf == NULL)
311 return 0;
312 }
313
314 dctx->plat.s390x.size = sctx->plat.s390x.size;
315 dctx->plat.s390x.num = sctx->plat.s390x.num;
316
317 return 1;
318 }
319
320 int s390x_HMAC_CTX_cleanup(HMAC_CTX *ctx)
321 {
322 OPENSSL_clear_free(ctx->plat.s390x.buf, ctx->plat.s390x.size);
323 ctx->plat.s390x.buf = NULL;
324 ctx->plat.s390x.size = 0;
325 ctx->plat.s390x.num = 0;
326
327 OPENSSL_cleanse(&ctx->plat.s390x.param, sizeof(ctx->plat.s390x.param));
328
329 ctx->plat.s390x.blk_size = 0;
330 ctx->plat.s390x.ikp = 0;
331 ctx->plat.s390x.iimp = 1;
332
333 ctx->plat.s390x.fc = 0;
334
335 return 1;
336 }
337
338 #endif