Botan 3.14.1
Crypto and TLS for C&
ocb.h
Go to the documentation of this file.
1/*
2* OCB Mode
3* (C) 2013,2014 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_OCB_H_
10#define BOTAN_AEAD_OCB_H_
11
12#include <botan/aead.h>
13
14#include <botan/block_cipher.h>
15
16namespace Botan {
17
18class L_computer;
19
20/**
21* OCB Mode (base class for OCB_Encryption and OCB_Decryption).
22* OCB was previously patented in the United States but the patent
23* has now been allowed to lapse.
24*
25* @see "The OCB Authenticated-Encryption Algorithm" RFC 7253
26* https://tools.ietf.org/html/rfc7253
27* @see "OCB For Block Ciphers Without 128-Bit Blocks"
28* (draft-krovetz-ocb-wide-d3) for the extension of OCB to
29* block ciphers with larger block sizes.
30* @see https://mailarchive.ietf.org/arch/msg/cfrg/qLTveWOdTJcLn4HP3ev-vrj05Vg/
31*/
32class BOTAN_TEST_API OCB_Mode : public AEAD_Mode /* NOLINT(*-special-member-functions) */ {
33 public:
34 void set_associated_data_n(size_t idx, std::span<const uint8_t> ad) final;
35
36 std::string name() const final;
37
38 size_t update_granularity() const final;
39
40 size_t ideal_granularity() const final;
41
43
44 bool valid_nonce_length(size_t length) const final;
45
46 size_t tag_size() const final { return m_tag_size; }
47
48 void clear() final;
49
50 void reset() final;
51
52 bool has_keying_material() const final;
53
54 ~OCB_Mode() override;
55
56 protected:
57 /**
58 * @param cipher the block cipher to use
59 * @param tag_size is how big the auth tag will be
60 */
61 OCB_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size);
62
63 size_t block_size() const { return m_block_size; }
64
65 size_t par_blocks() const { return m_par_blocks; }
66
67 size_t par_bytes() const { return m_checksum.size(); }
68
69 // fixme make these private
70 std::unique_ptr<BlockCipher> m_cipher; // NOLINT(*non-private-member-variables*)
71 std::unique_ptr<L_computer> m_L; // NOLINT(*non-private-member-variables*)
72
73 uint64_t m_block_index = 0; // NOLINT(*non-private-member-variables*)
74
75 secure_vector<uint8_t> m_checksum; // NOLINT(*non-private-member-variables*)
76 secure_vector<uint8_t> m_ad_hash; // NOLINT(*non-private-member-variables*)
77
78 private:
79 void start_msg(const uint8_t nonce[], size_t nonce_len) final;
80
81 void key_schedule(std::span<const uint8_t> key) final;
82
83 const secure_vector<uint8_t>& update_nonce(const uint8_t nonce[], size_t nonce_len);
84
85 const size_t m_tag_size;
86 const size_t m_block_size;
87 const size_t m_par_blocks;
88 secure_vector<uint8_t> m_last_nonce;
89 secure_vector<uint8_t> m_stretch;
90 secure_vector<uint8_t> m_nonce_buf;
92};
93
95 public:
96 /**
97 * @param cipher the block cipher to use
98 * @param tag_size is how big the auth tag will be
99 */
100 explicit OCB_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
101 OCB_Mode(std::move(cipher), tag_size) {}
102
103 size_t output_length(size_t input_length) const override;
104
105 size_t minimum_final_size() const override { return 0; }
106
107 private:
108 void encrypt(uint8_t input[], size_t blocks);
109 size_t process_msg(uint8_t buf[], size_t size) override;
110 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
111};
112
114 public:
115 /**
116 * @param cipher the block cipher to use
117 * @param tag_size is how big the auth tag will be
118 */
119 explicit OCB_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
120 OCB_Mode(std::move(cipher), tag_size) {}
121
122 size_t output_length(size_t input_length) const override;
123
124 size_t minimum_final_size() const override { return tag_size(); }
125
126 private:
127 void decrypt(uint8_t input[], size_t blocks);
128 size_t process_msg(uint8_t buf[], size_t size) override;
129 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
130};
131
132} // namespace Botan
133
134#endif
#define BOTAN_TEST_API
Definition api.h:41
size_t minimum_final_size() const override
Definition ocb.h:124
OCB_Decryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition ocb.h:119
OCB_Encryption(std::unique_ptr< BlockCipher > cipher, size_t tag_size=16)
Definition ocb.h:100
size_t minimum_final_size() const override
Definition ocb.h:105
uint64_t m_block_index
Definition ocb.h:73
size_t par_bytes() const
Definition ocb.h:67
size_t block_size() const
Definition ocb.h:63
size_t par_blocks() const
Definition ocb.h:65
Key_Length_Specification key_spec() const final
Definition ocb.cpp:229
size_t tag_size() const final
Definition ocb.h:46
secure_vector< uint8_t > m_checksum
Definition ocb.h:75
bool valid_nonce_length(size_t length) const final
Definition ocb.cpp:206
std::unique_ptr< BlockCipher > m_cipher
Definition ocb.h:70
secure_vector< uint8_t > m_ad_hash
Definition ocb.h:76
size_t ideal_granularity() const final
Definition ocb.cpp:225
OCB_Mode(std::unique_ptr< BlockCipher > cipher, size_t tag_size)
Definition ocb.cpp:165
void set_associated_data_n(size_t idx, std::span< const uint8_t > ad) final
Definition ocb.cpp:252
size_t update_granularity() const final
Definition ocb.cpp:221
std::unique_ptr< L_computer > m_L
Definition ocb.h:71
const char * name
Definition ffi_tls.h:59
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128