Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/macs/kmac_prov.c
Line
Count
Source
1
/*
2
 * Copyright 2018-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
11
/*
12
 * See SP800-185 "Appendix A - KMAC, .... in Terms of Keccak[c]"
13
 *
14
 * Inputs are:
15
 *    K = Key                  (len(K) < 2^2040 bits)
16
 *    X = Input
17
 *    L = Output length        (0 <= L < 2^2040 bits)
18
 *    S = Customization String Default="" (len(S) < 2^2040 bits)
19
 *
20
 * KMAC128(K, X, L, S)
21
 * {
22
 *     newX = bytepad(encode_string(K), 168) ||  X || right_encode(L).
23
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 168).
24
 *     return KECCAK[256](T || newX || 00, L).
25
 * }
26
 *
27
 * KMAC256(K, X, L, S)
28
 * {
29
 *     newX = bytepad(encode_string(K), 136) ||  X || right_encode(L).
30
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 136).
31
 *     return KECCAK[512](T || newX || 00, L).
32
 * }
33
 *
34
 * KMAC128XOF(K, X, L, S)
35
 * {
36
 *     newX = bytepad(encode_string(K), 168) ||  X || right_encode(0).
37
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 168).
38
 *     return KECCAK[256](T || newX || 00, L).
39
 * }
40
 *
41
 * KMAC256XOF(K, X, L, S)
42
 * {
43
 *     newX = bytepad(encode_string(K), 136) ||  X || right_encode(0).
44
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 136).
45
 *     return KECCAK[512](T || newX || 00, L).
46
 * }
47
 *
48
 */
49
50
#include <stdlib.h>
51
#include <string.h>
52
#include <openssl/core_dispatch.h>
53
#include <openssl/core_names.h>
54
#include <openssl/params.h>
55
#include <openssl/evp.h>
56
#include <openssl/err.h>
57
#include <openssl/proverr.h>
58
#include <openssl/fips_names.h>
59
#include "prov/securitycheck.h"
60
#include "prov/implementations.h"
61
#include "prov/provider_ctx.h"
62
#include "prov/provider_util.h"
63
#include "prov/providercommon.h"
64
#include "internal/cryptlib.h" /* ossl_assert */
65
66
/*
67
 * Forward declaration of everything implemented here.  This is not strictly
68
 * necessary for the compiler, but provides an assurance that the signatures
69
 * of the functions in the dispatch table are correct.
70
 */
71
static OSSL_FUNC_mac_newctx_fn kmac128_new;
72
static OSSL_FUNC_mac_newctx_fn kmac256_new;
73
static OSSL_FUNC_mac_dupctx_fn kmac_dup;
74
static OSSL_FUNC_mac_freectx_fn kmac_free;
75
static OSSL_FUNC_mac_gettable_ctx_params_fn kmac_gettable_ctx_params;
76
static OSSL_FUNC_mac_get_ctx_params_fn kmac_get_ctx_params;
77
static OSSL_FUNC_mac_settable_ctx_params_fn kmac_settable_ctx_params;
78
static OSSL_FUNC_mac_set_ctx_params_fn kmac_set_ctx_params;
79
static OSSL_FUNC_mac_init_fn kmac_init;
80
static OSSL_FUNC_mac_update_fn kmac_update;
81
static OSSL_FUNC_mac_final_fn kmac_final;
82
83
#define KMAC_MAX_BLOCKSIZE ((1600 - 128 * 2) / 8) /* 168 */
84
85
/*
86
 * Length encoding will be  a 1 byte size + length in bits (3 bytes max)
87
 * This gives a range of 0..0XFFFFFF bits = 2097151 bytes).
88
 */
89
287
#define KMAC_MAX_OUTPUT_LEN (0xFFFFFF / 8)
90
#define KMAC_MAX_ENCODED_HEADER_LEN (1 + 3)
91
92
/*
93
 * Restrict the maximum length of the customisation string.  This must not
94
 * exceed 64 bits = 8k bytes.
95
 */
96
260
#define KMAC_MAX_CUSTOM 512
97
98
/* Maximum size of encoded custom string */
99
#define KMAC_MAX_CUSTOM_ENCODED (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_HEADER_LEN)
100
101
/* Maximum key size in bytes = 512 (4096 bits) */
102
7.29k
#define KMAC_MAX_KEY 512
103
14.7k
#define KMAC_MIN_KEY 4
104
105
/*
106
 * Maximum Encoded Key size will be padded to a multiple of the blocksize
107
 * i.e KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN = 512 + 4
108
 * Padded to a multiple of KMAC_MAX_BLOCKSIZE
109
 */
