Coverage Report

Created: 2024-06-28 06:19

/src/botan/build/include/public/botan/aead.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Interface for AEAD modes
3
* (C) 2013 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_AEAD_MODE_H_
9
#define BOTAN_AEAD_MODE_H_
10
11
#include <botan/cipher_mode.h>
12
13
#include <span>
14
15
namespace Botan {
16
17
/**
18
* Interface for AEAD (Authenticated Encryption with Associated Data)
19
* modes. These modes provide both encryption and message
20
* authentication, and can authenticate additional per-message data
21
* which is not included in the ciphertext (for instance a sequence
22
* number).
23
*/
24
class BOTAN_PUBLIC_API(2, 0) AEAD_Mode : public Cipher_Mode {
25
   public:
26
      /**
27
      * Create an AEAD mode
28
      * @param algo the algorithm to create
29
      * @param direction specify if this should be an encryption or decryption AEAD
30
      * @param provider optional specification for provider to use
31
      * @return an AEAD mode or a null pointer if not available
32
      */
33
      static std::unique_ptr<AEAD_Mode> create(std::string_view algo,
34
                                               Cipher_Dir direction,
35
                                               std::string_view provider = "");
36
37
      /**
38
      * Create an AEAD mode, or throw
39
      * @param algo the algorithm to create
40
      * @param direction specify if this should be an encryption or decryption AEAD
41
      * @param provider optional specification for provider to use
42
      * @return an AEAD mode, or throw an exception
43
      */
44
      static std::unique_ptr<AEAD_Mode> create_or_throw(std::string_view algo,
45
                                                        Cipher_Dir direction,
46
                                                        std::string_view provider = "");
47
48
      /**
49
      * Set associated data that is not included in the ciphertext but
50
      * that should be authenticated. Must be called after set_key and
51
      * before start.
52
      *
53
      * Unless reset by another call, the associated data is kept
54
      * between messages. Thus, if the AD does not change, calling
55
      * once (after set_key) is the optimum.
56
      *
57
      * @param ad the associated data
58
      */
59
0
      void set_associated_data(std::span<const uint8_t> ad) { set_associated_data_n(0, ad); }
60
61
0
      void set_associated_data(const uint8_t ad[], size_t ad_len) { set_associated_data(std::span(ad, ad_len)); }
62
63
      /**
64
      * Set associated data that is not included in the ciphertext but
65
      * that should be authenticated. Must be called after set_key and
66
      * before start.
67
      *
68
      * Unless reset by another call, the associated data is kept
69
      * between messages. Thus, if the AD does not change, calling
70
      * once (after set_key) is the optimum.
71
      *
72
      * Some AEADs (namely SIV) support multiple AD inputs. For
73
      * all other modes only nominal AD input 0 is supported; all
74
      * other values of idx will cause an exception.
75
      *
76
      * Derived AEADs must implement this. For AEADs where
77
      * `maximum_associated_data_inputs()` returns 1 (the default), the
78
      * @p idx must simply be ignored.
79
      *
80
      * @param idx which associated data to set
81
      * @param ad the associated data
82
      */
83
      virtual void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) = 0;
84
85
      /**
86
      * Returns the maximum supported number of associated data inputs which
87
      * can be provided to set_associated_data_n
88
      *
89
      * If returns 0, then no associated data is supported.
90
      */
91
0
      virtual size_t maximum_associated_data_inputs() const { return 1; }
92
93
      /**
94
      * Most AEADs require the key to be set prior to setting the AD
95
      * A few allow the AD to be set even before the cipher is keyed.
96
      * Such ciphers would return false from this function.
97
      */
98
0
      virtual bool associated_data_requires_key() const { return true; }
99
100
      /**
101
      * Set associated data that is not included in the ciphertext but
102
      * that should be authenticated. Must be called after set_key and
103
      * before start.
104
      *
105
      * See @ref set_associated_data().
106
      *
107
      * @param ad the associated data
108
      */
109
      template <typename Alloc>
110
      BOTAN_DEPRECATED("Simply use set_associated_data")
111
      void set_associated_data_vec(const std::vector<uint8_t, Alloc>& ad) {
112
         set_associated_data(ad);
113
      }
114
115
      /**
116
      * Set associated data that is not included in the ciphertext but
117
      * that should be authenticated. Must be called after set_key and
118
      * before start.
119
      *
120
      * See @ref set_associated_data().
121
      *
122
      * @param ad the associated data
123
      */
124
0
      BOTAN_DEPRECATED("Use set_associated_data") void set_ad(std::span<const uint8_t> ad) { set_associated_data(ad); }
125
126
      /**
127
      * @return default AEAD nonce size (a commonly supported value among AEAD
128
      * modes, and large enough that random collisions are unlikely)
129
      */
130
0
      size_t default_nonce_length() const override { return 12; }
131
132
      ~AEAD_Mode() override = default;
133
};
134
135
/**
136
* Get an AEAD mode by name (eg "AES-128/GCM" or "Serpent/EAX")
137
* @param name AEAD name
138
* @param direction Cipher_Dir::Encryption or Cipher_Dir::Decryption
139
*/
140
0
BOTAN_DEPRECATED("Use AEAD_Mode::create") inline AEAD_Mode* get_aead(std::string_view name, Cipher_Dir direction) {
141
0
   return AEAD_Mode::create(name, direction, "").release();
142
0
}
143
144
}  // namespace Botan
145
146
#endif