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