8#include <botan/internal/commoncrypto.h>
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>
21 Exception(
fmt(
"CommonCrypto op {} failed with err {} ({})",
what, status, ccryptorstatus_to_string(status))),
24std::string CommonCrypto_Error::ccryptorstatus_to_string(
CCCryptorStatus status) {
30 case kCCBufferTooSmall:
31 return "BufferTooSmall";
32 case kCCMemoryFailure:
33 return "MemoryFailure";
34 case kCCAlignmentError:
35 return "AlignmentError";
38 case kCCUnimplemented:
39 return "Unimplemented";
44 case kCCUnspecifiedError:
45 return "UnspecifiedError";
46 case kCCCallSequenceError:
47 return "CallSequenceError";
49 return "KeySizeError";
58 if(algo_name.compare(0, 3,
"AES") == 0) {
59 opts.
algo = kCCAlgorithmAES;
61 if(algo_name ==
"AES-128") {
63 }
else if(algo_name ==
"AES-192") {
65 }
else if(algo_name ==
"AES-256") {
70 }
else if(algo_name ==
"DES") {
71 opts.
algo = kCCAlgorithmDES;
74 }
else if(algo_name ==
"TripleDES") {
75 opts.
algo = kCCAlgorithm3DES;
78 }
else if(algo_name ==
"Blowfish") {
79 opts.
algo = kCCAlgorithmBlowfish;
82 }
else if(algo_name ==
"CAST-128") {
83 opts.
algo = kCCAlgorithmCAST;
98 std::string_view algo_name = spec.
head();
99 std::string_view cipher_mode;
100 std::string_view cipher_mode_padding;
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");
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;
127 if(cipher_mode_padding ==
"NoPadding") {
147 if(opts.
algo == kCCAlgorithmBlowfish && length < 8) {
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);
168 }
else if(opts.
algo == kCCAlgorithm3DES && length == 16) {
169 full_key += std::make_pair(key, 8);
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
std::string_view head() const
CommonCrypto_Error(std::string_view what)
const char * what() const noexcept override
Exception(std::string_view msg)
std::string fmt(std::string_view format, const T &... args)
constexpr void copy_mem(T *out, const T *in, size_t n)
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
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