Botan 3.14.1
Crypto and TLS for C&
commoncrypto_utils.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/cipher_mode.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/algorithm_spec.h>
13#include <botan/internal/commoncrypto_utils.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan {
17
19
20CommonCrypto_Error::CommonCrypto_Error(std::string_view what, int32_t status) :
21 Exception(fmt("CommonCrypto op {} failed with err {} ({})", what, status, ccryptorstatus_to_string(status))),
22 m_rc(status) {}
23
24std::string CommonCrypto_Error::ccryptorstatus_to_string(CCCryptorStatus status) {
25 switch(status) {
26 case kCCSuccess:
27 return "Success";
28 case kCCParamError:
29 return "ParamError";
30 case kCCBufferTooSmall:
31 return "BufferTooSmall";
32 case kCCMemoryFailure:
33 return "MemoryFailure";
34 case kCCAlignmentError:
35 return "AlignmentError";
36 case kCCDecodeError:
37 return "DecodeError";
38 case kCCUnimplemented:
39 return "Unimplemented";
40 case kCCOverflow:
41 return "Overflow";
42 case kCCRNGFailure:
43 return "RNGFailure";
44 case kCCUnspecifiedError:
45 return "UnspecifiedError";
46 case kCCCallSequenceError:
47 return "CallSequenceError";
48 case kCCKeySizeError:
49 return "KeySizeError";
50 default:
51 return "Unknown";
52 }
53};
54
57
58 if(algo_name.compare(0, 3, "AES") == 0) {
59 opts.algo = kCCAlgorithmAES;
60 opts.block_size = kCCBlockSizeAES128;
61 if(algo_name == "AES-128") {
62 opts.key_spec = Key_Length_Specification(kCCKeySizeAES128);
63 } else if(algo_name == "AES-192") {
64 opts.key_spec = Key_Length_Specification(kCCKeySizeAES192);
65 } else if(algo_name == "AES-256") {
66 opts.key_spec = Key_Length_Specification(kCCKeySizeAES256);
67 } else {
68 throw CommonCrypto_Error("Unknown AES algorithm");
69 }
70 } else if(algo_name == "DES") {
71 opts.algo = kCCAlgorithmDES;
72 opts.block_size = kCCBlockSizeDES;
73 opts.key_spec = Key_Length_Specification(kCCKeySizeDES);
74 } else if(algo_name == "TripleDES") {
75 opts.algo = kCCAlgorithm3DES;
76 opts.block_size = kCCBlockSize3DES;
77 opts.key_spec = Key_Length_Specification(16, kCCKeySize3DES, 8);
78 } else if(algo_name == "Blowfish") {
79 opts.algo = kCCAlgorithmBlowfish;
80 opts.block_size = kCCBlockSizeBlowfish;
81 opts.key_spec = Key_Length_Specification(1, kCCKeySizeMaxBlowfish, 1);
82 } else if(algo_name == "CAST-128") {
83 opts.algo = kCCAlgorithmCAST;
84 opts.block_size = kCCBlockSizeCAST;
85 // Botan's base implementation of CAST does not support shorter keys
86 // so we limit its minimum key size to 11 here.
87 opts.key_spec = Key_Length_Specification(11, kCCKeySizeMaxCAST, 1);
88 } else {
89 throw CommonCrypto_Error("Unsupported cipher");
90 }
91
92 return opts;
93}
94
97
98 std::string_view algo_name = spec.head();
99 std::string_view cipher_mode;
100 std::string_view cipher_mode_padding;
101
102 //TODO add CFB and XTS support
103 if(auto m = spec.match("*({cipher},{padding:str?})")) {
104 algo_name = m->str("cipher");
105 cipher_mode = m->head();
106 if(m->has("padding")) {
107 cipher_mode_padding = m->str("padding");
108 }
109 } else if(spec.arg_count() != 0) {
110 throw CommonCrypto_Error("Unsupported cipher mode!");
111 }
112
114
115 if(cipher_mode.empty() || cipher_mode == "ECB") {
116 opts.mode = kCCModeECB;
117 } else if(cipher_mode == "CBC") {
118 opts.mode = kCCModeCBC;
119 } else if(cipher_mode == "CTR") {
120 opts.mode = kCCModeCTR;
121 } else if(cipher_mode == "OFB") {
122 opts.mode = kCCModeOFB;
123 } else {
124 throw CommonCrypto_Error("Unsupported cipher mode!");
125 }
126
127 if(cipher_mode_padding == "NoPadding") {
128 opts.padding = ccNoPadding;
129 }
130 /*
131 else if(cipher_mode_padding.empty() || cipher_mode_padding == "PKCS7")
132 {
133 opts.padding = ccPKCS7Padding;
134 }
135 */
136 else {
137 throw CommonCrypto_Error("Unsupported cipher mode padding!");
138 }
139
140 return opts;
141}
142
143void commoncrypto_adjust_key_size(const uint8_t key[],
144 size_t length,
145 const CommonCryptor_Opts& opts,
146 secure_vector<uint8_t>& full_key) {
147 if(opts.algo == kCCAlgorithmBlowfish && length < 8) {
148 size_t repeat;
149 switch(length) {
150 case 1:
151 repeat = 8;
152 break;
153 case 2:
154 repeat = 4;
155 break;
156 case 3:
157 repeat = 3;
158 break;
159 default:
160 repeat = 2;
161 break;
162 }
163
164 full_key.resize(length * repeat);
165 for(size_t i = 0; i < repeat; i++) {
166 copy_mem(full_key.data() + i * length, key, length);
167 }
168 } else if(opts.algo == kCCAlgorithm3DES && length == 16) {
169 full_key += std::make_pair(key, 8);
170 }
171}
172} // namespace Botan
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
std::string_view head() const
size_t arg_count() const
CommonCrypto_Error(std::string_view what)
const char * what() const noexcept override
Definition exceptn.h:94
Exception(std::string_view msg)
Definition exceptn.cpp:71
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
int32_t CCCryptorStatus
void commoncrypto_adjust_key_size(const uint8_t key[], size_t length, const CommonCryptor_Opts &opts, secure_vector< uint8_t > &full_key)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
CommonCryptor_Opts commoncrypto_opts_from_algo(std::string_view algo)
CommonCryptor_Opts commoncrypto_opts_from_algo_name(std::string_view algo_name)
Key_Length_Specification key_spec