8#include <botan/internal/commoncrypto.h>
10#include <botan/cipher_mode.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/commoncrypto_utils.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/parsing.h>
15#include <botan/internal/scan_name.h>
22 Exception(
fmt(
"CommonCrypto op {} failed with err {} ({})", what, status, ccryptorstatus_to_string(status))),
25std::string CommonCrypto_Error::ccryptorstatus_to_string(
CCCryptorStatus status) {
31 case kCCBufferTooSmall:
32 return "BufferTooSmall";
33 case kCCMemoryFailure:
34 return "MemoryFailure";
35 case kCCAlignmentError:
36 return "AlignmentError";
39 case kCCUnimplemented:
40 return "Unimplemented";
45 case kCCUnspecifiedError:
46 return "UnspecifiedError";
47 case kCCCallSequenceError:
48 return "CallSequenceError";
50 return "KeySizeError";
59 if(algo_name.compare(0, 3,
"AES") == 0) {
60 opts.
algo = kCCAlgorithmAES;
62 if(algo_name ==
"AES-128") {
64 }
else if(algo_name ==
"AES-192") {
66 }
else if(algo_name ==
"AES-256") {
71 }
else if(algo_name ==
"DES") {
72 opts.
algo = kCCAlgorithmDES;
75 }
else if(algo_name ==
"TripleDES") {
76 opts.
algo = kCCAlgorithm3DES;
79 }
else if(algo_name ==
"Blowfish") {
80 opts.
algo = kCCAlgorithmBlowfish;
83 }
else if(algo_name ==
"CAST-128") {
84 opts.
algo = kCCAlgorithmCAST;
106 if(cipher_mode.empty() || cipher_mode ==
"ECB") {
107 opts.
mode = kCCModeECB;
108 }
else if(cipher_mode ==
"CBC") {
109 opts.
mode = kCCModeCBC;
110 }
else if(cipher_mode ==
"CTR") {
111 opts.
mode = kCCModeCTR;
112 }
else if(cipher_mode ==
"OFB") {
113 opts.
mode = kCCModeOFB;
118 if(cipher_mode_padding ==
"NoPadding") {
138 if(opts.
algo == kCCAlgorithmBlowfish && length < 8) {
155 full_key.resize(length * repeat);
156 for(
size_t i = 0; i < repeat; i++) {
157 copy_mem(full_key.data() + i * length, key, length);
159 }
else if(opts.
algo == kCCAlgorithm3DES && length == 16) {
160 full_key += std::make_pair(key, 8);
CommonCrypto_Error(std::string_view what)
const std::string & algo_name() const
std::string cipher_mode_pad() const
std::string cipher_mode() const
std::string fmt(std::string_view format, const T &... args)
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
constexpr void copy_mem(T *out, const T *in, size_t n)
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