summaryrefslogtreecommitdiff
path: root/tests/test_dsa.py
blob: 7823f5004ac27b756ce8e44621f06b34d44f6b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python

"""Unit tests for M2Crypto.DSA.

Copyright (c) 2000 Ng Pheng Siong. All rights reserved."""

import unittest
import sha
from M2Crypto import DSA, BIO, Rand, m2

class DSATestCase(unittest.TestCase):

    errkey  = 'tests/rsa.priv.pem'
    privkey = 'tests/dsa.priv.pem'
    pubkey  = 'tests/dsa.pub.pem'
    param   = 'tests/dsa.param.pem'

    data = sha.sha('Can you spell subliminal channel?').digest()
    different_data = sha.sha('I can spell.').digest()

    def callback(self, *args):
        pass

    def test_loadkey_junk(self):
        self.assertRaises(DSA.DSAError, DSA.load_key, self.errkey)

    def test_loadkey(self):
        dsa = DSA.load_key(self.privkey)
        assert len(dsa) == 1024
        self.assertRaises(AttributeError, getattr, dsa, 'foobar')
        for k in ('p', 'q', 'g', 'priv', 'pub'):
            self.assertRaises(DSA.DSAError, setattr, dsa, k, 1)

    def test_loadparam(self):
        self.assertRaises(DSA.DSAError, DSA.load_key, self.param)
        dsa = DSA.load_params(self.param)
        assert not dsa.check_key()
        assert len(dsa) == 1024

    def test_sign(self):
        dsa = DSA.load_key(self.privkey)
        assert dsa.check_key()
        r, s = dsa.sign(self.data)
        assert dsa.verify(self.data, r, s)
        assert not dsa.verify(self.data, s, r)

    def test_sign_asn1(self):
        dsa = DSA.load_key(self.privkey)
        blob = dsa.sign_asn1(self.data)
        assert dsa.verify_asn1(self.data, blob)

    def test_sign_with_params_only(self):
        dsa = DSA.load_params(self.param)
        self.assertRaises(AssertionError, dsa.sign, self.data)
        self.assertRaises(AssertionError, dsa.sign_asn1, self.data)

    def test_pub_verify(self):
        dsa = DSA.load_key(self.privkey)
        r, s = dsa.sign(self.data)
        dsapub = DSA.load_pub_key(self.pubkey)
        assert dsapub.check_key()
        assert dsapub.verify(self.data, r, s)
        self.assertRaises(DSA.DSAError, dsapub.sign)

    def test_verify_fail(self):
        dsa = DSA.load_key(self.privkey)
        r, s = dsa.sign(self.data)
        assert not dsa.verify(self.different_data, r, s)

    def test_verify_fail2(self):
        dsa = DSA.load_key(self.privkey)
        r,s = dsa.sign(self.data)
        dsa2 = DSA.load_params(self.param)
        assert not dsa2.check_key()
        self.assertRaises(AssertionError, dsa2.verify, self.data, r, s)

    def test_genparam_setparam_genkey(self):
        dsa = DSA.gen_params(1024, self.callback)
        assert len(dsa) == 1024
        p = dsa.p
        q = dsa.q
        g = dsa.g
        dsa2 = DSA.set_params(p,q,g)
        assert not dsa2.check_key()
        dsa2.gen_key()
        assert dsa2.check_key()
        r,s = dsa2.sign(self.data)
        assert dsa2.verify(self.data, r, s)

def suite():
    return unittest.makeSuite(DSATestCase)
    

if __name__ == '__main__':
    Rand.load_file('randpool.dat', -1) 
    unittest.TextTestRunner().run(suite())
    Rand.save_file('randpool.dat')