nettle  3.7.3
About: Nettle is a low-level cryptographic library.
  Fossies Dox: nettle-3.7.3.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

bignum-random.c
Go to the documentation of this file.
1 /* bignum-random.c
2 
3  Generating big random numbers
4 
5  Copyright (C) 2002, 2013 Niels Möller
6 
7  This file is part of GNU Nettle.
8 
9  GNU Nettle is free software: you can redistribute it and/or
10  modify it under the terms of either:
11 
12  * the GNU Lesser General Public License as published by the Free
13  Software Foundation; either version 3 of the License, or (at your
14  option) any later version.
15 
16  or
17 
18  * the GNU General Public License as published by the Free
19  Software Foundation; either version 2 of the License, or (at your
20  option) any later version.
21 
22  or both in parallel, as here.
23 
24  GNU Nettle is distributed in the hope that it will be useful,
25  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27  General Public License for more details.
28 
29  You should have received copies of the GNU General Public License and
30  the GNU Lesser General Public License along with this program. If
31  not, see http://www.gnu.org/licenses/.
32 */
33 
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37 
38 #include <stdlib.h>
39 
40 #include "bignum.h"
41 #include "gmp-glue.h"
42 
43 void
45  void *ctx, nettle_random_func *random,
46  unsigned bits)
47 {
48  unsigned length = (bits + 7) / 8;
49  TMP_GMP_DECL(data, uint8_t);
50 
51  TMP_GMP_ALLOC(data, length);
52 
53  random(ctx, length, data);
54  nettle_mpz_set_str_256_u(x, length, data);
55 
56  if (bits % 8)
57  mpz_fdiv_r_2exp(x, x, bits);
58 
59  TMP_GMP_FREE(data);
60 }
61 
62 /* Returns a random number x, 0 <= x < n */
63 void
65  void *ctx, nettle_random_func *random,
66  const mpz_t n)
67 {
68  /* NOTE: This leaves some bias, which may be bad for DSA. A better
69  * way might be to generate a random number of mpz_sizeinbase(n, 2)
70  * bits, and loop until one smaller than n is found. */
71 
72  /* From Daniel Bleichenbacher (via coderpunks):
73  *
74  * There is still a theoretical attack possible with 8 extra bits.
75  * But, the attack would need about 2^66 signatures 2^66 memory and
76  * 2^66 time (if I remember that correctly). Compare that to DSA,
77  * where the attack requires 2^22 signatures 2^40 memory and 2^64
78  * time. And of course, the numbers above are not a real threat for
79  * PGP. Using 16 extra bits (i.e. generating a 176 bit random number
80  * and reducing it modulo q) will defeat even this theoretical
81  * attack.
82  *
83  * More generally log_2(q)/8 extra bits are enough to defeat my
84  * attack. NIST also plans to update the standard.
85  */
86 
87  /* Add a few bits extra, to decrease the bias from the final modulo
88  * operation. NIST FIPS 186-3 specifies 64 extra bits, for use with
89  * DSA. */
90 
92  ctx, random,
93  mpz_sizeinbase(n, 2) + 64);
94 
95  mpz_fdiv_r(x, x, n);
96 }
void nettle_mpz_random(mpz_t x, void *ctx, nettle_random_func *random, const mpz_t n)
Definition: bignum-random.c:64
void nettle_mpz_random_size(mpz_t x, void *ctx, nettle_random_func *random, unsigned bits)
Definition: bignum-random.c:44
void nettle_mpz_set_str_256_u(mpz_t x, size_t length, const uint8_t *s)
Definition: bignum.c:143
#define x
#define TMP_GMP_DECL(name, type)
Definition: gmp-glue.h:51
#define TMP_GMP_FREE(name)
Definition: gmp-glue.h:57
#define TMP_GMP_ALLOC(name, size)
Definition: gmp-glue.h:53
size_t mpz_sizeinbase(const mpz_t u, int base)
Definition: mini-gmp.c:4167
void mpz_fdiv_r(mpz_t r, const mpz_t n, const mpz_t d)
Definition: mini-gmp.c:2329
void mpz_fdiv_r_2exp(mpz_t r, const mpz_t u, mp_bitcnt_t cnt)
Definition: mini-gmp.c:2495
__mpz_struct mpz_t[1]
Definition: mini-gmp.h:77
void nettle_random_func(void *ctx, size_t length, uint8_t *dst)
Definition: nettle-types.h:75