110
#define KMAC_MAX_KEY_ENCODED (KMAC_MAX_BLOCKSIZE * 4)
111
112
/* Fixed value of encode_string("KMAC") */
113
static const unsigned char kmac_string[] = {
114
    0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43
115
};
116
117
#define KMAC_FLAG_XOF_MODE          1
118
119
struct kmac_data_st {
120
    void  *provctx;
121
    EVP_MD_CTX *ctx;
122
    PROV_DIGEST digest;
123
    size_t out_len;
124
    size_t key_len;
125
    size_t custom_len;
126
    /* If xof_mode = 1 then we use right_encode(0) */
127
    int xof_mode;
128
    /* key and custom are stored in encoded form */
129
    unsigned char key[KMAC_MAX_KEY_ENCODED];
130
    unsigned char custom[KMAC_MAX_CUSTOM_ENCODED];
131
#ifdef FIPS_MODULE
132
    /*
133
     * 'internal' is set to 1 if KMAC is used inside another algorithm such as a
134
     * KDF. In this case it is the parent algorithm that is responsible for
135
     * performing any conditional FIPS indicator related checks for KMAC.
136
     */
137
    int internal;
138
#endif
139
    OSSL_FIPS_IND_DECLARE
140
};
141
142
static int encode_string(unsigned char *out, size_t out_max_len, size_t *out_len,
143
                         const unsigned char *in, size_t in_len);
144
static int right_encode(unsigned char *out, size_t out_max_len, size_t *out_len,
145
                        size_t bits);
146
static int bytepad(unsigned char *out, size_t *out_len,
147
                   const unsigned char *in1, size_t in1_len,
148
                   const unsigned char *in2, size_t in2_len,
149
                   size_t w);
150
static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len,
151
                                   size_t *out_len,
152
                                   const unsigned char *in, size_t in_len,
153
                                   size_t w);
154
155
static void kmac_free(void *vmacctx)
156
570
{
157
570
    struct kmac_data_st *kctx = vmacctx;
158
159
570
    if (kctx != NULL) {
160
570
        EVP_MD_CTX_free(kctx->ctx);
161
570
        ossl_prov_digest_reset(&kctx->digest);
162
570
        OPENSSL_cleanse(kctx->key, kctx->key_len);
163
570
        OPENSSL_cleanse(kctx->custom, kctx->custom_len);
164
570
        OPENSSL_free(kctx);
165
570
    }
166
570
}
167
168
/*
169
 * We have KMAC implemented as a hash, which we can use instead of
170
 * reimplementing the EVP functionality with direct use of
171
 * keccak_mac_init() and friends.
172
 */
173
static struct kmac_data_st *kmac_new(void *provctx)
174
570
{
175
570
    struct kmac_data_st *kctx;
176
177
570
    if (!ossl_prov_is_running())
178
0
        return NULL;
179
180
570
    if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL
181
570
            || (kctx->ctx = EVP_MD_CTX_new()) == NULL) {
182
0
        kmac_free(kctx);
183
0
        return NULL;
184
0
    }
185
570
    kctx->provctx = provctx;
186
570
    OSSL_FIPS_IND_INIT(kctx)
187
570
    return kctx;
188
570
}
189
190
static void *kmac_fetch_new(void *provctx, const OSSL_PARAM *params)
191
399
{
192
399
    struct kmac_data_st *kctx = kmac_new(provctx);
193
399
    int md_size;
194
195
399
    if (kctx == NULL)
196
0
        return 0;
197
399
    if (!ossl_prov_digest_load_from_params(&kctx->digest, params,
198
399
                                      PROV_LIBCTX_OF(provctx))) {
199
0
        kmac_free(kctx);
200
0
        return 0;
201
0
    }
202
203
399
    md_size = EVP_MD_get_size(ossl_prov_digest_md(&kctx->digest));
204
399
    if (md_size <= 0) {
205
0
        kmac_free(kctx);
206
0
        return 0;
207
0
    }
208
399
    kctx->out_len = (size_t)md_size;
209
399
    return kctx;
210
399
}
211
212
static void *kmac128_new(void *provctx)
213
243
{
214
243
    static const OSSL_PARAM kmac128_params[] = {
215
243
        OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC128,
216
243
                               sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC128)),
