Added new EVP/KDF API.
[openssl.git] / crypto / evp / kdf_lib.c
1 /*
2  * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2018, 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 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
24
25 /* This array needs to be in order of NIDs */
26 static const EVP_KDF_METHOD *standard_methods[] = {
27     &pbkdf2_kdf_meth,
28 #ifndef OPENSSL_NO_SCRYPT
29     &scrypt_kdf_meth,
30 #endif
31     &tls1_prf_kdf_meth,
32     &hkdf_kdf_meth
33 };
34
35 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
36                            kmeth);
37
38 static int kmeth_cmp(const EVP_KDF_METHOD *const *a,
39                      const EVP_KDF_METHOD *const *b)
40 {
41     return ((*a)->type - (*b)->type);
42 }
43
44 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
45                              kmeth);
46
47 static const EVP_KDF_METHOD *kdf_meth_find(int type)
48 {
49     EVP_KDF_METHOD tmp;
50     const EVP_KDF_METHOD *t = &tmp, **ret;
51
52     tmp.type = type;
53     ret = OBJ_bsearch_kmeth(&t, standard_methods,
54                             OSSL_NELEM(standard_methods));
55     if (ret == NULL || *ret == NULL)
56         return NULL;
57
58     return *ret;
59 }
60
61 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id)
62 {
63     EVP_KDF_CTX *ret;
64     const EVP_KDF_METHOD *kmeth;
65
66     kmeth = kdf_meth_find(id);
67     if (kmeth == NULL) {
68         EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, EVP_R_UNSUPPORTED_ALGORITHM);
69         return NULL;
70     }
71
72     ret = OPENSSL_zalloc(sizeof(*ret));
73     if (ret == NULL) {
74         EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, ERR_R_MALLOC_FAILURE);
75         return NULL;
76     }
77
78     if (kmeth->new != NULL && (ret->impl = kmeth->new()) == NULL) {
79         EVP_KDF_CTX_free(ret);
80         return NULL;
81     }
82
83     ret->kmeth = kmeth;
84     return ret;
85 }
86
87 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
88 {
89     if (ctx == NULL)
90         return;
91
92     ctx->kmeth->free(ctx->impl);
93     OPENSSL_free(ctx);
94 }
95
96 void EVP_KDF_reset(EVP_KDF_CTX *ctx)
97 {
98     if (ctx == NULL)
99         return;
100
101     if (ctx->kmeth->reset != NULL)
102         ctx->kmeth->reset(ctx->impl);
103 }
104
105 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...)
106 {
107     int ret;
108     va_list args;
109
110     va_start(args, cmd);
111     ret = EVP_KDF_vctrl(ctx, cmd, args);
112     va_end(args);
113
114     if (ret == -2)
115         EVPerr(EVP_F_EVP_KDF_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
116
117     return ret;
118 }
119
120 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args)
121 {
122     if (ctx == NULL)
123         return 0;
124
125     return ctx->kmeth->ctrl(ctx->impl, cmd, args);
126 }
127
128 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value)
129 {
130     int ret;
131
132     if (ctx == NULL)
133         return 0;
134
135     if (ctx->kmeth->ctrl_str == NULL) {
136         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
137         return -2;
138     }
139
140     ret = ctx->kmeth->ctrl_str(ctx->impl, type, value);
141     if (ret == -2)
142         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
143
144     return ret;
145 }
146
147 size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
148 {
149     if (ctx == NULL)
150         return 0;
151
152     if (ctx->kmeth->size == NULL)
153         return SIZE_MAX;
154
155     return ctx->kmeth->size(ctx->impl);
156 }
157
158 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
159 {
160     if (ctx == NULL)
161         return 0;
162
163     return ctx->kmeth->derive(ctx->impl, key, keylen);
164 }
165