5
5
import unittest
6
6
from cryptography .hazmat .backends import default_backend
7
7
from cryptography .hazmat .primitives .asymmetric import ec
8
+ from cryptography .hazmat .primitives .serialization import (
9
+ Encoding , PublicFormat
10
+ )
8
11
9
12
from nose .tools import eq_ , assert_raises
10
13
@@ -43,6 +46,7 @@ def b64d(arg):
43
46
return None
44
47
return base64 .urlsafe_b64decode (str (arg ) + '====' [:len (arg ) % 4 :])
45
48
49
+
46
50
def make_key ():
47
51
return ec .generate_private_key (ec .SECP256R1 (), default_backend ())
48
52
@@ -131,7 +135,8 @@ class TestEceChecking(unittest.TestCase):
131
135
def setUp (self ):
132
136
self .m_key = os .urandom (16 )
133
137
self .m_input = os .urandom (5 )
134
- # This header is specific to the padding tests, but can be used elsewhere
138
+ # This header is specific to the padding tests, but can be used
139
+ # elsewhere
135
140
self .m_header = b'\xaa \xd2 \x05 }3S\xb7 \xff 7\xbd \xe4 *\xe1 \xd5 \x0f \xda '
136
141
self .m_header += struct .pack ('!L' , 32 ) + b'\0 '
137
142
@@ -189,47 +194,54 @@ def test_encrypt_long_keyid(self):
189
194
self .m_input ,
190
195
version = 'aes128gcm' ,
191
196
key = self .m_key ,
192
- keyid = b64e (os .urandom (192 )), # 256 bytes
197
+ keyid = b64e (os .urandom (192 )), # 256 bytes
193
198
)
194
199
eq_ (ex .exception .message , "keyid is too long" )
195
200
196
201
def test_overlong_padding (self ):
197
202
with assert_raises (ECEException ) as ex :
198
203
ece .decrypt (
199
- self .m_header + b'\xbb \xc7 \xb9 ev\x0b \xf0 f+\x93 \xf4 \xe5 \xd6 \x94 \xb7 e\xf0 \xcd \x15 \x9b (\x01 \xa5 ' ,
204
+ self .m_header + b'\xbb \xc7 \xb9 ev\x0b \xf0 f+\x93 \xf4 '
205
+ b'\xe5 \xd6 \x94 \xb7 e\xf0 \xcd \x15 \x9b (\x01 \xa5 ' ,
200
206
version = 'aes128gcm' ,
201
207
key = b'd\xc7 \x0e d\xa7 %U\x14 Q\xf2 \x08 \xdf \xba \xa0 \xb9 r' ,
202
- keyid = b64e (os .urandom (192 )), # 256 bytes
208
+ keyid = b64e (os .urandom (192 )), # 256 bytes
203
209
)
204
210
eq_ (ex .exception .message , "all zero record plaintext" )
205
211
206
212
def test_bad_early_delimiter (self ):
207
213
with assert_raises (ECEException ) as ex :
208
214
ece .decrypt (
209
- self .m_header + b'\xb9 \xc7 \xb9 ev\x0b \xf0 \x9e B\xb1 \x08 C8u\xa3 \x06 \xc9 x\x06 \n \xfc |}\xe9 R\x85 \x91 \x8b X\x02 `\xf3 ' + b'E8z(\xe5 %f/H\xc1 \xc3 2\x04 \xb1 \x95 \xb5 N\x9e p\xd4 \x0e <\xf3 \xef \x0c g\x1b \xe0 \x14 I~\xdc ' ,
215
+ self .m_header + b'\xb9 \xc7 \xb9 ev\x0b \xf0 \x9e B\xb1 \x08 C8u'
216
+ b'\xa3 \x06 \xc9 x\x06 \n \xfc |}\xe9 R\x85 \x91 '
217
+ b'\x8b X\x02 `\xf3 ' +
218
+ b'E8z(\xe5 %f/H\xc1 \xc3 2\x04 \xb1 \x95 \xb5 N\x9e p\xd4 \x0e <\xf3 '
219
+ b'\xef \x0c g\x1b \xe0 \x14 I~\xdc ' ,
210
220
version = 'aes128gcm' ,
211
221
key = b'd\xc7 \x0e d\xa7 %U\x14 Q\xf2 \x08 \xdf \xba \xa0 \xb9 r' ,
212
- keyid = b64e (os .urandom (192 )), # 256 bytes
222
+ keyid = b64e (os .urandom (192 )), # 256 bytes
213
223
)
214
224
eq_ (ex .exception .message , "record delimiter != 1" )
215
225
216
226
def test_bad_final_delimiter (self ):
217
227
with assert_raises (ECEException ) as ex :
218
228
ece .decrypt (
219
- self .m_header + b'\xba \xc7 \xb9 ev\x0b \xf0 \x9e B\xb1 \x08 Ji\xe4 P\x1b \x8d I\xdb \xc6 y#MG\xc2 W\x16 ' ,
229
+ self .m_header + b'\xba \xc7 \xb9 ev\x0b \xf0 \x9e B\xb1 \x08 Ji'
230
+ b'\xe4 P\x1b \x8d I\xdb \xc6 y#MG\xc2 W\x16 ' ,
220
231
version = 'aes128gcm' ,
221
232
key = b'd\xc7 \x0e d\xa7 %U\x14 Q\xf2 \x08 \xdf \xba \xa0 \xb9 r' ,
222
- keyid = b64e (os .urandom (192 )), # 256 bytes
233
+ keyid = b64e (os .urandom (192 )), # 256 bytes
223
234
)
224
235
eq_ (ex .exception .message , "last record delimiter != 2" )
225
236
226
237
def test_damage (self ):
227
238
with assert_raises (ECEException ) as ex :
228
239
ece .decrypt (
229
- self .m_header + b'\xbb \xc6 \xb1 \x1d F:~\x0f \x07 +\xbe \xaa D\xe0 \xd6 .K\xe5 \xf9 ]%\xe3 \x86 q\xe0 }' ,
240
+ self .m_header + b'\xbb \xc6 \xb1 \x1d F:~\x0f \x07 +\xbe \xaa D'
241
+ b'\xe0 \xd6 .K\xe5 \xf9 ]%\xe3 \x86 q\xe0 }' ,
230
242
version = 'aes128gcm' ,
231
243
key = b'd\xc7 \x0e d\xa7 %U\x14 Q\xf2 \x08 \xdf \xba \xa0 \xb9 r' ,
232
- keyid = b64e (os .urandom (192 )), # 256 bytes
244
+ keyid = b64e (os .urandom (192 )), # 256 bytes
233
245
)
234
246
eq_ (ex .exception .message , "Decryption error: InvalidTag()" )
235
247
@@ -246,14 +258,14 @@ def tearDown(self):
246
258
247
259
def _rsoverhead (self , version ):
248
260
if version == 'aesgcm128' :
249
- return 1 ;
261
+ return 1
250
262
if version == 'aesgcm' :
251
- return 2 ;
252
- return 18 ;
263
+ return 2
264
+ return 18
253
265
254
266
def _generate_input (self , minLen = 0 ):
255
267
length = struct .unpack ('!B' , os .urandom (1 ))[0 ] + minLen
256
- return os .urandom (length );
268
+ return os .urandom (length )
257
269
258
270
def encrypt_decrypt (self , input , encrypt_params , decrypt_params = None ,
259
271
version = None ):
@@ -263,7 +275,7 @@ def encrypt_decrypt(self, input, encrypt_params, decrypt_params=None,
263
275
:type length: bytearray
264
276
:param encrypt_params: Dictionary of encryption parameters
265
277
:type encrypt_params: dict
266
- :param decrypt_params: Optional dictionary of decryption paramseters
278
+ :param decrypt_params: Optional dictionary of decryption parameters
267
279
:type decrypt_params: dict
268
280
:param version: Content-Type of the body, formulating encryption
269
281
:type enumerate("aes128gcm", "aesgcm", "aesgcm128"):
@@ -299,7 +311,8 @@ def encrypt_decrypt(self, input, encrypt_params, decrypt_params=None,
299
311
dh = decrypt_params .get ("dh" ),
300
312
private_key = decrypt_params .get ("private_key" ),
301
313
auth_secret = decrypt_params .get ("auth_secret" ),
302
- rs = decrypt_params .get ("rs" , decrypt_rs_default ),
314
+ rs = decrypt_params .get ("rs" ,
315
+ decrypt_rs_default ),
303
316
version = version )
304
317
logbuf ("Decrypted" , decrypted )
305
318
eq_ (input , decrypted )
@@ -346,12 +359,17 @@ def detect_truncation(self, version):
346
359
347
360
def use_dh (self , version ):
348
361
def pubbytes (k ):
349
- return k .public_key ().public_numbers ().encode_point ()
362
+ return k .public_key ().public_bytes (
363
+ Encoding .X962 ,
364
+ PublicFormat .UncompressedPoint
365
+ )
350
366
351
367
def privbytes (k ):
352
368
d = k .private_numbers ().private_value
353
369
b = b''
354
- for i in range (0 , k .private_numbers ().public_numbers .curve .key_size , 32 ):
370
+ for i in range (0 ,
371
+ k .private_numbers ().public_numbers .curve .key_size ,
372
+ 32 ):
355
373
b = struct .pack ("!L" , (d >> i ) & 0xffffffff ) + b
356
374
return b
357
375
@@ -424,13 +442,11 @@ def _run(self, mode):
424
442
if mode == 'encrypt' :
425
443
func = ece .encrypt
426
444
local = 'sender'
427
- remote = 'receiver'
428
445
inp = 'input'
429
446
outp = 'encrypted'
430
447
else :
431
448
func = ece .decrypt
432
449
local = 'receiver'
433
- remote = 'sender'
434
450
inp = 'encrypted'
435
451
outp = 'input'
436
452
@@ -445,7 +461,8 @@ def _run(self, mode):
445
461
if 'keys' in data :
446
462
key = None
447
463
decode_pub = ec .EllipticCurvePublicNumbers .from_encoded_point
448
- pubnum = decode_pub (ec .SECP256R1 (), b64d (data ['keys' ][local ]['public' ]))
464
+ pubnum = decode_pub (ec .SECP256R1 (),
465
+ b64d (data ['keys' ][local ]['public' ]))
449
466
d = 0
450
467
dbin = b64d (data ['keys' ][local ]['private' ])
451
468
for i in range (0 , len (dbin ), 4 ):
0 commit comments