| 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 |
"""Self-test suite for Crypto.Cipher.Blowfish""" |
| 26 |
|
| 27 |
import unittest |
| 28 |
|
| 29 |
from Crypto.Util.py3compat import bchr |
| 30 |
|
| 31 |
from Crypto.Cipher import Blowfish |
| 32 |
|
| 33 |
|
| 34 |
test_data = [ |
| 35 |
|
| 36 |
('0000000000000000', '4ef997456198dd78', '0000000000000000'), |
| 37 |
('ffffffffffffffff', '51866fd5b85ecb8a', 'ffffffffffffffff'), |
| 38 |
('1000000000000001', '7d856f9a613063f2', '3000000000000000'), |
| 39 |
('1111111111111111', '2466dd878b963c9d', '1111111111111111'), |
| 40 |
('1111111111111111', '61f9c3802281b096', '0123456789abcdef'), |
| 41 |
('0123456789abcdef', '7d0cc630afda1ec7', '1111111111111111'), |
| 42 |
('0000000000000000', '4ef997456198dd78', '0000000000000000'), |
| 43 |
('0123456789abcdef', '0aceab0fc6a0a28d', 'fedcba9876543210'), |
| 44 |
('01a1d6d039776742', '59c68245eb05282b', '7ca110454a1a6e57'), |
| 45 |
('5cd54ca83def57da', 'b1b8cc0b250f09a0', '0131d9619dc1376e'), |
| 46 |
('0248d43806f67172', '1730e5778bea1da4', '07a1133e4a0b2686'), |
| 47 |
('51454b582ddf440a', 'a25e7856cf2651eb', '3849674c2602319e'), |
| 48 |
('42fd443059577fa2', '353882b109ce8f1a', '04b915ba43feb5b6'), |
| 49 |
('059b5e0851cf143a', '48f4d0884c379918', '0113b970fd34f2ce'), |
| 50 |
('0756d8e0774761d2', '432193b78951fc98', '0170f175468fb5e6'), |
| 51 |
('762514b829bf486a', '13f04154d69d1ae5', '43297fad38e373fe'), |
| 52 |
('3bdd119049372802', '2eedda93ffd39c79', '07a7137045da2a16'), |
| 53 |
('26955f6835af609a', 'd887e0393c2da6e3', '04689104c2fd3b2f'), |
| 54 |
('164d5e404f275232', '5f99d04f5b163969', '37d06bb516cb7546'), |
| 55 |
('6b056e18759f5cca', '4a057a3b24d3977b', '1f08260d1ac2465e'), |
| 56 |
('004bd6ef09176062', '452031c1e4fada8e', '584023641aba6176'), |
| 57 |
('480d39006ee762f2', '7555ae39f59b87bd', '025816164629b007'), |
| 58 |
('437540c8698f3cfa', '53c55f9cb49fc019', '49793ebc79b3258f'), |
| 59 |
('072d43a077075292', '7a8e7bfa937e89a3', '4fb05e1515ab73a7'), |
| 60 |
('02fe55778117f12a', 'cf9c5d7a4986adb5', '49e95d6d4ca229bf'), |
| 61 |
('1d9d5c5018f728c2', 'd1abb290658bc778', '018310dc409b26d6'), |
| 62 |
('305532286d6f295a', '55cb3774d13ef201', '1c587f1c13924fef'), |
| 63 |
('0123456789abcdef', 'fa34ec4847b268b2', '0101010101010101'), |
| 64 |
('0123456789abcdef', 'a790795108ea3cae', '1f1f1f1f0e0e0e0e'), |
| 65 |
('0123456789abcdef', 'c39e072d9fac631d', 'e0fee0fef1fef1fe'), |
| 66 |
('ffffffffffffffff', '014933e0cdaff6e4', '0000000000000000'), |
| 67 |
('0000000000000000', 'f21e9a77b71c49bc', 'ffffffffffffffff'), |
| 68 |
('0000000000000000', '245946885754369a', '0123456789abcdef'), |
| 69 |
('ffffffffffffffff', '6b5c5a9c5d9e0a5a', 'fedcba9876543210'), |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
('fedcba9876543210', 'be1e639408640f05', 'f0e1d2c3'), |
| 74 |
('fedcba9876543210', 'b39e44481bdb1e6e', 'f0e1d2c3b4'), |
| 75 |
('fedcba9876543210', '9457aa83b1928c0d', 'f0e1d2c3b4a5'), |
| 76 |
('fedcba9876543210', '8bb77032f960629d', 'f0e1d2c3b4a596'), |
| 77 |
('fedcba9876543210', 'e87a244e2cc85e82', 'f0e1d2c3b4a59687'), |
| 78 |
('fedcba9876543210', '15750e7a4f4ec577', 'f0e1d2c3b4a5968778'), |
| 79 |
('fedcba9876543210', '122ba70b3ab64ae0', 'f0e1d2c3b4a596877869'), |
| 80 |
('fedcba9876543210', '3a833c9affc537f6', 'f0e1d2c3b4a5968778695a'), |
| 81 |
('fedcba9876543210', '9409da87a90f6bf2', 'f0e1d2c3b4a5968778695a4b'), |
| 82 |
('fedcba9876543210', '884f80625060b8b4', 'f0e1d2c3b4a5968778695a4b3c'), |
| 83 |
('fedcba9876543210', '1f85031c19e11968', 'f0e1d2c3b4a5968778695a4b3c2d'), |
| 84 |
('fedcba9876543210', '79d9373a714ca34f', 'f0e1d2c3b4a5968778695a4b3c2d1e'), |
| 85 |
('fedcba9876543210', '93142887ee3be15c', |
| 86 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f'), |
| 87 |
('fedcba9876543210', '03429e838ce2d14b', |
| 88 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f00'), |
| 89 |
('fedcba9876543210', 'a4299e27469ff67b', |
| 90 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f0011'), |
| 91 |
('fedcba9876543210', 'afd5aed1c1bc96a8', |
| 92 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f001122'), |
| 93 |
('fedcba9876543210', '10851c0e3858da9f', |
| 94 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f00112233'), |
| 95 |
('fedcba9876543210', 'e6f51ed79b9db21f', |
| 96 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f0011223344'), |
| 97 |
('fedcba9876543210', '64a6e14afd36b46f', |
| 98 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f001122334455'), |
| 99 |
('fedcba9876543210', '80c7d7d45a5479ad', |
| 100 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f00112233445566'), |
| 101 |
('fedcba9876543210', '05044b62fa52d080', |
| 102 |
'f0e1d2c3b4a5968778695a4b3c2d1e0f0011223344556677'), |
| 103 |
] |
| 104 |
|
| 105 |
|
| 106 |
class KeyLength(unittest.TestCase): |
| 107 |
|
| 108 |
def runTest(self): |
| 109 |
self.assertRaises(ValueError, Blowfish.new, bchr(0) * 3, |
| 110 |
Blowfish.MODE_ECB) |
| 111 |
self.assertRaises(ValueError, Blowfish.new, bchr(0) * 57, |
| 112 |
Blowfish.MODE_ECB) |
| 113 |
|
| 114 |
|
| 115 |
class TestOutput(unittest.TestCase): |
| 116 |
|
| 117 |
def runTest(self): |
| 118 |
|
| 119 |
|
| 120 |
cipher = Blowfish.new(b'4'*16, Blowfish.MODE_ECB) |
| 121 |
|
| 122 |
pt = b'5' * 16 |
| 123 |
ct = cipher.encrypt(pt) |
| 124 |
|
| 125 |
output = bytearray(16) |
| 126 |
res = cipher.encrypt(pt, output=output) |
| 127 |
self.assertEqual(ct, output) |
| 128 |
self.assertEqual(res, None) |
| 129 |
|
| 130 |
res = cipher.decrypt(ct, output=output) |
| 131 |
self.assertEqual(pt, output) |
| 132 |
self.assertEqual(res, None) |
| 133 |
|
| 134 |
output = memoryview(bytearray(16)) |
| 135 |
cipher.encrypt(pt, output=output) |
| 136 |
self.assertEqual(ct, output) |
| 137 |
|
| 138 |
cipher.decrypt(ct, output=output) |
| 139 |
self.assertEqual(pt, output) |
| 140 |
|
| 141 |
self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16) |
| 142 |
self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16) |
| 143 |
|
| 144 |
shorter_output = bytearray(7) |
| 145 |
self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output) |
| 146 |
self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output) |
| 147 |
|
| 148 |
|
| 149 |
def get_tests(config={}): |
| 150 |
from .common import make_block_tests |
| 151 |
tests = make_block_tests(Blowfish, "Blowfish", test_data) |
| 152 |
tests.append(KeyLength()) |
| 153 |
tests += [TestOutput()] |
| 154 |
return tests |
| 155 |
|
| 156 |
|
| 157 |
if __name__ == '__main__': |
| 158 |
import unittest |
| 159 |
suite = lambda: unittest.TestSuite(get_tests()) |
| 160 |
unittest.main(defaultTest='suite') |