/************************************************* * Keypair Checks Source File * * (C) 1999-2007 Jack Lloyd * *************************************************/ #include #include #include 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 enc(encryptor); std::auto_ptr dec(decryptor); SecureVector message(enc->maximum_input_size() - 1); rng.randomize(message, message.size()); SecureVector ciphertext = enc->encrypt(message, rng); if(ciphertext == message) throw Self_Test_Failure("Encryption key pair consistency failure"); SecureVector 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 sig(signer); std::auto_ptr ver(verifier); SecureVector message(16); rng.randomize(message, message.size()); SecureVector signature; try { signature = sig->sign_message(message); } 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"); } } }