monotone

monotone Mtn Source Tree

Root/botan/keypair.cpp

1/*************************************************
2* Keypair Checks Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/keypair.h>
7#include <botan/look_pk.h>
8#include <botan/rng.h>
9#include <memory>
10
11namespace Botan {
12
13namespace KeyPair {
14
15/*************************************************
16* Check an encryption key pair for consistency *
17*************************************************/
18void check_key(PK_Encryptor* encryptor, PK_Decryptor* decryptor)
19 {
20 if(encryptor->maximum_input_size() == 0)
21 return;
22
23 std::auto_ptr<PK_Encryptor> enc(encryptor);
24 std::auto_ptr<PK_Decryptor> dec(decryptor);
25
26 SecureVector<byte> message(enc->maximum_input_size() - 1);
27 Global_RNG::randomize(message, message.size());
28
29 SecureVector<byte> ciphertext = enc->encrypt(message);
30 if(ciphertext == message)
31 throw Self_Test_Failure("Encryption key pair consistency failure");
32
33 SecureVector<byte> message2 = dec->decrypt(ciphertext);
34 if(message != message2)
35 throw Self_Test_Failure("Encryption key pair consistency failure");
36 }
37
38/*************************************************
39* Check a signature key pair for consistency *
40*************************************************/
41void check_key(PK_Signer* signer, PK_Verifier* verifier)
42 {
43 std::auto_ptr<PK_Signer> sig(signer);
44 std::auto_ptr<PK_Verifier> ver(verifier);
45
46 SecureVector<byte> message(16);
47 Global_RNG::randomize(message, message.size());
48
49 SecureVector<byte> signature;
50
51 try {
52 signature = sig->sign_message(message);
53 }
54 catch(Encoding_Error)
55 {
56 return;
57 }
58
59 if(!ver->verify_message(message, signature))
60 throw Self_Test_Failure("Signature key pair consistency failure");
61
62 ++message[0];
63 if(ver->verify_message(message, signature))
64 throw Self_Test_Failure("Signature key pair consistency failure");
65 }
66
67}
68
69}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status