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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 hits per line

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

75.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
            }
54

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

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

60
         return result;
36✔
61
      }
43✔
62
};
63

64
BOTAN_REGISTER_TEST("pubkey", "eme_pkcs1v15", EME_PKCS1v15_Decoding_Tests);
65

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

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

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

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

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

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

136
         return {name_tests};
3✔
137
      }
1✔
138
};
139

140
BOTAN_REGISTER_TEST("pubkey", "pk_pad_emsa_unit", EMSA_unit_tests);
141

142
#endif
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