<filename>tests/tests.py
# Copyright (c) 2020 <NAME> open source software. This file is licensed under the MIT license. #

from kortex import kdf, ksp, utils, exceptions
import unittest
import os


# ------------------------------- VECTORS -------------------------------------
scrypt_vectors = [
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 16384,
            "r": 8,
            "p": 1,
            "expected": "34005bb40231b<PASSWORD>2078<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 32768,
            "r": 8,
            "p": 1,
            "expected": "c9f78106c7cca73711<PASSWORD>5<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 65536,
            "r": 8,
            "p": 1,
            "expected": "<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 131072,
            "r": 8,
            "p": 1,
            "expected": "371dd814edfc9d3<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 262144,
            "r": 8,
            "p": 1,
            "expected": "<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 524288,
            "r": 8,
            "p": 1,
            "expected": "<PASSWORD>"
        }
    ]
# -----------------------------------------------------------------------------


# ------------------------------- TESTS ---------------------------------------
class TestKortexKDF(unittest.TestCase):
    
    def test_scrypt(self):
        for case in scrypt_vectors:
            password = bytes.fromhex(case['password'])
            salt = bytes.fromhex(case['salt'])
            n = case['n']
            r = case['r']
            p = case['p']
            expected_val = bytes.fromhex(case['expected'])
            
            key = kdf.derive(password, salt, len(expected_val), n, r, p)
            
            self.assertEqual(key, expected_val, 'scrypt test failed')


class TestKoalaSecurityProtocols(unittest.TestCase):
    
    def test_ksp1(self):
        for i in range(2):
            key, nonce, mac_key = [os.urandom(16) for _ in range(3)]
            
            cipher = ksp.KSP1(key, nonce, mac_key)
            plaintext = b'The quick planet runs circles around the lazy star'
            
            # Encrypt
            encryptor = cipher.encryptor()
            ctext = encryptor.update(plaintext)
            tag = encryptor.finalize()
            
            # Decrypt
            decryptor = cipher.decryptor()
            ptext = decryptor.update(ctext)
            
            try:
                decryptor.finalize(tag)
            except exceptions.InvalidToken:
                self.fail('Verification failed. Please submit a bug report at github.com/fluffykoalas/kortex')
            
            self.assertEqual(ptext, plaintext)


class TestUtils(unittest.TestCase):
    
    def test_type_check_pass(self):
        
        try:
            utils.check_type(
                str,
                stringval1='string',
                stringval2='another string',
                still_a_string='still a string'
            )
        except TypeError:
            self.fail('utils.check_type raised unexpected TypeError')
        
    def test_type_check_fail(self):
        
        with self.assertRaises(TypeError):
            utils.check_type(
                bytes,
                notbytes='this is a string not a byte string',
                alsonotbytes=123456789
            )

<filename>tests/tests.py
# Copyright (c) 2020 <NAME> open source software. This file is licensed under the MIT license. #

from kortex import kdf, ksp, utils, exceptions
import unittest
import os


# ------------------------------- VECTORS -------------------------------------
scrypt_vectors = [
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 16384,
            "r": 8,
            "p": 1,
            "expected": "34005bb40231b<PASSWORD>2078<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 32768,
            "r": 8,
            "p": 1,
            "expected": "c9f78106c7cca73711<PASSWORD>5<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 65536,
            "r": 8,
            "p": 1,
            "expected": "<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 131072,
            "r": 8,
            "p": 1,
            "expected": "371dd814edfc9d3<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 262144,
            "r": 8,
            "p": 1,
            "expected": "<PASSWORD>"
        },
        {
            "password": "<PASSWORD>",
            "salt": "<PASSWORD>",
            "n": 524288,
            "r": 8,
            "p": 1,
            "expected": "<PASSWORD>"
        }
    ]
# -----------------------------------------------------------------------------


# ------------------------------- TESTS ---------------------------------------
class TestKortexKDF(unittest.TestCase):
    
    def test_scrypt(self):
        for case in scrypt_vectors:
            password = bytes.fromhex(case['password'])
            salt = bytes.fromhex(case['salt'])
            n = case['n']
            r = case['r']
            p = case['p']
            expected_val = bytes.fromhex(case['expected'])
            
            key = kdf.derive(password, salt, len(expected_val), n, r, p)
            
            self.assertEqual(key, expected_val, 'scrypt test failed')


class TestKoalaSecurityProtocols(unittest.TestCase):
    
    def test_ksp1(self):
        for i in range(2):
            key, nonce, mac_key = [os.urandom(16) for _ in range(3)]
            
            cipher = ksp.KSP1(key, nonce, mac_key)
            plaintext = b'The quick planet runs circles around the lazy star'
            
            # Encrypt
            encryptor = cipher.encryptor()
            ctext = encryptor.update(plaintext)
            tag = encryptor.finalize()
            
            # Decrypt
            decryptor = cipher.decryptor()
            ptext = decryptor.update(ctext)
            
            try:
                decryptor.finalize(tag)
            except exceptions.InvalidToken:
                self.fail('Verification failed. Please submit a bug report at github.com/fluffykoalas/kortex')
            
            self.assertEqual(ptext, plaintext)


class TestUtils(unittest.TestCase):
    
    def test_type_check_pass(self):
        
        try:
            utils.check_type(
                str,
                stringval1='string',
                stringval2='another string',
                still_a_string='still a string'
            )
        except TypeError:
            self.fail('utils.check_type raised unexpected TypeError')
        
    def test_type_check_fail(self):
        
        with self.assertRaises(TypeError):
            utils.check_type(
                bytes,
                notbytes='this is a string not a byte string',
                alsonotbytes=123456789
            )

# Copyright (c) 2020 <NAME> open source software. This file is licensed under the MIT license. # # ------------------------------- VECTORS ------------------------------------- # ----------------------------------------------------------------------------- # ------------------------------- TESTS --------------------------------------- # Encrypt # Decrypt