ocrypto_chacha20_poly1305.h 8.3 KB

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