~ubuntu-branches/ubuntu/saucy/python-crypto/saucy-proposed

« back to all changes in this revision

Viewing changes to lib/Crypto/SelfTest/Cipher/common.py

  • Committer: Package Import Robot
  • Author(s): Sebastian Ramacher
  • Date: 2012-05-24 20:16:34 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20120524201634-de6vxznjsdgekuwp
Tags: 2.6-1
* New upstream release.
  - Fixes CVE-2012-2417: insecure ElGamal key generation.
* Set urgency to high since this fixes a security issue.
* debian/copyright:
  - Fix formatting.
  - Update Format URL.
* debian/control:
  - Bump Standards-Version to 3.9.3 (no changes required).
  - Drop qNEW from Description since qNEW has been removed.
* debian/patches: Remove posixread.patch (not needed anymore).

Show diffs side-by-side

added added

removed removed

Lines of Context:
74
74
            self.mode = getattr(self.module, "MODE_" + mode)
75
75
            self.iv = _extract(params, 'iv', None)
76
76
            if self.iv is not None: self.iv = b(self.iv)
 
77
 
 
78
            # Only relevant for OPENPGP mode
 
79
            self.encrypted_iv = _extract(params, 'encrypted_iv', None)
 
80
            if self.encrypted_iv is not None:
 
81
                self.encrypted_iv = b(self.encrypted_iv)
77
82
        else:
78
83
            # Stream cipher
79
84
            self.mode = None
84
89
    def shortDescription(self):
85
90
        return self.description
86
91
 
87
 
    def _new(self):
 
92
    def _new(self, do_decryption=0):
88
93
        params = self.extra_params.copy()
89
94
 
90
95
        # Handle CTR mode parameters.  By default, we use Counter.new(self.module.block_size)
106
111
            return self.module.new(a2b_hex(self.key), self.mode, **params)
107
112
        else:
108
113
            # Block cipher with iv
109
 
            return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.iv), **params)
 
114
            if do_decryption and self.mode == self.module.MODE_OPENPGP:
 
115
                # In PGP mode, the IV to feed for decryption is the *encrypted* one
 
116
                return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.encrypted_iv), **params)
 
117
            else:
 
118
                return self.module.new(a2b_hex(self.key), self.mode, a2b_hex(self.iv), **params)
110
119
 
111
120
    def runTest(self):
112
121
        plaintext = a2b_hex(self.plaintext)
113
122
        ciphertext = a2b_hex(self.ciphertext)
114
123
 
115
124
        ct1 = b2a_hex(self._new().encrypt(plaintext))
116
 
        pt1 = b2a_hex(self._new().decrypt(ciphertext))
 
125
        pt1 = b2a_hex(self._new(1).decrypt(ciphertext))
117
126
        ct2 = b2a_hex(self._new().encrypt(plaintext))
118
 
        pt2 = b2a_hex(self._new().decrypt(ciphertext))
 
127
        pt2 = b2a_hex(self._new(1).decrypt(ciphertext))
 
128
 
 
129
        if hasattr(self.module, "MODE_OPENPGP") and self.mode == self.module.MODE_OPENPGP:
 
130
            # In PGP mode, data returned by the first encrypt()
 
131
            # is prefixed with the encrypted IV.
 
132
            # Here we check it and then remove it from the ciphertexts.
 
133
            eilen = len(self.encrypted_iv)
 
134
            self.assertEqual(self.encrypted_iv, ct1[:eilen])
 
135
            self.assertEqual(self.encrypted_iv, ct2[:eilen])
 
136
            ct1 = ct1[eilen:]
 
137
            ct2 = ct2[eilen:]
119
138
 
120
139
        self.assertEqual(self.ciphertext, ct1)  # encrypt
121
140
        self.assertEqual(self.ciphertext, ct2)  # encrypt (second time)
204
223
        """Regression test: m.new(key, m.MODE_CFB, segment_size=N) should require segment_size to be a multiple of 8 bits"""
205
224
        for i in range(1, 8):
