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