Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sha1extra.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  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. #include <gpxe/crypto.h>
  20. #include <gpxe/sha1.h>
  21. #include <gpxe/hmac.h>
  22. #include <stdint.h>
  23. #include <byteswap.h>
  24. /**
  25. * SHA1 pseudorandom function for creating derived keys
  26. *
  27. * @v key Master key with which this call is associated
  28. * @v key_len Length of key
  29. * @v label NUL-terminated ASCII string describing purpose of PRF data
  30. * @v data Further data that should be included in the PRF
  31. * @v data_len Length of further PRF data
  32. * @v prf_len Bytes of PRF to generate
  33. * @ret prf Pseudorandom function bytes
  34. *
  35. * This is the PRF variant used by 802.11, defined in IEEE 802.11-2007
  36. * 8.5.5.1. EAP-FAST uses a different SHA1-based PRF, and TLS uses an
  37. * MD5-based PRF.
  38. */
  39. void prf_sha1 ( const void *key, size_t key_len, const char *label,
  40. const void *data, size_t data_len, void *prf, size_t prf_len )
  41. {
  42. u32 blk;
  43. u8 keym[key_len]; /* modifiable copy of key */
  44. u8 in[strlen ( label ) + 1 + data_len + 1]; /* message to HMAC */
  45. u8 *in_blknr; /* pointer to last byte of in, block number */
  46. u8 out[SHA1_SIZE]; /* HMAC-SHA1 result */
  47. u8 sha1_ctx[SHA1_CTX_SIZE]; /* SHA1 context */
  48. const size_t label_len = strlen ( label );
  49. /* The HMAC-SHA-1 is calculated using the given key on the
  50. message text `label', followed by a NUL, followed by one
  51. byte indicating the block number (0 for first). */
  52. memcpy ( keym, key, key_len );
  53. memcpy ( in, label, strlen ( label ) + 1 );
  54. memcpy ( in + label_len + 1, data, data_len );
  55. in_blknr = in + label_len + 1 + data_len;
  56. for ( blk = 0 ;; blk++ ) {
  57. *in_blknr = blk;
  58. hmac_init ( &sha1_algorithm, sha1_ctx, keym, &key_len );
  59. hmac_update ( &sha1_algorithm, sha1_ctx, in, sizeof ( in ) );
  60. hmac_final ( &sha1_algorithm, sha1_ctx, keym, &key_len, out );
  61. if ( prf_len <= SHA1_SIZE ) {
  62. memcpy ( prf, out, prf_len );
  63. break;
  64. }
  65. memcpy ( prf, out, SHA1_SIZE );
  66. prf_len -= SHA1_SIZE;
  67. prf += SHA1_SIZE;
  68. }
  69. }
  70. /**
  71. * PBKDF2 key derivation function inner block operation
  72. *
  73. * @v passphrase Passphrase from which to derive key
  74. * @v pass_len Length of passphrase
  75. * @v salt Salt to include in key
  76. * @v salt_len Length of salt
  77. * @v iterations Number of iterations of SHA1 to perform
  78. * @v blocknr Index of this block, starting at 1
  79. * @ret block SHA1_SIZE bytes of PBKDF2 data
  80. *
  81. * The operation of this function is described in RFC 2898.
  82. */
  83. static void pbkdf2_sha1_f ( const void *passphrase, size_t pass_len,
  84. const void *salt, size_t salt_len,
  85. int iterations, u32 blocknr, u8 *block )
  86. {
  87. u8 pass[pass_len]; /* modifiable passphrase */
  88. u8 in[salt_len + 4]; /* input buffer to first round */
  89. u8 last[SHA1_SIZE]; /* output of round N, input of N+1 */
  90. u8 sha1_ctx[SHA1_CTX_SIZE];
  91. u8 *next_in = in; /* changed to `last' after first round */
  92. int next_size = sizeof ( in );
  93. int i, j;
  94. blocknr = htonl ( blocknr );
  95. memcpy ( pass, passphrase, pass_len );
  96. memcpy ( in, salt, salt_len );
  97. memcpy ( in + salt_len, &blocknr, 4 );
  98. memset ( block, 0, SHA1_SIZE );
  99. for ( i = 0; i < iterations; i++ ) {
  100. hmac_init ( &sha1_algorithm, sha1_ctx, pass, &pass_len );
  101. hmac_update ( &sha1_algorithm, sha1_ctx, next_in, next_size );
  102. hmac_final ( &sha1_algorithm, sha1_ctx, pass, &pass_len, last );
  103. for ( j = 0; j < SHA1_SIZE; j++ ) {
  104. block[j] ^= last[j];
  105. }
  106. next_in = last;
  107. next_size = SHA1_SIZE;
  108. }
  109. }
  110. /**
  111. * PBKDF2 key derivation function using SHA1
  112. *
  113. * @v passphrase Passphrase from which to derive key
  114. * @v pass_len Length of passphrase
  115. * @v salt Salt to include in key
  116. * @v salt_len Length of salt
  117. * @v iterations Number of iterations of SHA1 to perform
  118. * @v key_len Length of key to generate
  119. * @ret key Generated key bytes
  120. *
  121. * This is used most notably in 802.11 WPA passphrase hashing, in
  122. * which case the salt is the SSID, 4096 iterations are used, and a
  123. * 32-byte key is generated that serves as the Pairwise Master Key for
  124. * EAPOL authentication.
  125. *
  126. * The operation of this function is further described in RFC 2898.
  127. */
  128. void pbkdf2_sha1 ( const void *passphrase, size_t pass_len,
  129. const void *salt, size_t salt_len,
  130. int iterations, void *key, size_t key_len )
  131. {
  132. u32 blocks = ( key_len + SHA1_SIZE - 1 ) / SHA1_SIZE;
  133. u32 blk;
  134. u8 buf[SHA1_SIZE];
  135. for ( blk = 1; blk <= blocks; blk++ ) {
  136. pbkdf2_sha1_f ( passphrase, pass_len, salt, salt_len,
  137. iterations, blk, buf );
  138. if ( key_len <= SHA1_SIZE ) {
  139. memcpy ( key, buf, key_len );
  140. break;
  141. }
  142. memcpy ( key, buf, SHA1_SIZE );
  143. key_len -= SHA1_SIZE;
  144. key += SHA1_SIZE;
  145. }
  146. }