1 /*
2 Written in the D programming language.
3 For git maintenance (ensure at least one congruent line with originating C header):
4 #define crypto_stream_chacha20_H
5 */
6 
7 module deimos.sodium.crypto_stream_chacha20;
8 
9 /*
10  *  WARNING: This is just a stream cipher. It is NOT authenticated encryption.
11  *  While it provides some protection against eavesdropping, it does NOT
12  *  provide any security against active attacks.
13  *  Unless you know what you're doing, what you are looking for is probably
14  *  the crypto_box functions.
15  */
16 
17 import deimos.sodium.export_;
18 
19 
20 extern(C) @nogc:
21 
22 
23 enum crypto_stream_chacha20_KEYBYTES = 32U;
24 
25 size_t crypto_stream_chacha20_keybytes() pure @trusted;
26 
27 enum crypto_stream_chacha20_NONCEBYTES = 8U;
28 
29 size_t crypto_stream_chacha20_noncebytes() pure @trusted;
30 
31 alias crypto_stream_chacha20_MESSAGEBYTES_MAX = SODIUM_SIZE_MAX;
32 
33 size_t crypto_stream_chacha20_messagebytes_max() pure @trusted;
34 
35 /* ChaCha20 with a 64-bit nonce and a 64-bit counter, as originally designed */
36 
37 int crypto_stream_chacha20(ubyte* c, ulong clen,
38                            const(ubyte)* n, const(ubyte)* k) pure; // __attribute__ ((nonnull));
39 
40 int crypto_stream_chacha20_xor(ubyte* c, const(ubyte)* m,
41                                ulong mlen, const(ubyte)* n,
42                                const(ubyte)* k) pure; // __attribute__ ((nonnull));
43 
44 int crypto_stream_chacha20_xor_ic(ubyte* c, const(ubyte)* m,
45                                   ulong mlen,
46                                   const(ubyte)* n, ulong ic,
47                                   const(ubyte)* k) pure; // __attribute__ ((nonnull));
48 
49 void crypto_stream_chacha20_keygen(ref ubyte[crypto_stream_chacha20_KEYBYTES] k) nothrow @trusted; // __attribute__ ((nonnull));
50 
51 /* ChaCha20 with a 96-bit nonce and a 32-bit counter (IETF) */
52 
53 enum crypto_stream_chacha20_ietf_KEYBYTES = 32U;
54 
55 size_t crypto_stream_chacha20_ietf_keybytes() pure @trusted;
56 
57 enum crypto_stream_chacha20_ietf_NONCEBYTES = 12U;
58 
59 size_t crypto_stream_chacha20_ietf_noncebytes() pure @trusted;
60 
61 enum crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX =
62     SODIUM_MIN(SODIUM_SIZE_MAX, 64UL * (1UL << 32));
63 
64 size_t crypto_stream_chacha20_ietf_messagebytes_max() pure @trusted;
65 
66 int crypto_stream_chacha20_ietf(ubyte* c, ulong clen,
67                                 const(ubyte)* n, const(ubyte)* k) pure; // __attribute__ ((nonnull));
68 
69 int crypto_stream_chacha20_ietf_xor(ubyte* c, const(ubyte)* m,
70                                     ulong mlen, const(ubyte)* n,
71                                     const(ubyte)* k) pure; // __attribute__ ((nonnull));
72 
73 int crypto_stream_chacha20_ietf_xor_ic(ubyte* c, const(ubyte)* m,
74                                        ulong mlen,
75                                        const(ubyte)* n, uint ic,
76                                        const(ubyte)* k) pure; // __attribute__ ((nonnull));
77 
78 void crypto_stream_chacha20_ietf_keygen(ref ubyte[crypto_stream_chacha20_ietf_KEYBYTES] k) nothrow @trusted; // __attribute__ ((nonnull));
79 
80 /* Aliases */
81 
82 alias crypto_stream_chacha20_IETF_KEYBYTES         = crypto_stream_chacha20_ietf_KEYBYTES;
83 alias crypto_stream_chacha20_IETF_NONCEBYTES       = crypto_stream_chacha20_ietf_NONCEBYTES;
84 alias crypto_stream_chacha20_IETF_MESSAGEBYTES_MAX = crypto_stream_chacha20_ietf_MESSAGEBYTES_MAX;