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.5KB

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