/src/botan/build/include/botan/internal/eax.h
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  | * EAX Mode  | 
3  |  | * (C) 1999-2007,2013 Jack Lloyd  | 
4  |  | * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity  | 
5  |  | *  | 
6  |  | * Botan is released under the Simplified BSD License (see license.txt)  | 
7  |  | */  | 
8  |  |  | 
9  |  | #ifndef BOTAN_AEAD_EAX_H_  | 
10  |  | #define BOTAN_AEAD_EAX_H_  | 
11  |  |  | 
12  |  | #include <botan/aead.h>  | 
13  |  | #include <botan/block_cipher.h>  | 
14  |  | #include <botan/mac.h>  | 
15  |  | #include <botan/stream_cipher.h>  | 
16  |  |  | 
17  |  | namespace Botan { | 
18  |  |  | 
19  |  | /**  | 
20  |  | * EAX base class  | 
21  |  | */  | 
22  |  | class EAX_Mode : public AEAD_Mode { | 
23  |  |    public:  | 
24  |  |       void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) final;  | 
25  |  |  | 
26  |  |       std::string name() const final;  | 
27  |  |  | 
28  |  |       size_t update_granularity() const final;  | 
29  |  |  | 
30  |  |       size_t ideal_granularity() const final;  | 
31  |  |  | 
32  |  |       Key_Length_Specification key_spec() const final;  | 
33  |  |  | 
34  |  |       // EAX supports arbitrary nonce lengths  | 
35  | 0  |       bool valid_nonce_length(size_t) const final { return true; } | 
36  |  |  | 
37  | 0  |       size_t tag_size() const final { return m_tag_size; } | 
38  |  |  | 
39  |  |       void clear() final;  | 
40  |  |  | 
41  |  |       void reset() final;  | 
42  |  |  | 
43  |  |       bool has_keying_material() const final;  | 
44  |  |  | 
45  |  |    protected:  | 
46  |  |       /**  | 
47  |  |       * @param cipher the cipher to use  | 
48  |  |       * @param tag_size is how big the auth tag will be  | 
49  |  |       */  | 
50  |  |       EAX_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size);  | 
51  |  |  | 
52  | 0  |       size_t block_size() const { return m_cipher->block_size(); } | 
53  |  |  | 
54  |  |       size_t m_tag_size;  | 
55  |  |  | 
56  |  |       std::unique_ptr<BlockCipher> m_cipher;  | 
57  |  |       std::unique_ptr<StreamCipher> m_ctr;  | 
58  |  |       std::unique_ptr<MessageAuthenticationCode> m_cmac;  | 
59  |  |  | 
60  |  |       secure_vector<uint8_t> m_ad_mac;  | 
61  |  |  | 
62  |  |       secure_vector<uint8_t> m_nonce_mac;  | 
63  |  |  | 
64  |  |    private:  | 
65  |  |       void start_msg(const uint8_t nonce[], size_t nonce_len) final;  | 
66  |  |  | 
67  |  |       void key_schedule(std::span<const uint8_t> key) final;  | 
68  |  | };  | 
69  |  |  | 
70  |  | /**  | 
71  |  | * EAX Encryption  | 
72  |  | */  | 
73  |  | class EAX_Encryption final : public EAX_Mode { | 
74  |  |    public:  | 
75  |  |       /**  | 
76  |  |       * @param cipher a 128-bit block cipher  | 
77  |  |       * @param tag_size is how big the auth tag will be  | 
78  |  |       */  | 
79  |  |       EAX_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 0) :  | 
80  | 0  |             EAX_Mode(std::move(cipher), tag_size) {} | 
81  |  |  | 
82  | 0  |       size_t output_length(size_t input_length) const override { return input_length + tag_size(); } | 
83  |  |  | 
84  | 0  |       size_t minimum_final_size() const override { return 0; } | 
85  |  |  | 
86  |  |    private:  | 
87  |  |       size_t process_msg(uint8_t buf[], size_t size) override;  | 
88  |  |       void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;  | 
89  |  | };  | 
90  |  |  | 
91  |  | /**  | 
92  |  | * EAX Decryption  | 
93  |  | */  | 
94  |  | class EAX_Decryption final : public EAX_Mode { | 
95  |  |    public:  | 
96  |  |       /**  | 
97  |  |       * @param cipher a 128-bit block cipher  | 
98  |  |       * @param tag_size is how big the auth tag will be  | 
99  |  |       */  | 
100  |  |       EAX_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 0) :  | 
101  | 0  |             EAX_Mode(std::move(cipher), tag_size) {} | 
102  |  |  | 
103  | 0  |       size_t output_length(size_t input_length) const override { | 
104  | 0  |          BOTAN_ARG_CHECK(input_length >= tag_size(), "Sufficient input");  | 
105  | 0  |          return input_length - tag_size();  | 
106  | 0  |       }  | 
107  |  |  | 
108  | 0  |       size_t minimum_final_size() const override { return tag_size(); } | 
109  |  |  | 
110  |  |    private:  | 
111  |  |       size_t process_msg(uint8_t buf[], size_t size) override;  | 
112  |  |       void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;  | 
113  |  | };  | 
114  |  |  | 
115  |  | }  // namespace Botan  | 
116  |  |  | 
117  |  | #endif  |