217
243
        OSSL_PARAM_END
218
243
    };
219
243
    return kmac_fetch_new(provctx, kmac128_params);
220
243
}
221
222
static void *kmac256_new(void *provctx)
223
283
{
224
283
    static const OSSL_PARAM kmac256_params[] = {
225
283
        OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC256,
226
283
                               sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC256)),
227
283
        OSSL_PARAM_END
228
283
    };
229
283
    return kmac_fetch_new(provctx, kmac256_params);
230
283
}
231
232
static void *kmac_dup(void *vsrc)
233
44
{
234
44
    struct kmac_data_st *src = vsrc;
235
44
    struct kmac_data_st *dst;
236
237
44
    if (!ossl_prov_is_running())
238
0
        return NULL;
239
240
44
    dst = kmac_new(src->provctx);
241
44
    if (dst == NULL)
242
0
        return NULL;
243
244
44
    if (!EVP_MD_CTX_copy(dst->ctx, src->ctx)
245
44
        || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
246
0
        kmac_free(dst);
247
0
        return NULL;
248
0
    }
249
#ifdef FIPS_MODULE
250
    dst->internal = src->internal;
251
#endif
252
44
    dst->out_len = src->out_len;
253
44
    dst->key_len = src->key_len;
254
44
    dst->custom_len = src->custom_len;
255
44
    dst->xof_mode = src->xof_mode;
256
44
    memcpy(dst->key, src->key, src->key_len);
257
44
    memcpy(dst->custom, src->custom, dst->custom_len);
258
44
    OSSL_FIPS_IND_COPY(dst, src)
259
260
44
    return dst;
261
44
}
262
263
static int kmac_setkey(struct kmac_data_st *kctx, const unsigned char *key,
264
                       size_t keylen)
265
7.38k
{
266
7.38k
    const EVP_MD *digest = ossl_prov_digest_md(&kctx->digest);
267
7.38k
    int w = EVP_MD_get_block_size(digest);
268
269
7.38k
    if (keylen < KMAC_MIN_KEY || keylen > KMAC_MAX_KEY) {
270
131
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
271
131
        return 0;
272
131
    }
273
#ifdef FIPS_MODULE
274
    /*
275
     * Only do the key check if KMAC is fetched directly.
276
     * Other algorithms that embed KMAC such as SSKDF will ignore this check.
277
     */
278
    if (!kctx->internal) {
279
        int approved = ossl_mac_check_key_size(keylen);
280
281
        if (!approved) {
282
            if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE1,
283
                                             PROV_LIBCTX_OF(kctx->provctx),
284
                                             "KMAC", "Key size",
285
                                             ossl_fips_config_kmac_key_check)) {
286
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
287
                return 0;
288
            }
289
        }
290
    }
291
#endif
292
7.25k
    if (w <= 0) {
293
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
294
0
        return 0;
295
0
    }
296
7.25k
    if (!kmac_bytepad_encode_key(kctx->key, sizeof(kctx->key), &kctx->key_len,
297
7.25k
                                 key, keylen, (size_t)w))
298
0
        return 0;
299
7.25k
    return 1;
300
7.25k
}
301
302
/*
303
 * The init() assumes that any ctrl methods are set beforehand for
304
 * md, key and custom. Setting the fields afterwards will have no
305
 * effect on the output mac.
306
 */
307
static int kmac_init(void *vmacctx, const unsigned char *key,
308
                     size_t keylen, const OSSL_PARAM params[])
309
6.90k
{
310
6.90k
    struct kmac_data_st *kctx = vmacctx;
311
6.90k
    EVP_MD_CTX *ctx = kctx->ctx;
312
6.90k
    unsigned char *out;
313
6.90k
    size_t out_len, block_len;
314
6.90k
    int res, t;
315
316
6.90k
    if (!ossl_prov_is_running() || !kmac_set_ctx_params(kctx, params))
317
135
        return 0;
318
319
6.77k
    if (key != NULL) {
320
6.77k
        if (!kmac_setkey(kctx, key, keylen))
321
19
            return 0;
322
6.77k
    } else if (kctx->key_len == 0) {
323
        /* Check key has been set */
324
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
325
0
        return 0;
326
0
    }
327
6.75k
    if (!EVP_DigestInit_ex(kctx->ctx, ossl_prov_digest_md(&kctx->digest),
328
6.75k
                           NULL))
329
0
        return 0;
330
331
6.75k
    t = EVP_MD_get_block_size(ossl_prov_digest_md(&kctx->digest));
332
6.75k
    if (t <= 0) {
333
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
334
0
        return 0;
335
0
    }
336
6.75k
    block_len = t;
337
338
    /* Set default custom string if it is not already set */
339
6.75k
    if (kctx->custom_len == 0) {
340
37
        const OSSL_PARAM cparams[] = {
341
37
            OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, "", 0),
342
37
            OSSL_PARAM_END
343
37
        };
344
37
        (void)kmac_set_ctx_params(kctx, cparams);
345
37
    }
