/src/botan/build/include/botan/internal/gcm.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * GCM Mode |
3 | | * (C) 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_GCM_H_ |
10 | | #define BOTAN_AEAD_GCM_H_ |
11 | | |
12 | | #include <botan/aead.h> |
13 | | #include <botan/block_cipher.h> |
14 | | #include <botan/sym_algo.h> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | class StreamCipher; |
19 | | class GHASH; |
20 | | |
21 | | /** |
22 | | * GCM Mode |
23 | | */ |
24 | | class GCM_Mode : public AEAD_Mode { |
25 | | public: |
26 | | void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) override final; |
27 | | |
28 | | std::string name() const override final; |
29 | | |
30 | | size_t update_granularity() const override final; |
31 | | |
32 | | size_t ideal_granularity() const override final; |
33 | | |
34 | | Key_Length_Specification key_spec() const override final; |
35 | | |
36 | | bool valid_nonce_length(size_t len) const override final; |
37 | | |
38 | 0 | size_t tag_size() const override final { return m_tag_size; } |
39 | | |
40 | | void clear() override final; |
41 | | |
42 | | void reset() override final; |
43 | | |
44 | | std::string provider() const override final; |
45 | | |
46 | | bool has_keying_material() const override final; |
47 | | |
48 | | ~GCM_Mode(); |
49 | | |
50 | | protected: |
51 | | GCM_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size); |
52 | | |
53 | | static const size_t GCM_BS = 16; |
54 | | |
55 | | const size_t m_tag_size; |
56 | | const std::string m_cipher_name; |
57 | | |
58 | | std::unique_ptr<StreamCipher> m_ctr; |
59 | | std::unique_ptr<GHASH> m_ghash; |
60 | | |
61 | | private: |
62 | | void start_msg(const uint8_t nonce[], size_t nonce_len) override; |
63 | | |
64 | | void key_schedule(std::span<const uint8_t> key) override; |
65 | | |
66 | | secure_vector<uint8_t> m_y0; |
67 | | }; |
68 | | |
69 | | /** |
70 | | * GCM Encryption |
71 | | */ |
72 | | class GCM_Encryption final : public GCM_Mode { |
73 | | public: |
74 | | /** |
75 | | * @param cipher the 128 bit block cipher to use |
76 | | * @param tag_size is how big the auth tag will be |
77 | | */ |
78 | | GCM_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) : |
79 | 0 | GCM_Mode(std::move(cipher), tag_size) {} |
80 | | |
81 | 0 | size_t output_length(size_t input_length) const override { return input_length + tag_size(); } |
82 | | |
83 | 0 | size_t minimum_final_size() const override { return 0; } |
84 | | |
85 | | private: |
86 | | size_t process_msg(uint8_t buf[], size_t size) override; |
87 | | void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
88 | | }; |
89 | | |
90 | | /** |
91 | | * GCM Decryption |
92 | | */ |
93 | | class GCM_Decryption final : public GCM_Mode { |
94 | | public: |
95 | | /** |
96 | | * @param cipher the 128 bit block cipher to use |
97 | | * @param tag_size is how big the auth tag will be |
98 | | */ |
99 | | GCM_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) : |
100 | 0 | GCM_Mode(std::move(cipher), tag_size) {} |
101 | | |
102 | 0 | size_t output_length(size_t input_length) const override { |
103 | 0 | BOTAN_ARG_CHECK(input_length >= tag_size(), "Sufficient input"); |
104 | 0 | return input_length - tag_size(); |
105 | 0 | } |
106 | | |
107 | 0 | size_t minimum_final_size() const override { return tag_size(); } |
108 | | |
109 | | private: |
110 | | size_t process_msg(uint8_t buf[], size_t size) override; |
111 | | void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
112 | | }; |
113 | | |
114 | | } // namespace Botan |
115 | | |
116 | | #endif |