mirror of
https://github.com/adulau/aha.git
synced 2024-12-28 19:56:18 +00:00
378f4f51f9
Note: From now on the collective of ablkcipher/blkcipher/givcipher will be known as skcipher, i.e., symmetric key cipher. The name blkcipher has always been much of a misnomer since it supports stream ciphers too. This patch adds the function crypto_grab_skcipher as a new way of getting an ablkcipher spawn. The problem is that previously we did this in two steps, first getting the algorithm and then calling crypto_init_spawn. This meant that each spawn user had to be aware of what type and mask to use for these two steps. This is difficult and also presents a problem when the type/mask changes as they're about to be for IV generators. The new interface does both steps together just like crypto_alloc_ablkcipher. As a side-effect this also allows us to be stronger on type enforcement for spawns. For now this is only done for ablkcipher but it's trivial to extend for other types. This patch also moves the type/mask logic for skcipher into the helpers crypto_skcipher_type and crypto_skcipher_mask. Finally this patch introduces the function crypto_require_sync to determine whether the user is specifically requesting a sync algorithm. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
130 lines
3.5 KiB
C
130 lines
3.5 KiB
C
/*
|
|
* Asynchronous block chaining cipher operations.
|
|
*
|
|
* This is the asynchronous version of blkcipher.c indicating completion
|
|
* via a callback.
|
|
*
|
|
* Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
* Software Foundation; either version 2 of the License, or (at your option)
|
|
* any later version.
|
|
*
|
|
*/
|
|
|
|
#include <crypto/internal/skcipher.h>
|
|
#include <linux/err.h>
|
|
#include <linux/init.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/seq_file.h>
|
|
|
|
#include "internal.h"
|
|
|
|
static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
|
|
unsigned int keylen)
|
|
{
|
|
struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
|
|
unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
|
|
int ret;
|
|
u8 *buffer, *alignbuffer;
|
|
unsigned long absize;
|
|
|
|
absize = keylen + alignmask;
|
|
buffer = kmalloc(absize, GFP_ATOMIC);
|
|
if (!buffer)
|
|
return -ENOMEM;
|
|
|
|
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
|
|
memcpy(alignbuffer, key, keylen);
|
|
ret = cipher->setkey(tfm, alignbuffer, keylen);
|
|
memset(alignbuffer, 0, keylen);
|
|
kfree(buffer);
|
|
return ret;
|
|
}
|
|
|
|
static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
|
|
unsigned int keylen)
|
|
{
|
|
struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
|
|
unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
|
|
|
|
if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
|
|
crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
|
|
return -EINVAL;
|
|
}
|
|
|
|
if ((unsigned long)key & alignmask)
|
|
return setkey_unaligned(tfm, key, keylen);
|
|
|
|
return cipher->setkey(tfm, key, keylen);
|
|
}
|
|
|
|
static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
|
|
u32 mask)
|
|
{
|
|
return alg->cra_ctxsize;
|
|
}
|
|
|
|
static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
|
|
u32 mask)
|
|
{
|
|
struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
|
|
struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
|
|
|
|
if (alg->ivsize > PAGE_SIZE / 8)
|
|
return -EINVAL;
|
|
|
|
crt->setkey = setkey;
|
|
crt->encrypt = alg->encrypt;
|
|
crt->decrypt = alg->decrypt;
|
|
crt->ivsize = alg->ivsize;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
|
|
__attribute__ ((unused));
|
|
static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
|
|
{
|
|
struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
|
|
|
|
seq_printf(m, "type : ablkcipher\n");
|
|
seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
|
|
seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
|
|
seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
|
|
seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
|
|
}
|
|
|
|
const struct crypto_type crypto_ablkcipher_type = {
|
|
.ctxsize = crypto_ablkcipher_ctxsize,
|
|
.init = crypto_init_ablkcipher_ops,
|
|
#ifdef CONFIG_PROC_FS
|
|
.show = crypto_ablkcipher_show,
|
|
#endif
|
|
};
|
|
EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
|
|
|
|
int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
|
|
u32 type, u32 mask)
|
|
{
|
|
struct crypto_alg *alg;
|
|
int err;
|
|
|
|
type = crypto_skcipher_type(type);
|
|
mask = crypto_skcipher_mask(mask);
|
|
|
|
alg = crypto_alg_mod_lookup(name, type, mask);
|
|
if (IS_ERR(alg))
|
|
return PTR_ERR(alg);
|
|
|
|
err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
|
|
crypto_mod_put(alg);
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
|
|
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_DESCRIPTION("Asynchronous block chaining cipher type");
|