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