Skip to content

Commit

Permalink
[CRYPTO] users: Use block ciphers where applicable
Browse files Browse the repository at this point in the history
This patch converts all remaining users to use the new block cipher type
where applicable.  It also changes all simple cipher operations to use
the new encrypt_one/decrypt_one interface.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
Herbert Xu committed Sep 21, 2006
1 parent 378c669 commit f12cc20
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 66 deletions.
32 changes: 19 additions & 13 deletions drivers/net/ppp_mppe.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* deprecated in 2.6
*/

#include <linux/err.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>
Expand Down Expand Up @@ -95,7 +96,7 @@ static inline void sha_pad_init(struct sha_pad *shapad)
* State for an MPPE (de)compressor.
*/
struct ppp_mppe_state {
struct crypto_tfm *arc4;
struct crypto_blkcipher *arc4;
struct crypto_tfm *sha1;
unsigned char *sha1_digest;
unsigned char master_key[MPPE_MAX_KEY_LEN];
Expand Down Expand Up @@ -156,14 +157,15 @@ static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
{
unsigned char InterimKey[MPPE_MAX_KEY_LEN];
struct scatterlist sg_in[1], sg_out[1];
struct blkcipher_desc desc = { .tfm = state->arc4 };

get_new_key_from_sha(state, InterimKey);
if (!initial_key) {
crypto_cipher_setkey(state->arc4, InterimKey, state->keylen);
crypto_blkcipher_setkey(state->arc4, InterimKey, state->keylen);
setup_sg(sg_in, InterimKey, state->keylen);
setup_sg(sg_out, state->session_key, state->keylen);
if (crypto_cipher_encrypt(state->arc4, sg_out, sg_in,
state->keylen) != 0) {
if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in,
state->keylen) != 0) {
printk(KERN_WARNING "mppe_rekey: cipher_encrypt failed\n");
}
} else {
Expand All @@ -175,7 +177,7 @@ static void mppe_rekey(struct ppp_mppe_state * state, int initial_key)
state->session_key[1] = 0x26;
state->session_key[2] = 0x9e;
}
crypto_cipher_setkey(state->arc4, state->session_key, state->keylen);
crypto_blkcipher_setkey(state->arc4, state->session_key, state->keylen);
}

