Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

aead.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2013,2015 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #include <botan/aead.h>
8 #include <botan/scan_name.h>
9 #include <botan/parsing.h>
10 #include <sstream>
11
12 #if defined(BOTAN_HAS_BLOCK_CIPHER)
13 #include <botan/block_cipher.h>
14 #endif
15
16 #if defined(BOTAN_HAS_AEAD_CCM)
17 #include <botan/ccm.h>
18 #endif
19
20 #if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
21 #include <botan/chacha20poly1305.h>
22 #endif
23
24 #if defined(BOTAN_HAS_AEAD_EAX)
25 #include <botan/eax.h>
26 #endif
27
28 #if defined(BOTAN_HAS_AEAD_GCM)
29 #include <botan/gcm.h>
30 #endif
31
32 #if defined(BOTAN_HAS_AEAD_OCB)
33 #include <botan/ocb.h>
34 #endif
35
36 #if defined(BOTAN_HAS_AEAD_SIV)
37 #include <botan/siv.h>
38 #endif
39
40 namespace Botan {
41
42 std::unique_ptr<AEAD_Mode> AEAD_Mode::create_or_throw(const std::string& algo,
43 Cipher_Dir dir,
44 const std::string& provider)
45 {
46 if(auto aead = AEAD_Mode::create(algo, dir, provider))
47 return aead;
48
49 throw Lookup_Error("AEAD", algo, provider);
50 }
51
52 std::unique_ptr<AEAD_Mode> AEAD_Mode::create(const std::string& algo,
53 Cipher_Dir dir,
54 const std::string& provider)
55 {
56 BOTAN_UNUSED(provider);
57 #if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
58 if(algo == "ChaCha20Poly1305")
59 {
60 if(dir == ENCRYPTION)
61 return std::unique_ptr<AEAD_Mode>(new ChaCha20Poly1305_Encryption);
62 else
63 return std::unique_ptr<AEAD_Mode>(new ChaCha20Poly1305_Decryption);
64
65 }
66 #endif
67
68 if(algo.find('/') != std::string::npos)
69 {
70 const std::vector<std::string> algo_parts = split_on(algo, '/');
71 const std::string cipher_name = algo_parts[0];
72 const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
73
74 if(mode_info.empty())
75 return std::unique_ptr<AEAD_Mode>();
76
77 std::ostringstream alg_args;
78
79 alg_args << '(' << cipher_name;
80 for(size_t i = 1; i < mode_info.size(); ++i)
81 alg_args << ',' << mode_info[i];
82 for(size_t i = 2; i < algo_parts.size(); ++i)
83 alg_args << ',' << algo_parts[i];
84 alg_args << ')';
85
86 const std::string mode_name = mode_info[0] + alg_args.str();
87 return AEAD_Mode::create(mode_name, dir);
88 }
89
90 #if defined(BOTAN_HAS_BLOCK_CIPHER)
91
92 SCAN_Name req(algo);
93
94 if(req.arg_count() == 0)
95 {
96 return std::unique_ptr<AEAD_Mode>();
97 }
98
99 std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0), provider));
100
101 if(!bc)
102 {
103 return std::unique_ptr<AEAD_Mode>();
104 }
105
106 #if defined(BOTAN_HAS_AEAD_CCM)
107 if(req.algo_name() == "CCM")
108 {
109 size_t tag_len = req.arg_as_integer(1, 16);
110 size_t L_len = req.arg_as_integer(2, 3);
111 if(dir == ENCRYPTION)
112 return std::unique_ptr<AEAD_Mode>(new CCM_Encryption(bc.release(), tag_len, L_len));
113 else
114 return std::unique_ptr<AEAD_Mode>(new CCM_Decryption(bc.release(), tag_len, L_len));
115 }
116 #endif
117
118 #if defined(BOTAN_HAS_AEAD_GCM)
119 if(req.algo_name() == "GCM")
120 {
121 size_t tag_len = req.arg_as_integer(1, 16);
122 if(dir == ENCRYPTION)
123 return std::unique_ptr<AEAD_Mode>(new GCM_Encryption(bc.release(), tag_len));
124 else
125 return std::unique_ptr<AEAD_Mode>(new GCM_Decryption(bc.release(), tag_len));
126 }
127 #endif
128
129 #if defined(BOTAN_HAS_AEAD_OCB)
130 if(req.algo_name() == "OCB")
131 {
132 size_t tag_len = req.arg_as_integer(1, 16);
133 if(dir == ENCRYPTION)
134 return std::unique_ptr<AEAD_Mode>(new OCB_Encryption(bc.release(), tag_len));
135 else
136 return std::unique_ptr<AEAD_Mode>(new OCB_Decryption(bc.release(), tag_len));
137 }
138 #endif
139
140 #if defined(BOTAN_HAS_AEAD_EAX)
141 if(req.algo_name() == "EAX")
142 {
143 size_t tag_len = req.arg_as_integer(1, bc->block_size());
144 if(dir == ENCRYPTION)
145 return std::unique_ptr<AEAD_Mode>(new EAX_Encryption(bc.release(), tag_len));
146 else
147 return std::unique_ptr<AEAD_Mode>(new EAX_Decryption(bc.release(), tag_len));
148 }
149 #endif
150
151 #if defined(BOTAN_HAS_AEAD_SIV)
152 if(req.algo_name() == "SIV")
153 {
154 if(dir == ENCRYPTION)
155 return std::unique_ptr<AEAD_Mode>(new SIV_Encryption(bc.release()));
156 else
157 return std::unique_ptr<AEAD_Mode>(new SIV_Decryption(bc.release()));
158 }
159 #endif
160
161 #endif
162
163 return std::unique_ptr<AEAD_Mode>();
164 }
165
166
167
168 }
Botan::SCAN_Name::arg
std::string arg(size_t i) const
Definition: scan_name.cpp:124
Botan::Lookup_Error
Definition: exceptn.h:247
Botan::split_on
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:148
Botan::SIV_Decryption
Definition: siv.h:106
Botan::SIV_Encryption
Definition: siv.h:87
Botan::SCAN_Name::arg_count
size_t arg_count() const
Definition: scan_name.h:56
Botan::EAX_Decryption
Definition: eax.h:94
Botan::EAX_Encryption
Definition: eax.h:71
Botan::ChaCha20Poly1305_Decryption
Definition: chacha20poly1305.h:86
Botan::CCM_Decryption
Definition: ccm.h:104
Botan::Cipher_Mode::provider
virtual std::string provider() const
Definition: cipher_mode.h:180
Botan::GCM_Decryption
Definition: gcm.h:92
Botan::ChaCha20Poly1305_Encryption
Definition: chacha20poly1305.h:70
Botan::GCM_Encryption
Definition: gcm.h:69
Botan::CCM_Encryption
Definition: ccm.h:80
Botan::OCB_Encryption
Definition: ocb.h:88
Botan::OCB_Decryption
Definition: ocb.h:110
Botan::Usage_Type::ENCRYPTION
Botan::AEAD_Mode::create
static std::unique_ptr< AEAD_Mode > create(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:52
Botan::SCAN_Name::arg_as_integer
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:139
Botan::parse_algorithm_name
std::vector< std::string > parse_algorithm_name(const std::string &namex)
Definition: parsing.cpp:95
Botan
Definition: alg_id.cpp:13
BOTAN_UNUSED
#define BOTAN_UNUSED(...)
Definition: assert.h:142
Botan::SCAN_Name
Definition: scan_name.h:23
Botan::BlockCipher::create
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
Definition: block_cipher.cpp:108
Botan::SCAN_Name::algo_name
const std::string & algo_name() const
Definition: scan_name.h:51
Botan::Cipher_Dir
Cipher_Dir
Definition: cipher_mode.h:23
Botan::AEAD_Mode::create_or_throw
static std::unique_ptr< AEAD_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:42