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. WOLFSSL_API
  42. int wc_ChaCha20Poly1305_Encrypt(
  43. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  44. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  45. const byte* inAAD, const word32 inAADLen,
  46. const byte* inPlaintext, const word32 inPlaintextLen,
  47. byte* outCiphertext,
  48. byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]);
  49. /*!
  50. \ingroup ChaCha20Poly1305
  51. \brief This function decrypts input ciphertext, inCiphertext, using the
  52. ChaCha20 stream cipher, into the output buffer, outPlaintext. It also
  53. performs Poly-1305 authentication, comparing the given inAuthTag to an
  54. authentication generated with the inAAD (arbitrary length additional
  55. authentication data). Note: If the generated authentication tag does
  56. not match the supplied authentication tag, the text is not decrypted.
  57. \return 0 Returned upon successfully decrypting the message
  58. \return BAD_FUNC_ARG Returned if any of the function arguments do not
  59. match what is expected
  60. \return MAC_CMP_FAILED_E Returned if the generated authentication tag
  61. does not match the supplied inAuthTag.
  62. \param inKey pointer to a buffer containing the 32 byte key to use for
  63. decryption
  64. \param inIv pointer to a buffer containing the 12 byte iv to use for
  65. decryption
  66. \param inAAD pointer to the buffer containing arbitrary length additional
  67. authenticated data (AAD)
  68. \param inAADLen length of the input AAD
  69. \param inCiphertext pointer to the buffer containing the ciphertext to
  70. decrypt
  71. \param outCiphertextLen the length of the ciphertext to decrypt
  72. \param inAuthTag pointer to the buffer containing the 16 byte digest
  73. for authentication
  74. \param outPlaintext pointer to the buffer in which to store the plaintext
  75. _Example_
  76. \code
  77. byte key[] = { // initialize 32 byte key };
  78. byte iv[] = { // initialize 12 byte key };
  79. byte inAAD[] = { // initialize AAD };
  80. byte cipher[] = { // initialize with received ciphertext };
  81. byte authTag[16] = { // initialize with received authentication tag };
  82. byte plain[sizeof(cipher)];
  83. int ret = wc_ChaCha20Poly1305_Decrypt(key, iv, inAAD, sizeof(inAAD),
  84. cipher, sizeof(cipher), plain, authTag);
  85. if(ret == MAC_CMP_FAILED_E) {
  86. // error during authentication
  87. } else if( ret != 0) {
  88. // error with function arguments
  89. }
  90. \endcode
  91. \sa wc_ChaCha20Poly1305_Encrypt
  92. \sa wc_ChaCha_*
  93. \sa wc_Poly1305*
  94. */
  95. WOLFSSL_API
  96. int wc_ChaCha20Poly1305_Decrypt(
  97. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  98. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  99. const byte* inAAD, const word32 inAADLen,
  100. const byte* inCiphertext, const word32 inCiphertextLen,
  101. const byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
  102. byte* outPlaintext);