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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

84.3
/src/tests/test_rsa.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 "test_rng.h"
8
#include "tests.h"
9

10
#if defined(BOTAN_HAS_RSA)
11
   #include "test_pubkey.h"
12
   #include <botan/rsa.h>
13
#endif
14

15
namespace Botan_Tests {
16

17
namespace {
18

19
#if defined(BOTAN_HAS_RSA)
20

21
std::unique_ptr<Botan::Private_Key> load_rsa_private_key(const VarMap& vars) {
425✔
22
   const BigInt p = vars.get_req_bn("P");
425✔
23
   const BigInt q = vars.get_req_bn("Q");
425✔
24
   const BigInt e = vars.get_req_bn("E");
425✔
25

26
   return std::make_unique<Botan::RSA_PrivateKey>(p, q, e);
425✔
27
}
1,275✔
28

29
std::unique_ptr<Botan::Public_Key> load_rsa_public_key(const VarMap& vars) {
503✔
30
   const BigInt n = vars.get_req_bn("N");
503✔
31
   const BigInt e = vars.get_req_bn("E");
503✔
32

33
   return std::make_unique<Botan::RSA_PublicKey>(n, e);
1,006✔
34
}
1,006✔
35

36
class RSA_ES_KAT_Tests final : public PK_Encryption_Decryption_Test {
×
37
   public:
38
      RSA_ES_KAT_Tests() : PK_Encryption_Decryption_Test("RSA", "pubkey/rsaes.vec", "E,P,Q,Msg,Ciphertext", "Nonce") {}
4✔
39

40
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
123✔
41
         return load_rsa_private_key(vars);
123✔
42
      }
43
};
44

45
class RSA_Decryption_KAT_Tests final : public PK_Decryption_Test {
×
46
   public:
47
      RSA_Decryption_KAT_Tests() : PK_Decryption_Test("RSA", "pubkey/rsa_decrypt.vec", "E,P,Q,Ciphertext,Msg") {}
4✔
48

49
      bool clear_between_callbacks() const override { return false; }
25✔
50

51
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
25✔
52
         return load_rsa_private_key(vars);
25✔
53
      }
54
};
55

56
class RSA_KEM_Tests final : public PK_KEM_Test {
×
57
   public:
58
      RSA_KEM_Tests() : PK_KEM_Test("RSA", "pubkey/rsa_kem.vec", "E,P,Q,R,C0,KDF,K") {}
4✔
59

60
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
10✔
61
         return load_rsa_private_key(vars);
10✔
62
      }
63
};
64

65
class RSA_Signature_KAT_Tests final : public PK_Signature_Generation_Test {
×
66
   public:
67
      RSA_Signature_KAT_Tests() :
1✔
68
            PK_Signature_Generation_Test("RSA", "pubkey/rsa_sig.vec", "E,P,Q,Msg,Signature", "Nonce") {}
4✔
69

70
      std::string default_padding(const VarMap& /*unused*/) const override { return "Raw"; }
×
71

72
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
107✔
73
         return load_rsa_private_key(vars);
107✔
74
      }
75
};
76

77
class RSA_PSS_KAT_Tests final : public PK_Signature_Generation_Test {
×
78
   public:
79
      RSA_PSS_KAT_Tests() :
1✔
80
            PK_Signature_Generation_Test("RSA", "pubkey/rsa_pss.vec", "P,Q,E,Hash,Nonce,Msg,Signature", "") {}
4✔
81

82
      std::string default_padding(const VarMap& vars) const override {
80✔
83
         const std::string hash_name = vars.get_req_str("Hash");
80✔
84
         const size_t salt_size = vars.get_req_bin("Nonce").size();
160✔
85
         return "PSSR(" + hash_name + ",MGF1," + std::to_string(salt_size) + ")";
160✔
86
      }
80✔
87

88
      bool clear_between_callbacks() const override { return false; }
80✔
89

90
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
80✔
91
         return load_rsa_private_key(vars);
80✔
92
      }
93
};
94

95
class RSA_PSS_Raw_KAT_Tests final : public PK_Signature_Generation_Test {
×
96
   public:
97
      RSA_PSS_Raw_KAT_Tests() :
1✔
98
            PK_Signature_Generation_Test("RSA", "pubkey/rsa_pss_raw.vec", "P,Q,E,Hash,Nonce,Msg,Signature", "") {}
4✔
99

100
      std::string default_padding(const VarMap& vars) const override {
80✔
101
         const std::string hash_name = vars.get_req_str("Hash");
80✔
102
         const size_t salt_size = vars.get_req_bin("Nonce").size();
160✔
103
         return "PSSR_Raw(" + hash_name + ",MGF1," + std::to_string(salt_size) + ")";
160✔
104
      }
80✔
105

106
      bool clear_between_callbacks() const override { return false; }
80✔
107

108
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
80✔
109
         return load_rsa_private_key(vars);
80✔
110
      }
