1 | /*
|
---|
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <time.h>
|
---|
12 | #include "internal/cryptlib.h"
|
---|
13 | #include "bn_lcl.h"
|
---|
14 | #include <openssl/rand.h>
|
---|
15 | #include <openssl/sha.h>
|
---|
16 |
|
---|
17 | static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
|
---|
18 | {
|
---|
19 | unsigned char *buf = NULL;
|
---|
20 | int ret = 0, bit, bytes, mask;
|
---|
21 | time_t tim;
|
---|
22 |
|
---|
23 | if (bits == 0) {
|
---|
24 | if (top != BN_RAND_TOP_ANY || bottom != BN_RAND_BOTTOM_ANY)
|
---|
25 | goto toosmall;
|
---|
26 | BN_zero(rnd);
|
---|
27 | return 1;
|
---|
28 | }
|
---|
29 | if (bits < 0 || (bits == 1 && top > 0))
|
---|
30 | goto toosmall;
|
---|
31 |
|
---|
32 | bytes = (bits + 7) / 8;
|
---|
33 | bit = (bits - 1) % 8;
|
---|
34 | mask = 0xff << (bit + 1);
|
---|
35 |
|
---|
36 | buf = OPENSSL_malloc(bytes);
|
---|
37 | if (buf == NULL) {
|
---|
38 | BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
|
---|
39 | goto err;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /* make a random number and set the top and bottom bits */
|
---|
43 | time(&tim);
|
---|
44 | RAND_add(&tim, sizeof(tim), 0.0);
|
---|
45 |
|
---|
46 | if (RAND_bytes(buf, bytes) <= 0)
|
---|
47 | goto err;
|
---|
48 |
|
---|
49 | if (pseudorand == 2) {
|
---|
50 | /*
|
---|
51 | * generate patterns that are more likely to trigger BN library bugs
|
---|
52 | */
|
---|
53 | int i;
|
---|
54 | unsigned char c;
|
---|
55 |
|
---|
56 | for (i = 0; i < bytes; i++) {
|
---|
57 | if (RAND_bytes(&c, 1) <= 0)
|
---|
58 | goto err;
|
---|
59 | if (c >= 128 && i > 0)
|
---|
60 | buf[i] = buf[i - 1];
|
---|
61 | else if (c < 42)
|
---|
62 | buf[i] = 0;
|
---|
63 | else if (c < 84)
|
---|
64 | buf[i] = 255;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (top >= 0) {
|
---|
69 | if (top) {
|
---|
70 | if (bit == 0) {
|
---|
71 | buf[0] = 1;
|
---|
72 | buf[1] |= 0x80;
|
---|
73 | } else {
|
---|
74 | buf[0] |= (3 << (bit - 1));
|
---|
75 | }
|
---|
76 | } else {
|
---|
77 | buf[0] |= (1 << bit);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | buf[0] &= ~mask;
|
---|
81 | if (bottom) /* set bottom bit if requested */
|
---|
82 | buf[bytes - 1] |= 1;
|
---|
83 | if (!BN_bin2bn(buf, bytes, rnd))
|
---|
84 | goto err;
|
---|
85 | ret = 1;
|
---|
86 | err:
|
---|
87 | OPENSSL_clear_free(buf, bytes);
|
---|
88 | bn_check_top(rnd);
|
---|
89 | return (ret);
|
---|
90 |
|
---|
91 | toosmall:
|
---|
92 | BNerr(BN_F_BNRAND, BN_R_BITS_TOO_SMALL);
|
---|
93 | return 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | int BN_rand(BIGNUM *rnd, int bits, int top, int bottom)
|
---|
97 | {
|
---|
98 | return bnrand(0, rnd, bits, top, bottom);
|
---|
99 | }
|
---|
100 |
|
---|
101 | int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom)
|
---|
102 | {
|
---|
103 | return bnrand(1, rnd, bits, top, bottom);
|
---|
104 | }
|
---|
105 |
|
---|
106 | int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom)
|
---|
107 | {
|
---|
108 | return bnrand(2, rnd, bits, top, bottom);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /* random number r: 0 <= r < range */
|
---|
112 | static int bn_rand_range(int pseudo, BIGNUM *r, const BIGNUM *range)
|
---|
113 | {
|
---|
114 | int (*bn_rand) (BIGNUM *, int, int, int) =
|
---|
115 | pseudo ? BN_pseudo_rand : BN_rand;
|
---|
116 | int n;
|
---|
117 | int count = 100;
|
---|
118 |
|
---|
119 | if (range->neg || BN_is_zero(range)) {
|
---|
120 | BNerr(BN_F_BN_RAND_RANGE, BN_R_INVALID_RANGE);
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | n = BN_num_bits(range); /* n > 0 */
|
---|
125 |
|
---|
126 | /* BN_is_bit_set(range, n - 1) always holds */
|
---|
127 |
|
---|
128 | if (n == 1)
|
---|
129 | BN_zero(r);
|
---|
130 | else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
|
---|
131 | /*
|
---|
132 | * range = 100..._2, so 3*range (= 11..._2) is exactly one bit longer
|
---|
133 | * than range
|
---|
134 | */
|
---|
135 | do {
|
---|
136 | if (!bn_rand(r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
|
---|
137 | return 0;
|
---|
138 | /*
|
---|
139 | * If r < 3*range, use r := r MOD range (which is either r, r -
|
---|
140 | * range, or r - 2*range). Otherwise, iterate once more. Since
|
---|
141 | * 3*range = 11..._2, each iteration succeeds with probability >=
|
---|
142 | * .75.
|
---|
143 | */
|
---|
144 | if (BN_cmp(r, range) >= 0) {
|
---|
145 | if (!BN_sub(r, r, range))
|
---|
146 | return 0;
|
---|
147 | if (BN_cmp(r, range) >= 0)
|
---|
148 | if (!BN_sub(r, r, range))
|
---|
149 | return 0;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (!--count) {
|
---|
153 | BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | }
|
---|
158 | while (BN_cmp(r, range) >= 0);
|
---|
159 | } else {
|
---|
160 | do {
|
---|
161 | /* range = 11..._2 or range = 101..._2 */
|
---|
162 | if (!bn_rand(r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
|
---|
163 | return 0;
|
---|
164 |
|
---|
165 | if (!--count) {
|
---|
166 | BNerr(BN_F_BN_RAND_RANGE, BN_R_TOO_MANY_ITERATIONS);
|
---|
167 | return 0;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | while (BN_cmp(r, range) >= 0);
|
---|
171 | }
|
---|
172 |
|
---|
173 | bn_check_top(r);
|
---|
174 | return 1;
|
---|
175 | }
|
---|
176 |
|
---|
177 | int BN_rand_range(BIGNUM *r, const BIGNUM *range)
|
---|
178 | {
|
---|
179 | return bn_rand_range(0, r, range);
|
---|
180 | }
|
---|
181 |
|
---|
182 | int BN_pseudo_rand_range(BIGNUM *r, const BIGNUM *range)
|
---|
183 | {
|
---|
184 | return bn_rand_range(1, r, range);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * BN_generate_dsa_nonce generates a random number 0 <= out < range. Unlike
|
---|
189 | * BN_rand_range, it also includes the contents of |priv| and |message| in
|
---|
190 | * the generation so that an RNG failure isn't fatal as long as |priv|
|
---|
191 | * remains secret. This is intended for use in DSA and ECDSA where an RNG
|
---|
192 | * weakness leads directly to private key exposure unless this function is
|
---|
193 | * used.
|
---|
194 | */
|
---|
195 | int BN_generate_dsa_nonce(BIGNUM *out, const BIGNUM *range,
|
---|
196 | const BIGNUM *priv, const unsigned char *message,
|
---|
197 | size_t message_len, BN_CTX *ctx)
|
---|
198 | {
|
---|
199 | SHA512_CTX sha;
|
---|
200 | /*
|
---|
201 | * We use 512 bits of random data per iteration to ensure that we have at
|
---|
202 | * least |range| bits of randomness.
|
---|
203 | */
|
---|
204 | unsigned char random_bytes[64];
|
---|
205 | unsigned char digest[SHA512_DIGEST_LENGTH];
|
---|
206 | unsigned done, todo;
|
---|
207 | /* We generate |range|+8 bytes of random output. */
|
---|
208 | const unsigned num_k_bytes = BN_num_bytes(range) + 8;
|
---|
209 | unsigned char private_bytes[96];
|
---|
210 | unsigned char *k_bytes;
|
---|
211 | int ret = 0;
|
---|
212 |
|
---|
213 | k_bytes = OPENSSL_malloc(num_k_bytes);
|
---|
214 | if (k_bytes == NULL)
|
---|
215 | goto err;
|
---|
216 |
|
---|
217 | /* We copy |priv| into a local buffer to avoid exposing its length. */
|
---|
218 | todo = sizeof(priv->d[0]) * priv->top;
|
---|
219 | if (todo > sizeof(private_bytes)) {
|
---|
220 | /*
|
---|
221 | * No reasonable DSA or ECDSA key should have a private key this
|
---|
222 | * large and we don't handle this case in order to avoid leaking the
|
---|
223 | * length of the private key.
|
---|
224 | */
|
---|
225 | BNerr(BN_F_BN_GENERATE_DSA_NONCE, BN_R_PRIVATE_KEY_TOO_LARGE);
|
---|
226 | goto err;
|
---|
227 | }
|
---|
228 | memcpy(private_bytes, priv->d, todo);
|
---|
229 | memset(private_bytes + todo, 0, sizeof(private_bytes) - todo);
|
---|
230 |
|
---|
231 | for (done = 0; done < num_k_bytes;) {
|
---|
232 | if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1)
|
---|
233 | goto err;
|
---|
234 | SHA512_Init(&sha);
|
---|
235 | SHA512_Update(&sha, &done, sizeof(done));
|
---|
236 | SHA512_Update(&sha, private_bytes, sizeof(private_bytes));
|
---|
237 | SHA512_Update(&sha, message, message_len);
|
---|
238 | SHA512_Update(&sha, random_bytes, sizeof(random_bytes));
|
---|
239 | SHA512_Final(digest, &sha);
|
---|
240 |
|
---|
241 | todo = num_k_bytes - done;
|
---|
242 | if (todo > SHA512_DIGEST_LENGTH)
|
---|
243 | todo = SHA512_DIGEST_LENGTH;
|
---|
244 | memcpy(k_bytes + done, digest, todo);
|
---|
245 | done += todo;
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (!BN_bin2bn(k_bytes, num_k_bytes, out))
|
---|
249 | goto err;
|
---|
250 | if (BN_mod(out, out, range, ctx) != 1)
|
---|
251 | goto err;
|
---|
252 | ret = 1;
|
---|
253 |
|
---|
254 | err:
|
---|
255 | OPENSSL_free(k_bytes);
|
---|
256 | OPENSSL_cleanse(private_bytes, sizeof(private_bytes));
|
---|
257 | return ret;
|
---|
258 | }
|
---|