-
Notifications
You must be signed in to change notification settings - Fork 7
/
jhydro.c
1191 lines (1107 loc) · 53.4 KB
/
jhydro.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 (c) 2019 Calvin Rose
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <janet.h>
#include <stdlib.h>
#include <hydrogen.h>
#include <math.h>
/*********/
/* Utils */
/*********/
/* Get an optional buffer for keygen functions, and ensure it has capacity for
* len bytes */
static JanetBuffer *util_keygen_prep(int32_t argc, const Janet *argv, int len) {
janet_arity(argc, 0, 1);
JanetBuffer *buffer;
if (argc == 0) {
buffer = janet_buffer(len);
buffer->count = len;
} else {
buffer = janet_getbuffer(argv, 0);
janet_buffer_ensure(buffer, len, 1);
if (buffer->count < len) {
buffer->count = len;
}
}
return buffer;
}
/* Get a byte view with at least nbytes bytes. Otherwise the same janet_getbytes. */
static JanetByteView util_getnbytes(const Janet *argv, int32_t n, int nbytes) {
JanetByteView view = janet_getbytes(argv, n);
if (view.len != nbytes) {
janet_panicf("bad slot #%d, expected %d bytes, got %d", n, nbytes, view.len);
}
return view;
}
/* Get a positive, 32 bit integer */
static int32_t util_getnat(const Janet *argv, int32_t n) {
int32_t x = janet_getinteger(argv, n);
if (x < 0) {
janet_panicf("bad slot #%d, expected non-negative integer, got %d", n, x);
}
return x;
}
/****************************/
/* Random Number Generation */
/****************************/
static Janet cfun_random_u32(int32_t argc, Janet *argv) {
(void) argv;
janet_fixarity(argc, 0);
uint32_t x = hydro_random_u32();
return janet_wrap_number((double) x);
}
static Janet cfun_random_uniform(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
double d = janet_getnumber(argv, 0);
if (d < 0 || d > UINT32_MAX || floor(d) != d) {
janet_panicf("expected integer in range [0, 2^32), got %v", argv[0]);
}
uint32_t x = hydro_random_uniform((uint32_t) d);
return janet_wrap_number((double) x);
}
static Janet cfun_random_buf(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
JanetBuffer *buf;
size_t outlen;
if (janet_checktype(argv[0], JANET_NUMBER)) {
janet_fixarity(argc, 1);
size_t outlen = janet_getsize(argv, 0);
if (outlen > INT32_MAX) janet_panic("size too large");
buf = janet_buffer(outlen);
hydro_random_buf(buf->data, outlen);
buf->count = outlen;
} else {
buf = janet_getbuffer(argv, 0);
if (argc < 2) {
outlen = buf->count;
buf->count = 0;
} else {
outlen = janet_getsize(argv, 1);
janet_buffer_extra(buf, outlen);
}
if (outlen > INT32_MAX) janet_panic("size too large");
hydro_random_buf(buf->data + buf->count, outlen);
buf->count += outlen;
}
return janet_wrap_buffer(buf);
}
static Janet cfun_random_ratchet(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
hydro_random_ratchet();
return janet_wrap_nil();
}
static Janet cfun_random_reseed(int32_t argc, Janet *argv) {
janet_fixarity(argc, 0);
(void) argv;
hydro_random_reseed();
return janet_wrap_nil();
}
static Janet cfun_random_buf_deterministic(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
JanetBuffer *buf = janet_getbuffer(argv, 0);
size_t len = janet_getsize(argv, 1);
if (len > INT32_MAX) janet_panic("size too large");
JanetByteView seed = util_getnbytes(argv, 2, hydro_random_SEEDBYTES);
janet_buffer_extra(buf, len);
hydro_random_buf_deterministic(buf->data + buf->count, len, seed.bytes);
buf->count += len;
return janet_wrap_buffer(buf);
}
/***********/
/* Hashing */
/***********/
static Janet cfun_hash_keygen(int32_t argc, Janet *argv) {
JanetBuffer *buffer = util_keygen_prep(argc, argv, hydro_hash_KEYBYTES);
hydro_hash_keygen(buffer->data);
return janet_wrap_buffer(buffer);
}
static const JanetAbstractType HashState = {
"jhydro/hash-state",
#ifdef JANET_ATEND_NAME
JANET_ATEND_NAME
#endif
};
static Janet cfun_hash_new(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JanetByteView ctx = util_getnbytes(argv, 0, hydro_hash_CONTEXTBYTES);
JanetByteView key = util_getnbytes(argv, 1, hydro_hash_KEYBYTES);
hydro_hash_state *state = janet_abstract(&HashState, sizeof(hydro_hash_state));
int result = hydro_hash_init(state, (const char *) ctx.bytes, key.bytes);
if (result) {
janet_panic("failed to create hash-state");
}
return janet_wrap_abstract(state);
}
static Janet cfun_hash_update(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
hydro_hash_state *state = janet_getabstract(argv, 0, &HashState);
JanetByteView bytes = janet_getbytes(argv, 1);
int result = hydro_hash_update(state, (const char *) bytes.bytes, bytes.len);
if (result) {
janet_panic("failed to update hash-state");
}
return argv[0];
}
static Janet cfun_hash_final(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
hydro_hash_state *state = janet_getabstract(argv, 0, &HashState);
int32_t outlen = janet_getinteger(argv, 1);
if (outlen < 1) {
janet_panicf("outlen must be a positive integer, got %v", argv[1]);
}
uint8_t *out = janet_string_begin(outlen);
int result = hydro_hash_final(state, out, outlen);
if (result) {
janet_panic("failed to generate hash");
}
return janet_wrap_string(janet_string_end(out));
}
static Janet cfun_hash_hash(int32_t argc, Janet *argv) {
janet_arity(argc, 3, 4);
int32_t size = util_getnat(argv, 0);
if (size < hydro_hash_BYTES_MIN || size > hydro_hash_BYTES_MAX)
janet_panicf("hash size must be in range [%d, %d], got %v",
hydro_hash_BYTES_MIN, hydro_hash_BYTES_MAX,
argv[0]);
JanetByteView msg = janet_getbytes(argv, 1);
JanetByteView ctx = util_getnbytes(argv, 2, hydro_hash_CONTEXTBYTES);
JanetByteView key;
key.bytes = NULL;
key.len = 0;
if (argc >= 4 && !janet_checktype(argv[4], JANET_NIL)) {
key = util_getnbytes(argv, 3, hydro_hash_KEYBYTES);
}
uint8_t *out = janet_string_begin(size);
int result = hydro_hash_hash(out, size, (const char *) msg.bytes, msg.len, (const char *) ctx.bytes, key.bytes);
if (result) {
janet_panic("failed to hash message");
}
return janet_wrap_string(janet_string_end(out));
}
/**************/
/* Secret Box */
/**************/
static Janet cfun_secretbox_keygen(int32_t argc, Janet *argv) {
JanetBuffer *buffer = util_keygen_prep(argc, argv, hydro_secretbox_KEYBYTES);
hydro_secretbox_keygen(buffer->data);
return janet_wrap_buffer(buffer);
}
static Janet cfun_secretbox_encrypt(int32_t argc, Janet *argv) {
janet_arity(argc, 4, 5);
JanetByteView msg = janet_getbytes(argv, 0);
uint64_t msg_id = (uint64_t) janet_getinteger64(argv, 1);
JanetByteView ctx = util_getnbytes(argv, 2, hydro_secretbox_CONTEXTBYTES);
JanetByteView key = util_getnbytes(argv, 3, hydro_secretbox_KEYBYTES);
JanetBuffer *cipher;
if (argc == 5) {
cipher = janet_getbuffer(argv, 4);
janet_buffer_extra(cipher, msg.len + hydro_secretbox_HEADERBYTES);
} else {
cipher = janet_buffer(msg.len + hydro_secretbox_HEADERBYTES);
}
int result = hydro_secretbox_encrypt(cipher->data + cipher->count,
msg.bytes, msg.len, msg_id, (const char *) ctx.bytes, key.bytes);
if (result) {
janet_panic("encryption failed");
}
cipher->count += msg.len + hydro_secretbox_HEADERBYTES;
return janet_wrap_buffer(cipher);
}
static Janet cfun_secretbox_decrypt(int32_t argc, Janet *argv) {
janet_arity(argc, 4, 5);
JanetByteView ciphertext = janet_getbytes(argv, 0);
uint64_t msg_id = (uint64_t) janet_getinteger64(argv, 1);
JanetByteView ctx = util_getnbytes(argv, 2, hydro_secretbox_CONTEXTBYTES);
JanetByteView key = util_getnbytes(argv, 3, hydro_secretbox_KEYBYTES);
JanetBuffer *msg;
if (argc == 5) {
msg = janet_getbuffer(argv, 4);
janet_buffer_extra(msg, ciphertext.len - hydro_secretbox_HEADERBYTES);
} else {
msg = janet_buffer(ciphertext.len - hydro_secretbox_HEADERBYTES);
}
int result = hydro_secretbox_decrypt(msg->data + msg->count,
ciphertext.bytes, ciphertext.len, msg_id, (const char *) ctx.bytes, key.bytes);
if (result) {
janet_panic("decryption failed");
}
msg->count += ciphertext.len - hydro_secretbox_HEADERBYTES;
return janet_wrap_buffer(msg);
}
static Janet cfun_secretbox_probe_create(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
JanetByteView c = janet_getbytes(argv, 0);
JanetByteView ctx = util_getnbytes(argv, 1, hydro_secretbox_CONTEXTBYTES);
JanetByteView key = util_getnbytes(argv, 2, hydro_secretbox_KEYBYTES);
uint8_t *probe = janet_string_begin(hydro_secretbox_PROBEBYTES);
hydro_secretbox_probe_create(probe, c.bytes, c.len, (const char *) ctx.bytes, key.bytes);
return janet_wrap_string(janet_string_end(probe));
}
static Janet cfun_secretbox_probe_verify(int32_t argc, Janet *argv) {
janet_fixarity(argc, 4);
JanetByteView probe = util_getnbytes(argv, 0, hydro_secretbox_PROBEBYTES);
JanetByteView c = janet_getbytes(argv, 1);
JanetByteView ctx = util_getnbytes(argv, 2, hydro_secretbox_CONTEXTBYTES);
JanetByteView key = util_getnbytes(argv, 3, hydro_secretbox_KEYBYTES);
return janet_wrap_boolean(
!hydro_secretbox_probe_verify(probe.bytes, c.bytes, c.len, (const char *) ctx.bytes, key.bytes));
}
/*******/
/* KDF */
/*******/
static Janet cfun_kdf_keygen(int32_t argc, Janet *argv) {
JanetBuffer *buffer = util_keygen_prep(argc, argv, hydro_kdf_KEYBYTES);
hydro_kdf_keygen(buffer->data);
return janet_wrap_buffer(buffer);
}
static Janet cfun_kdf_derive_from_key(int32_t argc, Janet *argv) {
janet_fixarity(argc, 4);
int32_t subkey_len = janet_getinteger(argv, 0);
if (subkey_len < hydro_kdf_BYTES_MIN)
janet_panicf("subkey length must be at least %d, got %d",
hydro_kdf_BYTES_MIN, subkey_len);
if (subkey_len > hydro_kdf_BYTES_MAX)
janet_panicf("subkey length must be at most %d, got %d",
hydro_kdf_BYTES_MAX, subkey_len);
uint64_t subkey_id = (uint64_t) janet_getinteger64(argv, 1);
JanetByteView ctx = util_getnbytes(argv, 2, hydro_kdf_CONTEXTBYTES);
JanetByteView key = util_getnbytes(argv, 3, hydro_kdf_KEYBYTES);
uint8_t *subkey = janet_string_begin(subkey_len);
int result = hydro_kdf_derive_from_key(subkey, subkey_len, subkey_id, (const char *) ctx.bytes, key.bytes);
if (result) {
janet_panic("failed to derive key");
}
return janet_wrap_string(janet_string_end(subkey));
}
/*************************/
/* Public Key Signatures */
/*************************/
static Janet util_make_keypair(hydro_sign_keypair *kp) {
Janet pk = janet_stringv(kp->pk, hydro_sign_PUBLICKEYBYTES);
Janet sk = janet_stringv(kp->sk, hydro_sign_SECRETKEYBYTES);
JanetKV *st = janet_struct_begin(2);
janet_struct_put(st, janet_ckeywordv("public-key"), pk);
janet_struct_put(st, janet_ckeywordv("secret-key"), sk);
return janet_wrap_struct(janet_struct_end(st));
}
static Janet cfun_sign_keygen(int32_t argc, Janet *argv) {
hydro_sign_keypair kp;
(void) argv;
janet_fixarity(argc, 0);
hydro_sign_keygen(&kp);
return util_make_keypair(&kp);
}
static Janet cfun_sign_keygen_deterministic(int32_t argc, Janet *argv) {
hydro_sign_keypair kp;
janet_fixarity(argc, 1);
JanetByteView seed = util_getnbytes(argv, 0, hydro_sign_SEEDBYTES);
hydro_sign_keygen_deterministic(&kp, seed.bytes);
return util_make_keypair(&kp);
}
static Janet cfun_sign_create(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
JanetByteView msg = janet_getbytes(argv, 0);
JanetByteView ctx = util_getnbytes(argv, 1, hydro_sign_CONTEXTBYTES);
JanetByteView sk = util_getnbytes(argv, 2, hydro_sign_SECRETKEYBYTES);
uint8_t *csig = janet_string_begin(hydro_sign_BYTES);
int result = hydro_sign_create(csig, msg.bytes, msg.len, (const char *) ctx.bytes, sk.bytes);
if (result) {
janet_panic("failed to create signature");
}
return janet_wrap_string(janet_string_end(csig));
}
static Janet cfun_sign_verify(int32_t argc, Janet *argv) {
janet_fixarity(argc, 4);
JanetByteView csig = util_getnbytes(argv, 0, hydro_sign_BYTES);
JanetByteView msg = janet_getbytes(argv, 1);
JanetByteView ctx = util_getnbytes(argv, 2, hydro_sign_CONTEXTBYTES);
JanetByteView pk = util_getnbytes(argv, 3, hydro_sign_PUBLICKEYBYTES);
return janet_wrap_boolean(!hydro_sign_verify(
csig.bytes, msg.bytes, msg.len, (const char *) ctx.bytes, pk.bytes));
}
static const JanetAbstractType SignState = {
"jhydro/sign-state",
#ifdef JANET_ATEND_NAME
JANET_ATEND_NAME
#endif
};
static Janet cfun_sign_new(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JanetByteView ctx = util_getnbytes(argv, 0, hydro_sign_CONTEXTBYTES);
hydro_sign_state *state = janet_abstract(&SignState, sizeof(hydro_sign_state));
int result = hydro_sign_init(state, (const char *) ctx.bytes);
if (result) {
janet_panic("failed to create signature state");
}
return janet_wrap_abstract(state);
}
static Janet cfun_sign_update(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
hydro_sign_state *state = janet_getabstract(argv, 0, &SignState);
JanetByteView msg = janet_getbytes(argv, 1);
int result = hydro_sign_update(state, msg.bytes, msg.len);
if (result) {
janet_panic("failed to update signature state");
}
return argv[0];
}
static Janet cfun_sign_final_create(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
hydro_sign_state *state = janet_getabstract(argv, 0, &SignState);
JanetByteView sk = util_getnbytes(argv, 1, hydro_sign_SECRETKEYBYTES);
uint8_t *csig = janet_string_begin(hydro_sign_BYTES);
int result = hydro_sign_final_create(state, csig, sk.bytes);
if (result) {
janet_panic("failed to create signature");
}
return janet_wrap_string(janet_string_end(csig));
}
static Janet cfun_sign_final_verify(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
hydro_sign_state *state = janet_getabstract(argv, 0, &SignState);
JanetByteView csig = util_getnbytes(argv, 1, hydro_sign_BYTES);
JanetByteView pk = util_getnbytes(argv, 2, hydro_sign_PUBLICKEYBYTES);
int result = hydro_sign_final_verify(state, csig.bytes, pk.bytes);
return janet_wrap_boolean(!result);
}
/*******************/
/* Password Hashing */
/*******************/
typedef struct {
uint64_t opslimit;
size_t memlimit;
uint8_t threads;
} PwhashOpts;
static PwhashOpts util_pwhash_opts(int32_t argc, const Janet *argv, int32_t n) {
PwhashOpts opts;
opts.opslimit = 2000;
opts.memlimit = 2000;
opts.threads = 4;
if (argc > n && !janet_checktype(argv[n], JANET_NIL)) {
opts.opslimit = (uint64_t) util_getnat(argv, n);
}
if (argc > n + 1 && !janet_checktype(argv[n], JANET_NIL)) {
opts.memlimit = (size_t) util_getnat(argv, n + 1);
}
if (argc > n + 2 && !janet_checktype(argv[n], JANET_NIL)) {
int32_t threads_int = util_getnat(argv, n + 2);
if (threads_int > 255) {
janet_panicf("expected integer in range [0, 255] for threads, got %v", argv[6]);
}
opts.threads = (uint8_t) threads_int;
}
return opts;
}
static Janet cfun_pwhash_keygen(int32_t argc, Janet *argv) {
JanetBuffer *buffer = util_keygen_prep(argc, argv, hydro_pwhash_MASTERKEYBYTES);
hydro_pwhash_keygen(buffer->data);
return janet_wrap_buffer(buffer);
}
static Janet cfun_pwhash_deterministic(int32_t argc, Janet *argv) {
janet_arity(argc, 4, 7);
int32_t h_len = util_getnat(argv, 0);
JanetByteView passwd = janet_getbytes(argv, 1);
JanetByteView ctx = util_getnbytes(argv, 2, hydro_pwhash_CONTEXTBYTES);
JanetByteView mk = util_getnbytes(argv, 3, hydro_pwhash_MASTERKEYBYTES);
PwhashOpts opts = util_pwhash_opts(argc, argv, 4);
uint8_t *str = janet_string_begin(h_len);
int result = hydro_pwhash_deterministic(str, h_len, (const char *) passwd.bytes, passwd.len,
(const char *) ctx.bytes, mk.bytes, opts.opslimit, opts.memlimit, opts.threads);
if (result) {
janet_panic("failed to hash password");
}
return janet_wrap_string(janet_string_end(str));
}
static Janet cfun_pwhash_create(int32_t argc, Janet *argv) {
janet_arity(argc, 2, 5);
JanetByteView passwd = janet_getbytes(argv, 0);
JanetByteView mk = util_getnbytes(argv, 1, hydro_pwhash_MASTERKEYBYTES);
PwhashOpts opts = util_pwhash_opts(argc, argv, 2);
uint8_t *stored = janet_string_begin(hydro_pwhash_STOREDBYTES);
int result = hydro_pwhash_create(stored, (const char *) passwd.bytes,
passwd.len, mk.bytes, opts.opslimit, opts.memlimit, opts.threads);
if (result) {
janet_panic("failed hashing password");
}
return janet_wrap_string(janet_string_end(stored));
}
static Janet cfun_pwhash_verify(int32_t argc, Janet *argv) {
janet_arity(argc, 3, 6);
JanetByteView stored = util_getnbytes(argv, 0, hydro_pwhash_STOREDBYTES);
JanetByteView passwd = janet_getbytes(argv, 1);
JanetByteView mk = util_getnbytes(argv, 2, hydro_pwhash_MASTERKEYBYTES);
PwhashOpts opts = util_pwhash_opts(argc, argv, 3);
int result = hydro_pwhash_verify(stored.bytes, (const char *) passwd.bytes, passwd.len,
mk.bytes, opts.opslimit, opts.memlimit, opts.threads);
return janet_wrap_boolean(!result);
}
static Janet cfun_pwhash_derive_static_key(int32_t argc, Janet *argv) {
janet_arity(argc, 5, 8);
int32_t klen = util_getnat(argv, 0);
JanetByteView stored = util_getnbytes(argv, 1, hydro_pwhash_STOREDBYTES);
JanetByteView passwd = janet_getbytes(argv, 2);
JanetByteView ctx = util_getnbytes(argv, 3, hydro_pwhash_CONTEXTBYTES);
JanetByteView mk = util_getnbytes(argv, 4, hydro_pwhash_MASTERKEYBYTES);
PwhashOpts opts = util_pwhash_opts(argc, argv, 5);
uint8_t *static_key = janet_string_begin(klen);
int result = hydro_pwhash_derive_static_key(static_key, klen, stored.bytes,
(const char *) passwd.bytes, passwd.len,
(const char *) ctx.bytes,
mk.bytes,
opts.opslimit, opts.memlimit, opts.threads);
if (result) {
janet_panic("failed to create static key");
}
return janet_wrap_string(janet_string_end(static_key));
}
static Janet cfun_pwhash_reencrypt(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
JanetByteView stored = util_getnbytes(argv, 0, hydro_pwhash_STOREDBYTES);
JanetByteView mk = util_getnbytes(argv, 1, hydro_pwhash_MASTERKEYBYTES);
JanetByteView newmk = util_getnbytes(argv, 2, hydro_pwhash_MASTERKEYBYTES);
uint8_t *newstored = janet_string_begin(hydro_pwhash_STOREDBYTES);
memcpy(newstored, stored.bytes, hydro_pwhash_STOREDBYTES);
int result = hydro_pwhash_reencrypt(newstored, mk.bytes, newmk.bytes);
if (result) {
janet_panic("failed to reencrypt password hash");
}
return janet_wrap_string(janet_string_end(newstored));
}
static Janet cfun_pwhash_upgrade(int32_t argc, Janet *argv) {
janet_arity(argc, 2, 5);
JanetByteView stored = util_getnbytes(argv, 0, hydro_pwhash_STOREDBYTES);
JanetByteView mk = util_getnbytes(argv, 1, hydro_pwhash_MASTERKEYBYTES);
PwhashOpts opts = util_pwhash_opts(argc, argv, 2);
uint8_t *newstored = janet_string_begin(hydro_pwhash_STOREDBYTES);
memcpy(newstored, stored.bytes, hydro_pwhash_STOREDBYTES);
int result = hydro_pwhash_upgrade(newstored, mk.bytes,
opts.opslimit, opts.memlimit, opts.threads);
if (result) {
janet_panic("failed to upgrade password hash");
}
return janet_wrap_string(janet_string_end(newstored));
}
/* Utilities */
static Janet cfun_memzero(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JanetBuffer *buffer = janet_getbuffer(argv, 0);
hydro_memzero(buffer->data, buffer->count);
return janet_wrap_buffer(buffer);
}
static Janet cfun_increment(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JanetBuffer *buffer = janet_getbuffer(argv, 0);
hydro_increment(buffer->data, buffer->count);
return janet_wrap_buffer(buffer);
}
static Janet cfun_equal(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JanetByteView lhs = janet_getbytes(argv, 0);
JanetByteView rhs = janet_getbytes(argv, 1);
if (lhs.len != rhs.len) return janet_wrap_false();
return janet_wrap_boolean(hydro_equal(lhs.bytes, rhs.bytes, lhs.len));
}
static Janet cfun_compare(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JanetByteView lhs = janet_getbytes(argv, 0);
JanetByteView rhs = janet_getbytes(argv, 1);
if (lhs.len < rhs.len) return janet_wrap_integer(-1);
if (lhs.len > rhs.len) return janet_wrap_integer(1);
return janet_wrap_integer(hydro_compare(lhs.bytes, rhs.bytes, lhs.len));
}
static Janet cfun_bin2hex(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2);
JanetByteView bin = janet_getbytes(argv, 0);
JanetBuffer *hex = (argc == 2) ? janet_getbuffer(argv, 1) : janet_buffer(bin.len * 2 + 1);
if (argc == 2) {
janet_buffer_extra(hex, 2 * bin.len + 1);
}
hydro_bin2hex((char *)(hex->data + hex->count), bin.len * 2 + 1, bin.bytes, bin.len);
hex->count += 2 * bin.len;
return janet_wrap_buffer(hex);
}
static Janet cfun_hex2bin(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 3);
JanetByteView hex = janet_getbytes(argv, 0);
JanetBuffer *bin = (argc >= 2) ? janet_getbuffer(argv, 1) : janet_buffer(hex.len >> 1);
const char *ignore = NULL;
if (argc >= 3 && !janet_checktype(argv[2], JANET_NIL)) {
ignore = janet_getcstring(argv, 2);
}
janet_buffer_extra(bin, (hex.len >> 1));
int result = hydro_hex2bin(bin->data + bin->count,
hex.len >> 1, (const char *) hex.bytes, hex.len,
ignore, NULL);
if (result < 0) {
janet_panic("failed to convert hex to binary");
}
bin->count += result;
return janet_wrap_buffer(bin);
}
static Janet cfun_pad(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JanetBuffer *buffer = janet_getbuffer(argv, 0);
size_t pad = janet_getsize(argv, 1);
janet_buffer_extra(buffer, pad + 2);
int result = hydro_pad(buffer->data, buffer->count, pad, buffer->capacity);
if (result < 0) {
janet_panic("failed to pad bytes");
}
buffer->count = result;
return janet_wrap_buffer(buffer);
}
static Janet cfun_unpad(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JanetBuffer *buffer = janet_getbuffer(argv, 0);
size_t blocksize = janet_getsize(argv, 1);
int result = hydro_unpad(buffer->data, buffer->count, blocksize);
if (result < 0) {
janet_panic("failed to unpad buffer");
}
buffer->count = result;
return janet_wrap_buffer(buffer);
}
/* Key Exchange (KX) */
static Janet util_kx_sessionkeypair(hydro_kx_session_keypair *kp) {
JanetKV *st = janet_struct_begin(2);
Janet tx = janet_stringv(kp->tx, hydro_kx_SESSIONKEYBYTES);
Janet rx = janet_stringv(kp->rx, hydro_kx_SESSIONKEYBYTES);
janet_struct_put(st, janet_ckeywordv("tx"), tx);
janet_struct_put(st, janet_ckeywordv("rx"), rx);
return janet_wrap_struct(janet_struct_end(st));
}
static Janet cfun_kx_keygen(int32_t argc, Janet *argv) {
(void) argv;
janet_fixarity(argc, 0);
hydro_kx_keypair kp;
hydro_kx_keygen(&kp);
JanetKV *st = janet_struct_begin(2);
Janet pk = janet_stringv(kp.pk, hydro_kx_PUBLICKEYBYTES);
Janet sk = janet_stringv(kp.sk, hydro_kx_SECRETKEYBYTES);
janet_struct_put(st, janet_ckeywordv("public-key"), pk);
janet_struct_put(st, janet_ckeywordv("secret-key"), sk);
return janet_wrap_struct(janet_struct_end(st));
}
static Janet cfun_kx_n_1(int32_t argc, Janet *argv) {
janet_fixarity(argc, 3);
JanetBuffer *packet = janet_getbuffer(argv, 0);
JanetByteView psk = util_getnbytes(argv, 1, hydro_kx_PSKBYTES);
JanetByteView peer_psk = util_getnbytes(argv, 2, hydro_kx_PUBLICKEYBYTES);
hydro_kx_session_keypair kp;
janet_buffer_extra(packet, hydro_kx_N_PACKET1BYTES);
int result = hydro_kx_n_1(&kp, packet->data + packet->count, psk.bytes, peer_psk.bytes);
if (result < 0) {
janet_panic("failed to generate packet 1 to send to peer");
}
packet->count += hydro_kx_N_PACKET1BYTES;
return util_kx_sessionkeypair(&kp);
}
static Janet cfun_kx_n_2(int32_t argc, Janet *argv) {
janet_fixarity(argc, 4);
JanetByteView packet = util_getnbytes(argv, 0, hydro_kx_N_PACKET1BYTES);
JanetByteView psk = util_getnbytes(argv, 1, hydro_kx_PSKBYTES);
JanetByteView static_pk = util_getnbytes(argv, 2, hydro_kx_PUBLICKEYBYTES);
JanetByteView static_sk = util_getnbytes(argv, 3, hydro_kx_SECRETKEYBYTES);
hydro_kx_keypair static_kp;
memcpy(static_kp.sk, static_sk.bytes, hydro_kx_SECRETKEYBYTES);
memcpy(static_kp.pk, static_pk.bytes, hydro_kx_PUBLICKEYBYTES);
hydro_kx_session_keypair kp;
int result = hydro_kx_n_2(&kp, packet.bytes, psk.bytes, &static_kp);
if (result < 0) {
janet_panic("failed to generate packet 2 to send to peer");
}
return util_kx_sessionkeypair(&kp);
}
/* KK variant */
static const JanetAbstractType KxState = {
"jhydro/kx-state",
#ifdef JANET_ATEND_NAME
JANET_ATEND_NAME
#endif
};
static Janet cfun_kx_kk_1(int32_t argc, Janet *argv) {
janet_fixarity(argc, 4);
JanetBuffer *packet1 = janet_getbuffer(argv, 0);
JanetByteView static_pk = util_getnbytes(argv, 1, hydro_kx_PUBLICKEYBYTES);
JanetByteView pk = util_getnbytes(argv, 2, hydro_kx_PUBLICKEYBYTES);
JanetByteView sk = util_getnbytes(argv, 3, hydro_kx_SECRETKEYBYTES);
hydro_kx_state *state = janet_abstract(&KxState, sizeof(hydro_kx_state));
janet_buffer_extra(packet1, hydro_kx_KK_PACKET1BYTES);
hydro_kx_keypair kp;
memcpy(&kp.pk, pk.bytes, hydro_kx_PUBLICKEYBYTES);
memcpy(&kp.sk, sk.bytes, hydro_kx_SECRETKEYBYTES);
int result = hydro_kx_kk_1(state, packet1->data + packet1->count, static_pk.bytes, &kp);
if (result < 0) {
janet_panic("failed to generate packet 1 to send to peer");
}
packet1->count += hydro_kx_KK_PACKET1BYTES;
return janet_wrap_abstract(state);
}
static Janet cfun_kx_kk_2(int32_t argc, Janet *argv) {
janet_fixarity(argc, 5);
JanetBuffer *packet2 = janet_getbuffer(argv, 0);
janet_buffer_extra(packet2, hydro_kx_KK_PACKET2BYTES);
JanetByteView packet1 = util_getnbytes(argv, 1, hydro_kx_KK_PACKET1BYTES);
JanetByteView static_pk = util_getnbytes(argv, 2, hydro_kx_PUBLICKEYBYTES);
JanetByteView pk = util_getnbytes(argv, 3, hydro_kx_PUBLICKEYBYTES);
JanetByteView sk = util_getnbytes(argv, 4, hydro_kx_SECRETKEYBYTES);
hydro_kx_keypair kp;
memcpy(&kp.pk, pk.bytes, hydro_kx_PUBLICKEYBYTES);
memcpy(&kp.sk, sk.bytes, hydro_kx_SECRETKEYBYTES);
hydro_kx_session_keypair skp;
int result = hydro_kx_kk_2(&skp, packet2->data, packet1.bytes, static_pk.bytes, &kp);
if (result < 0) {
janet_panic("failed to generate session keypair");
}
packet2->count += hydro_kx_KK_PACKET2BYTES;
return util_kx_sessionkeypair(&skp);
}
static Janet cfun_kx_kk_3(int32_t argc, Janet *argv) {
janet_fixarity(argc, 4);
hydro_kx_state *state = janet_getabstract(argv, 0, &KxState);
JanetByteView packet2 = util_getnbytes(argv, 1, hydro_kx_KK_PACKET2BYTES);
JanetByteView pk = util_getnbytes(argv, 2, hydro_kx_PUBLICKEYBYTES);
JanetByteView sk = util_getnbytes(argv, 3, hydro_kx_SECRETKEYBYTES);
hydro_kx_session_keypair skp;
hydro_kx_keypair kp;
memcpy(&kp.pk, pk.bytes, hydro_kx_PUBLICKEYBYTES);
memcpy(&kp.sk, sk.bytes, hydro_kx_SECRETKEYBYTES);
int result = hydro_kx_kk_3(state, &skp, packet2.bytes, &kp);
if (result < 0) {
janet_panic("failed to generate session keypair");
}
return util_kx_sessionkeypair(&skp);
}
/* XX Variant */
static Janet cfun_kx_xx_1(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JanetBuffer *packet1 = janet_getbuffer(argv, 0);
JanetByteView psk = util_getnbytes(argv, 1, hydro_kx_PSKBYTES);
janet_buffer_extra(packet1, hydro_kx_XX_PACKET1BYTES);
hydro_kx_state *state = janet_abstract(&KxState, sizeof(hydro_kx_state));
int result = hydro_kx_xx_1(state, packet1->data + packet1->count, psk.bytes);
if (result) {
janet_panic("failed to generate packet 1 to send to peer");
}
packet1->count += hydro_kx_XX_PACKET1BYTES;
return janet_wrap_abstract(state);
}
static Janet cfun_kx_xx_2(int32_t argc, Janet *argv) {
janet_fixarity(argc, 5);
JanetBuffer *packet2 = janet_getbuffer(argv, 0);
JanetByteView packet1 = util_getnbytes(argv, 1, hydro_kx_XX_PACKET1BYTES);
JanetByteView psk = util_getnbytes(argv, 2, hydro_kx_PSKBYTES);
JanetByteView pk = util_getnbytes(argv, 3, hydro_kx_PUBLICKEYBYTES);
JanetByteView sk = util_getnbytes(argv, 4, hydro_kx_SECRETKEYBYTES);
hydro_kx_keypair kp;
memcpy(&kp.pk, pk.bytes, hydro_kx_PUBLICKEYBYTES);
memcpy(&kp.sk, sk.bytes, hydro_kx_SECRETKEYBYTES);
hydro_kx_state *state = janet_abstract(&KxState, sizeof(hydro_kx_state));
janet_buffer_extra(packet2, hydro_kx_XX_PACKET2BYTES);
int result = hydro_kx_xx_2(state, packet2->data + packet2->count, packet1.bytes, psk.bytes, &kp);
if (result < 0) {
janet_panic("failed to generate packet 2 to send to peer");
}
packet2->count += hydro_kx_XX_PACKET2BYTES;
return janet_wrap_abstract(state);
}
static Janet cfun_kx_xx_3(int32_t argc, Janet *argv) {
janet_arity(argc, 6, 7);
hydro_kx_state *state = janet_getabstract(argv, 0, &KxState);
JanetBuffer *packet3 = janet_getbuffer(argv, 1);
JanetByteView packet2 = util_getnbytes(argv, 2, hydro_kx_XX_PACKET2BYTES);
JanetByteView psk = util_getnbytes(argv, 3, hydro_kx_PSKBYTES);
JanetByteView pk = util_getnbytes(argv, 4, hydro_kx_PUBLICKEYBYTES);
JanetByteView sk = util_getnbytes(argv, 5, hydro_kx_SECRETKEYBYTES);
hydro_kx_keypair kp;
hydro_kx_session_keypair skp;
memcpy(&kp.pk, pk.bytes, hydro_kx_PUBLICKEYBYTES);
memcpy(&kp.sk, sk.bytes, hydro_kx_SECRETKEYBYTES);
janet_buffer_extra(packet3, hydro_kx_XX_PACKET3BYTES);
uint8_t *peer_pk = NULL;
if (argc > 6) {
JanetBuffer *buffer = janet_getbuffer(argv, 6);
janet_buffer_extra(buffer, hydro_kx_PUBLICKEYBYTES);
peer_pk = buffer->data + buffer->count;
buffer->count += hydro_kx_PUBLICKEYBYTES;
}
int result = hydro_kx_xx_3(state, &skp, packet3->data + packet3->count, peer_pk, packet2.bytes, psk.bytes, &kp);
if (result < 0) {
janet_panic("failed to generate session keypair");
}
packet3->count += hydro_kx_XX_PACKET3BYTES;
return util_kx_sessionkeypair(&skp);
}
static Janet cfun_kx_xx_4(int32_t argc, Janet *argv) {
janet_arity(argc, 3, 4);
hydro_kx_state *state = janet_getabstract(argv, 0, &KxState);
JanetByteView packet3 = util_getnbytes(argv, 1, hydro_kx_XX_PACKET3BYTES);
JanetByteView psk = util_getnbytes(argv, 2, hydro_kx_PSKBYTES);
uint8_t *peer_pk = NULL;
hydro_kx_session_keypair skp;
if (argc > 3) {
JanetBuffer *buffer = janet_getbuffer(argv, 3);
janet_buffer_extra(buffer, hydro_kx_PUBLICKEYBYTES);
peer_pk = buffer->data + buffer->count;
buffer->count += hydro_kx_PUBLICKEYBYTES;
}
int result = hydro_kx_xx_4(state, &skp, peer_pk, packet3.bytes, psk.bytes);
if (result) {
janet_panic("failed to generate session keypair");
}
return util_kx_sessionkeypair(&skp);
}
/****************/
/* Module Entry */
/****************/
static const JanetReg cfuns[] = {
/* Random */
{ "random/u32", cfun_random_u32, "(random/u32)\n\n"
"Generate a psuedo random 32 bit unsigned integer"
},
{ "random/uniform", cfun_random_uniform, "(random/uniform top)\n\n"
"Generate a random 32 bit unsigned integer less than top."
},
{ "random/buf", cfun_random_buf, "(random/buf buf &opt size)\n\n"
"Fill a buffer with random bytes. If size is not provided, it will clear "
"and fill the given buffer. If size is provided, will append size random "
"bytes to the buffer. if you provide just the size argument,"
"a new randomized buffer will be returned."
},
{ "random/ratchet", cfun_random_ratchet, "(random/ratchet)\n\n"
"Increment the internal state of the RNG."
},
{ "random/reseed", cfun_random_reseed, "(random/reseed)\n\n"
"Provide a new random seed for the internal RNG."
},
{ "random/buf-deterministic", cfun_random_buf_deterministic,
"(random/buf-deterministic buf len seed)\n\n"
"Generate len random bytes and push them into a buffer buf. seed "
"is a byte sequence of 32 bytes that initializes the state of the RNG. "
"With same seed and len returns always the same buffer. Suitable for testing. "
"Returns the modified buffer."
},
/* Hashing */
{ "hash/keygen", cfun_hash_keygen, "(hash/keygen &opt buf)\n\n"
"Generate a key suitable for use in hashing. The key is a buffer of at "
"least 32 bytes. If a buffer buf is provided, the first 32 bytes of buf "
"will be set to a new random key. Returns a key buffer."
},
{ "hash/new-state", cfun_hash_new, "(hash/new-state ctx key)\n\n"
"Create a new hash-state. Takes a context ctx and a key and returns a new abstract type, "
"jhydro/hash-state. Both ctx and key should be byte sequences, of lengths 8 and 32 "
"respectively. Returns the new state."
},
{ "hash/update", cfun_hash_update, "(hash/update state bytes)\n\n"
"Add more bytes to the hash state. Returns the modified state"
},
{ "hash/final", cfun_hash_final, "(hash/final state len)\n\n"
"Get the final hash after digesting all of the input as a string. The resulting "
"hash will be a string of length len."
},
{ "hash/hash", cfun_hash_hash, "(hash/hash size input ctx &opt key)\n\n"
"Hash some input bytes into an output string of length size. Optionally provide "
"a key that can be used to generate different hashes on the same input."
},
/* Secret Box - symmetric encryption */
{ "secretbox/keygen", cfun_secretbox_keygen, "(secretbox/keygen)\n\n"
"Generate a key suitable for secretbox. The returned key is a 32 byte buffer."
},
{ "secretbox/encrypt", cfun_secretbox_encrypt,
"(secretbox/encrypt msg msg-id ctx key &opt buf)\n\n"
"Encrypt a message with a secretbox key and return the cipher text in a buffer. "
"Also requires a message id, which is an integer, and a ctx, which is a non-secret "
"byte-sequence. Lastly, requires a secret symmetric key for encryption. An optional "
"buffer will prevent Janet from creating a new buffer, and instead append to and return "
"the provided buffer."
},
{ "secretbox/decrypt", cfun_secretbox_decrypt,
"(secretbox/decrypt cipher-text msg-id ctx key &opt buf)\n\n"
"Decrypt a cipher text that was produced with secretbox/encrypt. msg-id, "
"ctx, and key must be the same as those used to encrypt the message. An optional "
"buffer can be used to contain the plain text, otherwise a new buffer is created. "
"Returns a buffer containing the plain text."
},
{ "secretbox/probe-create", cfun_secretbox_probe_create,
"(secretbox/probe-create cipher-text ctx key)\n\n"
"Create a probe for some cipher text created by secretbox/encrypt. The "
"resulting probe is a constant length string that can be used to verify if cipher text "
"is valid before decrypting the entire text. Probes can help mitigate "
"attack with large invalid ciphertexts. Returns a string."
},
{ "secretbox/probe-verify", cfun_secretbox_probe_verify,
"(secretbox/probe-verify probe cipher-text ctx key)\n\n"
"Use a probe produced by secretbox/probe-create to check if some cipher text "
"is genuine. If the cipher text is not forged or tampered with, returns true, otherwise "
"false. Genuine cipher text can then be decrypted. Returns a boolean."
},
/* KDF */
{ "kdf/keygen", cfun_kdf_keygen, "(kdf/keygen &opt buf)\n\n"
"Generate a key for use in KDFs. Returns the modified buf if provided, or "
"a new random buffer."
},
{ "kdf/derive-from-key", cfun_kdf_derive_from_key,
"(kdf/derive-from-key sublen subid ctx key)\n\n"
"Generate a subkey from a master key. Takes a subid, which is "
"a positive integer that represents the key id, and ctx, which is "
"an 8 byte string that is usually an application constant. Finally, the "
"last parameter is the master key. Returns a string of length sublen."
},
/* Public Key Signatures */
{ "sign/keygen", cfun_sign_keygen, "(sign/keygen)\n\n"
"Create a random key pair for public key signing. Returns a struct containing a "
":public-key and a :secret-key as strings."
},
{ "sign/keygen-deterministic", cfun_sign_keygen_deterministic,
"(sign/keygen-deterministic seed)\n\n"
"Create a key pair from a seed. Seed should be a byte sequence of at least "
"32 bytes; random/buf should work well. Returns a struct of two key value "
"pairs, a :secret-key and a :public-key. Each key is a string."
},
{ "sign/create", cfun_sign_create, "(sign/create msg ctx sk)\n\n"
"Create a new sigature from a message, ctx, and secret key. The message "
"can be any byte sequence, the context ctx should be a byte sequence of "
"8 bytes, and the secret key sk should be secret key as generated from sign/keygen or "
"sign/keygen-deterministic. Returns a signature, which is a 64 byte string."
},
{ "sign/verify", cfun_sign_verify, "(sign/verify csig msg ctx pk)\n\n"
"Check a signature to determine if a message is authentic. csig is the signature as "
"generated by sign/create or sign/final-create, msg is the message that "
"we are checking, ctx is the context string, and pk is the public key. Returns a boolean, "
"true if the signature is valid, false otherwise."
},
{ "sign/new-state", cfun_sign_new, "(sign/new-state ctx)\n\n"
"Create a new state machine for generating a signature. A state machine allows "
"processing a message in chunks to generate a signature. A string ctx of 8 bytes "
"is also required, and can be a hard coded string. Returns a new jhydro/sign-state."
},
{ "sign/update", cfun_sign_update, "(sign/update state msg)\n\n"
"Process a message chunk for generating a signature. Returns the modified signature state."
},
{ "sign/final-create", cfun_sign_final_create, "(sign/final-create state sk)\n\n"
"Create a signature from the sign-state. Takes a jhydro/sign-state state and a secret key sk. "
"Returns the signature and also modifies the state."
},
{ "sign/final-verify", cfun_sign_final_verify, "(sign/final-verify state csig pk)\n\n"
"Verify a signature with a public key. Given a sign-state state, signature csig, and "
"public key pk, return true if csig is valid, otherwise false."
},
/* Password Hashing */
{ "pwhash/keygen", cfun_pwhash_keygen, "(pwhash/keygen &opt buf)\n\n"
"Generate a master key for use in hashing passwords. The master key is used to "
"encrypt all hashed passwords for an extra level of security. Returns a buffer with "
"the new key."
},
{ "pwhash/deterministic", cfun_pwhash_deterministic,
"(pwhash/deterministic hlen passwd ctx master-key &opt opslimit memlimit threads)\n\n"
"Hash a password to produce a high entropy key. "
"The returned hashed password is a string of length hlen."
},
{ "pwhash/create", cfun_pwhash_create,
"(pwhash/create passwd masterkey &opt opslimit memlimit threads)\n\n"
"Hash a password and get a blob that can be safely stored in a database. "
"The returned result is a 128 byte string. Can take optional parameters to tune "
"the difficulty of the hash."
},