Coverage Report

Created: 2021-11-25 09:31

/src/botan/build/include/botan/internal/ocb.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* OCB Mode
3
* (C) 2013,2014 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_OCB_H_
10
#define BOTAN_AEAD_OCB_H_
11
12
#include <botan/aead.h>
13
14
namespace Botan {
15
16
class BlockCipher;
17
class L_computer;
18
19
/**
20
* OCB Mode (base class for OCB_Encryption and OCB_Decryption).
21
* OCB was previously patented in the United States but the patent
22
* has now been allowed to lapse.
23
*
24
* @see "The OCB Authenticated-Encryption Algorithm" RFC 7253
25
*      https://tools.ietf.org/html/rfc7253
26
* @see "OCB For Block Ciphers Without 128-Bit Blocks"
27
*      (draft-krovetz-ocb-wide-d3) for the extension of OCB to
28
*      block ciphers with larger block sizes.
29
* @see https://mailarchive.ietf.org/arch/msg/cfrg/qLTveWOdTJcLn4HP3ev-vrj05Vg/
30
*/
31
class BOTAN_TEST_API OCB_Mode : public AEAD_Mode
32
   {
33
   public:
34
      void set_associated_data(const uint8_t ad[], size_t ad_len) override;
35
36
      std::string name() const override;
37
38
      size_t update_granularity() const override;
39
40
      Key_Length_Specification key_spec() const override;
41
42
      bool valid_nonce_length(size_t) const override;
43
44
974
      size_t tag_size() const override { return m_tag_size; }
45
46
      void clear() override;
47
48
      void reset() override;
49
50
      ~OCB_Mode();
51
   protected:
52
      /**
53
      * @param cipher the block cipher to use
54
      * @param tag_size is how big the auth tag will be
55
      */
56
      OCB_Mode(std::unique_ptr<BlockCipher> cipher, size_t tag_size);
57
58
1.10k
      size_t block_size() const { return m_block_size; }
59
448
      size_t par_blocks() const { return m_par_blocks; }
60
0
      size_t par_bytes() const { return m_checksum.size(); }
61
62
      // fixme make these private
63
      std::unique_ptr<BlockCipher> m_cipher;
64
      std::unique_ptr<L_computer> m_L;
65
66
      size_t m_block_index = 0;
67
68
      secure_vector<uint8_t> m_checksum;
69
      secure_vector<uint8_t> m_ad_hash;
70
   private:
71
      void start_msg(const uint8_t nonce[], size_t nonce_len) override;
72
73
      void key_schedule(const uint8_t key[], size_t length) override;
74
75
      const secure_vector<uint8_t>& update_nonce(const uint8_t nonce[], size_t nonce_len);
76
77
      const size_t m_tag_size;
78
      const size_t m_block_size;
79
      const size_t m_par_blocks;
80
      secure_vector<uint8_t> m_last_nonce;
81
      secure_vector<uint8_t> m_stretch;
82
      secure_vector<uint8_t> m_nonce_buf;
83
      secure_vector<uint8_t> m_offset;
84
   };
85
86
class BOTAN_TEST_API OCB_Encryption final : public OCB_Mode
87
   {
88
   public:
89
      /**
90
      * @param cipher the block cipher to use
91
      * @param tag_size is how big the auth tag will be
92
      */
93
      OCB_Encryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
94
89
         OCB_Mode(std::move(cipher), tag_size) {}
95
96
      size_t output_length(size_t input_length) const override
97
140
         { return input_length + tag_size(); }
98
99
0
      size_t minimum_final_size() const override { return 0; }
100
101
      size_t process(uint8_t buf[], size_t size) override;
102
103
      void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
104
   private:
105
      void encrypt(uint8_t input[], size_t blocks);
106
   };
107
108
class BOTAN_TEST_API OCB_Decryption final : public OCB_Mode
109
   {
110
   public:
111
      /**
112
      * @param cipher the block cipher to use
113
      * @param tag_size is how big the auth tag will be
114
      */
115
      OCB_Decryption(std::unique_ptr<BlockCipher> cipher, size_t tag_size = 16) :
116
139
         OCB_Mode(std::move(cipher), tag_size) {}
117
118
      size_t output_length(size_t input_length) const override
119
79
         {
120
79
         BOTAN_ASSERT(input_length >= tag_size(), "Sufficient input");
121
79
         return input_length - tag_size();
122
79
         }
123
124
80
      size_t minimum_final_size() const override { return tag_size(); }
125
126
      size_t process(uint8_t buf[], size_t size) override;
127
128
      void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
129
   private:
130
      void decrypt(uint8_t input[], size_t blocks);
131
   };
132
133
}
134
135
#endif