Botan  2.13.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 
16 
17 namespace 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);
37 
38  void set_associated_data(const uint8_t ad[], size_t ad_len) override
39  {
40  set_associated_data_n(0, ad, ad_len);
41  }
42 
43  std::string name() const override;
44 
45  size_t update_granularity() const override;
46 
47  Key_Length_Specification key_spec() const override;
48 
49  bool valid_nonce_length(size_t) const override;
50 
51  void clear() override;
52 
53  void reset() override;
54 
55  size_t tag_size() const override { return 16; }
56 
57  ~SIV_Mode();
58 
59  protected:
60  explicit SIV_Mode(BlockCipher* cipher);
61 
62  size_t block_size() const { return m_bs; }
63 
64  StreamCipher& ctr() { return *m_ctr; }
65 
66  void set_ctr_iv(secure_vector<uint8_t> V);
67 
68  secure_vector<uint8_t>& msg_buf() { return m_msg_buf; }
69 
70  secure_vector<uint8_t> S2V(const uint8_t text[], size_t text_len);
71  private:
72  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
73 
74  void key_schedule(const uint8_t key[], size_t length) override;
75 
76  const std::string m_name;
77  std::unique_ptr<StreamCipher> m_ctr;
78  std::unique_ptr<MessageAuthenticationCode> m_mac;
79  secure_vector<uint8_t> m_nonce, m_msg_buf;
80  std::vector<secure_vector<uint8_t>> m_ad_macs;
81  const size_t m_bs;
82  };
83 
84 /**
85 * SIV Encryption
86 */
88  {
89  public:
90  /**
91  * @param cipher a block cipher
92  */
93  explicit SIV_Encryption(BlockCipher* cipher) : SIV_Mode(cipher) {}
94 
95  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
96 
97  size_t output_length(size_t input_length) const override
98  { return input_length + tag_size(); }
99 
100  size_t minimum_final_size() const override { return 0; }
101  };
102 
103 /**
104 * SIV Decryption
105 */
107  {
108  public:
109  /**
110  * @param cipher a 128-bit block cipher
111  */
112  explicit SIV_Decryption(BlockCipher* cipher) : SIV_Mode(cipher) {}
113 
114  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
115 
116  size_t output_length(size_t input_length) const override
117  {
118  BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
119  return input_length - tag_size();
120  }
121 
122  size_t minimum_final_size() const override { return tag_size(); }
123  };
124 
125 }
126 
127 #endif
secure_vector< uint8_t > & msg_buf()
Definition: siv.h:68
size_t output_length(size_t input_length) const override
Definition: siv.h:116
int(* final)(unsigned char *, CTX *)
virtual size_t update_granularity() const =0
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
SIV_Encryption(BlockCipher *cipher)
Definition: siv.h:93
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)=0
virtual void reset()=0
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
size_t minimum_final_size() const override
Definition: siv.h:122
SIV_Decryption(BlockCipher *cipher)
Definition: siv.h:112
virtual std::string name() const =0
void set_associated_data(const uint8_t ad[], size_t ad_len) override
Definition: siv.h:38
size_t block_size() const
Definition: siv.h:62
virtual void clear()=0
Definition: alg_id.cpp:13
size_t tag_size() const override
Definition: siv.h:55
virtual Key_Length_Specification key_spec() const =0
virtual size_t process(uint8_t msg[], size_t msg_len)=0
StreamCipher & ctr()
Definition: siv.h:64
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
size_t output_length(size_t input_length) const override
Definition: siv.h:97
#define BOTAN_FUTURE_INTERNAL_HEADER(hdr)
Definition: compiler.h:139
virtual bool valid_nonce_length(size_t nonce_len) const =0
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0
size_t minimum_final_size() const override
Definition: siv.h:100