@@ -1265,16 +1265,39 @@ def runTest(self):
1265
1265
self .assertEqual (SHA256 .new (ct ).hexdigest (), expected )
1266
1266
1267
1267
1268
+ class TestIncompleteBlocks (unittest .TestCase ):
1269
+
1270
+ def __init__ (self , use_aesni ):
1271
+ unittest .TestCase .__init__ (self )
1272
+ self .use_aesni = use_aesni
1273
+
1274
+ def runTest (self ):
1275
+ # Encrypt data with length not multiple of 16 bytes
1276
+
1277
+ cipher = AES .new (b'4' * 16 , AES .MODE_ECB , use_aesni = self .use_aesni )
1278
+
1279
+ for msg_len in range (1 , 16 ):
1280
+ self .assertRaises (ValueError , cipher .encrypt , b'1' * msg_len )
1281
+ self .assertRaises (ValueError , cipher .encrypt , b'1' * (msg_len + 16 ))
1282
+ self .assertRaises (ValueError , cipher .decrypt , b'1' * msg_len )
1283
+ self .assertRaises (ValueError , cipher .decrypt , b'1' * (msg_len + 16 ))
1284
+
1285
+ self .assertEqual (cipher .encrypt (b'' ), b'' )
1286
+ self .assertEqual (cipher .decrypt (b'' ), b'' )
1287
+
1288
+
1268
1289
def get_tests (config = {}):
1269
1290
from Crypto .Util import _cpu_features
1270
1291
from common import make_block_tests
1271
1292
1272
1293
tests = make_block_tests (AES , "AES" , test_data , {'use_aesni' : False })
1273
1294
tests += [ TestMultipleBlocks (False ) ]
1295
+ tests += [ TestIncompleteBlocks (False ) ]
1274
1296
if _cpu_features .have_aes_ni ():
1275
1297
# Run tests with AES-NI instructions if they are available.
1276
1298
tests += make_block_tests (AES , "AESNI" , test_data , {'use_aesni' : True })
1277
1299
tests += [ TestMultipleBlocks (True ) ]
1300
+ tests += [ TestIncompleteBlocks (True ) ]
1278
1301
else :
1279
1302
print "Skipping AESNI tests"
1280
1303
return tests
0 commit comments