Commit 88056ec346ccf41f63dbc7080b24b5fd19d1358d

Authored by Herbert Xu
1 parent 2ca33da1de

crypto: ahash - Convert to new style algorithms

This patch converts crypto_ahash to the new style.  The old ahash
algorithm type is retained until the existing ahash implementations
are also converted.  All ahash users will automatically get the
new crypto_ahash type.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 5 changed files with 148 additions and 91 deletions Side-by-side Diff

... ... @@ -24,6 +24,12 @@
24 24  
25 25 #include "internal.h"
26 26  
  27 +static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
  28 +{
  29 + return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
  30 + halg);
  31 +}
  32 +
27 33 static int hash_walk_next(struct crypto_hash_walk *walk)
28 34 {
29 35 unsigned int alignmask = walk->alignmask;
30 36  
... ... @@ -169,30 +175,11 @@
169 175 return -ENOSYS;
170 176 }
171 177  
172   -int crypto_ahash_import(struct ahash_request *req, const u8 *in)
173   -{
174   - struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
175   - struct ahash_alg *alg = crypto_ahash_alg(tfm);
176   -
177   - memcpy(ahash_request_ctx(req), in, crypto_ahash_reqsize(tfm));
178   -
179   - if (alg->reinit)
180   - alg->reinit(req);
181   -
182   - return 0;
183   -}
184   -EXPORT_SYMBOL_GPL(crypto_ahash_import);
185   -
186   -static unsigned int crypto_ahash_ctxsize(struct crypto_alg *alg, u32 type,
187   - u32 mask)
188   -{
189   - return alg->cra_ctxsize;
190   -}
191   -
192 178 static int crypto_init_ahash_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
193 179 {
194   - struct ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
195   - struct ahash_tfm *crt = &tfm->crt_ahash;
  180 + struct old_ahash_alg *alg = &tfm->__crt_alg->cra_ahash;
  181 + struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
  182 + struct ahash_alg *nalg = crypto_ahash_alg(crt);
196 183  
197 184 if (alg->digestsize > PAGE_SIZE / 8)
198 185 return -EINVAL;
199 186  
... ... @@ -204,9 +191,42 @@
204 191 crt->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;
205 192 crt->digestsize = alg->digestsize;
206 193  
  194 + nalg->setkey = alg->setkey;
  195 + nalg->halg.digestsize = alg->digestsize;
  196 +
207 197 return 0;
208 198 }
209 199  
  200 +static int crypto_ahash_init_tfm(struct crypto_tfm *tfm)
  201 +{
  202 + struct crypto_ahash *hash = __crypto_ahash_cast(tfm);
  203 + struct ahash_alg *alg = crypto_ahash_alg(hash);
  204 + struct old_ahash_alg *oalg = crypto_old_ahash_alg(hash);
  205 +
  206 + if (tfm->__crt_alg->cra_type != &crypto_ahash_type)
  207 + return crypto_init_shash_ops_async(tfm);
  208 +
  209 + if (oalg->init)
  210 + return crypto_init_ahash_ops(tfm, 0, 0);
  211 +
  212 + hash->init = alg->init;
  213 + hash->update = alg->update;
  214 + hash->final = alg->final;
  215 + hash->digest = alg->digest;
  216 + hash->setkey = alg->setkey ? ahash_setkey : ahash_nosetkey;
  217 + hash->digestsize = alg->halg.digestsize;
  218 +
  219 + return 0;
  220 +}
  221 +
  222 +static unsigned int crypto_ahash_extsize(struct crypto_alg *alg)
  223 +{
  224 + if (alg->cra_type == &crypto_ahash_type)
  225 + return alg->cra_ctxsize;
  226 +
  227 + return sizeof(struct crypto_shash *);
  228 +}
  229 +
210 230 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
211 231 __attribute__ ((unused));
212 232 static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg)
213 233  
214 234  
215 235  
... ... @@ -215,17 +235,29 @@
215 235 seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
216 236 "yes" : "no");
217 237 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
218   - seq_printf(m, "digestsize : %u\n", alg->cra_ahash.digestsize);
  238 + seq_printf(m, "digestsize : %u\n",
  239 + __crypto_hash_alg_common(alg)->digestsize);
