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.2KB

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