| 1 |
|
| 2 | |
| 3 | * Hash algorithms.
|
| 4 | *
|
| 5 | * Copyright (c) 2008 Herbert Xu <[email protected]>
|
| 6 |
|
| 7 |
|
| 8 | #ifndef _CRYPTO_INTERNAL_HASH_H
|
| 9 | #define _CRYPTO_INTERNAL_HASH_H
|
| 10 |
|
| 11 | #include <crypto/algapi.h>
|
| 12 | #include <crypto/hash.h>
|
| 13 |
|
| 14 |
|
| 15 | #define CRYPTO_AHASH_ALG_BLOCK_ONLY 0x01000000
|
| 16 |
|
| 17 |
|
| 18 | #define CRYPTO_AHASH_ALG_FINAL_NONZERO 0x02000000
|
| 19 |
|
| 20 |
|
| 21 | #define CRYPTO_AHASH_ALG_FINUP_MAX 0x04000000
|
| 22 |
|
| 23 |
|
| 24 | #define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000
|
| 25 |
|
| 26 | #define HASH_FBREQ_ON_STACK(name, req) \
|
| 27 | char __##name##_req[sizeof(struct ahash_request) + \
|
| 28 | MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \
|
| 29 | struct ahash_request *name = ahash_fbreq_on_stack_init( \
|
| 30 | __##name##_req, (req))
|
| 31 |
|
| 32 | struct ahash_request;
|
| 33 | struct scatterlist;
|
| 34 |
|
| 35 | struct crypto_hash_walk {
|
| 36 | const char *data;
|
| 37 |
|
| 38 | unsigned int offset;
|
| 39 | unsigned int flags;
|
| 40 |
|
| 41 | struct page *pg;
|
| 42 | unsigned int entrylen;
|
| 43 |
|
| 44 | unsigned int total;
|
| 45 | struct scatterlist *sg;
|
| 46 | };
|
| 47 |
|
| 48 | struct ahash_instance {
|
| 49 | void (*free)(struct ahash_instance *inst);
|
| 50 | union {
|
| 51 | struct {
|
| 52 | char head[offsetof(struct ahash_alg, halg.base)];
|
| 53 | struct crypto_instance base;
|
| 54 | } s;
|
| 55 | struct ahash_alg alg;
|
| 56 | };
|
| 57 | };
|
| 58 |
|
| 59 | struct shash_instance {
|
| 60 | void (*free)(struct shash_instance *inst);
|
| 61 | union {
|
| 62 | struct {
|
| 63 | char head[offsetof(struct shash_alg, base)];
|
| 64 | struct crypto_instance base;
|
| 65 | } s;
|
| 66 | struct shash_alg alg;
|
| 67 | };
|
| 68 | };
|
| 69 |
|
| 70 | struct crypto_ahash_spawn {
|
| 71 | struct crypto_spawn base;
|
| 72 | };
|
| 73 |
|
| 74 | struct crypto_shash_spawn {
|
| 75 | struct crypto_spawn base;
|
| 76 | };
|
| 77 |
|
| 78 | int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
|
| 79 | int crypto_hash_walk_first(struct ahash_request *req,
|
| 80 | struct crypto_hash_walk *walk);
|
| 81 |
|
| 82 | static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
|
| 83 | {
|
| 84 | return !(walk->entrylen | walk->total);
|
| 85 | }
|
| 86 |
|
| 87 | int crypto_register_ahash(struct ahash_alg *alg);
|
| 88 | void crypto_unregister_ahash(struct ahash_alg *alg);
|
| 89 | int crypto_register_ahashes(struct ahash_alg *algs, int count);
|
| 90 | void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
|
| 91 | int ahash_register_instance(struct crypto_template *tmpl,
|
| 92 | struct ahash_instance *inst);
|
| 93 | void ahash_free_singlespawn_instance(struct ahash_instance *inst);
|
| 94 |
|
| 95 | int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
|
| 96 | unsigned int keylen);
|
| 97 |
|
| 98 | static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
|
| 99 | {
|
| 100 | return alg->setkey != shash_no_setkey;
|
| 101 | }
|
| 102 |
|
| 103 | bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
|
| 104 |
|
| 105 | static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
|
| 106 | {
|
| 107 | return crypto_shash_alg_has_setkey(alg) &&
|
| 108 | !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
|
| 109 | }
|
| 110 |
|
| 111 | static inline bool crypto_hash_alg_needs_key(struct hash_alg_common *alg)
|
| 112 | {
|
| 113 | return crypto_hash_alg_has_setkey(alg) &&
|
| 114 | !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
|
| 115 | }
|
| 116 |
|
| 117 | static inline bool crypto_hash_no_export_core(struct crypto_ahash *tfm)
|
| 118 | {
|
| 119 | return crypto_hash_alg_common(tfm)->base.cra_flags &
|
| 120 | CRYPTO_AHASH_ALG_NO_EXPORT_CORE;
|
| 121 | }
|
| 122 |
|
| 123 | int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
|
| 124 | struct crypto_instance *inst,
|
| 125 | const char *name, u32 type, u32 mask);
|
| 126 |
|
| 127 | static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
|
| 128 | {
|
| 129 | crypto_drop_spawn(&spawn->base);
|
| 130 | }
|
| 131 |
|
| 132 | static inline struct hash_alg_common *crypto_spawn_ahash_alg(
|
| 133 | struct crypto_ahash_spawn *spawn)
|
| 134 | {
|
| 135 | return __crypto_hash_alg_common(spawn->base.alg);
|
| 136 | }
|
| 137 |
|
| 138 | int crypto_register_shash(struct shash_alg *alg);
|
| 139 | void crypto_unregister_shash(struct shash_alg *alg);
|
| 140 | int crypto_register_shashes(struct shash_alg *algs, int count);
|
| 141 | void crypto_unregister_shashes(struct shash_alg *algs, int count);
|
| 142 | int shash_register_instance(struct crypto_template *tmpl,
|
| 143 | struct shash_instance *inst);
|
| 144 | void shash_free_singlespawn_instance(struct shash_instance *inst);
|
| 145 |
|
| 146 | int crypto_grab_shash(struct crypto_shash_spawn *spawn,
|
| 147 | struct crypto_instance *inst,
|
| 148 | const char *name, u32 type, u32 mask);
|
| 149 |
|
| 150 | static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
|
| 151 | {
|
| 152 | crypto_drop_spawn(&spawn->base);
|
| 153 | }
|
| 154 |
|
| 155 | static inline struct shash_alg *crypto_spawn_shash_alg(
|
| 156 | struct crypto_shash_spawn *spawn)
|
| 157 | {
|
| 158 | return __crypto_shash_alg(spawn->base.alg);
|
| 159 | }
|
| 160 |
|
| 161 | int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
|
| 162 | int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
|
| 163 | int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
|
| 164 |
|
| 165 | static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
|
| 166 | {
|
| 167 | return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
|
| 168 | }
|
| 169 |
|
| 170 | static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm)
|
| 171 | {
|
| 172 | return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm));
|
| 173 | }
|
| 174 |
|
| 175 | static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
|
| 176 | {
|
| 177 | return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
|
| 178 | halg);
|
| 179 | }
|
| 180 |
|
| 181 | static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
|
| 182 | {
|
| 183 | return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
|
| 184 | halg);
|
| 185 | }
|
| 186 |
|
| 187 | static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm,
|
| 188 | unsigned int size)
|
| 189 | {
|
| 190 | tfm->statesize = size;
|
| 191 | }
|
| 192 |
|
| 193 | static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
|
| 194 | unsigned int reqsize)
|
| 195 | {
|
| 196 | tfm->reqsize = reqsize;
|
| 197 | }
|
| 198 |
|
| 199 | static inline bool crypto_ahash_tested(struct crypto_ahash *tfm)
|
| 200 | {
|
| 201 | struct crypto_tfm *tfm_base = crypto_ahash_tfm(tfm);
|
| 202 |
|
| 203 | return tfm_base->__crt_alg->cra_flags & CRYPTO_ALG_TESTED;
|
| 204 | }
|
| 205 |
|
| 206 | static inline void crypto_ahash_set_reqsize_dma(struct crypto_ahash *ahash,
|
| 207 | unsigned int reqsize)
|
| 208 | {
|
| 209 | reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
|
| 210 | ahash->reqsize = reqsize;
|
| 211 | }
|
| 212 |
|
| 213 | static inline struct crypto_instance *ahash_crypto_instance(
|
| 214 | struct ahash_instance *inst)
|
| 215 | {
|
| 216 | return &inst->s.base;
|
| 217 | }
|
| 218 |
|
| 219 | static inline struct ahash_instance *ahash_instance(
|
| 220 | struct crypto_instance *inst)
|
| 221 | {
|
| 222 | return container_of(inst, struct ahash_instance, s.base);
|
| 223 | }
|
| 224 |
|
| 225 | static inline struct ahash_instance *ahash_alg_instance(
|
| 226 | struct crypto_ahash *ahash)
|
| 227 | {
|
| 228 | return ahash_instance(crypto_tfm_alg_instance(&ahash->base));
|
| 229 | }
|
| 230 |
|
| 231 | static inline void *ahash_instance_ctx(struct ahash_instance *inst)
|
| 232 | {
|
| 233 | return crypto_instance_ctx(ahash_crypto_instance(inst));
|
| 234 | }
|
| 235 |
|
| 236 | static inline void *ahash_request_ctx_dma(struct ahash_request *req)
|
| 237 | {
|
| 238 | unsigned int align = crypto_dma_align();
|
| 239 |
|
| 240 | if (align <= crypto_tfm_ctx_alignment())
|
| 241 | align = 1;
|
| 242 |
|
| 243 | return PTR_ALIGN(ahash_request_ctx(req), align);
|
| 244 | }
|
| 245 |
|
| 246 | static inline void ahash_request_complete(struct ahash_request *req, int err)
|
| 247 | {
|
| 248 | crypto_request_complete(&req->base, err);
|
| 249 | }
|
| 250 |
|
| 251 | static inline u32 ahash_request_flags(struct ahash_request *req)
|
| 252 | {
|
| 253 | return crypto_request_flags(&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE;
|
| 254 | }
|
| 255 |
|
| 256 | static inline struct crypto_ahash *crypto_spawn_ahash(
|
| 257 | struct crypto_ahash_spawn *spawn)
|
| 258 | {
|
| 259 | return crypto_spawn_tfm2(&spawn->base);
|
| 260 | }
|
| 261 |
|
| 262 | static inline int ahash_enqueue_request(struct crypto_queue *queue,
|
| 263 | struct ahash_request *request)
|
| 264 | {
|
| 265 | return crypto_enqueue_request(queue, &request->base);
|
| 266 | }
|
| 267 |
|
| 268 | static inline struct ahash_request *ahash_dequeue_request(
|
| 269 | struct crypto_queue *queue)
|
| 270 | {
|
| 271 | return ahash_request_cast(crypto_dequeue_request(queue));
|
| 272 | }
|
| 273 |
|
| 274 | static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
|
| 275 | {
|
| 276 | return crypto_tfm_ctx(&tfm->base);
|
| 277 | }
|
| 278 |
|
| 279 | static inline struct crypto_instance *shash_crypto_instance(
|
| 280 | struct shash_instance *inst)
|
| 281 | {
|
| 282 | return &inst->s.base;
|
| 283 | }
|
| 284 |
|
| 285 | static inline struct shash_instance *shash_instance(
|
| 286 | struct crypto_instance *inst)
|
| 287 | {
|
| 288 | return container_of(inst, struct shash_instance, s.base);
|
| 289 | }
|
| 290 |
|
| 291 | static inline struct shash_instance *shash_alg_instance(
|
| 292 | struct crypto_shash *shash)
|
| 293 | {
|
| 294 | return shash_instance(crypto_tfm_alg_instance(&shash->base));
|
| 295 | }
|
| 296 |
|
| 297 | static inline void *shash_instance_ctx(struct shash_instance *inst)
|
| 298 | {
|
| 299 | return crypto_instance_ctx(shash_crypto_instance(inst));
|
| 300 | }
|
| 301 |
|
| 302 | static inline struct crypto_shash *crypto_spawn_shash(
|
| 303 | struct crypto_shash_spawn *spawn)
|
| 304 | {
|
| 305 | return crypto_spawn_tfm2(&spawn->base);
|
| 306 | }
|
| 307 |
|
| 308 | static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
|
| 309 | {
|
| 310 | return container_of(tfm, struct crypto_shash, base);
|
| 311 | }
|
| 312 |
|
| 313 | static inline bool ahash_request_isvirt(struct ahash_request *req)
|
| 314 | {
|
| 315 | return req->base.flags & CRYPTO_AHASH_REQ_VIRT;
|
| 316 | }
|
| 317 |
|
| 318 | static inline bool crypto_ahash_req_virt(struct crypto_ahash *tfm)
|
| 319 | {
|
| 320 | return crypto_tfm_req_virt(&tfm->base);
|
| 321 | }
|
| 322 |
|
| 323 | static inline struct crypto_ahash *crypto_ahash_fb(struct crypto_ahash *tfm)
|
| 324 | {
|
| 325 | return __crypto_ahash_cast(crypto_ahash_tfm(tfm)->fb);
|
| 326 | }
|
| 327 |
|
| 328 | static inline struct ahash_request *ahash_fbreq_on_stack_init(
|
| 329 | char *buf, struct ahash_request *old)
|
| 330 | {
|
| 331 | struct crypto_ahash *tfm = crypto_ahash_reqtfm(old);
|
| 332 | struct ahash_request *req = (void *)buf;
|
| 333 |
|
| 334 | crypto_stack_request_init(&req->base,
|
| 335 | crypto_ahash_tfm(crypto_ahash_fb(tfm)));
|
| 336 | ahash_request_set_callback(req, ahash_request_flags(old), NULL, NULL);
|
| 337 | req->base.flags &= ~CRYPTO_AHASH_REQ_PRIVATE;
|
| 338 | req->base.flags |= old->base.flags & CRYPTO_AHASH_REQ_PRIVATE;
|
| 339 | req->src = old->src;
|
| 340 | req->result = old->result;
|
| 341 | req->nbytes = old->nbytes;
|
| 342 |
|
| 343 | return req;
|
| 344 | }
|
| 345 |
|
| 346 |
|
| 347 | static inline unsigned int crypto_shash_coresize(struct crypto_shash *tfm)
|
| 348 | {
|
| 349 | return crypto_shash_statesize(tfm) - crypto_shash_blocksize(tfm) - 1;
|
| 350 | }
|
| 351 |
|
| 352 |
|
| 353 | #define HASH_REQUEST_ZERO(name) \
|
| 354 | memzero_explicit(__##name##_req, sizeof(__##name##_req))
|
| 355 |
|
| 356 | |
| 357 | * crypto_ahash_export_core() - extract core state for message digest
|
| 358 | * @req: reference to the ahash_request handle whose state is exported
|
| 359 | * @out: output buffer of sufficient size that can hold the hash state
|
| 360 | *
|
| 361 | * Export the hash state without the partial block buffer.
|
| 362 | *
|
| 363 | * Context: Softirq or process context.
|
| 364 | * Return: 0 if the export creation was successful; < 0 if an error occurred
|
| 365 |
|
| 366 | int crypto_ahash_export_core(struct ahash_request *req, void *out);
|
| 367 |
|
| 368 | |
| 369 | * crypto_ahash_import_core() - import core state
|
| 370 | * @req: reference to ahash_request handle the state is imported into
|
| 371 | * @in: buffer holding the state
|
| 372 | *
|
| 373 | * Import the hash state without the partial block buffer.
|
| 374 | *
|
| 375 | * Context: Softirq or process context.
|
| 376 | * Return: 0 if the import was successful; < 0 if an error occurred
|
| 377 |
|
| 378 | int crypto_ahash_import_core(struct ahash_request *req, const void *in);
|
| 379 |
|
| 380 | |
| 381 | * crypto_shash_export_core() - extract core state for message digest
|
| 382 | * @desc: reference to the operational state handle whose state is exported
|
| 383 | * @out: output buffer of sufficient size that can hold the hash state
|
| 384 | *
|
| 385 | * Export the hash state without the partial block buffer.
|
| 386 | *
|
| 387 | * Context: Softirq or process context.
|
| 388 | * Return: 0 if the export creation was successful; < 0 if an error occurred
|
| 389 |
|
| 390 | int crypto_shash_export_core(struct shash_desc *desc, void *out);
|
| 391 |
|
| 392 | |
| 393 | * crypto_shash_import_core() - import core state
|
| 394 | * @desc: reference to the operational state handle the state imported into
|
| 395 | * @in: buffer holding the state
|
| 396 | *
|
| 397 | * Import the hash state without the partial block buffer.
|
| 398 | *
|
| 399 | * Context: Softirq or process context.
|
| 400 | * Return: 0 if the import was successful; < 0 if an error occurred
|
| 401 |
|
| 402 | int crypto_shash_import_core(struct shash_desc *desc, const void *in);
|
| 403 |
|
| 404 | #endif
|
| 405 |
|