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