/src/botan/src/lib/pubkey/keypair/keypair.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Keypair Checks |
3 | | * (C) 1999-2010 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/internal/keypair.h> |
9 | | #include <botan/pubkey.h> |
10 | | #include <botan/rng.h> |
11 | | |
12 | | namespace Botan::KeyPair { |
13 | | |
14 | | /* |
15 | | * Check an encryption key pair for consistency |
16 | | */ |
17 | | bool encryption_consistency_check(RandomNumberGenerator& rng, |
18 | | const Private_Key& private_key, |
19 | | const Public_Key& public_key, |
20 | | const std::string& padding) |
21 | 0 | { |
22 | 0 | PK_Encryptor_EME encryptor(public_key, rng, padding); |
23 | 0 | PK_Decryptor_EME decryptor(private_key, rng, padding); |
24 | | |
25 | | /* |
26 | | Weird corner case, if the key is too small to encrypt anything at |
27 | | all. This can happen with very small RSA keys with PSS |
28 | | */ |
29 | 0 | if(encryptor.maximum_input_size() == 0) |
30 | 0 | return true; |
31 | | |
32 | 0 | std::vector<uint8_t> plaintext; |
33 | 0 | rng.random_vec(plaintext, encryptor.maximum_input_size() - 1); |
34 | |
|
35 | 0 | std::vector<uint8_t> ciphertext = encryptor.encrypt(plaintext, rng); |
36 | 0 | if(ciphertext == plaintext) |
37 | 0 | return false; |
38 | | |
39 | 0 | std::vector<uint8_t> decrypted = unlock(decryptor.decrypt(ciphertext)); |
40 | |
|
41 | 0 | return (plaintext == decrypted); |
42 | 0 | } |
43 | | |
44 | | /* |
45 | | * Check a signature key pair for consistency |
46 | | */ |
47 | | bool signature_consistency_check(RandomNumberGenerator& rng, |
48 | | const Private_Key& private_key, |
49 | | const Public_Key& public_key, |
50 | | const std::string& padding) |
51 | 0 | { |
52 | 0 | PK_Signer signer(private_key, rng, padding); |
53 | 0 | PK_Verifier verifier(public_key, padding); |
54 | |
|
55 | 0 | std::vector<uint8_t> message(32); |
56 | 0 | rng.randomize(message.data(), message.size()); |
57 | |
|
58 | 0 | std::vector<uint8_t> signature; |
59 | |
|
60 | 0 | try |
61 | 0 | { |
62 | 0 | signature = signer.sign_message(message, rng); |
63 | 0 | } |
64 | 0 | catch(Encoding_Error&) |
65 | 0 | { |
66 | 0 | return false; |
67 | 0 | } |
68 | | |
69 | 0 | if(!verifier.verify_message(message, signature)) |
70 | 0 | return false; |
71 | | |
72 | | // Now try to check a corrupt signature, ensure it does not succeed |
73 | 0 | ++signature[0]; |
74 | |
|
75 | 0 | if(verifier.verify_message(message, signature)) |
76 | 0 | return false; |
77 | | |
78 | 0 | return true; |
79 | 0 | } |
80 | | |
81 | | } |