def test_get_fingerprint(self): """ Test that PublicKey.get_fingerprint() always returns the same fingerprint for the same PublicKey object and a different one for another object. """ # Create two public keys from the same cryptosystem public_key1 = self.cryptosystem.new_key_pair().public_key public_key2 = self.cryptosystem.new_key_pair().public_key # And a third from a different (insecure, 128bit) cryptosystem insecure_cryptosys = EGCryptoSystem.new(nbits=128) public_key3 = insecure_cryptosys.new_key_pair().public_key # Get each key's fingerprint pk1_fingerprint = public_key1.get_fingerprint() pk2_fingerprint = public_key2.get_fingerprint() pk3_fingerprint = public_key3.get_fingerprint() # Test that multiple calls to get_fingerprint() for the same PublicKey # object always return the same fingerprint for i in range(0, 20): self.assertEqual(public_key1.get_fingerprint(), pk1_fingerprint) self.assertEqual(public_key2.get_fingerprint(), pk2_fingerprint) self.assertEqual(public_key3.get_fingerprint(), pk3_fingerprint) # Check that fingerprints from different PublicKey objects are # different self.assertFalse(pk1_fingerprint == pk2_fingerprint) self.assertFalse(pk1_fingerprint == pk3_fingerprint) self.assertFalse(pk2_fingerprint == pk3_fingerprint)
def test_decryption_incompatible_cyphertext_error_cryptosys_bits(self): """ Test that attempting to decrypt a ciphertext with a private key generated using a cryptosystem of a different bit size (nbits) than the one of the public key used to encrypt the ciphertext raises an IncompatibleCiphertextError exception. """ # For coverage completeness (A different error message is raised when # the problem is an incompatible size for the cryptosystem than for any # other incompatible private key). # Use the public key to encrypt the message ciphertext = self.public_key.encrypt_text(self.message) # Construct a new 128 bits (terribly unsafe) cryptosystem other_cryptosys = EGCryptoSystem.new(nbits=128) # Generate a new key pair using this cryptosystem and take its private # key other_key_pair = other_cryptosys.new_key_pair() other_private_key = other_key_pair.private_key # This key should no be capable of decrypting the ciphertext and an # error should be raised self.assertRaises(IncompatibleCiphertextError, other_private_key.decrypt_to_text, ciphertext)
def run_tool(nbits, filename, name, description): """ Runs the plonevote.gen_cryptosys tool and generates a new cryptosystem. """ # Define callbacks for the TaskMonitor for progress monitoring def cb_task_start(task): print task.task_name + ":" def cb_task_progress(task): sys.stdout.write(".") sys.stdout.flush() def cb_task_end(task): print "" # Create new TaskMonitor and register the callbacks taskmon = TaskMonitor() taskmon.add_on_task_start_callback(cb_task_start) taskmon.add_on_tick_callback(cb_task_progress) taskmon.add_on_task_end_callback(cb_task_end) # Generate a new cryptosystem of the requested size try: cryptosys = EGCryptoSystem.new(nbits, task_monitor=taskmon) except KeyLengthTooLowError: print "ERROR: The given bit size does not meet PloneVoteCryptoLib "\ "minimum security requirements (too short)." except KeyLengthNonBytableError: print "ERROR: The given bit size must be a multiple of 8." # Save the cryptosystem to file print "\nSaving cryptosystem to %s..." % filename, cryptosys.to_file(name, description, filename) print "SAVED.\n"
def run_tool(nbits, filename, name, description): """ Runs the plonevote.gen_cryptosys tool and generates a new cryptosystem. """ # Define callbacks for the TaskMonitor for progress monitoring def cb_task_start(task): print task.task_name + ":" def cb_task_progress(task): sys.stdout.write(".") sys.stdout.flush() def cb_task_end(task): print "" # Create new TaskMonitor and register the callbacks taskmon = TaskMonitor() taskmon.add_on_task_start_callback(cb_task_start) taskmon.add_on_tick_callback(cb_task_progress) taskmon.add_on_task_end_callback(cb_task_end) # Generate a new cryptosystem of the requested size try: cryptosys = EGCryptoSystem.new(nbits, task_monitor = taskmon) except KeyLengthTooLowError: print "ERROR: The given bit size does not meet PloneVoteCryptoLib "\ "minimum security requirements (too short)." except KeyLengthNonBytableError: print "ERROR: The given bit size must be a multiple of 8." # Save the cryptosystem to file print "\nSaving cryptosystem to %s..." % filename, cryptosys.to_file(name, description, filename) print "SAVED.\n"
# -*- coding: utf-8 -*- # ToDo: Turn this into an actual test case / doctest from __future__ import absolute_import from __future__ import print_function from plonevotecryptolib.EGCryptoSystem import EGCryptoSystem as egcs from plonevotecryptolib.Threshold.ThresholdEncryptionSetUp import ThresholdEncryptionSetUp as tesu from plonevotecryptolib.Threshold.ThresholdDecryptionCombinator import ThresholdDecryptionCombinator from six.moves import range cs = egcs.new() class Trustee: def __init__(self, cs): kp = cs.new_key_pair() self.private_key = kp.private_key self.public_key = kp.public_key self.commitment = None self.tesu_fingerprint = None self.threshold_public_key = None self.threshold_private_key = None trustees = [Trustee(cs) for i in range(0,5)] tesetup = tesu(cs, 5, 3) for i in range(0,5): tesetup.add_trustee_public_key(i, trustees[i].public_key) for i in range(0,5):
# -*- coding: utf-8 -*- # ToDo: Turn this into an actual test case / doctest from plonevotecryptolib.EGCryptoSystem import EGCryptoSystem as egcs from plonevotecryptolib.Threshold.ThresholdEncryptionSetUp import ThresholdEncryptionSetUp as tesu from plonevotecryptolib.Threshold.ThresholdDecryptionCombinator import ThresholdDecryptionCombinator cs = egcs.new() class Trustee: def __init__(self, cs): kp = cs.new_key_pair() self.private_key = kp.private_key self.public_key = kp.public_key self.commitment = None self.tesu_fingerprint = None self.threshold_public_key = None self.threshold_private_key = None trustees = [Trustee(cs) for i in range(0,5)] tesetup = tesu(cs, 5, 3) for i in range(0,5): tesetup.add_trustee_public_key(i, trustees[i].public_key) for i in range(0,5): trustees[i].commitment = tesetup.generate_commitment() for i in range(0,5):