]>
Commit | Line | Data |
---|---|---|
5a285add | 1 | /* |
8020d79b | 2 | * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved. |
d2ba8123 | 3 | * Copyright (c) 2018-2019, Oracle and/or its affiliates. All rights reserved. |
5a285add DM |
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/engine.h> | |
15 | #include <openssl/evp.h> | |
16 | #include <openssl/x509v3.h> | |
17 | #include <openssl/kdf.h> | |
fb9e6dd6 P |
18 | #include <openssl/core.h> |
19 | #include <openssl/core_names.h> | |
25f2138b DMSP |
20 | #include "crypto/asn1.h" |
21 | #include "crypto/evp.h" | |
5a285add | 22 | #include "internal/numbers.h" |
fb9e6dd6 | 23 | #include "internal/provider.h" |
706457b7 | 24 | #include "evp_local.h" |
5a285add | 25 | |
660c5344 | 26 | EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf) |
5a285add | 27 | { |
8bbeaaa4 | 28 | EVP_KDF_CTX *ctx = NULL; |
d2ba8123 | 29 | |
8bbeaaa4 SL |
30 | if (kdf == NULL) |
31 | return NULL; | |
32 | ||
33 | ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX)); | |
fb9e6dd6 | 34 | if (ctx == NULL |
7c14d0c1 | 35 | || (ctx->algctx = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL |
fb9e6dd6 | 36 | || !EVP_KDF_up_ref(kdf)) { |
9311d0c4 | 37 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
fb9e6dd6 | 38 | if (ctx != NULL) |
7c14d0c1 | 39 | kdf->freectx(ctx->algctx); |
d2ba8123 SL |
40 | OPENSSL_free(ctx); |
41 | ctx = NULL; | |
42 | } else { | |
43 | ctx->meth = kdf; | |
44 | } | |
45 | return ctx; | |
5a285add DM |
46 | } |
47 | ||
660c5344 | 48 | void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx) |
5a285add | 49 | { |
543e740b RS |
50 | if (ctx == NULL) |
51 | return; | |
7c14d0c1 SL |
52 | ctx->meth->freectx(ctx->algctx); |
53 | ctx->algctx = NULL; | |
543e740b RS |
54 | EVP_KDF_free(ctx->meth); |
55 | OPENSSL_free(ctx); | |
5a285add DM |
56 | } |
57 | ||
660c5344 | 58 | EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src) |
5a285add | 59 | { |
fb9e6dd6 P |
60 | EVP_KDF_CTX *dst; |
61 | ||
7c14d0c1 | 62 | if (src == NULL || src->algctx == NULL || src->meth->dupctx == NULL) |
fb9e6dd6 P |
63 | return NULL; |
64 | ||
65 | dst = OPENSSL_malloc(sizeof(*dst)); | |
66 | if (dst == NULL) { | |
9311d0c4 | 67 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
fb9e6dd6 P |
68 | return NULL; |
69 | } | |
70 | ||
71 | memcpy(dst, src, sizeof(*dst)); | |
72 | if (!EVP_KDF_up_ref(dst->meth)) { | |
9311d0c4 | 73 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
fb9e6dd6 P |
74 | OPENSSL_free(dst); |
75 | return NULL; | |
76 | } | |
77 | ||
7c14d0c1 SL |
78 | dst->algctx = src->meth->dupctx(src->algctx); |
79 | if (dst->algctx == NULL) { | |
660c5344 | 80 | EVP_KDF_CTX_free(dst); |
fb9e6dd6 P |
81 | return NULL; |
82 | } | |
83 | return dst; | |
d2ba8123 | 84 | } |
5a285add | 85 | |
ed576acd | 86 | int EVP_KDF_get_number(const EVP_KDF *kdf) |
506cb0f6 RL |
87 | { |
88 | return kdf->name_id; | |
89 | } | |
90 | ||
ed576acd | 91 | const char *EVP_KDF_get0_name(const EVP_KDF *kdf) |
c9452d74 | 92 | { |
6c9bc258 | 93 | return kdf->type_name; |
c9452d74 P |
94 | } |
95 | ||
ed576acd | 96 | const char *EVP_KDF_get0_description(const EVP_KDF *kdf) |
03888233 RL |
97 | { |
98 | return kdf->description; | |
99 | } | |
100 | ||
251e610c RL |
101 | int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name) |
102 | { | |
e4a1d023 | 103 | return evp_is_a(kdf->prov, kdf->name_id, NULL, name); |
251e610c RL |
104 | } |
105 | ||
ed576acd | 106 | const OSSL_PROVIDER *EVP_KDF_get0_provider(const EVP_KDF *kdf) |
5a285add | 107 | { |
fb9e6dd6 P |
108 | return kdf->prov; |
109 | } | |
5a285add | 110 | |
660c5344 | 111 | const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx) |
fb9e6dd6 P |
112 | { |
113 | return ctx->meth; | |
5a285add DM |
114 | } |
115 | ||
a49d0a49 | 116 | void EVP_KDF_CTX_reset(EVP_KDF_CTX *ctx) |
5a285add DM |
117 | { |
118 | if (ctx == NULL) | |
119 | return; | |
120 | ||
d2ba8123 | 121 | if (ctx->meth->reset != NULL) |
7c14d0c1 | 122 | ctx->meth->reset(ctx->algctx); |
5a285add DM |
123 | } |
124 | ||
1ba21239 | 125 | size_t EVP_KDF_CTX_get_kdf_size(EVP_KDF_CTX *ctx) |
5a285add | 126 | { |
fb9e6dd6 P |
127 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
128 | size_t s; | |
5a285add | 129 | |
5a285add DM |
130 | if (ctx == NULL) |
131 | return 0; | |
132 | ||
fb9e6dd6 P |
133 | *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s); |
134 | if (ctx->meth->get_ctx_params != NULL | |
7c14d0c1 | 135 | && ctx->meth->get_ctx_params(ctx->algctx, params)) |
fb9e6dd6 P |
136 | return s; |
137 | if (ctx->meth->get_params != NULL | |
138 | && ctx->meth->get_params(params)) | |
139 | return s; | |
140 | return 0; | |
5a285add DM |
141 | } |
142 | ||
7c75f2da P |
143 | int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen, |
144 | const OSSL_PARAM params[]) | |
5a285add | 145 | { |
5a285add DM |
146 | if (ctx == NULL) |
147 | return 0; | |
148 | ||
7c14d0c1 | 149 | return ctx->meth->derive(ctx->algctx, key, keylen, params); |
5a285add DM |
150 | } |
151 | ||
fb9e6dd6 P |
152 | /* |
153 | * The {get,set}_params functions return 1 if there is no corresponding | |
154 | * function in the implementation. This is the same as if there was one, | |
155 | * but it didn't recognise any of the given params, i.e. nothing in the | |
156 | * bag of parameters was useful. | |
157 | */ | |
158 | int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[]) | |
5a285add | 159 | { |
fb9e6dd6 P |
160 | if (kdf->get_params != NULL) |
161 | return kdf->get_params(params); | |
162 | return 1; | |
5a285add DM |
163 | } |
164 | ||
660c5344 | 165 | int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[]) |
5a285add | 166 | { |
fb9e6dd6 | 167 | if (ctx->meth->get_ctx_params != NULL) |
7c14d0c1 | 168 | return ctx->meth->get_ctx_params(ctx->algctx, params); |
fb9e6dd6 P |
169 | return 1; |
170 | } | |
5a285add | 171 | |
660c5344 | 172 | int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[]) |
fb9e6dd6 P |
173 | { |
174 | if (ctx->meth->set_ctx_params != NULL) | |
7c14d0c1 | 175 | return ctx->meth->set_ctx_params(ctx->algctx, params); |
fb9e6dd6 | 176 | return 1; |
5a285add | 177 | } |
f651c727 | 178 | |
d84f5515 MC |
179 | int EVP_KDF_names_do_all(const EVP_KDF *kdf, |
180 | void (*fn)(const char *name, void *data), | |
181 | void *data) | |
f651c727 RL |
182 | { |
183 | if (kdf->prov != NULL) | |
d84f5515 MC |
184 | return evp_names_do_all(kdf->prov, kdf->name_id, fn, data); |
185 | ||
186 | return 1; | |
f651c727 | 187 | } |