You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

random_nz.c 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * Random non-zero bytes
  27. *
  28. * The RSA algorithm requires the generation of random non-zero bytes,
  29. * i.e. bytes in the range [0x01,0xff].
  30. *
  31. * This algorithm is designed to comply with ANS X9.82 Part 1-2006
  32. * Section 9.2.1. This standard is not freely available, but most of
  33. * the text appears to be shared with NIST SP 800-90, which can be
  34. * downloaded from
  35. *
  36. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  37. *
  38. * Where possible, references are given to both documents. In the
  39. * case of any disagreement, ANS X9.82 takes priority over NIST SP
  40. * 800-90. (In particular, note that some algorithms that are
  41. * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
  42. */
  43. #include <stddef.h>
  44. #include <stdint.h>
  45. #include <ipxe/rbg.h>
  46. #include <ipxe/random_nz.h>
  47. /**
  48. * Get random non-zero bytes
  49. *
  50. * @v data Output buffer
  51. * @v len Length of output buffer
  52. * @ret rc Return status code
  53. *
  54. * This algorithm is designed to be isomorphic to the Simple Discard
  55. * Method described in ANS X9.82 Part 1-2006 Section 9.2.1 (NIST SP
  56. * 800-90 Section B.5.1.1).
  57. */
  58. int get_random_nz ( void *data, size_t len ) {
  59. uint8_t *bytes = data;
  60. int rc;
  61. while ( len ) {
  62. /* Generate random byte */
  63. if ( ( rc = rbg_generate ( NULL, 0, 0, bytes, 1 ) ) != 0 )
  64. return rc;
  65. /* Move to next byte if this byte is acceptable */
  66. if ( *bytes != 0 ) {
  67. bytes++;
  68. len--;
  69. }
  70. }
  71. return 0;
  72. }