1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/*
* Block Ciphers via CommonCrypto
* (C) 2018 Jose Luis Pereira
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/hex.h>
#include <botan/block_cipher.h>
#include <botan/internal/commoncrypto.h>
#include <CommonCrypto/CommonCrypto.h>
#include "commoncrypto_utils.h"
namespace Botan {
namespace {
class CommonCrypto_BlockCipher final : public BlockCipher
{
public:
CommonCrypto_BlockCipher(const std::string& name, const CommonCryptor_Opts& opts);
~CommonCrypto_BlockCipher();
void clear() override;
std::string provider() const override { return "commoncrypto"; }
std::string name() const override { return m_cipher_name; }
BlockCipher* clone() const override;
size_t block_size() const override { return m_opts.block_size; }
Key_Length_Specification key_spec() const override { return m_opts.key_spec; }
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
{
verify_key_set(m_key_set);
size_t total_len = blocks * m_opts.block_size;
size_t out_len = 0;
CCCryptorStatus status = CCCryptorUpdate(m_encrypt, in, total_len,
out, total_len, &out_len);
if(status != kCCSuccess)
{
throw CommonCrypto_Error("CCCryptorUpdate encrypt", status);
}
}
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
{
verify_key_set(m_key_set);
size_t total_len = blocks * m_opts.block_size;
size_t out_len = 0;
CCCryptorStatus status = CCCryptorUpdate(m_decrypt, in, total_len,
out, total_len, &out_len);
if(status != kCCSuccess)
{
throw CommonCrypto_Error("CCCryptorUpdate decrypt", status);
}
}
void key_schedule(const uint8_t key[], size_t key_len) override;
std::string m_cipher_name;
CommonCryptor_Opts m_opts;
CCCryptorRef m_encrypt = nullptr;
CCCryptorRef m_decrypt = nullptr;
bool m_key_set;
};
CommonCrypto_BlockCipher::CommonCrypto_BlockCipher(const std::string& algo_name,
const CommonCryptor_Opts& opts) :
m_cipher_name(algo_name),
m_opts(opts),
m_key_set(false)
{
}
CommonCrypto_BlockCipher::~CommonCrypto_BlockCipher()
{
if(m_encrypt)
{
CCCryptorRelease(m_encrypt);
}
if(m_decrypt)
{
CCCryptorRelease(m_decrypt);
}
}
/*
* Set the key
*/
void CommonCrypto_BlockCipher::key_schedule(const uint8_t key[], size_t length)
{
secure_vector<uint8_t> full_key(key, key + length);
commoncrypto_adjust_key_size(key, length, m_opts, full_key);
CCCryptorStatus status;
status = CCCryptorCreate(kCCEncrypt, m_opts.algo, kCCOptionECBMode,
full_key.data(), full_key.size(), nullptr, &m_encrypt);
if(status != kCCSuccess)
{
throw CommonCrypto_Error("CCCryptorCreate encrypt", status);
}
status = CCCryptorCreate(kCCDecrypt, m_opts.algo, kCCOptionECBMode,
full_key.data(), full_key.size(), nullptr, &m_decrypt);
if(status != kCCSuccess)
{
throw CommonCrypto_Error("CCCryptorCreate decrypt", status);
}
m_key_set = true;
}
/*
* Return a clone of this object
*/
BlockCipher* CommonCrypto_BlockCipher::clone() const
{
return new CommonCrypto_BlockCipher(m_cipher_name, m_opts);
}
/*
* Clear memory of sensitive data
*/
void CommonCrypto_BlockCipher::clear()
{
m_key_set = false;
}
}
std::unique_ptr<BlockCipher>
make_commoncrypto_block_cipher(const std::string& name)
{
try
{
CommonCryptor_Opts opts = commoncrypto_opts_from_algo(name);
return std::unique_ptr<BlockCipher>(new CommonCrypto_BlockCipher(name, opts));
}
catch(CommonCrypto_Error e)
{
return nullptr;
}
}
}
|