346
347
6.75k
    if (!bytepad(NULL, &out_len, kmac_string, sizeof(kmac_string),
348
6.75k
                 kctx->custom, kctx->custom_len, block_len)) {
349
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
350
0
        return 0;
351
0
    }
352
6.75k
    out = OPENSSL_malloc(out_len);
353
6.75k
    if (out == NULL)
354
0
        return 0;
355
6.75k
    res = bytepad(out, NULL, kmac_string, sizeof(kmac_string),
356
6.75k
                  kctx->custom, kctx->custom_len, block_len)
357
6.75k
          && EVP_DigestUpdate(ctx, out, out_len)
358
6.75k
          && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len);
359
6.75k
    OPENSSL_free(out);
360
6.75k
    return res;
361
6.75k
}
362
363
static int kmac_update(void *vmacctx, const unsigned char *data,
364
                       size_t datalen)
365
7.25k
{
366
7.25k
    struct kmac_data_st *kctx = vmacctx;
367
368
7.25k
    return EVP_DigestUpdate(kctx->ctx, data, datalen);
369
7.25k
}
370
371
static int kmac_final(void *vmacctx, unsigned char *out, size_t *outl,
372
                      size_t outsize)
373
6.75k
{
374
6.75k
    struct kmac_data_st *kctx = vmacctx;
375
6.75k
    EVP_MD_CTX *ctx = kctx->ctx;
376
6.75k
    size_t lbits, len;
377
6.75k
    unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];
378
6.75k
    int ok;
379
380
6.75k
    if (!ossl_prov_is_running())
381
0
        return 0;
382
383
    /* KMAC XOF mode sets the encoded length to 0 */
384
6.75k
    lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));
385
386
6.75k
    ok = right_encode(encoded_outlen, sizeof(encoded_outlen), &len, lbits)
387
6.75k
        && EVP_DigestUpdate(ctx, encoded_outlen, len)
388
6.74k
        && EVP_DigestFinalXOF(ctx, out, kctx->out_len);
389
6.75k
    *outl = kctx->out_len;
390
6.75k
    return ok;
391
6.75k
}
392
393
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
394
#ifndef kmac_get_ctx_params_list
395
static const OSSL_PARAM kmac_get_ctx_params_list[] = {
396
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
397
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
398
# if defined(FIPS_MODULE)
399
    OSSL_PARAM_int(OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR, NULL),
400
# endif
401
    OSSL_PARAM_END
402
};
403
#endif
404
405
#ifndef kmac_get_ctx_params_st
406
struct kmac_get_ctx_params_st {
407
    OSSL_PARAM *bsize;
408
# if defined(FIPS_MODULE)
409
    OSSL_PARAM *ind;
410
# endif
411
    OSSL_PARAM *size;
412
};
413
#endif
414
415
#ifndef kmac_get_ctx_params_decoder
416
static int kmac_get_ctx_params_decoder
417
    (const OSSL_PARAM *p, struct kmac_get_ctx_params_st *r)
418
2.75k
{
419
2.75k
    const char *s;
420
421
2.75k
    memset(r, 0, sizeof(*r));
422
2.75k
    if (p != NULL)
423
5.51k
        for (; (s = p->key) != NULL; p++)
424
2.75k
            switch(s[0]) {
425
0
            default:
426
0
                break;
427
0
            case 'b':
428
0
                if (ossl_likely(strcmp("lock-size", s + 1) == 0)) {
429
                    /* OSSL_MAC_PARAM_BLOCK_SIZE */
430
0
                    if (ossl_unlikely(r->bsize != NULL)) {
431
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
432
0
                                       "param %s is repeated", s);
433
0
                        return 0;
434
0
                    }
435
0
                    r->bsize = (OSSL_PARAM *)p;
436
0
                }
437
0
                break;
438
0
            case 'f':
439
# if defined(FIPS_MODULE)
440
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
441
                    /* OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR */
442
                    if (ossl_unlikely(r->ind != NULL)) {
443
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
444
                                       "param %s is repeated", s);
445
                        return 0;
446
                    }
