def get_cryptosystem():
    """
    This function returns a predetermined EGCryptoSystem object.
    
    The EGCryptoSystem object is loaded from a resource file and guaranteed to 
    be always the same, at least for same execution of the test suite. The 
    cryptosystem is also cached in memory for quicker access.
    """
    global _cryptosys
    # Check if we have already loaded a cryptosystem for this test run
    if (_cryptosys == None):
        # If not, load it now:
        # Construct the path to the cryptosystem test resource file
        cryptosys_file = os.path.join(
            os.path.dirname(__file__),
            "TestThresholdEncryptionSetUp.resources",
            "test1024bits.pvcryptosys")

        # Load the cryptosystem from file
        _cryptosys = EGCryptoSystem.from_file(cryptosys_file)

    # Return the cached cryptosystem
    # (Note: this is the original reference, not a deepcopy, tests using the
    #  cryptosystem object should treat it as read-only to preserve isolation)
    return _cryptosys
    def test_commitmet_adding(self):
        """
        Creates a new ThresholdEncryptionSetUp and try to add a trustee c
        commitment        
        """
        cryptosystem = get_cryptosystem()
        commitments = []

        # Generate a new instance of ThresholdEncryptionSetUp
        tSetUp = ThresholdEncryptionSetUp(cryptosystem, self.num_trustees,
                                          self.threshold)

        # Adding the keys from trustees
        for i in range(self.num_trustees):
            tSetUp.add_trustee_public_key(i, self.trustees[i].public_key)

        # Generate and add a valid commitment
        commitment = tSetUp.generate_commitment()
        tSetUp.add_trustee_commitment(0, commitment)

        #The commitment for trustee 0 must be the same we just add
        self.assertEqual(tSetUp._trustees_commitments[0], commitment)

        # add_trustee_commitmnet must raise ValueError with invalid trustee
        # values (-1 and self.num_trustees+1)
        self.assertRaises(ValueError, tSetUp.add_trustee_commitment, -1,
                          commitment)
        self.assertRaises(ValueError, tSetUp.add_trustee_commitment,
                          self.num_trustees + 1, commitment)

        # Change commitment.num_trustees value to try erros
        commitment.num_trustees = self.num_trustees + 1
        # add_trustee_commitmnet must raise IncompatibleCommitmentError when
        # the commitment has diferent num_trustees value
        self.assertRaises(IncompatibleCommitmentError,
                          tSetUp.add_trustee_commitment, 1, commitment)

        # Change commitment.threshold value to try erros
        commitment.num_trustees = self.num_trustees
        commitment.threshold = self.threshold + 1
        # add_trustee_commitmnet must raise IncompatibleCommitmentError when
        # the commitment has diferent theshold  value
        self.assertRaises(IncompatibleCommitmentError,
                          tSetUp.add_trustee_commitment, 1, commitment)

        # Test what happens with invalid cryptosystem
        # Create another cryptosystem
        second_cryptosys_file = os.path.join(
            os.path.dirname(__file__),
            "TestThresholdEncryptionSetUp.resources",
            "test1024bits_second.pvcryptosys")
        # Load the cryptosystem from file
        second_cryptosys = EGCryptoSystem.from_file(second_cryptosys_file)
        commitment.threshold = self.threshold
        commitment.cryptosystem = second_cryptosys
        # Must raise IncompatibleCommitmentError with diferent cryptosys
        self.assertRaises(IncompatibleCommitmentError,
                          tSetUp.add_trustee_commitment, 1, commitment)
    def test_commitmet_adding(self):    
        """
        Creates a new ThresholdEncryptionSetUp and try to add a trustee c
        commitment        
        """
        cryptosystem = get_cryptosystem()
        commitments = []     
           
        # Generate a new instance of ThresholdEncryptionSetUp
        tSetUp = ThresholdEncryptionSetUp(cryptosystem, 
                                          self.num_trustees, self.threshold)
                                         
        # Adding the keys from trustees
        for i in range(self.num_trustees):
            tSetUp.add_trustee_public_key(i, self.trustees[i].public_key)
        
        # Generate and add a valid commitment
        commitment = tSetUp.generate_commitment()   
        tSetUp.add_trustee_commitment(0, commitment)
        
        #The commitment for trustee 0 must be the same we just add
        self.assertEqual(tSetUp._trustees_commitments[0], commitment)
        
        # add_trustee_commitmnet must raise ValueError with invalid trustee  
        # values (-1 and self.num_trustees+1)
        self.assertRaises(ValueError, tSetUp.add_trustee_commitment,
                                     -1, commitment)
        self.assertRaises(ValueError, tSetUp.add_trustee_commitment, 
                                      self.num_trustees+1, commitment)
                                      
        # Change commitment.num_trustees value to try erros
        commitment.num_trustees = self.num_trustees +1
        # add_trustee_commitmnet must raise IncompatibleCommitmentError when
        # the commitment has diferent num_trustees value
        self.assertRaises(IncompatibleCommitmentError, 
                          tSetUp.add_trustee_commitment, 1, commitment)
                          

        # Change commitment.threshold value to try erros
        commitment.num_trustees = self.num_trustees
        commitment.threshold = self.threshold+1
        # add_trustee_commitmnet must raise IncompatibleCommitmentError when
        # the commitment has diferent theshold  value
        self.assertRaises(IncompatibleCommitmentError, 
                          tSetUp.add_trustee_commitment, 1, commitment)
                          
        # Test what happens with invalid cryptosystem
        # Create another cryptosystem
        second_cryptosys_file = os.path.join(os.path.dirname(__file__), 
                                      "TestThresholdEncryptionSetUp.resources",
                                      "test1024bits_second.pvcryptosys")
        # Load the cryptosystem from file
        second_cryptosys = EGCryptoSystem.from_file(second_cryptosys_file)
        commitment.threshold = self.threshold
        commitment.cryptosystem = second_cryptosys
        # Must raise IncompatibleCommitmentError with diferent cryptosys  
        self.assertRaises(IncompatibleCommitmentError, 
                          tSetUp.add_trustee_commitment, 1, commitment)
