2 # Test script for Crypto.Util.PublicKey.
5 __revision__ = "$Id: test_publickey.py,v 1.7 2003/04/04 19:38:28 akuchling Exp $"
8 from sancho.unittest import TestScenario, parse_args, run_scenarios
9 from allmydata.Crypto.PublicKey import *
10 from allmydata.Crypto.Util.randpool import RandomPool
11 from allmydata.Crypto.Util import number
13 tested_modules = [ "Crypto.PublicKey.RSA", "Crypto.PublicKey.DSA",
14 "Crypto.PublicKey.ElGamal", "Crypto.PublicKey.qNEW"]
16 class PublicKeyTest (TestScenario):
19 # Set up a random pool; we won't bother to actually fill it with
20 # entropy from the keyboard
21 self.pool = RandomPool(384)
27 def testkey (self, key, randfunc, verbose=0):
29 # Generate maximum-size plaintext
30 maxplain = (key.size() // 8) * '\377'
33 if verbose: print ' Encryption/decryption test'
34 K=number.getPrime(10, randfunc)
35 ciphertext=key.encrypt(plaintext, K)
36 self.test_val('key.decrypt(ciphertext)', plaintext)
37 ciphertext=key.encrypt(maxplain, K)
38 self.test_val('key.decrypt(ciphertext)', maxplain)
41 if verbose: print ' Signature test'
42 K=number.getPrime(30, randfunc)
43 signature=key.sign(plaintext, K)
44 self.test_bool('key.verify(plaintext, signature)')
45 self.test_bool('key.verify(plaintext[:-1], signature)',
48 # Change a single bit in the plaintext
49 badtext=plaintext[:-3]+chr( 1 ^ ord(plaintext[-3]) )+plaintext[-3:]
50 self.test_bool('key.verify(badtext, signature)', want_true=0)
52 if verbose: print ' Removing private key data'
53 pubonly=key.publickey()
54 self.test_bool('pubonly.verify(plaintext, signature)')
58 if verbose: print ' Blinding test'
59 K=number.getPrime(30, randfunc)
61 blindedtext=key.blind(plaintext, B)
62 signature=key.sign(blindedtext, K)
63 unblindedsignature=(key.unblind(signature[0], B),)
64 self.test_bool('key.verify(plaintext, unblindedsignature)')
65 self.test_val('key.sign(plaintext, K)', unblindedsignature)
67 # Change a single bit in the blinding factor
68 badB=B[:-3]+chr( 1 ^ ord(B[-3]) )+B[-3:]
69 badunblindedsignature=(key.unblind(signature[0], badB),)
70 self.test_false('key.verify(badtext, badunblindedsignature)')
72 badblindedtext=key.blind(plaintext, badB)
73 badsignature=key.sign(blindedtext, K)
74 badunblindedsignature2=(key.unblind(signature[0], B),)
75 self.test_false('key.verify(badtext, badunblindedsignature2)')
78 def exercise (self, randfunc, pk_mod, verbose=0):
79 N=256 # Key size, measured in bits
81 key=pk_mod.generate(N, randfunc)
85 for field in key.keydata:
86 print " ", field, ':', hex(getattr(key,field))
88 if verbose: print " Testing newly generated key"
89 self.testkey(key, randfunc, verbose)
90 if verbose: print " Testing pickled/unpickled key"
92 s = pickle.dumps(key) ; key2 = pickle.loads(s)
93 self.testkey(key2, randfunc, verbose)
95 if verbose: print " Testing cPickled key"
96 s = cPickle.dumps(key) ; key2 = cPickle.loads(s)
97 self.testkey(key2, randfunc, verbose)
102 "Check RSA algorithm"
103 self.exercise(self.pool.get_bytes, RSA)
106 "Check DSA algorithm"
107 self.exercise(self.pool.get_bytes, DSA)
109 def check_elgamal(self):
110 "Check ElGamal algorithm"
111 self.exercise(self.pool.get_bytes, ElGamal)
113 def check_qnew(self):
114 "Check qNEW algorithm"
115 self.exercise(self.pool.get_bytes, qNEW)
117 # class PublicKeyTest
120 if __name__ == "__main__":
121 (scenarios, options) = parse_args()
122 run_scenarios(scenarios, options)