forked from paixaop/node-sodium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_box.cc
688 lines (578 loc) · 22.6 KB
/
crypto_box.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
/**
* Node Native Module for Lib Sodium
*
* @Author Pedro Paixao
* @email paixaop at gmail dot com
* @License MIT
*/
#include "node_sodium.h"
/**
* 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) {
Nan::EscapableHandleScope scope;
ARGS(4,"arguments message, nonce, publicKey and secretKey must be buffers");
ARG_TO_UCHAR_BUFFER(message);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(publicKey, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(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) {
return info.GetReturnValue().Set(ctxt);
} else {
return;
}
}
/**
* 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) {
Nan::EscapableHandleScope scope;
ARGS(4,"arguments message, nonce, publicKey and secretKey must be buffers");
ARG_TO_UCHAR_BUFFER(message);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(publicKey, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(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) {
return info.GetReturnValue().Set(ctxt);
} else {
return;
}
}
/**
* Randomly generates a secret key and a corresponding public key.
*
* int crypto_box_keypair(
* unsigned char * pk,
* unsigned char * sk)
*
* Parameters:
* [out] pk the buffer for the public key with length crypto_box_PUBLICKEYBYTES
* [out] sk the buffer for the private key with length crypto_box_SECRETKEYTBYTES
*
* Returns:
* 0 if generation successful.
*
* Precondition:
* the buffer for pk must be at least crypto_box_PUBLICKEYBYTES in length
* the buffer for sk must be at least crypto_box_SECRETKEYTBYTES in length
*
* Postcondition:
* first crypto_box_PUBLICKEYTBYTES of pk will be the key data.
* first crypto_box_SECRETKEYTBYTES of sk will be the key data.
*/
NAN_METHOD(bind_crypto_box_keypair) {
Nan::EscapableHandleScope scope;
NEW_BUFFER_AND_PTR(pk, crypto_box_PUBLICKEYBYTES);
NEW_BUFFER_AND_PTR(sk, crypto_box_SECRETKEYBYTES);
if (crypto_box_keypair(pk_ptr, sk_ptr) == 0) {
Local<Object> result = Nan::New<Object>();
result->ForceSet(Nan::New<String>("publicKey").ToLocalChecked(), pk, DontDelete);
result->ForceSet(Nan::New<String>("secretKey").ToLocalChecked(), sk, DontDelete);
return info.GetReturnValue().Set(result);
} else {
return;
}
}
/**
* Decrypts a ciphertext ctxt given the receivers private key, and senders public key.
*
* int crypto_box_open(
* unsigned char * msg,
* const unsigned char * ctxt,
* unsigned long long clen,
* const unsigned char * nonce,
* const unsigned char * pk,
* const unsigned char * sk)
*
* 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.
* [in] pk the senders public key, used for verification.
* [in] sk the receivers private key, used for decryption.
*
Returns:
* 0 if successful and -1 if verification fails.
*
Precondition:
* first crypto_box_BOXZEROBYTES of ctxt be all 0.
* the nonce must have size crypto_box_NONCEBYTES.
*
* Postcondition:
* first clen bytes of msg will contain the plaintext.
* first crypto_box_ZEROBYTES of msg will be all 0.
*/
NAN_METHOD(bind_crypto_box_open) {
Nan::EscapableHandleScope scope;
ARGS(4,"arguments cipherText, nonce, publicKey and secretKey must be buffers");
ARG_TO_UCHAR_BUFFER(cipherText);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(publicKey, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(secretKey, crypto_box_SECRETKEYBYTES);
// API requires that the first crypto_box_BOXZEROBYTES of msg be 0 so lets check
if (cipherText_size < crypto_box_BOXZEROBYTES) {
std::ostringstream oss;
oss << "argument cipherText must have a length of at least " << crypto_box_BOXZEROBYTES << " bytes";
return Nan::ThrowError(oss.str().c_str());
}
unsigned int i;
for (i = 0; i < crypto_box_BOXZEROBYTES; i++) {
if( cipherText[i] ) break;
}
if (i < crypto_box_BOXZEROBYTES) {
std::ostringstream oss;
oss << "the first " << crypto_box_BOXZEROBYTES << " bytes of argument cipherText must be 0";
return Nan::ThrowError(oss.str().c_str());
}
NEW_BUFFER_AND_PTR(msg, cipherText_size);
if (crypto_box_open(msg_ptr, cipherText, cipherText_size, nonce, publicKey, secretKey) == 0) {
// Remove the padding at the beginning of the message
NEW_BUFFER_AND_PTR(plain_text, cipherText_size - crypto_box_ZEROBYTES);
memcpy(plain_text_ptr,(void*) (msg_ptr + crypto_box_ZEROBYTES), cipherText_size - crypto_box_ZEROBYTES);
return info.GetReturnValue().Set(plain_text);
} else {
return;
}
}
/**
* Decrypts a ciphertext ctxt given the receivers private key, and senders public key.
*
* int crypto_box_open_easy(
* unsigned char * msg,
* const unsigned char * ctxt,
* unsigned long long clen,
* const unsigned char * nonce,
* const unsigned char * pk,
* const unsigned char * sk)
*
* 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.
* [in] pk the senders public key, used for verification.
* [in] sk the receivers private key, used for decryption.
*
Returns:
* 0 if successful and -1 if verification fails.
*
Precondition:
* the nonce must have size crypto_box_NONCEBYTES.
*
* Postcondition:
* first clen bytes of msg will contain the plaintext.
*/
NAN_METHOD(bind_crypto_box_open_easy) {
Nan::EscapableHandleScope scope;
ARGS(4,"arguments cipherText, nonce, publicKey and secretKey must be buffers");
ARG_TO_UCHAR_BUFFER(cipherText);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(publicKey, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(secretKey, crypto_box_SECRETKEYBYTES);
// cipherText should have crypto_box_MACBYTES + encrypted message chars so lets check
if (cipherText_size < crypto_box_MACBYTES) {
std::ostringstream oss;
oss << "argument cipherText must have a length of at least " << crypto_box_MACBYTES << " bytes";
return Nan::ThrowError(oss.str().c_str());
}
NEW_BUFFER_AND_PTR(msg, cipherText_size - crypto_box_MACBYTES);
if( crypto_box_open_easy(msg_ptr, cipherText, cipherText_size, nonce, publicKey, secretKey) == 0) {
return info.GetReturnValue().Set(msg);
} else {
return;
}
}
/**
* Partially performs the computation required for both encryption and decryption of data.
*
* int crypto_box_beforenm(
* unsigned char* k,
* const unsigned char* pk,
* const unsigned char* sk)
*
* Parameters:
* [out] k the result of the computation.
* [in] pk the receivers public key, used for encryption.
* [in] sk the senders private key, used for signing.
*
* The intermediate data computed by crypto_box_beforenm is suitable for both
* crypto_box_afternm and crypto_box_open_afternm, and can be reused for any
* number of messages.
*/
NAN_METHOD(bind_crypto_box_beforenm) {
Nan::EscapableHandleScope scope;
ARGS(2,"arguments publicKey, and secretKey must be buffers");
ARG_TO_UCHAR_BUFFER_LEN(publicKey, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(secretKey, crypto_box_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(k, crypto_box_BEFORENMBYTES);
if( crypto_box_beforenm(k_ptr, publicKey, secretKey) != 0) {
return Nan::ThrowError("crypto_box_beforenm failed");
}
return info.GetReturnValue().Set(k);
}
/**
* Encrypts a given a message m, using partial computed data.
*
* int crypto_box_afternm(
* unsigned char * ctxt,
* const unsigned char * msg,
* unsigned long long mlen,
* const unsigned char * nonce,
* const unsigned char * k)
*
* 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] k the partial computed data.
*
* Returns:
* 0 if operation is successful.
*/
NAN_METHOD(bind_crypto_box_afternm) {
Nan::EscapableHandleScope scope;
ARGS(3,"arguments message, nonce and k must be buffers");
ARG_TO_UCHAR_BUFFER(message);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(k, crypto_box_BEFORENMBYTES);
// Pad the message with crypto_box_ZEROBYTES zeros
NEW_BUFFER_AND_PTR(msg, message_size + crypto_box_ZEROBYTES);
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_afternm(ctxt_ptr, msg_ptr, message_size, nonce, k) == 0) {
return info.GetReturnValue().Set(ctxt);
} else {
return;
}
}
/**
* Decrypts a ciphertext ctxt given the receivers private key, and senders public key.
*
* int crypto_box_open_afternm ( unsigned char * msg,
* const unsigned char * ctxt,
* unsigned long long clen,
* const unsigned char * nonce,
* const unsigned char * k)
*
* 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] k the partial computed data.
*
* Returns:
* 0 if successful and -1 if verification fails.
*
* Precondition:
* first crypto_box_BOXZEROBYTES of ctxt be all 0.
* the nonce must have size crypto_box_NONCEBYTES.
*
* Postcondition:
* first clen bytes of msg will contain the plaintext.
* first crypto_box_ZEROBYTES of msg will be all 0.
*/
NAN_METHOD(bind_crypto_box_open_afternm) {
Nan::EscapableHandleScope scope;
ARGS(3,"arguments cipherText, nonce, k");
ARG_TO_UCHAR_BUFFER(cipherText);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(k, crypto_box_BEFORENMBYTES);
// API requires that the first crypto_box_BOXZEROBYTES of msg be 0 so lets check
if (cipherText_size < crypto_box_BOXZEROBYTES) {
std::ostringstream oss;
oss << "argument cipherText must have a length of at least " << crypto_box_BOXZEROBYTES << " bytes";
return Nan::ThrowError(oss.str().c_str());
}
unsigned int i;
for(i = 0; i < crypto_box_BOXZEROBYTES; i++) {
if( cipherText[i] ) break;
}
if (i < crypto_box_BOXZEROBYTES) {
std::ostringstream oss;
oss << "the first " << crypto_box_BOXZEROBYTES << " bytes of argument cipherText must be 0";
return Nan::ThrowError(oss.str().c_str());
}
NEW_BUFFER_AND_PTR(msg, cipherText_size);
if (crypto_box_open_afternm(msg_ptr, cipherText, cipherText_size, nonce, k) == 0) {
// Remove the padding at the beginning of the message
NEW_BUFFER_AND_PTR(plain_text,cipherText_size - crypto_box_ZEROBYTES);
memcpy(plain_text_ptr,(void*) (msg_ptr + crypto_box_ZEROBYTES), cipherText_size - crypto_box_ZEROBYTES);
return info.GetReturnValue().Set(plain_text);
} else {
return;
}
}
/*
int crypto_box_detached(unsigned char *c,
unsigned char *mac,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk)
*/
NAN_METHOD(bind_crypto_box_detached) {
Nan::EscapableHandleScope scope;
ARGS(5,"arguments mac, message, nonce, and public and private key must be buffers");
ARG_TO_UCHAR_BUFFER_LEN(mac, crypto_secretbox_MACBYTES);
ARG_TO_UCHAR_BUFFER(message);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_secretbox_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(pk, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(sk, crypto_box_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(c, message_size);
if (crypto_box_detached(c_ptr, mac, message, message_size, nonce, pk, sk) == 0) {
return info.GetReturnValue().Set(c);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
*int crypto_box_open_detached(unsigned char *m,
* const unsigned char *c,
const unsigned char *mac,
unsigned long long clen,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk)
*/
NAN_METHOD(bind_crypto_box_open_detached) {
Nan::EscapableHandleScope scope;
ARGS(5,"arguments encrypted message, mac, nonce, and key must be buffers");
ARG_TO_UCHAR_BUFFER(c);
ARG_TO_UCHAR_BUFFER_LEN(mac, crypto_secretbox_MACBYTES);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_secretbox_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(pk, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(sk, crypto_box_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(m, c_size);
if (crypto_box_open_detached(m_ptr, c, mac, c_size, nonce, pk, sk) == 0) {
return info.GetReturnValue().Set(m);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
*int crypto_box_seal(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *pk);
*/
NAN_METHOD(bind_crypto_box_seal) {
Nan::EscapableHandleScope scope;
ARGS(2,"arguments encrypted message, mac, nonce, and key must be buffers");
ARG_TO_UCHAR_BUFFER(message);
ARG_TO_UCHAR_BUFFER_LEN(pk, crypto_box_PUBLICKEYBYTES);
NEW_BUFFER_AND_PTR(c, message_size);
if (crypto_box_seal(c_ptr, message, message_size, pk) == 0) {
return info.GetReturnValue().Set(c);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
*int crypto_box_seal_open(unsigned char *m, const unsigned char *c,
unsigned long long clen,
const unsigned char *pk, const unsigned char *sk)
*/
NAN_METHOD(bind_crypto_box_seal_open) {
Nan::EscapableHandleScope scope;
ARGS(2,"arguments encrypted message, mac, nonce, and key must be buffers");
ARG_TO_UCHAR_BUFFER(c);
ARG_TO_UCHAR_BUFFER_LEN(pk, crypto_box_PUBLICKEYBYTES);
ARG_TO_UCHAR_BUFFER_LEN(sk, crypto_box_SECRETKEYBYTES);
NEW_BUFFER_AND_PTR(m, c_size);
if (crypto_box_seal_open(m_ptr, c, c_size, pk, sk) == 0) {
return info.GetReturnValue().Set(m);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
*int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk,
const unsigned char *seed);
*/
NAN_METHOD(bind_crypto_box_seed_keypair) {
Nan::EscapableHandleScope scope;
ARGS(1,"argument seed must be a buffer");
ARG_TO_UCHAR_BUFFER_LEN(seed, crypto_box_SEEDBYTES);
NEW_BUFFER_AND_PTR(pk, crypto_box_PUBLICKEYBYTES);
NEW_BUFFER_AND_PTR(sk, crypto_box_SECRETKEYBYTES);
if (crypto_box_seed_keypair(pk_ptr, sk_ptr, seed) == 0) {
Local<Object> result = Nan::New<Object>();
result->ForceSet(Nan::New<String>("publicKey").ToLocalChecked(), pk, DontDelete);
result->ForceSet(Nan::New<String>("secretKey").ToLocalChecked(), sk, DontDelete);
return info.GetReturnValue().Set(result);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
int crypto_box_detached_afternm(unsigned char *c, unsigned char *mac,
const unsigned char *m, unsigned long long mlen,
const unsigned char *n, const unsigned char *k);
*/
NAN_METHOD(bind_crypto_box_detached_afternm) {
Nan::EscapableHandleScope scope;
ARGS(4,"arguments message, nonce and k must be buffers");
ARG_TO_UCHAR_BUFFER(message);
ARG_TO_UCHAR_BUFFER_LEN(mac, crypto_box_MACBYTES);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(k, crypto_box_BEFORENMBYTES);
// Pad the message with crypto_box_ZEROBYTES zeros
NEW_BUFFER_AND_PTR(ctxt, message_size);
if (crypto_box_detached_afternm(ctxt_ptr, mac, message, message_size, nonce, k) == 0) {
return info.GetReturnValue().Set(ctxt);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
int crypto_box_open_detached_afternm(unsigned char *m, const unsigned char *c,
const unsigned char *mac,
unsigned long long clen, const unsigned char *n,
const unsigned char *k)
*/
NAN_METHOD(bind_crypto_box_open_detached_afternm) {
Nan::EscapableHandleScope scope;
ARGS(4,"arguments message, nonce and k must be buffers");
ARG_TO_UCHAR_BUFFER(ctxt);
ARG_TO_UCHAR_BUFFER_LEN(mac, crypto_box_MACBYTES);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(k, crypto_box_BEFORENMBYTES);
// Pad the message with crypto_box_ZEROBYTES zeros
NEW_BUFFER_AND_PTR(message, ctxt_size);
if (crypto_box_open_detached_afternm(message_ptr, ctxt, mac, ctxt_size, nonce, k) == 0) {
return info.GetReturnValue().Set(message);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
int crypto_box_easy_afternm(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k);
*/
NAN_METHOD(bind_crypto_box_easy_afternm) {
Nan::EscapableHandleScope scope;
ARGS(3,"arguments message, nonce and k must be buffers");
ARG_TO_UCHAR_BUFFER(message);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(k, crypto_box_BEFORENMBYTES);
// Pad the message with crypto_box_ZEROBYTES zeros
NEW_BUFFER_AND_PTR(ctxt, message_size);
if (crypto_box_easy_afternm(ctxt_ptr, message, message_size, nonce, k) == 0) {
return info.GetReturnValue().Set(ctxt);
}
return info.GetReturnValue().Set(Nan::Null());
}
/*
int crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c,
unsigned long long clen, const unsigned char *n,
const unsigned char *k)
*/
NAN_METHOD(bind_crypto_box_open_easy_afternm) {
Nan::EscapableHandleScope scope;
ARGS(3,"arguments message, nonce and k must be buffers");
ARG_TO_UCHAR_BUFFER(ctxt);
ARG_TO_UCHAR_BUFFER_LEN(nonce, crypto_box_NONCEBYTES);
ARG_TO_UCHAR_BUFFER_LEN(k, crypto_box_BEFORENMBYTES);
// Pad the message with crypto_box_ZEROBYTES zeros
NEW_BUFFER_AND_PTR(message, ctxt_size);
if (crypto_box_open_easy_afternm(message_ptr, ctxt, ctxt_size, nonce, k) == 0) {
return info.GetReturnValue().Set(message);
}
return info.GetReturnValue().Set(Nan::Null());
}
/**
* Register function calls in node binding
*/
void register_crypto_box(Handle<Object> target) {
// Box
NEW_METHOD(crypto_box);
NEW_METHOD(crypto_box_keypair);
NEW_METHOD(crypto_box_easy);
NEW_METHOD(crypto_box_easy_afternm);
NEW_METHOD(crypto_box_beforenm);
NEW_METHOD(crypto_box_afternm);
NEW_METHOD(crypto_box_seed_keypair);
NEW_METHOD(crypto_box_detached);
NEW_METHOD(crypto_box_detached_afternm);
NEW_METHOD(crypto_box_open);
NEW_METHOD(crypto_box_open_afternm);
NEW_METHOD(crypto_box_open_easy);
NEW_METHOD(crypto_box_open_detached);
NEW_METHOD(crypto_box_open_detached_afternm);
NEW_METHOD(crypto_box_open_easy_afternm);
NEW_METHOD(crypto_box_seal);
NEW_METHOD(crypto_box_seal_open);
NEW_INT_PROP(crypto_box_NONCEBYTES);
NEW_INT_PROP(crypto_box_MACBYTES);
NEW_INT_PROP(crypto_box_BEFORENMBYTES);
NEW_INT_PROP(crypto_box_BOXZEROBYTES);
NEW_INT_PROP(crypto_box_PUBLICKEYBYTES);
NEW_INT_PROP(crypto_box_SECRETKEYBYTES);
NEW_INT_PROP(crypto_box_ZEROBYTES);
NEW_INT_PROP(crypto_box_SEEDBYTES);
NEW_INT_PROP(crypto_box_SEALBYTES);
NEW_STRING_PROP(crypto_box_PRIMITIVE);
}