447
                    r->ind = (OSSL_PARAM *)p;
448
                }
449
# endif
450
0
                break;
451
2.75k
            case 's':
452
2.75k
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
453
                    /* OSSL_MAC_PARAM_SIZE */
454
2.75k
                    if (ossl_unlikely(r->size != NULL)) {
455
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
456
0
                                       "param %s is repeated", s);
457
0
                        return 0;
458
0
                    }
459
2.75k
                    r->size = (OSSL_PARAM *)p;
460
2.75k
                }
461
2.75k
            }
462
2.75k
    return 1;
463
2.75k
}
464
#endif
465
/* End of machine generated */
466
467
static const OSSL_PARAM *kmac_gettable_ctx_params(ossl_unused void *ctx,
468
                                                  ossl_unused void *provctx)
469
0
{
470
0
    return kmac_get_ctx_params_list;
471
0
}
472
473
static int kmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
474
2.75k
{
475
2.75k
    struct kmac_data_st *kctx = vmacctx;
476
2.75k
    struct kmac_get_ctx_params_st p;
477
2.75k
    int sz;
478
479
2.75k
    if (kctx == NULL || !kmac_get_ctx_params_decoder(params, &p))
480
0
        return 0;
481
482
2.75k
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, kctx->out_len))
483
0
        return 0;
484
485
2.75k
    if (p.bsize != NULL) {
486
0
        sz = EVP_MD_block_size(ossl_prov_digest_md(&kctx->digest));
487
0
        if (!OSSL_PARAM_set_int(p.bsize, sz))
488
0
            return 0;
489
0
    }
490
491
2.75k
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(kctx, p.ind))
492
0
        return 0;
493
494
2.75k
    return 1;
495
2.75k
}
496
497
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
498
#ifndef kmac_set_ctx_params_list
499
static const OSSL_PARAM kmac_set_ctx_params_list[] = {
500
    OSSL_PARAM_int(OSSL_MAC_PARAM_XOF, NULL),
501
    OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
502
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
503
    OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0),
504
# if defined(FIPS_MODULE)
505
    OSSL_PARAM_int(OSSL_MAC_PARAM_FIPS_KEY_CHECK, NULL),
506
# endif
507
# if defined(FIPS_MODULE)
508
    OSSL_PARAM_int(OSSL_MAC_PARAM_FIPS_NO_SHORT_MAC, NULL),
509
# endif
510
    OSSL_PARAM_END
511
};
512
#endif
513
514
#ifndef kmac_set_ctx_params_st
515
struct kmac_set_ctx_params_st {
516
    OSSL_PARAM *custom;
517
# if defined(FIPS_MODULE)
518
    OSSL_PARAM *ind_k;
519
# endif
520
# if defined(FIPS_MODULE)
521
    OSSL_PARAM *ind_sht;
522
# endif
523
    OSSL_PARAM *key;
524
    OSSL_PARAM *size;
525
    OSSL_PARAM *xof;
526
};
527
#endif
528
529
#ifndef kmac_set_ctx_params_decoder
530
static int kmac_set_ctx_params_decoder
531
    (const OSSL_PARAM *p, struct kmac_set_ctx_params_st *r)
