def test_encryption_invalid_nonce(random_key, random_nonce, random_plaintext,
                                  random_associated):
    with pytest.raises(ValueError):
        spectral.encrypt_disjoint(random_key, random_nonce[:-1],
                                  random_plaintext, random_associated)

    with pytest.raises(ValueError):
        spectral.encrypt_disjoint(random_key, random_nonce + b"\0",
                                  random_plaintext, random_associated)
def test_encryption_invalid_key(random_nonce, random_plaintext,
                                random_associated):
    with pytest.raises(ValueError):
        spectral.encrypt_disjoint(b"\0" * (spectral.KEY_SIZE - 1),
                                  random_nonce, random_plaintext,
                                  random_associated)

    with pytest.raises(ValueError):
        spectral.encrypt_disjoint(b"\0" * (spectral.KEY_SIZE + 1),
                                  random_nonce, random_plaintext,
                                  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)
Esempio n. 4
0
def test_encryption(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_ciphertext, computed_mac = spectral.encrypt_disjoint(
            key, nonce, plaintext, associated)

        assert computed_ciphertext == ciphertext
        assert computed_mac == mac
Esempio n. 5
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)
Esempio n. 6
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_encryption_invalid_key_type(random_nonce, random_plaintext,
                                     random_associated):
    with pytest.raises(TypeError):
        spectral.encrypt_disjoint("", random_nonce, random_plaintext,
                                  random_associated)
def test_encryption_invalid_associated(random_key, random_nonce,
                                       random_plaintext):
    with pytest.raises(ValueError):
        spectral.encrypt_disjoint(random_key, random_nonce, random_plaintext,
                                  MockedLengthBytes())
def test_encryption_disjoint(random_key, random_nonce, random_plaintext,
                             random_associated):
    spectral.encrypt_disjoint(random_key, random_nonce, random_plaintext,
                              random_associated)