• 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

85.92
/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) { result.test_failure("", e.what()); }
36✔
42

43
         return result;
6✔
44
      }
×
45
};
46

47
BOTAN_REGISTER_TEST("keywrap", "rfc3394", RFC3394_Keywrap_Tests);
48
#endif
49

50
#if defined(BOTAN_HAS_NIST_KEYWRAP) && defined(BOTAN_HAS_AES)
51

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

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

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

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

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

70
            bc->set_key(key);
136✔
71

72
            std::vector<uint8_t> wrapped;
136✔
73

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

80
            result.test_eq("key wrap", wrapped, expected);
136✔
81

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

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

96
         return result;
136✔
97
      }
×
98
};
99

100
BOTAN_REGISTER_TEST("keywrap", "nist_key_wrap", NIST_Keywrap_Tests);
101

102
class NIST_Keywrap_Invalid_Tests final : public Text_Based_Test {
×
103
   public:
104
      NIST_Keywrap_Invalid_Tests() : Text_Based_Test("keywrap/nist_key_wrap_invalid.vec", "Key,Input") {}
2✔
105

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

109
         try {
10✔
110
            if(typ != "KW" && typ != "KWP")
10✔
111
               throw Test_Error("Unknown type in NIST key wrap tests");
×
112

113
            const std::vector<uint8_t> input = vars.get_req_bin("Input");
10✔
114
            const std::vector<uint8_t> key = vars.get_req_bin("Key");
10✔
115

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

119
            bc->set_key(key);
10✔
120

121
            try {
10✔
122
               Botan::secure_vector<uint8_t> unwrapped;
10✔
123
               if(typ == "KW") {
10✔
124
                  unwrapped = nist_key_unwrap(input.data(), input.size(), *bc);
7✔
125
               } else if(typ == "KWP") {
3✔
126
                  unwrapped = nist_key_unwrap_padded(input.data(), input.size(), *bc);
3✔
127
               }
128

129
               result.test_failure("Was able to unwrap invalid keywrap input");
10✔
130
            } catch(Botan::Integrity_Failure&) { result.test_success("Rejected invalid input"); }
20✔
131
         } catch(std::exception& e) { result.test_failure("", e.what()); }
30✔
132

133
         return result;
10✔
134
      }
×
135
};
136

137
BOTAN_REGISTER_TEST("keywrap", "nist_key_wrap_invalid", NIST_Keywrap_Invalid_Tests);
138
#endif
139

140
}  // namespace
141

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