/src/botan/src/lib/modes/aead/aead.cpp
Line | Count | Source (jump to first uncovered line) |
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/internal/scan_name.h> |
9 | | #include <botan/internal/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/internal/ccm.h> |
18 | | #endif |
19 | | |
20 | | #if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305) |
21 | | #include <botan/internal/chacha20poly1305.h> |
22 | | #endif |
23 | | |
24 | | #if defined(BOTAN_HAS_AEAD_EAX) |
25 | | #include <botan/internal/eax.h> |
26 | | #endif |
27 | | |
28 | | #if defined(BOTAN_HAS_AEAD_GCM) |
29 | | #include <botan/internal/gcm.h> |
30 | | #endif |
31 | | |
32 | | #if defined(BOTAN_HAS_AEAD_OCB) |
33 | | #include <botan/internal/ocb.h> |
34 | | #endif |
35 | | |
36 | | #if defined(BOTAN_HAS_AEAD_SIV) |
37 | | #include <botan/internal/siv.h> |
38 | | #endif |
39 | | |
40 | | namespace Botan { |
41 | | |
42 | | void AEAD_Mode::set_associated_data_n(size_t i, const uint8_t ad[], size_t ad_len) |
43 | 0 | { |
44 | 0 | if(i == 0) |
45 | 0 | this->set_associated_data(ad, ad_len); |
46 | 0 | else |
47 | 0 | throw Invalid_Argument("AEAD '" + name() + "' does not support multiple associated data"); |
48 | 0 | } |
49 | | |
50 | | std::unique_ptr<AEAD_Mode> AEAD_Mode::create_or_throw(const std::string& algo, |
51 | | Cipher_Dir dir, |
52 | | const std::string& provider) |
53 | 672 | { |
54 | 672 | if(auto aead = AEAD_Mode::create(algo, dir, provider)) |
55 | 672 | return aead; |
56 | | |
57 | 0 | throw Lookup_Error("AEAD", algo, provider); |
58 | 672 | } |
59 | | |
60 | | std::unique_ptr<AEAD_Mode> AEAD_Mode::create(const std::string& algo, |
61 | | Cipher_Dir dir, |
62 | | const std::string& provider) |
63 | 1.28k | { |
64 | 1.28k | BOTAN_UNUSED(provider); |
65 | 1.28k | #if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305) |
66 | 1.28k | if(algo == "ChaCha20Poly1305") |
67 | 59 | { |
68 | 59 | if(dir == ENCRYPTION) |
69 | 11 | return std::make_unique<ChaCha20Poly1305_Encryption>(); |
70 | 48 | else |
71 | 48 | return std::make_unique<ChaCha20Poly1305_Decryption>(); |
72 | | |
73 | 59 | } |
74 | 1.22k | #endif |
75 | | |
76 | 1.22k | if(algo.find('/') != std::string::npos) |
77 | 613 | { |
78 | 613 | const std::vector<std::string> algo_parts = split_on(algo, '/'); |
79 | 613 | const std::string cipher_name = algo_parts[0]; |
80 | 613 | const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]); |
81 | | |
82 | 613 | if(mode_info.empty()) |
83 | 0 | return std::unique_ptr<AEAD_Mode>(); |
84 | | |
85 | 613 | std::ostringstream mode_name; |
86 | | |
87 | 613 | mode_name << mode_info[0] << '(' << cipher_name; |
88 | 865 | for(size_t i = 1; i < mode_info.size(); ++i) |
89 | 252 | mode_name << ',' << mode_info[i]; |
90 | 613 | for(size_t i = 2; i < algo_parts.size(); ++i) |
91 | 0 | mode_name << ',' << algo_parts[i]; |
92 | 613 | mode_name << ')'; |
93 | | |
94 | 613 | return AEAD_Mode::create(mode_name.str(), dir); |
95 | 613 | } |
96 | | |
97 | 613 | #if defined(BOTAN_HAS_BLOCK_CIPHER) |
98 | | |
99 | 613 | SCAN_Name req(algo); |
100 | | |
101 | 613 | if(req.arg_count() == 0) |
102 | 0 | { |
103 | 0 | return std::unique_ptr<AEAD_Mode>(); |
104 | 0 | } |
105 | | |
106 | 613 | std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0), provider)); |
107 | | |
108 | 613 | if(!bc) |
109 | 0 | { |
110 | 0 | return std::unique_ptr<AEAD_Mode>(); |
111 | 0 | } |
112 | | |
113 | 613 | #if defined(BOTAN_HAS_AEAD_CCM) |
114 | 613 | if(req.algo_name() == "CCM") |
115 | 56 | { |
116 | 56 | size_t tag_len = req.arg_as_integer(1, 16); |
117 | 56 | size_t L_len = req.arg_as_integer(2, 3); |
118 | 56 | if(dir == ENCRYPTION) |
119 | 13 | return std::make_unique<CCM_Encryption>(std::move(bc), tag_len, L_len); |
120 | 43 | else |
121 | 43 | return std::make_unique<CCM_Decryption>(std::move(bc), tag_len, L_len); |
122 | 56 | } |
123 | 557 | #endif |
124 | | |
125 | 557 | #if defined(BOTAN_HAS_AEAD_GCM) |
126 | 557 | if(req.algo_name() == "GCM") |
127 | 351 | { |
128 | 351 | size_t tag_len = req.arg_as_integer(1, 16); |
129 | 351 | if(dir == ENCRYPTION) |
130 | 238 | return std::make_unique<GCM_Encryption>(std::move(bc), tag_len); |
131 | 113 | else |
132 | 113 | return std::make_unique<GCM_Decryption>(std::move(bc), tag_len); |
133 | 351 | } |
134 | 206 | #endif |
135 | | |
136 | 206 | #if defined(BOTAN_HAS_AEAD_OCB) |
137 | 206 | if(req.algo_name() == "OCB") |
138 | 206 | { |
139 | 206 | size_t tag_len = req.arg_as_integer(1, 16); |
140 | 206 | if(dir == ENCRYPTION) |
141 | 86 | return std::make_unique<OCB_Encryption>(std::move(bc), tag_len); |
142 | 120 | else |
143 | 120 | return std::make_unique<OCB_Decryption>(std::move(bc), tag_len); |
144 | 206 | } |
145 | 0 | #endif |
146 | | |
147 | 0 | #if defined(BOTAN_HAS_AEAD_EAX) |
148 | 0 | if(req.algo_name() == "EAX") |
149 | 0 | { |
150 | 0 | size_t tag_len = req.arg_as_integer(1, bc->block_size()); |
151 | 0 | if(dir == ENCRYPTION) |
152 | 0 | return std::make_unique<EAX_Encryption>(std::move(bc), tag_len); |
153 | 0 | else |
154 | 0 | return std::make_unique<EAX_Decryption>(std::move(bc), tag_len); |
155 | 0 | } |
156 | 0 | #endif |
157 | | |
158 | 0 | #if defined(BOTAN_HAS_AEAD_SIV) |
159 | 0 | if(req.algo_name() == "SIV") |
160 | 0 | { |
161 | 0 | if(dir == ENCRYPTION) |
162 | 0 | return std::make_unique<SIV_Encryption>(std::move(bc)); |
163 | 0 | else |
164 | 0 | return std::make_unique<SIV_Decryption>(std::move(bc)); |
165 | 0 | } |
166 | 0 | #endif |
167 | | |
168 | 0 | #endif |
169 | | |
170 | 0 | return std::unique_ptr<AEAD_Mode>(); |
171 | 0 | } |
172 | | |
173 | | } |