2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 * Random non-zero bytes
30 * The RSA algorithm requires the generation of random non-zero bytes,
31 * i.e. bytes in the range [0x01,0xff].
33 * This algorithm is designed to comply with ANS X9.82 Part 1-2006
34 * Section 9.2.1. This standard is not freely available, but most of
35 * the text appears to be shared with NIST SP 800-90, which can be
38 * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
40 * Where possible, references are given to both documents. In the
41 * case of any disagreement, ANS X9.82 takes priority over NIST SP
42 * 800-90. (In particular, note that some algorithms that are
43 * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
49 #include <ipxe/random_nz.h>
52 * Get random non-zero bytes
54 * @v data Output buffer
55 * @v len Length of output buffer
56 * @ret rc Return status code
58 * This algorithm is designed to be isomorphic to the Simple Discard
59 * Method described in ANS X9.82 Part 1-2006 Section 9.2.1 (NIST SP
60 * 800-90 Section B.5.1.1).
62 int get_random_nz ( void *data, size_t len ) {
63 uint8_t *bytes = data;
68 /* Generate random byte */
69 if ( ( rc = rbg_generate ( NULL, 0, 0, bytes, 1 ) ) != 0 )
72 /* Move to next byte if this byte is acceptable */