iPXE
random_nz.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 /** @file
28  *
29  * Random non-zero bytes
30  *
31  * The RSA algorithm requires the generation of random non-zero bytes,
32  * i.e. bytes in the range [0x01,0xff].
33  *
34  * This algorithm is designed to comply with ANS X9.82 Part 1-2006
35  * Section 9.2.1. This standard is not freely available, but most of
36  * the text appears to be shared with NIST SP 800-90, which can be
37  * downloaded from
38  *
39  * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
40  *
41  * Where possible, references are given to both documents. In the
42  * case of any disagreement, ANS X9.82 takes priority over NIST SP
43  * 800-90. (In particular, note that some algorithms that are
44  * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
45  */
46 
47 #include <stddef.h>
48 #include <stdint.h>
49 #include <ipxe/rbg.h>
50 #include <ipxe/random_nz.h>
51 
52 /**
53  * Get random non-zero bytes
54  *
55  * @v data Output buffer
56  * @v len Length of output buffer
57  * @ret rc Return status code
58  *
59  * This algorithm is designed to be isomorphic to the Simple Discard
60  * Method described in ANS X9.82 Part 1-2006 Section 9.2.1 (NIST SP
61  * 800-90 Section B.5.1.1).
62  */
63 int get_random_nz ( void *data, size_t len ) {
64  uint8_t *bytes = data;
65  int rc;
66 
67  while ( len ) {
68 
69  /* Generate random byte */
70  if ( ( rc = rbg_generate ( NULL, 0, 0, bytes, 1 ) ) != 0 )
71  return rc;
72 
73  /* Move to next byte if this byte is acceptable */
74  if ( *bytes != 0 ) {
75  bytes++;
76  len--;
77  }
78  }
79 
80  return 0;
81 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int rbg_generate(const void *additional, size_t additional_len, int prediction_resist, void *data, size_t len)
Generate bits using RBG.
Definition: rbg.c:117
int get_random_nz(void *data, size_t len)
Get random non-zero bytes.
Definition: random_nz.c:63
HMAC_DRBG algorithm.
ring len
Length.
Definition: dwmac.h:231
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
RBG mechanism.
unsigned char uint8_t
Definition: stdint.h:10
FILE_SECBOOT(PERMITTED)
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint8_t bytes[64]
Definition: ib_mad.h:17
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322