This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsalty_nif.c
2249 lines (1775 loc) · 87.9 KB
/
salty_nif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2017 Jan van de Molengraft <jan@artemisc.eu>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdbool.h>
#include <string.h>
#include "sodium.h"
#include "erl_nif.h"
/*******************************************************************************
*
* IMPORTANT NOTE
*
* The following macros require that every method using them defines its
* arguments using the same names.
*
* (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]);
*
******************************************************************************/
/* SALTY_MAX_CLEAN_SIZE is the maximum amount of bytes a nif call can process
* before we should consider invoking the dirty schedulers.
*
* TODO validate this number, and use it.
* TODO can dirty schedulers be called at runtime?
*/
#define SALTY_MAX_CLEAN_SIZE (16 * 1024)
#define SALTY_NOERR 0
#define SALTY_BADARG enif_make_badarg(env)
#define SALTY_BADALLOC SALTY_BADARG /* TODO make this return an actual failed-to-alloc error */
#define SALTY_ERROR tuple_error_unknown
#define SALTY_ERROR_PAIR(err) enif_make_tuple2(env, atom_error, err)
#define SALTY_OK atom_ok
#define SALTY_OK_PAIR(a) enif_make_tuple2(env, atom_ok, a)
#define SALTY_OK_TRIPLET(a, b) enif_make_tuple3(env, atom_ok, a, b)
#define SALTY_BIN_NO_SIZE 0
/*#define SALTY_FORCE_BOUNDS(val, lower, upper) \
if (val < lower || val > upper) { \
return (SALTY_BADARG); \
}*/
#define SALTY_INPUT_UINT64(index, dst) \
ErlNifUInt64 dst; \
if (!enif_get_uint64(env, argv[index], &dst)) { \
return (SALTY_BADARG); \
}
#define SALTY_INPUT_BIN(index, dst, len) \
ErlNifBinary dst; \
if (!enif_inspect_binary(env, argv[index], &dst)) { \
if (!enif_inspect_iolist_as_binary(env, argv[index], &dst)) { \
return (SALTY_BADARG); \
} \
} \
if (dst.size < len) { \
return (SALTY_BADARG); \
}
/* TODO implement this macro */
#define SALTY_INPUT_RES(index, dst) \
ErlNifResourceType
#define SALTY_OUTPUT_BIN(dst, len) \
ErlNifBinary dst; \
if (!enif_alloc_binary(len, &dst)) { \
return (SALTY_BADALLOC); \
}
#define OUT(a) enif_make_binary(env, &a)
#define SALTY_FUNC(name, args) \
static ERL_NIF_TERM \
salty_##name (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { \
if (argc != args) { \
return (SALTY_BADARG); \
}
#define DO
#define END }
#define END_OK return (SALTY_OK); END
#define END_OK_WITH(out) return (SALTY_OK_PAIR(OUT(out))); END
#define END_OK_WITH2(a, b) return (SALTY_OK_TRIPLET(OUT(a), OUT(b))); END
#define SALTY_CONST_INT64(constant) \
static ERL_NIF_TERM \
salty_##constant (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { \
return enif_make_uint64(env, crypto_##constant); \
}
#define SALTY_CONST_INT64_NOPREFIX(constant) \
static ERL_NIF_TERM \
salty_##constant (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { \
return enif_make_uint64(env, constant); \
}
#define SALTY_EXPORT_NAME(name) #name
#define SALTY_EXPORT_CONS(name, args) { SALTY_EXPORT_NAME(name), args, salty_##name }
#define SALTY_EXPORT_FUNC(name, args) { SALTY_EXPORT_NAME(name), args, salty_##name }
#define SALTY_EXPORT_FUNC_DIRTY(name, args) { SALTY_EXPORT_NAME(name), args, salty_##name, ERL_NIF_DIRTY_JOB_CPU_BOUND }
#define SALTY_CALL_SIMPLE(call) \
if (( call ) != SALTY_NOERR) { \
return (SALTY_ERROR); \
}
#define SALTY_CALL_SIMPLE_WITHERR(call, error) \
if (( call ) != SALTY_NOERR) { \
return (SALTY_ERROR_PAIR(error)); \
}
#define SALTY_CALL(call, output) \
if (( call ) != SALTY_NOERR) { \
enif_release_binary(&output); \
return (SALTY_ERROR); \
}
#define SALTY_CALL2(call, outputa, outputb) \
if (( call ) != SALTY_NOERR) { \
enif_release_binary(&outputa); \
enif_release_binary(&outputb); \
return (SALTY_ERROR); \
}
#define SALTY_CALL_WITHERR(call, error, output) \
if (( call ) != SALTY_NOERR) { \
enif_release_binary(&output); \
return (SALTY_ERROR_PAIR(error)); \
}
/**
* !FIX for randombytes_SEEDBYTES
*/
#define crypto_randombytes_SEEDBYTES randombytes_seedbytes()
#undef randombytes_SEEDBYTES
/**
* Additional self-defined constants that are missing but required
*/
#define crypto_box_curve25519xchacha20poly1305_SEALBYTES (crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES + crypto_box_curve25519xchacha20poly1305_MACBYTES)
#define crypto_box_curve25519xsalsa20poly1305_SEALBYTES (crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES + crypto_box_curve25519xsalsa20poly1305_MACBYTES)
/**
* Additional self defined functions to make writing bindings easier. These are
* all based on code found in libsodium's source.
*/
int
crypto_auth_hmacsha256_final_verify(crypto_auth_hmacsha256_state *state,
const unsigned char *h) {
unsigned char correct[32];
crypto_auth_hmacsha256_final(state, correct);
return crypto_verify_32(h, correct) | (-(h == correct)) |
sodium_memcmp(correct, h, 32);
}
int
crypto_auth_hmacsha512_final_verify(crypto_auth_hmacsha512_state *state,
const unsigned char *h) {
unsigned char correct[64];
crypto_auth_hmacsha512_final(state, correct);
return crypto_verify_64(h, correct) | (-(h == correct)) |
sodium_memcmp(correct, h, 64);
}
int
crypto_auth_hmacsha512256_final_verify(crypto_auth_hmacsha512256_state *state,
const unsigned char *h) {
unsigned char correct[32];
crypto_auth_hmacsha512256_final(state, correct);
return crypto_verify_32(h, correct) | (-(h == correct)) |
sodium_memcmp(correct, h, 32);
}
static int
_crypto_box_curve25519xchacha20poly1305_seal_nonce(unsigned char *nonce,
const unsigned char *pk1,
const unsigned char *pk2) {
crypto_generichash_state st;
crypto_generichash_init(&st, NULL, 0U, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
crypto_generichash_update(&st, pk1, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
crypto_generichash_update(&st, pk2, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
crypto_generichash_final(&st, nonce, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
return 0;
}
int
crypto_box_curve25519xchacha20poly1305_seal(unsigned char *c,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *pk) {
unsigned char nonce[crypto_box_curve25519xchacha20poly1305_NONCEBYTES];
unsigned char epk[crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES];
unsigned char esk[crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES];
int ret;
if (crypto_box_curve25519xchacha20poly1305_keypair(epk, esk) != 0) {
return -1; /* LCOV_EXCL_LINE */
}
memcpy(c, epk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
_crypto_box_curve25519xchacha20poly1305_seal_nonce(nonce, epk, pk);
ret = crypto_box_easy(c + crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES,
m, mlen, nonce, pk, esk);
sodium_memzero(esk, sizeof esk);
sodium_memzero(epk, sizeof epk);
sodium_memzero(nonce, sizeof nonce);
return ret;
}
int
crypto_box_curve25519xchacha20poly1305_seal_open(unsigned char *m,
const unsigned char *c,
unsigned long long clen,
const unsigned char *pk,
const unsigned char *sk) {
unsigned char nonce[crypto_box_curve25519xchacha20poly1305_NONCEBYTES];
if (clen < crypto_box_curve25519xchacha20poly1305_SEALBYTES) {
return -1;
}
_crypto_box_curve25519xchacha20poly1305_seal_nonce(nonce, c, pk);
/*COMPILER_ASSERT(crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES < crypto_box_curve25519xchacha20poly1305_SEALBYTES);*/
return crypto_box_open_easy(m, c + crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES,
clen - crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES,
nonce, c, sk);
}
int
crypto_box_curve25519xsalsa20poly1305_easy(unsigned char *c,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk) {
return crypto_box_easy(c, m, mlen, n, pk, sk);
}
int
crypto_box_curve25519xsalsa20poly1305_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) {
return crypto_box_detached(c, mac, m, mlen, n, pk, sk);
}
int
crypto_box_curve25519xsalsa20poly1305_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) {
return crypto_box_open_detached(m, c, mac, clen, n, pk, sk);
}
int
crypto_box_curve25519xsalsa20poly1305_easy_afternm(unsigned char *c,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k) {
return crypto_box_easy_afternm(c, m, mlen, n, k);
}
int
crypto_box_curve25519xsalsa20poly1305_detached_afternm(unsigned char *c,
unsigned char *mac,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k) {
return crypto_box_detached_afternm(c, mac, m, mlen, n, k);
}
int
crypto_box_curve25519xsalsa20poly1305_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) {
return crypto_box_open_detached_afternm(m, c, mac, clen, n, k);
}
int
crypto_box_curve25519xsalsa20poly1305_seal(unsigned char *c,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *pk) {
return crypto_box_seal(c, m, mlen, pk);
}
int
crypto_box_curve25519xsalsa20poly1305_seal_open(unsigned char *m,
const unsigned char *c,
unsigned long long clen,
const unsigned char *pk,
const unsigned char *sk) {
return crypto_box_seal_open(m, c, clen, pk, sk);
}
int
crypto_hash_sha256_verify(const unsigned char *h,
const unsigned char *in,
unsigned long long inlen) {
unsigned char correct[32];
crypto_hash_sha256(correct, in, inlen);
return crypto_verify_32(h, correct) | (-(h == correct)) |
sodium_memcmp(correct, h, 32);
}
int
crypto_hash_sha256_final_verify(crypto_hash_sha256_state *state,
const unsigned char *h) {
unsigned char correct[32];
crypto_hash_sha256_final(state, correct);
return crypto_verify_32(h, correct) | (-(h == correct)) |
sodium_memcmp(correct, h, 32);
}
int
crypto_hash_sha512_verify(const unsigned char *h,
const unsigned char *in,
unsigned long long inlen) {
unsigned char correct[64];
crypto_hash_sha512(correct, in, inlen);
return crypto_verify_64(h, correct) | (-(h == correct)) |
sodium_memcmp(correct, h, 64);
}
int
crypto_hash_sha512_final_verify(crypto_hash_sha512_state *state,
const unsigned char *h) {
unsigned char correct[64];
crypto_hash_sha512_final(state, correct);
return crypto_verify_64(h, correct) | (-(h == correct)) |
sodium_memcmp(correct, h, 64);
}
int
crypto_onetimeauth_poly1305_final_verify(crypto_onetimeauth_poly1305_state *state,
const unsigned char *h) {
unsigned char correct[16];
crypto_onetimeauth_poly1305_final(state, correct);
return crypto_verify_16(h, correct);
}
int
crypto_secretbox_xsalsa20poly1305_easy(unsigned char *c,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k) {
return crypto_secretbox_easy(c, m, mlen, n, k);
}
int
crypto_secretbox_xsalsa20poly1305_detached(unsigned char *c,
unsigned char *mac,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k) {
return crypto_secretbox_detached(c, mac, m, mlen, n, k);
}
int
crypto_secretbox_xsalsa20poly1305_open_detached(unsigned char *m,
const unsigned char *c,
const unsigned char *mac,
unsigned long long clen,
const unsigned char *n,
const unsigned char *k) {
return crypto_secretbox_open_detached(m, c, mac, clen, n, k);
}
/* STATIC VALUES */
ERL_NIF_TERM atom_ok;
ERL_NIF_TERM atom_error;
ERL_NIF_TERM atom_error_no_match;
ERL_NIF_TERM atom_error_not_available;
ERL_NIF_TERM atom_error_forged;
ERL_NIF_TERM atom_error_unknown;
ERL_NIF_TERM tuple_error_unknown;
/* erl_nif code */
static int
salty_onload(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) {
/* register the safe resource types for keys and private data */
/* cache atom values */
atom_ok = enif_make_atom(env, "ok");
atom_error = enif_make_atom(env, "error");
atom_error_no_match = enif_make_atom(env, "no_match");
atom_error_not_available = enif_make_atom(env, "not_available");
atom_error_forged = enif_make_atom(env, "forged");
atom_error_unknown = enif_make_atom(env, "salty_error_unknown");
tuple_error_unknown = enif_make_tuple2(env, atom_error, atom_error_unknown);
return 0;
}
/**
* Sodium internal
*/
/* sodium_init */
SALTY_FUNC(init, 0) DO
SALTY_CALL_SIMPLE(sodium_init());
END_OK;
/* sodium_memcmp */
SALTY_FUNC(memcmp, 2) DO
SALTY_INPUT_BIN(0, a, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, b, SALTY_BIN_NO_SIZE);
if (a.size != b.size) {
return (SALTY_ERROR);
}
SALTY_CALL_SIMPLE(sodium_memcmp(a.data, b.data, a.size));
END_OK;
/* safe key-gen (using locked memory binary resource) */
/**
* AEAD aes256gcm
*/
SALTY_CONST_INT64(aead_aes256gcm_KEYBYTES);
SALTY_CONST_INT64(aead_aes256gcm_NSECBYTES);
SALTY_CONST_INT64(aead_aes256gcm_NPUBBYTES);
SALTY_CONST_INT64(aead_aes256gcm_ABYTES);
SALTY_FUNC(aead_aes256gcm_is_available, 0) DO
if (crypto_aead_aes256gcm_is_available() == 0) {
return (SALTY_ERROR_PAIR(atom_error_not_available));
}
END_OK;
SALTY_FUNC(aead_aes256gcm_encrypt, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_aes256gcm_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_aes256gcm_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_aes256gcm_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, crypto_aead_aes256gcm_ABYTES + plain.size);
SALTY_CALL(crypto_aead_aes256gcm_encrypt(
cipher.data, NULL, plain.data, plain.size, ad.data, ad.size,
NULL, npub.data, key.data), cipher);
END_OK_WITH(cipher);
SALTY_FUNC(aead_aes256gcm_encrypt_detached, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_aes256gcm_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_aes256gcm_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_aes256gcm_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, plain.size);
SALTY_OUTPUT_BIN(mac, crypto_aead_aes256gcm_ABYTES);
SALTY_CALL2(crypto_aead_aes256gcm_encrypt_detached(
cipher.data, mac.data, NULL, plain.data, plain.size, ad.data,
ad.size, NULL, npub.data, key.data), cipher, mac);
END_OK_WITH2(cipher, mac);
SALTY_FUNC(aead_aes256gcm_decrypt_detached, 6) DO
/*SALTY_INPUT_BIN(0, nsec, crypto_aead_aes256gcm_NSECBYTES);*/
SALTY_INPUT_BIN(1, cipher, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, mac, crypto_aead_aes256gcm_ABYTES);
SALTY_INPUT_BIN(3, ad, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(4, npub, crypto_aead_aes256gcm_NPUBBYTES);
SALTY_INPUT_BIN(5, key, crypto_aead_aes256gcm_KEYBYTES);
SALTY_OUTPUT_BIN(plain, cipher.size);
SALTY_CALL_WITHERR(crypto_aead_aes256gcm_decrypt_detached(
plain.data, NULL, cipher.data, cipher.size, mac.data,
ad.data, ad.size, npub.data, key.data),
atom_error_forged, plain);
END_OK_WITH(plain);
/**
* AEAD chacha20poly1305
*/
SALTY_CONST_INT64(aead_chacha20poly1305_KEYBYTES);
SALTY_CONST_INT64(aead_chacha20poly1305_NSECBYTES);
SALTY_CONST_INT64(aead_chacha20poly1305_NPUBBYTES);
SALTY_CONST_INT64(aead_chacha20poly1305_ABYTES);
SALTY_FUNC(aead_chacha20poly1305_encrypt, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_chacha20poly1305_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_chacha20poly1305_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_chacha20poly1305_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, crypto_aead_chacha20poly1305_ABYTES + plain.size);
SALTY_CALL(crypto_aead_chacha20poly1305_encrypt(
cipher.data, NULL, plain.data, plain.size, ad.data, ad.size,
NULL, npub.data, key.data), cipher);
END_OK_WITH(cipher);
SALTY_FUNC(aead_chacha20poly1305_encrypt_detached, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_chacha20poly1305_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_chacha20poly1305_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_chacha20poly1305_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, plain.size);
SALTY_OUTPUT_BIN(mac, crypto_aead_chacha20poly1305_ABYTES);
SALTY_CALL2(crypto_aead_chacha20poly1305_encrypt_detached(
cipher.data, mac.data, NULL, plain.data, plain.size, ad.data,
ad.size, NULL, npub.data, key.data), cipher, mac);
END_OK_WITH2(cipher, mac);
SALTY_FUNC(aead_chacha20poly1305_decrypt_detached, 6) DO
/*SALTY_INPUT_BIN(0, nsec, crypto_aead_chacha20poly1305_NSECBYTES);*/
SALTY_INPUT_BIN(1, cipher, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, mac, crypto_aead_chacha20poly1305_ABYTES);
SALTY_INPUT_BIN(3, ad, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(4, npub, crypto_aead_chacha20poly1305_NPUBBYTES);
SALTY_INPUT_BIN(5, key, crypto_aead_chacha20poly1305_KEYBYTES);
SALTY_OUTPUT_BIN(plain, cipher.size);
SALTY_CALL_WITHERR(crypto_aead_chacha20poly1305_decrypt_detached(
plain.data, NULL, cipher.data, cipher.size, mac.data,
ad.data, ad.size, npub.data, key.data),
atom_error_forged, plain);
END_OK_WITH(plain);
/**
* AEAD chacha20poly1305_ietf
*/
SALTY_CONST_INT64(aead_chacha20poly1305_ietf_KEYBYTES);
SALTY_CONST_INT64(aead_chacha20poly1305_ietf_NSECBYTES);
SALTY_CONST_INT64(aead_chacha20poly1305_ietf_NPUBBYTES);
SALTY_CONST_INT64(aead_chacha20poly1305_ietf_ABYTES);
SALTY_FUNC(aead_chacha20poly1305_ietf_encrypt, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_chacha20poly1305_ietf_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_chacha20poly1305_ietf_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, crypto_aead_chacha20poly1305_ietf_ABYTES + plain.size);
SALTY_CALL(crypto_aead_chacha20poly1305_ietf_encrypt(
cipher.data, NULL, plain.data, plain.size, ad.data, ad.size,
NULL, npub.data, key.data), cipher);
END_OK_WITH(cipher);
SALTY_FUNC(aead_chacha20poly1305_ietf_encrypt_detached, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_chacha20poly1305_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_chacha20poly1305_ietf_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, plain.size);
SALTY_OUTPUT_BIN(mac, crypto_aead_chacha20poly1305_ietf_ABYTES);
SALTY_CALL2(crypto_aead_chacha20poly1305_ietf_encrypt_detached(
cipher.data, mac.data, NULL, plain.data, plain.size, ad.data,
ad.size, NULL, npub.data, key.data), cipher, mac);
END_OK_WITH2(cipher, mac);
SALTY_FUNC(aead_chacha20poly1305_ietf_decrypt_detached, 6) DO
/*SALTY_INPUT_BIN(0, nsec, crypto_aead_chacha20poly1305_ietf_NSECBYTES);*/
SALTY_INPUT_BIN(1, cipher, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, mac, crypto_aead_chacha20poly1305_ietf_ABYTES);
SALTY_INPUT_BIN(3, ad, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(4, npub, crypto_aead_chacha20poly1305_ietf_NPUBBYTES);
SALTY_INPUT_BIN(5, key, crypto_aead_chacha20poly1305_ietf_KEYBYTES);
SALTY_OUTPUT_BIN(plain, cipher.size);
SALTY_CALL_WITHERR(crypto_aead_chacha20poly1305_ietf_decrypt_detached(
plain.data, NULL, cipher.data, cipher.size, mac.data,
ad.data, ad.size, npub.data, key.data),
atom_error_forged, plain);
END_OK_WITH(plain);
/**
* AEAD xchacha20poly1305_ietf
*/
SALTY_CONST_INT64(aead_xchacha20poly1305_ietf_KEYBYTES);
SALTY_CONST_INT64(aead_xchacha20poly1305_ietf_NSECBYTES);
SALTY_CONST_INT64(aead_xchacha20poly1305_ietf_NPUBBYTES);
SALTY_CONST_INT64(aead_xchacha20poly1305_ietf_ABYTES);
SALTY_FUNC(aead_xchacha20poly1305_ietf_encrypt, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_xchacha20poly1305_ietf_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, crypto_aead_xchacha20poly1305_ietf_ABYTES + plain.size);
SALTY_CALL(crypto_aead_xchacha20poly1305_ietf_encrypt(
cipher.data, NULL, plain.data, plain.size, ad.data, ad.size,
NULL, npub.data, key.data), cipher);
END_OK_WITH(cipher);
SALTY_FUNC(aead_xchacha20poly1305_ietf_encrypt_detached, 5) DO
SALTY_INPUT_BIN(0, plain, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, ad, SALTY_BIN_NO_SIZE);
/*SALTY_INPUT_BIN(2, nsec, crypto_aead_xchacha20poly1305_NSECBYTES);*/
SALTY_INPUT_BIN(3, npub, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
SALTY_INPUT_BIN(4, key, crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
SALTY_OUTPUT_BIN(cipher, plain.size);
SALTY_OUTPUT_BIN(mac, crypto_aead_xchacha20poly1305_ietf_ABYTES);
SALTY_CALL2(crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
cipher.data, mac.data, NULL, plain.data, plain.size, ad.data,
ad.size, NULL, npub.data, key.data), cipher, mac);
END_OK_WITH2(cipher, mac);
SALTY_FUNC(aead_xchacha20poly1305_ietf_decrypt_detached, 6) DO
/*SALTY_INPUT_BIN(0, nsec, crypto_aead_xchacha20poly1305_ietf_NSECBYTES);*/
SALTY_INPUT_BIN(1, cipher, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, mac, crypto_aead_xchacha20poly1305_ietf_ABYTES);
SALTY_INPUT_BIN(3, ad, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(4, npub, crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
SALTY_INPUT_BIN(5, key, crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
SALTY_OUTPUT_BIN(plain, cipher.size);
SALTY_CALL_WITHERR(crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
plain.data, NULL, cipher.data, cipher.size, mac.data,
ad.data, ad.size, npub.data, key.data),
atom_error_forged, plain);
END_OK_WITH(plain);
/**
* AUTH hmacsha256
*/
SALTY_CONST_INT64(auth_hmacsha256_BYTES);
SALTY_CONST_INT64(auth_hmacsha256_KEYBYTES);
SALTY_FUNC(auth_hmacsha256, 2) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, key, crypto_auth_hmacsha256_KEYBYTES);
SALTY_OUTPUT_BIN(mac, crypto_auth_hmacsha256_BYTES);
SALTY_CALL(crypto_auth_hmacsha256(mac.data, msg.data, msg.size, key.data), mac);
END_OK_WITH(mac);
SALTY_FUNC(auth_hmacsha256_verify, 3) DO
SALTY_INPUT_BIN(0, mac, crypto_auth_hmacsha256_BYTES);
SALTY_INPUT_BIN(1, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, key, crypto_auth_hmacsha256_KEYBYTES);
SALTY_CALL_SIMPLE_WITHERR(crypto_auth_hmacsha256_verify(
mac.data, msg.data, msg.size, key.data),
atom_error_no_match);
END_OK;
SALTY_FUNC(auth_hmacsha256_init, 1) DO
SALTY_INPUT_BIN(0, key, SALTY_BIN_NO_SIZE);
SALTY_OUTPUT_BIN(state, crypto_auth_hmacsha256_statebytes());
SALTY_CALL(crypto_auth_hmacsha256_init(
(crypto_auth_hmacsha256_state *) state.data,
key.data, key.size), state);
END_OK_WITH(state);
SALTY_FUNC(auth_hmacsha256_update, 2) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha256_statebytes());
SALTY_INPUT_BIN(1, input, SALTY_BIN_NO_SIZE);
SALTY_CALL(crypto_auth_hmacsha256_update(
(crypto_auth_hmacsha256_state *) state.data,
input.data, input.size), state);
END_OK_WITH(state);
SALTY_FUNC(auth_hmacsha256_final, 1) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha256_statebytes());
SALTY_OUTPUT_BIN(hash, crypto_auth_hmacsha256_BYTES);
SALTY_CALL(crypto_auth_hmacsha256_final(
(crypto_auth_hmacsha256_state *) state.data,
hash.data), hash);
END_OK_WITH(hash);
SALTY_FUNC(auth_hmacsha256_final_verify, 2) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha256_statebytes());
SALTY_INPUT_BIN(1, expect, crypto_auth_hmacsha256_BYTES);
SALTY_CALL_SIMPLE_WITHERR(crypto_auth_hmacsha256_final_verify(
(crypto_auth_hmacsha256_state *) state.data, expect.data),
atom_error_forged);
END_OK;
/**
* AUTH hmacsha512
*/
SALTY_CONST_INT64(auth_hmacsha512_BYTES);
SALTY_CONST_INT64(auth_hmacsha512_KEYBYTES);
SALTY_FUNC(auth_hmacsha512, 2) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, key, crypto_auth_hmacsha512_KEYBYTES);
SALTY_OUTPUT_BIN(mac, crypto_auth_hmacsha512_BYTES);
SALTY_CALL(crypto_auth_hmacsha512(mac.data, msg.data, msg.size, key.data), mac);
END_OK_WITH(mac);
SALTY_FUNC(auth_hmacsha512_verify, 3) DO
SALTY_INPUT_BIN(0, mac, crypto_auth_hmacsha512_BYTES);
SALTY_INPUT_BIN(1, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, key, crypto_auth_hmacsha512_KEYBYTES);
SALTY_CALL_SIMPLE_WITHERR(crypto_auth_hmacsha512_verify(
mac.data, msg.data, msg.size, key.data),
atom_error_no_match);
END_OK;
SALTY_FUNC(auth_hmacsha512_init, 1) DO
SALTY_INPUT_BIN(0, key, SALTY_BIN_NO_SIZE);
SALTY_OUTPUT_BIN(state, crypto_auth_hmacsha512_statebytes());
SALTY_CALL(crypto_auth_hmacsha512_init(
(crypto_auth_hmacsha512_state *) state.data,
key.data, key.size), state);
END_OK_WITH(state);
SALTY_FUNC(auth_hmacsha512_update, 2) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha512_statebytes());
SALTY_INPUT_BIN(1, input, SALTY_BIN_NO_SIZE);
SALTY_CALL(crypto_auth_hmacsha512_update(
(crypto_auth_hmacsha512_state *) state.data,
input.data, input.size), state);
END_OK_WITH(state);
SALTY_FUNC(auth_hmacsha512_final, 1) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha512_statebytes());
SALTY_OUTPUT_BIN(hash, crypto_auth_hmacsha512_BYTES);
SALTY_CALL(crypto_auth_hmacsha512_final(
(crypto_auth_hmacsha512_state *) state.data,
hash.data), hash);
END_OK_WITH(hash);
SALTY_FUNC(auth_hmacsha512_final_verify, 2) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha512_statebytes());
SALTY_INPUT_BIN(1, expect, crypto_auth_hmacsha512_BYTES);
SALTY_CALL_SIMPLE_WITHERR(crypto_auth_hmacsha512_final_verify(
(crypto_auth_hmacsha512_state *) state.data, expect.data),
atom_error_forged);
END_OK;
/**
* AUTH hmacsha512256
*/
SALTY_CONST_INT64(auth_hmacsha512256_BYTES);
SALTY_CONST_INT64(auth_hmacsha512256_KEYBYTES);
SALTY_FUNC(auth_hmacsha512256, 2) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, key, crypto_auth_hmacsha512256_KEYBYTES);
SALTY_OUTPUT_BIN(mac, crypto_auth_hmacsha512256_BYTES);
SALTY_CALL(crypto_auth_hmacsha512256(mac.data, msg.data, msg.size, key.data), mac);
END_OK_WITH(mac);
SALTY_FUNC(auth_hmacsha512256_verify, 3) DO
SALTY_INPUT_BIN(0, mac, crypto_auth_hmacsha512256_BYTES);
SALTY_INPUT_BIN(1, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(2, key, crypto_auth_hmacsha512256_KEYBYTES);
SALTY_CALL_SIMPLE_WITHERR(crypto_auth_hmacsha512256_verify(
mac.data, msg.data, msg.size, key.data),
atom_error_no_match);
END_OK;
SALTY_FUNC(auth_hmacsha512256_init, 1) DO
SALTY_INPUT_BIN(0, key, SALTY_BIN_NO_SIZE);
SALTY_OUTPUT_BIN(state, crypto_auth_hmacsha512256_statebytes());
SALTY_CALL(crypto_auth_hmacsha512256_init(
(crypto_auth_hmacsha512256_state *) state.data,
key.data, key.size), state);
END_OK_WITH(state);
SALTY_FUNC(auth_hmacsha512256_update, 2) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha512256_statebytes());
SALTY_INPUT_BIN(1, input, SALTY_BIN_NO_SIZE);
SALTY_CALL(crypto_auth_hmacsha512256_update(
(crypto_auth_hmacsha512256_state *) state.data,
input.data, input.size), state);
END_OK_WITH(state);
SALTY_FUNC(auth_hmacsha512256_final, 1) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha512256_statebytes());
SALTY_OUTPUT_BIN(hash, crypto_auth_hmacsha512256_BYTES);
SALTY_CALL(crypto_auth_hmacsha512256_final(
(crypto_auth_hmacsha512256_state *) state.data,
hash.data), hash);
END_OK_WITH(hash);
SALTY_FUNC(auth_hmacsha512256_final_verify, 2) DO
SALTY_INPUT_BIN(0, state, crypto_auth_hmacsha512256_statebytes());
SALTY_INPUT_BIN(1, expect, crypto_auth_hmacsha512256_BYTES);
SALTY_CALL_SIMPLE_WITHERR(crypto_auth_hmacsha512256_final_verify(
(crypto_auth_hmacsha512256_state *) state.data, expect.data),
atom_error_forged);
END_OK;
/**
* BOX curve25519xchacha20poly1305
*/
SALTY_CONST_INT64(box_curve25519xchacha20poly1305_SEEDBYTES);
SALTY_CONST_INT64(box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_CONST_INT64(box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_CONST_INT64(box_curve25519xchacha20poly1305_BEFORENMBYTES);
SALTY_CONST_INT64(box_curve25519xchacha20poly1305_NONCEBYTES);
SALTY_CONST_INT64(box_curve25519xchacha20poly1305_MACBYTES);
SALTY_CONST_INT64(box_curve25519xchacha20poly1305_SEALBYTES);
SALTY_FUNC(box_curve25519xchacha20poly1305_seed_keypair, 1) DO
SALTY_INPUT_BIN(0, seed, crypto_box_curve25519xchacha20poly1305_SEEDBYTES);
SALTY_OUTPUT_BIN(pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_OUTPUT_BIN(sk, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_CALL2(crypto_box_curve25519xchacha20poly1305_seed_keypair(
pk.data, sk.data, seed.data), pk, sk);
END_OK_WITH2(pk, sk);
SALTY_FUNC(box_curve25519xchacha20poly1305_keypair, 0) DO
SALTY_OUTPUT_BIN(pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_OUTPUT_BIN(sk, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_CALL2(crypto_box_curve25519xchacha20poly1305_keypair(
pk.data, sk.data), pk, sk);
END_OK_WITH2(pk, sk);
SALTY_FUNC(box_curve25519xchacha20poly1305_easy, 4) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, nonce, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
SALTY_INPUT_BIN(2, pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_INPUT_BIN(3, sk, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_OUTPUT_BIN(cipher, crypto_box_curve25519xchacha20poly1305_MACBYTES + msg.size);
SALTY_CALL(crypto_box_curve25519xchacha20poly1305_easy(cipher.data,
msg.data, msg.size, nonce.data, pk.data, sk.data), cipher);
END_OK_WITH(cipher);
SALTY_FUNC(box_curve25519xchacha20poly1305_detached, 4) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, nonce, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
SALTY_INPUT_BIN(2, pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_INPUT_BIN(3, sk, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_OUTPUT_BIN(cipher, msg.size);
SALTY_OUTPUT_BIN(mac, crypto_box_curve25519xchacha20poly1305_MACBYTES);
SALTY_CALL2(crypto_box_curve25519xchacha20poly1305_detached(cipher.data,
mac.data, msg.data, msg.size, nonce.data, pk.data, sk.data),
cipher, mac);
END_OK_WITH2(cipher, mac);
SALTY_FUNC(box_curve25519xchacha20poly1305_open_detached, 5) DO
SALTY_INPUT_BIN(0, cipher, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, mac, crypto_box_curve25519xchacha20poly1305_MACBYTES);
SALTY_INPUT_BIN(2, nonce, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
SALTY_INPUT_BIN(3, pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_INPUT_BIN(4, sk, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_OUTPUT_BIN(msg, cipher.size);
SALTY_CALL_WITHERR(crypto_box_curve25519xchacha20poly1305_open_detached(
msg.data, cipher.data, mac.data, cipher.size, nonce.data, pk.data, sk.data),
atom_error_forged, msg);
END_OK_WITH(msg);
SALTY_FUNC(box_curve25519xchacha20poly1305_beforenm, 2) DO
SALTY_INPUT_BIN(1, pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_INPUT_BIN(2, sk, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_OUTPUT_BIN(k, crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES);
SALTY_CALL(crypto_box_curve25519xchacha20poly1305_beforenm(
k.data, pk.data, sk.data), k);
END_OK_WITH(k);
SALTY_FUNC(box_curve25519xchacha20poly1305_easy_afternm, 3) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, nonce, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
SALTY_INPUT_BIN(2, k, crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES);
SALTY_OUTPUT_BIN(cipher, crypto_box_curve25519xchacha20poly1305_MACBYTES + msg.size);
SALTY_CALL(crypto_box_curve25519xchacha20poly1305_easy_afternm(cipher.data,
msg.data, msg.size, nonce.data, k.data), cipher);
END_OK_WITH(cipher);
SALTY_FUNC(box_curve25519xchacha20poly1305_detached_afternm, 3) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, nonce, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
SALTY_INPUT_BIN(2, k, crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES);
SALTY_OUTPUT_BIN(cipher, msg.size);
SALTY_OUTPUT_BIN(mac, crypto_box_curve25519xchacha20poly1305_MACBYTES);
SALTY_CALL2(crypto_box_curve25519xchacha20poly1305_detached_afternm(
cipher.data, mac.data, msg.data, msg.size, nonce.data, k.data),
cipher, mac);
END_OK_WITH2(cipher, mac);
SALTY_FUNC(box_curve25519xchacha20poly1305_open_detached_afternm, 4) DO
SALTY_INPUT_BIN(0, cipher, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, mac, crypto_box_curve25519xchacha20poly1305_MACBYTES);
SALTY_INPUT_BIN(2, nonce, crypto_box_curve25519xchacha20poly1305_NONCEBYTES);
SALTY_INPUT_BIN(3, k, crypto_box_curve25519xchacha20poly1305_BEFORENMBYTES);
SALTY_OUTPUT_BIN(msg, cipher.size);
SALTY_CALL_WITHERR(crypto_box_curve25519xchacha20poly1305_open_detached_afternm(
msg.data, cipher.data, mac.data, cipher.size, nonce.data, k.data),
atom_error_forged, msg);
END_OK_WITH(msg);
SALTY_FUNC(box_curve25519xchacha20poly1305_seal, 2) DO
SALTY_INPUT_BIN(0, msg, SALTY_BIN_NO_SIZE);
SALTY_INPUT_BIN(1, pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_OUTPUT_BIN(cipher, crypto_box_curve25519xchacha20poly1305_SEALBYTES + msg.size);
SALTY_CALL(crypto_box_curve25519xchacha20poly1305_seal(
cipher.data, msg.data, msg.size, pk.data), cipher);
END_OK_WITH(cipher);
SALTY_FUNC(box_curve25519xchacha20poly1305_seal_open, 3) DO
SALTY_INPUT_BIN(0, cipher, crypto_box_curve25519xchacha20poly1305_SEALBYTES);
SALTY_INPUT_BIN(1, pk, crypto_box_curve25519xchacha20poly1305_PUBLICKEYBYTES);
SALTY_INPUT_BIN(2, sk, crypto_box_curve25519xchacha20poly1305_SECRETKEYBYTES);
SALTY_OUTPUT_BIN(msg, cipher.size - crypto_box_curve25519xchacha20poly1305_SEALBYTES);