Example #4
0
def get_cryptosystem():
    """
    This function returns a predetermined EGCryptoSystem object.
    
    The EGCryptoSystem object is loaded from a resource file and guaranteed to 
    be always the same, at least for same execution of the test suite. The 
    cryptosystem is also cached in memory for quicker access.
    """
    global _cryptosys
    # Check if we have already loaded a cryptosystem for this test run
    if(_cryptosys == None):
        # If not, load it now:
        # Construct the path to the cryptosystem test resource file
        cryptosys_file = os.path.join(os.path.dirname(__file__), 
                                      "TestBasicEncryption.resources",
                                      "test1024bits.pvcryptosys")
        
        # Load the cryptosystem from file
        _cryptosys = EGCryptoSystem.from_file(cryptosys_file)
    
    # Return the cached cryptosystem 
    # (Note: this is the original reference, not a deepcopy, tests using the 
    #  cryptosystem object should treat it as read-only to preserve isolation)
    return _cryptosys
    def test_partial_decryption(self):
        """
        Create a ciphertext with the threshold public key and decrypt it and 
        create others ciphertext to prove IncompatibleCiphertextError
        """

        tprk = self.tSetUp.generate_private_key(0,
                                                self.trustees[0].private_key)
        text_to_encrypt_dir = os.path.join(
            os.path.dirname(__file__), "TestThresholdPrivateKey.resources")
        text_to_encrypt = os.path.join(text_to_encrypt_dir, "text_to_encrypt")
        text_encrypted = self.tpkey.encrypt_text(text_to_encrypt)

        # Decrypt the file created with our public key must be fine
        tprk.generate_partial_decryption(text_encrypted)

        # Create another ThresholdEcryptuonSetUp with other 1024 bits
        # cryptosys to create a cypthertext that cant be decrypted
        second_cryptosys_file = os.path.join(
            os.path.dirname(__file__),
            "TestThresholdEncryptionSetUp.resources",
            "test1024bits_second.pvcryptosys")
        # Load the cryptosystem from file
        second_cryptosys = EGCryptoSystem.from_file(second_cryptosys_file)
        secondtSetUp = ThresholdEncryptionSetUp(second_cryptosys,
                                                self.num_trustees,
                                                self.threshold)
        # Adding the keys from trustees for 2ndsetUp
        for i in range(self.num_trustees):
            secondtSetUp.add_trustee_public_key(i, self.trustees[i].public_key)
        secondcommitments = []
        # Generate commitmes for trustees for 2ndsetUp
        for i in range(self.num_trustees):
            secondcommitments.append(secondtSetUp.generate_commitment())
        # Adding the secont trustees  commitments
        for i in range(self.num_trustees):
            secondtSetUp.add_trustee_commitment(i, secondcommitments[i])
        # Generate secon cryptosis publickey
        secondtpkey = secondtSetUp.generate_public_key()
        # Encrypt the file with the secon cryptosis publickey
        secondtext_encrypted = secondtpkey.encrypt_text(text_to_encrypt)

        # Try to decryp something created with other ThresholdEcryptuonSetUp
        # must raise IncompatibleCiphertextError

        self.assertRaises(IncompatibleCiphertextError,
                          tprk.generate_partial_decryption,
                          secondtext_encrypted)

        # Create another ThresholdEcryptuonSetUp with other 512 bits
        # cryptosys to create a cypthertext that cant be decrypted
        third_cryptosys_file = os.path.join(
            os.path.dirname(__file__),
            "TestThresholdEncryptionSetUp.resources",
            "test512bits.pvcryptosys")
        # Load the cryptosystem from file
        third_cryptosys = EGCryptoSystem.from_file(third_cryptosys_file)
        thirdtSetUp = ThresholdEncryptionSetUp(third_cryptosys,
                                               self.num_trustees,
                                               self.threshold)
        # Adding the keys from trustees for 2ndsetUp
        for i in range(self.num_trustees):
            thirdtSetUp.add_trustee_public_key(i, self.trustees[i].public_key)
        thirdcommitments = []
        # Generate commitmes for trustees for 2ndsetUp
        for i in range(self.num_trustees):
            thirdcommitments.append(thirdtSetUp.generate_commitment())
        # Adding the secont trustees  commitments
        for i in range(self.num_trustees):
            thirdtSetUp.add_trustee_commitment(i, thirdcommitments[i])
        # Generate secon cryptosis publickey
        thirdtpkey = thirdtSetUp.generate_public_key()
        # Encrypt the file with the secon cryptosis publickey
        thirdtext_encrypted = thirdtpkey.encrypt_text(text_to_encrypt)

        # Try to decryp something created with other ThresholdEcryptuonSetUp
        # must raise IncompatibleCiphertextError

        self.assertRaises(IncompatibleCiphertextError,
                          tprk.generate_partial_decryption,
                          thirdtext_encrypted)
    def test_partial_decryption(self):
        """
        Create a ciphertext with the threshold public key and decrypt it and 
        create others ciphertext to prove IncompatibleCiphertextError
        """

        tprk = self.tSetUp.generate_private_key(0, self.trustees[0].private_key)
        text_to_encrypt_dir = os.path.join(os.path.dirname(__file__), 
                                           "TestThresholdPrivateKey.resources")
        text_to_encrypt = os.path.join(text_to_encrypt_dir, "text_to_encrypt")
        text_encrypted = self.tpkey.encrypt_text(text_to_encrypt)
        
        # Decrypt the file created with our public key must be fine
        tprk.generate_partial_decryption(text_encrypted)
        
        # Create another ThresholdEcryptuonSetUp with other 1024 bits
        # cryptosys to create a cypthertext that cant be decrypted
        second_cryptosys_file = os.path.join(os.path.dirname(__file__), 
                                      "TestThresholdEncryptionSetUp.resources",
                                      "test1024bits_second.pvcryptosys")
        # Load the cryptosystem from file
        second_cryptosys = EGCryptoSystem.from_file(second_cryptosys_file)      
        secondtSetUp = ThresholdEncryptionSetUp(second_cryptosys, 
                                          self.num_trustees, self.threshold)
         # Adding the keys from trustees for 2ndsetUp
        for i in range(self.num_trustees):
            secondtSetUp.add_trustee_public_key(i, self.trustees[i].public_key)
        secondcommitments = []
        # Generate commitmes for trustees for 2ndsetUp
        for i in range(self.num_trustees):
            secondcommitments.append(secondtSetUp.generate_commitment()) 
        # Adding the secont trustees  commitments 
        for i in range(self.num_trustees):
            secondtSetUp.add_trustee_commitment(i, secondcommitments[i])
        # Generate secon cryptosis publickey
        secondtpkey = secondtSetUp.generate_public_key()
        # Encrypt the file with the secon cryptosis publickey
        secondtext_encrypted = secondtpkey.encrypt_text(text_to_encrypt)
        
        
        # Try to decryp something created with other ThresholdEcryptuonSetUp 
        # must raise IncompatibleCiphertextError
        
        self.assertRaises(IncompatibleCiphertextError, 
                         tprk.generate_partial_decryption, secondtext_encrypted)


        # Create another ThresholdEcryptuonSetUp with other 512 bits
        # cryptosys to create a cypthertext that cant be decrypted
        third_cryptosys_file = os.path.join(os.path.dirname(__file__), 
                                      "TestThresholdEncryptionSetUp.resources",
                                      "test512bits.pvcryptosys")
        # Load the cryptosystem from file
        third_cryptosys = EGCryptoSystem.from_file(third_cryptosys_file)      
        thirdtSetUp = ThresholdEncryptionSetUp(third_cryptosys, 
                                          self.num_trustees, self.threshold)
         # Adding the keys from trustees for 2ndsetUp
        for i in range(self.num_trustees):
            thirdtSetUp.add_trustee_public_key(i, self.trustees[i].public_key)
        thirdcommitments = []
        # Generate commitmes for trustees for 2ndsetUp
        for i in range(self.num_trustees):
            thirdcommitments.append(thirdtSetUp.generate_commitment()) 
        # Adding the secont trustees  commitments 
        for i in range(self.num_trustees):
            thirdtSetUp.add_trustee_commitment(i, thirdcommitments[i])
        # Generate secon cryptosis publickey
        thirdtpkey = thirdtSetUp.generate_public_key()
        # Encrypt the file with the secon cryptosis publickey
        thirdtext_encrypted = thirdtpkey.encrypt_text(text_to_encrypt)
        
        
        # Try to decryp something created with other ThresholdEcryptuonSetUp 
        # must raise IncompatibleCiphertextError
        
        self.assertRaises(IncompatibleCiphertextError, 
                         tprk.generate_partial_decryption, thirdtext_encrypted)