Botan 3.5.0
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 if((ic >> 6) != 0) { // otherwise multiply overflows
29 return -1;
30 }
31
32 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
33 chacha->set_key(key, crypto_stream_chacha20_KEYBYTES);
34 chacha->set_iv(nonce, crypto_stream_chacha20_NONCEBYTES);
35 chacha->seek(ic * 64);
36 chacha->cipher(in, out, in_len);
37 return 0;
38}
39
40int Sodium::crypto_stream_chacha20_ietf(uint8_t out[], size_t out_len, const uint8_t nonce[], const uint8_t key[]) {
41 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
42 chacha->set_key(key, crypto_stream_chacha20_ietf_KEYBYTES);
43 chacha->set_iv(nonce, crypto_stream_chacha20_ietf_NONCEBYTES);
44 chacha->write_keystream(out, out_len);
45 return 0;
46}
47
49 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], const uint8_t key[]) {
50 return crypto_stream_chacha20_ietf_xor_ic(out, in, in_len, nonce, 0, key);
51}
52
54 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], uint32_t ic, const uint8_t key[]) {
55 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
56 chacha->set_key(key, crypto_stream_chacha20_ietf_KEYBYTES);
57 chacha->set_iv(nonce, crypto_stream_chacha20_ietf_NONCEBYTES);
58 chacha->seek(static_cast<uint64_t>(ic) * 64);
59 chacha->cipher(in, out, in_len);
60 return 0;
61}
62
63int Sodium::crypto_stream_xchacha20(uint8_t out[], size_t out_len, const uint8_t nonce[], const uint8_t key[]) {
64 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
65 chacha->set_key(key, crypto_stream_xchacha20_KEYBYTES);
66 chacha->set_iv(nonce, crypto_stream_xchacha20_NONCEBYTES);
67 chacha->write_keystream(out, out_len);
68 return 0;
69}
70
72 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], const uint8_t key[]) {
73 return crypto_stream_xchacha20_xor_ic(out, in, in_len, nonce, 0, key);
74}
75
77 uint8_t out[], const uint8_t in[], size_t in_len, const uint8_t nonce[], uint64_t ic, const uint8_t key[]) {
78 if((ic >> 6) != 0) { // otherwise multiply overflows
79 return -1;
80 }
81
82 auto chacha = StreamCipher::create_or_throw("ChaCha(20)");
83 chacha->set_key(key, crypto_stream_xchacha20_KEYBYTES);
84 chacha->set_iv(nonce, crypto_stream_xchacha20_NONCEBYTES);
85 chacha->seek(ic * 64);
86 chacha->cipher(in, out, in_len);
87 return 0;
88}
89
90} // 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[])