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.

hmac.c 3.7KB

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