• 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

80.0
/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/emsa.h>
11
#endif
12

13
#if defined(BOTAN_HAS_EME_PKCS1)
14
   #include <botan/internal/eme_pkcs.h>
15
#endif
16

17
namespace Botan_Tests {
18

19
#if defined(BOTAN_HAS_EME_PKCS1)
20

21
class EME_PKCS1v15_Decoding_Tests final : public Text_Based_Test {
×
22
   public:
23
      EME_PKCS1v15_Decoding_Tests() : Text_Based_Test("pk_pad_eme/pkcs1.vec", "RawCiphertext", "Plaintext") {}
2✔
24

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

28
         Test::Result result("PKCSv15 Decoding");
18✔
29

30
         Botan::EME_PKCS1v15 pkcs;
18✔
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
         uint8_t valid_mask = 0;
18✔
40
         Botan::secure_vector<uint8_t> decoded = pkcs.unpad(valid_mask, ciphertext.data(), ciphertext.size());
18✔
41

42
         result.confirm("EME valid_mask has expected value", valid_mask == 0x00 || valid_mask == 0xFF);
36✔
43
         result.test_eq("EME decoding valid/invalid matches", valid_mask == 0xFF, is_valid);
18✔
44

45
         if(valid_mask == 0xFF) {
18✔
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)
11✔
50
               if(decoded[i] != 0)
×
51
                  all_zeros = false;
×
52

53
            result.confirm("On invalid padding output is all zero", all_zeros);
33✔
54
         }
55

56
         // TODO: also test that encoding is accepted
57

58
         return result;
36✔
59
      }
43✔
60
};
61

62
BOTAN_REGISTER_TEST("pubkey", "eme_pkcs1v15", EME_PKCS1v15_Decoding_Tests);
63

64
class EMSA_unit_tests final : public Test {
×
65
   public:
66
      std::vector<Test::Result> run() override {
1✔
67
         Test::Result name_tests("EMSA_name_tests");
1✔
68

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

86
         std::vector<std::string> pads_no_hash = {
1✔
87
   #if BOTAN_HAS_EMSA_RAW
88
            "Raw",
89
   #endif
90
   #if BOTAN_HAS_EMSA_PKCS1
91
            "EMSA3(Raw)",
92
            "EMSA3(Raw,SHA-512)",
93
   #endif
94
         };
4✔
95

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

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

120
         for(const auto& pad : pads_no_hash) {
4✔
121
            try {
3✔
122
               auto emsa_1 = Botan::EMSA::create(pad);
3✔
123
               auto emsa_2 = Botan::EMSA::create(emsa_1->name());
3✔
124
               name_tests.test_eq("EMSA_name_test for " + pad, emsa_1->name(), emsa_2->name());
8✔
125
            } catch(Botan::Lookup_Error&) {
6✔
126
               name_tests.test_note("Skipping test due to missing hash");
×
127
            } catch(const std::exception& e) { name_tests.test_failure("EMSA_name_test for " + pad + ": " + e.what()); }
×
128
         }
129

130
         return {name_tests};
3✔
131
      }
1✔
132
};
133

134
BOTAN_REGISTER_TEST("pubkey", "pk_pad_emsa_unit", EMSA_unit_tests);
135

136
#endif
137

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