111
};
112

113
class RSA_Signature_Verify_Tests final : public PK_Signature_Verification_Test {
×
114
   public:
115
      RSA_Signature_Verify_Tests() :
1✔
116
            PK_Signature_Verification_Test("RSA", "pubkey/rsa_verify.vec", "E,N,Msg,Signature") {}
4✔
117

118
      std::string default_padding(const VarMap& /*unused*/) const override { return "Raw"; }
×
119

120
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
137✔
121
         return load_rsa_public_key(vars);
137✔
122
      }
123
};
124

125
class RSA_Signature_Verify_Invalid_Tests final : public PK_Signature_NonVerification_Test {
×
126
   public:
127
      RSA_Signature_Verify_Invalid_Tests() :
1✔
128
            PK_Signature_NonVerification_Test("RSA", "pubkey/rsa_invalid.vec", "E,N,Msg,InvalidSignature") {}
4✔
129

130
      std::string default_padding(const VarMap& /*unused*/) const override { return "Raw"; }
×
131

132
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
366✔
133
         return load_rsa_public_key(vars);
366✔
134
      }
135
};
136

137
class RSA_Keygen_Tests final : public PK_Key_Generation_Test {
×
138
   public:
139
      std::vector<std::string> keygen_params() const override { return {"1024", "1280"}; }
3✔
140

141
      std::string algo_name() const override { return "RSA"; }
6✔
142
};
143

144
class RSA_Keygen_Stability_Tests final : public PK_Key_Generation_Stability_Test {
×
145
   public:
146
      RSA_Keygen_Stability_Tests() : PK_Key_Generation_Stability_Test("RSA", "pubkey/rsa_keygen.vec") {}
3✔
147
};
148

149
class RSA_Keygen_Bad_RNG_Test final : public Test {
×
150
   public:
151
      std::vector<Test::Result> run() override {
1✔
152
         Test::Result result("RSA keygen with bad RNG");
1✔
153

154
         /*
155
         We don't need to count requests here; actually this test
156
         is relying on the fact that the Request_Counting_RNG outputs
157
         repeating 808080...
158
         */
159
         Request_Counting_RNG rng;
1✔
160

161
         try {
1✔
162
            Botan::RSA_PrivateKey rsa(rng, 1024);
1✔
163
            result.test_failure("Generated a key with a bad RNG");
×
164
         } catch(Botan::Internal_Error& e) {
1✔
165
            result.test_success("Key generation with bad RNG failed");
1✔
166
            result.test_eq("Expected message", e.what(), "Internal error: RNG failure during RSA key generation");
1✔
167
         }
1✔
168

169
         return {result};
2✔
170
      }
1✔
171
};
172

