annotate random.c @ 758:1c607a62d235 ecc

Set LTC_SOURCE for proper ltm_desc etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 29 Mar 2013 00:26:46 +0800
parents b07eb3dc23ec
children 76fba0856749
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"
698
5f2d16d3e598 fix signedness error in prototype
Matt Johnston <matt@ucc.asn.au>
parents: 694
diff changeset
29 #include "random.h"
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 /* 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
32 static uint32_t counter = 0;
298
7dad470ad4aa minor cleanups for some warnings
Matt Johnston <matt@ucc.asn.au>
parents: 272
diff changeset
33 /* 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
34 #define MAX_COUNTER 1<<30
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
36 static unsigned char hashpool[SHA1_HASH_SIZE] = {0};
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
37 static int donerandinit = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
39 int dropbear_ltc_prng = -1;
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
40
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #define INIT_SEED_SIZE 32 /* 256 bits */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
149
ed9ca2a9705c Cleaned up the random code, use /dev/random by default,
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
43 /* 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
44 * 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
45 * 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
46 * data into the pool.
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 * 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
49 * feed in new entropy.
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 *
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
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
53 /* Pass len=0 to hash an entire file */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
54 static int
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
55 process_file(hash_state *hs, const char *filename,
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
56 unsigned int len, int prngd)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
57 {
185
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
58 static int already_blocked = 0;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 int readfd;
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
60 unsigned int readcount;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
61 int ret = DROPBEAR_FAILURE;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
149
ed9ca2a9705c Cleaned up the random code, use /dev/random by default,
Matt Johnston <matt@ucc.asn.au>
parents: 70
diff changeset
63 #ifdef DROPBEAR_PRNGD_SOCKET
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
64 if (prngd)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
65 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
66 readfd = connect_unix(filename);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
67 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
68 else
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
69 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
70 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
71 readfd = open(filename, O_RDONLY);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
74 if (readfd < 0) {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
75 goto out;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
76 }
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
78 readcount = 0;
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
79 while (len == 0 || readcount < len)
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
80 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
81 int readlen, wantread;
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
82 unsigned char readbuf[2048];
185
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
83 if (!already_blocked)
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
84 {
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
85 int ret;
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
86 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
87 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
88
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
89 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
90 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
91 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
92 if (ret == 0)
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
93 {
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
94 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
95 already_blocked = 1;
5ccad7634388 * warn if we seem to be blocking on /dev/random
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
96 }
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
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
99 if (len == 0)
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
100 {
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
101 wantread = sizeof(readbuf);
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
102 }
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
103 else
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 = MIN(sizeof(readbuf), len-readcount);
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
106 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
107
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
108 #ifdef DROPBEAR_PRNGD_SOCKET
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
109 if (prngd)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
110 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
111 char egdcmd[2];
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
112 egdcmd[0] = 0x02; /* blocking read */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
113 egdcmd[1] = (unsigned char)wantread;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
114 if (write(readfd, egdcmd, 2) < 0)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
115 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
116 dropbear_exit("Can't send command to egd");
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
117 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
118 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
119 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
120
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
121 readlen = read(readfd, readbuf, wantread);
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 if (readlen <= 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 if (readlen < 0 && errno == EINTR) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 continue;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
126 if (readlen == 0 && len == 0)
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
127 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
128 /* whole file was read as requested */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
129 break;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
130 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
131 goto out;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 }
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
133 sha1_process(hs, readbuf, readlen);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
134 readcount += readlen;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
135 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
136 ret = DROPBEAR_SUCCESS;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
137 out:
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
138 close(readfd);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
139 return ret;
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141
698
5f2d16d3e598 fix signedness error in prototype
Matt Johnston <matt@ucc.asn.au>
parents: 694
diff changeset
142 void addrandom(char * buf, unsigned int len)
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
143 {
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 hash_state hs;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 /* hash in the new seed data */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 sha1_init(&hs);
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
148 /* existing state (zeroes on startup) */
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
150
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
151 /* new */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
152 sha1_process(&hs, buf, len);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
153 sha1_done(&hs, hashpool);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
154 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
155
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
156 static void write_urandom()
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
157 {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
158 #ifndef DROPBEAR_PRNGD_SOCKET
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
159 /* This is opportunistic, don't worry about failure */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
160 unsigned char buf[INIT_SEED_SIZE];
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
161 FILE *f = fopen(DROPBEAR_URANDOM_DEV, "w");
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
162 genrandom(buf, sizeof(buf));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
163 fwrite(buf, sizeof(buf), 1, f);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
164 fclose(f);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
165 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
166 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
167
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
168 /* 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
169 * be called multiple times */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
170 void seedrandom() {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
171
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
172 hash_state hs;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
173
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
174 pid_t pid;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
175 struct timeval tv;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
176 clock_t clockval;
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
177
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
178 /* hash in the new seed data */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
179 sha1_init(&hs);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
180 /* existing state */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
181 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
182
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
183 #ifdef DROPBEAR_PRNGD_SOCKET
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
184 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
185 != DROPBEAR_SUCCESS) {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
186 dropbear_exit("Failure reading random device %s",
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
187 DROPBEAR_PRNGD_SOCKET);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
188 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
189 #else
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
190 /* non-blocking random source (probably /dev/urandom) */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
191 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
192 != DROPBEAR_SUCCESS) {
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
193 dropbear_exit("Failure reading random device %s",
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
194 DROPBEAR_URANDOM_DEV);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
195 }
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
196 #endif
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
197
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
198 /* 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
199 * Add more here for other platforms */
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
200 #ifdef __linux__
688
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
201 /* 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
202 * for even local attackers to reproduce */
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
203 process_file(&hs, "/proc/timer_list", 0, 0);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
204 /* Might help on systems with wireless */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
205 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
206
694
c85bb68e1db6 add loadavg and entropy_avail as sources
Matt Johnston <matt@ucc.asn.au>
parents: 689
diff changeset
207 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
208 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
209
688
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
210 /* Mostly network visible but useful in some situations */
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
211 process_file(&hs, "/proc/net/netstat", 0, 0);
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
212 process_file(&hs, "/proc/net/dev", 0, 0);
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
213 process_file(&hs, "/proc/net/tcp", 0, 0);
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
214 /* Also includes interface lo */
650c41a4909a Add a few more files in /proc for Linux
Matt Johnston <matt@ucc.asn.au>
parents: 687
diff changeset
215 process_file(&hs, "/proc/net/rt_cache", 0, 0);
702
48e173a0c52b Include /proc/vmstat as another random source
Matt Johnston <matt@ucc.asn.au>
parents: 698
diff changeset
216 process_file(&hs, "/proc/vmstat", 0, 0);
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
217 #endif
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 pid = getpid();
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
220 sha1_process(&hs, (void*)&pid, sizeof(pid));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
221
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
222 // gettimeofday() doesn't completely fill out struct timeval on
84157e435c52 Make sure "struct timeval" is initialised on OS X to avoid valgrind warnings
Matt Johnston <matt@ucc.asn.au>
parents: 702
diff changeset
223 // OS X (10.8.3), avoid valgrind warnings by clearing it first
84157e435c52 Make sure "struct timeval" is initialised on OS X to avoid valgrind warnings
Matt Johnston <matt@ucc.asn.au>
parents: 702
diff changeset
224 memset(&tv, 0x0, sizeof(tv));
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
225 gettimeofday(&tv, NULL);
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
226 sha1_process(&hs, (void*)&tv, sizeof(tv));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
227
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
228 clockval = clock();
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
229 sha1_process(&hs, (void*)&clockval, sizeof(clockval));
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
230
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
231 /* 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
232 * be added to the hashpool - see runopts.c */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
233
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
234 sha1_done(&hs, hashpool);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
235
755
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
236 #ifdef DROPBEAR_LTC_PRNG
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
237 if (dropbear_ltc_prng == -1) {
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
238 dropbear_ltc_prng = register_prng(&dropbear_prng_desc);
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
239 dropbear_assert(dropbear_ltc_prng != -1);
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
240 }
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
241 #endif
b07eb3dc23ec refactor kexdh code a bit, start working on ecdh etc
Matt Johnston <matt@ucc.asn.au>
parents: 714
diff changeset
242
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
243 counter = 0;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
244 donerandinit = 1;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
245
687
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
246 /* 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
247 * is running from inetd and gets new state each time */
167fdc091c05 Improve RNG seeding.
Matt Johnston <matt@ucc.asn.au>
parents: 602
diff changeset
248 write_urandom();
272
3be7ae2e8dfa Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
249 }
3be7ae2e8dfa Only read /dev/random once when the program starts
Matt Johnston <matt@ucc.asn.au>
parents: 198
diff changeset
250
4
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
251 /* return len bytes of pseudo-random data */
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
252 void genrandom(unsigned char* buf, unsigned int len) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
253
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
254 hash_state hs;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
255 unsigned char hash[SHA1_HASH_SIZE];
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
256 unsigned int copylen;
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 if (!donerandinit) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
259 dropbear_exit("seedrandom not done");
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
260 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
261
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
262 while (len > 0) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
263 sha1_init(&hs);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
264 sha1_process(&hs, (void*)hashpool, sizeof(hashpool));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
265 sha1_process(&hs, (void*)&counter, sizeof(counter));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
266 sha1_done(&hs, hash);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
267
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
268 counter++;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
269 if (counter > MAX_COUNTER) {
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
270 seedrandom();
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
271 }
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 copylen = MIN(len, SHA1_HASH_SIZE);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
274 memcpy(buf, hash, copylen);
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
275 len -= copylen;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
276 buf += copylen;
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
277 }
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
278 m_burn(hash, sizeof(hash));
fe6bca95afa7 Makefile.in contains updated files required
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
279 }
188
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
280
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
281 /* 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
282 * 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
283 * 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
284 * 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
285 * */
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
286 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
287
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
288 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
289 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
290 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
291
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
292 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
293
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
294 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
295 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
296 len += 1;
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
297 }
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
298
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
299 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
300 do {
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
301 genrandom(randbuf, len);
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
302 /* 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
303 * MSB first.*/
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
304 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
305
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
306 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
307
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
308 /* 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
309 * 0 < rand < max */
405
00703f1df67a Remove extraneous tests in random mpint generation,
Matt Johnston <matt@ucc.asn.au>
parents: 311
diff changeset
310 } 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
311 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
312 m_free(randbuf);
c9483550701b - refactored random mp_int generation and byte->mp_int code
Matt Johnston <matt@ucc.asn.au>
parents: 159
diff changeset
313 }