iPXE
random_nz.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * Random non-zero bytes
00029  *
00030  * The RSA algorithm requires the generation of random non-zero bytes,
00031  * i.e. bytes in the range [0x01,0xff].
00032  *
00033  * This algorithm is designed to comply with ANS X9.82 Part 1-2006
00034  * Section 9.2.1.  This standard is not freely available, but most of
00035  * the text appears to be shared with NIST SP 800-90, which can be
00036  * downloaded from
00037  *
00038  *     http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
00039  *
00040  * Where possible, references are given to both documents.  In the
00041  * case of any disagreement, ANS X9.82 takes priority over NIST SP
00042  * 800-90.  (In particular, note that some algorithms that are
00043  * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
00044  */
00045 
00046 #include <stddef.h>
00047 #include <stdint.h>
00048 #include <ipxe/rbg.h>
00049 #include <ipxe/random_nz.h>
00050 
00051 /**
00052  * Get random non-zero bytes
00053  *
00054  * @v data              Output buffer
00055  * @v len               Length of output buffer
00056  * @ret rc              Return status code
00057  *
00058  * This algorithm is designed to be isomorphic to the Simple Discard
00059  * Method described in ANS X9.82 Part 1-2006 Section 9.2.1 (NIST SP
00060  * 800-90 Section B.5.1.1).
00061  */
00062 int get_random_nz ( void *data, size_t len ) {
00063         uint8_t *bytes = data;
00064         int rc;
00065 
00066         while ( len ) {
00067 
00068                 /* Generate random byte */
00069                 if ( ( rc = rbg_generate ( NULL, 0, 0, bytes, 1 ) ) != 0 )
00070                         return rc;
00071 
00072                 /* Move to next byte if this byte is acceptable */
00073                 if ( *bytes != 0 ) {
00074                         bytes++;
00075                         len--;
00076                 }
00077         }
00078 
00079         return 0;
00080 }