acd94491b3cc74c2b084ab7117648a79347a96d5
[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
27 EVP_KDF_CTX *EVP_KDF_CTX_new_id(int id);
28 void EVP_KDF_CTX_free(EVP_KDF_CTX *ctx);
29
30 void EVP_KDF_reset(EVP_KDF_CTX *ctx);
31 int EVP_KDF_ctrl(EVP_KDF_CTX *ctx, int cmd, ...);
32 int EVP_KDF_vctrl(EVP_KDF_CTX *ctx, int cmd, va_list args);
33 int EVP_KDF_ctrl_str(EVP_KDF_CTX *ctx, const char *type, const char *value);
34 size_t EVP_KDF_size(EVP_KDF_CTX *ctx);
35 int EVP_KDF_derive(EVP_KDF_CTX *ctx, unsigned char *key, size_t keylen);
36
37
38 # define EVP_KDF_CTRL_SET_PASS          0x01 /* unsigned char *, size_t */
39 # define EVP_KDF_CTRL_SET_SALT          0x02 /* unsigned char *, size_t */
40 # define EVP_KDF_CTRL_SET_ITER          0x03 /* int */
41 # define EVP_KDF_CTRL_SET_MD            0x04 /* EVP_MD * */
42 # define EVP_KDF_CTRL_SET_KEY           0x05 /* unsigned char *, size_t */
43 # define EVP_KDF_CTRL_SET_MAXMEM_BYTES  0x06 /* uint64_t */
44 # define EVP_KDF_CTRL_SET_TLS_SECRET    0x07 /* unsigned char *, size_t */
45 # define EVP_KDF_CTRL_RESET_TLS_SEED    0x08
46 # define EVP_KDF_CTRL_ADD_TLS_SEED      0x09 /* unsigned char *, size_t */
47 # define EVP_KDF_CTRL_RESET_HKDF_INFO   0x0a
48 # define EVP_KDF_CTRL_ADD_HKDF_INFO     0x0b /* unsigned char *, size_t */
49 # define EVP_KDF_CTRL_SET_HKDF_MODE     0x0c /* int */
50 # define EVP_KDF_CTRL_SET_SCRYPT_N      0x0d /* uint64_t */
51 # define EVP_KDF_CTRL_SET_SCRYPT_R      0x0e /* uint32_t */
52 # define EVP_KDF_CTRL_SET_SCRYPT_P      0x0f /* uint32_t */
53 # define EVP_KDF_CTRL_SET_SSHKDF_XCGHASH    0x10 /* unsigned char *, size_t */
54 # define EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID 0x11 /* unsigned char *, size_t */
55 # define EVP_KDF_CTRL_SET_SSHKDF_TYPE       0x12 /* int */
56
57 # define EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND  0
58 # define EVP_KDF_HKDF_MODE_EXTRACT_ONLY        1
59 # define EVP_KDF_HKDF_MODE_EXPAND_ONLY         2
60
61 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV 65
62 #define EVP_KDF_SSHKDF_TYPE_INITIAL_IV_SRV_TO_CLI 66
63 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_CLI_TO_SRV 67
64 #define EVP_KDF_SSHKDF_TYPE_ENCRYPTION_KEY_SRV_TO_CLI 68
65 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_CLI_TO_SRV 69
66 #define EVP_KDF_SSHKDF_TYPE_INTEGRITY_KEY_SRV_TO_CLI 70
67
68 /**** The legacy PKEY-based KDF API follows. ****/
69
70 # define EVP_PKEY_CTRL_TLS_MD                   (EVP_PKEY_ALG_CTRL)
71 # define EVP_PKEY_CTRL_TLS_SECRET               (EVP_PKEY_ALG_CTRL + 1)
72 # define EVP_PKEY_CTRL_TLS_SEED                 (EVP_PKEY_ALG_CTRL + 2)
73 # define EVP_PKEY_CTRL_HKDF_MD                  (EVP_PKEY_ALG_CTRL + 3)
74 # define EVP_PKEY_CTRL_HKDF_SALT                (EVP_PKEY_ALG_CTRL + 4)
75 # define EVP_PKEY_CTRL_HKDF_KEY                 (EVP_PKEY_ALG_CTRL + 5)
76 # define EVP_PKEY_CTRL_HKDF_INFO                (EVP_PKEY_ALG_CTRL + 6)
77 # define EVP_PKEY_CTRL_HKDF_MODE                (EVP_PKEY_ALG_CTRL + 7)
78 # define EVP_PKEY_CTRL_PASS                     (EVP_PKEY_ALG_CTRL + 8)
79 # define EVP_PKEY_CTRL_SCRYPT_SALT              (EVP_PKEY_ALG_CTRL + 9)
80 # define EVP_PKEY_CTRL_SCRYPT_N                 (EVP_PKEY_ALG_CTRL + 10)
81 # define EVP_PKEY_CTRL_SCRYPT_R                 (EVP_PKEY_ALG_CTRL + 11)
82 # define EVP_PKEY_CTRL_SCRYPT_P                 (EVP_PKEY_ALG_CTRL + 12)
83 # define EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES      (EVP_PKEY_ALG_CTRL + 13)
84
85 # define EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND \
86             EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND
87 # define EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY       \
88             EVP_KDF_HKDF_MODE_EXTRACT_ONLY
89 # define EVP_PKEY_HKDEF_MODE_EXPAND_ONLY        \
90             EVP_KDF_HKDF_MODE_EXPAND_ONLY
91
92 # define EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) \
93             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
94                               EVP_PKEY_CTRL_TLS_MD, 0, (void *)(md))
95
96 # define EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, seclen) \
97             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
98                               EVP_PKEY_CTRL_TLS_SECRET, seclen, (void *)(sec))
99
100 # define EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed, seedlen) \
101             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
102                               EVP_PKEY_CTRL_TLS_SEED, seedlen, (void *)(seed))
103
104 # define EVP_PKEY_CTX_set_hkdf_md(pctx, md) \
105             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
106                               EVP_PKEY_CTRL_HKDF_MD, 0, (void *)(md))
107
108 # define EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt, saltlen) \
109             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
110                               EVP_PKEY_CTRL_HKDF_SALT, saltlen, (void *)(salt))
111
112 # define EVP_PKEY_CTX_set1_hkdf_key(pctx, key, keylen) \
113             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
114                               EVP_PKEY_CTRL_HKDF_KEY, keylen, (void *)(key))
115
116 # define EVP_PKEY_CTX_add1_hkdf_info(pctx, info, infolen) \
117             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
118                               EVP_PKEY_CTRL_HKDF_INFO, infolen, (void *)(info))
119
120 # define EVP_PKEY_CTX_hkdf_mode(pctx, mode) \
121             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
122                               EVP_PKEY_CTRL_HKDF_MODE, mode, NULL)
123
124 # define EVP_PKEY_CTX_set1_pbe_pass(pctx, pass, passlen) \
125             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
126                             EVP_PKEY_CTRL_PASS, passlen, (void *)(pass))
127
128 # define EVP_PKEY_CTX_set1_scrypt_salt(pctx, salt, saltlen) \
129             EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_DERIVE, \
130                             EVP_PKEY_CTRL_SCRYPT_SALT, saltlen, (void *)(salt))
131
132 # define EVP_PKEY_CTX_set_scrypt_N(pctx, n) \
133             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
134                             EVP_PKEY_CTRL_SCRYPT_N, n)
135
136 # define EVP_PKEY_CTX_set_scrypt_r(pctx, r) \
137             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
138                             EVP_PKEY_CTRL_SCRYPT_R, r)
139
140 # define EVP_PKEY_CTX_set_scrypt_p(pctx, p) \
141             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
142                             EVP_PKEY_CTRL_SCRYPT_P, p)
143
144 # define EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, maxmem_bytes) \
145             EVP_PKEY_CTX_ctrl_uint64(pctx, -1, EVP_PKEY_OP_DERIVE, \
146                             EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, maxmem_bytes)
147
148
149 # ifdef __cplusplus
150 }
151 # endif
152 #endif