Exemplo n.º 1
0
def test_decryption_invalid_mac(random_key, random_nonce, random_ciphertext,
                                random_mac, random_associated):
    with pytest.raises(ValueError):
        spectral.decrypt_disjoint(random_key, random_nonce, random_ciphertext,
                                  random_mac[:-1], random_associated)

    with pytest.raises(ValueError):
        spectral.decrypt_disjoint(random_key, random_nonce, random_ciphertext,
                                  random_mac + b"\0", random_associated)
Exemplo n.º 2
0
def test_disjoint_mac(execution, random_key, random_nonce, random_plaintext,
                      random_associated):
    ciphertext, mac = spectral.encrypt_disjoint(random_key, random_nonce,
                                                random_plaintext,
                                                random_associated)

    mac = bytearray(mac)
    flip_index = random.randint(0, len(mac) - 1)
    mac[flip_index] = 255 - mac[flip_index]
    mac = bytes(mac)

    with pytest.raises(RuntimeError):
        spectral.decrypt_disjoint(random_key, random_nonce, ciphertext, mac,
                                  random_associated)
Exemplo n.º 3
0
def test_disjoint_ciphertext(execution, random_key, random_nonce,
                             random_plaintext, random_associated):
    ciphertext, mac = spectral.encrypt_disjoint(random_key, random_nonce,
                                                random_plaintext,
                                                random_associated)

    ciphertext = bytearray(ciphertext)
    flip_index = random.randint(0, len(ciphertext) - 1)
    ciphertext[flip_index] = 255 - ciphertext[flip_index]
    ciphertext = bytes(ciphertext)

    with pytest.raises(RuntimeError):
        spectral.decrypt_disjoint(random_key, random_nonce, ciphertext, mac,
                                  random_associated)
def test_decryption_disjoint(random_key, random_nonce, random_plaintext,
                             random_associated):
    ciphertext, mac = spectral.encrypt_disjoint(random_key, random_nonce,
                                                random_plaintext,
                                                random_associated)
    assert random_plaintext == spectral.decrypt_disjoint(
        random_key, random_nonce, ciphertext, mac, random_associated)
Exemplo n.º 5
0
def test_decryption(cases):
    for key, nonce, plaintext, associated, ciphertext, mac in cases:
        key, nonce, plaintext, associated, ciphertext, mac = map(
            binascii.unhexlify,
            (key, nonce, plaintext, associated, ciphertext, mac))

        computed_plaintext = spectral.decrypt_disjoint(key, nonce, ciphertext,
                                                       mac, associated)

        assert computed_plaintext == plaintext
Exemplo n.º 6
0
def test_decryption_invalid_mac_type(random_key, random_nonce,
                                     random_ciphertext, random_associated):
    with pytest.raises(TypeError):
        spectral.decrypt_disjoint(random_key, random_nonce, random_ciphertext,
                                  "", random_associated)
Exemplo n.º 7
0
def test_decryption_invalid_associated(random_key, random_nonce,
                                       random_ciphertext, random_mac):
    with pytest.raises(ValueError):
        spectral.decrypt_disjoint(random_key, random_nonce, random_ciphertext,
                                  random_mac, MockedLengthBytes())