ocrypto_hmac_sha256.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Copyright (c) 2019 - 2020, 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. * @defgroup nrf_oberon_hmac HMAC - Hash-based Aessage Authentication Code
  42. * @ingroup nrf_oberon
  43. * @{
  44. * @brief HMAC is a hash-based Message Authentication Code utilizing a secure hash function.
  45. * @}
  46. * @defgroup nrf_oberon_hmac_256 HMAC APIs using SHA-256
  47. * @ingroup nrf_oberon_hmac
  48. * @{
  49. * @brief Type declarations and APIs for the HMAC-SHA256 algorithm.
  50. *
  51. * HMAC-SHA256 is an algorithm for message authentication using the
  52. * cryptographic hash function SHA256 and a reusable secret key. Users in
  53. * possession of the key can verify the integrity and authenticity of the
  54. * message.
  55. *
  56. * @see [RFC 2104 - HMAC: Keyed-Hashing for Message Authentication](http://tools.ietf.org/html/rfc2104)
  57. */
  58. #ifndef OCRYPTO_HMAC_SHA256_H
  59. #define OCRYPTO_HMAC_SHA256_H
  60. #include <stddef.h>
  61. #include <stdint.h>
  62. #include "include/ocrypto_sha256.h"
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. /**
  67. * Maximum key length.
  68. */
  69. #define ocrypto_hmac_sha256_KEY_BYTES_MAX (64)
  70. /**
  71. * Length of the authenticator.
  72. */
  73. #define ocrypto_hmac_sha256_BYTES (32)
  74. /**@cond */
  75. typedef struct
  76. {
  77. ocrypto_sha256_ctx hash_ctx;
  78. uint8_t ikey[ocrypto_hmac_sha256_KEY_BYTES_MAX];
  79. uint8_t okey[ocrypto_hmac_sha256_KEY_BYTES_MAX];
  80. uint8_t key[ocrypto_hmac_sha256_KEY_BYTES_MAX];
  81. } ocrypto_hmac_sha256_ctx;
  82. /**@endcond */
  83. /**@name Incremental HMAC-SHA256 generator.
  84. *
  85. * This group of functions can be used to incrementally compute HMAC-SHA256
  86. * for a given message.
  87. */
  88. /**@{*/
  89. /**
  90. * HMAC-SHA256 initialization.
  91. *
  92. * The generator state @p ctx is initialized by this function.
  93. *
  94. * @param[out] ctx Generator state.
  95. * @param key HMAC key.
  96. * @param key_len Length of @p key.
  97. */
  98. void ocrypto_hmac_sha256_init(ocrypto_hmac_sha256_ctx * ctx,
  99. const uint8_t* key, size_t key_len);
  100. /**
  101. * HMAC-SHA256 incremental data input.
  102. *
  103. * The generator state @p ctx is updated to hash a message chunk @p in.
  104. *
  105. * This function can be called repeatedly until the whole message is processed.
  106. *
  107. * @param[in,out] ctx Generator state.
  108. * @param in Input data.
  109. * @param in_len Length of @p in.
  110. *
  111. * @remark Initialization of the generator state @p ctx through
  112. * @c ocrypto_hmac_sha256_init is required before this function can be called.
  113. */
  114. void ocrypto_hmac_sha256_update(ocrypto_hmac_sha256_ctx * ctx,
  115. const uint8_t* in, size_t in_len);
  116. /**
  117. * HMAC-SHA256 output.
  118. *
  119. * The generator state @p ctx is updated to finalize the HMAC calculation.
  120. * The HMAC digest is put into @p r.
  121. *
  122. * @param[in,out] ctx Generator state.
  123. * @param[out] r Generated HMAC digest.
  124. *
  125. * @remark Initialization of the generator state @p ctx through
  126. * @c ocrypto_hmac_sha256_init is required before this function can be called.
  127. *
  128. * @remark After return, the generator state @p ctx must no longer be used with
  129. * @c ocrypto_hmac_sha256_update and @c ocrypto_hmac_sha256_final unless it is
  130. * reinitialized using @c ocrypto_hmac_sha256_init.
  131. */
  132. void ocrypto_hmac_sha256_final(ocrypto_hmac_sha256_ctx * ctx,
  133. uint8_t r[ocrypto_hmac_sha256_BYTES]);
  134. /**@}*/
  135. /**
  136. * HMAC-SHA256 algorithm.
  137. *
  138. * The input message @p in is authenticated using the key @p k. The computed
  139. * authenticator is put into @p r. To verify the authenticator, the recipient
  140. * needs to recompute the HMAC authenticator and can then compare it with the
  141. * received authenticator.
  142. *
  143. * @param[out] r HMAC output.
  144. * @param key HMAC key.
  145. * @param key_len Length of @p key. 0 <= @p key_len <= @c ocrypto_hmac_sha256_KEY_BYTES_MAX.
  146. * @param in Input data.
  147. * @param in_len Length of @p in.
  148. */
  149. void ocrypto_hmac_sha256(
  150. uint8_t r[ocrypto_hmac_sha256_BYTES],
  151. const uint8_t* key, size_t key_len,
  152. const uint8_t* in, size_t in_len);
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif /* #ifndef OCRYPTO_HMAC_SHA256_H */
  157. /** @} */