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 #define HASH_FBREQ_ON_STACK(name, req) \ 24 char __##name##_req[sizeof(struct ahash_request) + \ 25 MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \ 26 struct ahash_request *name = ahash_fbreq_on_stack_init( \ 27 __##name##_req, (req)) 28 29 struct ahash_request; 30 31 struct ahash_instance { 32 void (*free)(struct ahash_instance *inst); 33 union { 34 struct { 35 char head[offsetof(struct ahash_alg, halg.base)]; 36 struct crypto_instance base; 37 } s; 38 struct ahash_alg alg; 39 }; 40 }; 41 42 struct shash_instance { 43 void (*free)(struct shash_instance *inst); 44 union { 45 struct { 46 char head[offsetof(struct shash_alg, base)]; 47 struct crypto_instance base; 48 } s; 49 struct shash_alg alg; 50 }; 51 }; 52 53 struct crypto_ahash_spawn { 54 struct crypto_spawn base; 55 }; 56 57 struct crypto_shash_spawn { 58 struct crypto_spawn base; 59 }; 60 61 int crypto_register_ahash(struct ahash_alg *alg); 62 void crypto_unregister_ahash(struct ahash_alg *alg); 63 int crypto_register_ahashes(struct ahash_alg *algs, int count); 64 void crypto_unregister_ahashes(struct ahash_alg *algs, int count); 65 int ahash_register_instance(struct crypto_template *tmpl, 66 struct ahash_instance *inst); 67 68 int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, 69 unsigned int keylen); 70 71 static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg) 72 { 73 return alg->setkey != shash_no_setkey; 74 } 75 76 static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg) 77 { 78 return crypto_shash_alg_has_setkey(alg) && 79 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY); 80 } 81 82 int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, 83 struct crypto_instance *inst, 84 const char *name, u32 type, u32 mask); 85 86 static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn) 87 { 88 crypto_drop_spawn(&spawn->base); 89 } 90 91 static inline struct hash_alg_common *crypto_spawn_ahash_alg( 92 struct crypto_ahash_spawn *spawn) 93 { 94 return __crypto_hash_alg_common(spawn->base.alg); 95 } 96 97 int crypto_register_shash(struct shash_alg *alg); 98 void crypto_unregister_shash(struct shash_alg *alg); 99 int crypto_register_shashes(struct shash_alg *algs, int count); 100 void crypto_unregister_shashes(struct shash_alg *algs, int count); 101 int shash_register_instance(struct crypto_template *tmpl, 102 struct shash_instance *inst); 103 void shash_free_singlespawn_instance(struct shash_instance *inst); 104 105 int crypto_grab_shash(struct crypto_shash_spawn *spawn, 106 struct crypto_instance *inst, 107 const char *name, u32 type, u32 mask); 108 109 static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn) 110 { 111 crypto_drop_spawn(&spawn->base); 112 } 113 114 static inline struct shash_alg *crypto_spawn_shash_alg( 115 struct crypto_shash_spawn *spawn) 116 { 117 return __crypto_shash_alg(spawn->base.alg); 118 } 119 120 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc); 121 int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc); 122 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc); 123 124 static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm) 125 { 126 return crypto_tfm_ctx(crypto_ahash_tfm(tfm)); 127 } 128 129 static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm) 130 { 131 return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm)); 132 } 133 134 static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg) 135 { 136 return container_of(__crypto_hash_alg_common(alg), struct ahash_alg, 137 halg); 138 } 139 140 static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash) 141 { 142 return container_of(crypto_hash_alg_common(hash), struct ahash_alg, 143 halg); 144 } 145 146 static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm, 147 unsigned int size) 148 { 149 tfm->statesize = size; 150 } 151 152 static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm, 153 unsigned int reqsize) 154 { 155 tfm->reqsize = reqsize; 156 } 157 158 static inline void crypto_ahash_set_reqsize_dma(struct crypto_ahash *ahash, 159 unsigned int reqsize) 160 { 161 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1); 162 ahash->reqsize = reqsize; 163 } 164 165 static inline struct crypto_instance *ahash_crypto_instance( 166 struct ahash_instance *inst) 167 { 168 return &inst->s.base; 169 } 170 171 static inline struct ahash_instance *ahash_instance( 172 struct crypto_instance *inst) 173 { 174 return container_of(inst, struct ahash_instance, s.base); 175 } 176 177 static inline struct ahash_instance *ahash_alg_instance( 178 struct crypto_ahash *ahash) 179 { 180 return ahash_instance(crypto_tfm_alg_instance(&ahash->base)); 181 } 182 183 static inline void *ahash_instance_ctx(struct ahash_instance *inst) 184 { 185 return crypto_instance_ctx(ahash_crypto_instance(inst)); 186 } 187 188 static inline void *ahash_request_ctx_dma(struct ahash_request *req) 189 { 190 unsigned int align = crypto_dma_align(); 191 192 if (align <= crypto_tfm_ctx_alignment()) 193 align = 1; 194 195 return PTR_ALIGN(ahash_request_ctx(req), align); 196 } 197 198 static inline void ahash_request_complete(struct ahash_request *req, int err) 199 { 200 crypto_request_complete(&req->base, err); 201 } 202 203 static inline u32 ahash_request_flags(struct ahash_request *req) 204 { 205 return crypto_request_flags(&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE; 206 } 207 208 static inline struct crypto_ahash *crypto_spawn_ahash( 209 struct crypto_ahash_spawn *spawn) 210 { 211 return crypto_spawn_tfm2(&spawn->base); 212 } 213 214 static inline int ahash_enqueue_request(struct crypto_queue *queue, 215 struct ahash_request *request) 216 { 217 return crypto_enqueue_request(queue, &request->base); 218 } 219 220 static inline struct ahash_request *ahash_dequeue_request( 221 struct crypto_queue *queue) 222 { 223 return ahash_request_cast(crypto_dequeue_request(queue)); 224 } 225 226 static inline void *crypto_shash_ctx(struct crypto_shash *tfm) 227 { 228 return crypto_tfm_ctx(&tfm->base); 229 } 230 231 static inline struct crypto_instance *shash_crypto_instance( 232 struct shash_instance *inst) 233 { 234 return &inst->s.base; 235 } 236 237 static inline struct shash_instance *shash_instance( 238 struct crypto_instance *inst) 239 { 240 return container_of(inst, struct shash_instance, s.base); 241 } 242 243 static inline struct shash_instance *shash_alg_instance( 244 struct crypto_shash *shash) 245 { 246 return shash_instance(crypto_tfm_alg_instance(&shash->base)); 247 } 248 249 static inline void *shash_instance_ctx(struct shash_instance *inst) 250 { 251 return crypto_instance_ctx(shash_crypto_instance(inst)); 252 } 253 254 static inline struct crypto_shash *crypto_spawn_shash( 255 struct crypto_shash_spawn *spawn) 256 { 257 return crypto_spawn_tfm2(&spawn->base); 258 } 259 260 static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm) 261 { 262 return container_of(tfm, struct crypto_shash, base); 263 } 264 265 static inline bool ahash_request_isvirt(struct ahash_request *req) 266 { 267 return req->base.flags & CRYPTO_AHASH_REQ_VIRT; 268 } 269 270 static inline bool crypto_ahash_req_virt(struct crypto_ahash *tfm) 271 { 272 return crypto_tfm_req_virt(&tfm->base); 273 } 274 275 static inline struct crypto_ahash *crypto_ahash_fb(struct crypto_ahash *tfm) 276 { 277 return __crypto_ahash_cast(crypto_ahash_tfm(tfm)->fb); 278 } 279 280 static inline struct ahash_request *ahash_fbreq_on_stack_init( 281 char *buf, struct ahash_request *old) 282 { 283 struct crypto_ahash *tfm = crypto_ahash_reqtfm(old); 284 struct ahash_request *req = (void *)buf; 285 286 crypto_stack_request_init(&req->base, 287 crypto_ahash_tfm(crypto_ahash_fb(tfm))); 288 ahash_request_set_callback(req, ahash_request_flags(old), NULL, NULL); 289 req->base.flags &= ~CRYPTO_AHASH_REQ_PRIVATE; 290 req->base.flags |= old->base.flags & CRYPTO_AHASH_REQ_PRIVATE; 291 req->src = old->src; 292 req->result = old->result; 293 req->nbytes = old->nbytes; 294 295 return req; 296 } 297 298 /* Return the state size without partial block for block-only algorithms. */ 299 static inline unsigned int crypto_shash_coresize(struct crypto_shash *tfm) 300 { 301 return crypto_shash_statesize(tfm) - crypto_shash_blocksize(tfm) - 1; 302 } 303 304 /* This can only be used if the request was never cloned. */ 305 #define HASH_REQUEST_ZERO(name) \ 306 memzero_explicit(__##name##_req, sizeof(__##name##_req)) 307 308 #endif /* _CRYPTO_INTERNAL_HASH_H */ 309 310