Mercurial > dropbear
annotate random.c @ 687:167fdc091c05
Improve RNG seeding.
Try to read from /dev/urandom multiple times, take input from extra sources,
and use /dev/random when generating private keys
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 29 Jun 2012 23:19:43 +0800 |
parents | 2d896267f16d |
children | 650c41a4909a |
rev | line source |
---|---|
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
1 /* |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
2 * Dropbear - a SSH2 server |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
3 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
4 * Copyright (c) 2002,2003 Matt Johnston |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
5 * All rights reserved. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
6 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
7 * Permission is hereby granted, free of charge, to any person obtaining a copy |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
8 * of this software and associated documentation files (the "Software"), to deal |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
9 * in the Software without restriction, including without limitation the rights |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
11 * copies of the Software, and to permit persons to whom the Software is |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
12 * furnished to do so, subject to the following conditions: |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
13 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
14 * The above copyright notice and this permission notice shall be included in |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
15 * all copies or substantial portions of the Software. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
16 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
23 * SOFTWARE. */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
24 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
25 #include "includes.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
26 #include "buffer.h" |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
27 #include "dbutil.h" |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
28 #include "bignum.h" |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
29 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
30 /* this is used to generate unique output from the same hashpool */ |
272
3be7ae2e8dfa
Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
31 static uint32_t counter = 0; |
298
7dad470ad4aa
minor cleanups for some warnings
Matt Johnston <matt@ucc.asn.au>
parents:
272
diff
changeset
|
32 /* the max value for the counter, so it won't integer overflow */ |
7dad470ad4aa
minor cleanups for some warnings
Matt Johnston <matt@ucc.asn.au>
parents:
272
diff
changeset
|
33 #define MAX_COUNTER 1<<30 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
34 |
687 | 35 static unsigned char hashpool[SHA1_HASH_SIZE] = {0}; |
36 static int donerandinit = 0; | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
37 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
38 #define INIT_SEED_SIZE 32 /* 256 bits */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
39 |
149
ed9ca2a9705c
Cleaned up the random code, use /dev/random by default,
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
40 /* The basic setup is we read some data from /dev/(u)random or prngd and hash it |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
41 * into hashpool. To read data, we hash together current hashpool contents, |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
42 * and a counter. We feed more data in by hashing the current pool and new |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
43 * data into the pool. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
44 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
45 * It is important to ensure that counter doesn't wrap around before we |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
46 * feed in new entropy. |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
47 * |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
48 */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
49 |
687 | 50 /* Pass len=0 to hash an entire file */ |
51 static int | |
52 process_file(hash_state *hs, const char *filename, | |
53 unsigned int len, int prngd) | |
54 { | |
185
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
55 static int already_blocked = 0; |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
56 int readfd; |
687 | 57 unsigned int readcount; |
58 int ret = DROPBEAR_FAILURE; | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
59 |
149
ed9ca2a9705c
Cleaned up the random code, use /dev/random by default,
Matt Johnston <matt@ucc.asn.au>
parents:
70
diff
changeset
|
60 #ifdef DROPBEAR_PRNGD_SOCKET |
687 | 61 if (prngd) |
62 { | |
63 readfd = connect_unix(filename); | |
64 } | |
65 else | |
66 #endif | |
67 { | |
68 readfd = open(filename, O_RDONLY); | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
69 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
70 |
687 | 71 if (readfd < 0) { |
72 goto out; | |
73 } | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
74 |
687 | 75 readcount = 0; |
76 while (readcount < len) | |
77 { | |
78 int readlen, wantread; | |
79 unsigned char readbuf[128]; | |
185
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
80 if (!already_blocked) |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
81 { |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
82 int ret; |
687 | 83 struct timeval timeout = { .tv_sec = 2, .tv_usec = 0}; |
185
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
84 fd_set read_fds; |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
85 |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
86 FD_ZERO(&read_fds); |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
87 FD_SET(readfd, &read_fds); |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
88 ret = select(readfd + 1, &read_fds, NULL, NULL, &timeout); |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
89 if (ret == 0) |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
90 { |
687 | 91 dropbear_log(LOG_WARNING, "Warning: Reading the randomness source '%s' seems to have blocked.\nYou may need to find a better entropy source.", filename); |
185
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
92 already_blocked = 1; |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
93 } |
5ccad7634388
* warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
94 } |
687 | 95 |
96 wantread = MIN(sizeof(readbuf), len-readcount); | |
97 | |
98 #ifdef DROPBEAR_PRNGD_SOCKET | |
99 if (prngd) | |
100 { | |
101 char egdcmd[2]; | |
102 egdcmd[0] = 0x02; /* blocking read */ | |
103 egdcmd[1] = (unsigned char)wantread; | |
104 if (write(readfd, egdcmd, 2) < 0) | |
105 { | |
106 dropbear_exit("Can't send command to egd"); | |
107 } | |
108 } | |
109 #endif | |
110 | |
111 readlen = read(readfd, readbuf, wantread); | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
112 if (readlen <= 0) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
113 if (readlen < 0 && errno == EINTR) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
114 continue; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
115 } |
687 | 116 if (readlen == 0 && len == 0) |
117 { | |
118 /* whole file was read as requested */ | |
119 break; | |
120 } | |
121 goto out; | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
122 } |
687 | 123 sha1_process(hs, readbuf, readlen); |
124 readcount += readlen; | |
125 } | |
126 ret = DROPBEAR_SUCCESS; | |
127 out: | |
128 close(readfd); | |
129 return ret; | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
130 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
131 |
687 | 132 void addrandom(char * buf, int len) |
133 { | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
134 hash_state hs; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
135 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
136 /* hash in the new seed data */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
137 sha1_init(&hs); |
687 | 138 /* existing state (zeroes on startup) */ |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
139 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); |
687 | 140 |
141 /* new */ | |
142 sha1_process(&hs, buf, len); | |
143 sha1_done(&hs, hashpool); | |
144 } | |
145 | |
146 static void write_urandom() | |
147 { | |
148 #ifndef DROPBEAR_PRNGD_SOCKET | |
149 /* This is opportunistic, don't worry about failure */ | |
150 unsigned char buf[INIT_SEED_SIZE]; | |
151 FILE *f = fopen(DROPBEAR_URANDOM_DEV, "w"); | |
152 genrandom(buf, sizeof(buf)); | |
153 fwrite(buf, sizeof(buf), 1, f); | |
154 fclose(f); | |
155 #endif | |
156 } | |
157 | |
158 /* add entropy from the stronger, blocking source /dev/random. Only used | |
159 * for generating persistent private keys (RSA and DSS) */ | |
160 void seedstrongrandom() | |
161 { | |
162 /* We assume that PRNGD is a strong source, so don't need to do anything here */ | |
163 #ifndef DROPBEAR_PRNGD_SOCKET | |
164 hash_state hs; | |
165 | |
166 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); | |
167 if (process_file(&hs, "/dev/random", INIT_SEED_SIZE, 0) | |
168 != DROPBEAR_SUCCESS) { | |
169 dropbear_exit("Failure reading random device %s", "/dev/random"); | |
170 } | |
171 | |
172 sha1_done(&hs, hashpool); | |
173 #endif | |
174 } | |
175 | |
176 /* Initialise the prng from /dev/urandom or prngd. This function can | |
177 * be called multiple times */ | |
178 void seedrandom() { | |
179 | |
180 hash_state hs; | |
181 | |
182 pid_t pid; | |
183 struct timeval tv; | |
184 clock_t clockval; | |
185 | |
186 /* hash in the new seed data */ | |
187 sha1_init(&hs); | |
188 /* existing state */ | |
189 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); | |
190 | |
191 #ifdef DROPBEAR_PRNGD_SOCKET | |
192 if (process_file(&hs, DROPBEAR_PRNGD_SOCKET, INIT_SEED_SIZE, 1) | |
193 != DROPBEAR_SUCCESS) { | |
194 dropbear_exit("Failure reading random device %s", | |
195 DROPBEAR_PRNGD_SOCKET); | |
196 } | |
197 #else | |
198 /* non-blocking random source (probably /dev/urandom) */ | |
199 if (process_file(&hs, DROPBEAR_URANDOM_DEV, INIT_SEED_SIZE, 0) | |
200 != DROPBEAR_SUCCESS) { | |
201 dropbear_exit("Failure reading random device %s", | |
202 DROPBEAR_URANDOM_DEV); | |
203 } | |
204 #endif | |
205 | |
206 /* A few other sources to fall back on. Add more here for other platforms */ | |
207 #ifdef __linux__ | |
208 /* Seems to be a reasonable source of entropy from timers */ | |
209 process_file(&hs, "/proc/timer_list", 0, 0); | |
210 /* Might help on systems with wireless */ | |
211 process_file(&hs, "/proc/interrupts", 0, 0); | |
212 #endif | |
213 | |
214 pid = getpid(); | |
215 sha1_process(&hs, (void*)&pid, sizeof(pid)); | |
216 | |
217 gettimeofday(&tv, NULL); | |
218 sha1_process(&hs, (void*)&tv, sizeof(tv)); | |
219 | |
220 clockval = clock(); | |
221 sha1_process(&hs, (void*)&clockval, sizeof(clockval)); | |
222 | |
223 /* When a private key is read by the client or server it will | |
224 * be added to the hashpool - see runopts.c */ | |
225 | |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
226 sha1_done(&hs, hashpool); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
227 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
228 counter = 0; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
229 donerandinit = 1; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
230 |
687 | 231 /* Feed it all back into /dev/urandom - this might help if Dropbear |
232 * is running from inetd and gets new state each time */ | |
233 write_urandom(); | |
272
3be7ae2e8dfa
Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
234 } |
3be7ae2e8dfa
Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents:
198
diff
changeset
|
235 |
4
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
236 /* return len bytes of pseudo-random data */ |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
237 void genrandom(unsigned char* buf, unsigned int len) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
238 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
239 hash_state hs; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
240 unsigned char hash[SHA1_HASH_SIZE]; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
241 unsigned int copylen; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
242 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
243 if (!donerandinit) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
244 dropbear_exit("seedrandom not done"); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
245 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
246 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
247 while (len > 0) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
248 sha1_init(&hs); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
249 sha1_process(&hs, (void*)hashpool, sizeof(hashpool)); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
250 sha1_process(&hs, (void*)&counter, sizeof(counter)); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
251 sha1_done(&hs, hash); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
252 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
253 counter++; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
254 if (counter > MAX_COUNTER) { |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
255 seedrandom(); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
256 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
257 |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
258 copylen = MIN(len, SHA1_HASH_SIZE); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
259 memcpy(buf, hash, copylen); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
260 len -= copylen; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
261 buf += copylen; |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
262 } |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
263 m_burn(hash, sizeof(hash)); |
fe6bca95afa7
Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff
changeset
|
264 } |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
265 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
266 /* Generates a random mp_int. |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
267 * max is a *mp_int specifying an upper bound. |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
268 * rand must be an initialised *mp_int for the result. |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
269 * the result rand satisfies: 0 < rand < max |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
270 * */ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
271 void gen_random_mpint(mp_int *max, mp_int *rand) { |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
272 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
273 unsigned char *randbuf = NULL; |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
274 unsigned int len = 0; |
433
c216212001fc
Fix for -pedantic -ansi compilation, change // to /**/, plus some signedness
Matt Johnston <matt@ucc.asn.au>
parents:
405
diff
changeset
|
275 const unsigned char masks[] = {0xff, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f}; |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
276 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
277 const int size_bits = mp_count_bits(max); |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
278 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
279 len = size_bits / 8; |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
280 if ((size_bits % 8) != 0) { |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
281 len += 1; |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
282 } |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
283 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
284 randbuf = (unsigned char*)m_malloc(len); |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
285 do { |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
286 genrandom(randbuf, len); |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
287 /* Mask out the unrequired bits - mp_read_unsigned_bin expects |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
288 * MSB first.*/ |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
289 randbuf[0] &= masks[size_bits % 8]; |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
290 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
291 bytes_to_mp(rand, randbuf, len); |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
292 |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
293 /* keep regenerating until we get one satisfying |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
294 * 0 < rand < max */ |
405
00703f1df67a
Remove extraneous tests in random mpint generation,
Matt Johnston <matt@ucc.asn.au>
parents:
311
diff
changeset
|
295 } while (mp_cmp(rand, max) != MP_LT); |
188
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
296 m_burn(randbuf, len); |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
297 m_free(randbuf); |
c9483550701b
- refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents:
159
diff
changeset
|
298 } |