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