/src/openssl30/crypto/evp/kdf_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved. |
4 | | * |
5 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
6 | | * this file except in compliance with the License. You can obtain a copy |
7 | | * in the file LICENSE in the source distribution or at |
8 | | * https://www.openssl.org/source/license.html |
9 | | */ |
10 | | |
11 | | #include <stdio.h> |
12 | | #include <stdlib.h> |
13 | | #include "internal/cryptlib.h" |
14 | | #include <openssl/evp.h> |
15 | | #include <openssl/kdf.h> |
16 | | #include <openssl/core.h> |
17 | | #include <openssl/core_names.h> |
18 | | #include "crypto/evp.h" |
19 | | #include "internal/numbers.h" |
20 | | #include "internal/provider.h" |
21 | | #include "evp_local.h" |
22 | | |
23 | | EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf) |
24 | 628k | { |
25 | 628k | EVP_KDF_CTX *ctx = NULL; |
26 | | |
27 | 628k | if (kdf == NULL) |
28 | 0 | return NULL; |
29 | | |
30 | 628k | ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX)); |
31 | 628k | if (ctx == NULL |
32 | 628k | || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL |
33 | 628k | || !EVP_KDF_up_ref(kdf)) { |
34 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
35 | 0 | if (ctx != NULL) |
36 | 0 | kdf->freectx(ctx->algctx); |
37 | 0 | OPENSSL_free(ctx); |
38 | 0 | ctx = NULL; |
39 | 628k | } else { |
40 | 628k | ctx->meth = kdf; |
41 | 628k | } |
42 | 628k | return ctx; |
43 | 628k | } |
44 | | |
45 | | void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx) |
46 | 628k | { |
47 | 628k | if (ctx == NULL) |
48 | 0 | return; |
49 | 628k | ctx->meth->freectx(ctx->algctx); |
50 | 628k | ctx->algctx = NULL; |
51 | 628k | EVP_KDF_free(ctx->meth); |
52 | 628k | OPENSSL_free(ctx); |
53 | 628k | } |
54 | | |
55 | | EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src) |
56 | 0 | { |
57 | 0 | EVP_KDF_CTX *dst; |
58 | |
|
59 | 0 | if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL) |
60 | 0 | return NULL; |
61 | | |
62 | 0 | dst = OPENSSL_malloc(sizeof(*dst)); |
63 | 0 | if (dst == NULL) { |
64 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
65 | 0 | return NULL; |
66 | 0 | } |
67 | | |
68 | 0 | memcpy(dst, src, sizeof(*dst)); |
69 | 0 | if (!EVP_KDF_up_ref(dst->meth)) { |
70 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
71 | 0 | OPENSSL_free(dst); |
72 | 0 | return NULL; |
73 | 0 | } |
74 | | |
75 | 0 | dst->algctx = src->meth->dupctx(src->algctx); |
76 | 0 | if (dst->algctx == NULL) { |
77 | 0 | EVP_KDF_CTX_free(dst); |
78 | 0 | return NULL; |
79 | 0 | } |
80 | 0 | return dst; |
81 | 0 | } |
82 | | |
83 | | int evp_kdf_get_number(const EVP_KDF *kdf) |
84 | 0 | { |
85 | 0 | return kdf->name_id; |
86 | 0 | } |
87 | | |
88 | | const char *EVP_KDF_get0_name(const EVP_KDF *kdf) |
89 | 0 | { |
90 | 0 | return kdf->type_name; |
91 | 0 | } |
92 | | |
93 | | const char *EVP_KDF_get0_description(const EVP_KDF *kdf) |
94 | 0 | { |
95 | 0 | return kdf->description; |
96 | 0 | } |
97 | | |
98 | | int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name) |
99 | 0 | { |
100 | 0 | return kdf != NULL && evp_is_a(kdf->prov, kdf->name_id, NULL, name); |
101 | 0 | } |
102 | | |
103 | | const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf) |
104 | 2.06k | { |
105 | 2.06k | return kdf->prov; |
106 | 2.06k | } |
107 | | |
108 | | const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx) |
109 | 0 | { |
110 | 0 | return ctx->meth; |
111 | 0 | } |
112 | | |
113 | | void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx) |
114 | 0 | { |
115 | 0 | if (ctx == NULL) |
116 | 0 | return; |
117 | | |
118 | 0 | if (ctx->meth->reset != NULL) |
119 | 0 | ctx->meth->reset(ctx->algctx); |
120 | 0 | } |
121 | | |
122 | | size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx) |
123 | 0 | { |
124 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
125 | 0 | size_t s = 0; |
126 | |
|
127 | 0 | if (ctx == NULL) |
128 | 0 | return 0; |
129 | | |
130 | 0 | *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s); |
131 | 0 | if (ctx->meth->get_ctx_params != NULL |
132 | 0 | && ctx->meth->get_ctx_params(ctx->algctx, params)) |
133 | 0 | return s; |
134 | 0 | if (ctx->meth->get_params != NULL |
135 | 0 | && ctx->meth->get_params(params)) |
136 | 0 | return s; |
137 | 0 | return 0; |
138 | 0 | } |
139 | | |
140 | | int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen, |
141 | | const OSSL_PARAM params[]) |
142 | 627k | { |
143 | 627k | if (ctx == NULL) |
144 | 0 | return 0; |
145 | | |
146 | 627k | return ctx->meth->derive(ctx->algctx, key, keylen, params); |
147 | 627k | } |
148 | | |
149 | | /* |
150 | | * The {get,set}_params functions return 1 if there is no corresponding |
151 | | * function in the implementation. This is the same as if there was one, |
152 | | * but it didn't recognise any of the given params, i.e. nothing in the |
153 | | * bag of parameters was useful. |
154 | | */ |
155 | | int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[]) |
156 | 0 | { |
157 | 0 | if (kdf->get_params != NULL) |
158 | 0 | return kdf->get_params(params); |
159 | 0 | return 1; |
160 | 0 | } |
161 | | |
162 | | int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[]) |
163 | 0 | { |
164 | 0 | if (ctx->meth->get_ctx_params != NULL) |
165 | 0 | return ctx->meth->get_ctx_params(ctx->algctx, params); |
166 | 0 | return 1; |
167 | 0 | } |
168 | | |
169 | | int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[]) |
170 | 2.05k | { |
171 | 2.05k | if (ctx->meth->set_ctx_params != NULL) |
172 | 2.05k | return ctx->meth->set_ctx_params(ctx->algctx, params); |
173 | 0 | return 1; |
174 | 2.05k | } |
175 | | |
176 | | int EVP_KDF_names_do_all(const EVP_KDF *kdf, |
177 | | void (*fn)(const char *name, void *data), |
178 | | void *data) |
179 | 0 | { |
180 | 0 | if (kdf->prov != NULL) |
181 | 0 | return evp_names_do_all(kdf->prov, kdf->name_id, fn, data); |
182 | | |
183 | 0 | return 1; |
184 | 0 | } |