annotate dbrandom.c @ 1348:5c2899e35b63 fuzz

fuzz harness
author Matt Johnston <matt@ucc.asn.au>
date Sat, 13 May 2017 22:50:54 +0800
parents b28624698130
children 3fdd8c5a0195
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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"
858
220f55d540ae rename random.h to dbrandom.h since some OSes have a system random.h
Matt Johnston <matt@ucc.asn.au>
parents: 857
diff changeset
29 #include "dbrandom.h"
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
30 #include "runopts.h"
1348
5c2899e35b63 fuzz harness
Matt Johnston <matt@ucc.asn.au>
parents: 1347
diff changeset
31 #include "fuzz.h"
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 /* 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
35 static uint32_t counter = 0;
298
7dad470ad4aa minor cleanups for some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 272
diff changeset
36 /* the max value for the counter, so it won't integer overflow */
1274
9da3e7b4fe55 add parentheses to macro
Francois Perrad <francois.perrad@gadz.org>
parents: 1096
diff changeset
37 #define MAX_COUNTER (1<<30)
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
39 static unsigned char hashpool[SHA1_HASH_SIZE] = {0};
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
40 static int donerandinit = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #define INIT_SEED_SIZE 32 /* 256 bits */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
149
ed9ca2a9705c Cleaned up the random code, use /dev/random by default,
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
44 /* 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
45 * 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
46 * 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
47 * data into the pool.
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 * 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
50 * feed in new entropy.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 *
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
54 /* Pass len=0 to hash an entire file */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
55 static int
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
56 process_file(hash_state *hs, const char *filename,
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
57 unsigned int len, int prngd)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
58 {
185
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
59 static int already_blocked = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 int readfd;
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
61 unsigned int readcount;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
62 int ret = DROPBEAR_FAILURE;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
149
ed9ca2a9705c Cleaned up the random code, use /dev/random by default,
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
64 #ifdef DROPBEAR_PRNGD_SOCKET
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
65 if (prngd)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
66 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
67 readfd = connect_unix(filename);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
68 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
69 else
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
70 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
71 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
72 readfd = open(filename, O_RDONLY);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
75 if (readfd < 0) {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
76 goto out;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
77 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
79 readcount = 0;
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
80 while (len == 0 || readcount < len)
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
81 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
82 int readlen, wantread;
811
5de524aec047 limit how much we read from rt_cache etc
Matt Johnston <matt@ucc.asn.au>
parents: 737
diff changeset
83 unsigned char readbuf[4096];
855
04ede40a529a - Some fixes for old compilers like tru64 v4 from Daniel Richard G.
Matt Johnston <matt@ucc.asn.au>
parents: 843
diff changeset
84 if (!already_blocked && !prngd)
185
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
85 {
843
3a9ec98808c3 Fix shadowed "ret" variable
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
86 int res;
855
04ede40a529a - Some fixes for old compilers like tru64 v4 from Daniel Richard G.
Matt Johnston <matt@ucc.asn.au>
parents: 843
diff changeset
87 struct timeval timeout;
185
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
88 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
89
855
04ede40a529a - Some fixes for old compilers like tru64 v4 from Daniel Richard G.
Matt Johnston <matt@ucc.asn.au>
parents: 843
diff changeset
90 timeout.tv_sec = 2;
04ede40a529a - Some fixes for old compilers like tru64 v4 from Daniel Richard G.
Matt Johnston <matt@ucc.asn.au>
parents: 843
diff changeset
91 timeout.tv_usec = 0;
04ede40a529a - Some fixes for old compilers like tru64 v4 from Daniel Richard G.
Matt Johnston <matt@ucc.asn.au>
parents: 843
diff changeset
92
185
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
93 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
94 FD_SET(readfd, &read_fds);
843
3a9ec98808c3 Fix shadowed "ret" variable
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
95 res = select(readfd + 1, &read_fds, NULL, NULL, &timeout);
3a9ec98808c3 Fix shadowed "ret" variable
Matt Johnston <matt@ucc.asn.au>
parents: 835
diff changeset
96 if (res == 0)
185
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
97 {
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
98 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
99 already_blocked = 1;
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
100 }
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
101 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
102
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
103 if (len == 0)
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
104 {
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
105 wantread = sizeof(readbuf);
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
106 }
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
107 else
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
108 {
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
109 wantread = MIN(sizeof(readbuf), len-readcount);
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
110 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
111
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
112 #ifdef DROPBEAR_PRNGD_SOCKET
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
113 if (prngd)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
114 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
115 char egdcmd[2];
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
116 egdcmd[0] = 0x02; /* blocking read */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
117 egdcmd[1] = (unsigned char)wantread;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
118 if (write(readfd, egdcmd, 2) < 0)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
119 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
120 dropbear_exit("Can't send command to egd");
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
121 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
122 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
123 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
124
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
125 readlen = read(readfd, readbuf, wantread);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 if (readlen <= 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 if (readlen < 0 && errno == EINTR) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 continue;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
130 if (readlen == 0 && len == 0)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
131 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
132 /* whole file was read as requested */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
133 break;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
134 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
135 goto out;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
137 sha1_process(hs, readbuf, readlen);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
138 readcount += readlen;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
139 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
140 ret = DROPBEAR_SUCCESS;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
141 out:
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
142 close(readfd);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
143 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
1096
a9e4d66ffb2c Turn addrandom()'s buf argument into unsigned char *
Gaël PORTAY <gael.portay@gmail.com>
parents: 1013
diff changeset
146 void addrandom(unsigned char * buf, unsigned int len)
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
147 {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148 hash_state hs;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
150 #ifdef DROPBEAR_FUZZ
1348
5c2899e35b63 fuzz harness
Matt Johnston <matt@ucc.asn.au>
parents: 1347
diff changeset
151 if (fuzz.fuzzing || fuzz.recordf) {
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
152 return;
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
153 }
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
154 #endif
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
155
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 /* hash in the new seed data */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 sha1_init(&hs);
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
158 /* existing state (zeroes on startup) */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
160
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
161 /* new */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
162 sha1_process(&hs, buf, len);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
163 sha1_done(&hs, hashpool);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
164 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
165
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
166 static void write_urandom()
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
167 {
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
168 #ifdef DROPBEAR_FUZZ
1348
5c2899e35b63 fuzz harness
Matt Johnston <matt@ucc.asn.au>
parents: 1347
diff changeset
169 if (fuzz.fuzzing || fuzz.recordf) {
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
170 return;
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
171 }
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
172 #endif
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
173 #ifndef DROPBEAR_PRNGD_SOCKET
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
174 /* This is opportunistic, don't worry about failure */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
175 unsigned char buf[INIT_SEED_SIZE];
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
176 FILE *f = fopen(DROPBEAR_URANDOM_DEV, "w");
737
7deb6fda1319 fix tabs
Matt Johnston <matt@ucc.asn.au>
parents: 735
diff changeset
177 if (!f) {
7deb6fda1319 fix tabs
Matt Johnston <matt@ucc.asn.au>
parents: 735
diff changeset
178 return;
7deb6fda1319 fix tabs
Matt Johnston <matt@ucc.asn.au>
parents: 735
diff changeset
179 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
180 genrandom(buf, sizeof(buf));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
181 fwrite(buf, sizeof(buf), 1, f);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
182 fclose(f);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
183 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
184 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
185
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
186 static void seedfuzz(void) {
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
187 hash_state hs;
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
188 sha1_init(&hs);
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
189 sha1_process(&hs, "fuzzfuzzfuzz", strlen("fuzzfuzzfuzz"));
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
190 sha1_done(&hs, hashpool);
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
191
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
192 counter = 0;
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
193 donerandinit = 1;
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
194 }
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
195
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
196 /* Initialise the prng from /dev/urandom or prngd. This function can
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
197 * be called multiple times */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
198 void seedrandom() {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
199
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
200 hash_state hs;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
201
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
202 pid_t pid;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
203 struct timeval tv;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
204 clock_t clockval;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
205
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
206 #ifdef DROPBEAR_FUZZ
1348
5c2899e35b63 fuzz harness
Matt Johnston <matt@ucc.asn.au>
parents: 1347
diff changeset
207 if (fuzz.fuzzing || fuzz.recordf) {
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
208 seedfuzz();
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
209 return;
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
210 }
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
211 #endif
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
212
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
213 /* hash in the new seed data */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
214 sha1_init(&hs);
1347
b28624698130 copy over some fuzzing code from AFL branch
Matt Johnston <matt@ucc.asn.au>
parents: 1274
diff changeset
215
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
216 /* existing state */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
217 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
218
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
219 #ifdef DROPBEAR_PRNGD_SOCKET
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
220 if (process_file(&hs, DROPBEAR_PRNGD_SOCKET, INIT_SEED_SIZE, 1)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
221 != DROPBEAR_SUCCESS) {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
222 dropbear_exit("Failure reading random device %s",
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
223 DROPBEAR_PRNGD_SOCKET);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
224 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
225 #else
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
226 /* non-blocking random source (probably /dev/urandom) */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
227 if (process_file(&hs, DROPBEAR_URANDOM_DEV, INIT_SEED_SIZE, 0)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
228 != DROPBEAR_SUCCESS) {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
229 dropbear_exit("Failure reading random device %s",
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
230 DROPBEAR_URANDOM_DEV);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
231 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
232 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
233
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
234 /* A few other sources to fall back on.
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
235 * Add more here for other platforms */
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
236 #ifdef __linux__
688
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
237 /* Seems to be a reasonable source of entropy from timers. Possibly hard
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
238 * for even local attackers to reproduce */
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
239 process_file(&hs, "/proc/timer_list", 0, 0);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
240 /* Might help on systems with wireless */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
241 process_file(&hs, "/proc/interrupts", 0, 0);
688
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
242
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
243 process_file(&hs, "/proc/loadavg", 0, 0);
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
244 process_file(&hs, "/proc/sys/kernel/random/entropy_avail", 0, 0);
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
245
811
5de524aec047 limit how much we read from rt_cache etc
Matt Johnston <matt@ucc.asn.au>
parents: 737
diff changeset
246 /* Mostly network visible but useful in some situations.
5de524aec047 limit how much we read from rt_cache etc
Matt Johnston <matt@ucc.asn.au>
parents: 737
diff changeset
247 * Limit size to avoid slowdowns on systems with lots of routes */
5de524aec047 limit how much we read from rt_cache etc
Matt Johnston <matt@ucc.asn.au>
parents: 737
diff changeset
248 process_file(&hs, "/proc/net/netstat", 4096, 0);
5de524aec047 limit how much we read from rt_cache etc
Matt Johnston <matt@ucc.asn.au>
parents: 737
diff changeset
249 process_file(&hs, "/proc/net/dev", 4096, 0);
5de524aec047 limit how much we read from rt_cache etc
Matt Johnston <matt@ucc.asn.au>
parents: 737
diff changeset
250 process_file(&hs, "/proc/net/tcp", 4096, 0);
688
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
251 /* Also includes interface lo */
811
5de524aec047 limit how much we read from rt_cache etc
Matt Johnston <matt@ucc.asn.au>
parents: 737
diff changeset
252 process_file(&hs, "/proc/net/rt_cache", 4096, 0);
702
48e173a0c52b Include /proc/vmstat as another random source
Matt Johnston <matt@ucc.asn.au>
parents: 698
diff changeset
253 process_file(&hs, "/proc/vmstat", 0, 0);
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
254 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
255
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
256 pid = getpid();
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
257 sha1_process(&hs, (void*)&pid, sizeof(pid));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
258
857
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 855
diff changeset
259 /* gettimeofday() doesn't completely fill out struct timeval on
c19acba28590 use oldstyle comments
Matt Johnston <matt@ucc.asn.au>
parents: 855
diff changeset
260 OS X (10.8.3), avoid valgrind warnings by clearing it first */
714
84157e435c52 Make sure "struct timeval" is initialised on OS X to avoid valgrind warnings
Matt Johnston <matt@ucc.asn.au>
parents: 702
diff changeset
261 memset(&tv, 0x0, sizeof(tv));
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
262 gettimeofday(&tv, NULL);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
263 sha1_process(&hs, (void*)&tv, sizeof(tv));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
264
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
265 clockval = clock();
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
266 sha1_process(&hs, (void*)&clockval, sizeof(clockval));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
267
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
268 /* When a private key is read by the client or server it will
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
269 * be added to the hashpool - see runopts.c */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
270
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 sha1_done(&hs, hashpool);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
272
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
273 counter = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 donerandinit = 1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
276 /* Feed it all back into /dev/urandom - this might help if Dropbear
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
277 * is running from inetd and gets new state each time */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
278 write_urandom();
272
3be7ae2e8dfa Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
279 }
3be7ae2e8dfa Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
280
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
281 /* return len bytes of pseudo-random data */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
282 void genrandom(unsigned char* buf, unsigned int len) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
283
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
284 hash_state hs;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
285 unsigned char hash[SHA1_HASH_SIZE];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
286 unsigned int copylen;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
287
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
288 if (!donerandinit) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
289 dropbear_exit("seedrandom not done");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
290 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
291
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
292 while (len > 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
293 sha1_init(&hs);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
294 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
295 sha1_process(&hs, (void*)&counter, sizeof(counter));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
296 sha1_done(&hs, hash);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
297
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
298 counter++;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
299 if (counter > MAX_COUNTER) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
300 seedrandom();
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
301 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
302
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
303 copylen = MIN(len, SHA1_HASH_SIZE);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
304 memcpy(buf, hash, copylen);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
305 len -= copylen;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
306 buf += copylen;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
307 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
308 m_burn(hash, sizeof(hash));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
309 }
188
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
310
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
311 /* 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
312 * 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
313 * 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
314 * 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
315 * */
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
316 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
317
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
318 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
319 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
320 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
321
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
322 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
323
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
324 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
325 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
326 len += 1;
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
327 }
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
328
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
329 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
330 do {
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
331 genrandom(randbuf, len);
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
332 /* 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
333 * MSB first.*/
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
334 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
335
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
336 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
337
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
338 /* 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
339 * 0 < rand < max */
1013
a1e79ffa5862 Tighten validation of DH values. Odds of x==0 being generated are
Matt Johnston <matt@ucc.asn.au>
parents: 858
diff changeset
340 } while (!(mp_cmp(rand, max) == MP_LT && mp_cmp_d(rand, 0) == MP_GT));
188
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
341 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
342 m_free(randbuf);
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
343 }