Coverage Report

Created: 2025-08-25 06:30

/src/openssl/providers/implementations/macs/kmac_prov.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2018-2024 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
0
#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
0
#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
0
#define KMAC_MAX_KEY 512
103
0
#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
0
{
157
0
    struct kmac_data_st *kctx = vmacctx;
158
159
0
    if (kctx != NULL) {
160
0
        EVP_MD_CTX_free(kctx->ctx);
161
0
        ossl_prov_digest_reset(&kctx->digest);
162
0
        OPENSSL_cleanse(kctx->key, kctx->key_len);
163
0
        OPENSSL_cleanse(kctx->custom, kctx->custom_len);
164
0
        OPENSSL_free(kctx);
165
0
    }
166
0
}
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
0
{
175
0
    struct kmac_data_st *kctx;
176
177
0
    if (!ossl_prov_is_running())
178
0
        return NULL;
179
180
0
    if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL
181
0
            || (kctx->ctx = EVP_MD_CTX_new()) == NULL) {
182
0
        kmac_free(kctx);
183
0
        return NULL;
184
0
    }
185
0
    kctx->provctx = provctx;
186
0
    OSSL_FIPS_IND_INIT(kctx)
187
0
    return kctx;
188
0
}
189
190
static void *kmac_fetch_new(void *provctx, const OSSL_PARAM *params)
191
0
{
192
0
    struct kmac_data_st *kctx = kmac_new(provctx);
193
0
    int md_size;
194
195
0
    if (kctx == NULL)
196
0
        return 0;
197
0
    if (!ossl_prov_digest_load_from_params(&kctx->digest, params,
198
0
                                      PROV_LIBCTX_OF(provctx))) {
199
0
        kmac_free(kctx);
200
0
        return 0;
201
0
    }
202
203
0
    md_size = EVP_MD_get_size(ossl_prov_digest_md(&kctx->digest));
204
0
    if (md_size <= 0) {
205
0
        kmac_free(kctx);
206
0
        return 0;
207
0
    }
208
0
    kctx->out_len = (size_t)md_size;
209
0
    return kctx;
210
0
}
211
212
static void *kmac128_new(void *provctx)
213
0
{
214
0
    static const OSSL_PARAM kmac128_params[] = {
215
0
        OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC128,
216
0
                               sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC128)),
217
0
        OSSL_PARAM_END
218
0
    };
219
0
    return kmac_fetch_new(provctx, kmac128_params);
220
0
}
221
222
static void *kmac256_new(void *provctx)
223
0
{
224
0
    static const OSSL_PARAM kmac256_params[] = {
225
0
        OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC256,
226
0
                               sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC256)),
227
0
        OSSL_PARAM_END
228
0
    };
229
0
    return kmac_fetch_new(provctx, kmac256_params);
230
0
}
231
232
static void *kmac_dup(void *vsrc)
233
0
{
234
0
    struct kmac_data_st *src = vsrc;
235
0
    struct kmac_data_st *dst;
236
237
0
    if (!ossl_prov_is_running())
238
0
        return NULL;
239
240
0
    dst = kmac_new(src->provctx);
241
0
    if (dst == NULL)
242
0
        return NULL;
243
244
0
    if (!EVP_MD_CTX_copy(dst->ctx, src->ctx)
245
0
        || !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
0
    dst->out_len = src->out_len;
253
0
    dst->key_len = src->key_len;
254
0
    dst->custom_len = src->custom_len;
255
0
    dst->xof_mode = src->xof_mode;
256
0
    memcpy(dst->key, src->key, src->key_len);
257
0
    memcpy(dst->custom, src->custom, dst->custom_len);
258
0
    OSSL_FIPS_IND_COPY(dst, src)
259
260
0
    return dst;
261
0
}
262
263
static int kmac_setkey(struct kmac_data_st *kctx, const unsigned char *key,
264
                       size_t keylen)
265
0
{
266
0
    const EVP_MD *digest = ossl_prov_digest_md(&kctx->digest);
267
0
    int w = EVP_MD_get_block_size(digest);
268
269
0
    if (keylen < KMAC_MIN_KEY || keylen > KMAC_MAX_KEY) {
270
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
271
0
        return 0;
272
0
    }
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
0
    if (w <= 0) {
293
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
294
0
        return 0;
295
0
    }
296
0
    if (!kmac_bytepad_encode_key(kctx->key, sizeof(kctx->key), &kctx->key_len,
297
0
                                 key, keylen, (size_t)w))
298
0
        return 0;
299
0
    return 1;
300
0
}
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
0
{
310
0
    struct kmac_data_st *kctx = vmacctx;
311
0
    EVP_MD_CTX *ctx = kctx->ctx;
312
0
    unsigned char *out;
313
0
    size_t out_len, block_len;
314
0
    int res, t;
315
316
0
    if (!ossl_prov_is_running() || !kmac_set_ctx_params(kctx, params))
317
0
        return 0;
318
319
0
    if (key != NULL) {
320
0
        if (!kmac_setkey(kctx, key, keylen))
321
0
            return 0;
322
0
    } 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
0
    if (!EVP_DigestInit_ex(kctx->ctx, ossl_prov_digest_md(&kctx->digest),
328
0
                           NULL))
329
0
        return 0;
330
331
0
    t = EVP_MD_get_block_size(ossl_prov_digest_md(&kctx->digest));
332
0
    if (t <= 0) {
333
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
334
0
        return 0;
335
0
    }
336
0
    block_len = t;
337
338
    /* Set default custom string if it is not already set */
339
0
    if (kctx->custom_len == 0) {
340
0
        const OSSL_PARAM cparams[] = {
341
0
            OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, "", 0),
342
0
            OSSL_PARAM_END
343
0
        };
344
0
        (void)kmac_set_ctx_params(kctx, cparams);
345
0
    }
346
347
0
    if (!bytepad(NULL, &out_len, kmac_string, sizeof(kmac_string),
348
0
                 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
0
    out = OPENSSL_malloc(out_len);
353
0
    if (out == NULL)
354
0
        return 0;
355
0
    res = bytepad(out, NULL, kmac_string, sizeof(kmac_string),
356
0
                  kctx->custom, kctx->custom_len, block_len)
357
0
          && EVP_DigestUpdate(ctx, out, out_len)
358
0
          && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len);
359
0
    OPENSSL_free(out);
360
0
    return res;
361
0
}
362
363
static int kmac_update(void *vmacctx, const unsigned char *data,
364
                       size_t datalen)
365
0
{
366
0
    struct kmac_data_st *kctx = vmacctx;
367
368
0
    return EVP_DigestUpdate(kctx->ctx, data, datalen);
369
0
}
370
371
static int kmac_final(void *vmacctx, unsigned char *out, size_t *outl,
372
                      size_t outsize)
373
0
{
374
0
    struct kmac_data_st *kctx = vmacctx;
375
0
    EVP_MD_CTX *ctx = kctx->ctx;
376
0
    size_t lbits, len;
377
0
    unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];
378
0
    int ok;
379
380
0
    if (!ossl_prov_is_running())
381
0
        return 0;
382
383
    /* KMAC XOF mode sets the encoded length to 0 */
384
0
    lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));
385
386
0
    ok = right_encode(encoded_outlen, sizeof(encoded_outlen), &len, lbits)
387
0
        && EVP_DigestUpdate(ctx, encoded_outlen, len)
388
0
        && EVP_DigestFinalXOF(ctx, out, kctx->out_len);
389
0
    *outl = kctx->out_len;
390
0
    return ok;
391
0
}
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
0
{
419
0
    const char *s;
420
421
0
    memset(r, 0, sizeof(*r));
422
0
    if (p != NULL)
423
0
        for (; (s = p->key) != NULL; p++)
424
0
            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
                    /* 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
                    /* 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
0
            case 's':
452
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
453
                    /* MAC_PARAM_SIZE */
454
0
                    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
0
                    r->size = (OSSL_PARAM *)p;
460
0
                }
461
0
            }
462
0
    return 1;
463
0
}
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
0
{
475
0
    struct kmac_data_st *kctx = vmacctx;
476
0
    struct kmac_get_ctx_params_st p;
477
0
    int sz;
478
479
0
    if (kctx == NULL || !kmac_get_ctx_params_decoder(params, &p))
480
0
        return 0;
481
482
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, kctx->out_len))
483
0
        return 0;
484
485
0
    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
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(kctx, p.ind))
492
0
        return 0;
493
494
0
    return 1;
495
0
}
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
0
{
533
0
    const char *s;
534
535
0
    memset(r, 0, sizeof(*r));
536
0
    if (p != NULL)
537
0
        for (; (s = p->key) != NULL; p++)
538
0
            switch(s[0]) {
539
0
            default:
540
0
                break;
541
0
            case 'c':
542
0
                if (ossl_likely(strcmp("ustom", s + 1) == 0)) {
543
                    /* MAC_PARAM_CUSTOM */
544
0
                    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
0
                    r->custom = (OSSL_PARAM *)p;
550
0
                }
551
0
                break;
552
0
            case 'k':
553
0
                switch(s[1]) {
554
0
                default:
555
0
                    break;
556
0
                case 'e':
557
0
                    switch(s[2]) {
558
0
                    default:
559
0
                        break;
560
0
                    case 'y':
561
0
                        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
                                /* 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
0
                        case '\0':
578
0
                            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
0
                            r->key = (OSSL_PARAM *)p;
584
0
                        }
585
0
                    }
586
0
                }
587
0
                break;
588
0
            case 'n':
589
# if defined(FIPS_MODULE)
590
                if (ossl_likely(strcmp("o-short-mac", s + 1) == 0)) {
591
                    /* 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
0
            case 's':
602
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
603
                    /* MAC_PARAM_SIZE */
604
0
                    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
0
                    r->size = (OSSL_PARAM *)p;
610
0
                }
611
0
                break;
612
0
            case 'x':
613
0
                if (ossl_likely(strcmp("of", s + 1) == 0)) {
614
                    /* MAC_PARAM_XOF */
615
0
                    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
0
                    r->xof = (OSSL_PARAM *)p;
621
0
                }
622
0
            }
623
0
    return 1;
624
0
}
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
0
{
631
0
    return kmac_set_ctx_params_list;
632
0
}
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
0
{
645
0
    struct kmac_data_st *kctx = vmacctx;
646
0
    struct kmac_set_ctx_params_st p;
647
648
0
    if (kctx == NULL || !kmac_set_ctx_params_decoder(params, &p))
649
0
        return 0;
650
651
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE0,
652
0
                                          p.ind_sht))
653
0
        return 0;
654
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k))
655
0
        return 0;
656
657
0
    if (p.xof != NULL && !OSSL_PARAM_get_int(p.xof, &kctx->xof_mode))
658
0
        return 0;
659
660
0
    if (p.size != NULL) {
661
0
        size_t sz = 0;
662
663
0
        if (!OSSL_PARAM_get_size_t(p.size, &sz))
664
0
            return 0;
665
0
        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
0
        kctx->out_len = sz;
682
0
    }
683
684
0
    if (p.key != NULL)
685
0
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
686
0
                || !kmac_setkey(kctx, p.key->data, p.key->data_size))
687
0
            return 0;
688
689
0
    if (p.custom != NULL) {
690
0
        if (p.custom->data_type != OSSL_PARAM_OCTET_STRING)
691
0
            return 0;
692
0
        if (p.custom->data_size > KMAC_MAX_CUSTOM) {
693
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
694
0
            return 0;
695
0
        }
696
0
        if (!encode_string(kctx->custom, sizeof(kctx->custom), &kctx->custom_len,
697
0
                           p.custom->data, p.custom->data_size))
698
0
            return 0;
699
0
    }
700
701
0
    return 1;
702
0
}
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
0
{
709
0
    unsigned int cnt = 0, sz = sizeof(size_t);
710
711
0
    while (bits && (cnt < sz)) {
712
0
        ++cnt;
713
0
        bits >>= 8;
714
0
    }
715
    /* If bits is zero 1 byte is required */
716
0
    if (cnt == 0)
717
0
        cnt = 1;
718
0
    return cnt;
719
0
}
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
0
{
731
0
    unsigned int len = get_encode_size(bits);
732
0
    int i;
733
734
0
    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
0
    for (i = len - 1; i >= 0; --i) {
741
0
        out[i] = (unsigned char)(bits & 0xFF);
742
0
        bits >>= 8;
743
0
    }
744
    /* Tack the length onto the end */
745
0
    out[len] = (unsigned char)len;
746
747
    /* The Returned length includes the tacked on byte */
748
0
    *out_len = len + 1;
749
0
    return 1;
750
0
}
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
0
{
762
0
    if (in == NULL) {
763
0
        *out_len = 0;
764
0
    } else {
765
0
        size_t i, bits, len, sz;
766
767
0
        bits = 8 * in_len;
768
0
        len = get_encode_size(bits);
769
0
        sz = 1 + len + in_len;
770
771
0
        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
0
        out[0] = (unsigned char)len;
777
0
        for (i = len; i > 0; --i) {
778
0
            out[i] = (bits & 0xFF);
779
0
            bits >>= 8;
780
0
        }
781
0
        memcpy(out + len + 1, in, in_len);
782
0
        *out_len = sz;
783
0
    }
784
0
    return 1;
785
0
}
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
0
{
799
0
    size_t len;
800
0
    unsigned char *p = out;
801
0
    size_t sz = w;
802
803
0
    if (out == NULL) {
804
0
        if (out_len == NULL) {
805
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
806
0
            return 0;
807
0
        }
808
0
        sz = 2 + in1_len + (in2 != NULL ? in2_len : 0);
809
0
        *out_len = (sz + w - 1) / w * w;
810
0
        return 1;
811
0
    }
812
813
0
    if (!ossl_assert(w <= 255))
814
0
        return 0;
815
816
    /* Left encoded w */
817
0
    *p++ = 1;
818
0
    *p++ = (unsigned char)w;
819
    /* || in1 */
820
0
    memcpy(p, in1, in1_len);
821
0
    p += in1_len;
822
    /* [ || in2 ] */
823
0
    if (in2 != NULL && in2_len > 0) {
824
0
        memcpy(p, in2, in2_len);
825
0
        p += in2_len;
826
0
    }
827
    /* Figure out the pad size (divisible by w) */
828
0
    len = p - out;
829
0
    sz = (len + w - 1) / w * w;
830
    /* zero pad the end of the buffer */
831
0
    if (sz != len)
832
0
        memset(p, 0, sz - len);
833
0
    if (out_len != NULL)
834
0
        *out_len = sz;
835
0
    return 1;
836
0
}
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
0
{
844
0
    unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN];
845
0
    size_t tmp_len;
846
847
0
    if (!encode_string(tmp, sizeof(tmp), &tmp_len, in, in_len))
848
0
        return 0;
849
0
    if (!bytepad(NULL, out_len, tmp, tmp_len, NULL, 0, w))
850
0
        return 0;
851
0
    if (!ossl_assert(*out_len <= out_max_len))
852
0
        return 0;
853
0
    return bytepad(out, NULL, tmp, tmp_len, NULL, 0, w);
854
0
}
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 */