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