hash.h 31 KB

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