-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathderpnet.h
1772 lines (1471 loc) · 50.5 KB
/
derpnet.h
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
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifndef DERPNET_API
# ifdef DERPNET_STATIC
# define DERPNET_API static inline
# else
# define DERPNET_API extern
# endif
#endif
//
// interface
//
typedef struct {
uint8_t Bytes[32];
} DerpKey;
DERPNET_API void DerpNet_CreateNewKey(DerpKey* UserSecret);
DERPNET_API void DerpNet_GetPublicKey(const DerpKey* UserSecret, DerpKey* UserPublic);
typedef struct {
uintptr_t Socket;
void* SocketEvent;
void* CredHandle[2];
void* CtxHandle[2];
uint8_t UserPrivateKey[32];
uint8_t LastPublicKey[32];
uint8_t LastSharedKey[32];
size_t BufferSize;
size_t BufferReceived;
size_t LastFrameSize;
size_t TotalReceived;
size_t TotalSent;
uint8_t Buffer[1 << 16];
} DerpNet;
// use DERP server hostname from https://login.tailscale.com/derpmap/default
DERPNET_API bool DerpNet_Open(DerpNet* Net, const char* DerpServer, const DerpKey* UserSecret);
DERPNET_API void DerpNet_Close(DerpNet* Net);
// returns 1 when received data from other user, pointer is valid till next call
// returns -1 if disconnected from server
// returns 0 if no new info is available to read
// if Wait=true, then never returns 0 - always waits for one incoming message
DERPNET_API int DerpNet_Recv(DerpNet* Net, DerpKey* ReceivedUserPublicKey, uint8_t** ReceivedData, uint32_t* ReceivedSize, bool Wait);
// returns false if disconnected
DERPNET_API bool DerpNet_Send(DerpNet* Net, const DerpKey* TargetUserPublicKey, const void* Data, size_t DataSize);
// use this if you're an expert!
DERPNET_API bool DerpNet_SendEx(DerpNet* Net, const DerpKey* TargetUserPublicKey, const uint8_t SharedKey[32], const uint8_t Nonce[24], const void* Data, size_t DataSize);
//
// implementation
//
#if defined(DERPNET_STATIC) || defined(DERPNET_IMPLEMENTATION)
#include <stdio.h>
#include <string.h>
#define SECURITY_WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <security.h>
#include <schannel.h>
#include <bcrypt.h>
#pragma comment (lib, "bcrypt")
#pragma comment (lib, "ws2_32")
#pragma comment (lib, "secur32")
//
// helpers
//
#if defined(__SIZEOF_INT128__)
typedef unsigned __int128 uint128;
# define mul64x64_128(out,a,b) out = (uint128)a * b
# define shr128_pair(out,hi,lo,shift) out = (uint64_t)((((uint128)hi << 64) | lo) >> (shift))
# define shl128_pair(out,hi,lo,shift) out = (uint64_t)(((((uint128)hi << 64) | lo) << (shift)) >> 64);
# define shr128(out,in,shift) out = (uint64_t)(in >> (shift))
# define shl128(out,in,shift) out = (uint64_t)((in << shift) >> 64)
# define add128(a,b) a += b
# define add128_64(a,b) a += (uint64_t)b
# define lo128(a) ((uint64_t)a)
# define hi128(a) ((uint64_t)(a >> 64))
#elif defined(_MSC_VER)
typedef struct { uint64_t lo, hi; } uint128;
# include <intrin.h>
# define mul64x64_128(out,a,b) out.lo = _umul128(a,b,&out.hi)
# define shr128_pair(out,hi,lo,shift) out = __shiftright128(lo, hi, shift)
# define shl128_pair(out,hi,lo,shift) out = __shiftleft128(lo, hi, shift)
# define shr128(out,in,shift) shr128_pair(out, in.hi, in.lo, shift)
# define shl128(out,in,shift) shl128_pair(out, in.hi, in.lo, shift)
# define add128(a,b) do { uint64_t p = a.lo; a.lo += b.lo; a.hi += b.hi + (a.lo < p); } while (0)
# define add128_64(a,b) do { uint64_t p = a.lo; a.lo += b; a.hi += (a.lo < p); } while (0)
# define lo128(a) (a.lo)
# define hi128(a) (a.hi)
#else
# error unsupported compiler/target
#endif
#if defined(__clang__)
# define rol32(x, n) __builtin_rotateleft32(x, n)
#elif defined(_MSC_VER)
# define rol32(x, n) _rotl(x, n)
#else
# define rol32(x, n) ( ((x) << (n)) | ((x) >> (32-(n))) )
#endif
#if !defined(NDEBUG)
# define DERPNET_ASSERT(cond) do { if (!(cond)) __debugbreak(); } while (0)
# define DERPNET_LOG(...) do { \
char LogBuffer[256]; \
snprintf(LogBuffer, sizeof(LogBuffer), "DERP: " __VA_ARGS__); \
OutputDebugStringA(LogBuffer); \
OutputDebugStringA("\n"); \
} while (0)
#else
# define DERPNET_ASSERT(cond) do { (void)(cond); } while (0)
# define DERPNET_LOG(...) do { (void)sizeof(__VA_ARGS__); } while (0)
#endif
static inline uint32_t Get32LE(const uint8_t* Buffer)
{
return (Buffer[3] << 24) + (Buffer[2] << 16) + (Buffer[1] << 8) + Buffer[0];
}
static inline uint32_t Get32BE(const uint8_t* Buffer)
{
return (Buffer[0] << 24) + (Buffer[1] << 16) + (Buffer[2] << 8) + Buffer[3];
}
static inline uint64_t Get64LE(const uint8_t* Buffer)
{
return ((uint64_t)Buffer[7] << 56)
+ ((uint64_t)Buffer[6] << 48)
+ ((uint64_t)Buffer[5] << 40)
+ ((uint64_t)Buffer[4] << 32)
+ ((uint64_t)Buffer[3] << 24)
+ ((uint64_t)Buffer[2] << 16)
+ ((uint64_t)Buffer[1] << 8)
+ ((uint64_t)Buffer[0]);
}
static inline void Set32LE(uint8_t* Buffer, uint32_t Value)
{
Buffer[0] = Value;
Buffer[1] = Value >> 8;
Buffer[2] = Value >> 16;
Buffer[3] = Value >> 24;
}
static inline void Set32BE(uint8_t* Buffer, uint32_t Value)
{
Buffer[3] = Value;
Buffer[2] = Value >> 8;
Buffer[1] = Value >> 16;
Buffer[0] = Value >> 24;
}
static inline void Set64LE(uint8_t* Buffer, uint64_t Value)
{
Buffer[0] = (uint8_t)(Value);
Buffer[1] = (uint8_t)(Value >> 8);
Buffer[2] = (uint8_t)(Value >> 16);
Buffer[3] = (uint8_t)(Value >> 24);
Buffer[4] = (uint8_t)(Value >> 32);
Buffer[5] = (uint8_t)(Value >> 40);
Buffer[6] = (uint8_t)(Value >> 48);
Buffer[7] = (uint8_t)(Value >> 56);
}
static inline void DerpNet__GetRandom(void* Buffer, size_t BufferSize)
{
int Status = BCryptGenRandom(NULL, (PUCHAR)Buffer, (ULONG)BufferSize, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
DERPNET_ASSERT(Status == 0);
}
//
// curve25519, based on public domain code from https://github.com/floodyberry/curve25519-donna
//
typedef uint64_t bignum25519[5];
static const uint64_t reduce_mask_51 = (1ULL << 51) - 1;
static void curve25519_copy(bignum25519 out, const bignum25519 in)
{
out[0] = in[0];
out[1] = in[1];
out[2] = in[2];
out[3] = in[3];
out[4] = in[4];
}
static void curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b)
{
out[0] = a[0] + b[0];
out[1] = a[1] + b[1];
out[2] = a[2] + b[2];
out[3] = a[3] + b[3];
out[4] = a[4] + b[4];
}
static void curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b)
{
const uint64_t two54m152 = (1ULL << 54) - 152;
const uint64_t two54m8 = (1ULL << 54) - 8;
out[0] = a[0] + two54m152 - b[0];
out[1] = a[1] + two54m8 - b[1];
out[2] = a[2] + two54m8 - b[2];
out[3] = a[3] + two54m8 - b[3];
out[4] = a[4] + two54m8 - b[4];
}
static void curve25519_scalar_product(bignum25519 out, const bignum25519 in, const uint64_t scalar)
{
uint128 a;
uint64_t c;
mul64x64_128(a, in[0], scalar); out[0] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
mul64x64_128(a, in[1], scalar); add128_64(a, c); out[1] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
mul64x64_128(a, in[2], scalar); add128_64(a, c); out[2] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
mul64x64_128(a, in[3], scalar); add128_64(a, c); out[3] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
mul64x64_128(a, in[4], scalar); add128_64(a, c); out[4] = lo128(a) & reduce_mask_51; shr128(c, a, 51);
out[0] += c * 19;
}
static void curve25519_mul(bignum25519 out, const bignum25519 a, const bignum25519 b)
{
uint128 mul;
uint128 t[5];
uint64_t r0,r1,r2,r3,r4,s0,s1,s2,s3,s4,c;
r0 = b[0];
r1 = b[1];
r2 = b[2];
r3 = b[3];
r4 = b[4];
s0 = a[0];
s1 = a[1];
s2 = a[2];
s3 = a[3];
s4 = a[4];
mul64x64_128(t[0], r0, s0);
mul64x64_128(t[1], r0, s1); mul64x64_128(mul, r1, s0); add128(t[1], mul);
mul64x64_128(t[2], r0, s2); mul64x64_128(mul, r2, s0); add128(t[2], mul); mul64x64_128(mul, r1, s1); add128(t[2], mul);
mul64x64_128(t[3], r0, s3); mul64x64_128(mul, r3, s0); add128(t[3], mul); mul64x64_128(mul, r1, s2); add128(t[3], mul); mul64x64_128(mul, r2, s1); add128(t[3], mul);
mul64x64_128(t[4], r0, s4); mul64x64_128(mul, r4, s0); add128(t[4], mul); mul64x64_128(mul, r3, s1); add128(t[4], mul); mul64x64_128(mul, r1, s3); add128(t[4], mul); mul64x64_128(mul, r2, s2); add128(t[4], mul);
r1 *= 19;
r2 *= 19;
r3 *= 19;
r4 *= 19;
mul64x64_128(mul, r4, s1); add128(t[0], mul); mul64x64_128(mul, r1, s4); add128(t[0], mul); mul64x64_128(mul, r2, s3); add128(t[0], mul); mul64x64_128(mul, r3, s2); add128(t[0], mul);
mul64x64_128(mul, r4, s2); add128(t[1], mul); mul64x64_128(mul, r2, s4); add128(t[1], mul); mul64x64_128(mul, r3, s3); add128(t[1], mul);
mul64x64_128(mul, r4, s3); add128(t[2], mul); mul64x64_128(mul, r3, s4); add128(t[2], mul);
mul64x64_128(mul, r4, s4); add128(t[3], mul);
r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
add128_64(t[1], c); r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
add128_64(t[2], c); r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
add128_64(t[3], c); r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
add128_64(t[4], c); r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
r1 += c;
out[0] = r0;
out[1] = r1;
out[2] = r2;
out[3] = r3;
out[4] = r4;
}
static void curve25519_square_times(bignum25519 out, const bignum25519 in, uint64_t count)
{
uint128 mul;
uint128 t[5];
uint64_t r0,r1,r2,r3,r4,c;
uint64_t d0,d1,d2,d4,d419;
r0 = in[0];
r1 = in[1];
r2 = in[2];
r3 = in[3];
r4 = in[4];
do
{
d0 = r0 * 2;
d1 = r1 * 2;
d2 = r2 * 2 * 19;
d419 = r4 * 19;
d4 = d419 * 2;
mul64x64_128(t[0], r0, r0); mul64x64_128(mul, d4, r1); add128(t[0], mul); mul64x64_128(mul, d2, r3); add128(t[0], mul);
mul64x64_128(t[1], d0, r1); mul64x64_128(mul, d4, r2); add128(t[1], mul); mul64x64_128(mul, r3, r3 * 19); add128(t[1], mul);
mul64x64_128(t[2], d0, r2); mul64x64_128(mul, r1, r1); add128(t[2], mul); mul64x64_128(mul, d4, r3); add128(t[2], mul);
mul64x64_128(t[3], d0, r3); mul64x64_128(mul, d1, r2); add128(t[3], mul); mul64x64_128(mul, r4, d419); add128(t[3], mul);
mul64x64_128(t[4], d0, r4); mul64x64_128(mul, d1, r3); add128(t[4], mul); mul64x64_128(mul, r2, r2); add128(t[4], mul);
r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
add128_64(t[1], c); r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
add128_64(t[2], c); r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
add128_64(t[3], c); r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
add128_64(t[4], c); r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
r1 += c;
}
while(--count);
out[0] = r0;
out[1] = r1;
out[2] = r2;
out[3] = r3;
out[4] = r4;
}
static void curve25519_square(bignum25519 out, const bignum25519 in)
{
uint128 mul;
uint128 t[5];
uint64_t r0,r1,r2,r3,r4,c;
uint64_t d0,d1,d2,d4,d419;
r0 = in[0];
r1 = in[1];
r2 = in[2];
r3 = in[3];
r4 = in[4];
d0 = r0 * 2;
d1 = r1 * 2;
d2 = r2 * 2 * 19;
d419 = r4 * 19;
d4 = d419 * 2;
mul64x64_128(t[0], r0, r0); mul64x64_128(mul, d4, r1); add128(t[0], mul); mul64x64_128(mul, d2, r3); add128(t[0], mul);
mul64x64_128(t[1], d0, r1); mul64x64_128(mul, d4, r2); add128(t[1], mul); mul64x64_128(mul, r3, r3 * 19); add128(t[1], mul);
mul64x64_128(t[2], d0, r2); mul64x64_128(mul, r1, r1); add128(t[2], mul); mul64x64_128(mul, d4, r3); add128(t[2], mul);
mul64x64_128(t[3], d0, r3); mul64x64_128(mul, d1, r2); add128(t[3], mul); mul64x64_128(mul, r4, d419); add128(t[3], mul);
mul64x64_128(t[4], d0, r4); mul64x64_128(mul, d1, r3); add128(t[4], mul); mul64x64_128(mul, r2, r2); add128(t[4], mul);
r0 = lo128(t[0]) & reduce_mask_51; shr128(c, t[0], 51);
add128_64(t[1], c); r1 = lo128(t[1]) & reduce_mask_51; shr128(c, t[1], 51);
add128_64(t[2], c); r2 = lo128(t[2]) & reduce_mask_51; shr128(c, t[2], 51);
add128_64(t[3], c); r3 = lo128(t[3]) & reduce_mask_51; shr128(c, t[3], 51);
add128_64(t[4], c); r4 = lo128(t[4]) & reduce_mask_51; shr128(c, t[4], 51);
r0 += c * 19; c = r0 >> 51; r0 = r0 & reduce_mask_51;
r1 += c;
out[0] = r0;
out[1] = r1;
out[2] = r2;
out[3] = r3;
out[4] = r4;
}
static void curve25519_expand(bignum25519 out, const uint8_t* in)
{
uint64_t x0 = Get64LE(&in[ 0]);
uint64_t x1 = Get64LE(&in[ 8]);
uint64_t x2 = Get64LE(&in[16]);
uint64_t x3 = Get64LE(&in[24]);
out[0] = x0 & reduce_mask_51; x0 = (x0 >> 51) | (x1 << 13);
out[1] = x0 & reduce_mask_51; x1 = (x1 >> 38) | (x2 << 26);
out[2] = x1 & reduce_mask_51; x2 = (x2 >> 25) | (x3 << 39);
out[3] = x2 & reduce_mask_51; x3 = (x3 >> 12);
out[4] = x3 & reduce_mask_51; /* ignore the top bit */
}
static void curve25519_contract(uint8_t* out, const bignum25519 input)
{
uint64_t t[5];
uint64_t f;
t[0] = input[0];
t[1] = input[1];
t[2] = input[2];
t[3] = input[3];
t[4] = input[4];
#define curve25519_contract_carry() \
t[1] += t[0] >> 51; t[0] &= reduce_mask_51; \
t[2] += t[1] >> 51; t[1] &= reduce_mask_51; \
t[3] += t[2] >> 51; t[2] &= reduce_mask_51; \
t[4] += t[3] >> 51; t[3] &= reduce_mask_51;
#define curve25519_contract_carry_full() curve25519_contract_carry() \
t[0] += 19 * (t[4] >> 51); t[4] &= reduce_mask_51;
#define curve25519_contract_carry_final() curve25519_contract_carry() \
t[4] &= reduce_mask_51;
curve25519_contract_carry_full()
curve25519_contract_carry_full()
/* now t is between 0 and 2^255-1, properly carried. */
/* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
t[0] += 19;
curve25519_contract_carry_full()
/* now between 19 and 2^255-1 in both cases, and offset by 19. */
t[0] += 0x8000000000000 - 19;
t[1] += 0x8000000000000 - 1;
t[2] += 0x8000000000000 - 1;
t[3] += 0x8000000000000 - 1;
t[4] += 0x8000000000000 - 1;
/* now between 2^255 and 2^256-20, and offset by 2^255. */
curve25519_contract_carry_final()
#define write51full(n,shift) \
f = ((t[n] >> shift) | (t[n+1] << (51 - shift))); \
for (int i = 0; i < 8; i++, f >>= 8) *out++ = (uint8_t)f;
#define write51(n) write51full(n,13*n)
write51(0)
write51(1)
write51(2)
write51(3)
#undef curve25519_contract_carry
#undef curve25519_contract_carry_full
#undef curve25519_contract_carry_final
#undef write51full
#undef write51
}
static void curve25519_swap_conditional(bignum25519 x, bignum25519 qpx, uint64_t iswap)
{
const uint64_t swap = (uint64_t)(-(int64_t)iswap);
uint64_t x0,x1,x2,x3,x4;
x0 = swap & (x[0] ^ qpx[0]); x[0] ^= x0; qpx[0] ^= x0;
x1 = swap & (x[1] ^ qpx[1]); x[1] ^= x1; qpx[1] ^= x1;
x2 = swap & (x[2] ^ qpx[2]); x[2] ^= x2; qpx[2] ^= x2;
x3 = swap & (x[3] ^ qpx[3]); x[3] ^= x3; qpx[3] ^= x3;
x4 = swap & (x[4] ^ qpx[4]); x[4] ^= x4; qpx[4] ^= x4;
}
static void curve25519_pow_two5mtwo0_two250mtwo0(bignum25519 b)
{
bignum25519 t0, c;
/* 2^5 - 2^0 */ /* b */
/* 2^10 - 2^5 */ curve25519_square_times(t0, b, 5);
/* 2^10 - 2^0 */ curve25519_mul(b, t0, b);
/* 2^20 - 2^10 */ curve25519_square_times(t0, b, 10);
/* 2^20 - 2^0 */ curve25519_mul(c, t0, b);
/* 2^40 - 2^20 */ curve25519_square_times(t0, c, 20);
/* 2^40 - 2^0 */ curve25519_mul(t0, t0, c);
/* 2^50 - 2^10 */ curve25519_square_times(t0, t0, 10);
/* 2^50 - 2^0 */ curve25519_mul(b, t0, b);
/* 2^100 - 2^50 */ curve25519_square_times(t0, b, 50);
/* 2^100 - 2^0 */ curve25519_mul(c, t0, b);
/* 2^200 - 2^100 */ curve25519_square_times(t0, c, 100);
/* 2^200 - 2^0 */ curve25519_mul(t0, t0, c);
/* 2^250 - 2^50 */ curve25519_square_times(t0, t0, 50);
/* 2^250 - 2^0 */ curve25519_mul(b, t0, b);
}
static void curve25519_recip(bignum25519 out, const bignum25519 z)
{
bignum25519 a, t0, b;
/* 2 */ curve25519_square(a, z); /* a = 2 */
/* 8 */ curve25519_square_times(t0, a, 2);
/* 9 */ curve25519_mul(b, t0, z); /* b = 9 */
/* 11 */ curve25519_mul(a, b, a); /* a = 11 */
/* 22 */ curve25519_square(t0, a);
/* 2^5 - 2^0 = 31 */ curve25519_mul(b, t0, b);
/* 2^250 - 2^0 */ curve25519_pow_two5mtwo0_two250mtwo0(b);
/* 2^255 - 2^5 */ curve25519_square_times(b, b, 5);
/* 2^255 - 21 */ curve25519_mul(out, b, a);
}
static void curve25519_scalarmult(uint8_t* mypublic, const uint8_t* n, const uint8_t* basepoint)
{
// curve25519-donna-64bit.h
// curve25519-donna-common.h
bignum25519 nqpqx = { 1 }, nqpqz = { 0 }, nqz = { 1 }, nqx;
bignum25519 q, qx, qpqx, qqx, zzz, zmone;
curve25519_expand(q, basepoint);
curve25519_copy(nqx, q);
/* bit 255 is always 0, and bit 254 is always 1, so skip bit 255 and
start pre-swapped on bit 254 */
size_t lastbit = 1;
/* we are doing bits 254..3 in the loop, but are swapping in bits 253..2 */
for (int i = 253; i >= 2; i--)
{
curve25519_add(qx, nqx, nqz);
curve25519_sub(nqz, nqx, nqz);
curve25519_add(qpqx, nqpqx, nqpqz);
curve25519_sub(nqpqz, nqpqx, nqpqz);
curve25519_mul(nqpqx, qpqx, nqz);
curve25519_mul(nqpqz, qx, nqpqz);
curve25519_add(qqx, nqpqx, nqpqz);
curve25519_sub(nqpqz, nqpqx, nqpqz);
curve25519_square(nqpqz, nqpqz);
curve25519_square(nqpqx, qqx);
curve25519_mul(nqpqz, nqpqz, q);
curve25519_square(qx, qx);
curve25519_square(nqz, nqz);
curve25519_mul(nqx, qx, nqz);
curve25519_sub(nqz, qx, nqz);
curve25519_scalar_product(zzz, nqz, 121665);
curve25519_add(zzz, zzz, qx);
curve25519_mul(nqz, nqz, zzz);
size_t bit = (n[i/8] >> (i & 7)) & 1;
curve25519_swap_conditional(nqx, nqpqx, bit ^ lastbit);
curve25519_swap_conditional(nqz, nqpqz, bit ^ lastbit);
lastbit = bit;
}
/* the final 3 bits are always zero, so we only need to double */
for (int i = 0; i < 3; i++)
{
curve25519_add(qx, nqx, nqz);
curve25519_sub(nqz, nqx, nqz);
curve25519_square(qx, qx);
curve25519_square(nqz, nqz);
curve25519_mul(nqx, qx, nqz);
curve25519_sub(nqz, qx, nqz);
curve25519_scalar_product(zzz, nqz, 121665);
curve25519_add(zzz, zzz, qx);
curve25519_mul(nqz, nqz, zzz);
}
curve25519_recip(zmone, nqz);
curve25519_mul(nqz, nqx, zmone);
curve25519_contract(mypublic, nqz);
}
//
// poly1305, based on public domain code from https://github.com/floodyberry/poly1305-donna
//
#define poly1305_block_size 16
typedef struct {
uint64_t r[3];
uint64_t h[3];
uint64_t pad[2];
size_t leftover;
unsigned char buffer[poly1305_block_size];
unsigned char final;
} poly1305_state_internal_t;
static void poly1305_init(poly1305_state_internal_t* st, const uint8_t key[32])
{
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
uint64_t t0 = Get64LE(&key[0]);
uint64_t t1 = Get64LE(&key[8]);
st->r[0] = ( t0 ) & 0xffc0fffffff;
st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
st->r[2] = ((t1 >> 24) ) & 0x00ffffffc0f;
/* h = 0 */
st->h[0] = 0;
st->h[1] = 0;
st->h[2] = 0;
/* save pad for later */
st->pad[0] = Get64LE(&key[16]);
st->pad[1] = Get64LE(&key[24]);
st->leftover = 0;
st->final = 0;
}
static void poly1305_blocks(poly1305_state_internal_t* st, const uint8_t* m, size_t bytes)
{
const uint64_t hibit = (st->final) ? 0 : (1ULL << 40); /* 1 << 128 */
uint64_t r0,r1,r2;
uint64_t s1,s2;
uint64_t h0,h1,h2;
uint64_t c;
uint128 d0,d1,d2,d;
r0 = st->r[0];
r1 = st->r[1];
r2 = st->r[2];
h0 = st->h[0];
h1 = st->h[1];
h2 = st->h[2];
s1 = r1 * (5 << 2);
s2 = r2 * (5 << 2);
while (bytes >= poly1305_block_size)
{
/* h += m[i] */
uint64_t t0 = Get64LE(&m[0]);
uint64_t t1 = Get64LE(&m[8]);
h0 += (( t0 ) & 0xfffffffffff);
h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
h2 += (((t1 >> 24) ) & 0x3ffffffffff) | hibit;
/* h *= r */
mul64x64_128(d0, h0, r0); mul64x64_128(d, h1, s2); add128(d0, d); mul64x64_128(d, h2, s1); add128(d0, d);
mul64x64_128(d1, h0, r1); mul64x64_128(d, h1, r0); add128(d1, d); mul64x64_128(d, h2, s2); add128(d1, d);
mul64x64_128(d2, h0, r2); mul64x64_128(d, h1, r1); add128(d2, d); mul64x64_128(d, h2, r0); add128(d2, d);
/* (partial) h %= p */
shr128(c, d0, 44); h0 = lo128(d0) & 0xfffffffffff;
add128_64(d1, c); shr128(c, d1, 44); h1 = lo128(d1) & 0xfffffffffff;
add128_64(d2, c); shr128(c, d2, 42); h2 = lo128(d2) & 0x3ffffffffff;
h0 += c * 5; c = (h0 >> 44); h0 = h0 & 0xfffffffffff;
h1 += c;
m += poly1305_block_size;
bytes -= poly1305_block_size;
}
st->h[0] = h0;
st->h[1] = h1;
st->h[2] = h2;
}
void poly1305_finish(poly1305_state_internal_t* st, uint8_t mac[16])
{
uint64_t h0,h1,h2,c;
uint64_t g0,g1,g2;
uint64_t t0,t1;
/* process the remaining block */
if (st->leftover)
{
size_t i = st->leftover;
st->buffer[i] = 1;
for (i = i + 1; i < poly1305_block_size; i++)
st->buffer[i] = 0;
st->final = 1;
poly1305_blocks(st, st->buffer, poly1305_block_size);
}
/* fully carry h */
h0 = st->h[0];
h1 = st->h[1];
h2 = st->h[2];
c = (h1 >> 44); h1 &= 0xfffffffffff;
h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff;
h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
h1 += c;
/* compute h + -p */
g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff;
g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff;
g2 = h2 + c - (1ULL << 42);
/* select h if h < p, or h + -p if h >= p */
c = (g2 >> ((sizeof(uint64_t) * 8) - 1)) - 1;
g0 &= c;
g1 &= c;
g2 &= c;
c = ~c;
h0 = (h0 & c) | g0;
h1 = (h1 & c) | g1;
h2 = (h2 & c) | g2;
/* h = (h + pad) */
t0 = st->pad[0];
t1 = st->pad[1];
h0 += (( t0 ) & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff;
h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff;
h2 += (((t1 >> 24) ) & 0x3ffffffffff) + c; h2 &= 0x3ffffffffff;
/* mac = h % (2^128) */
h0 = ((h0 ) | (h1 << 44));
h1 = ((h1 >> 20) | (h2 << 24));
Set64LE(&mac[0], h0);
Set64LE(&mac[8], h1);
}
static void poly1305_update(poly1305_state_internal_t* st, const uint8_t* m, size_t bytes)
{
/* handle leftover */
if (st->leftover)
{
size_t want = (poly1305_block_size - st->leftover);
if (want > bytes)
want = bytes;
memcpy(st->buffer + st->leftover, m, want);
bytes -= want;
m += want;
st->leftover += want;
if (st->leftover < poly1305_block_size)
return;
poly1305_blocks(st, st->buffer, poly1305_block_size);
st->leftover = 0;
}
/* process full blocks */
if (bytes >= poly1305_block_size)
{
size_t want = (bytes & ~(poly1305_block_size - 1));
poly1305_blocks(st, m, want);
m += want;
bytes -= want;
}
/* store leftover */
if (bytes)
{
memcpy(st->buffer + st->leftover, m, bytes);
st->leftover += bytes;
}
}
static void poly1305_auth(uint8_t mac[16], const uint8_t* m, size_t bytes, const uint8_t key[32])
{
poly1305_state_internal_t st;
poly1305_init(&st, key);
poly1305_update(&st, m, bytes);
poly1305_finish(&st, mac);
}
static int poly1305_verify(const uint8_t mac1[16], const uint8_t mac2[16])
{
uint64_t a1, a2, b1, b2;
memcpy(&a1, mac1 + 0, sizeof(a1));
memcpy(&a2, mac1 + 8, sizeof(a2));
memcpy(&b1, mac2 + 0, sizeof(b1));
memcpy(&b2, mac2 + 8, sizeof(b2));
return ((a1 ^ b1) | (a2 ^ b2)) == 0;
}
//
// salsa20, based on info from https://en.wikipedia.org/wiki/Salsa20#Structure
//
static void salsa20_rounds(uint32_t x[16])
{
for (int i = 0; i < 20; i += 2)
{
uint32_t t;
#define Q(a,b,c,d) \
t = x[a] + x[d]; x[b] ^= rol32(t, 7); \
t = x[b] + x[a]; x[c] ^= rol32(t, 9); \
t = x[c] + x[b]; x[d] ^= rol32(t, 13); \
t = x[d] + x[c]; x[a] ^= rol32(t, 18)
Q( 0, 4, 8, 12);
Q( 5, 9, 13, 1);
Q(10, 14, 2, 6);
Q(15, 3, 7, 11);
Q( 0, 1, 2, 3);
Q( 5, 6, 7, 4);
Q(10, 11, 8, 9);
Q(15, 12, 13, 14);
#undef Q
}
}
static const char salsa20_constant[] = "expand 32-byte k";
static void hsalsa20(uint8_t Output[32], const uint8_t Input[16], const uint8_t Key[32])
{
uint32_t x[16];
x[ 0] = Get32LE((uint8_t*)&salsa20_constant[0]);
x[ 1] = Get32LE(&Key[ 0]);
x[ 2] = Get32LE(&Key[ 4]);
x[ 3] = Get32LE(&Key[ 8]);
x[ 4] = Get32LE(&Key[12]);
x[ 5] = Get32LE((uint8_t*)&salsa20_constant[4]);
x[ 6] = Get32LE(&Input[ 0]);
x[ 7] = Get32LE(&Input[ 4]);
x[ 8] = Get32LE(&Input[ 8]);
x[ 9] = Get32LE(&Input[12]);
x[10] = Get32LE((uint8_t*)&salsa20_constant[8]);
x[11] = Get32LE(&Key[16]);
x[12] = Get32LE(&Key[20]);
x[13] = Get32LE(&Key[24]);
x[14] = Get32LE(&Key[28]);
x[15] = Get32LE((uint8_t*)&salsa20_constant[12]);
salsa20_rounds(x);
Set32LE(&Output[ 0], x[ 0]);
Set32LE(&Output[ 4], x[ 5]);
Set32LE(&Output[ 8], x[10]);
Set32LE(&Output[12], x[15]);
Set32LE(&Output[16], x[ 6]);
Set32LE(&Output[20], x[ 7]);
Set32LE(&Output[24], x[ 8]);
Set32LE(&Output[28], x[ 9]);
}
static void salsa20(uint8_t Output[64], const uint8_t Input[16], const uint8_t Key[32])
{
uint32_t x[16];
x[ 0] = Get32LE((uint8_t*)&salsa20_constant[0]);
x[ 1] = Get32LE(&Key[ 0]);
x[ 2] = Get32LE(&Key[ 4]);
x[ 3] = Get32LE(&Key[ 8]);
x[ 4] = Get32LE(&Key[12]);
x[ 5] = Get32LE((uint8_t*)&salsa20_constant[4]);
x[ 6] = Get32LE(&Input[ 0]);
x[ 7] = Get32LE(&Input[ 4]);
x[ 8] = Get32LE(&Input[ 8]);
x[ 9] = Get32LE(&Input[12]);
x[10] = Get32LE((uint8_t*)&salsa20_constant[8]);
x[11] = Get32LE(&Key[16]);
x[12] = Get32LE(&Key[20]);
x[13] = Get32LE(&Key[24]);
x[14] = Get32LE(&Key[28]);
x[15] = Get32LE((uint8_t*)&salsa20_constant[12]);
uint32_t j[16];
memcpy(j, x, sizeof(j));
salsa20_rounds(x);
for (int i = 0; i < 16; i++)
{
Set32LE(&Output[i * 4], x[i] + j[i]);
}
}
static void salsa20_xor(uint8_t* Output, const uint8_t* Input, size_t InputSize, const uint8_t Key[32], const uint8_t Nonce[8], uint64_t Counter)
{
uint8_t TempInput[16];
uint8_t Block[64];
memcpy(TempInput, Nonce, 8);
while (InputSize >= 64)
{
memcpy(TempInput + 8, &Counter, sizeof(Counter));
salsa20(Block, TempInput, Key);
for (size_t i = 0; i < 64; ++i)
{
Output[i] = Input[i] ^ Block[i];
}
Counter += 1;
Output += 64;
Input += 64;
InputSize -= 64;
}
if (InputSize)
{
memcpy(TempInput + 8, &Counter, sizeof(Counter));
salsa20(Block, TempInput, Key);
for (size_t i = 0; i < InputSize; ++i)
{
Output[i] = Input[i] ^ Block[i];
}
}
}
//
// nacl box seal/unseal
//
static void DerpNet__GetSharedKey(uint8_t SharedKey[32], const uint8_t PrivateKey[32], const uint8_t PublicKey[32])
{
uint8_t SharedSecret[32];
curve25519_scalarmult(SharedSecret, PrivateKey, PublicKey);
uint8_t ZeroInput[16] = { 0 };
hsalsa20(SharedKey, ZeroInput, SharedSecret);
}
static void DerpNet__BoxSealEx(uint8_t Nonce[24], uint8_t Auth[16], uint8_t* Output, const uint8_t* Input, size_t InputSize, const uint8_t SharedKey[32])
{
// xsalsa20 key construction
uint8_t SubKey[32];
hsalsa20(SubKey, Nonce, SharedKey);
uint8_t FirstBlock[64] = { 0 };
salsa20_xor(FirstBlock, FirstBlock, sizeof(FirstBlock), SubKey, Nonce + 16, 0);
size_t FirstSize = InputSize > 32 ? 32 : InputSize;
for (size_t i = 0; i < FirstSize; i++)
{
Output[i] = Input[i] ^ FirstBlock[32 + i];
}
salsa20_xor(Output + FirstSize, Input + FirstSize, InputSize - FirstSize, SubKey, Nonce + 16, 1);
poly1305_auth(Auth, Output, InputSize, FirstBlock);
}
static void DerpNet__BoxSeal(uint8_t Nonce[24], uint8_t Auth[16], uint8_t* Output, const uint8_t* Input, size_t InputSize, const uint8_t PrivateKey[32], const uint8_t PublicKey[32])
{
uint8_t SharedKey[32];
DerpNet__GetSharedKey(SharedKey, PrivateKey, PublicKey);
DerpNet__GetRandom(Nonce, 24);
DerpNet__BoxSealEx(Nonce, Auth, Output, Input, InputSize, SharedKey);
}
static bool DerpNet__BoxUnsealEx(uint8_t* Output, const uint8_t* Input, size_t InputSize, const uint8_t Auth[16], const uint8_t Nonce[24], const uint8_t SharedKey[32])
{
// xsalsa20 key construction
uint8_t SubKey[32];
hsalsa20(SubKey, Nonce, SharedKey);
uint8_t FirstBlock[64] = { 0 };
salsa20_xor(FirstBlock, FirstBlock, sizeof(FirstBlock), SubKey, Nonce + 16, 0);
uint8_t ExpectedAuth[16];
poly1305_auth(ExpectedAuth, Input, InputSize, FirstBlock);
if (poly1305_verify(Auth, ExpectedAuth) == 0)
{
return false;
}
size_t FirstSize = InputSize > 32 ? 32 : InputSize;
for (size_t i = 0; i < FirstSize; i++)
{
Output[i] = Input[i] ^ FirstBlock[32 + i];
}
salsa20_xor(Output + FirstSize, Input + FirstSize, InputSize - FirstSize, SubKey, Nonce + 16, 1);
return true;
}
static bool DerpNet__BoxUnseal(uint8_t* Output, const uint8_t* Input, size_t InputSize, const uint8_t Auth[16], const uint8_t Nonce[24], const uint8_t PrivateKey[32], const uint8_t PublicKey[32])
{
uint8_t SharedKey[32];
DerpNet__GetSharedKey(SharedKey, PrivateKey, PublicKey);
return DerpNet__BoxUnsealEx(Output, Input, InputSize, Auth, Nonce, SharedKey);
}
void DerpNet_CreateNewKey(DerpKey* UserSecret)
{
DerpNet__GetRandom(UserSecret->Bytes, sizeof(UserSecret->Bytes));
UserSecret->Bytes[ 0] &= 0xf8;
UserSecret->Bytes[31] &= 0x7f;
UserSecret->Bytes[31] |= 0x40;
}
void DerpNet_GetPublicKey(const DerpKey* UserSecret, DerpKey* UserPublic)
{
static const uint8_t Base[32] = { 9 };
curve25519_scalarmult(UserPublic->Bytes, UserSecret->Bytes, Base);
}
static bool DerpNet__TlsHandshake(DerpNet* Net, const char* Hostname, CredHandle* CredentialHandle, CtxtHandle* ContextHandle)
{
SCHANNEL_CRED Cred = { 0 };
Cred.dwVersion = SCHANNEL_CRED_VERSION;
Cred.dwFlags = SCH_USE_STRONG_CRYPTO | SCH_CRED_AUTO_CRED_VALIDATION | SCH_CRED_NO_DEFAULT_CREDS;
Cred.grbitEnabledProtocols = SP_PROT_TLS1_2;
SECURITY_STATUS SecStatus = AcquireCredentialsHandleA(NULL, UNISP_NAME_A, SECPKG_CRED_OUTBOUND, NULL, &Cred, NULL, NULL, CredentialHandle, NULL);
DERPNET_ASSERT(SecStatus == SEC_E_OK);
CtxtHandle* Context = NULL;
for (;;)
{
SecBuffer InBuffers[2] = { 0 };
InBuffers[0].BufferType = SECBUFFER_TOKEN;
InBuffers[0].pvBuffer = Net->Buffer;
InBuffers[0].cbBuffer = (unsigned)Net->BufferReceived;
InBuffers[1].BufferType = SECBUFFER_EMPTY;