1 /*
2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/core_dispatch.h>
11 #include "prov/seeding.h"
12
13 static OSSL_FUNC_get_entropy_fn *c_get_entropy = NULL;
14 static OSSL_FUNC_cleanup_entropy_fn *c_cleanup_entropy = NULL;
15 static OSSL_FUNC_get_nonce_fn *c_get_nonce = NULL;
16 static OSSL_FUNC_cleanup_nonce_fn *c_cleanup_nonce = NULL;
17
ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH * fns)18 int ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH *fns)
19 {
20 for (; fns->function_id != 0; fns++) {
21 /*
22 * We do not support the scenario of an application linked against
23 * multiple versions of libcrypto (e.g. one static and one dynamic), but
24 * sharing a single fips.so. We do a simple sanity check here.
25 */
26 #define set_func(c, f) \
27 do { if (c == NULL) c = f; else if (c != f) return 0; } while (0)
28 switch (fns->function_id) {
29 case OSSL_FUNC_GET_ENTROPY:
30 set_func(c_get_entropy, OSSL_FUNC_get_entropy(fns));
31 break;
32 case OSSL_FUNC_CLEANUP_ENTROPY:
33 set_func(c_cleanup_entropy, OSSL_FUNC_cleanup_entropy(fns));
34 break;
35 case OSSL_FUNC_GET_NONCE:
36 set_func(c_get_nonce, OSSL_FUNC_get_nonce(fns));
37 break;
38 case OSSL_FUNC_CLEANUP_NONCE:
39 set_func(c_cleanup_nonce, OSSL_FUNC_cleanup_nonce(fns));
40 break;
41 }
42 #undef set_func
43 }
44 return 1;
45 }
46
ossl_prov_get_entropy(PROV_CTX * prov_ctx,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)47 size_t ossl_prov_get_entropy(PROV_CTX *prov_ctx, unsigned char **pout,
48 int entropy, size_t min_len, size_t max_len)
49 {
50 if (c_get_entropy == NULL)
51 return 0;
52 return c_get_entropy(ossl_prov_ctx_get0_handle(prov_ctx),
53 pout, entropy, min_len, max_len);
54 }
55
ossl_prov_cleanup_entropy(PROV_CTX * prov_ctx,unsigned char * buf,size_t len)56 void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,
57 size_t len)
58 {
59 if (c_cleanup_entropy != NULL)
60 c_cleanup_entropy(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
61 }
62
ossl_prov_get_nonce(PROV_CTX * prov_ctx,unsigned char ** pout,size_t min_len,size_t max_len,const void * salt,size_t salt_len)63 size_t ossl_prov_get_nonce(PROV_CTX *prov_ctx, unsigned char **pout,
64 size_t min_len, size_t max_len,
65 const void *salt,size_t salt_len)
66 {
67 if (c_get_nonce == NULL)
68 return 0;
69 return c_get_nonce(ossl_prov_ctx_get0_handle(prov_ctx), pout,
70 min_len, max_len, salt, salt_len);
71 }
72
ossl_prov_cleanup_nonce(PROV_CTX * prov_ctx,unsigned char * buf,size_t len)73 void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)
74 {
75 if (c_cleanup_nonce != NULL)
76 c_cleanup_nonce(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
77 }
78