Example #1
0
    def encrypt(self, plainData):
        """
        Encrypt the plainData using the existing Content Key (CK) and return a
        new EncryptedContent.

        :param plainData: The data to encrypt.
        :type plainData: Blob or an array which implements the buffer protocol
        :return: The new EncryptedContent.
        :rtype: EncryptedContent
        """
        # Generate the initial vector.
        initialVector = bytearray(EncryptorV2.AES_IV_SIZE)
        for i in range(len(initialVector)):
            initialVector[i] = _systemRandom.randint(0, 0xff)

        params = EncryptParams(EncryptAlgorithmType.AesCbc)
        params.setInitialVector(Blob(initialVector, False))
        encryptedData = AesAlgorithm.encrypt(Blob(self._ckBits, False),
                                             Blob(plainData, False), params)

        content = EncryptedContent()
        content.setInitialVector(params.getInitialVector())
        content.setPayload(encryptedData)
        content.setKeyLocatorName(self._ckName)

        return content
Example #2
0
    def _encryptSymmetric(payload, key, keyName, params):
        """
        Encrypt the payload using the symmetric key according to params, and
        return an EncryptedContent.

        :param Blob payload: The data to encrypt.
        :param Blob key: The key value.
        :param Name keyName: The key name for the EncryptedContent key locator.
        :param EncryptParams params: The parameters for encryption.
        :return: A new EncryptedContent.
        :rtype: EncryptedContent
        """
        algorithmType = params.getAlgorithmType()
        initialVector = params.getInitialVector()
        keyLocator = KeyLocator()
        keyLocator.setType(KeyLocatorType.KEYNAME)
        keyLocator.setKeyName(keyName)

        if (algorithmType == EncryptAlgorithmType.AesCbc or
            algorithmType == EncryptAlgorithmType.AesEcb):
            if (algorithmType == EncryptAlgorithmType.AesCbc):
                if initialVector.size() != AesAlgorithm.BLOCK_SIZE:
                    raise RuntimeError("incorrect initial vector size")

            encryptedPayload = AesAlgorithm.encrypt(key, payload, params)

            result = EncryptedContent()
            result.setAlgorithmType(algorithmType)
            result.setKeyLocator(keyLocator)
            result.setPayload(encryptedPayload)
            result.setInitialVector(initialVector)
            return result
        else:
            raise RuntimeError("Unsupported encryption method")
Example #3
0
    def encrypt(self, plainData):
        """
        Encrypt the plainData using the existing Content Key (CK) and return a
        new EncryptedContent.

        :param plainData: The data to encrypt.
        :type plainData: Blob or an array which implements the buffer protocol
        :return: The new EncryptedContent.
        :rtype: EncryptedContent
        """
        # Generate the initial vector.
        initialVector = bytearray(EncryptorV2.AES_IV_SIZE)
        for i in range(len(initialVector)):
            initialVector[i] = _systemRandom.randint(0, 0xff)

        params = EncryptParams(EncryptAlgorithmType.AesCbc)
        params.setInitialVector(Blob(initialVector, False))
        encryptedData = AesAlgorithm.encrypt(
          Blob(self._ckBits, False), Blob(plainData, False), params)

        content = EncryptedContent()
        content.setInitialVector(params.getInitialVector())
        content.setPayload(encryptedData)
        content.setKeyLocatorName(self._ckName)

        return content
Example #4
0
    def _encryptSymmetric(payload, key, keyName, params):
        """
        Encrypt the payload using the symmetric key according to params, and
        return an EncryptedContent.

        :param Blob payload: The data to encrypt.
        :param Blob key: The key value.
        :param Name keyName: The key name for the EncryptedContent key locator.
        :param EncryptParams params: The parameters for encryption.
        :return: A new EncryptedContent.
        :rtype: EncryptedContent
        """
        algorithmType = params.getAlgorithmType()
        initialVector = params.getInitialVector()
        keyLocator = KeyLocator()
        keyLocator.setType(KeyLocatorType.KEYNAME)
        keyLocator.setKeyName(keyName)

        if (algorithmType == EncryptAlgorithmType.AesCbc
                or algorithmType == EncryptAlgorithmType.AesEcb):
            if (algorithmType == EncryptAlgorithmType.AesCbc):
                if initialVector.size() != AesAlgorithm.BLOCK_SIZE:
                    raise RuntimeError("incorrect initial vector size")

            encryptedPayload = AesAlgorithm.encrypt(key, payload, params)

            result = EncryptedContent()
            result.setAlgorithmType(algorithmType)
            result.setKeyLocator(keyLocator)
            result.setPayload(encryptedPayload)
            result.setInitialVector(initialVector)
            return result
        else:
            raise RuntimeError("Unsupported encryption method")