Botan  2.8.0
Crypto and TLS for C++11
ccm.h
Go to the documentation of this file.
1 /*
2 * CCM 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_CCM_H_
10 #define BOTAN_AEAD_CCM_H_
11 
12 #include <botan/aead.h>
13 #include <botan/block_cipher.h>
14 
15 namespace Botan {
16 
17 /**
18 * Base class for CCM encryption and decryption
19 * @see RFC 3610
20 */
21 class BOTAN_PUBLIC_API(2,0) CCM_Mode : public AEAD_Mode
22  {
23  public:
24  size_t process(uint8_t buf[], size_t sz) override;
25 
26  void set_associated_data(const uint8_t ad[], size_t ad_len) override;
27 
28  bool associated_data_requires_key() const override { return false; }
29 
30  std::string name() const override;
31 
32  size_t update_granularity() const override;
33 
34  Key_Length_Specification key_spec() const override;
35 
36  bool valid_nonce_length(size_t) const override;
37 
38  size_t default_nonce_length() const override;
39 
40  void clear() override;
41 
42  void reset() override;
43 
44  size_t tag_size() const override { return m_tag_size; }
45 
46  protected:
47  CCM_Mode(BlockCipher* cipher, size_t tag_size, size_t L);
48 
49  size_t L() const { return m_L; }
50 
51  const BlockCipher& cipher() const { return *m_cipher; }
52 
53  void encode_length(uint64_t len, uint8_t out[]);
54 
55  void inc(secure_vector<uint8_t>& C);
56 
57  const secure_vector<uint8_t>& ad_buf() const { return m_ad_buf; }
58 
59  secure_vector<uint8_t>& msg_buf() { return m_msg_buf; }
60 
61  secure_vector<uint8_t> format_b0(size_t msg_size);
62  secure_vector<uint8_t> format_c0();
63  private:
64  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
65 
66  void key_schedule(const uint8_t key[], size_t length) override;
67 
68  const size_t m_tag_size;
69  const size_t m_L;
70 
71  std::unique_ptr<BlockCipher> m_cipher;
72  secure_vector<uint8_t> m_nonce, m_msg_buf, m_ad_buf;
73  };
74 
75 /**
76 * CCM Encryption
77 */
79  {
80  public:
81  /**
82  * @param cipher a 128-bit block cipher
83  * @param tag_size is how big the auth tag will be (even values
84  * between 4 and 16 are accepted)
85  * @param L length of L parameter. The total message length
86  * must be less than 2**L bytes, and the nonce is 15-L bytes.
87  */
88  CCM_Encryption(BlockCipher* cipher, size_t tag_size = 16, size_t L = 3) :
89  CCM_Mode(cipher, tag_size, L) {}
90 
91  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
92 
93  size_t output_length(size_t input_length) const override
94  { return input_length + tag_size(); }
95 
96  size_t minimum_final_size() const override { return 0; }
97  };
98 
99 /**
100 * CCM Decryption
101 */
103  {
104  public:
105  /**
106  * @param cipher a 128-bit block cipher
107  * @param tag_size is how big the auth tag will be (even values
108  * between 4 and 16 are accepted)
109  * @param L length of L parameter. The total message length
110  * must be less than 2**L bytes, and the nonce is 15-L bytes.
111  */
112  CCM_Decryption(BlockCipher* cipher, size_t tag_size = 16, size_t L = 3) :
113  CCM_Mode(cipher, tag_size, L) {}
114 
115  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
116 
117  size_t output_length(size_t input_length) const override
118  {
119  BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
120  return input_length - tag_size();
121  }
122 
123  size_t minimum_final_size() const override { return tag_size(); }
124  };
125 
126 }
127 
128 #endif
size_t output_length(size_t input_length) const override
Definition: ccm.h:93
size_t L() const
Definition: ccm.h:49
const BlockCipher & cipher() const
Definition: ccm.h:51
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
CCM_Decryption(BlockCipher *cipher, size_t tag_size=16, size_t L=3)
Definition: ccm.h:112
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
std::string name
bool associated_data_requires_key() const override
Definition: ccm.h:28
Definition: alg_id.cpp:13
size_t minimum_final_size() const override
Definition: ccm.h:96
size_t output_length(size_t input_length) const override
Definition: ccm.h:117
size_t minimum_final_size() const override
Definition: ccm.h:123
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
CCM_Encryption(BlockCipher *cipher, size_t tag_size=16, size_t L=3)
Definition: ccm.h:88
const secure_vector< uint8_t > & ad_buf() const
Definition: ccm.h:57
secure_vector< uint8_t > & msg_buf()
Definition: ccm.h:59
size_t tag_size() const override
Definition: ccm.h:44