crypto_stream_chacha20.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef crypto_stream_chacha20_H
  2. #define crypto_stream_chacha20_H
  3. /*
  4. * WARNING: This is just a stream cipher. It is NOT authenticated encryption.
  5. * While it provides some protection against eavesdropping, it does NOT
  6. * provide any security against active attacks.
  7. * Unless you know what you're doing, what you are looking for is probably
  8. * the crypto_box functions.
  9. */
  10. #include <stddef.h>
  11. #include <stdint.h>
  12. #include "export.h"
  13. #ifdef __cplusplus
  14. # ifdef __GNUC__
  15. # pragma GCC diagnostic ignored "-Wlong-long"
  16. # endif
  17. extern "C" {
  18. #endif
  19. #define crypto_stream_chacha20_KEYBYTES 32U
  20. SODIUM_EXPORT
  21. size_t crypto_stream_chacha20_keybytes(void);
  22. #define crypto_stream_chacha20_NONCEBYTES 8U
  23. SODIUM_EXPORT
  24. size_t crypto_stream_chacha20_noncebytes(void);
  25. #define crypto_stream_chacha20_MESSAGEBYTES_MAX SODIUM_SIZE_MAX
  26. SODIUM_EXPORT
  27. size_t crypto_stream_chacha20_messagebytes_max(void);
  28. /* ChaCha20 with a 64-bit nonce and a 64-bit counter, as originally designed */
  29. SODIUM_EXPORT
  30. int crypto_stream_chacha20(unsigned char *c, unsigned long long clen,
  31. const unsigned char *n, const unsigned char *k)
  32. __attribute__ ((nonnull));
  33. SODIUM_EXPORT
  34. int crypto_stream_chacha20_xor(unsigned char *c, const unsigned char *m,
  35. unsigned long long mlen, const unsigned char *n,
  36. const unsigned char *k)
  37. __attribute__ ((nonnull));
  38. SODIUM_EXPORT
  39. int crypto_stream_chacha20_xor_ic(unsigned char *c, const unsigned char *m,
  40. unsigned long long mlen,
  41. const unsigned char *n, uint64_t ic,
  42. const unsigned char *k)
  43. __attribute__ ((nonnull));
  44. SODIUM_EXPORT
  45. void crypto_stream_chacha20_keygen(unsigned char k[crypto_stream_chacha20_KEYBYTES])
  46. __attribute__ ((nonnull));
  47. /* ChaCha20 with a 96-bit nonce and a 32-bit counter (IETF) */
  48. #define crypto_stream_chacha20_ietf_KEYBYTES 32U
  49. SODIUM_EXPORT
  50. size_t crypto_stream_chacha20_ietf_keybytes(void);
  51. #define crypto_stream_chacha20_ietf_NONCEBYTES 12U
  52. SODIUM_EXPORT
  53. size_t crypto_stream_chacha20_ietf_noncebytes(void);
  54. #define crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX \
  55. SODIUM_MIN(SODIUM_SIZE_MAX, 64ULL * (1ULL << 32))
  56. SODIUM_EXPORT
  57. size_t crypto_stream_chacha20_ietf_messagebytes_max(void);
  58. SODIUM_EXPORT
  59. int crypto_stream_chacha20_ietf(unsigned char *c, unsigned long long clen,
  60. const unsigned char *n, const unsigned char *k)
  61. __attribute__ ((nonnull));
  62. SODIUM_EXPORT
  63. int crypto_stream_chacha20_ietf_xor(unsigned char *c, const unsigned char *m,
  64. unsigned long long mlen, const unsigned char *n,
  65. const unsigned char *k)
  66. __attribute__ ((nonnull));
  67. SODIUM_EXPORT
  68. int crypto_stream_chacha20_ietf_xor_ic(unsigned char *c, const unsigned char *m,
  69. unsigned long long mlen,
  70. const unsigned char *n, uint32_t ic,
  71. const unsigned char *k)
  72. __attribute__ ((nonnull));
  73. SODIUM_EXPORT
  74. void crypto_stream_chacha20_ietf_keygen(unsigned char k[crypto_stream_chacha20_ietf_KEYBYTES])
  75. __attribute__ ((nonnull));
  76. /* Aliases */
  77. #define crypto_stream_chacha20_IETF_KEYBYTES crypto_stream_chacha20_ietf_KEYBYTES
  78. #define crypto_stream_chacha20_IETF_NONCEBYTES crypto_stream_chacha20_ietf_NONCEBYTES
  79. #define crypto_stream_chacha20_IETF_MESSAGEBYTES_MAX crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX
  80. #ifdef __cplusplus
  81. }
  82. #endif
  83. #endif