occ_hmac_sha256.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. * HMAC-SHA256 is an algorithm for message authentication using the
  42. * cryptographic hash function SHA256 and a reusable secret key. Users in
  43. * possession of the key can verify the integrity and authenticity of the
  44. * message.
  45. *
  46. * @see [RFC 2104 - HMAC: Keyed-Hashing for Message Authentication](http://tools.ietf.org/html/rfc2104)
  47. */
  48. // REPLACEMENT
  49. #ifndef OCC_HMAC_SHA256_H
  50. #define OCC_HMAC_SHA256_H
  51. #include <stdint.h>
  52. #include <stddef.h>
  53. #include "include/occ_sha256.h"
  54. /**
  55. * Maximum key length.
  56. */
  57. #define occ_hmac_sha256_KEY_BYTES_MAX (64)
  58. /**
  59. * Length of the authenticator.
  60. */
  61. #define occ_hmac_sha256_BYTES (32)
  62. /**@cond */
  63. typedef struct
  64. {
  65. occ_sha256_ctx hash_ctx;
  66. uint8_t ikey[occ_hmac_sha256_KEY_BYTES_MAX];
  67. uint8_t okey[occ_hmac_sha256_KEY_BYTES_MAX];
  68. uint8_t key[occ_hmac_sha256_KEY_BYTES_MAX];
  69. } occ_hmac_sha256_ctx;
  70. /**@endcond */
  71. /**@name Incremental HMAC-SHA256 generator.
  72. *
  73. * This group of functions can be used to incrementally compute HMAC-SHA256
  74. * for a given message.
  75. */
  76. /**@{*/
  77. /**
  78. * HMAC-SHA256 initialization.
  79. *
  80. * The generator state @p ctx is initialized by this function.
  81. *
  82. * @param[out] ctx Generator state.
  83. * @param key HMAC key.
  84. * @param key_len Length of @p key.
  85. */
  86. void occ_hmac_sha256_init(occ_hmac_sha256_ctx * ctx,
  87. const uint8_t* key, size_t key_len);
  88. /**
  89. * HMAC-SHA256 incremental data input.
  90. *
  91. * The generator state @p ctx is updated to hash a message chunk @p in.
  92. *
  93. * This function can be called repeatedly until the whole message is processed.
  94. *
  95. * @param[in,out] ctx Generator state.
  96. * @param in Input data.
  97. * @param in_len Length of @p in.
  98. *
  99. * @remark Initialization of the generator state @p ctx through
  100. * @c occ_hmac_sha256_init is required before this function can be called.
  101. */
  102. void occ_hmac_sha256_update(occ_hmac_sha256_ctx * ctx,
  103. const uint8_t* in, size_t in_len);
  104. /**
  105. * HMAC-SHA256 output.
  106. *
  107. * The generator state @p ctx is updated to finalize the HMAC calculation.
  108. * The HMAC digest is put into @p r.
  109. *
  110. * @param[out] r Generated HMAC digest.
  111. * @param[in,out] ctx Generator state.
  112. *
  113. * @remark Initialization of the generator state @p ctx through
  114. * @c occ_hmac_sha256_init is required before this function can be called.
  115. *
  116. * @remark After return, the generator state @p ctx must no longer be used with
  117. * @c occ_hmac_sha256_update and @c occ_hmac_sha256_final unless it is
  118. * reinitialized using @c occ_hmac_sha256_init.
  119. */
  120. void occ_hmac_sha256_final(uint8_t r[occ_hmac_sha256_BYTES],
  121. occ_hmac_sha256_ctx * ctx);
  122. /**@}*/
  123. /**
  124. * HMAC-SHA256 algorithm.
  125. *
  126. * The input message @p in is authenticated using the key @p k. The computed
  127. * authenticator is put into @p r. To verify the authenticator, the recipient
  128. * needs to recompute the HMAC authenticator and can then compare it with the
  129. * received authenticator.
  130. *
  131. * **Example**
  132. * @include occ_hmac_sha256.c
  133. *
  134. * @param[out] r HMAC output.
  135. * @param key HMAC key.
  136. * @param key_len Length of @p key. 0 <= @p key_len <= @c occ_hmac_sha256_KEY_BYTES_MAX.
  137. * @param in Input data.
  138. * @param in_len Length of @p in.
  139. */
  140. void occ_hmac_sha256(uint8_t r[occ_hmac_sha256_BYTES],
  141. const uint8_t* key, size_t key_len,
  142. const uint8_t* in, size_t in_len);
  143. #endif