/src/botan/src/lib/modes/aead/chacha20poly1305/chacha20poly1305.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * ChaCha20Poly1305 AEAD |
3 | | * (C) 2014,2016,2018 Jack Lloyd |
4 | | * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #include <botan/chacha20poly1305.h> |
10 | | #include <botan/loadstor.h> |
11 | | |
12 | | namespace Botan { |
13 | | |
14 | | ChaCha20Poly1305_Mode::ChaCha20Poly1305_Mode() : |
15 | | m_chacha(StreamCipher::create("ChaCha")), |
16 | | m_poly1305(MessageAuthenticationCode::create("Poly1305")) |
17 | 123 | { |
18 | 123 | if(!m_chacha || !m_poly1305) |
19 | 0 | throw Algorithm_Not_Found("ChaCha20Poly1305"); |
20 | 123 | } |
21 | | |
22 | | bool ChaCha20Poly1305_Mode::valid_nonce_length(size_t n) const |
23 | 177 | { |
24 | 177 | return (n == 8 || n == 12 || n == 24); |
25 | 177 | } |
26 | | |
27 | | void ChaCha20Poly1305_Mode::clear() |
28 | 0 | { |
29 | 0 | m_chacha->clear(); |
30 | 0 | m_poly1305->clear(); |
31 | 0 | reset(); |
32 | 0 | } |
33 | | |
34 | | void ChaCha20Poly1305_Mode::reset() |
35 | 0 | { |
36 | 0 | m_ad.clear(); |
37 | 0 | m_ctext_len = 0; |
38 | 0 | m_nonce_len = 0; |
39 | 0 | } |
40 | | |
41 | | void ChaCha20Poly1305_Mode::key_schedule(const uint8_t key[], size_t length) |
42 | 123 | { |
43 | 123 | m_chacha->set_key(key, length); |
44 | 123 | } |
45 | | |
46 | | void ChaCha20Poly1305_Mode::set_associated_data(const uint8_t ad[], size_t length) |
47 | 177 | { |
48 | 177 | if(m_ctext_len > 0 || m_nonce_len > 0) |
49 | 0 | throw Invalid_State("Cannot set AD for ChaCha20Poly1305 while processing a message"); |
50 | 177 | m_ad.assign(ad, ad + length); |
51 | 177 | } |
52 | | |
53 | | void ChaCha20Poly1305_Mode::update_len(size_t len) |
54 | 354 | { |
55 | 354 | uint8_t len8[8] = { 0 }; |
56 | 354 | store_le(static_cast<uint64_t>(len), len8); |
57 | 354 | m_poly1305->update(len8, 8); |
58 | 354 | } |
59 | | |
60 | | void ChaCha20Poly1305_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) |
61 | 177 | { |
62 | 177 | if(!valid_nonce_length(nonce_len)) |
63 | 0 | throw Invalid_IV_Length(name(), nonce_len); |
64 | 177 | |
65 | 177 | m_ctext_len = 0; |
66 | 177 | m_nonce_len = nonce_len; |
67 | 177 | |
68 | 177 | m_chacha->set_iv(nonce, nonce_len); |
69 | 177 | |
70 | 177 | secure_vector<uint8_t> first_block(64); |
71 | 177 | m_chacha->write_keystream(first_block.data(), first_block.size()); |
72 | 177 | |
73 | 177 | m_poly1305->set_key(first_block.data(), 32); |
74 | 177 | // Remainder of first block is discarded |
75 | 177 | |
76 | 177 | m_poly1305->update(m_ad); |
77 | 177 | |
78 | 177 | if(cfrg_version()) |
79 | 177 | { |
80 | 177 | if(m_ad.size() % 16) |
81 | 177 | { |
82 | 177 | const uint8_t zeros[16] = { 0 }; |
83 | 177 | m_poly1305->update(zeros, 16 - m_ad.size() % 16); |
84 | 177 | } |
85 | 177 | } |
86 | 0 | else |
87 | 0 | { |
88 | 0 | update_len(m_ad.size()); |
89 | 0 | } |
90 | 177 | } |
91 | | |
92 | | size_t ChaCha20Poly1305_Encryption::process(uint8_t buf[], size_t sz) |
93 | 122 | { |
94 | 122 | m_chacha->cipher1(buf, sz); |
95 | 122 | m_poly1305->update(buf, sz); // poly1305 of ciphertext |
96 | 122 | m_ctext_len += sz; |
97 | 122 | return sz; |
98 | 122 | } |
99 | | |
100 | | void ChaCha20Poly1305_Encryption::finish(secure_vector<uint8_t>& buffer, size_t offset) |
101 | 122 | { |
102 | 122 | update(buffer, offset); |
103 | 122 | if(cfrg_version()) |
104 | 122 | { |
105 | 122 | if(m_ctext_len % 16) |
106 | 59 | { |
107 | 59 | const uint8_t zeros[16] = { 0 }; |
108 | 59 | m_poly1305->update(zeros, 16 - m_ctext_len % 16); |
109 | 59 | } |
110 | 122 | update_len(m_ad.size()); |
111 | 122 | } |
112 | 122 | update_len(m_ctext_len); |
113 | 122 | |
114 | 122 | const secure_vector<uint8_t> mac = m_poly1305->final(); |
115 | 122 | buffer += std::make_pair(mac.data(), tag_size()); |
116 | 122 | m_ctext_len = 0; |
117 | 122 | m_nonce_len = 0; |
118 | 122 | } |
119 | | |
120 | | size_t ChaCha20Poly1305_Decryption::process(uint8_t buf[], size_t sz) |
121 | 0 | { |
122 | 0 | m_poly1305->update(buf, sz); // poly1305 of ciphertext |
123 | 0 | m_chacha->cipher1(buf, sz); |
124 | 0 | m_ctext_len += sz; |
125 | 0 | return sz; |
126 | 0 | } |
127 | | |
128 | | void ChaCha20Poly1305_Decryption::finish(secure_vector<uint8_t>& buffer, size_t offset) |
129 | 55 | { |
130 | 55 | BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane"); |
131 | 55 | const size_t sz = buffer.size() - offset; |
132 | 55 | uint8_t* buf = buffer.data() + offset; |
133 | 55 | |
134 | 55 | BOTAN_ASSERT(sz >= tag_size(), "Have the tag as part of final input"); |
135 | 55 | |
136 | 55 | const size_t remaining = sz - tag_size(); |
137 | 55 | |
138 | 55 | if(remaining) |
139 | 54 | { |
140 | 54 | m_poly1305->update(buf, remaining); // poly1305 of ciphertext |
141 | 54 | m_chacha->cipher1(buf, remaining); |
142 | 54 | m_ctext_len += remaining; |
143 | 54 | } |
144 | 55 | |
145 | 55 | if(cfrg_version()) |
146 | 55 | { |
147 | 55 | if(m_ctext_len % 16) |
148 | 42 | { |
149 | 42 | const uint8_t zeros[16] = { 0 }; |
150 | 42 | m_poly1305->update(zeros, 16 - m_ctext_len % 16); |
151 | 42 | } |
152 | 55 | update_len(m_ad.size()); |
153 | 55 | } |
154 | 55 | |
155 | 55 | update_len(m_ctext_len); |
156 | 55 | const secure_vector<uint8_t> mac = m_poly1305->final(); |
157 | 55 | |
158 | 55 | const uint8_t* included_tag = &buf[remaining]; |
159 | 55 | |
160 | 55 | m_ctext_len = 0; |
161 | 55 | m_nonce_len = 0; |
162 | 55 | |
163 | 55 | if(!constant_time_compare(mac.data(), included_tag, tag_size())) |
164 | 55 | throw Invalid_Authentication_Tag("ChaCha20Poly1305 tag check failed"); |
165 | 0 | buffer.resize(offset + remaining); |
166 | 0 | } |
167 | | |
168 | | } |