219 240 }
220 241  
221 242 const struct crypto_type crypto_ahash_type = {
222   - .ctxsize = crypto_ahash_ctxsize,
223   - .init = crypto_init_ahash_ops,
  243 + .extsize = crypto_ahash_extsize,
  244 + .init_tfm = crypto_ahash_init_tfm,
224 245 #ifdef CONFIG_PROC_FS
225 246 .show = crypto_ahash_show,
226 247 #endif
  248 + .maskclear = ~CRYPTO_ALG_TYPE_MASK,
  249 + .maskset = CRYPTO_ALG_TYPE_AHASH_MASK,
  250 + .type = CRYPTO_ALG_TYPE_AHASH,
  251 + .tfmsize = offsetof(struct crypto_ahash, base),
227 252 };
228 253 EXPORT_SYMBOL_GPL(crypto_ahash_type);
  254 +
  255 +struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  256 + u32 mask)
  257 +{
  258 + return crypto_alloc_tfm(alg_name, &crypto_ahash_type, type, mask);
  259 +}
  260 +EXPORT_SYMBOL_GPL(crypto_alloc_ahash);
229 261  
230 262 MODULE_LICENSE("GPL");
231 263 MODULE_DESCRIPTION("Asynchronous cryptographic hash type");
... ... @@ -267,11 +267,11 @@
267 267 crypto_free_shash(*ctx);
268 268 }
269 269  
270   -static int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
  270 +int crypto_init_shash_ops_async(struct crypto_tfm *tfm)
271 271 {
272 272 struct crypto_alg *calg = tfm->__crt_alg;
273 273 struct shash_alg *alg = __crypto_shash_alg(calg);
274   - struct ahash_tfm *crt = &tfm->crt_ahash;
  274 + struct crypto_ahash *crt = __crypto_ahash_cast(tfm);
275 275 struct crypto_shash **ctx = crypto_tfm_ctx(tfm);
276 276 struct crypto_shash *shash;
277 277  
... ... @@ -428,8 +428,6 @@
428 428 switch (mask & CRYPTO_ALG_TYPE_MASK) {
429 429 case CRYPTO_ALG_TYPE_HASH_MASK:
430 430 return crypto_init_shash_ops_compat(tfm);
431   - case CRYPTO_ALG_TYPE_AHASH_MASK:
432   - return crypto_init_shash_ops_async(tfm);
433 431 }
434 432  
435 433 return -EINVAL;
... ... @@ -441,8 +439,6 @@
441 439 switch (mask & CRYPTO_ALG_TYPE_MASK) {
442 440 case CRYPTO_ALG_TYPE_HASH_MASK:
443 441 return sizeof(struct shash_desc *);
444   - case CRYPTO_ALG_TYPE_AHASH_MASK:
445   - return sizeof(struct crypto_shash *);
446 442 }
447 443  
448 444 return 0;
include/crypto/hash.h
... ... @@ -15,6 +15,39 @@
15 15  
16 16 #include <linux/crypto.h>
17 17  
  18 +struct crypto_ahash;
  19 +
  20 +struct hash_alg_common {
  21 + unsigned int digestsize;
  22 + unsigned int statesize;
  23 +
  24 + struct crypto_alg base;
  25 +};
  26 +
  27 +struct ahash_request {
  28 + struct crypto_async_request base;
  29 +
  30 + unsigned int nbytes;
  31 + struct scatterlist *src;
  32 + u8 *result;
  33 +
  34 + void *__ctx[] CRYPTO_MINALIGN_ATTR;
  35 +};
  36 +
  37 +struct ahash_alg {
  38 + int (*init)(struct ahash_request *req);
  39 + int (*update)(struct ahash_request *req);
  40 + int (*final)(struct ahash_request *req);
  41 + int (*finup)(struct ahash_request *req);
  42 + int (*digest)(struct ahash_request *req);
  43 + int (*export)(struct ahash_request *req, void *out);
  44 + int (*import)(struct ahash_request *req, const void *in);
  45 + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
  46 + unsigned int keylen);
  47 +
  48 + struct hash_alg_common halg;
  49 +};
  50 +
18 51 struct shash_desc {
19 52 struct crypto_shash *tfm;
20 53 u32 flags;
... ... @@ -37,6 +70,8 @@
37 70 unsigned int keylen);
38 71  
39 72 unsigned int descsize;
  73 +
  74 + /* These fields must match hash_alg_common. */
40 75 unsigned int digestsize;
41 76 unsigned int statesize;
42 77  
... ... @@ -44,6 +79,18 @@
44 79 };
45 80  
46 81 struct crypto_ahash {
  82 + int (*init)(struct ahash_request *req);
  83 + int (*update)(struct ahash_request *req);
  84 + int (*final)(struct ahash_request *req);
  85 + int (*finup)(struct ahash_request *req);
  86 + int (*digest)(struct ahash_request *req);
  87 + int (*export)(struct ahash_request *req, void *out);
  88 + int (*import)(struct ahash_request *req, const void *in);
  89 + int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
  90 + unsigned int keylen);
  91 +
  92 + unsigned int digestsize;
  93 + unsigned int reqsize;
