chacha20poly1305.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef CHACHA20POLY1305_H
  15. #define CHACHA20POLY1305_H
  16. #include <stdint.h>
  17. #include <stddef.h>
  18. /**
  19. * The ChaCha20-Poly1305 AEAD construction
  20. * =======================================
  21. * This is a composition of the ChaCha20 stream cipher and
  22. * the Poly1305 polynomial MAC to form an AEAD.
  23. * It's specified for use in TLS in the form of RFC7539.
  24. *
  25. * It uses a 256-bit key and a 96-bit nonce.
  26. *
  27. * This is a one-shot interface.
  28. */
  29. /* .. c:function:: $DECL
  30. * ChaCha20-Poly1305 authenticated encryption.
  31. *
  32. * :param key: key material.
  33. * :param nonce: per-message nonce.
  34. * :param header: header buffer.
  35. * :param nheader: number of header bytes.
  36. * :param plaintext: plaintext bytes to be encrypted.
  37. * :param nbytes: number of plaintext/ciphertext bytes.
  38. * :param ciphertext: ciphertext output buffer, nbytes in length.
  39. * :param tag: authentication tag output buffer.
  40. */
  41. void cf_chacha20poly1305_encrypt(const uint8_t key[32],
  42. const uint8_t nonce[12],
  43. const uint8_t *header, size_t nheader,
  44. const uint8_t *plaintext, size_t nbytes,
  45. uint8_t *ciphertext,
  46. uint8_t tag[16]);
  47. /* .. c:function:: $DECL
  48. * ChaCha20-Poly1305 authenticated decryption.
  49. *
  50. * :return: 0 on success, non-zero on error. Plaintext is zeroed on error.
  51. *
  52. * :param key: key material.
  53. * :param nonce: per-message nonce.
  54. * :param header: header buffer.
  55. * :param nheader: number of header bytes.
  56. * :param ciphertext: ciphertext bytes to be decrypted.
  57. * :param nbytes: number of plaintext/ciphertext bytes.
  58. * :param plaintext: plaintext output buffer, nbytes in length.
  59. * :param tag: authentication tag output buffer.
  60. */
  61. int cf_chacha20poly1305_decrypt(const uint8_t key[32],
  62. const uint8_t nonce[12],
  63. const uint8_t *header, size_t nheader,
  64. const uint8_t *ciphertext, size_t nbytes,
  65. const uint8_t tag[16],
  66. uint8_t *plaintext);
  67. #endif