summaryrefslogtreecommitdiff
path: root/src/pycryptopp/cipher/aes.py
blob: 7280063801f4d4e233f71f96c263afab59d26538 (plain)
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

from pycryptopp import _import_my_names

# These initializations to None are just to pacify pyflakes, which
# doesn't understand that we have to do some funky import trickery
# below in _import_my_names() in order to get sensible namespaces.
AES=None
Error=None

_import_my_names(globals(), "aes_")

del _import_my_names

def start_up_self_test():
    """
    This is a quick test intended to detect major errors such as the library being
    miscompiled and segfaulting or returning incorrect answers.  We've had problems
    of that kind many times, thus justifying running this self-test on import.
    This idea was suggested to me by the second edition of "Practical
    Cryptography" by Ferguson, Schneier, and Kohno.
    These tests were copied from pycryptopp/test/test_aes.py on 2009-10-30.
    """
    enc0 = "dc95c078a2408989ad48a21492842087530f8afbc74536b9a963b4f1c4cb738b"
    from binascii import a2b_hex, b2a_hex

    cryptor = AES(key="\x00"*32)
    ct = cryptor.process("\x00"*32)
    if enc0 != b2a_hex(ct):
        raise Error("pycryptopp failed startup self-test. Please run pycryptopp unit tests.")

    cryptor = AES(key="\x00"*32)
    ct1 = cryptor.process("\x00"*15)
    ct2 = cryptor.process("\x00"*17)
    if enc0 != b2a_hex(ct1+ct2):
        raise Error("pycryptopp failed startup self-test. Please run pycryptopp unit tests.")

    enc0 = "66e94bd4ef8a2c3b884cfa59ca342b2e"
    cryptor = AES(key="\x00"*16)
    ct = cryptor.process("\x00"*16)
    if enc0 != b2a_hex(ct):
        raise Error("pycryptopp failed startup self-test. Please run pycryptopp unit tests.")

    cryptor = AES(key="\x00"*16)
    ct1 = cryptor.process("\x00"*8)
    ct2 = cryptor.process("\x00"*8)
    if enc0 != b2a_hex(ct1+ct2):
        raise Error("pycryptopp failed startup self-test. Please run pycryptopp unit tests.")

    def _test_from_Niels_AES(keysize, result):
        def fake_ecb_using_ctr(k, p):
            return AES(key=k, iv=p).process('\x00'*16)

        E = fake_ecb_using_ctr
        b = 16
        k = keysize
        S = '\x00' * (k+b)
        for i in range(1000):
            K = S[-k:]
            P = S[-k-b:-k]
            S += E(K, E(K, P))

        if S[-b:] != a2b_hex(result):
            raise Error("pycryptopp failed startup self-test. Please run pycryptopp unit tests.")

    _test_from_Niels_AES(16, 'bd883f01035e58f42f9d812f2dacbcd8')
    _test_from_Niels_AES(32, 'c84b0f3a2c76dd9871900b07f09bdd3e')

start_up_self_test()