root/dpwgen/dpwgen.c
/*DEFINITIONS
This source file includes following definitions.1 /* 2 ** Copyright (c) 2024 Amelia Campbell 3 ** 4 ** Redistribution and use in source and binary forms, with or without 5 ** modification, are permitted provided that the following conditions are 6 ** met: 7 ** 8 ** 1. Redistributions of source code must retain the above copyright 9 ** notice, this list of conditions and the following disclaimer. 10 ** 2. Redistributions in binary form must reproduce the above 11 ** copyright notice, this list of conditions and the following 12 ** disclaimer in the documentation and/or other materials provided 13 ** with the distribution. 14 ** 3. Neither the name of the copyright holder nor the names of its 15 ** contributors may be used to endorse or promote products derived 16 ** from this software without specific prior written permission. 17 ** 18 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 19 ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 21 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <stdio.h> 32 #include <string.h> 33 #include <stdlib.h> 34 #include <ctype.h> 35 #include <math.h> 36 37 #include <shared/macro.h> 38 #include <shared/term.h> 39 #include <shared/rand.h> 40 41 #define BUFSZ 4096 42 43 #ifdef USE_LONG_WORDS 44 extern char *words_long[]; 45 extern int words_long_n; 46 #endif 47 extern char *words_short[]; 48 extern int words_short_n; 49 char **words = words_short; 50 int *words_n = &words_short_n; 51 52 enum mode { 53 MODE_NONE, 54 MODE_PASS10, /*0123456789*/ 55 MODE_PASS26, /*ABCDEFGHIJKLMNOPQRSTUVWXYZ*/ 56 MODE_PASS36, /*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*/ 57 MODE_PASS62, /*all alphanumeric*/ 58 MODE_PASS94, /*all printable except space*/ 59 MODE_PASSPP /*the quick fox jumped over the lazy dog*/ 60 }; 61 62 63 static void usage(char *name) 64 { 65 fatal("%s [-123456eh] [-l <length>] [-a <times>]\n" 66 "flags 123456 denote increasingly complex password atoms\n" 67 "l[en] specifies the length of each password in atoms\n" 68 "a[mount] specifies the amount of passwords to generate\n" 69 #ifdef USE_LONG_WORDS 70 "e[xtra] uses the longer word database in mode 6\n" 71 #endif 72 "h[elp] prints this help\n", name); 73 } 74 75 76 static void gen_atom(int m) 77 { 78 #define RNUM (int)randrange(48, 57 ) 79 #define RALU (int)randrange(65, 90 ) 80 #define RALL (int)randrange(97, 122) 81 #define RPRI (int)randrange(33, 126) 82 switch (m) 83 { 84 default: fatal("no mode specified (see -h)\n"); break; 85 case MODE_PASS10: ppick_r(1, RNUM ); break; 86 case MODE_PASS26: ppick_r(1, RALU ); break; 87 case MODE_PASS36: ppick_r(2, RNUM, RALU ); break; 88 case MODE_PASS62: ppick_r(3, RNUM, RALU, RALL); break; 89 case MODE_PASS94: ppick_r(1, RPRI ); break; 90 case MODE_PASSPP: printf("%s", 91 words[randrange(0, *words_n-1)]); break; 92 } 93 } 94 95 96 int main(int argc, char **argv) 97 { 98 int mode = MODE_NONE, length = 12, times = 1, longw = 0, i; 99 float entropy; 100 101 ARG_LOOP 102 { 103 ARG_TYPC; 104 case '1': ARG_FLG(mode, MODE_PASS10); break; 105 case '2': ARG_FLG(mode, MODE_PASS26); break; 106 case '3': ARG_FLG(mode, MODE_PASS36); break; 107 case '4': ARG_FLG(mode, MODE_PASS62); break; 108 case '5': ARG_FLG(mode, MODE_PASS94); break; 109 case '6': ARG_FLG(mode, MODE_PASSPP); break; 110 #ifdef USE_LONG_WORDS 111 case 'e': ARG_FLG(longw, 1); break; 112 #endif 113 case 'l': ARG_INT(length); break; 114 case 'a': ARG_INT(times ); break; 115 } 116 ARG_END; 117 118 #ifdef USE_LONG_WORDS 119 if (longw) 120 { 121 words = words_long; 122 words_n = &words_long_n; 123 } 124 #endif 125 126 if (mode == MODE_PASSPP) 127 { 128 entropy = log2f(*words_n); 129 fprintf(stderr, 130 "INFO: words %d, entropy %0.1f bits per word\n" 131 "INFO: you will need ~%d words for minimum secure" 132 "entropy\n", 133 *words_n, entropy, (int)ceilf(72.0 / entropy)); 134 } 135 136 while (times-- > 0) 137 { 138 for (i = 0; i < length; i++) 139 { 140 gen_atom(mode); 141 if (mode == MODE_PASSPP && length > 0) 142 printf(" "); 143 } 144 printf("\n"); 145 } 146 147 return 0; 148 }
/*