prov: update digests to support modified ctx params
[openssl.git] / providers / implementations / digests / sha3_prov.c
1 /*
2  * Copyright 2019-2021 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 <openssl/core_names.h>
12 #include <openssl/crypto.h>
13 #include <openssl/evp.h>
14 #include <openssl/params.h>
15 #include <openssl/err.h>
16 #include <openssl/proverr.h>
17 #include "internal/sha3.h"
18 #include "prov/digestcommon.h"
19 #include "prov/implementations.h"
20
21 #define SHA3_FLAGS PROV_DIGEST_FLAG_ALGID_ABSENT
22 #define SHAKE_FLAGS PROV_DIGEST_FLAG_XOF
23 #define KMAC_FLAGS PROV_DIGEST_FLAG_XOF
24
25 /*
26  * Forward declaration of any unique methods implemented here. This is not strictly
27  * necessary for the compiler, but provides an assurance that the signatures
28  * of the functions in the dispatch table are correct.
29  */
30 static OSSL_FUNC_digest_init_fn keccak_init;
31 static OSSL_FUNC_digest_update_fn keccak_update;
32 static OSSL_FUNC_digest_final_fn keccak_final;
33 static OSSL_FUNC_digest_freectx_fn keccak_freectx;
34 static OSSL_FUNC_digest_dupctx_fn keccak_dupctx;
35 static OSSL_FUNC_digest_set_ctx_params_fn shake_set_ctx_params;
36 static OSSL_FUNC_digest_settable_ctx_params_fn shake_settable_ctx_params;
37 static sha3_absorb_fn generic_sha3_absorb;
38 static sha3_final_fn generic_sha3_final;
39
40 #if defined(OPENSSL_CPUID_OBJ) && defined(__s390__) && defined(KECCAK1600_ASM)
41 /*
42  * IBM S390X support
43  */
44 # include "s390x_arch.h"
45 # define S390_SHA3 1
46 # define S390_SHA3_CAPABLE(name) \
47     ((OPENSSL_s390xcap_P.kimd[0] & S390X_CAPBIT(S390X_##name)) && \
48      (OPENSSL_s390xcap_P.klmd[0] & S390X_CAPBIT(S390X_##name)))
49
50 #endif
51
52 static int keccak_init(void *vctx)
53 {
54     if (!ossl_prov_is_running())
55         return 0;
56     /* The newctx() handles most of the ctx fixed setup. */
57     ossl_sha3_reset((KECCAK1600_CTX *)vctx);
58     return 1;
59 }
60
61 static int keccak_update(void *vctx, const unsigned char *inp, size_t len)
62 {
63     KECCAK1600_CTX *ctx = vctx;
64     const size_t bsz = ctx->block_size;
65     size_t num, rem;
66
67     if (len == 0)
68         return 1;
69
70     /* Is there anything in the buffer already ? */
71     if ((num = ctx->bufsz) != 0) {
72         /* Calculate how much space is left in the buffer */
73         rem = bsz - num;
74         /* If the new input does not fill the buffer then just add it */
75         if (len < rem) {
76             memcpy(ctx->buf + num, inp, len);
77             ctx->bufsz += len;
78             return 1;
79         }
80         /* otherwise fill up the buffer and absorb the buffer */
81         memcpy(ctx->buf + num, inp, rem);
82         /* Update the input pointer */
83         inp += rem;
84         len -= rem;
85         ctx->meth.absorb(ctx, ctx->buf, bsz);
86         ctx->bufsz = 0;
87     }
88     /* Absorb the input - rem = leftover part of the input < blocksize) */
89     rem = ctx->meth.absorb(ctx, inp, len);
90     /* Copy the leftover bit of the input into the buffer */
91     if (rem) {
92         memcpy(ctx->buf, inp + len - rem, rem);
93         ctx->bufsz = rem;
94     }
95     return 1;
96 }
97
98 static int keccak_final(void *vctx, unsigned char *out, size_t *outl,
99                         size_t outsz)
100 {
101     int ret = 1;
102     KECCAK1600_CTX *ctx = vctx;
103
104     if (!ossl_prov_is_running())
105         return 0;
106     if (outsz > 0)
107         ret = ctx->meth.final(out, ctx);
108
109     *outl = ctx->md_size;
110     return ret;
111 }
112
113 /*-
114  * Generic software version of the absorb() and final().
115  */
116 static size_t generic_sha3_absorb(void *vctx, const void *inp, size_t len)
117 {
118     KECCAK1600_CTX *ctx = vctx;
119
120     return SHA3_absorb(ctx->A, inp, len, ctx->block_size);
121 }
122
123 static int generic_sha3_final(unsigned char *md, void *vctx)
124 {
125     return ossl_sha3_final(md, (KECCAK1600_CTX *)vctx);
126 }
127
128 static PROV_SHA3_METHOD sha3_generic_md =
129 {
130     generic_sha3_absorb,
131     generic_sha3_final
132 };
133
134 #if defined(S390_SHA3)
135
136 static sha3_absorb_fn s390x_sha3_absorb;
137 static sha3_final_fn s390x_sha3_final;
138 static sha3_final_fn s390x_shake_final;
139
140 /*-
141  * The platform specific parts of the absorb() and final() for S390X.
142  */
143 static size_t s390x_sha3_absorb(void *vctx, const void *inp, size_t len)
144 {
145     KECCAK1600_CTX *ctx = vctx;
146     size_t rem = len % ctx->block_size;
147
148     s390x_kimd(inp, len - rem, ctx->pad, ctx->A);
149     return rem;
150 }
151
152 static int s390x_sha3_final(unsigned char *md, void *vctx)
153 {
154     KECCAK1600_CTX *ctx = vctx;
155
156     if (!ossl_prov_is_running())
157         return 0;
158     s390x_klmd(ctx->buf, ctx->bufsz, NULL, 0, ctx->pad, ctx->A);
159     memcpy(md, ctx->A, ctx->md_size);
160     return 1;
161 }
162
163 static int s390x_shake_final(unsigned char *md, void *vctx)
164 {
165     KECCAK1600_CTX *ctx = vctx;
166
167     if (!ossl_prov_is_running())
168         return 0;
169     s390x_klmd(ctx->buf, ctx->bufsz, md, ctx->md_size, ctx->pad, ctx->A);
170     return 1;
171 }
172
173 static PROV_SHA3_METHOD sha3_s390x_md =
174 {
175     s390x_sha3_absorb,
176     s390x_sha3_final
177 };
178
179 static PROV_SHA3_METHOD shake_s390x_md =
180 {
181     s390x_sha3_absorb,
182     s390x_shake_final
183 };
184
185 # define SHA3_SET_MD(uname, typ)                                               \
186     if (S390_SHA3_CAPABLE(uname)) {                                            \
187         ctx->pad = S390X_##uname;                                              \
188         ctx->meth = typ##_s390x_md;                                            \
189     } else {                                                                   \
190         ctx->meth = sha3_generic_md;                                           \
191     }
192 #else
193 # define SHA3_SET_MD(uname, typ) ctx->meth = sha3_generic_md;
194 #endif /* S390_SHA3 */
195
196 #define SHA3_newctx(typ, uname, name, bitlen, pad)                             \
197 static OSSL_FUNC_digest_newctx_fn name##_newctx;                               \
198 static void *name##_newctx(void *provctx)                                      \
199 {                                                                              \
200     KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \
201                                                 : NULL;                        \
202                                                                                \
203     if (ctx == NULL)                                                           \
204         return NULL;                                                           \
205     ossl_sha3_init(ctx, pad, bitlen);                                          \
206     SHA3_SET_MD(uname, typ)                                                    \
207     return ctx;                                                                \
208 }
209
210 #define KMAC_newctx(uname, bitlen, pad)                                        \
211 static OSSL_FUNC_digest_newctx_fn uname##_newctx;                              \
212 static void *uname##_newctx(void *provctx)                                     \
213 {                                                                              \
214     KECCAK1600_CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) \
215                                                 : NULL;                        \
216                                                                                \
217     if (ctx == NULL)                                                           \
218         return NULL;                                                           \
219     ossl_keccak_kmac_init(ctx, pad, bitlen);                                   \
220     ctx->meth = sha3_generic_md;                                               \
221     return ctx;                                                                \
222 }
223
224 #define PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags)   \
225 PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags)                     \
226 const OSSL_DISPATCH ossl_##name##_functions[] = {                              \
227     { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx },                \
228     { OSSL_FUNC_DIGEST_INIT, (void (*)(void))keccak_init },                    \
229     { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))keccak_update },                \
230     { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))keccak_final },                  \
231     { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))keccak_freectx },              \
232     { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))keccak_dupctx },                \
233     PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
234
235 #define PROV_FUNC_SHA3_DIGEST(name, bitlen, blksize, dgstsize, flags)          \
236     PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags),      \
237     PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
238
239 #define PROV_FUNC_SHAKE_DIGEST(name, bitlen, blksize, dgstsize, flags)         \
240     PROV_FUNC_SHA3_DIGEST_COMMON(name, bitlen, blksize, dgstsize, flags),      \
241     { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))shake_set_ctx_params }, \
242     { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS,                                    \
243      (void (*)(void))shake_settable_ctx_params },                              \
244     PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
245
246 static void keccak_freectx(void *vctx)
247 {
248     KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
249
250     OPENSSL_clear_free(ctx,  sizeof(*ctx));
251 }
252
253 static void *keccak_dupctx(void *ctx)
254 {
255     KECCAK1600_CTX *in = (KECCAK1600_CTX *)ctx;
256     KECCAK1600_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret))
257                                                 : NULL;
258
259     if (ret != NULL)
260         *ret = *in;
261     return ret;
262 }
263
264 static const OSSL_PARAM known_shake_settable_ctx_params[] = {
265     {OSSL_DIGEST_PARAM_XOFLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
266     OSSL_PARAM_END
267 };
268 static const OSSL_PARAM *shake_settable_ctx_params(ossl_unused void *ctx,
269                                                    ossl_unused void *provctx)
270 {
271     return known_shake_settable_ctx_params;
272 }
273
274 static int shake_set_ctx_params(void *vctx, const OSSL_PARAM params[])
275 {
276     const OSSL_PARAM *p;
277     KECCAK1600_CTX *ctx = (KECCAK1600_CTX *)vctx;
278
279     if (ctx != NULL && params != NULL) {
280         p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_XOFLEN);
281         if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->md_size)) {
282             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
283             return 0;
284         }
285         return 1;
286     }
287     return 0; /* Null Parameter */
288 }
289
290 #define IMPLEMENT_SHA3_functions(bitlen)                                       \
291     SHA3_newctx(sha3, SHA3_##bitlen, sha3_##bitlen, bitlen, '\x06')            \
292     PROV_FUNC_SHA3_DIGEST(sha3_##bitlen, bitlen,                               \
293                           SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen),         \
294                           SHA3_FLAGS)
295
296 #define IMPLEMENT_SHAKE_functions(bitlen)                                      \
297     SHA3_newctx(shake, SHAKE_##bitlen, shake_##bitlen, bitlen, '\x1f')         \
298     PROV_FUNC_SHAKE_DIGEST(shake_##bitlen, bitlen,                             \
299                           SHA3_BLOCKSIZE(bitlen), SHA3_MDSIZE(bitlen),         \
300                           SHAKE_FLAGS)
301 #define IMPLEMENT_KMAC_functions(bitlen)                                       \
302     KMAC_newctx(keccak_kmac_##bitlen, bitlen, '\x04')                          \
303     PROV_FUNC_SHAKE_DIGEST(keccak_kmac_##bitlen, bitlen,                       \
304                            SHA3_BLOCKSIZE(bitlen), KMAC_MDSIZE(bitlen),        \
305                            KMAC_FLAGS)
306
307 /* ossl_sha3_224_functions */
308 IMPLEMENT_SHA3_functions(224)
309 /* ossl_sha3_256_functions */
310 IMPLEMENT_SHA3_functions(256)
311 /* ossl_sha3_384_functions */
312 IMPLEMENT_SHA3_functions(384)
313 /* ossl_sha3_512_functions */
314 IMPLEMENT_SHA3_functions(512)
315 /* ossl_shake_128_functions */
316 IMPLEMENT_SHAKE_functions(128)
317 /* ossl_shake_256_functions */
318 IMPLEMENT_SHAKE_functions(256)
319 /* ossl_keccak_kmac_128_functions */
320 IMPLEMENT_KMAC_functions(128)
321 /* ossl_keccak_kmac_256_functions */
322 IMPLEMENT_KMAC_functions(256)