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 | |
Stephan Mueller | 5d8c723 | 2014-11-12 05:26:03 +0100 | [diff] [blame] | 20 | /** |
| 21 | * DOC: Message Digest Algorithm Definitions |
| 22 | * |
| 23 | * These data structures define modular message digest algorithm |
| 24 | * implementations, managed via crypto_register_ahash(), |
| 25 | * crypto_register_shash(), crypto_unregister_ahash() and |
| 26 | * crypto_unregister_shash(). |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * struct hash_alg_common - define properties of message digest |
| 31 | * @digestsize: Size of the result of the transformation. A buffer of this size |
| 32 | * must be available to the @final and @finup calls, so they can |
| 33 | * store the resulting hash into it. For various predefined sizes, |
| 34 | * search include/crypto/ using |
| 35 | * git grep _DIGEST_SIZE include/crypto. |
| 36 | * @statesize: Size of the block for partial state of the transformation. A |
| 37 | * buffer of this size must be passed to the @export function as it |
| 38 | * will save the partial state of the transformation into it. On the |
| 39 | * other side, the @import function will load the state from a |
| 40 | * buffer of this size as well. |
| 41 | */ |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 42 | struct hash_alg_common { |
| 43 | unsigned int digestsize; |
| 44 | unsigned int statesize; |
| 45 | |
| 46 | struct crypto_alg base; |
| 47 | }; |
| 48 | |
| 49 | struct ahash_request { |
| 50 | struct crypto_async_request base; |
| 51 | |
| 52 | unsigned int nbytes; |
| 53 | struct scatterlist *src; |
| 54 | u8 *result; |
| 55 | |
Herbert Xu | 66f6ce5 | 2009-07-15 12:40:40 +0800 | [diff] [blame] | 56 | /* This field may only be used by the ahash API code. */ |
| 57 | void *priv; |
| 58 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 59 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 60 | }; |
| 61 | |
Stephan Mueller | 5d8c723 | 2014-11-12 05:26:03 +0100 | [diff] [blame] | 62 | /** |
| 63 | * struct ahash_alg - asynchronous message digest definition |
| 64 | * @init: Initialize the transformation context. Intended only to initialize the |
| 65 | * state of the HASH transformation at the begining. This shall fill in |
| 66 | * the internal structures used during the entire duration of the whole |
| 67 | * transformation. No data processing happens at this point. |
| 68 | * @update: Push a chunk of data into the driver for transformation. This |
| 69 | * function actually pushes blocks of data from upper layers into the |
| 70 | * driver, which then passes those to the hardware as seen fit. This |
| 71 | * function must not finalize the HASH transformation by calculating the |
| 72 | * final message digest as this only adds more data into the |
| 73 | * transformation. This function shall not modify the transformation |
| 74 | * context, as this function may be called in parallel with the same |
| 75 | * transformation object. Data processing can happen synchronously |
| 76 | * [SHASH] or asynchronously [AHASH] at this point. |
| 77 | * @final: Retrieve result from the driver. This function finalizes the |
| 78 | * transformation and retrieves the resulting hash from the driver and |
| 79 | * pushes it back to upper layers. No data processing happens at this |
| 80 | * point. |
| 81 | * @finup: Combination of @update and @final. This function is effectively a |
| 82 | * combination of @update and @final calls issued in sequence. As some |
| 83 | * hardware cannot do @update and @final separately, this callback was |
| 84 | * added to allow such hardware to be used at least by IPsec. Data |
| 85 | * processing can happen synchronously [SHASH] or asynchronously [AHASH] |
| 86 | * at this point. |
| 87 | * @digest: Combination of @init and @update and @final. This function |
| 88 | * effectively behaves as the entire chain of operations, @init, |
| 89 | * @update and @final issued in sequence. Just like @finup, this was |
| 90 | * added for hardware which cannot do even the @finup, but can only do |
| 91 | * the whole transformation in one run. Data processing can happen |
| 92 | * synchronously [SHASH] or asynchronously [AHASH] at this point. |
| 93 | * @setkey: Set optional key used by the hashing algorithm. Intended to push |
| 94 | * optional key used by the hashing algorithm from upper layers into |
| 95 | * the driver. This function can store the key in the transformation |
| 96 | * context or can outright program it into the hardware. In the former |
| 97 | * case, one must be careful to program the key into the hardware at |
| 98 | * appropriate time and one must be careful that .setkey() can be |
| 99 | * called multiple times during the existence of the transformation |
| 100 | * object. Not all hashing algorithms do implement this function as it |
| 101 | * is only needed for keyed message digests. SHAx/MDx/CRCx do NOT |
| 102 | * implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement |
| 103 | * this function. This function must be called before any other of the |
| 104 | * @init, @update, @final, @finup, @digest is called. No data |
| 105 | * processing happens at this point. |
| 106 | * @export: Export partial state of the transformation. This function dumps the |
| 107 | * entire state of the ongoing transformation into a provided block of |
| 108 | * data so it can be @import 'ed back later on. This is useful in case |
| 109 | * you want to save partial result of the transformation after |
| 110 | * processing certain amount of data and reload this partial result |
| 111 | * multiple times later on for multiple re-use. No data processing |
| 112 | * happens at this point. |
| 113 | * @import: Import partial state of the transformation. This function loads the |
| 114 | * entire state of the ongoing transformation from a provided block of |
| 115 | * data so the transformation can continue from this point onward. No |
| 116 | * data processing happens at this point. |
| 117 | */ |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 118 | struct ahash_alg { |
| 119 | int (*init)(struct ahash_request *req); |
| 120 | int (*update)(struct ahash_request *req); |
| 121 | int (*final)(struct ahash_request *req); |
| 122 | int (*finup)(struct ahash_request *req); |
| 123 | int (*digest)(struct ahash_request *req); |
| 124 | int (*export)(struct ahash_request *req, void *out); |
| 125 | int (*import)(struct ahash_request *req, const void *in); |
| 126 | int (*setkey)(struct crypto_ahash *tfm, const u8 *key, |
| 127 | unsigned int keylen); |
| 128 | |
| 129 | struct hash_alg_common halg; |
| 130 | }; |
| 131 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 132 | struct shash_desc { |
| 133 | struct crypto_shash *tfm; |
| 134 | u32 flags; |
| 135 | |
| 136 | void *__ctx[] CRYPTO_MINALIGN_ATTR; |
| 137 | }; |
| 138 | |
Behan Webster | a0a77af | 2014-09-08 00:05:09 -0500 | [diff] [blame] | 139 | #define SHASH_DESC_ON_STACK(shash, ctx) \ |
| 140 | char __##shash##_desc[sizeof(struct shash_desc) + \ |
| 141 | crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \ |
| 142 | struct shash_desc *shash = (struct shash_desc *)__##shash##_desc |
| 143 | |
Stephan Mueller | 5d8c723 | 2014-11-12 05:26:03 +0100 | [diff] [blame] | 144 | /** |
| 145 | * struct shash_alg - synchronous message digest definition |
| 146 | * @init: see struct ahash_alg |
| 147 | * @update: see struct ahash_alg |
| 148 | * @final: see struct ahash_alg |
| 149 | * @finup: see struct ahash_alg |
| 150 | * @digest: see struct ahash_alg |
| 151 | * @export: see struct ahash_alg |
| 152 | * @import: see struct ahash_alg |
| 153 | * @setkey: see struct ahash_alg |
| 154 | * @digestsize: see struct ahash_alg |
| 155 | * @statesize: see struct ahash_alg |
| 156 | * @dedcsize: Size of the operational state for the message digest. This state |
| 157 | * size is the memory size that needs to be allocated for |
| 158 | * shash_desc.__ctx |
| 159 | * @base: internally used |
| 160 | */ |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 161 | struct shash_alg { |
| 162 | int (*init)(struct shash_desc *desc); |
| 163 | int (*update)(struct shash_desc *desc, const u8 *data, |
| 164 | unsigned int len); |
| 165 | int (*final)(struct shash_desc *desc, u8 *out); |
| 166 | int (*finup)(struct shash_desc *desc, const u8 *data, |
| 167 | unsigned int len, u8 *out); |
| 168 | int (*digest)(struct shash_desc *desc, const u8 *data, |
| 169 | unsigned int len, u8 *out); |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 170 | int (*export)(struct shash_desc *desc, void *out); |
| 171 | int (*import)(struct shash_desc *desc, const void *in); |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 172 | int (*setkey)(struct crypto_shash *tfm, const u8 *key, |
| 173 | unsigned int keylen); |
| 174 | |
| 175 | unsigned int descsize; |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 176 | |
| 177 | /* These fields must match hash_alg_common. */ |
Herbert Xu | fa64966 | 2009-07-15 21:16:05 +0800 | [diff] [blame] | 178 | unsigned int digestsize |
| 179 | __attribute__ ((aligned(__alignof__(struct hash_alg_common)))); |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 180 | unsigned int statesize; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 181 | |
| 182 | struct crypto_alg base; |
| 183 | }; |
| 184 | |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 185 | struct crypto_ahash { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 186 | int (*init)(struct ahash_request *req); |
| 187 | int (*update)(struct ahash_request *req); |
| 188 | int (*final)(struct ahash_request *req); |
| 189 | int (*finup)(struct ahash_request *req); |
| 190 | int (*digest)(struct ahash_request *req); |
| 191 | int (*export)(struct ahash_request *req, void *out); |
| 192 | int (*import)(struct ahash_request *req, const void *in); |
| 193 | int (*setkey)(struct crypto_ahash *tfm, const u8 *key, |
| 194 | unsigned int keylen); |
| 195 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 196 | unsigned int reqsize; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 197 | struct crypto_tfm base; |
| 198 | }; |
| 199 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 200 | struct crypto_shash { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame] | 201 | unsigned int descsize; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 202 | struct crypto_tfm base; |
| 203 | }; |
| 204 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 205 | /** |
| 206 | * DOC: Asynchronous Message Digest API |
| 207 | * |
| 208 | * The asynchronous message digest API is used with the ciphers of type |
| 209 | * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto) |
| 210 | * |
| 211 | * The asynchronous cipher operation discussion provided for the |
| 212 | * CRYPTO_ALG_TYPE_ABLKCIPHER API applies here as well. |
| 213 | */ |
| 214 | |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 215 | static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) |
| 216 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 217 | return container_of(tfm, struct crypto_ahash, base); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 220 | /** |
| 221 | * crypto_alloc_ahash() - allocate ahash cipher handle |
| 222 | * @alg_name: is the cra_name / name or cra_driver_name / driver name of the |
| 223 | * ahash cipher |
| 224 | * @type: specifies the type of the cipher |
| 225 | * @mask: specifies the mask for the cipher |
| 226 | * |
| 227 | * Allocate a cipher handle for an ahash. The returned struct |
| 228 | * crypto_ahash is the cipher handle that is required for any subsequent |
| 229 | * API invocation for that ahash. |
| 230 | * |
| 231 | * Return: allocated cipher handle in case of success; IS_ERR() is true in case |
| 232 | * of an error, PTR_ERR() returns the error code. |
| 233 | */ |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 234 | struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, |
| 235 | u32 mask); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 236 | |
| 237 | static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm) |
| 238 | { |
| 239 | return &tfm->base; |
| 240 | } |
| 241 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 242 | /** |
| 243 | * crypto_free_ahash() - zeroize and free the ahash handle |
| 244 | * @tfm: cipher handle to be freed |
| 245 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 246 | static inline void crypto_free_ahash(struct crypto_ahash *tfm) |
| 247 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 248 | crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm)); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static inline unsigned int crypto_ahash_alignmask( |
| 252 | struct crypto_ahash *tfm) |
| 253 | { |
| 254 | return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm)); |
| 255 | } |
| 256 | |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 257 | static inline struct hash_alg_common *__crypto_hash_alg_common( |
| 258 | struct crypto_alg *alg) |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 259 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 260 | return container_of(alg, struct hash_alg_common, base); |
| 261 | } |
| 262 | |
| 263 | static inline struct hash_alg_common *crypto_hash_alg_common( |
| 264 | struct crypto_ahash *tfm) |
| 265 | { |
| 266 | return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 267 | } |
| 268 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 269 | /** |
| 270 | * crypto_ahash_digestsize() - obtain message digest size |
| 271 | * @tfm: cipher handle |
| 272 | * |
| 273 | * The size for the message digest created by the message digest cipher |
| 274 | * referenced with the cipher handle is returned. |
| 275 | * |
| 276 | * |
| 277 | * Return: message digest size of cipher |
| 278 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 279 | static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm) |
| 280 | { |
Herbert Xu | 500b3e3 | 2009-07-14 20:29:57 +0800 | [diff] [blame] | 281 | return crypto_hash_alg_common(tfm)->digestsize; |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm) |
| 285 | { |
| 286 | return crypto_hash_alg_common(tfm)->statesize; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm) |
| 290 | { |
| 291 | return crypto_tfm_get_flags(crypto_ahash_tfm(tfm)); |
| 292 | } |
| 293 | |
| 294 | static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags) |
| 295 | { |
| 296 | crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags); |
| 297 | } |
| 298 | |
| 299 | static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags) |
| 300 | { |
| 301 | crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags); |
| 302 | } |
| 303 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 304 | /** |
| 305 | * crypto_ahash_reqtfm() - obtain cipher handle from request |
| 306 | * @req: asynchronous request handle that contains the reference to the ahash |
| 307 | * cipher handle |
| 308 | * |
| 309 | * Return the ahash cipher handle that is registered with the asynchronous |
| 310 | * request handle ahash_request. |
| 311 | * |
| 312 | * Return: ahash cipher handle |
| 313 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 314 | static inline struct crypto_ahash *crypto_ahash_reqtfm( |
| 315 | struct ahash_request *req) |
| 316 | { |
| 317 | return __crypto_ahash_cast(req->base.tfm); |
| 318 | } |
| 319 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 320 | /** |
| 321 | * crypto_ahash_reqsize() - obtain size of the request data structure |
| 322 | * @tfm: cipher handle |
| 323 | * |
| 324 | * Return the size of the ahash state size. With the crypto_ahash_export |
| 325 | * function, the caller can export the state into a buffer whose size is |
| 326 | * defined with this function. |
| 327 | * |
| 328 | * Return: size of the ahash state |
| 329 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 330 | static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm) |
| 331 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 332 | return tfm->reqsize; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 333 | } |
| 334 | |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 335 | static inline void *ahash_request_ctx(struct ahash_request *req) |
| 336 | { |
| 337 | return req->__ctx; |
| 338 | } |
| 339 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 340 | /** |
| 341 | * crypto_ahash_setkey - set key for cipher handle |
| 342 | * @tfm: cipher handle |
| 343 | * @key: buffer holding the key |
| 344 | * @keylen: length of the key in bytes |
| 345 | * |
| 346 | * The caller provided key is set for the ahash cipher. The cipher |
| 347 | * handle must point to a keyed hash in order for this function to succeed. |
| 348 | * |
| 349 | * Return: 0 if the setting of the key was successful; < 0 if an error occurred |
| 350 | */ |
Herbert Xu | 66f6ce5 | 2009-07-15 12:40:40 +0800 | [diff] [blame] | 351 | int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, |
| 352 | unsigned int keylen); |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 353 | |
| 354 | /** |
| 355 | * crypto_ahash_finup() - update and finalize message digest |
| 356 | * @req: reference to the ahash_request handle that holds all information |
| 357 | * needed to perform the cipher operation |
| 358 | * |
| 359 | * This function is a "short-hand" for the function calls of |
| 360 | * crypto_ahash_update and crypto_shash_final. The parameters have the same |
| 361 | * meaning as discussed for those separate functions. |
| 362 | * |
| 363 | * Return: 0 if the message digest creation was successful; < 0 if an error |
| 364 | * occurred |
| 365 | */ |
Herbert Xu | 66f6ce5 | 2009-07-15 12:40:40 +0800 | [diff] [blame] | 366 | int crypto_ahash_finup(struct ahash_request *req); |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 367 | |
| 368 | /** |
| 369 | * crypto_ahash_final() - calculate message digest |
| 370 | * @req: reference to the ahash_request handle that holds all information |
| 371 | * needed to perform the cipher operation |
| 372 | * |
| 373 | * Finalize the message digest operation and create the message digest |
| 374 | * based on all data added to the cipher handle. The message digest is placed |
| 375 | * into the output buffer registered with the ahash_request handle. |
| 376 | * |
| 377 | * Return: 0 if the message digest creation was successful; < 0 if an error |
| 378 | * occurred |
| 379 | */ |
Herbert Xu | 66f6ce5 | 2009-07-15 12:40:40 +0800 | [diff] [blame] | 380 | int crypto_ahash_final(struct ahash_request *req); |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 381 | |
| 382 | /** |
| 383 | * crypto_ahash_digest() - calculate message digest for a buffer |
| 384 | * @req: reference to the ahash_request handle that holds all information |
| 385 | * needed to perform the cipher operation |
| 386 | * |
| 387 | * This function is a "short-hand" for the function calls of crypto_ahash_init, |
| 388 | * crypto_ahash_update and crypto_ahash_final. The parameters have the same |
| 389 | * meaning as discussed for those separate three functions. |
| 390 | * |
| 391 | * Return: 0 if the message digest creation was successful; < 0 if an error |
| 392 | * occurred |
| 393 | */ |
Herbert Xu | 66f6ce5 | 2009-07-15 12:40:40 +0800 | [diff] [blame] | 394 | int crypto_ahash_digest(struct ahash_request *req); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 395 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 396 | /** |
| 397 | * crypto_ahash_export() - extract current message digest state |
| 398 | * @req: reference to the ahash_request handle whose state is exported |
| 399 | * @out: output buffer of sufficient size that can hold the hash state |
| 400 | * |
| 401 | * This function exports the hash state of the ahash_request handle into the |
| 402 | * caller-allocated output buffer out which must have sufficient size (e.g. by |
| 403 | * calling crypto_ahash_reqsize). |
| 404 | * |
| 405 | * Return: 0 if the export was successful; < 0 if an error occurred |
| 406 | */ |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 407 | static inline int crypto_ahash_export(struct ahash_request *req, void *out) |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 408 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 409 | return crypto_ahash_reqtfm(req)->export(req, out); |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 410 | } |
| 411 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 412 | /** |
| 413 | * crypto_ahash_import() - import message digest state |
| 414 | * @req: reference to ahash_request handle the state is imported into |
| 415 | * @in: buffer holding the state |
| 416 | * |
| 417 | * This function imports the hash state into the ahash_request handle from the |
| 418 | * input buffer. That buffer should have been generated with the |
| 419 | * crypto_ahash_export function. |
| 420 | * |
| 421 | * Return: 0 if the import was successful; < 0 if an error occurred |
| 422 | */ |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 423 | static inline int crypto_ahash_import(struct ahash_request *req, const void *in) |
| 424 | { |
| 425 | return crypto_ahash_reqtfm(req)->import(req, in); |
| 426 | } |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 427 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 428 | /** |
| 429 | * crypto_ahash_init() - (re)initialize message digest handle |
| 430 | * @req: ahash_request handle that already is initialized with all necessary |
| 431 | * data using the ahash_request_* API functions |
| 432 | * |
| 433 | * The call (re-)initializes the message digest referenced by the ahash_request |
| 434 | * handle. Any potentially existing state created by previous operations is |
| 435 | * discarded. |
| 436 | * |
| 437 | * Return: 0 if the message digest initialization was successful; < 0 if an |
| 438 | * error occurred |
| 439 | */ |
Herbert Xu | 318e531 | 2008-08-05 13:34:30 +0800 | [diff] [blame] | 440 | static inline int crypto_ahash_init(struct ahash_request *req) |
| 441 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 442 | return crypto_ahash_reqtfm(req)->init(req); |
Herbert Xu | 318e531 | 2008-08-05 13:34:30 +0800 | [diff] [blame] | 443 | } |
| 444 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 445 | /** |
| 446 | * crypto_ahash_update() - add data to message digest for processing |
| 447 | * @req: ahash_request handle that was previously initialized with the |
| 448 | * crypto_ahash_init call. |
| 449 | * |
| 450 | * Updates the message digest state of the &ahash_request handle. The input data |
| 451 | * is pointed to by the scatter/gather list registered in the &ahash_request |
| 452 | * handle |
| 453 | * |
| 454 | * Return: 0 if the message digest update was successful; < 0 if an error |
| 455 | * occurred |
| 456 | */ |
Herbert Xu | 318e531 | 2008-08-05 13:34:30 +0800 | [diff] [blame] | 457 | static inline int crypto_ahash_update(struct ahash_request *req) |
| 458 | { |
Herbert Xu | 88056ec | 2009-07-14 12:28:26 +0800 | [diff] [blame] | 459 | return crypto_ahash_reqtfm(req)->update(req); |
Herbert Xu | 318e531 | 2008-08-05 13:34:30 +0800 | [diff] [blame] | 460 | } |
| 461 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 462 | /** |
| 463 | * DOC: Asynchronous Hash Request Handle |
| 464 | * |
| 465 | * The &ahash_request data structure contains all pointers to data |
| 466 | * required for the asynchronous cipher operation. This includes the cipher |
| 467 | * handle (which can be used by multiple &ahash_request instances), pointer |
| 468 | * to plaintext and the message digest output buffer, asynchronous callback |
| 469 | * function, etc. It acts as a handle to the ahash_request_* API calls in a |
| 470 | * similar way as ahash handle to the crypto_ahash_* API calls. |
| 471 | */ |
| 472 | |
| 473 | /** |
| 474 | * ahash_request_set_tfm() - update cipher handle reference in request |
| 475 | * @req: request handle to be modified |
| 476 | * @tfm: cipher handle that shall be added to the request handle |
| 477 | * |
| 478 | * Allow the caller to replace the existing ahash handle in the request |
| 479 | * data structure with a different one. |
| 480 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 481 | static inline void ahash_request_set_tfm(struct ahash_request *req, |
| 482 | struct crypto_ahash *tfm) |
| 483 | { |
| 484 | req->base.tfm = crypto_ahash_tfm(tfm); |
| 485 | } |
| 486 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 487 | /** |
| 488 | * ahash_request_alloc() - allocate request data structure |
| 489 | * @tfm: cipher handle to be registered with the request |
| 490 | * @gfp: memory allocation flag that is handed to kmalloc by the API call. |
| 491 | * |
| 492 | * Allocate the request data structure that must be used with the ahash |
| 493 | * message digest API calls. During |
| 494 | * the allocation, the provided ahash handle |
| 495 | * is registered in the request data structure. |
| 496 | * |
| 497 | * Return: allocated request handle in case of success; IS_ERR() is true in case |
| 498 | * of an error, PTR_ERR() returns the error code. |
| 499 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 500 | static inline struct ahash_request *ahash_request_alloc( |
| 501 | struct crypto_ahash *tfm, gfp_t gfp) |
| 502 | { |
| 503 | struct ahash_request *req; |
| 504 | |
| 505 | req = kmalloc(sizeof(struct ahash_request) + |
| 506 | crypto_ahash_reqsize(tfm), gfp); |
| 507 | |
| 508 | if (likely(req)) |
| 509 | ahash_request_set_tfm(req, tfm); |
| 510 | |
| 511 | return req; |
| 512 | } |
| 513 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 514 | /** |
| 515 | * ahash_request_free() - zeroize and free the request data structure |
| 516 | * @req: request data structure cipher handle to be freed |
| 517 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 518 | static inline void ahash_request_free(struct ahash_request *req) |
| 519 | { |
Herbert Xu | aef73cf | 2009-07-11 22:22:14 +0800 | [diff] [blame] | 520 | kzfree(req); |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static inline struct ahash_request *ahash_request_cast( |
| 524 | struct crypto_async_request *req) |
| 525 | { |
| 526 | return container_of(req, struct ahash_request, base); |
| 527 | } |
| 528 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 529 | /** |
| 530 | * ahash_request_set_callback() - set asynchronous callback function |
| 531 | * @req: request handle |
| 532 | * @flags: specify zero or an ORing of the flags |
| 533 | * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and |
| 534 | * increase the wait queue beyond the initial maximum size; |
| 535 | * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep |
| 536 | * @compl: callback function pointer to be registered with the request handle |
| 537 | * @data: The data pointer refers to memory that is not used by the kernel |
| 538 | * crypto API, but provided to the callback function for it to use. Here, |
| 539 | * the caller can provide a reference to memory the callback function can |
| 540 | * operate on. As the callback function is invoked asynchronously to the |
| 541 | * related functionality, it may need to access data structures of the |
| 542 | * related functionality which can be referenced using this pointer. The |
| 543 | * callback function can access the memory via the "data" field in the |
| 544 | * &crypto_async_request data structure provided to the callback function. |
| 545 | * |
| 546 | * This function allows setting the callback function that is triggered once |
| 547 | * the cipher operation completes. |
| 548 | * |
| 549 | * The callback function is registered with the &ahash_request handle and |
| 550 | * must comply with the following template |
| 551 | * |
| 552 | * void callback_function(struct crypto_async_request *req, int error) |
| 553 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 554 | static inline void ahash_request_set_callback(struct ahash_request *req, |
| 555 | u32 flags, |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 556 | crypto_completion_t compl, |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 557 | void *data) |
| 558 | { |
Mark Rustad | 3e3dc25 | 2014-07-25 02:53:38 -0700 | [diff] [blame] | 559 | req->base.complete = compl; |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 560 | req->base.data = data; |
| 561 | req->base.flags = flags; |
| 562 | } |
| 563 | |
Stephan Mueller | 90240ff | 2014-11-12 05:26:41 +0100 | [diff] [blame^] | 564 | /** |
| 565 | * ahash_request_set_crypt() - set data buffers |
| 566 | * @req: ahash_request handle to be updated |
| 567 | * @src: source scatter/gather list |
| 568 | * @result: buffer that is filled with the message digest -- the caller must |
| 569 | * ensure that the buffer has sufficient space by, for example, calling |
| 570 | * crypto_ahash_digestsize() |
| 571 | * @nbytes: number of bytes to process from the source scatter/gather list |
| 572 | * |
| 573 | * By using this call, the caller references the source scatter/gather list. |
| 574 | * The source scatter/gather list points to the data the message digest is to |
| 575 | * be calculated for. |
| 576 | */ |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 577 | static inline void ahash_request_set_crypt(struct ahash_request *req, |
| 578 | struct scatterlist *src, u8 *result, |
| 579 | unsigned int nbytes) |
| 580 | { |
| 581 | req->src = src; |
| 582 | req->nbytes = nbytes; |
| 583 | req->result = result; |
| 584 | } |
| 585 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 586 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
| 587 | u32 mask); |
| 588 | |
| 589 | static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm) |
| 590 | { |
| 591 | return &tfm->base; |
| 592 | } |
| 593 | |
| 594 | static inline void crypto_free_shash(struct crypto_shash *tfm) |
| 595 | { |
Herbert Xu | 412e87a | 2009-02-05 16:51:25 +1100 | [diff] [blame] | 596 | crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm)); |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static inline unsigned int crypto_shash_alignmask( |
| 600 | struct crypto_shash *tfm) |
| 601 | { |
| 602 | return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm)); |
| 603 | } |
| 604 | |
Herbert Xu | 9749598 | 2009-02-03 12:47:44 +1100 | [diff] [blame] | 605 | static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm) |
| 606 | { |
| 607 | return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm)); |
| 608 | } |
| 609 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 610 | static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg) |
| 611 | { |
| 612 | return container_of(alg, struct shash_alg, base); |
| 613 | } |
| 614 | |
| 615 | static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm) |
| 616 | { |
| 617 | return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg); |
| 618 | } |
| 619 | |
| 620 | static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm) |
| 621 | { |
| 622 | return crypto_shash_alg(tfm)->digestsize; |
| 623 | } |
| 624 | |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 625 | static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm) |
| 626 | { |
| 627 | return crypto_shash_alg(tfm)->statesize; |
| 628 | } |
| 629 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 630 | static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm) |
| 631 | { |
| 632 | return crypto_tfm_get_flags(crypto_shash_tfm(tfm)); |
| 633 | } |
| 634 | |
| 635 | static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags) |
| 636 | { |
| 637 | crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags); |
| 638 | } |
| 639 | |
| 640 | static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags) |
| 641 | { |
| 642 | crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags); |
| 643 | } |
| 644 | |
| 645 | static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm) |
| 646 | { |
Herbert Xu | 113adef | 2009-07-14 12:50:12 +0800 | [diff] [blame] | 647 | return tfm->descsize; |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static inline void *shash_desc_ctx(struct shash_desc *desc) |
| 651 | { |
| 652 | return desc->__ctx; |
| 653 | } |
| 654 | |
| 655 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, |
| 656 | unsigned int keylen); |
| 657 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, |
| 658 | unsigned int len, u8 *out); |
| 659 | |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 660 | static inline int crypto_shash_export(struct shash_desc *desc, void *out) |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 661 | { |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 662 | return crypto_shash_alg(desc->tfm)->export(desc, out); |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 663 | } |
| 664 | |
Herbert Xu | 99d27e1 | 2009-07-09 20:30:57 +0800 | [diff] [blame] | 665 | static inline int crypto_shash_import(struct shash_desc *desc, const void *in) |
| 666 | { |
| 667 | return crypto_shash_alg(desc->tfm)->import(desc, in); |
| 668 | } |
Herbert Xu | dec8b78 | 2008-11-02 21:38:11 +0800 | [diff] [blame] | 669 | |
Herbert Xu | 7b5a080b | 2008-08-31 15:47:27 +1000 | [diff] [blame] | 670 | static inline int crypto_shash_init(struct shash_desc *desc) |
| 671 | { |
| 672 | return crypto_shash_alg(desc->tfm)->init(desc); |
| 673 | } |
| 674 | |
| 675 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, |
| 676 | unsigned int len); |
| 677 | int crypto_shash_final(struct shash_desc *desc, u8 *out); |
| 678 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, |
| 679 | unsigned int len, u8 *out); |
| 680 | |
Herbert Xu | 18e33e6 | 2008-07-10 16:01:22 +0800 | [diff] [blame] | 681 | #endif /* _CRYPTO_HASH_H */ |