Coverage Report

Created: 2021-05-04 09:02

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