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