-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonocypher.c
2150 lines (1943 loc) · 73.7 KB
/
monocypher.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
#include "monocypher.h"
// Monocypher 2.0.5 - https://monocypher.org/
// released under CC0 license (essentially public domain)
//
// - modified to not require C99
/////////////////
/// Utilities ///
/////////////////
// By default, EdDSA signatures use blake2b. SHA-512 is provided as an
// option for full ed25519 compatibility. To use with SHA-512, compile
// with option -DED25519_SHA512 and provide the "sha512" header.
#ifdef ED25519_SHA512
#define HASH crypto_sha512
#else
#define HASH crypto_blake2b
#endif
#define COMBINE1(x, y) x ## y
#define COMBINE2(x, y) COMBINE1(x, y)
#define HASH_CTX COMBINE2(HASH, _ctx)
#define HASH_INIT COMBINE2(HASH, _init)
#define HASH_UPDATE COMBINE2(HASH, _update)
#define HASH_FINAL COMBINE2(HASH, _final)
#define FOR(i, start, end) for (/*size_t*/ (i) = (start); (i) < (end); (i)++)
#define WIPE_CTX(ctx) crypto_wipe(ctx , sizeof(*(ctx)))
#define WIPE_BUFFER(buffer) crypto_wipe(buffer, sizeof(buffer))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))
#define ALIGN(x, block_size) ((~(x) + 1) & ((block_size) - 1))
typedef int8_t i8;
typedef uint8_t u8;
typedef uint32_t u32;
typedef int32_t i32;
typedef int64_t i64;
typedef uint64_t u64;
static const u8 zero[128] = {0};
static u32 load24_le(const u8 s[3])
{
return (u32)s[0]
| ((u32)s[1] << 8)
| ((u32)s[2] << 16);
}
static u32 load32_le(const u8 s[4])
{
return (u32)s[0]
| ((u32)s[1] << 8)
| ((u32)s[2] << 16)
| ((u32)s[3] << 24);
}
static u64 load64_le(const u8 s[8])
{
return load32_le(s) | ((u64)load32_le(s+4) << 32);
}
static void store32_le(u8 out[4], u32 in)
{
out[0] = in & 0xff;
out[1] = (in >> 8) & 0xff;
out[2] = (in >> 16) & 0xff;
out[3] = (in >> 24) & 0xff;
}
static void store64_le(u8 out[8], u64 in)
{
store32_le(out , (u32)in );
store32_le(out + 4, in >> 32);
}
static u64 rotr64(u64 x, u64 n) { return (x >> n) ^ (x << (64 - n)); }
static u32 rotl32(u32 x, u32 n) { return (x << n) ^ (x >> (32 - n)); }
static int neq0(u64 diff)
{ // constant time comparison to zero
// return diff != 0 ? -1 : 0
u64 half = (diff >> 32) | ((u32)diff);
return (1 & ((half - 1) >> 32)) - 1;
}
static u64 x16(const u8 a[16], const u8 b[16])
{
return (load64_le(a + 0) ^ load64_le(b + 0))
| (load64_le(a + 8) ^ load64_le(b + 8));
}
static u64 x32(const u8 a[16],const u8 b[16]){return x16(a,b)| x16(a+16, b+16);}
static u64 x64(const u8 a[64],const u8 b[64]){return x32(a,b)| x32(a+32, b+32);}
int crypto_verify16(const u8 a[16], const u8 b[16]){ return neq0(x16(a, b)); }
int crypto_verify32(const u8 a[32], const u8 b[32]){ return neq0(x32(a, b)); }
int crypto_verify64(const u8 a[64], const u8 b[64]){ return neq0(x64(a, b)); }
static int zerocmp32(const u8 p[32])
{
return crypto_verify32(p, zero);
}
void crypto_wipe(void *secret, size_t size)
{
volatile u8 *v_secret = (u8*)secret;
size_t i;
FOR (i, 0, size) {
v_secret[i] = 0;
}
}
/////////////////
/// Chacha 20 ///
/////////////////
#define QUARTERROUND(a, b, c, d) \
a += b; d = rotl32(d ^ a, 16); \
c += d; b = rotl32(b ^ c, 12); \
a += b; d = rotl32(d ^ a, 8); \
c += d; b = rotl32(b ^ c, 7)
static void chacha20_rounds(u32 out[16], const u32 in[16])
{
// The temporary variables make Chacha20 10% faster.
u32 t0 = in[ 0]; u32 t1 = in[ 1]; u32 t2 = in[ 2]; u32 t3 = in[ 3];
u32 t4 = in[ 4]; u32 t5 = in[ 5]; u32 t6 = in[ 6]; u32 t7 = in[ 7];
u32 t8 = in[ 8]; u32 t9 = in[ 9]; u32 t10 = in[10]; u32 t11 = in[11];
u32 t12 = in[12]; u32 t13 = in[13]; u32 t14 = in[14]; u32 t15 = in[15];
size_t i;
FOR (i, 0, 10) { // 20 rounds, 2 rounds per loop.
QUARTERROUND(t0, t4, t8 , t12); // column 0
QUARTERROUND(t1, t5, t9 , t13); // column 1
QUARTERROUND(t2, t6, t10, t14); // column 2
QUARTERROUND(t3, t7, t11, t15); // column 3
QUARTERROUND(t0, t5, t10, t15); // diagonal 0
QUARTERROUND(t1, t6, t11, t12); // diagonal 1
QUARTERROUND(t2, t7, t8 , t13); // diagonal 2
QUARTERROUND(t3, t4, t9 , t14); // diagonal 3
}
out[ 0] = t0; out[ 1] = t1; out[ 2] = t2; out[ 3] = t3;
out[ 4] = t4; out[ 5] = t5; out[ 6] = t6; out[ 7] = t7;
out[ 8] = t8; out[ 9] = t9; out[10] = t10; out[11] = t11;
out[12] = t12; out[13] = t13; out[14] = t14; out[15] = t15;
}
static void chacha20_init_key(crypto_chacha_ctx *ctx, const u8 key[32])
{
size_t i;
// constant
ctx->input[0] = load32_le((u8*)"expa");
ctx->input[1] = load32_le((u8*)"nd 3");
ctx->input[2] = load32_le((u8*)"2-by");
ctx->input[3] = load32_le((u8*)"te k");
// key
FOR (i, 0, 8) {
ctx->input[i+4] = load32_le(key + i*4);
}
}
static u8 chacha20_pool_byte(crypto_chacha_ctx *ctx)
{
u32 pool_word = ctx->pool[ctx->pool_idx >> 2];
u8 pool_byte = pool_word >> (8*(ctx->pool_idx & 3));
ctx->pool_idx++;
return pool_byte;
}
// Fill the pool if needed, update the counters
static void chacha20_refill_pool(crypto_chacha_ctx *ctx)
{
size_t j;
chacha20_rounds(ctx->pool, ctx->input);
FOR (j, 0, 16) {
ctx->pool[j] += ctx->input[j];
}
ctx->pool_idx = 0;
ctx->input[12]++;
if (ctx->input[12] == 0) {
ctx->input[13]++;
}
}
void crypto_chacha20_H(u8 out[32], const u8 key[32], const u8 in[16])
{
size_t i;
crypto_chacha_ctx ctx;
chacha20_init_key(&ctx, key);
FOR (i, 0, 4) {
ctx.input[i+12] = load32_le(in + i*4);
}
u32 buffer[16];
chacha20_rounds(buffer, ctx.input);
// prevents reversal of the rounds by revealing only half of the buffer.
FOR (i, 0, 4) {
store32_le(out + i*4, buffer[i ]); // constant
store32_le(out + 16 + i*4, buffer[i + 12]); // counter and nonce
}
WIPE_CTX(&ctx);
WIPE_BUFFER(buffer);
}
static void chacha20_encrypt(crypto_chacha_ctx *ctx,
u8 *cipher_text,
const u8 *plain_text,
size_t text_size)
{
size_t i;
FOR (i, 0, text_size) {
if (ctx->pool_idx == 64) {
chacha20_refill_pool(ctx);
}
u8 plain = 0;
if (plain_text != 0) {
plain = *plain_text;
plain_text++;
}
*cipher_text = chacha20_pool_byte(ctx) ^ plain;
cipher_text++;
}
}
void crypto_chacha20_init(crypto_chacha_ctx *ctx,
const u8 key[32],
const u8 nonce[8])
{
chacha20_init_key (ctx, key); // key
crypto_chacha20_set_ctr(ctx, 0 ); // counter
ctx->input[14] = load32_le(nonce + 0); // nonce
ctx->input[15] = load32_le(nonce + 4); // nonce
}
void crypto_chacha20_x_init(crypto_chacha_ctx *ctx,
const u8 key[32],
const u8 nonce[24])
{
u8 derived_key[32];
crypto_chacha20_H(derived_key, key, nonce);
crypto_chacha20_init(ctx, derived_key, nonce + 16);
WIPE_BUFFER(derived_key);
}
void crypto_chacha20_set_ctr(crypto_chacha_ctx *ctx, u64 ctr)
{
ctx->input[12] = ctr & 0xffffffff;
ctx->input[13] = ctr >> 32;
ctx->pool_idx = 64; // The random pool (re)starts empty
}
void crypto_chacha20_encrypt(crypto_chacha_ctx *ctx,
u8 *cipher_text,
const u8 *plain_text,
size_t text_size)
{
size_t i, j;
// Align ourselves with block boundaries
size_t align = MIN(ALIGN(ctx->pool_idx, 64), text_size);
chacha20_encrypt(ctx, cipher_text, plain_text, align);
if (plain_text != 0) {
plain_text += align;
}
cipher_text += align;
text_size -= align;
// Process the message block by block
FOR (i, 0, text_size >> 6) { // number of blocks
chacha20_refill_pool(ctx);
if (plain_text != 0) {
FOR (j, 0, 16) {
u32 plain = load32_le(plain_text);
store32_le(cipher_text, ctx->pool[j] ^ plain);
plain_text += 4;
cipher_text += 4;
}
} else {
FOR (j, 0, 16) {
store32_le(cipher_text, ctx->pool[j]);
cipher_text += 4;
}
}
ctx->pool_idx = 64;
}
text_size &= 63;
// remaining bytes
chacha20_encrypt(ctx, cipher_text, plain_text, text_size);
}
void crypto_chacha20_stream(crypto_chacha_ctx *ctx,
uint8_t *stream, size_t size)
{
crypto_chacha20_encrypt(ctx, stream, 0, size);
}
/////////////////
/// Poly 1305 ///
/////////////////
// h = (h + c) * r
// preconditions:
// ctx->h <= 4_ffffffff_ffffffff_ffffffff_ffffffff
// ctx->c <= 1_ffffffff_ffffffff_ffffffff_ffffffff
// ctx->r <= 0ffffffc_0ffffffc_0ffffffc_0fffffff
// Postcondition:
// ctx->h <= 4_ffffffff_ffffffff_ffffffff_ffffffff
static void poly_block(crypto_poly1305_ctx *ctx)
{
// s = h + c, without carry propagation
const u64 s0 = ctx->h[0] + (u64)ctx->c[0]; // s0 <= 1_fffffffe
const u64 s1 = ctx->h[1] + (u64)ctx->c[1]; // s1 <= 1_fffffffe
const u64 s2 = ctx->h[2] + (u64)ctx->c[2]; // s2 <= 1_fffffffe
const u64 s3 = ctx->h[3] + (u64)ctx->c[3]; // s3 <= 1_fffffffe
const u32 s4 = ctx->h[4] + ctx->c[4]; // s4 <= 5
// Local all the things!
const u32 r0 = ctx->r[0]; // r0 <= 0fffffff
const u32 r1 = ctx->r[1]; // r1 <= 0ffffffc
const u32 r2 = ctx->r[2]; // r2 <= 0ffffffc
const u32 r3 = ctx->r[3]; // r3 <= 0ffffffc
const u32 rr0 = (r0 >> 2) * 5; // rr0 <= 13fffffb // lose 2 bits...
const u32 rr1 = (r1 >> 2) + r1; // rr1 <= 13fffffb // rr1 == (r1 >> 2) * 5
const u32 rr2 = (r2 >> 2) + r2; // rr2 <= 13fffffb // rr1 == (r2 >> 2) * 5
const u32 rr3 = (r3 >> 2) + r3; // rr3 <= 13fffffb // rr1 == (r3 >> 2) * 5
// (h + c) * r, without carry propagation
const u64 x0 = s0*r0 + s1*rr3 + s2*rr2 + s3*rr1 +s4*rr0;//<=97ffffe007fffff8
const u64 x1 = s0*r1 + s1*r0 + s2*rr3 + s3*rr2 +s4*rr1;//<=8fffffe20ffffff6
const u64 x2 = s0*r2 + s1*r1 + s2*r0 + s3*rr3 +s4*rr2;//<=87ffffe417fffff4
const u64 x3 = s0*r3 + s1*r2 + s2*r1 + s3*r0 +s4*rr3;//<=7fffffe61ffffff2
const u32 x4 = s4 * (r0 & 3); // ...recover 2 bits //<= f
// partial reduction modulo 2^130 - 5
const u32 u5 = x4 + (x3 >> 32); // u5 <= 7ffffff5
const u64 u0 = (u5 >> 2) * 5 + (x0 & 0xffffffff);
const u64 u1 = (u0 >> 32) + (x1 & 0xffffffff) + (x0 >> 32);
const u64 u2 = (u1 >> 32) + (x2 & 0xffffffff) + (x1 >> 32);
const u64 u3 = (u2 >> 32) + (x3 & 0xffffffff) + (x2 >> 32);
const u64 u4 = (u3 >> 32) + (u5 & 3);
// Update the hash
ctx->h[0] = u0 & 0xffffffff; // u0 <= 1_9ffffff0
ctx->h[1] = u1 & 0xffffffff; // u1 <= 1_97ffffe0
ctx->h[2] = u2 & 0xffffffff; // u2 <= 1_8fffffe2
ctx->h[3] = u3 & 0xffffffff; // u3 <= 1_87ffffe4
ctx->h[4] = (u32)u4; // u4 <= 4
}
// (re-)initializes the input counter and input buffer
static void poly_clear_c(crypto_poly1305_ctx *ctx)
{
ctx->c[0] = 0;
ctx->c[1] = 0;
ctx->c[2] = 0;
ctx->c[3] = 0;
ctx->c_idx = 0;
}
static void poly_take_input(crypto_poly1305_ctx *ctx, u8 input)
{
size_t word = ctx->c_idx >> 2;
size_t byte = ctx->c_idx & 3;
ctx->c[word] |= (u32)input << (byte * 8);
ctx->c_idx++;
}
static void poly_update(crypto_poly1305_ctx *ctx,
const u8 *message, size_t message_size)
{
size_t i;
FOR (i, 0, message_size) {
poly_take_input(ctx, message[i]);
if (ctx->c_idx == 16) {
poly_block(ctx);
poly_clear_c(ctx);
}
}
}
void crypto_poly1305_init(crypto_poly1305_ctx *ctx, const u8 key[32])
{
size_t i;
// Initial hash is zero
FOR (i, 0, 5) {
ctx->h[i] = 0;
}
// add 2^130 to every input block
ctx->c[4] = 1;
poly_clear_c(ctx);
// load r and pad (r has some of its bits cleared)
FOR (i, 0, 1) { ctx->r [0] = load32_le(key ) & 0x0fffffff; }
FOR (i, 1, 4) { ctx->r [i] = load32_le(key + i*4 ) & 0x0ffffffc; }
FOR (i, 0, 4) { ctx->pad[i] = load32_le(key + i*4 + 16); }
}
void crypto_poly1305_update(crypto_poly1305_ctx *ctx,
const u8 *message, size_t message_size)
{
size_t i;
// Align ourselves with block boundaries
size_t align = MIN(ALIGN(ctx->c_idx, 16), message_size);
poly_update(ctx, message, align);
message += align;
message_size -= align;
// Process the message block by block
size_t nb_blocks = message_size >> 4;
FOR (i, 0, nb_blocks) {
ctx->c[0] = load32_le(message + 0);
ctx->c[1] = load32_le(message + 4);
ctx->c[2] = load32_le(message + 8);
ctx->c[3] = load32_le(message + 12);
poly_block(ctx);
message += 16;
}
if (nb_blocks > 0) {
poly_clear_c(ctx);
}
message_size &= 15;
// remaining bytes
poly_update(ctx, message, message_size);
}
void crypto_poly1305_final(crypto_poly1305_ctx *ctx, u8 mac[16])
{
// Process the last block (if any)
if (ctx->c_idx != 0) {
// move the final 1 according to remaining input length
// (We may add less than 2^130 to the last input block)
ctx->c[4] = 0;
poly_take_input(ctx, 1);
// one last hash update
poly_block(ctx);
}
// check if we should subtract 2^130-5 by performing the
// corresponding carry propagation.
const u64 u0 = (u64)5 + ctx->h[0]; // <= 1_00000004
const u64 u1 = (u0 >> 32) + ctx->h[1]; // <= 1_00000000
const u64 u2 = (u1 >> 32) + ctx->h[2]; // <= 1_00000000
const u64 u3 = (u2 >> 32) + ctx->h[3]; // <= 1_00000000
const u64 u4 = (u3 >> 32) + ctx->h[4]; // <= 5
// u4 indicates how many times we should subtract 2^130-5 (0 or 1)
// h + pad, minus 2^130-5 if u4 exceeds 3
const u64 uu0 = (u4 >> 2) * 5 + ctx->h[0] + ctx->pad[0]; // <= 2_00000003
const u64 uu1 = (uu0 >> 32) + ctx->h[1] + ctx->pad[1]; // <= 2_00000000
const u64 uu2 = (uu1 >> 32) + ctx->h[2] + ctx->pad[2]; // <= 2_00000000
const u64 uu3 = (uu2 >> 32) + ctx->h[3] + ctx->pad[3]; // <= 2_00000000
store32_le(mac , (u32)uu0);
store32_le(mac + 4, (u32)uu1);
store32_le(mac + 8, (u32)uu2);
store32_le(mac + 12, (u32)uu3);
WIPE_CTX(ctx);
}
void crypto_poly1305(u8 mac[16], const u8 *message,
size_t message_size, const u8 key[32])
{
crypto_poly1305_ctx ctx;
crypto_poly1305_init (&ctx, key);
crypto_poly1305_update(&ctx, message, message_size);
crypto_poly1305_final (&ctx, mac);
}
////////////////
/// Blake2 b ///
////////////////
static const u64 iv[8] = {
0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL,
};
// increment the input offset
static void blake2b_incr(crypto_blake2b_ctx *ctx)
{
u64 *x = ctx->input_offset;
size_t y = ctx->input_idx;
x[0] += y;
if (x[0] < y) {
x[1]++;
}
}
static void blake2b_compress(crypto_blake2b_ctx *ctx, int is_last_block)
{
static const u8 sigma[12][16] = {
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
};
// init work vector
u64 v0 = ctx->hash[0]; u64 v8 = iv[0];
u64 v1 = ctx->hash[1]; u64 v9 = iv[1];
u64 v2 = ctx->hash[2]; u64 v10 = iv[2];
u64 v3 = ctx->hash[3]; u64 v11 = iv[3];
u64 v4 = ctx->hash[4]; u64 v12 = iv[4] ^ ctx->input_offset[0];
u64 v5 = ctx->hash[5]; u64 v13 = iv[5] ^ ctx->input_offset[1];
u64 v6 = ctx->hash[6]; u64 v14 = iv[6] ^ is_last_block;
u64 v7 = ctx->hash[7]; u64 v15 = iv[7];
// mangle work vector
uint64_t *input = ctx->input;
#define BLAKE2_G(v, a, b, c, d, x, y) \
v##a += v##b + x; v##d = rotr64(v##d ^ v##a, 32); \
v##c += v##d; v##b = rotr64(v##b ^ v##c, 24); \
v##a += v##b + y; v##d = rotr64(v##d ^ v##a, 16); \
v##c += v##d; v##b = rotr64(v##b ^ v##c, 63);
#define BLAKE2_ROUND(i) \
BLAKE2_G(v, 0, 4, 8, 12, input[sigma[i][ 0]], input[sigma[i][ 1]]);\
BLAKE2_G(v, 1, 5, 9, 13, input[sigma[i][ 2]], input[sigma[i][ 3]]);\
BLAKE2_G(v, 2, 6, 10, 14, input[sigma[i][ 4]], input[sigma[i][ 5]]);\
BLAKE2_G(v, 3, 7, 11, 15, input[sigma[i][ 6]], input[sigma[i][ 7]]);\
BLAKE2_G(v, 0, 5, 10, 15, input[sigma[i][ 8]], input[sigma[i][ 9]]);\
BLAKE2_G(v, 1, 6, 11, 12, input[sigma[i][10]], input[sigma[i][11]]);\
BLAKE2_G(v, 2, 7, 8, 13, input[sigma[i][12]], input[sigma[i][13]]);\
BLAKE2_G(v, 3, 4, 9, 14, input[sigma[i][14]], input[sigma[i][15]])
BLAKE2_ROUND(0); BLAKE2_ROUND(1); BLAKE2_ROUND(2); BLAKE2_ROUND(3);
BLAKE2_ROUND(4); BLAKE2_ROUND(5); BLAKE2_ROUND(6); BLAKE2_ROUND(7);
BLAKE2_ROUND(8); BLAKE2_ROUND(9); BLAKE2_ROUND(0); BLAKE2_ROUND(1);
// update hash
ctx->hash[0] ^= v0 ^ v8;
ctx->hash[1] ^= v1 ^ v9;
ctx->hash[2] ^= v2 ^ v10;
ctx->hash[3] ^= v3 ^ v11;
ctx->hash[4] ^= v4 ^ v12;
ctx->hash[5] ^= v5 ^ v13;
ctx->hash[6] ^= v6 ^ v14;
ctx->hash[7] ^= v7 ^ v15;
}
static void blake2b_set_input(crypto_blake2b_ctx *ctx, u8 input, size_t index)
{
size_t i;
if (index == 0) {
FOR (i, 0, 16) {
ctx->input[i] = 0;
}
}
size_t word = index >> 3;
size_t byte = index & 7;
ctx->input[word] |= (u64)input << (byte << 3);
}
static void blake2b_end_block(crypto_blake2b_ctx *ctx)
{
if (ctx->input_idx == 128) { // If buffer is full,
blake2b_incr(ctx); // update the input offset
blake2b_compress(ctx, 0); // and compress the (not last) block
ctx->input_idx = 0;
}
}
static void blake2b_update(crypto_blake2b_ctx *ctx,
const u8 *message, size_t message_size)
{
size_t i;
FOR (i, 0, message_size) {
blake2b_end_block(ctx);
blake2b_set_input(ctx, message[i], ctx->input_idx);
ctx->input_idx++;
}
}
void crypto_blake2b_general_init(crypto_blake2b_ctx *ctx, size_t hash_size,
const u8 *key, size_t key_size)
{
size_t i;
// initial hash
FOR (i, 0, 8) {
ctx->hash[i] = iv[i];
}
ctx->hash[0] ^= 0x01010000 ^ (key_size << 8) ^ hash_size;
ctx->input_offset[0] = 0; // begining of the input, no offset
ctx->input_offset[1] = 0; // begining of the input, no offset
ctx->hash_size = hash_size; // remember the hash size we want
ctx->input_idx = 0;
// if there is a key, the first block is that key (padded with zeroes)
if (key_size > 0) {
crypto_blake2b_update(ctx, key , key_size);
crypto_blake2b_update(ctx, zero, 128 - key_size);
}
}
void crypto_blake2b_init(crypto_blake2b_ctx *ctx)
{
crypto_blake2b_general_init(ctx, 64, 0, 0);
}
void crypto_blake2b_update(crypto_blake2b_ctx *ctx,
const u8 *message, size_t message_size)
{
size_t i, j;
// Align ourselves with block boundaries
size_t align = MIN(ALIGN(ctx->input_idx, 128), message_size);
blake2b_update(ctx, message, align);
message += align;
message_size -= align;
// Process the message block by block
FOR (i, 0, message_size >> 7) { // number of blocks
blake2b_end_block(ctx);
FOR (j, 0, 16) {
ctx->input[j] = load64_le(message + j*8);
}
message += 128;
ctx->input_idx = 128;
}
message_size &= 127;
// remaining bytes
blake2b_update(ctx, message, message_size);
}
void crypto_blake2b_final(crypto_blake2b_ctx *ctx, u8 *hash)
{
size_t i;
// Pad the end of the block with zeroes
FOR (i, ctx->input_idx, 128) {
blake2b_set_input(ctx, 0, i);
}
blake2b_incr(ctx); // update the input offset
blake2b_compress(ctx, -1); // compress the last block
size_t nb_words = ctx->hash_size >> 3;
FOR (i, 0, nb_words) {
store64_le(hash + i*8, ctx->hash[i]);
}
FOR (i, nb_words * 8, ctx->hash_size) {
hash[i] = (ctx->hash[i >> 3] >> (8 * (i & 7))) & 0xff;
}
WIPE_CTX(ctx);
}
void crypto_blake2b_general(u8 *hash , size_t hash_size,
const u8 *key , size_t key_size,
const u8 *message, size_t message_size)
{
crypto_blake2b_ctx ctx;
crypto_blake2b_general_init(&ctx, hash_size, key, key_size);
crypto_blake2b_update(&ctx, message, message_size);
crypto_blake2b_final(&ctx, hash);
}
void crypto_blake2b(u8 hash[64], const u8 *message, size_t message_size)
{
crypto_blake2b_general(hash, 64, 0, 0, message, message_size);
}
////////////////
/// Argon2 i ///
////////////////
// references to R, Z, Q etc. come from the spec
// Argon2 operates on 1024 byte blocks.
typedef struct { u64 a[128]; } block;
static void wipe_block(block *b)
{
size_t i;
volatile u64* a = b->a;
FOR (i, 0, 128) {
a[i] = 0;
}
}
// updates a blake2 hash with a 32 bit word, little endian.
static void blake_update_32(crypto_blake2b_ctx *ctx, u32 input)
{
u8 buf[4];
store32_le(buf, input);
crypto_blake2b_update(ctx, buf, 4);
WIPE_BUFFER(buf);
}
static void load_block(block *b, const u8 bytes[1024])
{
size_t i;
FOR (i, 0, 128) {
b->a[i] = load64_le(bytes + i*8);
}
}
static void store_block(u8 bytes[1024], const block *b)
{
size_t i;
FOR (i, 0, 128) {
store64_le(bytes + i*8, b->a[i]);
}
}
static void copy_block(block *o,const block*in){size_t i;FOR(i,0,128)o->a[i] = in->a[i];}
static void xor_block(block *o,const block*in){size_t i;FOR(i,0,128)o->a[i]^= in->a[i];}
// Hash with a virtually unlimited digest size.
// Doesn't extract more entropy than the base hash function.
// Mainly used for filling a whole kilobyte block with pseudo-random bytes.
// (One could use a stream cipher with a seed hash as the key, but
// this would introduce another dependency —and point of failure.)
static void extended_hash(u8 *digest, u32 digest_size,
const u8 *input , u32 input_size)
{
crypto_blake2b_ctx ctx;
crypto_blake2b_general_init(&ctx, MIN(digest_size, 64), 0, 0);
blake_update_32 (&ctx, digest_size);
crypto_blake2b_update (&ctx, input, input_size);
crypto_blake2b_final (&ctx, digest);
if (digest_size > 64) {
// the conversion to u64 avoids integer overflow on
// ludicrously big hash sizes.
u32 r = (((u64)digest_size + 31) >> 5) - 2;
u32 i = 1;
u32 in = 0;
u32 out = 32;
while (i < r) {
// Input and output overlap. This is intentional
crypto_blake2b(digest + out, digest + in, 64);
i += 1;
in += 32;
out += 32;
}
crypto_blake2b_general(digest + out, digest_size - (32 * r),
0, 0, // no key
digest + in , 64);
}
}
#define LSB(x) ((x) & 0xffffffff)
#define G(a, b, c, d) \
a += b + 2 * LSB(a) * LSB(b); d ^= a; d = rotr64(d, 32); \
c += d + 2 * LSB(c) * LSB(d); b ^= c; b = rotr64(b, 24); \
a += b + 2 * LSB(a) * LSB(b); d ^= a; d = rotr64(d, 16); \
c += d + 2 * LSB(c) * LSB(d); b ^= c; b = rotr64(b, 63)
#define ROUND(v0, v1, v2, v3, v4, v5, v6, v7, \
v8, v9, v10, v11, v12, v13, v14, v15) \
G(v0, v4, v8, v12); G(v1, v5, v9, v13); \
G(v2, v6, v10, v14); G(v3, v7, v11, v15); \
G(v0, v5, v10, v15); G(v1, v6, v11, v12); \
G(v2, v7, v8, v13); G(v3, v4, v9, v14)
// Core of the compression function G. Computes Z from R in place.
static void g_rounds(block *work_block)
{
int i;
// column rounds (work_block = Q)
for (i = 0; i < 128; i += 16) {
ROUND(work_block->a[i ], work_block->a[i + 1],
work_block->a[i + 2], work_block->a[i + 3],
work_block->a[i + 4], work_block->a[i + 5],
work_block->a[i + 6], work_block->a[i + 7],
work_block->a[i + 8], work_block->a[i + 9],
work_block->a[i + 10], work_block->a[i + 11],
work_block->a[i + 12], work_block->a[i + 13],
work_block->a[i + 14], work_block->a[i + 15]);
}
// row rounds (work_block = Z)
for (i = 0; i < 16; i += 2) {
ROUND(work_block->a[i ], work_block->a[i + 1],
work_block->a[i + 16], work_block->a[i + 17],
work_block->a[i + 32], work_block->a[i + 33],
work_block->a[i + 48], work_block->a[i + 49],
work_block->a[i + 64], work_block->a[i + 65],
work_block->a[i + 80], work_block->a[i + 81],
work_block->a[i + 96], work_block->a[i + 97],
work_block->a[i + 112], work_block->a[i + 113]);
}
}
// The compression function G (copy version for the first pass)
static void g_copy(block *result, const block *x, const block *y, block* tmp)
{
copy_block(tmp , x ); // tmp = X
xor_block (tmp , y ); // tmp = X ^ Y = R
copy_block(result, tmp); // result = R (only difference with g_xor)
g_rounds (tmp); // tmp = Z
xor_block (result, tmp); // result = R ^ Z
}
// The compression function G (xor version for subsequent passes)
static void g_xor(block *result, const block *x, const block *y, block *tmp)
{
copy_block(tmp , x ); // tmp = X
xor_block (tmp , y ); // tmp = X ^ Y = R
xor_block (result, tmp); // result = R ^ old (only difference with g_copy)
g_rounds (tmp); // tmp = Z
xor_block (result, tmp); // result = R ^ old ^ Z
}
// unary version of the compression function.
// The missing argument is implied zero.
// Does the transformation in place.
static void unary_g(block *work_block)
{
// work_block == R
block tmp;
copy_block(&tmp, work_block); // tmp = R
g_rounds(work_block); // work_block = Z
xor_block(work_block, &tmp); // work_block = Z ^ R
wipe_block(&tmp);
}
// Argon2i uses a kind of stream cipher to determine which reference
// block it will take to synthesise the next block. This context hold
// that stream's state. (It's very similar to Chacha20. The block b
// is anologous to Chacha's own pool)
typedef struct {
block b;
u32 pass_number;
u32 slice_number;
u32 nb_blocks;
u32 nb_iterations;
u32 ctr;
u32 offset;
} gidx_ctx;
// The block in the context will determine array indices. To avoid
// timing attacks, it only depends on public information. No looking
// at a previous block to seed the next. This makes offline attacks
// easier, but timing attacks are the bigger threat in many settings.
static void gidx_refresh(gidx_ctx *ctx)
{
size_t i;
// seed the begining of the block...
ctx->b.a[0] = ctx->pass_number;
ctx->b.a[1] = 0; // lane number (we have only one)
ctx->b.a[2] = ctx->slice_number;
ctx->b.a[3] = ctx->nb_blocks;
ctx->b.a[4] = ctx->nb_iterations;
ctx->b.a[5] = 1; // type: Argon2i
ctx->b.a[6] = ctx->ctr;
FOR (i, 7, 128) { ctx->b.a[i] = 0; } // ...then zero the rest out
// Shuffle the block thus: ctx->b = G((G(ctx->b, zero)), zero)
// (G "square" function), to get cheap pseudo-random numbers.
unary_g(&ctx->b);
unary_g(&ctx->b);
}
static void gidx_init(gidx_ctx *ctx,
u32 pass_number, u32 slice_number,
u32 nb_blocks, u32 nb_iterations)
{
ctx->pass_number = pass_number;
ctx->slice_number = slice_number;
ctx->nb_blocks = nb_blocks;
ctx->nb_iterations = nb_iterations;
ctx->ctr = 0;
// Offset from the begining of the segment. For the first slice
// of the first pass, we start at the *third* block, so the offset
// starts at 2, not 0.
if (pass_number != 0 || slice_number != 0) {
ctx->offset = 0;
} else {
ctx->offset = 2;
ctx->ctr++; // Compensates for missed lazy creation
gidx_refresh(ctx); // at the start of gidx_next()
}
}
static u32 gidx_next(gidx_ctx *ctx)
{
// lazily creates the offset block we need
if ((ctx->offset & 127) == 0) {
ctx->ctr++;
gidx_refresh(ctx);
}
u32 index = ctx->offset & 127; // save index for current call
u32 offset = ctx->offset; // save offset for current call
ctx->offset++; // update offset for next call
// Computes the area size.
// Pass 0 : all already finished segments plus already constructed
// blocks in this segment
// Pass 1+: 3 last segments plus already constructed
// blocks in this segment. THE SPEC SUGGESTS OTHERWISE.
// I CONFORM TO THE REFERENCE IMPLEMENTATION.
int first_pass = ctx->pass_number == 0;
u32 slice_size = ctx->nb_blocks >> 2;
u32 nb_segments = first_pass ? ctx->slice_number : 3;
u32 area_size = nb_segments * slice_size + offset - 1;
// Computes the starting position of the reference area.
// CONTRARY TO WHAT THE SPEC SUGGESTS, IT STARTS AT THE
// NEXT SEGMENT, NOT THE NEXT BLOCK.
u32 next_slice = ((ctx->slice_number + 1) & 3) * slice_size;
u32 start_pos = first_pass ? 0 : next_slice;
// Generate offset from J1 (no need for J2, there's only one lane)
u64 j1 = ctx->b.a[index] & 0xffffffff; // pseudo-random number
u64 x = (j1 * j1) >> 32;
u64 y = (area_size * x) >> 32;
u64 z = (area_size - 1) - y;
return (start_pos + z) % ctx->nb_blocks;
}
// Main algorithm
void crypto_argon2i_general(u8 *hash, u32 hash_size,
void *work_area, u32 nb_blocks,
u32 nb_iterations,
const u8 *password, u32 password_size,
const u8 *salt, u32 salt_size,
const u8 *key, u32 key_size,
const u8 *ad, u32 ad_size)
{
size_t i, pass_number, segment, current_block;
// work area seen as blocks (must be suitably aligned)
block *blocks = (block*)work_area;
{
crypto_blake2b_ctx ctx;
crypto_blake2b_init(&ctx);
blake_update_32 (&ctx, 1 ); // p: number of threads
blake_update_32 (&ctx, hash_size );
blake_update_32 (&ctx, nb_blocks );
blake_update_32 (&ctx, nb_iterations);
blake_update_32 (&ctx, 0x13 ); // v: version number
blake_update_32 (&ctx, 1 ); // y: Argon2i
blake_update_32 (&ctx, password_size);
crypto_blake2b_update(&ctx, password, password_size);
blake_update_32 (&ctx, salt_size);
crypto_blake2b_update(&ctx, salt, salt_size);
blake_update_32 (&ctx, key_size);
crypto_blake2b_update(&ctx, key, key_size);
blake_update_32 (&ctx, ad_size);
crypto_blake2b_update(&ctx, ad, ad_size);
u8 initial_hash[72]; // 64 bytes plus 2 words for future hashes
crypto_blake2b_final(&ctx, initial_hash);
// fill first 2 blocks
block tmp_block;
u8 hash_area[1024];
store32_le(initial_hash + 64, 0); // first additional word
store32_le(initial_hash + 68, 0); // second additional word
extended_hash(hash_area, 1024, initial_hash, 72);
load_block(&tmp_block, hash_area);
copy_block(blocks, &tmp_block);
store32_le(initial_hash + 64, 1); // slight modification
extended_hash(hash_area, 1024, initial_hash, 72);
load_block(&tmp_block, hash_area);
copy_block(blocks + 1, &tmp_block);
WIPE_BUFFER(initial_hash);
WIPE_BUFFER(hash_area);
wipe_block(&tmp_block);
}
// Actual number of blocks
nb_blocks -= nb_blocks & 3; // round down to 4 p (p == 1 thread)
const u32 segment_size = nb_blocks >> 2;
// fill (then re-fill) the rest of the blocks
block tmp;
gidx_ctx ctx;
FOR (pass_number, 0, nb_iterations) {
int first_pass = pass_number == 0;
FOR (segment, 0, 4) {
gidx_init(&ctx, (u32)pass_number, (u32)segment,
nb_blocks, nb_iterations);
// On the first segment of the first pass,
// blocks 0 and 1 are already filled.
// We use the offset to skip them.
u32 start_offset = first_pass && segment == 0 ? 2 : 0;
u32 segment_start = (u32)segment * segment_size + start_offset;
u32 segment_end = ((u32)segment + 1) * segment_size;
FOR (current_block, segment_start, segment_end) {
u32 reference_block = gidx_next(&ctx);
u32 previous_block = current_block == 0
? nb_blocks - 1
: (u32)current_block - 1;
block *c = blocks + current_block;
block *p = blocks + previous_block;
block *r = blocks + reference_block;
if (first_pass) { g_copy(c, p, r, &tmp); }
else { g_xor (c, p, r, &tmp); }
}
}
}
wipe_block(&ctx.b);
wipe_block(&tmp);