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