-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid_sc.c
1667 lines (1291 loc) · 53.1 KB
/
hybrid_sc.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
#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/cmac.h>
#include <openssl/core_names.h>
#include <openssl/crypto.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/kdf.h>
#include <openssl/objects.h>
#include <openssl/params.h>
#include <openssl/pem.h>
#include <openssl/pkcs12.h>
#include <openssl/ssl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ec_keypair_t {
EVP_PKEY *priv;
EVP_PKEY *pub;
unsigned char *pub_hex;
unsigned char *pub_bin;
size_t pub_bin_len;
} ec_keypair_t;
typedef struct hybrid_signcryption_t{
unsigned char *iv;
size_t iv_len;
unsigned char *ciphertext;
size_t ciphertext_len;
unsigned char *tag;
size_t tag_len;
unsigned char *s;
size_t s_len;
} hybrid_signcryption_t;
typedef struct imessage_t{
unsigned char *iv;
size_t iv_len;
unsigned char *c1;
size_t c1_len;
unsigned char *c2;
size_t c2_len;
unsigned char *sig;
size_t sig_len;
} imessage_t;
typedef struct hpke_enc_t{
unsigned char *ye;
size_t ye_len;
unsigned char *ciphertext;
size_t ciphertext_len;
unsigned char *tag;
size_t tag_len;
} hpke_enc_t;
typedef struct ecies_encrypt_t{
unsigned char *ye;
size_t ye_len;
unsigned char *iv;
size_t iv_len;
unsigned char *ciphertext;
size_t ciphertext_len;
} ecies_encrypt_t;
typedef struct ecies_signature_t {
ecies_encrypt_t *c;
unsigned char *sig;
size_t sig_len;
} ecies_signature_t;
EVP_PKEY *export_rsa_public_pkey(EVP_PKEY *rsa_priv)
{
OSSL_LIB_CTX *libctx = NULL;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY *rsa_pub = NULL;
OSSL_PARAM params[3];
BIGNUM *n = NULL, *e = NULL;
size_t n_bin_len, e_bin_len;
unsigned char *n_bin=NULL, *e_bin =NULL;
EVP_PKEY_get_bn_param(rsa_priv, OSSL_PKEY_PARAM_RSA_N, &n);
EVP_PKEY_get_bn_param(rsa_priv, OSSL_PKEY_PARAM_RSA_E, &e);
n_bin_len = BN_num_bytes(n);
n_bin = OPENSSL_malloc(n_bin_len);
BN_bn2nativepad(n, n_bin, n_bin_len);
e_bin_len = BN_num_bytes(e);
e_bin = OPENSSL_malloc(e_bin_len);
BN_bn2nativepad(e, e_bin, e_bin_len);
params[0] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_RSA_N, n_bin, n_bin_len);
params[1] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_RSA_E, e_bin, e_bin_len);
params[2] = OSSL_PARAM_construct_end();
ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL);
EVP_PKEY_CTX_set_params(ctx, params);
EVP_PKEY_fromdata_init(ctx);
EVP_PKEY_fromdata(ctx, &rsa_pub, EVP_PKEY_PUBLIC_KEY, params);
EVP_PKEY_CTX_free(ctx);
OSSL_LIB_CTX_free(libctx);
BN_free(n);
BN_free(e);
OPENSSL_free(n_bin);
OPENSSL_free(e_bin);
return rsa_pub;
}
size_t rsa_oaep_encrypt(unsigned char *digest_name, EVP_PKEY *pub, unsigned char* in, size_t in_len, unsigned char ** out)
{
int ret=0;
OSSL_LIB_CTX *libctx=NULL;
EVP_PKEY_CTX *ctx = NULL;
char *propq = NULL;
size_t out_len;
OSSL_PARAM params[3];
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, OSSL_PKEY_RSA_PAD_MODE_OAEP, 0);
params[1]= OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, digest_name, 0);
params[2] = OSSL_PARAM_construct_end();
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pub, propq);
EVP_PKEY_encrypt_init_ex(ctx, params);
EVP_PKEY_encrypt(ctx, NULL, &out_len, in, in_len);
*out = OPENSSL_zalloc(out_len);
if( EVP_PKEY_encrypt(ctx, *out, &out_len, in, in_len) <=0 ){
OPENSSL_free(*out);
out_len = -1;
}
EVP_PKEY_CTX_free(ctx);
return out_len;
}
size_t rsa_oaep_decrypt(unsigned char *digest_name, EVP_PKEY *priv, unsigned char* in, size_t in_len, unsigned char ** out)
{
int ret=0;
OSSL_LIB_CTX *libctx=NULL;
EVP_PKEY_CTX *ctx = NULL;
char *propq = NULL;
size_t out_len;
OSSL_PARAM params[3];
params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, OSSL_PKEY_RSA_PAD_MODE_OAEP, 0);
params[1]= OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, digest_name, 0);
params[2] = OSSL_PARAM_construct_end();
ctx = EVP_PKEY_CTX_new_from_pkey(libctx, priv, propq);
EVP_PKEY_decrypt_init_ex(ctx, params);
EVP_PKEY_decrypt(ctx, NULL, &out_len, in, in_len);
*out = OPENSSL_zalloc(out_len);
if( EVP_PKEY_decrypt(ctx, *out, &out_len, in, in_len) <=0 ){
OPENSSL_free(*out);
out_len = -1;
}
EVP_PKEY_CTX_free(ctx);
return out_len;
}
EVP_PKEY* evp_pkey_from_point_hex(EC_GROUP* group, char* point_hex, BN_CTX* ctx)
{
EC_KEY* ec_key = EC_KEY_new();
EC_KEY_set_group(ec_key, group);
EC_POINT* ec_pub_point = EC_POINT_new(group);
ec_pub_point = EC_POINT_hex2point(group, point_hex, ec_pub_point, ctx);
EC_KEY_set_public_key(ec_key, ec_pub_point);
EVP_PKEY *pkey = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(pkey, ec_key);
return pkey;
}
int aes_ctr_raw(unsigned char *cipher_name, unsigned char *in, int in_len, unsigned char *key, unsigned char *iv, int iv_len, unsigned char **out, int is_encrypt )
{
EVP_CIPHER_CTX *ctx;
int out_len;
int len;
if(!(ctx = EVP_CIPHER_CTX_new()))
return -1;
const EVP_CIPHER *cipher = EVP_get_cipherbyname(cipher_name);
if(!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, is_encrypt))
return -1;
if(!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, is_encrypt))
return -1;
*out = OPENSSL_malloc(in_len);
if(!EVP_CipherUpdate(ctx, *out, &out_len, in, in_len))
return -1;
if(!EVP_CipherFinal_ex(ctx, *out, &len))
return -1;
out_len += len;
EVP_CIPHER_CTX_cleanup(ctx);
/*EVP_CIPHER_free(cipher);*/
return out_len;
}
int aead_encrypt_raw(unsigned char *cipher_name, unsigned char *plaintext, int plaintext_len, unsigned char *aad, int aad_len, unsigned char *key, unsigned char *iv, int iv_len, unsigned char **ciphertext, unsigned char **tag, int tag_len)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
if(!(ctx = EVP_CIPHER_CTX_new()))
return -1;
const EVP_CIPHER *cipher = EVP_get_cipherbyname(cipher_name);
if(1 != EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL))
return -1;
if(OPENSSL_strcasecmp(cipher_name, "gcm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
return -1;
}else if(OPENSSL_strcasecmp(cipher_name, "ccm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_len, NULL))
return -1;
}
if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
return -1;
if(1 != EVP_EncryptUpdate(ctx, NULL, &len, aad, aad_len))
return -1;
*ciphertext = OPENSSL_malloc(plaintext_len);
if(1 != EVP_EncryptUpdate(ctx, *ciphertext, &len, plaintext, plaintext_len))
return -1;
ciphertext_len = len;
if(1 != EVP_EncryptFinal_ex(ctx, *ciphertext + len, &len))
return -1;
ciphertext_len += len;
*tag = OPENSSL_malloc(tag_len);
if(OPENSSL_strcasecmp(cipher_name, "gcm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, tag_len, *tag))
return -1;
}else if(OPENSSL_strcasecmp(cipher_name, "ccm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, tag_len, *tag))
return -1;
}
EVP_CIPHER_CTX_free(ctx);
/*EVP_CIPHER_free(cipher);*/
return ciphertext_len;
}
int aead_decrypt_raw( unsigned char *cipher_name, unsigned char *ciphertext, int ciphertext_len, unsigned char *aad, int aad_len, unsigned char *tag, int tag_len, unsigned char *key, unsigned char *iv, int iv_len, unsigned char **plaintext)
{
EVP_CIPHER_CTX *ctx;
int len;
int plaintext_len;
int ret;
if(!(ctx = EVP_CIPHER_CTX_new()))
return -1;
const EVP_CIPHER *cipher = EVP_get_cipherbyname(cipher_name);
if(!EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL))
return -1;
if(OPENSSL_strcasecmp(cipher_name, "gcm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
return -1;
}else if(OPENSSL_strcasecmp(cipher_name, "ccm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_len, NULL))
return -1;
}
if(!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
return -1;
if(!EVP_DecryptUpdate(ctx, NULL, &len, aad, aad_len))
return -1;
*plaintext = OPENSSL_malloc(ciphertext_len);
if(!EVP_DecryptUpdate(ctx, *plaintext, &len, ciphertext, ciphertext_len))
return -1;
plaintext_len = len;
if(OPENSSL_strcasecmp(cipher_name, "gcm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag_len, tag))
return -1;
}else if(OPENSSL_strcasecmp(cipher_name, "ccm")){
if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_len, tag))
return -1;
}
ret = EVP_DecryptFinal_ex(ctx, *plaintext + len, &len);
EVP_CIPHER_CTX_free(ctx);
/*EVP_CIPHER_free(cipher);*/
if(ret > 0) {
plaintext_len += len;
return plaintext_len;
} else {
return -1;
}
}
int hmac_raw3(char *digest_name, unsigned char* key, size_t key_len, unsigned char *data, size_t data_len, unsigned char *data2, size_t data2_len, unsigned char *data3, size_t data3_len, unsigned char **out)
{
char *propq = NULL;
OSSL_LIB_CTX *library_context = NULL;
EVP_MAC *mac = NULL;
EVP_MAC_CTX *mctx = NULL;
EVP_MD_CTX *digest_context = NULL;
size_t out_len = 0;
OSSL_PARAM params[4], *p = params;
library_context = OSSL_LIB_CTX_new();
mac = EVP_MAC_fetch(library_context, "HMAC", propq);
mctx = EVP_MAC_CTX_new(mac);
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name, sizeof(digest_name));
*p = OSSL_PARAM_construct_end();
EVP_MAC_init(mctx, key, key_len, params);
EVP_MAC_update(mctx, data, data_len);
EVP_MAC_update(mctx, data2, data2_len);
EVP_MAC_update(mctx, data3, data3_len);
EVP_MAC_final(mctx, NULL, &out_len, 0);
*out = OPENSSL_malloc(out_len);
EVP_MAC_final(mctx, *out, &out_len, out_len);
EVP_MD_CTX_free(digest_context);
EVP_MAC_CTX_free(mctx);
EVP_MAC_free(mac);
OSSL_LIB_CTX_free(library_context);
return out_len;
}
int hmac_raw(char *digest_name, unsigned char* key, size_t key_len, unsigned char *data, size_t data_len, unsigned char **out)
{
char *propq = NULL;
OSSL_LIB_CTX *library_context = NULL;
EVP_MAC *mac = NULL;
EVP_MAC_CTX *mctx = NULL;
EVP_MD_CTX *digest_context = NULL;
size_t out_len = 0;
OSSL_PARAM params[4], *p = params;
library_context = OSSL_LIB_CTX_new();
mac = EVP_MAC_fetch(library_context, "HMAC", propq);
mctx = EVP_MAC_CTX_new(mac);
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, digest_name, sizeof(digest_name));
*p = OSSL_PARAM_construct_end();
EVP_MAC_init(mctx, key, key_len, params);
EVP_MAC_update(mctx, data, data_len);
EVP_MAC_final(mctx, NULL, &out_len, 0);
*out = OPENSSL_malloc(out_len);
EVP_MAC_final(mctx, *out, &out_len, out_len);
EVP_MD_CTX_free(digest_context);
EVP_MAC_CTX_free(mctx);
EVP_MAC_free(mac);
OSSL_LIB_CTX_free(library_context);
return out_len;
}
unsigned char* digest_raw(unsigned char* digest_name, unsigned char* msg, size_t msg_len, size_t *out_len_ptr)
{
unsigned char *out = NULL;
const EVP_MD *digest;
digest = EVP_get_digestbyname(digest_name);
*out_len_ptr = EVP_MD_get_size(digest);
out = OPENSSL_malloc(*out_len_ptr);
EVP_Digest(msg, msg_len, out, (unsigned int *) out_len_ptr, digest, NULL);
return out;
}
unsigned char * bin2hex(const unsigned char * bin, size_t bin_len)
{
unsigned char *out = NULL;
size_t out_len;
size_t n = bin_len*2 + 1;
out = OPENSSL_malloc(n);
OPENSSL_buf2hexstr_ex(out, n, &out_len, bin, bin_len, '\0');
return out;
}
unsigned char* export_pubkey(EVP_PKEY *priv_pkey)
{
unsigned char *pubkey = NULL;
size_t pubkey_len;
unsigned char *pub_hex = NULL;
pubkey_len = EVP_PKEY_get1_encoded_public_key(priv_pkey, &pubkey);
pub_hex = bin2hex(pubkey, pubkey_len);
if(pubkey) OPENSSL_free(pubkey);
return pub_hex;
}
ec_keypair_t * gen_ec_keypair(unsigned char* group_name){
ec_keypair_t *out = (ec_keypair_t *)malloc(sizeof(ec_keypair_t));
BN_CTX * bnctx= BN_CTX_new();
int nid = OBJ_sn2nid(group_name);
EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
EVP_PKEY_keygen_init(ctx);
EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
EVP_PKEY *xa_pkey = EVP_PKEY_new();
EVP_PKEY_keygen(ctx, &xa_pkey);
// PEM_write_PrivateKey(stdout, xa_pkey, NULL, NULL, 0, NULL, NULL);
unsigned char *ya_hex = export_pubkey(xa_pkey);
//printf("pub_hex: %s\n", ya_hex);
EVP_PKEY *ya_pkey=evp_pkey_from_point_hex(group, ya_hex, bnctx);
// PEM_write_PUBKEY(stdout, ya_pkey);
size_t ya_len;
unsigned char* ya = OPENSSL_hexstr2buf(ya_hex, &ya_len);
out->priv = xa_pkey;
out->pub = ya_pkey;
out->pub_hex = ya_hex;
out->pub_bin = ya;
out->pub_bin_len = ya_len;
EC_GROUP_free(group);
EVP_PKEY_CTX_free(ctx);
BN_CTX_free(bnctx);
return out;
}
int hkdf_raw(int mode, unsigned char *digest_name, unsigned char *ikm, size_t ikm_len, unsigned char *salt, size_t salt_len, unsigned char *info, size_t info_len, unsigned char **okm, size_t okm_len )
{
EVP_KDF *kdf = NULL;
EVP_KDF_CTX *kctx = NULL;
OSSL_PARAM params[6], *p = params;
OSSL_LIB_CTX *library_context = NULL;
library_context = OSSL_LIB_CTX_new();
kdf = EVP_KDF_fetch(library_context, "HKDF", NULL);
kctx = EVP_KDF_CTX_new(kdf);
*p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest_name, 0);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, ikm, ikm_len);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, info, info_len);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, salt_len);
*p = OSSL_PARAM_construct_end();
*okm = OPENSSL_malloc(okm_len);
if (EVP_KDF_derive(kctx, *okm, okm_len, params) != 1) {
OPENSSL_free(*okm);
okm_len = -1;
}
EVP_KDF_CTX_free(kctx);
EVP_KDF_free(kdf);
OSSL_LIB_CTX_free(library_context);
return okm_len;
}
EVP_PKEY* pubhex2pkey(unsigned char* group_name, unsigned char* yb_hex){
BN_CTX *bnctx = BN_CTX_new();
int nid = OBJ_sn2nid(group_name);
EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
EVP_PKEY *yb_pkey=evp_pkey_from_point_hex(group, yb_hex, bnctx);
BN_CTX_free(bnctx);
EC_GROUP_free(group);
return yb_pkey;
}
unsigned char* ecdh_raw(EVP_PKEY *priv, EVP_PKEY *peer_pub, size_t *z_len_ptr)
{
unsigned char* z=NULL;
EVP_PKEY_CTX *ctx;
ctx = EVP_PKEY_CTX_new(priv, NULL);
EVP_PKEY_derive_init(ctx);
EVP_PKEY_derive_set_peer(ctx, peer_pub);
EVP_PKEY_derive(ctx, NULL, z_len_ptr);
z = OPENSSL_malloc(*z_len_ptr);
EVP_PKEY_derive(ctx, z, z_len_ptr);
EVP_PKEY_CTX_free(ctx);
return z;
}
int LabeledExpand(unsigned char *suite_id, size_t suite_id_len, unsigned char* prk, size_t prk_len, unsigned char* label, size_t label_len, unsigned char* info, size_t info_len, size_t okm_len, unsigned char** okm){
unsigned char pre[] = { okm_len >> 8, okm_len & 0xff, 'H', 'P', 'K', 'E', '-', 'v', '1' };
size_t pre_len = strlen(pre);
unsigned char *labeled_info;
size_t labeled_info_len = pre_len + suite_id_len + label_len + info_len;
labeled_info = OPENSSL_malloc(labeled_info_len);
memcpy(labeled_info, pre, pre_len);
memcpy(labeled_info+pre_len, suite_id, suite_id_len);
memcpy(labeled_info+pre_len+suite_id_len, label, label_len);
memcpy(labeled_info+pre_len+suite_id_len+label_len, info, info_len);
unsigned char salt[] = "";
int out_len = hkdf_raw(2, "SHA256", prk, prk_len, salt, strlen(salt), labeled_info, labeled_info_len, okm, okm_len);
//BIO_dump_indent_fp(stdout, *okm, out_len, 2);
OPENSSL_free(labeled_info);
return out_len;
}
int LabeledExtract(unsigned char *suite_id, size_t suite_id_len, unsigned char* salt, size_t salt_len, unsigned char *label, size_t label_len, unsigned char* ikm, size_t ikm_len, size_t out_len, unsigned char **out)
{
unsigned char pre[] = "HPKE-v1";
size_t pre_len = strlen(pre);
unsigned char info[] = "";
unsigned char *labeled_ikm;
size_t labeled_ikm_len = pre_len + suite_id_len + label_len + ikm_len;
labeled_ikm = OPENSSL_malloc(labeled_ikm_len);
memcpy(labeled_ikm, pre, pre_len);
memcpy(labeled_ikm+pre_len, suite_id, suite_id_len);
memcpy(labeled_ikm+pre_len+suite_id_len, label, label_len);
memcpy(labeled_ikm+pre_len+suite_id_len+label_len, ikm, ikm_len);
out_len = hkdf_raw(1, "SHA256", labeled_ikm, labeled_ikm_len, salt, salt_len, info, strlen(info), out, out_len);
//BIO_dump_indent_fp(stdout, *out, out_len, 2);
OPENSSL_free(labeled_ikm);
return out_len;
}
int ExtractAndExpand(unsigned char *suite_id, size_t suite_id_len, unsigned char* dh, size_t dh_len, unsigned char *kem_context, size_t kem_context_len, unsigned char **out)
{
//printf("prk:\n");
unsigned char *eae_prk;
size_t eae_prk_len;
unsigned char salt[] ="";
unsigned char label_prk[] = "eae_prk";
eae_prk_len = LabeledExtract(suite_id, suite_id_len, salt, strlen(salt), label_prk, strlen(label_prk), dh, dh_len, 32, &eae_prk);
//printf("ss:\n");
unsigned char label_ss[] = "shared_secret";
size_t ss_len = LabeledExpand(suite_id, suite_id_len, eae_prk, eae_prk_len, label_ss, strlen(label_ss), kem_context, kem_context_len, 32, out);
OPENSSL_free(eae_prk);
return ss_len;
}
int hpke_nonce_xor(size_t nonce_len, uint64_t seq, uint8_t* base_nonce, uint8_t **out_nonce_ptr)
{
*out_nonce_ptr = OPENSSL_malloc(nonce_len);
uint8_t *out_nonce = *out_nonce_ptr;
memset(out_nonce, 0, nonce_len);
uint64_t seq_copy = seq;
for (size_t i = 0; i < 8; i++) {
out_nonce[nonce_len - i - 1] = seq_copy & 0xff;
seq_copy >>= 8;
}
for (size_t i = 0; i < nonce_len; i++) {
out_nonce[i] ^= base_nonce[i];
}
return nonce_len;
}
imessage_t* imessage_enc_raw(unsigned char* group_name, unsigned char* digest_name, unsigned char* sig_name, ec_keypair_t *ec_a, EVP_PKEY *yb_rsa_pkey, unsigned char* msg, size_t msg_len)
{
//printf("88-bits L:");
BIGNUM *L_bn=BN_new();
BN_rand(L_bn, 88, 0, 0);
unsigned char *L = OPENSSL_malloc(88);
int L_len = BN_bn2bin(L_bn, L);
//BIO_dump_indent_fp(stdout, L, L_len, 2);
//printf("h_bin:\n");
unsigned char *yb_bin;
size_t yb_bin_len = EVP_PKEY_get1_encoded_public_key(yb_rsa_pkey, &yb_bin);
unsigned char *h_bin;
size_t h_bin_len = hmac_raw3(digest_name, L, L_len, ec_a->pub_bin, ec_a->pub_bin_len, yb_bin, yb_bin_len, msg, msg_len, &h_bin);
//BIO_dump_indent_fp(stdout, h_bin, h_bin_len, 2);
//printf("k:\n");
unsigned char *k;
size_t k_len = 16;
k =OPENSSL_malloc(k_len);
memcpy(k, L, L_len);
memcpy(k + L_len, h_bin, 5);
//BIO_dump_indent_fp(stdout, k, k_len, 2);
//printf("iv:\n");
BIGNUM *iv_bn=BN_new();
BN_rand(iv_bn, 128, 0, 0);
unsigned char *iv = OPENSSL_malloc(128);
int iv_len = BN_bn2bin(iv_bn, iv);
//BIO_dump_indent_fp(stdout, iv, iv_len, 2);
//printf("c1:\n");
unsigned char* c1;
int c1_len = aes_ctr_raw("aes-128-ctr", msg, msg_len, k, iv, iv_len, &c1, 1);
//BIO_dump_indent_fp(stdout, c1, c1_len, 2);
//printf("c2:\n");
unsigned char *c2;
size_t c2_len = rsa_oaep_encrypt(digest_name, yb_rsa_pkey, k, k_len, &c2);
//BIO_dump_indent_fp(stdout, c2, c2_len, 2);
//printf("s:\n");
unsigned char *sig;
const char *propq = NULL;
OSSL_LIB_CTX *libctx = NULL;
size_t sig_len = 0;
/*unsigned char *sig_value = NULL;*/
EVP_MD_CTX *sign_context = NULL;
libctx = OSSL_LIB_CTX_new();
sign_context = EVP_MD_CTX_new();
EVP_DigestSignInit_ex(sign_context, NULL, sig_name, libctx, NULL, ec_a->priv, NULL);
EVP_DigestSignUpdate(sign_context, iv, iv_len);
EVP_DigestSignUpdate(sign_context, c1, c1_len);
EVP_DigestSignUpdate(sign_context, c2, c2_len);
EVP_DigestSignFinal(sign_context, NULL, &sig_len);
sig = OPENSSL_malloc(sig_len);
if (!EVP_DigestSignFinal(sign_context, sig, &sig_len)){
OPENSSL_free(sig);
sig_len = -1;
}
EVP_MD_CTX_free(sign_context);
OSSL_LIB_CTX_free(libctx);
//BIO_dump_indent_fp(stdout, sig, sig_len, 2);
imessage_t *out = (imessage_t *)malloc(sizeof(imessage_t));
out->iv = iv;
out->iv_len = iv_len;
out->c1 = c1;
out->c1_len = c1_len;
out->c2 = c2;
out->c2_len = c2_len;
out->sig = sig;
out->sig_len = sig_len;
OPENSSL_free(L);
/*OPENSSL_free(yb_bin);*/
OPENSSL_free(h_bin);
OPENSSL_free(k);
return out;
}
int imessage_dec_raw(unsigned char* group_name, unsigned char* digest_name, unsigned char* sig_name, EVP_PKEY *xb_rsa_pkey, EVP_PKEY *ya_pkey, imessage_t *c, unsigned char** msg)
{
BN_CTX *bnctx = BN_CTX_new();
int nid = OBJ_sn2nid(group_name);
EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
const char *propq = NULL;
OSSL_LIB_CTX *libctx = NULL;
EVP_MD_CTX *verify_context = NULL;
int ret = 0;
libctx = OSSL_LIB_CTX_new();
verify_context = EVP_MD_CTX_new();
EVP_DigestVerifyInit_ex(verify_context, NULL, sig_name, libctx, NULL, ya_pkey, NULL);
EVP_DigestVerifyUpdate(verify_context, c->iv, c->iv_len);
EVP_DigestVerifyUpdate(verify_context, c->c1, c->c1_len);
EVP_DigestVerifyUpdate(verify_context, c->c2, c->c2_len);
if (EVP_DigestVerifyFinal(verify_context, c->sig, c->sig_len)) {
ret = 1;
}
EVP_MD_CTX_free(verify_context);
OSSL_LIB_CTX_free(libctx);
OPENSSL_free(bnctx);
OPENSSL_free(group);
if(ret!=1)
return -1;
//printf("imessage's ecdsa verify: success!\n");
//printf("rsa dec k :\n");
unsigned char *k;
size_t k_len = rsa_oaep_decrypt(digest_name, xb_rsa_pkey, c->c2, c->c2_len, &k);
//BIO_dump_indent_fp(stdout, k, k_len, 2);
unsigned char k_tail[5];
memcpy(k_tail, k+11, 5);
//printf("dec msg:\n");
size_t msg_len;
msg_len = aes_ctr_raw("aes-128-ctr", c->c1, c->c1_len, k, c->iv, c->iv_len, msg, 0);
//BIO_dump_indent_fp(stdout, *msg, msg_len, 2);
//printf("h_bin:\n");
unsigned char *yb_bin;
size_t yb_bin_len = EVP_PKEY_get1_encoded_public_key(xb_rsa_pkey, &yb_bin);
unsigned char *ya_bin;
size_t ya_bin_len = EVP_PKEY_get1_encoded_public_key(ya_pkey, &ya_bin);
unsigned char *h_bin;
size_t h_bin_len = hmac_raw3(digest_name, k, 11, ya_bin, ya_bin_len, yb_bin, yb_bin_len, *msg, msg_len, &h_bin);
//BIO_dump_indent_fp(stdout, h_bin, h_bin_len, 2);
unsigned char h_head[5];
memcpy(h_head, h_bin, 5);
if(memcmp(k_tail, h_head, 5)==0){
//printf("imessage's hmac verify: success!\n");
}else{
msg_len = -1;
OPENSSL_free(*msg);
}
OPENSSL_free(k);
/*OPENSSL_free(yb_bin);*/
/*OPENSSL_free(ya_bin);*/
OPENSSL_free(h_bin);
return msg_len;
}
hpke_enc_t* hpke_auth_enc_raw(unsigned char* group_name, unsigned char* digest_name, ec_keypair_t *ec_a, unsigned char* yb_hex, unsigned char* msg, size_t msg_len)
{
unsigned char psk[] = "";
unsigned char psk_id[] = "";
unsigned char info[] = "";
uint64_t seq = 0;
//printf("xe:\n");
ec_keypair_t *ec_e = gen_ec_keypair(group_name);
//printf("yb:%s\n", yb_hex);
EVP_PKEY *yb_pkey=pubhex2pkey(group_name, yb_hex);
size_t yb_len;
unsigned char* yb = OPENSSL_hexstr2buf(yb_hex, &yb_len);
//printf("z_ER:\n");
unsigned char *zER;
size_t zERlen;
zER = ecdh_raw(ec_e->priv, yb_pkey, &zERlen);
//BIO_dump_indent_fp(stdout, zER, zERlen, 2);
//printf("z_SR:\n");
unsigned char *zSR;
size_t zSRlen;
zSR = ecdh_raw(ec_a->priv, yb_pkey, &zSRlen);
//BIO_dump_indent_fp(stdout, zSR, zSRlen, 2);
//printf("dh:\n");
unsigned char *dh;
size_t dh_len = zERlen + zSRlen;
dh =OPENSSL_malloc(dh_len);
memcpy(dh, zER, zERlen);
memcpy(dh + zERlen, zSR, zSRlen);
//BIO_dump_indent_fp(stdout, dh, dh_len, 2);
//printf("kem_context:\n");
unsigned char *kem_context;
size_t kem_context_len = ec_e->pub_bin_len + yb_len + ec_a->pub_bin_len;
kem_context = OPENSSL_malloc(kem_context_len);
memcpy(kem_context, ec_e->pub_bin, ec_e->pub_bin_len);
memcpy(kem_context + ec_e->pub_bin_len, yb, yb_len);
memcpy(kem_context + ec_e->pub_bin_len + ec_a->pub_bin_len, ec_a->pub_bin, ec_a->pub_bin_len);
//BIO_dump_indent_fp(stdout, kem_context, kem_context_len, 2);
//printf("shared_secret:\n");
int kem_id = 0x0010;
unsigned char suite_id_kem[] = { 'K', 'E', 'M', kem_id >> 8, kem_id & 0xff };
unsigned char* shared_secret;
size_t shared_secret_len = ExtractAndExpand(suite_id_kem, sizeof(suite_id_kem), dh, dh_len, kem_context, kem_context_len, &shared_secret);
int kdf_id = 1;
/*int aead_id = 2; //aes-256-gcm*/
int aead_id = 1; //aes-128-gcm
unsigned char suite_id[] = { 'H', 'P', 'K', 'E',
kem_id >> 8, kem_id & 0xff ,
kdf_id >> 8, kdf_id & 0xff ,
aead_id >> 8, aead_id & 0xff ,
};
//printf("psk_id_hash:\n");
unsigned char *psk_id_hash;
int psk_id_hash_len = LabeledExtract(suite_id, sizeof(suite_id), "", strlen(""), "psk_id_hash", strlen("psk_id_hash"), psk_id, strlen(psk_id), 32, &psk_id_hash);
//printf("info_hash:\n");
unsigned char *info_hash;
int info_hash_len = LabeledExtract(suite_id, sizeof(suite_id), "", strlen(""), "info_hash", strlen("info_hash"), info, strlen(info), 32, &info_hash);
//printf("key_schedule_context:\n");
unsigned char mode[] = { 0x02, };
unsigned char *key_schedule_context;
int key_schedule_context_len = sizeof(mode) + psk_id_hash_len + info_hash_len;
key_schedule_context = OPENSSL_malloc(key_schedule_context_len);
memcpy(key_schedule_context, mode, sizeof(mode));
memcpy(key_schedule_context +sizeof(mode), psk_id_hash, psk_id_hash_len);
memcpy(key_schedule_context +sizeof(mode)+psk_id_hash_len, info_hash, info_hash_len);
//BIO_dump_indent_fp(stdout, key_schedule_context, key_schedule_context_len, 2);
//printf("secret:\n");
unsigned char *secret;
size_t secret_len = LabeledExtract(suite_id, sizeof(suite_id), shared_secret, shared_secret_len, "secret", strlen("secret"), psk, strlen(psk), 32, &secret);
//printf("key:\n");
unsigned char *key;
size_t key_len = LabeledExpand(suite_id, sizeof(suite_id), secret, secret_len, "key", strlen("key"), key_schedule_context, key_schedule_context_len, 16, &key);
//printf("base_nonce:\n");
unsigned char *base_nonce;
size_t base_nonce_len = LabeledExpand(suite_id, sizeof(suite_id), secret, secret_len, "base_nonce", strlen("base_nonce"), key_schedule_context, key_schedule_context_len, 12, &base_nonce);
//printf("nonce:\n");
uint8_t *nonce;
hpke_nonce_xor(12, seq, base_nonce, &nonce);
//BIO_dump_indent_fp(stdout, nonce, base_nonce_len, 2);
//printf("msg:\n");
//BIO_dump_indent_fp(stdout, msg, msg_len, 2);
//printf("enc ciphertext:\n");
unsigned char *ciphertext=NULL;
unsigned char *tag=NULL;
unsigned char aad[] = "";
size_t tag_len = 16;
int ciphertext_len = aead_encrypt_raw("aes-128-gcm", msg, msg_len, aad, strlen(aad), key, nonce, base_nonce_len, &ciphertext, &tag, tag_len);
/*int ciphertext_len = aes_ctr_raw("aes-256-ctr", msg, msg_len, key, nonce, base_nonce_len, &ciphertext, 1);*/
//BIO_dump_indent_fp(stdout, ciphertext, ciphertext_len, 2);
//printf("enc tag:\n");
//BIO_dump_indent_fp(stdout, tag, tag_len, 2);
hpke_enc_t *out = (hpke_enc_t *)malloc(sizeof(hpke_enc_t));
out->ye = ec_e->pub_bin;
out->ye_len = ec_e->pub_bin_len;
out->ciphertext = ciphertext;
out->ciphertext_len = ciphertext_len;
out->tag = tag;
out->tag_len = tag_len;
OPENSSL_free(base_nonce);
OPENSSL_free(key);
OPENSSL_free(secret);
OPENSSL_free(key_schedule_context);
OPENSSL_free(info_hash);
OPENSSL_free(psk_id_hash);
OPENSSL_free(shared_secret);
OPENSSL_free(kem_context);
OPENSSL_free(dh);
OPENSSL_free(zER);
OPENSSL_free(zSR);
EVP_PKEY_free(yb_pkey);
EVP_PKEY_free(ec_e->priv);
EVP_PKEY_free(ec_e->pub);
OPENSSL_free(ec_e->pub_hex);
return out;
}
size_t hpke_auth_dec_raw(unsigned char* group_name, unsigned char* digest_name, EVP_PKEY *xb_pkey, EVP_PKEY *ya_pkey, hpke_enc_t *c, unsigned char** msg)
{
unsigned char psk[] = "";
unsigned char psk_id[] = "";
unsigned char info[] = "";
uint64_t seq = 0;
BN_CTX *bnctx = BN_CTX_new();
int nid = OBJ_sn2nid(group_name);
EC_GROUP *group = EC_GROUP_new_by_curve_name(nid);
unsigned char* ye_hex = bin2hex(c->ye, c->ye_len);
EVP_PKEY *ye_pkey=evp_pkey_from_point_hex(group, ye_hex, bnctx);
//printf("z_ER:\n");
unsigned char *zER;
size_t zERlen;
zER = ecdh_raw(xb_pkey, ye_pkey, &zERlen);
//BIO_dump_indent_fp(stdout, zER, zERlen, 2);
//printf("z_SR:\n");
unsigned char *zSR;
size_t zSRlen;
zSR = ecdh_raw(xb_pkey, ya_pkey, &zSRlen);
//BIO_dump_indent_fp(stdout, zSR, zSRlen, 2);
//printf("dh:\n");
unsigned char *dh;
size_t dh_len = zERlen + zSRlen;
dh =OPENSSL_malloc(dh_len);
memcpy(dh, zER, zERlen);
memcpy(dh + zERlen, zSR, zSRlen);
//BIO_dump_indent_fp(stdout, dh, dh_len, 2);
//printf("kem_context:\n");
unsigned char *kem_context;
unsigned char *yb_bin;
size_t yb_bin_len = EVP_PKEY_get1_encoded_public_key(xb_pkey, &yb_bin);
unsigned char *ya_bin;
size_t ya_bin_len = EVP_PKEY_get1_encoded_public_key(ya_pkey, &ya_bin);
size_t kem_context_len = c->ye_len + yb_bin_len + ya_bin_len;
kem_context = OPENSSL_malloc(kem_context_len);
memcpy(kem_context, c->ye, c->ye_len);
memcpy(kem_context + c->ye_len, yb_bin, yb_bin_len);
memcpy(kem_context + c->ye_len + yb_bin_len, ya_bin, ya_bin_len);
//BIO_dump_indent_fp(stdout, kem_context, kem_context_len, 2);
//printf("shared_secret:\n");
int kem_id = 0x0010;
unsigned char suite_id_kem[] = { 'K', 'E', 'M', kem_id >> 8, kem_id & 0xff };
unsigned char* shared_secret;
size_t shared_secret_len = ExtractAndExpand(suite_id_kem, sizeof(suite_id_kem), dh, dh_len, kem_context, kem_context_len, &shared_secret);
int kdf_id = 1;
/*int aead_id = 2; //aes-256-gcm*/
int aead_id = 1; //aes-128-gcm
unsigned char suite_id[] = { 'H', 'P', 'K', 'E',
kem_id >> 8, kem_id & 0xff ,
kdf_id >> 8, kdf_id & 0xff ,
aead_id >> 8, aead_id & 0xff ,
};
//printf("psk_id_hash:\n");
unsigned char *psk_id_hash;
int psk_id_hash_len = LabeledExtract(suite_id, sizeof(suite_id), "", strlen(""), "psk_id_hash", strlen("psk_id_hash"), psk_id, strlen(psk_id), 32, &psk_id_hash);