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