diff options
| author | Herbert Xu <herbert@gondor.apana.org.au> | 2007-04-16 06:48:54 -0400 |
|---|---|---|
| committer | Herbert Xu <herbert@gondor.apana.org.au> | 2007-05-02 00:38:31 -0400 |
| commit | b5b7f08869340aa8cfa23303f7d195f161479592 (patch) | |
| tree | dd1f3f00165e7ca31e29a52d64909439cdfab8fd | |
| parent | ebc610e5bc76df073221e64e86c3f7533a09ea40 (diff) | |
[CRYPTO] api: Add async blkcipher type
This patch adds the mid-level interface for asynchronous block ciphers.
It also includes a generic queueing mechanism that can be used by other
asynchronous crypto operations in future.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
| -rw-r--r-- | crypto/Kconfig | 4 | ||||
| -rw-r--r-- | crypto/Makefile | 1 | ||||
| -rw-r--r-- | crypto/ablkcipher.c | 83 | ||||
| -rw-r--r-- | crypto/algapi.c | 62 | ||||
| -rw-r--r-- | include/crypto/algapi.h | 58 | ||||
| -rw-r--r-- | include/linux/crypto.h | 22 |
6 files changed, 230 insertions, 0 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig index 086fcec44720..a20a3f14d324 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig | |||
| @@ -16,6 +16,10 @@ config CRYPTO_ALGAPI | |||
| 16 | help | 16 | help |
| 17 | This option provides the API for cryptographic algorithms. | 17 | This option provides the API for cryptographic algorithms. |
| 18 | 18 | ||
| 19 | config CRYPTO_ABLKCIPHER | ||
| 20 | tristate | ||
| 21 | select CRYPTO_BLKCIPHER | ||
| 22 | |||
| 19 | config CRYPTO_BLKCIPHER | 23 | config CRYPTO_BLKCIPHER |
| 20 | tristate | 24 | tristate |
| 21 | select CRYPTO_ALGAPI | 25 | select CRYPTO_ALGAPI |
diff --git a/crypto/Makefile b/crypto/Makefile index 12f93f578171..3820d4c5ccc2 100644 --- a/crypto/Makefile +++ b/crypto/Makefile | |||
| @@ -8,6 +8,7 @@ crypto_algapi-$(CONFIG_PROC_FS) += proc.o | |||
| 8 | crypto_algapi-objs := algapi.o $(crypto_algapi-y) | 8 | crypto_algapi-objs := algapi.o $(crypto_algapi-y) |
| 9 | obj-$(CONFIG_CRYPTO_ALGAPI) += crypto_algapi.o | 9 | obj-$(CONFIG_CRYPTO_ALGAPI) += crypto_algapi.o |
| 10 | 10 | ||
| 11 | obj-$(CONFIG_CRYPTO_ABLKCIPHER) += ablkcipher.o | ||
| 11 | obj-$(CONFIG_CRYPTO_BLKCIPHER) += blkcipher.o | 12 | obj-$(CONFIG_CRYPTO_BLKCIPHER) += blkcipher.o |
| 12 | 13 | ||
| 13 | crypto_hash-objs := hash.o | 14 | crypto_hash-objs := hash.o |
diff --git a/crypto/ablkcipher.c b/crypto/ablkcipher.c new file mode 100644 index 000000000000..9348ddd84a56 --- /dev/null +++ b/crypto/ablkcipher.c | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | /* | ||
| 2 | * Asynchronous block chaining cipher operations. | ||
| 3 | * | ||
| 4 | * This is the asynchronous version of blkcipher.c indicating completion | ||
| 5 | * via a callback. | ||
| 6 | * | ||
| 7 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> | ||
| 8 | * | ||
| 9 | * This program is free software; you can redistribute it and/or modify it | ||
| 10 | * under the terms of the GNU General Public License as published by the Free | ||
| 11 | * Software Foundation; either version 2 of the License, or (at your option) | ||
| 12 | * any later version. | ||
| 13 | * | ||
| 14 | */ | ||
| 15 | |||
| 16 | #include <crypto/algapi.h> | ||
| 17 | #include <linux/errno.h> | ||
| 18 | #include <linux/init.h> | ||
| 19 | #include <linux/module.h> | ||
| 20 | #include <linux/seq_file.h> | ||
| 21 | |||
| 22 | static int setkey(struct crypto_ablkcipher *tfm, const u8 *key, | ||
| 23 | unsigned int keylen) | ||
| 24 | { | ||
| 25 | struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm); | ||
| 26 | |||
| 27 | if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) { | ||
| 28 | crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); | ||
| 29 | return -EINVAL; | ||
| 30 | } | ||
| 31 | |||
| 32 | return cipher->setkey(tfm, key, keylen); | ||
| 33 | } | ||
| 34 | |||
| 35 | static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type, | ||
| 36 | u32 mask) | ||
| 37 | { | ||
| 38 | return alg->cra_ctxsize; | ||
| 39 | } | ||
| 40 | |||
| 41 | static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type, | ||
| 42 | u32 mask) | ||
| 43 | { | ||
| 44 | struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher; | ||
| 45 | struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher; | ||
| 46 | |||
| 47 | if (alg->ivsize > PAGE_SIZE / 8) | ||
| 48 | return -EINVAL; | ||
| 49 | |||
| 50 | crt->setkey = setkey; | ||
| 51 | crt->encrypt = alg->encrypt; | ||
| 52 | crt->decrypt = alg->decrypt; | ||
| 53 | crt->ivsize = alg->ivsize; | ||
| 54 | |||
| 55 | return 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg) | ||
| 59 | __attribute__ ((unused)); | ||
| 60 | static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg) | ||
| 61 | { | ||
| 62 | struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher; | ||
| 63 | |||
| 64 | seq_printf(m, "type : ablkcipher\n"); | ||
| 65 | seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); | ||
| 66 | seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize); | ||
| 67 | seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize); | ||
| 68 | seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize); | ||
| 69 | seq_printf(m, "qlen : %u\n", ablkcipher->queue->qlen); | ||
| 70 | seq_printf(m, "max qlen : %u\n", ablkcipher->queue->max_qlen); | ||
| 71 | } | ||
| 72 | |||
| 73 | const struct crypto_type crypto_ablkcipher_type = { | ||
| 74 | .ctxsize = crypto_ablkcipher_ctxsize, | ||
| 75 | .init = crypto_init_ablkcipher_ops, | ||
| 76 | #ifdef CONFIG_PROC_FS | ||
| 77 | .show = crypto_ablkcipher_show, | ||
| 78 | #endif | ||
| 79 | }; | ||
| 80 | EXPORT_SYMBOL_GPL(crypto_ablkcipher_type); | ||
| 81 | |||
| 82 | MODULE_LICENSE("GPL"); | ||
| 83 | MODULE_DESCRIPTION("Asynchronous block chaining cipher type"); | ||
diff --git a/crypto/algapi.c b/crypto/algapi.c index 491205e11cbe..1c2185b5b005 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c | |||
| @@ -507,6 +507,68 @@ err_free_inst: | |||
| 507 | } | 507 | } |
| 508 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); | 508 | EXPORT_SYMBOL_GPL(crypto_alloc_instance); |
| 509 | 509 | ||
| 510 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen) | ||
| 511 | { | ||
| 512 | INIT_LIST_HEAD(&queue->list); | ||
| 513 | queue->backlog = &queue->list; | ||
| 514 | queue->qlen = 0; | ||
| 515 | queue->max_qlen = max_qlen; | ||
| 516 | } | ||
| 517 | EXPORT_SYMBOL_GPL(crypto_init_queue); | ||
| 518 | |||
| 519 | int crypto_enqueue_request(struct crypto_queue *queue, | ||
| 520 | struct crypto_async_request *request) | ||
| 521 | { | ||
| 522 | int err = -EINPROGRESS; | ||
| 523 | |||
| 524 | if (unlikely(queue->qlen >= queue->max_qlen)) { | ||
| 525 | err = -EBUSY; | ||
| 526 | if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) | ||
| 527 | goto out; | ||
| 528 | if (queue->backlog == &queue->list) | ||
| 529 | queue->backlog = &request->list; | ||
| 530 | } | ||
| 531 | |||
| 532 | queue->qlen++; | ||
| 533 | list_add_tail(&request->list, &queue->list); | ||
| 534 | |||
| 535 | out: | ||
| 536 | return err; | ||
| 537 | } | ||
| 538 | EXPORT_SYMBOL_GPL(crypto_enqueue_request); | ||
| 539 | |||
| 540 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue) | ||
| 541 | { | ||
| 542 | struct list_head *request; | ||
| 543 | |||
| 544 | if (unlikely(!queue->qlen)) | ||
| 545 | return NULL; | ||
| 546 | |||
| 547 | queue->qlen--; | ||
| 548 | |||
| 549 | if (queue->backlog != &queue->list) | ||
| 550 | queue->backlog = queue->backlog->next; | ||
| 551 | |||
| 552 | request = queue->list.next; | ||
| 553 | list_del(request); | ||
| 554 | |||
| 555 | return list_entry(request, struct crypto_async_request, list); | ||
| 556 | } | ||
| 557 | EXPORT_SYMBOL_GPL(crypto_dequeue_request); | ||
| 558 | |||
| 559 | int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm) | ||
| 560 | { | ||
| 561 | struct crypto_async_request *req; | ||
| 562 | |||
| 563 | list_for_each_entry(req, &queue->list, list) { | ||
| 564 | if (req->tfm == tfm) | ||
| 565 | return 1; | ||
| 566 | } | ||
| 567 | |||
| 568 | return 0; | ||
| 569 | } | ||
| 570 | EXPORT_SYMBOL_GPL(crypto_tfm_in_queue); | ||
| 571 | |||
| 510 | static int __init crypto_algapi_init(void) | 572 | static int __init crypto_algapi_init(void) |
| 511 | { | 573 | { |
| 512 | crypto_init_proc(); | 574 | crypto_init_proc(); |
diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index d0c190b4d02f..469f511315c5 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h | |||
| @@ -13,6 +13,8 @@ | |||
| 13 | #define _CRYPTO_ALGAPI_H | 13 | #define _CRYPTO_ALGAPI_H |
| 14 | 14 | ||
| 15 | #include <linux/crypto.h> | 15 | #include <linux/crypto.h> |
| 16 | #include <linux/list.h> | ||
| 17 | #include <linux/kernel.h> | ||
| 16 | 18 | ||
| 17 | struct module; | 19 | struct module; |
| 18 | struct rtattr; | 20 | struct rtattr; |
| @@ -51,6 +53,14 @@ struct crypto_spawn { | |||
| 51 | struct crypto_instance *inst; | 53 | struct crypto_instance *inst; |
| 52 | }; | 54 | }; |
| 53 | 55 | ||
| 56 | struct crypto_queue { | ||
| 57 | struct list_head list; | ||
| 58 | struct list_head *backlog; | ||
| 59 | |||
| 60 | unsigned int qlen; | ||
| 61 | unsigned int max_qlen; | ||
| 62 | }; | ||
| 63 | |||
| 54 | struct scatter_walk { | 64 | struct scatter_walk { |
| 55 | struct scatterlist *sg; | 65 | struct scatterlist *sg; |
| 56 | unsigned int offset; | 66 | unsigned int offset; |
| @@ -82,6 +92,7 @@ struct blkcipher_walk { | |||
| 82 | int flags; | 92 | int flags; |
| 83 | }; | 93 | }; |
| 84 | 94 | ||
| 95 | extern const struct crypto_type crypto_ablkcipher_type; | ||
| 85 | extern const struct crypto_type crypto_blkcipher_type; | 96 | extern const struct crypto_type crypto_blkcipher_type; |
| 86 | extern const struct crypto_type crypto_hash_type; | 97 | extern const struct crypto_type crypto_hash_type; |
| 87 | 98 | ||
| @@ -103,6 +114,12 @@ struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask); | |||
| 103 | struct crypto_instance *crypto_alloc_instance(const char *name, | 114 | struct crypto_instance *crypto_alloc_instance(const char *name, |
| 104 | struct crypto_alg *alg); | 115 | struct crypto_alg *alg); |
| 105 | 116 | ||
| 117 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen); | ||
| 118 | int crypto_enqueue_request(struct crypto_queue *queue, | ||
| 119 | struct crypto_async_request *request); | ||
| 120 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue); | ||
| 121 | int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm); | ||
| 122 | |||
| 106 | int blkcipher_walk_done(struct blkcipher_desc *desc, | 123 | int blkcipher_walk_done(struct blkcipher_desc *desc, |
| 107 | struct blkcipher_walk *walk, int err); | 124 | struct blkcipher_walk *walk, int err); |
| 108 | int blkcipher_walk_virt(struct blkcipher_desc *desc, | 125 | int blkcipher_walk_virt(struct blkcipher_desc *desc, |
| @@ -125,6 +142,17 @@ static inline void *crypto_instance_ctx(struct crypto_instance *inst) | |||
| 125 | return inst->__ctx; | 142 | return inst->__ctx; |
| 126 | } | 143 | } |
| 127 | 144 | ||
| 145 | static inline struct ablkcipher_alg *crypto_ablkcipher_alg( | ||
| 146 | struct crypto_ablkcipher *tfm) | ||
| 147 | { | ||
| 148 | return &crypto_ablkcipher_tfm(tfm)->__crt_alg->cra_ablkcipher; | ||
| 149 | } | ||
| 150 | |||
| 151 | static inline void *crypto_ablkcipher_ctx(struct crypto_ablkcipher *tfm) | ||
| 152 | { | ||
| 153 | return crypto_tfm_ctx(&tfm->base); | ||
| 154 | } | ||
| 155 | |||
| 128 | static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm) | 156 | static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm) |
| 129 | { | 157 | { |
| 130 | return crypto_tfm_ctx(&tfm->base); | 158 | return crypto_tfm_ctx(&tfm->base); |
| @@ -172,5 +200,35 @@ static inline void blkcipher_walk_init(struct blkcipher_walk *walk, | |||
| 172 | walk->total = nbytes; | 200 | walk->total = nbytes; |
| 173 | } | 201 | } |
| 174 | 202 | ||
| 203 | static inline struct crypto_async_request *crypto_get_backlog( | ||
| 204 | struct crypto_queue *queue) | ||
| 205 | { | ||
| 206 | return queue->backlog == &queue->list ? NULL : | ||
| 207 | container_of(queue->backlog, struct crypto_async_request, list); | ||
| 208 | } | ||
| 209 | |||
| 210 | static inline int ablkcipher_enqueue_request(struct ablkcipher_alg *alg, | ||
| 211 | struct ablkcipher_request *request) | ||
| 212 | { | ||
| 213 | return crypto_enqueue_request(alg->queue, &request->base); | ||
| 214 | } | ||
| 215 | |||
| 216 | static inline struct ablkcipher_request *ablkcipher_dequeue_request( | ||
| 217 | struct ablkcipher_alg *alg) | ||
| 218 | { | ||
| 219 | return ablkcipher_request_cast(crypto_dequeue_request(alg->queue)); | ||
| 220 | } | ||
| 221 | |||
| 222 | static inline void *ablkcipher_request_ctx(struct ablkcipher_request *req) | ||
| 223 | { | ||
| 224 | return req->__ctx; | ||
| 225 | } | ||
| 226 | |||
| 227 | static inline int ablkcipher_tfm_in_queue(struct crypto_ablkcipher *tfm) | ||
| 228 | { | ||
| 229 | return crypto_tfm_in_queue(crypto_ablkcipher_alg(tfm)->queue, | ||
| 230 | crypto_ablkcipher_tfm(tfm)); | ||
| 231 | } | ||
| 232 | |||
| 175 | #endif /* _CRYPTO_ALGAPI_H */ | 233 | #endif /* _CRYPTO_ALGAPI_H */ |
| 176 | 234 | ||
diff --git a/include/linux/crypto.h b/include/linux/crypto.h index 67830e7c2c31..0ec2467891db 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h | |||
| @@ -93,6 +93,7 @@ struct crypto_ablkcipher; | |||
| 93 | struct crypto_async_request; | 93 | struct crypto_async_request; |
| 94 | struct crypto_blkcipher; | 94 | struct crypto_blkcipher; |
| 95 | struct crypto_hash; | 95 | struct crypto_hash; |
| 96 | struct crypto_queue; | ||
| 96 | struct crypto_tfm; | 97 | struct crypto_tfm; |
| 97 | struct crypto_type; | 98 | struct crypto_type; |
| 98 | 99 | ||
| @@ -143,6 +144,19 @@ struct hash_desc { | |||
| 143 | * Algorithms: modular crypto algorithm implementations, managed | 144 | * Algorithms: modular crypto algorithm implementations, managed |
| 144 | * via crypto_register_alg() and crypto_unregister_alg(). | 145 | * via crypto_register_alg() and crypto_unregister_alg(). |
| 145 | */ | 146 | */ |
| 147 | struct ablkcipher_alg { | ||
| 148 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, | ||
| 149 | unsigned int keylen); | ||
| 150 | int (*encrypt)(struct ablkcipher_request *req); | ||
| 151 | int (*decrypt)(struct ablkcipher_request *req); | ||
| 152 | |||
| 153 | struct crypto_queue *queue; | ||
| 154 | |||
| 155 | unsigned int min_keysize; | ||
| 156 | unsigned int max_keysize; | ||
| 157 | unsigned int ivsize; | ||
| 158 | }; | ||
| 159 | |||
| 146 | struct blkcipher_alg { | 160 | struct blkcipher_alg { |
| 147 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, | 161 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, |
| 148 | unsigned int keylen); | 162 | unsigned int keylen); |
| @@ -197,6 +211,7 @@ struct compress_alg { | |||
| 197 | unsigned int slen, u8 *dst, unsigned int *dlen); | 211 | unsigned int slen, u8 *dst, unsigned int *dlen); |
| 198 | }; | 212 | }; |
| 199 | 213 | ||
| 214 | #define cra_ablkcipher cra_u.ablkcipher | ||
| 200 | #define cra_blkcipher cra_u.blkcipher | 215 | #define cra_blkcipher cra_u.blkcipher |
| 201 | #define cra_cipher cra_u.cipher | 216 | #define cra_cipher cra_u.cipher |
| 202 | #define cra_digest cra_u.digest | 217 | #define cra_digest cra_u.digest |
| @@ -221,6 +236,7 @@ struct crypto_alg { | |||
| 221 | const struct crypto_type *cra_type; | 236 | const struct crypto_type *cra_type; |
| 222 | 237 | ||
| 223 | union { | 238 | union { |
| 239 | struct ablkcipher_alg ablkcipher; | ||
| 224 | struct blkcipher_alg blkcipher; | 240 | struct blkcipher_alg blkcipher; |
| 225 | struct cipher_alg cipher; | 241 | struct cipher_alg cipher; |
| 226 | struct digest_alg digest; | 242 | struct digest_alg digest; |
| @@ -572,6 +588,12 @@ static inline int crypto_ablkcipher_reqsize(struct crypto_ablkcipher *tfm) | |||
| 572 | return crypto_ablkcipher_crt(tfm)->reqsize; | 588 | return crypto_ablkcipher_crt(tfm)->reqsize; |
| 573 | } | 589 | } |
| 574 | 590 | ||
| 591 | static inline struct ablkcipher_request *ablkcipher_request_cast( | ||
| 592 | struct crypto_async_request *req) | ||
| 593 | { | ||
| 594 | return container_of(req, struct ablkcipher_request, base); | ||
| 595 | } | ||
| 596 | |||
| 575 | static inline struct ablkcipher_request *ablkcipher_request_alloc( | 597 | static inline struct ablkcipher_request *ablkcipher_request_alloc( |
| 576 | struct crypto_ablkcipher *tfm, gfp_t gfp) | 598 | struct crypto_ablkcipher *tfm, gfp_t gfp) |
| 577 | { | 599 | { |
