Botan 2.19.5 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#include <botan/stream_cipher.h>
14
15 BOTAN_FUTURE_INTERNAL_HEADER(siv.h)
16
17namespace Botan {
18
19 class BlockCipher;
20 class MessageAuthenticationCode;
21
22/**
23* Base class for SIV encryption and decryption (@see RFC 5297)
24*/
25 class BOTAN_PUBLIC_API(2,0) SIV_Mode : public AEAD_Mode
26 {
27 public:
28 size_t process(uint8_t buf[], size_t size) override;
29
30 /**
31 * Sets the nth element of the vector of associated data
32 * @param n index into the AD vector
33 * @param ad associated data
34 * @param ad_len length of associated data in bytes
35 */
36 void set_associated_data_n(size_t n, const uint8_t ad[], size_t ad_len) override;
37
38 size_t maximum_associated_data_inputs() const override;
39
40 void set_associated_data(const uint8_t ad[], size_t ad_len) override
41 {
42 set_associated_data_n(0, ad, ad_len);
43 }
44
45 std::string name() const override;
46
47 size_t update_granularity() const override;
48
49 Key_Length_Specification key_spec() const override;
50
51 bool valid_nonce_length(size_t) const override;
52
53 void clear() override;
54
55 void reset() override;
56
57 size_t tag_size() const override { return 16; }
58
59 ~SIV_Mode();
60
61 protected:
62 explicit SIV_Mode(BlockCipher* cipher);
63
64 size_t block_size() const { return m_bs; }
65
66 StreamCipher& ctr() { return *m_ctr; }
67
68 void set_ctr_iv(secure_vector<uint8_t> V);
69
70 secure_vector<uint8_t>& msg_buf() { return m_msg_buf; }
71
72 secure_vector<uint8_t> S2V(const uint8_t text[], size_t text_len);
73 private:
74 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
75
76 void key_schedule(const uint8_t key[], size_t length) override;
77
78 const std::string m_name;
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 const size_t m_bs;
84 };
85
86/**
87* SIV Encryption
88*/
89 class BOTAN_PUBLIC_API(2,0) SIV_Encryption final : public SIV_Mode
90 {
91 public:
92 /**
93 * @param cipher a block cipher
94 */
95 explicit SIV_Encryption(BlockCipher* cipher) : SIV_Mode(cipher) {}
96
97 void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
98
99 size_t output_length(size_t input_length) const override
100 { return input_length + tag_size(); }
101
102 size_t minimum_final_size() const override { return 0; }
103 };
104
105/**
106* SIV Decryption
107*/
108 class BOTAN_PUBLIC_API(2,0) SIV_Decryption final : public SIV_Mode
109 {
110 public:
111 /**
112 * @param cipher a 128-bit block cipher
113 */
114 explicit SIV_Decryption(BlockCipher* cipher) : SIV_Mode(cipher) {}
115
116 void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
117
118 size_t output_length(size_t input_length) const override
119 {
120 BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
121 return input_length - tag_size();
122 }
123
124 size_t minimum_final_size() const override { return tag_size(); }
125 };
126
127}
128
129#endif
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
Botan::AEAD_Mode
Definition: aead.h:23
Botan::AEAD_Mode::maximum_associated_data_inputs
virtual size_t maximum_associated_data_inputs() const
Definition: aead.h:87
Botan::AEAD_Mode::set_associated_data_n
virtual void set_associated_data_n(size_t i, const uint8_t ad[], size_t ad_len)
Definition: aead.cpp:42
Botan::BlockCipher
Definition: block_cipher.h:22
Botan::Cipher_Mode::start_msg
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)=0
Botan::Cipher_Mode::process
virtual size_t process(uint8_t msg[], size_t msg_len)=0
Botan::Cipher_Mode::reset
virtual void reset()=0
Botan::Cipher_Mode::finish
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0
Botan::Cipher_Mode::tag_size
virtual size_t tag_size() const
Definition: cipher_mode.h:174
Botan::Cipher_Mode::update_granularity
virtual size_t update_granularity() const =0
Botan::Cipher_Mode::valid_nonce_length
virtual bool valid_nonce_length(size_t nonce_len) const =0
Botan::Key_Length_Specification
Definition: sym_algo.h:20
Botan::SIV_Decryption
Definition: siv.h:109
Botan::SIV_Decryption::SIV_Decryption
SIV_Decryption(BlockCipher *cipher)
Definition: siv.h:114
Botan::SIV_Decryption::minimum_final_size
size_t minimum_final_size() const override
Definition: siv.h:124
Botan::SIV_Decryption::output_length
size_t output_length(size_t input_length) const override
Definition: siv.h:118
Botan::SIV_Encryption
Definition: siv.h:90
Botan::SIV_Encryption::minimum_final_size
size_t minimum_final_size() const override
Definition: siv.h:102
Botan::SIV_Encryption::output_length
size_t output_length(size_t input_length) const override
Definition: siv.h:99
Botan::SIV_Encryption::SIV_Encryption
SIV_Encryption(BlockCipher *cipher)
Definition: siv.h:95
Botan::SIV_Mode
Definition: siv.h:26
Botan::SIV_Mode::set_associated_data
void set_associated_data(const uint8_t ad[], size_t ad_len) override
Definition: siv.h:40
Botan::SIV_Mode::block_size
size_t block_size() const
Definition: siv.h:64
Botan::SIV_Mode::ctr
StreamCipher & ctr()
Definition: siv.h:66
Botan::SIV_Mode::tag_size
size_t tag_size() const override
Definition: siv.h:57
Botan::SIV_Mode::msg_buf
secure_vector< uint8_t > & msg_buf()
Definition: siv.h:70
Botan::StreamCipher
Definition: stream_cipher.h:22
Botan::SymmetricAlgorithm::name
virtual std::string name() const =0
Botan::SymmetricAlgorithm::clear
virtual void clear()=0
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
BOTAN_FUTURE_INTERNAL_HEADER
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:136
Botan
Definition: alg_id.cpp:13
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65