Home | History | Annotate | Line # | Download | only in kdfs
      1  1.1  christos /*
      2  1.1  christos  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1  christos  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
      4  1.1  christos  *
      5  1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6  1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      7  1.1  christos  * in the file LICENSE in the source distribution or at
      8  1.1  christos  * https://www.openssl.org/source/license.html
      9  1.1  christos  */
     10  1.1  christos 
     11  1.1  christos /*
     12  1.1  christos  * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
     13  1.1  christos  * Section 4.1.
     14  1.1  christos  *
     15  1.1  christos  * The Single Step KDF algorithm is given by:
     16  1.1  christos  *
     17  1.1  christos  * Result(0) = empty bit string (i.e., the null string).
     18  1.1  christos  * For i = 1 to reps, do the following:
     19  1.1  christos  *   Increment counter by 1.
     20  1.1  christos  *   Result(i) = Result(i - 1) || H(counter || Z || FixedInfo).
     21  1.1  christos  * DKM = LeftmostBits(Result(reps), L))
     22  1.1  christos  *
     23  1.1  christos  * NOTES:
     24  1.1  christos  *   Z is a shared secret required to produce the derived key material.
     25  1.1  christos  *   counter is a 4 byte buffer.
     26  1.1  christos  *   FixedInfo is a bit string containing context specific data.
     27  1.1  christos  *   DKM is the output derived key material.
     28  1.1  christos  *   L is the required size of the DKM.
     29  1.1  christos  *   reps = [L / H_outputBits]
     30  1.1  christos  *   H(x) is the auxiliary function that can be either a hash, HMAC or KMAC.
     31  1.1  christos  *   H_outputBits is the length of the output of the auxiliary function H(x).
     32  1.1  christos  *
     33  1.1  christos  * Currently there is not a comprehensive list of test vectors for this
     34  1.1  christos  * algorithm, especially for H(x) = HMAC and H(x) = KMAC.
     35  1.1  christos  * Test vectors for H(x) = Hash are indirectly used by CAVS KAS tests.
     36  1.1  christos  */
     37  1.1  christos #include <stdlib.h>
     38  1.1  christos #include <stdarg.h>
     39  1.1  christos #include <string.h>
     40  1.1  christos #include <openssl/hmac.h>
     41  1.1  christos #include <openssl/evp.h>
     42  1.1  christos #include <openssl/kdf.h>
     43  1.1  christos #include <openssl/core_names.h>
     44  1.1  christos #include <openssl/params.h>
     45  1.1  christos #include <openssl/proverr.h>
     46  1.1  christos #include "internal/cryptlib.h"
     47  1.1  christos #include "internal/numbers.h"
     48  1.1  christos #include "crypto/evp.h"
     49  1.1  christos #include "prov/provider_ctx.h"
     50  1.1  christos #include "prov/providercommon.h"
     51  1.1  christos #include "prov/implementations.h"
     52  1.1  christos #include "prov/provider_util.h"
     53  1.1  christos 
     54  1.1  christos typedef struct {
     55  1.1  christos     void *provctx;
     56  1.1  christos     EVP_MAC_CTX *macctx;         /* H(x) = HMAC_hash OR H(x) = KMAC */
     57  1.1  christos     PROV_DIGEST digest;          /* H(x) = hash(x) */
     58  1.1  christos     unsigned char *secret;
     59  1.1  christos     size_t secret_len;
     60  1.1  christos     unsigned char *info;
     61  1.1  christos     size_t info_len;
     62  1.1  christos     unsigned char *salt;
     63  1.1  christos     size_t salt_len;
     64  1.1  christos     size_t out_len; /* optional KMAC parameter */
     65  1.1  christos     int is_kmac;
     66  1.1  christos } KDF_SSKDF;
     67  1.1  christos 
     68  1.1  christos #define SSKDF_MAX_INLEN (1<<30)
     69  1.1  christos #define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4)
     70  1.1  christos #define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4)
     71  1.1  christos 
     72  1.1  christos /* KMAC uses a Customisation string of 'KDF' */
     73  1.1  christos static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 };
     74  1.1  christos 
     75  1.1  christos static OSSL_FUNC_kdf_newctx_fn sskdf_new;
     76  1.1  christos static OSSL_FUNC_kdf_freectx_fn sskdf_free;
     77  1.1  christos static OSSL_FUNC_kdf_reset_fn sskdf_reset;
     78  1.1  christos static OSSL_FUNC_kdf_derive_fn sskdf_derive;
     79  1.1  christos static OSSL_FUNC_kdf_derive_fn x963kdf_derive;
     80  1.1  christos static OSSL_FUNC_kdf_settable_ctx_params_fn sskdf_settable_ctx_params;
     81  1.1  christos static OSSL_FUNC_kdf_set_ctx_params_fn sskdf_set_ctx_params;
     82  1.1  christos static OSSL_FUNC_kdf_gettable_ctx_params_fn sskdf_gettable_ctx_params;
     83  1.1  christos static OSSL_FUNC_kdf_get_ctx_params_fn sskdf_get_ctx_params;
     84  1.1  christos 
     85  1.1  christos /*
     86  1.1  christos  * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
     87  1.1  christos  * Section 4. One-Step Key Derivation using H(x) = hash(x)
     88  1.1  christos  * Note: X9.63 also uses this code with the only difference being that the
     89  1.1  christos  * counter is appended to the secret 'z'.
     90  1.1  christos  * i.e.
     91  1.1  christos  *   result[i] = Hash(counter || z || info) for One Step OR
     92  1.1  christos  *   result[i] = Hash(z || counter || info) for X9.63.
     93  1.1  christos  */
     94  1.1  christos static int SSKDF_hash_kdm(const EVP_MD *kdf_md,
     95  1.1  christos                           const unsigned char *z, size_t z_len,
     96  1.1  christos                           const unsigned char *info, size_t info_len,
     97  1.1  christos                           unsigned int append_ctr,
     98  1.1  christos                           unsigned char *derived_key, size_t derived_key_len)
     99  1.1  christos {
    100  1.1  christos     int ret = 0, hlen;
    101  1.1  christos     size_t counter, out_len, len = derived_key_len;
    102  1.1  christos     unsigned char c[4];
    103  1.1  christos     unsigned char mac[EVP_MAX_MD_SIZE];
    104  1.1  christos     unsigned char *out = derived_key;
    105  1.1  christos     EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
    106  1.1  christos 
    107  1.1  christos     if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
    108  1.1  christos             || derived_key_len > SSKDF_MAX_INLEN
    109  1.1  christos             || derived_key_len == 0)
    110  1.1  christos         return 0;
    111  1.1  christos 
    112  1.1  christos     hlen = EVP_MD_get_size(kdf_md);
    113  1.1  christos     if (hlen <= 0)
    114  1.1  christos         return 0;
    115  1.1  christos     out_len = (size_t)hlen;
    116  1.1  christos 
    117  1.1  christos     ctx = EVP_MD_CTX_create();
    118  1.1  christos     ctx_init = EVP_MD_CTX_create();
    119  1.1  christos     if (ctx == NULL || ctx_init == NULL)
    120  1.1  christos         goto end;
    121  1.1  christos 
    122  1.1  christos     if (!EVP_DigestInit(ctx_init, kdf_md))
    123  1.1  christos         goto end;
    124  1.1  christos 
    125  1.1  christos     for (counter = 1;; counter++) {
    126  1.1  christos         c[0] = (unsigned char)((counter >> 24) & 0xff);
    127  1.1  christos         c[1] = (unsigned char)((counter >> 16) & 0xff);
    128  1.1  christos         c[2] = (unsigned char)((counter >> 8) & 0xff);
    129  1.1  christos         c[3] = (unsigned char)(counter & 0xff);
    130  1.1  christos 
    131  1.1  christos         if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init)
    132  1.1  christos                 && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
    133  1.1  christos                 && EVP_DigestUpdate(ctx, z, z_len)
    134  1.1  christos                 && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
    135  1.1  christos                 && EVP_DigestUpdate(ctx, info, info_len)))
    136  1.1  christos             goto end;
    137  1.1  christos         if (len >= out_len) {
    138  1.1  christos             if (!EVP_DigestFinal_ex(ctx, out, NULL))
    139  1.1  christos                 goto end;
    140  1.1  christos             out += out_len;
    141  1.1  christos             len -= out_len;
    142  1.1  christos             if (len == 0)
    143  1.1  christos                 break;
    144  1.1  christos         } else {
    145  1.1  christos             if (!EVP_DigestFinal_ex(ctx, mac, NULL))
    146  1.1  christos                 goto end;
    147  1.1  christos             memcpy(out, mac, len);
    148  1.1  christos             break;
    149  1.1  christos         }
    150  1.1  christos     }
    151  1.1  christos     ret = 1;
    152  1.1  christos end:
    153  1.1  christos     EVP_MD_CTX_destroy(ctx);
    154  1.1  christos     EVP_MD_CTX_destroy(ctx_init);
    155  1.1  christos     OPENSSL_cleanse(mac, sizeof(mac));
    156  1.1  christos     return ret;
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom,
    160  1.1  christos                      size_t custom_len, size_t kmac_out_len,
    161  1.1  christos                      size_t derived_key_len, unsigned char **out)
    162  1.1  christos {
    163  1.1  christos     OSSL_PARAM params[2];
    164  1.1  christos 
    165  1.1  christos     /* Only KMAC has custom data - so return if not KMAC */
    166  1.1  christos     if (custom == NULL)
    167  1.1  christos         return 1;
    168  1.1  christos 
    169  1.1  christos     params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM,
    170  1.1  christos                                                   (void *)custom, custom_len);
    171  1.1  christos     params[1] = OSSL_PARAM_construct_end();
    172  1.1  christos 
    173  1.1  christos     if (!EVP_MAC_CTX_set_params(ctx, params))
    174  1.1  christos         return 0;
    175  1.1  christos 
    176  1.1  christos     /* By default only do one iteration if kmac_out_len is not specified */
    177  1.1  christos     if (kmac_out_len == 0)
    178  1.1  christos         kmac_out_len = derived_key_len;
    179  1.1  christos     /* otherwise check the size is valid */
    180  1.1  christos     else if (!(kmac_out_len == derived_key_len
    181  1.1  christos             || kmac_out_len == 20
    182  1.1  christos             || kmac_out_len == 28
    183  1.1  christos             || kmac_out_len == 32
    184  1.1  christos             || kmac_out_len == 48
    185  1.1  christos             || kmac_out_len == 64))
    186  1.1  christos         return 0;
    187  1.1  christos 
    188  1.1  christos     params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE,
    189  1.1  christos                                             &kmac_out_len);
    190  1.1  christos 
    191  1.1  christos     if (EVP_MAC_CTX_set_params(ctx, params) <= 0)
    192  1.1  christos         return 0;
    193  1.1  christos 
    194  1.1  christos     /*
    195  1.1  christos      * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so
    196  1.1  christos      * alloc a buffer for this case.
    197  1.1  christos      */
    198  1.1  christos     if (kmac_out_len > EVP_MAX_MD_SIZE) {
    199  1.1  christos         *out = OPENSSL_zalloc(kmac_out_len);
    200  1.1  christos         if (*out == NULL)
    201  1.1  christos             return 0;
    202  1.1  christos     }
    203  1.1  christos     return 1;
    204  1.1  christos }
    205  1.1  christos 
    206  1.1  christos /*
    207  1.1  christos  * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
    208  1.1  christos  * Section 4. One-Step Key Derivation using MAC: i.e either
    209  1.1  christos  *     H(x) = HMAC-hash(salt, x) OR
    210  1.1  christos  *     H(x) = KMAC#(salt, x, outbits, CustomString='KDF')
    211  1.1  christos  */
    212  1.1  christos static int SSKDF_mac_kdm(EVP_MAC_CTX *ctx_init,
    213  1.1  christos                          const unsigned char *kmac_custom,
    214  1.1  christos                          size_t kmac_custom_len, size_t kmac_out_len,
    215  1.1  christos                          const unsigned char *salt, size_t salt_len,
    216  1.1  christos                          const unsigned char *z, size_t z_len,
    217  1.1  christos                          const unsigned char *info, size_t info_len,
    218  1.1  christos                          unsigned char *derived_key, size_t derived_key_len)
    219  1.1  christos {
    220  1.1  christos     int ret = 0;
    221  1.1  christos     size_t counter, out_len, len;
    222  1.1  christos     unsigned char c[4];
    223  1.1  christos     unsigned char mac_buf[EVP_MAX_MD_SIZE];
    224  1.1  christos     unsigned char *out = derived_key;
    225  1.1  christos     EVP_MAC_CTX *ctx = NULL;
    226  1.1  christos     unsigned char *mac = mac_buf, *kmac_buffer = NULL;
    227  1.1  christos 
    228  1.1  christos     if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
    229  1.1  christos             || derived_key_len > SSKDF_MAX_INLEN
    230  1.1  christos             || derived_key_len == 0)
    231  1.1  christos         return 0;
    232  1.1  christos 
    233  1.1  christos     if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len,
    234  1.1  christos                    derived_key_len, &kmac_buffer))
    235  1.1  christos         goto end;
    236  1.1  christos     if (kmac_buffer != NULL)
    237  1.1  christos         mac = kmac_buffer;
    238  1.1  christos 
    239  1.1  christos     if (!EVP_MAC_init(ctx_init, salt, salt_len, NULL))
    240  1.1  christos         goto end;
    241  1.1  christos 
    242  1.1  christos     out_len = EVP_MAC_CTX_get_mac_size(ctx_init); /* output size */
    243  1.1  christos     if (out_len <= 0 || (mac == mac_buf && out_len > sizeof(mac_buf)))
    244  1.1  christos         goto end;
    245  1.1  christos     len = derived_key_len;
    246  1.1  christos 
    247  1.1  christos     for (counter = 1;; counter++) {
    248  1.1  christos         c[0] = (unsigned char)((counter >> 24) & 0xff);
    249  1.1  christos         c[1] = (unsigned char)((counter >> 16) & 0xff);
    250  1.1  christos         c[2] = (unsigned char)((counter >> 8) & 0xff);
    251  1.1  christos         c[3] = (unsigned char)(counter & 0xff);
    252  1.1  christos 
    253  1.1  christos         ctx = EVP_MAC_CTX_dup(ctx_init);
    254  1.1  christos         if (!(ctx != NULL
    255  1.1  christos                 && EVP_MAC_update(ctx, c, sizeof(c))
    256  1.1  christos                 && EVP_MAC_update(ctx, z, z_len)
    257  1.1  christos                 && EVP_MAC_update(ctx, info, info_len)))
    258  1.1  christos             goto end;
    259  1.1  christos         if (len >= out_len) {
    260  1.1  christos             if (!EVP_MAC_final(ctx, out, NULL, len))
    261  1.1  christos                 goto end;
    262  1.1  christos             out += out_len;
    263  1.1  christos             len -= out_len;
    264  1.1  christos             if (len == 0)
    265  1.1  christos                 break;
    266  1.1  christos         } else {
    267  1.1  christos             if (!EVP_MAC_final(ctx, mac, NULL, out_len))
    268  1.1  christos                 goto end;
    269  1.1  christos             memcpy(out, mac, len);
    270  1.1  christos             break;
    271  1.1  christos         }
    272  1.1  christos         EVP_MAC_CTX_free(ctx);
    273  1.1  christos         ctx = NULL;
    274  1.1  christos     }
    275  1.1  christos     ret = 1;
    276  1.1  christos end:
    277  1.1  christos     if (kmac_buffer != NULL)
    278  1.1  christos         OPENSSL_clear_free(kmac_buffer, kmac_out_len);
    279  1.1  christos     else
    280  1.1  christos         OPENSSL_cleanse(mac_buf, sizeof(mac_buf));
    281  1.1  christos 
    282  1.1  christos     EVP_MAC_CTX_free(ctx);
    283  1.1  christos     return ret;
    284  1.1  christos }
    285  1.1  christos 
    286  1.1  christos static void *sskdf_new(void *provctx)
    287  1.1  christos {
    288  1.1  christos     KDF_SSKDF *ctx;
    289  1.1  christos 
    290  1.1  christos     if (!ossl_prov_is_running())
    291  1.1  christos         return NULL;
    292  1.1  christos 
    293  1.1  christos     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
    294  1.1  christos         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
    295  1.1  christos     ctx->provctx = provctx;
    296  1.1  christos     return ctx;
    297  1.1  christos }
    298  1.1  christos 
    299  1.1  christos static void sskdf_reset(void *vctx)
    300  1.1  christos {
    301  1.1  christos     KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
    302  1.1  christos     void *provctx = ctx->provctx;
    303  1.1  christos 
    304  1.1  christos     EVP_MAC_CTX_free(ctx->macctx);
    305  1.1  christos     ossl_prov_digest_reset(&ctx->digest);
    306  1.1  christos     OPENSSL_clear_free(ctx->secret, ctx->secret_len);
    307  1.1  christos     OPENSSL_clear_free(ctx->info, ctx->info_len);
    308  1.1  christos     OPENSSL_clear_free(ctx->salt, ctx->salt_len);
    309  1.1  christos     memset(ctx, 0, sizeof(*ctx));
    310  1.1  christos     ctx->provctx = provctx;
    311  1.1  christos }
    312  1.1  christos 
    313  1.1  christos static void sskdf_free(void *vctx)
    314  1.1  christos {
    315  1.1  christos     KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
    316  1.1  christos 
    317  1.1  christos     if (ctx != NULL) {
    318  1.1  christos         sskdf_reset(ctx);
    319  1.1  christos         OPENSSL_free(ctx);
    320  1.1  christos     }
    321  1.1  christos }
    322  1.1  christos 
    323  1.1  christos static int sskdf_set_buffer(unsigned char **out, size_t *out_len,
    324  1.1  christos                             const OSSL_PARAM *p)
    325  1.1  christos {
    326  1.1  christos     if (p->data == NULL || p->data_size == 0)
    327  1.1  christos         return 1;
    328  1.1  christos     OPENSSL_free(*out);
    329  1.1  christos     *out = NULL;
    330  1.1  christos     return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos static size_t sskdf_size(KDF_SSKDF *ctx)
    334  1.1  christos {
    335  1.1  christos     int len;
    336  1.1  christos     const EVP_MD *md = NULL;
    337  1.1  christos 
    338  1.1  christos     if (ctx->is_kmac)
    339  1.1  christos         return SIZE_MAX;
    340  1.1  christos 
    341  1.1  christos     md = ossl_prov_digest_md(&ctx->digest);
    342  1.1  christos     if (md == NULL) {
    343  1.1  christos         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
    344  1.1  christos         return 0;
    345  1.1  christos     }
    346  1.1  christos     len = EVP_MD_get_size(md);
    347  1.1  christos     return (len <= 0) ? 0 : (size_t)len;
    348  1.1  christos }
    349  1.1  christos 
    350  1.1  christos static int sskdf_derive(void *vctx, unsigned char *key, size_t keylen,
    351  1.1  christos                         const OSSL_PARAM params[])
    352  1.1  christos {
    353  1.1  christos     KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
    354  1.1  christos     const EVP_MD *md;
    355  1.1  christos 
    356  1.1  christos     if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx, params))
    357  1.1  christos         return 0;
    358  1.1  christos     if (ctx->secret == NULL) {
    359  1.1  christos         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
    360  1.1  christos         return 0;
    361  1.1  christos     }
    362  1.1  christos     md = ossl_prov_digest_md(&ctx->digest);
    363  1.1  christos 
    364  1.1  christos     if (ctx->macctx != NULL) {
    365  1.1  christos         /* H(x) = KMAC or H(x) = HMAC */
    366  1.1  christos         int ret;
    367  1.1  christos         const unsigned char *custom = NULL;
    368  1.1  christos         size_t custom_len = 0;
    369  1.1  christos         int default_salt_len;
    370  1.1  christos         EVP_MAC *mac = EVP_MAC_CTX_get0_mac(ctx->macctx);
    371  1.1  christos 
    372  1.1  christos         if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_HMAC)) {
    373  1.1  christos             /* H(x) = HMAC(x, salt, hash) */
    374  1.1  christos             if (md == NULL) {
    375  1.1  christos                 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
    376  1.1  christos                 return 0;
    377  1.1  christos             }
    378  1.1  christos             default_salt_len = EVP_MD_get_size(md);
    379  1.1  christos             if (default_salt_len <= 0)
    380  1.1  christos                 return 0;
    381  1.1  christos         } else if (ctx->is_kmac) {
    382  1.1  christos             /* H(x) = KMACzzz(x, salt, custom) */
    383  1.1  christos             custom = kmac_custom_str;
    384  1.1  christos             custom_len = sizeof(kmac_custom_str);
    385  1.1  christos             if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_KMAC128))
    386  1.1  christos                 default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE;
    387  1.1  christos             else
    388  1.1  christos                 default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE;
    389  1.1  christos         } else {
    390  1.1  christos             ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_MAC_TYPE);
    391  1.1  christos             return 0;
    392  1.1  christos         }
    393  1.1  christos         /* If no salt is set then use a default_salt of zeros */
    394  1.1  christos         if (ctx->salt == NULL || ctx->salt_len <= 0) {
    395  1.1  christos             ctx->salt = OPENSSL_zalloc(default_salt_len);
    396  1.1  christos             if (ctx->salt == NULL) {
    397  1.1  christos                 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
    398  1.1  christos                 return 0;
    399  1.1  christos             }
    400  1.1  christos             ctx->salt_len = default_salt_len;
    401  1.1  christos         }
    402  1.1  christos         ret = SSKDF_mac_kdm(ctx->macctx,
    403  1.1  christos                             custom, custom_len, ctx->out_len,
    404  1.1  christos                             ctx->salt, ctx->salt_len,
    405  1.1  christos                             ctx->secret, ctx->secret_len,
    406  1.1  christos                             ctx->info, ctx->info_len, key, keylen);
    407  1.1  christos         return ret;
    408  1.1  christos     } else {
    409  1.1  christos         /* H(x) = hash */
    410  1.1  christos         if (md == NULL) {
    411  1.1  christos             ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
    412  1.1  christos             return 0;
    413  1.1  christos         }
    414  1.1  christos         return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len,
    415  1.1  christos                               ctx->info, ctx->info_len, 0, key, keylen);
    416  1.1  christos     }
    417  1.1  christos }
    418  1.1  christos 
    419  1.1  christos static int x963kdf_derive(void *vctx, unsigned char *key, size_t keylen,
    420  1.1  christos                           const OSSL_PARAM params[])
    421  1.1  christos {
    422  1.1  christos     KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
    423  1.1  christos     const EVP_MD *md;
    424  1.1  christos 
    425  1.1  christos     if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx, params))
    426  1.1  christos         return 0;
    427  1.1  christos 
    428  1.1  christos     if (ctx->secret == NULL) {
    429  1.1  christos         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
    430  1.1  christos         return 0;
    431  1.1  christos     }
    432  1.1  christos 
    433  1.1  christos     if (ctx->macctx != NULL) {
    434  1.1  christos         ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED);
    435  1.1  christos         return 0;
    436  1.1  christos     }
    437  1.1  christos 
    438  1.1  christos     /* H(x) = hash */
    439  1.1  christos     md = ossl_prov_digest_md(&ctx->digest);
    440  1.1  christos     if (md == NULL) {
    441  1.1  christos         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
    442  1.1  christos         return 0;
    443  1.1  christos     }
    444  1.1  christos 
    445  1.1  christos     return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len,
    446  1.1  christos                           ctx->info, ctx->info_len, 1, key, keylen);
    447  1.1  christos }
    448  1.1  christos 
    449  1.1  christos static int sskdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
    450  1.1  christos {
    451  1.1  christos     const OSSL_PARAM *p;
    452  1.1  christos     KDF_SSKDF *ctx = vctx;
    453  1.1  christos     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
    454  1.1  christos     size_t sz;
    455  1.1  christos 
    456  1.1  christos     if (params == NULL)
    457  1.1  christos         return 1;
    458  1.1  christos 
    459  1.1  christos     if (!ossl_prov_macctx_load_from_params(&ctx->macctx, params,
    460  1.1  christos                                            NULL, NULL, NULL, libctx))
    461  1.1  christos         return 0;
    462  1.1  christos    if (ctx->macctx != NULL) {
    463  1.1  christos         if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx),
    464  1.1  christos                          OSSL_MAC_NAME_KMAC128)
    465  1.1  christos             || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx),
    466  1.1  christos                             OSSL_MAC_NAME_KMAC256)) {
    467  1.1  christos             ctx->is_kmac = 1;
    468  1.1  christos         }
    469  1.1  christos    }
    470  1.1  christos 
    471  1.1  christos    if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
    472  1.1  christos        return 0;
    473  1.1  christos 
    474  1.1  christos     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL
    475  1.1  christos         || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
    476  1.1  christos         if (!sskdf_set_buffer(&ctx->secret, &ctx->secret_len, p))
    477  1.1  christos             return 0;
    478  1.1  christos 
    479  1.1  christos     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO)) != NULL)
    480  1.1  christos         if (!sskdf_set_buffer(&ctx->info, &ctx->info_len, p))
    481  1.1  christos             return 0;
    482  1.1  christos 
    483  1.1  christos     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
    484  1.1  christos         if (!sskdf_set_buffer(&ctx->salt, &ctx->salt_len, p))
    485  1.1  christos             return 0;
    486  1.1  christos 
    487  1.1  christos     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MAC_SIZE))
    488  1.1  christos         != NULL) {
    489  1.1  christos         if (!OSSL_PARAM_get_size_t(p, &sz) || sz == 0)
    490  1.1  christos             return 0;
    491  1.1  christos         ctx->out_len = sz;
    492  1.1  christos     }
    493  1.1  christos     return 1;
    494  1.1  christos }
    495  1.1  christos 
    496  1.1  christos static const OSSL_PARAM *sskdf_settable_ctx_params(ossl_unused void *ctx,
    497  1.1  christos                                                    ossl_unused void *provctx)
    498  1.1  christos {
    499  1.1  christos     static const OSSL_PARAM known_settable_ctx_params[] = {
    500  1.1  christos         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
    501  1.1  christos         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
    502  1.1  christos         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
    503  1.1  christos         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
    504  1.1  christos         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
    505  1.1  christos         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
    506  1.1  christos         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
    507  1.1  christos         OSSL_PARAM_size_t(OSSL_KDF_PARAM_MAC_SIZE, NULL),
    508  1.1  christos         OSSL_PARAM_END
    509  1.1  christos     };
    510  1.1  christos     return known_settable_ctx_params;
    511  1.1  christos }
    512  1.1  christos 
    513  1.1  christos static int sskdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
    514  1.1  christos {
    515  1.1  christos     KDF_SSKDF *ctx = (KDF_SSKDF *)vctx;
    516  1.1  christos     OSSL_PARAM *p;
    517  1.1  christos 
    518  1.1  christos     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
    519  1.1  christos         return OSSL_PARAM_set_size_t(p, sskdf_size(ctx));
    520  1.1  christos     return -2;
    521  1.1  christos }
    522  1.1  christos 
    523  1.1  christos static const OSSL_PARAM *sskdf_gettable_ctx_params(ossl_unused void *ctx,
    524  1.1  christos                                                    ossl_unused void *provctx)
    525  1.1  christos {
    526  1.1  christos     static const OSSL_PARAM known_gettable_ctx_params[] = {
    527  1.1  christos         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
    528  1.1  christos         OSSL_PARAM_END
    529  1.1  christos     };
    530  1.1  christos     return known_gettable_ctx_params;
    531  1.1  christos }
    532  1.1  christos 
    533  1.1  christos const OSSL_DISPATCH ossl_kdf_sskdf_functions[] = {
    534  1.1  christos     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new },
    535  1.1  christos     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free },
    536  1.1  christos     { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset },
    537  1.1  christos     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))sskdf_derive },
    538  1.1  christos     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
    539  1.1  christos       (void(*)(void))sskdf_settable_ctx_params },
    540  1.1  christos     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params },
    541  1.1  christos     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
    542  1.1  christos       (void(*)(void))sskdf_gettable_ctx_params },
    543  1.1  christos     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params },
    544  1.1  christos     { 0, NULL }
    545  1.1  christos };
    546  1.1  christos 
    547  1.1  christos const OSSL_DISPATCH ossl_kdf_x963_kdf_functions[] = {
    548  1.1  christos     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new },
    549  1.1  christos     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free },
    550  1.1  christos     { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset },
    551  1.1  christos     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x963kdf_derive },
    552  1.1  christos     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
    553  1.1  christos       (void(*)(void))sskdf_settable_ctx_params },
    554  1.1  christos     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params },
    555  1.1  christos     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
    556  1.1  christos       (void(*)(void))sskdf_gettable_ctx_params },
    557  1.1  christos     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params },
    558  1.1  christos     { 0, NULL }
    559  1.1  christos };
    560