hmac.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
  4. The redistribution and use of this software (with or without changes)
  5. is allowed without the payment of fees or royalties provided that:
  6. source code distributions include the above copyright notice, this
  7. list of conditions and the following disclaimer;
  8. binary distributions include the above copyright notice, this list
  9. of conditions and the following disclaimer in their documentation.
  10. This software is provided 'as is' with no explicit or implied warranties
  11. in respect of its operation, including, but not limited to, correctness
  12. and fitness for purpose.
  13. ---------------------------------------------------------------------------
  14. Issue Date: 20/12/2007
  15. This is an implementation of HMAC, the FIPS standard keyed hash function
  16. */
  17. #ifndef _HMAC2_H
  18. #define _HMAC2_H
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #if defined(__cplusplus)
  22. extern "C"
  23. {
  24. #endif
  25. #include "sha1.h"
  26. #if defined(SHA_224) || defined(SHA_256) || defined(SHA_384) || defined(SHA_512)
  27. #define HMAC_MAX_OUTPUT_SIZE SHA2_MAX_DIGEST_SIZE
  28. #define HMAC_MAX_BLOCK_SIZE SHA2_MAX_BLOCK_SIZE
  29. #else
  30. #define HMAC_MAX_OUTPUT_SIZE SHA1_DIGEST_SIZE
  31. #define HMAC_MAX_BLOCK_SIZE SHA1_BLOCK_SIZE
  32. #endif
  33. #define HMAC_IN_DATA 0xffffffff
  34. enum hmac_hash
  35. {
  36. #ifdef SHA_1
  37. HMAC_SHA1,
  38. #endif
  39. #ifdef SHA_224
  40. HMAC_SHA224,
  41. #endif
  42. #ifdef SHA_256
  43. HMAC_SHA256,
  44. #endif
  45. #ifdef SHA_384
  46. HMAC_SHA384,
  47. #endif
  48. #ifdef SHA_512
  49. HMAC_SHA512,
  50. HMAC_SHA512_256,
  51. HMAC_SHA512_224,
  52. HMAC_SHA512_192,
  53. HMAC_SHA512_128
  54. #endif
  55. };
  56. typedef VOID_RETURN hf_begin(void*);
  57. typedef VOID_RETURN hf_hash(const void*, unsigned long len, void*);
  58. typedef VOID_RETURN hf_end(void*, void*);
  59. typedef struct
  60. { hf_begin *f_begin;
  61. hf_hash *f_hash;
  62. hf_end *f_end;
  63. unsigned char key[HMAC_MAX_BLOCK_SIZE];
  64. union
  65. {
  66. #ifdef SHA_1
  67. sha1_ctx u_sha1;
  68. #endif
  69. #ifdef SHA_224
  70. sha224_ctx u_sha224;
  71. #endif
  72. #ifdef SHA_256
  73. sha256_ctx u_sha256;
  74. #endif
  75. #ifdef SHA_384
  76. sha384_ctx u_sha384;
  77. #endif
  78. #ifdef SHA_512
  79. sha512_ctx u_sha512;
  80. #endif
  81. } sha_ctx[1];
  82. unsigned long input_len;
  83. unsigned long output_len;
  84. unsigned long klen;
  85. } hmac_ctx;
  86. /* returns the length of hash digest for the hash used */
  87. /* mac_len must not be greater than this */
  88. int hmac_sha_begin(enum hmac_hash hash, hmac_ctx cx[1]);
  89. int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]);
  90. void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]);
  91. void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]);
  92. void hmac_sha(enum hmac_hash hash, const unsigned char key[], unsigned long key_len,
  93. const unsigned char data[], unsigned long data_len,
  94. unsigned char mac[], unsigned long mac_len);
  95. #if defined(__cplusplus)
  96. }
  97. #endif
  98. #endif