Botan 3.13.0
Crypto and TLS for C&
siv.h
Go to the documentation of this file.
1/*
2* SIV 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_SIV_H_
10#define BOTAN_AEAD_SIV_H_
11
12#include <botan/aead.h>
13
14#include <botan/assert.h>
15#include <botan/block_cipher.h>
16#include <botan/stream_cipher.h>
17
18namespace Botan {
19
21
22/**
23* Base class for SIV encryption and decryption (@see RFC 5297)
24*/
25class BOTAN_TEST_API SIV_Mode : public AEAD_Mode /* NOLINT(*-special-member-functions) */ {
26 public:
27 /**
28 * Sets the nth element of the vector of associated data
29 * @param n index into the AD vector
30 * @param ad associated data
31 */
32 void set_associated_data_n(size_t n, std::span<const uint8_t> ad) final;
33
34 size_t maximum_associated_data_inputs() const 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 bool requires_entire_message() const final;
47
48 void clear() final;
49
50 void reset() final;
51
52 size_t tag_size() const final { return 16; }
53
54 bool has_keying_material() const final;
55
56 ~SIV_Mode() override;
57
58 protected:
59 explicit SIV_Mode(std::unique_ptr<BlockCipher> cipher);
60
61 size_t block_size() const { return m_bs; }
62
63 StreamCipher& ctr() { return *m_ctr; }
64
65 void set_ctr_iv(secure_vector<uint8_t> V);
66
67 secure_vector<uint8_t>& msg_buf() { return m_msg_buf; }
68
69 secure_vector<uint8_t> S2V(const uint8_t text[], size_t text_len);
70
71 private:
72 void start_msg(const uint8_t nonce[], size_t nonce_len) final;
73 size_t process_msg(uint8_t buf[], size_t size) final;
74
75 void key_schedule(std::span<const uint8_t> key) final;
76
77 const std::string m_name;
78 const size_t m_bs;
79
80 std::unique_ptr<StreamCipher> m_ctr;
81 std::unique_ptr<MessageAuthenticationCode> m_mac;
82 secure_vector<uint8_t> m_nonce, m_msg_buf;
83 std::vector<secure_vector<uint8_t>> m_ad_macs;
84 bool m_in_msg = false;
85};
86
87/**
88* SIV Encryption
89*/
91 public:
92 /**
93 * @param cipher a block cipher
94 */
95 explicit SIV_Encryption(std::unique_ptr<BlockCipher> cipher) : SIV_Mode(std::move(cipher)) {}
96
97 size_t output_length(size_t input_length) const override;
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* SIV Decryption
107*/
109 public:
110 /**
111 * @param cipher a 128-bit block cipher
112 */
113 explicit SIV_Decryption(std::unique_ptr<BlockCipher> cipher) : SIV_Mode(std::move(cipher)) {}
114
115 size_t output_length(size_t input_length) const override;
116
117 size_t minimum_final_size() const override { return tag_size(); }
118
119 private:
120 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
121};
122
123} // namespace Botan
124
125#endif
#define BOTAN_TEST_API
Definition api.h:41
SIV_Decryption(std::unique_ptr< BlockCipher > cipher)
Definition siv.h:113
size_t minimum_final_size() const override
Definition siv.h:117
SIV_Encryption(std::unique_ptr< BlockCipher > cipher)
Definition siv.h:95
size_t minimum_final_size() const override
Definition siv.h:99
void clear() final
Definition siv.cpp:35
size_t block_size() const
Definition siv.h:61
bool requires_entire_message() const final
Definition siv.cpp:65
SIV_Mode(std::unique_ptr< BlockCipher > cipher)
Definition siv.cpp:22
StreamCipher & ctr()
Definition siv.h:63
size_t tag_size() const final
Definition siv.h:52
size_t ideal_granularity() const final
Definition siv.cpp:60
size_t update_granularity() const final
Definition siv.cpp:56
bool valid_nonce_length(size_t length) const final
Definition siv.cpp:52
std::string name() const final
Definition siv.cpp:48
secure_vector< uint8_t > & msg_buf()
Definition siv.h:67
size_t maximum_associated_data_inputs() const final
Definition siv.cpp:85
void reset() final
Definition siv.cpp:42
Key_Length_Specification key_spec() const final
Definition siv.cpp:69
void set_associated_data_n(size_t n, std::span< const uint8_t > ad) final
Definition siv.cpp:89
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128