Commit e853c3cf authored by Herbert Xu's avatar Herbert Xu

[CRYPTO] api: Added crypto_type support

This patch adds the crypto_type structure which will be used for all new
crypto algorithm types, beginning with block ciphers.

The primary purpose of this abstraction is to allow different crypto_type
objects for crypto algorithms of the same type, in particular, there will
be a different crypto_type objects for asynchronous algorithms.
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 8f21cf0d
...@@ -226,17 +226,18 @@ static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags) ...@@ -226,17 +226,18 @@ static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
case CRYPTO_ALG_TYPE_COMPRESS: case CRYPTO_ALG_TYPE_COMPRESS:
return crypto_init_compress_flags(tfm, flags); return crypto_init_compress_flags(tfm, flags);
default:
break;
} }
BUG(); return 0;
return -EINVAL;
} }
static int crypto_init_ops(struct crypto_tfm *tfm) static int crypto_init_ops(struct crypto_tfm *tfm)
{ {
const struct crypto_type *type = tfm->__crt_alg->cra_type;
if (type)
return type->init(tfm);
switch (crypto_tfm_alg_type(tfm)) { switch (crypto_tfm_alg_type(tfm)) {
case CRYPTO_ALG_TYPE_CIPHER: case CRYPTO_ALG_TYPE_CIPHER:
return crypto_init_cipher_ops(tfm); return crypto_init_cipher_ops(tfm);
...@@ -257,6 +258,14 @@ static int crypto_init_ops(struct crypto_tfm *tfm) ...@@ -257,6 +258,14 @@ static int crypto_init_ops(struct crypto_tfm *tfm)
static void crypto_exit_ops(struct crypto_tfm *tfm) static void crypto_exit_ops(struct crypto_tfm *tfm)
{ {
const struct crypto_type *type = tfm->__crt_alg->cra_type;
if (type) {
if (type->exit)
type->exit(tfm);
return;
}
switch (crypto_tfm_alg_type(tfm)) { switch (crypto_tfm_alg_type(tfm)) {
case CRYPTO_ALG_TYPE_CIPHER: case CRYPTO_ALG_TYPE_CIPHER:
crypto_exit_cipher_ops(tfm); crypto_exit_cipher_ops(tfm);
...@@ -278,26 +287,31 @@ static void crypto_exit_ops(struct crypto_tfm *tfm) ...@@ -278,26 +287,31 @@ static void crypto_exit_ops(struct crypto_tfm *tfm)
static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags) static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
{ {
const struct crypto_type *type = alg->cra_type;
unsigned int len; unsigned int len;
len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
if (type)
return len + type->ctxsize(alg);
switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) { switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
default: default:
BUG(); BUG();
case CRYPTO_ALG_TYPE_CIPHER: case CRYPTO_ALG_TYPE_CIPHER:
len = crypto_cipher_ctxsize(alg, flags); len += crypto_cipher_ctxsize(alg, flags);
break; break;
case CRYPTO_ALG_TYPE_DIGEST: case CRYPTO_ALG_TYPE_DIGEST:
len = crypto_digest_ctxsize(alg, flags); len += crypto_digest_ctxsize(alg, flags);
break; break;
case CRYPTO_ALG_TYPE_COMPRESS: case CRYPTO_ALG_TYPE_COMPRESS:
len = crypto_compress_ctxsize(alg, flags); len += crypto_compress_ctxsize(alg, flags);
break; break;
} }
return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1)); return len;
} }
void crypto_shoot_alg(struct crypto_alg *alg) void crypto_shoot_alg(struct crypto_alg *alg)
......
...@@ -78,7 +78,10 @@ static int c_show(struct seq_file *m, void *p) ...@@ -78,7 +78,10 @@ static int c_show(struct seq_file *m, void *p)
seq_printf(m, "type : compression\n"); seq_printf(m, "type : compression\n");
break; break;
default: default:
seq_printf(m, "type : unknown\n"); if (alg->cra_type && alg->cra_type->show)
alg->cra_type->show(m, alg);
else
seq_printf(m, "type : unknown\n");
break; break;
} }
......
...@@ -15,6 +15,14 @@ ...@@ -15,6 +15,14 @@
#include <linux/crypto.h> #include <linux/crypto.h>
struct module; struct module;
struct seq_file;
struct crypto_type {
unsigned int (*ctxsize)(struct crypto_alg *alg);
int (*init)(struct crypto_tfm *tfm);
void (*exit)(struct crypto_tfm *tfm);
void (*show)(struct seq_file *m, struct crypto_alg *alg);
};
struct crypto_instance { struct crypto_instance {
struct crypto_alg alg; struct crypto_alg alg;
......
...@@ -90,6 +90,7 @@ ...@@ -90,6 +90,7 @@
struct scatterlist; struct scatterlist;
struct crypto_tfm; struct crypto_tfm;
struct crypto_type;
struct cipher_desc { struct cipher_desc {
struct crypto_tfm *tfm; struct crypto_tfm *tfm;
...@@ -161,6 +162,8 @@ struct crypto_alg { ...@@ -161,6 +162,8 @@ struct crypto_alg {
char cra_name[CRYPTO_MAX_ALG_NAME]; char cra_name[CRYPTO_MAX_ALG_NAME];
char cra_driver_name[CRYPTO_MAX_ALG_NAME]; char cra_driver_name[CRYPTO_MAX_ALG_NAME];
const struct crypto_type *cra_type;
union { union {
struct cipher_alg cipher; struct cipher_alg cipher;
struct digest_alg digest; struct digest_alg digest;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment