Coverage Report

Created: 2025-10-28 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
 * See SP800-185 "Appendix A - KMAC, .... in Terms of Keccak[c]"
12
 *
13
 * Inputs are:
14
 *    K = Key                  (len(K) < 2^2040 bits)
15
 *    X = Input
16
 *    L = Output length        (0 <= L < 2^2040 bits)
17
 *    S = Customization String Default="" (len(S) < 2^2040 bits)
18
 *
19
 * KMAC128(K, X, L, S)
20
 * {
21
 *     newX = bytepad(encode_string(K), 168) ||  X || right_encode(L).
22
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 168).
23
 *     return KECCAK[256](T || newX || 00, L).
24
 * }
25
 *
26
 * KMAC256(K, X, L, S)
27
 * {
28
 *     newX = bytepad(encode_string(K), 136) ||  X || right_encode(L).
29
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 136).
30
 *     return KECCAK[512](T || newX || 00, L).
31
 * }
32
 *
33
 * KMAC128XOF(K, X, L, S)
34
 * {
35
 *     newX = bytepad(encode_string(K), 168) ||  X || right_encode(0).
36
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 168).
37
 *     return KECCAK[256](T || newX || 00, L).
38
 * }
39
 *
40
 * KMAC256XOF(K, X, L, S)
41
 * {
42
 *     newX = bytepad(encode_string(K), 136) ||  X || right_encode(0).
43
 *     T = bytepad(encode_string("KMAC") || encode_string(S), 136).
44
 *     return KECCAK[512](T || newX || 00, L).
45
 * }
46
 *
47
 */
48
49
#include <stdlib.h>
50
#include <string.h>
51
#include <openssl/core_dispatch.h>
52
#include <openssl/core_names.h>
53
#include <openssl/params.h>
54
#include <openssl/evp.h>
55
#include <openssl/err.h>
56
#include <openssl/proverr.h>
57
#include <openssl/fips_names.h>
58
#include "prov/securitycheck.h"
59
#include "prov/implementations.h"
60
#include "prov/provider_ctx.h"
61
#include "prov/provider_util.h"
62
#include "prov/providercommon.h"
63
#include "internal/cryptlib.h" /* ossl_assert */
64
#include "providers/implementations/macs/kmac_prov.inc"
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
#define kmac_new_list
191
192
static void *kmac_fetch_new(void *provctx, const OSSL_PARAM *params)
193
0
{
194
0
    struct kmac_data_st *kctx = kmac_new(provctx);
195
0
    struct kmac_new_st p;
196
0
    int md_size;
197
198
0
    if (kctx == NULL)
199
0
        return 0;
200
0
    if (!kmac_new_decoder(params, &p))
201
0
        goto err;
202
0
    if (!ossl_prov_digest_load(&kctx->digest, p.digest, p.propq, p.engine,
203
0
                               PROV_LIBCTX_OF(provctx)))
204
0
        goto err;
205
206
0
    md_size = EVP_MD_get_size(ossl_prov_digest_md(&kctx->digest));
207
0
    if (md_size <= 0)
208
0
        goto err;
209
0
    kctx->out_len = (size_t)md_size;
210
0
    return kctx;
211
212
0
err:
213
0
    kmac_free(kctx);
214
0
    return NULL;
215
0
}
216
217
static void *kmac128_new(void *provctx)
218
0
{
219
0
    static const OSSL_PARAM kmac128_params[] = {
220
0
        OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC128,
221
0
                               sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC128)),
222
0
        OSSL_PARAM_END
223
0
    };
224
0
    return kmac_fetch_new(provctx, kmac128_params);
225
0
}
226
227
static void *kmac256_new(void *provctx)
228
0
{
229
0
    static const OSSL_PARAM kmac256_params[] = {
230
0
        OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC256,
231
0
                               sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC256)),
232
0
        OSSL_PARAM_END
233
0
    };
234
0
    return kmac_fetch_new(provctx, kmac256_params);
235
0
}
236
237
static void *kmac_dup(void *vsrc)
238
0
{
239
0
    struct kmac_data_st *src = vsrc;
240
0
    struct kmac_data_st *dst;
241
242
0
    if (!ossl_prov_is_running())
243
0
        return NULL;
244
245
0
    dst = kmac_new(src->provctx);
246
0
    if (dst == NULL)
247
0
        return NULL;
248
249
0
    if (!EVP_MD_CTX_copy(dst->ctx, src->ctx)
250
0
        || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
251
0
        kmac_free(dst);
252
0
        return NULL;
253
0
    }
254
#ifdef FIPS_MODULE
255
    dst->internal = src->internal;
256
#endif
257
0
    dst->out_len = src->out_len;
258
0
    dst->key_len = src->key_len;
259
0
    dst->custom_len = src->custom_len;
260
0
    dst->xof_mode = src->xof_mode;
261
0
    memcpy(dst->key, src->key, src->key_len);
262
0
    memcpy(dst->custom, src->custom, dst->custom_len);
263
0
    OSSL_FIPS_IND_COPY(dst, src)
264
265
0
    return dst;
266
0
}
267
268
static int kmac_setkey(struct kmac_data_st *kctx, const unsigned char *key,
269
                       size_t keylen)
270
0
{
271
0
    const EVP_MD *digest = ossl_prov_digest_md(&kctx->digest);
272
0
    int w = EVP_MD_get_block_size(digest);
273
274
0
    if (keylen < KMAC_MIN_KEY || keylen > KMAC_MAX_KEY) {
275
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
276
0
        return 0;
277
0
    }
278
#ifdef FIPS_MODULE
279
    /*
280
     * Only do the key check if KMAC is fetched directly.
281
     * Other algorithms that embed KMAC such as SSKDF will ignore this check.
282
     */
283
    if (!kctx->internal) {
284
        int approved = ossl_mac_check_key_size(keylen);
285
286
        if (!approved) {
287
            if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE1,
288
                                             PROV_LIBCTX_OF(kctx->provctx),
289
                                             "KMAC", "Key size",
290
                                             ossl_fips_config_kmac_key_check)) {
291
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
292
                return 0;
293
            }
294
        }
295
    }
296
#endif
297
0
    if (w <= 0) {
298
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
299
0
        return 0;
300
0
    }
301
0
    if (!kmac_bytepad_encode_key(kctx->key, sizeof(kctx->key), &kctx->key_len,
302
0
                                 key, keylen, (size_t)w))
303
0
        return 0;
304
0
    return 1;
305
0
}
306
307
/*
308
 * The init() assumes that any ctrl methods are set beforehand for
309
 * md, key and custom. Setting the fields afterwards will have no
310
 * effect on the output mac.
311
 */
312
static int kmac_init(void *vmacctx, const unsigned char *key,
313
                     size_t keylen, const OSSL_PARAM params[])
314
0
{
315
0
    struct kmac_data_st *kctx = vmacctx;
316
0
    EVP_MD_CTX *ctx = kctx->ctx;
317
0
    unsigned char *out;
318
0
    size_t out_len, block_len;
319
0
    int res, t;
320
321
0
    if (!ossl_prov_is_running() || !kmac_set_ctx_params(kctx, params))
322
0
        return 0;
323
324
0
    if (key != NULL) {
325
0
        if (!kmac_setkey(kctx, key, keylen))
326
0
            return 0;
327
0
    } else if (kctx->key_len == 0) {
328
        /* Check key has been set */
329
0
        ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
330
0
        return 0;
331
0
    }
332
0
    if (!EVP_DigestInit_ex(kctx->ctx, ossl_prov_digest_md(&kctx->digest),
333
0
                           NULL))
334
0
        return 0;
335
336
0
    t = EVP_MD_get_block_size(ossl_prov_digest_md(&kctx->digest));
337
0
    if (t <= 0) {
338
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
339
0
        return 0;
340
0
    }
341
0
    block_len = t;
342
343
    /* Set default custom string if it is not already set */
344
0
    if (kctx->custom_len == 0) {
345
0
        const OSSL_PARAM cparams[] = {
346
0
            OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, "", 0),
347
0
            OSSL_PARAM_END
348
0
        };
349
0
        (void)kmac_set_ctx_params(kctx, cparams);
350
0
    }
351
352
0
    if (!bytepad(NULL, &out_len, kmac_string, sizeof(kmac_string),
353
0
                 kctx->custom, kctx->custom_len, block_len)) {
354
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
355
0
        return 0;
356
0
    }
357
0
    out = OPENSSL_malloc(out_len);
358
0
    if (out == NULL)
359
0
        return 0;
360
0
    res = bytepad(out, NULL, kmac_string, sizeof(kmac_string),
361
0
                  kctx->custom, kctx->custom_len, block_len)
362
0
          && EVP_DigestUpdate(ctx, out, out_len)
363
0
          && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len);
364
0
    OPENSSL_free(out);
365
0
    return res;
366
0
}
367
368
static int kmac_update(void *vmacctx, const unsigned char *data,
369
                       size_t datalen)
370
0
{
371
0
    struct kmac_data_st *kctx = vmacctx;
372
373
0
    return EVP_DigestUpdate(kctx->ctx, data, datalen);
374
0
}
375
376
static int kmac_final(void *vmacctx, unsigned char *out, size_t *outl,
377
                      size_t outsize)
378
0
{
379
0
    struct kmac_data_st *kctx = vmacctx;
380
0
    EVP_MD_CTX *ctx = kctx->ctx;
381
0
    size_t lbits, len;
382
0
    unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];
383
0
    int ok;
384
385
0
    if (!ossl_prov_is_running())
386
0
        return 0;
387
388
    /* KMAC XOF mode sets the encoded length to 0 */
389
0
    lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));
390
391
0
    ok = right_encode(encoded_outlen, sizeof(encoded_outlen), &len, lbits)
392
0
        && EVP_DigestUpdate(ctx, encoded_outlen, len)
393
0
        && EVP_DigestFinalXOF(ctx, out, kctx->out_len);
394
0
    *outl = kctx->out_len;
395
0
    return ok;
396
0
}
397
398
static const OSSL_PARAM *kmac_gettable_ctx_params(ossl_unused void *ctx,
399
                                                  ossl_unused void *provctx)
400
0
{
401
0
    return kmac_get_ctx_params_list;
402
0
}
403
404
static int kmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
405
0
{
406
0
    struct kmac_data_st *kctx = vmacctx;
407
0
    struct kmac_get_ctx_params_st p;
408
0
    int sz;
409
410
0
    if (kctx == NULL || !kmac_get_ctx_params_decoder(params, &p))
411
0
        return 0;
412
413
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, kctx->out_len))
414
0
        return 0;
415
416
0
    if (p.bsize != NULL) {
417
0
        sz = EVP_MD_block_size(ossl_prov_digest_md(&kctx->digest));
418
0
        if (!OSSL_PARAM_set_int(p.bsize, sz))
419
0
            return 0;
420
0
    }
421
422
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(kctx, p.ind))
423
0
        return 0;
424
425
0
    return 1;
426
0
}
427
428
static const OSSL_PARAM *kmac_settable_ctx_params(ossl_unused void *ctx,
429
                                                  ossl_unused void *provctx)
430
0
{
431
0
    return kmac_set_ctx_params_list;
432
0
}
433
434
/*
435
 * The following params can be set any time before final():
436
 *     - "outlen" or "size":    The requested output length.
437
 *     - "xof":                 If set, this indicates that right_encoded(0)
438
 *                              is part of the digested data, otherwise it
439
 *                              uses right_encoded(requested output length).
440
 *
441
 * All other params should be set before init().
442
 */
443
static int kmac_set_ctx_params(void *vmacctx, const OSSL_PARAM *params)
444
0
{
445
0
    struct kmac_data_st *kctx = vmacctx;
446
0
    struct kmac_set_ctx_params_st p;
447
448
0
    if (kctx == NULL || !kmac_set_ctx_params_decoder(params, &p))
449
0
        return 0;
450
451
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE0,
452
0
                                          p.ind_sht))
453
0
        return 0;
454
0
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k))
455
0
        return 0;
456
457
0
    if (p.xof != NULL && !OSSL_PARAM_get_int(p.xof, &kctx->xof_mode))
458
0
        return 0;
459
460
0
    if (p.size != NULL) {
461
0
        size_t sz = 0;
462
463
0
        if (!OSSL_PARAM_get_size_t(p.size, &sz))
464
0
            return 0;
465
0
        if (sz > KMAC_MAX_OUTPUT_LEN) {
466
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
467
0
            return 0;
468
0
        }
469
#ifdef FIPS_MODULE
470
        /* SP 800-185 8.4.2 mandates a minimum of 32 bits of output */
471
        if (sz < 32 / 8) {
472
            if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE0,
473
                                             PROV_LIBCTX_OF(kctx->provctx),
474
                                             "KMAC", "length",
475
                                             ossl_fips_config_no_short_mac)) {
476
                ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH);
