This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbinding.c
1101 lines (1055 loc) · 35.8 KB
/
binding.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 <assert.h>
#include <math.h>
#include <node_api.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <stdint.h>
#include <stdlib.h>
#define RESOURCE_NAME "@ronomon/crypto-async"
#define E_AAD "aad must be a buffer"
#define E_AAD_INVALID "aadSize invalid"
#define E_AAD_OFFSET "aadOffset must be an unsigned integer"
#define E_AAD_RANGE "aadOffset + aadSize > aad.length"
#define E_AAD_SIZE "aadSize must be an unsigned integer"
#define E_ALGORITHM "algorithm must be a string"
#define E_ALGORITHM_DISABLED "algorithm disabled"
#define E_ALGORITHM_UNKNOWN "algorithm unknown"
#define E_ARGUMENTS "wrong number of arguments"
#define E_BUFFER_LENGTH "buffer.length > INT_MAX"
#define E_CALLBACK "callback must be a function"
#define E_CANCELLED "asynchronous task was cancelled"
#define E_CORRUPT "corrupt"
#define E_ENCRYPT "encrypt must be 0 or 1"
#define E_IV "iv must be a buffer"
#define E_IV_INVALID "ivSize invalid"
#define E_IV_OFFSET "ivOffset must be an unsigned integer"
#define E_IV_RANGE "ivOffset + ivSize > iv.length"
#define E_IV_SIZE "ivSize must be an unsigned integer"
#define E_KEY "key must be a buffer"
#define E_KEY_INVALID "keySize invalid"
#define E_KEY_OFFSET "keyOffset must be an unsigned integer"
#define E_KEY_RANGE "keyOffset + keySize > key.length"
#define E_KEY_SIZE "keySize must be an unsigned integer"
#define E_OOM "out of memory"
#define E_SOURCE "source must be a buffer"
#define E_SOURCE_OFFSET "sourceOffset must be an unsigned integer"
#define E_SOURCE_RANGE "sourceOffset + sourceSize > source.length"
#define E_SOURCE_SIZE "sourceSize must be an unsigned integer"
#define E_TAG "tag must be a buffer"
#define E_TAG_INVALID "tagSize invalid"
#define E_TAG_OFFSET "tagOffset must be an unsigned integer"
#define E_TAG_RANGE "tagOffset + tagSize > tag.length"
#define E_TAG_SIZE "tagSize must be an unsigned integer"
#define E_TARGET "target must be a buffer"
#define E_TARGET_OFFSET "targetOffset must be an unsigned integer"
#define E_TARGET_RANGE "targetOffset + targetSize > target.length"
#define FLAG_CIPHER 1
#define FLAG_HASH 2
#define FLAG_HMAC 4
#define OK(call) \
assert((call) == napi_ok);
#define THROW(env, message) \
do { \
napi_throw_error((env), NULL, (message)); \
return NULL; \
} while (0)
static int arg_buf(
napi_env env,
napi_value value,
unsigned char** buffer,
int* length,
const char* error
) {
assert(*buffer == NULL);
assert(*length == 0);
bool is_buffer;
OK(napi_is_buffer(env, value, &is_buffer));
if (!is_buffer) {
napi_throw_error(env, NULL, error);
return 0;
}
size_t size = 0;
OK(napi_get_buffer_info(env, value, (void**) buffer, &size));
assert(*buffer != NULL);
if (size > INT_MAX) {
napi_throw_error(env, NULL, E_BUFFER_LENGTH);
return 0;
}
*length = (int) size;
assert(*length >= 0);
return 1;
}
static int arg_int(
napi_env env,
napi_value value,
int* integer,
const char* error
) {
assert(*integer == 0);
double temp = 0;
if (
// We get the value as a double so we can check for NaN, Infinity and float:
// https://github.com/nodejs/node/issues/26323
napi_get_value_double(env, value, &temp) != napi_ok ||
temp < 0 ||
// NaN:
isnan(temp) ||
// Infinity, also prevent UB for double->int cast below:
// https://groups.google.com/forum/#!topic/comp.lang.c/rhPzd4bgKJk
temp > INT_MAX ||
// Float:
(double) ((int) temp) != temp
) {
napi_throw_error(env, NULL, error);
return 0;
}
*integer = (int) temp;
assert(*integer >= 0);
return 1;
}
static int arg_str(
napi_env env,
napi_value value,
char* string,
const size_t length,
const char* error
) {
size_t out = 0;
if (napi_get_value_string_utf8(env, value, string, length, &out) != napi_ok) {
napi_throw_error(env, NULL, error);
return 0;
}
return 1;
}
static int cipher_aead(const EVP_CIPHER* evp_cipher) {
assert(evp_cipher);
if (EVP_CIPHER_nid(evp_cipher) == NID_chacha20_poly1305) return 1;
const int mode = EVP_CIPHER_mode(evp_cipher);
if (mode == EVP_CIPH_GCM_MODE) return 1;
if (mode == EVP_CIPH_OCB_MODE) return 1;
return 0;
}
static int cipher_supported(const EVP_CIPHER* evp_cipher) {
// CCM is slow, macs then encrypts, and has a complicated OpenSSL interface.
// CBC has a poor track record, and leaves padding after the decrypted target.
int nid = EVP_CIPHER_nid(evp_cipher);
if (nid == NID_chacha20_poly1305) return 1;
if (nid == NID_chacha20) return 1;
int mode = EVP_CIPHER_mode(evp_cipher);
if (mode == EVP_CIPH_CTR_MODE) return 1;
if (mode == EVP_CIPH_GCM_MODE) return 1;
// Disable OCB (patented):
// if (mode == EVP_CIPH_OCB_MODE) return 1;
return 0;
}
static int cipher_target_size(
const EVP_CIPHER* evp_cipher,
const int encrypt,
const int source_size
) {
assert(evp_cipher);
assert(encrypt == 0 || encrypt == 1);
assert(source_size >= 0);
const int block_size = EVP_CIPHER_block_size(evp_cipher);
assert(block_size >= 0);
if (block_size == 1) return source_size;
if (encrypt) {
// "The amount of data written depends on the block alignment of the
// encrypted data: as a result the amount of data written may be anything
// from zero bytes to (inl + cipher_block_size - 1)."
// We DO NOT subtract 1 according to the OpenSSL documentation above because
// a `source_size` of 0 plus a `block_size` of 16 minus 1 would be
// 15, but AES-CBC, for example, will write a minimum of `block_size`.
//
// Therefore, OpenSSL's explanation is only half-true, instead:
// 1. Maximum `target_size` must be aligned to `block_size`.
// 2. Maximum `target_size` must be at least `block_size`.
//
// We must adjust `target_size` once we know the final write offset.
assert(block_size <= INT_MAX - source_size);
return source_size + block_size;
} else {
// "The parameters and restrictions are identical to the encryption
// operations except that if padding is enabled the decrypted data buffer
// out passed to EVP_DecryptUpdate() should have sufficient room for
// (inl + cipher_block_size) bytes, unless the cipher block size is 1 in
// which case inl bytes is sufficient."
assert(block_size <= INT_MAX - source_size);
return source_size + block_size;
}
}
static int cipher_valid_aad_size(
const EVP_CIPHER* evp_cipher,
const int aad_size
) {
assert(evp_cipher);
assert(aad_size >= 0);
if (!cipher_aead(evp_cipher) && aad_size != 0) return 0;
return 1;
}
static int cipher_valid_iv_size(
const EVP_CIPHER* evp_cipher,
const int iv_size
) {
assert(evp_cipher);
assert(iv_size >= 0);
// OpenSSL allows variable length IVs for AEAD ciphers:
// "The maximum nonce length is 16 (CHACHA_CTR_SIZE, i.e. 128-bits)."
// "For OCB mode the maximum is 15."
//
// However, OpenSSL had CVE-2019-1543 because of this for ChaCha20-Poly1305:
// https://www.openssl.org/news/secadv/20190306.txt
// https://github.com/openssl/openssl/issues/8345
//
// Allowing variable length IVs also opens the door for further nonce reuse:
// https://github.com/openssl/openssl/pull/8406#issuecomment-470615087
//
// We therefore require all IVs to be the default length.
// Anything else is a recipe for disaster.
if (iv_size == EVP_CIPHER_iv_length(evp_cipher)) return 1;
return 0;
}
static int cipher_valid_key_size(
const EVP_CIPHER* evp_cipher,
const int key_size
) {
assert(evp_cipher);
assert(key_size >= 0);
if (key_size == EVP_CIPHER_key_length(evp_cipher)) return 1;
return 0;
}
static int cipher_valid_tag_size(
const EVP_CIPHER* evp_cipher,
const int tag_size
) {
assert(evp_cipher);
assert(tag_size >= 0);
if (!cipher_aead(evp_cipher)) {
if (tag_size != 0) return 0;
return 1;
}
if (EVP_CIPHER_nid(evp_cipher) == NID_chacha20_poly1305) {
// "taglen must be between 1 and 16 (POLY1305_BLOCK_SIZE) inclusive."
if (tag_size < 1 || tag_size > 16) return 0;
return 1;
}
const int mode = EVP_CIPHER_mode(evp_cipher);
if (mode == EVP_CIPH_GCM_MODE) {
// "The bit length of the tag, denoted t, is a security parameter, as
// discussed in Appendix B. In general, t may be any one of the following
// five values: 128, 120, 112, 104, or 96. For certain applications, t may
// be 64 or 32; guidance for the use of these two tag lengths, including
// requirements on the length of the input data and the lifetime of the key
// in these cases, is given in Appendix C. An implementation shall not
// support values for t that are different from the seven choices in the
// preceding paragraph. An implementation may restrict its support to as few
// as one of these values."
// https://nvlpubs.nist.gov/nistpubs/Legacy/SP/
// nistspecialpublication800-38d.pdf#page=17
if (!(tag_size >= 12 && tag_size <= 16) && tag_size != 8 && tag_size != 4) {
return 0;
}
return 1;
}
if (mode == EVP_CIPH_OCB_MODE) {
// "taglen must be between 1 and 16 inclusive."
if (tag_size < 1 || tag_size > 16) return 0;
return 1;
}
if (tag_size < 1) return 0;
// Defer validation to OpenSSL.
return 1;
}
static const char* execute_cipher(
const int nid,
const int encrypt,
const unsigned char* key,
const int key_size,
const unsigned char* iv,
const int iv_size,
const unsigned char* source,
const int source_size,
unsigned char* target,
int* target_size,
const unsigned char* aad,
const int aad_size,
unsigned char* tag,
const int tag_size
) {
const EVP_CIPHER* evp_cipher = EVP_get_cipherbynid(nid);
if (!evp_cipher) return "nid invalid";
assert(encrypt == 0 || encrypt == 1);
assert(key != NULL);
assert(iv != NULL);
assert(source != NULL);
assert(target != NULL);
assert(aad != NULL);
assert(tag != NULL);
assert(cipher_valid_key_size(evp_cipher, key_size));
assert(cipher_valid_iv_size(evp_cipher, iv_size));
assert(source_size >= 0);
assert(*target_size >= 0);
int aead = cipher_aead(evp_cipher);
assert(cipher_valid_aad_size(evp_cipher, aad_size));
assert(cipher_valid_tag_size(evp_cipher, tag_size));
// Initialize the context without setting the key or IV:
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx) return "allocation failed";
if (!EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, encrypt)) {
EVP_CIPHER_CTX_free(ctx);
return "initialization failed";
}
// Disable padding to prevent an accidental padding oracle:
// https://blog.cloudflare.com/padding-oracles-and-the-decline-of-
// cbc-mode-ciphersuites/
if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) {
EVP_CIPHER_CTX_free(ctx);
return "set padding failed";
}
// Set the tag length only for OCB before encryption (and decryption):
if (aead && EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_OCB_MODE) {
// "In OCB mode, calling this before encryption with tag set to NULL sets
// the tag length. If this is not called prior to encryption, a default tag
// length is used."
// Contrary to the OpenSSL docs, this is also necessary before decryption.
// https://github.com/openssl/openssl/issues/8331
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_size, NULL)) {
EVP_CIPHER_CTX_free(ctx);
return "tagSize invalid";
}
}
// Set the tag and tag length before decryption:
if (aead && !encrypt) {
// GCM and OCB:
// "Sets the expected tag to taglen bytes from tag. The tag length can only
// be set before specifying an IV. taglen must be between 1 and 16
// inclusive.
//
// For GCM, this call is only valid when decrypting data.
// For OCB, this call is valid when decrypting data to set the expected tag,
// and before encryption to set the desired tag length.
// For OCB AES, the default tag length is 16 (i.e. 128 bits). It is also the
// maximum tag length for OCB."
// ChaCha20-Poly1305:
// "Sets the expected tag to taglen bytes from tag. The tag length can only
// be set before specifying an IV. taglen must be between 1 and 16
// (POLY1305_BLOCK_SIZE) inclusive. This call is only valid when decrypting
// data."
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, tag_size, tag)) {
EVP_CIPHER_CTX_free(ctx);
return "tag initialization failed";
}
}
// Assert key and IV length:
assert(key_size == EVP_CIPHER_key_length(evp_cipher));
assert(iv_size == EVP_CIPHER_iv_length(evp_cipher));
// Set the key and IV:
// "The operation performed depends on the value of the enc parameter. It
// should be set to 1 for encryption, 0 for decryption and -1 to leave the
// value unchanged (the actual value of 'enc' being supplied in a previous
// call)."
if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, -1)) {
EVP_CIPHER_CTX_free(ctx);
return "key and iv initialization failed";
}
// Set the additional authenticated data when encrypting and decrypting:
if (aead && aad_size > 0) {
// "To specify additional authenticated data (AAD), a call to
// EVP_CipherUpdate(), EVP_EncryptUpdate() or EVP_DecryptUpdate() should be
// made with the output parameter out set to NULL."
int aad_update_size = 0;
if (!EVP_CipherUpdate(ctx, NULL, &aad_update_size, aad, aad_size)) {
EVP_CIPHER_CTX_free(ctx);
return "aad initialization failed";
}
// OCB rounds outlen down to the nearest multiple of block size:
// https://github.com/openssl/openssl/issues/8310
const int block_size = EVP_CIPHER_block_size(evp_cipher);
assert(aad_update_size == (aad_size / block_size) * block_size);
}
// Update and finalize:
int target_offset = 0;
int update_size = 0;
int final_size = 0;
if (!EVP_CipherUpdate(ctx, target, &update_size, source, source_size)) {
EVP_CIPHER_CTX_free(ctx);
return "update failed";
}
assert(update_size >= 0);
assert(update_size <= INT_MAX - target_offset);
target_offset += update_size;
if (!EVP_CipherFinal_ex(ctx, target + target_offset, &final_size)) {
EVP_CIPHER_CTX_free(ctx);
if (aead && !encrypt) {
// "When decrypting, the return value of EVP_CipherFinal() indicates if
// the operation was successful. If it does not indicate success, the
// authentication operation has failed and any output data MUST NOT be
// used as it is corrupted."
return E_CORRUPT;
}
return "finalization failed";
}
assert(final_size >= 0);
assert(final_size <= INT_MAX - target_offset);
target_offset += final_size;
// Detect an out-of-bounds write in case it ever happens (it never should):
// We already do range checks before writing to buffers to prevent this.
// This is defense in depth.
assert(target_offset <= *target_size);
// Set target_size to what was actually written:
*target_size = target_offset;
// Get the tag after encrypting:
if (aead && encrypt) {
// GCM and OCB:
// "Writes taglen bytes of the tag value to the buffer indicated by tag.
// This call can only be made when encrypting data and after all data has
// been processed (e.g. after an EVP_EncryptFinal() call).
//
// For OCB mode, the taglen must either be 16 or the value previously set
// via EVP_CTRL_OCB_SET_TAGLEN."
// ChaCha20-Poly1305:
// "taglen specified here must be 16 (POLY1305_BLOCK_SIZE, i.e. 128-bits) or
// less."
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, tag_size, tag)) {
EVP_CIPHER_CTX_free(ctx);
return "tag finalization failed";
};
}
EVP_CIPHER_CTX_free(ctx);
return NULL;
}
static const char* execute_hash(
const int nid,
const unsigned char* source,
const int source_size,
unsigned char* target
) {
const EVP_MD* evp_md = EVP_get_digestbynid(nid);
if (!evp_md) return "nid invalid";
assert(source != NULL);
assert(target != NULL);
assert(source_size >= 0);
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (!ctx) return "allocation failed";
if (!EVP_DigestInit_ex(ctx, evp_md, NULL)) {
EVP_MD_CTX_free(ctx);
return "initialization failed";
}
if (!EVP_DigestUpdate(ctx, source, source_size)) {
EVP_MD_CTX_free(ctx);
return "update failed";
}
if (!EVP_DigestFinal_ex(ctx, target, NULL)) {
EVP_MD_CTX_free(ctx);
return "finalization failed";
}
EVP_MD_CTX_free(ctx);
return NULL;
}
static const char* execute_hmac(
const int nid,
const unsigned char* key,
const int key_size,
const unsigned char* source,
const int source_size,
unsigned char* target
) {
const EVP_MD* evp_md = EVP_get_digestbynid(nid);
if (!evp_md) return "nid invalid";
assert(key != NULL);
assert(source != NULL);
assert(target != NULL);
assert(key_size >= 0);
assert(source_size >= 0);
HMAC_CTX *ctx = HMAC_CTX_new();
if (!ctx) return "allocation failed";
if (!HMAC_Init_ex(ctx, key, key_size, evp_md, NULL)) {
HMAC_CTX_free(ctx);
return "initialization failed";
}
if (!HMAC_Update(ctx, source, source_size)) {
HMAC_CTX_free(ctx);
return "update failed";
}
if (!HMAC_Final(ctx, target, NULL)) {
HMAC_CTX_free(ctx);
return "finalization failed";
}
HMAC_CTX_free(ctx);
return NULL;
}
static int range(
napi_env env,
const int offset,
const int size,
const int length,
const char* error
) {
assert(offset >= 0);
assert(size >= 0);
assert(length >= 0);
// We must avoid undefined behavior from signed overflow before testing range:
if (size > INT_MAX - offset) {
napi_throw_error(env, NULL, error);
return 0;
}
// Signed overflow on some compilers may wrap, assert as last line of defense:
assert(offset + size >= 0);
if (offset + size > length) {
napi_throw_error(env, NULL, error);
return 0;
}
return 1;
}
struct task_data {
int flags;
int nid;
int encrypt;
unsigned char* key;
unsigned char* iv;
unsigned char* source;
unsigned char* target;
unsigned char* aad;
unsigned char* tag;
int key_size;
int iv_size;
int source_size;
int target_size;
int aad_size;
int tag_size;
napi_ref ref_key;
napi_ref ref_iv;
napi_ref ref_source;
napi_ref ref_target;
napi_ref ref_aad;
napi_ref ref_tag;
napi_ref ref_callback;
napi_async_work async_work;
const char* error;
};
void task_execute(napi_env env, void* data) {
struct task_data* task = data;
assert(task->flags > 0);
if (task->flags & FLAG_CIPHER) {
task->error = execute_cipher(
task->nid,
task->encrypt,
task->key,
task->key_size,
task->iv,
task->iv_size,
task->source,
task->source_size,
task->target,
&task->target_size,
task->aad,
task->aad_size,
task->tag,
task->tag_size
);
} else if (task->flags & FLAG_HASH) {
task->error = execute_hash(
task->nid,
task->source,
task->source_size,
task->target
);
} else if (task->flags & FLAG_HMAC) {
task->error = execute_hmac(
task->nid,
task->key,
task->key_size,
task->source,
task->source_size,
task->target
);
} else {
printf("unrecognized task->flags=%i\n", task->flags);
abort();
}
}
void task_complete(napi_env env, napi_status status, void* data) {
struct task_data* task = data;
if (status == napi_cancelled) {
task->error = E_CANCELLED;
} else {
assert(status == napi_ok);
}
int argc = 0;
napi_value argv[2];
if (task->error) {
argc = 1;
napi_value message;
OK(napi_create_string_utf8(env, task->error, NAPI_AUTO_LENGTH, &message));
OK(napi_create_error(env, NULL, message, &argv[0]));
} else {
argc = 2;
OK(napi_get_undefined(env, &argv[0]));
OK(napi_create_int64(env, task->target_size, &argv[1]));
}
napi_value scope;
OK(napi_get_global(env, &scope));
napi_value callback;
OK(napi_get_reference_value(env, task->ref_callback, &callback));
// Do not assert the return status of napi_call_function():
// If the callback throws then the return status will not be napi_ok.
napi_call_function(env, scope, callback, argc, argv, NULL);
if (task->ref_key) OK(napi_delete_reference(env, task->ref_key));
if (task->ref_iv) OK(napi_delete_reference(env, task->ref_iv));
assert(task->ref_source != NULL);
assert(task->ref_target != NULL);
OK(napi_delete_reference(env, task->ref_source));
OK(napi_delete_reference(env, task->ref_target));
if (task->ref_aad) OK(napi_delete_reference(env, task->ref_aad));
if (task->ref_tag) OK(napi_delete_reference(env, task->ref_tag));
assert(task->ref_callback != NULL);
assert(task->async_work != NULL);
OK(napi_delete_reference(env, task->ref_callback));
OK(napi_delete_async_work(env, task->async_work));
free(task);
task = NULL;
}
static napi_value task_create(
napi_env env,
int flags,
int nid,
int encrypt,
unsigned char* key,
unsigned char* iv,
unsigned char* source,
unsigned char* target,
unsigned char* aad,
unsigned char* tag,
int key_size,
int iv_size,
int source_size,
int target_size,
int aad_size,
int tag_size,
napi_value ref_key,
napi_value ref_iv,
napi_value ref_source,
napi_value ref_target,
napi_value ref_aad,
napi_value ref_tag,
napi_value ref_callback
) {
napi_valuetype callback_type;
OK(napi_typeof(env, ref_callback, &callback_type));
if (callback_type != napi_function) THROW(env, E_CALLBACK);
assert(flags > 0);
assert(encrypt == 0 || encrypt == 1);
assert(source != NULL);
assert(target != NULL);
assert(key_size >= 0);
assert(iv_size >= 0);
assert(source_size >= 0);
assert(target_size >= 0);
assert(aad_size >= 0);
assert(tag_size >= 0);
assert(ref_source != NULL);
assert(ref_target != NULL);
assert(ref_callback != NULL);
struct task_data* task = calloc(1, sizeof(struct task_data));
if (!task) THROW(env, E_OOM);
task->flags = flags;
task->nid = nid;
task->encrypt = encrypt;
task->key = key;
task->iv = iv;
task->source = source;
task->target = target;
task->aad = aad;
task->tag = tag;
task->key_size = key_size;
task->iv_size = iv_size;
task->source_size = source_size;
task->target_size = target_size;
task->aad_size = aad_size;
task->tag_size = tag_size;
assert(task->ref_key == NULL);
assert(task->ref_iv == NULL);
assert(task->ref_source == NULL);
assert(task->ref_target == NULL);
assert(task->ref_aad == NULL);
assert(task->ref_tag == NULL);
assert(task->ref_callback == NULL);
assert(task->error == NULL);
if (ref_key) OK(napi_create_reference(env, ref_key, 1, &task->ref_key));
if (ref_iv) OK(napi_create_reference(env, ref_iv, 1, &task->ref_iv));
OK(napi_create_reference(env, ref_source, 1, &task->ref_source));
OK(napi_create_reference(env, ref_target, 1, &task->ref_target));
if (ref_aad) OK(napi_create_reference(env, ref_aad, 1, &task->ref_aad));
if (ref_tag) OK(napi_create_reference(env, ref_tag, 1, &task->ref_tag));
OK(napi_create_reference(env, ref_callback, 1, &task->ref_callback));
napi_value name;
OK(napi_create_string_utf8(env, RESOURCE_NAME, NAPI_AUTO_LENGTH, &name));
OK(napi_create_async_work(
env,
NULL,
name,
task_execute,
task_complete,
task,
&task->async_work
));
OK(napi_queue_async_work(env, task->async_work));
return NULL;
}
static napi_value cipher(napi_env env, napi_callback_info info) {
size_t argc = 20;
napi_value argv[20];
OK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
if (argc != 19 && argc != 20) THROW(env, E_ARGUMENTS);
char algorithm[32];
int encrypt = 0;
unsigned char* key = NULL;
unsigned char* iv = NULL;
unsigned char* source = NULL;
unsigned char* target = NULL;
unsigned char* aad = NULL;
unsigned char* tag = NULL;
int key_length = 0;
int iv_length = 0;
int source_length = 0;
int target_length = 0;
int aad_length = 0;
int tag_length = 0;
int key_offset = 0;
int iv_offset = 0;
int source_offset = 0;
int target_offset = 0;
int aad_offset = 0;
int tag_offset = 0;
int key_size = 0;
int iv_size = 0;
int source_size = 0;
int target_size = 0;
int aad_size = 0;
int tag_size = 0;
if (!arg_str(env, argv[0], algorithm, 32, E_ALGORITHM)) return NULL;
const EVP_CIPHER* evp_cipher = EVP_get_cipherbyname(algorithm);
if (!evp_cipher) THROW(env, E_ALGORITHM_UNKNOWN);
if (!cipher_supported(evp_cipher)) THROW(env, E_ALGORITHM_DISABLED);
// We avoid EVP_CIPHER_type() since this returns `NID_undef` for some ciphers:
int nid = EVP_CIPHER_nid(evp_cipher);
assert(nid != NID_undef);
if (!arg_int(env, argv[1], &encrypt, E_ENCRYPT)) return NULL;
if (encrypt != 0 && encrypt != 1) THROW(env, E_ENCRYPT);
if (
!arg_buf(env, argv[2], &key, &key_length, E_KEY) ||
!arg_int(env, argv[3], &key_offset, E_KEY_OFFSET) ||
!arg_int(env, argv[4], &key_size, E_KEY_SIZE) ||
!arg_buf(env, argv[5], &iv, &iv_length, E_IV) ||
!arg_int(env, argv[6], &iv_offset, E_IV_OFFSET) ||
!arg_int(env, argv[7], &iv_size, E_IV_SIZE) ||
!arg_buf(env, argv[8], &source, &source_length, E_SOURCE) ||
!arg_int(env, argv[9], &source_offset, E_SOURCE_OFFSET) ||
!arg_int(env, argv[10], &source_size, E_SOURCE_SIZE) ||
!arg_buf(env, argv[11], &target, &target_length, E_TARGET) ||
!arg_int(env, argv[12], &target_offset, E_TARGET_OFFSET) ||
!arg_buf(env, argv[13], &aad, &aad_length, E_AAD) ||
!arg_int(env, argv[14], &aad_offset, E_AAD_OFFSET) ||
!arg_int(env, argv[15], &aad_size, E_AAD_SIZE) ||
!arg_buf(env, argv[16], &tag, &tag_length, E_TAG) ||
!arg_int(env, argv[17], &tag_offset, E_TAG_OFFSET) ||
!arg_int(env, argv[18], &tag_size, E_TAG_SIZE)
) {
return NULL;
}
if (
!range(env, key_offset, key_size, key_length, E_KEY_RANGE) ||
!range(env, iv_offset, iv_size, iv_length, E_IV_RANGE) ||
!range(env, source_offset, source_size, source_length, E_SOURCE_RANGE)
) {
return NULL;
}
target_size = cipher_target_size(evp_cipher, encrypt, source_size);
assert(target_size >= 0);
if (
!range(env, target_offset, target_size, target_length, E_TARGET_RANGE) ||
!range(env, aad_offset, aad_size, aad_length, E_AAD_RANGE) ||
!range(env, tag_offset, tag_size, tag_length, E_TAG_RANGE)
) {
return NULL;
}
if (!cipher_valid_key_size(evp_cipher, key_size)) THROW(env, E_KEY_INVALID);
if (!cipher_valid_iv_size(evp_cipher, iv_size)) THROW(env, E_IV_INVALID);
if (!cipher_valid_aad_size(evp_cipher, aad_size)) THROW(env, E_AAD_INVALID);
if (!cipher_valid_tag_size(evp_cipher, tag_size)) THROW(env, E_TAG_INVALID);
key += key_offset;
iv += iv_offset;
source += source_offset;
target += target_offset;
aad += aad_offset;
tag += tag_offset;
if (argc == 19) {
const char* error = execute_cipher(
nid,
encrypt,
key,
key_size,
iv,
iv_size,
source,
source_size,
target,
&target_size,
aad,
aad_size,
tag,
tag_size
);
if (error) THROW(env, error);
napi_value result;
OK(napi_create_int64(env, target_size, &result));
return result;
}
return task_create(
env, // env
FLAG_CIPHER, // flags
nid, // nid
encrypt, // encrypt
key, // key
iv, // iv
source, // source
target, // target
aad, // aad
tag, // tag
key_size, // key_size
iv_size, // iv_size
source_size, // source_size
target_size, // target_size
aad_size, // aad_size
tag_size, // tag_size
argv[2], // ref_key
argv[5], // ref_iv
argv[8], // ref_source
argv[11], // ref_target
argv[13], // ref_aad
argv[16], // ref_tag
argv[19] // ref_callback
);
}
static napi_value hash(napi_env env, napi_callback_info info) {
size_t argc = 7;
napi_value argv[7];
OK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
if (argc != 6 && argc != 7) THROW(env, E_ARGUMENTS);
char algorithm[32];
unsigned char* source = NULL;
unsigned char* target = NULL;
int source_length = 0;
int target_length = 0;
int source_offset = 0;
int target_offset = 0;
int source_size = 0;
int target_size = 0;
if (!arg_str(env, argv[0], algorithm, 32, E_ALGORITHM)) return NULL;
const EVP_MD* evp_md = EVP_get_digestbyname(algorithm);
if (!evp_md) THROW(env, E_ALGORITHM_UNKNOWN);
int nid = EVP_MD_type(evp_md);
assert(nid != NID_undef);
target_size = EVP_MD_size(evp_md);
assert(target_size > 0);
if (
!arg_buf(env, argv[1], &source, &source_length, E_SOURCE) ||
!arg_int(env, argv[2], &source_offset, E_SOURCE_OFFSET) ||
!arg_int(env, argv[3], &source_size, E_SOURCE_SIZE) ||
!arg_buf(env, argv[4], &target, &target_length, E_TARGET) ||
!arg_int(env, argv[5], &target_offset, E_TARGET_OFFSET) ||
!range(env, source_offset, source_size, source_length, E_SOURCE_RANGE) ||
!range(env, target_offset, target_size, target_length, E_TARGET_RANGE)
) {
return NULL;
}
source += source_offset;
target += target_offset;
if (argc == 6) {
const char* error = execute_hash(nid, source, source_size, target);
if (error) THROW(env, error);
napi_value result;
OK(napi_create_int64(env, target_size, &result));
return result;
}
return task_create(
env, // env
FLAG_HASH, // flags
nid, // nid
0, // encrypt
NULL, // key
NULL, // iv
source, // source
target, // target
NULL, // aad
NULL, // tag
0, // key_size
0, // iv_size
source_size, // source_size
target_size, // target_size
0, // aad_size
0, // tag_size
NULL, // ref_key
NULL, // ref_iv
argv[1], // ref_source
argv[4], // ref_target
NULL, // ref_aad
NULL, // ref_tag
argv[6] // ref_callback
);
}
static napi_value hmac(napi_env env, napi_callback_info info) {
size_t argc = 10;
napi_value argv[10];
OK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
if (argc != 9 && argc != 10) THROW(env, E_ARGUMENTS);
char algorithm[32];
unsigned char* key = NULL;
unsigned char* source = NULL;
unsigned char* target = NULL;
int key_length = 0;
int source_length = 0;
int target_length = 0;
int key_offset = 0;
int source_offset = 0;
int target_offset = 0;
int key_size = 0;
int source_size = 0;
int target_size = 0;
if (!arg_str(env, argv[0], algorithm, 32, E_ALGORITHM)) return NULL;
const EVP_MD* evp_md = EVP_get_digestbyname(algorithm);
if (!evp_md) THROW(env, E_ALGORITHM_UNKNOWN);
int nid = EVP_MD_type(evp_md);
assert(nid != NID_undef);
target_size = EVP_MD_size(evp_md);
assert(target_size > 0);
if (
!arg_buf(env, argv[1], &key, &key_length, E_KEY) ||
!arg_int(env, argv[2], &key_offset, E_KEY_OFFSET) ||
!arg_int(env, argv[3], &key_size, E_KEY_SIZE) ||
!arg_buf(env, argv[4], &source, &source_length, E_SOURCE) ||
!arg_int(env, argv[5], &source_offset, E_SOURCE_OFFSET) ||
!arg_int(env, argv[6], &source_size, E_SOURCE_SIZE) ||
!arg_buf(env, argv[7], &target, &target_length, E_TARGET) ||
!arg_int(env, argv[8], &target_offset, E_TARGET_OFFSET) ||
!range(env, key_offset, key_size, key_length, E_KEY_RANGE) ||
!range(env, source_offset, source_size, source_length, E_SOURCE_RANGE) ||
!range(env, target_offset, target_size, target_length, E_TARGET_RANGE)
) {
return NULL;
}
key += key_offset;
source += source_offset;
target += target_offset;
if (argc == 9) {
const char* error = execute_hmac(
nid,
key,
key_size,
source,
source_size,
target
);
if (error) THROW(env, error);
napi_value result;
OK(napi_create_int64(env, target_size, &result));
return result;
}