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