47 94 struct crypto_tfm base;
48 95 };
49 96  
50 97  
51 98  
... ... @@ -54,20 +101,12 @@
54 101  
55 102 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
56 103 {
57   - return (struct crypto_ahash *)tfm;
  104 + return container_of(tfm, struct crypto_ahash, base);
58 105 }
59 106  
60   -static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
61   - u32 type, u32 mask)
62   -{
63   - type &= ~CRYPTO_ALG_TYPE_MASK;
64   - mask &= ~CRYPTO_ALG_TYPE_MASK;
65   - type |= CRYPTO_ALG_TYPE_AHASH;
66   - mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
  107 +struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  108 + u32 mask);
67 109  
68   - return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
69   -}
70   -
71 110 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
72 111 {
73 112 return &tfm->base;
... ... @@ -75,7 +114,7 @@
75 114  
76 115 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
77 116 {
78   - crypto_free_tfm(crypto_ahash_tfm(tfm));
  117 + crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
79 118 }
80 119  
81 120 static inline unsigned int crypto_ahash_alignmask(
82 121  
83 122  
84 123  
85 124  
... ... @@ -84,16 +123,28 @@
84 123 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
85 124 }
86 125  
87   -static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
  126 +static inline struct hash_alg_common *__crypto_hash_alg_common(
  127 + struct crypto_alg *alg)
88 128 {
89   - return &crypto_ahash_tfm(tfm)->crt_ahash;
  129 + return container_of(alg, struct hash_alg_common, base);
90 130 }
91 131  
  132 +static inline struct hash_alg_common *crypto_hash_alg_common(
  133 + struct crypto_ahash *tfm)
  134 +{
  135 + return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
  136 +}
  137 +
92 138 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
93 139 {
94   - return crypto_ahash_crt(tfm)->digestsize;
  140 + return tfm->digestsize;
95 141 }
96 142  
  143 +static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
  144 +{
  145 + return crypto_hash_alg_common(tfm)->statesize;
  146 +}
  147 +
97 148 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
98 149 {
99 150 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
... ... @@ -117,7 +168,7 @@
117 168  
118 169 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
119 170 {
120   - return crypto_ahash_crt(tfm)->reqsize;
  171 + return tfm->reqsize;
121 172 }
122 173  
123 174 static inline void *ahash_request_ctx(struct ahash_request *req)
124 175  
125 176  
126 177  
127 178  
128 179  
129 180  
130 181  
... ... @@ -128,41 +179,37 @@
128 179 static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
129 180 const u8 *key, unsigned int keylen)
130 181 {
131   - struct ahash_tfm *crt = crypto_ahash_crt(tfm);
132   -
133   - return crt->setkey(tfm, key, keylen);
  182 + return tfm->setkey(tfm, key, keylen);
134 183 }
135 184  
136 185 static inline int crypto_ahash_digest(struct ahash_request *req)
137 186 {
138   - struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
139   - return crt->digest(req);
  187 + return crypto_ahash_reqtfm(req)->digest(req);
140 188 }
141 189  
142   -static inline void crypto_ahash_export(struct ahash_request *req, u8 *out)
  190 +static inline int crypto_ahash_export(struct ahash_request *req, void *out)
143 191 {
144   - memcpy(out, ahash_request_ctx(req),
145   - crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
  192 + return crypto_ahash_reqtfm(req)->export(req, out);
146 193 }
147 194  
148   -int crypto_ahash_import(struct ahash_request *req, const u8 *in);
  195 +static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
  196 +{
  197 + return crypto_ahash_reqtfm(req)->import(req, in);
  198 +}
149 199  
150 200 static inline int crypto_ahash_init(struct ahash_request *req)
151 201 {
152   - struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
153   - return crt->init(req);
  202 + return crypto_ahash_reqtfm(req)->init(req);
154 203 }
155 204  
156 205 static inline int crypto_ahash_update(struct ahash_request *req)
157 206 {
158   - struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
159   - return crt->update(req);
  207 + return crypto_ahash_reqtfm(req)->update(req);
160 208 }
161 209  
162 210 static inline int crypto_ahash_final(struct ahash_request *req)
163 211 {
164   - struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
165   - return crt->final(req);
  212 + return crypto_ahash_reqtfm(req)->final(req);
166 213 }
167 214  
168 215 static inline void ahash_request_set_tfm(struct ahash_request *req,
include/crypto/internal/hash.h
... ... @@ -51,6 +51,9 @@
51 51 struct crypto_hash_walk *walk,
52 52 struct scatterlist *sg, unsigned int len);
53 53  
  54 +int crypto_register_ahash(struct ahash_alg *alg);
  55 +int crypto_unregister_ahash(struct ahash_alg *alg);
  56 +
54 57 int crypto_register_shash(struct shash_alg *alg);
55 58 int crypto_unregister_shash(struct shash_alg *alg);
56 59 int shash_register_instance(struct crypto_template *tmpl,
57 60  
58 61  
... ... @@ -66,12 +69,14 @@
66 69 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
67 70 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
68 71  
  72 +int crypto_init_shash_ops_async(struct crypto_tfm *tfm);
  73 +
69 74 static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
70 75 {
71   - return crypto_tfm_ctx(&tfm->base);
  76 + return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
72 77 }
73 78  
74   -static inline struct ahash_alg *crypto_ahash_alg(
  79 +static inline struct old_ahash_alg *crypto_old_ahash_alg(
75 80 struct crypto_ahash *tfm)
76 81 {
77 82 return &crypto_ahash_tfm(tfm)->__crt_alg->cra_ahash;
... ... @@ -80,7 +85,7 @@
80 85 static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
81 86 unsigned int reqsize)
82 87 {
83   - crypto_ahash_crt(tfm)->reqsize = reqsize;
  88 + tfm->reqsize = reqsize;
84 89 }
85 90  
86 91 static inline int ahash_enqueue_request(struct crypto_queue *queue,
include/linux/crypto.h
... ... @@ -120,6 +120,7 @@
120 120 struct crypto_tfm;
121 121 struct crypto_type;
122 122 struct aead_givcrypt_request;
  123 +struct ahash_request;
123 124 struct skcipher_givcrypt_request;
124 125  
125 126 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
... ... @@ -146,16 +147,6 @@
146 147 void *__ctx[] CRYPTO_MINALIGN_ATTR;
147 148 };
148 149  
149   -struct ahash_request {
150   - struct crypto_async_request base;
151   -
152   - unsigned int nbytes;
153   - struct scatterlist *src;
154   - u8 *result;
155   -
156   - void *__ctx[] CRYPTO_MINALIGN_ATTR;
157   -};
158   -
159 150 /**
160 151 * struct aead_request - AEAD request
161 152 * @base: Common attributes for async crypto requests
... ... @@ -220,7 +211,7 @@
220 211 unsigned int ivsize;
221 212 };
222 213  
223   -struct ahash_alg {
  214 +struct old_ahash_alg {
224 215 int (*init)(struct ahash_request *req);
225 216 int (*reinit)(struct ahash_request *req);
226 217 int (*update)(struct ahash_request *req);
... ... @@ -346,7 +337,7 @@
346 337 struct cipher_alg cipher;
347 338 struct digest_alg digest;
348 339 struct hash_alg hash;
349   - struct ahash_alg ahash;
  340 + struct old_ahash_alg ahash;
350 341 struct compress_alg compress;
351 342 struct rng_alg rng;
352 343 } cra_u;
... ... @@ -433,18 +424,6 @@
433 424 unsigned int digestsize;
434 425 };
435 426  
436   -struct ahash_tfm {
437   - int (*init)(struct ahash_request *req);
438   - int (*update)(struct ahash_request *req);
439   - int (*final)(struct ahash_request *req);
440   - int (*digest)(struct ahash_request *req);
441   - int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
442   - unsigned int keylen);
443   -
444   - unsigned int digestsize;
445   - unsigned int reqsize;
446   -};
447   -
448 427 struct compress_tfm {
449 428 int (*cot_compress)(struct crypto_tfm *tfm,
450 429 const u8 *src, unsigned int slen,
... ... @@ -465,7 +444,6 @@
465 444 #define crt_blkcipher crt_u.blkcipher
466 445 #define crt_cipher crt_u.cipher
467 446 #define crt_hash crt_u.hash
468   -#define crt_ahash crt_u.ahash
469 447 #define crt_compress crt_u.compress
470 448 #define crt_rng crt_u.rng
471 449  
... ... @@ -479,7 +457,6 @@
479 457 struct blkcipher_tfm blkcipher;
480 458 struct cipher_tfm cipher;
481 459 struct hash_tfm hash;
482   - struct ahash_tfm ahash;
483 460 struct compress_tfm compress;
484 461 struct rng_tfm rng;
485 462 } crt_u;