hkdf: when HMAC key is all zeros, still set a valid key length
[openssl.git] / providers / common / provider_seeding.c
1 /*
2  * Copyright 2020 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
18 int ossl_prov_seeding_from_dispatch(const OSSL_DISPATCH *fns)
19 {
20     for (; fns->function_id != 0; fns++) {
21         switch (fns->function_id) {
22         case OSSL_FUNC_GET_ENTROPY:
23             if (c_get_entropy == NULL)
24                 c_get_entropy = OSSL_FUNC_get_entropy(fns);
25             break;
26         case OSSL_FUNC_CLEANUP_ENTROPY:
27             if (c_cleanup_entropy == NULL)
28                 c_cleanup_entropy = OSSL_FUNC_cleanup_entropy(fns);
29             break;
30         case OSSL_FUNC_GET_NONCE:
31             if (c_get_nonce == NULL)
32                 c_get_nonce = OSSL_FUNC_get_nonce(fns);
33             break;
34         case OSSL_FUNC_CLEANUP_NONCE:
35             if (c_cleanup_nonce == NULL)
36                 c_cleanup_nonce = OSSL_FUNC_cleanup_nonce(fns);
37             break;
38         }
39     }
40     return 1;
41 }
42
43 size_t ossl_prov_get_entropy(PROV_CTX *prov_ctx, unsigned char **pout,
44                              int entropy, size_t min_len, size_t max_len)
45 {
46     if (c_get_entropy == NULL)
47         return 0;
48     return c_get_entropy(ossl_prov_ctx_get0_handle(prov_ctx),
49                          pout, entropy, min_len, max_len);
50 }
51
52 void ossl_prov_cleanup_entropy(PROV_CTX *prov_ctx, unsigned char *buf,
53                                size_t len)
54 {
55     if (c_cleanup_entropy != NULL)
56         c_cleanup_entropy(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
57 }
58
59 size_t ossl_prov_get_nonce(PROV_CTX *prov_ctx, unsigned char **pout,
60                            size_t min_len, size_t max_len,
61                            const void *salt,size_t salt_len)
62 {
63     if (c_get_nonce == NULL)
64         return 0;
65     return c_get_nonce(ossl_prov_ctx_get0_handle(prov_ctx), pout,
66                        min_len, max_len, salt, salt_len);
67 }
68
69 void ossl_prov_cleanup_nonce(PROV_CTX *prov_ctx, unsigned char *buf, size_t len)
70 {
71     if (c_cleanup_nonce != NULL)
72         c_cleanup_nonce(ossl_prov_ctx_get0_handle(prov_ctx), buf, len);
73 }