Coverage Report

Created: 2026-02-14 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.4.0/src/lib/modes/aead/aead.cpp
Line
Count
Source
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
41
namespace Botan {
42
43
std::unique_ptr<AEAD_Mode> AEAD_Mode::create_or_throw(std::string_view algo,
44
                                                      Cipher_Dir dir,
45
0
                                                      std::string_view provider) {
46
0
   if(auto aead = AEAD_Mode::create(algo, dir, provider)) {
47
0
      return aead;
48
0
   }
49
50
0
   throw Lookup_Error("AEAD", algo, provider);
51
0
}
52
53
0
std::unique_ptr<AEAD_Mode> AEAD_Mode::create(std::string_view algo, Cipher_Dir dir, std::string_view provider) {
54
0
   BOTAN_UNUSED(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
0
   if(algo.find('/') != std::string::npos) {
66
0
      const std::vector<std::string> algo_parts = split_on(algo, '/');
67
0
      std::string_view cipher_name = algo_parts[0];
68
0
      const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
69
70
0
      if(mode_info.empty()) {
71
0
         return std::unique_ptr<AEAD_Mode>();
72
0
      }
73
74
0
      std::ostringstream mode_name;
75
76
0
      mode_name << mode_info[0] << '(' << cipher_name;
77
0
      for(size_t i = 1; i < mode_info.size(); ++i) {
78
0
         mode_name << ',' << mode_info[i];
79
0
      }
80
0
      for(size_t i = 2; i < algo_parts.size(); ++i) {
81
0
         mode_name << ',' << algo_parts[i];
82
0
      }
83
0
      mode_name << ')';
84
85
0
      return AEAD_Mode::create(mode_name.str(), dir);
86
0
   }
87
88
0
#if defined(BOTAN_HAS_BLOCK_CIPHER)
89
90
0
   SCAN_Name req(algo);
91
92
0
   if(req.arg_count() == 0) {
93
0
      return std::unique_ptr<AEAD_Mode>();
94
0
   }
95
96
0
   auto bc = BlockCipher::create(req.arg(0), provider);
97
98
0
   if(!bc) {
99
0
      return std::unique_ptr<AEAD_Mode>();
100
0
   }
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
0
   #if defined(BOTAN_HAS_AEAD_OCB)
126
0
   if(req.algo_name() == "OCB") {
127
0
      size_t tag_len = req.arg_as_integer(1, 16);
128
0
      if(dir == Cipher_Dir::Encryption) {
129
0
         return std::make_unique<OCB_Encryption>(std::move(bc), tag_len);
130
0
      } else {
131
0
         return std::make_unique<OCB_Decryption>(std::move(bc), tag_len);
132
0
      }
133
0
   }
134
0
   #endif
135
136
0
   #if defined(BOTAN_HAS_AEAD_EAX)
137
0
   if(req.algo_name() == "EAX") {
138
0
      size_t tag_len = req.arg_as_integer(1, bc->block_size());
139
0
      if(dir == Cipher_Dir::Encryption) {
140
0
         return std::make_unique<EAX_Encryption>(std::move(bc), tag_len);
141
0
      } else {
142
0
         return std::make_unique<EAX_Decryption>(std::move(bc), tag_len);
143
0
      }
144
0
   }
145
0
   #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
0
#endif
158
159
0
   return std::unique_ptr<AEAD_Mode>();
160
0
}
161
162
}  // namespace Botan