/*
Expand All @@ -196,9 +198,11 @@ static void *mppe_alloc(unsigned char *options, int optlen)

memset(state, 0, sizeof(*state));

state->arc4 = crypto_alloc_tfm("arc4", 0);
if (!state->arc4)
state->arc4 = crypto_alloc_blkcipher("ecb(arc4)", 0, CRYPTO_ALG_ASYNC);
if (IS_ERR(state->arc4)) {
state->arc4 = NULL;
goto out_free;
}

state->sha1 = crypto_alloc_tfm("sha1", 0);
if (!state->sha1)
Expand Down Expand Up @@ -231,7 +235,7 @@ static void *mppe_alloc(unsigned char *options, int optlen)
if (state->sha1)
crypto_free_tfm(state->sha1);
if (state->arc4)
crypto_free_tfm(state->arc4);
crypto_free_blkcipher(state->arc4);
kfree(state);
out:
return NULL;
Expand All @@ -249,7 +253,7 @@ static void mppe_free(void *arg)
if (state->sha1)
crypto_free_tfm(state->sha1);
if (state->arc4)
crypto_free_tfm(state->arc4);
crypto_free_blkcipher(state->arc4);
kfree(state);
}
}
Expand Down Expand Up @@ -356,6 +360,7 @@ mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
int isize, int osize)
{
struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
struct blkcipher_desc desc = { .tfm = state->arc4 };
int proto;
struct scatterlist sg_in[1], sg_out[1];

Expand Down Expand Up @@ -413,7 +418,7 @@ mppe_compress(void *arg, unsigned char *ibuf, unsigned char *obuf,
/* Encrypt packet */
setup_sg(sg_in, ibuf, isize);
setup_sg(sg_out, obuf, osize);
if (crypto_cipher_encrypt(state->arc4, sg_out, sg_in, isize) != 0) {
if (crypto_blkcipher_encrypt(&desc, sg_out, sg_in, isize) != 0) {
printk(KERN_DEBUG "crypto_cypher_encrypt failed\n");
return -1;
}
Expand Down Expand Up @@ -462,6 +467,7 @@ mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
int osize)
{
struct ppp_mppe_state *state = (struct ppp_mppe_state *) arg;
struct blkcipher_desc desc = { .tfm = state->arc4 };
unsigned ccount;
int flushed = MPPE_BITS(ibuf) & MPPE_BIT_FLUSHED;
int sanity = 0;
Expand Down Expand Up @@ -599,7 +605,7 @@ mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
*/
setup_sg(sg_in, ibuf, 1);
setup_sg(sg_out, obuf, 1);
if (crypto_cipher_decrypt(state->arc4, sg_out, sg_in, 1) != 0) {
if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, 1) != 0) {
printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
return DECOMP_ERROR;
}
Expand All @@ -619,7 +625,7 @@ mppe_decompress(void *arg, unsigned char *ibuf, int isize, unsigned char *obuf,
/* And finally, decrypt the rest of the packet. */
setup_sg(sg_in, ibuf + 1, isize - 1);
setup_sg(sg_out, obuf + 1, osize - 1);
if (crypto_cipher_decrypt(state->arc4, sg_out, sg_in, isize - 1) != 0) {
if (crypto_blkcipher_decrypt(&desc, sg_out, sg_in, isize - 1)) {
printk(KERN_DEBUG "crypto_cypher_decrypt failed\n");
return DECOMP_ERROR;
}
Expand Down Expand Up @@ -694,7 +700,7 @@ static struct compressor ppp_mppe = {
static int __init ppp_mppe_init(void)
{
int answer;
if (!(crypto_alg_available("arc4", 0) &&
if (!(crypto_alg_available("ecb(arc4)", 0) &&
crypto_alg_available("sha1", 0)))
return -ENODEV;

Expand Down
22 changes: 12 additions & 10 deletions drivers/net/wireless/airo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
======================================================================*/

#include <linux/err.h>
#include <linux/init.h>

#include <linux/kernel.h>
Expand Down Expand Up @@ -1203,7 +1204,7 @@ struct airo_info {
struct iw_spy_data spy_data;
struct iw_public_data wireless_data;
/* MIC stuff */
struct crypto_tfm *tfm;
struct crypto_cipher *tfm;
mic_module mod[2];
mic_statistics micstats;
HostRxDesc rxfids[MPI_MAX_FIDS]; // rx/tx/config MPI350 descriptors
Expand Down Expand Up @@ -1271,7 +1272,8 @@ static int flashrestart(struct airo_info *ai,struct net_device *dev);

static int RxSeqValid (struct airo_info *ai,miccntx *context,int mcast,u32 micSeq);
static void MoveWindow(miccntx *context, u32 micSeq);
static void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen, struct crypto_tfm *);
static void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen,
struct crypto_cipher *tfm);
static void emmh32_init(emmh32_context *context);
static void emmh32_update(emmh32_context *context, u8 *pOctets, int len);
static void emmh32_final(emmh32_context *context, u8 digest[4]);
Expand Down Expand Up @@ -1339,10 +1341,11 @@ static int micsetup(struct airo_info *ai) {
int i;

if (ai->tfm == NULL)
ai->tfm = crypto_alloc_tfm("aes", CRYPTO_TFM_REQ_MAY_SLEEP);
ai->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);

if (ai->tfm == NULL) {
if (IS_ERR(ai->tfm)) {
airo_print_err(ai->dev->name, "failed to load transform for AES");
ai->tfm = NULL;
return ERROR;
}

Expand Down Expand Up @@ -1608,15 +1611,15 @@ static void MoveWindow(miccntx *context, u32 micSeq)
static unsigned char aes_counter[16];

/* expand the key to fill the MMH coefficient array */
static void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen, struct crypto_tfm *tfm)
static void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen,
struct crypto_cipher *tfm)
{
/* take the keying material, expand if necessary, truncate at 16-bytes */
/* run through AES counter mode to generate context->coeff[] */

int i,j;
u32 counter;
u8 *cipher, plain[16];
struct scatterlist sg[1];

crypto_cipher_setkey(tfm, pkey, 16);
counter = 0;
Expand All @@ -1627,9 +1630,8 @@ static void emmh32_setseed(emmh32_context *context, u8 *pkey, int keylen, struct
aes_counter[12] = (u8)(counter >> 24);
counter++;
memcpy (plain, aes_counter, 16);
sg_set_buf(sg, plain, 16);
crypto_cipher_encrypt(tfm, sg, sg, 16);
cipher = kmap(sg->page) + sg->offset;
crypto_cipher_encrypt_one(tfm, plain, plain);
cipher = plain;
for (j=0; (j<16) && (i< (sizeof(context->coeff)/sizeof(context->coeff[0]))); ) {
context->coeff[i++] = ntohl(*(u32 *)&cipher[j]);
j += 4;
Expand Down Expand Up @@ -2432,7 +2434,7 @@ void stop_airo_card( struct net_device *dev, int freeres )
ai->shared, ai->shared_dma);
}
}
crypto_free_tfm(ai->tfm);
crypto_free_cipher(ai->tfm);
del_airo_dev( dev );
free_netdev( dev );
}
Expand Down
32 changes: 12 additions & 20 deletions net/ieee80211/ieee80211_crypt_ccmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* more details.
*/

#include <linux/err.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
Expand Down Expand Up @@ -48,28 +49,18 @@ struct ieee80211_ccmp_data {

int key_idx;

struct crypto_tfm *tfm;
struct crypto_cipher *tfm;

/* scratch buffers for virt_to_page() (crypto API) */
u8 tx_b0[AES_BLOCK_LEN], tx_b[AES_BLOCK_LEN],
tx_e[AES_BLOCK_LEN], tx_s0[AES_BLOCK_LEN];
u8 rx_b0[AES_BLOCK_LEN], rx_b[AES_BLOCK_LEN], rx_a[AES_BLOCK_LEN];
};

static void ieee80211_ccmp_aes_encrypt(struct crypto_tfm *tfm,
const u8 pt[16], u8 ct[16])
static inline void ieee80211_ccmp_aes_encrypt(struct crypto_cipher *tfm,
const u8 pt[16], u8 ct[16])
{
struct scatterlist src, dst;

src.page = virt_to_page(pt);
src.offset = offset_in_page(pt);
src.length = AES_BLOCK_LEN;

dst.page = virt_to_page(ct);
dst.offset = offset_in_page(ct);
dst.length = AES_BLOCK_LEN;

crypto_cipher_encrypt(tfm, &dst, &src, AES_BLOCK_LEN);
crypto_cipher_encrypt_one(tfm, ct, pt);
}

static void *ieee80211_ccmp_init(int key_idx)
Expand All @@ -81,10 +72,11 @@ static void *ieee80211_ccmp_init(int key_idx)
goto fail;
priv->key_idx = key_idx;

priv->tfm = crypto_alloc_tfm("aes", 0);
if (priv->tfm == NULL) {
priv->tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC);
if (IS_ERR(priv->tfm)) {
printk(KERN_DEBUG "ieee80211_crypt_ccmp: could not allocate "
"crypto API aes\n");
priv->tfm = NULL;
goto fail;
}

Expand All @@ -93,7 +85,7 @@ static void *ieee80211_ccmp_init(int key_idx)
fail:
if (priv) {
if (priv->tfm)
crypto_free_tfm(priv->tfm);
crypto_free_cipher(priv->tfm);
kfree(priv);
}

Expand All @@ -104,7 +96,7 @@ static void ieee80211_ccmp_deinit(void *priv)
{
struct ieee80211_ccmp_data *_priv = priv;
if (_priv && _priv->tfm)
crypto_free_tfm(_priv->tfm);
crypto_free_cipher(_priv->tfm);
kfree(priv);
}

Expand All @@ -115,7 +107,7 @@ static inline void xor_block(u8 * b, u8 * a, size_t len)
b[i] ^= a[i];
}

static void ccmp_init_blocks(struct crypto_tfm *tfm,
static void ccmp_init_blocks(struct crypto_cipher *tfm,
struct ieee80211_hdr_4addr *hdr,
u8 * pn, size_t dlen, u8 * b0, u8 * auth, u8 * s0)
{
Expand Down Expand Up @@ -377,7 +369,7 @@ static int ieee80211_ccmp_set_key(void *key, int len, u8 * seq, void *priv)
{
struct ieee80211_ccmp_data *data = priv;
int keyidx;
struct crypto_tfm *tfm = data->tfm;
struct crypto_cipher *tfm = data->tfm;

keyidx = data->key_idx;
memset(data, 0, sizeof(*data));
Expand Down
Loading

0 comments on commit f12cc20

Please sign in to comment.