forked from wolfSSL/wolfProvider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp_hkdf.c
More file actions
740 lines (672 loc) · 21 KB
/
wp_hkdf.c
File metadata and controls
740 lines (672 loc) · 21 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/* wp_hkdf.c
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfProvider.
*
* wolfProvider is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfProvider is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfProvider. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/evp.h>
#include <openssl/kdf.h>
#include <openssl/err.h>
#include <wolfprovider/alg_funcs.h>
#include <wolfprovider/internal.h>
/** Base set of parameters settable against context. */
#define WP_HKDF_BASE_SETTABLES \
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0), \
OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, NULL), \
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), \
OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), \
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0), \
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0)
/** Max size of the info data to HKDF. */
#define WP_MAX_INFO_SIZE 1024
/**
* The HKDF context structure.
* Includes everything for TLS 1.3 HKDF.
*/
typedef struct wp_HkdfCtx {
/** wolfSSL provider context. */
WOLFPROV_CTX* provCtx;
/** Mode to use with HKDF. */
int mode;
/** Digest to use with HKDF. */
enum wc_HashType mdType;
/** Digest to use with HKDF. */
size_t mdLen;
/** Key for KDF. */
unsigned char *key;
/** Size of key in bytes. */
size_t keySz;
/** Salt for KDF. */
unsigned char *salt;
/** Size of salt in bytes. */
size_t saltSz;
/** Info for KDF. */
unsigned char info[WP_MAX_INFO_SIZE];
/** Size of info in bytes. */
size_t infoSz;
/** Prefix for TLS 1.3 HKDF. */
unsigned char* prefix;
/** Size of prefix in bytes. */
size_t prefixLen;
/** Label for TLS 1.3 HKDF. */
unsigned char* label;
/** Size of label in bytes. */
size_t labelLen;
/** Data for TLS 1.3 HKDF. */
unsigned char* data;
/** Size of data in bytes. */
size_t dataLen;
} wp_HkdfCtx;
/** Prototyped for the derive function. */
static int wp_kdf_hkdf_set_ctx_params(wp_HkdfCtx* ctx,
const OSSL_PARAM params[]);
/**
* Create a new HKDF context object.
*
* @param [in] provCtx wolfProvider context object.
* @return NULL on failure.
* @return HKDF context object.
*/
static wp_HkdfCtx* wp_kdf_hkdf_new(WOLFPROV_CTX* provCtx)
{
wp_HkdfCtx* ctx = NULL;
if (wolfssl_prov_is_running()) {
ctx = OPENSSL_zalloc(sizeof(*ctx));
}
if (ctx != NULL) {
ctx->provCtx = provCtx;
}
return ctx;
}
/**
* Dispose of data in HKDF context object.
*
* @param [in, out] ctx HKDF context object.
*/
static void wp_kdf_hkdf_clear(wp_HkdfCtx* ctx)
{
if (ctx->key != NULL) {
OPENSSL_clear_free(ctx->key, ctx->keySz);
}
if (ctx->data != NULL) {
OPENSSL_clear_free(ctx->data, ctx->dataLen);
}
OPENSSL_free(ctx->label);
OPENSSL_free(ctx->prefix);
OPENSSL_free(ctx->salt);
OPENSSL_cleanse(ctx->info, ctx->infoSz);
}
/**
* Dispose of an HKDF context object.
*
* @param [in, out] ctx HKDF context object.
*/
static void wp_kdf_hkdf_free(wp_HkdfCtx* ctx)
{
if (ctx != NULL) {
wp_kdf_hkdf_clear(ctx);
OPENSSL_free(ctx);
}
}
/**
* Reset HKDF context object.
*
* Disposes of allocated data.
*
* @param [in, out] ctx HKDF context object.
*/
static void wp_kdf_hkdf_reset(wp_HkdfCtx* ctx)
{
if (ctx != NULL) {
WOLFPROV_CTX* provCtx = ctx->provCtx;
wp_kdf_hkdf_clear(ctx);
XMEMSET(ctx, 0, sizeof(*ctx));
ctx->provCtx = provCtx;
}
}
/**
* Derive a key using HKDF.
*
* @param [in, out] ctx HKDF context object.
* @param [out] key Buffer to hold derived key.
* @param [in] keyLen Size of buffer in bytes.
* @param [in] params Array of parameters to set before deriving.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_kdf_hkdf_derive(wp_HkdfCtx* ctx, unsigned char* key,
size_t keyLen, const OSSL_PARAM params[])
{
int ok = 1;
if (!wolfssl_prov_is_running()) {
ok = 0;
}
if (ok && (!wp_kdf_hkdf_set_ctx_params(ctx, params))) {
ok = 0;
}
if (ok && (ctx->key == NULL)) {
ok = 0;
}
if (ok && (keyLen == 0)) {
ok = 0;
}
if (ok) {
int rc;
switch (ctx->mode) {
case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:
if (keyLen != ctx->mdLen) {
ok = 0;
}
if (ok) {
PRIVATE_KEY_UNLOCK();
rc = wc_HKDF_Extract(ctx->mdType, ctx->salt,
(word32)ctx->saltSz, ctx->key, (word32)ctx->keySz, key);
PRIVATE_KEY_LOCK();
if (rc != 0) {
ok = 0;
}
}
break;
case EVP_KDF_HKDF_MODE_EXPAND_ONLY:
PRIVATE_KEY_UNLOCK();
rc = wc_HKDF_Expand(ctx->mdType, ctx->key, (word32)ctx->keySz,
ctx->info, (word32)ctx->infoSz, key, (word32)keyLen);
PRIVATE_KEY_LOCK();
if (rc != 0) {
ok = 0;
}
break;
case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND:
default:
PRIVATE_KEY_UNLOCK();
rc = wc_HKDF(ctx->mdType, ctx->key, (word32)ctx->keySz, ctx->salt,
(word32)ctx->saltSz, ctx->info, (word32)ctx->infoSz, key,
(word32)keyLen);
PRIVATE_KEY_LOCK();
if (rc != 0) {
ok = 0;
}
break;
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Find and set the HKDF mode into the context.
*
* @param [in] params Array of parameters.
* @param [in, out] ctx HKDF context object.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_hkdf_base_get_mode(const OSSL_PARAM params[], int* mode)
{
int ok = 1;
const OSSL_PARAM* p;
p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE);
if (p != NULL) {
int n;
if (p->data_type == OSSL_PARAM_UTF8_STRING) {
if (strcasecmp(p->data, "EXTRACT_AND_EXPAND") == 0) {
*mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND;
}
else if (strcasecmp(p->data, "EXTRACT_ONLY") == 0) {
*mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;
}
else if (strcasecmp(p->data, "EXPAND_ONLY") == 0) {
*mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;
}
else {
ok = 0;
}
}
else if (OSSL_PARAM_get_int(p, &n)) {
*mode = n;
if ((n != EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND) &&
(n != EVP_KDF_HKDF_MODE_EXTRACT_ONLY) &&
(n != EVP_KDF_HKDF_MODE_EXPAND_ONLY)) {
ok = 0;
}
}
else {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the base HKDF context parameters.
*
* @param [in, out] ctx HKDF context object.
* @param [in] params Array of parameters.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_hkdf_base_set_ctx_params(wp_HkdfCtx* ctx,
const OSSL_PARAM params[])
{
int ok = 1;
OSSL_PARAM *p;
if (params != NULL) {
if (!wp_params_get_digest(params, NULL, ctx->provCtx->libCtx,
&ctx->mdType, &ctx->mdLen)) {
ok = 0;
}
if (ok && (!wp_hkdf_base_get_mode(params, &ctx->mode))) {
ok = 0;
}
if (ok) {
p = OSSL_PARAM_locate((OSSL_PARAM *)params, OSSL_KDF_PARAM_KEY);
if ((p != NULL) && (p->data != NULL)) {
OPENSSL_clear_free(ctx->key, ctx->keySz);
ctx->key = NULL;
if (!OSSL_PARAM_get_octet_string(
p, (void**)&ctx->key, 0, &ctx->keySz)) {
ok = 0;
}
}
}
if (ok) {
p = OSSL_PARAM_locate((OSSL_PARAM *)params, OSSL_KDF_PARAM_SALT);
if ((p != NULL) && (p->data != NULL)) {
OPENSSL_free(ctx->salt);
ctx->salt = NULL;
if (!OSSL_PARAM_get_octet_string(
p, (void**)&ctx->salt, 0, &ctx->saltSz)) {
ok = 0;
}
}
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Retrieve parameter values from the HKDF context object.
*
* @param [in] ctx HKDF context object.
* @param [in, out] params Array of parameters.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_kdf_hkdf_get_ctx_params(wp_HkdfCtx* ctx, OSSL_PARAM params[])
{
int ok = 1;
OSSL_PARAM* p;
p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
if (p != NULL) {
size_t sz;
if (ctx->mode == EVP_KDF_HKDF_MODE_EXTRACT_ONLY) {
sz = ctx->mdLen;
}
else {
sz = MAX_SIZE_T;
}
if (ok && !OSSL_PARAM_set_size_t(p, sz)) {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Find and set the combined info parameters into the context.
*
* @param [in, out] ctx HKDF context object.
* @param [in] params Array of parameters.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_hkdf_base_set_info(wp_HkdfCtx* ctx, const OSSL_PARAM params[])
{
int ok = 1;
const OSSL_PARAM* p;
unsigned char* q = ctx->info;
ctx->infoSz = 0;
/* Combine all the data in the info parameters. */
while (ok && ((p = OSSL_PARAM_locate_const(params,
OSSL_KDF_PARAM_INFO)) != NULL)) {
size_t sz = 0;
if ((p->data_size != 0) && (p->data != NULL) &&
(!OSSL_PARAM_get_octet_string(p, (void**)&q,
WP_MAX_INFO_SIZE - ctx->infoSz, &sz))) {
ok = 0;
}
if (ok) {
ctx->infoSz += sz;
q += sz;
}
params = p + 1;
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set parameters into HKDF context object.
*
* @param [in, out] ctx HKDF context object.
* @param [in] params Array of parameters with values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_kdf_hkdf_set_ctx_params(wp_HkdfCtx* ctx,
const OSSL_PARAM params[])
{
int ok = 1;
if (params != NULL) {
if (!wp_hkdf_base_set_ctx_params(ctx, params)) {
ok = 0;
}
if (ok && (!wp_hkdf_base_set_info(ctx, params))) {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Returns the parameters to set against an HKDF context for HKDF ops.
*
* @param [in] ctx HKDF context object. Unused.
* @param [in] provCtx wolfProvider context. Unused.
* @return Array of parameters.
*/
static const OSSL_PARAM* wp_kdf_hkdf_settable_ctx_params(wp_HkdfCtx* ctx,
WOLFPROV_CTX* provCtx)
{
/** Parameters to set against an HKDF context for HKDF ops. */
static const OSSL_PARAM wp_hkdf_supported_settable_ctx_params[] = {
WP_HKDF_BASE_SETTABLES,
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
OSSL_PARAM_END
};
(void)ctx;
(void)provCtx;
return wp_hkdf_supported_settable_ctx_params;
}
/**
* Return parameters that can be retrieved from the HKDF context.
*
* @param [in] ctx HKDF context object. Unused.
* @param [in] provCtx wolfProvider context. Unused.
* @return Array of parameters.
*/
static const OSSL_PARAM* wp_kdf_hkdf_gettable_ctx_params(wp_HkdfCtx* ctx,
WOLFPROV_CTX* provCtx)
{
/**
* Parameters that can be retrieved from the HKDF context.
*/
static const OSSL_PARAM wp_kdf_hkdf_supported_gettable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
OSSL_PARAM_END
};
(void)ctx;
(void)provCtx;
return wp_kdf_hkdf_supported_gettable_ctx_params;
}
/** Dispatch table for HKDF functions implemented using wolfSSL. */
const OSSL_DISPATCH wp_kdf_hkdf_functions[] = {
{ OSSL_FUNC_KDF_NEWCTX, (DFUNC)wp_kdf_hkdf_new },
{ OSSL_FUNC_KDF_FREECTX, (DFUNC)wp_kdf_hkdf_free },
{ OSSL_FUNC_KDF_RESET, (DFUNC)wp_kdf_hkdf_reset },
{ OSSL_FUNC_KDF_DERIVE, (DFUNC)wp_kdf_hkdf_derive },
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
(DFUNC)wp_kdf_hkdf_settable_ctx_params },
{ OSSL_FUNC_KDF_SET_CTX_PARAMS, (DFUNC)wp_kdf_hkdf_set_ctx_params },
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
(DFUNC)wp_kdf_hkdf_gettable_ctx_params },
{ OSSL_FUNC_KDF_GET_CTX_PARAMS, (DFUNC)wp_kdf_hkdf_get_ctx_params },
{ 0, NULL }
};
/*
* TLS 1.3 KDF
*/
/* Protyped for derivation function. */
static int wp_kdf_tls1_3_set_ctx_params(wp_HkdfCtx* ctx,
const OSSL_PARAM params[]);
/**
* TLS 1.3 HKDF expansion.
*
* This function is called by wp_tls13_hkdf_extract().
*
* Label and prefix from the context are used in calculation.
* info field updated with data to expand.
*
* @param [in, out] ctx HKDF context object.
* @param [in] inKey Input key.
* @param [in] inKeyLen Length of input key in bytes.
* @param [in] data Data to be expanded.
* @param [in] inKeyLen Length of data in bytes.
* @param [out] key Buffer to hold output key
* @param [in] keyLen Size of buffer in bytes.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_tls13_hkdf_expand(wp_HkdfCtx* ctx, unsigned char* inKey,
size_t inKeyLen, unsigned char* data, size_t dataLen, unsigned char* key,
size_t keyLen)
{
int ok = 1;
size_t idx = 0;
int rc;
/* Construct info to expand from:
* - output key length
* - label
* - prefix/protocol
* - data
*/
ctx->info[idx++] = (byte)(keyLen >> 8);
ctx->info[idx++] = (byte)keyLen;
ctx->info[idx++] = (byte)(ctx->prefixLen + ctx->labelLen);
XMEMCPY(ctx->info + idx, ctx->prefix, ctx->prefixLen);
idx += ctx->prefixLen;
XMEMCPY(ctx->info + idx, ctx->label, ctx->labelLen);
idx += ctx->labelLen;
ctx->info[idx++] = (byte)(dataLen);
if (dataLen > 0) {
XMEMCPY(ctx->info + idx, data, dataLen);
idx += dataLen;
}
ctx->infoSz = idx;
PRIVATE_KEY_UNLOCK();
rc = wc_HKDF_Expand(ctx->mdType, inKey, (word32)inKeyLen, ctx->info,
(word32)ctx->infoSz, key, (word32)keyLen);
PRIVATE_KEY_LOCK();
if (rc != 0) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* TLS 1.3 HKDF extract operation.
*
* @param [in, out] ctx HKDF context object.
* @param [out] key Buffer to hold output key
* @param [in] keyLen Size of buffer in bytes.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_tls13_hkdf_extract(wp_HkdfCtx* ctx, unsigned char* key,
size_t keyLen)
{
int ok = 1;
int rc;
unsigned char secret[WC_MAX_DIGEST_SIZE];
unsigned char zeros[WC_MAX_DIGEST_SIZE];
unsigned char* inKey;
size_t inKeyLen;
unsigned char* salt;
size_t saltLen;
if (ctx->key == NULL) {
inKey = zeros;
inKeyLen = ctx->mdLen;
XMEMSET(zeros, 0, inKeyLen);
}
else {
inKey = ctx->key;
inKeyLen = ctx->keySz;
}
if (ctx->salt == NULL) {
salt = zeros;
saltLen = 0;
}
else {
salt = secret;
saltLen = ctx->mdLen;
/* Calculate the digest of an empty string. */
rc = wc_Hash(ctx->mdType, zeros, 0, secret, (word32)ctx->mdLen);
if (rc != 0) {
ok = 0;
}
else if (!wp_tls13_hkdf_expand(ctx, ctx->salt, ctx->saltSz, secret,
ctx->mdLen, salt, saltLen)) {
ok = 0;
}
}
if (ok) {
(void)keyLen;
PRIVATE_KEY_UNLOCK();
if (saltLen == 0) {
rc = wc_HKDF_Extract(ctx->mdType, NULL, 0, inKey,
(word32)inKeyLen, key);
}
else {
rc = wc_HKDF_Extract(ctx->mdType, salt, (word32)saltLen, inKey,
(word32)inKeyLen, key);
}
PRIVATE_KEY_LOCK();
if (rc != 0) {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Derive a key using TLS 1.3 HKDF.
*
* @param [in, out] ctx HKDF context object.
* @param [out] key Buffer to hold derived key.
* @param [in] keyLen Size of buffer in bytes.
* @param [in] params Array of parameters to set before deriving.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_kdf_tls1_3_derive(wp_HkdfCtx* ctx, unsigned char* key,
size_t keyLen, const OSSL_PARAM params[])
{
int ok = 1;
if (!wolfssl_prov_is_running()) {
ok = 0;
}
if (ok && !wp_kdf_tls1_3_set_ctx_params(ctx, params)) {
ok = 0;
}
if (ok) {
if (ctx->mode == EVP_KDF_HKDF_MODE_EXTRACT_ONLY) {
if (!wp_tls13_hkdf_extract(ctx, key, keyLen)) {
ok = 0;
}
}
else if (ctx->mode == EVP_KDF_HKDF_MODE_EXPAND_ONLY) {
if (!wp_tls13_hkdf_expand(ctx, ctx->key, ctx->keySz, ctx->data,
ctx->dataLen, key, keyLen)) {
ok = 0;
}
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set parameters into HKDF context object for TLS 1.3 HKDF.
*
* @param [in, out] ctx HKDF context object.
* @param [in] params Array of parameters with values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_kdf_tls1_3_set_ctx_params(wp_HkdfCtx* ctx,
const OSSL_PARAM params[])
{
int ok = 1;
if (params != NULL) {
if (!wp_hkdf_base_set_ctx_params(ctx, params)) {
ok = 0;
}
if (ok && (ctx->mode == EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND)) {
ok = 0;
}
if (ok && (!wp_params_get_octet_string(params, OSSL_KDF_PARAM_PREFIX,
&ctx->prefix, &ctx->prefixLen, 0))) {
ok = 0;
}
if (ok && (!wp_params_get_octet_string(params, OSSL_KDF_PARAM_LABEL,
&ctx->label, &ctx->labelLen, 0))) {
ok = 0;
}
if (ok && (!wp_params_get_octet_string(params, OSSL_KDF_PARAM_DATA,
&ctx->data, &ctx->dataLen, 0))) {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_KDF, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Returns the parameters to set against an HKDF context for TLS 1.3 HKDF ops.
*
* @param [in] ctx HKDF context object. Unused.
* @param [in] provCtx wolfProvider context. Unused.
* @return Array of parameters.
*/
static const OSSL_PARAM* wp_kdf_tls1_3_settable_ctx_params(wp_HkdfCtx* ctx,
WOLFPROV_CTX* provCtx)
{
/** Parameters to set against an HKDF context for TLS 1.3 HKDF ops. */
static const OSSL_PARAM wp_kdf_tls1_3_supported_settable_ctx_params[] = {
WP_HKDF_BASE_SETTABLES,
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PREFIX, NULL, 0),
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_LABEL, NULL, 0),
OSSL_PARAM_octet_string(OSSL_KDF_PARAM_DATA, NULL, 0),
OSSL_PARAM_END
};
(void)ctx;
(void)provCtx;
return wp_kdf_tls1_3_supported_settable_ctx_params;
}
/** Dispatch table for TLS 1.3 HKDF functions implemented using wolfSSL. */
const OSSL_DISPATCH wp_kdf_tls1_3_kdf_functions[] = {
{ OSSL_FUNC_KDF_NEWCTX, (DFUNC)wp_kdf_hkdf_new },
{ OSSL_FUNC_KDF_FREECTX, (DFUNC)wp_kdf_hkdf_free },
{ OSSL_FUNC_KDF_RESET, (DFUNC)wp_kdf_hkdf_reset },
{ OSSL_FUNC_KDF_DERIVE, (DFUNC)wp_kdf_tls1_3_derive },
{ OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
(DFUNC)wp_kdf_tls1_3_settable_ctx_params },
{ OSSL_FUNC_KDF_SET_CTX_PARAMS , (DFUNC)wp_kdf_tls1_3_set_ctx_params },
{ OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
(DFUNC)wp_kdf_hkdf_gettable_ctx_params },
{ OSSL_FUNC_KDF_GET_CTX_PARAMS , (DFUNC)wp_kdf_hkdf_get_ctx_params },
{ 0, NULL }
};