206
225
            self.assertRaises(ValueError, self.module.new, a2b_hex(self.key), self.module.MODE_CFB, segment_size=i)
207
 
        self.module.new(a2b_hex(self.key), self.module.MODE_CFB, segment_size=8) # should succeed
 
226
        self.module.new(a2b_hex(self.key), self.module.MODE_CFB, "\0"*self.module.block_size, segment_size=8) # should succeed
208
227
 
209
228
class RoundtripTest(unittest.TestCase):
210
229
    def __init__(self, module, params):
220
239
        return """%s .decrypt() output of .encrypt() should not be garbled""" % (self.module_name,)
221
240
 
222
241
    def runTest(self):
223
 
        for mode in (self.module.MODE_ECB, self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_PGP, self.module.MODE_OFB):
 
242
        for mode in (self.module.MODE_ECB, self.module.MODE_CBC, self.module.MODE_CFB, self.module.MODE_OFB, self.module.MODE_OPENPGP):
224
243
            encryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv)
225
 
            decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv)
226
244
            ciphertext = encryption_cipher.encrypt(self.plaintext)
 
245
            
 
246
            if mode != self.module.MODE_OPENPGP:
 
247
                decryption_cipher = self.module.new(a2b_hex(self.key), mode, self.iv)
 
248
            else:
 
249
                eiv = ciphertext[:self.module.block_size+2]
 
250
                ciphertext = ciphertext[self.module.block_size+2:]
 
251
                decryption_cipher = self.module.new(a2b_hex(self.key), mode, eiv)
227
252
            decrypted_plaintext = decryption_cipher.decrypt(ciphertext)
228
253
            self.assertEqual(self.plaintext, decrypted_plaintext)
229
254
 
 
255
class PGPTest(unittest.TestCase):
 
256
    def __init__(self, module, params):
 
257
        unittest.TestCase.__init__(self)
 
258
        self.module = module
 
259
        self.key = b(params['key'])
 
260
 
 
261
    def shortDescription(self):
 
262
        return "MODE_PGP was implemented incorrectly and insecurely. It's completely banished now."
 
263
 
 
264
    def runTest(self):
 
265
        self.assertRaises(ValueError, self.module.new, a2b_hex(self.key),
 
266
                self.module.MODE_PGP)
 
267
 
 
268
class IVLengthTest(unittest.TestCase):
 
269
    def __init__(self, module, params):
 
270
        unittest.TestCase.__init__(self)
 
271
        self.module = module
 
272
        self.key = b(params['key'])
 
273
 
 
274
    def shortDescription(self):
 
275
        return "Check that all modes except MODE_ECB and MODE_CTR require an IV of the proper length"
 
276
 
 
277
    def runTest(self):
 
278
        self.assertRaises(ValueError, self.module.new, a2b_hex(self.key),
 
279
                self.module.MODE_CBC, "")
 
280
        self.assertRaises(ValueError, self.module.new, a2b_hex(self.key),
 
281
                self.module.MODE_CFB, "")
 
282
        self.assertRaises(ValueError, self.module.new, a2b_hex(self.key),
 
283
                self.module.MODE_OFB, "")
 
284
        self.assertRaises(ValueError, self.module.new, a2b_hex(self.key),
 
285
                self.module.MODE_OPENPGP, "")
 
286
        self.module.new(a2b_hex(self.key), self.module.MODE_ECB, "")
 
287
        self.module.new(a2b_hex(self.key), self.module.MODE_CTR, "", counter=self._dummy_counter)
 
288
 
 
289
    def _dummy_counter(self):
 
290
        return "\0" * self.module.block_size
 
291
 
230
292
def make_block_tests(module, module_name, test_data):
231
293
    tests = []
232
294
    extra_tests_added = 0
272
334
                CTRWraparoundTest(module, params),
273
335
                CFBSegmentSizeTest(module, params),
274
336
                RoundtripTest(module, params),
 
337
                PGPTest(module, params),
 
338
                IVLengthTest(module, params),
275
339
            ]
276
340
            extra_tests_added = 1
277
341