532
3.03k
{
533
3.03k
    const char *s;
534
535
3.03k
    memset(r, 0, sizeof(*r));
536
3.03k
    if (p != NULL)
537
1.62k
        for (; (s = p->key) != NULL; p++)
538
1.24k
            switch(s[0]) {
539
84
            default:
540
84
                break;
541
332
            case 'c':
542
332
                if (ossl_likely(strcmp("ustom", s + 1) == 0)) {
543
                    /* OSSL_MAC_PARAM_CUSTOM */
544
302
                    if (ossl_unlikely(r->custom != NULL)) {
545
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
546
0
                                       "param %s is repeated", s);
547
0
                        return 0;
548
0
                    }
549
302
                    r->custom = (OSSL_PARAM *)p;
550
302
                }
551
332
                break;
552
332
            case 'k':
553
270
                switch(s[1]) {
554
0
                default:
555
0
                    break;
556
270
                case 'e':
557
270
                    switch(s[2]) {
558
0
                    default:
559
0
                        break;
560
270
                    case 'y':
561
270
                        switch(s[3]) {
562
0
                        default:
563
0
                            break;
564
0
                        case '-':
565
# if defined(FIPS_MODULE)
566
                            if (ossl_likely(strcmp("check", s + 4) == 0)) {
567
                                /* OSSL_MAC_PARAM_FIPS_KEY_CHECK */
568
                                if (ossl_unlikely(r->ind_k != NULL)) {
569
                                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
570
                                                   "param %s is repeated", s);
571
                                    return 0;
572
                                }
573
                                r->ind_k = (OSSL_PARAM *)p;
574
                            }
575
# endif
576
0
                            break;
577
270
                        case '\0':
578
270
                            if (ossl_unlikely(r->key != NULL)) {
579
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
580
0
                                               "param %s is repeated", s);
581
0
                                return 0;
582
0
                            }
583
270
                            r->key = (OSSL_PARAM *)p;
584
270
                        }
585
270
                    }
586
270
                }
587
270
                break;
588
270
            case 'n':
589
# if defined(FIPS_MODULE)
590
                if (ossl_likely(strcmp("o-short-mac", s + 1) == 0)) {
591
                    /* OSSL_MAC_PARAM_FIPS_NO_SHORT_MAC */
592
                    if (ossl_unlikely(r->ind_sht != NULL)) {
593
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
594
                                       "param %s is repeated", s);
595
                        return 0;
596
                    }
597
                    r->ind_sht = (OSSL_PARAM *)p;
598
                }
599
# endif
600
0
                break;
601
287
            case 's':
602
287
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
603
                    /* OSSL_MAC_PARAM_SIZE */
604
287
                    if (ossl_unlikely(r->size != NULL)) {
605
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
606
0
                                       "param %s is repeated", s);
607
0
                        return 0;
608
0
                    }
609
287
                    r->size = (OSSL_PARAM *)p;
610
287
                }
611
287
                break;
612
287
            case 'x':
613
270
                if (ossl_likely(strcmp("of", s + 1) == 0)) {
614
                    /* OSSL_MAC_PARAM_XOF */
615
270
                    if (ossl_unlikely(r->xof != NULL)) {
616
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
617
0
                                       "param %s is repeated", s);
618
0
                        return 0;
619
0
                    }
620
270
                    r->xof = (OSSL_PARAM *)p;
621
270
                }
622
1.24k
            }
623
3.03k
    return 1;
624
3.03k
}
625
#endif
626
/* End of machine generated */
627
628
static const OSSL_PARAM *kmac_settable_ctx_params(ossl_unused void *ctx,
629
                                                  ossl_unused void *provctx)
630
373
{
631
373
    return kmac_set_ctx_params_list;
632
373
}
633
634
/*
635
 * The following params can be set any time before final():
636
 *     - "outlen" or "size":    The requested output length.
637
 *     - "xof":                 If set, this indicates that right_encoded(0)
638
 *                              is part of the digested data, otherwise it
639
 *                              uses right_encoded(requested output length).
640
 *
641
 * All other params should be set before init().
642
 */
643
static int kmac_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
644
3.03k
{
645
3.03k
    struct kmac_data_st *kctx = vmacctx;
646
3.03k
    struct kmac_set_ctx_params_st p;
647
648
3.03k
    if (kctx == NULL || !kmac_set_ctx_params_decoder(params, &p))
649
0
        return 0;
650
651
3.03k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE0,
652
3.03k
                                          p.ind_sht))
653
0
        return 0;
654
3.03k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k))
655
0
        return 0;
656
657
3.03k
    if (p.xof != NULL && !OSSL_PARAM_get_int(p.xof, &kctx->xof_mode))
658
0
        return 0;
659
660
3.03k
    if (p.size != NULL) {
661
287
        size_t sz = 0;
662
663
287
        if (!OSSL_PARAM_get_size_t(p.size, &sz))
664
0
            return 0;
665
287
        if (sz > KMAC_MAX_OUTPUT_LEN) {
666
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
667
0
            return 0;
668
0
        }
669
#ifdef FIPS_MODULE
670
        /* SP 800-185 8.4.2 mandates a minimum of 32 bits of output */
671
        if (sz < 32 / 8) {
672
            if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE0,
673
                                             PROV_LIBCTX_OF(kctx->provctx),
674
                                             "KMAC", "length",
675
                                             ossl_fips_config_no_short_mac)) {
676
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
677
                return 0;
678
            }
