hash.h 29 KB

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