Botan 3.8.1
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*/
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) override final;
33
34 size_t maximum_associated_data_inputs() const override final;
35
36 std::string name() const override final;
37
38 size_t update_granularity() const override final;
39
40 size_t ideal_granularity() const override final;
41
42 Key_Length_Specification key_spec() const override final;
43
44 bool valid_nonce_length(size_t) const override final;
45
46 bool requires_entire_message() const override final;
47
48 void clear() override final;
49
50 void reset() override final;
51
52 size_t tag_size() const override final { return 16; }
53
54 bool has_keying_material() const override final;
55
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) override final;
73 size_t process_msg(uint8_t buf[], size_t size) override final;
74
75 void key_schedule(std::span<const uint8_t> key) override 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};
85
86/**
87* SIV Encryption
88*/
90 public:
91 /**
92 * @param cipher a block cipher
93 */
94 explicit SIV_Encryption(std::unique_ptr<BlockCipher> cipher) : SIV_Mode(std::move(cipher)) {}
95
96 size_t output_length(size_t input_length) const override { return input_length + tag_size(); }
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* SIV Decryption
106*/
108 public:
109 /**
110 * @param cipher a 128-bit block cipher
111 */
112 explicit SIV_Decryption(std::unique_ptr<BlockCipher> cipher) : SIV_Mode(std::move(cipher)) {}
113
114 size_t output_length(size_t input_length) const override {
115 BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
116 return input_length - tag_size();
117 }
118
119 size_t minimum_final_size() const override { return tag_size(); }
120
121 private:
122 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
123};
124
125} // namespace Botan
126
127#endif
#define BOTAN_TEST_API
Definition api.h:39
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:52
SIV_Decryption(std::unique_ptr< BlockCipher > cipher)
Definition siv.h:112
size_t minimum_final_size() const override
Definition siv.h:119
size_t output_length(size_t input_length) const override
Definition siv.h:114
SIV_Encryption(std::unique_ptr< BlockCipher > cipher)
Definition siv.h:94
size_t minimum_final_size() const override
Definition siv.h:98
size_t output_length(size_t input_length) const override
Definition siv.h:96
size_t block_size() const
Definition siv.h:61
SIV_Mode(std::unique_ptr< BlockCipher > cipher)
Definition siv.cpp:20
void clear() override final
Definition siv.cpp:33
StreamCipher & ctr()
Definition siv.h:63
void reset() override final
Definition siv.cpp:39
size_t update_granularity() const override final
Definition siv.cpp:53
std::string name() const override final
Definition siv.cpp:45
size_t ideal_granularity() const override final
Definition siv.cpp:57
secure_vector< uint8_t > & msg_buf()
Definition siv.h:67
size_t tag_size() const override final
Definition siv.h:52
Key_Length_Specification key_spec() const override final
Definition siv.cpp:66
bool valid_nonce_length(size_t) const override final
Definition siv.cpp:49
size_t maximum_associated_data_inputs() const override final
Definition siv.cpp:81
bool requires_entire_message() const override final
Definition siv.cpp:62
void set_associated_data_n(size_t n, std::span< const uint8_t > ad) override final
Definition siv.cpp:85
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:65