-
Notifications
You must be signed in to change notification settings - Fork 41
/
bcrypto.cc
12717 lines (10113 loc) · 384 KB
/
bcrypto.cc
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
/**
* bcrypto.cc - fast native bindings to crypto functions
* Copyright (c) 2016-2020, Christopher Jeffrey (MIT License)
* https://github.com/bcoin-org/bcrypto
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <node_api.h>
#include <torsion/aead.h>
#include <torsion/chacha20.h>
#include <torsion/cipher.h>
#include <torsion/drbg.h>
#include <torsion/dsa.h>
#include <torsion/ecc.h>
#include <torsion/hash.h>
#include <torsion/kdf.h>
#include <torsion/poly1305.h>
#include <torsion/rsa.h>
#include <torsion/salsa20.h>
#include <torsion/secretbox.h>
#include <torsion/siphash.h>
#include <torsion/util.h>
#include "base58/base58.h"
#include "bech32/bech32.h"
#include "cash32/cash32.h"
#include "murmur3/murmur3.h"
#ifdef BCRYPTO_USE_SECP256K1
#include "secp256k1/include/secp256k1.h"
#include "secp256k1/include/secp256k1_ecdh.h"
#include "secp256k1/include/secp256k1_elligator.h"
#include "secp256k1/include/secp256k1_extra.h"
#include "secp256k1/include/secp256k1_recovery.h"
#include "secp256k1/include/secp256k1_schnorrleg.h"
#ifdef BCRYPTO_USE_SECP256K1_LATEST
#include "secp256k1/include/secp256k1_schnorrsig.h"
#endif
#include "secp256k1/contrib/lax_der_parsing.h"
#endif
#define CHECK(expr) do { \
if (!(expr)) \
bcrypto_assert_fail(__FILE__, __LINE__, #expr); \
} while (0)
#define ENTROPY_SIZE 32
#define JS_ERR_CONTEXT "Could not create context."
#define JS_ERR_FINAL "Could not finalize context."
#define JS_ERR_SIGNATURE "Invalid signature."
#define JS_ERR_SIGNATURE_SIZE "Invalid signature size."
#define JS_ERR_PRIVKEY "Invalid private key."
#define JS_ERR_PRIVKEY_SIZE "Invalid private key size."
#define JS_ERR_PUBKEY "Invalid public key."
#define JS_ERR_PUBKEY_SIZE "Invalid public key size."
#define JS_ERR_SCALAR "Invalid scalar."
#define JS_ERR_SCALAR_SIZE "Invalid scalar size."
#define JS_ERR_POINT "Invalid point."
#define JS_ERR_POINT_SIZE "Invalid point size."
#define JS_ERR_SIGN "Could not sign."
#define JS_ERR_KEY "Invalid key."
#define JS_ERR_KEY_SIZE "Invalid key size."
#define JS_ERR_IV "Invalid IV."
#define JS_ERR_IV_SIZE "Invalid IV size."
#define JS_ERR_NONCE "Invalid nonce."
#define JS_ERR_NONCE_SIZE "Invalid nonce size."
#define JS_ERR_SECRET_SIZE "Invalid secret size."
#define JS_ERR_TAG "Invalid tag."
#define JS_ERR_TAG_SIZE "Invalid tag size."
#define JS_ERR_ENTROPY "Invalid entropy."
#define JS_ERR_ENTROPY_SIZE "Invalid entropy size."
#define JS_ERR_PREIMAGE "Invalid preimage."
#define JS_ERR_PREIMAGE_SIZE "Invalid preimage size."
#define JS_ERR_RECOVERY_PARAM "Invalid recovery parameter."
#define JS_ERR_NO_SCHNORR "Schnorr is not supported."
#define JS_ERR_RANDOM "Randomization failed."
#define JS_ERR_PREFIX_SIZE "Invalid prefix length."
#define JS_ERR_GENERATE "Could not generate key."
#define JS_ERR_ENCRYPT "Could not encrypt."
#define JS_ERR_DECRYPT "Could not decrypt."
#define JS_ERR_VEIL "Could not veil."
#define JS_ERR_UNVEIL "Could not unveil."
#define JS_ERR_PARAMS "Invalid params."
#define JS_ERR_INIT "Context is not initialized."
#define JS_ERR_STATE "Invalid state."
#define JS_ERR_ENCODE "Encoding failed."
#define JS_ERR_DECODE "Decoding failed."
#define JS_ERR_OUTPUT_SIZE "Invalid output size."
#define JS_ERR_NODE_SIZE "Invalid node sizes."
#define JS_ERR_DERIVE "Derivation failed."
#define JS_ERR_MSG_SIZE "Invalid message size."
#define JS_ERR_ALLOC "Allocation failed."
#define JS_ERR_ARG "Invalid argument."
#define JS_ERR_OPT "Could not set option."
#define JS_ERR_GET "Could not get value."
#define JS_ERR_CRYPT "Could not encipher."
#define JS_THROW(msg) do { \
CHECK(napi_throw_error(env, NULL, (msg)) == napi_ok); \
return NULL; \
} while (0)
#define JS_ASSERT(cond, msg) if (!(cond)) JS_THROW(msg)
#define SCRATCH_SIZE 64
/*
* Structs
*/
typedef struct bcrypto_blake2b_s {
blake2b_t ctx;
int started;
} bcrypto_blake2b_t;
typedef struct bcrypto_blake2s_s {
blake2s_t ctx;
int started;
} bcrypto_blake2s_t;
typedef struct bcrypto_chacha20_s {
chacha20_t ctx;
int started;
} bcrypto_chacha20_t;
typedef struct bcrypto_cipher_s {
cipher_stream_t ctx;
int type;
int mode;
int encrypt;
int started;
int has_tag;
} bcrypto_cipher_t;
typedef struct bcrypto_ctr_drbg_s {
ctr_drbg_t ctx;
uint32_t bits;
int derivation;
int started;
} bcrypto_ctr_drbg_t;
typedef struct bcrypto_mont_s {
mont_curve_t *ctx;
size_t scalar_size;
size_t scalar_bits;
size_t field_size;
size_t field_bits;
} bcrypto_mont_curve_t;
typedef struct bcrypto_edwards_s {
edwards_curve_t *ctx;
edwards_scratch_t *scratch;
size_t scalar_size;
size_t scalar_bits;
size_t field_size;
size_t field_bits;
size_t priv_size;
size_t pub_size;
size_t sig_size;
} bcrypto_edwards_curve_t;
typedef struct bcrypto_hash_s {
hash_t ctx;
int type;
int started;
} bcrypto_hash_t;
typedef struct bcrypto_hash_drbg_s {
hash_drbg_t ctx;
int type;
int started;
} bcrypto_hash_drbg_t;
typedef struct bcrypto_hmac_s {
hmac_t ctx;
int type;
int started;
} bcrypto_hmac_t;
typedef struct bcrypto_hmac_drbg_s {
hmac_drbg_t ctx;
int type;
int started;
} bcrypto_hmac_drbg_t;
typedef struct bcrypto_keccak_s {
keccak_t ctx;
int started;
} bcrypto_keccak_t;
typedef struct bcrypto_poly1305_s {
poly1305_t ctx;
int started;
} bcrypto_poly1305_t;
typedef struct bcrypto_rc4_s {
rc4_t ctx;
int started;
} bcrypto_rc4_t;
typedef struct bcrypto_salsa20_s {
salsa20_t ctx;
int started;
} bcrypto_salsa20_t;
#ifdef BCRYPTO_USE_SECP256K1
typedef struct bcrypto_secp256k1_s {
secp256k1_context *ctx;
secp256k1_scratch_space *scratch;
} bcrypto_secp256k1_t;
#endif
typedef struct bcrypto_wei_s {
wei_curve_t *ctx;
wei_scratch_t *scratch;
size_t scalar_size;
size_t scalar_bits;
size_t field_size;
size_t field_bits;
size_t sig_size;
size_t legacy_size;
size_t schnorr_size;
} bcrypto_wei_curve_t;
/*
* Assertions
*/
static void
bcrypto_assert_fail(const char *file, int line, const char *expr) {
fprintf(stderr, "%s:%d: Assertion `%s' failed.\n", file, line, expr);
fflush(stderr);
abort();
}
/*
* N-API Extras
*/
static napi_status
read_value_string_utf8(napi_env env, napi_value value,
char **str, size_t *length) {
char *buf;
size_t buflen;
napi_status status;
status = napi_get_value_string_utf8(env, value, NULL, 0, &buflen);
if (status != napi_ok)
return status;
buf = (char *)torsion_malloc(buflen + 1);
if (buf == NULL)
return napi_generic_failure;
status = napi_get_value_string_utf8(env,
value,
buf,
buflen + 1,
length);
if (status != napi_ok) {
torsion_free(buf);
return status;
}
CHECK(*length == buflen);
*str = buf;
return napi_ok;
}
static void
finalize_buffer(napi_env env, void *data, void *hint) {
torsion_free(data);
}
static napi_status
create_external_buffer(napi_env env, size_t length,
void *data, napi_value *result) {
const napi_node_version *version;
CHECK(napi_get_node_version(env, &version) == napi_ok);
/* Temporary until we figure out how to work around this bug:
https://github.com/nodejs/node/issues/32463 */
if (version->major >= 14) {
napi_status status = napi_create_buffer_copy(env, length, data,
NULL, result);
cleanse(data, length);
torsion_free(data);
return status;
}
return napi_create_external_buffer(env,
length,
data,
finalize_buffer,
NULL,
result);
}
/*
* AEAD
*/
static void
bcrypto_aead_destroy_(napi_env env, void *data, void *hint) {
cleanse(data, sizeof(aead_t));
torsion_free(data);
}
static napi_value
bcrypto_aead_create(napi_env env, napi_callback_info info) {
aead_t *ctx = (aead_t *)torsion_xmalloc(sizeof(aead_t));
napi_value handle;
ctx->mode = -1;
CHECK(napi_create_external(env,
ctx,
bcrypto_aead_destroy_,
NULL,
&handle) == napi_ok);
return handle;
}
static napi_value
bcrypto_aead_init(napi_env env, napi_callback_info info) {
napi_value argv[3];
size_t argc = 3;
const uint8_t *key, *iv;
size_t key_len, iv_len;
aead_t *ctx;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 3);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&key, &key_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[2], (void **)&iv, &iv_len) == napi_ok);
JS_ASSERT(key_len >= 32, JS_ERR_KEY_SIZE);
JS_ASSERT(iv_len == 8 || iv_len == 12 || iv_len == 16
|| iv_len == 24 || iv_len == 28 || iv_len == 32, JS_ERR_IV_SIZE);
aead_init(ctx, key, iv, iv_len);
return argv[0];
}
static napi_value
bcrypto_aead_aad(napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
const uint8_t *aad;
size_t aad_len;
aead_t *ctx;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 2);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&aad, &aad_len) == napi_ok);
JS_ASSERT(ctx->mode != -1, JS_ERR_INIT);
JS_ASSERT(ctx->mode == 0, JS_ERR_STATE);
aead_aad(ctx, aad, aad_len);
return argv[0];
}
static napi_value
bcrypto_aead_encrypt(napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
uint8_t *msg;
size_t msg_len;
aead_t *ctx;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 2);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&msg, &msg_len) == napi_ok);
JS_ASSERT(ctx->mode != -1, JS_ERR_INIT);
JS_ASSERT(ctx->mode == 0 || ctx->mode == 1, JS_ERR_STATE);
aead_encrypt(ctx, msg, msg, msg_len);
return argv[1];
}
static napi_value
bcrypto_aead_decrypt(napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
uint8_t *msg;
size_t msg_len;
aead_t *ctx;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 2);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&msg, &msg_len) == napi_ok);
JS_ASSERT(ctx->mode != -1, JS_ERR_INIT);
JS_ASSERT(ctx->mode == 0 || ctx->mode == 2, JS_ERR_STATE);
aead_decrypt(ctx, msg, msg, msg_len);
return argv[1];
}
static napi_value
bcrypto_aead_auth(napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
const uint8_t *msg;
size_t msg_len;
aead_t *ctx;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 2);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&msg, &msg_len) == napi_ok);
JS_ASSERT(ctx->mode != -1, JS_ERR_INIT);
JS_ASSERT(ctx->mode == 0 || ctx->mode == 3, JS_ERR_STATE);
aead_auth(ctx, msg, msg_len);
return argv[1];
}
static napi_value
bcrypto_aead_final(napi_env env, napi_callback_info info) {
napi_value argv[1];
size_t argc = 1;
uint8_t out[16];
aead_t *ctx;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 1);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
JS_ASSERT(ctx->mode != -1, JS_ERR_INIT);
aead_final(ctx, out);
CHECK(napi_create_buffer_copy(env, 16, out, NULL, &result) == napi_ok);
return result;
}
static napi_value
bcrypto_aead_destroy(napi_env env, napi_callback_info info) {
napi_value argv[1];
size_t argc = 1;
aead_t *ctx;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 1);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
ctx->mode = -1;
return argv[0];
}
static napi_value
bcrypto_aead_verify(napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
uint8_t mac[16];
const uint8_t *tag;
size_t tag_len;
aead_t *ctx;
napi_value result;
int ok;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 2);
CHECK(napi_get_value_external(env, argv[0], (void **)&ctx) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&tag, &tag_len) == napi_ok);
JS_ASSERT(ctx->mode != -1, JS_ERR_INIT);
JS_ASSERT(tag_len == 16, JS_ERR_TAG_SIZE);
aead_final(ctx, mac);
ok = aead_verify(mac, tag);
CHECK(napi_get_boolean(env, ok, &result) == napi_ok);
return result;
}
static napi_value
bcrypto_aead_static_encrypt(napi_env env, napi_callback_info info) {
napi_value argv[4];
size_t argc = 4;
uint8_t out[16];
const uint8_t *key, *iv, *aad;
size_t key_len, iv_len, aad_len;
uint8_t *msg;
size_t msg_len;
aead_t ctx;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 4);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&key, &key_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&iv, &iv_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[2], (void **)&msg, &msg_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[3], (void **)&aad, &aad_len) == napi_ok);
JS_ASSERT(key_len >= 32, JS_ERR_KEY_SIZE);
JS_ASSERT(iv_len == 8 || iv_len == 12 || iv_len == 16
|| iv_len == 24 || iv_len == 28 || iv_len == 32, JS_ERR_IV_SIZE);
aead_init(&ctx, key, iv, iv_len);
aead_aad(&ctx, aad, aad_len);
aead_encrypt(&ctx, msg, msg, msg_len);
aead_final(&ctx, out);
cleanse(&ctx, sizeof(aead_t));
CHECK(napi_create_buffer_copy(env, 16, out, NULL, &result) == napi_ok);
return result;
}
static napi_value
bcrypto_aead_static_decrypt(napi_env env, napi_callback_info info) {
napi_value argv[5];
size_t argc = 5;
uint8_t mac[16];
const uint8_t *key, *iv, *tag, *aad;
size_t key_len, iv_len, tag_len, aad_len;
uint8_t *msg;
size_t msg_len;
aead_t ctx;
napi_value result;
int ok;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 5);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&key, &key_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&iv, &iv_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[2], (void **)&msg, &msg_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[3], (void **)&tag, &tag_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[4], (void **)&aad, &aad_len) == napi_ok);
JS_ASSERT(key_len >= 32, JS_ERR_KEY_SIZE);
JS_ASSERT(iv_len == 8 || iv_len == 12 || iv_len == 16
|| iv_len == 24 || iv_len == 28 || iv_len == 32, JS_ERR_IV_SIZE);
JS_ASSERT(tag_len == 16, JS_ERR_TAG_SIZE);
aead_init(&ctx, key, iv, iv_len);
aead_aad(&ctx, aad, aad_len);
aead_decrypt(&ctx, msg, msg, msg_len);
aead_final(&ctx, mac);
cleanse(&ctx, sizeof(aead_t));
ok = aead_verify(mac, tag);
CHECK(napi_get_boolean(env, ok, &result) == napi_ok);
return result;
}
static napi_value
bcrypto_aead_static_auth(napi_env env, napi_callback_info info) {
napi_value argv[5];
size_t argc = 5;
uint8_t mac[16];
const uint8_t *key, *iv, *msg, *tag, *aad;
size_t key_len, iv_len, msg_len, tag_len, aad_len;
aead_t ctx;
napi_value result;
int ok;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 5);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&key, &key_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&iv, &iv_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[2], (void **)&msg, &msg_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[3], (void **)&tag, &tag_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[4], (void **)&aad, &aad_len) == napi_ok);
JS_ASSERT(key_len >= 32, JS_ERR_KEY_SIZE);
JS_ASSERT(iv_len == 8 || iv_len == 12 || iv_len == 16
|| iv_len == 24 || iv_len == 28 || iv_len == 32, JS_ERR_IV_SIZE);
JS_ASSERT(tag_len == 16, JS_ERR_TAG_SIZE);
aead_init(&ctx, key, iv, iv_len);
aead_aad(&ctx, aad, aad_len);
aead_auth(&ctx, msg, msg_len);
aead_final(&ctx, mac);
cleanse(&ctx, sizeof(aead_t));
ok = aead_verify(mac, tag);
CHECK(napi_get_boolean(env, ok, &result) == napi_ok);
return result;
}
/*
* Base58
*/
static napi_value
bcrypto_base58_encode(napi_env env, napi_callback_info info) {
napi_value argv[1];
size_t argc = 1;
char *out;
size_t out_len;
const uint8_t *data;
size_t data_len;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 1);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&data,
&data_len) == napi_ok);
JS_ASSERT(base58_encode(&out, &out_len, data, data_len), JS_ERR_ENCODE);
CHECK(napi_create_string_utf8(env, out, out_len, &result) == napi_ok);
torsion_free(out);
return result;
}
static napi_value
bcrypto_base58_decode(napi_env env, napi_callback_info info) {
napi_value argv[1];
size_t argc = 1;
uint8_t *out;
size_t out_len;
char *str;
size_t str_len;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 1);
CHECK(read_value_string_utf8(env, argv[0], &str, &str_len) == napi_ok);
if (!base58_decode(&out, &out_len, str, str_len)) {
torsion_free(str);
JS_THROW(JS_ERR_DECODE);
}
CHECK(create_external_buffer(env, out_len, out, &result) == napi_ok);
torsion_free(str);
return result;
}
static napi_value
bcrypto_base58_test(napi_env env, napi_callback_info info) {
napi_value argv[1];
size_t argc = 1;
char *str;
size_t str_len;
napi_value result;
int ok;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 1);
CHECK(read_value_string_utf8(env, argv[0], &str, &str_len) == napi_ok);
ok = base58_test(str, str_len);
CHECK(napi_get_boolean(env, ok, &result) == napi_ok);
torsion_free(str);
return result;
}
/*
* Bcrypt
*/
static napi_value
bcrypto_bcrypt_hash192(napi_env env, napi_callback_info info) {
napi_value argv[3];
size_t argc = 3;
uint8_t out[24];
uint32_t rounds;
const uint8_t *pass, *salt;
size_t pass_len, salt_len;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 3);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&pass,
&pass_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&salt,
&salt_len) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[2], &rounds) == napi_ok);
JS_ASSERT(rounds >= 4 && rounds <= 31, JS_ERR_DERIVE);
bcrypt_hash192(out, pass, pass_len, salt, salt_len, rounds);
CHECK(napi_create_buffer_copy(env, 24, out, NULL, &result) == napi_ok);
return result;
}
static napi_value
bcrypto_bcrypt_hash256(napi_env env, napi_callback_info info) {
napi_value argv[3];
size_t argc = 3;
uint8_t out[32];
uint32_t rounds;
const uint8_t *pass, *salt;
size_t pass_len, salt_len;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 3);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&pass,
&pass_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&salt,
&salt_len) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[2], &rounds) == napi_ok);
JS_ASSERT(rounds >= 4 && rounds <= 31, JS_ERR_DERIVE);
bcrypt_hash256(out, pass, pass_len, salt, salt_len, rounds);
CHECK(napi_create_buffer_copy(env, 32, out, NULL, &result) == napi_ok);
return result;
}
static napi_value
bcrypto_bcrypt_pbkdf(napi_env env, napi_callback_info info) {
napi_value argv[4];
size_t argc = 4;
uint8_t *out;
uint32_t rounds, out_len;
const uint8_t *pass, *salt;
size_t pass_len, salt_len;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 4);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&pass,
&pass_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&salt,
&salt_len) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[2], &rounds) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[3], &out_len) == napi_ok);
out = (uint8_t *)torsion_malloc(out_len);
if (out == NULL && out_len != 0)
goto fail;
if (!bcrypt_pbkdf(out, pass, pass_len, salt, salt_len, rounds, out_len))
goto fail;
CHECK(create_external_buffer(env, out_len, out, &result) == napi_ok);
return result;
fail:
torsion_free(out);
JS_THROW(JS_ERR_DERIVE);
return NULL;
}
typedef struct bcrypto_bcrypt_worker_s {
uint8_t *pass;
size_t pass_len;
uint8_t *salt;
size_t salt_len;
uint32_t rounds;
uint8_t *out;
uint32_t out_len;
const char *error;
napi_async_work work;
napi_deferred deferred;
} bcrypto_bcrypt_worker_t;
static void
bcrypto_bcrypt_execute_(napi_env env, void *data) {
bcrypto_bcrypt_worker_t *w = (bcrypto_bcrypt_worker_t *)data;
if (!bcrypt_pbkdf(w->out, w->pass, w->pass_len,
w->salt, w->salt_len,
w->rounds, w->out_len)) {
w->error = JS_ERR_DERIVE;
}
cleanse(w->pass, w->pass_len);
cleanse(w->salt, w->salt_len);
}
static void
bcrypto_bcrypt_complete_(napi_env env, napi_status status, void *data) {
bcrypto_bcrypt_worker_t *w = (bcrypto_bcrypt_worker_t *)data;
napi_value result, strval;
if (status != napi_ok)
w->error = JS_ERR_DERIVE;
if (w->error == NULL) {
CHECK(create_external_buffer(env, w->out_len, w->out, &result) == napi_ok);
CHECK(napi_resolve_deferred(env, w->deferred, result) == napi_ok);
} else {
CHECK(napi_create_string_utf8(env, w->error,
NAPI_AUTO_LENGTH, &strval) == napi_ok);
CHECK(napi_create_error(env, NULL, strval, &result) == napi_ok);
CHECK(napi_reject_deferred(env, w->deferred, result) == napi_ok);
torsion_free(w->out);
}
CHECK(napi_delete_async_work(env, w->work) == napi_ok);
torsion_free(w->pass);
torsion_free(w->salt);
torsion_free(w);
}
static napi_value
bcrypto_bcrypt_pbkdf_async(napi_env env, napi_callback_info info) {
bcrypto_bcrypt_worker_t *worker =
(bcrypto_bcrypt_worker_t *)torsion_xmalloc(sizeof(bcrypto_bcrypt_worker_t));
napi_value argv[4];
size_t argc = 4;
uint8_t *out;
uint32_t rounds, out_len;
const uint8_t *pass, *salt;
size_t pass_len, salt_len;
napi_value workname, result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 4);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&pass,
&pass_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&salt,
&salt_len) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[2], &rounds) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[3], &out_len) == napi_ok);
out = (uint8_t *)torsion_malloc(out_len);
if (out == NULL && out_len != 0) {
torsion_free(worker);
JS_THROW(JS_ERR_DERIVE);
}
worker->pass = (uint8_t *)torsion_xmalloc(pass_len);
worker->pass_len = pass_len;
worker->salt = (uint8_t *)torsion_xmalloc(salt_len);
worker->salt_len = salt_len;
worker->rounds = rounds;
worker->out = out;
worker->out_len = out_len;
worker->error = NULL;
memcpy(worker->pass, pass, pass_len);
memcpy(worker->salt, salt, salt_len);
CHECK(napi_create_string_utf8(env, "bcrypto:bcrypt_pbkdf",
NAPI_AUTO_LENGTH, &workname) == napi_ok);
CHECK(napi_create_promise(env, &worker->deferred, &result) == napi_ok);
CHECK(napi_create_async_work(env,
NULL,
workname,
bcrypto_bcrypt_execute_,
bcrypto_bcrypt_complete_,
worker,
&worker->work) == napi_ok);
CHECK(napi_queue_async_work(env, worker->work) == napi_ok);
return result;
}
static napi_value
bcrypto_bcrypt_derive(napi_env env, napi_callback_info info) {
napi_value argv[4];
size_t argc = 4;
uint8_t out[31];
uint32_t rounds, minor;
const uint8_t *pass, *salt;
size_t pass_len, salt_len;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 4);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&pass,
&pass_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&salt,
&salt_len) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[2], &rounds) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[3], &minor) == napi_ok);
JS_ASSERT(bcrypt_derive(out, pass, pass_len, salt, salt_len, rounds, minor),
JS_ERR_DERIVE);
CHECK(napi_create_buffer_copy(env, 31, out, NULL, &result) == napi_ok);
return result;
}
static napi_value
bcrypto_bcrypt_generate(napi_env env, napi_callback_info info) {
napi_value argv[4];
size_t argc = 4;
char out[62];
uint32_t rounds, minor;
const uint8_t *pass, *salt;
size_t pass_len, salt_len;
napi_value result;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 4);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&pass,
&pass_len) == napi_ok);
CHECK(napi_get_buffer_info(env, argv[1], (void **)&salt,
&salt_len) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[2], &rounds) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[3], &minor) == napi_ok);
JS_ASSERT(bcrypt_generate(out, pass, pass_len, salt, salt_len, rounds, minor),
JS_ERR_DERIVE);
CHECK(napi_create_string_utf8(env, out, NAPI_AUTO_LENGTH,
&result) == napi_ok);
return result;
}
static napi_value
bcrypto_bcrypt_generate_with_salt64(napi_env env, napi_callback_info info) {
napi_value argv[4];
size_t argc = 4;
char out[62];
uint32_t rounds, minor;
const uint8_t *pass;
char salt[23 + 1];
size_t pass_len, salt_len;
napi_value result;
int ok;
CHECK(napi_get_cb_info(env, info, &argc, argv, NULL, NULL) == napi_ok);
CHECK(argc == 4);
CHECK(napi_get_buffer_info(env, argv[0], (void **)&pass,
&pass_len) == napi_ok);
CHECK(napi_get_value_string_utf8(env, argv[1],
salt, sizeof(salt), &salt_len) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[2], &rounds) == napi_ok);
CHECK(napi_get_value_uint32(env, argv[3], &minor) == napi_ok);
JS_ASSERT(salt_len != sizeof(salt) - 1, JS_ERR_DERIVE);
ok = bcrypt_generate_with_salt64(out, pass, pass_len, salt, rounds, minor);
JS_ASSERT(ok, JS_ERR_DERIVE);
CHECK(napi_create_string_utf8(env, out, NAPI_AUTO_LENGTH,
&result) == napi_ok);
return result;
}
static napi_value
bcrypto_bcrypt_verify(napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
const uint8_t *pass;
char record[62 + 1];
size_t pass_len, record_len;