Home | History | Annotate | Line # | Download | only in digests
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include <openssl/crypto.h>
     11      1.1  christos #include <openssl/core_names.h>
     12      1.1  christos #include <openssl/proverr.h>
     13      1.1  christos #include <openssl/err.h>
     14      1.1  christos #include "prov/blake2.h"
     15      1.1  christos #include "prov/digestcommon.h"
     16      1.1  christos #include "prov/implementations.h"
     17      1.1  christos 
     18  1.1.1.2  christos #define IMPLEMENT_BLAKE_functions(variant, VARIANT, variantsize)                                                  \
     19  1.1.1.2  christos     static const OSSL_PARAM known_blake##variant##_ctx_params[] = {                                               \
     20  1.1.1.2  christos         { OSSL_DIGEST_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0 },                                      \
     21  1.1.1.2  christos         OSSL_PARAM_END                                                                                            \
     22  1.1.1.2  christos     };                                                                                                            \
     23  1.1.1.2  christos                                                                                                                   \
     24  1.1.1.2  christos     const OSSL_PARAM *ossl_blake##variant##_gettable_ctx_params(ossl_unused void *ctx,                            \
     25  1.1.1.2  christos         ossl_unused void *pctx)                                                                                   \
     26  1.1.1.2  christos     {                                                                                                             \
     27  1.1.1.2  christos         return known_blake##variant##_ctx_params;                                                                 \
     28  1.1.1.2  christos     }                                                                                                             \
     29  1.1.1.2  christos                                                                                                                   \
     30  1.1.1.2  christos     const OSSL_PARAM *ossl_blake##variant##_settable_ctx_params(ossl_unused void *ctx,                            \
     31  1.1.1.2  christos         ossl_unused void *pctx)                                                                                   \
     32  1.1.1.2  christos     {                                                                                                             \
     33  1.1.1.2  christos         return known_blake##variant##_ctx_params;                                                                 \
     34  1.1.1.2  christos     }                                                                                                             \
     35  1.1.1.2  christos                                                                                                                   \
     36  1.1.1.2  christos     int ossl_blake##variant##_get_ctx_params(void *vctx, OSSL_PARAM params[])                                     \
     37  1.1.1.2  christos     {                                                                                                             \
     38  1.1.1.2  christos         struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
     39  1.1.1.2  christos         OSSL_PARAM *p;                                                                                            \
     40  1.1.1.2  christos                                                                                                                   \
     41  1.1.1.2  christos         BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
     42  1.1.1.2  christos                                                                                                                   \
     43  1.1.1.2  christos         if (ctx == NULL)                                                                                          \
     44  1.1.1.2  christos             return 0;                                                                                             \
     45  1.1.1.2  christos         if (ossl_param_is_empty(params))                                                                          \
     46  1.1.1.2  christos             return 1;                                                                                             \
     47  1.1.1.2  christos                                                                                                                   \
     48  1.1.1.2  christos         p = OSSL_PARAM_locate(params, OSSL_DIGEST_PARAM_SIZE);                                                    \
     49  1.1.1.2  christos         if (p != NULL                                                                                             \
     50  1.1.1.2  christos             && !OSSL_PARAM_set_uint(p, (unsigned int)mdctx->params.digest_length)) {                              \
     51  1.1.1.2  christos             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);                                              \
     52  1.1.1.2  christos             return 0;                                                                                             \
     53  1.1.1.2  christos         }                                                                                                         \
     54  1.1.1.2  christos                                                                                                                   \
     55  1.1.1.2  christos         return 1;                                                                                                 \
     56  1.1.1.2  christos     }                                                                                                             \
     57  1.1.1.2  christos                                                                                                                   \
     58  1.1.1.2  christos     int ossl_blake##variant##_set_ctx_params(void *vctx, const OSSL_PARAM params[])                               \
     59  1.1.1.2  christos     {                                                                                                             \
     60  1.1.1.2  christos         size_t size;                                                                                              \
     61  1.1.1.2  christos         struct blake##variant##_md_data_st *mdctx = vctx;                                                         \
     62  1.1.1.2  christos         const OSSL_PARAM *p;                                                                                      \
     63  1.1.1.2  christos                                                                                                                   \
     64  1.1.1.2  christos         BLAKE##VARIANT##_CTX *ctx = &mdctx->ctx;                                                                  \
     65  1.1.1.2  christos                                                                                                                   \
     66  1.1.1.2  christos         if (ctx == NULL)                                                                                          \
     67  1.1.1.2  christos             return 0;                                                                                             \
     68  1.1.1.2  christos         if (ossl_param_is_empty(params))                                                                          \
     69  1.1.1.2  christos             return 1;                                                                                             \
     70  1.1.1.2  christos                                                                                                                   \
     71  1.1.1.2  christos         p = OSSL_PARAM_locate_const(params, OSSL_DIGEST_PARAM_SIZE);                                              \
     72  1.1.1.2  christos         if (p != NULL) {                                                                                          \
     73  1.1.1.2  christos             if (!OSSL_PARAM_get_size_t(p, &size)) {                                                               \
     74  1.1.1.2  christos                 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);                                          \
     75  1.1.1.2  christos                 return 0;                                                                                         \
     76  1.1.1.2  christos             }                                                                                                     \
     77  1.1.1.2  christos             if (size < 1 || size > BLAKE##VARIANT##_OUTBYTES) {                                                   \
     78  1.1.1.2  christos                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                              \
     79  1.1.1.2  christos                 return 0;                                                                                         \
     80  1.1.1.2  christos             }                                                                                                     \
     81  1.1.1.2  christos             ossl_blake##variant##_param_set_digest_length(&mdctx->params, (uint8_t)size);                         \
     82  1.1.1.2  christos         }                                                                                                         \
     83  1.1.1.2  christos                                                                                                                   \
     84  1.1.1.2  christos         return 1;                                                                                                 \
     85  1.1.1.2  christos     }                                                                                                             \
     86  1.1.1.2  christos                                                                                                                   \
     87  1.1.1.2  christos     static int ossl_blake##variantsize##_init(void *ctx)                                                          \
     88  1.1.1.2  christos     {                                                                                                             \
     89  1.1.1.2  christos         struct blake##variant##_md_data_st *mdctx = ctx;                                                          \
     90  1.1.1.2  christos         uint8_t digest_length = mdctx->params.digest_length;                                                      \
     91  1.1.1.2  christos                                                                                                                   \
     92  1.1.1.2  christos         ossl_blake##variant##_param_init(&mdctx->params);                                                         \
     93  1.1.1.2  christos         if (digest_length != 0)                                                                                   \
     94  1.1.1.2  christos             mdctx->params.digest_length = digest_length;                                                          \
     95  1.1.1.2  christos         return ossl_blake##variant##_init(&mdctx->ctx, &mdctx->params);                                           \
     96  1.1.1.2  christos     }                                                                                                             \
     97  1.1.1.2  christos                                                                                                                   \
     98  1.1.1.2  christos     static OSSL_FUNC_digest_init_fn blake##variantsize##_internal_init;                                           \
     99  1.1.1.2  christos     static OSSL_FUNC_digest_newctx_fn blake##variantsize##_newctx;                                                \
    100  1.1.1.2  christos     static OSSL_FUNC_digest_freectx_fn blake##variantsize##_freectx;                                              \
    101  1.1.1.2  christos     static OSSL_FUNC_digest_dupctx_fn blake##variantsize##_dupctx;                                                \
    102  1.1.1.2  christos     static OSSL_FUNC_digest_final_fn blake##variantsize##_internal_final;                                         \
    103  1.1.1.2  christos     static OSSL_FUNC_digest_get_params_fn blake##variantsize##_get_params;                                        \
    104  1.1.1.2  christos                                                                                                                   \
    105  1.1.1.2  christos     static int blake##variantsize##_internal_init(void *ctx, const OSSL_PARAM params[])                           \
    106  1.1.1.2  christos     {                                                                                                             \
    107  1.1.1.2  christos         return ossl_prov_is_running() && ossl_blake##variant##_set_ctx_params(ctx, params)                        \
    108  1.1.1.2  christos             && ossl_blake##variantsize##_init(ctx);                                                               \
    109  1.1.1.2  christos     }                                                                                                             \
    110  1.1.1.2  christos                                                                                                                   \
    111  1.1.1.2  christos     static void *blake##variantsize##_newctx(void *prov_ctx)                                                      \
    112  1.1.1.2  christos     {                                                                                                             \
    113  1.1.1.2  christos         struct blake##variant##_md_data_st *ctx;                                                                  \
    114  1.1.1.2  christos                                                                                                                   \
    115  1.1.1.2  christos         ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL;                                       \
    116  1.1.1.2  christos         return ctx;                                                                                               \
    117  1.1.1.2  christos     }                                                                                                             \
    118  1.1.1.2  christos                                                                                                                   \
    119  1.1.1.2  christos     static void blake##variantsize##_freectx(void *vctx)                                                          \
    120  1.1.1.2  christos     {                                                                                                             \
    121  1.1.1.2  christos         struct blake##variant##_md_data_st *ctx;                                                                  \
    122  1.1.1.2  christos                                                                                                                   \
    123  1.1.1.2  christos         ctx = (struct blake##variant##_md_data_st *)vctx;                                                         \
    124  1.1.1.2  christos         OPENSSL_clear_free(ctx, sizeof(*ctx));                                                                    \
    125  1.1.1.2  christos     }                                                                                                             \
    126  1.1.1.2  christos                                                                                                                   \
    127  1.1.1.2  christos     static void *blake##variantsize##_dupctx(void *ctx)                                                           \
    128  1.1.1.2  christos     {                                                                                                             \
    129  1.1.1.2  christos         struct blake##variant##_md_data_st *in, *ret;                                                             \
    130  1.1.1.2  christos                                                                                                                   \
    131  1.1.1.2  christos         in = (struct blake##variant##_md_data_st *)ctx;                                                           \
    132  1.1.1.2  christos         ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL;                                       \
    133  1.1.1.2  christos         if (ret != NULL)                                                                                          \
    134  1.1.1.2  christos             *ret = *in;                                                                                           \
    135  1.1.1.2  christos         return ret;                                                                                               \
    136  1.1.1.2  christos     }                                                                                                             \
    137  1.1.1.2  christos                                                                                                                   \
    138  1.1.1.2  christos     static void blake##variantsize##_copyctx(void *voutctx, void *vinctx)                                         \
    139  1.1.1.2  christos     {                                                                                                             \
    140  1.1.1.2  christos         struct blake##variant##_md_data_st *inctx, *outctx;                                                       \
    141  1.1.1.2  christos                                                                                                                   \
    142  1.1.1.2  christos         outctx = (struct blake##variant##_md_data_st *)voutctx;                                                   \
    143  1.1.1.2  christos         inctx = (struct blake##variant##_md_data_st *)vinctx;                                                     \
    144  1.1.1.2  christos         *outctx = *inctx;                                                                                         \
    145  1.1.1.2  christos     }                                                                                                             \
    146  1.1.1.2  christos                                                                                                                   \
    147  1.1.1.2  christos     static int blake##variantsize##_internal_final(void *ctx, unsigned char *out,                                 \
    148  1.1.1.2  christos         size_t *outl, size_t outsz)                                                                               \
    149  1.1.1.2  christos     {                                                                                                             \
    150  1.1.1.2  christos         struct blake##variant##_md_data_st *b_ctx;                                                                \
    151  1.1.1.2  christos                                                                                                                   \
    152  1.1.1.2  christos         b_ctx = (struct blake##variant##_md_data_st *)ctx;                                                        \
    153  1.1.1.2  christos                                                                                                                   \
    154  1.1.1.2  christos         if (!ossl_prov_is_running())                                                                              \
    155  1.1.1.2  christos             return 0;                                                                                             \
    156  1.1.1.2  christos                                                                                                                   \
    157  1.1.1.2  christos         *outl = b_ctx->ctx.outlen;                                                                                \
    158  1.1.1.2  christos                                                                                                                   \
    159  1.1.1.2  christos         if (outsz == 0)                                                                                           \
    160  1.1.1.2  christos             return 1;                                                                                             \
    161  1.1.1.2  christos                                                                                                                   \
    162  1.1.1.2  christos         if (outsz < *outl) {                                                                                      \
    163  1.1.1.2  christos             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE);                                                  \
    164  1.1.1.2  christos             return 0;                                                                                             \
    165  1.1.1.2  christos         }                                                                                                         \
    166  1.1.1.2  christos                                                                                                                   \
    167  1.1.1.2  christos         return ossl_blake##variant##_final(out, ctx);                                                             \
    168  1.1.1.2  christos     }                                                                                                             \
    169  1.1.1.2  christos                                                                                                                   \
    170  1.1.1.2  christos     static int blake##variantsize##_get_params(OSSL_PARAM params[])                                               \
    171  1.1.1.2  christos     {                                                                                                             \
    172  1.1.1.2  christos         return ossl_digest_default_get_params(params, BLAKE##VARIANT##_BLOCKBYTES, BLAKE##VARIANT##_OUTBYTES, 0); \
    173  1.1.1.2  christos     }                                                                                                             \
    174  1.1.1.2  christos                                                                                                                   \
    175  1.1.1.2  christos     const OSSL_DISPATCH ossl_blake##variantsize##_functions[] = {                                                 \
    176  1.1.1.2  christos         { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))blake##variantsize##_newctx },                                 \
    177  1.1.1.2  christos         { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))ossl_blake##variant##_update },                                \
    178  1.1.1.2  christos         { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))blake##variantsize##_internal_final },                          \
    179  1.1.1.2  christos         { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))blake##variantsize##_freectx },                               \
    180  1.1.1.2  christos         { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))blake##variantsize##_dupctx },                                 \
    181  1.1.1.2  christos         { OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))blake##variantsize##_copyctx },                               \
    182  1.1.1.2  christos         { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))blake##variantsize##_get_params },                         \
    183  1.1.1.2  christos         { OSSL_FUNC_DIGEST_GETTABLE_PARAMS,                                                                       \
    184  1.1.1.2  christos             (void (*)(void))ossl_digest_default_gettable_params },                                                \
    185  1.1.1.2  christos         { OSSL_FUNC_DIGEST_INIT, (void (*)(void))blake##variantsize##_internal_init },                            \
    186  1.1.1.2  christos         { OSSL_FUNC_DIGEST_GETTABLE_CTX_PARAMS,                                                                   \
    187  1.1.1.2  christos             (void (*)(void))ossl_blake##variant##_gettable_ctx_params },                                          \
    188  1.1.1.2  christos         { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS,                                                                   \
    189  1.1.1.2  christos             (void (*)(void))ossl_blake##variant##_settable_ctx_params },                                          \
    190  1.1.1.2  christos         { OSSL_FUNC_DIGEST_GET_CTX_PARAMS,                                                                        \
    191  1.1.1.2  christos             (void (*)(void))ossl_blake##variant##_get_ctx_params },                                               \
    192  1.1.1.2  christos         { OSSL_FUNC_DIGEST_SET_CTX_PARAMS,                                                                        \
    193  1.1.1.2  christos             (void (*)(void))ossl_blake##variant##_set_ctx_params },                                               \
    194  1.1.1.2  christos         { 0, NULL }                                                                                               \
    195  1.1.1.2  christos     };
    196      1.1  christos 
    197      1.1  christos IMPLEMENT_BLAKE_functions(2s, 2S, 2s256)
    198      1.1  christos IMPLEMENT_BLAKE_functions(2b, 2B, 2b512)
    199