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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 hits per line

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

79.75
/src/tests/test_keywrap.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_RFC3394_KEYWRAP)
10
   #include <botan/rfc3394.h>
11
#endif
12

13
#if defined(BOTAN_HAS_NIST_KEYWRAP)
14
   #include <botan/block_cipher.h>
15
   #include <botan/nist_keywrap.h>
16
#endif
17

18
namespace Botan_Tests {
19

20
namespace {
21

22
#if defined(BOTAN_HAS_RFC3394_KEYWRAP)
23
class RFC3394_Keywrap_Tests final : public Text_Based_Test {
×
24
   public:
25
      RFC3394_Keywrap_Tests() : Text_Based_Test("keywrap/rfc3394.vec", "Key,KEK,Output") {}
2✔
26

27
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) override {
6✔
28
         Test::Result result("RFC3394 keywrap");
6✔
29

30
         try {
6✔
31
            const std::vector<uint8_t> expected = vars.get_req_bin("Output");
6✔
32
            const std::vector<uint8_t> key = vars.get_req_bin("Key");
6✔
33
            const std::vector<uint8_t> kek = vars.get_req_bin("KEK");
6✔
34

35
            const Botan::SymmetricKey kek_sym(kek);
6✔
36
            const Botan::secure_vector<uint8_t> key_l(key.begin(), key.end());
6✔
37
            const Botan::secure_vector<uint8_t> exp_l(expected.begin(), expected.end());
6✔
38

39
            result.test_eq("encryption", Botan::rfc3394_keywrap(key_l, kek_sym), expected);
12✔
40
            result.test_eq("decryption", Botan::rfc3394_keyunwrap(exp_l, kek_sym), key);
18✔
41
         } catch(std::exception& e) {
36✔
42
            result.test_failure("", e.what());
×
43
         }
×
44

45
         return result;
6✔
46
      }
×
47
};
48

49
BOTAN_REGISTER_TEST("keywrap", "rfc3394", RFC3394_Keywrap_Tests);
50
#endif
51

52
#if defined(BOTAN_HAS_NIST_KEYWRAP) && defined(BOTAN_HAS_AES)
53

54
class NIST_Keywrap_Tests final : public Text_Based_Test {
×
55
   public:
56
      NIST_Keywrap_Tests() : Text_Based_Test("keywrap/nist_key_wrap.vec", "Input,Key,Output") {}
3✔
57

58
      Test::Result run_one_test(const std::string& typ, const VarMap& vars) override {
136✔
59
         Test::Result result("NIST keywrap");
136✔
60

61
         try {
136✔
62
            if(typ != "KW" && typ != "KWP") {
136✔
63
               throw Test_Error("Unknown type in NIST key wrap tests");
×
64
            }
65

66
            const std::vector<uint8_t> expected = vars.get_req_bin("Output");
136✔
67
            const std::vector<uint8_t> input = vars.get_req_bin("Input");
136✔
68
            const std::vector<uint8_t> key = vars.get_req_bin("Key");
136✔
69

70
            std::unique_ptr<Botan::BlockCipher> bc =
136✔
71
               Botan::BlockCipher::create_or_throw("AES-" + std::to_string(key.size() * 8));
272✔
72

73
            bc->set_key(key);
136✔
74

75
            std::vector<uint8_t> wrapped;
136✔
76

77
            if(typ == "KW") {
136✔
78
               wrapped = nist_key_wrap(input.data(), input.size(), *bc);
14✔
79
            } else if(typ == "KWP") {
129✔
80
               wrapped = nist_key_wrap_padded(input.data(), input.size(), *bc);
258✔
81
            }
82

83
            result.test_eq("key wrap", wrapped, expected);
136✔
84

85
            try {
136✔
86
               Botan::secure_vector<uint8_t> unwrapped;
136✔
87
               if(typ == "KW") {
136✔
88
                  unwrapped = nist_key_unwrap(expected.data(), expected.size(), *bc);
14✔
89
               } else if(typ == "KWP") {
129✔
90
                  unwrapped = nist_key_unwrap_padded(expected.data(), expected.size(), *bc);
258✔
91
               }
92

93
               result.test_eq("key unwrap", unwrapped, input);
272✔
94
            } catch(Botan::Integrity_Failure& e) {
136✔
95
               result.test_failure("NIST key unwrap failed with integrity failure", e.what());
×
96
            }
×
97
         } catch(std::exception& e) {
680✔
98
            result.test_failure("", e.what());
×
99
         }
×
100

101
         return result;
136✔
102
      }
×
103
};
104

105
BOTAN_REGISTER_TEST("keywrap", "nist_key_wrap", NIST_Keywrap_Tests);
106

107
class NIST_Keywrap_Invalid_Tests final : public Text_Based_Test {
×
108
   public:
109
      NIST_Keywrap_Invalid_Tests() : Text_Based_Test("keywrap/nist_key_wrap_invalid.vec", "Key,Input") {}
2✔
110

111
      Test::Result run_one_test(const std::string& typ, const VarMap& vars) override {
10✔
112
         Test::Result result("NIST keywrap (invalid inputs)");
10✔
113

114
         try {
10✔
115
            if(typ != "KW" && typ != "KWP") {
10✔
116
               throw Test_Error("Unknown type in NIST key wrap tests");
×
117
            }
118

119
            const std::vector<uint8_t> input = vars.get_req_bin("Input");
10✔
120
            const std::vector<uint8_t> key = vars.get_req_bin("Key");
10✔
121

122
            std::unique_ptr<Botan::BlockCipher> bc =
10✔
123
               Botan::BlockCipher::create_or_throw("AES-" + std::to_string(key.size() * 8));
20✔
124

125
            bc->set_key(key);
10✔
126

127
            try {
10✔
128
               Botan::secure_vector<uint8_t> unwrapped;
10✔
129
               if(typ == "KW") {
10✔
130
                  unwrapped = nist_key_unwrap(input.data(), input.size(), *bc);
7✔
131
               } else if(typ == "KWP") {
3✔
132
                  unwrapped = nist_key_unwrap_padded(input.data(), input.size(), *bc);
3✔
133
               }
134

135
               result.test_failure("Was able to unwrap invalid keywrap input");
10✔
136
            } catch(Botan::Integrity_Failure&) {
10✔
137
               result.test_success("Rejected invalid input");
10✔
138
            }
10✔
139
         } catch(std::exception& e) {
30✔
140
            result.test_failure("", e.what());
×
141
         }
×
142

143
         return result;
10✔
144
      }
×
145
};
146

147
BOTAN_REGISTER_TEST("keywrap", "nist_key_wrap_invalid", NIST_Keywrap_Invalid_Tests);
148
#endif
149

150
}  // namespace
151

152
}  // 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