Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2007 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. /**
  20. * @file
  21. *
  22. * Keyed-Hashing for Message Authentication
  23. */
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <gpxe/crypto.h>
  27. #include <gpxe/hmac.h>
  28. /**
  29. * Reduce HMAC key length
  30. *
  31. * @v digest Digest algorithm to use
  32. * @v digest_ctx Digest context
  33. * @v key Key
  34. * @v key_len Length of key
  35. */
  36. static void hmac_reduce_key ( struct digest_algorithm *digest,
  37. void *key, size_t *key_len ) {
  38. uint8_t digest_ctx[digest->ctxsize];
  39. digest_init ( digest, digest_ctx );
  40. digest_update ( digest, digest_ctx, key, *key_len );
  41. digest_final ( digest, digest_ctx, key );
  42. *key_len = digest->digestsize;
  43. }
  44. /**
  45. * Initialise HMAC
  46. *
  47. * @v digest Digest algorithm to use
  48. * @v digest_ctx Digest context
  49. * @v key Key
  50. * @v key_len Length of key
  51. *
  52. * The length of the key should be less than the block size of the
  53. * digest algorithm being used. (If the key length is greater, it
  54. * will be replaced with its own digest, and key_len will be updated
  55. * accordingly).
  56. */
  57. void hmac_init ( struct digest_algorithm *digest, void *digest_ctx,
  58. void *key, size_t *key_len ) {
  59. unsigned char k_ipad[digest->blocksize];
  60. unsigned int i;
  61. /* Reduce key if necessary */
  62. if ( *key_len > sizeof ( k_ipad ) )
  63. hmac_reduce_key ( digest, key, key_len );
  64. /* Construct input pad */
  65. memset ( k_ipad, 0, sizeof ( k_ipad ) );
  66. memcpy ( k_ipad, key, *key_len );
  67. for ( i = 0 ; i < sizeof ( k_ipad ) ; i++ ) {
  68. k_ipad[i] ^= 0x36;
  69. }
  70. /* Start inner hash */
  71. digest_init ( digest, digest_ctx );
  72. digest_update ( digest, digest_ctx, k_ipad, sizeof ( k_ipad ) );
  73. }
  74. /**
  75. * Finalise HMAC
  76. *
  77. * @v digest Digest algorithm to use
  78. * @v digest_ctx Digest context
  79. * @v key Key
  80. * @v key_len Length of key
  81. * @v hmac HMAC digest to fill in
  82. *
  83. * The length of the key should be less than the block size of the
  84. * digest algorithm being used. (If the key length is greater, it
  85. * will be replaced with its own digest, and key_len will be updated
  86. * accordingly).
  87. */
  88. void hmac_final ( struct digest_algorithm *digest, void *digest_ctx,
  89. void *key, size_t *key_len, void *hmac ) {
  90. unsigned char k_opad[digest->blocksize];
  91. unsigned int i;
  92. /* Reduce key if necessary */
  93. if ( *key_len > sizeof ( k_opad ) )
  94. hmac_reduce_key ( digest, key, key_len );
  95. /* Construct output pad */
  96. memset ( k_opad, 0, sizeof ( k_opad ) );
  97. memcpy ( k_opad, key, *key_len );
  98. for ( i = 0 ; i < sizeof ( k_opad ) ; i++ ) {
  99. k_opad[i] ^= 0x5c;
  100. }
  101. /* Finish inner hash */
  102. digest_final ( digest, digest_ctx, hmac );
  103. /* Perform outer hash */
  104. digest_init ( digest, digest_ctx );
  105. digest_update ( digest, digest_ctx, k_opad, sizeof ( k_opad ) );
  106. digest_update ( digest, digest_ctx, hmac, digest->digestsize );
  107. digest_final ( digest, digest_ctx, hmac );
  108. }