Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Hash: Hash algorithms under the crypto API |
| 3 | * |
| 4 | * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #ifndef _CRYPTO_HASH_H |
| 14 | #define _CRYPTO_HASH_H |
| 15 | |
| 16 | #include <linux/crypto.h> |
| 17 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 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 | |
Herbert Xu | 66f6ce5 | 2009-07-15 12:40:40 +0800 | [diff] [blame] | 34 | /* This field may only be used by the ahash API code. */ |
| 35 | void *priv; |
| 36 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 37 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 38 | }; |
| 39 | |
| 40 | struct ahash_alg { |
| 41 | int (*init)(struct ahash_request *req); |
| 42 | int (*update)(struct ahash_request *req); |
| 43 | int (*final)(struct ahash_request *req); |
| 44 | int (*finup)(struct ahash_request *req); |
| 45 | int (*digest)(struct ahash_request *req); |
| 46 | int (*export)(struct ahash_request *req, void *out); |
| 47 | int (*import)(struct ahash_request *req, const void *in); |
| 48 | int (*setkey)(struct crypto_ahash *tfm, const u8 *key, |
| 49 | unsigned int keylen); |
| 50 | |
| 51 | struct hash_alg_common halg; |
| 52 | }; |
| 53 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 54 | struct shash_desc { |
| 55 | struct crypto_shash *tfm; |
| 56 | u32 flags; |
| 57 | |
| 58 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 59 | }; |
| 60 | |
Behan Webster | a0a77af | 2014-09-08 00:05:09 -0500 | [diff] [blame] | 61 | #define SHASH_DESC_ON_STACK(shash, ctx) \ |
| 62 | char __##shash##_desc[sizeof(struct shash_desc) + \ |
| 63 | crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \ |
| 64 | struct shash_desc *shash = (struct shash_desc *)__##shash##_desc |
| 65 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 66 | struct shash_alg { |
| 67 | int (*init)(struct shash_desc *desc); |
| 68 | int (*update)(struct shash_desc *desc, const u8 *data, |
| 69 | unsigned int len); |
| 70 | int (*final)(struct shash_desc *desc, u8 *out); |
| 71 | int (*finup)(struct shash_desc *desc, const u8 *data, |
| 72 | unsigned int len, u8 *out); |
| 73 | int (*digest)(struct shash_desc *desc, const u8 *data, |
| 74 | unsigned int len, u8 *out); |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 75 | int (*export)(struct shash_desc *desc, void *out); |
| 76 | int (*import)(struct shash_desc *desc, const void *in); |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 77 | int (*setkey)(struct crypto_shash *tfm, const u8 *key, |
| 78 | unsigned int keylen); |
| 79 | |
| 80 | unsigned int descsize; |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 81 | |
| 82 | /* These fields must match hash_alg_common. */ |
Herbert Xu | fa64966 | 2009-07-15 21:16:05 +0800 | [diff] [blame] | 83 | unsigned int digestsize |
| 84 | __attribute__ ((aligned(__alignof__(struct hash_alg_common)))); |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 85 | unsigned int statesize; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 86 | |
| 87 | struct crypto_alg base; |
| 88 | }; |
| 89 | |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 90 | struct crypto_ahash { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 91 | int (*init)(struct ahash_request *req); |
| 92 | int (*update)(struct ahash_request *req); |
| 93 | int (*final)(struct ahash_request *req); |
| 94 | int (*finup)(struct ahash_request *req); |
| 95 | int (*digest)(struct ahash_request *req); |
| 96 | int (*export)(struct ahash_request *req, void *out); |
| 97 | int (*import)(struct ahash_request *req, const void *in); |
| 98 | int (*setkey)(struct crypto_ahash *tfm, const u8 *key, |
| 99 | unsigned int keylen); |
| 100 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 101 | unsigned int reqsize; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 102 | struct crypto_tfm base; |
| 103 | }; |
| 104 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 105 | struct crypto_shash { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame] | 106 | unsigned int descsize; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 107 | struct crypto_tfm base; |
| 108 | }; |
| 109 | |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 110 | static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) |
| 111 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 112 | return container_of(tfm, struct crypto_ahash, base); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 115 | struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, |
| 116 | u32 mask); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 117 | |
| 118 | static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm) |
| 119 | { |
| 120 | return &tfm->base; |
| 121 | } |
| 122 | |
| 123 | static inline void crypto_free_ahash(struct crypto_ahash *tfm) |
| 124 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 125 | crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm)); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static inline unsigned int crypto_ahash_alignmask( |
| 129 | struct crypto_ahash *tfm) |
| 130 | { |
| 131 | return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm)); |
| 132 | } |
| 133 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 134 | static inline struct hash_alg_common *__crypto_hash_alg_common( |
| 135 | struct crypto_alg *alg) |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 136 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 137 | return container_of(alg, struct hash_alg_common, base); |
| 138 | } |
| 139 | |
| 140 | static inline struct hash_alg_common *crypto_hash_alg_common( |
| 141 | struct crypto_ahash *tfm) |
| 142 | { |
| 143 | return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm) |
| 147 | { |
Herbert Xu | 500b3e3 | 2009-07-14 20:29:57 +0800 | [diff] [blame] | 148 | return crypto_hash_alg_common(tfm)->digestsize; |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm) |
| 152 | { |
| 153 | return crypto_hash_alg_common(tfm)->statesize; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm) |
| 157 | { |
| 158 | return crypto_tfm_get_flags(crypto_ahash_tfm(tfm)); |
| 159 | } |
| 160 | |
| 161 | static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags) |
| 162 | { |
| 163 | crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags); |
| 164 | } |
| 165 | |
| 166 | static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags) |
| 167 | { |
| 168 | crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags); |
| 169 | } |
| 170 | |
| 171 | static inline struct crypto_ahash *crypto_ahash_reqtfm( |
| 172 | struct ahash_request *req) |
| 173 | { |
| 174 | return __crypto_ahash_cast(req->base.tfm); |
| 175 | } |
| 176 | |
| 177 | static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm) |
| 178 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 179 | return tfm->reqsize; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 182 | static inline void *ahash_request_ctx(struct ahash_request *req) |
| 183 | { |
| 184 | return req->__ctx; |
| 185 | } |
| 186 | |
Herbert Xu | 66f6ce5 | 2009-07-15 12:40:40 +0800 | [diff] [blame] | 187 | int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 188 | unsigned int keylen); |
| 189 | int crypto_ahash_finup(struct ahash_request *req); |
| 190 | int crypto_ahash_final(struct ahash_request *req); |
| 191 | int crypto_ahash_digest(struct ahash_request *req); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 192 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 193 | static inline int crypto_ahash_export(struct ahash_request *req, void *out) |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 194 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 195 | return crypto_ahash_reqtfm(req)->export(req, out); |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 198 | static inline int crypto_ahash_import(struct ahash_request *req, const void *in) |
| 199 | { |
| 200 | return crypto_ahash_reqtfm(req)->import(req, in); |
| 201 | } |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 202 | |
Herbert Xu | 318e531 | 2008-08-05 13:34:30 +0800 | [diff] [blame] | 203 | static inline int crypto_ahash_init(struct ahash_request *req) |
| 204 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 205 | return crypto_ahash_reqtfm(req)->init(req); |
Herbert Xu | 318e531 | 2008-08-05 13:34:30 +0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static inline int crypto_ahash_update(struct ahash_request *req) |
| 209 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 210 | return crypto_ahash_reqtfm(req)->update(req); |
Herbert Xu | 318e531 | 2008-08-05 13:34:30 +0800 | [diff] [blame] | 211 | } |
| 212 | |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 213 | static inline void ahash_request_set_tfm(struct ahash_request *req, |
| 214 | struct crypto_ahash *tfm) |
| 215 | { |
| 216 | req->base.tfm = crypto_ahash_tfm(tfm); |
| 217 | } |
| 218 | |
| 219 | static inline struct ahash_request *ahash_request_alloc( |
| 220 | struct crypto_ahash *tfm, gfp_t gfp) |
| 221 | { |
| 222 | struct ahash_request *req; |
| 223 | |
| 224 | req = kmalloc(sizeof(struct ahash_request) + |
| 225 | crypto_ahash_reqsize(tfm), gfp); |
| 226 | |
| 227 | if (likely(req)) |
| 228 | ahash_request_set_tfm(req, tfm); |
| 229 | |
| 230 | return req; |
| 231 | } |
| 232 | |
| 233 | static inline void ahash_request_free(struct ahash_request *req) |
| 234 | { |
Herbert Xu | aef73cf | 2009-07-11 22:22:14 +0800 | [diff] [blame] | 235 | kzfree(req); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static inline struct ahash_request *ahash_request_cast( |
| 239 | struct crypto_async_request *req) |
| 240 | { |
| 241 | return container_of(req, struct ahash_request, base); |
| 242 | } |
| 243 | |
| 244 | static inline void ahash_request_set_callback(struct ahash_request *req, |
| 245 | u32 flags, |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 246 | crypto_completion_t compl, |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 247 | void *data) |
| 248 | { |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 249 | req->base.complete = compl; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 250 | req->base.data = data; |
| 251 | req->base.flags = flags; |
| 252 | } |
| 253 | |
| 254 | static inline void ahash_request_set_crypt(struct ahash_request *req, |
| 255 | struct scatterlist *src, u8 *result, |
| 256 | unsigned int nbytes) |
| 257 | { |
| 258 | req->src = src; |
| 259 | req->nbytes = nbytes; |
| 260 | req->result = result; |
| 261 | } |
| 262 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 263 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
| 264 | u32 mask); |
| 265 | |
| 266 | static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm) |
| 267 | { |
| 268 | return &tfm->base; |
| 269 | } |
| 270 | |
| 271 | static inline void crypto_free_shash(struct crypto_shash *tfm) |
| 272 | { |
Herbert Xu | 412e87a | 2009-02-05 16:51:25 +1100 | [diff] [blame] | 273 | crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm)); |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static inline unsigned int crypto_shash_alignmask( |
| 277 | struct crypto_shash *tfm) |
| 278 | { |
| 279 | return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm)); |
| 280 | } |
| 281 | |
Herbert Xu | 9749598 | 2009-02-03 12:47:44 +1100 | [diff] [blame] | 282 | static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm) |
| 283 | { |
| 284 | return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm)); |
| 285 | } |
| 286 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 287 | static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg) |
| 288 | { |
| 289 | return container_of(alg, struct shash_alg, base); |
| 290 | } |
| 291 | |
| 292 | static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm) |
| 293 | { |
| 294 | return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg); |
| 295 | } |
| 296 | |
| 297 | static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm) |
| 298 | { |
| 299 | return crypto_shash_alg(tfm)->digestsize; |
| 300 | } |
| 301 | |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 302 | static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm) |
| 303 | { |
| 304 | return crypto_shash_alg(tfm)->statesize; |
| 305 | } |
| 306 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 307 | static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm) |
| 308 | { |
| 309 | return crypto_tfm_get_flags(crypto_shash_tfm(tfm)); |
| 310 | } |
| 311 | |
| 312 | static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags) |
| 313 | { |
| 314 | crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags); |
| 315 | } |
| 316 | |
| 317 | static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags) |
| 318 | { |
| 319 | crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags); |
| 320 | } |
| 321 | |
| 322 | static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm) |
| 323 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame] | 324 | return tfm->descsize; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static inline void *shash_desc_ctx(struct shash_desc *desc) |
| 328 | { |
| 329 | return desc->__ctx; |
| 330 | } |
| 331 | |
| 332 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, |
| 333 | unsigned int keylen); |
| 334 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
| 335 | unsigned int len, u8 *out); |
| 336 | |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 337 | static inline int crypto_shash_export(struct shash_desc *desc, void *out) |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 338 | { |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 339 | return crypto_shash_alg(desc->tfm)->export(desc, out); |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 340 | } |
| 341 | |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 342 | static inline int crypto_shash_import(struct shash_desc *desc, const void *in) |
| 343 | { |
| 344 | return crypto_shash_alg(desc->tfm)->import(desc, in); |
| 345 | } |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 346 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 347 | static inline int crypto_shash_init(struct shash_desc *desc) |
| 348 | { |
| 349 | return crypto_shash_alg(desc->tfm)->init(desc); |
| 350 | } |
| 351 | |
| 352 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, |
| 353 | unsigned int len); |
| 354 | int crypto_shash_final(struct shash_desc *desc, u8 *out); |
| 355 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, |
| 356 | unsigned int len, u8 *out); |
| 357 | |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 358 | #endif /* _CRYPTO_HASH_H */ |