Botan 3.14.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/assert.h>
10#include <botan/exceptn.h>
11#include <botan/internal/algorithm_spec.h>
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_GCM_SIV)
34 #include <botan/internal/gcm_siv.h>
35#endif
36
37#if defined(BOTAN_HAS_AEAD_OCB)
38 #include <botan/internal/ocb.h>
39#endif
40
41#if defined(BOTAN_HAS_AEAD_SIV)
42 #include <botan/internal/siv.h>
43#endif
44
45#if defined(BOTAN_HAS_ASCON_AEAD128)
46 #include <botan/internal/ascon_aead128.h>
47#endif
48
49namespace Botan {
50
51std::unique_ptr<AEAD_Mode> AEAD_Mode::create_or_throw(std::string_view algo,
52 Cipher_Dir dir,
53 std::string_view provider) {
54 if(auto aead = AEAD_Mode::create(algo, dir, provider)) {
55 return aead;
56 }
57
58 throw Lookup_Error("AEAD", algo, provider);
59}
60
61std::unique_ptr<AEAD_Mode> AEAD_Mode::create(std::string_view algo, Cipher_Dir dir, std::string_view provider) {
63#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
64 if(algo == "ChaCha20Poly1305") {
65 if(dir == Cipher_Dir::Encryption) {
66 return std::make_unique<ChaCha20Poly1305_Encryption>();
67 } else {
68 return std::make_unique<ChaCha20Poly1305_Decryption>();
69 }
70 }
71#endif
72
73#if defined(BOTAN_HAS_ASCON_AEAD128)
74 if(algo == "Ascon-AEAD128") {
75 if(dir == Cipher_Dir::Encryption) {
76 return std::make_unique<Ascon_AEAD128_Encryption>();
77 } else {
78 return std::make_unique<Ascon_AEAD128_Decryption>();
79 }
80 }
81#endif
82
83#if defined(BOTAN_HAS_BLOCK_CIPHER)
85
86 #if defined(BOTAN_HAS_AEAD_CCM)
87 if(auto m = req.match("CCM({cipher},{tag_len:int=16},{L:int=3})")) {
88 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
89 const size_t tag_len = m->integer("tag_len");
90 const size_t L_len = m->integer("L");
91 if(dir == Cipher_Dir::Encryption) {
92 return std::make_unique<CCM_Encryption>(std::move(bc), tag_len, L_len);
93 } else {
94 return std::make_unique<CCM_Decryption>(std::move(bc), tag_len, L_len);
95 }
96 }
97 }
98 #endif
99
100 #if defined(BOTAN_HAS_AEAD_GCM)
101 if(auto m = req.match("GCM({cipher},{tag_len:int=16})")) {
102 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
103 const size_t tag_len = m->integer("tag_len");
104 if(dir == Cipher_Dir::Encryption) {
105 return std::make_unique<GCM_Encryption>(std::move(bc), tag_len);
106 } else {
107 return std::make_unique<GCM_Decryption>(std::move(bc), tag_len);
108 }
109 }
110 }
111 #endif
112
113 #if defined(BOTAN_HAS_AEAD_GCM_SIV)
114 // Unlike GCM the tag length is fixed, so reject eg "AES-128/GCM-SIV(12)"
115 if(auto m = req.match("GCM-SIV({cipher})")) {
116 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
117 if(dir == Cipher_Dir::Encryption) {
118 return std::make_unique<GCM_SIV_Encryption>(std::move(bc));
119 } else {
120 return std::make_unique<GCM_SIV_Decryption>(std::move(bc));
121 }
122 }
123 }
124 #endif
125
126 #if defined(BOTAN_HAS_AEAD_OCB)
127 if(auto m = req.match("OCB({cipher},{tag_len:int=16})")) {
128 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
129 const size_t tag_len = m->integer("tag_len");
130 if(dir == Cipher_Dir::Encryption) {
131 return std::make_unique<OCB_Encryption>(std::move(bc), tag_len);
132 } else {
133 return std::make_unique<OCB_Decryption>(std::move(bc), tag_len);
134 }
135 }
136 }
137 #endif
138
139 #if defined(BOTAN_HAS_AEAD_EAX)
140 if(auto m = req.match("EAX({cipher},{tag_len:int?})")) {
141 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
142 const size_t tag_len = m->has("tag_len") ? m->integer("tag_len") : bc->block_size();
143 if(dir == Cipher_Dir::Encryption) {
144 return std::make_unique<EAX_Encryption>(std::move(bc), tag_len);
145 } else {
146 return std::make_unique<EAX_Decryption>(std::move(bc), tag_len);
147 }
148 }
149 }
150 #endif
151
152 #if defined(BOTAN_HAS_AEAD_SIV)
153 if(auto m = req.match("SIV({cipher})")) {
154 if(auto bc = BlockCipher::create(m->str("cipher"), provider)) {
155 if(dir == Cipher_Dir::Encryption) {
156 return std::make_unique<SIV_Encryption>(std::move(bc));
157 } else {
158 return std::make_unique<SIV_Decryption>(std::move(bc));
159 }
160 }
161 }
162 #endif
163
164 BOTAN_UNUSED(req);
165#endif
166
167 return std::unique_ptr<AEAD_Mode>();
168}
169
170} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:51
static std::unique_ptr< AEAD_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:61
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
virtual std::string provider() const