/src/openssl/crypto/evp/kdf_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2021 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 | 0 | { |
25 | 0 | EVP_KDF_CTX *ctx = NULL; |
26 | |
|
27 | 0 | if (kdf == NULL) |
28 | 0 | return NULL; |
29 | | |
30 | 0 | ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX)); |
31 | 0 | if (ctx == NULL |
32 | 0 | || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL |
33 | 0 | || !EVP_KDF_up_ref(kdf)) { |
34 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
35 | 0 | if (ctx != NULL) |
36 | 0 | kdf->freectx(ctx->algctx); |
37 | 0 | OPENSSL_free(ctx); |
38 | 0 | ctx = NULL; |
39 | 0 | } else { |
40 | 0 | ctx->meth = kdf; |
41 | 0 | } |
42 | 0 | return ctx; |
43 | 0 | } |
44 | | |
45 | | void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx) |
46 | 0 | { |
47 | 0 | if (ctx == NULL) |
48 | 0 | return; |
49 | 0 | ctx->meth->freectx(ctx->algctx); |
50 | 0 | ctx->algctx = NULL; |
51 | 0 | EVP_KDF_free(ctx->meth); |
52 | 0 | OPENSSL_free(ctx); |
53 | 0 | } |
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 | return NULL; |
65 | | |
66 | 0 | memcpy(dst, src, sizeof(*dst)); |
67 | 0 | if (!EVP_KDF_up_ref(dst->meth)) { |
68 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
69 | 0 | OPENSSL_free(dst); |
70 | 0 | return NULL; |
71 | 0 | } |
72 | | |
73 | 0 | dst->algctx = src->meth->dupctx(src->algctx); |
74 | 0 | if (dst->algctx == NULL) { |
75 | 0 | EVP_KDF_CTX_free(dst); |
76 | 0 | return NULL; |
77 | 0 | } |
78 | 0 | return dst; |
79 | 0 | } |
80 | | |
81 | | int evp_kdf_get_number(const EVP_KDF *kdf) |
82 | 0 | { |
83 | 0 | return kdf->name_id; |
84 | 0 | } |
85 | | |
86 | | const char *EVP_KDF_get0_name(const EVP_KDF *kdf) |
87 | 0 | { |
88 | 0 | return kdf->type_name; |
89 | 0 | } |
90 | | |
91 | | const char *EVP_KDF_get0_description(const EVP_KDF *kdf) |
92 | 0 | { |
93 | 0 | return kdf->description; |
94 | 0 | } |
95 | | |
96 | | int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name) |
97 | 0 | { |
98 | 0 | return kdf != NULL && evp_is_a(kdf->prov, kdf->name_id, NULL, name); |
99 | 0 | } |
100 | | |
101 | | const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf) |
102 | 0 | { |
103 | 0 | return kdf->prov; |
104 | 0 | } |
105 | | |
106 | | const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx) |
107 | 0 | { |
108 | 0 | return ctx->meth; |
109 | 0 | } |
110 | | |
111 | | void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx) |
112 | 0 | { |
113 | 0 | if (ctx == NULL) |
114 | 0 | return; |
115 | | |
116 | 0 | if (ctx->meth->reset != NULL) |
117 | 0 | ctx->meth->reset(ctx->algctx); |
118 | 0 | } |
119 | | |
120 | | size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx) |
121 | 0 | { |
122 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
123 | 0 | size_t s = 0; |
124 | |
|
125 | 0 | if (ctx == NULL) |
126 | 0 | return 0; |
127 | | |
128 | 0 | *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s); |
129 | 0 | if (ctx->meth->get_ctx_params != NULL |
130 | 0 | && ctx->meth->get_ctx_params(ctx->algctx, params)) |
131 | 0 | return s; |
132 | 0 | if (ctx->meth->get_params != NULL |
133 | 0 | && ctx->meth->get_params(params)) |
134 | 0 | return s; |
135 | 0 | return 0; |
136 | 0 | } |
137 | | |
138 | | int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen, |
139 | | const OSSL_PARAM params[]) |
140 | 0 | { |
141 | 0 | if (ctx == NULL) |
142 | 0 | return 0; |
143 | | |
144 | 0 | return ctx->meth->derive(ctx->algctx, key, keylen, params); |
145 | 0 | } |
146 | | |
147 | | /* |
148 | | * The {get,set}_params functions return 1 if there is no corresponding |
149 | | * function in the implementation. This is the same as if there was one, |
150 | | * but it didn't recognise any of the given params, i.e. nothing in the |
151 | | * bag of parameters was useful. |
152 | | */ |
153 | | int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[]) |
154 | 0 | { |
155 | 0 | if (kdf->get_params != NULL) |
156 | 0 | return kdf->get_params(params); |
157 | 0 | return 1; |
158 | 0 | } |
159 | | |
160 | | int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[]) |
161 | 0 | { |
162 | 0 | if (ctx->meth->get_ctx_params != NULL) |
163 | 0 | return ctx->meth->get_ctx_params(ctx->algctx, params); |
164 | 0 | return 1; |
165 | 0 | } |
166 | | |
167 | | int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[]) |
168 | 0 | { |
169 | 0 | if (ctx->meth->set_ctx_params != NULL) |
170 | 0 | return ctx->meth->set_ctx_params(ctx->algctx, params); |
171 | 0 | return 1; |
172 | 0 | } |
173 | | |
174 | | int EVP_KDF_names_do_all(const EVP_KDF *kdf, |
175 | | void (*fn)(const char *name, void *data), |
176 | | void *data) |
177 | 0 | { |
178 | 0 | if (kdf->prov != NULL) |
179 | 0 | return evp_names_do_all(kdf->prov, kdf->name_id, fn, data); |
180 | | |
181 | 0 | return 1; |
182 | 0 | } |