Coverage Report

Created: 2026-11-16 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/providers/implementations/digests/kt_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2026 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_dispatch.h>
12
#include <openssl/core_names.h>
13
#include <openssl/crypto.h>
14
#include <openssl/err.h>
15
#include <openssl/params.h>
16
#include <openssl/proverr.h>
17
#include "internal/common.h"
18
#include "internal/numbers.h" /* includes SIZE_MAX */
19
#include "internal/sha3.h"
20
#include "prov/digestcommon.h"
21
#include "prov/implementations.h"
22
#include "turboshake_prov.h"
23
24
#define KT_FLAGS (PROV_DIGEST_FLAG_XOF | PROV_DIGEST_FLAG_ALGID_ABSENT)
25
26
0
#define KT_CHUNK_SIZE 8192
27
0
#define KT_MAX_CUSTOM_STRING 512
28
0
#define KT_DOMAIN_FINAL_NODE 0x06
29
0
#define KT_DOMAIN_SINGLE_NODE 0x07
30
0
#define KT_DOMAIN_INTERMEDIATE_NODE 0x0b
31
32
#include "providers/implementations/digests/kt_prov.inc"
33
34
typedef struct kt_ctx_st {
35
    size_t bitlen;
36
    size_t xoflen;
37
    size_t cvlen;
38
    unsigned char *custom;
39
    size_t custom_len;
40
    KECCAK1600_CTX root;
41
    KECCAK1600_CTX leaf;
42
    size_t chunk_len;
43
    size_t cv_count;
44
    int tree_started;
45
    int finalized;
46
} KT_CTX;
47
48
static OSSL_FUNC_digest_init_fn kt_init;
49
static OSSL_FUNC_digest_update_fn kt_update;
50
static OSSL_FUNC_digest_final_fn kt_final;
51
static OSSL_FUNC_digest_squeeze_fn kt_squeeze;
52
static OSSL_FUNC_digest_freectx_fn kt_freectx;
53
static OSSL_FUNC_digest_dupctx_fn kt_dupctx;
54
static OSSL_FUNC_digest_get_ctx_params_fn kt_get_ctx_params;
55
static OSSL_FUNC_digest_gettable_ctx_params_fn kt_gettable_ctx_params;
56
static OSSL_FUNC_digest_set_ctx_params_fn kt_set_ctx_params;
57
static OSSL_FUNC_digest_settable_ctx_params_fn kt_settable_ctx_params;
58
59
/*
60
 * RFC 9861 length_encode() is not SP800-185 right_encode(): in particular,
61
 * length_encode(0) is 00, not 00 01.
62
 */
