]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/common/digests/sha3_prov.c
63a57dbcb321d9bb9cc256b8340470edae5744c7
[thirdparty/openssl.git] / providers / common / digests / sha3_prov.c
1 /*
2 * Copyright 2019 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 <openssl/core_names.h>
11 #include <string.h>
12 #include <openssl/crypto.h>
13 #include <openssl/evp.h>
14 #include <openssl/params.h>
15 #include "internal/sha3.h"
16 #include "internal/core_mkdigest.h"
17 #include "internal/provider_algs.h"
18
19 /*
20 * Forward declaration of any unique methods implemented here. This is not strictly
21 * necessary for the compiler, but provides an assurance that the signatures
22 * of the functions in the dispatch table are correct.
23 */
24 static OSSL_OP_digest_init_fn keccak_init;
25 static OSSL_OP_digest_update_fn keccak_update;
26 static OSSL_OP_digest_final_fn keccak_final;
27 static OSSL_OP_digest_freectx_fn keccak_freectx;
28 static OSSL_OP_digest_dupctx_fn keccak_dupctx;
29 static OSSL_OP_digest_ctx_set_params_fn shake_set_params;
30 static sha3_absorb_fn generic_sha3_absorb;
31 static sha3_final_fn generic_sha3_final;
32
33 #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM)
34 /*
35 * IBM S390X support
36 */
37 # include "s390x_arch.h"
38 # define S390_SHA3 1
39 # define S390_SHA3_CAPABLE(name) \
40 ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && \
41 (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name)))
42
43 #endif
44
45 static int keccak_init(void *vctx)
46 {
47 /* The newctx() handles most of the ctx fixed setup. */
48 sha3_reset((KECCAK1600_CTX *)vctx);
49 return 1;
50 }
51
52 static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
53 {
54 KECCAK1600_CTX *ctx = vctx;
55 const size_t bsz = ctx->block_size;
56 size_t num, rem;
57
58 if (len == 0)
59 return 1;
60
61 /* Is there anything in the buffer already ? */
62 if ((num = ctx->bufsz) != 0) {
63 /* Calculate how much space is left in the buffer */
64 rem = bsz - num;
65 /* If the new input does not fill the buffer then just add it */
66 if (len < rem) {
67 memcpy(ctx->buf + num, inp, len);
68 ctx->bufsz += len;
69 return 1;
70 }
71 /* otherwise fill up the buffer and absorb the buffer */
72 memcpy(ctx->buf + num, inp, rem);
73 /* Update the input pointer */
74 inp += rem;
75 len -= rem;
76 ctx->meth.absorb(ctx, ctx->buf, bsz);
77 ctx->bufsz = 0;
78 }
79 /* Absorb the input - rem = leftover part of the input < blocksize) */
80 rem = ctx->meth.absorb(ctx, inp, len);
81 /* Copy the leftover bit of the input into the buffer */
82 if (rem) {
83 memcpy(ctx->buf, inp + len - rem, rem);
84 ctx->bufsz = rem;
85 }
86 return 1;
87 }
88
89 static int keccak_final(void *vctx, unsigned char *out, size_t *outl,
90 size_t outsz)
91 {
92 int ret;
93 KECCAK1600_CTX *ctx = vctx;
94
95 ret = ctx->meth.final(out, ctx);
96 *outl = ctx->md_size;
97 return ret;
98 }
99
100 /*-
101 * Generic software version of the absorb() and final().
102 */
103 static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len)
104 {
105 KECCAK1600_CTX *ctx = vctx;
106
107 return SHA3_absorb(ctx->A, inp, len, ctx->block_size);
108 }
109
110 static int generic_sha3_final(unsigned char *md, void *vctx)
111 {
112 return sha3_final(md, (KECCAK1600_CTX *)vctx);
113 }
114
115 static PROV_SHA3_METHOD sha3_generic_md =
116 {
117 generic_sha3_absorb,
118 generic_sha3_final
119 };
120
121 #if defined(S390_SHA3)
122
123 static sha3_absorb_fn s390x_sha3_absorb;
124 static sha3_final_fn s390x_sha3_final;
125 static sha3_final_fn s390x_shake_final;
126
127 /*-
128 * The platform specific parts of the absorb() and final() for S390X.
129 */
130 static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len)
131 {
132 KECCAK1600_CTX *ctx = vctx;
133 size_t rem = len % ctx->block_size;
134
135 s390x_kimd(inp, len - rem, ctx->pad, ctx->A);
136 return rem;
137 }
138
139 static int s390x_sha3_final(unsigned char *md, void *vctx)
140 {
141 KECCAK1600_CTX *ctx = vctx;
142
143 s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, ctx->pad, ctx->A);
144 memcpy(md, ctx->A, ctx->md_size);
145 return 1;
146 }
147
148 static int s390x_shake_final(unsigned char *md, void *vctx)
149 {
150 KECCAK1600_CTX *ctx = vctx;
151
152 s390x_klmd(ctx->buf, ctx->bufsz, md, ctx->md_size, ctx->pad, ctx->A);
153 return 1;
154 }
155
156 static PROV_SHA3_METHOD sha3_s390x_md =
157 {
158 s390x_sha3_absorb,
159 s390x_sha3_final
160 };
161
162 static PROV_SHA3_METHOD shake_s390x_md =
163 {
164 s390x_sha3_absorb,
165 s390x_shake_final
166 };
167
168 # define SHA3_SET_MD(uname, typ) \
169 if (S390_SHA3_CAPABLE(uname)) { \
170 ctx->pad = S390X_##uname; \
171 ctx->meth = typ##_s390x_md; \
172 } else { \
173 ctx->meth = sha3_generic_md; \
174 }
175 #else
176 # define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md;
177 #endif /* S390_SHA3 */
178
179 #define SHA3_newctx(typ, uname, name, bitlen, pad) \
180 static OSSL_OP_digest_newctx_fn name##_newctx; \
181 static void *name##_newctx(void *provctx) \
182 { \
183 KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
184 \
185 if (ctx == NULL) \
186 return NULL; \
187 sha3_init(ctx, pad, bitlen); \
188 SHA3_SET_MD(uname, typ) \
189 return ctx; \
190 }
191
192 #define KMAC_newctx(uname, bitlen, pad) \
193 static OSSL_OP_digest_newctx_fn uname##_newctx; \
194 static void *uname##_newctx(void *provctx) \
195 { \
196 KECCAK1600_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); \
197 \
198 if (ctx == NULL) \
199 return NULL; \
200 keccak_kmac_init(ctx, pad, bitlen); \
201 ctx->meth = sha3_generic_md; \
202 return ctx; \
203 }
204
205 #define OSSL_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags, \
206 stparams) \
207 static OSSL_OP_digest_get_params_fn name##_get_params; \
208 static int name##_get_params(OSSL_PARAM params[]) \
209 { \
210 OSSL_PARAM *p = NULL; \
211 \
212 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_BLOCK_SIZE); \
213 if (p != NULL && !OSSL_PARAM_set_int(p, (blksize))) \
214 return 0; \
215 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE); \
216 if (p != NULL && !OSSL_PARAM_set_int(p, (dgstsize))) \
217 return 0; \
218 p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_FLAGS); \
219 if (p != NULL && !OSSL_PARAM_set_ulong(p, (flags))) \
220 return 0; \
221 return 1; \
222 } \
223 const OSSL_DISPATCH name##_functions[] = { \
224 { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx }, \
225 { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init }, \
226 { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update }, \
227 { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final }, \
228 { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx }, \
229 { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx }, \
230 { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
231 { OSSL_FUNC_DIGEST_CTX_SET_PARAMS, (void (*)(void))stparams }, \
232 OSSL_FUNC_DIGEST_CONSTRUCT_END
233
234 static void keccak_freectx(void *vctx)
235 {
236 KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
237
238 OPENSSL_clear_free(ctx, sizeof(*ctx));
239 }
240
241 static void *keccak_dupctx(void *ctx)
242 {
243 KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx;
244 KECCAK1600_CTX *ret = OPENSSL_malloc(sizeof(*ret));
245
246 *ret = *in;
247 return ret;
248 }
249
250 static int shake_set_params(void *vctx, const OSSL_PARAM params[])
251 {
252 const OSSL_PARAM *p;
253 KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
254
255 if (ctx != NULL && params != NULL) {
256 p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN);
257 if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size))
258 return 0;
259 return 1;
260 }
261 return 0; /* Null Parameter */
262 }
263
264 #define SHA3(bitlen) \
265 SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06') \
266 OSSL_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen, \
267 SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
268 EVP_MD_FLAG_DIGALGID_ABSENT, NULL)
269
270 #define SHAKE(bitlen) \
271 SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f') \
272 OSSL_FUNC_SHA3_DIGEST(shake_##bitlen, bitlen, \
273 SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen), \
274 EVP_MD_FLAG_XOF, shake_set_params)
275 #define KMAC(bitlen) \
276 KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04') \
277 OSSL_FUNC_SHA3_DIGEST(keccak_kmac_##bitlen, bitlen, \
278 SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen), \
279 EVP_MD_FLAG_XOF, shake_set_params)
280
281 SHA3(224)
282 SHA3(256)
283 SHA3(384)
284 SHA3(512)
285 SHAKE(128)
286 SHAKE(256)
287 KMAC(128)
288 KMAC(256)