Botan  2.6.0
Crypto and TLS for C++11
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 #if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
57  if(algo == "ChaCha20Poly1305")
58  {
59  if(dir == ENCRYPTION)
60  return std::unique_ptr<AEAD_Mode>(new ChaCha20Poly1305_Encryption);
61  else
62  return std::unique_ptr<AEAD_Mode>(new ChaCha20Poly1305_Decryption);
63 
64  }
65 #endif
66 
67  if(algo.find('/') != std::string::npos)
68  {
69  const std::vector<std::string> algo_parts = split_on(algo, '/');
70  const std::string cipher_name = algo_parts[0];
71  const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
72 
73  if(mode_info.empty())
74  return std::unique_ptr<AEAD_Mode>();
75 
76  std::ostringstream alg_args;
77 
78  alg_args << '(' << cipher_name;
79  for(size_t i = 1; i < mode_info.size(); ++i)
80  alg_args << ',' << mode_info[i];
81  for(size_t i = 2; i < algo_parts.size(); ++i)
82  alg_args << ',' << algo_parts[i];
83  alg_args << ')';
84 
85  const std::string mode_name = mode_info[0] + alg_args.str();
86  return AEAD_Mode::create(mode_name, dir);
87  }
88 
89 #if defined(BOTAN_HAS_BLOCK_CIPHER)
90 
91  SCAN_Name req(algo);
92 
93  if(req.arg_count() == 0)
94  {
95  return std::unique_ptr<AEAD_Mode>();
96  }
97 
98  std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0), provider));
99 
100  if(!bc)
101  {
102  return std::unique_ptr<AEAD_Mode>();
103  }
104 
105 #if defined(BOTAN_HAS_AEAD_CCM)
106  if(req.algo_name() == "CCM")
107  {
108  size_t tag_len = req.arg_as_integer(1, 16);
109  size_t L_len = req.arg_as_integer(2, 3);
110  if(dir == ENCRYPTION)
111  return std::unique_ptr<AEAD_Mode>(new CCM_Encryption(bc.release(), tag_len, L_len));
112  else
113  return std::unique_ptr<AEAD_Mode>(new CCM_Decryption(bc.release(), tag_len, L_len));
114  }
115 #endif
116 
117 #if defined(BOTAN_HAS_AEAD_GCM)
118  if(req.algo_name() == "GCM")
119  {
120  size_t tag_len = req.arg_as_integer(1, 16);
121  if(dir == ENCRYPTION)
122  return std::unique_ptr<AEAD_Mode>(new GCM_Encryption(bc.release(), tag_len));
123  else
124  return std::unique_ptr<AEAD_Mode>(new GCM_Decryption(bc.release(), tag_len));
125  }
126 #endif
127 
128 #if defined(BOTAN_HAS_AEAD_OCB)
129  if(req.algo_name() == "OCB")
130  {
131  size_t tag_len = req.arg_as_integer(1, 16);
132  if(dir == ENCRYPTION)
133  return std::unique_ptr<AEAD_Mode>(new OCB_Encryption(bc.release(), tag_len));
134  else
135  return std::unique_ptr<AEAD_Mode>(new OCB_Decryption(bc.release(), tag_len));
136  }
137 #endif
138 
139 #if defined(BOTAN_HAS_AEAD_EAX)
140  if(req.algo_name() == "EAX")
141  {
142  size_t tag_len = req.arg_as_integer(1, bc->block_size());
143  if(dir == ENCRYPTION)
144  return std::unique_ptr<AEAD_Mode>(new EAX_Encryption(bc.release(), tag_len));
145  else
146  return std::unique_ptr<AEAD_Mode>(new EAX_Decryption(bc.release(), tag_len));
147  }
148 #endif
149 
150 #if defined(BOTAN_HAS_AEAD_SIV)
151  if(req.algo_name() == "SIV")
152  {
153  if(dir == ENCRYPTION)
154  return std::unique_ptr<AEAD_Mode>(new SIV_Encryption(bc.release()));
155  else
156  return std::unique_ptr<AEAD_Mode>(new SIV_Decryption(bc.release()));
157  }
158 #endif
159 
160 #endif
161 
162  return std::unique_ptr<AEAD_Mode>();
163  }
164 
165 
166 
167 }
size_t arg_count() const
Definition: scan_name.h:49
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:144
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:137
std::string arg(size_t i) const
Definition: scan_name.cpp:122
static std::unique_ptr< AEAD_Mode > create(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:52
std::vector< std::string > parse_algorithm_name(const std::string &namex)
Definition: parsing.cpp:91
Definition: alg_id.cpp:13
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
const std::string & algo_name() const
Definition: scan_name.h:44
Cipher_Dir
Definition: cipher_mode.h:24
virtual std::string provider() const
Definition: cipher_mode.h:236
static std::unique_ptr< AEAD_Mode > create_or_throw(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: aead.cpp:42