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.

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