63
static size_t kt_length_encode(size_t x, unsigned char *out)
64
0
{
65
0
    size_t n = 0, t = x, i;
66
67
0
    while (t > 0) {
68
0
        n++;
69
0
        t >>= 8;
70
0
    }
71
0
    for (i = 0; i < n; i++)
72
0
        out[n - i - 1] = (unsigned char)(x >> (8 * i));
73
0
    out[n] = (unsigned char)n;
74
0
    return n + 1;
75
0
}
76
77
static int kt_start_tree(KT_CTX *ctx)
78
0
{
79
0
    static const unsigned char kt_tree_marker[8] = {
80
0
        0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
81
0
    };
82
83
0
    if (ctx->tree_started)
84
0
        return 1;
85
0
    ctx->root.pad = KT_DOMAIN_FINAL_NODE;
86
0
    if (!ossl_sha3_absorb(&ctx->root, kt_tree_marker,
87
0
            sizeof(kt_tree_marker))
88
0
        || !ossl_turboshake_init_keccak(&ctx->leaf, ctx->bitlen,
89
0
            KT_DOMAIN_INTERMEDIATE_NODE, ctx->cvlen))
90
0
        return 0;
91
0
    ctx->tree_started = 1;
92
0
    ctx->chunk_len = 0;
93
0
    return 1;
94
0
}
95
96
static int kt_finish_leaf(KT_CTX *ctx)
97
0
{
98
0
    unsigned char cv[64];
99
0
    int ret = 0;
100
101
0
    if (ctx->cv_count == SIZE_MAX) {
102
0
        ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
103
0
        return 0;
104
0
    }
105
0
    if (!ossl_sha3_final(&ctx->leaf, cv, ctx->cvlen)
106
0
        || !ossl_sha3_absorb(&ctx->root, cv, ctx->cvlen))
107
0
        goto err;
108
0
    ctx->cv_count++;
109
0
    ctx->chunk_len = 0;
110
0
    ret = ossl_turboshake_init_keccak(&ctx->leaf, ctx->bitlen,
111
0
        KT_DOMAIN_INTERMEDIATE_NODE, ctx->cvlen);
112
0
err:
113
0
    OPENSSL_cleanse(cv, sizeof(cv));
114
0
    return ret;
115
0
}
116
117
static int kt_absorb_s(KT_CTX *ctx, const unsigned char *in, size_t inlen)
118
0
{
119
0
    size_t len;
120
121
0
    if (inlen == 0)
122
0
        return 1;
123
0
    if (ctx->finalized)
124
0
        return 0;
125
126
0
    while (inlen > 0) {
127
        /* The next byte is byte 8193 of S, so switch to tree mode. */
128
0
        if (!ctx->tree_started && ctx->chunk_len == KT_CHUNK_SIZE
129
0
            && !kt_start_tree(ctx))
130
0
            return 0;
131
132
0
        len = KT_CHUNK_SIZE - ctx->chunk_len;
133
0
        if (len > inlen)
134
0
            len = inlen;
135
0
        if (!ossl_sha3_absorb(ctx->tree_started ? &ctx->leaf : &ctx->root,
136
0
                in, len))
137
0
            return 0;
138
0
        ctx->chunk_len += len;
139
0
        in += len;
140
0
        inlen -= len;
141
0
        if (ctx->tree_started && ctx->chunk_len == KT_CHUNK_SIZE
142
0
            && !kt_finish_leaf(ctx))
143
0
            return 0;
144
0
    }
145
0
    return 1;
146
0
}
147
148
static int kt_final_absorb(KT_CTX *ctx)
149
0
{
150
0
    unsigned char enc[sizeof(size_t) + 1];
151
0
    size_t enclen;
152
0
    static const unsigned char kt_final_suffix[2] = { 0xff, 0xff };
153
154
0
    if (ctx->finalized)
155
0
        return 1;
156
157
0
    enclen = kt_length_encode(ctx->custom_len, enc);
158
0
    if (!kt_absorb_s(ctx, ctx->custom, ctx->custom_len)
159
0
        || !kt_absorb_s(ctx, enc, enclen))
160
0
        return 0;
161
162
0
    if (ctx->tree_started) {
163
0
        if (ctx->chunk_len > 0 && !kt_finish_leaf(ctx))
164
0
            return 0;
165
0
        enclen = kt_length_encode(ctx->cv_count, enc);
166
0
        if (!ossl_sha3_absorb(&ctx->root, enc, enclen)
167
0
            || !ossl_sha3_absorb(&ctx->root, kt_final_suffix,
168
0
                sizeof(kt_final_suffix)))
169
0
            return 0;
170
0
    }
171
0
    ctx->finalized = 1;
172
0
    return 1;
173
0
}
174
175
static void *kt_newctx(ossl_unused void *provctx, size_t bitlen)
176
0
{
177
0
    KT_CTX *ctx;
178
179
0
    if (ossl_unlikely(!ossl_prov_is_running()))
180
0
        return NULL;
181
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
182
0
    if (ctx == NULL)
183
0
        return NULL;
184
0
    ctx->bitlen = bitlen;
185
0
    ctx->cvlen = bitlen == 128 ? 32 : 64;
186
0
    ctx->xoflen = ctx->cvlen;
187
0
    return ctx;
188
0
}
189
190
static int kt_init(void *vctx, const OSSL_PARAM params[])
191
0
{
192
0
    KT_CTX *ctx = vctx;
193
194
0
    if (ossl_unlikely(!ossl_prov_is_running()))
195
0
        return 0;
196
0
    OPENSSL_clear_free(ctx->custom, ctx->custom_len);
197
0
    ctx->custom = NULL;
198
0
    ctx->custom_len = 0;
199
0
    ctx->xoflen = ctx->cvlen;
200
0
    ctx->chunk_len = 0;
201
0
    ctx->cv_count = 0;
202
0
    ctx->tree_started = 0;
203
0
    ctx->finalized = 0;
204
0
    memset(&ctx->leaf, 0, sizeof(ctx->leaf));
205
0
    if (!ossl_turboshake_init_keccak(&ctx->root, ctx->bitlen,
206
0
            KT_DOMAIN_SINGLE_NODE, ctx->xoflen))
207
0
        return 0;
208
0
    return kt_set_ctx_params(vctx, params);
209
0
}
210
211
static int kt_update(void *vctx, const unsigned char *in, size_t inlen)
212
0
{
213
0
    KT_CTX *ctx = vctx;
214
215
0
    return kt_absorb_s(ctx, in, inlen);
216
0
}
217
218
static int kt_final(void *vctx, unsigned char *out, size_t *outl,
219
    size_t outsz)
