chacha20_poly1305.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* chacha20_poly1305.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. DESCRIPTION
  23. This library contains implementation for the ChaCha20 stream cipher and
  24. the Poly1305 authenticator, both as as combined-mode,
  25. or Authenticated Encryption with Additional Data (AEAD) algorithm.
  26. */
  27. /*!
  28. \file wolfssl/wolfcrypt/chacha20_poly1305.h
  29. */
  30. #ifndef WOLF_CRYPT_CHACHA20_POLY1305_H
  31. #define WOLF_CRYPT_CHACHA20_POLY1305_H
  32. #include <wolfssl/wolfcrypt/types.h>
  33. #include <wolfssl/wolfcrypt/chacha.h>
  34. #include <wolfssl/wolfcrypt/poly1305.h>
  35. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #define CHACHA20_POLY1305_AEAD_KEYSIZE 32
  40. #define CHACHA20_POLY1305_AEAD_IV_SIZE 12
  41. #define CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE 16
  42. #define CHACHA20_POLY1305_MAX 4294967295U
  43. #define XCHACHA20_POLY1305_AEAD_NONCE_SIZE 24
  44. enum {
  45. CHACHA20_POLY_1305_ENC_TYPE = 8, /* cipher unique type */
  46. /* AEAD Cipher Direction */
  47. CHACHA20_POLY1305_AEAD_DECRYPT = 0,
  48. CHACHA20_POLY1305_AEAD_ENCRYPT = 1,
  49. /* AEAD State */
  50. CHACHA20_POLY1305_STATE_INIT = 0,
  51. CHACHA20_POLY1305_STATE_READY = 1,
  52. CHACHA20_POLY1305_STATE_AAD = 2,
  53. CHACHA20_POLY1305_STATE_DATA = 3,
  54. };
  55. typedef struct ChaChaPoly_Aead {
  56. ChaCha chacha;
  57. Poly1305 poly;
  58. word32 aadLen;
  59. word32 dataLen;
  60. byte state;
  61. byte isEncrypt:1;
  62. } ChaChaPoly_Aead;
  63. /*
  64. * The IV for this implementation is 96 bits to give the most flexibility.
  65. *
  66. * Some protocols may have unique per-invocation inputs that are not
  67. * 96-bit in length. For example, IPsec may specify a 64-bit nonce. In
  68. * such a case, it is up to the protocol document to define how to
  69. * transform the protocol nonce into a 96-bit nonce, for example by
  70. * concatenating a constant value.
  71. */
  72. WOLFSSL_ABI WOLFSSL_API
  73. int wc_ChaCha20Poly1305_Encrypt(
  74. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  75. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  76. const byte* inAAD, word32 inAADLen,
  77. const byte* inPlaintext, word32 inPlaintextLen,
  78. byte* outCiphertext,
  79. byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]);
  80. WOLFSSL_ABI WOLFSSL_API
  81. int wc_ChaCha20Poly1305_Decrypt(
  82. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  83. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  84. const byte* inAAD, word32 inAADLen,
  85. const byte* inCiphertext, word32 inCiphertextLen,
  86. const byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
  87. byte* outPlaintext);
  88. WOLFSSL_API
  89. int wc_ChaCha20Poly1305_CheckTag(
  90. const byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
  91. const byte authTagChk[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]);
  92. /* Implementation of AEAD, which includes support for adding
  93. data, then final calculation of authentication tag */
  94. WOLFSSL_API int wc_ChaCha20Poly1305_Init(ChaChaPoly_Aead* aead,
  95. const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
  96. const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
  97. int isEncrypt);
  98. WOLFSSL_API int wc_ChaCha20Poly1305_UpdateAad(ChaChaPoly_Aead* aead,
  99. const byte* inAAD, word32 inAADLen);
  100. WOLFSSL_API int wc_ChaCha20Poly1305_UpdateData(ChaChaPoly_Aead* aead,
  101. const byte* inData, byte* outData, word32 dataLen);
  102. WOLFSSL_API int wc_ChaCha20Poly1305_Final(ChaChaPoly_Aead* aead,
  103. byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE]);
  104. #ifdef HAVE_XCHACHA
  105. WOLFSSL_API int wc_XChaCha20Poly1305_Init(
  106. ChaChaPoly_Aead* aead,
  107. const byte *ad, word32 ad_len,
  108. const byte *inKey, word32 inKeySz,
  109. const byte *inIV, word32 inIVSz,
  110. int isEncrypt);
  111. WOLFSSL_API int wc_XChaCha20Poly1305_Encrypt(
  112. byte *dst, size_t dst_space,
  113. const byte *src, size_t src_len,
  114. const byte *ad, size_t ad_len,
  115. const byte *nonce, size_t nonce_len,
  116. const byte *key, size_t key_len);
  117. WOLFSSL_API int wc_XChaCha20Poly1305_Decrypt(
  118. byte *dst, size_t dst_space,
  119. const byte *src, size_t src_len,
  120. const byte *ad, size_t ad_len,
  121. const byte *nonce, size_t nonce_len,
  122. const byte *key, size_t key_len);
  123. #endif /* HAVE_XCHACHA */
  124. #ifdef __cplusplus
  125. } /* extern "C" */
  126. #endif
  127. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  128. #endif /* WOLF_CRYPT_CHACHA20_POLY1305_H */