chacha20_poly1305.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. \ingroup ChaCha20Poly1305
  3. \brief This function encrypts an input message, inPlaintext, using the
  4. ChaCha20 stream cipher, into the output buffer, outCiphertext. It
  5. also performs Poly-1305 authentication (on the cipher text), and
  6. stores the generated authentication tag in the output buffer, outAuthTag.
  7. \return 0 Returned upon successfully encrypting the message
  8. \return BAD_FUNC_ARG returned if there is an error during the encryption
  9. process
  10. \param inKey pointer to a buffer containing the 32 byte key to use
  11. for encryption
  12. \param inIv pointer to a buffer containing the 12 byte iv to use for
  13. encryption
  14. \param inAAD pointer to the buffer containing arbitrary length additional
  15. authenticated data (AAD)
  16. \param inAADLen length of the input AAD
  17. \param inPlaintext pointer to the buffer containing the plaintext to
  18. encrypt
  19. \param inPlaintextLen the length of the plain text to encrypt
  20. \param outCiphertext pointer to the buffer in which to store the ciphertext
  21. \param outAuthTag pointer to a 16 byte wide buffer in which to store the
  22. authentication tag
  23. _Example_
  24. \code
  25. byte key[] = { // initialize 32 byte key };
  26. byte iv[] = { // initialize 12 byte key };
  27. byte inAAD[] = { // initialize AAD };
  28. byte plain[] = { // initialize message to encrypt };
  29. byte cipher[sizeof(plain)];
  30. byte authTag[16];
  31. int ret = wc_ChaCha20Poly1305_Encrypt(key, iv, inAAD, sizeof(inAAD),
  32. plain, sizeof(plain), cipher, authTag);
  33. if(ret != 0) {
  34. // error running encrypt
  35. }
  36. \endcode
  37. \sa wc_ChaCha20Poly1305_Decrypt
  38. \sa wc_ChaCha_*
  39. \sa wc_Poly1305*
  40. */
  41. int wc_ChaCha20Poly1305_Encrypt(
  42. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  43. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  44. const byte* inAAD, const word32 inAADLen,
  45. const byte* inPlaintext, const word32 inPlaintextLen,
  46. byte* outCiphertext,
  47. byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]);
  48. /*!
  49. \ingroup ChaCha20Poly1305
  50. \brief This function decrypts input ciphertext, inCiphertext, using the
  51. ChaCha20 stream cipher, into the output buffer, outPlaintext. It also
  52. performs Poly-1305 authentication, comparing the given inAuthTag to an
  53. authentication generated with the inAAD (arbitrary length additional
  54. authentication data). Note: If the generated authentication tag does
  55. not match the supplied authentication tag, the text is not decrypted.
  56. \return 0 Returned upon successfully decrypting the message
  57. \return BAD_FUNC_ARG Returned if any of the function arguments do not
  58. match what is expected
  59. \return MAC_CMP_FAILED_E Returned if the generated authentication tag
  60. does not match the supplied inAuthTag.
  61. \param inKey pointer to a buffer containing the 32 byte key to use for
  62. decryption
  63. \param inIv pointer to a buffer containing the 12 byte iv to use for
  64. decryption
  65. \param inAAD pointer to the buffer containing arbitrary length additional
  66. authenticated data (AAD)
  67. \param inAADLen length of the input AAD
  68. \param inCiphertext pointer to the buffer containing the ciphertext to
  69. decrypt
  70. \param outCiphertextLen the length of the ciphertext to decrypt
  71. \param inAuthTag pointer to the buffer containing the 16 byte digest
  72. for authentication
  73. \param outPlaintext pointer to the buffer in which to store the plaintext
  74. _Example_
  75. \code
  76. byte key[] = { // initialize 32 byte key };
  77. byte iv[] = { // initialize 12 byte key };
  78. byte inAAD[] = { // initialize AAD };
  79. byte cipher[] = { // initialize with received ciphertext };
  80. byte authTag[16] = { // initialize with received authentication tag };
  81. byte plain[sizeof(cipher)];
  82. int ret = wc_ChaCha20Poly1305_Decrypt(key, iv, inAAD, sizeof(inAAD),
  83. cipher, sizeof(cipher), authTag, plain);
  84. if(ret == MAC_CMP_FAILED_E) {
  85. // error during authentication
  86. } else if( ret != 0) {
  87. // error with function arguments
  88. }
  89. \endcode
  90. \sa wc_ChaCha20Poly1305_Encrypt
  91. \sa wc_ChaCha_*
  92. \sa wc_Poly1305*
  93. */
  94. int wc_ChaCha20Poly1305_Decrypt(
  95. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  96. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  97. const byte* inAAD, const word32 inAADLen,
  98. const byte* inCiphertext, const word32 inCiphertextLen,
  99. const byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
  100. byte* outPlaintext);