Remove init of MACs from EVP
[openssl.git] / crypto / evp / kdf_lib.c
1 /*
2  * Copyright 2018-2019 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/engine.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/kdf.h>
18 #include "internal/asn1_int.h"
19 #include "internal/evp_int.h"
20 #include "internal/numbers.h"
21 #include "evp_locl.h"
22
23 EVP_KDF_CTX *EVP_KDF_CTX_new(const EVP_KDF *kdf)
24 {
25     EVP_KDF_CTX *ctx = NULL;
26
27     if (kdf == NULL)
28         return NULL;
29
30     ctx = OPENSSL_zalloc(sizeof(EVP_KDF_CTX));
31     if (ctx == NULL || (ctx->impl = kdf->new()) == NULL) {
32         EVPerr(EVP_F_EVP_KDF_CTX_NEW, ERR_R_MALLOC_FAILURE);
33         OPENSSL_free(ctx);
34         ctx = NULL;
35     } else {
36         ctx->meth = kdf;
37     }
38     return ctx;
39 }
40
41 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id)
42 {
43     const EVP_KDF *kdf = EVP_get_kdfbynid(id);
44
45     return EVP_KDF_CTX_new(kdf);
46 }
47
48 int EVP_KDF_nid(const EVP_KDF *kdf)
49 {
50     return kdf->type;
51 }
52
53 const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx)
54 {
55     return ctx->meth;
56 }
57
58 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
59 {
60     if (ctx == NULL)
61         return;
62
63     ctx->meth->free(ctx->impl);
64     OPENSSL_free(ctx);
65 }
66
67 void EVP_KDF_reset(EVP_KDF_CTX *ctx)
68 {
69     if (ctx == NULL)
70         return;
71
72     if (ctx->meth->reset != NULL)
73         ctx->meth->reset(ctx->impl);
74 }
75
76 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...)
77 {
78     int ret;
79     va_list args;
80
81     va_start(args, cmd);
82     ret = EVP_KDF_vctrl(ctx, cmd, args);
83     va_end(args);
84
85     if (ret == -2)
86         EVPerr(EVP_F_EVP_KDF_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
87
88     return ret;
89 }
90
91 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args)
92 {
93     if (ctx == NULL)
94         return 0;
95
96     return ctx->meth->ctrl(ctx->impl, cmd, args);
97 }
98
99 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value)
100 {
101     int ret;
102
103     if (ctx == NULL)
104         return 0;
105
106     if (ctx->meth->ctrl_str == NULL) {
107         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
108         return -2;
109     }
110
111     ret = ctx->meth->ctrl_str(ctx->impl, type, value);
112     if (ret == -2)
113         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
114
115     return ret;
116 }
117
118 size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
119 {
120     if (ctx == NULL)
121         return 0;
122
123     if (ctx->meth->size == NULL)
124         return SIZE_MAX;
125
126     return ctx->meth->size(ctx->impl);
127 }
128
129 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
130 {
131     if (ctx == NULL)
132         return 0;
133
134     return ctx->meth->derive(ctx->impl, key, keylen);
135 }