b3dee525dcddf5db8e083fbb1f06e75e0d72a9ee
[openssl.git] / include / openssl / kdf.h
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OPENSSL_KDF_H
11 # define OPENSSL_KDF_H
12 # pragma once
13
14 # include <openssl/macros.h>
15 # ifndef OPENSSL_NO_DEPRECATED_3_0
16 #  define HEADER_KDF_H
17 # endif
18
19 # include <stdarg.h>
20 # include <stddef.h>
21 # include <openssl/types.h>
22 # include <openssl/core.h>
23
24 # ifdef __cplusplus
25 extern "C" {
26 # endif
27
28 int EVP_KDF_up_ref(EVP_KDF *kdf);
29 void EVP_KDF_free(EVP_KDF *kdf);
30 EVP_KDF *EVP_KDF_fetch(OPENSSL_CTX *libctx, const char *algorithm,
31                        const char *properties);
32
33 EVP_KDF_CTX *EVP_KDF_CTX_new(EVP_KDF *kdf);
34 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx);
35 EVP_KDF_CTX *EVP_KDF_CTX_dup(const EVP_KDF_CTX *src);
36 int EVP_KDF_number(const EVP_KDF *kdf);
37 int EVP_KDF_is_a(const EVP_KDF *kdf, const char *name);
38 const OSSL_PROVIDER *EVP_KDF_provider(const EVP_KDF *kdf);
39 const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx);
40
41 void EVP_KDF_reset(EVP_KDF_CTX *ctx);
42 size_t EVP_KDF_size(EVP_KDF_CTX *ctx);
43 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen);
44 int EVP_KDF_get_params(EVP_KDF *kdf, OSSL_PARAM params[]);
45 int EVP_KDF_CTX_get_params(EVP_KDF_CTX *ctx, OSSL_PARAM params[]);
46 int EVP_KDF_CTX_set_params(EVP_KDF_CTX *ctx, const OSSL_PARAM params[]);
47 const OSSL_PARAM *EVP_KDF_gettable_params(const EVP_KDF *kdf);
48 const OSSL_PARAM *EVP_KDF_gettable_ctx_params(const EVP_KDF *kdf);
49 const OSSL_PARAM *EVP_KDF_settable_ctx_params(const EVP_KDF *kdf);
50
51 void EVP_KDF_do_all_provided(OPENSSL_CTX *libctx,
52                              void (*fn)(EVP_KDF *kdf, void *arg),
53                              void *arg);
54 void EVP_KDF_names_do_all(const EVP_KDF *kdf,
55                           void (*fn)(const char *name, void *data),
56                           void *data);
57
58 # define EVP_KDF_CTRL_SET_PASS               0x01 /* unsigned char *, size_t */
59 # define EVP_KDF_CTRL_SET_SALT               0x02 /* unsigned char *, size_t */
60 # define EVP_KDF_CTRL_SET_ITER               0x03 /* int */
61 # define EVP_KDF_CTRL_SET_MD                 0x04 /* EVP_MD * */
62 # define EVP_KDF_CTRL_SET_KEY                0x05 /* unsigned char *, size_t */
63 # define EVP_KDF_CTRL_SET_MAXMEM_BYTES       0x06 /* uint64_t */
64 # define EVP_KDF_CTRL_SET_TLS_SECRET         0x07 /* unsigned char *, size_t */
65 # define EVP_KDF_CTRL_ADD_TLS_SEED           0x08 /* unsigned char *, size_t */
66 # define EVP_KDF_CTRL_RESET_HKDF_INFO        0x09
67 # define EVP_KDF_CTRL_ADD_HKDF_INFO          0x0a /* unsigned char *, size_t */
68 # define EVP_KDF_CTRL_SET_HKDF_MODE          0x0b /* int */
69 # define EVP_KDF_CTRL_SET_SCRYPT_N           0x0c /* uint64_t */
70 # define EVP_KDF_CTRL_SET_SCRYPT_R           0x0d /* uint32_t */
71 # define EVP_KDF_CTRL_SET_SCRYPT_P           0x0e /* uint32_t */
72 # define EVP_KDF_CTRL_SET_SSHKDF_XCGHASH     0x0f /* unsigned char *, size_t */
73 # define EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID  0x10 /* unsigned char *, size_t */
74 # define EVP_KDF_CTRL_SET_SSHKDF_TYPE        0x11 /* int */
75 # define EVP_KDF_CTRL_SET_MAC                0x12 /* EVP_MAC * */
76 # define EVP_KDF_CTRL_SET_MAC_SIZE           0x13 /* size_t */
77 # define EVP_KDF_CTRL_SET_SSKDF_INFO         0x14 /* unsigned char *, size_t */
78 # define EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE  0x15 /* int */
79 # define EVP_KDF_CTRL_SET_UKM                0x16 /* unsigned char *, size_t */
80 # define EVP_KDF_CTRL_SET_CEK_ALG            0x17 /* char * */
81 # define EVP_KDF_CTRL_SET_SHARED_INFO        EVP_KDF_CTRL_SET_SSKDF_INFO
82
83 # define EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND  0
84 # define EVP_KDF_HKDF_MODE_EXTRACT_ONLY        1
85 # define EVP_KDF_HKDF_MODE_EXPAND_ONLY         2
86
87 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV     65
88 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_SRV_TO_CLI     66
89 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_CLI_TO_SRV 67
90 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_SRV_TO_CLI 68
91 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_CLI_TO_SRV  69
92 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_SRV_TO_CLI  70
93
94 /**** The legacy PKEY-based KDF API follows. ****/
95
96 # define EVP_PKEY_CTRL_TLS_MD                   (EVP_PKEY_ALG_CTRL)
97 # define EVP_PKEY_CTRL_TLS_SECRET               (EVP_PKEY_ALG_CTRL + 1)
98 # define EVP_PKEY_CTRL_TLS_SEED                 (EVP_PKEY_ALG_CTRL + 2)
99 # define EVP_PKEY_CTRL_HKDF_MD                  (EVP_PKEY_ALG_CTRL + 3)
100 # define EVP_PKEY_CTRL_HKDF_SALT                (EVP_PKEY_ALG_CTRL + 4)
101 # define EVP_PKEY_CTRL_HKDF_KEY                 (EVP_PKEY_ALG_CTRL + 5)
102 # define EVP_PKEY_CTRL_HKDF_INFO                (EVP_PKEY_ALG_CTRL + 6)
103 # define EVP_PKEY_CTRL_HKDF_MODE                (EVP_PKEY_ALG_CTRL + 7)
104 # define EVP_PKEY_CTRL_PASS                     (EVP_PKEY_ALG_CTRL + 8)
105 # define EVP_PKEY_CTRL_SCRYPT_SALT              (EVP_PKEY_ALG_CTRL + 9)
106 # define EVP_PKEY_CTRL_SCRYPT_N                 (EVP_PKEY_ALG_CTRL + 10)
107 # define EVP_PKEY_CTRL_SCRYPT_R                 (EVP_PKEY_ALG_CTRL + 11)
108 # define EVP_PKEY_CTRL_SCRYPT_P                 (EVP_PKEY_ALG_CTRL + 12)
109 # define EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES      (EVP_PKEY_ALG_CTRL + 13)
110
111 # define EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND \
112             EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND
113 # define EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY       \
114             EVP_KDF_HKDF_MODE_EXTRACT_ONLY
115 # define EVP_PKEY_HKDEF_MODE_EXPAND_ONLY        \
116             EVP_KDF_HKDF_MODE_EXPAND_ONLY
117
118 # define EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) \
119             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
120                               EVP_PKEY_CTRL_TLS_MD, 0, (void *)(md))
121
122 # define EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, seclen) \
123             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
124                               EVP_PKEY_CTRL_TLS_SECRET, seclen, (void *)(sec))
125
126 # define EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seedlen) \
127             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
128                               EVP_PKEY_CTRL_TLS_SEED, seedlen, (void *)(seed))
129
130 # define EVP_PKEY_CTX_set_hkdf_md(pctx, md) \
131             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
132                               EVP_PKEY_CTRL_HKDF_MD, 0, (void *)(md))
133
134 # define EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltlen) \
135             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
136                               EVP_PKEY_CTRL_HKDF_SALT, saltlen, (void *)(salt))
137
138 # define EVP_PKEY_CTX_set1_hkdf_key(pctx, key, keylen) \
139             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
140                               EVP_PKEY_CTRL_HKDF_KEY, keylen, (void *)(key))
141
142 # define EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infolen) \
143             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
144                               EVP_PKEY_CTRL_HKDF_INFO, infolen, (void *)(info))
145
146 # define EVP_PKEY_CTX_hkdf_mode(pctx, mode) \
147             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
148                               EVP_PKEY_CTRL_HKDF_MODE, mode, NULL)
149
150 # define EVP_PKEY_CTX_set1_pbe_pass(pctx, pass, passlen) \
151             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
152                             EVP_PKEY_CTRL_PASS, passlen, (void *)(pass))
153
154 # define EVP_PKEY_CTX_set1_scrypt_salt(pctx, salt, saltlen) \
155             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
156                             EVP_PKEY_CTRL_SCRYPT_SALT, saltlen, (void *)(salt))
157
158 # define EVP_PKEY_CTX_set_scrypt_N(pctx, n) \
159             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
160                             EVP_PKEY_CTRL_SCRYPT_N, n)
161
162 # define EVP_PKEY_CTX_set_scrypt_r(pctx, r) \
163             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
164                             EVP_PKEY_CTRL_SCRYPT_R, r)
165
166 # define EVP_PKEY_CTX_set_scrypt_p(pctx, p) \
167             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
168                             EVP_PKEY_CTRL_SCRYPT_P, p)
169
170 # define EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, maxmem_bytes) \
171             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
172                             EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, maxmem_bytes)
173
174
175 # ifdef __cplusplus
176 }
177 # endif
178 #endif