Coverage Report

Created: 2021-10-13 08:49

/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
679
   {
54
679
   if(auto aead = AEAD_Mode::create(algo, dir, provider))
55
679
      return aead;
56
57
0
   throw Lookup_Error("AEAD", algo, provider);
58
679
   }
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.31k
   {
64
1.31k
   BOTAN_UNUSED(provider);
65
1.31k
#if defined(BOTAN_HAS_AEAD_CHACHA20_POLY1305)
66
1.31k
   if(algo == "ChaCha20Poly1305")
67
45
      {
68
45
      if(dir == ENCRYPTION)
69
7
         return std::make_unique<ChaCha20Poly1305_Encryption>();
70
38
      else
71
38
         return std::make_unique<ChaCha20Poly1305_Decryption>();
72
73
45
      }
74
1.26k
#endif
75
76
1.26k
   if(algo.find('/') != std::string::npos)
77
634
      {
78
634
      const std::vector<std::string> algo_parts = split_on(algo, '/');
79
634
      const std::string cipher_name = algo_parts[0];
80
634
      const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
81
82
634
      if(mode_info.empty())
83
0
         return std::unique_ptr<AEAD_Mode>();
84
85
634
      std::ostringstream mode_name;
86
87
634
      mode_name << mode_info[0] << '(' << cipher_name;
88
872
      for(size_t i = 1; i < mode_info.size(); ++i)
89
238
         mode_name << ',' << mode_info[i];
90
634
      for(size_t i = 2; i < algo_parts.size(); ++i)
91
0
         mode_name << ',' << algo_parts[i];
92
634
      mode_name << ')';
93
94
634
      return AEAD_Mode::create(mode_name.str(), dir);
95
634
      }
96
97
634
#if defined(BOTAN_HAS_BLOCK_CIPHER)
98
99
634
   SCAN_Name req(algo);
100
101
634
   if(req.arg_count() == 0)
102
0
      {
103
0
      return std::unique_ptr<AEAD_Mode>();
104
0
      }
105
106
634
   std::unique_ptr<BlockCipher> bc(BlockCipher::create(req.arg(0), provider));
107
108
634
   if(!bc)
109
0
      {
110
0
      return std::unique_ptr<AEAD_Mode>();
111
0
      }
112
113
634
#if defined(BOTAN_HAS_AEAD_CCM)
114
634
   if(req.algo_name() == "CCM")
115
117
      {
116
117
      size_t tag_len = req.arg_as_integer(1, 16);
117
117
      size_t L_len = req.arg_as_integer(2, 3);
118
117
      if(dir == ENCRYPTION)
119
47
         return std::make_unique<CCM_Encryption>(std::move(bc), tag_len, L_len);
120
70
      else
121
70
         return std::make_unique<CCM_Decryption>(std::move(bc), tag_len, L_len);
122
117
      }
123
517
#endif
124
125
517
#if defined(BOTAN_HAS_AEAD_GCM)
126
517
   if(req.algo_name() == "GCM")
127
392
      {
128
392
      size_t tag_len = req.arg_as_integer(1, 16);
129
392
      if(dir == ENCRYPTION)
130
286
         return std::make_unique<GCM_Encryption>(std::move(bc), tag_len);
131
106
      else
132
106
         return std::make_unique<GCM_Decryption>(std::move(bc), tag_len);
133
392
      }
134
125
#endif
135
136
125
#if defined(BOTAN_HAS_AEAD_OCB)
137
125
   if(req.algo_name() == "OCB")
138
125
      {
139
125
      size_t tag_len = req.arg_as_integer(1, 16);
140
125
      if(dir == ENCRYPTION)
141
48
         return std::make_unique<OCB_Encryption>(std::move(bc), tag_len);
142
77
      else
143
77
         return std::make_unique<OCB_Decryption>(std::move(bc), tag_len);
144
125
      }
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
}