Botan 3.14.1
Crypto and TLS for C&
sodium_secretbox.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/mac.h>
10#include <botan/mem_ops.h>
11#include <botan/secmem.h>
12#include <botan/stream_cipher.h>
13#include <botan/internal/ct_utils.h>
14#include <cstring>
15
16namespace Botan {
17
18namespace {
19
20bool partially_overlaps(const uint8_t x[], const uint8_t y[], size_t len) {
21 const auto xi = reinterpret_cast<uintptr_t>(x);
22 const auto yi = reinterpret_cast<uintptr_t>(y);
23 return (xi > yi && xi - yi < len) || (yi > xi && yi - xi < len);
24}
25
26} // namespace
27
29 uint8_t ctext[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[]) {
30 if(ptext_len < 32) {
31 return -1;
32 }
33
34 auto salsa = StreamCipher::create_or_throw("Salsa20");
35 salsa->set_key(key, crypto_secretbox_KEYBYTES);
36 salsa->set_iv(nonce, crypto_secretbox_NONCEBYTES);
37
38 secure_vector<uint8_t> auth_key(32);
39 salsa->write_keystream(auth_key.data(), auth_key.size());
40
41 salsa->cipher(ptext + 32, ctext + 32, ptext_len - 32);
42
43 auto poly1305 = MessageAuthenticationCode::create_or_throw("Poly1305");
44 poly1305->set_key(auth_key);
45 poly1305->update(ctext + 32, ptext_len - 32);
46 poly1305->final(ctext + 16);
47
48 clear_mem(ctext, 16);
49 return 0;
50}
51
53 uint8_t ptext[], const uint8_t ctext[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[]) {
55 return -1;
56 }
57
58 auto salsa = StreamCipher::create_or_throw("Salsa20");
59 salsa->set_key(key, crypto_secretbox_KEYBYTES);
60 salsa->set_iv(nonce, crypto_secretbox_NONCEBYTES);
61
62 secure_vector<uint8_t> auth_key(32);
63 salsa->write_keystream(auth_key.data(), auth_key.size());
64
65 auto poly1305 = MessageAuthenticationCode::create_or_throw("Poly1305");
66 poly1305->set_key(auth_key);
67 poly1305->update(ctext + 32, ctext_len - 32);
68 secure_vector<uint8_t> computed = poly1305->final();
69
70 if(CT::is_not_equal(computed.data(), ctext + 16, 16).as_bool()) {
71 return -1;
72 }
73
74 salsa->cipher(ctext + 32, ptext + 32, ctext_len - 32);
75
76 clear_mem(ptext, 32);
77 return 0;
78}
79
81 uint8_t mac[],
82 const uint8_t ptext[],
83 size_t ptext_len,
84 const uint8_t nonce[],
85 const uint8_t key[]) {
86 auto salsa = StreamCipher::create_or_throw("Salsa20");
87 salsa->set_key(key, crypto_secretbox_KEYBYTES);
88 salsa->set_iv(nonce, crypto_secretbox_NONCEBYTES);
89
90 secure_vector<uint8_t> auth_key(32);
91 salsa->write_keystream(auth_key.data(), auth_key.size());
92
93 // The easy API places the ciphertext 16 bytes after the plaintext when used in-place
94 if(partially_overlaps(ctext, ptext, ptext_len)) {
95 std::memmove(ctext, ptext, ptext_len);
96 ptext = ctext;
97 }
98
99 salsa->cipher(ptext, ctext, ptext_len);
100
101 auto poly1305 = MessageAuthenticationCode::create_or_throw("Poly1305");
102 poly1305->set_key(auth_key);
103 poly1305->update(ctext, ptext_len);
104 poly1305->final(mac);
105
106 return 0;
107}
108
110 const uint8_t ctext[],
111 const uint8_t mac[],
112 size_t ctext_len,
113 const uint8_t nonce[],
114 const uint8_t key[]) {
115 auto salsa = StreamCipher::create_or_throw("Salsa20");
116 salsa->set_key(key, crypto_secretbox_KEYBYTES);
117 salsa->set_iv(nonce, crypto_secretbox_NONCEBYTES);
118
119 secure_vector<uint8_t> auth_key(32);
120 salsa->write_keystream(auth_key.data(), auth_key.size());
121
122 auto poly1305 = MessageAuthenticationCode::create_or_throw("Poly1305");
123 poly1305->set_key(auth_key);
124 poly1305->update(ctext, ctext_len);
125 secure_vector<uint8_t> computed_mac = poly1305->final();
126
127 if(!CT::is_equal(mac, computed_mac.data(), computed_mac.size()).as_bool()) {
128 return -1;
129 }
130
131 // Verification only
132 if(ptext == nullptr) {
133 return 0;
134 }
135
136 if(partially_overlaps(ptext, ctext, ctext_len)) {
137 std::memmove(ptext, ctext, ctext_len);
138 ctext = ptext;
139 }
140
141 salsa->cipher(ctext, ptext, ctext_len);
142
143 return 0;
144}
145
146} // namespace Botan
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:142
static std::unique_ptr< StreamCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
constexpr CT::Mask< T > is_not_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:843
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:803
@ crypto_secretbox_KEYBYTES
Definition sodium.h:104
@ crypto_box_curve25519xsalsa20poly1305_ZEROBYTES
Definition sodium.h:58
@ crypto_secretbox_NONCEBYTES
Definition sodium.h:107
int crypto_secretbox_xsalsa20poly1305_open(uint8_t ptext[], const uint8_t ctext[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_secretbox_open_detached(uint8_t ptext[], const uint8_t ctext[], const uint8_t mac[], size_t ctext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_secretbox_xsalsa20poly1305(uint8_t ctext[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[])
int crypto_secretbox_detached(uint8_t ctext[], uint8_t mac[], const uint8_t ptext[], size_t ptext_len, const uint8_t nonce[], const uint8_t key[])
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118