Botan  2.4.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  std::string name() const override;
29 
30  size_t update_granularity() const override;
31 
32  Key_Length_Specification key_spec() const override;
33 
34  bool valid_nonce_length(size_t) const override;
35 
36  size_t default_nonce_length() const override;
37 
38  void clear() override;
39 
40  void reset() override;
41 
42  size_t tag_size() const override { return m_tag_size; }
43 
44  protected:
45  CCM_Mode(BlockCipher* cipher, size_t tag_size, size_t L);
46 
47  size_t L() const { return m_L; }
48 
49  const BlockCipher& cipher() const { return *m_cipher; }
50 
51  void encode_length(size_t len, uint8_t out[]);
52 
53  void inc(secure_vector<uint8_t>& C);
54 
55  const secure_vector<uint8_t>& ad_buf() const { return m_ad_buf; }
56 
57  secure_vector<uint8_t>& msg_buf() { return m_msg_buf; }
58 
59  secure_vector<uint8_t> format_b0(size_t msg_size);
60  secure_vector<uint8_t> format_c0();
61  private:
62  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
63 
64  void key_schedule(const uint8_t key[], size_t length) override;
65 
66  const size_t m_tag_size;
67  const size_t m_L;
68 
69  std::unique_ptr<BlockCipher> m_cipher;
70  secure_vector<uint8_t> m_nonce, m_msg_buf, m_ad_buf;
71  };
72 
73 /**
74 * CCM Encryption
75 */
76 class BOTAN_PUBLIC_API(2,0) CCM_Encryption final : public CCM_Mode
77  {
78  public:
79  /**
80  * @param cipher a 128-bit block cipher
81  * @param tag_size is how big the auth tag will be (even values
82  * between 4 and 16 are accepted)
83  * @param L length of L parameter. The total message length
84  * must be less than 2**L bytes, and the nonce is 15-L bytes.
85  */
86  CCM_Encryption(BlockCipher* cipher, size_t tag_size = 16, size_t L = 3) :
87  CCM_Mode(cipher, tag_size, L) {}
88 
89  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
90 
91  size_t output_length(size_t input_length) const override
92  { return input_length + tag_size(); }
93 
94  size_t minimum_final_size() const override { return 0; }
95  };
96 
97 /**
98 * CCM Decryption
99 */
100 class BOTAN_PUBLIC_API(2,0) CCM_Decryption final : public CCM_Mode
101  {
102  public:
103  /**
104  * @param cipher a 128-bit block cipher
105  * @param tag_size is how big the auth tag will be (even values
106  * between 4 and 16 are accepted)
107  * @param L length of L parameter. The total message length
108  * must be less than 2**L bytes, and the nonce is 15-L bytes.
109  */
110  CCM_Decryption(BlockCipher* cipher, size_t tag_size = 16, size_t L = 3) :
111  CCM_Mode(cipher, tag_size, L) {}
112 
113  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
114 
115  size_t output_length(size_t input_length) const override
116  {
117  BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
118  return input_length - tag_size();
119  }
120 
121  size_t minimum_final_size() const override { return tag_size(); }
122  };
123 
124 }
125 
126 #endif
size_t output_length(size_t input_length) const override
Definition: ccm.h:91
size_t L() const
Definition: ccm.h:47
const BlockCipher & cipher() const
Definition: ccm.h:49
#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:110
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:29
Definition: alg_id.cpp:13
size_t minimum_final_size() const override
Definition: ccm.h:94
size_t output_length(size_t input_length) const override
Definition: ccm.h:115
size_t minimum_final_size() const override
Definition: ccm.h:121
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:86
const secure_vector< uint8_t > & ad_buf() const
Definition: ccm.h:55
secure_vector< uint8_t > & msg_buf()
Definition: ccm.h:57
size_t tag_size() const override
Definition: ccm.h:42