-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathage89.c
More file actions
1850 lines (1713 loc) · 58.6 KB
/
age89.c
File metadata and controls
1850 lines (1713 loc) · 58.6 KB
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
/*
* age89.verbose.c - age89 with optional diagnostic logging
*
* Use the -v flag to print each derivation step to stderr.
* Useful for debugging cross-platform differences (Unix vs MSVC 6 / Windows 98).
*
* Build (gcc): gcc -ansi -pedantic -O0 -o age89v age89.verbose.c
* Build (MSVC): cl /Za /Od age89.verbose.c
*
* Algorithms: X25519, ChaCha20-Poly1305, HKDF-SHA256, scrypt, Bech32
*
* Usage:
* Generate key pair: age89 -k
* Encrypt (public key): age89 -e -r age1PUBKEY [-o OUTPUT] [INPUT]
* Encrypt (passphrase): age89 -e -p [-o OUTPUT] [INPUT]
* Decrypt (private key): age89 -d -i AGE-SECRET-KEY-1... [-o OUTPUT] [INPUT]
* Decrypt (passphrase): age89 -d -p [-o OUTPUT] [INPUT]
*
* C89 CHANGES vs original:
* - u64/s64 emulated with two u32 (hi + lo)
* - All variable declarations moved to top of each block
* - Removed all double-slash comments, only block comments used
* - Removed 1LL literals, replaced with u64/s64 helpers
*/
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32; /* int = 32 bits on MSVC6/Win98, 32-bit Linux, and 64-bit Mac/Linux */
/* ================================================================
* VERBOSE DIAGNOSTIC LOGGING
* All output goes to stderr so stdout binary data is not corrupted.
* Enabled only when the -v flag is passed.
* ================================================================ */
static int verbose = 0;
static void dbg_hex(const char *lbl, const u8 *d, int n)
{
int i;
if(!verbose) return;
fprintf(stderr,"%s (%d bytes):\n",lbl,n);
for(i=0;i<n;i++){
if(i%16==0) fprintf(stderr," %04x: ",i);
fprintf(stderr,"%02x",d[i]);
if(i%16==15||i==n-1) fprintf(stderr,"\n");
else fprintf(stderr," ");
}
}
static void dbg_str(const char *lbl, const char *s)
{
if(!verbose) return;
fprintf(stderr,"%s: \"%s\"\n",lbl,s);
}
/* ================================================================
* 64-BIT EMULATION USING TWO u32
* hi = upper 32 bits, lo = lower 32 bits
* ================================================================ */
typedef struct { u32 lo; u32 hi; } u64;
typedef struct { u32 lo; u32 hi; } s64; /* signed: hi bit 31 = sign */
/* Build a u64 from two u32 */
static u64 u64_from32(u32 v)
{
u64 r;
r.hi = 0; r.lo = v;
return r;
}
/* Build a s64 from a single u32 (zero-extend, always positive) */
static s64 s64_from32(u32 v)
{
s64 r;
r.hi = 0; r.lo = v;
return r;
}
/* u64 addition */
static u64 u64_add(u64 a, u64 b)
{
u64 r;
r.lo = a.lo + b.lo;
r.hi = a.hi + b.hi + (r.lo < a.lo ? 1u : 0u);
return r;
}
/* s64 addition (same bit pattern as u64_add) */
static s64 s64_add(s64 a, s64 b)
{
s64 r;
r.lo = a.lo + b.lo;
r.hi = a.hi + b.hi + (r.lo < a.lo ? 1u : 0u);
return r;
}
/* s64 subtraction */
static s64 s64_sub(s64 a, s64 b)
{
s64 r;
r.lo = a.lo - b.lo;
r.hi = a.hi - b.hi - (a.lo < b.lo ? 1u : 0u);
return r;
}
/* u64 subtraction */
static u64 u64_sub(u64 a, u64 b)
{
u64 r;
r.lo = a.lo - b.lo;
r.hi = a.hi - b.hi - (a.lo < b.lo ? 1u : 0u);
return r;
}
/* u64 multiply: only lower 64 bits (sufficient for scrypt counters) */
static u64 u64_mul(u64 a, u64 b)
{
u64 r;
u32 a0, a1, b0, b1;
u32 mid, lo;
a0 = a.lo & 0xffffu; a1 = a.lo >> 16;
b0 = b.lo & 0xffffu; b1 = b.lo >> 16;
lo = a0 * b0;
mid = (a0 * b1) + (a1 * b0) + (lo >> 16);
r.lo = (mid << 16) | (lo & 0xffffu);
r.hi = a.hi * b.lo + a.lo * b.hi + (a1 * b1) + (mid >> 16);
return r;
}
/* s64 multiply: lower 64 bits of a*b, correct for signed values.
* a*b mod 2^64 = lo32(a)*lo32(b) + [hi32(a)*lo32(b) + lo32(a)*hi32(b)] * 2^32
* We split lo32 into two 16-bit halves to avoid 32-bit overflow. */
static s64 s64_mul(s64 a, s64 b)
{
s64 r;
u32 a0, a1, b0, b1;
u32 p00, p01, p10, mid, carry;
a0 = a.lo & 0xffffu; a1 = a.lo >> 16;
b0 = b.lo & 0xffffu; b1 = b.lo >> 16;
p00 = a0 * b0;
p01 = a0 * b1;
p10 = a1 * b0;
mid = (p00 >> 16) + (p01 & 0xffffu) + (p10 & 0xffffu);
carry = (mid >> 16) + (p01 >> 16) + (p10 >> 16) + a1 * b1;
r.lo = (mid << 16) | (p00 & 0xffffu);
r.hi = a.lo * b.hi + a.hi * b.lo + carry;
return r;
}
/* u64 logical shift right */
static u64 u64_shr(u64 a, int n)
{
u64 r;
if(n == 0) { r = a; }
else if(n < 32) {
r.lo = (a.lo >> n) | (a.hi << (32 - n));
r.hi = a.hi >> n;
} else {
r.lo = a.hi >> (n - 32);
r.hi = 0;
}
return r;
}
/* s64 arithmetic shift right */
static s64 s64_sar(s64 a, int n)
{
s64 r;
if(n == 0) { r = a; }
else if(n < 32) {
r.lo = (a.lo >> n) | (a.hi << (32 - n));
r.hi = (u32)((int)a.hi >> n);
} else {
r.lo = (u32)((int)a.hi >> (n - 32));
r.hi = (u32)((int)a.hi >> 31);
}
return r;
}
/* u64 logical shift left */
static u64 u64_shl(u64 a, int n)
{
u64 r;
if(n == 0) { r = a; }
else if(n < 32) {
r.hi = (a.hi << n) | (a.lo >> (32 - n));
r.lo = a.lo << n;
} else {
r.hi = a.lo << (n - 32);
r.lo = 0;
}
return r;
}
/* s64 shift left */
static s64 s64_shl(s64 a, int n)
{
s64 r;
if(n == 0) { r = a; }
else if(n < 32) {
r.hi = (a.hi << n) | (a.lo >> (32 - n));
r.lo = a.lo << n;
} else {
r.hi = a.lo << (n - 32);
r.lo = 0;
}
return r;
}
/* u64 bitwise AND */
static u64 u64_and(u64 a, u64 b)
{
u64 r;
r.lo = a.lo & b.lo;
r.hi = a.hi & b.hi;
return r;
}
/* s64 bitwise AND */
static s64 s64_and(s64 a, s64 b)
{
s64 r;
r.lo = a.lo & b.lo;
r.hi = a.hi & b.hi;
return r;
}
/* s64 bitwise XOR */
static s64 s64_xor(s64 a, s64 b)
{
s64 r;
r.lo = a.lo ^ b.lo;
r.hi = a.hi ^ b.hi;
return r;
}
/* s64 bitwise NOT */
static s64 s64_not(s64 a)
{
s64 r;
r.lo = ~a.lo;
r.hi = ~a.hi;
return r;
}
/* u64 less-than comparison */
static int u64_lt(u64 a, u64 b)
{
if(a.hi != b.hi) return a.hi < b.hi;
return a.lo < b.lo;
}
/* extract low/high 32 bits */
static u32 u64_lo32(u64 a) { return a.lo; }
static u32 u64_hi32(u64 a) { return a.hi; }
static u32 s64_lo32(s64 a) { return a.lo; }
/* store u64 little-endian */
static void u64_store_le(u8 *b, u64 x)
{
b[0]=(u8)x.lo; b[1]=(u8)(x.lo>>8); b[2]=(u8)(x.lo>>16); b[3]=(u8)(x.lo>>24);
b[4]=(u8)x.hi; b[5]=(u8)(x.hi>>8); b[6]=(u8)(x.hi>>16); b[7]=(u8)(x.hi>>24);
}
/* u64 increment */
static u64 u64_inc(u64 a)
{
return u64_add(a, u64_from32(1u));
}
/* s64 constant: value 1 */
static s64 s64_one(void)
{
s64 r; r.lo = 1; r.hi = 0;
return r;
}
/* s64 from signed int */
static s64 s64_from_int(int v)
{
s64 r;
r.lo = (u32)v;
r.hi = (v < 0) ? 0xffffffffu : 0u;
return r;
}
/* s64 to int (low 32 bits, for use as array index etc.) */
static int s64_to_int(s64 a) { return (int)a.lo; }
/* s64 multiply by small int */
static s64 s64_mul_int(s64 a, int b)
{
return s64_mul(a, s64_from_int(b));
}
/* ================================================================
* UTILITIES
* ================================================================ */
static void mem_wipe(void *p, size_t n)
{
volatile u8 *v = (volatile u8 *)p;
while (n--) *v++ = 0;
}
static u32 load32_le(const u8 *b)
{
return (u32)b[0]|((u32)b[1]<<8)|((u32)b[2]<<16)|((u32)b[3]<<24);
}
static void store32_le(u8 *b, u32 x)
{
b[0]=(u8)x; b[1]=(u8)(x>>8); b[2]=(u8)(x>>16); b[3]=(u8)(x>>24);
}
static u32 load32_be(const u8 *b)
{
return ((u32)b[0]<<24)|((u32)b[1]<<16)|((u32)b[2]<<8)|(u32)b[3];
}
static void store32_be(u8 *b, u32 x)
{
b[0]=(u8)(x>>24); b[1]=(u8)(x>>16); b[2]=(u8)(x>>8); b[3]=(u8)x;
}
static void store64_le(u8 *b, u64 x)
{
u64_store_le(b, x);
}
static u32 rotl32(u32 x, int n) { return (x<<n)|(x>>(32-n)); }
/* ASCII tolower (defined before bech32) */
static int ascii_tolower(int c)
{
if (c>='A' && c<='Z') return c+32;
return c;
}
/* ================================================================
* SHA-256
* ================================================================ */
typedef struct { u32 s[8]; u64 n; u8 b[64]; int bl; } sha256_ctx;
static const u32 sha256_K[64]={
0x428a2f98UL,0x71374491UL,0xb5c0fbcfUL,0xe9b5dba5UL,
0x3956c25bUL,0x59f111f1UL,0x923f82a4UL,0xab1c5ed5UL,
0xd807aa98UL,0x12835b01UL,0x243185beUL,0x550c7dc3UL,
0x72be5d74UL,0x80deb1feUL,0x9bdc06a7UL,0xc19bf174UL,
0xe49b69c1UL,0xefbe4786UL,0x0fc19dc6UL,0x240ca1ccUL,
0x2de92c6fUL,0x4a7484aaUL,0x5cb0a9dcUL,0x76f988daUL,
0x983e5152UL,0xa831c66dUL,0xb00327c8UL,0xbf597fc7UL,
0xc6e00bf3UL,0xd5a79147UL,0x06ca6351UL,0x14292967UL,
0x27b70a85UL,0x2e1b2138UL,0x4d2c6dfcUL,0x53380d13UL,
0x650a7354UL,0x766a0abbUL,0x81c2c92eUL,0x92722c85UL,
0xa2bfe8a1UL,0xa81a664bUL,0xc24b8b70UL,0xc76c51a3UL,
0xd192e819UL,0xd6990624UL,0xf40e3585UL,0x106aa070UL,
0x19a4c116UL,0x1e376c08UL,0x2748774cUL,0x34b0bcb5UL,
0x391c0cb3UL,0x4ed8aa4aUL,0x5b9cca4fUL,0x682e6ff3UL,
0x748f82eeUL,0x78a5636fUL,0x84c87814UL,0x8cc70208UL,
0x90befffaUL,0xa4506cebUL,0xbef9a3f7UL,0xc67178f2UL
};
static void sha256_transform(sha256_ctx *ctx, const u8 *blk)
{
u32 w[64],a,b,c,d,e,f,g,h,t1,t2,s0,s1;
int i;
for(i=0;i<16;i++) w[i]=load32_be(blk+i*4);
for(i=16;i<64;i++){
s0=rotl32(w[i-15],25)^rotl32(w[i-15],14)^(w[i-15]>>3);
s1=rotl32(w[i-2],15)^rotl32(w[i-2],13)^(w[i-2]>>10);
w[i]=w[i-16]+s0+w[i-7]+s1;
}
a=ctx->s[0];b=ctx->s[1];c=ctx->s[2];d=ctx->s[3];
e=ctx->s[4];f=ctx->s[5];g=ctx->s[6];h=ctx->s[7];
for(i=0;i<64;i++){
t1=h+(rotl32(e,26)^rotl32(e,21)^rotl32(e,7))+((e&f)^(~e&g))+sha256_K[i]+w[i];
t2=(rotl32(a,30)^rotl32(a,19)^rotl32(a,10))+((a&b)^(a&c)^(b&c));
h=g;g=f;f=e;e=d+t1;d=c;c=b;b=a;a=t1+t2;
}
ctx->s[0]+=a;ctx->s[1]+=b;ctx->s[2]+=c;ctx->s[3]+=d;
ctx->s[4]+=e;ctx->s[5]+=f;ctx->s[6]+=g;ctx->s[7]+=h;
}
static void sha256_init(sha256_ctx *ctx)
{
ctx->s[0]=0x6a09e667UL;ctx->s[1]=0xbb67ae85UL;
ctx->s[2]=0x3c6ef372UL;ctx->s[3]=0xa54ff53aUL;
ctx->s[4]=0x510e527fUL;ctx->s[5]=0x9b05688cUL;
ctx->s[6]=0x1f83d9abUL;ctx->s[7]=0x5be0cd19UL;
ctx->n = u64_from32(0);
ctx->bl=0;
}
static void sha256_update(sha256_ctx *ctx, const u8 *d, size_t len)
{
size_t i;
for(i=0;i<len;i++){
ctx->b[ctx->bl++]=d[i];
ctx->n = u64_add(ctx->n, u64_from32(1u));
if(ctx->bl==64){sha256_transform(ctx,ctx->b);ctx->bl=0;}
}
}
static void sha256_final(sha256_ctx *ctx, u8 *out)
{
u8 pad[64];
u64 bits;
int pl,i;
bits = u64_shl(ctx->n, 3); /* bits = n * 8 */
memset(pad,0,64); pad[0]=0x80;
pl=(ctx->bl<56)?(56-ctx->bl):(120-ctx->bl);
sha256_update(ctx,pad,(size_t)pl);
pad[0]=(u8)(bits.hi>>24);pad[1]=(u8)(bits.hi>>16);
pad[2]=(u8)(bits.hi>>8); pad[3]=(u8)(bits.hi);
pad[4]=(u8)(bits.lo>>24);pad[5]=(u8)(bits.lo>>16);
pad[6]=(u8)(bits.lo>>8); pad[7]=(u8)(bits.lo);
sha256_update(ctx,pad,8);
for(i=0;i<8;i++) store32_be(out+i*4,ctx->s[i]);
}
static void sha256_hash(const u8 *d, size_t n, u8 *out)
{
sha256_ctx c;
sha256_init(&c); sha256_update(&c,d,n); sha256_final(&c,out);
mem_wipe(&c,sizeof(c));
}
/* ================================================================
* HMAC-SHA256
* ================================================================ */
typedef struct { sha256_ctx in, out; } hmac_ctx;
static void hmac_init(hmac_ctx *ctx, const u8 *key, size_t klen)
{
u8 k[64],ip[64],op[64];
int i;
memset(k,0,64);
if(klen>64) sha256_hash(key,klen,k); else memcpy(k,key,klen);
for(i=0;i<64;i++){ip[i]=k[i]^0x36;op[i]=k[i]^0x5c;}
sha256_init(&ctx->in); sha256_update(&ctx->in,ip,64);
sha256_init(&ctx->out); sha256_update(&ctx->out,op,64);
mem_wipe(k,64);
}
static void hmac_update(hmac_ctx *ctx, const u8 *d, size_t n)
{
sha256_update(&ctx->in,d,n);
}
static void hmac_final(hmac_ctx *ctx, u8 *mac)
{
u8 t[32];
sha256_final(&ctx->in,t);
sha256_update(&ctx->out,t,32); sha256_final(&ctx->out,mac);
mem_wipe(t,32);
}
static void hmac_sha256(const u8 *k, size_t kl, const u8 *d, size_t dl, u8 *mac)
{
hmac_ctx c;
hmac_init(&c,k,kl); hmac_update(&c,d,dl); hmac_final(&c,mac);
mem_wipe(&c,sizeof(c));
}
/* ================================================================
* HKDF-SHA256
* ================================================================ */
static void hkdf_sha256(const u8 *ikm, size_t il, const u8 *salt, size_t sl,
const u8 *info, size_t nl, u8 *out, size_t ol)
{
static const u8 z32[32]={0};
u8 prk[32],t[32];
hmac_ctx c;
size_t done;
u8 ctr;
if(verbose) fprintf(stderr,"[HKDF] --- hkdf_sha256 begin ---\n");
dbg_hex("[HKDF] IKM", ikm, (int)il);
if(salt&&sl) { dbg_hex("[HKDF] salt", salt, (int)sl); hmac_sha256(salt,sl,ikm,il,prk); }
else { if(verbose) fprintf(stderr,"[HKDF] salt: (null -> 32 zero bytes)\n"); hmac_sha256(z32,32,ikm,il,prk); }
dbg_hex("[HKDF] PRK (extract)", prk, 32);
if(info&&nl) dbg_hex("[HKDF] info", info, (int)nl);
else { if(verbose) fprintf(stderr,"[HKDF] info: (none)\n"); }
done=0; ctr=0;
while(done<ol){
size_t chunk;
ctr++; hmac_init(&c,prk,32);
if(done>0) hmac_update(&c,t,32);
if(info&&nl) hmac_update(&c,info,nl);
hmac_update(&c,&ctr,1);
hmac_final(&c,t);
chunk=ol-done; if(chunk>32)chunk=32;
memcpy(out+done,t,chunk); done+=chunk;
}
dbg_hex("[HKDF] OUT (expand)", out, (int)ol);
if(verbose) fprintf(stderr,"[HKDF] --- hkdf_sha256 end ---\n");
mem_wipe(prk,32); mem_wipe(t,32); mem_wipe(&c,sizeof(c));
}
/* ================================================================
* PBKDF2-HMAC-SHA256
* ================================================================ */
static void pbkdf2_sha256(const u8 *pw, size_t pl, const u8 *salt, size_t sl,
u32 iters, u8 *out, size_t ol)
{
u32 blk;
size_t done;
done=0; blk=0;
while(done<ol){
u8 u[32],t[32],tmp[4];
u32 i;
int j;
hmac_ctx c;
size_t chunk;
blk++;
tmp[0]=(u8)(blk>>24);tmp[1]=(u8)(blk>>16);tmp[2]=(u8)(blk>>8);tmp[3]=(u8)blk;
hmac_init(&c,pw,pl);
hmac_update(&c,salt,sl); hmac_update(&c,tmp,4);
hmac_final(&c,u); memcpy(t,u,32);
for(i=1;i<iters;i++){
hmac_sha256(pw,pl,u,32,u);
for(j=0;j<32;j++) t[j]^=u[j];
}
chunk=ol-done; if(chunk>32)chunk=32;
memcpy(out+done,t,chunk); done+=chunk;
mem_wipe(u,32); mem_wipe(t,32);
}
}
/* ================================================================
* SALSA20/8 (used by scrypt)
* ================================================================ */
#define SR(a,b) (((a)<<(b))|((a)>>(32-(b))))
static void salsa20_8(u8 *p)
{
u32 x[16],B[16];
int i;
for(i=0;i<16;i++) B[i]=x[i]=load32_le(p+i*4);
for(i=0;i<4;i++){
x[4]^=SR(x[0]+x[12],7); x[8]^=SR(x[4]+x[0],9);
x[12]^=SR(x[8]+x[4],13); x[0]^=SR(x[12]+x[8],18);
x[9]^=SR(x[5]+x[1],7); x[13]^=SR(x[9]+x[5],9);
x[1]^=SR(x[13]+x[9],13); x[5]^=SR(x[1]+x[13],18);
x[14]^=SR(x[10]+x[6],7); x[2]^=SR(x[14]+x[10],9);
x[6]^=SR(x[2]+x[14],13); x[10]^=SR(x[6]+x[2],18);
x[3]^=SR(x[15]+x[11],7); x[7]^=SR(x[3]+x[15],9);
x[11]^=SR(x[7]+x[3],13); x[15]^=SR(x[11]+x[7],18);
x[1]^=SR(x[0]+x[3],7); x[2]^=SR(x[1]+x[0],9);
x[3]^=SR(x[2]+x[1],13); x[0]^=SR(x[3]+x[2],18);
x[6]^=SR(x[5]+x[4],7); x[7]^=SR(x[6]+x[5],9);
x[4]^=SR(x[7]+x[6],13); x[5]^=SR(x[4]+x[7],18);
x[11]^=SR(x[10]+x[9],7); x[8]^=SR(x[11]+x[10],9);
x[9]^=SR(x[8]+x[11],13); x[10]^=SR(x[9]+x[8],18);
x[12]^=SR(x[15]+x[14],7);x[13]^=SR(x[12]+x[15],9);
x[14]^=SR(x[13]+x[12],13);x[15]^=SR(x[14]+x[13],18);
}
for(i=0;i<16;i++) store32_le(p+i*4,B[i]+x[i]);
}
#undef SR
/* scryptBlockMix: B = 2*r*64 bytes, r=8 -> 1024 bytes */
static void blockmix(u8 *B, u8 *Y, int r)
{
u8 X[64];
int i;
int nb=2*r;
memcpy(X,B+(nb-1)*64,64);
for(i=0;i<nb;i++){
int j;
for(j=0;j<64;j++) X[j]^=B[i*64+j];
salsa20_8(X); memcpy(Y+i*64,X,64);
}
for(i=0;i<r;i++) memcpy(B+i*64, Y+2*i*64, 64);
for(i=0;i<r;i++) memcpy(B+(r+i)*64,Y+(2*i+1)*64,64);
}
/* scrypt_romix_file: fallback cuando no hay RAM suficiente.
* Usa un archivo temporal en disco para el array V.
* Es correcto pero MUY lento en HDDs (accesos aleatorios). */
static int scrypt_romix_file(u8 *B, u64 N, int r, unsigned long bsz, u8 *X, u8 *Y)
{
FILE *vf;
u64 jraw;
u8 *last;
unsigned long j, k;
unsigned long Nlo = u64_lo32(N);
vf = tmpfile();
if(!vf){
fprintf(stderr,"error: could not create temporary file on disk\n");
return -1;
}
fprintf(stderr,"[scrypt] using disk as temporary memory "
"(this may take a very long time)...\n");
/* Phase 1: fill V on disk sequentially */
memcpy(X,B,bsz);
for(j=0;j<Nlo;j++){
if(fwrite(X,1,bsz,vf)!=bsz){ fclose(vf); return -1; }
blockmix(X,Y,(int)r);
if((j & 4095)==0)
fprintf(stderr,"[scrypt] filling V: %lu/%lu\r",
(unsigned long)j, (unsigned long)Nlo);
}
fprintf(stderr,"\n[scrypt] V ready, mixing...\n");
/* Phase 2: mix with random seeks into the file */
for(j=0;j<Nlo;j++){
last=X+(2*(unsigned long)r-1)*64;
jraw.lo = load32_le(last);
jraw.hi = load32_le(last+4);
jraw = u64_and(jraw, u64_sub(N, u64_from32(1u)));
if(fseek(vf, (long)(u64_lo32(jraw) * bsz), SEEK_SET)!=0){
fclose(vf); return -1;
}
if(fread(Y,1,bsz,vf)!=bsz){ fclose(vf); return -1; }
for(k=0;k<bsz;k++) X[k]^=Y[k];
blockmix(X,Y,r);
if((j & 4095)==0)
fprintf(stderr,"[scrypt] mixing: %lu/%lu\r",
(unsigned long)j, (unsigned long)Nlo);
}
fprintf(stderr,"\n[scrypt] mix complete.\n");
fclose(vf);
memcpy(B,X,bsz);
return 0;
}
static int scrypt_romix(u8 *B, u64 N, int r)
{
unsigned long bsz;
u8 *V,*Y,*X;
u64 i;
u64 jraw;
u8 *last;
unsigned long ioff, joff, k;
int use_disk;
/*
* bsz = 128*r. For scrypt r=8, bsz=1024.
*/
bsz = (unsigned long)(128*r);
fprintf(stderr,"[scrypt] needs %lu KB of RAM...\n",
(unsigned long)(u64_lo32(N) * bsz / 1024));
V=(u8*)malloc(u64_lo32(N)*bsz);
Y=(u8*)malloc(bsz);
X=(u8*)malloc(bsz);
use_disk=0;
if(!V){
free(V); V=NULL;
fprintf(stderr,"[scrypt] not enough RAM, falling back to disk...\n");
use_disk=1;
}
if(!Y||!X){
free(V);free(Y);free(X);
fprintf(stderr,"error: not enough memory even for temporary buffers\n");
return -1;
}
if(use_disk){
int ret=scrypt_romix_file(B,N,r,bsz,X,Y);
mem_wipe(X,bsz); mem_wipe(Y,bsz);
free(X); free(Y);
return ret;
}
memcpy(X,B,bsz);
i = u64_from32(0);
while(u64_lt(i,N)){
ioff = u64_lo32(i) * bsz;
memcpy(V+ioff,X,bsz);
blockmix(X,Y,r);
i = u64_inc(i);
}
i = u64_from32(0);
while(u64_lt(i,N)){
last=X+(2*r-1)*64;
jraw.lo = load32_le(last);
jraw.hi = load32_le(last+4);
jraw = u64_and(jraw, u64_sub(N, u64_from32(1u)));
joff = u64_lo32(jraw) * bsz;
for(k=0;k<bsz;k++) X[k]^=V[joff+k];
blockmix(X,Y,r);
i = u64_inc(i);
}
memcpy(B,X,bsz);
mem_wipe(X,bsz);mem_wipe(Y,bsz);mem_wipe(V,u64_lo32(N)*bsz);
free(X);free(Y);free(V);
return 0;
}
static int scrypt_kdf(const u8 *pw, size_t pl, const u8 *salt, size_t sl,
int logN, u8 *out, size_t ol)
{
u64 N;
u8 B[1024];
if(logN<1||logN>30) return -1;
N = u64_shl(u64_from32(1u), logN);
pbkdf2_sha256(pw,pl,salt,sl,1,B,1024);
if(scrypt_romix(B,N,8)!=0) return -1;
pbkdf2_sha256(pw,pl,B,1024,1,out,ol);
mem_wipe(B,1024); return 0;
}
/* ================================================================
* CHACHA20
* ================================================================ */
typedef struct{u32 s[16];u8 ks[64];int pos;} cha_ctx;
#define QR(a,b,c,d) a+=b;d^=a;d=rotl32(d,16);c+=d;b^=c;b=rotl32(b,12);\
a+=b;d^=a;d=rotl32(d,8);c+=d;b^=c;b=rotl32(b,7)
static void cha_block(u32 *out, const u32 *in)
{
u32 x[16];
int i;
memcpy(x,in,64);
for(i=0;i<10;i++){
QR(x[0],x[4],x[8],x[12]);QR(x[1],x[5],x[9],x[13]);
QR(x[2],x[6],x[10],x[14]);QR(x[3],x[7],x[11],x[15]);
QR(x[0],x[5],x[10],x[15]);QR(x[1],x[6],x[11],x[12]);
QR(x[2],x[7],x[8],x[13]);QR(x[3],x[4],x[9],x[14]);
}
for(i=0;i<16;i++) out[i]=x[i]+in[i];
}
static void cha_init(cha_ctx *ctx, const u8 *key, const u8 *nonce, u32 ctr)
{
ctx->s[0]=0x61707865UL;ctx->s[1]=0x3320646eUL;
ctx->s[2]=0x79622d32UL;ctx->s[3]=0x6b206574UL;
ctx->s[4]=load32_le(key);ctx->s[5]=load32_le(key+4);
ctx->s[6]=load32_le(key+8);ctx->s[7]=load32_le(key+12);
ctx->s[8]=load32_le(key+16);ctx->s[9]=load32_le(key+20);
ctx->s[10]=load32_le(key+24);ctx->s[11]=load32_le(key+28);
ctx->s[12]=ctr;
ctx->s[13]=load32_le(nonce);ctx->s[14]=load32_le(nonce+4);ctx->s[15]=load32_le(nonce+8);
ctx->pos=64;
}
static void cha_xor(cha_ctx *ctx, const u8 *in, u8 *out, size_t n)
{
size_t i;
for(i=0;i<n;i++){
if(ctx->pos==64){
u32 bl[16];
int j;
cha_block(bl,ctx->s);
for(j=0;j<16;j++) store32_le(ctx->ks+j*4,bl[j]);
ctx->s[12]++; ctx->pos=0;
}
out[i]=in[i]^ctx->ks[ctx->pos++];
}
}
/* ================================================================
* POLY1305
* ================================================================ */
typedef struct{u32 r[5],h[5],pad[4];u8 buf[16];int left;} p1305_ctx;
static void p1305_init(p1305_ctx *ctx, const u8 *key)
{
ctx->r[0]= (load32_le(key )) &0x3ffffff;
ctx->r[1]= (load32_le(key+3)>>2) &0x3ffff03;
ctx->r[2]= (load32_le(key+6)>>4) &0x3ffc0ff;
ctx->r[3]= (load32_le(key+9)>>6) &0x3f03fff;
ctx->r[4]= (load32_le(key+12)>>8) &0x00fffff;
ctx->pad[0]=load32_le(key+16);ctx->pad[1]=load32_le(key+20);
ctx->pad[2]=load32_le(key+24);ctx->pad[3]=load32_le(key+28);
ctx->h[0]=ctx->h[1]=ctx->h[2]=ctx->h[3]=ctx->h[4]=0;
ctx->left=0;
}
static void p1305_block(p1305_ctx *ctx, const u8 *m, int hi)
{
u32 r0,r1,r2,r3,r4,s1,s2,s3,s4,h0,h1,h2,h3,h4,c;
u64 d0,d1,d2,d3,d4;
r0=ctx->r[0];r1=ctx->r[1];r2=ctx->r[2];r3=ctx->r[3];r4=ctx->r[4];
s1=r1*5;s2=r2*5;s3=r3*5;s4=r4*5;
h0=ctx->h[0];h1=ctx->h[1];h2=ctx->h[2];h3=ctx->h[3];h4=ctx->h[4];
h0+=(load32_le(m))&0x3ffffff;
h1+=(load32_le(m+3)>>2)&0x3ffffff;
h2+=(load32_le(m+6)>>4)&0x3ffffff;
h3+=(load32_le(m+9)>>6)&0x3ffffff;
h4+=(load32_le(m+12)>>8)|((u32)hi<<24);
/* d = h*r using u64 emulation */
d0=u64_add(u64_add(u64_add(u64_add(
u64_mul(u64_from32(h0),u64_from32(r0)),
u64_mul(u64_from32(h1),u64_from32(s4))),
u64_mul(u64_from32(h2),u64_from32(s3))),
u64_mul(u64_from32(h3),u64_from32(s2))),
u64_mul(u64_from32(h4),u64_from32(s1)));
d1=u64_add(u64_add(u64_add(u64_add(
u64_mul(u64_from32(h0),u64_from32(r1)),
u64_mul(u64_from32(h1),u64_from32(r0))),
u64_mul(u64_from32(h2),u64_from32(s4))),
u64_mul(u64_from32(h3),u64_from32(s3))),
u64_mul(u64_from32(h4),u64_from32(s2)));
d2=u64_add(u64_add(u64_add(u64_add(
u64_mul(u64_from32(h0),u64_from32(r2)),
u64_mul(u64_from32(h1),u64_from32(r1))),
u64_mul(u64_from32(h2),u64_from32(r0))),
u64_mul(u64_from32(h3),u64_from32(s4))),
u64_mul(u64_from32(h4),u64_from32(s3)));
d3=u64_add(u64_add(u64_add(u64_add(
u64_mul(u64_from32(h0),u64_from32(r3)),
u64_mul(u64_from32(h1),u64_from32(r2))),
u64_mul(u64_from32(h2),u64_from32(r1))),
u64_mul(u64_from32(h3),u64_from32(r0))),
u64_mul(u64_from32(h4),u64_from32(s4)));
d4=u64_add(u64_add(u64_add(u64_add(
u64_mul(u64_from32(h0),u64_from32(r4)),
u64_mul(u64_from32(h1),u64_from32(r3))),
u64_mul(u64_from32(h2),u64_from32(r2))),
u64_mul(u64_from32(h3),u64_from32(r1))),
u64_mul(u64_from32(h4),u64_from32(r0)));
/* carry reduction */
c=u64_lo32(u64_shr(d0,26)); h0=u64_lo32(d0)&0x3ffffff; d1=u64_add(d1,u64_from32(c));
c=u64_lo32(u64_shr(d1,26)); h1=u64_lo32(d1)&0x3ffffff; d2=u64_add(d2,u64_from32(c));
c=u64_lo32(u64_shr(d2,26)); h2=u64_lo32(d2)&0x3ffffff; d3=u64_add(d3,u64_from32(c));
c=u64_lo32(u64_shr(d3,26)); h3=u64_lo32(d3)&0x3ffffff; d4=u64_add(d4,u64_from32(c));
c=u64_lo32(u64_shr(d4,26)); h4=u64_lo32(d4)&0x3ffffff; h0+=c*5;
c=h0>>26;h0&=0x3ffffff;h1+=c;
ctx->h[0]=h0;ctx->h[1]=h1;ctx->h[2]=h2;ctx->h[3]=h3;ctx->h[4]=h4;
}
static void p1305_update(p1305_ctx *ctx, const u8 *m, size_t n)
{
size_t want;
if(ctx->left){
want=(size_t)(16-ctx->left); if(want>n)want=n;
memcpy(ctx->buf+ctx->left,m,want);
ctx->left+=(int)want; m+=want; n-=want;
if(ctx->left<16) return;
p1305_block(ctx,ctx->buf,1); ctx->left=0;
}
while(n>=16){p1305_block(ctx,m,1);m+=16;n-=16;}
if(n){memcpy(ctx->buf,m,n);ctx->left=(int)n;}
}
static void p1305_final(p1305_ctx *ctx, u8 *mac)
{
u32 h0,h1,h2,h3,h4,g0,g1,g2,g3,g4,c,mask;
u64 f;
if(ctx->left){
ctx->buf[ctx->left]=1;
memset(ctx->buf+ctx->left+1,0,(size_t)(16-ctx->left-1));
p1305_block(ctx,ctx->buf,0);
}
h0=ctx->h[0];h1=ctx->h[1];h2=ctx->h[2];h3=ctx->h[3];h4=ctx->h[4];
c=h1>>26;h1&=0x3ffffff;h2+=c;c=h2>>26;h2&=0x3ffffff;h3+=c;
c=h3>>26;h3&=0x3ffffff;h4+=c;c=h4>>26;h4&=0x3ffffff;h0+=c*5;
c=h0>>26;h0&=0x3ffffff;h1+=c;
g0=h0+5;c=g0>>26;g0&=0x3ffffff;g1=h1+c;c=g1>>26;g1&=0x3ffffff;
g2=h2+c;c=g2>>26;g2&=0x3ffffff;g3=h3+c;c=g3>>26;g3&=0x3ffffff;
g4=h4+c-(1<<26);
mask=(g4>>31)-1;
g0&=mask;g1&=mask;g2&=mask;g3&=mask;g4&=mask;mask=~mask;
h0=(h0&mask)|g0;h1=(h1&mask)|g1;h2=(h2&mask)|g2;h3=(h3&mask)|g3;h4=(h4&mask)|g4;
h0=((h0 )|(h1<<26))&0xffffffffu;
h1=((h1>> 6)|(h2<<20))&0xffffffffu;
h2=((h2>>12)|(h3<<14))&0xffffffffu;
h3=((h3>>18)|(h4<< 8))&0xffffffffu;
f=u64_add(u64_from32(h0),u64_from32(ctx->pad[0]));
store32_le(mac+ 0, u64_lo32(f));
f=u64_add(u64_add(u64_from32(h1),u64_from32(ctx->pad[1])),u64_from32(u64_hi32(f)));
store32_le(mac+ 4, u64_lo32(f));
f=u64_add(u64_add(u64_from32(h2),u64_from32(ctx->pad[2])),u64_from32(u64_hi32(f)));
store32_le(mac+ 8, u64_lo32(f));
f=u64_add(u64_add(u64_from32(h3),u64_from32(ctx->pad[3])),u64_from32(u64_hi32(f)));
store32_le(mac+12, u64_lo32(f));
mem_wipe(ctx,sizeof(*ctx));
}
/* ================================================================
* CHACHA20-POLY1305 AEAD (RFC 8439)
* ================================================================ */
static void cp_mac_segment(p1305_ctx *mac, const u8 *d, size_t n)
{
static const u8 z[16]={0};
p1305_update(mac,d,n);
if(n%16) p1305_update(mac,z,16-(n%16));
}
static void cp_seal(const u8 *key, const u8 *nonce,
const u8 *aad, size_t al,
const u8 *plain, size_t pl, u8 *out)
{
cha_ctx s;
p1305_ctx mac;
u8 otk[64],zeros[64],lens[16];
memset(zeros,0,64);
cha_init(&s,key,nonce,0); cha_xor(&s,zeros,otk,64);
cha_init(&s,key,nonce,1); cha_xor(&s,plain,out,pl);
p1305_init(&mac,otk);
cp_mac_segment(&mac,aad,al);
cp_mac_segment(&mac,out,pl);
store64_le(lens+0, u64_from32((u32)al));
store64_le(lens+8, u64_from32((u32)pl));
p1305_update(&mac,lens,16); p1305_final(&mac,out+pl);
mem_wipe(&s,sizeof(s)); mem_wipe(otk,64);
}
static int cp_open(const u8 *key, const u8 *nonce,
const u8 *aad, size_t al,
const u8 *cipher, size_t cl, u8 *out)
{
cha_ctx s;
p1305_ctx mac;
u8 otk[64],zeros[64],lens[16],tag[16];
size_t pl;
int diff,i;
if(verbose) fprintf(stderr,"[AEAD] cp_open cl=%u al=%u\n",(unsigned)cl,(unsigned)al);
dbg_hex("[AEAD] key", key, 32);
dbg_hex("[AEAD] nonce", nonce, 12);
if(cl<16){ if(verbose) fprintf(stderr,"[AEAD] ERROR: cl<16\n"); return -1; }
pl=cl-16;
dbg_hex("[AEAD] cipher (no tag)", cipher, (int)pl);
dbg_hex("[AEAD] expected tag", cipher+pl, 16);
memset(zeros,0,64);
cha_init(&s,key,nonce,0); cha_xor(&s,zeros,otk,64);
dbg_hex("[AEAD] OTK (Poly1305 key)", otk, 32);
p1305_init(&mac,otk);
cp_mac_segment(&mac,aad,al);
cp_mac_segment(&mac,cipher,pl);
store64_le(lens+0, u64_from32((u32)al));
store64_le(lens+8, u64_from32((u32)pl));
p1305_update(&mac,lens,16); p1305_final(&mac,tag);
dbg_hex("[AEAD] computed tag", tag, 16);
diff=0; for(i=0;i<16;i++) diff|=(tag[i]^cipher[pl+i]);
if(verbose){
if(diff) fprintf(stderr,"[AEAD] FAIL: tags do not match\n");
else fprintf(stderr,"[AEAD] OK: tags match\n");
}
mem_wipe(otk,64); mem_wipe(tag,16);
if(diff) return -1;
cha_init(&s,key,nonce,1); cha_xor(&s,cipher,out,pl);
mem_wipe(&s,sizeof(s)); return 0;
}
/* ================================================================
* CURVE25519 / X25519
* Based on TweetNaCl (public domain)
* GF(2^255-19) with 16 limbs of 16-bit signed values (stored in s64)
* ================================================================ */
typedef s64 gf[16];
static void gf0(gf r)
{
int i;
for(i=0;i<16;i++){r[i].lo=0;r[i].hi=0;}
}
static void gf1(gf r)
{
gf0(r); r[0]=s64_one();
}
static void gfcp(gf r, const gf a)
{
int i;
for(i=0;i<16;i++) r[i]=a[i];
}
static void gfadd(gf r, const gf a, const gf b)
{
int i;
for(i=0;i<16;i++) r[i]=s64_add(a[i],b[i]);
}
static void gfsub(gf r, const gf a, const gf b)
{
int i;
for(i=0;i<16;i++) r[i]=s64_sub(a[i],b[i]);
}
static void gfcar(gf r)
{
int i;
s64 c;
for(i=0;i<16;i++){
/* r[i] += (1<<16) */
r[i] = s64_add(r[i], s64_shl(s64_one(), 16));
/* c = r[i] >> 16 */
c = s64_sar(r[i], 16);
/* r[(i+1)*(i<15)] += c - 1 + 37*(c-1)*(i==15) */
if(i < 15){
r[i+1] = s64_add(r[i+1], s64_sub(c, s64_one()));
} else {
/* i==15: r[0] += 37*(c-1) */
r[0] = s64_add(r[0], s64_mul_int(s64_sub(c, s64_one()), 37));
}
/* r[i] -= c << 16 */
r[i] = s64_sub(r[i], s64_shl(c, 16));
}
}
static void gfsel(gf p, gf q, int b)
{
s64 t, c;
int i;
/* c = ~(b-1): if b==1 then c=0xFFFF...FFFF, if b==0 then c=0 */
c = s64_not(s64_from_int(b-1));
for(i=0;i<16;i++){
t = s64_and(c, s64_xor(p[i], q[i]));
p[i] = s64_xor(p[i], t);
q[i] = s64_xor(q[i], t);
}
}