OLD | NEW |
1 /* | 1 /* |
2 ** This module uses code from the NIST implementation of FIPS-181, | 2 ** This module uses code from the NIST implementation of FIPS-181, |
3 ** but the algorythm is CHANGED and I think that I CAN | 3 ** but the algorythm is CHANGED and I think that I CAN |
4 ** copyright it. See copiright notes below. | 4 ** copyright it. See copiright notes below. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 ** Copyright (c) 1999, 2000, 2001, 2002, 2003 | 8 ** Copyright (c) 1999, 2000, 2001, 2002, 2003 |
9 ** Adel I. Mirzazhanov. All rights reserved | 9 ** Adel I. Mirzazhanov. All rights reserved |
10 ** | 10 ** |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 | 36 |
37 #include <stdio.h> | 37 #include <stdio.h> |
38 #include <stdlib.h> | 38 #include <stdlib.h> |
39 #include <string.h> | 39 #include <string.h> |
40 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32
__) | 40 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32
__) |
41 #include <strings.h> | 41 #include <strings.h> |
42 #endif | 42 #endif |
43 #include <time.h> | 43 #include <time.h> |
44 #include <sys/types.h> | 44 #include <sys/types.h> |
45 #include "pronpass.h" | 45 #include "base/rand_util.h" |
| 46 #include "fips181.h" |
46 #include "randpass.h" | 47 #include "randpass.h" |
47 #include "convert.h" | 48 #include "convert.h" |
48 #include "errs.h" | |
49 | 49 |
50 struct unit | 50 struct unit |
51 { | 51 { |
52 char unit_code[5]; | 52 char unit_code[5]; |
53 USHORT flags; | 53 USHORT flags; |
54 }; | 54 }; |
55 | 55 |
56 static struct unit rules[] = | 56 static struct unit rules[] = |
57 { {"a", VOWEL}, | 57 { {"a", VOWEL}, |
58 {"b", NO_SPECIAL_RULE}, | 58 {"b", NO_SPECIAL_RULE}, |
(...skipping 1227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1286 if (maxlen == 0) | 1286 if (maxlen == 0) |
1287 { | 1287 { |
1288 word[0] = '\0'; | 1288 word[0] = '\0'; |
1289 hyphenated_word[0] = '\0'; | 1289 hyphenated_word[0] = '\0'; |
1290 return (0); | 1290 return (0); |
1291 } | 1291 } |
1292 | 1292 |
1293 /* | 1293 /* |
1294 * Find password. | 1294 * Find password. |
1295 */ | 1295 */ |
1296 pwlen = gen_word (word, hyphenated_word, get_random (minlen, maxlen), pass_m
ode); | 1296 pwlen = gen_word (word, hyphenated_word, base::RandInt(minlen, maxlen), |
| 1297 pass_mode); |
1297 return (pwlen); | 1298 return (pwlen); |
1298 } | 1299 } |
1299 | 1300 |
1300 | 1301 |
1301 /* | 1302 /* |
1302 * This is the routine that returns a Random word -- as | 1303 * This is the routine that returns a Random word -- as |
1303 * yet unchecked against the passwd file or the dictionary. | 1304 * yet unchecked against the passwd file or the dictionary. |
1304 * It collects Random syllables until a predetermined | 1305 * It collects Random syllables until a predetermined |
1305 * word length is found. If a retry threshold is reached, | 1306 * word length is found. If a retry threshold is reached, |
1306 * another word is tried. Given that the Random number | 1307 * another word is tried. Given that the Random number |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1357 | 1358 |
1358 /* | 1359 /* |
1359 * Find syllables until the entire word is constructed. | 1360 * Find syllables until the entire word is constructed. |
1360 */ | 1361 */ |
1361 while (word_length < pwlen) | 1362 while (word_length < pwlen) |
1362 { | 1363 { |
1363 /* | 1364 /* |
1364 * Get the syllable and find its length. | 1365 * Get the syllable and find its length. |
1365 */ | 1366 */ |
1366 (void) gen_syllable (new_syllable, pwlen - word_length, syllable_units, &sy
llable_size); | 1367 (void) gen_syllable (new_syllable, pwlen - word_length, syllable_units, &sy
llable_size); |
1367 syllable_length = strlen (new_syllable); | 1368 syllable_length = (USHORT) strlen (new_syllable); |
1368 | 1369 |
1369 /* | 1370 /* |
1370 * Append the syllable units to the word units. | 1371 * Append the syllable units to the word units. |
1371 */ | 1372 */ |
1372 for (word_place = 0; word_place <= syllable_size; word_place++) | 1373 for (word_place = 0; word_place <= syllable_size; word_place++) |
1373 word_units[word_size + word_place] = syllable_units[word_place]; | 1374 word_units[word_size + word_place] = syllable_units[word_place]; |
1374 word_size += syllable_size + 1; | 1375 word_size += syllable_size + 1; |
1375 | 1376 |
1376 /* | 1377 /* |
1377 * If the word has been improperly formed, throw out | 1378 * If the word has been improperly formed, throw out |
1378 * the syllable. The checks performed here are those | 1379 * the syllable. The checks performed here are those |
1379 * that must be formed on a word basis. The other | 1380 * that must be formed on a word basis. The other |
1380 * tests are performed entirely within the syllable. | 1381 * tests are performed entirely within the syllable. |
1381 * Otherwise, append the syllable to the word and | 1382 * Otherwise, append the syllable to the word and |
1382 * append the syllable to the hyphenated version of | 1383 * append the syllable to the hyphenated version of |
1383 * the word. | 1384 * the word. |
1384 */ | 1385 */ |
1385 if (improper_word (word_units, word_size) || | 1386 if (improper_word (word_units, word_size) || |
1386 ((word_length == 0) && have_initial_y (syllable_units, syllable_size)) |
| | 1387 ((word_length == 0) && have_initial_y (syllable_units, syllable_size)) |
| |
1387 ((word_length + syllable_length == pwlen) && have_final_split (syllable_
units, syllable_size))) | 1388 ((word_length + syllable_length == pwlen) && have_final_split (syllable_
units, syllable_size))) |
1388 word_size -= syllable_size + 1; | 1389 word_size -= syllable_size + 1; |
1389 else | 1390 else |
1390 { | 1391 { |
1391 if (word_length == 0) | 1392 if (word_length == 0) |
1392 { | 1393 { |
1393 /* | 1394 /* |
1394 ** Modify syllable for numeric or capital symbols required | 1395 ** Modify syllable for numeric or capital symbols required |
1395 ** Should be done after word quality check. | 1396 ** Should be done after word quality check. |
1396 */ | 1397 */ |
1397 » dsd = randint(2); | 1398 dsd = base::RandInt(0, 1); |
1398 if ( ((pass_mode & S_NB) > 0) && (syllable_length == 1) && dsd == 0) | 1399 if ( ((pass_mode & S_NB) > 0) && (syllable_length == 1) && dsd == 0) |
1399 { | 1400 { |
1400 numerize(new_syllable); | 1401 numerize(new_syllable); |
1401 ch_flag = TRUE; | 1402 ch_flag = TRUE; |
1402 } | 1403 } |
1403 if ( ((pass_mode & S_SS) > 0) && (syllable_length == 1) && (dsd == 1)) | 1404 if ( ((pass_mode & S_SS) > 0) && (syllable_length == 1) && (dsd == 1)) |
1404 { | 1405 { |
1405 specialize(new_syllable); | 1406 specialize(new_syllable); |
1406 ch_flag = TRUE; | 1407 ch_flag = TRUE; |
1407 } | 1408 } |
(...skipping 13 matching lines...) Expand all Loading... |
1421 } | 1422 } |
1422 (void)memset ( (void *)new_syllable, 0, (size_t)(pwlen * sizeof(USHORT
)+1)); | 1423 (void)memset ( (void *)new_syllable, 0, (size_t)(pwlen * sizeof(USHORT
)+1)); |
1423 (void)memset ( (void *)syllable_for_hyph, 0, 20); | 1424 (void)memset ( (void *)syllable_for_hyph, 0, 20); |
1424 } | 1425 } |
1425 else | 1426 else |
1426 { | 1427 { |
1427 /* | 1428 /* |
1428 ** Modify syllable for numeric or capital symbols required | 1429 ** Modify syllable for numeric or capital symbols required |
1429 ** Should be done after word quality check. | 1430 ** Should be done after word quality check. |
1430 */ | 1431 */ |
1431 » dsd = randint(2); | 1432 dsd = base::RandInt(0, 1); |
1432 if ( ((pass_mode & S_NB) > 0) && (syllable_length == 1) && (dsd == 0)) | 1433 if ( ((pass_mode & S_NB) > 0) && (syllable_length == 1) && (dsd == 0)) |
1433 { | 1434 { |
1434 numerize(new_syllable); | 1435 numerize(new_syllable); |
1435 ch_flag = TRUE; | 1436 ch_flag = TRUE; |
1436 } | 1437 } |
1437 if ( ( (pass_mode & S_SS) > 0) && (syllable_length == 1) && (dsd == 1)
) | 1438 if ( ( (pass_mode & S_SS) > 0) && (syllable_length == 1) && (dsd == 1)
) |
1438 { | 1439 { |
1439 specialize(new_syllable); | 1440 specialize(new_syllable); |
1440 ch_flag = TRUE; | 1441 ch_flag = TRUE; |
1441 } | 1442 } |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1727 * syllable) to be valid. We ignore the checks | 1728 * syllable) to be valid. We ignore the checks |
1728 * and place it in this syllable manually. | 1729 * and place it in this syllable manually. |
1729 */ | 1730 */ |
1730 if (saved_unit == 2) | 1731 if (saved_unit == 2) |
1731 { | 1732 { |
1732 units_in_syllable[0] = saved_pair[1]; | 1733 units_in_syllable[0] = saved_pair[1]; |
1733 if (rules[saved_pair[1]].flags & VOWEL) | 1734 if (rules[saved_pair[1]].flags & VOWEL) |
1734 vowel_count++; | 1735 vowel_count++; |
1735 current_unit++; | 1736 current_unit++; |
1736 (void) strcpy (syllable, rules[saved_pair[1]].unit_code); | 1737 (void) strcpy (syllable, rules[saved_pair[1]].unit_code); |
1737 length_left -= strlen (syllable); | 1738 length_left -= (short) strlen (syllable); |
1738 } | 1739 } |
1739 | 1740 |
1740 /* | 1741 /* |
1741 * The unit becomes the last unit checked in the | 1742 * The unit becomes the last unit checked in the |
1742 * previous syllable. | 1743 * previous syllable. |
1743 */ | 1744 */ |
1744 unit = saved_pair[0]; | 1745 unit = saved_pair[0]; |
1745 | 1746 |
1746 /* | 1747 /* |
1747 * The saved units have been used. Do not try to | 1748 * The saved units have been used. Do not try to |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2242 /* | 2243 /* |
2243 * Select a unit (a letter or a consonant group). If a vowel is | 2244 * Select a unit (a letter or a consonant group). If a vowel is |
2244 * expected, use the vowel_numbers array rather than looping through | 2245 * expected, use the vowel_numbers array rather than looping through |
2245 * the numbers array until a vowel is found. | 2246 * the numbers array until a vowel is found. |
2246 */ | 2247 */ |
2247 USHORT | 2248 USHORT |
2248 random_unit (USHORT type) | 2249 random_unit (USHORT type) |
2249 { | 2250 { |
2250 USHORT number; | 2251 USHORT number; |
2251 | 2252 |
2252 /* | 2253 /* |
2253 * Sometimes, we are asked to explicitly get a vowel (i.e., if | 2254 * Sometimes, we are asked to explicitly get a vowel (i.e., if |
2254 * a digram pair expects one following it). This is a shortcut | 2255 * a digram pair expects one following it). This is a shortcut |
2255 * to do that and avoid looping with rejected consonants. | 2256 * to do that and avoid looping with rejected consonants. |
2256 */ | 2257 */ |
2257 if (type & VOWEL) | 2258 if (type & VOWEL) |
2258 number = vowel_numbers[get_random (0, (sizeof (vowel_numbers) / sizeof (USH
ORT))-1)]; | 2259 number = vowel_numbers[ |
| 2260 base::RandInt(0, (sizeof (vowel_numbers) / sizeof (USHORT))-1)]; |
2259 else | 2261 else |
2260 /* | 2262 /* |
2261 * Get any letter according to the English distribution. | 2263 * Get any letter according to the English distribution. |
2262 */ | 2264 */ |
2263 number = numbers[get_random (0, (sizeof (numbers) / sizeof (USHORT))-1)]; | 2265 number = numbers[ |
| 2266 base::RandInt(0, (sizeof (numbers) / sizeof (USHORT))-1)]; |
2264 return (number); | 2267 return (number); |
2265 } | 2268 } |
2266 | |
2267 | |
2268 /* | |
2269 ** get_random() - | |
2270 ** This routine should return a uniformly distributed Random number between | |
2271 ** minlen and maxlen inclusive. The Electronic Code Book form of CAST is | |
2272 ** used to produce the Random number. The inputs to CAST are the old pass- | |
2273 ** word and a pseudoRandom key generated according to the procedure out- | |
2274 ** lined in Appendix C of ANSI X9.17. | |
2275 ** INPUT: | |
2276 ** USHORT - minimum | |
2277 ** USHORT - maximum | |
2278 ** OUTPUT: | |
2279 ** USHORT - random number | |
2280 ** NOTES: | |
2281 ** none. | |
2282 */ | |
2283 | |
2284 USHORT | |
2285 get_random (USHORT minlen, USHORT maxlen) | |
2286 { | |
2287 USHORT ret = 0; | |
2288 ret = minlen + (USHORT) randint ((int) (maxlen - minlen + 1)); | |
2289 return (ret); | |
2290 } | |
OLD | NEW |