Beispiel #1
0
def _cases_cipher(key_size, mult_keys, mult_state, mode='encrypt'):  # noqa
    if mult_keys:
        keys = np.random.randint(0,
                                 255, (number_of_keys, key_size),
                                 dtype='uint8')
        if mult_state:
            state = np.random.randint(0,
                                      255, (number_of_keys, 8),
                                      dtype='uint8')
            expected = np.empty((number_of_keys, 8), dtype='uint8')
            for i, key in enumerate(keys):
                if key_size == 8:
                    ciphertext = crypto_des.DESCipher(key)
                elif key_size == 16 or key_size == 24:
                    ciphertext = crypto_des3.DES3Cipher(key)
                expected[i] = np.frombuffer(getattr(ciphertext,
                                                    mode)(state[i]),
                                            dtype='uint8')
        else:
            state = np.random.randint(0, 255, (8), dtype='uint8')
            expected = np.empty((number_of_keys, 8), dtype='uint8')
            for i, key in enumerate(keys):
                if key_size == 8:
                    ciphertext = crypto_des.DESCipher(key)
                elif key_size == 16 or key_size == 24:
                    ciphertext = crypto_des3.DES3Cipher(key)
                expected[i] = np.frombuffer(getattr(ciphertext, mode)(state),
                                            dtype='uint8')
    else:
        keys = np.random.randint(0, 255, (key_size), dtype='uint8')
        if key_size == 8:
            ciphertext = crypto_des.DESCipher(keys)
        elif key_size == 16 or key_size == 24:
            ciphertext = crypto_des3.DES3Cipher(keys)
        if mult_state:
            state = np.random.randint(0,
                                      255, (number_of_keys, 8),
                                      dtype='uint8')
            expected = np.empty((number_of_keys, 8), dtype='uint8')
            for i, s in enumerate(state):
                expected[i] = np.frombuffer(getattr(ciphertext, mode)(s),
                                            dtype='uint8')
        else:
            state = np.random.randint(0, 255, (8), dtype='uint8')
            expected = np.frombuffer(getattr(ciphertext, mode)(state),
                                     dtype='uint8')
    return {
        'keys': keys,
        'state': state,
        'key_size': key_size,
        'expected': expected
    }
Beispiel #2
0
def test_get_master_key_retrieves_des_master_key_able_to_decrypt_the_ciphertext(
):
    des_key = np.random.randint(256, size=8, dtype=np.uint8)
    plaintext = np.random.randint(256, size=8, dtype=np.uint8)
    expected_ciphertext = des.encrypt(plaintext, des_key)
    des_key_schedule = des.key_schedule(des_key)

    for round_index, round_key in enumerate(des_key_schedule):
        found_master_key = des.get_master_key(round_key, round_index,
                                              plaintext, expected_ciphertext)
        assert found_master_key is not None
        plaintext_from_found_key = np.frombuffer(crypto_des.DESCipher(
            found_master_key).decrypt(expected_ciphertext),
                                                 dtype='uint8')
        assert np.array_equal(plaintext_from_found_key, plaintext)