Home | History | Annotate | Line # | Download | only in fuzz
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2016-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");
      5      1.1  christos  * you may not use this file except in compliance with the License.
      6      1.1  christos  * You may obtain a copy of the License at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  * or in the file LICENSE in the source distribution.
      9      1.1  christos  */
     10      1.1  christos 
     11      1.1  christos #include <openssl/core_names.h>
     12      1.1  christos #include <openssl/rand.h>
     13      1.1  christos #include <openssl/provider.h>
     14      1.1  christos #include "fuzzer.h"
     15      1.1  christos 
     16      1.1  christos static OSSL_FUNC_rand_newctx_fn fuzz_rand_newctx;
     17      1.1  christos static OSSL_FUNC_rand_freectx_fn fuzz_rand_freectx;
     18      1.1  christos static OSSL_FUNC_rand_instantiate_fn fuzz_rand_instantiate;
     19      1.1  christos static OSSL_FUNC_rand_uninstantiate_fn fuzz_rand_uninstantiate;
     20      1.1  christos static OSSL_FUNC_rand_generate_fn fuzz_rand_generate;
     21      1.1  christos static OSSL_FUNC_rand_gettable_ctx_params_fn fuzz_rand_gettable_ctx_params;
     22      1.1  christos static OSSL_FUNC_rand_get_ctx_params_fn fuzz_rand_get_ctx_params;
     23      1.1  christos static OSSL_FUNC_rand_enable_locking_fn fuzz_rand_enable_locking;
     24      1.1  christos 
     25      1.1  christos static void *fuzz_rand_newctx(
     26  1.1.1.2  christos     void *provctx, void *parent, const OSSL_DISPATCH *parent_dispatch)
     27      1.1  christos {
     28      1.1  christos     int *st = OPENSSL_malloc(sizeof(*st));
     29      1.1  christos 
     30      1.1  christos     if (st != NULL)
     31      1.1  christos         *st = EVP_RAND_STATE_UNINITIALISED;
     32      1.1  christos     return st;
     33      1.1  christos }
     34      1.1  christos 
     35      1.1  christos static void fuzz_rand_freectx(ossl_unused void *vrng)
     36      1.1  christos {
     37      1.1  christos     OPENSSL_free(vrng);
     38      1.1  christos }
     39      1.1  christos 
     40      1.1  christos static int fuzz_rand_instantiate(ossl_unused void *vrng,
     41  1.1.1.2  christos     ossl_unused unsigned int strength,
     42  1.1.1.2  christos     ossl_unused int prediction_resistance,
     43  1.1.1.2  christos     ossl_unused const unsigned char *pstr,
     44  1.1.1.2  christos     ossl_unused size_t pstr_len,
     45  1.1.1.2  christos     ossl_unused const OSSL_PARAM params[])
     46      1.1  christos {
     47      1.1  christos     *(int *)vrng = EVP_RAND_STATE_READY;
     48      1.1  christos     return 1;
     49      1.1  christos }
     50      1.1  christos 
     51      1.1  christos static int fuzz_rand_uninstantiate(ossl_unused void *vrng)
     52      1.1  christos {
     53      1.1  christos     *(int *)vrng = EVP_RAND_STATE_UNINITIALISED;
     54      1.1  christos     return 1;
     55      1.1  christos }
     56      1.1  christos 
     57      1.1  christos static int fuzz_rand_generate(ossl_unused void *vdrbg,
     58  1.1.1.2  christos     unsigned char *out, size_t outlen,
     59  1.1.1.2  christos     ossl_unused unsigned int strength,
     60  1.1.1.2  christos     ossl_unused int prediction_resistance,
     61  1.1.1.2  christos     ossl_unused const unsigned char *adin,
     62  1.1.1.2  christos     ossl_unused size_t adinlen)
     63      1.1  christos {
     64      1.1  christos     unsigned char val = 1;
     65      1.1  christos     size_t i;
     66      1.1  christos 
     67      1.1  christos     for (i = 0; i < outlen; i++)
     68      1.1  christos         out[i] = val++;
     69      1.1  christos     return 1;
     70      1.1  christos }
     71      1.1  christos 
     72      1.1  christos static int fuzz_rand_enable_locking(ossl_unused void *vrng)
     73      1.1  christos {
     74      1.1  christos     return 1;
     75      1.1  christos }
     76      1.1  christos 
     77      1.1  christos static int fuzz_rand_get_ctx_params(void *vrng, OSSL_PARAM params[])
     78      1.1  christos {
     79      1.1  christos     OSSL_PARAM *p;
     80      1.1  christos 
     81      1.1  christos     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
     82      1.1  christos     if (p != NULL && !OSSL_PARAM_set_int(p, *(int *)vrng))
     83      1.1  christos         return 0;
     84      1.1  christos 
     85      1.1  christos     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
     86      1.1  christos     if (p != NULL && !OSSL_PARAM_set_int(p, 500))
     87      1.1  christos         return 0;
     88      1.1  christos 
     89      1.1  christos     p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
     90      1.1  christos     if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX))
     91      1.1  christos         return 0;
     92      1.1  christos     return 1;
     93      1.1  christos }
     94      1.1  christos 
     95      1.1  christos static const OSSL_PARAM *fuzz_rand_gettable_ctx_params(ossl_unused void *vrng,
     96  1.1.1.2  christos     ossl_unused void *provctx)
     97      1.1  christos {
     98      1.1  christos     static const OSSL_PARAM known_gettable_ctx_params[] = {
     99      1.1  christos         OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
    100      1.1  christos         OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
    101      1.1  christos         OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
    102      1.1  christos         OSSL_PARAM_END
    103      1.1  christos     };
    104      1.1  christos     return known_gettable_ctx_params;
    105      1.1  christos }
    106      1.1  christos 
    107      1.1  christos static const OSSL_DISPATCH fuzz_rand_functions[] = {
    108      1.1  christos     { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))fuzz_rand_newctx },
    109      1.1  christos     { OSSL_FUNC_RAND_FREECTX, (void (*)(void))fuzz_rand_freectx },
    110      1.1  christos     { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))fuzz_rand_instantiate },
    111      1.1  christos     { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))fuzz_rand_uninstantiate },
    112      1.1  christos     { OSSL_FUNC_RAND_GENERATE, (void (*)(void))fuzz_rand_generate },
    113      1.1  christos     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))fuzz_rand_enable_locking },
    114      1.1  christos     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
    115  1.1.1.2  christos         (void (*)(void))fuzz_rand_gettable_ctx_params },
    116  1.1.1.2  christos     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void (*)(void))fuzz_rand_get_ctx_params },
    117      1.1  christos     OSSL_DISPATCH_END
    118      1.1  christos };
    119      1.1  christos 
    120      1.1  christos static const OSSL_ALGORITHM fuzz_rand_rand[] = {
    121      1.1  christos     { "fuzz", "provider=fuzz-rand", fuzz_rand_functions },
    122      1.1  christos     { NULL, NULL, NULL }
    123      1.1  christos };
    124      1.1  christos 
    125      1.1  christos static const OSSL_ALGORITHM *fuzz_rand_query(void *provctx,
    126  1.1.1.2  christos     int operation_id,
    127  1.1.1.2  christos     int *no_cache)
    128      1.1  christos {
    129      1.1  christos     *no_cache = 0;
    130      1.1  christos     switch (operation_id) {
    131      1.1  christos     case OSSL_OP_RAND:
    132      1.1  christos         return fuzz_rand_rand;
    133      1.1  christos     }
    134      1.1  christos     return NULL;
    135      1.1  christos }
    136      1.1  christos 
    137      1.1  christos /* Functions we provide to the core */
    138      1.1  christos static const OSSL_DISPATCH fuzz_rand_method[] = {
    139      1.1  christos     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
    140      1.1  christos     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fuzz_rand_query },
    141      1.1  christos     OSSL_DISPATCH_END
    142      1.1  christos };
    143      1.1  christos 
    144      1.1  christos static int fuzz_rand_provider_init(const OSSL_CORE_HANDLE *handle,
    145  1.1.1.2  christos     const OSSL_DISPATCH *in,
    146  1.1.1.2  christos     const OSSL_DISPATCH **out, void **provctx)
    147      1.1  christos {
    148      1.1  christos     *provctx = OSSL_LIB_CTX_new();
    149      1.1  christos     if (*provctx == NULL)
    150      1.1  christos         return 0;
    151      1.1  christos     *out = fuzz_rand_method;
    152      1.1  christos     return 1;
    153      1.1  christos }
    154      1.1  christos 
    155      1.1  christos static OSSL_PROVIDER *r_prov;
    156      1.1  christos 
    157      1.1  christos void FuzzerSetRand(void)
    158      1.1  christos {
    159      1.1  christos     if (!OSSL_PROVIDER_add_builtin(NULL, "fuzz-rand", fuzz_rand_provider_init)
    160      1.1  christos         || !RAND_set_DRBG_type(NULL, "fuzz", NULL, NULL, NULL)
    161      1.1  christos         || (r_prov = OSSL_PROVIDER_try_load(NULL, "fuzz-rand", 1)) == NULL)
    162      1.1  christos         exit(1);
    163      1.1  christos }
    164      1.1  christos 
    165      1.1  christos void FuzzerClearRand(void)
    166      1.1  christos {
    167      1.1  christos     OSSL_PROVIDER_unload(r_prov);
    168      1.1  christos }
    169