summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/compat/sodium/sodium_chacha.cpp
blob: fed7a52f6f592403a5a9964d396a07fb68fd8e8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* (C) 2019 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/sodium.h>
#include <botan/stream_cipher.h>

namespace Botan {

int Sodium::crypto_stream_chacha20(uint8_t out[], size_t out_len,
                                   const uint8_t nonce[], const uint8_t key[])
   {
   auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
   chacha->set_key(key, crypto_stream_chacha20_KEYBYTES);
   chacha->set_iv(nonce, crypto_stream_chacha20_NONCEBYTES);
   chacha->write_keystream(out, out_len);
   return 0;
   }

int Sodium::crypto_stream_chacha20_xor(uint8_t out[], const uint8_t in[],
                                       size_t in_len, const uint8_t nonce[],
                                       const uint8_t key[])
   {
   return crypto_stream_chacha20_xor_ic(out, in, in_len, nonce, 0, key);
   }

int Sodium::crypto_stream_chacha20_xor_ic(uint8_t out[], const uint8_t in[],
                                          size_t in_len,
                                          const uint8_t nonce[], uint64_t ic,
                                          const uint8_t key[])
   {
   if((ic >> 6) != 0) // otherwise multiply overflows
      return -1;

   auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
   chacha->set_key(key, crypto_stream_chacha20_KEYBYTES);
   chacha->set_iv(nonce, crypto_stream_chacha20_NONCEBYTES);
   chacha->seek(ic * 64);
   chacha->cipher(in, out, in_len);
   return 0;
   }

int Sodium::crypto_stream_chacha20_ietf(uint8_t out[], size_t out_len,
                                        const uint8_t nonce[], const uint8_t key[])
   {
   auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
   chacha->set_key(key, crypto_stream_chacha20_ietf_KEYBYTES);
   chacha->set_iv(nonce, crypto_stream_chacha20_ietf_NONCEBYTES);
   chacha->write_keystream(out, out_len);
   return 0;
   }

int Sodium::crypto_stream_chacha20_ietf_xor(uint8_t out[],
                                            const uint8_t in[], size_t in_len,
                                            const uint8_t nonce[],
                                            const uint8_t key[])
   {
   return crypto_stream_chacha20_ietf_xor_ic(out, in, in_len, nonce, 0, key);
   }

int Sodium::crypto_stream_chacha20_ietf_xor_ic(uint8_t out[],
                                               const uint8_t in[], size_t in_len,
                                               const uint8_t nonce[], uint32_t ic,
                                               const uint8_t key[])
   {
   auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
   chacha->set_key(key, crypto_stream_chacha20_ietf_KEYBYTES);
   chacha->set_iv(nonce, crypto_stream_chacha20_ietf_NONCEBYTES);
   chacha->seek(static_cast<uint64_t>(ic) * 64);
   chacha->cipher(in, out, in_len);
   return 0;
   }

int Sodium::crypto_stream_xchacha20(uint8_t out[], size_t out_len,
                                    const uint8_t nonce[], const uint8_t key[])
   {
   auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
   chacha->set_key(key, crypto_stream_xchacha20_KEYBYTES);
   chacha->set_iv(nonce, crypto_stream_xchacha20_NONCEBYTES);
   chacha->write_keystream(out, out_len);
   return 0;
   }

int Sodium::crypto_stream_xchacha20_xor(uint8_t out[], const uint8_t in[],
                                       size_t in_len, const uint8_t nonce[],
                                       const uint8_t key[])
   {
   return crypto_stream_xchacha20_xor_ic(out, in, in_len, nonce, 0, key);
   }

int Sodium::crypto_stream_xchacha20_xor_ic(uint8_t out[], const uint8_t in[],
                                           size_t in_len,
                                           const uint8_t nonce[], uint64_t ic,
                                           const uint8_t key[])
   {
   if((ic >> 6) != 0) // otherwise multiply overflows
      return -1;

   auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
   chacha->set_key(key, crypto_stream_xchacha20_KEYBYTES);
   chacha->set_iv(nonce, crypto_stream_xchacha20_NONCEBYTES);
   chacha->seek(ic * 64);
   chacha->cipher(in, out, in_len);
   return 0;
   }

}