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

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