Coverage Report

Created: 2025-08-28 07:07

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