Implement SSH KDF
[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     &sshkdf_kdf_meth,
34 };
35
36 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
37                            kmeth);
38
39 static int kmeth_cmp(const EVP_KDF_METHOD *const *a,
40                      const EVP_KDF_METHOD *const *b)
41 {
42     return ((*a)->type - (*b)->type);
43 }
44
45 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_KDF_METHOD *, const EVP_KDF_METHOD *,
46                              kmeth);
47
48 static const EVP_KDF_METHOD *kdf_meth_find(int type)
49 {
50     EVP_KDF_METHOD tmp;
51     const EVP_KDF_METHOD *t = &tmp, **ret;
52
53     tmp.type = type;
54     ret = OBJ_bsearch_kmeth(&t, standard_methods,
55                             OSSL_NELEM(standard_methods));
56     if (ret == NULL || *ret == NULL)
57         return NULL;
58
59     return *ret;
60 }
61
62 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id)
63 {
64     EVP_KDF_CTX *ret;
65     const EVP_KDF_METHOD *kmeth;
66
67     kmeth = kdf_meth_find(id);
68     if (kmeth == NULL) {
69         EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, EVP_R_UNSUPPORTED_ALGORITHM);
70         return NULL;
71     }
72
73     ret = OPENSSL_zalloc(sizeof(*ret));
74     if (ret == NULL) {
75         EVPerr(EVP_F_EVP_KDF_CTX_NEW_ID, ERR_R_MALLOC_FAILURE);
76         return NULL;
77     }
78
79     if (kmeth->new != NULL && (ret->impl = kmeth->new()) == NULL) {
80         EVP_KDF_CTX_free(ret);
81         return NULL;
82     }
83
84     ret->kmeth = kmeth;
85     return ret;
86 }
87
88 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx)
89 {
90     if (ctx == NULL)
91         return;
92
93     ctx->kmeth->free(ctx->impl);
94     OPENSSL_free(ctx);
95 }
96
97 void EVP_KDF_reset(EVP_KDF_CTX *ctx)
98 {
99     if (ctx == NULL)
100         return;
101
102     if (ctx->kmeth->reset != NULL)
103         ctx->kmeth->reset(ctx->impl);
104 }
105
106 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...)
107 {
108     int ret;
109     va_list args;
110
111     va_start(args, cmd);
112     ret = EVP_KDF_vctrl(ctx, cmd, args);
113     va_end(args);
114
115     if (ret == -2)
116         EVPerr(EVP_F_EVP_KDF_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
117
118     return ret;
119 }
120
121 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args)
122 {
123     if (ctx == NULL)
124         return 0;
125
126     return ctx->kmeth->ctrl(ctx->impl, cmd, args);
127 }
128
129 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value)
130 {
131     int ret;
132
133     if (ctx == NULL)
134         return 0;
135
136     if (ctx->kmeth->ctrl_str == NULL) {
137         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
138         return -2;
139     }
140
141     ret = ctx->kmeth->ctrl_str(ctx->impl, type, value);
142     if (ret == -2)
143         EVPerr(EVP_F_EVP_KDF_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
144
145     return ret;
146 }
147
148 size_t EVP_KDF_size(EVP_KDF_CTX *ctx)
149 {
150     if (ctx == NULL)
151         return 0;
152
153     if (ctx->kmeth->size == NULL)
154         return SIZE_MAX;
155
156     return ctx->kmeth->size(ctx->impl);
157 }
158
159 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen)
160 {
161     if (ctx == NULL)
162         return 0;
163
164     return ctx->kmeth->derive(ctx->impl, key, keylen);
165 }
166