• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5111374265

29 May 2023 11:19AM UTC coverage: 92.227% (+0.5%) from 91.723%
5111374265

push

github

randombit
Next release will be 3.1.0. Update release notes

75588 of 81959 relevant lines covered (92.23%)

11886470.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.42
/src/tests/test_ocb.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "tests.h"
8

9
#if defined(BOTAN_HAS_AEAD_OCB)
10
   #include <botan/block_cipher.h>
11
   #include <botan/internal/loadstor.h>
12
   #include <botan/internal/ocb.h>
13
   #include <botan/internal/poly_dbl.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
#if defined(BOTAN_HAS_AEAD_OCB)
21

22
// Toy cipher used for wide block tests
23

24
class OCB_Wide_Test_Block_Cipher final : public Botan::BlockCipher {
25
   public:
26
      explicit OCB_Wide_Test_Block_Cipher(size_t bs) : m_bs(bs) {}
24✔
27

28
      std::string name() const override { return "OCB_ToyCipher"; }
×
29

30
      size_t block_size() const override { return m_bs; }
11,967✔
31

32
      void clear() override { m_key.clear(); }
×
33

34
      std::unique_ptr<Botan::BlockCipher> new_object() const override {
×
35
         return std::make_unique<OCB_Wide_Test_Block_Cipher>(m_bs);
×
36
      }
37

38
      bool has_keying_material() const override { return !m_key.empty(); }
5,712✔
39

40
      void key_schedule(const uint8_t key[], size_t length) override { m_key.assign(key, key + length); }
24✔
41

42
      Botan::Key_Length_Specification key_spec() const override { return Botan::Key_Length_Specification(m_bs); }
48✔
43

44
      void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override {
10,257✔
45
         while(blocks) {
21,620✔
46
            Botan::copy_mem(out, in, m_bs);
11,363✔
47
            Botan::poly_double_n(out, m_bs);
11,363✔
48

49
            for(size_t i = 0; i != m_bs; ++i)
335,667✔
50
               out[i] ^= m_key[i];
324,304✔
51

52
            blocks--;
11,363✔
53
            in += block_size();
11,363✔
54
            out += block_size();
11,363✔
55
         }
56
      }
10,257✔
57

58
      void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override {
6✔
59
         while(blocks) {
14✔
60
            for(size_t i = 0; i != m_bs; ++i)
200✔
61
               out[i] = in[i] ^ m_key[i];
192✔
62

63
            uint8_t carry = in[m_bs - 1] & 0x01;
8✔
64

65
            if(carry) {
8✔
66
               if(m_bs == 16 || m_bs == 24) {
7✔
67
                  out[m_bs - 1] ^= 0x87;
7✔
68
               } else if(m_bs == 32) {
×
69
                  out[m_bs - 2] ^= 0x4;
×
70
                  out[m_bs - 1] ^= 0x25;
×
71
               } else if(m_bs == 64) {
×
72
                  out[m_bs - 2] ^= 0x1;
×
73
                  out[m_bs - 1] ^= 0x25;
×
74
               } else
75
                  throw Test_Error("Bad OCB test block size");
×
76
            }
77

78
            carry <<= 7;
8✔
79

80
            for(size_t i = 0; i != m_bs; ++i) {
200✔
81
               const uint8_t temp = out[i];
192✔
82
               out[i] = (temp >> 1) | carry;
192✔
83
               carry = (temp & 0x1);
192✔
84
               carry <<= 7;
192✔
85
            }
86

87
            blocks--;
8✔
88
            in += block_size();
8✔
89
            out += block_size();
8✔
90
         }
91
      }
6✔
92

93
   private:
94
      size_t m_bs;
95
      std::vector<uint8_t> m_key;
96
};
97

98
class OCB_Wide_KAT_Tests final : public Text_Based_Test {
×
99
   public:
100
      OCB_Wide_KAT_Tests() : Text_Based_Test("ocb/ocb_wide.vec", "Key,Nonce,AD,In,Out") {}
3✔
101

102
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
10✔
103
         Test::Result result("OCB wide block KAT");
10✔
104

105
         const std::vector<uint8_t> key = vars.get_req_bin("Key");
10✔
106
         const std::vector<uint8_t> nonce = vars.get_req_bin("Nonce");
10✔
107
         const std::vector<uint8_t> ad = vars.get_req_bin("AD");
10✔
108
         const std::vector<uint8_t> input = vars.get_req_bin("In");
10✔
109
         const std::vector<uint8_t> expected = vars.get_req_bin("Out");
10✔
110

111
         const size_t bs = key.size();
10✔
112
         Botan::secure_vector<uint8_t> buf(input.begin(), input.end());
10✔
113

114
         Botan::OCB_Encryption enc(std::make_unique<OCB_Wide_Test_Block_Cipher>(bs), std::min<size_t>(bs, 32));
20✔
115
         enc.set_key(key);
10✔
116
         enc.set_associated_data(ad);
10✔
117
         enc.start(nonce);
10✔
118
         enc.finish(buf);
10✔
119
         result.test_eq("Ciphertext matches", buf, expected);
10✔
120

121
         Botan::OCB_Decryption dec(std::make_unique<OCB_Wide_Test_Block_Cipher>(bs), std::min<size_t>(bs, 32));
20✔
122
         dec.set_key(key);
10✔
123
         dec.set_associated_data(ad);
10✔
124
         dec.start(nonce);
10✔
125
         dec.finish(buf);
10✔
126
         result.test_eq("Decryption correct", buf, input);
10✔
127

128
         return result;
10✔
129
      }
