Commit e853c3cfa8cc24869ecd2526e589bcb176bc12e9

Authored by Herbert Xu
1 parent 8f21cf0d2b

[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>

Showing 4 changed files with 38 additions and 10 deletions Side-by-side Diff

... ... @@ -226,17 +226,18 @@
226 226  
227 227 case CRYPTO_ALG_TYPE_COMPRESS:
228 228 return crypto_init_compress_flags(tfm, flags);
229   -
230   - default:
231   - break;
232 229 }
233 230  
234   - BUG();
235   - return -EINVAL;
  231 + return 0;
236 232 }
237 233  
238 234 static int crypto_init_ops(struct crypto_tfm *tfm)
239 235 {
  236 + const struct crypto_type *type = tfm->__crt_alg->cra_type;
  237 +
  238 + if (type)
  239 + return type->init(tfm);
  240 +
240 241 switch (crypto_tfm_alg_type(tfm)) {
241 242 case CRYPTO_ALG_TYPE_CIPHER:
242 243 return crypto_init_cipher_ops(tfm);
... ... @@ -257,6 +258,14 @@
257 258  
258 259 static void crypto_exit_ops(struct crypto_tfm *tfm)
259 260 {
  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 +
260 269 switch (crypto_tfm_alg_type(tfm)) {
261 270 case CRYPTO_ALG_TYPE_CIPHER:
262 271 crypto_exit_cipher_ops(tfm);
263 272  
264 273  
265 274  
266 275  
267 276  
... ... @@ -278,26 +287,31 @@
278 287  
279 288 static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
280 289 {
  290 + const struct crypto_type *type = alg->cra_type;
281 291 unsigned int len;
282 292  
  293 + len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  294 + if (type)
  295 + return len + type->ctxsize(alg);
  296 +
283 297 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
284 298 default:
285 299 BUG();
286 300  
287 301 case CRYPTO_ALG_TYPE_CIPHER:
288   - len = crypto_cipher_ctxsize(alg, flags);
  302 + len += crypto_cipher_ctxsize(alg, flags);
289 303 break;
290 304  
291 305 case CRYPTO_ALG_TYPE_DIGEST:
292   - len = crypto_digest_ctxsize(alg, flags);
  306 + len += crypto_digest_ctxsize(alg, flags);
293 307 break;
294 308  
295 309 case CRYPTO_ALG_TYPE_COMPRESS:
296   - len = crypto_compress_ctxsize(alg, flags);
  310 + len += crypto_compress_ctxsize(alg, flags);
297 311 break;
298 312 }
299 313  
300   - return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  314 + return len;
301 315 }
302 316  
303 317 void crypto_shoot_alg(struct crypto_alg *alg)
... ... @@ -78,7 +78,10 @@
78 78 seq_printf(m, "type : compression\n");
79 79 break;
80 80 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");
82 85 break;
83 86 }
84 87  
include/crypto/algapi.h
... ... @@ -15,6 +15,14 @@
15 15 #include <linux/crypto.h>
16 16  
17 17 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 +};
18 26  
19 27 struct crypto_instance {
20 28 struct crypto_alg alg;
include/linux/crypto.h
... ... @@ -90,6 +90,7 @@
90 90  
91 91 struct scatterlist;
92 92 struct crypto_tfm;
  93 +struct crypto_type;
93 94  
94 95 struct cipher_desc {
95 96 struct crypto_tfm *tfm;
... ... @@ -160,6 +161,8 @@
160 161  
161 162 char cra_name[CRYPTO_MAX_ALG_NAME];
162 163 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
  164 +
  165 + const struct crypto_type *cra_type;
163 166  
164 167 union {
165 168 struct cipher_alg cipher;