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

randombit / botan / 21794448852

08 Feb 2026 12:09AM UTC coverage: 90.065% (-0.008%) from 90.073%
21794448852

push

github

web-flow
Merge pull request #5295 from randombit/jack/header-patrol-3

Reduce header dependencies in tests and cli

102230 of 113507 relevant lines covered (90.06%)

11492365.41 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/exceptn.h>
16
   #include <botan/nist_keywrap.h>
17
#endif
18

19
namespace Botan_Tests {
20

21
namespace {
22

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

84
            result.test_eq("key wrap", wrapped, expected);
272✔
85

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

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

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

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

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

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

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

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

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

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

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

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

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

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

151
}  // namespace
152

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

© 2026 Coveralls, Inc