]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/kdf_lib.c
In provider implemented methods, save the name number, not the name string
[thirdparty/openssl.git] / crypto / evp / kdf_lib.c
CommitLineData
5a285add 1/*
d2ba8123
SL
2 * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
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>
5a285add
DM
20#include "internal/asn1_int.h"
21#include "internal/evp_int.h"
22#include "internal/numbers.h"
fb9e6dd6 23#include "internal/provider.h"
5a285add
DM
24#include "evp_locl.h"
25
fb9e6dd6 26EVP_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
P
34 if (ctx == NULL
35 || (ctx->data = kdf->newctx(ossl_provider_ctx(kdf->prov))) == NULL
36 || !EVP_KDF_up_ref(kdf)) {
d2ba8123 37 EVPerr(EVP_F_EVP_KDF_CTX_NEW, ERR_R_MALLOC_FAILURE);
fb9e6dd6
P
38 if (ctx != NULL)
39 kdf->freectx(ctx->data);
d2ba8123
SL
40 OPENSSL_free(ctx);
41 ctx = NULL;
42 } else {
43 ctx->meth = kdf;
44 }
45 return ctx;
5a285add
DM
46}
47
fb9e6dd6 48void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
5a285add 49{
fb9e6dd6
P
50 if (ctx != NULL) {
51 ctx->meth->freectx(ctx->data);
52 ctx->data = NULL;
53 EVP_KDF_free(ctx->meth);
54 OPENSSL_free(ctx);
55 }
5a285add
DM
56}
57
fb9e6dd6 58EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src)
5a285add 59{
fb9e6dd6
P
60 EVP_KDF_CTX *dst;
61
6ce4ff19 62 if (src == NULL || src->data == NULL || src->meth->dupctx == NULL)
fb9e6dd6
P
63 return NULL;
64
65 dst = OPENSSL_malloc(sizeof(*dst));
66 if (dst == NULL) {
67 EVPerr(EVP_F_EVP_KDF_CTX_DUP, ERR_R_MALLOC_FAILURE);
68 return NULL;
69 }
70
71 memcpy(dst, src, sizeof(*dst));
72 if (!EVP_KDF_up_ref(dst->meth)) {
73 EVPerr(EVP_F_EVP_KDF_CTX_DUP, ERR_R_MALLOC_FAILURE);
74 OPENSSL_free(dst);
75 return NULL;
76 }
77
78 dst->data = src->meth->dupctx(src->data);
79 if (dst->data == NULL) {
80 EVP_KDF_CTX_free(dst);
81 return NULL;
82 }
83 return dst;
d2ba8123 84}
5a285add 85
fb9e6dd6 86const char *EVP_KDF_name(const EVP_KDF *kdf)
d2ba8123 87{
f7c16d48 88 return evp_first_name(kdf->prov, kdf->name_id);
5a285add
DM
89}
90
fb9e6dd6 91const OSSL_PROVIDER *EVP_KDF_provider(const EVP_KDF *kdf)
5a285add 92{
fb9e6dd6
P
93 return kdf->prov;
94}
5a285add 95
fb9e6dd6
P
96const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
97{
98 return ctx->meth;
5a285add
DM
99}
100
101void EVP_KDF_reset(EVP_KDF_CTX *ctx)
102{
103 if (ctx == NULL)
104 return;
105
d2ba8123 106 if (ctx->meth->reset != NULL)
fb9e6dd6 107 ctx->meth->reset(ctx->data);
5a285add
DM
108}
109
fb9e6dd6 110size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
5a285add 111{
fb9e6dd6
P
112 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
113 size_t s;
5a285add 114
5a285add
DM
115 if (ctx == NULL)
116 return 0;
117
fb9e6dd6
P
118 *params = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &s);
119 if (ctx->meth->get_ctx_params != NULL
6b3d0423 120 && ctx->meth->get_ctx_params(ctx->data, params))
fb9e6dd6
P
121 return s;
122 if (ctx->meth->get_params != NULL
123 && ctx->meth->get_params(params))
124 return s;
125 return 0;
5a285add
DM
126}
127
fb9e6dd6 128int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
5a285add 129{
5a285add
DM
130 if (ctx == NULL)
131 return 0;
132
fb9e6dd6 133 return ctx->meth->derive(ctx->data, key, keylen);
5a285add
DM
134}
135
fb9e6dd6
P
136/*
137 * The {get,set}_params functions return 1 if there is no corresponding
138 * function in the implementation. This is the same as if there was one,
139 * but it didn't recognise any of the given params, i.e. nothing in the
140 * bag of parameters was useful.
141 */
142int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[])
5a285add 143{
fb9e6dd6
P
144 if (kdf->get_params != NULL)
145 return kdf->get_params(params);
146 return 1;
5a285add
DM
147}
148
fb9e6dd6 149int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[])
5a285add 150{
fb9e6dd6
P
151 if (ctx->meth->get_ctx_params != NULL)
152 return ctx->meth->get_ctx_params(ctx->data, params);
153 return 1;
154}
5a285add 155
fb9e6dd6
P
156int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[])
157{
158 if (ctx->meth->set_ctx_params != NULL)
159 return ctx->meth->set_ctx_params(ctx->data, params);
160 return 1;
5a285add 161}