Skip to content

Instantly share code, notes, and snippets.

@stumped2
Created November 11, 2013 02:22
Show Gist options
  • Save stumped2/7406762 to your computer and use it in GitHub Desktop.
Save stumped2/7406762 to your computer and use it in GitHub Desktop.
#define KEY_SIZE 32 /* AES has a maximum key size of 256 bits */
static char crypto_key[KEY_SIZE];
static int key_size = 0; /* size of the current key */
// Put a check that key size must be 16, 24 or 32 bytes long, required by AES.
static void sbd_transfer(struct sbd_device *dev, sector_t sector,
unsigned long nsect, char *buffer, int write) {
... // check if key set
}else{
crypto_cipher_clear_flags(tfm, ~0);
crypto_cipher_setkey(tfm, crypto_key, key_size);
}
...
if (write){
if(key_size != 0){
for (k = 0; k < nbytes; k+=
crypto_cipher_blocksize(tfm)) {
crypto_cipher_encrypt_one(tfm, dev->data+
offset+k, buffer+k);
}
}else{
memcpy(dev->data + offset, buffer, nbytes);
}
}else{
if(key_size != 0){
for (k = 0; k < nbytes; k+=
crypto_cipher_blocksize(tfm)) {
crypto_cipher_decrypt_one(tfm, buffer+k,
dev->data+offset+k);
}
}else{
memcpy(buffer, dev->data + offset, nbytes);
}
}
static int __init sbd_init(void) {
tfm = crypto_alloc_cipher("aes", 0, 16);
if (IS_ERR(tfm)){
printk(KERN_ERR "alg: cipher: Failed to load transform");
return PTR_ERR(tfm);
}
...
static void __exit sbd_exit(void) {
...
crypto_free_cipher(tfm);
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment