1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/*
* Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <string.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/buffer.h>
#include <openssl/kdf.h>
#include <openssl/core.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include "internal/numbers.h"
#include "crypto/evp.h"
#define MAX_PARAM 20
typedef struct {
EVP_KDF_CTX *kctx;
/*
* EVP_PKEY implementations collect bits of certain data
*/
BUF_MEM *collected_seed;
BUF_MEM *collected_info;
} EVP_PKEY_KDF_CTX;
static void pkey_kdf_free_collected(EVP_PKEY_KDF_CTX *pkctx)
{
BUF_MEM_free(pkctx->collected_seed);
pkctx->collected_seed = NULL;
BUF_MEM_free(pkctx->collected_info);
pkctx->collected_info = NULL;
}
static int pkey_kdf_init(EVP_PKEY_CTX *ctx)
{
EVP_PKEY_KDF_CTX *pkctx;
EVP_KDF_CTX *kctx;
const char *kdf_name = OBJ_nid2sn(ctx->pmeth->pkey_id);
EVP_KDF *kdf;
pkctx = OPENSSL_zalloc(sizeof(*pkctx));
if (pkctx == NULL)
return 0;
kdf = EVP_KDF_fetch(NULL, kdf_name, NULL);
kctx = EVP_KDF_new_ctx(kdf);
EVP_KDF_free(kdf);
if (kctx == NULL) {
OPENSSL_free(pkctx);
return 0;
}
pkctx->kctx = kctx;
ctx->data = pkctx;
return 1;
}
static void pkey_kdf_cleanup(EVP_PKEY_CTX *ctx)
{
EVP_PKEY_KDF_CTX *pkctx = ctx->data;
EVP_KDF_free_ctx(pkctx->kctx);
pkey_kdf_free_collected(pkctx);
OPENSSL_free(pkctx);
}
static int collect(BUF_MEM **collector, void *data, size_t datalen)
{
size_t i;
if (*collector == NULL)
*collector = BUF_MEM_new();
if (*collector == NULL) {
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
return 0;
}
if (data != NULL && datalen > 0) {
i = (*collector)->length; /* BUF_MEM_grow() changes it! */
if (!BUF_MEM_grow(*collector, i + datalen))
return 0;
memcpy((*collector)->data + i, data, datalen);
}
return 1;
}
static int pkey_kdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
{
EVP_PKEY_KDF_CTX *pkctx = ctx->data;
EVP_KDF_CTX *kctx = pkctx->kctx;
enum { T_OCTET_STRING, T_UINT64, T_DIGEST, T_INT } cmd;
const char *name, *mdname;
BUF_MEM **collector = NULL;
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
switch (type) {
case EVP_PKEY_CTRL_PASS:
cmd = T_OCTET_STRING;
name = OSSL_KDF_PARAM_PASSWORD;
break;
case EVP_PKEY_CTRL_HKDF_SALT:
case EVP_PKEY_CTRL_SCRYPT_SALT:
cmd = T_OCTET_STRING;
name = OSSL_KDF_PARAM_SALT;
break;
case EVP_PKEY_CTRL_TLS_MD:
case EVP_PKEY_CTRL_HKDF_MD:
cmd = T_DIGEST;
name = OSSL_KDF_PARAM_DIGEST;
break;
case EVP_PKEY_CTRL_TLS_SECRET:
cmd = T_OCTET_STRING;
name = OSSL_KDF_PARAM_SECRET;
/*
* Perform the semantics described in
* EVP_PKEY_CTX_add1_tls1_prf_seed(3)
*/
if (ctx->pmeth->pkey_id == NID_tls1_prf) {
BUF_MEM_free(pkctx->collected_seed);
pkctx->collected_seed = NULL;
}
break;
case EVP_PKEY_CTRL_TLS_SEED:
cmd = T_OCTET_STRING;
name = OSSL_KDF_PARAM_SEED;
collector = &pkctx->collected_seed;
break;
case EVP_PKEY_CTRL_HKDF_KEY:
cmd = T_OCTET_STRING;
name = OSSL_KDF_PARAM_KEY;
break;
case EVP_PKEY_CTRL_HKDF_INFO:
cmd = T_OCTET_STRING;
name = OSSL_KDF_PARAM_INFO;
collector = &pkctx->collected_info;
break;
case EVP_PKEY_CTRL_HKDF_MODE:
cmd = T_INT;
name = OSSL_KDF_PARAM_MODE;
break;
case EVP_PKEY_CTRL_SCRYPT_N:
cmd = T_UINT64;
name = OSSL_KDF_PARAM_SCRYPT_N;
break;
case EVP_PKEY_CTRL_SCRYPT_R:
cmd = T_UINT64; /* Range checking occurs on the provider side */
name = OSSL_KDF_PARAM_SCRYPT_R;
break;
case EVP_PKEY_CTRL_SCRYPT_P:
cmd = T_UINT64; /* Range checking occurs on the provider side */
name = OSSL_KDF_PARAM_SCRYPT_P;
break;
case EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES:
cmd = T_UINT64;
name = OSSL_KDF_PARAM_SCRYPT_MAXMEM;
break;
default:
return -2;
}
if (collector != NULL) {
switch (cmd) {
case T_OCTET_STRING:
return collect(collector, p2, p1);
default:
OPENSSL_assert("You shouldn't be here");
break;
}
return 1;
}
switch (cmd) {
case T_OCTET_STRING:
params[0] =
OSSL_PARAM_construct_octet_string(name, (unsigned char *)p2,
(size_t)p1);
break;
case T_DIGEST:
mdname = EVP_MD_name((const EVP_MD *)p2);
params[0] = OSSL_PARAM_construct_utf8_string(name, (char *)mdname, 0);
break;
/*
* These are special because the helper macros pass a pointer to the
* stack, so a local copy is required.
*/
case T_INT:
params[0] = OSSL_PARAM_construct_int(name, &p1);
break;
case T_UINT64:
params[0] = OSSL_PARAM_construct_uint64(name, (uint64_t *)p2);
break;
}
return EVP_KDF_set_ctx_params(kctx, params);
}
static int pkey_kdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
const char *value)
{
EVP_PKEY_KDF_CTX *pkctx = ctx->data;
EVP_KDF_CTX *kctx = pkctx->kctx;
const EVP_KDF *kdf = EVP_KDF_get_ctx_kdf(kctx);
BUF_MEM **collector = NULL;
const OSSL_PARAM *defs = EVP_KDF_settable_ctx_params(kdf);
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
int ok = 0;
/* Deal with ctrl name aliasing */
if (strcmp(type, "md") == 0)
type = OSSL_KDF_PARAM_DIGEST;
/* scrypt uses 'N', params uses 'n' */
if (strcmp(type, "N") == 0)
type = OSSL_KDF_PARAM_SCRYPT_N;
if (!OSSL_PARAM_allocate_from_text(¶ms[0], defs, type,
value, strlen(value), NULL))
return 0;
/*
* We do the same special casing of seed and info here as in
* pkey_kdf_ctrl()
*/
if (strcmp(params[0].key, OSSL_KDF_PARAM_SEED) == 0)
collector = &pkctx->collected_seed;
else if (strcmp(params[0].key, OSSL_KDF_PARAM_INFO) == 0)
collector = &pkctx->collected_info;
if (collector != NULL)
ok = collect(collector, params[0].data, params[0].data_size);
else
ok = EVP_KDF_set_ctx_params(kctx, params);
OPENSSL_free(params[0].data);
return ok;
}
static int pkey_kdf_derive_init(EVP_PKEY_CTX *ctx)
{
EVP_PKEY_KDF_CTX *pkctx = ctx->data;
pkey_kdf_free_collected(pkctx);
if (pkctx->kctx != NULL)
EVP_KDF_reset(pkctx->kctx);
return 1;
}
/*
* For fixed-output algorithms the keylen parameter is an "out" parameter
* otherwise it is an "in" parameter.
*/
static int pkey_kdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
size_t *keylen)
{
EVP_PKEY_KDF_CTX *pkctx = ctx->data;
EVP_KDF_CTX *kctx = pkctx->kctx;
size_t outlen = EVP_KDF_size(kctx);
int r;
if (pkctx->collected_seed != NULL) {
OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
params[0] =
OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
pkctx->collected_seed->data,
pkctx->collected_seed->length);
r = EVP_KDF_set_ctx_params(kctx, params);
pkey_kdf_free_collected(pkctx);
if (!r)
return 0;
}
if (pkctx->collected_info != NULL) {
OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
params[0] =
OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
pkctx->collected_info->data,
pkctx->collected_info->length);
r = EVP_KDF_set_ctx_params(kctx, params);
pkey_kdf_free_collected(pkctx);
if (!r)
return 0;
}
if (outlen == 0 || outlen == SIZE_MAX) {
/* Variable-output algorithm */
if (key == NULL)
return 0;
} else {
/* Fixed-output algorithm */
*keylen = outlen;
if (key == NULL)
return 1;
}
return EVP_KDF_derive(kctx, key, *keylen);
}
#ifndef OPENSSL_NO_SCRYPT
static const EVP_PKEY_METHOD scrypt_pkey_meth = {
EVP_PKEY_SCRYPT,
0,
pkey_kdf_init,
0,
pkey_kdf_cleanup,
0, 0,
0, 0,
0,
0,
0,
0,
0, 0,
0, 0, 0, 0,
0, 0,
0, 0,
pkey_kdf_derive_init,
pkey_kdf_derive,
pkey_kdf_ctrl,
pkey_kdf_ctrl_str
};
const EVP_PKEY_METHOD *scrypt_pkey_method(void)
{
return &scrypt_pkey_meth;
}
#endif
static const EVP_PKEY_METHOD tls1_prf_pkey_meth = {
EVP_PKEY_TLS1_PRF,
0,
pkey_kdf_init,
0,
pkey_kdf_cleanup,
0, 0,
0, 0,
0,
0,
0,
0,
0, 0,
0, 0, 0, 0,
0, 0,
0, 0,
pkey_kdf_derive_init,
pkey_kdf_derive,
pkey_kdf_ctrl,
pkey_kdf_ctrl_str
};
const EVP_PKEY_METHOD *tls1_prf_pkey_method(void)
{
return &tls1_prf_pkey_meth;
}
static const EVP_PKEY_METHOD hkdf_pkey_meth = {
EVP_PKEY_HKDF,
0,
pkey_kdf_init,
0,
pkey_kdf_cleanup,
0, 0,
0, 0,
0,
0,
0,
0,
0, 0,
0, 0, 0, 0,
0, 0,
0, 0,
pkey_kdf_derive_init,
pkey_kdf_derive,
pkey_kdf_ctrl,
pkey_kdf_ctrl_str
};
const EVP_PKEY_METHOD *hkdf_pkey_method(void)
{
return &hkdf_pkey_meth;
}
|