220
0
{
221
0
    KT_CTX *ctx = vctx;
222
223
0
    if (ossl_unlikely(!ossl_prov_is_running()))
224
0
        return 0;
225
0
    if (outsz < ctx->xoflen)
226
0
        return 0;
227
0
    if (ctx->xoflen > 0
228
0
        && (!kt_final_absorb(ctx)
229
0
            || !ossl_sha3_final(&ctx->root, out, ctx->xoflen)))
230
0
        return 0;
231
0
    if (outl != NULL)
232
0
        *outl = ctx->xoflen;
233
0
    return 1;
234
0
}
235
236
static int kt_squeeze(void *vctx, unsigned char *out, size_t *outl,
237
    size_t outsz)
238
0
{
239
0
    KT_CTX *ctx = vctx;
240
241
0
    if (ossl_unlikely(!ossl_prov_is_running()))
242
0
        return 0;
243
0
    if (outsz > 0
244
0
        && (!kt_final_absorb(ctx)
245
0
            || !ossl_sha3_squeeze(&ctx->root, out, outsz)))
246
0
        return 0;
247
0
    if (outl != NULL)
248
0
        *outl = outsz;
249
0
    return 1;
250
0
}
251
252
static void kt_freectx(void *vctx)
253
0
{
254
0
    KT_CTX *ctx = vctx;
255
256
0
    if (ctx != NULL)
257
0
        OPENSSL_clear_free(ctx->custom, ctx->custom_len);
258
0
    OPENSSL_clear_free(ctx, sizeof(*ctx));
259
0
}
260
261
static void *kt_dupctx(void *vctx)
262
0
{
263
0
    KT_CTX *in = vctx;
264
0
    KT_CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL;
265
266
0
    if (ret == NULL)
267
0
        return NULL;
268
0
    *ret = *in;
269
0
    ret->custom = NULL;
270
0
    if (in->custom_len > 0) {
271
0
        ret->custom = OPENSSL_memdup(in->custom, in->custom_len);
272
0
        if (ret->custom == NULL) {
273
0
            OPENSSL_clear_free(ret, sizeof(*ret));
274
0
            return NULL;
275
0
        }
276
0
    }
277
0
    return ret;
278
0
}
279
280
static const OSSL_PARAM *kt_gettable_ctx_params(ossl_unused void *ctx,
281
    ossl_unused void *provctx)
282
0
{
283
0
    return kt_get_ctx_params_list;
284
0
}
285
286
static int kt_get_ctx_params(void *vctx, OSSL_PARAM params[])
287
0
{
288
0
    KT_CTX *ctx = vctx;
289
0
    struct kt_get_ctx_params_st p;
290
291
0
    if (ctx == NULL || !kt_get_ctx_params_decoder(params, &p))
292
0
        return 0;
293
294
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, ctx->xoflen)) {
295
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
296
0
        return 0;
297
0
    }
298
0
    if (p.xoflen != NULL && !OSSL_PARAM_set_size_t(p.xoflen, ctx->xoflen)) {
299
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
300
0
        return 0;
301
0
    }
302
0
    return 1;
303
0
}
304
305
static const OSSL_PARAM *kt_settable_ctx_params(ossl_unused void *ctx,
306
    ossl_unused void *provctx)
