hmac.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* hmac.h - TinyCrypt interface to an HMAC implementation */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /**
  32. * @file
  33. * @brief Interface to an HMAC implementation.
  34. *
  35. * Overview: HMAC is a message authentication code based on hash functions.
  36. * TinyCrypt hard codes SHA-256 as the hash function. A message
  37. * authentication code based on hash functions is also called a
  38. * keyed cryptographic hash function since it performs a
  39. * transformation specified by a key in an arbitrary length data
  40. * set into a fixed length data set (also called tag).
  41. *
  42. * Security: The security of the HMAC depends on the length of the key and
  43. * on the security of the hash function. Note that HMAC primitives
  44. * are much less affected by collision attacks than their
  45. * corresponding hash functions.
  46. *
  47. * Requires: SHA-256
  48. *
  49. * Usage: 1) call tc_hmac_set_key to set the HMAC key.
  50. *
  51. * 2) call tc_hmac_init to initialize a struct hash_state before
  52. * processing the data.
  53. *
  54. * 3) call tc_hmac_update to process the next input segment;
  55. * tc_hmac_update can be called as many times as needed to process
  56. * all of the segments of the input; the order is important.
  57. *
  58. * 4) call tc_hmac_final to out put the tag.
  59. */
  60. #ifndef __TC_HMAC_H__
  61. #define __TC_HMAC_H__
  62. #include <tinycrypt/sha256.h>
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. struct tc_hmac_state_struct {
  67. /* the internal state required by h */
  68. struct tc_sha256_state_struct hash_state;
  69. /* HMAC key schedule */
  70. uint8_t key[2*TC_SHA256_BLOCK_SIZE];
  71. };
  72. typedef struct tc_hmac_state_struct *TCHmacState_t;
  73. /**
  74. * @brief HMAC set key procedure
  75. * Configures ctx to use key
  76. * @return returns TC_CRYPTO_SUCCESS (1)
  77. * returns TC_CRYPTO_FAIL (0) if
  78. * ctx == NULL or
  79. * key == NULL or
  80. * key_size == 0
  81. * @param ctx IN/OUT -- the struct tc_hmac_state_struct to initial
  82. * @param key IN -- the HMAC key to configure
  83. * @param key_size IN -- the HMAC key size
  84. */
  85. int tc_hmac_set_key(TCHmacState_t ctx, const uint8_t *key,
  86. unsigned int key_size);
  87. /**
  88. * @brief HMAC init procedure
  89. * Initializes ctx to begin the next HMAC operation
  90. * @return returns TC_CRYPTO_SUCCESS (1)
  91. * returns TC_CRYPTO_FAIL (0) if: ctx == NULL or key == NULL
  92. * @param ctx IN/OUT -- struct tc_hmac_state_struct buffer to init
  93. */
  94. int tc_hmac_init(TCHmacState_t ctx);
  95. /**
  96. * @brief HMAC update procedure
  97. * Mixes data_length bytes addressed by data into state
  98. * @return returns TC_CRYPTO_SUCCCESS (1)
  99. * returns TC_CRYPTO_FAIL (0) if: ctx == NULL or key == NULL
  100. * @note Assumes state has been initialized by tc_hmac_init
  101. * @param ctx IN/OUT -- state of HMAC computation so far
  102. * @param data IN -- data to incorporate into state
  103. * @param data_length IN -- size of data in bytes
  104. */
  105. int tc_hmac_update(TCHmacState_t ctx, const void *data,
  106. unsigned int data_length);
  107. /**
  108. * @brief HMAC final procedure
  109. * Writes the HMAC tag into the tag buffer
  110. * @return returns TC_CRYPTO_SUCCESS (1)
  111. * returns TC_CRYPTO_FAIL (0) if:
  112. * tag == NULL or
  113. * ctx == NULL or
  114. * key == NULL or
  115. * taglen != TC_SHA256_DIGEST_SIZE
  116. * @note ctx is erased before exiting. This should never be changed/removed.
  117. * @note Assumes the tag bufer is at least sizeof(hmac_tag_size(state)) bytes
  118. * state has been initialized by tc_hmac_init
  119. * @param tag IN/OUT -- buffer to receive computed HMAC tag
  120. * @param taglen IN -- size of tag in bytes
  121. * @param ctx IN/OUT -- the HMAC state for computing tag
  122. */
  123. int tc_hmac_final(uint8_t *tag, unsigned int taglen, TCHmacState_t ctx);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif /*__TC_HMAC_H__*/