forked from paixaop/node-sodium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sodium.cc
executable file
·1468 lines (1235 loc) · 43.9 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 = NanGetCurrentContext()->Global();
// Retrieve the buffer constructor function
Local<Function> bufferConstructor =
Local<Function>::Cast(globalObj->Get(NanNew<String>("Buffer")));
// Check if a function argument is a node Buffer. If not throw V8 exception
#define ARG_IS_BUFFER(i,msg) \
if (!Buffer::HasInstance(args[i])) { \
std::ostringstream oss; \
oss << "argument " << msg << " must be a buffer"; \
return NanThrowError(oss.str().c_str()); \
}
// Create a new buffer, and get a pointer to it
#define NEW_BUFFER_AND_PTR(name, size) \
Local<Object> name = NanNewBufferHandle(size); \
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(args[i]->ToObject()); \
unsigned long long NAME ## _size = Buffer::Length(args[i]->ToObject()); \
if( NAME ## _size == 0 ) { \
std::ostringstream oss; \
oss << "argument " << #NAME << " length cannot be zero" ; \
return NanThrowError(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 NanThrowError(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 (args.Length() < (n)) { \
return NanThrowError(message); \
}
#define TO_REAL_BUFFER(slowBuffer, actualBuffer) \
Handle<Value> constructorArgs ## slowBuffer[3] = \
{ slowBuffer->handle_, \
NanNew<Integer>(Buffer::Length(slowBuffer)), \
NanNew<Integer>(0) }; \
Local<Object> actualBuffer = bufferConstructor->NewInstance(3, constructorArgs ## slowBuffer);
// Lib Sodium Version Functions
NAN_METHOD(bind_sodium_version_string) {
NanEscapableScope();
NanReturnValue(NanNew<String>(sodium_version_string()));
}
NAN_METHOD(bind_sodium_library_version_minor) {
NanEscapableScope();
NanReturnValue(
NanNew(sodium_library_version_minor())
);
}
NAN_METHOD(bind_sodium_library_version_major) {
NanEscapableScope();
NanReturnValue(
NanNew(sodium_library_version_major())
);
}
// Lib Sodium Utils
NAN_METHOD(bind_memzero) {
NanEscapableScope();
NUMBER_OF_MANDATORY_ARGS(1,"argument must be a buffer");
GET_ARG_AS_VOID(0, buffer);
sodium_memzero(buffer, buffer_size);
NanReturnValue(NanNull());
}
/**
* int sodium_memcmp(const void * const b1_, const void * const b2_, size_t size);
*/
NAN_METHOD(bind_memcmp) {
NanEscapableScope();
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 (args[2]->IsUint32()) {
size = args[2]->Int32Value();
} else {
return NanThrowError("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;
}
NanReturnValue(NanNew<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) {
NanScope();
return NanThrowError("use node's native Buffer.toString()");
}
// Lib Sodium Random
// void randombytes_buf(void *const buf, const size_t size)
NAN_METHOD(bind_randombytes_buf) {
NanEscapableScope();
NUMBER_OF_MANDATORY_ARGS(1,"argument must be a buffer");
GET_ARG_AS_VOID(0, buffer);
randombytes_buf(buffer, buffer_size);
NanReturnValue(NanNull());
}
// void randombytes_stir()
NAN_METHOD(bind_randombytes_stir) {
NanEscapableScope();
randombytes_stir();
NanReturnValue(NanNull());
}
NAN_METHOD(bind_randombytes_close) {
NanEscapableScope();
// int randombytes_close()
NanReturnValue(NanNew<Integer>(randombytes_close()));
}
NAN_METHOD(bind_randombytes_random) {
NanEscapableScope();
// uint_32 randombytes_random()
NanReturnValue(NanNew<Int32>(randombytes_random()));
}
NAN_METHOD(bind_randombytes_uniform) {
NanEscapableScope();
uint32_t upper_bound;
NUMBER_OF_MANDATORY_ARGS(1,"argument size must be a positive number");
if (args[0]->IsUint32()) {
upper_bound = args[0]->Int32Value();
} else {
return NanThrowError("argument size must be a positive number");
}
// uint32_t randombytes_uniform(const uint32_t upper_bound)
NanReturnValue(NanNew<Int32>(randombytes_uniform(upper_bound)));
}
NAN_METHOD(bind_crypto_verify_16) {
NanEscapableScope();
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);
NanReturnValue(NanNew<Integer>(crypto_verify_16(string1, string2)));
}
// int crypto_verify_16(const unsigned char * string1, const unsigned char * string2)
NAN_METHOD(bind_crypto_verify_32) {
NanEscapableScope();
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);
NanReturnValue(NanNew<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) {
NanEscapableScope();
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 ) {
NanReturnValue(hash);
}
NanReturnValue(NanNull());
}
/**
* int crypto_hash(
* unsigned char * hbuf,
* const unsigned char * msg,
* unsigned long long mlen)
*/
NAN_METHOD(bind_crypto_hash) {
NanEscapableScope();
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 ) {
NanReturnValue(hash);
}
NanReturnValue(NanNull());
}
/**
* int crypto_hash_sha256(
* unsigned char * hbuf,
* const unsigned char * msg,
* unsigned long long mlen)
*/
NAN_METHOD(bind_crypto_hash_sha256) {
NanEscapableScope();
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 ) {
NanReturnValue(hash);
}
NanReturnValue(NanNull());
}
/**
* int crypto_hash_sha512(
* unsigned char * hbuf,
* const unsigned char * msg,
* unsigned long long mlen)
*/
NAN_METHOD(bind_crypto_hash_sha512) {
NanEscapableScope();
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 ) {
NanReturnValue(hash);
}
NanReturnValue(NanNull());
}
/**
* 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) {
NanEscapableScope();
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 ) {
NanReturnValue(token);
}
NanReturnValue(NanNull());
}
/**
* 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) {
NanEscapableScope();
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);
NanReturnValue(NanNew<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) {
NanEscapableScope();
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 ) {
NanReturnValue(token);
}
NanReturnValue(NanNull());
}
/**
* 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) {
NanEscapableScope();
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);
NanReturnValue(NanNew<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) {
NanEscapableScope();
NUMBER_OF_MANDATORY_ARGS(3,"argument length must be a positive number, arguments nonce, and key must be buffers");
if (!args[0]->IsUint32())
return NanThrowError("argument length must be positive number");
unsigned long long slen = args[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) {
NanReturnValue(stream);
}
NanReturnUndefined();
}
/**
* 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) {
NanEscapableScope();
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) {
NanReturnValue(ctxt);
}
NanReturnUndefined();
}
/**
* 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) {
NanEscapableScope();
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) {
NanReturnValue(ctxt);
}
NanReturnUndefined();
}
/**
* 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) {
NanEscapableScope();
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 NanThrowError(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 NanThrowError(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);
NanReturnValue(plain_text);
}
NanReturnUndefined();
}
/**
* 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) {
NanEscapableScope();
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) {
NanReturnValue(sig);
}
NanReturnUndefined();
}
/**
* 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) {
NanEscapableScope();
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) {
NanReturnValue(sig);
}
NanReturnValue(NanUndefined());
}
/**
* 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) {
NanEscapableScope();
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 = NanNew<Object>();
result->ForceSet(NanNew<String>("publicKey"), vk, DontDelete);
result->ForceSet(NanNew<String>("secretKey"), sk, DontDelete);
NanReturnValue(result);
}
NanReturnUndefined();
}
/**
* 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) {
NanEscapableScope();
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 = NanNew<Object>();
result->ForceSet(NanNew<String>("publicKey"), vk, DontDelete);
result->ForceSet(NanNew<String>("secretKey"), sk, DontDelete);
NanReturnValue(result);
}
NanReturnUndefined();
}
/**
* 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) {
NanEscapableScope();
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);
NanReturnValue(m);
}
NanReturnUndefined();
}
/**
* 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.
*
* Returns:
* 0 if successful, -1 if verification fails.
*
* Precondition:
* length of sig must be crypto_sign_BYTES
*
* Warning:
* if verification fails msg may contain data from the computation.
*/
NAN_METHOD(bind_crypto_sign_verify_detached) {
NanEscapableScope();
NUMBER_OF_MANDATORY_ARGS(2,"arguments signedMessage and verificationKey must be buffers");
GET_ARG_AS_UCHAR_LEN(0, signature, crypto_sign_BYTES);
GET_ARG_AS_UCHAR(1, message);
GET_ARG_AS_UCHAR_LEN(2, publicKey, crypto_sign_PUBLICKEYBYTES);
if( crypto_sign_verify_detached(signature, message, message_size, publicKey) == 0) {
NanReturnValue(NanTrue());
}
NanReturnValue(NanFalse());
}
/**
* Encrypts a message given the senders secret key, and receivers public key.
* int crypto_box (
* unsigned char * ctxt,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * nonce,
* const unsigned char * pk,
* const unsigned char * sk)
*
* 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 randomly generated nonce.
* [in] pk the receivers public key, used for encryption.
* [in] sk the senders private key, used for signing.
*
* Returns:
* 0 if operation is successful.
*
* Precondition:
* first crypto_box_ZEROBYTES of msg be all 0.
* the nonce must have size crypto_box_NONCEBYTES.
*
* Postcondition:
* first crypto_box_BOXZEROBYTES of ctxt be all 0.
* first mlen bytes of ctxt will contain the ciphertext.
*/
NAN_METHOD(bind_crypto_box) {
NanEscapableScope();
NUMBER_OF_MANDATORY_ARGS(4,"arguments message, nonce, publicKey and secretKey must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_box_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, publicKey, crypto_box_PUBLICKEYBYTES);
GET_ARG_AS_UCHAR_LEN(3, secretKey, crypto_box_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(msg, message_size + crypto_box_ZEROBYTES);
// Fill the first crypto_box_ZEROBYTES with 0
unsigned int i;
for(i=0; i < crypto_box_ZEROBYTES; i++) {
msg_ptr[i] = 0U;
}
//Copy the message to the new buffer
memcpy((void*) (msg_ptr + crypto_box_ZEROBYTES), (void *) message, message_size);
message_size += crypto_box_ZEROBYTES;
NEW_BUFFER_AND_PTR(ctxt, message_size);
if( crypto_box(ctxt_ptr, msg_ptr, message_size, nonce, publicKey, secretKey) == 0) {
NanReturnValue(ctxt);
}
NanReturnUndefined();
}
/**
* Encrypts a message given the senders secret key, and receivers public key.
* int crypto_box_easy (
* unsigned char * ctxt,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * nonce,
* const unsigned char * pk,
* const unsigned char * sk)
*
* 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 randomly generated nonce.
* [in] pk the receivers public key, used for encryption.
* [in] sk the senders private key, used for signing.
*
* Returns:
* 0 if operation is successful.
*
* Precondition:
* the nonce must have size crypto_box_NONCEBYTES.
*
* Postcondition:
* first mlen bytes of ctxt will contain the ciphertext.
*/
NAN_METHOD(bind_crypto_box_easy) {
NanEscapableScope();
NUMBER_OF_MANDATORY_ARGS(4,"arguments message, nonce, publicKey and secretKey must be buffers");
GET_ARG_AS_UCHAR(0, message);
GET_ARG_AS_UCHAR_LEN(1, nonce, crypto_box_NONCEBYTES);
GET_ARG_AS_UCHAR_LEN(2, publicKey, crypto_box_PUBLICKEYBYTES);
GET_ARG_AS_UCHAR_LEN(3, secretKey, crypto_box_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(ctxt, message_size + crypto_box_MACBYTES);
if( crypto_box_easy(ctxt_ptr, message, message_size, nonce, publicKey, secretKey) == 0) {
NanReturnValue(ctxt);
}
NanReturnUndefined();
}
/**
* Randomly generates a secret key and a corresponding public key.
*
* int crypto_box_keypair(
* unsigned char * pk,