occ_chacha20_poly1305.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. /*!
  41. * @brief ChaCha20-Poly1305 is an authenticated encryption algorithm with
  42. * optional additional authenticated data developed by Daniel J.
  43. * Bernstein.
  44. */
  45. /**@file
  46. * The ChaCha20 stream cipher is combined with the Poly1305 authenticator.
  47. *
  48. * @see [RFC 7539 - ChaCha20 and Poly1305 for IETF Protocols](http://tools.ietf.org/html/rfc7539)
  49. */
  50. #ifndef OCC_CHACHA20_POLY1305_H
  51. #define OCC_CHACHA20_POLY1305_H
  52. #include <stdint.h>
  53. #include <stddef.h>
  54. /**
  55. * Length of the encryption key.
  56. */
  57. #define occ_chacha20_poly1305_KEY_BYTES (32)
  58. /**
  59. * Maximum length of the nonce.
  60. */
  61. #define occ_chacha20_poly1305_NONCE_BYTES_MAX (12)
  62. /**
  63. * Length of the authentication tag.
  64. */
  65. #define occ_chacha20_poly1305_TAG_BYTES (16)
  66. /**@{*/
  67. /**
  68. * AEAD ChaCha20-Poly1305 encrypt.
  69. *
  70. * The message @p m is encrypted using a ChaCha20 cipher stream derived from the
  71. * encryption key @p k and the nonce @p n. The resulting ciphertext has the same
  72. * length @p m_len as the input message @p m and is put into @p c.
  73. *
  74. * Additionally, the ciphertext @p c is authenticated with a tag that is
  75. * generated with Poly1305 using a unique subkey derived from @p k and @p n, and
  76. * then put into @p tag.
  77. *
  78. * **Example**
  79. * @include occ_chacha20_poly1305_encrypt.c
  80. *
  81. * @param[out] tag Generated authentication tag.
  82. * @param[out] c Generated ciphertext. Same length as input message.
  83. * @param m Input message.
  84. * @param m_len Length of @p m and @p c.
  85. * @param n Nonce.
  86. * @param n_len Length of @p n. 0 <= @p n_len <= @c occ_chacha20_poly1305_NONCE_BYTES_MAX.
  87. * @param k Encryption key.
  88. *
  89. * @remark @p c and @p m can point to the same address.
  90. *
  91. * @remark When reusing an encryption key @p k for a different message @p m, a
  92. * different nonce @p n must be used.
  93. */
  94. void occ_chacha20_poly1305_encrypt(uint8_t tag[occ_chacha20_poly1305_TAG_BYTES],
  95. uint8_t *c,
  96. const uint8_t *m, size_t m_len,
  97. const uint8_t *n, size_t n_len,
  98. const uint8_t k[occ_chacha20_poly1305_KEY_BYTES]);
  99. /**
  100. * AEAD ChaCha20-Poly1305 encrypt with AAD.
  101. *
  102. * The message @p m is encrypted using a ChaCha20 cipher stream derived from the
  103. * encryption key @p k and the nonce @p n. The resulting ciphertext has the same
  104. * length @p m_len as the input message @p m and is put into @p c.
  105. *
  106. * Additionally, the ciphertext @p c as well as the additional authenticated
  107. * data @p a is authenticated with a tag that is generated with Poly1305 using a
  108. * unique subkey derived from @p k and @p n, and then put into @p tag.
  109. *
  110. * **Example**
  111. * @include occ_chacha20_poly1305_encrypt_aad.c
  112. *
  113. * @param[out] tag Generated authentication tag.
  114. * @param[out] c Generated ciphertext. Same length as input message.
  115. * @param m Input message.
  116. * @param m_len Length of @p m and @p c.
  117. * @param a Additional authenticated data.
  118. * @param a_len Length of @p a.
  119. * @param n Nonce.
  120. * @param n_len Length of @p n. 0 <= @p n_len <= @c occ_chacha20_poly1305_NONCE_BYTES_MAX.
  121. * @param k Encryption key.
  122. *
  123. * @remark @p c and @p m can point to the same address.
  124. *
  125. * @remark When reusing an encryption key @p k for a different message @p m or
  126. * different additional authenticated data @p a, a different nonce @p n
  127. * must be used.
  128. */
  129. void occ_chacha20_poly1305_encrypt_aad(uint8_t tag[occ_chacha20_poly1305_TAG_BYTES],
  130. uint8_t *c,
  131. const uint8_t *m, size_t m_len,
  132. const uint8_t *a, size_t a_len,
  133. const uint8_t *n, size_t n_len,
  134. const uint8_t k[occ_chacha20_poly1305_KEY_BYTES]);
  135. /**@}*/
  136. /**@{*/
  137. /**
  138. * AEAD ChaCha20-Poly1305 decrypt.
  139. *
  140. * If the authentication tag @p tag is valid for the ciphertext @p c, the
  141. * encryption key @p k and the nonce @p n, the ciphertext is decrypted and put
  142. * into @p m. The decrypted message @p m has the same length @p c_len as the
  143. * original ciphertext.
  144. *
  145. * **Example**
  146. * @include occ_chacha20_poly1305_decrypt.c
  147. *
  148. * @param tag Received authentication tag.
  149. * @param[out] m Decoded message. Same length as received ciphertext.
  150. * @param c Received ciphertext.
  151. * @param c_len Length of @p c and @p m.
  152. * @param n Nonce.
  153. * @param n_len Length of @p n. 0 <= @p n_len <= @c occ_chacha20_poly1305_NONCE_BYTES_MAX.
  154. * @param k Encryption key.
  155. *
  156. * @returns 0 If @p tag is valid.
  157. * @returns 1 Otherwise.
  158. *
  159. * @remark @p m and @p c can point to the same address.
  160. */
  161. int occ_chacha20_poly1305_decrypt(const uint8_t tag[occ_chacha20_poly1305_TAG_BYTES],
  162. uint8_t *m,
  163. const uint8_t *c, size_t c_len,
  164. const uint8_t *n, size_t n_len,
  165. const uint8_t k[occ_chacha20_poly1305_KEY_BYTES]);
  166. /**
  167. * AEAD ChaCha20-Poly1305 decrypt with AAD.
  168. *
  169. * If the authentication tag @p tag is valid for the ciphertext @p c, the
  170. * additional authenticated data @p a, the encryption key @p k and the nonce
  171. * @p n, the ciphertext is decrypted and put into @p m. The decrypted message
  172. * @p m has the same length @p c_len as the original ciphertext.
  173. *
  174. * **Example**
  175. * @include occ_chacha20_poly1305_decrypt_aad.c
  176. *
  177. * @param tag Received authentication tag.
  178. * @param[out] m Decoded message. Same length as received ciphertext.
  179. * @param c Received ciphertext.
  180. * @param c_len Length of @p c and @p m.
  181. * @param a Received additional authenticated data.
  182. * @param a_len Length of @p a.
  183. * @param n Nonce.
  184. * @param n_len Length of @p n. 0 <= @p n_len <= @c occ_chacha20_poly1305_NONCE_BYTES_MAX.
  185. * @param k Encryption key.
  186. *
  187. * @returns 0 If @p tag is valid.
  188. * @returns 1 Otherwise.
  189. *
  190. * @remark @p m and @p c can point to the same address.
  191. */
  192. int occ_chacha20_poly1305_decrypt_aad(const uint8_t tag[occ_chacha20_poly1305_TAG_BYTES],
  193. uint8_t *m,
  194. const uint8_t *c, size_t c_len,
  195. const uint8_t *a, size_t a_len,
  196. const uint8_t *n, size_t n_len,
  197. const uint8_t k[occ_chacha20_poly1305_KEY_BYTES]);
  198. /**@}*/
  199. #endif