Botan 3.14.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/block_cipher.h>
15#include <botan/stream_cipher.h>
16
17namespace Botan {
18
20
21/**
22* Base class for SIV encryption and decryption (@see RFC 5297)
23*/
24class BOTAN_TEST_API SIV_Mode : public AEAD_Mode /* NOLINT(*-special-member-functions) */ {
25 public:
26 /**
27 * Sets the nth element of the vector of associated data
28 * @param n index into the AD vector
29 * @param ad associated data
30 */
31 void set_associated_data_n(size_t n, std::span<const uint8_t> ad) final;
32
33 size_t maximum_associated_data_inputs() const final;
34
35 std::string name() const final;
36
37 size_t update_granularity() const final;
38
39 size_t ideal_granularity() const final;
40
42
43 bool valid_nonce_length(size_t length) const final;
44
45 bool requires_entire_message() const final;
46
47 void clear() final;
48
49 void reset() final;
50
51 size_t tag_size() const final { return 16; }
52
53 bool has_keying_material() const final;
54
55 ~SIV_Mode() override;
56
57 protected:
58 explicit SIV_Mode(std::unique_ptr<BlockCipher> cipher);
59
60 size_t block_size() const { return m_bs; }
61
62 StreamCipher& ctr() { return *m_ctr; }
63
64 void set_ctr_iv(secure_vector<uint8_t> V);
65
66 secure_vector<uint8_t>& msg_buf() { return m_msg_buf; }
67
68 secure_vector<uint8_t> S2V(const uint8_t text[], size_t text_len);
69
70 private:
71 void start_msg(const uint8_t nonce[], size_t nonce_len) final;
72 size_t process_msg(uint8_t buf[], size_t size) final;
73
74 void key_schedule(std::span<const uint8_t> key) final;
75
76 const std::string m_name;
77 const size_t m_bs;
78
79 std::unique_ptr<StreamCipher> m_ctr;
80 std::unique_ptr<MessageAuthenticationCode> m_mac;
81 secure_vector<uint8_t> m_nonce, m_msg_buf;
82 std::vector<secure_vector<uint8_t>> m_ad_macs;
83 bool m_in_msg = false;
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;
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
116 size_t minimum_final_size() const override { return tag_size(); }
117
118 private:
119 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
120};
121
122} // namespace Botan
123
124#endif
#define BOTAN_TEST_API
Definition api.h:41
SIV_Decryption(std::unique_ptr< BlockCipher > cipher)
Definition siv.h:112
size_t minimum_final_size() const override
Definition siv.h:116
SIV_Encryption(std::unique_ptr< BlockCipher > cipher)
Definition siv.h:94
size_t minimum_final_size() const override
Definition siv.h:98
void clear() final
Definition siv.cpp:35
size_t block_size() const
Definition siv.h:60
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:62
size_t tag_size() const final
Definition siv.h:51
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
secure_vector< uint8_t > & msg_buf()
Definition siv.h:66
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
const char * text
Definition ffi_tls.h:75
const char * name
Definition ffi_tls.h:59
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128