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