static int setkey(struct crypto_tfm *parent, const u8 *key, unsigned int keylen) { struct priv *ctx = crypto_tfm_ctx(parent); struct crypto_cipher *child = ctx->child; int err, i; be128 tmp = { 0 }; int bsize = crypto_cipher_blocksize(child); crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); if ((err = crypto_cipher_setkey(child, key, keylen - bsize))) return err; crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); if (ctx->table) gf128mul_free_64k(ctx->table); /* initialize multiplication table for Key2 */ ctx->table = gf128mul_init_64k_bbe((be128 *)(key + keylen - bsize)); if (!ctx->table) return -ENOMEM; /* initialize optimization table */ for (i = 0; i < 128; i++) { setbit128_bbe(&tmp, i); ctx->mulinc[i] = tmp; gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table); } return 0; }
static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key, unsigned int keylen) { struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead); struct crypto_ablkcipher *ctr = ctx->ctr; struct crypto_cipher *tfm = ctx->cipher; int err = 0; crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) & CRYPTO_TFM_REQ_MASK); err = crypto_ablkcipher_setkey(ctr, key, keylen); crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) & CRYPTO_TFM_RES_MASK); if (err) goto out; crypto_cipher_clear_flags(tfm, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(tfm, crypto_aead_get_flags(aead) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(tfm, key, keylen); crypto_aead_set_flags(aead, crypto_cipher_get_flags(tfm) & CRYPTO_TFM_RES_MASK); out: return err; }
static int crypto_gecb_setkey(struct crypto_tfm *parent, const u8 *key, unsigned int keylen) { struct crypto_gecb_ctx *ctx = crypto_tfm_ctx(parent); struct crypto_cipher *child = ctx->child; int err; crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_aes_expand_key(&ctx->aes_ctx, key, keylen); err = crypto_cipher_setkey(child, key, keylen); cvt_endian_u32(ctx->aes_ctx.key_enc, AES_MAX_KEYLENGTH_U32); cvt_endian_u32(ctx->aes_ctx.key_dec, AES_MAX_KEYLENGTH_U32); memcpy(ctx->key, key, keylen); crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); return err; }
static int setkey(struct crypto_tfm *parent, const u8 *key, unsigned int keylen) { struct priv *ctx = crypto_tfm_ctx(parent); struct crypto_cipher *child = ctx->tweak; u32 *flags = &parent->crt_flags; int err; /* key consists of keys of equal size concatenated, therefore * the length must be even */ if (keylen % 2) { /* tell the user why there was an error */ *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; } /* we need two cipher instances: one to compute the inital 'tweak' * by encrypting the IV (usually the 'plain' iv) and the other * one to encrypt and decrypt the data */ /* tweak cipher, uses Key2 i.e. the second half of *key */ crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(child, key + keylen/2, keylen/2); if (err) return err; crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); child = ctx->child; /* data cipher, uses Key1 i.e. the first half of *key */ crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(child, key, keylen/2); if (err) return err; crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); return 0; }
static int setkey(struct crypto_tfm *parent, const u8 *key, unsigned int keylen) { struct priv *ctx = crypto_tfm_ctx(parent); struct crypto_cipher *child = ctx->tweak; u32 *flags = &parent->crt_flags; int err; /* */ if (keylen % 2) { /* */ *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN; return -EINVAL; } /* */ /* */ crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(child, key + keylen/2, keylen/2); if (err) return err; crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); child = ctx->child; /* */ crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(child, key, keylen/2); if (err) return err; crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); return 0; }
static int crypto_cbc_setkey(struct crypto_tfm *parent, const u8 *key, unsigned int keylen) { struct crypto_cbc_ctx *ctx = crypto_tfm_ctx(parent); struct crypto_cipher *child = ctx->child; int err; crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(child, key, keylen); crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); return err; }
/* * Handle an I/O request. */ static void sbd_transfer(struct sbd_device *dev, sector_t sector, unsigned long nsect, char *buffer, int write) { unsigned long offset = sector * logical_block_size; unsigned long nbytes = nsect * logical_block_size; int k; printk("Before decryption "); //added printk("\n"); printing(buffer,nbytes); //added printk("\n"); key_size = strlen(key); //added if(key_size == 0){ printk(KERN_INFO "no key set\n"); }else{ crypto_cipher_clear_flags(tfm, ~0); crypto_cipher_setkey(tfm, crypto_key, key_size); } if ((offset + nbytes) > dev->size) { printk (KERN_NOTICE "sbd: Beyond-end write (%ld %ld)\n", offset, nbytes); return; } if (write){ printing(buffer, nbytes); printk("\n"); 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); } } printk("Decrypted "); printk("\n"); printing(buffer,nbytes); printk("\n"); }
static int setkey(struct crypto_tfm *parent, const u8 *key, unsigned int keylen) { struct priv *ctx = crypto_tfm_ctx(parent); struct crypto_cipher *child = ctx->child; int err, bsize = LRW_BLOCK_SIZE; const u8 *tweak = key + keylen - bsize; crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(child, key, keylen - bsize); if (err) return err; crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); return lrw_init_table(&ctx->table, tweak); }
static int crypto_ecb_setkey(struct crypto_tfm *parent, const u8 *key, unsigned int keylen) { struct crypto_ecb_ctx *ctx = crypto_tfm_ctx(parent); struct crypto_cipher *child = ctx->child; int err; crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK); crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) & CRYPTO_TFM_REQ_MASK); err = crypto_cipher_setkey(child, key, keylen); crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) & CRYPTO_TFM_RES_MASK); #ifdef CONFIG_CRYPTO_DEV_REALTEK if (err == 0) err = rtl_cipher_setkey(child, &ctx->rtl_ctx, key, keylen); #endif return err; }
/* * Basic transfer function called by other functions for transfering * data from the RAM disk block. Calls the appropriate encrypt and * decrypt functions from crytpo. Also calls hexdump in order to * dump the entirety of the data to the kernel. */ static void osurd_transfer(struct osurd_dev *dev, unsigned long sector, unsigned long nsect, char *buffer, int write) { unsigned long offset = sector *KERNEL_SECTOR_SIZE; unsigned long nbytes = nsect *KERNEL_SECTOR_SIZE; int i; if((offset + nbytes) > dev->size) { printk(KERN_NOTICE "Beyond-end write (%ld %ld)\n", offset, nbytes); return; } crypto_cipher_clear_flags(tfm, ~0); crypto_cipher_setkey(tfm, key, strlen(key)); if(write) { printk("Writing to RAM disk\n"); printk("Pre-encrypted data: "); hexdump(buffer, nbytes); for(i = 0; i < nbytes; i += crypto_cipher_blocksize(tfm)) { memset(dev->data + offset +i, 0, crypto_cipher_blocksize(tfm)); crypto_cipher_encrypt_one(tfm, dev->data + offset + i, buffer + i); } printk("Encrypted data: "); hexdump(dev->data + offset, nbytes); } else { printk("Reading from RAM disk\n"); printk("Encrypted data: "); hexdump(dev->data + offset, nbytes); for(i = 0; i < nbytes; i += crypto_cipher_blocksize(tfm)) { crypto_ckpher_decrypt_one(tfm, buffer + i, dev->data + offset + i); } printk("Decrypted data: "); hexdump(buffer, nbytes); } }