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