Botan 3.14.1
Crypto and TLS for C&
sodium_chacha.cpp
Go to the documentation of this file.
1/*
2* (C) 2019 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/sodium.h>
8
9#include <botan/stream_cipher.h>
10
11namespace Botan {
12
13int Sodium::crypto_stream_chacha20(uint8_t out[], size_t out_len, const uint8_t nonce[], const uint8_t key[]) {
14 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
15 chacha->set_key(key, crypto_stream_chacha20_KEYBYTES);
16 chacha->set_iv(nonce, crypto_stream_chacha20_NONCEBYTES);
17 chacha->write_keystream(out, out_len);
18 return 0;
19}
20
22 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], const uint8_t key[]) {
23 return crypto_stream_chacha20_xor_ic(out, in, in_len, nonce, 0, key);
24}
25
27 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], uint64_t ic, const uint8_t key[]) {
28 // libsodium actually aborts the process for such arguments, which is why we don't support
29 // full range counters here, unlike in crypto_stream_salsa20_xor_ic
30 if((ic >> 58) != 0) { // otherwise multiply overflows
31 return -1;
32 }
33
34 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
35 chacha->set_key(key, crypto_stream_chacha20_KEYBYTES);
36 chacha->set_iv(nonce, crypto_stream_chacha20_NONCEBYTES);
37 chacha->seek(ic * 64);
38 chacha->cipher(in, out, in_len);
39 return 0;
40}
41
42int Sodium::crypto_stream_chacha20_ietf(uint8_t out[], size_t out_len, const uint8_t nonce[], const uint8_t key[]) {
43 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
44 chacha->set_key(key, crypto_stream_chacha20_ietf_KEYBYTES);
45 chacha->set_iv(nonce, crypto_stream_chacha20_ietf_NONCEBYTES);
46 chacha->write_keystream(out, out_len);
47 return 0;
48}
49
51 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], const uint8_t key[]) {
52 return crypto_stream_chacha20_ietf_xor_ic(out, in, in_len, nonce, 0, key);
53}
54
56 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], uint32_t ic, const uint8_t key[]) {
57 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
58 chacha->set_key(key, crypto_stream_chacha20_ietf_KEYBYTES);
59 chacha->set_iv(nonce, crypto_stream_chacha20_ietf_NONCEBYTES);
60 chacha->seek(static_cast<uint64_t>(ic) * 64);
61 chacha->cipher(in, out, in_len);
62 return 0;
63}
64
65int Sodium::crypto_stream_xchacha20(uint8_t out[], size_t out_len, const uint8_t nonce[], const uint8_t key[]) {
66 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
67 chacha->set_key(key, crypto_stream_xchacha20_KEYBYTES);
68 chacha->set_iv(nonce, crypto_stream_xchacha20_NONCEBYTES);
69 chacha->write_keystream(out, out_len);
70 return 0;
71}
72
74 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], const uint8_t key[]) {
75 return crypto_stream_xchacha20_xor_ic(out, in, in_len, nonce, 0, key);
76}
77
79 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], uint64_t ic, const uint8_t key[]) {
80 // libsodium actually aborts the process for such arguments, which is why we don't support
81 // full range counters here, unlike in crypto_stream_salsa20_xor_ic
82 if((ic >> 58) != 0) { // otherwise multiply overflows
83 return -1;
84 }
85
86 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
87 chacha->set_key(key, crypto_stream_xchacha20_KEYBYTES);
88 chacha->set_iv(nonce, crypto_stream_xchacha20_NONCEBYTES);
89 chacha->seek(ic * 64);
90 chacha->cipher(in, out, in_len);
91 return 0;
92}
93
94} // namespace Botan
static std::unique_ptr< StreamCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
int crypto_stream_xchacha20_xor(uint8_t out[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_stream_chacha20_ietf_xor(uint8_t out[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[])
@ crypto_stream_chacha20_ietf_KEYBYTES
Definition sodium.h:129
@ crypto_stream_chacha20_ietf_NONCEBYTES
Definition sodium.h:131
@ crypto_stream_xchacha20_KEYBYTES
Definition sodium.h:135
@ crypto_stream_xchacha20_NONCEBYTES
Definition sodium.h:137
@ crypto_stream_chacha20_NONCEBYTES
Definition sodium.h:128
@ crypto_stream_chacha20_KEYBYTES
Definition sodium.h:126
int crypto_stream_xchacha20_xor_ic(uint8_t out[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], uint64_t ic, const uint8_t key[])
int crypto_stream_chacha20_xor_ic(uint8_t out[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], uint64_t ic, const uint8_t key[])
int crypto_stream_chacha20(uint8_t out[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_stream_chacha20_ietf(uint8_t out[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_stream_chacha20_xor(uint8_t out[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_stream_xchacha20(uint8_t out[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_stream_chacha20_ietf_xor_ic(uint8_t out[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], uint32_t ic, const uint8_t key[])