Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: hash - move "ahash wrapping shash" functions to ahash.c

The functions that are involved in implementing the ahash API on top of
an shash algorithm belong better in ahash.c, not in shash.c where they
currently are. Move them.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
ecf889b7 9826d1d6

+188 -191
+186
crypto/ahash.c
··· 29 29 30 30 static const struct crypto_type crypto_ahash_type; 31 31 32 + static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key, 33 + unsigned int keylen) 34 + { 35 + struct crypto_shash **ctx = crypto_ahash_ctx(tfm); 36 + 37 + return crypto_shash_setkey(*ctx, key, keylen); 38 + } 39 + 40 + static int shash_async_init(struct ahash_request *req) 41 + { 42 + struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 43 + struct shash_desc *desc = ahash_request_ctx(req); 44 + 45 + desc->tfm = *ctx; 46 + 47 + return crypto_shash_init(desc); 48 + } 49 + 50 + int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc) 51 + { 52 + struct crypto_hash_walk walk; 53 + int nbytes; 54 + 55 + for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0; 56 + nbytes = crypto_hash_walk_done(&walk, nbytes)) 57 + nbytes = crypto_shash_update(desc, walk.data, nbytes); 58 + 59 + return nbytes; 60 + } 61 + EXPORT_SYMBOL_GPL(shash_ahash_update); 62 + 63 + static int shash_async_update(struct ahash_request *req) 64 + { 65 + return shash_ahash_update(req, ahash_request_ctx(req)); 66 + } 67 + 68 + static int shash_async_final(struct ahash_request *req) 69 + { 70 + return crypto_shash_final(ahash_request_ctx(req), req->result); 71 + } 72 + 73 + int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc) 74 + { 75 + struct crypto_hash_walk walk; 76 + int nbytes; 77 + 78 + nbytes = crypto_hash_walk_first(req, &walk); 79 + if (!nbytes) 80 + return crypto_shash_final(desc, req->result); 81 + 82 + do { 83 + nbytes = crypto_hash_walk_last(&walk) ? 84 + crypto_shash_finup(desc, walk.data, nbytes, 85 + req->result) : 86 + crypto_shash_update(desc, walk.data, nbytes); 87 + nbytes = crypto_hash_walk_done(&walk, nbytes); 88 + } while (nbytes > 0); 89 + 90 + return nbytes; 91 + } 92 + EXPORT_SYMBOL_GPL(shash_ahash_finup); 93 + 94 + static int shash_async_finup(struct ahash_request *req) 95 + { 96 + struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 97 + struct shash_desc *desc = ahash_request_ctx(req); 98 + 99 + desc->tfm = *ctx; 100 + 101 + return shash_ahash_finup(req, desc); 102 + } 103 + 104 + int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc) 105 + { 106 + unsigned int nbytes = req->nbytes; 107 + struct scatterlist *sg; 108 + unsigned int offset; 109 + int err; 110 + 111 + if (nbytes && 112 + (sg = req->src, offset = sg->offset, 113 + nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) { 114 + void *data; 115 + 116 + data = kmap_local_page(sg_page(sg)); 117 + err = crypto_shash_digest(desc, data + offset, nbytes, 118 + req->result); 119 + kunmap_local(data); 120 + } else 121 + err = crypto_shash_init(desc) ?: 122 + shash_ahash_finup(req, desc); 123 + 124 + return err; 125 + } 126 + EXPORT_SYMBOL_GPL(shash_ahash_digest); 127 + 128 + static int shash_async_digest(struct ahash_request *req) 129 + { 130 + struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 131 + struct shash_desc *desc = ahash_request_ctx(req); 132 + 133 + desc->tfm = *ctx; 134 + 135 + return shash_ahash_digest(req, desc); 136 + } 137 + 138 + static int shash_async_export(struct ahash_request *req, void *out) 139 + { 140 + return crypto_shash_export(ahash_request_ctx(req), out); 141 + } 142 + 143 + static int shash_async_import(struct ahash_request *req, const void *in) 144 + { 145 + struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 146 + struct shash_desc *desc = ahash_request_ctx(req); 147 + 148 + desc->tfm = *ctx; 149 + 150 + return crypto_shash_import(desc, in); 151 + } 152 + 153 + static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) 154 + { 155 + struct crypto_shash **ctx = crypto_tfm_ctx(tfm); 156 + 157 + crypto_free_shash(*ctx); 158 + } 159 + 160 + static int crypto_init_shash_ops_async(struct crypto_tfm *tfm) 161 + { 162 + struct crypto_alg *calg = tfm->__crt_alg; 163 + struct shash_alg *alg = __crypto_shash_alg(calg); 164 + struct crypto_ahash *crt = __crypto_ahash_cast(tfm); 165 + struct crypto_shash **ctx = crypto_tfm_ctx(tfm); 166 + struct crypto_shash *shash; 167 + 168 + if (!crypto_mod_get(calg)) 169 + return -EAGAIN; 170 + 171 + shash = crypto_create_tfm(calg, &crypto_shash_type); 172 + if (IS_ERR(shash)) { 173 + crypto_mod_put(calg); 174 + return PTR_ERR(shash); 175 + } 176 + 177 + *ctx = shash; 178 + tfm->exit = crypto_exit_shash_ops_async; 179 + 180 + crt->init = shash_async_init; 181 + crt->update = shash_async_update; 182 + crt->final = shash_async_final; 183 + crt->finup = shash_async_finup; 184 + crt->digest = shash_async_digest; 185 + if (crypto_shash_alg_has_setkey(alg)) 186 + crt->setkey = shash_async_setkey; 187 + 188 + crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) & 189 + CRYPTO_TFM_NEED_KEY); 190 + 191 + crt->export = shash_async_export; 192 + crt->import = shash_async_import; 193 + 194 + crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); 195 + 196 + return 0; 197 + } 198 + 199 + static struct crypto_ahash * 200 + crypto_clone_shash_ops_async(struct crypto_ahash *nhash, 201 + struct crypto_ahash *hash) 202 + { 203 + struct crypto_shash **nctx = crypto_ahash_ctx(nhash); 204 + struct crypto_shash **ctx = crypto_ahash_ctx(hash); 205 + struct crypto_shash *shash; 206 + 207 + shash = crypto_clone_shash(*ctx); 208 + if (IS_ERR(shash)) { 209 + crypto_free_ahash(nhash); 210 + return ERR_CAST(shash); 211 + } 212 + 213 + *nctx = shash; 214 + 215 + return nhash; 216 + } 217 + 32 218 static int hash_walk_next(struct crypto_hash_walk *walk) 33 219 { 34 220 unsigned int offset = walk->offset;
+1 -3
crypto/hash.h
··· 31 31 return nla_put(skb, CRYPTOCFGA_STAT_HASH, sizeof(rhash), &rhash); 32 32 } 33 33 34 - int crypto_init_shash_ops_async(struct crypto_tfm *tfm); 35 - struct crypto_ahash *crypto_clone_shash_ops_async(struct crypto_ahash *nhash, 36 - struct crypto_ahash *hash); 34 + extern const struct crypto_type crypto_shash_type; 37 35 38 36 int hash_prepare_alg(struct hash_alg_common *alg); 39 37
+1 -188
crypto/shash.c
··· 16 16 17 17 #include "hash.h" 18 18 19 - static const struct crypto_type crypto_shash_type; 20 - 21 19 static inline struct crypto_istat_hash *shash_get_stat(struct shash_alg *alg) 22 20 { 23 21 return hash_get_stat(&alg->halg); ··· 191 193 } 192 194 EXPORT_SYMBOL_GPL(crypto_shash_import); 193 195 194 - static int shash_async_setkey(struct crypto_ahash *tfm, const u8 *key, 195 - unsigned int keylen) 196 - { 197 - struct crypto_shash **ctx = crypto_ahash_ctx(tfm); 198 - 199 - return crypto_shash_setkey(*ctx, key, keylen); 200 - } 201 - 202 - static int shash_async_init(struct ahash_request *req) 203 - { 204 - struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 205 - struct shash_desc *desc = ahash_request_ctx(req); 206 - 207 - desc->tfm = *ctx; 208 - 209 - return crypto_shash_init(desc); 210 - } 211 - 212 - int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc) 213 - { 214 - struct crypto_hash_walk walk; 215 - int nbytes; 216 - 217 - for (nbytes = crypto_hash_walk_first(req, &walk); nbytes > 0; 218 - nbytes = crypto_hash_walk_done(&walk, nbytes)) 219 - nbytes = crypto_shash_update(desc, walk.data, nbytes); 220 - 221 - return nbytes; 222 - } 223 - EXPORT_SYMBOL_GPL(shash_ahash_update); 224 - 225 - static int shash_async_update(struct ahash_request *req) 226 - { 227 - return shash_ahash_update(req, ahash_request_ctx(req)); 228 - } 229 - 230 - static int shash_async_final(struct ahash_request *req) 231 - { 232 - return crypto_shash_final(ahash_request_ctx(req), req->result); 233 - } 234 - 235 - int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc) 236 - { 237 - struct crypto_hash_walk walk; 238 - int nbytes; 239 - 240 - nbytes = crypto_hash_walk_first(req, &walk); 241 - if (!nbytes) 242 - return crypto_shash_final(desc, req->result); 243 - 244 - do { 245 - nbytes = crypto_hash_walk_last(&walk) ? 246 - crypto_shash_finup(desc, walk.data, nbytes, 247 - req->result) : 248 - crypto_shash_update(desc, walk.data, nbytes); 249 - nbytes = crypto_hash_walk_done(&walk, nbytes); 250 - } while (nbytes > 0); 251 - 252 - return nbytes; 253 - } 254 - EXPORT_SYMBOL_GPL(shash_ahash_finup); 255 - 256 - static int shash_async_finup(struct ahash_request *req) 257 - { 258 - struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 259 - struct shash_desc *desc = ahash_request_ctx(req); 260 - 261 - desc->tfm = *ctx; 262 - 263 - return shash_ahash_finup(req, desc); 264 - } 265 - 266 - int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc) 267 - { 268 - unsigned int nbytes = req->nbytes; 269 - struct scatterlist *sg; 270 - unsigned int offset; 271 - int err; 272 - 273 - if (nbytes && 274 - (sg = req->src, offset = sg->offset, 275 - nbytes <= min(sg->length, ((unsigned int)(PAGE_SIZE)) - offset))) { 276 - void *data; 277 - 278 - data = kmap_local_page(sg_page(sg)); 279 - err = crypto_shash_digest(desc, data + offset, nbytes, 280 - req->result); 281 - kunmap_local(data); 282 - } else 283 - err = crypto_shash_init(desc) ?: 284 - shash_ahash_finup(req, desc); 285 - 286 - return err; 287 - } 288 - EXPORT_SYMBOL_GPL(shash_ahash_digest); 289 - 290 - static int shash_async_digest(struct ahash_request *req) 291 - { 292 - struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 293 - struct shash_desc *desc = ahash_request_ctx(req); 294 - 295 - desc->tfm = *ctx; 296 - 297 - return shash_ahash_digest(req, desc); 298 - } 299 - 300 - static int shash_async_export(struct ahash_request *req, void *out) 301 - { 302 - return crypto_shash_export(ahash_request_ctx(req), out); 303 - } 304 - 305 - static int shash_async_import(struct ahash_request *req, const void *in) 306 - { 307 - struct crypto_shash **ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 308 - struct shash_desc *desc = ahash_request_ctx(req); 309 - 310 - desc->tfm = *ctx; 311 - 312 - return crypto_shash_import(desc, in); 313 - } 314 - 315 - static void crypto_exit_shash_ops_async(struct crypto_tfm *tfm) 316 - { 317 - struct crypto_shash **ctx = crypto_tfm_ctx(tfm); 318 - 319 - crypto_free_shash(*ctx); 320 - } 321 - 322 - int crypto_init_shash_ops_async(struct crypto_tfm *tfm) 323 - { 324 - struct crypto_alg *calg = tfm->__crt_alg; 325 - struct shash_alg *alg = __crypto_shash_alg(calg); 326 - struct crypto_ahash *crt = __crypto_ahash_cast(tfm); 327 - struct crypto_shash **ctx = crypto_tfm_ctx(tfm); 328 - struct crypto_shash *shash; 329 - 330 - if (!crypto_mod_get(calg)) 331 - return -EAGAIN; 332 - 333 - shash = crypto_create_tfm(calg, &crypto_shash_type); 334 - if (IS_ERR(shash)) { 335 - crypto_mod_put(calg); 336 - return PTR_ERR(shash); 337 - } 338 - 339 - *ctx = shash; 340 - tfm->exit = crypto_exit_shash_ops_async; 341 - 342 - crt->init = shash_async_init; 343 - crt->update = shash_async_update; 344 - crt->final = shash_async_final; 345 - crt->finup = shash_async_finup; 346 - crt->digest = shash_async_digest; 347 - if (crypto_shash_alg_has_setkey(alg)) 348 - crt->setkey = shash_async_setkey; 349 - 350 - crypto_ahash_set_flags(crt, crypto_shash_get_flags(shash) & 351 - CRYPTO_TFM_NEED_KEY); 352 - 353 - crt->export = shash_async_export; 354 - crt->import = shash_async_import; 355 - 356 - crt->reqsize = sizeof(struct shash_desc) + crypto_shash_descsize(shash); 357 - 358 - return 0; 359 - } 360 - 361 - struct crypto_ahash *crypto_clone_shash_ops_async(struct crypto_ahash *nhash, 362 - struct crypto_ahash *hash) 363 - { 364 - struct crypto_shash **nctx = crypto_ahash_ctx(nhash); 365 - struct crypto_shash **ctx = crypto_ahash_ctx(hash); 366 - struct crypto_shash *shash; 367 - 368 - shash = crypto_clone_shash(*ctx); 369 - if (IS_ERR(shash)) { 370 - crypto_free_ahash(nhash); 371 - return ERR_CAST(shash); 372 - } 373 - 374 - *nctx = shash; 375 - 376 - return nhash; 377 - } 378 - 379 196 static void crypto_shash_exit_tfm(struct crypto_tfm *tfm) 380 197 { 381 198 struct crypto_shash *hash = __crypto_shash_cast(tfm); ··· 269 456 return crypto_hash_report_stat(skb, alg, "shash"); 270 457 } 271 458 272 - static const struct crypto_type crypto_shash_type = { 459 + const struct crypto_type crypto_shash_type = { 273 460 .extsize = crypto_alg_extsize, 274 461 .init_tfm = crypto_shash_init_tfm, 275 462 .free = crypto_shash_free_instance,