This repository has been archived by the owner on May 29, 2020. It is now read-only.
forked from paixaop/node-sodium
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sodium.cc
1681 lines (1399 loc) · 49.7 KB
/
sodium.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
/**
* Node Native Module for Lib Sodium
*
* @Author Pedro Paixao
* @email paixaop at gmail dot com
* @License MIT
*/
#include <node.h>
#include <node_buffer.h>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <sstream>
#include <nan.h>
#include "sodium.h"
using namespace node;
using namespace v8;
// get handle to the global object
Local<Object> globalObj = Nan::GetCurrentContext()->Global();
// Retrieve the buffer constructor function
Local<Function> bufferConstructor =
Local<Function>::Cast(globalObj->Get(Nan::New<String>("Buffer").ToLocalChecked()));
// Check if a function argument is a node Buffer. If not throw V8 exception
#define ARG_IS_BUFFER(i,msg) \
if (!Buffer::HasInstance(info[i])) { \
std::ostringstream oss; \
oss << "argument " << msg << " must be a buffer"; \
return Nan::ThrowError(oss.str().c_str()); \
}
// Create a new buffer, and get a pointer to it
#define NEW_BUFFER_AND_PTR(name, size) \
Local<Object> name = Nan::NewBuffer(size).ToLocalChecked(); \
unsigned char* name ## _ptr = (unsigned char*)Buffer::Data(name)
#define GET_ARG_AS(i, NAME, TYPE) \
ARG_IS_BUFFER(i,#NAME); \
TYPE NAME = (TYPE) Buffer::Data(info[i]->ToObject()); \
unsigned long long NAME ## _size = Buffer::Length(info[i]->ToObject()); \
if( NAME ## _size == 0 ) { \
std::ostringstream oss; \
oss << "argument " << #NAME << " length cannot be zero" ; \
return Nan::ThrowError(oss.str().c_str()); \
}
#define GET_ARG_AS_LEN(i, NAME, MAXLEN, TYPE) \
GET_ARG_AS(i, NAME, TYPE); \
if( NAME ## _size != MAXLEN ) { \
std::ostringstream oss; \
oss << "argument " << #NAME << " must be " << MAXLEN << " bytes long" ; \
return Nan::ThrowError(oss.str().c_str()); \
}
#define GET_ARG_AS_UCHAR(i, NAME) \
GET_ARG_AS(i, NAME, unsigned char*)
#define GET_ARG_AS_UCHAR_LEN(i, NAME, MAXLEN) \
GET_ARG_AS_LEN(i, NAME, MAXLEN, unsigned char*)
#define GET_ARG_AS_VOID(i, NAME) \
GET_ARG_AS(i, NAME, void*)
#define GET_ARG_AS_VOID_LEN(i, NAME, MAXLEN) \
GET_ARG_AS_LEN(i, NAME, MAXLEN, void*)
#define NUMBER_OF_MANDATORY_ARGS(n, message) \
if (info.Length() < (n)) { \
return Nan::ThrowError(message); \
}
#define TO_REAL_BUFFER(slowBuffer, actualBuffer) \
Handle<Value> constructorArgs ## slowBuffer[3] = \
{ slowBuffer->handle_, \
Nan::New<Integer>(Buffer::Length(slowBuffer)), \
Nan::New<Integer>(0) }; \
Local<Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs ## slowBuffer);
// Lib Sodium Version Functions
NAN_METHOD(bind_sodium_version_string) {
Nan::EscapableHandleScope scope;
return info.GetReturnValue().Set(Nan::New<String>(sodium_version_string()).ToLocalChecked());
}
NAN_METHOD(bind_sodium_library_version_minor) {
Nan::EscapableHandleScope scope;
return info.GetReturnValue().Set(
Nan::New(sodium_library_version_minor())
);
}
NAN_METHOD(bind_sodium_library_version_major) {
Nan::EscapableHandleScope scope;
return info.GetReturnValue().Set(
Nan::New(sodium_library_version_major())
);
}
// Lib Sodium Utils
NAN_METHOD(bind_memzero) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(1,"argument must be a buffer");
GET_ARG_AS_VOID(0, buffer);
sodium_memzero(buffer, buffer_size);
return info.GetReturnValue().Set(Nan::Null());
}
/**
* int sodium_memcmp(const void * const b1_, const void * const b2_, size_t size);
*/
NAN_METHOD(bind_memcmp) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"argument must be a buffer");
GET_ARG_AS_VOID(0, buffer_1);
GET_ARG_AS_VOID(1, buffer_2);
size_t size;
if (info[2]->IsUint32()) {
size = info[2]->Int32Value();
} else {
return Nan::ThrowError("argument size must be a positive number");
}
size_t s = (buffer_1_size < buffer_2_size)? buffer_1_size : buffer_2_size;
if( s < size ) {
size = s;
}
return info.GetReturnValue().Set(
Nan::New<Integer>(sodium_memcmp(buffer_1, buffer_2, size))
);
}
/**
* char *sodium_bin2hex(char * const hex, const size_t hexlen,
* const unsigned char *bin, const size_t binlen);
*/
NAN_METHOD(bind_sodium_bin2hex) {
Nan::HandleScope scope;
return Nan::ThrowError("use node's native Buffer.toString()");
}
// Lib Sodium Random
// void randombytes_buf(void *const buf, const size_t size)
NAN_METHOD(bind_randombytes_buf) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(1,"argument must be a buffer");
GET_ARG_AS_VOID(0, buffer);
randombytes_buf(buffer, buffer_size);
return info.GetReturnValue().Set(Nan::Null());
}
// void randombytes_stir()
NAN_METHOD(bind_randombytes_stir) {
Nan::EscapableHandleScope scope;
randombytes_stir();
return info.GetReturnValue().Set(Nan::Null());
}
NAN_METHOD(bind_randombytes_close) {
Nan::EscapableHandleScope scope;
// int randombytes_close()
return info.GetReturnValue().Set(
Nan::New<Integer>(randombytes_close())
);
}
NAN_METHOD(bind_randombytes_random) {
Nan::EscapableHandleScope scope;
// uint_32 randombytes_random()
return info.GetReturnValue().Set(
Nan::New<Int32>(randombytes_random())
);
}
NAN_METHOD(bind_randombytes_uniform) {
Nan::EscapableHandleScope scope;
uint32_t upper_bound;
NUMBER_OF_MANDATORY_ARGS(1,"argument size must be a positive number");
if (info[0]->IsUint32()) {
upper_bound = info[0]->Int32Value();
} else {
return Nan::ThrowError("argument size must be a positive number");
}
// uint32_t randombytes_uniform(const uint32_t upper_bound)
return info.GetReturnValue().Set(
Nan::New<Int32>(randombytes_uniform(upper_bound))
);
}
NAN_METHOD(bind_crypto_verify_16) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"arguments must be two buffers");
GET_ARG_AS_UCHAR_LEN(0,string1, crypto_verify_16_BYTES);
GET_ARG_AS_UCHAR_LEN(1,string2, crypto_verify_16_BYTES);
return info.GetReturnValue().Set(
Nan::New<Integer>(crypto_verify_16(string1, string2))
);
}
// int crypto_verify_16(const unsigned char * string1, const unsigned char * string2)
NAN_METHOD(bind_crypto_verify_32) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"arguments must be two buffers");
GET_ARG_AS_UCHAR_LEN(0,string1, crypto_verify_32_BYTES);
GET_ARG_AS_UCHAR_LEN(1,string2, crypto_verify_32_BYTES);
return info.GetReturnValue().Set(
Nan::New<Integer>(crypto_verify_32(string1, string2))
);
}
/**
* int crypto_shorthash(
* unsigned char *out,
* const unsigned char *in,
* unsigned long long inlen,
* const unsigned char *key)
*
* Parameters:
* [out] out result of hash
* [in] in input buffer
* [in] inlen size of input buffer
* [in] key key buffer
*
* A lot of applications and programming language implementations have been
* recently found to be vulnerable to denial-of-service attacks when a hash
* function with weak security guarantees, like Murmurhash 3, was used to
* construct a hash table.
* In order to address this, Sodium provides the �shorthash� function,
* currently implemented using SipHash-2-4. This very fast hash function
* outputs short, but unpredictable (without knowing the secret key) values
* suitable for picking a list in a hash table for a given key.
*/
NAN_METHOD(bind_crypto_shorthash) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(1,"argument message must be a buffer");
GET_ARG_AS_UCHAR(0,message);
GET_ARG_AS_UCHAR_LEN(1, key, crypto_shorthash_KEYBYTES);
NEW_BUFFER_AND_PTR(hash, crypto_shorthash_BYTES);
if( crypto_shorthash(hash_ptr, message, message_size, key) == 0 ) {
return info.GetReturnValue().Set(hash);
} else {
return info.GetReturnValue().Set(Nan::Null());
}
}
/**
* int crypto_hash(
* unsigned char * hbuf,
* const unsigned char * msg,
* unsigned long long mlen)
*/
NAN_METHOD(bind_crypto_hash) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(1,"argument message must be a buffer");
GET_ARG_AS_UCHAR(0,msg);
NEW_BUFFER_AND_PTR(hash, crypto_hash_BYTES);
if( crypto_hash(hash_ptr, msg, msg_size) == 0 ) {
return info.GetReturnValue().Set(hash);
} else {
return info.GetReturnValue().Set(Nan::Null());
}
}
/**
* int crypto_hash_sha256(
* unsigned char * hbuf,
* const unsigned char * msg,
* unsigned long long mlen)
*/
NAN_METHOD(bind_crypto_hash_sha256) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(1,"argument message must be a buffer");
GET_ARG_AS_UCHAR(0, msg);
NEW_BUFFER_AND_PTR(hash, 32);
if( crypto_hash_sha256(hash_ptr, msg, msg_size) == 0 ) {
return info.GetReturnValue().Set(hash);
} else {
return info.GetReturnValue().Set(Nan::Null());
}
}
/**
* int crypto_hash_sha512(
* unsigned char * hbuf,
* const unsigned char * msg,
* unsigned long long mlen)
*/
NAN_METHOD(bind_crypto_hash_sha512) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(1,"argument message must be a buffer");
GET_ARG_AS_UCHAR(0, msg);
NEW_BUFFER_AND_PTR(hash, 64);
if( crypto_hash_sha512(hash_ptr, msg, msg_size) == 0 ) {
return info.GetReturnValue().Set(hash);
} else {
return info.GetReturnValue().Set(Nan::Null());
}
}
/**
* int crypto_auth(
* unsigned char* tok,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * key)
*
* Parameters:
* [out] tok the generated authentication token.
* [in] msg the message to be authenticated.
* [in] mlen the length of msg.
* [in] key the key used to compute the token.
*/
NAN_METHOD(bind_crypto_auth) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"arguments message, and key must be buffers");
GET_ARG_AS_UCHAR(0, msg);
GET_ARG_AS_UCHAR_LEN(1, key, crypto_auth_KEYBYTES);
NEW_BUFFER_AND_PTR(token, crypto_auth_BYTES);
if( crypto_auth(token_ptr, msg, msg_size, key) == 0 ) {
return info.GetReturnValue().Set(token);
} else {
return info.GetReturnValue().Set(Nan::Null());
}
}
/**
* int crypto_auth_verify(
* unsigned char* tok,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * key)
*
* Parameters:
* [out] tok the generated authentication token.
* [in] msg the message to be authenticated.
* [in] mlen the length of msg.
* [in] key the key used to compute the token.
*/
NAN_METHOD(bind_crypto_auth_verify) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"arguments token, message, and key must be buffers");
GET_ARG_AS_UCHAR_LEN(0, token, crypto_auth_BYTES);
GET_ARG_AS_UCHAR(1, message);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_auth_KEYBYTES);
return info.GetReturnValue().Set(
Nan::New<Integer>(crypto_auth_verify(token, message, message_size, key))
);
}
/**
* int crypto_onetimeauth(
* unsigned char* tok,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * key)
*
* Parameters:
* [out] tok the generated authentication token.
* [in] msg the message to be authenticated.
* [in] mlen the length of msg.
* [in] key the key used to compute the token.
*/
NAN_METHOD(bind_crypto_onetimeauth) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"arguments message, and key must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, key, crypto_onetimeauth_KEYBYTES);
NEW_BUFFER_AND_PTR(token, crypto_onetimeauth_BYTES);
if( crypto_onetimeauth(token_ptr, message, message_size, key) == 0 ) {
return info.GetReturnValue().Set(token);
} else {
return info.GetReturnValue().Set(Nan::Null());
}
}
/**
* int crypto_onetimeauth_verify(
* unsigned char* tok,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * key)
*
* Parameters:
* [out] tok the generated authentication token.
* [in] msg the message to be authenticated.
* [in] mlen the length of msg.
* [in] key the key used to compute the token.
*/
NAN_METHOD(bind_crypto_onetimeauth_verify) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"arguments token, message, and key must be buffers");
GET_ARG_AS_UCHAR_LEN(0, token, crypto_onetimeauth_BYTES);
GET_ARG_AS_UCHAR(1, message);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_onetimeauth_KEYBYTES);
return info.GetReturnValue().Set(
Nan::New<Integer>(crypto_onetimeauth_verify(token, message, message_size, key))
);
}
/**
* int crypto_stream(
* unsigned char * stream,
* unsigned long long slen,
* const unsigned char * nonce,
* const unsigned char * key)
*
* Generates a stream using the given secret key and nonce.
*
* Parameters:
* [out] stream the generated stream.
* [out] slen the length of the generated stream.
* [in] nonce the nonce used to generate the stream.
* [in] key the key used to generate the stream.
*
* Returns:
* 0 if operation successful
*/
NAN_METHOD(bind_crypto_stream) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"argument length must be a positive number, arguments nonce, and key must be buffers");
if (!info[0]->IsUint32())
return Nan::ThrowError("argument length must be positive number");
unsigned long long slen = info[0]->ToUint32()->Value();
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_stream_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_stream_KEYBYTES);
NEW_BUFFER_AND_PTR(stream, slen);
if (crypto_stream(stream_ptr, slen, nonce, key) == 0) {
return info.GetReturnValue().Set(stream);
} else {
return;
}
}
/**
* int crypto_stream_xor(
* unsigned char *c,
* const unsigned char *m,
* unsigned long long mlen,
* const unsigned char *n,
* const unsigned char *k)
*
* Parameters:
* [out] ctxt buffer for the resulting ciphertext.
* [in] msg the message to be encrypted.
* [in] mlen the length of the message.
* [in] nonce the nonce used during encryption.
* [in] key secret key used during encryption.
*
* Returns:
* 0 if operation successful.
*
* Precondition:
* ctxt must have length minimum mlen.
* nonce must have length minimum crypto_stream_NONCEBYTES.
* key must have length minimum crpyto_stream_KEYBYTES
*/
NAN_METHOD(bind_crypto_stream_xor) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"arguments message, nonce, and key must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_stream_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_stream_KEYBYTES);
NEW_BUFFER_AND_PTR(ctxt, message_size);
if (crypto_stream_xor(ctxt_ptr, message, message_size, nonce, key) == 0) {
return info.GetReturnValue().Set(ctxt);
} else {
return;
}
}
/**
* Encrypts and authenticates a message using the given secret key, and nonce.
*
* int crypto_secretbox(
* unsigned char *ctxt,
* const unsigned char *msg,
* unsigned long long mlen,
* const unsigned char *nonce,
* const unsigned char *key)
*
* Parameters:
* [out] ctxt the buffer for the cipher-text.
* [in] msg the message to be encrypted.
* [in] mlen the length of msg.
* [in] nonce a nonce with length crypto_box_NONCEBYTES.
* [in] key the shared secret key.
*
* Returns:
* 0 if operation is successful.
*
* Precondition:
* first crypto_secretbox_ZEROBYTES of msg be all 0..
*
* Postcondition:
* first crypto_secretbox_BOXZERBYTES of ctxt be all 0.
* first mlen bytes of ctxt will contain the ciphertext.
*/
NAN_METHOD(bind_crypto_secretbox) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"arguments message, nonce, and key must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_secretbox_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_secretbox_KEYBYTES);
NEW_BUFFER_AND_PTR(pmb, message_size + crypto_secretbox_ZEROBYTES);
// Fill the first crypto_secretbox_ZEROBYTES with 0
unsigned int i;
for(i = 0; i < crypto_secretbox_ZEROBYTES; i++) {
pmb_ptr[i] = 0U;
}
//Copy the message to the new buffer
memcpy((void*) (pmb_ptr + crypto_secretbox_ZEROBYTES), (void *) message, message_size);
message_size += crypto_secretbox_ZEROBYTES;
NEW_BUFFER_AND_PTR(ctxt, message_size);
if( crypto_secretbox(ctxt_ptr, pmb_ptr, message_size, nonce, key) == 0) {
return info.GetReturnValue().Set(ctxt);
} else {
return;
}
}
/**
* Decrypts a ciphertext ctxt given the receivers private key, and senders public key.
*
* int crypto_secretbox_open(
* unsigned char *msg,
* const unsigned char *ctxt,
* unsigned long long clen,
* const unsigned char *nonce,
* const unsigned char *key)
*
* Parameters:
* [out] msg the buffer to place resulting plaintext.
* [in] ctxt the ciphertext to be decrypted.
* [in] clen the length of the ciphertext.
* [in] nonce a randomly generated nonce.
* [in] key the shared secret key.
*
* Returns:
* 0 if successful and -1 if verification fails.
*
* Precondition:
* first crypto_secretbox_BOXZEROBYTES of ctxt be all 0.
* the nonce must be of length crypto_secretbox_NONCEBYTES
*
* Postcondition:
* first clen bytes of msg will contain the plaintext.
* first crypto_secretbox_ZEROBYTES of msg will be all 0.
*
* Warning:
* if verification fails msg may contain data from the computation.
*/
NAN_METHOD(bind_crypto_secretbox_open) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"arguments cipherText, nonce, and key must be buffers");
GET_ARG_AS_UCHAR(0, cipher_text);
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_secretbox_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_secretbox_KEYBYTES);
NEW_BUFFER_AND_PTR(message, cipher_text_size);
// API requires that the first crypto_secretbox_ZEROBYTES of msg be 0 so lets check
if (cipher_text_size < crypto_secretbox_BOXZEROBYTES) {
std::ostringstream oss;
oss << "argument cipherText must have at least " << crypto_secretbox_BOXZEROBYTES << " bytes";
return Nan::ThrowError(oss.str().c_str());
}
unsigned int i;
for(i = 0; i < crypto_secretbox_BOXZEROBYTES; i++) {
if( cipher_text[i] ) break;
}
if (i < crypto_secretbox_BOXZEROBYTES) {
std::ostringstream oss;
oss << "the first " << crypto_secretbox_BOXZEROBYTES << " bytes of argument cipherText must be 0";
return Nan::ThrowError(oss.str().c_str());
}
if (crypto_secretbox_open(message_ptr, cipher_text, cipher_text_size, nonce, key) == 0) {
// Remove the padding at the beginning of the message
NEW_BUFFER_AND_PTR(plain_text, cipher_text_size - crypto_secretbox_ZEROBYTES);
memcpy(plain_text_ptr,(void*) (message_ptr + crypto_secretbox_ZEROBYTES), cipher_text_size - crypto_secretbox_ZEROBYTES);
return info.GetReturnValue().Set(plain_text);
} else {
return;
}
}
/**
* Encrypts and authenticates a message using the given secret key, and nonce.
*
* int crypto_secretbox_easy(
* unsigned char *ctxt,
* const unsigned char *msg,
* unsigned long long mlen,
* const unsigned char *nonce,
* const unsigned char *key)
*
* Parameters:
* [out] ctxt the buffer for the cipher-text.
* [in] msg the message to be encrypted.
* [in] mlen the length of msg.
* [in] nonce a nonce with length crypto_box_NONCEBYTES.
* [in] key the shared secret key.
*
* Returns:
* 0 if operation is successful.
*
* Precondition:
*
* Postcondition:
* first mlen + crypto_secretbox_MACLENGTH bytes of ctxt will contain the ciphertext.
*/
NAN_METHOD(bind_crypto_secretbox_easy) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"arguments message, nonce, and key must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_secretbox_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_secretbox_KEYBYTES);
NEW_BUFFER_AND_PTR(c, message_size + crypto_secretbox_MACBYTES);
if (crypto_secretbox_easy(c_ptr, message, message_size, nonce, key) == 0) {
return info.GetReturnValue().Set(c);
} else {
return;
}
}
/**
* int crypto_secretbox_open_easy(
* unsigned char *msg,
* const unsigned char *ctxt,
* unsigned long long clen,
* const unsigned char *nonce,
* const unsigned char *key)
* Parameters:
* [out] msg the buffer to place resulting plaintext.
* [in] ctxt the ciphertext to be decrypted.
* [in] clen the length of the ciphertext.
* [in] nonce a randomly generated nonce.
* [in] key the shared secret key.
*
* Returns:
* 0 if successful and -1 if verification fails.
*
* Precondition:
* the nonce must be of length crypto_secretbox_NONCEBYTES
*
* Postcondition:
* first clen - crypto_secretbox_MACBYTES bytes of msg will contain the plaintext.
*
* Warning:
* if verification fails msg may contain data from the computation.
*/
NAN_METHOD(bind_crypto_secretbox_open_easy) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(3,"arguments message, nonce, and key must be buffers");
GET_ARG_AS_UCHAR(0, cipher_text);
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_secretbox_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, key, crypto_secretbox_KEYBYTES);
NEW_BUFFER_AND_PTR(c, cipher_text_size - crypto_secretbox_MACBYTES);
if (crypto_secretbox_open_easy(c_ptr, cipher_text, cipher_text_size, nonce, key) == 0) {
return info.GetReturnValue().Set(c);
} else {
return;
}
}
/**
* Signs a given message using the signer's signing key.
*
* int crypto_sign(
* unsigned char * sig,
* unsigned long long * slen,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * sk)
*
* Parameters:
* [out] sig the resulting signature.
* [out] slen the length of the signature.
* [in] msg the message to be signed.
* [in] mlen the length of the message.
* [in] sk the signing key.
*
* Returns:
* 0 if operation successful
*
* Precondition:
* sig must be of length mlen+crypto_sign_BYTES
* sk must be of length crypto_sign_SECRETKEYBYTES
*/
NAN_METHOD(bind_crypto_sign) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"arguments message, and secretKey must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, secretKey, crypto_sign_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(sig, message_size + crypto_sign_BYTES);
unsigned long long slen = 0;
if (crypto_sign(sig_ptr, &slen, message, message_size, secretKey) == 0) {
return info.GetReturnValue().Set(sig);
} else {
return;
}
}
/**
* Signs a given message using the signer's signing key (detached mode).
*
* int crypto_sign_detached(
* unsigned char * sig,
* unsigned long long * slen,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * sk)
*
* Parameters:
* [out] sig the resulting signature.
* [out] slen the length of the signature.
* [in] msg the message to be signed.
* [in] mlen the length of the message.
* [in] sk the signing key.
*
* Returns:
* 0 if operation successful
*
* Precondition:
* sig must be of length crypto_sign_BYTES
* sk must be of length crypto_sign_SECRETKEYBYTES
*/
NAN_METHOD(bind_crypto_sign_detached) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"arguments message, and secretKey must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, secretKey, crypto_sign_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(sig, crypto_sign_BYTES);
unsigned long long slen = 0;
if (crypto_sign_detached(sig_ptr, &slen, message, message_size, secretKey) == 0) {
return info.GetReturnValue().Set(sig);
} else {
return info.GetReturnValue().Set(Nan::Undefined());
}
}
/**
* Generates a signing/verification key pair.
*
* int crypto_sign_keypair(
* unsigned char * vk,
* unsigned char * sk)
*
* Parameters:
* [out] vk the verification key.
* [out] sk the signing key.
*
* Returns:
* 0 if operation successful.
*
* Precondition:
* the buffer for vk must be at least crypto_sign_PUBLICKEYBYTES in length
* the buffer for sk must be at least crypto_sign_SECRETKEYTBYTES in length
*
* Postcondition:
* first crypto_sign_PUBLICKEYTBYTES of vk will be the key data.
* first crypto_sign_SECRETKEYTBYTES of sk will be the key data.
*/
NAN_METHOD(bind_crypto_sign_keypair) {
Nan::EscapableHandleScope scope;
NEW_BUFFER_AND_PTR(vk, crypto_sign_PUBLICKEYBYTES);
NEW_BUFFER_AND_PTR(sk, crypto_sign_SECRETKEYBYTES);
if (crypto_sign_keypair(vk_ptr, sk_ptr) == 0) {
Local<Object> result = Nan::New<Object>();
result->ForceSet(Nan::New<String>("publicKey").ToLocalChecked(), vk, DontDelete);
result->ForceSet(Nan::New<String>("secretKey").ToLocalChecked(), sk, DontDelete);
return info.GetReturnValue().Set(result);
} else {
return;
}
}
/**
* Deterministically generate a signing/verification key pair from a seed.
*
* int crypto_sign_keypair(
* unsigned char * vk,
* unsigned char * sk,
* const unsigned char * ps)
*
* Parameters:
* [out] vk the verification key.
* [out] sk the signing key.
* [in] sd the seed for the key-pair.
*
* Returns:
* 0 if operation successful.
*
* Precondition:
* the buffer for vk must be at least crypto_sign_PUBLICKEYBYTES in length
* the buffer for sk must be at least crypto_sign_SECRETKEYTBYTES in length
* the buffer for sd must be at least crypto_sign_SEEDBYTES in length
*
* Postcondition:
* first crypto_sign_PUBLICKEYTBYTES of vk will be the key data.
* first crypto_sign_SECRETKEYTBYTES of sk will be the key data.
*/
NAN_METHOD(bind_crypto_sign_seed_keypair) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(1,"the argument seed must be a buffer");
GET_ARG_AS_UCHAR_LEN(0, sd, crypto_sign_SEEDBYTES);
NEW_BUFFER_AND_PTR(vk, crypto_sign_PUBLICKEYBYTES);
NEW_BUFFER_AND_PTR(sk, crypto_sign_SECRETKEYBYTES);
if (crypto_sign_seed_keypair(vk_ptr, sk_ptr, sd) == 0) {
Local<Object> result = Nan::New<Object>();
result->ForceSet(Nan::New<String>("publicKey").ToLocalChecked(), vk, DontDelete);
result->ForceSet(Nan::New<String>("secretKey").ToLocalChecked(), sk, DontDelete);
return info.GetReturnValue().Set(result);
} else {
return;
}
}
/**
* Verifies the signed message sig using the signer's verification key.
*
* int crypto_sign_open(
* unsigned char * msg,
* unsigned long long * mlen,
* const unsigned char * sig,
* unsigned long long smlen,
* const unsigned char * vk)
*
* Parameters:
*
* [out] msg the resulting message.
* [out] mlen the length of msg.
* [in] sig the signed message.
* [in] smlen length of the signed message.
* [in] vk the verification key.
*
* Returns:
* 0 if successful, -1 if verification fails.
*
* Precondition:
* length of msg must be at least smlen
*
* Warning:
* if verification fails msg may contain data from the computation.
*/
NAN_METHOD(bind_crypto_sign_open) {
Nan::EscapableHandleScope scope;
NUMBER_OF_MANDATORY_ARGS(2,"arguments signedMessage and verificationKey must be buffers");
GET_ARG_AS_UCHAR(0, signedMessage);
GET_ARG_AS_UCHAR_LEN(1, publicKey, crypto_sign_PUBLICKEYBYTES);
unsigned long long mlen = 0;
NEW_BUFFER_AND_PTR(msg, signedMessage_size);
if (crypto_sign_open(msg_ptr, &mlen, signedMessage, signedMessage_size, publicKey) == 0) {
NEW_BUFFER_AND_PTR(m, mlen);
memcpy(m_ptr, msg_ptr, mlen);
return info.GetReturnValue().Set(m);
} else {
return;
}
}
/**
* Verifies the signed message sig using the signer's verification key.
*
* int crypto_sign_verify_detached(
* const unsigned char * sig,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * vk)
*
* Parameters:
*
* [in] sig the signature
* [in] msg the message.
* [in] mlen the length of msg.
* [in] vk the verification key.