Botan  2.1.0
Crypto and TLS for C++11
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/block_cipher.h>
14 #include <botan/stream_cipher.h>
15 #include <botan/mac.h>
16 
17 namespace Botan {
18 
19 /**
20 * Base class for SIV encryption and decryption (@see RFC 5297)
21 */
22 class BOTAN_DLL SIV_Mode : public AEAD_Mode
23  {
24  public:
25  size_t process(uint8_t buf[], size_t size) override;
26 
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  * @param ad_len length of associated data in bytes
32  */
33  void set_associated_data_n(size_t n, const uint8_t ad[], size_t ad_len);
34 
35  void set_associated_data(const uint8_t ad[], size_t ad_len) override
36  {
37  set_associated_data_n(0, ad, ad_len);
38  }
39 
40  std::string name() const override;
41 
42  size_t update_granularity() const override;
43 
44  Key_Length_Specification key_spec() const override;
45 
46  bool valid_nonce_length(size_t) const override;
47 
48  void clear() override;
49 
50  void reset() override;
51 
52  size_t tag_size() const override { return 16; }
53 
54  protected:
55  explicit SIV_Mode(BlockCipher* cipher);
56 
57  StreamCipher& ctr() { return *m_ctr; }
58 
59  void set_ctr_iv(secure_vector<uint8_t> V);
60 
61  secure_vector<uint8_t>& msg_buf() { return m_msg_buf; }
62 
63  secure_vector<uint8_t> S2V(const uint8_t text[], size_t text_len);
64  private:
65  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
66 
67  void key_schedule(const uint8_t key[], size_t length) override;
68 
69  const std::string m_name;
70  std::unique_ptr<StreamCipher> m_ctr;
71  std::unique_ptr<MessageAuthenticationCode> m_cmac;
72  secure_vector<uint8_t> m_nonce, m_msg_buf;
73  std::vector<secure_vector<uint8_t>> m_ad_macs;
74  };
75 
76 /**
77 * SIV Encryption
78 */
79 class BOTAN_DLL SIV_Encryption final : public SIV_Mode
80  {
81  public:
82  /**
83  * @param cipher a block cipher
84  */
85  explicit SIV_Encryption(BlockCipher* cipher) : SIV_Mode(cipher) {}
86 
87  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
88 
89  size_t output_length(size_t input_length) const override
90  { return input_length + tag_size(); }
91 
92  size_t minimum_final_size() const override { return 0; }
93  };
94 
95 /**
96 * SIV Decryption
97 */
98 class BOTAN_DLL SIV_Decryption final : public SIV_Mode
99  {
100  public:
101  /**
102  * @param cipher a 128-bit block cipher
103  */
104  explicit SIV_Decryption(BlockCipher* cipher) : SIV_Mode(cipher) {}
105 
106  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
107 
108  size_t output_length(size_t input_length) const override
109  {
110  BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
111  return input_length - tag_size();
112  }
113 
114  size_t minimum_final_size() const override { return tag_size(); }
115  };
116 
117 }
118 
119 #endif
secure_vector< uint8_t > & msg_buf()
Definition: siv.h:61
size_t output_length(size_t input_length) const override
Definition: siv.h:108
std::string m_name
SIV_Encryption(BlockCipher *cipher)
Definition: siv.h:85
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
size_t minimum_final_size() const override
Definition: siv.h:114
SIV_Decryption(BlockCipher *cipher)
Definition: siv.h:104
void set_associated_data(const uint8_t ad[], size_t ad_len) override
Definition: siv.h:35
Definition: alg_id.cpp:13
size_t tag_size() const override
Definition: siv.h:52
StreamCipher & ctr()
Definition: siv.h:57
size_t output_length(size_t input_length) const override
Definition: siv.h:89
size_t minimum_final_size() const override
Definition: siv.h:92