679
        }
680
#endif
681
287
        kctx->out_len = sz;
682
287
    }
683
684
3.03k
    if (p.key != NULL)
685
270
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
686
270
                || !kmac_setkey(kctx, p.key->data, p.key->data_size))
687
42
            return 0;
688
689
2.99k
    if (p.custom != NULL) {
690
260
        if (p.custom->data_type != OSSL_PARAM_OCTET_STRING)
691
0
            return 0;
692
260
        if (p.custom->data_size > KMAC_MAX_CUSTOM) {
693
17
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
694
17
            return 0;
695
17
        }
696
243
        if (!encode_string(kctx->custom, sizeof(kctx->custom), &kctx->custom_len,
697
243
                           p.custom->data, p.custom->data_size))
698
0
            return 0;
699
243
    }
700
701
2.98k
    return 1;
702
2.99k
}
703
704
/* Encoding/Padding Methods. */
705
706
/* Returns the number of bytes required to store 'bits' into a byte array */
707
static unsigned int get_encode_size(size_t bits)
708
14.5k
{
709
14.5k
    unsigned int cnt = 0, sz = sizeof(size_t);
710
711
36.8k
    while (bits && (cnt < sz)) {
712
22.3k
        ++cnt;
713
22.3k
        bits >>= 8;
714
22.3k
    }
715
    /* If bits is zero 1 byte is required */
716
14.5k
    if (cnt == 0)
717
502
        cnt = 1;
718
14.5k
    return cnt;
719
14.5k
}
720
721
/*
722
 * Convert an integer into bytes . The number of bytes is appended
723
 * to the end of the buffer. Returns an array of bytes 'out' of size
724
 * *out_len.
725
 *
726
 * e.g if bits = 32, out[2] = { 0x20, 0x01 }
727
 */
728
static int right_encode(unsigned char *out, size_t out_max_len, size_t *out_len,
729
                        size_t bits)
730
6.75k
{
731
6.75k
    unsigned int len = get_encode_size(bits);
732
6.75k
    int i;
733
734
6.75k
    if (len >= out_max_len) {
735
0
        ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
736
0
        return 0;
737
0
    }
738
739
    /* MSB's are at the start of the bytes array */
740
20.0k
    for (i = len - 1; i >= 0; --i) {
741
13.2k
        out[i] = (unsigned char)(bits & 0xFF);
742
13.2k
        bits >>= 8;
743
13.2k
    }
744
    /* Tack the length onto the end */
745
6.75k
    out[len] = (unsigned char)len;
746
747
    /* The Returned length includes the tacked on byte */
748
6.75k
    *out_len = len + 1;
749
6.75k
    return 1;
750
6.75k
}
751
752
/*
753
 * Encodes a string with a left encoded length added. Note that the
754
 * in_len is converted to bits (*8).
755
 *
756
 * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
757
 *                                 len   bits    K     M     A     C
758
 */
759
static int encode_string(unsigned char *out, size_t out_max_len, size_t *out_len,
760
                         const unsigned char *in, size_t in_len)
761
7.83k
{
762
7.83k
    if (in == NULL) {
763
0
        *out_len = 0;
764
7.83k
    } else {
765
7.83k
        size_t i, bits, len, sz;
766
767
7.83k
        bits = 8 * in_len;
768
7.83k
        len = get_encode_size(bits);
769
7.83k
        sz = 1 + len + in_len;
770
771
7.83k
        if (sz > out_max_len) {
772
0
            ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
773
0
            return 0;
774
0
        }
775
776
7.83k
        out[0] = (unsigned char)len;
777
17.3k
        for (i = len; i > 0; --i) {
778
9.54k
            out[i] = (bits & 0xFF);
779
9.54k
            bits >>= 8;
780
9.54k
        }
781
7.83k
        memcpy(out + len + 1, in, in_len);
782
7.83k
        *out_len = sz;
783
7.83k
    }
784
7.83k
    return 1;
785
7.83k
}
786
787
/*
788
 * Returns a zero padded encoding of the inputs in1 and an optional
789
 * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'.
790
 * The value of w is in bytes (< 256).
791
 *
792
 * The returned output is:
793
 *    zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
794
 */
