occ_poly1305.h 5.6 KB

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