Skip to content

Commit e853c3c

Browse files
committed
[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: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 8f21cf0 commit e853c3c

4 files changed

Lines changed: 38 additions & 10 deletions

File tree

‎crypto/api.c‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,18 @@ static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
226226

227227
case CRYPTO_ALG_TYPE_COMPRESS:
228228
return crypto_init_compress_flags(tfm, flags);
229-
230-
default:
231-
break;
232229
}
233230

234-
BUG();
235-
return -EINVAL;
231+
return 0;
236232
}
237233

238234
static int crypto_init_ops(struct crypto_tfm *tfm)
239235
{
236+
const struct crypto_type *type = tfm->__crt_alg->cra_type;
237+
238+
if (type)
239+
return type->init(tfm);
240+
240241
switch (crypto_tfm_alg_type(tfm)) {
241242
case CRYPTO_ALG_TYPE_CIPHER:
242243
return crypto_init_cipher_ops(tfm);
@@ -257,6 +258,14 @@ static int crypto_init_ops(struct crypto_tfm *tfm)
257258

258259
static void crypto_exit_ops(struct crypto_tfm *tfm)
259260
{
261+
const struct crypto_type *type = tfm->__crt_alg->cra_type;
262+
263+
if (type) {
264+
if (type->exit)
265+
type->exit(tfm);
266+
return;
267+
}
268+
260269
switch (crypto_tfm_alg_type(tfm)) {
261270
case CRYPTO_ALG_TYPE_CIPHER:
262271
crypto_exit_cipher_ops(tfm);
@@ -278,26 +287,31 @@ static void crypto_exit_ops(struct crypto_tfm *tfm)
278287

279288
static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
280289
{
290+
const struct crypto_type *type = alg->cra_type;
281291
unsigned int len;
282292

293+
len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
294+
if (type)
295+
return len + type->ctxsize(alg);
296+
283297
switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
284298
default:
285299
BUG();
286300

287301
case CRYPTO_ALG_TYPE_CIPHER:
288-
len = crypto_cipher_ctxsize(alg, flags);
302+
len += crypto_cipher_ctxsize(alg, flags);
289303
break;
290304

291305
case CRYPTO_ALG_TYPE_DIGEST:
292-
len = crypto_digest_ctxsize(alg, flags);
306+
len += crypto_digest_ctxsize(alg, flags);
293307
break;
294308

295309
case CRYPTO_ALG_TYPE_COMPRESS:
296-
len = crypto_compress_ctxsize(alg, flags);
310+
len += crypto_compress_ctxsize(alg, flags);
297311
break;
298312
}
299313

300-
return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
314+
return len;
301315
}
302316

303317
void crypto_shoot_alg(struct crypto_alg *alg)

‎crypto/proc.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ static int c_show(struct seq_file *m, void *p)
7878
seq_printf(m, "type : compression\n");
7979
break;
8080
default:
81-
seq_printf(m, "type : unknown\n");
81+
if (alg->cra_type && alg->cra_type->show)
82+
alg->cra_type->show(m, alg);
83+
else
84+
seq_printf(m, "type : unknown\n");
8285
break;
8386
}
8487

‎include/crypto/algapi.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
#include <linux/crypto.h>
1616

1717
struct module;
18+
struct seq_file;
19+
20+
struct crypto_type {
21+
unsigned int (*ctxsize)(struct crypto_alg *alg);
22+
int (*init)(struct crypto_tfm *tfm);
23+
void (*exit)(struct crypto_tfm *tfm);
24+
void (*show)(struct seq_file *m, struct crypto_alg *alg);
25+
};
1826

1927
struct crypto_instance {
2028
struct crypto_alg alg;

‎include/linux/crypto.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090

9191
struct scatterlist;
9292
struct crypto_tfm;
93+
struct crypto_type;
9394

9495
struct cipher_desc {
9596
struct crypto_tfm *tfm;
@@ -161,6 +162,8 @@ struct crypto_alg {
161162
char cra_name[CRYPTO_MAX_ALG_NAME];
162163
char cra_driver_name[CRYPTO_MAX_ALG_NAME];
163164

165+
const struct crypto_type *cra_type;
166+
164167
union {
165168
struct cipher_alg cipher;
166169
struct digest_alg digest;

0 commit comments

Comments
 (0)