307
0
{
308
0
    return kt_set_ctx_params_list;
309
0
}
310
311
static int kt_set_custom(KT_CTX *ctx, const OSSL_PARAM *p)
312
0
{
313
0
    const unsigned char *data = p->data;
314
0
    unsigned char *custom = NULL;
315
0
    size_t custom_len;
316
317
0
    if (p->data_type != OSSL_PARAM_OCTET_STRING)
318
0
        return 0;
319
0
    custom_len = p->data_size;
320
321
    /* Match the existing CSHAKE provider limit for customization strings. */
322
0
    if (custom_len > KT_MAX_CUSTOM_STRING) {
323
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
324
0
        return 0;
325
0
    }
326
327
0
    if (custom_len > 0) {
328
0
        if (data == NULL)
329
0
            return 0;
330
0
        custom = OPENSSL_memdup(data, custom_len);
331
0
        if (custom == NULL)
332
0
            return 0;
333
0
    }
334
0
    OPENSSL_clear_free(ctx->custom, ctx->custom_len);
335
0
    ctx->custom = custom;
336
0
    ctx->custom_len = custom_len;
337
0
    return 1;
338
0
}
339
340
static int kt_set_ctx_params(void *vctx, const OSSL_PARAM params[])
341
0
{
342
0
    KT_CTX *ctx = vctx;
343
0
    struct kt_set_ctx_params_st p;
344
0
    size_t xoflen;
345
0
    int has_change = 0;
346
347
0
    if (ctx == NULL || !kt_set_ctx_params_decoder(params, &p))
348
0
        return 0;
349
350
0
    has_change = p.xoflen != NULL || p.custom != NULL;
351
0
    if (has_change && ctx->finalized)
352
0
        return 0;
353
354
0
    xoflen = ctx->xoflen;
355
0
    if (p.xoflen != NULL
356
0
        && !OSSL_PARAM_get_size_t(p.xoflen, &xoflen)) {
357
0
        ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
358
0
        return 0;
359
0
    }
360
361
0
    if (p.custom != NULL && !kt_set_custom(ctx, p.custom))
362
0
        return 0;
363
0
    ctx->xoflen = xoflen;
364
0
    ctx->root.md_size = xoflen;
365
0
    return 1;
366
0
}
367
368
#define IMPLEMENT_KT(bitlen)                                                    \
369
    static OSSL_FUNC_digest_newctx_fn kt_##bitlen##_newctx;                     \
370
    static void *kt_##bitlen##_newctx(void *provctx)                            \
371
0
    {                                                                           \
372
0
        return kt_newctx(provctx, bitlen);                                      \
373
0
    }                                                                           \
Unexecuted instantiation: kt_prov.c:kt_128_newctx
Unexecuted instantiation: kt_prov.c:kt_256_newctx
374
    PROV_FUNC_DIGEST_GET_PARAM(kt_##bitlen, SHA3_BLOCKSIZE(bitlen),             \
375
        bitlen == 128 ? 32 : 64, KT_FLAGS)                                      \
376
    const OSSL_DISPATCH ossl_kt_##bitlen##_functions[] = {                      \
377
        { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))kt_##bitlen##_newctx },      \
378
        { OSSL_FUNC_DIGEST_INIT, (void (*)(void))kt_init },                     \
379
        { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))kt_update },                 \
380
        { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))kt_final },                   \
381
        { OSSL_FUNC_DIGEST_SQUEEZE, (void (*)(void))kt_squeeze },               \
382
        { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))kt_freectx },               \
383
        { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))kt_dupctx },                 \
384
        { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))kt_set_ctx_params }, \
385
        { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS,                                 \
386
            (void (*)(void))kt_settable_ctx_params },                           \
387
        { OSSL_FUNC_DIGEST_GET_CTX_PARAMS, (void (*)(void))kt_get_ctx_params }, \
388
        { OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS,                                 \
389
            (void (*)(void))kt_gettable_ctx_params },                           \
390
        PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(kt_##bitlen),                      \
391
        PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
392
393
/* ossl_kt_128_functions */
394
IMPLEMENT_KT(128)
395
/* ossl_kt_256_functions */
396
IMPLEMENT_KT(256)