Skip to content

Commit

Permalink
Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
Browse files Browse the repository at this point in the history
* master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6:
  [CRYPTO] padlock: Remove pointless padlock module
  [CRYPTO] api: Add ablkcipher_request_set_tfm
  [CRYPTO] cryptd: Add software async crypto daemon
  [CRYPTO] api: Do not remove users unless new algorithm matches
  [CRYPTO] cryptomgr: Fix parsing of nested templates 
  [CRYPTO] api: Add async blkcipher type
  [CRYPTO] templates: Pass type/mask when creating instances
  [CRYPTO] tcrypt: Use async blkcipher interface
  [CRYPTO] api: Add async block cipher interface
  [CRYPTO] api: Proc functions should be marked as unused
  • Loading branch information
Linus Torvalds committed May 5, 2007
2 parents 253f04e + f6259de commit 6adae5d
Show file tree
Hide file tree
Showing 20 changed files with 1,166 additions and 199 deletions.
13 changes: 13 additions & 0 deletions crypto/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ config CRYPTO_ALGAPI
help
This option provides the API for cryptographic algorithms.

config CRYPTO_ABLKCIPHER
tristate
select CRYPTO_BLKCIPHER

config CRYPTO_BLKCIPHER
tristate
select CRYPTO_ALGAPI
Expand Down Expand Up @@ -171,6 +175,15 @@ config CRYPTO_LRW
The first 128, 192 or 256 bits in the key are used for AES and the
rest is used to tie each cipher block to its logical position.

config CRYPTO_CRYPTD
tristate "Software async crypto daemon"
select CRYPTO_ABLKCIPHER
select CRYPTO_MANAGER
help
This is a generic software asynchronous crypto daemon that
converts an arbitrary synchronous software crypto algorithm
into an asynchronous algorithm that executes in a kernel thread.

config CRYPTO_DES
tristate "DES and Triple DES EDE cipher algorithms"
select CRYPTO_ALGAPI
Expand Down
2 changes: 2 additions & 0 deletions crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crypto_algapi-$(CONFIG_PROC_FS) += proc.o
crypto_algapi-objs := algapi.o $(crypto_algapi-y)
obj-$(CONFIG_CRYPTO_ALGAPI) += crypto_algapi.o

obj-$(CONFIG_CRYPTO_ABLKCIPHER) += ablkcipher.o
obj-$(CONFIG_CRYPTO_BLKCIPHER) += blkcipher.o

crypto_hash-objs := hash.o
Expand All @@ -29,6 +30,7 @@ obj-$(CONFIG_CRYPTO_ECB) += ecb.o
obj-$(CONFIG_CRYPTO_CBC) += cbc.o
obj-$(CONFIG_CRYPTO_PCBC) += pcbc.o
obj-$(CONFIG_CRYPTO_LRW) += lrw.o
obj-$(CONFIG_CRYPTO_CRYPTD) += cryptd.o
obj-$(CONFIG_CRYPTO_DES) += des.o
obj-$(CONFIG_CRYPTO_FCRYPT) += fcrypt.o
obj-$(CONFIG_CRYPTO_BLOWFISH) += blowfish.o
Expand Down
83 changes: 83 additions & 0 deletions crypto/ablkcipher.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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/algapi.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/seq_file.h>

static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
unsigned int keylen)
{
struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);

if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
return -EINVAL;
}

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);
seq_printf(m, "qlen : %u\n", ablkcipher->queue->qlen);
seq_printf(m, "max qlen : %u\n", ablkcipher->queue->max_qlen);
}

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);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Asynchronous block chaining cipher type");
169 changes: 137 additions & 32 deletions crypto/algapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,47 @@ static void crypto_destroy_instance(struct crypto_alg *alg)
crypto_tmpl_put(tmpl);
}

static void crypto_remove_spawns(struct list_head *spawns,
struct list_head *list)
static void crypto_remove_spawn(struct crypto_spawn *spawn,
struct list_head *list,
struct list_head *secondary_spawns)
{
struct crypto_spawn *spawn, *n;
struct crypto_instance *inst = spawn->inst;
struct crypto_template *tmpl = inst->tmpl;

list_for_each_entry_safe(spawn, n, spawns, list) {
struct crypto_instance *inst = spawn->inst;
struct crypto_template *tmpl = inst->tmpl;
list_del_init(&spawn->list);
spawn->alg = NULL;

list_del_init(&spawn->list);
spawn->alg = NULL;
if (crypto_is_dead(&inst->alg))
return;

if (crypto_is_dead(&inst->alg))
continue;
inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
if (!tmpl || !crypto_tmpl_get(tmpl))
return;

inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
if (!tmpl || !crypto_tmpl_get(tmpl))
crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
list_move(&inst->alg.cra_list, list);
hlist_del(&inst->list);
inst->alg.cra_destroy = crypto_destroy_instance;

list_splice(&inst->alg.cra_users, secondary_spawns);
}

static void crypto_remove_spawns(struct list_head *spawns,
struct list_head *list, u32 new_type)
{
struct crypto_spawn *spawn, *n;
LIST_HEAD(secondary_spawns);

list_for_each_entry_safe(spawn, n, spawns, list) {
if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
continue;

crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
list_move(&inst->alg.cra_list, list);
hlist_del(&inst->list);
inst->alg.cra_destroy = crypto_destroy_instance;
crypto_remove_spawn(spawn, list, &secondary_spawns);
}

if (!list_empty(&inst->alg.cra_users)) {
if (&n->list == spawns)
n = list_entry(inst->alg.cra_users.next,
typeof(*n), list);
__list_splice(&inst->alg.cra_users, spawns->prev);
}
while (!list_empty(&secondary_spawns)) {
list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
crypto_remove_spawn(spawn, list, &secondary_spawns);
}
}

Expand Down Expand Up @@ -164,7 +175,7 @@ static int __crypto_register_alg(struct crypto_alg *alg,
q->cra_priority > alg->cra_priority)
continue;

crypto_remove_spawns(&q->cra_users, list);
crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
}

