def test_sign_encrypt_sign_and_verify_decrypt_verify(self):
        from PQencryption.pub_key.pk_signature.quantum_vulnerable import signing_Curve25519_PyNaCl
        from PQencryption.pub_key.pk_encryption.quantum_vulnerable import encryption_Curve25519_PyNaCl
        from PQencryption.symmetric_encryption import salsa20_256_PyNaCl
        from PQencryption import utilities
        import nacl.encoding

        signing_key, verify_key = signing_Curve25519_PyNaCl.key_gen()
        encryption_key = salsa20_256_PyNaCl.key_gen()

        message = 'This is my message.'

        signed_encrypted_signed_message = utilities.sign_encrypt_sign(
            message, signing_key, encryption_key)

        # verify positive
        verified_decrypted_verified_message = utilities.verify_decrypt_verify(
            signed_encrypted_signed_message, verify_key, encryption_key)
        self.assertEqual(message, verified_decrypted_verified_message)

        # verify negative
        # we should test all layers of the onion, not only the outer one. TODO
        with self.assertRaises(Exception) as bad_signature:
            spoof = "0" * len(
                nacl.encoding.HexEncoder.encode(
                    verified_decrypted_verified_message))
            verified_decrypted_verified_message = \
                    utilities.verify_decrypt_verify(spoof, verify_key,
                            encryption_key)
        self.assertTrue(
            "Signature was forged or corrupt" in bad_signature.exception)
    def test_symmetric_key_import_export(self, input):
        import os
        from PQencryption import utilities
        from PQencryption.symmetric_encryption import salsa20_256_PyNaCl
        s_raw = salsa20_256_PyNaCl.key_gen()
        symmetric_key_for_export_hex = utilities.to_hex(s_raw)
        path = ".tmp"
        s_header = ("# This is an encrypted symmetric key."
                    "KEEP IT PRIVATE!\n")
        s_name = "_PRIVATE_symmetric_key_CBS"

        utilities.export_key(symmetric_key_for_export_hex,
                             path,
                             s_name,
                             s_header,
                             key_type="SymmetricKey")

        symmetric_key_imported = utilities.import_key(path, s_name,
                                                      "SymmetricKey")

        os.remove(path + "/" + s_name)

        symmetric_key_imported_hex = utilities.to_hex(symmetric_key_imported)

        self.assertEqual(symmetric_key_for_export_hex,
                         symmetric_key_imported_hex)
    def test_symmetric_encryption_salsa20(self):
        from PQencryption.symmetric_encryption import salsa20_256_PyNaCl
        from PQencryption import utilities

        key = salsa20_256_PyNaCl.key_gen()
        message = 'This is my message.'

        # encryption
        my_encrypted_message = salsa20_256_PyNaCl.encrypt(message, key)

        # decryption
        my_decrypted_message = salsa20_256_PyNaCl.decrypt(
            my_encrypted_message, key)

        self.assertNotEqual(message, my_encrypted_message)
        self.assertEqual(message, my_decrypted_message)
    def test_generate_symmetric_key(self):
        from PQencryption import utilities
        from PQencryption.symmetric_encryption import salsa20_256_PyNaCl

        sym_raw = salsa20_256_PyNaCl.key_gen()
        symmetric_key_hex = utilities.to_hex(sym_raw)

        # Is it of the correct type?
        self.assertIsInstance(sym_raw, str)

        # Does it have the length correct?
        self.assertEqual(len(sym_raw), 32)

        # Is it properly converted to Hex?
        try:
            # if converting to "long, base16" works, it is Hex
            s_to_long = long(symmetric_key_hex, 16)
        except:
            raise ValueError("Key not converted to Hex format.")
Example #5
0
# encryption part
####################################
from PQencryption.pub_key.pk_signature.quantum_vulnerable import signing_Curve25519_PyNaCl
from PQencryption.pub_key.pk_encryption.quantum_vulnerable import encryption_Curve25519_PyNaCl
from PQencryption.symmetric_encryption import salsa20_256_PyNaCl
from PQencryption import utilities
import nacl.encoding
import base64

#create signing key
signing_key, verify_key = signing_Curve25519_PyNaCl.key_gen()

verifyBase64 = verify_key.encode(encoder=nacl.encoding.Base64Encoder)

#create symmetrical encryption key
encryption_key = salsa20_256_PyNaCl.key_gen()
encryptionKeyBase64 = base64.b64encode(encryption_key)

#read file
with open('input.json', 'r') as f:
    input = json.load(f)
fileStr = input['party_name']

myStr = open('/data/encrypted_%s.csv' %(fileStr), 'rb').read()

#sign->encrypt->sign procedure
signed_encrypted_signed_message = utilities.sign_encrypt_sign(myStr, signing_key, encryption_key)

#save encrypted file temporarily
text_file = open("/data/%s.enc" %(fileStr), "wb")
text_file.write(signed_encrypted_signed_message)