70✔
130
};
131

132
BOTAN_REGISTER_TEST("modes", "ocb_wide", OCB_Wide_KAT_Tests);
133

134
class OCB_Wide_Long_KAT_Tests final : public Text_Based_Test {
×
135
   public:
136
      OCB_Wide_Long_KAT_Tests() : Text_Based_Test("ocb/ocb_wide_long.vec", "Output") {}
2✔
137

138
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override {
5✔
139
         Test::Result result("OCB wide block long test");
5✔
140

141
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
5✔
142

143
         std::unique_ptr<Botan::BlockCipher> cipher;
5✔
144
         size_t bs = 0;
5✔
145

146
         if(algo == "SHACAL2") {
5✔
147
   #if defined(BOTAN_HAS_SHACAL2)
148
            cipher = Botan::BlockCipher::create_or_throw("SHACAL2");
1✔
149
            bs = 32;
1✔
150
   #else
151
            return {result};
152
   #endif
153
         } else {
154
            if(algo == "Toy128")
4✔
155
               bs = 16;
1✔
156
            else if(algo == "Toy192")
3✔
157
               bs = 24;
1✔
158
            else if(algo == "Toy256")
2✔
159
               bs = 32;
1✔
160
            else if(algo == "Toy512")
1✔
161
               bs = 64;
1✔
162
            else
163
               throw Test_Error("Unknown cipher for OCB wide block long test");
×
164
            cipher = std::make_unique<OCB_Wide_Test_Block_Cipher>(bs);
4✔
165
         }
166

167
         Botan::OCB_Encryption enc(std::move(cipher), std::min<size_t>(bs, 32));
6✔
168

169
         /*
170
         Y, string of length min(B, 256) bits
171

172
         Y is defined as follows.
173

174
         K = (0xA0 || 0xA1 || 0xA2 || ...)[1..B]
175
         C = <empty string>
176
         for i = 0 to 127 do
177
           S = (0x50 || 0x51 || 0x52 || ...)[1..8i]
178
           N = num2str(3i+1,16)
179
           C = C || OCB-ENCRYPT(K,N,S,S)
180
           N = num2str(3i+2,16)
181
           C = C || OCB-ENCRYPT(K,N,<empty string>,S)
182
           N = num2str(3i+3,16)
183
           C = C || OCB-ENCRYPT(K,N,S,<empty string>)
184
         end for
185
         N = num2str(385,16)
186
         Y = OCB-ENCRYPT(K,N,C,<empty string>)
187
         */
188

189
         std::vector<uint8_t> key(bs);
5✔
190
         for(size_t i = 0; i != bs; ++i)
173✔
191
            key[i] = static_cast<uint8_t>(0xA0 + i);
168✔
192

193
         enc.set_key(key);
5✔
194

195
         const std::vector<uint8_t> empty;
5✔
196
         std::vector<uint8_t> N(2);
5✔
197
         std::vector<uint8_t> C;
5✔
198

199
         for(size_t i = 0; i != 128; ++i) {
645✔
200
            std::vector<uint8_t> S(i);
640✔
201
            for(size_t j = 0; j != S.size(); ++j)
41,280✔
202
               S[j] = static_cast<uint8_t>(0x50 + j);
40,640✔
203

204
            Botan::store_be(static_cast<uint16_t>(3 * i + 1), &N[0]);
640✔
205

206
            ocb_encrypt(result, C, enc, N, S, S);
640✔
207
            Botan::store_be(static_cast<uint16_t>(3 * i + 2), &N[0]);
640✔
208
            ocb_encrypt(result, C, enc, N, S, empty);
640✔
209
            Botan::store_be(static_cast<uint16_t>(3 * i + 3), &N[0]);
640✔
210
            ocb_encrypt(result, C, enc, N, empty, S);
640✔
211
         }
640✔
212

213
         Botan::store_be(static_cast<uint16_t>(385), &N[0]);
5✔
214
         std::vector<uint8_t> final_result;
5✔
215
         ocb_encrypt(result, final_result, enc, N, empty, C);
5✔
216

217
         result.test_eq("correct value", final_result, expected);
5✔
218

219
         return result;
5✔
220
      }
25✔
221

222
   private:
223
      static void ocb_encrypt(Test::Result& /*result*/,
1,925✔
224
                              std::vector<uint8_t>& output_to,
225
                              Botan::OCB_Encryption& enc,
226
                              const std::vector<uint8_t>& nonce,
227
                              const std::vector<uint8_t>& pt,
228
                              const std::vector<uint8_t>& ad) {
229
         enc.set_associated_data(ad.data(), ad.size());
1,925✔
230
         enc.start(nonce.data(), nonce.size());
1,925✔
231
         Botan::secure_vector<uint8_t> buf(pt.begin(), pt.end());
1,925✔
232
         enc.finish(buf, 0);
1,925✔
233
         output_to.insert(output_to.end(), buf.begin(), buf.end());
1,925✔
234
      }
1,925✔
235
};
236

