fuzz_rand.c 5.4 KB

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