~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/tests/test_block.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <botan/block_cipher.h>
 
9
 
 
10
namespace Botan_Tests {
 
11
 
 
12
class Block_Cipher_Tests final : public Text_Based_Test
 
13
   {
 
14
   public:
 
15
      Block_Cipher_Tests() : Text_Based_Test("block", "Key,In,Out", "Iterations") {}
 
16
 
 
17
      std::vector<std::string> possible_providers(const std::string& algo) override
 
18
         {
 
19
         return provider_filter(Botan::BlockCipher::providers(algo));
 
20
         }
 
21
 
 
22
      Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
 
23
         {
 
24
         const std::vector<uint8_t> key      = get_req_bin(vars, "Key");
 
25
         const std::vector<uint8_t> input    = get_req_bin(vars, "In");
 
26
         const std::vector<uint8_t> expected = get_req_bin(vars, "Out");
 
27
         const size_t iterations             = get_opt_sz(vars, "Iterations", 1);
 
28
 
 
29
         Test::Result result(algo);
 
30
 
 
31
         const std::vector<std::string> providers = possible_providers(algo);
 
32
 
 
33
         if(providers.empty())
 
34
            {
 
35
            result.note_missing("block cipher " + algo);
 
36
            return result;
 
37
            }
 
38
 
 
39
         for(auto const& provider_ask : providers)
 
40
            {
 
41
            std::unique_ptr<Botan::BlockCipher> cipher(Botan::BlockCipher::create(algo, provider_ask));
 
42
 
 
43
            if(!cipher)
 
44
               {
 
45
               result.test_failure("Cipher " + algo + " supported by " + provider_ask + " but not found");
 
46
               continue;
 
47
               }
 
48
 
 
49
            const std::string provider(cipher->provider());
 
50
            result.test_is_nonempty("provider", provider);
 
51
            result.test_eq(provider, cipher->name(), algo);
 
52
            result.test_gte(provider, cipher->parallelism(), 1);
 
53
            result.test_gte(provider, cipher->block_size(), 8);
 
54
            result.test_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism());
 
55
 
 
56
            // Test that trying to encrypt or decrypt with now key set throws Botan::Invalid_State
 
57
            try
 
58
               {
 
59
               std::vector<uint8_t> block(cipher->block_size());
 
60
               cipher->encrypt(block);
 
61
               result.test_failure("Was able to encrypt without a key being set");
 
62
               }
 
63
            catch(Botan::Invalid_State&)
 
64
               {
 
65
               result.test_success("Trying to encrypt with no key set fails");
 
66
               }
 
67
 
 
68
            try
 
69
               {
 
70
               std::vector<uint8_t> block(cipher->block_size());
 
71
               cipher->decrypt(block);
 
72
               result.test_failure("Was able to decrypt without a key being set");
 
73
               }
 
74
            catch(Botan::Invalid_State&)
 
75
               {
 
76
               result.test_success("Trying to encrypt with no key set fails");
 
77
               }
 
78
 
 
79
            // Test to make sure clear() resets what we need it to
 
80
            cipher->set_key(Test::rng().random_vec(cipher->key_spec().maximum_keylength()));
 
81
            Botan::secure_vector<uint8_t> garbage = Test::rng().random_vec(cipher->block_size());
 
82
            cipher->encrypt(garbage);
 
83
            cipher->clear();
 
84
 
 
85
            cipher->set_key(key);
 
86
 
 
87
            // Test that clone works and does not affect parent object
 
88
            std::unique_ptr<Botan::BlockCipher> clone(cipher->clone());
 
89
            result.confirm("Clone has different pointer", cipher.get() != clone.get());
 
90
            result.test_eq("Clone has same name", cipher->name(), clone->name());
 
91
            clone->set_key(Test::rng().random_vec(cipher->maximum_keylength()));
 
92
 
 
93
            // have called set_key on clone: process input values
 
94
            std::vector<uint8_t> buf = input;
 
95
 
 
96
            for(size_t i = 0; i != iterations; ++i)
 
97
               {
 
98
               cipher->encrypt(buf);
 
99
               }
 
100
 
 
101
            result.test_eq(provider, "encrypt", buf, expected);
 
102
 
 
103
            // always decrypt expected ciphertext vs what we produced above
 
104
            buf = expected;
 
105
 
 
106
            for(size_t i = 0; i != iterations; ++i)
 
107
               {
 
108
               cipher->decrypt(buf);
 
109
               }
 
110
 
 
111
            cipher->clear();
 
112
 
 
113
            result.test_eq(provider, "decrypt", buf, input);
 
114
 
 
115
            try
 
116
               {
 
117
               std::vector<uint8_t> block(cipher->block_size());
 
118
               cipher->encrypt(block);
 
119
               result.test_failure("Was able to encrypt without a key being set");
 
120
               }
 
121
            catch(Botan::Invalid_State&)
 
122
               {
 
123
               result.test_success("Trying to encrypt with no key set (after clear) fails");
 
124
               }
 
125
 
 
126
            try
 
127
               {
 
128
               std::vector<uint8_t> block(cipher->block_size());
 
129
               cipher->decrypt(block);
 
130
               result.test_failure("Was able to decrypt without a key being set");
 
131
               }
 
132
            catch(Botan::Invalid_State&)
 
133
               {
 
134
               result.test_success("Trying to decrypt with no key set (after clear) fails");
 
135
               }
 
136
 
 
137
            }
 
138
 
 
139
         return result;
 
140
         }
 
141
 
 
142
   };
 
143
 
 
144
BOTAN_REGISTER_TEST("block", Block_Cipher_Tests);
 
145
 
 
146
}