795
static int bytepad(unsigned char *out, size_t *out_len,
796
                   const unsigned char *in1, size_t in1_len,
797
                   const unsigned char *in2, size_t in2_len, size_t w)
798
28.0k
{
799
28.0k
    size_t len;
800
28.0k
    unsigned char *p = out;
801
28.0k
    size_t sz = w;
802
803
28.0k
    if (out == NULL) {
804
14.0k
        if (out_len == NULL) {
805
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
806
0
            return 0;
807
0
        }
808
14.0k
        sz = 2 + in1_len + (in2 != NULL ? in2_len : 0);
809
14.0k
        *out_len = (sz + w - 1) / w * w;
810
14.0k
        return 1;
811
14.0k
    }
812
813
14.0k
    if (!ossl_assert(w <= 255))
814
0
        return 0;
815
816
    /* Left encoded w */
817
14.0k
    *p++ = 1;
818
14.0k
    *p++ = (unsigned char)w;
819
    /* || in1 */
820
14.0k
    memcpy(p, in1, in1_len);
821
14.0k
    p += in1_len;
822
    /* [ || in2 ] */
823
14.0k
    if (in2 != NULL && in2_len > 0) {
824
6.75k
        memcpy(p, in2, in2_len);
825
6.75k
        p += in2_len;
826
6.75k
    }
827
    /* Figure out the pad size (divisible by w) */
828
14.0k
    len = p - out;
829
14.0k
    sz = (len + w - 1) / w * w;
830
    /* zero pad the end of the buffer */
831
14.0k
    if (sz != len)
832
13.9k
        memset(p, 0, sz - len);
833
14.0k
    if (out_len != NULL)
834
0
        *out_len = sz;
835
14.0k
    return 1;
836
14.0k
}
837
838
/* Returns out = bytepad(encode_string(in), w) */
839
static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len,
840
                                   size_t *out_len,
841
                                   const unsigned char *in, size_t in_len,
842
                                   size_t w)
843
7.25k
{
844
7.25k
    unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN];
845
7.25k
    size_t tmp_len;
846
847
7.25k
    if (!encode_string(tmp, sizeof(tmp), &tmp_len, in, in_len))
848
0
        return 0;
849
7.25k
    if (!bytepad(NULL, out_len, tmp, tmp_len, NULL, 0, w))
850
0
        return 0;
851
7.25k
    if (!ossl_assert(*out_len <= out_max_len))
852
0
        return 0;
853
7.25k
    return bytepad(out, NULL, tmp, tmp_len, NULL, 0, w);
854
7.25k
}
855
856
#define IMPLEMENT_KMAC_TABLE(size, funcname, newname)                          \
857
const OSSL_DISPATCH ossl_kmac##size##_##funcname[] =                           \
858
{                                                                              \
859
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))kmac##size##_##newname },          \
860
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))kmac_dup },                        \
861
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))kmac_free },                      \
862
    { OSSL_FUNC_MAC_INIT, (void (*)(void))kmac_init },                         \
863
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))kmac_update },                     \
864
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))kmac_final },                       \
865
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,                                       \
866
      (void (*)(void))kmac_gettable_ctx_params },                              \
867
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))kmac_get_ctx_params },     \
868
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,                                       \
869
      (void (*)(void))kmac_settable_ctx_params },                              \
870
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))kmac_set_ctx_params },     \
871
    OSSL_DISPATCH_END                                                          \
872
}
873
874
#define KMAC_TABLE(size) IMPLEMENT_KMAC_TABLE(size, functions, new)
875
876
KMAC_TABLE(128);
877
KMAC_TABLE(256);
878
879
#ifdef FIPS_MODULE
880
# define KMAC_INTERNAL_TABLE(size)                                             \
881
static OSSL_FUNC_mac_newctx_fn kmac##size##_internal_new;                      \
882
static void *kmac##size##_internal_new(void *provctx)                          \
883
{                                                                              \
884
    struct kmac_data_st *macctx = kmac##size##_new(provctx);                   \
885
                                                                               \
886
    if (macctx != NULL)                                                        \
887
        macctx->internal = 1;                                                  \
888
    return macctx;                                                             \
889
}                                                                              \
890
IMPLEMENT_KMAC_TABLE(size, internal_functions, internal_new)
891
892
KMAC_INTERNAL_TABLE(128);
893
KMAC_INTERNAL_TABLE(256);
894
#endif /* FIPS_MODULE */