477
                return 0;
478
            }
479
        }
480
#endif
481
0
        kctx->out_len = sz;
482
0
    }
483
484
0
    if (p.key != NULL)
485
0
        if (p.key->data_type != OSSL_PARAM_OCTET_STRING
486
0
                || !kmac_setkey(kctx, p.key->data, p.key->data_size))
487
0
            return 0;
488
489
0
    if (p.custom != NULL) {
490
0
        if (p.custom->data_type != OSSL_PARAM_OCTET_STRING)
491
0
            return 0;
492
0
        if (p.custom->data_size > KMAC_MAX_CUSTOM) {
493
0
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH);
494
0
            return 0;
495
0
        }
496
0
        if (!encode_string(kctx->custom, sizeof(kctx->custom), &kctx->custom_len,
497
0
                           p.custom->data, p.custom->data_size))
498
0
            return 0;
499
0
    }
500
501
0
    return 1;
502
0
}
503
504
/* Encoding/Padding Methods. */
505
506
/* Returns the number of bytes required to store 'bits' into a byte array */
507
static unsigned int get_encode_size(size_t bits)
508
0
{
509
0
    unsigned int cnt = 0, sz = sizeof(size_t);
510
511
0
    while (bits && (cnt < sz)) {
512
0
        ++cnt;
513
0
        bits >>= 8;
514
0
    }
515
    /* If bits is zero 1 byte is required */
516
0
    if (cnt == 0)
517
0
        cnt = 1;
518
0
    return cnt;
519
0
}
520
521
/*
522
 * Convert an integer into bytes . The number of bytes is appended
523
 * to the end of the buffer. Returns an array of bytes 'out' of size
524
 * *out_len.
525
 *
526
 * e.g if bits = 32, out[2] = { 0x20, 0x01 }
527
 */
528
static int right_encode(unsigned char *out, size_t out_max_len, size_t *out_len,
529
                        size_t bits)
530
0
{
531
0
    unsigned int len = get_encode_size(bits);
532
0
    int i;
533
534
0
    if (len >= out_max_len) {
535
0
        ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
536
0
        return 0;
537
0
    }
538
539
    /* MSB's are at the start of the bytes array */
540
0
    for (i = len - 1; i >= 0; --i) {
541
0
        out[i] = (unsigned char)(bits & 0xFF);
542
0
        bits >>= 8;
543
0
    }
544
    /* Tack the length onto the end */
545
0
    out[len] = (unsigned char)len;
546
547
    /* The Returned length includes the tacked on byte */
548
0
    *out_len = len + 1;
549
0
    return 1;
550
0
}
551
552
/*
553
 * Encodes a string with a left encoded length added. Note that the
554
 * in_len is converted to bits (*8).
555
 *
556
 * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
557
 *                                 len   bits    K     M     A     C
558
 */
559
static int encode_string(unsigned char *out, size_t out_max_len, size_t *out_len,
560
                         const unsigned char *in, size_t in_len)
561
0
{
562
0
    if (in == NULL) {
563
0
        *out_len = 0;
564
0
    } else {
565
0
        size_t i, bits, len, sz;
566
567
0
        bits = 8 * in_len;
568
0
        len = get_encode_size(bits);
569
0
        sz = 1 + len + in_len;
570
571
0
        if (sz > out_max_len) {
572
0
            ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
573
0
            return 0;
574
0
        }
575
576
0
        out[0] = (unsigned char)len;
577
0
        for (i = len; i > 0; --i) {
578
0
            out[i] = (bits & 0xFF);
579
0
            bits >>= 8;
580
0
        }
581
0
        memcpy(out + len + 1, in, in_len);
582
0
        *out_len = sz;
583
0
    }
584
0
    return 1;
585
0
}
586
587
/*
588
 * Returns a zero padded encoding of the inputs in1 and an optional
589
 * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'.
590
 * The value of w is in bytes (< 256).
591
 *
592
 * The returned output is:
593
 *    zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
594
 */
595
static int bytepad(unsigned char *out, size_t *out_len,
596
                   const unsigned char *in1, size_t in1_len,
597
                   const unsigned char *in2, size_t in2_len, size_t w)
