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.

hash_df.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * Hash-based derivation function (Hash_df)
  22. *
  23. * This algorithm is designed to comply with ANS X9.82 Part 3-2007
  24. * Section 10.5.2. This standard is not freely available, but most of
  25. * the text appears to be shared with NIST SP 800-90, which can be
  26. * downloaded from
  27. *
  28. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  29. *
  30. * Where possible, references are given to both documents. In the
  31. * case of any disagreement, ANS X9.82 takes priority over NIST SP
  32. * 800-90. (In particular, note that some algorithms that are
  33. * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
  34. */
  35. #include <stdint.h>
  36. #include <string.h>
  37. #include <byteswap.h>
  38. #include <ipxe/crypto.h>
  39. #include <ipxe/hash_df.h>
  40. /**
  41. * Distribute entropy throughout a buffer
  42. *
  43. * @v input Input data
  44. * @v input_len Length of input data, in bytes
  45. * @v output Output buffer
  46. * @v output_len Length of output buffer, in bytes
  47. *
  48. * This is the Hash_df function defined in ANS X9.82 Part 3-2007
  49. * Section 10.5.2 (NIST SP 800-90 Section 10.4.1).
  50. *
  51. * The number of bits requested is implicit in the length of the
  52. * output buffer. Requests must be for an integral number of bytes.
  53. *
  54. * The output buffer is filled incrementally with each iteration of
  55. * the central loop, rather than constructing an overall "temp" and
  56. * then taking the leftmost(no_of_bits_to_return) bits.
  57. *
  58. * There is no way for the Hash_df function to fail. The returned
  59. * status SUCCESS is implicit.
  60. */
  61. void hash_df ( const void *input, size_t input_len, void *output,
  62. size_t output_len ) {
  63. uint8_t context[HASH_DF_CTX_SIZE];
  64. uint8_t digest[HASH_DF_OUTLEN_BYTES];
  65. size_t frag_len;
  66. struct {
  67. uint8_t pad[3];
  68. uint8_t counter;
  69. uint32_t no_of_bits_to_return;
  70. } __attribute__ (( packed )) prefix;
  71. void *temp;
  72. size_t remaining;
  73. DBGC ( &hash_df, "HASH_DF input:\n" );
  74. DBGC_HDA ( &hash_df, 0, input, input_len );
  75. /* Sanity checks */
  76. assert ( input != NULL );
  77. assert ( output != NULL );
  78. /* 1. temp = the Null string
  79. * 2. len = ceil ( no_of_bits_to_return / outlen )
  80. *
  81. * (Nothing to do. We fill the output buffer incrementally,
  82. * rather than constructing the complete "temp" in-memory.
  83. * "len" is implicit in the number of iterations required to
  84. * fill the output buffer, and so is not calculated
  85. * explicitly.)
  86. */
  87. /* 3. counter = an 8-bit binary value representing the integer "1" */
  88. prefix.counter = 1;
  89. /* 4. For i = 1 to len do */
  90. for ( temp = output, remaining = output_len ; remaining > 0 ; ) {
  91. /* Comment: in step 5.1 (sic), no_of_bits_to_return is
  92. * used as a 32-bit string.
  93. *
  94. * 4.1 temp = temp || Hash ( counter || no_of_bits_to_return
  95. * || input_string )
  96. */
  97. prefix.no_of_bits_to_return = htonl ( output_len * 8 );
  98. digest_init ( &hash_df_algorithm, context );
  99. digest_update ( &hash_df_algorithm, context, &prefix.counter,
  100. ( sizeof ( prefix ) -
  101. offsetof ( typeof ( prefix ), counter ) ) );
  102. digest_update ( &hash_df_algorithm, context, input, input_len );
  103. digest_final ( &hash_df_algorithm, context, digest );
  104. /* 4.2 counter = counter + 1 */
  105. prefix.counter++;
  106. /* 5. requested_bits = Leftmost ( no_of_bits_to_return )
  107. * of temp
  108. *
  109. * (We fill the output buffer incrementally.)
  110. */
  111. frag_len = sizeof ( digest );
  112. if ( frag_len > remaining )
  113. frag_len = remaining;
  114. memcpy ( temp, digest, frag_len );
  115. temp += frag_len;
  116. remaining -= frag_len;
  117. }
  118. /* 6. Return SUCCESS and requested_bits */
  119. DBGC ( &hash_df, "HASH_DF output:\n" );
  120. DBGC_HDA ( &hash_df, 0, output, output_len );
  121. return;
  122. }