237
BOTAN_REGISTER_TEST("modes", "ocb_long_wide", OCB_Wide_Long_KAT_Tests);
238

239
   #if defined(BOTAN_HAS_AES)
240

241
class OCB_Long_KAT_Tests final : public Text_Based_Test {
×
242
   public:
243
      OCB_Long_KAT_Tests() : Text_Based_Test("ocb/ocb_long.vec", "Keylen,Taglen,Output") {}
3✔
244

245
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
9✔
246
         const size_t keylen = vars.get_req_sz("Keylen");
9✔
247
         const size_t taglen = vars.get_req_sz("Taglen");
9✔
248
         const std::vector<uint8_t> expected = vars.get_req_bin("Output");
9✔
249

250
         // Test from RFC 7253 Appendix A
251

252
         const std::string algo = "AES-" + std::to_string(keylen);
9✔
253

254
         Test::Result result("OCB long");
9✔
255

256
         auto aes = Botan::BlockCipher::create_or_throw(algo);
9✔
257

258
         Botan::OCB_Encryption enc(aes->new_object(), taglen / 8);
9✔
259
         Botan::OCB_Decryption dec(std::move(aes), taglen / 8);
9✔
260

261
         std::vector<uint8_t> key(keylen / 8);
9✔
262
         key[keylen / 8 - 1] = static_cast<uint8_t>(taglen);
9✔
263

264
         enc.set_key(key);
9✔
265
         dec.set_key(key);
9✔
266

267
         const std::vector<uint8_t> empty;
9✔
268
         std::vector<uint8_t> N(12);
9✔
269
         std::vector<uint8_t> C;
9✔
270

271
         for(size_t i = 0; i != 128; ++i) {
1,161✔
272
            const std::vector<uint8_t> S(i);
1,152✔
273

274
            Botan::store_be(static_cast<uint32_t>(3 * i + 1), &N[8]);
1,152✔
275

276
            ocb_encrypt(result, C, enc, dec, N, S, S);
1,152✔
277
            Botan::store_be(static_cast<uint32_t>(3 * i + 2), &N[8]);
1,152✔
278
            ocb_encrypt(result, C, enc, dec, N, S, empty);
1,152✔
279
            Botan::store_be(static_cast<uint32_t>(3 * i + 3), &N[8]);
1,152✔
280
            ocb_encrypt(result, C, enc, dec, N, empty, S);
1,152✔
281
         }
1,152✔
282

283
         Botan::store_be(static_cast<uint32_t>(385), &N[8]);
9✔
284
         std::vector<uint8_t> final_result;
9✔
285
         ocb_encrypt(result, final_result, enc, dec, N, empty, C);
9✔
286

287
         result.test_eq("correct value", final_result, expected);
9✔
288

289
         return result;
9✔
290
      }
45✔
291

292
   private:
293
      static void ocb_encrypt(Test::Result& result,
3,465✔
294
                              std::vector<uint8_t>& output_to,
295
                              Botan::AEAD_Mode& enc,
296
                              Botan::AEAD_Mode& dec,
297
                              const std::vector<uint8_t>& nonce,
298
                              const std::vector<uint8_t>& pt,
299
                              const std::vector<uint8_t>& ad) {
300
         enc.set_associated_data(ad.data(), ad.size());
3,465✔
301

302
         enc.start(nonce.data(), nonce.size());
3,465✔
303

304
         Botan::secure_vector<uint8_t> buf(pt.begin(), pt.end());
3,465✔
305
         enc.finish(buf, 0);
3,465✔
306
         output_to.insert(output_to.end(), buf.begin(), buf.end());
3,465✔
307

308
         try {
3,465✔
309
            dec.set_associated_data(ad.data(), ad.size());
3,465✔
310

311
            dec.start(nonce.data(), nonce.size());
3,465✔
312

313
            dec.finish(buf, 0);
3,465✔
314

315
            result.test_eq("OCB round tripped", buf, pt);
6,930✔
316
         } catch(std::exception& e) { result.test_failure("OCB round trip error", e.what()); }
×
317
      }
3,465✔
318
};
319

320
BOTAN_REGISTER_TEST("modes", "ocb_long", OCB_Long_KAT_Tests);
321

322
   #endif
323

324
#endif
325

326
}  // namespace
327

328
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc