Botan 3.14.1
Crypto and TLS for C&
dlies.cpp
Go to the documentation of this file.
1/*
2* DLIES
3* (C) 1999-2007 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/dlies.h>
10#include <botan/mem_ops.h>
11#include <botan/internal/concat_util.h>
12#include <botan/internal/ct_utils.h>
13#include <span>
14
15namespace Botan {
16
19 std::unique_ptr<KDF> kdf,
20 std::unique_ptr<MessageAuthenticationCode> mac,
21 size_t mac_key_length) :
22 DLIES_Encryptor(own_priv_key, rng, std::move(kdf), nullptr, 0, std::move(mac), mac_key_length) {}
23
26 std::unique_ptr<KDF> kdf,
27 std::unique_ptr<Cipher_Mode> cipher,
28 size_t cipher_key_len,
29 std::unique_ptr<MessageAuthenticationCode> mac,
30 size_t mac_key_length) :
31
32 m_own_pub_key(own_priv_key.public_value()),
33 m_ka(own_priv_key, rng, PK_Key_Agreement_Options().with_raw_shared_key()),
34 m_kdf(std::move(kdf)),
35 m_cipher(std::move(cipher)),
36 m_cipher_key_len(cipher_key_len),
37 m_mac(std::move(mac)),
38 m_mac_keylen(mac_key_length) {
41}
42
43std::vector<uint8_t> DLIES_Encryptor::enc(const uint8_t in[], size_t length, RandomNumberGenerator& /*unused*/) const {
44 if(m_other_pub_key.empty()) {
45 throw Invalid_State("DLIES: The other key was never set");
46 }
47
48 // calculate secret value
49 const SymmetricKey secret_value = m_ka.derive_key(0, m_other_pub_key);
50
51 // derive secret key from secret value
52 const size_t required_key_length = m_cipher ? m_cipher_key_len + m_mac_keylen : length + m_mac_keylen;
53 const secure_vector<uint8_t> secret_keys = m_kdf->derive_key(required_key_length, secret_value.bits_of());
54
55 if(secret_keys.size() != required_key_length) {
56 throw Encoding_Error("DLIES: KDF did not provide sufficient output");
57 }
58
59 secure_vector<uint8_t> ciphertext(in, in + length);
60 const size_t cipher_key_len = m_cipher ? m_cipher_key_len : length;
61
62 if(m_cipher) {
63 const SymmetricKey enc_key(secret_keys.data(), cipher_key_len);
64 m_cipher->set_key(enc_key);
65
66 if(m_iv.empty() && !m_cipher->valid_nonce_length(m_iv.size())) {
67 throw Invalid_Argument("DLIES with " + m_cipher->name() + " requires an IV be set");
68 }
69 m_cipher->start(m_iv.bits_of());
70 m_cipher->finish(ciphertext);
71 } else {
72 xor_buf(ciphertext, secret_keys, cipher_key_len);
73 }
74
75 // calculate MAC
76 m_mac->set_key(secret_keys.data() + cipher_key_len, m_mac_keylen);
77 const auto tag = m_mac->process(ciphertext);
78
79 // out = (ephemeral) public key + ciphertext + tag
80 return concat(m_own_pub_key, ciphertext, tag);
81}
82
83/**
84* Return the max size, in bytes, of a message
85* We assume DLIES is only used for key transport and limit the maximum size
86* to 512 bits
87*/
89 return 64;
90}
91
92size_t DLIES_Encryptor::ciphertext_length(size_t ptext_len) const {
93 const size_t ctext_len = (m_cipher != nullptr) ? m_cipher->output_length(ptext_len) : ptext_len;
94 return m_own_pub_key.size() + m_mac->output_length() + ctext_len;
95}
96
99 std::unique_ptr<KDF> kdf,
100 std::unique_ptr<Cipher_Mode> cipher,
101 size_t cipher_key_len,
102 std::unique_ptr<MessageAuthenticationCode> mac,
103 size_t mac_key_length) :
104 m_pub_key_size(own_priv_key.public_value().size()),
105 m_ka(own_priv_key, rng, PK_Key_Agreement_Options().with_raw_shared_key()),
106 m_kdf(std::move(kdf)),
107 m_cipher(std::move(cipher)),
108 m_cipher_key_len(cipher_key_len),
109 m_mac(std::move(mac)),
110 m_mac_keylen(mac_key_length) {
113}
114
117 std::unique_ptr<KDF> kdf,
118 std::unique_ptr<MessageAuthenticationCode> mac,
119 size_t mac_key_length) :
120 DLIES_Decryptor(own_priv_key, rng, std::move(kdf), nullptr, 0, std::move(mac), mac_key_length) {}
121
122size_t DLIES_Decryptor::plaintext_length(size_t ctext_len) const {
123 if(ctext_len < m_pub_key_size + m_mac->output_length()) {
124 return 0; // will throw if attempted
125 }
126
127 return ctext_len - (m_pub_key_size + m_mac->output_length());
128}
129
130size_t DLIES_Decryptor::ciphertext_length(size_t ptext_len) const {
131 const auto ctext_len = [&]() -> size_t {
132 if(m_cipher != nullptr) {
133 // We need the encryption direction to estimate ciphertext lengths
134 const auto cipher = Cipher_Mode::create(m_cipher->name(), Cipher_Dir::Encryption);
135 return cipher->output_length(ptext_len);
136 } else {
137 return ptext_len;
138 }
139 }();
140
141 return m_pub_key_size + m_mac->output_length() + ctext_len;
142}
143
144size_t DLIES_Decryptor::do_decrypt(uint8_t& valid_mask,
145 std::span<uint8_t> ptext,
146 std::span<const uint8_t> ctext) const {
147 const uint8_t* msg = ctext.data();
148 const size_t length = ctext.size();
149
150 if(length < m_pub_key_size + m_mac->output_length()) {
151 throw Decoding_Error("DLIES decryption: ciphertext is too short");
152 }
153
154 // calculate secret value
155 std::vector<uint8_t> other_pub_key(msg, msg + m_pub_key_size);
156 const SymmetricKey secret_value = m_ka.derive_key(0, other_pub_key);
157
158 const size_t ciphertext_len = length - m_pub_key_size - m_mac->output_length();
159 const size_t cipher_key_len = m_cipher ? m_cipher_key_len : ciphertext_len;
160
161 // derive secret key from secret value
162 const size_t required_key_length = cipher_key_len + m_mac_keylen;
163 secure_vector<uint8_t> secret_keys = m_kdf->derive_key(required_key_length, secret_value.bits_of());
164
165 if(secret_keys.size() != required_key_length) {
166 throw Encoding_Error("DLIES: KDF did not provide sufficient output");
167 }
168
169 secure_vector<uint8_t> ciphertext(msg + m_pub_key_size, msg + m_pub_key_size + ciphertext_len);
170
171 // calculate MAC
172 m_mac->set_key(secret_keys.data() + cipher_key_len, m_mac_keylen);
173 secure_vector<uint8_t> calculated_tag = m_mac->process(ciphertext);
174
175 // calculated tag == received tag ?
176
177 const std::span<const uint8_t> tag(msg + m_pub_key_size + ciphertext_len, m_mac->output_length());
178
179 valid_mask = CT::is_equal<uint8_t>(tag, calculated_tag).value();
180
181 // decrypt
182 if(m_cipher) {
183 if(valid_mask == 0xFF) {
184 const SymmetricKey dec_key(secret_keys.data(), cipher_key_len);
185 m_cipher->set_key(dec_key);
186
187 try {
188 // the decryption can fail:
189 // e.g. Invalid_Authentication_Tag is thrown if GCM is used and the message does not have a valid tag
190
191 if(m_iv.empty() && !m_cipher->valid_nonce_length(m_iv.size())) {
192 throw Invalid_Argument("DLIES with " + m_cipher->name() + " requires an IV be set");
193 }
194 m_cipher->start(m_iv.bits_of());
195 m_cipher->finish(ciphertext);
196 } catch(...) {
197 valid_mask = 0;
198 }
199
200 } else {
201 return 0;
202 }
203 } else {
204 xor_buf(ciphertext, secret_keys.data(), cipher_key_len);
205 }
206
207 BOTAN_ARG_CHECK(ptext.size() >= ciphertext.size(), "Output buffer too small for decryption");
208 copy_mem(ptext.first(ciphertext.size()), ciphertext);
209 return ciphertext.size();
210}
211
212} // namespace Botan
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static std::unique_ptr< Cipher_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
DLIES_Decryptor(const DH_PrivateKey &own_priv_key, RandomNumberGenerator &rng, std::unique_ptr< KDF > kdf, std::unique_ptr< MessageAuthenticationCode > mac, size_t mac_key_len=20)
Definition dlies.cpp:115
DLIES_Encryptor(const DH_PrivateKey &own_priv_key, RandomNumberGenerator &rng, std::unique_ptr< KDF > kdf, std::unique_ptr< MessageAuthenticationCode > mac, size_t mac_key_len=20)
Definition dlies.cpp:17
virtual size_t maximum_input_size() const =0
SymmetricKey derive_key(size_t key_len, std::span< const uint8_t > peer_key, std::span< const uint8_t > salt) const
Definition pubkey.cpp:305
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:803
OctetString SymmetricKey
Definition symkey.h:153
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:95
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:403
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128