forked from keepassxreboot/keepassxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zxcvbn.c
1763 lines (1658 loc) · 59.8 KB
/
zxcvbn.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
/**********************************************************************************
* C implementation of the zxcvbn password strength estimation method.
* Copyright (c) 2015-2017 Tony Evans
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
**********************************************************************************/
#include <zxcvbn.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <float.h>
/* printf */
#ifdef __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif
#ifdef USE_DICT_FILE
#if defined(USE_FILE_IO) || !defined(__cplusplus)
#include <stdio.h>
#else
#include <fstream>
#endif
#endif
/* Minimum number of characters in a incrementing/decrementing sequence match */
#define MIN_SEQUENCE_LEN 3
/* Year range for data matching */
#define MIN_YEAR 1901
#define MAX_YEAR 2050
/* Minimum number of characters in a spatial matching sequence */
#define MIN_SPATIAL_LEN 3
/* Minimum number of characters in a repeat sequence match */
#define MIN_REPEAT_LEN 2
/* Additional entropy to add when password is made of multiple matches. Use different
* amounts depending on whether the match is at the end of the password, or in the
* middle. If the match is at the begining then there is no additional entropy.
*/
#define MULTI_END_ADDITION 1.0
#define MULTI_MID_ADDITION 1.75
/*################################################################################*
*################################################################################*
* Begin utility function code
*################################################################################*
*################################################################################*/
/**********************************************************************************
* Binomial coefficient function. Uses method described at
* http://blog.plover.com/math/choose.html
*/
static double nCk(int n, int k)
{
int d;
double r;
if (k > n)
return 0.0;
if (!k)
return 1.0;
r = 1.0;
for(d = 1; d <= k; ++d)
{
r *= n--;
r /= d;
}
return r;
}
/**********************************************************************************
* Binary search function to find a character in a string.
* Parameters:
* Ch The character to find
* Ents The string to search
* NumEnts The number character groups in the string Ents
* SizeEnt The size of each character group.
* Returns a pointer to the found character, or null if not found.
*/
static const uint8_t *CharBinSearch(uint8_t Ch, const uint8_t *Ents, unsigned int NumEnts, unsigned int SizeEnt)
{
while(NumEnts > 0)
{
const uint8_t *Mid = Ents + (NumEnts >> 1) * SizeEnt;
int Dif = Ch - *Mid;
if (!Dif)
{
return Mid;
}
if (Dif > 0)
{
Ents = Mid + SizeEnt;
--NumEnts;
}
NumEnts /= 2;
}
return 0;
}
/**********************************************************************************
* Calculate potential number of different characters in the passed string.
* Parameters:
* Str The string of characters
* Len The maximum number of characters to scan
* Returns the potential number of different characters in the string.
*/
static int Cardinality(const uint8_t *Str, int Len)
{
int Card=0, Types=0;
int c;
while(Len > 0)
{
c = *Str++ & 0xFF;
if (!c)
break;
if (islower(c)) Types |= 1; /* Lowercase letter */
else if (isupper(c)) Types |= 2; /* Uppercase letter */
else if (isdigit(c)) Types |= 4; /* Numeric digit */
else if (c <= 0x7F) Types |= 8; /* Punctuation character */
else Types |= 16; /* Other (Unicode?) */
--Len;
}
if (Types & 1) Card += 26;
if (Types & 2) Card += 26;
if (Types & 4) Card += 10;
if (Types & 8) Card += 33;
if (Types & 16) Card += 100;
return Card;
}
/**********************************************************************************
* Allocate a ZxcMatch_t struct, clear it to zero
*/
static ZxcMatch_t *AllocMatch()
{
ZxcMatch_t *p = MallocFn(ZxcMatch_t, 1);
memset(p, 0, sizeof *p);
return p;
}
/**********************************************************************************
* Add new match struct to linked list of matches. List ordered with shortest at
* head of list. Note: passed new match struct in parameter Nu may be de allocated.
*/
static void AddResult(ZxcMatch_t **HeadRef, ZxcMatch_t *Nu, int MaxLen)
{
/* Adjust the entropy to be used for calculations depending on whether the passed match is
* at the begining, middle or end of the password
*/
if (Nu->Begin)
{
if (Nu->Length >= MaxLen)
Nu->MltEnpy = Nu->Entrpy + MULTI_END_ADDITION * log(2.0);
else
Nu->MltEnpy = Nu->Entrpy + MULTI_MID_ADDITION * log(2.0);
}
else
Nu->MltEnpy = Nu->Entrpy;
/* Find the correct insert point */
while(*HeadRef && ((*HeadRef)->Length < Nu->Length))
HeadRef = &((*HeadRef)->Next);
/* Add new entry or replace existing */
if (*HeadRef && ((*HeadRef)->Length == Nu->Length))
{
/* New entry has same length as existing, so one of them needs discarding */
if ((*HeadRef)->MltEnpy <= Nu->MltEnpy)
{
/* Existing entry has lower entropy - keep it, discard new entry */
FreeFn(Nu);
}
else
{
/* New entry has lower entropy - replace existing entry */
Nu->Next = (*HeadRef)->Next;
FreeFn(*HeadRef);
*HeadRef = Nu;
}
}
else
{
/* New entry has different length, so add it */
Nu->Next = *HeadRef;
*HeadRef = Nu;
}
}
/**********************************************************************************
* See if the match is repeated. If it is then add a new repeated match to the results.
*/
static void AddMatchRepeats(ZxcMatch_t **Result, ZxcMatch_t *Match, const uint8_t *Passwd, int MaxLen)
{
int Len = Match->Length;
const uint8_t *Rpt = Passwd + Len;
int RepeatCount = 2;
while(MaxLen >= (Len * RepeatCount))
{
if (strncmp((const char *)Passwd, (const char *)Rpt, Len) == 0)
{
/* Found a repeat */
ZxcMatch_t *p = AllocMatch();
p->Entrpy = Match->Entrpy + log(RepeatCount);
p->Type = (ZxcTypeMatch_t)(Match->Type + MULTIPLE_MATCH);
p->Length = Len * RepeatCount;
p->Begin = Match->Begin;
AddResult(Result, p, MaxLen);
}
else
break;
++RepeatCount;
Rpt += Len;
}
}
/*################################################################################*
*################################################################################*
* Begin dictionary matching code
*################################################################################*
*################################################################################*/
#ifdef USE_DICT_FILE
/* Use dictionary data from file */
#if defined(USE_FILE_IO) || !defined(__cplusplus)
/* Use the FILE streams from stdio.h */
typedef FILE *FileHandle;
#define MyOpenFile(f, name) (f = fopen(name, "rb"))
#define MyReadFile(f, buf, bytes) (fread(buf, 1, bytes, f) == (bytes))
#define MyCloseFile(f) fclose(f)
#else
/* Use the C++ iostreams */
typedef std::ifstream FileHandle;
static inline void MyOpenFile(FileHandle & f, const char *Name)
{
f.open(Name, std::ifstream::in | std::ifstream::binary);
}
static inline bool MyReadFile(FileHandle & f, void *Buf, unsigned int Num)
{
return (bool)f.read((char *)Buf, Num);
}
static inline void MyCloseFile(FileHandle & f)
{
f.close();
}
#endif
/* Include file contains the CRC of the dictionary data file. Used to detect corruption */
/* of the file. */
#include "dict-crc.h"
#define MAX_DICT_FILE_SIZE (100+WORD_FILE_SIZE)
#define CHK_INIT 0xffffffffffffffffULL
/* Static table used for the crc implementation. */
static const uint64_t CrcTable[16] =
{
0x0000000000000000ULL, 0x7d08ff3b88be6f81ULL, 0xfa11fe77117cdf02ULL, 0x8719014c99c2b083ULL,
0xdf7adabd7a6e2d6fULL, 0xa2722586f2d042eeULL, 0x256b24ca6b12f26dULL, 0x5863dbf1e3ac9decULL,
0x95ac9329ac4bc9b5ULL, 0xe8a46c1224f5a634ULL, 0x6fbd6d5ebd3716b7ULL, 0x12b5926535897936ULL,
0x4ad64994d625e4daULL, 0x37deb6af5e9b8b5bULL, 0xb0c7b7e3c7593bd8ULL, 0xcdcf48d84fe75459ULL
};
static const unsigned int MAGIC = 'z' + ('x'<< 8) + ('c' << 16) + ('v' << 24);
static unsigned int NumNodes, NumChildLocs, NumRanks, NumWordEnd, NumChildMaps;
static unsigned int SizeChildMapEntry, NumLargeCounts, NumSmallCounts, SizeCharSet;
static unsigned int *DictNodes;
static uint8_t *WordEndBits;
static unsigned int *ChildLocs;
static unsigned short *Ranks;
static uint8_t *ChildMap;
static uint8_t *EndCountLge;
static uint8_t *EndCountSml;
static char *CharSet;
/**********************************************************************************
* Calculate the CRC-64 of passed data.
* Parameters:
* Crc The initial or previous CRC value
* v Pointer to the data to add to CRC calculation
* Len Length of the passed data
* Returns the updated CRC value.
*/
static uint64_t CalcCrc64(uint64_t Crc, const void *v, unsigned int Len)
{
const uint8_t *Data = (const unsigned char *)v;
while(Len--)
{
Crc = CrcTable[(Crc ^ (*Data >> 0)) & 0x0f] ^ (Crc >> 4);
Crc = CrcTable[(Crc ^ (*Data >> 4)) & 0x0f] ^ (Crc >> 4);
++Data;
}
return Crc;
}
/**********************************************************************************
* Read the dictionary data from file.
* Parameters:
* Filename Name of the file to read.
* Returns 1 on success, 0 on error
*/
int ZxcvbnInit(const char *Filename)
{
FileHandle f;
uint64_t Crc = CHK_INIT;
if (DictNodes)
return 1;
MyOpenFile(f, Filename);
if (f)
{
unsigned int i, DictSize;
/* Get magic number */
if (!MyReadFile(f, &i, sizeof i))
i = 0;
/* Get header data */
if (!MyReadFile(f, &NumNodes, sizeof NumNodes))
i = 0;
if (!MyReadFile(f, &NumChildLocs, sizeof NumChildLocs))
i = 0;
if (!MyReadFile(f, &NumRanks, sizeof NumRanks))
i = 0;
if (!MyReadFile(f, &NumWordEnd, sizeof NumWordEnd))
i = 0;
if (!MyReadFile(f, &NumChildMaps, sizeof NumChildMaps))
i = 0;
if (!MyReadFile(f, &SizeChildMapEntry, sizeof SizeChildMapEntry))
i = 0;
if (!MyReadFile(f, &NumLargeCounts, sizeof NumLargeCounts))
i = 0;
if (!MyReadFile(f, &NumSmallCounts, sizeof NumSmallCounts))
i = 0;
if (!MyReadFile(f, &SizeCharSet, sizeof SizeCharSet))
i = 0;
/* Validate the header data */
if (NumNodes >= (1<<17))
i = 1;
if (NumChildLocs >= (1<<BITS_CHILD_MAP_INDEX))
i = 2;
if (NumChildMaps >= (1<<BITS_CHILD_PATT_INDEX))
i = 3;
if ((SizeChildMapEntry*8) < SizeCharSet)
i = 4;
if (NumLargeCounts >= (1<<9))
i = 5;
if (NumSmallCounts != NumNodes)
i = 6;
if (i != MAGIC)
{
MyCloseFile(f);
return 0;
}
Crc = CalcCrc64(Crc, &i, sizeof i);
Crc = CalcCrc64(Crc, &NumNodes, sizeof NumNodes);
Crc = CalcCrc64(Crc, &NumChildLocs, sizeof NumChildLocs);
Crc = CalcCrc64(Crc, &NumRanks, sizeof NumRanks);
Crc = CalcCrc64(Crc, &NumWordEnd, sizeof NumWordEnd);
Crc = CalcCrc64(Crc, &NumChildMaps, sizeof NumChildMaps);
Crc = CalcCrc64(Crc, &SizeChildMapEntry, sizeof SizeChildMapEntry);
Crc = CalcCrc64(Crc, &NumLargeCounts, sizeof NumLargeCounts);
Crc = CalcCrc64(Crc, &NumSmallCounts, sizeof NumSmallCounts);
Crc = CalcCrc64(Crc, &SizeCharSet, sizeof SizeCharSet);
DictSize = NumNodes*sizeof(*DictNodes) + NumChildLocs*sizeof(*ChildLocs) + NumRanks*sizeof(*Ranks) +
NumWordEnd + NumChildMaps*SizeChildMapEntry + NumLargeCounts + NumSmallCounts + SizeCharSet;
if (DictSize < MAX_DICT_FILE_SIZE)
{
DictNodes = MallocFn(unsigned int, DictSize / sizeof(unsigned int) + 1);
if (!MyReadFile(f, DictNodes, DictSize))
{
FreeFn(DictNodes);
DictNodes = 0;
}
}
MyCloseFile(f);
if (!DictNodes)
return 0;
/* Check crc */
Crc = CalcCrc64(Crc, DictNodes, DictSize);
if (memcmp(&Crc, WordCheck, sizeof Crc))
{
/* File corrupted */
FreeFn(DictNodes);
DictNodes = 0;
return 0;
}
fflush(stdout);
/* Set pointers to the data */
ChildLocs = DictNodes + NumNodes;
Ranks = (unsigned short *)(ChildLocs + NumChildLocs);
WordEndBits = (unsigned char *)(Ranks + NumRanks);
ChildMap = (unsigned char*)(WordEndBits + NumWordEnd);
EndCountLge = ChildMap + NumChildMaps*SizeChildMapEntry;
EndCountSml = EndCountLge + NumLargeCounts;
CharSet = (char *)EndCountSml + NumSmallCounts;
CharSet[SizeCharSet] = 0;
return 1;
}
return 0;
}
/**********************************************************************************
* Free the data allocated by ZxcvbnInit().
*/
void ZxcvbnUnInit()
{
if (DictNodes)
FreeFn(DictNodes);
DictNodes = 0;
}
#else
/* Include the source file containing the dictionary data */
#include "dict-src.h"
#endif
/**********************************************************************************
* Leet conversion strings
*/
/* String of normal chars that could be given as leet chars in the password */
static const uint8_t L33TChr[] = "abcegilostxz";
/* String of leet,normal,normal char triples. Used to convert supplied leet char to normal. */
static const uint8_t L33TCnv[] = "!i $s %x (c +t 0o 1il2z 3e 4a 5s 6g 7lt8b 9g <c @a [c {c |il";
#define LEET_NORM_MAP_SIZE 3
/* Struct holding additional data on the word match */
typedef struct
{
int Rank; /* Rank of word in dictionary */
int Caps; /* Number of capital letters */
int Lower; /* Number of lower case letters */
int NumLeet; /* Total number of leeted characters */
uint8_t Leeted[sizeof L33TChr]; /* Number of leeted chars for each char found in L33TChr */
uint8_t UnLeet[sizeof L33TChr]; /* Number of normal chars for each char found in L33TChr */
} DictMatchInfo_t;
/* Struct holding working data for the word match */
typedef struct
{
uint32_t StartLoc;
int Ordinal;
int PwdLength;
int Begin;
int Caps;
int Lower;
int NumPossChrs;
uint8_t Leeted[sizeof L33TChr];
uint8_t UnLeet[sizeof L33TChr];
uint8_t LeetCnv[sizeof L33TCnv / LEET_NORM_MAP_SIZE + 1];
uint8_t First;
uint8_t PossChars[CHARSET_SIZE];
} DictWork_t;
/**********************************************************************************
* Given a map entry create a string of all possible characters for following to
* a child node
*/
static int ListPossibleChars(uint8_t *List, const uint8_t *Map)
{
unsigned int i, j, k;
int Len = 0;
for(k = i = 0; i < SizeChildMapEntry; ++i, ++Map)
{
if (!*Map)
{
k += 8;
continue;
}
for(j = 0; j < 8; ++j)
{
if (*Map & (1<<j))
{
*List++ = CharSet[k];
++Len;
}
++k;
}
}
*List=0;
return Len;
}
/**********************************************************************************
* Increment count of each char that could be leeted.
*/
static void AddLeetChr(uint8_t c, int IsLeet, uint8_t *Leeted, uint8_t *UnLeet)
{
const uint8_t *p = CharBinSearch(c, L33TChr, sizeof L33TChr - 1, 1);
if (p)
{
int i = p - L33TChr;
if (IsLeet > 0)
{
Leeted[i] += 1;
}
else if (IsLeet < 0)
{
Leeted[i] += 1;
UnLeet[i] -= 1;
}
else
{
UnLeet[i] += 1;
}
}
}
/**********************************************************************************
* Given details of a word match, update it with the entropy (as natural log of
* number of possiblities)
*/
static void DictionaryEntropy(ZxcMatch_t *m, DictMatchInfo_t *Extra, const uint8_t *Pwd)
{
double e = 0.0;
/* Add allowance for uppercase letters */
if (Extra->Caps)
{
if (Extra->Caps == m->Length)
{
/* All uppercase, common case so only 1 bit */
e += log(2.0);
}
else if ((Extra->Caps == 1) && (isupper(*Pwd) || isupper(Pwd[m->Length - 1])))
{
/* Only first or last uppercase, also common so only 1 bit */
e += log(2.0);
}
else
{
/* Get number of combinations of lowercase, uppercase letters */
int Up = Extra->Caps;
int Lo = Extra->Lower;
int i = Up;
if (i > Lo)
i = Lo;
for(Lo += Up; i >= 0; --i)
e += nCk(Lo, i);
if (e > 0.0)
e = log(e);
}
}
/* Add allowance for using leet substitution */
if (Extra->NumLeet)
{
int i;
double d = 0.0;
for(i = sizeof Extra->Leeted - 1; i >= 0; --i)
{
int Sb = Extra->Leeted[i];
if (Sb)
{
int Un = Extra->UnLeet[i];
int j = m->Length - Extra->NumLeet;
if ((j >= 0) && (Un > j))
Un = j;
j = Sb;
if (j > Un)
j = Un;
for(Un += Sb; j >= 0; --j)
{
double z = nCk(Un, j);
d += z;
}
}
}
if (d > 0.0)
d = log(d);
if (d < log(2.0))
d = log(2.0);
e += d;
}
/* Add entropy due to word's rank */
e += log((double)Extra->Rank);
m->Entrpy = e;
}
/**********************************************************************************
* Function that does the word matching
*/
static void DoDictMatch(const uint8_t *Passwd, int Start, int MaxLen, DictWork_t *Wrk, ZxcMatch_t **Result, DictMatchInfo_t *Extra, int Lev)
{
int Len;
uint8_t TempLeet[LEET_NORM_MAP_SIZE];
int Ord = Wrk->Ordinal;
int Caps = Wrk->Caps;
int Lower = Wrk->Lower;
unsigned int NodeLoc = Wrk->StartLoc;
uint8_t *PossChars = Wrk->PossChars;
int NumPossChrs = Wrk->NumPossChrs;
const uint8_t *Pwd = Passwd;
uint32_t NodeData = DictNodes[NodeLoc];
Passwd += Start;
for(Len = 0; *Passwd && (Len < MaxLen); ++Len, ++Passwd)
{
uint8_t c;
int w, x, y, z;
const uint8_t *q;
z = 0;
if (!Len && Wrk->First)
{
c = Wrk->First;
}
else
{
/* Get char and set of possible chars at current point in word. */
const uint8_t *Bmap;
c = *Passwd;
Bmap = ChildMap + (NodeData & ((1<<BITS_CHILD_PATT_INDEX)-1)) * SizeChildMapEntry;
NumPossChrs = ListPossibleChars(PossChars, Bmap);
/* Make it lowercase and update lowercase, uppercase counts */
if (isupper(c))
{
c = tolower(c);
++Caps;
}
else if (islower(c))
{
++Lower;
}
/* See if current char is a leet and can be converted */
q = CharBinSearch(c, L33TCnv, sizeof L33TCnv / LEET_NORM_MAP_SIZE, LEET_NORM_MAP_SIZE);
if (q)
{
/* Found, see if used before */
unsigned int j;
unsigned int i = (q - L33TCnv ) / LEET_NORM_MAP_SIZE;
if (Wrk->LeetCnv[i])
{
/* Used before, so limit characters to try */
TempLeet[0] = c;
TempLeet[1] = Wrk->LeetCnv[i];
TempLeet[2] = 0;
q = TempLeet;
}
for(j = 0; (*q > ' ') && (j < LEET_NORM_MAP_SIZE); ++j, ++q)
{
const uint8_t *r = CharBinSearch(*q, PossChars, NumPossChrs, 1);
if (r)
{
/* valid conversion from leet */
DictWork_t wrk;
wrk = *Wrk;
wrk.StartLoc = NodeLoc;
wrk.Ordinal = Ord;
wrk.PwdLength += Len;
wrk.Caps = Caps;
wrk.Lower = Lower;
wrk.First = *r;
wrk.NumPossChrs = NumPossChrs;
memcpy(wrk.PossChars, PossChars, sizeof wrk.PossChars);
if (j)
{
wrk.LeetCnv[i] = *r;
AddLeetChr(*r, -1, wrk.Leeted, wrk.UnLeet);
}
DoDictMatch(Pwd, Passwd - Pwd, MaxLen - Len, &wrk, Result, Extra, Lev + 1);
}
}
return;
}
}
q = CharBinSearch(c, PossChars, NumPossChrs, 1);
if (q)
{
/* Found the char as a normal char */
if (CharBinSearch(c, L33TChr, sizeof L33TChr - 1, 1))
{
/* Char matches, but also a normal equivalent to a leet char */
AddLeetChr(c, 0, Wrk->Leeted, Wrk->UnLeet);
}
}
if (!q)
{
/* No match for char - return */
return;
}
/* Add all the end counts of the child nodes before the one that matches */
x = (q - Wrk->PossChars);
y = (NodeData >> BITS_CHILD_PATT_INDEX) & ((1 << BITS_CHILD_MAP_INDEX) - 1);
NodeLoc = ChildLocs[x+y];
for(w=0; w<x; ++w)
{
unsigned int Cloc = ChildLocs[w+y];
z = EndCountSml[Cloc];
if (Cloc < NumLargeCounts)
z += EndCountLge[Cloc]*256;
Ord += z;
}
/* Move to next node */
NodeData = DictNodes[NodeLoc];
if (WordEndBits[NodeLoc >> 3] & (1<<(NodeLoc & 7)))
{
/* Word matches, save result */
unsigned int v;
ZxcMatch_t *p;
v = Ranks[Ord];
if (v & (1<<15))
v = (v & ((1 << 15) - 1)) * 4 + (1 << 15);
Extra->Caps = Caps;
Extra->Rank = v;
Extra->Lower = Lower;
for(x = 0, y = sizeof Extra->Leeted - 1; y >= 0; --y)
x += Wrk->Leeted[y];
Extra->NumLeet = x;
memcpy(Extra->UnLeet, Wrk->UnLeet, sizeof Extra->UnLeet);
memcpy(Extra->Leeted, Wrk->Leeted, sizeof Extra->Leeted);
p = AllocMatch();
if (x)
p->Type = DICT_LEET_MATCH;
else
p->Type = DICTIONARY_MATCH;
p->Length = Wrk->PwdLength + Len + 1;
p->Begin = Wrk->Begin;
DictionaryEntropy(p, Extra, Pwd);
AddMatchRepeats(Result, p, Pwd, MaxLen);
AddResult(Result, p, MaxLen);
++Ord;
}
}
}
/**********************************************************************************
* Try to match password part with user supplied dictionary words
* Parameters:
* Result Pointer head of linked list used to store results
* Words Array of pointers to dictionary words
* Passwd The start of the password
* Start Where in the password to start attempting to match
* MaxLen Maximum number characters to consider
*/
static void UserMatch(ZxcMatch_t **Result, const char *Words[], const uint8_t *Passwd, int Start, int MaxLen)
{
int Rank;
if (!Words)
return;
Passwd += Start;
for(Rank = 0; Words[Rank]; ++Rank)
{
DictMatchInfo_t Extra;
uint8_t LeetChr[sizeof L33TCnv / LEET_NORM_MAP_SIZE + 1];
uint8_t TempLeet[3];
int Len = 0;
int Caps = 0;
int Lowers = 0;
int Leets = 0;
const uint8_t *Wrd = (const uint8_t *)(Words[Rank]);
const uint8_t *Pwd = Passwd;
memset(Extra.Leeted, 0, sizeof Extra.Leeted);
memset(Extra.UnLeet, 0, sizeof Extra.UnLeet);
memset(LeetChr, 0, sizeof LeetChr);
while(*Wrd)
{
const uint8_t *q;
uint8_t d = tolower(*Wrd++);
uint8_t c = *Pwd++;
if (isupper(c))
{
c = tolower(c);
++Caps;
}
else if (islower(c))
{
++Lowers;
}
/* See if current char is a leet and can be converted */
q = CharBinSearch(c, L33TCnv, sizeof L33TCnv / LEET_NORM_MAP_SIZE, LEET_NORM_MAP_SIZE);
if (q)
{
/* Found, see if used before */
unsigned int j;
unsigned int i = (q - L33TCnv ) / LEET_NORM_MAP_SIZE;
if (LeetChr[i])
{
/* Used before, so limit characters to try */
TempLeet[0] = c;
TempLeet[1] = LeetChr[i];
TempLeet[2] = 0;
q = TempLeet;
}
c = d+1;
for(j = 0; (*q > ' ') && (j < LEET_NORM_MAP_SIZE); ++j, ++q)
{
if (d == *q)
{
c = d;
if (i)
{
LeetChr[i] = c;
AddLeetChr(c, 1, Extra.Leeted, Extra.UnLeet);
++Leets;
}
break;
}
}
if (c != d)
{
Len = 0;
break;
}
}
else if (c == d)
{
/* Found the char as a normal char */
if (CharBinSearch(c, L33TChr, sizeof L33TChr - 1, 1))
{
/* Char matches, but also a normal equivalent to a leet char */
AddLeetChr(c, 0, Extra.Leeted, Extra.UnLeet);
}
}
else
{
/* No Match */
Len = 0;
break;
}
if (++Len > MaxLen)
{
Len = 0;
break;
}
}
if (Len)
{
ZxcMatch_t *p = AllocMatch();
if (!Leets)
p->Type = USER_MATCH;
else
p->Type = USER_LEET_MATCH;
p->Length = Len;
p->Begin = Start;
/* Add Entrpy */
Extra.Caps = Caps;
Extra.Lower = Lowers;
Extra.NumLeet = Leets;
Extra.Rank = Rank+1;
DictionaryEntropy(p, &Extra, Passwd);
AddMatchRepeats(Result, p, Passwd, MaxLen);
AddResult(Result, p, MaxLen);
}
}
}
/**********************************************************************************
* Try to match password part with the dictionary words
* Parameters:
* Result Pointer head of linked list used to store results
* Passwd The start of the password
* Start Where in the password to start attempting to match
* MaxLen Maximum number characters to consider
*/
static void DictionaryMatch(ZxcMatch_t **Result, const uint8_t *Passwd, int Start, int MaxLen)
{
DictWork_t Wrk;
DictMatchInfo_t Extra;
memset(&Extra, 0, sizeof Extra);
memset(&Wrk, 0, sizeof Wrk);
Wrk.Ordinal = 1;
Wrk.StartLoc = ROOT_NODE_LOC;
Wrk.Begin = Start;
DoDictMatch(Passwd+Start, 0, MaxLen, &Wrk, Result, &Extra, 0);
}
/*################################################################################*
*################################################################################*
* Begin keyboard spatial sequence matching code
*################################################################################*
*################################################################################*/
/* Struct to hold information on a keyboard layout */
typedef struct Keyboard
{
const uint8_t *Keys;
const uint8_t *Shifts;
int NumKeys;
int NumNear;
int NumShift;
int NumBlank;
} Keyboard_t;
/* Struct to hold information on the match */
typedef struct
{
int Keyb;
int Turns;
int Shifts;
} SpatialMatchInfo_t;
/* Shift mapping, characters in pairs: first is shifted, second un-shifted. */
static const uint8_t UK_Shift[] = "!1\"2$4%5&7(9)0*8:;<,>.?/@'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz^6_-{[|\\}]~#�4�3�`";
static const uint8_t US_Shift[] = "!1\"'#3$4%5&7(9)0*8:;<,>.?/@2AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz^6_-{[|\\}]~`";
/* Neighbour tables */
static const uint8_t UK_Qwerty[48*7] =
{
/* key, left, up-left, up-right, right, down-right, down-left */
'#', '\'',']', 0, 0, 0, 0, '\'',';', '[', ']', '#', 0, '/',
',', 'm', 'k', 'l', '.', 0, 0, '-', '0', 0, 0, '=', '[', 'p',
'.', ',', 'l', ';', '/', 0, 0, '/', '.', ';', '\'', 0, 0, 0,
'0', '9', 0, 0, '-', 'p', 'o', '1', '`', 0, 0, '2', 'q', 0,
'2', '1', 0, 0, '3', 'w', 'q', '3', '2', 0, 0, '4', 'e', 'w',
'4', '3', 0, 0, '5', 'r', 'e', '5', '4', 0, 0, '6', 't', 'r',
'6', '5', 0, 0, '7', 'y', 't', '7', '6', 0, 0, '8', 'u', 'y',
'8', '7', 0, 0, '9', 'i', 'u', '9', '8', 0, 0, '0', 'o', 'i',
';', 'l', 'p', '[','\'', '/', '.', '=', '-', 0, 0, 0, ']', '[',
'[', 'p', '-', '=', ']', '\'',';', '\\', 0, 0, 'a', 'z', 0, 0,
']', '[', '=', 0, 0, '#','\'', '`', 0, 0, 0, '1', 0, 0,
'a', 0, 'q', 'w', 's', 'z','\\', 'b', 'v', 'g', 'h', 'n', 0, 0,
'c', 'x', 'd', 'f', 'v', 0, 0, 'd', 's', 'e', 'r', 'f', 'c', 'x',
'e', 'w', '3', '4', 'r', 'd', 's', 'f', 'd', 'r', 't', 'g', 'v', 'c',
'g', 'f', 't', 'y', 'h', 'b', 'v', 'h', 'g', 'y', 'u', 'j', 'n', 'b',
'i', 'u', '8', '9', 'o', 'k', 'j', 'j', 'h', 'u', 'i', 'k', 'm', 'n',
'k', 'j', 'i', 'o', 'l', ',', 'm', 'l', 'k', 'o', 'p', ';', '.', ',',
'm', 'n', 'j', 'k', ',', 0, 0, 'n', 'b', 'h', 'j', 'm', 0, 0,
'o', 'i', '9', '0', 'p', 'l', 'k', 'p', 'o', '0', '-', '[', ';', 'l',
'q', 0, '1', '2', 'w', 'a', 0, 'r', 'e', '4', '5', 't', 'f', 'd',
's', 'a', 'w', 'e', 'd', 'x', 'z', 't', 'r', '5', '6', 'y', 'g', 'f',
'u', 'y', '7', '8', 'i', 'j', 'h', 'v', 'c', 'f', 'g', 'b', 0, 0,
'w', 'q', '2', '3', 'e', 's', 'a', 'x', 'z', 's', 'd', 'c', 0, 0,
'y', 't', '6', '7', 'u', 'h', 'g', 'z', '\\','a', 's', 'x', 0, 0
};
static const uint8_t US_Qwerty[47*7] =
{
/* key, left, up-left, up-right, right, down-right, down-left */
'\'',';', '[', ']', 0, 0, '/', ',', 'm', 'k', 'l', '.', 0, 0,
'-', '0', 0, 0, '=', '[', 'p', '.', ',', 'l', ';', '/', 0, 0,
'/', '.', ';','\'', 0, 0, 0, '0', '9', 0, 0, '-', 'p', 'o',
'1', '`', 0, 0, '2', 'q', 0, '2', '1', 0, 0, '3', 'w', 'q',
'3', '2', 0, 0, '4', 'e', 'w', '4', '3', 0, 0, '5', 'r', 'e',
'5', '4', 0, 0, '6', 't', 'r', '6', '5', 0, 0, '7', 'y', 't',
'7', '6', 0, 0, '8', 'u', 'y', '8', '7', 0, 0, '9', 'i', 'u',
'9', '8', 0, 0, '0', 'o', 'i', ';', 'l', 'p', '[','\'', '/', '.',
'=', '-', 0, 0, 0, ']', '[', '[', 'p', '-', '=', ']','\'', ';',
'\\',']', 0, 0, 0, 0, 0, ']', '[', '=', 0,'\\', 0,'\'',
'`', 0, 0, 0, '1', 0, 0, 'a', 0, 'q', 'w', 's', 'z', 0,
'b', 'v', 'g', 'h', 'n', 0, 0, 'c', 'x', 'd', 'f', 'v', 0, 0,
'd', 's', 'e', 'r', 'f', 'c', 'x', 'e', 'w', '3', '4', 'r', 'd', 's',
'f', 'd', 'r', 't', 'g', 'v', 'c', 'g', 'f', 't', 'y', 'h', 'b', 'v',
'h', 'g', 'y', 'u', 'j', 'n', 'b', 'i', 'u', '8', '9', 'o', 'k', 'j',
'j', 'h', 'u', 'i', 'k', 'm', 'n', 'k', 'j', 'i', 'o', 'l', ',', 'm',
'l', 'k', 'o', 'p', ';', '.', ',', 'm', 'n', 'j', 'k', ',', 0, 0,
'n', 'b', 'h', 'j', 'm', 0, 0, 'o', 'i', '9', '0', 'p', 'l', 'k',
'p', 'o', '0', '-', '[', ';', 'l', 'q', 0, '1', '2', 'w', 'a', 0,
'r', 'e', '4', '5', 't', 'f', 'd', 's', 'a', 'w', 'e', 'd', 'x', 'z',
't', 'r', '5', '6', 'y', 'g', 'f', 'u', 'y', '7', '8', 'i', 'j', 'h',
'v', 'c', 'f', 'g', 'b', 0, 0, 'w', 'q', '2', '3', 'e', 's', 'a',
'x', 'z', 's', 'd', 'c', 0, 0, 'y', 't', '6', '7', 'u', 'h', 'g',
'z', 0, 'a', 's', 'x', 0, 0,
};
static const uint8_t Dvorak[47*7] =
{
'\'', 0, '1', '2', ',', 'a', 0, ',','\'', '2', '3', '.', 'o', 'a',
'-', 's', '/', '=', 0, 0, 'z', '.', ',', '3', '4', 'p', 'e', 'o',
'/', 'l', '[', ']', '=', '-', 's', '0', '9', 0, 0, '[', 'l', 'r',