b13b54cfc9de4c031633e910ad358dd1aeced881
[openssl.git] / include / openssl / kdf.h
1 /*
2  * Copyright 2016-2018 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 HEADER_KDF_H
11 # define HEADER_KDF_H
12
13 # include <stdarg.h>
14 # include <stddef.h>
15 # include <openssl/ossl_typ.h>
16 # include <openssl/kdferr.h>
17 # ifdef __cplusplus
18 extern "C" {
19 # endif
20
21 # define EVP_KDF_PBKDF2     NID_id_pbkdf2
22 # define EVP_KDF_SCRYPT     NID_id_scrypt
23 # define EVP_KDF_TLS1_PRF   NID_tls1_prf
24 # define EVP_KDF_HKDF       NID_hkdf
25 # define EVP_KDF_SSHKDF     NID_sshkdf
26 # define EVP_KDF_SS         NID_sskdf
27 # define EVP_KDF_X963       NID_x963kdf
28
29 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id);
30 EVP_KDF_CTX *EVP_KDF_CTX_new(const EVP_KDF *kdf);
31 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx);
32 const EVP_KDF *EVP_KDF_CTX_kdf(EVP_KDF_CTX *ctx);
33
34 void EVP_KDF_reset(EVP_KDF_CTX *ctx);
35 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...);
36 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args);
37 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value);
38 size_t EVP_KDF_size(EVP_KDF_CTX *ctx);
39 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen);
40
41 int EVP_KDF_nid(const EVP_KDF *kdf);
42 # define EVP_get_kdfbynid(a)    EVP_get_kdfbyname(OBJ_nid2sn(a))
43 # define EVP_get_kdfbyobj(a)    EVP_get_kdfbynid(OBJ_obj2nid(a))
44 # define EVP_KDF_name(o)        OBJ_nid2sn(EVP_KDF_nid(o))
45 const EVP_KDF *EVP_get_kdfbyname(const char *name);
46
47 # define EVP_KDF_CTRL_SET_PASS          0x01 /* unsigned char *, size_t */
48 # define EVP_KDF_CTRL_SET_SALT          0x02 /* unsigned char *, size_t */
49 # define EVP_KDF_CTRL_SET_ITER          0x03 /* int */
50 # define EVP_KDF_CTRL_SET_MD            0x04 /* EVP_MD * */
51 # define EVP_KDF_CTRL_SET_KEY           0x05 /* unsigned char *, size_t */
52 # define EVP_KDF_CTRL_SET_MAXMEM_BYTES  0x06 /* uint64_t */
53 # define EVP_KDF_CTRL_SET_TLS_SECRET    0x07 /* unsigned char *, size_t */
54 # define EVP_KDF_CTRL_RESET_TLS_SEED    0x08
55 # define EVP_KDF_CTRL_ADD_TLS_SEED      0x09 /* unsigned char *, size_t */
56 # define EVP_KDF_CTRL_RESET_HKDF_INFO   0x0a
57 # define EVP_KDF_CTRL_ADD_HKDF_INFO     0x0b /* unsigned char *, size_t */
58 # define EVP_KDF_CTRL_SET_HKDF_MODE     0x0c /* int */
59 # define EVP_KDF_CTRL_SET_SCRYPT_N      0x0d /* uint64_t */
60 # define EVP_KDF_CTRL_SET_SCRYPT_R      0x0e /* uint32_t */
61 # define EVP_KDF_CTRL_SET_SCRYPT_P      0x0f /* uint32_t */
62 # define EVP_KDF_CTRL_SET_SSHKDF_XCGHASH    0x10 /* unsigned char *, size_t */
63 # define EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID 0x11 /* unsigned char *, size_t */
64 # define EVP_KDF_CTRL_SET_SSHKDF_TYPE       0x12 /* int */
65 # define EVP_KDF_CTRL_SET_MAC           0x13 /* EVP_MAC * */
66 # define EVP_KDF_CTRL_SET_MAC_SIZE      0x14 /* size_t */
67 # define EVP_KDF_CTRL_SET_SSKDF_INFO    0x15 /* unsigned char *, size_t */
68 # define EVP_KDF_CTRL_SET_SHARED_INFO   EVP_KDF_CTRL_SET_SSKDF_INFO
69
70 # define EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND  0
71 # define EVP_KDF_HKDF_MODE_EXTRACT_ONLY        1
72 # define EVP_KDF_HKDF_MODE_EXPAND_ONLY         2
73
74 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV 65
75 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_SRV_TO_CLI 66
76 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_CLI_TO_SRV 67
77 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_SRV_TO_CLI 68
78 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_CLI_TO_SRV 69
79 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_SRV_TO_CLI 70
80
81 /**** The legacy PKEY-based KDF API follows. ****/
82
83 # define EVP_PKEY_CTRL_TLS_MD                   (EVP_PKEY_ALG_CTRL)
84 # define EVP_PKEY_CTRL_TLS_SECRET               (EVP_PKEY_ALG_CTRL + 1)
85 # define EVP_PKEY_CTRL_TLS_SEED                 (EVP_PKEY_ALG_CTRL + 2)
86 # define EVP_PKEY_CTRL_HKDF_MD                  (EVP_PKEY_ALG_CTRL + 3)
87 # define EVP_PKEY_CTRL_HKDF_SALT                (EVP_PKEY_ALG_CTRL + 4)
88 # define EVP_PKEY_CTRL_HKDF_KEY                 (EVP_PKEY_ALG_CTRL + 5)
89 # define EVP_PKEY_CTRL_HKDF_INFO                (EVP_PKEY_ALG_CTRL + 6)
90 # define EVP_PKEY_CTRL_HKDF_MODE                (EVP_PKEY_ALG_CTRL + 7)
91 # define EVP_PKEY_CTRL_PASS                     (EVP_PKEY_ALG_CTRL + 8)
92 # define EVP_PKEY_CTRL_SCRYPT_SALT              (EVP_PKEY_ALG_CTRL + 9)
93 # define EVP_PKEY_CTRL_SCRYPT_N                 (EVP_PKEY_ALG_CTRL + 10)
94 # define EVP_PKEY_CTRL_SCRYPT_R                 (EVP_PKEY_ALG_CTRL + 11)
95 # define EVP_PKEY_CTRL_SCRYPT_P                 (EVP_PKEY_ALG_CTRL + 12)
96 # define EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES      (EVP_PKEY_ALG_CTRL + 13)
97
98 # define EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND \
99             EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND
100 # define EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY       \
101             EVP_KDF_HKDF_MODE_EXTRACT_ONLY
102 # define EVP_PKEY_HKDEF_MODE_EXPAND_ONLY        \
103             EVP_KDF_HKDF_MODE_EXPAND_ONLY
104
105 # define EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) \
106             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
107                               EVP_PKEY_CTRL_TLS_MD, 0, (void *)(md))
108
109 # define EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, seclen) \
110             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
111                               EVP_PKEY_CTRL_TLS_SECRET, seclen, (void *)(sec))
112
113 # define EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seedlen) \
114             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
115                               EVP_PKEY_CTRL_TLS_SEED, seedlen, (void *)(seed))
116
117 # define EVP_PKEY_CTX_set_hkdf_md(pctx, md) \
118             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
119                               EVP_PKEY_CTRL_HKDF_MD, 0, (void *)(md))
120
121 # define EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltlen) \
122             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
123                               EVP_PKEY_CTRL_HKDF_SALT, saltlen, (void *)(salt))
124
125 # define EVP_PKEY_CTX_set1_hkdf_key(pctx, key, keylen) \
126             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
127                               EVP_PKEY_CTRL_HKDF_KEY, keylen, (void *)(key))
128
129 # define EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infolen) \
130             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
131                               EVP_PKEY_CTRL_HKDF_INFO, infolen, (void *)(info))
132
133 # define EVP_PKEY_CTX_hkdf_mode(pctx, mode) \
134             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
135                               EVP_PKEY_CTRL_HKDF_MODE, mode, NULL)
136
137 # define EVP_PKEY_CTX_set1_pbe_pass(pctx, pass, passlen) \
138             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
139                             EVP_PKEY_CTRL_PASS, passlen, (void *)(pass))
140
141 # define EVP_PKEY_CTX_set1_scrypt_salt(pctx, salt, saltlen) \
142             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
143                             EVP_PKEY_CTRL_SCRYPT_SALT, saltlen, (void *)(salt))
144
145 # define EVP_PKEY_CTX_set_scrypt_N(pctx, n) \
146             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
147                             EVP_PKEY_CTRL_SCRYPT_N, n)
148
149 # define EVP_PKEY_CTX_set_scrypt_r(pctx, r) \
150             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
151                             EVP_PKEY_CTRL_SCRYPT_R, r)
152
153 # define EVP_PKEY_CTX_set_scrypt_p(pctx, p) \
154             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
155                             EVP_PKEY_CTRL_SCRYPT_P, p)
156
157 # define EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, maxmem_bytes) \
158             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
159                             EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, maxmem_bytes)
160
161
162 # ifdef __cplusplus
163 }
164 # endif
165 #endif