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

randombit / botan / 11365015734

16 Oct 2024 11:49AM UTC coverage: 91.117% (+0.007%) from 91.11%
11365015734

push

github

web-flow
Merge pull request #4380 from Rohde-Schwarz/fix/pk_pad_test_dependency

test_pk_pad.cpp #ifdef fix

90994 of 99865 relevant lines covered (91.12%)

9381368.17 hits per line

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

76.92
/src/tests/test_pk_pad.cpp
1
/*
2
* (C) 2016 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_PK_PADDING)
10
   #include <botan/internal/eme.h>
11
   #include <botan/internal/emsa.h>
12
   #include <botan/internal/fmt.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
#if defined(BOTAN_HAS_EME_PKCS1)
18
class EME_PKCS1v15_Decoding_Tests final : public Text_Based_Test {
×
19
   public:
20
      EME_PKCS1v15_Decoding_Tests() : Text_Based_Test("pk_pad_eme/pkcs1.vec", "RawCiphertext", "Plaintext") {}
2✔
21

22
      Test::Result run_one_test(const std::string& hdr, const VarMap& vars) override {
18✔
23
         const bool is_valid = (hdr == "valid");
18✔
24

25
         Test::Result result("PKCSv15 Decoding");
18✔
26

27
         auto pkcs = Botan::EME::create("PKCS1v15");
18✔
28
         if(!pkcs) {
18✔
29
            return result;
30
         }
31

32
         const std::vector<uint8_t> ciphertext = vars.get_req_bin("RawCiphertext");
18✔
33
         const std::vector<uint8_t> plaintext = vars.get_opt_bin("Plaintext");
18✔
34

35
         if(is_valid == false) {
18✔
36
            result.test_eq("Plaintext value should be empty for invalid EME inputs", plaintext.size(), 0);
22✔
37
         }
38

39
         std::vector<uint8_t> decoded(ciphertext.size());
18✔
40
         auto len = pkcs->unpad(decoded, ciphertext);
18✔
41

42
         result.test_eq("EME decoding valid/invalid matches", len.has_value().as_bool(), is_valid);
18✔
43

44
         if(len.has_value().as_bool()) {
18✔
45
            decoded.resize(len.value_or(0));
7✔
46
            result.test_eq("EME decoded plaintext correct", decoded, plaintext);
14✔
47
         } else {
48
            bool all_zeros = true;
49
            for(size_t i = 0; i != decoded.size(); ++i) {
137✔
50
               if(decoded[i] != 0) {
126✔
51
                  all_zeros = false;
×
52
               }
53
            }
54

55
            result.confirm("On invalid padding output is all zero", all_zeros);
22✔
56
         }
57

58
         // TODO: also test that encoding is accepted
59

60
         return result;
18✔
61
      }
61✔
62
};
63

64
BOTAN_REGISTER_TEST("pubkey", "eme_pkcs1v15", EME_PKCS1v15_Decoding_Tests);
65
#endif
66

67
#if defined(BOTAN_HAS_PK_PADDING)
68
class EMSA_unit_tests final : public Test {
×
69
   public:
70
      std::vector<Test::Result> run() override {
1✔
71
         Test::Result name_tests("EMSA_name_tests");
1✔
72

73
         std::vector<std::string> pads_need_hash = {
1✔
74
   #if BOTAN_HAS_EMSA_X931
75
            "EMSA2",
76
   #endif
77
   #if BOTAN_HAS_EMSA_PKCS1
78
            "EMSA3",
79
   #endif
80
   #if BOTAN_HAS_EMSA_PSSR
81
            "EMSA4",
82
            "PSSR_Raw",
83
   #endif
84
   #if BOTAN_HAS_ISO_9796
85
            "ISO_9796_DS2",
86
            "ISO_9796_DS3",
87
   #endif
88
         };
1✔
89

90
         std::vector<std::string> pads_no_hash = {
1✔
91
   #if BOTAN_HAS_EMSA_RAW
92
            "Raw",
93
   #endif
94
   #if BOTAN_HAS_EMSA_PKCS1
95
            "EMSA3(Raw)",
96
            "EMSA3(Raw,SHA-512)",
97
   #endif
98
         };
1✔
99

100
         for(const auto& pad : pads_need_hash) {
7✔
101
            try {
6✔
102
               const std::string hash_to_use = "SHA-256";
6✔
103
               auto emsa_1 = Botan::EMSA::create(Botan::fmt("{}({})", pad, hash_to_use));
6✔
104
               auto emsa_2 = Botan::EMSA::create(emsa_1->name());
6✔
105
               name_tests.test_eq("EMSA_name_test for " + pad, emsa_1->name(), emsa_2->name());
12✔
106
            } catch(Botan::Lookup_Error&) {
12✔
107
               name_tests.test_note("Skipping test due to missing hash");
×
108
            } catch(const std::exception& e) {
×
109
               name_tests.test_failure("EMSA_name_test for " + pad + ": " + e.what());
×
110
            }
×
111
         }
112

113
         for(const auto& pad : pads_need_hash) {
7✔
114
            std::string algo_name = pad + "(YYZ)";
6✔
115
            try {
6✔
116
               auto emsa = Botan::EMSA::create_or_throw(algo_name);
6✔
117
               name_tests.test_failure("EMSA_name_test for " + pad + ": " + "Could create EMSA with fantasy hash YYZ");
×
118
            } catch(Botan::Lookup_Error&) {
6✔
119
               name_tests.test_note("Skipping test due to missing hash");
6✔
120
            } catch(const std::exception& e) {
6✔
121
               name_tests.test_eq(
×
122
                  "EMSA_name_test for " + pad, e.what(), "Could not find any algorithm named \"" + algo_name + "\"");
×
123
            }
×
124
         }
6✔
125

126
         for(const auto& pad : pads_no_hash) {
4✔
127
            try {
3✔
128
               auto emsa_1 = Botan::EMSA::create(pad);
3✔
129
               auto emsa_2 = Botan::EMSA::create(emsa_1->name());
3✔
130
               name_tests.test_eq("EMSA_name_test for " + pad, emsa_1->name(), emsa_2->name());
6✔
131
            } catch(Botan::Lookup_Error&) {
6✔
132
               name_tests.test_note("Skipping test due to missing hash");
×
133
            } catch(const std::exception& e) {
×
134
               name_tests.test_failure("EMSA_name_test for " + pad + ": " + e.what());
×
135
            }
×
136
         }
137

138
         return {name_tests};
3✔
139
      }
2✔
140
};
141

142
BOTAN_REGISTER_TEST("pubkey", "pk_pad_emsa_unit", EMSA_unit_tests);
143

144
#endif
145

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