aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_package_transform.cpp
blob: 9fb63c7d8e420d61c6acd8bd74233729ddd5c327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* (C) 2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#define BOTAN_NO_DEPRECATED_WARNINGS

#include "tests.h"

#if defined(BOTAN_HAS_PACKAGE_TRANSFORM)
   #include <botan/package.h>
#endif

namespace Botan_Tests {

#if defined(BOTAN_HAS_PACKAGE_TRANSFORM)

class Package_Transform_Tests final : public Test
   {
   public:
      std::vector<Test::Result> run() override
         {
         Test::Result result("Package transform");

         std::unique_ptr<Botan::BlockCipher> cipher(Botan::BlockCipher::create("AES-128"));

         for(size_t input_len = 2; input_len != 256; ++input_len)
            {
            std::vector<uint8_t> input = unlock(Test::rng().random_vec(input_len));
            std::vector<uint8_t> output(input.size() + cipher->block_size());

            // aont_package owns/deletes the passed cipher object, kind of a bogus API
            Botan::aont_package(Test::rng(),
                                cipher->clone(),
                                input.data(), input.size(),
                                output.data());

            std::vector<uint8_t> decoded(output.size() - cipher->block_size());
            Botan::aont_unpackage(cipher->clone(),
                                  output.data(), output.size(),
                                  decoded.data());
            result.test_eq("Package transform is reversible", decoded, input);

            output[0] ^= 1;
            Botan::aont_unpackage(cipher->clone(),
                                  output.data(), output.size(),
                                  decoded.data());
            result.test_ne("Bitflip breaks package transform", decoded, input);

            output[0] ^= 1;
            Botan::aont_unpackage(cipher->clone(),
                                  output.data(), output.size(),
                                  decoded.data());
            result.test_eq("Package transform is still reversible", decoded, input);

            // More tests including KATs would be useful for these functions
            }

         return std::vector<Test::Result> {result};
         }
   };

BOTAN_REGISTER_TEST("package_transform", Package_Transform_Tests);

#endif

}