-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhpke.c
4471 lines (4325 loc) · 157 KB
/
hpke.c
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (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
*/
/* An OpenSSL-based HPKE implementation of RFC9180 */
#ifdef HAPPYKEY
#include <stddef.h>
#include <stdint.h>
#endif
#include <string.h>
#ifdef HAPPYKEY
#include <openssl/ssl.h>
#endif
#include <openssl/rand.h>
#include <openssl/kdf.h>
#include <openssl/core_names.h>
#ifdef HAPPYKEY
#include <openssl/evp.h>
#include <openssl/params.h>
#include <openssl/param_build.h>
#endif
#ifdef HAPPYKEY
#include <internal/packet.h>
#include <internal/common.h>
#endif
#ifdef HAPPYKEY
/*
* If we're building standalone (from github.com/sftcd/happykey) then
* include the local headers.
*/
# include "hpke.h"
# include "hpke_util.h"
/*
* Define this for LOADS of printing of intermediate cryptographic values
* Really only needed when new crypto added (hopefully)
*/
# define SUPERVERBOSE
#else /* For OpenSSL library */
#include <openssl/hpke.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include "internal/hpke_util.h"
#include "internal/nelem.h"
#endif
/** default buffer size for keys and internal buffers we use */
#define OSSL_HPKE_MAXSIZE 512
/* Define HPKE labels from RFC9180 in hex for EBCDIC compatibility */
/* "HPKE" - "suite_id" label for section 5.1 */
static const char OSSL_HPKE_SEC51LABEL[] = "\x48\x50\x4b\x45";
/* "psk_id_hash" - in key_schedule_context */
static const char OSSL_HPKE_PSKIDHASH_LABEL[] = "\x70\x73\x6b\x5f\x69\x64\x5f\x68\x61\x73\x68";
/* "info_hash" - in key_schedule_context */
static const char OSSL_HPKE_INFOHASH_LABEL[] = "\x69\x6e\x66\x6f\x5f\x68\x61\x73\x68";
/* "base_nonce" - base nonce calc label */
static const char OSSL_HPKE_NONCE_LABEL[] = "\x62\x61\x73\x65\x5f\x6e\x6f\x6e\x63\x65";
/* "exp" - internal exporter secret generation label */
static const char OSSL_HPKE_EXP_LABEL[] = "\x65\x78\x70";
/* "sec" - external label for exporting secret */
static const char OSSL_HPKE_EXP_SEC_LABEL[] = "\x73\x65\x63";
/* "key" - label for use when generating key from shared secret */
static const char OSSL_HPKE_KEY_LABEL[] = "\x6b\x65\x79";
/* "psk_hash" - for hashing PSK */
static const char OSSL_HPKE_PSK_HASH_LABEL[] = "\x70\x73\x6b\x5f\x68\x61\x73\x68";
/* "secret" - for generating shared secret */
static const char OSSL_HPKE_SECRET_LABEL[] = "\x73\x65\x63\x72\x65\x74";
#ifdef HAPPYKEY
/* an error macro just to make things easier */
# define ERR_raise(__a__, __b__) \
{ \
if (erv == 1) { erv = 0; } \
}
#endif
#if defined(SUPERVERBOSE)
unsigned char *pbuf; /* global var for debug printing */
size_t pblen = 1024; /* global var for debug printing */
/**
* @brief string for KEMs
*/
static const char *kem_info_str(const OSSL_HPKE_KEM_INFO *kem_info)
{
if (kem_info == NULL)
return "null";
if (kem_info->groupname != NULL)
return kem_info->groupname;
else
return kem_info->keytype;
}
/**
* @brief string for KDFs
*/
static const char *kdf_info_str(const OSSL_HPKE_KDF_INFO *kdf_info)
{
if (kdf_info == NULL)
return "null";
return kdf_info->mdname;
}
/**
* @brief string for AEADs
*/
static const char *aead_info_str(const OSSL_HPKE_AEAD_INFO *aead_info)
{
if (aead_info == NULL)
return "null";
return aead_info->name;
}
/*
* @brief table of mode strings
*/
static const char *hpke_mode_strtab[] = {
OSSL_HPKE_MODESTR_BASE,
OSSL_HPKE_MODESTR_PSK,
OSSL_HPKE_MODESTR_AUTH,
OSSL_HPKE_MODESTR_PSKAUTH};
#endif
#ifdef HAPPYKEY
/*
* @brief Map ascii to binary - utility macro used in >1 place
*/
# define HPKE_A2B(_c_) (_c_ >= '0' && _c_ <= '9' ? (_c_ - '0') :\
(_c_ >= 'A' && _c_ <= 'F' ? (_c_ - 'A' + 10) :\
(_c_ >= 'a' && _c_ <= 'f' ? (_c_ - 'a' + 10) : 0)))
#endif
/**
* @brief sender or receiver context
*/
struct ossl_hpke_ctx_st
{
OSSL_LIB_CTX *libctx; /* library context */
char *propq; /* properties */
int mode; /* HPKE mode */
OSSL_HPKE_SUITE suite; /* suite */
uint64_t seq; /* aead sequence number */
unsigned char *shared_secret; /* KEM output, zz */
size_t shared_secretlen;
unsigned char *key; /* final aead key */
size_t keylen;
unsigned char *nonce; /* aead base nonce */
size_t noncelen;
unsigned char *exportersec; /* exporter secret */
size_t exporterseclen;
char *pskid; /* PSK stuff */
unsigned char *psk;
size_t psklen;
EVP_PKEY *authpriv; /* sender's authentication private key */
unsigned char *authpub; /* auth public key */
size_t authpublen;
unsigned char *ikme; /* IKM for sender deterministic key gen */
size_t ikmelen;
};
#if defined(SUPERVERBOSE)
/*
* @brief for odd/occasional debugging
*
* @param fout is a FILE * to use
* @param msg is prepended to print
* @param buf is the buffer to print
* @param blen is the length of the buffer
* @return 1 for success, 0 otherwise
*/
static int hpke_pbuf(FILE *fout, const char *msg,
const unsigned char *buf, size_t blen)
{
size_t i = 0;
if (fout == NULL) {
return 0;
}
if (msg == NULL) {
fprintf(fout, "NULL msg:");
} else {
fprintf(fout, "%s (%lu): ", msg, blen);
}
if (buf == NULL) {
fprintf(fout, "buf is NULL, so maybe something wrong (or not:-)\n");
return 1;
}
if (blen == OSSL_HPKE_MAXSIZE) {
fprintf(fout, "length is OSSL_HPKE_MAXSIZE, so probably unused\n");
return 1;
}
if (blen == 0) {
fprintf(fout, "length is 0, so probably something wrong\n");
return 1;
}
for (i = 0; i < blen; i++) { fprintf(fout, "%02x", buf[i]); }
fprintf(fout, "\n");
return 1;
}
#endif
#ifdef HAPPYKEY
/* Define more HPKE labels from RFC9180 in hex for EBCDIC compatibility */
/* "HPKE-v1" - version string label */
static const char OSSL_HPKE_VERLABEL[] = "\x48\x50\x4B\x45\x2D\x76\x31";
/* "eae_prk" - label in ExtractAndExpand */
static const char OSSL_HPKE_EAE_PRK_LABEL[] = "\x65\x61\x65\x5f\x70\x72\x6b";
/* "shared_secret" - shared secret calc label */
static const char OSSL_HPKE_SS_LABEL[] = "\x73\x68\x61\x72\x65\x64\x5f\x73\x65\x63\x72\x65\x74";
/* "KEM" - "suite_id" label for 4.1 */
static const char OSSL_HPKE_SEC41LABEL[] = "\x4b\x45\x4d";
/* "dkp_prk" - DeriveKeyPair label */
static const char OSSL_HPKE_DPK_LABEL[] = "\x64\x6b\x70\x5f\x70\x72\x6b";
/* "candidate" - used in deterministic key gen */
static const char OSSL_HPKE_CAND_LABEL[] = "\x63\x61\x6e\x64\x69\x64\x61\x74\x65";
/* "sk" - label used in deterministic key gen */
static const char OSSL_HPKE_SK_LABEL[] = "\x73\x6b";
/* polyfill for the DHKEM stuff being in the OpenSSL library */
#define OSSL_HPKE_5869_MODE_PURE 0 /* Do "pure" RFC5869 */
#define OSSL_HPKE_5869_MODE_KEM 1 /* Abide by HPKE section 4.1 */
#define OSSL_HPKE_5869_MODE_FULL 2 /* Abide by HPKE section 5.1 */
/*
/*
* @brief RFC5869 HKDF-Extract
*
* @param libctx is the context to use
* @param propq is a properties string
* @param suite is the ciphersuite
* @param mode5869 - controls labelling specifics
* @param salt - surprisingly this is the salt;-)
* @param saltlen - length of above
* @param label - label for separation
* @param labellen - length of above
* @param zz - the initial key material (IKM)
* @param zzlen - length of above
* @param secret - the result of extraction (allocated inside)
* @param secretlen - bufsize on input, used size on output
* @return 1 for success, 0 otherwise
*
* Mode can be:
* - OSSL_HPKE_5869_MODE_PURE meaning to ignore all the
* HPKE-specific labelling and produce an output that's
* RFC5869 compliant (useful for testing and maybe
* more)
* - OSSL_HPKE_5869_MODE_KEM meaning to follow section 4.1
* where the suite_id is used as:
* concat("KEM", I2OSP(kem_id, 2))
* - OSSL_HPKE_5869_MODE_FULL meaning to follow section 5.1
* where the suite_id is used as:
* concat("HPKE", I2OSP(kem_id, 2),
* I2OSP(kdf_id, 2), I2OSP(aead_id, 2))
*
* Isn't that a bit of a mess!
*/
static int hpke_extract(OSSL_LIB_CTX *libctx, const char *propq,
const OSSL_HPKE_SUITE suite, const int mode5869,
const unsigned char *salt, const size_t saltlen,
const char *label, const size_t labellen,
const unsigned char *ikm, const size_t ikmlen,
unsigned char *secret, size_t *secretlen)
{
EVP_KDF *kdf = NULL;
EVP_KDF_CTX *kctx = NULL;
OSSL_PARAM params[5], *p = params;
int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
const char *mdname = NULL;
unsigned char labeled_ikmbuf[2 * OSSL_HPKE_MAXSIZE];
unsigned char *labeled_ikm = labeled_ikmbuf;
size_t labeled_ikmlen = 0;
int erv = 1;
size_t lsecretlen = 0;
WPACKET pkt;
const OSSL_HPKE_KEM_INFO *kem_info = NULL;
const OSSL_HPKE_KDF_INFO *kdf_info = NULL;
if (!WPACKET_init_static_len(&pkt, labeled_ikmbuf,
sizeof(labeled_ikmbuf), 0))
goto err;
/* Handle oddities of HPKE labels (or not) */
switch (mode5869) {
case OSSL_HPKE_5869_MODE_PURE:
labeled_ikmlen = ikmlen;
labeled_ikm = (unsigned char *)ikm;
break;
case OSSL_HPKE_5869_MODE_KEM:
if (!WPACKET_memcpy(&pkt, OSSL_HPKE_VERLABEL,
strlen(OSSL_HPKE_VERLABEL))
|| !WPACKET_memcpy(&pkt, OSSL_HPKE_SEC41LABEL,
strlen(OSSL_HPKE_SEC41LABEL))
|| !WPACKET_put_bytes_u16(&pkt, suite.kem_id)
|| !WPACKET_memcpy(&pkt, label, labellen)
|| !WPACKET_memcpy(&pkt, ikm, ikmlen)
|| !WPACKET_get_total_written(&pkt, &labeled_ikmlen)
|| !WPACKET_finish(&pkt))
goto err;
break;
case OSSL_HPKE_5869_MODE_FULL:
if (!WPACKET_memcpy(&pkt, OSSL_HPKE_VERLABEL,
strlen(OSSL_HPKE_VERLABEL))
|| !WPACKET_memcpy(&pkt, OSSL_HPKE_SEC51LABEL,
strlen(OSSL_HPKE_SEC51LABEL))
|| !WPACKET_put_bytes_u16(&pkt, suite.kem_id)
|| !WPACKET_put_bytes_u16(&pkt, suite.kdf_id)
|| !WPACKET_put_bytes_u16(&pkt, suite.aead_id)
|| !WPACKET_memcpy(&pkt, label, labellen)
|| !WPACKET_memcpy(&pkt, ikm, ikmlen)
|| !WPACKET_get_total_written(&pkt, &labeled_ikmlen)
|| !WPACKET_finish(&pkt))
goto err;
break;
default:
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Find and allocate a context for the HKDF algorithm */
if ((kdf = EVP_KDF_fetch(libctx, "hkdf", propq)) == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
kctx = EVP_KDF_CTX_new(kdf);
EVP_KDF_free(kdf); /* The kctx keeps a reference so this is safe */
kdf = NULL;
if (kctx == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Build up the parameters for the derivation */
if (mode5869 == OSSL_HPKE_5869_MODE_KEM) {
kem_info = ossl_HPKE_KEM_INFO_find_id(suite.kem_id);
if (kem_info == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
mdname = kem_info->mdname;
} else {
kdf_info = ossl_HPKE_KDF_INFO_find_id(suite.kdf_id);
if (kdf_info == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
mdname = kdf_info->mdname;
}
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
(char *)mdname, 0);
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
(unsigned char *)labeled_ikm,
labeled_ikmlen);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
(unsigned char *)salt, saltlen);
*p = OSSL_PARAM_construct_end();
if (EVP_KDF_CTX_set_params(kctx, params) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
lsecretlen = EVP_KDF_CTX_get_kdf_size(kctx);
if (lsecretlen > *secretlen) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Do the derivation */
if (EVP_KDF_derive(kctx, secret, lsecretlen, params) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
*secretlen = lsecretlen;
err:
OPENSSL_cleanse(labeled_ikmbuf, 2 * OSSL_HPKE_MAXSIZE);
WPACKET_cleanup(&pkt);
EVP_KDF_free(kdf);
EVP_KDF_CTX_free(kctx);
memset(labeled_ikmbuf, 0, sizeof(labeled_ikmbuf));
return erv;
}
/*
* @brief RFC5869 HKDF-Expand
*
* @param libctx is the context to use
* @param propq is a properties string
* @param suite is the ciphersuite
* @param mode5869 - controls labelling specifics
* @param prk - the initial pseudo-random key material
* @param prk - length of above
* @param label - label to prepend to info
* @param labellen - label to prepend to info
* @param context - the info
* @param contextlen - length of above
* @param L - the length of the output desired
* @param out - the result of expansion (allocated by caller)
* @param outlen - buf size on input
* @return 1 for success, 0 otherwise
*/
static int hpke_expand(OSSL_LIB_CTX *libctx, const char *propq,
const OSSL_HPKE_SUITE suite, const int mode5869,
const unsigned char *prk, const size_t prklen,
const char *label, const size_t labellen,
const unsigned char *info, const size_t infolen,
const uint32_t L,
unsigned char *out, size_t *outlen)
{
int erv = 1;
unsigned char libuf[2 * OSSL_HPKE_MAXSIZE];
size_t concat_offset = 0;
size_t loutlen = L;
EVP_KDF *kdf = NULL;
EVP_KDF_CTX *kctx = NULL;
OSSL_PARAM params[5], *p = params;
int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
const char *mdname = NULL;
const OSSL_HPKE_KEM_INFO *kem_info = NULL;
const OSSL_HPKE_KDF_INFO *kdf_info = NULL;
WPACKET pkt;
if (!WPACKET_init_static_len(&pkt, libuf, sizeof(libuf), 0))
goto err;
if (L > *outlen) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Handle oddities of HPKE labels (or not) */
switch (mode5869) {
case OSSL_HPKE_5869_MODE_PURE:
if (!WPACKET_memcpy(&pkt, label, labellen)
|| !WPACKET_memcpy(&pkt, info, infolen)
|| !WPACKET_get_total_written(&pkt, &concat_offset)
|| !WPACKET_finish(&pkt))
goto err;
break;
case OSSL_HPKE_5869_MODE_KEM:
if (!WPACKET_put_bytes_u16(&pkt, L)
|| !WPACKET_memcpy(&pkt, OSSL_HPKE_VERLABEL,
strlen(OSSL_HPKE_VERLABEL))
|| !WPACKET_memcpy(&pkt, OSSL_HPKE_SEC41LABEL,
strlen(OSSL_HPKE_SEC41LABEL))
|| !WPACKET_put_bytes_u16(&pkt, suite.kem_id)
|| !WPACKET_memcpy(&pkt, label, labellen)
|| (info == NULL ? 0 : !WPACKET_memcpy(&pkt, info, infolen))
|| !WPACKET_get_total_written(&pkt, &concat_offset)
|| !WPACKET_finish(&pkt))
goto err;
break;
case OSSL_HPKE_5869_MODE_FULL:
if (!WPACKET_put_bytes_u16(&pkt, L)
|| !WPACKET_memcpy(&pkt, OSSL_HPKE_VERLABEL,
strlen(OSSL_HPKE_VERLABEL))
|| !WPACKET_memcpy(&pkt, OSSL_HPKE_SEC51LABEL,
strlen(OSSL_HPKE_SEC51LABEL))
|| !WPACKET_put_bytes_u16(&pkt, suite.kem_id)
|| !WPACKET_put_bytes_u16(&pkt, suite.kdf_id)
|| !WPACKET_put_bytes_u16(&pkt, suite.aead_id)
|| !WPACKET_memcpy(&pkt, label, labellen)
|| !WPACKET_memcpy(&pkt, info, infolen)
|| !WPACKET_get_total_written(&pkt, &concat_offset)
|| !WPACKET_finish(&pkt))
goto err;
break;
default:
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Find and allocate a context for the HKDF algorithm */
if ((kdf = EVP_KDF_fetch(libctx, "hkdf", propq)) == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
kctx = EVP_KDF_CTX_new(kdf);
EVP_KDF_free(kdf); /* The kctx keeps a reference so this is safe */
kdf = NULL;
if (kctx == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Build up the parameters for the derivation */
if (mode5869 == OSSL_HPKE_5869_MODE_KEM) {
kem_info = ossl_HPKE_KEM_INFO_find_id(suite.kem_id);
if (kem_info == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
mdname = kem_info->mdname;
} else {
kdf_info = ossl_HPKE_KDF_INFO_find_id(suite.kdf_id);
if (kdf_info == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
mdname = kdf_info->mdname;
}
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
(char *)mdname, 0);
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
(unsigned char *) prk, prklen);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
libuf, concat_offset);
*p = OSSL_PARAM_construct_end();
if (EVP_KDF_CTX_set_params(kctx, params) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Do the derivation */
if (EVP_KDF_derive(kctx, out, loutlen, params) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
*outlen = loutlen;
err:
OPENSSL_cleanse(libuf, 2 * OSSL_HPKE_MAXSIZE);
EVP_KDF_free(kdf);
EVP_KDF_CTX_free(kctx);
memset(libuf, 0, sizeof(libuf));
return erv;
}
/*
* @brief ExtractAndExpand
*
* @param libctx is the context to use
* @param propq is a properties string
* @param suite is the ciphersuite
* @param mode5869 - controls labelling specifics
* @param shared_secret - the initial DH shared secret
* @param shared_secretlen - length of above
* @param context - the info
* @param contextlen - length of above
* @param secret - the result of extract&expand
* @param secretlen - buf size on input
* @return 1 for success, 0 otherwise
*/
static int hpke_extract_and_expand(OSSL_LIB_CTX *libctx, const char *propq,
OSSL_HPKE_SUITE suite, int mode5869,
unsigned char *shared_secret,
size_t shared_secretlen,
unsigned char *context, size_t contextlen,
unsigned char *secret, size_t *secretlen)
{
int erv = 1;
unsigned char eae_prkbuf[OSSL_HPKE_MAXSIZE];
size_t eae_prklen = OSSL_HPKE_MAXSIZE;
size_t lsecretlen = 0;
const OSSL_HPKE_KEM_INFO *kem_info = NULL;
kem_info = ossl_HPKE_KEM_INFO_find_id(suite.kem_id);
if (kem_info == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
lsecretlen = kem_info->Nsecret;
#if defined(SUPERVERBOSE)
hpke_pbuf(stdout, "\teae_ssinput", shared_secret, shared_secretlen);
hpke_pbuf(stdout, "\teae_context", context, contextlen);
printf("\tNsecret: %lu\n", lsecretlen);
#endif
erv = hpke_extract(libctx, propq, suite, mode5869,
(const unsigned char *)"", 0,
OSSL_HPKE_EAE_PRK_LABEL, strlen(OSSL_HPKE_EAE_PRK_LABEL),
shared_secret, shared_secretlen,
eae_prkbuf, &eae_prklen);
if (erv != 1) { goto err; }
#if defined(SUPERVERBOSE)
hpke_pbuf(stdout, "\teae_prk", eae_prkbuf, eae_prklen);
#endif
erv = hpke_expand(libctx, propq, suite, mode5869,
eae_prkbuf, eae_prklen,
OSSL_HPKE_SS_LABEL, strlen(OSSL_HPKE_SS_LABEL),
context, contextlen,
lsecretlen, secret, &lsecretlen);
if (erv != 1) { goto err; }
*secretlen = lsecretlen;
#if defined(SUPERVERBOSE)
hpke_pbuf(stdout, "\tshared secret", secret, *secretlen);
#endif
err:
OPENSSL_cleanse(eae_prkbuf, OSSL_HPKE_MAXSIZE);
memset(eae_prkbuf, 0, sizeof(eae_prkbuf));
return erv;
}
/*
* @brief run the KEM with two keys as required
*
* @param libctx is the context to use
* @param propq is a properties string
* @param encrypting is 1 if we're encrypting, 0 for decrypting
* @param suite is the ciphersuite
* @param key1 is the first key, for which we have the private value
* @param key1enclen is the length of the encoded form of key1
* @param key1en is the encoded form of key1
* @param key2 is the peer's key
* @param key2enclen is the length of the encoded form of key1
* @param key2en is the encoded form of key1
* @param akey is the authentication private key
* @param apublen is the length of the encoded the authentication public key
* @param apub is the encoded form of the authentication public key
* @param ss is (a pointer to) the buffer for the shared secret result
* @param sslen is the size of the buffer (octets-used on exit)
* @return 1 for success, 0 otherwise
*/
int hpke_do_kem(OSSL_LIB_CTX *libctx, const char *propq,
int encrypting, OSSL_HPKE_SUITE suite,
EVP_PKEY *key1,
size_t key1enclen, const unsigned char *key1enc,
EVP_PKEY *key2,
size_t key2enclen, const unsigned char *key2enc,
EVP_PKEY *akey,
size_t apublen, const unsigned char *apub,
unsigned char **ss, size_t *sslen)
{
int erv = 1;
EVP_PKEY_CTX *pctx = NULL;
size_t zzlen = 2 * OSSL_HPKE_MAXSIZE;
unsigned char zz[2 * OSSL_HPKE_MAXSIZE];
size_t kem_contextlen = OSSL_HPKE_MAXSIZE;
unsigned char kem_context[OSSL_HPKE_MAXSIZE];
size_t lsslen = OSSL_HPKE_MAXSIZE;
unsigned char lss[OSSL_HPKE_MAXSIZE];
/* run DH KEM to get zz */
pctx = EVP_PKEY_CTX_new_from_pkey(libctx, key1, propq);
if (pctx == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (EVP_PKEY_derive_init(pctx) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (EVP_PKEY_derive_set_peer(pctx, key2) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (EVP_PKEY_derive(pctx, NULL, &zzlen) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (zzlen >= OSSL_HPKE_MAXSIZE) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (EVP_PKEY_derive(pctx, zz, &zzlen) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
EVP_PKEY_CTX_free(pctx);
pctx = NULL;
kem_contextlen = key1enclen + key2enclen;
if (kem_contextlen >= OSSL_HPKE_MAXSIZE) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (encrypting) {
memcpy(kem_context, key1enc, key1enclen);
memcpy(kem_context + key1enclen, key2enc, key2enclen);
} else {
memcpy(kem_context, key2enc, key2enclen);
memcpy(kem_context + key2enclen, key1enc, key1enclen);
}
if (apublen > 0) {
/* Append the public auth key (mypub) to kem_context */
if ((kem_contextlen + apublen) >= OSSL_HPKE_MAXSIZE) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
memcpy(kem_context + kem_contextlen, apub, apublen);
kem_contextlen += apublen;
}
if (akey != NULL) {
size_t zzlen2 = 0;
/* step 2 run to get 2nd half of zz */
if (encrypting) {
pctx = EVP_PKEY_CTX_new_from_pkey(libctx, akey, propq);
} else {
pctx = EVP_PKEY_CTX_new_from_pkey(libctx, key1, propq);
}
if (pctx == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (EVP_PKEY_derive_init(pctx) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (encrypting) {
if (EVP_PKEY_derive_set_peer(pctx, key2) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
} else {
if (EVP_PKEY_derive_set_peer(pctx, akey) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
}
if (EVP_PKEY_derive(pctx, NULL, &zzlen2) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (zzlen2 >= OSSL_HPKE_MAXSIZE) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (EVP_PKEY_derive(pctx, zz + zzlen, &zzlen2) <= 0) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
zzlen += zzlen2;
EVP_PKEY_CTX_free(pctx);
pctx = NULL;
}
#if defined(SUPERVERBOSE)
hpke_pbuf(stdout, "\tkem_context", kem_context, kem_contextlen);
hpke_pbuf(stdout, "\tzz", zz, zzlen);
#endif
erv = hpke_extract_and_expand(libctx, propq, suite, OSSL_HPKE_5869_MODE_KEM,
zz, zzlen, kem_context, kem_contextlen,
lss, &lsslen);
if (erv != 1) { goto err; }
*ss = OPENSSL_malloc(lsslen);
if (*ss == NULL) {
erv = 0;
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
memcpy(*ss, lss, lsslen);
*sslen = lsslen;
err:
OPENSSL_cleanse(zz, 2 * OSSL_HPKE_MAXSIZE);
OPENSSL_cleanse(kem_context, OSSL_HPKE_MAXSIZE);
OPENSSL_cleanse(lss, OSSL_HPKE_MAXSIZE);
EVP_PKEY_CTX_free(pctx);
return erv;
}
#endif
/**
* @brief check if KEM uses NIST curve or not
* @param kem_id is the externally supplied kem_id
* @return 1 for NIST curves, 0 for other
*/
static int hpke_kem_id_nist_curve(uint16_t kem_id)
{
const OSSL_HPKE_KEM_INFO *kem_info;
kem_info = ossl_HPKE_KEM_INFO_find_id(kem_id);
return kem_info != NULL && kem_info->groupname != NULL;
}
/**
* @brief wrapper to import NIST curve public key as easily as x25519/x448
* @param libctx is the context to use
* @param propq is a properties string
* @param gname is the curve groupname
* @param buf is the binary buffer with the (uncompressed) public value
* @param buflen is the length of the private key buffer
* @return a working EVP_PKEY * or NULL
*
* Note that this could be a useful function to make public in
* future, but would likely require a name change.
*/
static EVP_PKEY *evp_pkey_new_raw_nist_public_key(OSSL_LIB_CTX *libctx,
const char *propq,
const char *gname,
const unsigned char *buf,
size_t buflen)
{
#ifdef HAPPYKEY
/*
* s3_lib.c:ssl_generate_param_group has similar code so
* can be useful if the upstream code changes
*/
int erv = 0;
#endif
OSSL_PARAM params[2];
EVP_PKEY *ret = NULL;
EVP_PKEY_CTX *cctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq);
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
(char *)gname, 0);
params[1] = OSSL_PARAM_construct_end();
if (cctx == NULL
|| EVP_PKEY_paramgen_init(cctx) <= 0
|| EVP_PKEY_CTX_set_params(cctx, params) <= 0
|| EVP_PKEY_paramgen(cctx, &ret) <= 0
|| EVP_PKEY_set1_encoded_public_key(ret, buf, buflen) != 1) {
#if defined(SUPERVERBOSE)
printf("EARLY public fail\n");
#endif
EVP_PKEY_CTX_free(cctx);
EVP_PKEY_free(ret);
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
return NULL;
}
#if defined(SUPERVERBOSE)
if (ret != NULL) {
pblen = EVP_PKEY_get1_encoded_public_key(ret, &pbuf);
hpke_pbuf(stdout, "\tEARLY public", pbuf, pblen);
OPENSSL_free(pbuf);
} else {
printf("no EARLY public\n");
}
#endif
EVP_PKEY_CTX_free(cctx);
return ret;
}
/**
* @brief do the AEAD decryption
* @param libctx is the context to use
* @param propq is a properties string
* @param suite is the ciphersuite
* @param key is the secret
* @param keylen is the length of the secret
* @param iv is the initialisation vector
* @param ivlen is the length of the iv
* @param aad is the additional authenticated data
* @param aadlen is the length of the aad
* @param ct is the ciphertext buffer
* @param ctlen is the ciphertext length (including tag).
* @param pt is the output buffer
* @param ptlen input/output, better be big enough on input, exact on output
* @return 1 on success, 0 otherwise
*/
static int hpke_aead_dec(OSSL_LIB_CTX *libctx, const char *propq,
OSSL_HPKE_SUITE suite,
const unsigned char *key, size_t keylen,
const unsigned char *iv, size_t ivlen,
const unsigned char *aad, size_t aadlen,
const unsigned char *ct, size_t ctlen,
unsigned char *pt, size_t *ptlen)
{
int erv = 0;
EVP_CIPHER_CTX *ctx = NULL;
int len = 0;
size_t taglen;
EVP_CIPHER *enc = NULL;
const OSSL_HPKE_AEAD_INFO *aead_info = NULL;
if (pt == NULL || ptlen == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
aead_info = ossl_HPKE_AEAD_INFO_find_id(suite.aead_id);
if (aead_info == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
taglen = aead_info->taglen;
if (ctlen <= taglen || *ptlen < ctlen - taglen) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
goto err;
}
/* Create and initialise the context */
if ((ctx = EVP_CIPHER_CTX_new()) == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Initialise the encryption operation */
enc = EVP_CIPHER_fetch(libctx, aead_info->name, propq);
if (enc == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
if (EVP_DecryptInit_ex(ctx, enc, NULL, NULL, NULL) != 1) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
EVP_CIPHER_free(enc);
enc = NULL;
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL) != 1) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Initialise key and IV */
if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv) != 1) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Provide AAD. */
if (aadlen != 0 && aad != NULL) {
if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) != 1) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
}
if (EVP_DecryptUpdate(ctx, pt, &len, ct, ctlen - taglen) != 1) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
*ptlen = len;
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
taglen, (void *)(ct + ctlen - taglen))) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
/* Finalise decryption. */
if (EVP_DecryptFinal_ex(ctx, pt + len, &len) <= 0) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
erv = 1;
err:
if (erv != 1)
OPENSSL_cleanse(pt, *ptlen);
EVP_CIPHER_CTX_free(ctx);
EVP_CIPHER_free(enc);
return erv;
}
/**
* @brief do AEAD encryption as per the RFC
* @param libctx is the context to use
* @param propq is a properties string
* @param suite is the ciphersuite
* @param key is the secret
* @param keylen is the length of the secret
* @param iv is the initialisation vector
* @param ivlen is the length of the iv
* @param aad is the additional authenticated data
* @param aadlen is the length of the aad
* @param pt is the plaintext buffer
* @param ptlen is the length of pt
* @param ct is the output buffer
* @param ctlen input/output, needs space for tag on input, exact on output
* @return 1 for success, 0 otherwise
*/
static int hpke_aead_enc(OSSL_LIB_CTX *libctx, const char *propq,
OSSL_HPKE_SUITE suite,
const unsigned char *key, size_t keylen,
const unsigned char *iv, size_t ivlen,
const unsigned char *aad, size_t aadlen,
const unsigned char *pt, size_t ptlen,
unsigned char *ct, size_t *ctlen)
{
int erv = 0;
EVP_CIPHER_CTX *ctx = NULL;
int len;
size_t taglen = 0;
const OSSL_HPKE_AEAD_INFO *aead_info = NULL;
EVP_CIPHER *enc = NULL;
unsigned char tag[16];
if (ct == NULL || ctlen == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
goto err;
}
aead_info = ossl_HPKE_AEAD_INFO_find_id(suite.aead_id);
if (aead_info == NULL) {
ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);