diff options
Diffstat (limited to 'botan/src/pubkey/keypair')
-rw-r--r-- | botan/src/pubkey/keypair/info.txt | 14 | ||||
-rw-r--r-- | botan/src/pubkey/keypair/keypair.cpp | 73 | ||||
-rw-r--r-- | botan/src/pubkey/keypair/keypair.h | 47 |
3 files changed, 134 insertions, 0 deletions
diff --git a/botan/src/pubkey/keypair/info.txt b/botan/src/pubkey/keypair/info.txt new file mode 100644 index 0000000..9e75864 --- /dev/null +++ b/botan/src/pubkey/keypair/info.txt @@ -0,0 +1,14 @@ +realname "Keypair Testing" + +define KEYPAIR_TESTING + +load_on auto + +<add> +keypair.cpp +keypair.h +</add> + +<requires> +libstate +</requires> diff --git a/botan/src/pubkey/keypair/keypair.cpp b/botan/src/pubkey/keypair/keypair.cpp new file mode 100644 index 0000000..486577f --- /dev/null +++ b/botan/src/pubkey/keypair/keypair.cpp @@ -0,0 +1,73 @@ +/* +* Keypair Checks +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/keypair.h> +#include <botan/look_pk.h> +#include <memory> + +namespace Botan { + +namespace KeyPair { + +/* +* Check an encryption key pair for consistency +*/ +void check_key(RandomNumberGenerator& rng, + PK_Encryptor* encryptor, PK_Decryptor* decryptor) + { + if(encryptor->maximum_input_size() == 0) + return; + + std::auto_ptr<PK_Encryptor> enc(encryptor); + std::auto_ptr<PK_Decryptor> dec(decryptor); + + SecureVector<byte> message(enc->maximum_input_size() - 1); + rng.randomize(message, message.size()); + + SecureVector<byte> ciphertext = enc->encrypt(message, rng); + if(ciphertext == message) + throw Self_Test_Failure("Encryption key pair consistency failure"); + + SecureVector<byte> message2 = dec->decrypt(ciphertext); + if(message != message2) + throw Self_Test_Failure("Encryption key pair consistency failure"); + } + +/* +* Check a signature key pair for consistency +*/ +void check_key(RandomNumberGenerator& rng, + PK_Signer* signer, PK_Verifier* verifier) + { + std::auto_ptr<PK_Signer> sig(signer); + std::auto_ptr<PK_Verifier> ver(verifier); + + SecureVector<byte> message(16); + rng.randomize(message, message.size()); + + SecureVector<byte> signature; + + try + { + signature = sig->sign_message(message, rng); + } + catch(Encoding_Error) + { + return; + } + + if(!ver->verify_message(message, signature)) + throw Self_Test_Failure("Signature key pair consistency failure"); + + ++message[0]; + if(ver->verify_message(message, signature)) + throw Self_Test_Failure("Signature key pair consistency failure"); + } + +} + +} diff --git a/botan/src/pubkey/keypair/keypair.h b/botan/src/pubkey/keypair/keypair.h new file mode 100644 index 0000000..b1d5c2d --- /dev/null +++ b/botan/src/pubkey/keypair/keypair.h @@ -0,0 +1,47 @@ +/* +* Keypair Checks +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_KEYPAIR_H__ +#define BOTAN_KEYPAIR_H__ + +#include <botan/pubkey.h> + +namespace Botan { + +namespace KeyPair { + +/** +* Tests whether the specified encryptor and decryptor are related to each other, +* i.e. whether encrypting with the encryptor and consecutive decryption leads to +* the original plaintext. +* @param rng the rng to use +* @param enc the encryptor to test +* @param dec the decryptor to test +* @throw Self_Test_Failure if the arguments are not related to each other +*/ +BOTAN_DLL void check_key(RandomNumberGenerator& rng, + PK_Encryptor* enc, + PK_Decryptor* dec); + +/** +* Tests whether the specified signer and verifier are related to each other, +* i.e. whether a signature created with the signer and can be +* successfully verified with the verifier. +* @param rng the rng to use +* @param sig the signer to test +* @param ver the verifier to test +* @throw Self_Test_Failure if the arguments are not related to each other +*/ +BOTAN_DLL void check_key(RandomNumberGenerator& rng, + PK_Signer* sig, + PK_Verifier* ver); + +} + +} + +#endif |