Encryption support.
Subsystems (2)
cover Encryption support.
Routines to implement Suspend2 image encryption support. This is achieved via the cryptoapi plugins. We let the user configure every aspect. They just need to set the appropriate /proc entries and have the modules built-in or loadable from an initrd/ramfs for resume-time.
PATCH 1/12 Encryption support header
Add the header for the encryption support file.
kernel/power/encryption.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 47 insertions(+), 0 deletions(-)
kernel/power/encryption.c new file +47 -0
@@ -0,0 +1,47 @@ +/* +* kernel/power/suspend_core/encryption.c +* +* Copyright (C) 2003-2006 Nigel Cunningham <[email protected]> +* +* This file is released under the GPLv2. +* +* This file contains data encryption routines for suspend, +* using cryptoapi transforms. +* +* ToDo: +* - Apply min/max_keysize the cipher changes. +* - Test. +*/ + +#include <linux/suspend.h> +#include <linux/module.h> +#include <linux/highmem.h> +#include <linux/vmalloc.h> +#include <linux/crypto.h> +#include <asm/scatterlist.h> + +#include "suspend2.h" +#include "modules.h" +#include "proc.h" +#include "suspend2_common.h" +#include "io.h" + +#define S2C_WRITE 0 +#define S2C_READ 1 + +static struct suspend_module_ops suspend_encryption_ops; +static struct suspend_module_ops *next_driver; + +static char suspend_encryptor_name[32 ]; +static struct crypto_tfm *suspend_encryptor_transform; +static char suspend_encryptor_key[256 ]; +static int suspend_key_len; +static char suspend_encryptor_iv[256 ]; +static int suspend_encryptor_mode; +static int suspend_encryptor_save_key_and_iv; + +static u8 *page_buffer =NULL ; +static unsigned int bufofs; + +static struct scatterlist suspend_crypt_sg[PAGE_SIZE /8 ]; +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 2/12 Encryption buffer allocation.
In reply to Nigel CunninghamAdd support for allocating and freeing buffers used for encryption.
kernel/power/encryption.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 51 insertions(+), 0 deletions(-)
kernel/power/encryption.c +51 -0
@@ -45,3 +45,54 @@ static unsigned int bufofs;static struct scatterlist suspend_crypt_sg[PAGE_SIZE /8 ]; +/* ---- Local buffer management ---- */ + +/* allocate_local_buffer +* +* Description: Allocates a page of memory for buffering output. +* Returns: Int: Zero if successful, -ENONEM otherwise. +*/ +static int allocate_local_buffer (void ) +{ +if (!page_buffer) { +int i, remainder; + + page_buffer = (char *)get_zeroed_page (GFP_ATOMIC ); + +if (!page_buffer) { +printk (KERN_ERR +"Failed to allocate the page buffer for " +"suspend2 encryption driver.\n" ); +return -ENOMEM ; + } + +for (i=0 ; i < (PAGE_SIZE / suspend_key_len); i++) { + suspend_crypt_sg[i].page =virt_to_page (page_buffer); + suspend_crypt_sg[i].offset = suspend_key_len * i; + suspend_crypt_sg[i].length = suspend_key_len; + } + + remainder =PAGE_SIZE % suspend_key_len; + +if (remainder) { + suspend_crypt_sg[i].page =virt_to_page (page_buffer); + suspend_crypt_sg[i].offset = suspend_key_len * i; + suspend_crypt_sg[i].length = remainder; + } + } + +return 0 ; +} + +/* free_local_buffer +* +* Description: Frees memory allocated for buffering output. +*/ +static void free_local_buffer (void ) +{ +if (page_buffer) +free_page ((unsigned long ) page_buffer); + + page_buffer =NULL ; +} +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 3/12 Encryption rw init & cleanup routines.
In reply to Nigel CunninghamAdd routines for preparing to read or write an image, and cleaning up afterwards.
kernel/power/encryption.c | 61 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 61 insertions(+), 0 deletions(-)
kernel/power/encryption.c +61 -0
@@ -96,3 +96,64 @@ static void free_local_buffer(void) page_buffer =NULL ; } +/* suspend_encrypt_rw_cleanup +* +* Description: Frees memory allocated for our labours. +*/ +static int suspend_encrypt_rw_cleanup (int rw) +{ +if (suspend_encryptor_transform) { +crypto_free_tfm (suspend_encryptor_transform); + suspend_encryptor_transform =NULL ; + } + +free_local_buffer (); + +return 0 ; +} + +/* suspend_crypto_prepare +* +* Description: Prepare to do some work by allocating buffers and transforms. +* Returns: Int: Zero if successful, 1 otherwise. +*/ +static int suspend_encrypt_rw_prepare (int rw) +{ +if (!*suspend_encryptor_name) { +printk ("Suspend2: Encryptor enabled but no name set.\n" ); + suspend_encryption_ops.disabled =1 ; +return 1 ; + } + +if (!(suspend_encryptor_transform =crypto_alloc_tfm (suspend_encryptor_name, +1 << suspend_encryptor_mode))) { +printk ("Suspend2: Failed to initialise the encryption " +"transform (%s, mode %d).\n" , + suspend_encryptor_name, suspend_encryptor_mode); +return 1 ; + } + +if (rw ==READ ) + bufofs =PAGE_SIZE ; +else + bufofs =0 ; + + suspend_key_len =strlen (suspend_encryptor_key); + +if (crypto_cipher_setkey (suspend_encryptor_transform, suspend_encryptor_key, + suspend_key_len)) { +printk ("%d is an invalid key length for cipher %s.\n" , + suspend_key_len, + suspend_encryptor_name); +return 1 ; + } + +if (rw !=READ ) { +crypto_cipher_set_iv (suspend_encryptor_transform, + suspend_encryptor_iv, +crypto_tfm_alg_ivsize (suspend_encryptor_transform)); + } + +return 0 ; +} +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 4/12 Encrypt a page being written.
In reply to Nigel CunninghamAdd the routine that processes pages being written in the image.
kernel/power/encryption.c | 51 +++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 49 insertions(+), 2 deletions(-)
kernel/power/encryption.c +49 -2
@@ -125,11 +125,13 @@ static int suspend_encrypt_rw_prepare(inreturn 1 ; } -if (!(suspend_encryptor_transform =crypto_alloc_tfm (suspend_encryptor_name, -1 << suspend_encryptor_mode))) { + suspend_encryptor_transform =crypto_alloc_tfm (suspend_encryptor_name, +1 << suspend_encryptor_mode); +if (!suspend_encryptor_transform) {printk ("Suspend2: Failed to initialise the encryption " "transform (%s, mode %d).\n" , suspend_encryptor_name, suspend_encryptor_mode); + suspend_encryption_ops.disabled =1 ;return 1 ; }
@@ -157,3 +159,48 @@ static int suspend_encrypt_rw_prepare(inreturn 0 ; } +/* ---- Exported functions ---- */ + +/* suspend_encrypt_write_chunk() +* +* Description: Encrypt a page of data, buffering output and passing on +* filled pages to the next module in the pipeline. +* Arguments: Buffer_page: Pointer to a buffer of size PAGE_SIZE, +* containing data to be encrypted. +* Returns: 0 on success. Otherwise the error is that returned by later +* modules, -ECHILD if we have a broken pipeline or -EIO if +* zlib errs. +*/ +static int suspend_encrypt_write_chunk (struct page *buffer_page) +{ +int ret; +unsigned int len; +u16 len_written; +char *buffer_start; + +if (!suspend_encryptor_transform) +return next_driver->write_chunk (buffer_page); + + buffer_start =kmap (buffer_page); +memcpy (page_buffer, buffer_start,PAGE_SIZE ); +kunmap (buffer_page); + + bytes_in +=PAGE_SIZE ; + + len =PAGE_SIZE ; + + ret =crypto_cipher_encrypt (suspend_encryptor_transform, + suspend_crypt_sg, suspend_crypt_sg,PAGE_SIZE ); + +if (ret) { +printk ("Encryption failed.\n" ); +return -EIO ; + } + + len_written = (u16 ) len; + + ret = next_driver->write_chunk (virt_to_page (page_buffer)); + +return ret; +} +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 5/12 Prepare to encrypt or decrypt a stream of pages.
In reply to Nigel CunninghamThis routine adds initialisation code for encrypting or decrypting a stream of pages in the image.
kernel/power/encryption.c | 34 ++++++++++++++++++++++++++++++++++ 1 files changed, 34 insertions(+), 0 deletions(-)
kernel/power/encryption.c +34 -0
@@ -204,3 +204,37 @@ static int suspend_encrypt_write_chunk(sreturn ret; } +/* rw_init() +* +* Description: Prepare to read a new stream of data. +* Arguments: int: Section of image about to be read. +* Returns: int: Zero on success, error number otherwise. +*/ +static int suspend_encrypt_rw_init (int rw,int stream_number) +{ +int result; + + next_driver =suspend_get_next_filter (&suspend_encryption_ops); + +if (!next_driver) { +printk ("Encryption Driver: Argh! I'm at the end of the pipeline!" ); +return -ECHILD ; + } + +if ((result =suspend_encrypt_rw_prepare (rw))) { +set_result_state (SUSPEND_ENCRYPTION_SETUP_FAILED ); +suspend_encrypt_rw_cleanup (rw); +return result; + } + +if ((result =allocate_local_buffer ())) +return result; + +if (rw ==WRITE && stream_number ==2 ) + bytes_in = bytes_out =0 ; + + bufofs = (rw ==READ ) ?PAGE_SIZE :0 ; + +return 0 ; +} +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 6/12 Decrypt a page in the image.
In reply to Nigel CunninghamAdd support for decrypting a page being read from the storage.
kernel/power/encryption.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 41 insertions(+), 0 deletions(-)
kernel/power/encryption.c +41 -0
@@ -238,3 +238,44 @@ static int suspend_encrypt_rw_init(int rreturn 0 ; } +/* suspend_encrypt_read_chunk() +* +* Description: Retrieve data from later modules and deencrypt it until the +* input buffer is filled. +* Arguments: Buffer_start: Pointer to a buffer of size PAGE_SIZE. +* Sync: Whether the previous module (or core) wants its +* data synchronously. +* Returns: Zero if successful. Error condition from me or from downstream +* on failure. +*/ +static int suspend_encrypt_read_chunk (struct page *buffer_page,int sync) +{ +int ret; +char *buffer_start; + +if (!suspend_encryptor_transform) +return next_driver->read_chunk (buffer_page, sync); + +/* +* All our reads must be synchronous - we can't deencrypt +* data that hasn't been read yet. +*/ + +if ((ret = next_driver->read_chunk ( +virt_to_page (page_buffer),SUSPEND_SYNC )) <0 ) { +printk ("Failed to read an encrypted block.\n" ); +return ret; + } + + ret =crypto_cipher_decrypt (suspend_encryptor_transform, + suspend_crypt_sg, suspend_crypt_sg,PAGE_SIZE ); + +if (ret) +printk ("Decrypt function returned %d.\n" , ret); + + buffer_start =kmap (buffer_page); +memcpy (buffer_start, page_buffer,PAGE_SIZE ); +kunmap (buffer_page); +return ret; +} +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 7/12 Encryption debug stats.
In reply to Nigel CunninghamIf encryption is enabled, add a message to the debug information to indicate that it is enabled and which algorithm is being used.
kernel/power/encryption.c | 18 ++++++++++++++++++ 1 files changed, 18 insertions(+), 0 deletions(-)
kernel/power/encryption.c +18 -0
@@ -279,3 +279,21 @@ static int suspend_encrypt_read_chunk(streturn ret; } +/* suspend_encrypt_print_debug_stats +* +* Description: Print information to be recorded for debugging purposes into a +* buffer. +* Arguments: buffer: Pointer to a buffer into which the debug info will be +* printed. +* size: Size of the buffer. +* Returns: Number of characters written to the buffer. +*/ +static int suspend_encrypt_print_debug_stats (char *buffer,int size) +{ +int len; + + len =snprintf_used (buffer, size,"- Encryptor %s enabled.\n" , + suspend_encryptor_name); +return len; +} +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 8/12 Encryption resources needed.
In reply to Nigel CunninghamAdd routines to return the amount of memory and header storage needed for the encryption module.
kernel/power/encryption.c | 20 ++++++++++++++++++++ 1 files changed, 20 insertions(+), 0 deletions(-)
kernel/power/encryption.c +20 -0
@@ -297,3 +297,23 @@ static int suspend_encrypt_print_debug_sreturn len; } +/* encryption_memory_needed +* +* Description: Tell the caller how much memory we need to operate during +* suspend/resume. +* Returns: Unsigned long. Maximum number of bytes of memory required for +* operation. +*/ +static unsigned long suspend_encrypt_memory_needed (void ) +{ +return PAGE_SIZE ; +} + +static unsigned long suspend_encrypt_storage_needed (void ) +{ +return 4 +strlen (suspend_encryptor_name) + + (suspend_encryptor_save_key_and_iv ? + (4 +strlen (suspend_encryptor_key) + +strlen (suspend_encryptor_iv)) :0 ); +} +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 9/12 Encryption configuration serialisation.
In reply to Nigel CunninghamAdd routines to read and write the encryption module configuration in an image header.
kernel/power/encryption.c | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 83 insertions(+), 0 deletions(-)
kernel/power/encryption.c +83 -0
@@ -316,4 +316,87 @@ static unsigned long suspend_encrypt_sto (4 +strlen (suspend_encryptor_key) + strlen(suspend_encryptor_iv)) : 0); } + +/* suspend_encrypt_save_config_info +* +* Description: Save informaton needed when reloading the image at resume time. +* Arguments: Buffer: Pointer to a buffer of size PAGE_SIZE. +* Returns: Number of bytes used for saving our data. +*/ +static int suspend_encrypt_save_config_info (char *buffer) +{ +int buf_offset, str_size; + str_size =strlen (suspend_encryptor_name); + *buffer = (char ) str_size; +strncpy (buffer +1 , suspend_encryptor_name, str_size +1 ); + buf_offset = str_size +2 ; + + *(buffer + buf_offset) = (char ) suspend_encryptor_mode; + buf_offset++; + + *(buffer + buf_offset) = (char ) suspend_encryptor_save_key_and_iv; + buf_offset++; + +if (suspend_encryptor_save_key_and_iv) { + + str_size =strlen (suspend_encryptor_key); + *(buffer + buf_offset) = (char ) str_size; +strncpy (buffer + buf_offset +1 , suspend_encryptor_key, str_size +1 ); + + buf_offset+= str_size +2 ; + + str_size =strlen (suspend_encryptor_iv); + *(buffer + buf_offset) = (char ) str_size; +strncpy (buffer + buf_offset +1 , suspend_encryptor_iv, str_size +1 ); + + buf_offset += str_size +2 ; + } + +return buf_offset; +} + +/* suspend_encrypt_load_config_info +* +* Description: Reload information needed for deencrypting the image at +* resume time. +* Arguments: Buffer: Pointer to the start of the data. +* Size: Number of bytes that were saved. +*/ +static void suspend_encrypt_load_config_info (char *buffer,int size) +{ +int buf_offset, str_size; + + str_size = (int ) *buffer; +strncpy (suspend_encryptor_name, buffer +1 , str_size +1 ); + buf_offset = str_size +2 ; + + suspend_encryptor_mode = (int ) *(buffer + buf_offset); + buf_offset++; + + suspend_encryptor_save_key_and_iv = (int ) *(buffer + buf_offset); + buf_offset++; + +if (suspend_encryptor_save_key_and_iv) { + str_size = (int ) *(buffer + buf_offset); +strncpy (suspend_encryptor_key, buffer + buf_offset +1 , str_size +1 ); + + buf_offset+= str_size +2 ; + + str_size = (int ) *(buffer + buf_offset); +strncpy (suspend_encryptor_iv, buffer + buf_offset +1 , str_size +1 ); + + buf_offset += str_size +2 ; + }else { + *suspend_encryptor_key =0 ; + *suspend_encryptor_iv =0 ; + } + +if (buf_offset != size) { +printk ("Suspend Encryptor config info size mismatch (%d != %d): settings ignored.\n" , + buf_offset, size); + *suspend_encryptor_key =0 ; + *suspend_encryptor_iv =0 ; + } +return ; +}
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 10/12 Encryption proc entries.
In reply to Nigel CunninghamAdd data for the encryption related proc entries.
kernel/power/encryption.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 82 insertions(+), 0 deletions(-)
kernel/power/encryption.c +82 -0
@@ -400,3 +400,85 @@ static void suspend_encrypt_load_config_ }return ; } + +/* +* data for our proc entries. +*/ +static struct suspend_proc_data proc_params[] = { +{ + .filename ="encryptor" , + .permissions =PROC_RW , + .type =SUSPEND_PROC_DATA_STRING , + .data = { + .string = { + .variable = suspend_encryptor_name, + .max_length =31 , + } + }, +}, + +{ + .filename ="encryption_mode" , + .permissions =PROC_RW , + .type =SUSPEND_PROC_DATA_INTEGER , + .data = { + .integer = { + .variable = &suspend_encryptor_mode, + .minimum =0 , + .maximum =3 , + } + } +}, + +{ + .filename ="encryption_save_key_and_iv" , + .permissions =PROC_RW , + .type =SUSPEND_PROC_DATA_INTEGER , + .data = { + .integer = { + .variable = &suspend_encryptor_save_key_and_iv, + .minimum =0 , + .maximum =1 , + } + } +}, + +{ + .filename ="encryption_key" , + .permissions =PROC_RW , + .type =SUSPEND_PROC_DATA_STRING , + .data = { + .string = { + .variable = suspend_encryptor_key, + .max_length =255 , + } + } +}, + +{ + .filename ="encryption_iv" , + .permissions =PROC_RW , + .type =SUSPEND_PROC_DATA_STRING , + .data = { + .string = { + .variable = suspend_encryptor_iv, + .max_length =255 , + } + } +}, + +{ + .filename ="disable_encryption" , + .permissions =PROC_RW , + .type =SUSPEND_PROC_DATA_INTEGER , + .data = { + .integer = { + .variable = &suspend_encryption_ops.disabled, + .minimum =0 , + .maximum =1 , + } + } +}, + +}; +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 11/12 Encryption module accessors structure.
In reply to Nigel CunninghamAdd the struct that lets the encryption functions be used by the core.
kernel/power/encryption.c | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-)
kernel/power/encryption.c +21 -0
@@ -482,3 +482,24 @@ static struct suspend_proc_data proc_par }; +/* +* Ops structure. +*/ + +static struct suspend_module_ops suspend_encryption_ops = { + .type =FILTER_MODULE , + .name ="Encryptor" , + .module =THIS_MODULE , + .memory_needed = suspend_encrypt_memory_needed, + .print_debug_info = suspend_encrypt_print_debug_stats, + .save_config_info = suspend_encrypt_save_config_info, + .load_config_info = suspend_encrypt_load_config_info, + .storage_needed = suspend_encrypt_storage_needed, + + .rw_init = suspend_encrypt_rw_init, + .rw_cleanup = suspend_encrypt_rw_cleanup, + + .write_chunk = suspend_encrypt_write_chunk, + .read_chunk = suspend_encrypt_read_chunk, +}; +
--
Nigel Cunningham nigel at suspend2 dot net
PATCH 12/12 Load and unload routines.
In reply to Nigel CunninghamAdd routines to register and deregister the encryption support and proc entries on load and unload.
kernel/power/encryption.c | 33 +++++++++++++++++++++++++++++++++ 1 files changed, 33 insertions(+), 0 deletions(-)
kernel/power/encryption.c +33 -0
@@ -503,3 +503,36 @@ static struct suspend_module_ops suspend .read_chunk = suspend_encrypt_read_chunk, }; +/* ---- Registration ---- */ + +static __init int suspend_encrypt_load (void ) +{ +int result; +int i, numfiles =sizeof (proc_params) /sizeof (struct suspend_proc_data ); + +printk ("Suspend2 Encryption Driver loading.\n" ); +if (!(result =suspend_register_module (&suspend_encryption_ops))) { +for (i=0 ; i< numfiles; i++) +suspend_register_procfile (&proc_params[i]); + }else +printk ("Suspend2 Encryption Driver unable to register!\n" ); +return result; +} + +#ifdef MODULE +static __exit void suspend_compress_unload(void) +{ +printk ("Suspend2 Encryption Driver unloading.\n" ); +for (i=0 ; i< numfiles; i++) +suspend_unregister_procfile (&proc_params[i]); +suspend_unregister_module (&suspend_encryption_ops); +} + +module_init (suspend_encrypt_load); +module_exit (suspend_encrypt_unload); +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("Nigel Cunningham" ); +MODULE_DESCRIPTION ("Encryption Support for Suspend2" ); +#else +late_initcall (suspend_encrypt_load); +#endif
--
Nigel Cunningham nigel at suspend2 dot net