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 
26 /** @file
27  *
28  * Random non-zero bytes
29  *
30  * The RSA algorithm requires the generation of random non-zero bytes,
31  * i.e. bytes in the range [0x01,0xff].
32  *
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
36  * downloaded from
37  *
38  * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
39  *
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.)
44  */
45 
46 #include <stddef.h>
47 #include <stdint.h>
48 #include <ipxe/rbg.h>
49 #include <ipxe/random_nz.h>
50 
51 /**
52  * Get random non-zero bytes
53  *
54  * @v data Output buffer
55  * @v len Length of output buffer
56  * @ret rc Return status code
57  *
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).
61  */
62 int get_random_nz ( void *data, size_t len ) {
63  uint8_t *bytes = data;
64  int rc;
65 
66  while ( len ) {
67 
68  /* Generate random byte */
69  if ( ( rc = rbg_generate ( NULL, 0, 0, bytes, 1 ) ) != 0 )
70  return rc;
71 
72  /* Move to next byte if this byte is acceptable */
73  if ( *bytes != 0 ) {
74  bytes++;
75  len--;
76  }
77  }
78 
79  return 0;
80 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int rbg_generate(const void *additional, size_t additional_len, int prediction_resist, void *data, size_t len)
Generate bits using RBG.
Definition: rbg.h:36
int get_random_nz(void *data, size_t len)
Get random non-zero bytes.
Definition: random_nz.c:62
HMAC_DRBG algorithm.
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
RBG mechanism.
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
uint8_t bytes[64]
Definition: ib_mad.h:16
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362