Debian Codegraph @

lib/Crypto/SelfTest/PublicKey/test_ElGamal.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # SelfTest/PublicKey/test_ElGamal.py: Self-test for the ElGamal primitive
4 #
5 # ===================================================================
6 # The contents of this file are dedicated to the public domain. To
7 # the extent that dedication to the public domain is not available,
8 # everyone is granted a worldwide, perpetual, royalty-free,
9 # non-exclusive license to exercise all rights associated with the
10 # contents of this file for any purpose whatsoever.
11 # No rights are reserved.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21 # ===================================================================
22
23 """Self-test suite for Crypto.PublicKey.ElGamal"""
24
25 __revision__ = "$Id$"
26
27 import unittest
28 from Crypto.SelfTest.st_common import list_test_cases, a2b_hex, b2a_hex
29 from Crypto import Random
30 from Crypto.PublicKey import ElGamal
31 from Crypto.Util.number import bytes_to_long
32 from Crypto.Util.py3compat import *
33
34 class ElGamalTest(unittest.TestCase):
35
36 #
37 # Test vectors
38 #
39 # There seem to be no real ElGamal test vectors available in the
40 # public domain. The following test vectors have been generated
41 # with libgcrypt 1.5.0.
42 #
43 # Encryption
44 tve=[
45 {
46 # 256 bits
47 'p' :'BA4CAEAAED8CBE952AFD2126C63EB3B345D65C2A0A73D2A3AD4138B6D09BD933',
48 'g' :'05',
49 'y' :'60D063600ECED7C7C55146020E7A31C4476E9793BEAED420FEC9E77604CAE4EF',
50 'x' :'1D391BA2EE3C37FE1BA175A69B2C73A11238AD77675932',
51 'k' :'F5893C5BAB4131264066F57AB3D8AD89E391A0B68A68A1',
52 'pt' :'48656C6C6F207468657265',
53 'ct1':'32BFD5F487966CEA9E9356715788C491EC515E4ED48B58F0F00971E93AAA5EC7',
54 'ct2':'7BE8FBFF317C93E82FCEF9BD515284BA506603FEA25D01C0CB874A31F315EE68'
55 },
56
57 {
58 # 512 bits
59 'p' :'F1B18AE9F7B4E08FDA9A04832F4E919D89462FD31BF12F92791A93519F75076D6CE3942689CDFF2F344CAFF0F82D01864F69F3AECF566C774CBACF728B81A227',
60 'g' :'07',
61 'y' :'688628C676E4F05D630E1BE39D0066178CA7AA83836B645DE5ADD359B4825A12B02EF4252E4E6FA9BEC1DB0BE90F6D7C8629CABB6E531F472B2664868156E20C',
62 'x' :'14E60B1BDFD33436C0DA8A22FDC14A2CCDBBED0627CE68',
63 'k' :'38DBF14E1F319BDA9BAB33EEEADCAF6B2EA5250577ACE7',
64 'pt' :'48656C6C6F207468657265',
65 'ct1':'290F8530C2CC312EC46178724F196F308AD4C523CEABB001FACB0506BFED676083FE0F27AC688B5C749AB3CB8A80CD6F7094DBA421FB19442F5A413E06A9772B',
66 'ct2':'1D69AAAD1DC50493FB1B8E8721D621D683F3BF1321BE21BC4A43E11B40C9D4D9C80DE3AAC2AB60D31782B16B61112E68220889D53C4C3136EE6F6CE61F8A23A0'
67 }
68 ]
69
70 # Signature
71 tvs=[
72 {
73 # 256 bits
74 'p' :'D2F3C41EA66530838A704A48FFAC9334F4701ECE3A97CEE4C69DD01AE7129DD7',
75 'g' :'05',
76 'y' :'C3F9417DC0DAFEA6A05C1D2333B7A95E63B3F4F28CC962254B3256984D1012E7',
77 'x' :'165E4A39BE44D5A2D8B1332D416BC559616F536BC735BB',
78 'k' :'C7F0C794A7EAD726E25A47FF8928013680E73C51DD3D7D99BFDA8F492585928F',
79 'h' :'48656C6C6F207468657265',
80 'sig1':'35CA98133779E2073EF31165AFCDEB764DD54E96ADE851715495F9C635E1E7C2',
81 'sig2':'0135B88B1151279FE5D8078D4FC685EE81177EE9802AB123A73925FC1CB059A7',
82 },
83 {
84 # 512 bits
85 'p' :'E24CF3A4B8A6AF749DCA6D714282FE4AABEEE44A53BB6ED15FBE32B5D3C3EF9CC4124A2ECA331F3C1C1B667ACA3766825217E7B5F9856648D95F05330C6A19CF',
86 'g' :'0B',
87 'y' :'2AD3A1049CA5D4ED207B2431C79A8719BB4073D4A94E450EA6CEE8A760EB07ADB67C0D52C275EE85D7B52789061EE45F2F37D9B2AE522A51C28329766BFE68AC',
88 'x' :'16CBB4F46D9ECCF24FF9F7E63CAA3BD8936341555062AB',
89 'k' :'8A3D89A4E429FD2476D7D717251FB79BF900FFE77444E6BB8299DC3F84D0DD57ABAB50732AE158EA52F5B9E7D8813E81FD9F79470AE22F8F1CF9AEC820A78C69',
90 'h' :'48656C6C6F207468657265',
91 'sig1':'BE001AABAFFF976EC9016198FBFEA14CBEF96B000CCC0063D3324016F9E91FE80D8F9325812ED24DDB2B4D4CF4430B169880B3CE88313B53255BD4EC0378586F',
92 'sig2':'5E266F3F837BA204E3BBB6DBECC0611429D96F8C7CE8F4EFDF9D4CB681C2A954468A357BF4242CEC7418B51DFC081BCD21299EF5B5A0DDEF3A139A1817503DDE',
93 }
94 ]
95
96 def test_generate_180(self):
97 self._test_random_key(180)
98
99 def test_encryption(self):
100 for tv in self.tve:
101 d = self.convert_tv(tv, True)
102 key = ElGamal.construct(d['key'])
103 ct = key._encrypt(d['pt'], d['k'])
104 self.assertEqual(ct[0], d['ct1'])
105 self.assertEqual(ct[1], d['ct2'])
106
107 def test_decryption(self):
108 for tv in self.tve:
109 d = self.convert_tv(tv, True)
110 key = ElGamal.construct(d['key'])
111 pt = key._decrypt((d['ct1'], d['ct2']))
112 self.assertEqual(pt, d['pt'])
113
114 def test_signing(self):
115 for tv in self.tvs:
116 d = self.convert_tv(tv, True)
117 key = ElGamal.construct(d['key'])
118 sig1, sig2 = key._sign(d['h'], d['k'])
119 self.assertEqual(sig1, d['sig1'])
120 self.assertEqual(sig2, d['sig2'])
121
122 def test_verification(self):
123 for tv in self.tvs:
124 d = self.convert_tv(tv, True)
125 key = ElGamal.construct(d['key'])
126 # Positive test
127 res = key._verify( d['h'], (d['sig1'],d['sig2']) )
128 self.assertTrue(res)
129 # Negative test
130 res = key._verify( d['h'], (d['sig1']+1,d['sig2']) )
131 self.assertFalse(res)
132
133 def test_bad_key3(self):
134 tup = tup0 = list(self.convert_tv(self.tvs[0], 1)['key'])[:3]
135 tup[0] += 1 # p += 1 (not prime)
136 self.assertRaises(ValueError, ElGamal.construct, tup)
137
138 tup = tup0
139 tup[1] = 1 # g = 1
140 self.assertRaises(ValueError, ElGamal.construct, tup)
141
142 tup = tup0
143 tup[2] = tup[0]*2 # y = 2*p
144 self.assertRaises(ValueError, ElGamal.construct, tup)
145
146 def test_bad_key4(self):
147 tup = tup0 = list(self.convert_tv(self.tvs[0], 1)['key'])
148 tup[3] += 1 # x += 1
149 self.assertRaises(ValueError, ElGamal.construct, tup)
150
151 def convert_tv(self, tv, as_longs=0):
152 """Convert a test vector from textual form (hexadecimal ascii
153 to either integers or byte strings."""
154 key_comps = 'p','g','y','x'
155 tv2 = {}
156 for c in tv.keys():
157 tv2[c] = a2b_hex(tv[c])
158 if as_longs or c in key_comps or c in ('sig1','sig2'):
159 tv2[c] = bytes_to_long(tv2[c])
160 tv2['key']=[]
161 for c in key_comps:
162 tv2['key'] += [tv2[c]]
163 del tv2[c]
164 return tv2
165
166 def _test_random_key(self, bits):
167 elgObj = ElGamal.generate(bits, Random.new().read)
168 self._check_private_key(elgObj)
169 self._exercise_primitive(elgObj)
170 pub = elgObj.publickey()
171 self._check_public_key(pub)
172 self._exercise_public_primitive(elgObj)
173
174 def _check_private_key(self, elgObj):
175
176 # Check capabilities
177 self.assertTrue(elgObj.has_private())
178
179 # Sanity check key data
180 self.assertTrue(1<elgObj.g<(elgObj.p-1))
181 self.assertEqual(pow(elgObj.g, elgObj.p-1, elgObj.p), 1)
182 self.assertTrue(1<elgObj.x<(elgObj.p-1))
183 self.assertEqual(pow(elgObj.g, elgObj.x, elgObj.p), elgObj.y)
184
185 def _check_public_key(self, elgObj):
186
187 # Check capabilities
188 self.assertFalse(elgObj.has_private())
189
190 # Sanity check key data
191 self.assertTrue(1<elgObj.g<(elgObj.p-1))
192 self.assertEqual(pow(elgObj.g, elgObj.p-1, elgObj.p), 1)
193
194 def _exercise_primitive(self, elgObj):
195 # Test encryption/decryption
196 plaintext = 127218
197 ciphertext = elgObj._encrypt(plaintext, 123456789)
198 plaintextP = elgObj._decrypt(ciphertext)
199 self.assertEqual(plaintext, plaintextP)
200
201 # Test signature/verification
202 signature = elgObj._sign(plaintext, 987654321)
203 elgObj._verify(plaintext, signature)
204
205 def _exercise_public_primitive(self, elgObj):
206 plaintext = 92987276
207 ciphertext = elgObj._encrypt(plaintext, 123456789)
208
209 def get_tests(config={}):
210 tests = []
211 tests += list_test_cases(ElGamalTest)
212 return tests
213
214 if __name__ == '__main__':
215 suite = lambda: unittest.TestSuite(get_tests())
216 unittest.main(defaultTest='suite')
217