Coverage Report

Created: 2024-11-21 07:00

/src/botan/src/lib/modes/aead/siv/siv.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* SIV Mode Encryption
3
* (C) 2013,2017 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/internal/siv.h>
10
11
#include <botan/block_cipher.h>
12
#include <botan/internal/cmac.h>
13
#include <botan/internal/ct_utils.h>
14
#include <botan/internal/ctr.h>
15
#include <botan/internal/poly_dbl.h>
16
17
namespace Botan {
18
19
SIV_Mode::SIV_Mode(std::unique_ptr<BlockCipher> cipher) :
20
      m_name(cipher->name() + "/SIV"),
21
      m_bs(cipher->block_size()),
22
      m_ctr(std::make_unique<CTR_BE>(cipher->new_object(), 8)),
23
0
      m_mac(std::make_unique<CMAC>(std::move(cipher))) {
24
   // Not really true but only 128 bit allowed at the moment
25
0
   if(m_bs != 16) {
26
0
      throw Invalid_Argument("SIV requires a 128 bit block cipher");
27
0
   }
28
0
}
29
30
0
SIV_Mode::~SIV_Mode() = default;
31
32
0
void SIV_Mode::clear() {
33
0
   m_ctr->clear();
34
0
   m_mac->clear();
35
0
   reset();
36
0
}
37
38
0
void SIV_Mode::reset() {
39
0
   m_nonce.clear();
40
0
   m_msg_buf.clear();
41
0
   m_ad_macs.clear();
42
0
}
43
44
0
std::string SIV_Mode::name() const {
45
0
   return m_name;
46
0
}
47
48
0
bool SIV_Mode::valid_nonce_length(size_t /*nonce_len*/) const {
49
0
   return true;
50
0
}
51
52
0
size_t SIV_Mode::update_granularity() const {
53
0
   return 1;
54
0
}
55
56
0
size_t SIV_Mode::ideal_granularity() const {
57
   // Completely arbitrary value:
58
0
   return 128;
59
0
}
60
61
0
bool SIV_Mode::requires_entire_message() const {
62
0
   return true;
63
0
}
64
65
0
Key_Length_Specification SIV_Mode::key_spec() const {
66
0
   return m_mac->key_spec().multiple(2);
67
0
}
68
69
0
bool SIV_Mode::has_keying_material() const {
70
0
   return m_ctr->has_keying_material() && m_mac->has_keying_material();
71
0
}
72
73
0
void SIV_Mode::key_schedule(std::span<const uint8_t> key) {
74
0
   const size_t keylen = key.size() / 2;
75
0
   m_mac->set_key(key.first(keylen));
76
0
   m_ctr->set_key(key.last(keylen));
77
0
   m_ad_macs.clear();
78
0
}
79
80
0
size_t SIV_Mode::maximum_associated_data_inputs() const {
81
0
   return block_size() * 8 - 2;
82
0
}
83
84
0
void SIV_Mode::set_associated_data_n(size_t n, std::span<const uint8_t> ad) {
85
0
   const size_t max_ads = maximum_associated_data_inputs();
86
0
   if(n > max_ads) {
87
0
      throw Invalid_Argument(name() + " allows no more than " + std::to_string(max_ads) + " ADs");
88
0
   }
89
90
0
   if(n >= m_ad_macs.size()) {
91
0
      m_ad_macs.resize(n + 1);
92
0
   }
93
94
0
   m_ad_macs[n] = m_mac->process(ad);
95
0
}
96
97
0
void SIV_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
98
0
   if(!valid_nonce_length(nonce_len)) {
99
0
      throw Invalid_IV_Length(name(), nonce_len);
100
0
   }
101
102
0
   if(nonce_len) {
103
0
      m_nonce = m_mac->process(nonce, nonce_len);
104
0
   } else {
105
0
      m_nonce.clear();
106
0
   }
107
108
0
   m_msg_buf.clear();
109
0
}
110
111
0
size_t SIV_Mode::process_msg(uint8_t buf[], size_t sz) {
112
   // all output is saved for processing in finish
113
0
   m_msg_buf.insert(m_msg_buf.end(), buf, buf + sz);
114
0
   return 0;
115
0
}
116
117
0
secure_vector<uint8_t> SIV_Mode::S2V(const uint8_t* text, size_t text_len) {
118
0
   const std::vector<uint8_t> zeros(block_size());
119
120
0
   secure_vector<uint8_t> V = m_mac->process(zeros.data(), zeros.size());
121
122
0
   for(size_t i = 0; i != m_ad_macs.size(); ++i) {
123
0
      poly_double_n(V.data(), V.size());
124
0
      V ^= m_ad_macs[i];
125
0
   }
126
127
0
   if(!m_nonce.empty()) {
128
0
      poly_double_n(V.data(), V.size());
129
0
      V ^= m_nonce;
130
0
   }
131
132
0
   if(text_len < block_size()) {
133
0
      poly_double_n(V.data(), V.size());
134
0
      xor_buf(V.data(), text, text_len);
135
0
      V[text_len] ^= 0x80;
136
0
      return m_mac->process(V);
137
0
   }
138
139
0
   m_mac->update(text, text_len - block_size());
140
0
   xor_buf(V.data(), &text[text_len - block_size()], block_size());
141
0
   m_mac->update(V);
142
143
0
   return m_mac->final();
144
0
}
145
146
0
void SIV_Mode::set_ctr_iv(secure_vector<uint8_t> V) {
147
0
   V[m_bs - 8] &= 0x7F;
148
0
   V[m_bs - 4] &= 0x7F;
149
150
0
   ctr().set_iv(V.data(), V.size());
151
0
}
152
153
0
void SIV_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
154
0
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
155
156
0
   buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
157
0
   msg_buf().clear();
158
159
0
   const secure_vector<uint8_t> V = S2V(buffer.data() + offset, buffer.size() - offset);
160
161
0
   buffer.insert(buffer.begin() + offset, V.begin(), V.end());
162
163
0
   if(buffer.size() != offset + V.size()) {
164
0
      set_ctr_iv(V);
165
0
      ctr().cipher1(&buffer[offset + V.size()], buffer.size() - offset - V.size());
166
0
   }
167
0
}
168
169
0
void SIV_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
170
0
   BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
171
172
0
   if(!msg_buf().empty()) {
173
0
      buffer.insert(buffer.begin() + offset, msg_buf().begin(), msg_buf().end());
174
0
      msg_buf().clear();
175
0
   }
176
177
0
   const size_t sz = buffer.size() - offset;
178
179
0
   BOTAN_ARG_CHECK(sz >= tag_size(), "input did not include the tag");
180
181
0
   secure_vector<uint8_t> V(buffer.data() + offset, buffer.data() + offset + block_size());
182
183
0
   if(buffer.size() != offset + V.size()) {
184
0
      set_ctr_iv(V);
185
186
0
      ctr().cipher(buffer.data() + offset + V.size(), buffer.data() + offset, buffer.size() - offset - V.size());
187
0
   }
188
189
0
   const secure_vector<uint8_t> T = S2V(buffer.data() + offset, buffer.size() - offset - V.size());
190
191
0
   if(!CT::is_equal(T.data(), V.data(), T.size()).as_bool()) {
192
0
      throw Invalid_Authentication_Tag("SIV tag check failed");
193
0
   }
194
195
0
   buffer.resize(buffer.size() - tag_size());
196
0
}
197
198
}  // namespace Botan