def _encrypt_queue_message(message, key_encryption_key):
    '''
    Encrypts the given plain text message using AES256 in CBC mode with 128 bit padding.
    Wraps the generated content-encryption-key using the user-provided key-encryption-key (kek). 
    Returns a json-formatted string containing the encrypted message and the encryption metadata.

    :param object message:
        The plain text messge to be encrypted.
    :param object key_encryption_key:
        The user-provided key-encryption-key. Must implement the following methods:
        wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
        get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
        get_kid()--returns a string key id for this key-encryption-key.
    :return: A json-formatted string containing the encrypted message and the encryption metadata.
    :rtype: str
    '''

    _validate_not_none('message', message)
    _validate_not_none('key_encryption_key', key_encryption_key)
    _validate_key_encryption_key_wrap(key_encryption_key)

    # AES256 uses 256 bit (32 byte) keys and always with 16 byte blocks
    content_encryption_key = os.urandom(32)
    initialization_vector = os.urandom(16)

    # Queue encoding functions all return unicode strings, and encryption should
    # operate on binary strings.
    message = message.encode('utf-8')

    cipher = _generate_AES_CBC_cipher(content_encryption_key,
                                      initialization_vector)

    # PKCS7 with 16 byte blocks ensures compatibility with AES.
    padder = PKCS7(128).padder()
    padded_data = padder.update(message) + padder.finalize()

    # Encrypt the data.
    encryptor = cipher.encryptor()
    encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

    # Build the dictionary structure.
    queue_message = {
        'EncryptedMessageContents':
        _encode_base64(encrypted_data),
        'EncryptionData':
        _generate_encryption_data_dict(key_encryption_key,
                                       content_encryption_key,
                                       initialization_vector)
    }

    return dumps(queue_message)
Beispiel #2
0
def _encrypt_blob(blob, key_encryption_key):
    '''
    Encrypts the given blob using AES256 in CBC mode with 128 bit padding.
    Wraps the generated content-encryption-key using the user-provided key-encryption-key (kek). 
    Returns a json-formatted string containing the encryption metadata. This method should
    only be used when a blob is small enough for single shot upload. Encrypting larger blobs
    is done as a part of the _upload_blob_chunks method.

    :param bytes blob:
        The blob to be encrypted.
    :param object key_encryption_key:
        The user-provided key-encryption-key. Must implement the following methods:
        wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
        get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
        get_kid()--returns a string key id for this key-encryption-key.
    :return: A tuple of json-formatted string containing the encryption metadata and the encrypted blob data.
    :rtype: (str, bytes)
    '''

    _validate_not_none('blob', blob)
    _validate_not_none('key_encryption_key', key_encryption_key)
    _validate_key_encryption_key_wrap(key_encryption_key)

    # AES256 uses 256 bit (32 byte) keys and always with 16 byte blocks
    content_encryption_key = urandom(32)
    initialization_vector = urandom(16)

    cipher = _generate_AES_CBC_cipher(content_encryption_key,
                                      initialization_vector)

    # PKCS7 with 16 byte blocks ensures compatibility with AES.
    padder = PKCS7(128).padder()
    padded_data = padder.update(blob) + padder.finalize()

    # Encrypt the data.
    encryptor = cipher.encryptor()
    encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
    encryption_data = _generate_encryption_data_dict(key_encryption_key,
                                                     content_encryption_key,
                                                     initialization_vector)
    encryption_data['EncryptionMode'] = 'FullBlob'

    return dumps(encryption_data), encrypted_data
def _encrypt_blob(blob, key_encryption_key):
    '''
    Encrypts the given blob using AES256 in CBC mode with 128 bit padding.
    Wraps the generated content-encryption-key using the user-provided key-encryption-key (kek). 
    Returns a json-formatted string containing the encryption metadata. This method should
    only be used when a blob is small enough for single shot upload. Encrypting larger blobs
    is done as a part of the _upload_blob_chunks method.

    :param bytes blob:
        The blob to be encrypted.
    :param object key_encryption_key:
        The user-provided key-encryption-key. Must implement the following methods:
        wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
        get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
        get_kid()--returns a string key id for this key-encryption-key.
    :return: A tuple of json-formatted string containing the encryption metadata and the encrypted blob data.
    :rtype: (str, bytes)
    '''

    _validate_not_none('blob', blob)
    _validate_not_none('key_encryption_key', key_encryption_key)
    _validate_key_encryption_key_wrap(key_encryption_key)

    # AES256 uses 256 bit (32 byte) keys and always with 16 byte blocks
    content_encryption_key = urandom(32)
    initialization_vector = urandom(16)

    cipher = _generate_AES_CBC_cipher(content_encryption_key, initialization_vector)

    # PKCS7 with 16 byte blocks ensures compatibility with AES.
    padder = PKCS7(128).padder()
    padded_data = padder.update(blob) + padder.finalize()

    # Encrypt the data.
    encryptor = cipher.encryptor()
    encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
    encryption_data = _generate_encryption_data_dict(key_encryption_key, content_encryption_key,
                                                     initialization_vector)
    encryption_data['EncryptionMode'] = 'FullBlob'

    return dumps(encryption_data), encrypted_data
