Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / bignum-random.c
blobf305f0404d6696b6c7ded05ca9bcaf91aeefd9f6
1 /* bignum-random.c
3 * Generating big random numbers
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 Niels Möller
9 *
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <stdlib.h>
32 #include "bignum.h"
33 #include "nettle-internal.h"
35 void
36 nettle_mpz_random_size(mpz_t x,
37 void *ctx, nettle_random_func *random,
38 unsigned bits)
40 unsigned length = (bits + 7) / 8;
41 TMP_DECL(data, uint8_t, NETTLE_MAX_BIGNUM_SIZE);
42 TMP_ALLOC(data, length);
44 random(ctx, length, data);
46 nettle_mpz_set_str_256_u(x, length, data);
48 if (bits % 8)
49 mpz_fdiv_r_2exp(x, x, bits);
52 /* Returns a random number x, 0 <= x < n */
53 void
54 nettle_mpz_random(mpz_t x,
55 void *ctx, nettle_random_func *random,
56 const mpz_t n)
58 /* NOTE: This leaves some bias, which may be bad for DSA. A better
59 * way might be to generate a random number of mpz_sizeinbase(n, 2)
60 * bits, and loop until one smaller than n is found. */
62 /* From Daniel Bleichenbacher (via coderpunks):
64 * There is still a theoretical attack possible with 8 extra bits.
65 * But, the attack would need about 2^66 signatures 2^66 memory and
66 * 2^66 time (if I remember that correctly). Compare that to DSA,
67 * where the attack requires 2^22 signatures 2^40 memory and 2^64
68 * time. And of course, the numbers above are not a real threat for
69 * PGP. Using 16 extra bits (i.e. generating a 176 bit random number
70 * and reducing it modulo q) will defeat even this theoretical
71 * attack.
73 * More generally log_2(q)/8 extra bits are enough to defeat my
74 * attack. NIST also plans to update the standard.
77 /* Add a few bits extra, to decrease the bias from the final modulo
78 * operation. NIST FIPS 186-3 specifies 64 extra bits, for use with
79 * DSA. */
81 nettle_mpz_random_size(x,
82 ctx, random,
83 mpz_sizeinbase(n, 2) + 64);
85 mpz_fdiv_r(x, x, n);