Home | History | Annotate | Line # | Download | only in prov
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2019-2021 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 #ifndef OSSL_PROVIDERS_DIGESTCOMMON_H
     11  1.1.1.2  christos #define OSSL_PROVIDERS_DIGESTCOMMON_H
     12      1.1  christos 
     13  1.1.1.2  christos #include <openssl/core_dispatch.h>
     14  1.1.1.2  christos #include <openssl/core_names.h>
     15  1.1.1.2  christos #include <openssl/params.h>
     16  1.1.1.2  christos #include "prov/providercommon.h"
     17      1.1  christos 
     18      1.1  christos /* Internal flags that can be queried */
     19  1.1.1.2  christos #define PROV_DIGEST_FLAG_XOF 0x0001
     20  1.1.1.2  christos #define PROV_DIGEST_FLAG_ALGID_ABSENT 0x0002
     21      1.1  christos 
     22  1.1.1.2  christos #ifdef __cplusplus
     23      1.1  christos extern "C" {
     24  1.1.1.2  christos #endif
     25      1.1  christos 
     26  1.1.1.2  christos #define PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags)               \
     27  1.1.1.2  christos     static OSSL_FUNC_digest_get_params_fn name##_get_params;                     \
     28  1.1.1.2  christos     static int name##_get_params(OSSL_PARAM params[])                            \
     29  1.1.1.2  christos     {                                                                            \
     30  1.1.1.2  christos         return ossl_digest_default_get_params(params, blksize, dgstsize, flags); \
     31  1.1.1.2  christos     }
     32  1.1.1.2  christos 
     33  1.1.1.2  christos #define PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)                      \
     34  1.1.1.2  christos     { OSSL_FUNC_DIGEST_GET_PARAMS, (void (*)(void))name##_get_params }, \
     35  1.1.1.2  christos     {                                                                   \
     36  1.1.1.2  christos         OSSL_FUNC_DIGEST_GETTABLE_PARAMS,                               \
     37  1.1.1.2  christos             (void (*)(void))ossl_digest_default_gettable_params         \
     38  1.1.1.2  christos     }
     39  1.1.1.2  christos 
     40  1.1.1.2  christos #define PROV_FUNC_DIGEST_FINAL(name, dgstsize, fin)                               \
     41  1.1.1.2  christos     static OSSL_FUNC_digest_final_fn name##_internal_final;                       \
     42  1.1.1.2  christos     static int name##_internal_final(void *ctx, unsigned char *out, size_t *outl, \
     43  1.1.1.2  christos         size_t outsz)                                                             \
     44  1.1.1.2  christos     {                                                                             \
     45  1.1.1.2  christos         if (ossl_prov_is_running() && outsz >= dgstsize && fin(out, ctx)) {       \
     46  1.1.1.2  christos             *outl = dgstsize;                                                     \
     47  1.1.1.2  christos             return 1;                                                             \
     48  1.1.1.2  christos         }                                                                         \
     49  1.1.1.2  christos         return 0;                                                                 \
     50  1.1.1.2  christos     }
     51  1.1.1.2  christos 
     52  1.1.1.2  christos #define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(                               \
     53  1.1.1.2  christos     name, CTX, blksize, dgstsize, flags, upd, fin)                               \
     54  1.1.1.2  christos     static OSSL_FUNC_digest_newctx_fn name##_newctx;                             \
     55  1.1.1.2  christos     static OSSL_FUNC_digest_freectx_fn name##_freectx;                           \
     56  1.1.1.2  christos     static OSSL_FUNC_digest_dupctx_fn name##_dupctx;                             \
     57  1.1.1.2  christos     static void *name##_newctx(void *prov_ctx)                                   \
     58  1.1.1.2  christos     {                                                                            \
     59  1.1.1.2  christos         CTX *ctx = ossl_prov_is_running() ? OPENSSL_zalloc(sizeof(*ctx)) : NULL; \
     60  1.1.1.2  christos         return ctx;                                                              \
     61  1.1.1.2  christos     }                                                                            \
     62  1.1.1.2  christos     static void name##_freectx(void *vctx)                                       \
     63  1.1.1.2  christos     {                                                                            \
     64  1.1.1.2  christos         CTX *ctx = (CTX *)vctx;                                                  \
     65  1.1.1.2  christos         OPENSSL_clear_free(ctx, sizeof(*ctx));                                   \
     66  1.1.1.2  christos     }                                                                            \
     67  1.1.1.2  christos     static void *name##_dupctx(void *ctx)                                        \
     68  1.1.1.2  christos     {                                                                            \
     69  1.1.1.2  christos         CTX *in = (CTX *)ctx;                                                    \
     70  1.1.1.2  christos         CTX *ret = ossl_prov_is_running() ? OPENSSL_malloc(sizeof(*ret)) : NULL; \
     71  1.1.1.2  christos         if (ret != NULL)                                                         \
     72  1.1.1.2  christos             *ret = *in;                                                          \
     73  1.1.1.2  christos         return ret;                                                              \
     74  1.1.1.2  christos     }                                                                            \
     75  1.1.1.2  christos     static void name##_copyctx(void *voutctx, void *vinctx)                      \
     76  1.1.1.2  christos     {                                                                            \
     77  1.1.1.2  christos         CTX *outctx = (CTX *)voutctx;                                            \
     78  1.1.1.2  christos         CTX *inctx = (CTX *)vinctx;                                              \
     79  1.1.1.2  christos         *outctx = *inctx;                                                        \
     80  1.1.1.2  christos     }                                                                            \
     81  1.1.1.2  christos     PROV_FUNC_DIGEST_FINAL(name, dgstsize, fin)                                  \
     82  1.1.1.2  christos     PROV_FUNC_DIGEST_GET_PARAM(name, blksize, dgstsize, flags)                   \
     83  1.1.1.2  christos     const OSSL_DISPATCH ossl_##name##_functions[] = {                            \
     84  1.1.1.2  christos         { OSSL_FUNC_DIGEST_NEWCTX, (void (*)(void))name##_newctx },              \
     85  1.1.1.2  christos         { OSSL_FUNC_DIGEST_UPDATE, (void (*)(void))upd },                        \
     86  1.1.1.2  christos         { OSSL_FUNC_DIGEST_FINAL, (void (*)(void))name##_internal_final },       \
     87  1.1.1.2  christos         { OSSL_FUNC_DIGEST_FREECTX, (void (*)(void))name##_freectx },            \
     88  1.1.1.2  christos         { OSSL_FUNC_DIGEST_DUPCTX, (void (*)(void))name##_dupctx },              \
     89  1.1.1.2  christos         { OSSL_FUNC_DIGEST_COPYCTX, (void (*)(void))name##_copyctx },            \
     90  1.1.1.2  christos         PROV_DISPATCH_FUNC_DIGEST_GET_PARAMS(name)
     91  1.1.1.2  christos 
     92  1.1.1.2  christos #define PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END \
     93  1.1.1.2  christos     {                                           \
     94  1.1.1.2  christos         0, NULL                                 \
     95  1.1.1.2  christos     }                                           \
     96  1.1.1.2  christos     }                                           \
     97  1.1.1.2  christos     ;
     98  1.1.1.2  christos 
     99  1.1.1.2  christos #define IMPLEMENT_digest_functions(                                                \
    100  1.1.1.2  christos     name, CTX, blksize, dgstsize, flags, init, upd, fin)                           \
    101  1.1.1.2  christos     static OSSL_FUNC_digest_init_fn name##_internal_init;                          \
    102  1.1.1.2  christos     static int name##_internal_init(void *ctx,                                     \
    103  1.1.1.2  christos         ossl_unused const OSSL_PARAM params[])                                     \
    104  1.1.1.2  christos     {                                                                              \
    105  1.1.1.2  christos         return ossl_prov_is_running() && init(ctx);                                \
    106  1.1.1.2  christos     }                                                                              \
    107  1.1.1.2  christos     PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags, \
    108  1.1.1.2  christos         upd, fin),                                                                 \
    109  1.1.1.2  christos         { OSSL_FUNC_DIGEST_INIT, (void (*)(void))name##_internal_init },           \
    110  1.1.1.2  christos         PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
    111  1.1.1.2  christos 
    112  1.1.1.2  christos #define IMPLEMENT_digest_functions_with_settable_ctx(                                  \
    113  1.1.1.2  christos     name, CTX, blksize, dgstsize, flags, init, upd, fin,                               \
    114  1.1.1.2  christos     settable_ctx_params, set_ctx_params)                                               \
    115  1.1.1.2  christos     static OSSL_FUNC_digest_init_fn name##_internal_init;                              \
    116  1.1.1.2  christos     static int name##_internal_init(void *ctx, const OSSL_PARAM params[])              \
    117  1.1.1.2  christos     {                                                                                  \
    118  1.1.1.2  christos         return ossl_prov_is_running()                                                  \
    119  1.1.1.2  christos             && init(ctx)                                                               \
    120  1.1.1.2  christos             && set_ctx_params(ctx, params);                                            \
    121  1.1.1.2  christos     }                                                                                  \
    122  1.1.1.2  christos     PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_START(name, CTX, blksize, dgstsize, flags,     \
    123  1.1.1.2  christos         upd, fin),                                                                     \
    124  1.1.1.2  christos         { OSSL_FUNC_DIGEST_INIT, (void (*)(void))name##_internal_init },               \
    125  1.1.1.2  christos         { OSSL_FUNC_DIGEST_SETTABLE_CTX_PARAMS, (void (*)(void))settable_ctx_params }, \
    126  1.1.1.2  christos         { OSSL_FUNC_DIGEST_SET_CTX_PARAMS, (void (*)(void))set_ctx_params },           \
    127  1.1.1.2  christos         PROV_DISPATCH_FUNC_DIGEST_CONSTRUCT_END
    128      1.1  christos 
    129      1.1  christos const OSSL_PARAM *ossl_digest_default_gettable_params(void *provctx);
    130      1.1  christos int ossl_digest_default_get_params(OSSL_PARAM params[], size_t blksz,
    131  1.1.1.2  christos     size_t paramsz, unsigned long flags);
    132      1.1  christos 
    133  1.1.1.2  christos #ifdef __cplusplus
    134      1.1  christos }
    135  1.1.1.2  christos #endif
    136      1.1  christos 
    137      1.1  christos #endif /* OSSL_PROVIDERS_DIGESTCOMMON_H */
    138