PBKDF2 updates to conform to SP800-132
[openssl.git] / include / openssl / kdf.h
1 /*
2  * Copyright 2016-2019 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_PBKDF2_PKCS5_MODE  0x16 /* int */
69 # define EVP_KDF_CTRL_SET_SHARED_INFO        EVP_KDF_CTRL_SET_SSKDF_INFO
70
71 # define EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND  0
72 # define EVP_KDF_HKDF_MODE_EXTRACT_ONLY        1
73 # define EVP_KDF_HKDF_MODE_EXPAND_ONLY         2
74
75 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV 65
76 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_SRV_TO_CLI 66
77 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_CLI_TO_SRV 67
78 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_SRV_TO_CLI 68
79 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_CLI_TO_SRV 69
80 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_SRV_TO_CLI 70
81
82 /**** The legacy PKEY-based KDF API follows. ****/
83
84 # define EVP_PKEY_CTRL_TLS_MD                   (EVP_PKEY_ALG_CTRL)
85 # define EVP_PKEY_CTRL_TLS_SECRET               (EVP_PKEY_ALG_CTRL + 1)
86 # define EVP_PKEY_CTRL_TLS_SEED                 (EVP_PKEY_ALG_CTRL + 2)
87 # define EVP_PKEY_CTRL_HKDF_MD                  (EVP_PKEY_ALG_CTRL + 3)
88 # define EVP_PKEY_CTRL_HKDF_SALT                (EVP_PKEY_ALG_CTRL + 4)
89 # define EVP_PKEY_CTRL_HKDF_KEY                 (EVP_PKEY_ALG_CTRL + 5)
90 # define EVP_PKEY_CTRL_HKDF_INFO                (EVP_PKEY_ALG_CTRL + 6)
91 # define EVP_PKEY_CTRL_HKDF_MODE                (EVP_PKEY_ALG_CTRL + 7)
92 # define EVP_PKEY_CTRL_PASS                     (EVP_PKEY_ALG_CTRL + 8)
93 # define EVP_PKEY_CTRL_SCRYPT_SALT              (EVP_PKEY_ALG_CTRL + 9)
94 # define EVP_PKEY_CTRL_SCRYPT_N                 (EVP_PKEY_ALG_CTRL + 10)
95 # define EVP_PKEY_CTRL_SCRYPT_R                 (EVP_PKEY_ALG_CTRL + 11)
96 # define EVP_PKEY_CTRL_SCRYPT_P                 (EVP_PKEY_ALG_CTRL + 12)
97 # define EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES      (EVP_PKEY_ALG_CTRL + 13)
98
99 # define EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND \
100             EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND
101 # define EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY       \
102             EVP_KDF_HKDF_MODE_EXTRACT_ONLY
103 # define EVP_PKEY_HKDEF_MODE_EXPAND_ONLY        \
104             EVP_KDF_HKDF_MODE_EXPAND_ONLY
105
106 # define EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) \
107             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
108                               EVP_PKEY_CTRL_TLS_MD, 0, (void *)(md))
109
110 # define EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, seclen) \
111             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
112                               EVP_PKEY_CTRL_TLS_SECRET, seclen, (void *)(sec))
113
114 # define EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seedlen) \
115             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
116                               EVP_PKEY_CTRL_TLS_SEED, seedlen, (void *)(seed))
117
118 # define EVP_PKEY_CTX_set_hkdf_md(pctx, md) \
119             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
120                               EVP_PKEY_CTRL_HKDF_MD, 0, (void *)(md))
121
122 # define EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltlen) \
123             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
124                               EVP_PKEY_CTRL_HKDF_SALT, saltlen, (void *)(salt))
125
126 # define EVP_PKEY_CTX_set1_hkdf_key(pctx, key, keylen) \
127             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
128                               EVP_PKEY_CTRL_HKDF_KEY, keylen, (void *)(key))
129
130 # define EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infolen) \
131             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
132                               EVP_PKEY_CTRL_HKDF_INFO, infolen, (void *)(info))
133
134 # define EVP_PKEY_CTX_hkdf_mode(pctx, mode) \
135             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
136                               EVP_PKEY_CTRL_HKDF_MODE, mode, NULL)
137
138 # define EVP_PKEY_CTX_set1_pbe_pass(pctx, pass, passlen) \
139             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
140                             EVP_PKEY_CTRL_PASS, passlen, (void *)(pass))
141
142 # define EVP_PKEY_CTX_set1_scrypt_salt(pctx, salt, saltlen) \
143             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
144                             EVP_PKEY_CTRL_SCRYPT_SALT, saltlen, (void *)(salt))
145
146 # define EVP_PKEY_CTX_set_scrypt_N(pctx, n) \
147             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
148                             EVP_PKEY_CTRL_SCRYPT_N, n)
149
150 # define EVP_PKEY_CTX_set_scrypt_r(pctx, r) \
151             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
152                             EVP_PKEY_CTRL_SCRYPT_R, r)
153
154 # define EVP_PKEY_CTX_set_scrypt_p(pctx, p) \
155             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
156                             EVP_PKEY_CTRL_SCRYPT_P, p)
157
158 # define EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, maxmem_bytes) \
159             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
160                             EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, maxmem_bytes)
161
162
163 # ifdef __cplusplus
164 }
165 # endif
166 #endif