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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

121
            bc->set_key(key);
10✔
122

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

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

135
         return result;
10✔
136
      }
×
137
};
138

139
BOTAN_REGISTER_TEST("keywrap", "nist_key_wrap_invalid", NIST_Keywrap_Invalid_Tests);
140
#endif
141

142
}  // namespace
143

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