occ_hmac_sha512.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * Copyright (c) 2016 - 2018, 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-SHA512 is an algorithm for message authentication using the
  42. * cryptographic hash function SHA512 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. #ifndef OCC_HMAC_SHA512_H
  49. #define OCC_HMAC_SHA512_H
  50. #include <stdint.h>
  51. #include <stddef.h>
  52. #include "include/occ_sha512.h"
  53. /**
  54. * Maximum key length.
  55. */
  56. #define occ_hmac_sha512_KEY_BYTES_MAX (128)
  57. /**
  58. * Length of the authenticator.
  59. */
  60. #define occ_hmac_sha512_BYTES (64)
  61. /**@cond */
  62. typedef struct
  63. {
  64. occ_sha512_ctx hash_ctx;
  65. uint8_t ikey[occ_hmac_sha512_KEY_BYTES_MAX];
  66. uint8_t okey[occ_hmac_sha512_KEY_BYTES_MAX];
  67. uint8_t key[occ_hmac_sha512_KEY_BYTES_MAX];
  68. } occ_hmac_sha512_ctx;
  69. /**@endcond */
  70. /**@name Incremental HMAC-SHA512 generator.
  71. *
  72. * This group of functions can be used to incrementally compute HMAC-SHA512
  73. * for a given message.
  74. */
  75. /**@{*/
  76. /**
  77. * HMAC-SHA512 initialization.
  78. *
  79. * The generator state @p ctx is initialized by this function.
  80. *
  81. * @param[out] ctx Generator state.
  82. * @param key HMAC key.
  83. * @param key_len Length of @p key.
  84. */
  85. void occ_hmac_sha512_init(occ_hmac_sha512_ctx * ctx,
  86. const uint8_t* key, size_t key_len);
  87. /**
  88. * HMAC-SHA512 incremental data input.
  89. *
  90. * The generator state @p ctx is updated to hash a message chunk @p in.
  91. *
  92. * This function can be called repeatedly until the whole message is processed.
  93. *
  94. * @param[in,out] ctx Generator state.
  95. * @param in Input data.
  96. * @param in_len Length of @p in.
  97. *
  98. * @remark Initialization of the generator state @p ctx through
  99. * @c occ_hmac_sha512_init is required before this function can be called.
  100. */
  101. void occ_hmac_sha512_update(occ_hmac_sha512_ctx * ctx,
  102. const uint8_t* in, size_t in_len);
  103. /**
  104. * HMAC-SHA512 output.
  105. *
  106. * The generator state @p ctx is updated to finalize the HMAC calculation.
  107. * The HMAC digest is put into @p r.
  108. *
  109. * @param[out] r Generated HMAC digest.
  110. * @param[in,out] ctx Generator state.
  111. *
  112. * @remark Initialization of the generator state @p ctx through
  113. * @c occ_hmac_sha512_init is required before this function can be called.
  114. *
  115. * @remark After return, the generator state @p ctx must no longer be used with
  116. * @c occ_hmac_sha512_update and @c occ_hmac_sha512_final unless it is
  117. * reinitialized using @c occ_hmac_sha512_init.
  118. */
  119. void occ_hmac_sha512_final(uint8_t r[occ_hmac_sha512_BYTES],
  120. occ_hmac_sha512_ctx * ctx);
  121. /**@}*/
  122. /**
  123. * HMAC-SHA512 algorithm.
  124. *
  125. * The input message @p in is authenticated using the key @p k. The computed
  126. * authenticator is put into @p r. To verify the authenticator, the recipient
  127. * needs to recompute the HMAC authenticator and can then compare it with the
  128. * received authenticator.
  129. *
  130. * **Example**
  131. * @include occ_hmac_sha512.c
  132. *
  133. * @param[out] r HMAC output.
  134. * @param key HMAC key.
  135. * @param key_len Length of @p key. 0 <= @p key_len <= @c occ_hmac_sha512_KEY_BYTES_MAX.
  136. * @param in Input data.
  137. * @param in_len Length of @p in.
  138. */
  139. void occ_hmac_sha512(uint8_t r[occ_hmac_sha512_BYTES],
  140. const uint8_t* key, size_t key_len,
  141. const uint8_t* in, size_t in_len);
  142. #endif