ocrypto_poly1305.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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_poly1305 Poly1305 APIs
  42. * @ingroup nrf_oberon_chacha_poly
  43. @{
  44. * @brief Type declaration and APIs for the Poly1035 algorithm.
  45. *
  46. * Poly1305 is a message authentication code created by Daniel J.
  47. * Bernstein. It can be used to verify the data integrity and the
  48. * authenticity of a message.
  49. *
  50. * Poly1305 takes a one-time key to produce an authentication tag for a message.
  51. * Since a key can only be used to authenticate a single message, a new key
  52. * needs to be derived for each message.
  53. *
  54. * @see [RFC 7539 - ChaCha20 and Poly1305 for IETF Protocols](http://tools.ietf.org/html/rfc7539)
  55. * @see [Poly1305-AES: a state-of-the-art message-authentication code](http://cr.yp.to/mac.html)
  56. */
  57. #ifndef OCRYPTO_POLY1305_H
  58. #define OCRYPTO_POLY1305_H
  59. #include <stddef.h>
  60. #include <stdint.h>
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /**
  65. * Key length.
  66. */
  67. #define ocrypto_poly1305_KEY_BYTES (32)
  68. /**
  69. * Authenticator length.
  70. */
  71. #define ocrypto_poly1305_BYTES (16)
  72. /**@cond */
  73. typedef struct {
  74. uint32_t h[5];
  75. } ocrypto_poly1305_ctx;
  76. /**@endcond */
  77. /**@name Incremental Poly1305 generator.
  78. *
  79. * This group of functions can be used to incrementally compute the Poly1305
  80. * authenticator for a given message and key.
  81. */
  82. /**@{*/
  83. /**
  84. * Poly1305 generator initialize.
  85. *
  86. * The generator state @p ctx is initialized by this function.
  87. *
  88. * @param[out] ctx Generator state.
  89. */
  90. void ocrypto_poly1305_init(ocrypto_poly1305_ctx *ctx);
  91. /**
  92. * Poly1305 generator.
  93. *
  94. * The generator state @p ctx is updated to authenticate a message chunk @p in
  95. * with a key @p k.
  96. *
  97. * This function can be called repeatedly until the whole message has been
  98. * processed.
  99. *
  100. * @param ctx Generator state.
  101. * @param in Input data.
  102. * @param in_len Length of @p in.
  103. * @param k Encryption key.
  104. *
  105. * @remark Initialization of the generator state @p ctx through
  106. * @c ocrypto_poly1305_init is required before this function can be called.
  107. *
  108. * @remark The same key @p k needs to be supplied for all message chunks.
  109. */
  110. void ocrypto_poly1305_update(
  111. ocrypto_poly1305_ctx *ctx,
  112. const uint8_t *in, size_t in_len,
  113. const uint8_t k[ocrypto_poly1305_KEY_BYTES]);
  114. /**
  115. * Poly1305 generator output.
  116. *
  117. * The generator state @p ctx is updated to finalize the authenticator for the
  118. * previously processed message chunks with key @p k. The authentication tag is
  119. * put into @p r.
  120. *
  121. * @param ctx Generator state.
  122. * @param[out] r Generated authentication tag.
  123. * @param k Encryption key.
  124. *
  125. * @remark Initialization of the generator state @p ctx through
  126. * @c ocrypto_poly1305_init is required before this function can be called.
  127. *
  128. * @remark The same key @p k needs to be supplied that was used in previous
  129. * @c ocrypto_poly1305_update invocations.
  130. *
  131. * @remark After return, the generator state @p ctx must no longer be used with
  132. * @c ocrypto_poly1305_update and @c ocrypto_poly1305_final unless it is
  133. * reinitialized using @c ocrypto_poly1305_init.
  134. */
  135. void ocrypto_poly1305_final(
  136. ocrypto_poly1305_ctx *ctx,
  137. uint8_t r[ocrypto_poly1305_BYTES],
  138. const uint8_t k[ocrypto_poly1305_KEY_BYTES]);
  139. /**@}*/
  140. /**
  141. * Poly1305 message authentication tag.
  142. *
  143. * The Poly1305 authentication of a given input message @p in is computed and
  144. * put into @p r.
  145. *
  146. * @param[out] r Generated authentication tag.
  147. * @param in Input data.
  148. * @param in_len Length of @p in.
  149. * @param k Encryption key.
  150. */
  151. void ocrypto_poly1305(
  152. uint8_t r[ocrypto_poly1305_BYTES],
  153. const uint8_t *in, size_t in_len,
  154. const uint8_t k[ocrypto_poly1305_KEY_BYTES]);
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif /* #ifndef OCRYPTO_POLY1305_H */
  159. /**@}*/