Botan 3.14.1
Crypto and TLS for C&
commoncrypto_mode.cpp
Go to the documentation of this file.
1/*
2* Cipher Modes via CommonCrypto
3* (C) 2018 Jose Pereira
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/commoncrypto.h>
9
10#include <botan/assert.h>
11#include <botan/block_cipher.h>
12#include <botan/cipher_mode.h>
13#include <botan/mem_ops.h>
14#include <botan/internal/bit_ops.h>
15#include <botan/internal/commoncrypto_utils.h>
16
17#include <limits.h>
18
19namespace Botan {
20
21namespace {
22
23class CommonCrypto_Cipher_Mode final : public Cipher_Mode {
24 public:
25 CommonCrypto_Cipher_Mode(std::string_view name, Cipher_Dir direction, const CommonCryptor_Opts& opts);
26
27 ~CommonCrypto_Cipher_Mode();
28
29 std::string provider() const override { return "commoncrypto"; }
30
31 std::string name() const override { return m_mode_name; }
32
33 size_t output_length(size_t input_length) const override;
34 size_t update_granularity() const override;
35 size_t ideal_granularity() const override;
36 size_t minimum_final_size() const override;
37 size_t default_nonce_length() const override;
38 bool valid_nonce_length(size_t nonce_len) const override;
39 void clear() override;
40 void reset() override;
41 Key_Length_Specification key_spec() const override;
42
43 bool has_keying_material() const override { return m_key_set; }
44
45 private:
46 void key_schedule(std::span<const uint8_t> key) override;
47
48 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
49 size_t process_msg(uint8_t msg[], size_t msg_len) override;
50 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset0) override;
51
52 const std::string m_mode_name;
53 Cipher_Dir m_direction;
54 CommonCryptor_Opts m_opts;
55 CCCryptorRef m_cipher = nullptr;
56 bool m_key_set;
57 bool m_nonce_set;
58};
59
60CommonCrypto_Cipher_Mode::CommonCrypto_Cipher_Mode(std::string_view name,
61 Cipher_Dir direction,
62 const CommonCryptor_Opts& opts) :
63 m_mode_name(name), m_direction(direction), m_opts(opts), m_key_set(false), m_nonce_set(false) {}
64
65CommonCrypto_Cipher_Mode::~CommonCrypto_Cipher_Mode() {
66 if(m_cipher) {
67 CCCryptorRelease(m_cipher);
68 }
69}
70
71void CommonCrypto_Cipher_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
72 if(!valid_nonce_length(nonce_len)) {
73 throw Invalid_IV_Length(name(), nonce_len);
74 }
75
76 assert_key_material_set();
77
78 if(nonce_len) {
79 CCCryptorStatus status = CCCryptorReset(m_cipher, nonce);
80 if(status != kCCSuccess) {
81 throw CommonCrypto_Error("CCCryptorReset on start_msg", status);
82 }
83 }
84 m_nonce_set = true;
85}
86
87size_t CommonCrypto_Cipher_Mode::process_msg(uint8_t msg[], size_t msg_len) {
88 assert_key_material_set();
89 BOTAN_STATE_CHECK(m_nonce_set);
90
91 if(msg_len == 0) {
92 return 0;
93 }
94 if(msg_len > INT_MAX) {
95 throw Internal_Error("msg_len overflow");
96 }
97 size_t outl = CCCryptorGetOutputLength(m_cipher, msg_len, false);
98
99 secure_vector<uint8_t> out(outl);
100
101 if(m_opts.padding == ccNoPadding && msg_len % m_opts.block_size) {
102 msg_len = outl;
103 }
104
105 CCCryptorStatus status = CCCryptorUpdate(m_cipher, msg, msg_len, out.data(), outl, &outl);
106 if(status != kCCSuccess) {
107 throw CommonCrypto_Error("CCCryptorUpdate", status);
108 }
109 copy_mem(msg, out.data(), outl);
110
111 return outl;
112}
113
114void CommonCrypto_Cipher_Mode::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
115 assert_key_material_set();
116 BOTAN_STATE_CHECK(m_nonce_set);
117
118 BOTAN_ASSERT(buffer.size() >= offset, "Offset ok");
119 uint8_t* buf = buffer.data() + offset;
120 const size_t buf_size = buffer.size() - offset;
121
122 size_t written = process(buf, buf_size);
123
124 size_t outl = CCCryptorGetOutputLength(m_cipher, buf_size - written, true);
125 secure_vector<uint8_t> out(outl);
126
127 CCCryptorStatus status = CCCryptorFinal(m_cipher, out.data(), outl, &outl);
128 if(status != kCCSuccess) {
129 throw CommonCrypto_Error("CCCryptorFinal", status);
130 }
131
132 size_t new_len = offset + written + outl;
133 if(m_opts.padding != ccNoPadding || buffer.size() < new_len) {
134 buffer.resize(new_len);
135 }
136 copy_mem(buffer.data() + offset + written, out.data(), outl);
137 written += outl;
138}
139
140size_t CommonCrypto_Cipher_Mode::update_granularity() const {
141 return m_opts.block_size;
142}
143
144size_t CommonCrypto_Cipher_Mode::ideal_granularity() const {
145 return m_opts.block_size * BlockCipher::ParallelismMult;
146}
147
148size_t CommonCrypto_Cipher_Mode::minimum_final_size() const {
149 if(m_direction == Cipher_Dir::Encryption)
150 return 0;
151 else
152 return m_opts.block_size;
153}
154
155size_t CommonCrypto_Cipher_Mode::default_nonce_length() const {
156 return m_opts.block_size;
157}
158
159bool CommonCrypto_Cipher_Mode::valid_nonce_length(size_t nonce_len) const {
160 return (nonce_len == 0 || nonce_len == m_opts.block_size);
161}
162
163size_t CommonCrypto_Cipher_Mode::output_length(size_t input_length) const {
164 if(input_length == 0) {
165 return m_opts.block_size;
166 } else {
167 return m_opts.block_size * ceil_division(input_length, m_opts.block_size);
168 }
169}
170
171void CommonCrypto_Cipher_Mode::clear() {
172 m_key_set = false;
173
174 if(m_cipher == nullptr) {
175 return;
176 }
177
178 if(m_cipher) {
179 CCCryptorRelease(m_cipher);
180 m_cipher = nullptr;
181 }
182}
183
184void CommonCrypto_Cipher_Mode::reset() {
185 if(m_cipher == nullptr) {
186 return;
187 }
188
189 m_nonce_set = false;
190
191 CCCryptorStatus status = CCCryptorReset(m_cipher, nullptr);
192 if(status != kCCSuccess) {
193 throw CommonCrypto_Error("CCCryptorReset", status);
194 }
195}
196
197Key_Length_Specification CommonCrypto_Cipher_Mode::key_spec() const {
198 return m_opts.key_spec;
199}
200
201void CommonCrypto_Cipher_Mode::key_schedule(std::span<const uint8_t> key) {
202 CCCryptorStatus status;
203 CCOperation op = m_direction == Cipher_Dir::Encryption ? kCCEncrypt : kCCDecrypt;
204 status = CCCryptorCreateWithMode(
205 op, m_opts.mode, m_opts.algo, m_opts.padding, nullptr, key.data(), key.size(), nullptr, 0, 0, 0, &m_cipher);
206 if(status != kCCSuccess) {
207 throw CommonCrypto_Error("CCCryptorCreate", status);
208 }
209
210 m_key_set = true;
211 m_nonce_set = false;
212}
213} // namespace
214
215std::unique_ptr<Cipher_Mode> make_commoncrypto_cipher_mode(std::string_view name, Cipher_Dir direction) {
216 try {
218 return std::make_unique<CommonCrypto_Cipher_Mode>(name, direction, opts);
219 } catch(CommonCrypto_Error& e) {
220 return nullptr;
221 }
222}
223} // namespace Botan
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
const char * name
Definition ffi_tls.h:59
BOTAN_FORCE_INLINE constexpr T ceil_division(T a, T b)
Definition bit_ops.h:167
std::unique_ptr< Cipher_Mode > make_commoncrypto_cipher_mode(std::string_view name, Cipher_Dir direction)
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
int32_t CCCryptorStatus
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
CommonCryptor_Opts commoncrypto_opts_from_algo(std::string_view algo)