173
class RSA_Blinding_Tests final : public Test {
×
174
   public:
175
      std::vector<Test::Result> run() override {
1✔
176
         Test::Result result("RSA blinding");
1✔
177

178
         /* This test makes only sense with the base provider, else skip it. */
179
         if(provider_filter({"base"}).empty()) {
2✔
180
            result.note_missing("base provider");
×
181
            return std::vector<Test::Result>{result};
×
182
         }
183

184
   #if defined(BOTAN_HAS_EMSA_RAW) || defined(BOTAN_HAS_EME_RAW)
185
         Botan::RSA_PrivateKey rsa(Test::rng(), 1024);
1✔
186
         Botan::Null_RNG null_rng;
1✔
187
   #endif
188

189
   #if defined(BOTAN_HAS_EMSA_RAW)
190

191
         /*
192
         * The blinder chooses a new starting point BOTAN_BLINDING_REINIT_INTERVAL
193
         * so sign several times that with a single key.
194
         *
195
         * Very small values (padding/hashing disabled, only low byte set on input)
196
         * are used as an additional test on the blinders.
197
         */
198

199
         Botan::PK_Signer signer(
1✔
200
            rsa, Test::rng(), "Raw", Botan::Signature_Format::Standard, "base");  // don't try this at home
1✔
201
         Botan::PK_Verifier verifier(rsa, "Raw", Botan::Signature_Format::Standard, "base");
1✔
202

203
         for(size_t i = 1; i <= BOTAN_BLINDING_REINIT_INTERVAL * 6; ++i) {
385✔
204
            std::vector<uint8_t> input(16);
384✔
205
            input[input.size() - 1] = static_cast<uint8_t>(i | 1);
384✔
206

207
            signer.update(input);
384✔
208

209
            // assert RNG is not called in this situation
210
            std::vector<uint8_t> signature = signer.signature(null_rng);
384✔
211

212
            result.test_eq("Signature verifies", verifier.verify_message(input, signature), true);
768✔
213
         }
768✔
214
   #endif
215

216
   #if defined(BOTAN_HAS_EME_RAW)
217

218
         /*
219
         * The blinder chooses a new starting point BOTAN_BLINDING_REINIT_INTERVAL
220
         * so decrypt several times that with a single key.
221
         *
222
         * Very small values (padding/hashing disabled, only low byte set on input)
223
         * are used as an additional test on the blinders.
224
         */
225

226
         Botan::PK_Encryptor_EME encryptor(rsa, Test::rng(), "Raw", "base");  // don't try this at home
1✔
227

228
         /*
229
         Test blinding reinit interval
230

231
         Seed Fixed_Output_RNG only with enough bytes for the initial
232
         blinder initialization plus the exponent blinding bits which
233
         is 2*64 bits per operation.
234
         */
235
         const size_t rng_bytes = rsa.get_n().bytes() + (2 * 8 * BOTAN_BLINDING_REINIT_INTERVAL);
1✔
236

237
         Botan_Tests::Fixed_Output_RNG fixed_rng(Test::rng(), rng_bytes);
1✔
238
         Botan::PK_Decryptor_EME decryptor(rsa, fixed_rng, "Raw", "base");
1✔
239

240
         for(size_t i = 1; i <= BOTAN_BLINDING_REINIT_INTERVAL; ++i) {
65✔
241
            std::vector<uint8_t> input(16);
64✔
242
            input[input.size() - 1] = static_cast<uint8_t>(i);
64✔
243

244
            std::vector<uint8_t> ciphertext = encryptor.encrypt(input, null_rng);
64✔
245

246
            std::vector<uint8_t> plaintext = Botan::unlock(decryptor.decrypt(ciphertext));
64✔
247
            plaintext.insert(plaintext.begin(), input.size() - 1, 0);
64✔
248

249
            result.test_eq("Successful decryption", plaintext, input);
128✔
250
         }
192✔
251

252
         result.test_eq("RNG is no longer seeded", fixed_rng.is_seeded(), false);
1✔
253

254
         // one more decryption should trigger a blinder reinitialization
255
         result.test_throws("RSA blinding reinit",
3✔
256
                            "Test error Fixed output RNG ran out of bytes, test bug?",
257
                            [&decryptor, &encryptor, &null_rng]() {
2✔
258
                               std::vector<uint8_t> ciphertext =
1✔
259
                                  encryptor.encrypt(std::vector<uint8_t>(16, 5), null_rng);
1✔
260
                               decryptor.decrypt(ciphertext);
1✔
261
                            });
×
262

263
   #endif
264

265
         return std::vector<Test::Result>{result};
2✔
266
      }
1✔
267
};
268

269
BOTAN_REGISTER_TEST("pubkey", "rsa_encrypt", RSA_ES_KAT_Tests);
270
BOTAN_REGISTER_TEST("pubkey", "rsa_decrypt", RSA_Decryption_KAT_Tests);
271
BOTAN_REGISTER_TEST("pubkey", "rsa_sign", RSA_Signature_KAT_Tests);
272
BOTAN_REGISTER_TEST("pubkey", "rsa_pss", RSA_PSS_KAT_Tests);
273
BOTAN_REGISTER_TEST("pubkey", "rsa_pss_raw", RSA_PSS_Raw_KAT_Tests);
274
BOTAN_REGISTER_TEST("pubkey", "rsa_verify", RSA_Signature_Verify_Tests);
275
BOTAN_REGISTER_TEST("pubkey", "rsa_verify_invalid", RSA_Signature_Verify_Invalid_Tests);
276
BOTAN_REGISTER_TEST("pubkey", "rsa_kem", RSA_KEM_Tests);
277
BOTAN_REGISTER_TEST("pubkey", "rsa_keygen", RSA_Keygen_Tests);
278
BOTAN_REGISTER_TEST("pubkey", "rsa_keygen_stability", RSA_Keygen_Stability_Tests);
279
BOTAN_REGISTER_TEST("pubkey", "rsa_keygen_badrng", RSA_Keygen_Bad_RNG_Test);
280
BOTAN_REGISTER_TEST("pubkey", "rsa_blinding", RSA_Blinding_Tests);
281

282
#endif
283

284
}
285

286
}
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