-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
zen_ecdh.c
1162 lines (1091 loc) · 30.2 KB
/
zen_ecdh.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
/* This file is part of Zenroom (https://zenroom.dyne.org)
*
* Copyright (C) 2017-2020 Dyne.org foundation
* designed, written and maintained by Denis Roio <jaromil@dyne.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/// <h1>Elliptic Curve Diffie-Hellman encryption (ECDH)</h1>
//
// Asymmetric public/private key encryption technologies.
//
// ECDH encryption and ECDSA signing functionalities are provided by
// this module. New keyring instances are instantiated by calling the
// new() method, keys can be imported using the
//
// <code>
// Alice = ECDH.new()
// Bob = ECDH.new()
// </code>
//
// One can create more keyrings in the same script and call them with
// meaningful variable names to help making code more
// understandable. Each keyring instance offers methods prefixed with
// a double-colon that operate on arguments as well keys contained by
// the keyring: this way scripting can focus on the identities
// represented by each keyring, giving them names as 'Alice' or
// 'Bob'.
//
// @module ECDH
// @author Denis "Jaromil" Roio
// @license AGPLv3
// @copyright Dyne.org foundation 2017-2020
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <ecdh_support.h>
#include <zen_error.h>
#include <zen_octet.h>
#include <randombytes.h>
#include <lua_functions.h>
#include <zenroom.h>
#include <zen_memory.h>
#include <zen_hash.h>
#include <zen_ecdh.h>
#include <zen_big_factory.h>
// #include <ecp_SECP256K1.h>
#include <zen_big.h>
#define KEYPROT(alg, key) \
zerror(L, "%s engine has already a %s set:", alg, key); \
lerror(L, "Zenroom won't overwrite. Use a .new() instance.");
// from zen_ecdh_factory.h to setup function pointers
extern void ecdh_init(lua_State *L, ecdh *e);
ecdh ECDH;
/// Global ECDH functions
// @section ECDH.globals
// // internal to instance inside init.lua
// int ecdh_new(lua_State *L) {
// ecdh *e = (ecdh*)lua_newuserdata(L, sizeof(ecdh));
// ecdh_init(e);
// luaL_getmetatable(L, "zenroom.ecdh");
// lua_setmetatable(L, -2);
// ECDH = e; // global pointer to a single ECDH instance
// return(1);
// }
void ecdh_free(lua_State *L, ecdh *e) {
Z(L);
if(e) {
free(e);
Z->memcount_ecdhs--;
}
}
ecdh* ecdh_arg(lua_State *L,int n) {
Z(L);
void *ud = luaL_testudata(L, n, "zenroom.ecdh");
if(ud) {
ecdh *result = (ecdh*)malloc(sizeof(ecdh));
*result = *(ecdh*)ud;
Z->memcount_ecdhs++;
return result;
}
zerror(L, "invalid ecdh in argument");
return NULL;
}
int ecdh_destroy(lua_State *L) {
(void)L;
// no allocation done
return 0; }
/// Instance Methods
// @type keyring
/**
Generate an ECDH public/private key pair for a keyring
Keys generated are both returned and stored inside the keyring
table as public and private properties.
@function keyring:keygen()
@treturn[1] OCTET public key
@treturn[1] OCTET private key
*/
static int ecdh_keygen(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
// return a table
lua_createtable(L, 0, 2);
octet *pk = o_new(L,ECDH.fieldsize*2 +1);
if(pk == NULL) {
failed_msg = "Could not create public key";
goto end;
}
lua_setfield(L, -2, "public");
octet *sk = o_new(L,ECDH.fieldsize);
if(sk == NULL) {
failed_msg = "Could not create secret key";
goto end;
}
lua_setfield(L, -2, "private");
Z(L);
(*ECDH.ECP__KEY_PAIR_GENERATE)(Z->random_generator,sk,pk);
end:
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/**
Generate an ECDH public key from a secret key
Public key is returned.
@function keyring:pubgen()
@return OCTET public key
*/
static int ecdh_pubgen(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *sk = o_arg(L, 1);
if(sk == NULL) {
failed_msg = "Could not allocate secret key";
goto end;
}
octet *tmp = o_dup(L, sk);
if(sk == NULL) {
failed_msg = "Could not duplicate secret key";
goto end;
}
octet *pk = o_new(L,ECDH.fieldsize*2 +1);
if(pk == NULL) {
failed_msg = "Could not create public key";
goto end;
}
// If RNG is NULL then the private key is provided externally in S
// otherwise it is generated randomly internally
(*ECDH.ECP__KEY_PAIR_GENERATE)(NULL,tmp,pk);
end:
o_free(L, sk);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/*
Validate an ECDH public key.
This is done by:
1. Checking that it is not the point at infinity
2. Validating that it is on the correct group.
@param key the input public key octet to be validated
@function keyring:checkpub(key)
@return true if public key is OK, or false if not.
*/
static int ecdh_pubcheck(lua_State *L) {
BEGIN();
octet *pk = o_arg(L, 1);
if(pk == NULL) {
lerror(L, "Could not allocate public key");
lua_pushboolean(L, 0);
} else {
lua_pushboolean(L, (*ECDH.ECP__PUBLIC_KEY_VALIDATE)(pk)==0);
o_free(L, pk);
}
END(1);
}
/*
Generate a Diffie-Hellman shared session key. This function uses
two keyrings to calculate a shared key, then process it internally
through @{keyring:kdf2} to make it ready for use in
@{keyring:aead_encrypt}. This is compliant with the IEEE-1363
Diffie-Hellman shared secret specification for asymmetric key
encryption. It can be found here:
https://perso.telecom-paristech.fr/guilley/recherche/cryptoprocesseurs/ieee/00891000.pdf, section 6.2
@param keyring containing the public key to be used
@function keyring:session(keyring)
@treturn[1] octet KDF2 hashed session key ready for @{keyring:aead_encrypt}
@treturn[1] octet a @{BIG} number result of (private * public) % curve_order (before KDF2)
@see keyring:aead_encrypt
*/
static int ecdh_session(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *f = NULL, *s = NULL, *sk = NULL, *pk = NULL;
f = o_arg(L, 1);
if(f == NULL) {
failed_msg = "Could not allocate session key";
goto end;
}
s = o_arg(L, 2);
if(s == NULL) {
failed_msg = "Could not allocate session key";
goto end;
}
// ECDH_OK is 0 in milagro's ecdh.h.in
pk = (*ECDH.ECP__PUBLIC_KEY_VALIDATE)(s)== 0 ? s : NULL;
if(!pk) pk = (*ECDH.ECP__PUBLIC_KEY_VALIDATE)(f)== 0 ? f : NULL;
if(!pk) {
failed_msg = "public key not found in any argument";
goto end;
}
sk = (pk == s) ? f : s;
octet *kdf = o_new(L, SHA256);
if(!kdf) {
failed_msg = "Could not create KDF";
goto end;
}
octet *ses = o_new(L, 64); // modbytes of ecdh curve
if(!ses) {
failed_msg = "Could not create shared key";
goto end;
}
(*ECDH.ECP__SVDP_DH)(sk,pk,ses);
// NULL would be used internally by KDF2 as 'p' in the hash
// function ehashit(sha,z,counter,p,&H,0);
KDF2(SHA256,ses,NULL,SHA256,kdf);
end:
o_free(L, s);
o_free(L, f);
if(failed_msg) {
THROW(failed_msg);
lua_pushnil(L);
}
END(2);
}
/**
Returns X and Y coordinates of a public key
@function ECDH.xy(public_key)
@treturn[1] OCTET coordinate X of public key
@treturn[1] OCTET coordinate Y of public key
*/
static int ecdh_pub_xy(lua_State *L) {
BEGIN();
int res = 1;
char *failed_msg = NULL;
octet *pk = o_arg(L, 1);
if(pk == NULL) {
failed_msg = "Could not allocate public key";
goto end;
}
if((*ECDH.ECP__PUBLIC_KEY_VALIDATE)(pk)!=0) {
failed_msg = "Invalid public key passed as argument";
goto end;
}
// Export public key to octet. This is like o_dup but skips
// first byte since that is used internally by Milagro as a
// prefix for Montgomery (2) or non-Montgomery curves (4)
register int i;
octet *x = o_new(L, ECDH.fieldsize+1);
if(x == NULL) {
failed_msg = "Could not create x coordinate";
goto end;
}
for(i=0; i < ECDH.fieldsize; i++)
x->val[i] = pk->val[i+1]; // +1 skips first byte
x->val[ECDH.fieldsize+1] = 0x0;
x->len = ECDH.fieldsize;
// make sure y is there:
// could be omitted in montgomery notation
if(pk->len > ECDH.fieldsize<<1) {
octet *y = o_new(L, ECDH.fieldsize+1);
if(y == NULL) {
failed_msg = "Could not create y coordinate";
goto end;
}
for(i=0; i < ECDH.fieldsize; i++)
y->val[i] = pk->val[ECDH.fieldsize+i+1]; // +1 skips first byte
y->val[ECDH.fieldsize+1] = 0x0;
y->len = ECDH.fieldsize;
res = 2;
}
end:
o_free(L, pk);
if(failed_msg) {
THROW(failed_msg);
lua_pushnil(L);
}
END(res);
}
/**
Elliptic Curve Digital Signature Algorithm (ECDSA) signing
function. This method uses the private key inside a keyring to sign
a message, returning a signature to be used in @{keyring:verify}.
@param kp.private @{OCTET} of a public key
@param message string or @{OCTET} message to sign
@function ECDH.sign(kp.private, message)
@return table containing signature parameters octets (r,s)
@usage
kp = ECDH.keygen() -- generate keys or import them
m = "Message to be signed"
signature = ECDH.sign(kp.private, m)
assert( ECDH.verify(kp.public, m, signature) )
*/
static int ecdh_dsa_sign(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *sk = NULL, *m = NULL, *k = NULL;
sk = o_arg(L,1);
if(sk == NULL) {
failed_msg = "Could not allocate secret key";
goto end;
}
m = o_arg(L,2);
if(m == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
// IEEE ECDSA Signature, R and S are signature on F using private
// key S. One can either pass an RNG or have K already
// provide. For a correct K's generation see also RFC6979, however
// this argument is provided here mostly for testing purposes with
// pre-calculated vectors.
int max_size = 64;
if(lua_isnoneornil(L, 3)) {
// return a table
lua_createtable(L, 0, 2);
octet *r = o_new(L,max_size);
if(r == NULL) {
failed_msg = "Could not create signautre.r";
goto end;
}
lua_setfield(L, -2, "r");
octet *s = o_new(L,max_size);
if(s == NULL) {
failed_msg = "Could not create signautre.s";
goto end;
}
lua_setfield(L, -2, "s");
Z(L);
(*ECDH.ECP__SP_DSA)( max_size, Z->random_generator, NULL, sk, m, r, s);
} else {
octet *k = o_arg(L, 3);
if(k == NULL) {
failed_msg = "Could not allocate ephemeral key";
goto end;
}
// return a table
lua_createtable(L, 0, 2);
octet *r = o_new(L,max_size);
if(r == NULL) {
failed_msg = "Could not create signautre.r";
goto end;
}
lua_setfield(L, -2, "r");
octet *s = o_new(L,max_size);
if(s == NULL) {
failed_msg = "Could not create signautre.s";
goto end;
}
lua_setfield(L, -2, "s");
(*ECDH.ECP__SP_DSA)( max_size, NULL, k, sk, m, r, s );
}
end:
o_free(L, k);
o_free(L, m);
o_free(L, sk);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/**
Elliptic Curve Digital Signature Algorithm (ECDSA) signing
function with deterministic generation of k (see RFC6979 https://www.rfc-editor.org/rfc/rfc6979).
This method uses the private key inside a keyring to sign
a message, returning a signature to be used in @{keyring:verify_deterministic}.
@param kp.private @{OCTET} of a public key
@param message string or @{OCTET} message to sign
@param sha int length in bytes of the digest of the SHA function
@function ECDH.sign_deterministic(kp.private, message, sha)
@return table containing signature parameters octets and k (r,s,k)
@usage
kp = ECDH.keygen() -- generate keys or import them
m = "Message to be signed"
sha = 32 -- This is SHA256. Also 48 = SHA384 or 64 = SHA512 may be used.
signature = ECDH.sign_deterministic(kp.private, m, sha)
assert( ECDH.verify_determinitsic(kp.public, m, signature, sha) )
*/
static int ecdh_dsa_sign_det(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *sk = NULL, *m = NULL;
sk = o_arg(L,1);
if(sk == NULL) {
failed_msg = "Could not allocate secret key";
goto end;
}
m = o_arg(L,2);
if(m == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
int max_size = 0;
lua_Number n = lua_tointegerx(L, 3, &max_size);
if(max_size == 0) {
failed_msg = "invalid size zero for material to sign";
goto end;
}
// return a table
lua_createtable(L, 0, 2);
octet *r = o_new(L, (int) n);
if(r == NULL) {
failed_msg = "Could not create signautre.r";
goto end;
}
lua_setfield(L, -2, "r");
octet *s = o_new(L,(int) n);
if(s == NULL) {
failed_msg = "Could not create signautre.s";
goto end;
}
lua_setfield(L, -2, "s");
octet *k = o_new(L, (int) n);
if(k == NULL) {
failed_msg = "Could not create signautre.s";
goto end;
}
(*ECDH.ECP__SP_DSA_DET)( (int) n, sk, m, r, s, k);
end:
o_free(L, m);
o_free(L, sk);
if(failed_msg) {
THROW(failed_msg);
}
END(2);
}
/* Same as ecdha_dsa_sign_det but input message is already hashed.
For the generation of the k parameter we use HMAC with the SHA function of corresponding length.
*/
static int ecdh_dsa_sign_det_hashed(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *sk = NULL, *m = NULL;
sk = o_arg(L, 1);
if(sk == NULL) {
failed_msg = "Could not allocate secret key";
goto end;
}
m = o_arg(L, 2);
if(m == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
int max_size;
int parity;
lua_Number n = lua_tointegerx(L, 3, &max_size);
if(max_size==0) {
failed_msg = "missing 3rd argument: byte size of octet to sign";
goto end;
}
if (m->len != (int)n) {
failed_msg = "size of input does not match";
goto end;
}
// return a table
lua_createtable(L, 0, 2);
octet *r = o_new(L, (int)n);
if(r == NULL) {
failed_msg = "Could not create signautre.r";
goto end;
}
lua_setfield(L, -2, "r");
octet *s = o_new(L, (int)n);
if(s == NULL) {
failed_msg = "Could not create signautre.s";
goto end;
}
lua_setfield(L, -2, "s");
// Size of a big256 used with SECP256k1
(*ECDH.ECP__SP_DSA_DET_NOHASH)((int)n, sk, m, r, s, &parity);
lua_pushboolean(L, parity);
end:
o_free(L, m);
o_free(L, sk);
if(failed_msg) {
THROW(failed_msg);
lua_pushnil(L);
}
END(2);
}
/**
* Sign a message directly, without taking the hash (the input in an hashed message
* that is it is already hashed)
* @param sk private key
* @param m hashed message
* @param n size of the message
* @param k ephemeral private key (not mandatory)
* @return[1] table with r and s (r is the x of the ephemeral public key)
* @return[2] y of the ephemeral public key
*/
static int ecdh_dsa_sign_hashed(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *sk = NULL, *m = NULL, *k = NULL;
sk = o_arg(L, 1);
if(sk == NULL) {
failed_msg = "Could not allocate secret key";
goto end;
}
m = o_arg(L, 2);
if(m == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
// IEEE ECDSA Signature, R and S are signature on F using private
// key S. One can either pass an RNG or have K already
// provide. For a correct K's generation see also RFC6979, however
// this argument is provided here mostly for testing purposes with
// pre-calculated vectors.
int max_size;
int parity;
lua_Number n = lua_tointegerx(L, 3, &max_size);
if(max_size==0) {
failed_msg = "missing 3rd argument: byte size of octet to sign";
goto end;
}
if (m->len != (int)n) {
failed_msg = "size of input does not match";
goto end;
}
if(lua_isnoneornil(L, 4)) {
// return a table
lua_createtable(L, 0, 2);
octet *r = o_new(L, (int)n);
if(r == NULL) {
failed_msg = "Could not create signautre.r";
goto end;
}
lua_setfield(L, -2, "r");
octet *s = o_new(L, (int)n);
if(s == NULL) {
failed_msg = "Could not create signautre.s";
goto end;
}
lua_setfield(L, -2, "s");
// Size of a big256 used with SECP256k1
Z(L);
(*ECDH.ECP__SP_DSA_NOHASH)((int)n, Z->random_generator, NULL, sk, m, r, s, &parity);
} else {
k = o_arg(L, 4);
if(k == NULL) {
failed_msg = "Could not allocate ephemeral key";
goto end;
}
// return a table
lua_createtable(L, 0, 2);
octet *r = o_new(L, (int)n);
if(r == NULL) {
failed_msg = "Could not create signautre.r";
goto end;
}
lua_setfield(L, -2, "r");
octet *s = o_new(L, (int)n);
if(s == NULL) {
failed_msg = "Could not create signautre.s";
goto end;
}
lua_setfield(L, -2, "s");
// Size of a big256 used with SECP256k1
(*ECDH.ECP__SP_DSA_NOHASH)((int)n, NULL, k, sk, m, r, s, &parity);
}
lua_pushboolean(L, parity);
end:
o_free(L, k);
o_free(L, m);
o_free(L, sk);
if(failed_msg) {
THROW(failed_msg);
lua_pushnil(L);
}
END(2);
}
/**
Elliptic Curve Digital Signature Algorithm (ECDSA) verification
function. This method uses the public key inside a keyring to verify
a message, returning true or false. The signature parameters are
returned as 'r' and 's' in this same order by @{keyring:sign}.
@param message the message whose signature has to be verified
@param signature the signature table returned by @{keyring:sign}
@function ECDH.verify(kp.public, message,signature)
@return true if the signature is OK, or false if not.
@see ECDH.sign
*/
static int ecdh_dsa_verify(lua_State *L) {
BEGIN();
// IEEE1363 ECDSA Signature Verification. Signature C and D on F
// is verified using public key W
char *failed_msg = NULL;
octet *pk = NULL, *m = NULL, *r = NULL, *s = NULL;
pk = o_arg(L, 1);
if(pk == NULL) {
failed_msg = "Could not allocate public key";
goto end;
}
m = o_arg(L, 2);
if(m == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
if(lua_type(L, 3) == LUA_TTABLE) {
lua_getfield(L, 3, "r");
lua_getfield(L, 3, "s"); // -2 stack
r = o_arg(L, -2);
if(r == NULL) {
failed_msg = "Could not allocate signature.r";
goto end;
}
s = o_arg(L, -1);
if(s == NULL) {
failed_msg = "Could not allocate signautre.s";
goto end;
}
} else {
failed_msg = "signature argument invalid: not a table";
goto end;
}
int max_size = 64;
int res = (*ECDH.ECP__VP_DSA)(max_size, pk, m, r, s);
if(res <0) // ECDH_INVALID in milagro/include/ecdh.h.in (!?!)
// TODO: maybe suggest fixing since there seems to be
// no criteria between ERROR (used in the first check
// in VP_SDA) and INVALID (in the following two
// checks...)
lua_pushboolean(L, 0);
else
lua_pushboolean(L, 1);
end:
o_free(L, s);
o_free(L, r);
o_free(L, m);
o_free(L, pk);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/**
Elliptic Curve Digital Signature Algorithm (ECDSA) verification
function. The main difference between ecdh_dsa_verify and this function
is that here we also consider the input parameter sha int.
This method uses the public key inside a keyring to verify
a message, returning true or false. The signature parameters are
returned as 'r' and 's' in this same order by @{keyring:sign_deterministic}.
@param message the message whose signature has to be verified
@param signature the signature table returned by @{keyring:sign_deterministic}
@param sha int length in bytes of the digest of the SHA function
@function ECDH.verify_deterministic(kp.public, message, signature, sha)
@return true if the signature is OK, or false if not.
@see ECDH.sign_deterministic
*/
static int ecdh_dsa_verify_det(lua_State *L) {
BEGIN();
// IEEE1363 ECDSA Signature Verification. Signature C and D on F
// is verified using public key W
char *failed_msg = NULL;
octet *pk = NULL, *m = NULL, *r = NULL, *s = NULL;
pk = o_arg(L, 1);
if(pk == NULL) {
failed_msg = "Could not allocate public key";
goto end;
}
m = o_arg(L, 2);
if(m == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
if(lua_type(L, 3) == LUA_TTABLE) {
lua_getfield(L, 3, "r");
lua_getfield(L, 3, "s"); // -2 stack
r = o_arg(L, -2);
if(r == NULL) {
failed_msg = "Could not allocate signature.r";
goto end;
}
s = o_arg(L, -1);
if(s == NULL) {
failed_msg = "Could not allocate signautre.s";
goto end;
}
} else {
failed_msg = "signature argument invalid: not a table";
goto end;
}
int max_size = 0;
lua_Number n = lua_tointegerx(L, 4, &max_size);
if(max_size == 0) {
failed_msg = "invalid size zero for material to sign";
goto end;
}
int res = (*ECDH.ECP__VP_DSA)((int) n, pk, m, r, s);
if(res <0) // ECDH_INVALID in milagro/include/ecdh.h.in (!?!)
// TODO: maybe suggest fixing since there seems to be
// no criteria between ERROR (used in the first check
// in VP_SDA) and INVALID (in the following two
// checks...)
lua_pushboolean(L, 0);
else
lua_pushboolean(L, 1);
end:
o_free(L, s);
o_free(L, r);
o_free(L, m);
o_free(L, pk);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
static int ecdh_dsa_verify_hashed(lua_State *L) {
BEGIN();
// IEEE1363 ECDSA Signature Verification. Signature C and D on F
// is verified using public key W
char *failed_msg = NULL;
octet *pk = NULL, *m = NULL, *r = NULL, *s = NULL;
pk = o_arg(L, 1);
if(pk == NULL) {
failed_msg = "Could not allocate public key";
goto end;
}
m = o_arg(L, 2);
if(m == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
if(lua_type(L, 3) == LUA_TTABLE) {
lua_getfield(L, 3, "r");
lua_getfield(L, 3, "s"); // -2 stack
r = o_arg(L, -2);
if(r == NULL) {
failed_msg = "Could not allocate signautre.r";
goto end;
}
s = o_arg(L, -1);
if(s == NULL) {
failed_msg = "Could not allocate signautre.s";
goto end;
}
} else {
failed_msg = "signature argument invalid: not a table";
goto end;
}
int max_size = 0;
lua_Number n = lua_tointegerx(L, 4, &max_size);
if(max_size == 0) {
failed_msg = "invalid size zero for material to sign";
goto end;
}
if (m->len != (int)n) {
failed_msg = "size of input does not match";
}
int res = (*ECDH.ECP__VP_DSA_NOHASH)((int)n, pk, m, r, s);
if(res <0) // ECDH_INVALID in milagro/include/ecdh.h.in (!?!)
// TODO: maybe suggest fixing since there seems to be
// no criteria between ERROR (used in the first check
// in VP_SDA) and INVALID (in the following two
// checks...)
lua_pushboolean(L, 0);
else
lua_pushboolean(L, 1);
end:
o_free(L, s);
o_free(L, r);
o_free(L, m);
o_free(L, pk);
if(failed_msg) {
THROW(failed_msg);
}
END(1);
}
/*
AES-GCM encrypt with Additional Data (AEAD) encrypts and
authenticate a plaintext to a ciphtertext. Function compatible with
IEEE P802.1 specification. Errors out if encryption fails, else
returns the secret ciphertext and a SHA256 of the header to
checksum the integrity of the accompanying plaintext, to be
compared with the one obtained by @{aead_decrypt}.
@param key AES key octet (must be 16, 24 or 32 bytes long)
@param message input text in an octet
@param iv initialization vector. If the key is reused several times,
this param should be random, so the iv/key is different every time.
Follow RFC5116, section 3.1 for recommendations
@param header clear text, authenticated for integrity (checksum)
@param tag the authenticated tag. As per RFC5116, this should be 16 bytes
long
@function aead_encrypt(key, message, iv, h)
@treturn[1] octet containing the output ciphertext
@treturn[1] octet containing the authentication tag (checksum)
*/
static int ecdh_aead_encrypt(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *k = NULL, *in = NULL, *iv = NULL, *h = NULL;
k = o_arg(L, 1);
if(k == NULL) {
failed_msg = "Could not allocate aes key";
goto end;
}
// AES key size nk can be 16, 24 or 32 bytes
if(k->len > 32 || k->len < 16) {
zerror(L, "ECDH.aead_encrypt accepts only keys of 16, 24, 32, this is %u", k->len);
failed_msg = "ECDH encryption aborted";
goto end;
}
in = o_arg(L, 2);
if(in == NULL) {
failed_msg = "Could not allocate message";
goto end;
}
iv = o_arg(L, 3);
if(iv == NULL) {
failed_msg = "Could not allocate iv";
goto end;
}
if (iv->len < 12) {
zerror(L, "ECDH.aead_encrypt accepts an iv of 12 bytes minimum, this is %u", iv->len);
failed_msg = "ECDH encryption aborted";
goto end;
}
h = o_arg(L, 4);
if(h == NULL) {
failed_msg = "Could not allocate header";
goto end;
}
// output is padded to next word
octet *out = o_new(L, in->len+16);
if(out == NULL) {
failed_msg = "Could not create ciphertext";
goto end;
}
octet *t = o_new(L, 16);
if(t == NULL) {
failed_msg = "Could not create authentication tag";
goto end;
}
AES_GCM_ENCRYPT(k, iv, h, in, out, t);
end:
o_free(L, h);
o_free(L, iv);
o_free(L, in);
o_free(L, k);
if(failed_msg) {
THROW(failed_msg);
}
END(2);
}
/*
AES-GCM decrypt with Additional Data (AEAD) decrypts and
authenticate a plaintext to a ciphtertext . Compatible with IEEE
P802.1 specification.
@param key AES key octet
@param message input text in an octet
@param iv initialization vector
@param header the additional data
@treturn[1] octet containing the output ciphertext
@treturn[1] octet containing the authentication tag (checksum)
@function aead_decrypt(key, ciphertext, iv, h)
*/
static int ecdh_aead_decrypt(lua_State *L) {
BEGIN();
char *failed_msg = NULL;
octet *k = NULL, *in = NULL, *iv = NULL, *h = NULL;
k = o_arg(L, 1);
if(k == NULL) {
failed_msg = "Could not allocate aes key";
goto end;
}
if(k->len > 32 || k->len < 16) {
zerror(L, "ECDH.aead_decrypt accepts only keys of 16, 24, 32, this is %u", k->len);
failed_msg = "ECDH decryption aborted";
goto end;
}
in = o_arg(L, 2);
if(in == NULL) {
failed_msg = "Could not allocate messsage";
goto end;
}
iv = o_arg(L, 3);
if(iv == NULL) {
failed_msg = "Could not allocate iv";
goto end;
}
if (iv->len < 12) {
zerror(L, "ECDH.aead_decrypt accepts an iv of 12 bytes minimum, this is %u", iv->len);
failed_msg = "ECDH decryption aborted";
goto end;
}
h = o_arg(L, 4);
if(h == NULL) {
failed_msg = "Could not allocate header";
goto end;
}
// output is padded to next word
octet *out = o_new(L, in->len+16);
if(out == NULL) {
failed_msg = "Could not create ciphertext";
goto end;
}
octet *t2 = o_new(L, 16);
if(t2 == NULL) {
failed_msg = "Could not create authentication tag";
goto end;
}
AES_GCM_DECRYPT(k, iv, h, in, out, t2);
end:
o_free(L, h);
o_free(L, iv);
o_free(L, in);
o_free(L, k);
if(failed_msg) {
THROW(failed_msg);
}
END(2);
}
/**
Order of the curve underlying the ECDH implementation
@function ECDH.order()
@return BIG with the order
*/