-
Notifications
You must be signed in to change notification settings - Fork 43
/
crypto.c
2654 lines (2427 loc) · 89.9 KB
/
crypto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2010-2015 Rudolf Polzer (divVerent)
Copyright (C) 2010-2020 Ashley Rose Hale (LadyHavoc)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "quakedef.h"
#include "crypto.h"
#include "common.h"
#include "thread.h"
#include "hmac.h"
#include "libcurl.h"
cvar_t crypto_developer = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "crypto_developer", "0", "print extra info about crypto handshake"};
cvar_t crypto_aeslevel = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "crypto_aeslevel", "1", "whether to support AES encryption in authenticated connections (0 = no, 1 = supported, 2 = requested, 3 = required)"};
cvar_t crypto_servercpupercent = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "crypto_servercpupercent", "10", "allowed crypto CPU load in percent for server operation (0 = no limit, faster)"};
cvar_t crypto_servercpumaxtime = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "crypto_servercpumaxtime", "0.01", "maximum allowed crypto CPU time per frame (0 = no limit)"};
cvar_t crypto_servercpudebug = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "crypto_servercpudebug", "0", "print statistics about time usage by crypto"};
static double crypto_servercpu_accumulator = 0;
static double crypto_servercpu_lastrealtime = 0;
extern cvar_t net_sourceaddresscheck;
int crypto_keyfp_recommended_length;
static const char *crypto_idstring = NULL;
static char crypto_idstring_buf[512];
#define PROTOCOL_D0_BLIND_ID FOURCC_D0PK
#define PROTOCOL_VLEN (('v' << 0) | ('l' << 8) | ('e' << 16) | ('n' << 24))
// BEGIN stuff shared with crypto-keygen-standalone
#define FOURCC_D0PK (('d' << 0) | ('0' << 8) | ('p' << 16) | ('k' << 24))
#define FOURCC_D0SK (('d' << 0) | ('0' << 8) | ('s' << 16) | ('k' << 24))
#define FOURCC_D0PI (('d' << 0) | ('0' << 8) | ('p' << 16) | ('i' << 24))
#define FOURCC_D0SI (('d' << 0) | ('0' << 8) | ('s' << 16) | ('i' << 24))
#define FOURCC_D0IQ (('d' << 0) | ('0' << 8) | ('i' << 16) | ('q' << 24))
#define FOURCC_D0IR (('d' << 0) | ('0' << 8) | ('i' << 16) | ('r' << 24))
#define FOURCC_D0ER (('d' << 0) | ('0' << 8) | ('e' << 16) | ('r' << 24))
#define FOURCC_D0IC (('d' << 0) | ('0' << 8) | ('i' << 16) | ('c' << 24))
static unsigned long Crypto_LittleLong(const char *data)
{
return
((unsigned char) data[0]) |
(((unsigned char) data[1]) << 8) |
(((unsigned char) data[2]) << 16) |
(((unsigned char) data[3]) << 24);
}
static void Crypto_UnLittleLong(char *data, unsigned long l)
{
data[0] = l & 0xFF;
data[1] = (l >> 8) & 0xFF;
data[2] = (l >> 16) & 0xFF;
data[3] = (l >> 24) & 0xFF;
}
static size_t Crypto_ParsePack(const char *buf, size_t len, unsigned long header, const char **lumps, size_t *lumpsize, size_t nlumps)
{
size_t i;
size_t pos;
pos = 0;
if(header)
{
if(len < 4)
return 0;
if(Crypto_LittleLong(buf) != header)
return 0;
pos += 4;
}
for(i = 0; i < nlumps; ++i)
{
if(pos + 4 > len)
return 0;
lumpsize[i] = Crypto_LittleLong(&buf[pos]);
pos += 4;
if(pos + lumpsize[i] > len)
return 0;
lumps[i] = &buf[pos];
pos += lumpsize[i];
}
return pos;
}
static size_t Crypto_UnParsePack(char *buf, size_t len, unsigned long header, const char *const *lumps, const size_t *lumpsize, size_t nlumps)
{
size_t i;
size_t pos;
pos = 0;
if(header)
{
if(len < 4)
return 0;
Crypto_UnLittleLong(buf, header);
pos += 4;
}
for(i = 0; i < nlumps; ++i)
{
if(pos + 4 + lumpsize[i] > len)
return 0;
Crypto_UnLittleLong(&buf[pos], (unsigned long)lumpsize[i]);
pos += 4;
memcpy(&buf[pos], lumps[i], lumpsize[i]);
pos += lumpsize[i];
}
return pos;
}
// END stuff shared with xonotic-keygen
#define USE_AES
#ifdef LINK_TO_CRYPTO
#include <d0_blind_id/d0_blind_id.h>
#define d0_blind_id_dll 1
#define Crypto_OpenLibrary() true
#define Crypto_CloseLibrary()
#define qd0_blind_id_new d0_blind_id_new
#define qd0_blind_id_free d0_blind_id_free
//#define qd0_blind_id_clear d0_blind_id_clear
#define qd0_blind_id_copy d0_blind_id_copy
//#define qd0_blind_id_generate_private_key d0_blind_id_generate_private_key
//#define qd0_blind_id_generate_private_key_fastreject d0_blind_id_generate_private_key_fastreject
//#define qd0_blind_id_read_private_key d0_blind_id_read_private_key
#define qd0_blind_id_read_public_key d0_blind_id_read_public_key
//#define qd0_blind_id_write_private_key d0_blind_id_write_private_key
//#define qd0_blind_id_write_public_key d0_blind_id_write_public_key
#define qd0_blind_id_fingerprint64_public_key d0_blind_id_fingerprint64_public_key
//#define qd0_blind_id_generate_private_id_modulus d0_blind_id_generate_private_id_modulus
#define qd0_blind_id_read_private_id_modulus d0_blind_id_read_private_id_modulus
//#define qd0_blind_id_write_private_id_modulus d0_blind_id_write_private_id_modulus
#define qd0_blind_id_generate_private_id_start d0_blind_id_generate_private_id_start
#define qd0_blind_id_generate_private_id_request d0_blind_id_generate_private_id_request
//#define qd0_blind_id_answer_private_id_request d0_blind_id_answer_private_id_request
#define qd0_blind_id_finish_private_id_request d0_blind_id_finish_private_id_request
//#define qd0_blind_id_read_private_id_request_camouflage d0_blind_id_read_private_id_request_camouflage
//#define qd0_blind_id_write_private_id_request_camouflage d0_blind_id_write_private_id_request_camouflage
#define qd0_blind_id_read_private_id d0_blind_id_read_private_id
//#define qd0_blind_id_read_public_id d0_blind_id_read_public_id
#define qd0_blind_id_write_private_id d0_blind_id_write_private_id
//#define qd0_blind_id_write_public_id d0_blind_id_write_public_id
#define qd0_blind_id_authenticate_with_private_id_start d0_blind_id_authenticate_with_private_id_start
#define qd0_blind_id_authenticate_with_private_id_challenge d0_blind_id_authenticate_with_private_id_challenge
#define qd0_blind_id_authenticate_with_private_id_response d0_blind_id_authenticate_with_private_id_response
#define qd0_blind_id_authenticate_with_private_id_verify d0_blind_id_authenticate_with_private_id_verify
#define qd0_blind_id_fingerprint64_public_id d0_blind_id_fingerprint64_public_id
#define qd0_blind_id_sessionkey_public_id d0_blind_id_sessionkey_public_id
#define qd0_blind_id_INITIALIZE d0_blind_id_INITIALIZE
#define qd0_blind_id_SHUTDOWN d0_blind_id_SHUTDOWN
#define qd0_blind_id_util_sha256 d0_blind_id_util_sha256
#define qd0_blind_id_sign_with_private_id_sign d0_blind_id_sign_with_private_id_sign
#define qd0_blind_id_sign_with_private_id_sign_detached d0_blind_id_sign_with_private_id_sign_detached
#define qd0_blind_id_setmallocfuncs d0_blind_id_setmallocfuncs
#define qd0_blind_id_setmutexfuncs d0_blind_id_setmutexfuncs
#define qd0_blind_id_verify_public_id d0_blind_id_verify_public_id
#define qd0_blind_id_verify_private_id d0_blind_id_verify_private_id
#else
// d0_blind_id interface
#define D0_EXPORT
#if defined (__GNUC__) || (__clang__) || (__TINYC__)
#define D0_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
#define D0_WARN_UNUSED_RESULT
#endif
#define D0_BOOL int
typedef void *(d0_malloc_t)(size_t len);
typedef void (d0_free_t)(void *p);
typedef void *(d0_createmutex_t)(void);
typedef void (d0_destroymutex_t)(void *);
typedef int (d0_lockmutex_t)(void *); // zero on success
typedef int (d0_unlockmutex_t)(void *); // zero on success
typedef struct d0_blind_id_s d0_blind_id_t;
typedef D0_BOOL (*d0_fastreject_function) (const d0_blind_id_t *ctx, void *pass);
static D0_EXPORT D0_WARN_UNUSED_RESULT d0_blind_id_t *(*qd0_blind_id_new) (void);
static D0_EXPORT void (*qd0_blind_id_free) (d0_blind_id_t *a);
//static D0_EXPORT void (*qd0_blind_id_clear) (d0_blind_id_t *ctx);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_copy) (d0_blind_id_t *ctx, const d0_blind_id_t *src);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_key) (d0_blind_id_t *ctx, int k);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_key_fastreject) (d0_blind_id_t *ctx, int k, d0_fastreject_function reject, void *pass);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_key) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_public_key) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_key) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_public_key) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_fingerprint64_public_key) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_id_modulus) (d0_blind_id_t *ctx);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_id_modulus) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_id_modulus) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_id_start) (d0_blind_id_t *ctx);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_generate_private_id_request) (d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_answer_private_id_request) (const d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_finish_private_id_request) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_id_request_camouflage) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_id_request_camouflage) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_private_id) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_read_public_id) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_private_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
//static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_write_public_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_start) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_challenge) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL recv_modulus, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen, D0_BOOL *status);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_response) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_authenticate_with_private_id_verify) (d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, size_t *msglen, D0_BOOL *status);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_fingerprint64_public_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_sessionkey_public_id) (const d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); // can only be done after successful key exchange, this performs a modpow; key length is limited by SHA_DIGESTSIZE for now; also ONLY valid after successful d0_blind_id_authenticate_with_private_id_verify/d0_blind_id_fingerprint64_public_id
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_INITIALIZE) (void);
static D0_EXPORT void (*qd0_blind_id_SHUTDOWN) (void);
static D0_EXPORT void (*qd0_blind_id_util_sha256) (char *out, const char *in, size_t n);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_sign_with_private_id_sign) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_sign_with_private_id_sign_detached) (d0_blind_id_t *ctx, D0_BOOL is_first, D0_BOOL send_modulus, const char *message, size_t msglen, char *outbuf, size_t *outbuflen);
static D0_EXPORT void (*qd0_blind_id_setmallocfuncs)(d0_malloc_t *m, d0_free_t *f);
static D0_EXPORT void (*qd0_blind_id_setmutexfuncs)(d0_createmutex_t *c, d0_destroymutex_t *d, d0_lockmutex_t *l, d0_unlockmutex_t *u);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_verify_public_id)(const d0_blind_id_t *ctx, D0_BOOL *status);
static D0_EXPORT D0_WARN_UNUSED_RESULT D0_BOOL (*qd0_blind_id_verify_private_id)(const d0_blind_id_t *ctx);
static dllfunction_t d0_blind_id_funcs[] =
{
{"d0_blind_id_new", (void **) &qd0_blind_id_new},
{"d0_blind_id_free", (void **) &qd0_blind_id_free},
//{"d0_blind_id_clear", (void **) &qd0_blind_id_clear},
{"d0_blind_id_copy", (void **) &qd0_blind_id_copy},
//{"d0_blind_id_generate_private_key", (void **) &qd0_blind_id_generate_private_key},
//{"d0_blind_id_generate_private_key_fastreject", (void **) &qd0_blind_id_generate_private_key_fastreject},
//{"d0_blind_id_read_private_key", (void **) &qd0_blind_id_read_private_key},
{"d0_blind_id_read_public_key", (void **) &qd0_blind_id_read_public_key},
//{"d0_blind_id_write_private_key", (void **) &qd0_blind_id_write_private_key},
//{"d0_blind_id_write_public_key", (void **) &qd0_blind_id_write_public_key},
{"d0_blind_id_fingerprint64_public_key", (void **) &qd0_blind_id_fingerprint64_public_key},
//{"d0_blind_id_generate_private_id_modulus", (void **) &qd0_blind_id_generate_private_id_modulus},
{"d0_blind_id_read_private_id_modulus", (void **) &qd0_blind_id_read_private_id_modulus},
//{"d0_blind_id_write_private_id_modulus", (void **) &qd0_blind_id_write_private_id_modulus},
{"d0_blind_id_generate_private_id_start", (void **) &qd0_blind_id_generate_private_id_start},
{"d0_blind_id_generate_private_id_request", (void **) &qd0_blind_id_generate_private_id_request},
//{"d0_blind_id_answer_private_id_request", (void **) &qd0_blind_id_answer_private_id_request},
{"d0_blind_id_finish_private_id_request", (void **) &qd0_blind_id_finish_private_id_request},
//{"d0_blind_id_read_private_id_request_camouflage", (void **) &qd0_blind_id_read_private_id_request_camouflage},
//{"d0_blind_id_write_private_id_request_camouflage", (void **) &qd0_blind_id_write_private_id_request_camouflage},
{"d0_blind_id_read_private_id", (void **) &qd0_blind_id_read_private_id},
//{"d0_blind_id_read_public_id", (void **) &qd0_blind_id_read_public_id},
{"d0_blind_id_write_private_id", (void **) &qd0_blind_id_write_private_id},
//{"d0_blind_id_write_public_id", (void **) &qd0_blind_id_write_public_id},
{"d0_blind_id_authenticate_with_private_id_start", (void **) &qd0_blind_id_authenticate_with_private_id_start},
{"d0_blind_id_authenticate_with_private_id_challenge", (void **) &qd0_blind_id_authenticate_with_private_id_challenge},
{"d0_blind_id_authenticate_with_private_id_response", (void **) &qd0_blind_id_authenticate_with_private_id_response},
{"d0_blind_id_authenticate_with_private_id_verify", (void **) &qd0_blind_id_authenticate_with_private_id_verify},
{"d0_blind_id_fingerprint64_public_id", (void **) &qd0_blind_id_fingerprint64_public_id},
{"d0_blind_id_sessionkey_public_id", (void **) &qd0_blind_id_sessionkey_public_id},
{"d0_blind_id_INITIALIZE", (void **) &qd0_blind_id_INITIALIZE},
{"d0_blind_id_SHUTDOWN", (void **) &qd0_blind_id_SHUTDOWN},
{"d0_blind_id_util_sha256", (void **) &qd0_blind_id_util_sha256},
{"d0_blind_id_sign_with_private_id_sign", (void **) &qd0_blind_id_sign_with_private_id_sign},
{"d0_blind_id_sign_with_private_id_sign_detached", (void **) &qd0_blind_id_sign_with_private_id_sign_detached},
{"d0_blind_id_setmallocfuncs", (void **) &qd0_blind_id_setmallocfuncs},
{"d0_blind_id_setmutexfuncs", (void **) &qd0_blind_id_setmutexfuncs},
{"d0_blind_id_verify_public_id", (void **) &qd0_blind_id_verify_public_id},
{"d0_blind_id_verify_private_id", (void **) &qd0_blind_id_verify_private_id},
{NULL, NULL}
};
// end of d0_blind_id interface
static dllhandle_t d0_blind_id_dll = NULL;
static qbool Crypto_OpenLibrary (void)
{
const char* dllnames [] =
{
#if defined(WIN32)
"libd0_blind_id-0.dll",
#elif defined(MACOSX)
"libd0_blind_id.0.dylib",
#else
"libd0_blind_id.so.0",
"libd0_blind_id.so", // FreeBSD
#endif
NULL
};
// Already loaded?
if (d0_blind_id_dll)
return true;
// Load the DLL
return Sys_LoadDependency (dllnames, &d0_blind_id_dll, d0_blind_id_funcs);
}
static void Crypto_CloseLibrary (void)
{
Sys_FreeLibrary (&d0_blind_id_dll);
}
#endif
#ifdef LINK_TO_CRYPTO_RIJNDAEL
#include <d0_blind_id/d0_rijndael.h>
#define d0_rijndael_dll 1
#define Crypto_Rijndael_OpenLibrary() true
#define Crypto_Rijndael_CloseLibrary()
#define qd0_rijndael_setup_encrypt d0_rijndael_setup_encrypt
#define qd0_rijndael_setup_decrypt d0_rijndael_setup_decrypt
#define qd0_rijndael_encrypt d0_rijndael_encrypt
#define qd0_rijndael_decrypt d0_rijndael_decrypt
#else
// no need to do the #define dance here, as the upper part declares out macros either way
D0_EXPORT int (*qd0_rijndael_setup_encrypt) (unsigned long *rk, const unsigned char *key,
int keybits);
D0_EXPORT int (*qd0_rijndael_setup_decrypt) (unsigned long *rk, const unsigned char *key,
int keybits);
D0_EXPORT void (*qd0_rijndael_encrypt) (const unsigned long *rk, int nrounds,
const unsigned char plaintext[16], unsigned char ciphertext[16]);
D0_EXPORT void (*qd0_rijndael_decrypt) (const unsigned long *rk, int nrounds,
const unsigned char ciphertext[16], unsigned char plaintext[16]);
#define D0_RIJNDAEL_KEYLENGTH(keybits) ((keybits)/8)
#define D0_RIJNDAEL_RKLENGTH(keybits) ((keybits)/8+28)
#define D0_RIJNDAEL_NROUNDS(keybits) ((keybits)/32+6)
static dllfunction_t d0_rijndael_funcs[] =
{
{"d0_rijndael_setup_decrypt", (void **) &qd0_rijndael_setup_decrypt},
{"d0_rijndael_setup_encrypt", (void **) &qd0_rijndael_setup_encrypt},
{"d0_rijndael_decrypt", (void **) &qd0_rijndael_decrypt},
{"d0_rijndael_encrypt", (void **) &qd0_rijndael_encrypt},
{NULL, NULL}
};
// end of d0_blind_id interface
static dllhandle_t d0_rijndael_dll = NULL;
static qbool Crypto_Rijndael_OpenLibrary (void)
{
const char* dllnames [] =
{
#if defined(WIN32)
"libd0_rijndael-0.dll",
#elif defined(MACOSX)
"libd0_rijndael.0.dylib",
#else
"libd0_rijndael.so.0",
"libd0_rijndael.so", // FreeBSD
#endif
NULL
};
// Already loaded?
if (d0_rijndael_dll)
return true;
// Load the DLL
return Sys_LoadDependency (dllnames, &d0_rijndael_dll, d0_rijndael_funcs);
}
static void Crypto_Rijndael_CloseLibrary (void)
{
Sys_FreeLibrary (&d0_rijndael_dll);
}
#endif
// various helpers
void sha256(unsigned char *out, const unsigned char *in, int n)
{
qd0_blind_id_util_sha256((char *) out, (const char *) in, n);
}
static size_t Crypto_LoadFile(const char *path, char *buf, size_t nmax, qbool inuserdir)
{
char vabuf[1024];
qfile_t *f = NULL;
fs_offset_t n;
if(inuserdir)
f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%s%s", *fs_userdir ? fs_userdir : fs_basedir, path), "rb", false);
else
f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%s%s", fs_basedir, path), "rb", false);
if(!f)
return 0;
n = FS_Read(f, buf, nmax);
if(n < 0)
n = 0;
FS_Close(f);
return (size_t) n;
}
static qbool PutWithNul(char **data, size_t *len, const char *str)
{
// invariant: data points to insertion point
size_t l = strlen(str);
if(l >= *len)
return false;
memcpy(*data, str, l+1);
*data += l+1;
*len -= l+1;
return true;
}
static const char *GetUntilNul(const char **data, size_t *len)
{
// invariant: data points to next character to take
const char *data_save = *data;
size_t n;
const char *p;
if(!*data)
return NULL;
if(!*len)
{
*data = NULL;
return NULL;
}
p = (const char *) memchr(*data, 0, *len);
if(!p) // no terminating NUL
{
*data = NULL;
*len = 0;
return NULL;
}
n = (p - *data) + 1;
*len -= n;
*data += n;
if(*len == 0)
*data = NULL;
return (const char *) data_save;
}
// d0pk reading
static d0_blind_id_t *Crypto_ReadPublicKey(char *buf, size_t len)
{
d0_blind_id_t *pk = NULL;
const char *p[2];
size_t l[2];
if(Crypto_ParsePack(buf, len, FOURCC_D0PK, p, l, 2))
{
pk = qd0_blind_id_new();
if(pk)
if(qd0_blind_id_read_public_key(pk, p[0], l[0]))
if(qd0_blind_id_read_private_id_modulus(pk, p[1], l[1]))
return pk;
}
if(pk)
qd0_blind_id_free(pk);
return NULL;
}
// d0si reading
static qbool Crypto_AddPrivateKey(d0_blind_id_t *pk, char *buf, size_t len)
{
const char *p[1];
size_t l[1];
if(Crypto_ParsePack(buf, len, FOURCC_D0SI, p, l, 1))
{
if(qd0_blind_id_read_private_id(pk, p[0], l[0]))
return true;
}
return false;
}
#define MAX_PUBKEYS 16
static d0_blind_id_t *pubkeys[MAX_PUBKEYS];
static char pubkeys_fp64[MAX_PUBKEYS][FP64_SIZE+1];
static qbool pubkeys_havepriv[MAX_PUBKEYS];
static qbool pubkeys_havesig[MAX_PUBKEYS];
static char pubkeys_priv_fp64[MAX_PUBKEYS][FP64_SIZE+1];
static char challenge_append[1400];
static size_t challenge_append_length;
static int keygen_i = -1;
static char keygen_buf[8192];
#define MAX_CRYPTOCONNECTS 16
#define CRYPTOCONNECT_NONE 0
#define CRYPTOCONNECT_PRECONNECT 1
#define CRYPTOCONNECT_CONNECT 2
#define CRYPTOCONNECT_RECONNECT 3
#define CRYPTOCONNECT_DUPLICATE 4
typedef struct server_cryptoconnect_s
{
double lasttime;
lhnetaddress_t address;
crypto_t crypto;
int next_step;
}
server_cryptoconnect_t;
static server_cryptoconnect_t cryptoconnects[MAX_CRYPTOCONNECTS];
static int cdata_id = 0;
typedef struct
{
d0_blind_id_t *id;
int s, c;
int next_step;
char challenge[2048];
char wantserver_idfp[FP64_SIZE+1];
qbool wantserver_aes;
qbool wantserver_issigned;
int cdata_id;
}
crypto_data_t;
// crypto specific helpers
#define CDATA ((crypto_data_t *) crypto->data)
#define MAKE_CDATA if(!crypto->data) crypto->data = Z_Malloc(sizeof(crypto_data_t))
#define CLEAR_CDATA if(crypto->data) { if(CDATA->id) qd0_blind_id_free(CDATA->id); Z_Free(crypto->data); } crypto->data = NULL
static crypto_t *Crypto_ServerFindInstance(lhnetaddress_t *peeraddress, qbool allow_create)
{
crypto_t *crypto;
int i, best;
if(!d0_blind_id_dll)
return NULL; // no support
for(i = 0; i < MAX_CRYPTOCONNECTS; ++i)
if(LHNETADDRESS_Compare(peeraddress, &cryptoconnects[i].address))
break;
if(i < MAX_CRYPTOCONNECTS && (allow_create || cryptoconnects[i].crypto.data))
{
crypto = &cryptoconnects[i].crypto;
cryptoconnects[i].lasttime = host.realtime;
return crypto;
}
if(!allow_create)
return NULL;
best = 0;
for(i = 1; i < MAX_CRYPTOCONNECTS; ++i)
if(cryptoconnects[i].lasttime < cryptoconnects[best].lasttime)
best = i;
crypto = &cryptoconnects[best].crypto;
cryptoconnects[best].lasttime = host.realtime;
memcpy(&cryptoconnects[best].address, peeraddress, sizeof(cryptoconnects[best].address));
CLEAR_CDATA;
return crypto;
}
qbool Crypto_FinishInstance(crypto_t *out, crypto_t *crypto)
{
// no check needed here (returned pointers are only used in prefilled fields)
if(!crypto || !crypto->authenticated)
{
Con_Printf("Passed an invalid crypto connect instance\n");
memset(out, 0, sizeof(*out));
return false;
}
CLEAR_CDATA;
memcpy(out, crypto, sizeof(*out));
memset(crypto, 0, sizeof(*crypto));
return true;
}
crypto_t *Crypto_ServerGetInstance(lhnetaddress_t *peeraddress)
{
// no check needed here (returned pointers are only used in prefilled fields)
return Crypto_ServerFindInstance(peeraddress, false);
}
typedef struct crypto_storedhostkey_s
{
struct crypto_storedhostkey_s *next;
lhnetaddress_t addr;
int keyid;
char idfp[FP64_SIZE+1];
int aeslevel;
qbool issigned;
}
crypto_storedhostkey_t;
static crypto_storedhostkey_t *crypto_storedhostkey_hashtable[CRYPTO_HOSTKEY_HASHSIZE];
static void Crypto_InitHostKeys(void)
{
int i;
for(i = 0; i < CRYPTO_HOSTKEY_HASHSIZE; ++i)
crypto_storedhostkey_hashtable[i] = NULL;
}
static void Crypto_ClearHostKeys(void)
{
int i;
crypto_storedhostkey_t *hk, *hkn;
for(i = 0; i < CRYPTO_HOSTKEY_HASHSIZE; ++i)
{
for(hk = crypto_storedhostkey_hashtable[i]; hk; hk = hkn)
{
hkn = hk->next;
Z_Free(hk);
}
crypto_storedhostkey_hashtable[i] = NULL;
}
}
static qbool Crypto_ClearHostKey(lhnetaddress_t *peeraddress)
{
char buf[128];
int hashindex;
crypto_storedhostkey_t **hkp;
qbool found = false;
LHNETADDRESS_ToString(peeraddress, buf, sizeof(buf), 1);
hashindex = CRC_Block((const unsigned char *) buf, strlen(buf)) % CRYPTO_HOSTKEY_HASHSIZE;
for(hkp = &crypto_storedhostkey_hashtable[hashindex]; *hkp && LHNETADDRESS_Compare(&((*hkp)->addr), peeraddress); hkp = &((*hkp)->next));
if(*hkp)
{
crypto_storedhostkey_t *hk = *hkp;
*hkp = hk->next;
Z_Free(hk);
found = true;
}
return found;
}
static void Crypto_StoreHostKey(lhnetaddress_t *peeraddress, const char *keystring, qbool complain)
{
char buf[128];
int hashindex;
crypto_storedhostkey_t *hk;
int keyid;
char idfp[FP64_SIZE+1];
int aeslevel;
qbool issigned;
if(!d0_blind_id_dll)
return;
// syntax of keystring:
// aeslevel id@key id@key ...
if(!*keystring)
return;
aeslevel = bound(0, *keystring - '0', 3);
while(*keystring && *keystring != ' ')
++keystring;
keyid = -1;
issigned = false;
while(*keystring && keyid < 0)
{
// id@key
const char *idstart, *idend, *keystart, *keyend;
qbool thisissigned = true;
++keystring; // skip the space
idstart = keystring;
while(*keystring && *keystring != ' ' && *keystring != '@')
++keystring;
idend = keystring;
if(!*keystring)
break;
++keystring;
keystart = keystring;
while(*keystring && *keystring != ' ')
++keystring;
keyend = keystring;
if (keystart[0] == '~')
{
thisissigned = false;
++keystart;
}
if(idend - idstart == FP64_SIZE && keyend - keystart == FP64_SIZE)
{
int thiskeyid;
for(thiskeyid = MAX_PUBKEYS - 1; thiskeyid >= 0; --thiskeyid)
if(pubkeys[thiskeyid])
if(!memcmp(pubkeys_fp64[thiskeyid], keystart, FP64_SIZE))
{
memcpy(idfp, idstart, FP64_SIZE);
idfp[FP64_SIZE] = 0;
keyid = thiskeyid;
issigned = thisissigned;
break;
}
// If this failed, keyid will be -1.
}
}
if(keyid < 0)
return;
LHNETADDRESS_ToString(peeraddress, buf, sizeof(buf), 1);
hashindex = CRC_Block((const unsigned char *) buf, strlen(buf)) % CRYPTO_HOSTKEY_HASHSIZE;
for(hk = crypto_storedhostkey_hashtable[hashindex]; hk && LHNETADDRESS_Compare(&hk->addr, peeraddress); hk = hk->next);
if(hk)
{
if(complain)
{
if(hk->keyid != keyid || memcmp(hk->idfp, idfp, FP64_SIZE+1))
Con_Printf("Server %s tried to change the host key to a value not in the host cache. Connecting to it will fail. To accept the new host key, do crypto_hostkey_clear %s\n", buf, buf);
if(hk->aeslevel > aeslevel)
Con_Printf("Server %s tried to reduce encryption status, not accepted. Connecting to it will fail. To accept, do crypto_hostkey_clear %s\n", buf, buf);
if(hk->issigned > issigned)
Con_Printf("Server %s tried to reduce signature status, not accepted. Connecting to it will fail. To accept, do crypto_hostkey_clear %s\n", buf, buf);
}
hk->aeslevel = max(aeslevel, hk->aeslevel);
hk->issigned = issigned;
return;
}
// great, we did NOT have it yet
hk = (crypto_storedhostkey_t *) Z_Malloc(sizeof(*hk));
memcpy(&hk->addr, peeraddress, sizeof(hk->addr));
hk->keyid = keyid;
memcpy(hk->idfp, idfp, FP64_SIZE+1);
hk->next = crypto_storedhostkey_hashtable[hashindex];
hk->aeslevel = aeslevel;
hk->issigned = issigned;
crypto_storedhostkey_hashtable[hashindex] = hk;
}
qbool Crypto_RetrieveHostKey(lhnetaddress_t *peeraddress, int *keyid, char *keyfp, size_t keyfplen, char *idfp, size_t idfplen, int *aeslevel, qbool *issigned)
{
char buf[128];
int hashindex;
crypto_storedhostkey_t *hk;
if(!d0_blind_id_dll)
return false;
LHNETADDRESS_ToString(peeraddress, buf, sizeof(buf), 1);
hashindex = CRC_Block((const unsigned char *) buf, strlen(buf)) % CRYPTO_HOSTKEY_HASHSIZE;
for(hk = crypto_storedhostkey_hashtable[hashindex]; hk && LHNETADDRESS_Compare(&hk->addr, peeraddress); hk = hk->next);
if(!hk)
return false;
if(keyid)
*keyid = hk->keyid;
if(keyfp)
dp_strlcpy(keyfp, pubkeys_fp64[hk->keyid], keyfplen);
if(idfp)
dp_strlcpy(idfp, hk->idfp, idfplen);
if(aeslevel)
*aeslevel = hk->aeslevel;
if(issigned)
*issigned = hk->issigned;
return true;
}
int Crypto_RetrieveLocalKey(int keyid, char *keyfp, size_t keyfplen, char *idfp, size_t idfplen, qbool *issigned) // return value: -1 if more to come, +1 if valid, 0 if end of list
{
if(keyid < 0 || keyid >= MAX_PUBKEYS)
return 0;
if(keyfp)
*keyfp = 0;
if(idfp)
*idfp = 0;
if(!pubkeys[keyid])
return -1;
if(keyfp)
dp_strlcpy(keyfp, pubkeys_fp64[keyid], keyfplen);
if(idfp)
if(pubkeys_havepriv[keyid])
dp_strlcpy(idfp, pubkeys_priv_fp64[keyid], idfplen);
if(issigned)
*issigned = pubkeys_havesig[keyid];
return 1;
}
// end
// init/shutdown code
static void Crypto_BuildChallengeAppend(void)
{
char *p, *lengthptr, *startptr;
size_t n;
int i;
p = challenge_append;
n = sizeof(challenge_append);
Crypto_UnLittleLong(p, PROTOCOL_VLEN);
p += 4;
n -= 4;
lengthptr = p;
Crypto_UnLittleLong(p, 0);
p += 4;
n -= 4;
Crypto_UnLittleLong(p, PROTOCOL_D0_BLIND_ID);
p += 4;
n -= 4;
startptr = p;
for(i = 0; i < MAX_PUBKEYS; ++i)
if(pubkeys_havepriv[i])
PutWithNul(&p, &n, pubkeys_fp64[i]);
PutWithNul(&p, &n, "");
for(i = 0; i < MAX_PUBKEYS; ++i)
if(!pubkeys_havepriv[i] && pubkeys[i])
PutWithNul(&p, &n, pubkeys_fp64[i]);
Crypto_UnLittleLong(lengthptr, p - startptr);
challenge_append_length = p - challenge_append;
}
static qbool Crypto_SavePubKeyTextFile(int i)
{
qfile_t *f;
char vabuf[1024];
if(!pubkeys_havepriv[i])
return false;
f = FS_SysOpen(va(vabuf, sizeof(vabuf), "%skey_%d-public-fp%s.txt", *fs_userdir ? fs_userdir : fs_basedir, i, sessionid.string), "w", false);
if(!f)
return false;
// we ignore errors for this file, as it's not necessary to have
FS_Printf(f, "ID-Fingerprint: %s\n", pubkeys_priv_fp64[i]);
FS_Printf(f, "ID-Is-Signed: %s\n", pubkeys_havesig[i] ? "yes" : "no");
FS_Printf(f, "ID-Is-For-Key: %s\n", pubkeys_fp64[i]);
FS_Printf(f, "\n");
FS_Printf(f, "This is a PUBLIC ID file for DarkPlaces.\n");
FS_Printf(f, "You are free to share this file or its contents.\n");
FS_Printf(f, "\n");
FS_Printf(f, "This file will be automatically generated again if deleted.\n");
FS_Printf(f, "\n");
FS_Printf(f, "However, NEVER share the accompanying SECRET ID file called\n");
FS_Printf(f, "key_%d.d0si%s, as doing so would compromise security!\n", i, sessionid.string);
FS_Close(f);
return true;
}
static void Crypto_BuildIdString(void)
{
int i;
char vabuf[1024];
crypto_idstring = NULL;
dpsnprintf(crypto_idstring_buf, sizeof(crypto_idstring_buf), "%d", d0_rijndael_dll ? crypto_aeslevel.integer : 0);
for (i = 0; i < MAX_PUBKEYS; ++i)
if (pubkeys[i])
dp_strlcat(crypto_idstring_buf, va(vabuf, sizeof(vabuf), " %s@%s%s", pubkeys_priv_fp64[i], pubkeys_havesig[i] ? "" : "~", pubkeys_fp64[i]), sizeof(crypto_idstring_buf));
crypto_idstring = crypto_idstring_buf;
}
void Crypto_LoadKeys(void)
{
char buf[8192];
size_t len, len2;
int i;
char vabuf[1024];
if(!d0_blind_id_dll) // don't if we can't
return;
if(crypto_idstring) // already loaded? then not
return;
Host_LockSession(); // we use the session ID here
// load keys
// note: we are just a CLIENT
// so we load:
// PUBLIC KEYS to accept (including modulus)
// PRIVATE KEY of user
for(i = 0; i < MAX_PUBKEYS; ++i)
{
memset(pubkeys_fp64[i], 0, sizeof(pubkeys_fp64[i]));
memset(pubkeys_priv_fp64[i], 0, sizeof(pubkeys_fp64[i]));
pubkeys_havepriv[i] = false;
pubkeys_havesig[i] = false;
len = Crypto_LoadFile(va(vabuf, sizeof(vabuf), "key_%d.d0pk", i), buf, sizeof(buf), false);
if((pubkeys[i] = Crypto_ReadPublicKey(buf, len)))
{
len2 = FP64_SIZE;
if(qd0_blind_id_fingerprint64_public_key(pubkeys[i], pubkeys_fp64[i], &len2)) // keeps final NUL
{
Con_Printf("Loaded public key key_%d.d0pk (fingerprint: %s)\n", i, pubkeys_fp64[i]);
len = Crypto_LoadFile(va(vabuf, sizeof(vabuf), "key_%d.d0si%s", i, sessionid.string), buf, sizeof(buf), true);
if(len)
{
if(Crypto_AddPrivateKey(pubkeys[i], buf, len))
{
len2 = FP64_SIZE;
if(qd0_blind_id_fingerprint64_public_id(pubkeys[i], pubkeys_priv_fp64[i], &len2)) // keeps final NUL
{
D0_BOOL status = 0;
Con_Printf("Loaded private ID key_%d.d0si%s for key_%d.d0pk (public key fingerprint: %s)\n", i, sessionid.string, i, pubkeys_priv_fp64[i]);
// verify the key we just loaded (just in case)
if(qd0_blind_id_verify_private_id(pubkeys[i]) && qd0_blind_id_verify_public_id(pubkeys[i], &status))
{
pubkeys_havepriv[i] = true;
pubkeys_havesig[i] = status;
// verify the key we just got (just in case)
if(!status)
Con_Printf("NOTE: this ID has not yet been signed!\n");
Crypto_SavePubKeyTextFile(i);
}
else
{
Con_Printf("d0_blind_id_verify_private_id failed, this is not a valid key!\n");
qd0_blind_id_free(pubkeys[i]);
pubkeys[i] = NULL;
}
}
else
{
Con_Printf("d0_blind_id_fingerprint64_public_id failed\n");
qd0_blind_id_free(pubkeys[i]);
pubkeys[i] = NULL;
}
}
}
}
else
{
// can't really happen
qd0_blind_id_free(pubkeys[i]);
pubkeys[i] = NULL;
}
}
}
keygen_i = -1;
Crypto_BuildIdString();
Crypto_BuildChallengeAppend();
// find a good prefix length for all the keys we know (yes, algorithm is not perfect yet, may yield too long prefix length)
crypto_keyfp_recommended_length = 0;
memset(buf+256, 0, MAX_PUBKEYS + MAX_PUBKEYS);
while(crypto_keyfp_recommended_length < FP64_SIZE)
{
memset(buf, 0, 256);
for(i = 0; i < MAX_PUBKEYS; ++i)
if(pubkeys[i])
{
if(!buf[256 + i])
++buf[(unsigned char) pubkeys_fp64[i][crypto_keyfp_recommended_length]];
if(pubkeys_havepriv[i])
if(!buf[256 + MAX_PUBKEYS + i])
++buf[(unsigned char) pubkeys_priv_fp64[i][crypto_keyfp_recommended_length]];
}
for(i = 0; i < MAX_PUBKEYS; ++i)
if(pubkeys[i])
{
if(!buf[256 + i])
if(buf[(unsigned char) pubkeys_fp64[i][crypto_keyfp_recommended_length]] < 2)
buf[256 + i] = 1;
if(pubkeys_havepriv[i])
if(!buf[256 + MAX_PUBKEYS + i])
if(buf[(unsigned char) pubkeys_priv_fp64[i][crypto_keyfp_recommended_length]] < 2)
buf[256 + MAX_PUBKEYS + i] = 1;
}
++crypto_keyfp_recommended_length;
for(i = 0; i < MAX_PUBKEYS; ++i)
if(pubkeys[i])
{
if(!buf[256 + i])
break;
if(pubkeys_havepriv[i])
if(!buf[256 + MAX_PUBKEYS + i])
break;
}
if(i >= MAX_PUBKEYS)
break;
}
if(crypto_keyfp_recommended_length < 7)
crypto_keyfp_recommended_length = 7;
}
static void Crypto_UnloadKeys(void)
{
int i;
keygen_i = -1;
for(i = 0; i < MAX_PUBKEYS; ++i)
{
if(pubkeys[i])
qd0_blind_id_free(pubkeys[i]);
pubkeys[i] = NULL;
pubkeys_havepriv[i] = false;
pubkeys_havesig[i] = false;
memset(pubkeys_fp64[i], 0, sizeof(pubkeys_fp64[i]));
memset(pubkeys_priv_fp64[i], 0, sizeof(pubkeys_fp64[i]));
challenge_append_length = 0;
}