Botan 2.19.5 Crypto and TLS for C&

gcm.h
Go to the documentation of this file.
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 BOTAN_FUTURE_INTERNAL_HEADER(gcm.h)
16
17namespace Botan {
18
19 class BlockCipher;
20 class StreamCipher;
21 class GHASH;
22
23/**
24* GCM Mode
25*/
26 class BOTAN_PUBLIC_API(2,0) GCM_Mode : public AEAD_Mode
27 {
28 public:
29 void set_associated_data(const uint8_t ad[], size_t ad_len) override;
30
31 std::string name() const override;
32
33 size_t update_granularity() const override;
34
35 Key_Length_Specification key_spec() const override;
36
37 bool valid_nonce_length(size_t len) const override;
38
39 size_t tag_size() const override { return m_tag_size; }
40
41 void clear() override;
42
43 void reset() override;
44
45 std::string provider() const override;
46 protected:
47 GCM_Mode(BlockCipher* cipher, size_t tag_size);
48
49 ~GCM_Mode();
50
51 static const size_t GCM_BS = 16;
52
53 const size_t m_tag_size;
54 const std::string m_cipher_name;
55
56 std::unique_ptr<StreamCipher> m_ctr;
57 std::unique_ptr<GHASH> m_ghash;
58 private:
59 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
60
61 void key_schedule(const uint8_t key[], size_t length) override;
62
63 secure_vector<uint8_t> m_y0;
64 };
65
66/**
67* GCM Encryption
68*/
69 class BOTAN_PUBLIC_API(2,0) GCM_Encryption final : public GCM_Mode
70 {
71 public:
72 /**
73 * @param cipher the 128 bit block cipher to use
74 * @param tag_size is how big the auth tag will be
75 */
76 GCM_Encryption(BlockCipher* cipher, size_t tag_size = 16) :
77 GCM_Mode(cipher, tag_size) {}
78
79 size_t output_length(size_t input_length) const override
80 { return input_length + tag_size(); }
81
82 size_t minimum_final_size() const override { return 0; }
83
84 size_t process(uint8_t buf[], size_t size) override;
85
86 void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
87 };
88
89/**
90* GCM Decryption
91*/
92 class BOTAN_PUBLIC_API(2,0) GCM_Decryption final : public GCM_Mode
93 {
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(BlockCipher* cipher, size_t tag_size = 16) :
100 GCM_Mode(cipher, tag_size) {}
101
102 size_t output_length(size_t input_length) const override
103 {
104 BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
105 return input_length - tag_size();
106 }
107
108 size_t minimum_final_size() const override { return tag_size(); }
109
110 size_t process(uint8_t buf[], size_t size) override;
111
112 void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
113 };
114
115}
116
117#endif
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
Botan::AEAD_Mode
Definition: aead.h:23
Botan::AEAD_Mode::set_associated_data
virtual void set_associated_data(const uint8_t ad[], size_t ad_len)=0
Botan::BlockCipher
Definition: block_cipher.h:22
Botan::Cipher_Mode::start_msg
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)=0
Botan::Cipher_Mode::provider
virtual std::string provider() const
Definition: cipher_mode.h:180
Botan::Cipher_Mode::process
virtual size_t process(uint8_t msg[], size_t msg_len)=0
Botan::Cipher_Mode::reset
virtual void reset()=0
Botan::Cipher_Mode::finish
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0
Botan::Cipher_Mode::tag_size
virtual size_t tag_size() const
Definition: cipher_mode.h:174
Botan::Cipher_Mode::update_granularity
virtual size_t update_granularity() const =0
Botan::Cipher_Mode::valid_nonce_length
virtual bool valid_nonce_length(size_t nonce_len) const =0
Botan::GCM_Decryption
Definition: gcm.h:93
Botan::GCM_Decryption::minimum_final_size
size_t minimum_final_size() const override
Definition: gcm.h:108
Botan::GCM_Decryption::GCM_Decryption
GCM_Decryption(BlockCipher *cipher, size_t tag_size=16)
Definition: gcm.h:99
Botan::GCM_Decryption::output_length
size_t output_length(size_t input_length) const override
Definition: gcm.h:102
Botan::GCM_Encryption
Definition: gcm.h:70
Botan::GCM_Encryption::output_length
size_t output_length(size_t input_length) const override
Definition: gcm.h:79
Botan::GCM_Encryption::GCM_Encryption
GCM_Encryption(BlockCipher *cipher, size_t tag_size=16)
Definition: gcm.h:76
Botan::GCM_Encryption::minimum_final_size
size_t minimum_final_size() const override
Definition: gcm.h:82
Botan::GCM_Mode
Definition: gcm.h:27
Botan::GCM_Mode::m_ghash
std::unique_ptr< GHASH > m_ghash
Definition: gcm.h:57
Botan::GCM_Mode::m_tag_size
const size_t m_tag_size
Definition: gcm.h:53
Botan::GCM_Mode::m_cipher_name
const std::string m_cipher_name
Definition: gcm.h:54
Botan::GCM_Mode::m_ctr
std::unique_ptr< StreamCipher > m_ctr
Definition: gcm.h:56
Botan::GCM_Mode::tag_size
size_t tag_size() const override
Definition: gcm.h:39
Botan::Key_Length_Specification
Definition: sym_algo.h:20
Botan::SymmetricAlgorithm::name
virtual std::string name() const =0
Botan::SymmetricAlgorithm::clear
virtual void clear()=0
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:136
Botan
Definition: alg_id.cpp:13
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65