]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/Crypto/test/test_publickey.py
move all packages into src/, fix allmydata.Crypto build. Now you must perform a ...
[tahoe-lafs/tahoe-lafs.git] / src / Crypto / test / test_publickey.py
1 #
2 # Test script for Crypto.Util.PublicKey.
3 #
4
5 __revision__ = "$Id: test_publickey.py,v 1.7 2003/04/04 19:38:28 akuchling Exp $"
6
7 import sys, cPickle
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
12
13 tested_modules = [ "Crypto.PublicKey.RSA", "Crypto.PublicKey.DSA",
14                    "Crypto.PublicKey.ElGamal", "Crypto.PublicKey.qNEW"]
15
16 class PublicKeyTest (TestScenario):
17
18     def setup (self):
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)
22         self.pool.stir()
23
24     def shutdown (self):
25         del self.pool
26
27     def testkey (self, key, randfunc, verbose=0):
28         plaintext="Hello"
29         # Generate maximum-size plaintext
30         maxplain = (key.size() // 8) * '\377'
31
32         if key.can_encrypt():
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)
39
40         if key.can_sign():
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)',
46                            want_true=0)
47
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)
51
52             if verbose: print '  Removing private key data'
53             pubonly=key.publickey()
54             self.test_bool('pubonly.verify(plaintext, signature)')
55
56         # Test blinding
57         if key.can_blind():
58             if verbose: print '  Blinding test'
59             K=number.getPrime(30, randfunc)
60             B="garbage"
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)
66
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)')
71
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)')
76
77
78     def exercise (self, randfunc, pk_mod, verbose=0):
79         N=256                           # Key size, measured in bits
80
81         key=pk_mod.generate(N, randfunc)
82
83         if verbose:
84             print ' Key data:'
85             for field in key.keydata:
86                 print "  ", field, ':', hex(getattr(key,field))
87
88         if verbose: print " Testing newly generated key"
89         self.testkey(key, randfunc, verbose)
90         if verbose: print " Testing pickled/unpickled key"
91         import pickle
92         s = pickle.dumps(key) ; key2 = pickle.loads(s)
93         self.testkey(key2, randfunc, verbose)
94
95         if verbose: print " Testing cPickled key"
96         s = cPickle.dumps(key) ; key2 = cPickle.loads(s)
97         self.testkey(key2, randfunc, verbose)
98         if verbose: print
99
100
101     def check_rsa(self):
102         "Check RSA algorithm"
103         self.exercise(self.pool.get_bytes, RSA)
104
105     def check_dsa(self):
106         "Check DSA algorithm"
107         self.exercise(self.pool.get_bytes, DSA)
108
109     def check_elgamal(self):
110         "Check ElGamal algorithm"
111         self.exercise(self.pool.get_bytes, ElGamal)
112
113     def check_qnew(self):
114         "Check qNEW algorithm"
115         self.exercise(self.pool.get_bytes, qNEW)
116
117 # class PublicKeyTest
118
119
120 if __name__ == "__main__":
121     (scenarios, options) = parse_args()
122     run_scenarios(scenarios, options)