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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import pytest
from unicrypto import get_cipher_by_name
from unicrypto import symmetric
des_ecb = [
('0000000000000000', '0000000000000000', '8CA64DE9C1B123A7'),
('FFFFFFFFFFFFFFFF', 'FFFFFFFFFFFFFFFF', '7359B2163E4EDC58'),
('3000000000000000', '1000000000000001', '958E6E627A05557B'),
('1111111111111111', '1111111111111111', 'F40379AB9E0EC533'),
('0123456789ABCDEF', '1111111111111111', '17668DFC7292532D'),
('1111111111111111', '0123456789ABCDEF', '8A5AE1F81AB8F2DD'),
('FEDCBA9876543210', '0123456789ABCDEF', 'ED39D950FA74BCC4'),
('FEDCBA9876543210', '0123456789ABCDEF0123456789ABCDEF', 'ED39D950FA74BCC4ED39D950FA74BCC4'),
]
des_cbc = [
('0123456789abcdef','fedcba9876543210' , '37363534333231204E6F77206973207468652074696D6520', 'ccd173ffab2039f4acd8aefddfd8a1eb468e91157888ba68'),
]
def ecb_enc(cipherobj:symmetric.symmetricBASE, vector):
for i, res in enumerate(vector):
key, plaintext, ciphertext = res
plaintext = bytes.fromhex(plaintext)
key = bytes.fromhex(key)
ciphertext = bytes.fromhex(ciphertext)
ctx = cipherobj(key)
enc_data = ctx.encrypt(plaintext)
if enc_data != ciphertext:
raise Exception('Ciphertext doesnt match to vector! DES %s Cipher: \r\n%s \r\nVector: \r\n%s' % (i, enc_data, ciphertext))
ctx = cipherobj(key)
dec_data = ctx.decrypt(enc_data)
if dec_data != plaintext:
raise Exception('Decrypted data doesnt match plaintext! DES-ECB Cipher: \r\n%s \r\nPlaintext: \r\n%s' % (dec_data.hex(), plaintext.hex()))
return True
def cbc_enc(cipherobj:symmetric.symmetricBASE, vector):
for i, res in enumerate(vector):
key, iv, plaintext, ciphertext = res
plaintext = bytes.fromhex(plaintext)
key = bytes.fromhex(key)
ciphertext = bytes.fromhex(ciphertext)
iv = bytes.fromhex(iv)
ctx = cipherobj(key, symmetric.MODE_CBC, iv)
enc_data = ctx.encrypt(plaintext)
if enc_data != ciphertext:
raise Exception('Ciphertext doesnt match to vector! DES %s Cipher: \r\n%s \r\nVector: \r\n%s' % (i, enc_data, ciphertext))
ctx = cipherobj(key, symmetric.MODE_CBC, iv)
dec_data = ctx.decrypt(enc_data)
if dec_data != plaintext:
raise Exception('Decrypted data doesnt match plaintext! DES-CBC Cipher: \r\n%s \r\nPlaintext: \r\n%s' % (dec_data.hex(), plaintext.hex()))
return True
@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'pure')])
def test_ecb(cipherobj):
from unicrypto.backends.pure import DES
assert isinstance(cipherobj, DES)
ecb_enc(cipherobj, des_ecb)
@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'pure')])
def test_cbc(cipherobj):
cbc_enc(cipherobj, des_cbc)
#@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'crypto')])
#def test_ecb(cipherobj):
# ecb_enc(cipherobj, des_ecb)
#
#@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'crypto')])
#def test_cbc(cipherobj):
# cbc_enc(cipherobj, des_cbc)
@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'pycryptodome')])
def test_ecb(cipherobj):
ecb_enc(cipherobj, des_ecb)
@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'pycryptodome')])
def test_cbc(cipherobj):
cbc_enc(cipherobj, des_cbc)
@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'cryptography')])
def test_ecb(cipherobj):
ecb_enc(cipherobj, des_ecb)
@pytest.mark.parametrize("cipherobj", [get_cipher_by_name('DES', 'cryptography')])
def test_cbc(cipherobj):
cbc_enc(cipherobj, des_cbc)
|