598
0
{
599
0
    size_t len;
600
0
    unsigned char *p = out;
601
0
    size_t sz = w;
602
603
0
    if (out == NULL) {
604
0
        if (out_len == NULL) {
605
0
            ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
606
0
            return 0;
607
0
        }
608
0
        sz = 2 + in1_len + (in2 != NULL ? in2_len : 0);
609
0
        *out_len = (sz + w - 1) / w * w;
610
0
        return 1;
611
0
    }
612
613
0
    if (!ossl_assert(w <= 255))
614
0
        return 0;
615
616
    /* Left encoded w */
617
0
    *p++ = 1;
618
0
    *p++ = (unsigned char)w;
619
    /* || in1 */
620
0
    memcpy(p, in1, in1_len);
621
0
    p += in1_len;
622
    /* [ || in2 ] */
623
0
    if (in2 != NULL && in2_len > 0) {
624
0
        memcpy(p, in2, in2_len);
625
0
        p += in2_len;
626
0
    }
627
    /* Figure out the pad size (divisible by w) */
628
0
    len = p - out;
629
0
    sz = (len + w - 1) / w * w;
630
    /* zero pad the end of the buffer */
631
0
    if (sz != len)
632
0
        memset(p, 0, sz - len);
633
0
    if (out_len != NULL)
634
0
        *out_len = sz;
635
0
    return 1;
636
0
}
637
638
/* Returns out = bytepad(encode_string(in), w) */
639
static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len,
640
                                   size_t *out_len,
641
                                   const unsigned char *in, size_t in_len,
642
                                   size_t w)
643
0
{
644
0
    unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN];
645
0
    size_t tmp_len;
646
647
0
    if (!encode_string(tmp, sizeof(tmp), &tmp_len, in, in_len))
648
0
        return 0;
649
0
    if (!bytepad(NULL, out_len, tmp, tmp_len, NULL, 0, w))
650
0
        return 0;
651
0
    if (!ossl_assert(*out_len <= out_max_len))
652
0
        return 0;
653
0
    return bytepad(out, NULL, tmp, tmp_len, NULL, 0, w);
654
0
}
655
656
#define IMPLEMENT_KMAC_TABLE(size, funcname, newname)                          \
657
const OSSL_DISPATCH ossl_kmac##size##_##funcname[] =                           \
658
{                                                                              \
659
    { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))kmac##size##_##newname },          \
660
    { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))kmac_dup },                        \
661
    { OSSL_FUNC_MAC_FREECTX, (void (*)(void))kmac_free },                      \
662
    { OSSL_FUNC_MAC_INIT, (void (*)(void))kmac_init },                         \
663
    { OSSL_FUNC_MAC_UPDATE, (void (*)(void))kmac_update },                     \
664
    { OSSL_FUNC_MAC_FINAL, (void (*)(void))kmac_final },                       \
665
    { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,                                       \
666
      (void (*)(void))kmac_gettable_ctx_params },                              \
667
    { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))kmac_get_ctx_params },     \
668
    { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,                                       \
669
      (void (*)(void))kmac_settable_ctx_params },                              \
670
    { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))kmac_set_ctx_params },     \
671
    OSSL_DISPATCH_END                                                          \
672
}
673
674
#define KMAC_TABLE(size) IMPLEMENT_KMAC_TABLE(size, functions, new)
675
676
KMAC_TABLE(128);
677
KMAC_TABLE(256);
678
679
#ifdef FIPS_MODULE
680
# define KMAC_INTERNAL_TABLE(size)                                             \
681
static OSSL_FUNC_mac_newctx_fn kmac##size##_internal_new;                      \
682
static void *kmac##size##_internal_new(void *provctx)                          \
683
{                                                                              \
684
    struct kmac_data_st *macctx = kmac##size##_new(provctx);                   \
685
                                                                               \
686
    if (macctx != NULL)                                                        \
687
        macctx->internal = 1;                                                  \
688
    return macctx;                                                             \
689
}                                                                              \
690
IMPLEMENT_KMAC_TABLE(size, internal_functions, internal_new)
691
692
KMAC_INTERNAL_TABLE(128);
693
KMAC_INTERNAL_TABLE(256);
694
#endif /* FIPS_MODULE */