Beispiel #4
0
def _generate_blob_encryption_data(key_encryption_key):
    '''
    Generates the encryption_metadata for the blob.
    
    :param bytes key_encryption_key:
        The key-encryption-key used to wrap the cek associate with this blob.
    :return: A tuple containing the cek and iv for this blob as well as the 
        serialized encryption metadata for the blob.
    :rtype: (bytes, bytes, str)
    '''
    encryption_data = None
    content_encryption_key = None
    initialization_vector = None
    if key_encryption_key:
        _validate_key_encryption_key_wrap(key_encryption_key)
        content_encryption_key = urandom(32)
        initialization_vector = urandom(16)
        encryption_data = _generate_encryption_data_dict(
            key_encryption_key, content_encryption_key, initialization_vector)
        encryption_data['EncryptionMode'] = 'FullBlob'
        encryption_data = dumps(encryption_data)

    return content_encryption_key, initialization_vector, encryption_data
def _generate_blob_encryption_data(key_encryption_key):
    '''
    Generates the encryption_metadata for the blob.
    
    :param bytes key_encryption_key:
        The key-encryption-key used to wrap the cek associate with this blob.
    :return: A tuple containing the cek and iv for this blob as well as the 
        serialized encryption metadata for the blob.
    :rtype: (bytes, bytes, str)
    '''
    encryption_data = None
    content_encryption_key = None
    initialization_vector = None
    if key_encryption_key:
        _validate_key_encryption_key_wrap(key_encryption_key)
        content_encryption_key = urandom(32)
        initialization_vector = urandom(16)
        encryption_data = _generate_encryption_data_dict(key_encryption_key,
                                                         content_encryption_key,
                                                         initialization_vector)
        encryption_data['EncryptionMode'] = 'FullBlob'
        encryption_data = dumps(encryption_data)

    return content_encryption_key, initialization_vector, encryption_data
Beispiel #6
0
def _encrypt_entity(entity, key_encryption_key, encryption_resolver):
    '''
    Encrypts the given entity using AES256 in CBC mode with 128 bit padding.
    Will generate a content-encryption-key (cek) to encrypt the properties either
    stored in an EntityProperty with the 'encrypt' flag set or those
    specified by the encryption resolver. This cek is then wrapped using the 
    provided key_encryption_key (kek). Only strings may be encrypted and the
    result is stored as binary on the service. 

    :param entity:
        The entity to insert. Could be a dict or an entity object.
    :param object key_encryption_key:
        The user-provided key-encryption-key. Must implement the following methods:
        wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
        get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
        get_kid()--returns a string key id for this key-encryption-key.
    :param function(partition_key, row_key, property_name) encryption_resolver:
        A function that takes in an entities partition key, row key, and property name and returns 
        a boolean that indicates whether that property should be encrypted.
    :return: An entity with both the appropriate properties encrypted and the 
        encryption data.
    :rtype: object
    '''

    _validate_not_none('entity', entity)
    _validate_not_none('key_encryption_key', key_encryption_key)
    _validate_key_encryption_key_wrap(key_encryption_key)

    # AES256 uses 256 bit (32 byte) keys and always with 16 byte blocks
    content_encryption_key = os.urandom(32)
    entity_initialization_vector = os.urandom(16)

    encrypted_properties = []
    encrypted_entity = Entity()
    for key, value in entity.items():
        # If the property resolver says it should be encrypted
        # or it is an EntityProperty with the 'encrypt' property set.
        if (isinstance(value, EntityProperty) and value.encrypt) or \
                (encryption_resolver is not None \
                         and encryption_resolver(entity['PartitionKey'], entity['RowKey'], key)):

            # Only strings can be encrypted and None is not an instance of str.
            if isinstance(value, EntityProperty):
                if value.type == EdmType.STRING:
                    value = value.value
                else:
                    raise ValueError(_ERROR_UNSUPPORTED_TYPE_FOR_ENCRYPTION)
            if not isinstance(value, str):
                raise ValueError(_ERROR_UNSUPPORTED_TYPE_FOR_ENCRYPTION)

                # Value is now confirmed to hold a valid string value to be encrypted
            # and should be added to the list of encrypted properties.
            encrypted_properties.append(key)

            propertyIV = _generate_property_iv(entity_initialization_vector,
                                               entity['PartitionKey'], entity['RowKey'],
                                               key, False)

            # Encode the strings for encryption.
            value = value.encode('utf-8')

            cipher = _generate_AES_CBC_cipher(content_encryption_key, propertyIV)

            # PKCS7 with 16 byte blocks ensures compatibility with AES.
            padder = PKCS7(128).padder()
            padded_data = padder.update(value) + padder.finalize()

            # Encrypt the data.
            encryptor = cipher.encryptor()
            encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

            # Set the new value of this key to be a binary EntityProperty for proper serialization.
            value = EntityProperty(EdmType.BINARY, encrypted_data)

        encrypted_entity[key] = value

    encrypted_properties = dumps(encrypted_properties)

    # Generate the metadata iv.
    metadataIV = _generate_property_iv(entity_initialization_vector,
                                       entity['PartitionKey'], entity['RowKey'],
                                       '_ClientEncryptionMetadata2', False)

    encrypted_properties = encrypted_properties.encode('utf-8')

    cipher = _generate_AES_CBC_cipher(content_encryption_key, metadataIV)

    padder = PKCS7(128).padder()
    padded_data = padder.update(encrypted_properties) + padder.finalize()

    encryptor = cipher.encryptor()
    encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

    encrypted_entity['_ClientEncryptionMetadata2'] = EntityProperty(EdmType.BINARY, encrypted_data)

    encryption_data = _generate_encryption_data_dict(key_encryption_key, content_encryption_key,
                                                     entity_initialization_vector)

    encrypted_entity['_ClientEncryptionMetadata1'] = dumps(encryption_data)
    return encrypted_entity