Debian Codegraph @

src/generate.c

raw
1 /*
2 * libpwquality main API code for password generation
3 *
4 * See the end of the file for Copyright and License Information
5 */
6
7 #include "config.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <ctype.h>
18 #include <limits.h>
19 #ifdef HAVE_CRACK_H
20 #include <crack.h>
21 #endif
22 #include <errno.h>
23
24 #include "pwquality.h"
25 #include "pwqprivate.h"
26
27 #ifndef PATH_DEV_URANDOM
28 #define PATH_DEV_URANDOM "/dev/urandom"
29 #endif
30
31 static char vowels[] = { 'a', '4', 'A', 'e', 'E', '3', 'i', 'I', 'o', 'O',
32 '0', 'u', 'U', 'y', 'Y', '@' }; /* 4 bits */
33 static char consonants1[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
34 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z',
35 'B', 'D', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P',
36 'R', 'S' }; /* 5 bits */
37 static char consonants2[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
38 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'z',
39 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
40 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Z',
41 '1', '2', '5', '6', '7', '8', '9', '!', '#', '$',
42 '%', '^', '&', '*', '(', ')', '-', '+', '=', '[',
43 ']', ';', '.', ',' }; /* 6 bits */
44
45 static int
46 get_entropy_bits(char *buf, int nbits)
47 {
48 int fd;
49 int rv;
50 int offset = 0;
51 int bytes = (nbits + 7) / 8; /* round up on division */
52
53 fd = open(PATH_DEV_URANDOM, O_RDONLY);
54 if (fd == -1)
55 return -1;
56
57 while (bytes > 0) {
58 rv = read(fd, buf + offset, bytes);
59
60 if (rv < 0) {
61 if (errno == EINTR) continue;
62 (void)close(fd);
63 return -1;
64 }
65 if (rv == 0) {
66 (void)close(fd);
67 return -1;
68 }
69
70 offset += rv;
71 bytes -= rv;
72 }
73
74 (void)close(fd);
75 return 0;
76 }
77
78 static unsigned int
79 consume_entropy(char *buf, int bits, int *remaining, int *offset)
80 {
81 unsigned int low = (unsigned char)buf[*offset/8];
82 unsigned int high = 0;
83
84 if (remaining)
85 *remaining -= bits;
86
87 low >>= *offset % 8;
88 low &= (1 << bits) - 1;
89
90 if (8 - *offset % 8 < bits) {
91 high = (unsigned char)buf[*offset/8 + 1];
92 high &= (1 << (bits - (8 - *offset % 8))) - 1;
93 high <<= 8 - *offset % 8;
94 low |= high;
95 }
96
97 *offset += bits;
98 return low;
99 }
100
101 /* generate a random password according to the settings */
102 int
103 pwquality_generate(pwquality_settings_t *pwq, int entropy_bits, char **password)
104 {
105 char entropy[(PWQ_MAX_ENTROPY_BITS+PWQ_MAX_ENTROPY_BITS/9)/8 + 2];
106 char *tmp;
107 int maxlen;
108 int try = 0;
109
110 *password = NULL;
111
112 if (entropy_bits > PWQ_MAX_ENTROPY_BITS)
113 entropy_bits = PWQ_MAX_ENTROPY_BITS;
114
115 if (entropy_bits < PWQ_MIN_ENTROPY_BITS)
116 entropy_bits = PWQ_MIN_ENTROPY_BITS;
117
118 /* overestimate here only 9 bits per syllable of 3 characters */
119 maxlen = (entropy_bits + 8) / 9 * 3 + 1;
120
121 tmp = malloc(maxlen);
122 if (tmp == NULL) {
123 return PWQ_ERROR_MEM_ALLOC;
124 }
125
126 do {
127 int offset = 0;
128 int remaining = entropy_bits;
129 char *ptr;
130
131 memset(tmp, '\0', maxlen);
132 /* read one more byte for rounding overflow during generation
133 and for at most every 9th bit we also drop one bit */
134 if (get_entropy_bits(entropy, entropy_bits +
135 (entropy_bits+8)/9 + 8) < 0) {
136 free(tmp);
137 return PWQ_ERROR_RNG;
138 }
139
140 ptr = tmp;
141 while (remaining > 0) {
142 unsigned int idx;
143
144 if (consume_entropy(entropy, 1, NULL, &offset)) {
145 idx = consume_entropy(entropy, 6, &remaining, &offset);
146 *ptr = consonants2[idx];
147 ++ptr;
148 if (remaining < 0)
149 break;
150 }
151
152 idx = consume_entropy(entropy, 4, &remaining, &offset);
153 *ptr = vowels[idx];
154 ++ptr;
155 if (remaining < 0)
156 break;
157
158 idx = consume_entropy(entropy, 5, &remaining, &offset);
159 *ptr = consonants1[idx];
160 ++ptr;
161 }
162 } while (pwquality_check(pwq, tmp, NULL, NULL, NULL) < 0 &&
163 ++try < PWQ_NUM_GENERATION_TRIES);
164
165 /* clean up */
166 memset(entropy, '\0', sizeof(entropy));
167
168 if (try >= PWQ_NUM_GENERATION_TRIES) {
169 free(tmp);
170 return PWQ_ERROR_GENERATION_FAILED;
171 }
172
173 *password = tmp;
174 tmp = NULL;
175 return 0;
176 }
177
178
179 /*
180 * Copyright (c) Red Hat, Inc, 2011
181 * Copyright (c) Tomas Mraz <[email protected]>, 2011
182 *
183 * Redistribution and use in source and binary forms, with or without
184 * modification, are permitted provided that the following conditions
185 * are met:
186 * 1. Redistributions of source code must retain the above copyright
187 * notice, and the entire permission notice in its entirety,
188 * including the disclaimer of warranties.
189 * 2. Redistributions in binary form must reproduce the above copyright
190 * notice, this list of conditions and the following disclaimer in the
191 * documentation and/or other materials provided with the distribution.
192 * 3. The name of the author may not be used to endorse or promote
193 * products derived from this software without specific prior
194 * written permission.
195 *
196 * ALTERNATIVELY, this product may be distributed under the terms of
197 * the GNU General Public License version 2 or later, in which case the
198 * provisions of the GPL are required INSTEAD OF the above restrictions.
199 *
200 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
201 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
202 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
203 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
204 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
205 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
206 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
207 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
208 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
209 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
210 * OF THE POSSIBILITY OF SUCH DAMAGE.
211 */