list_add(&alg->cra_list, &crypto_alg_list);
Expand Down Expand Up @@ -214,7 +225,7 @@ static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)

crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
list_del_init(&alg->cra_list);
crypto_remove_spawns(&alg->cra_users, list);
crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);

return 0;
}
Expand Down Expand Up @@ -351,11 +362,12 @@ int crypto_register_instance(struct crypto_template *tmpl,
EXPORT_SYMBOL_GPL(crypto_register_instance);

int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
struct crypto_instance *inst)
struct crypto_instance *inst, u32 mask)
{
int err = -EAGAIN;

spawn->inst = inst;
spawn->mask = mask;

down_write(&crypto_alg_sem);
if (!crypto_is_moribund(alg)) {
Expand Down Expand Up @@ -425,15 +437,45 @@ int crypto_unregister_notifier(struct notifier_block *nb)
}
EXPORT_SYMBOL_GPL(crypto_unregister_notifier);

struct crypto_alg *crypto_get_attr_alg(void *param, unsigned int len,
u32 type, u32 mask)
struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
{
struct rtattr *rta = tb[CRYPTOA_TYPE - 1];
struct crypto_attr_type *algt;

if (!rta)
return ERR_PTR(-ENOENT);
if (RTA_PAYLOAD(rta) < sizeof(*algt))
return ERR_PTR(-EINVAL);

algt = RTA_DATA(rta);

return algt;
}
EXPORT_SYMBOL_GPL(crypto_get_attr_type);

int crypto_check_attr_type(struct rtattr **tb, u32 type)
{
struct rtattr *rta = param;
struct crypto_attr_type *algt;

algt = crypto_get_attr_type(tb);
if (IS_ERR(algt))
return PTR_ERR(algt);

if ((algt->type ^ type) & algt->mask)
return -EINVAL;

return 0;
}
EXPORT_SYMBOL_GPL(crypto_check_attr_type);

struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask)
{
struct rtattr *rta = tb[CRYPTOA_ALG - 1];
struct crypto_attr_alg *alga;

if (!RTA_OK(rta, len))
return ERR_PTR(-EBADR);
if (rta->rta_type != CRYPTOA_ALG || RTA_PAYLOAD(rta) < sizeof(*alga))
if (!rta)
return ERR_PTR(-ENOENT);
if (RTA_PAYLOAD(rta) < sizeof(*alga))
return ERR_PTR(-EINVAL);

alga = RTA_DATA(rta);
Expand Down Expand Up @@ -464,7 +506,8 @@ struct crypto_instance *crypto_alloc_instance(const char *name,
goto err_free_inst;

spawn = crypto_instance_ctx(inst);
err = crypto_init_spawn(spawn, alg, inst);
err = crypto_init_spawn(spawn, alg, inst,
CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);

if (err)
goto err_free_inst;
Expand All @@ -477,6 +520,68 @@ struct crypto_instance *crypto_alloc_instance(const char *name,
}
EXPORT_SYMBOL_GPL(crypto_alloc_instance);

void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
{
INIT_LIST_HEAD(&queue->list);
queue->backlog = &queue->list;
queue->qlen = 0;
queue->max_qlen = max_qlen;
}
EXPORT_SYMBOL_GPL(crypto_init_queue);

int crypto_enqueue_request(struct crypto_queue *queue,
struct crypto_async_request *request)
{
int err = -EINPROGRESS;

if (unlikely(queue->qlen >= queue->max_qlen)) {
err = -EBUSY;
if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
goto out;
if (queue->backlog == &queue->list)
queue->backlog = &request->list;
}

queue->qlen++;
list_add_tail(&request->list, &queue->list);

out:
return err;
}
EXPORT_SYMBOL_GPL(crypto_enqueue_request);

struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
{
struct list_head *request;

if (unlikely(!queue->qlen))
return NULL;

queue->qlen--;

if (queue->backlog != &queue->list)
queue->backlog = queue->backlog->next;

request = queue->list.next;
list_del(request);

return list_entry(request, struct crypto_async_request, list);
}
EXPORT_SYMBOL_GPL(crypto_dequeue_request);

int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
{
struct crypto_async_request *req;

list_for_each_entry(req, &queue->list, list) {
if (req->tfm == tfm)
return 1;
}

return 0;
}
EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);

static int __init crypto_algapi_init(void)
{
crypto_init_proc();
Expand Down
Loading

0 comments on commit 6adae5d

Please sign in to comment.