Coverage Report

Created: 2022-06-23 06:44

/src/botan/build/include/botan/internal/siv.h
Line
Count
Source (jump to first uncovered line)
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_TEST_API 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) override;
35
36
      size_t maximum_associated_data_inputs() const override;
37
38
      void set_associated_data(const uint8_t ad[], size_t ad_len) override
39
0
         {
40
0
         set_associated_data_n(0, ad, ad_len);
41
0
         }
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
0
      size_t tag_size() const override { return 16; }
56
57
      ~SIV_Mode();
58
59
   protected:
60
      explicit SIV_Mode(std::unique_ptr<BlockCipher> cipher);
61
62
0
      size_t block_size() const { return m_bs; }
63
64
0
      StreamCipher& ctr() { return *m_ctr; }
65
66
      void set_ctr_iv(secure_vector<uint8_t> V);
67
68
0
      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
      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
   };
84
85
/**
86
* SIV Encryption
87
*/
88
class BOTAN_TEST_API SIV_Encryption final : public SIV_Mode
89
   {
90
   public:
91
      /**
92
      * @param cipher a block cipher
93
      */
94
      explicit SIV_Encryption(std::unique_ptr<BlockCipher> cipher) :
95
0
         SIV_Mode(std::move(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
0
         { return input_length + tag_size(); }
101
102
0
      size_t minimum_final_size() const override { return 0; }
103
   };
104
105
/**
106
* SIV Decryption
107
*/
108
class BOTAN_TEST_API SIV_Decryption final : public SIV_Mode
109
   {
110
   public:
111
      /**
112
      * @param cipher a 128-bit block cipher
113
      */
114
      explicit SIV_Decryption(std::unique_ptr<BlockCipher> cipher) :
115
0
         SIV_Mode(std::move(cipher)) {}
116
117
      void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
118
119
      size_t output_length(size_t input_length) const override
120
0
         {
121
0
         BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
122
0
         return input_length - tag_size();
123
0
         }
124
125
0
      size_t minimum_final_size() const override { return tag_size(); }
126
   };
127
128
}
129
130
#endif