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