hash.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. bool has_setkey;
  186. struct crypto_tfm base;
  187. };
  188. struct crypto_shash {
  189. unsigned int descsize;
  190. struct crypto_tfm base;
  191. };
  192. /**
  193. * DOC: Asynchronous Message Digest API
  194. *
  195. * The asynchronous message digest API is used with the ciphers of type
  196. * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto)
  197. *
  198. * The asynchronous cipher operation discussion provided for the
  199. * CRYPTO_ALG_TYPE_ABLKCIPHER API applies here as well.
  200. */
  201. static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
  202. {
  203. return container_of(tfm, struct crypto_ahash, base);
  204. }
  205. /**
  206. * crypto_alloc_ahash() - allocate ahash cipher handle
  207. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  208. * ahash cipher
  209. * @type: specifies the type of the cipher
  210. * @mask: specifies the mask for the cipher
  211. *
  212. * Allocate a cipher handle for an ahash. The returned struct
  213. * crypto_ahash is the cipher handle that is required for any subsequent
  214. * API invocation for that ahash.
  215. *
  216. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  217. * of an error, PTR_ERR() returns the error code.
  218. */
  219. struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
  220. u32 mask);
  221. static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
  222. {
  223. return &tfm->base;
  224. }
  225. /**
  226. * crypto_free_ahash() - zeroize and free the ahash handle
  227. * @tfm: cipher handle to be freed
  228. */
  229. static inline void crypto_free_ahash(struct crypto_ahash *tfm)
  230. {
  231. crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
  232. }
  233. static inline unsigned int crypto_ahash_alignmask(
  234. struct crypto_ahash *tfm)
  235. {
  236. return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
  237. }
  238. /**
  239. * crypto_ahash_blocksize() - obtain block size for cipher
  240. * @tfm: cipher handle
  241. *
  242. * The block size for the message digest cipher referenced with the cipher
  243. * handle is returned.
  244. *
  245. * Return: block size of cipher
  246. */
  247. static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm)
  248. {
  249. return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  250. }
  251. static inline struct hash_alg_common *__crypto_hash_alg_common(
  252. struct crypto_alg *alg)
  253. {
  254. return container_of(alg, struct hash_alg_common, base);
  255. }
  256. static inline struct hash_alg_common *crypto_hash_alg_common(
  257. struct crypto_ahash *tfm)
  258. {
  259. return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
  260. }
  261. /**
  262. * crypto_ahash_digestsize() - obtain message digest size
  263. * @tfm: cipher handle
  264. *
  265. * The size for the message digest created by the message digest cipher
  266. * referenced with the cipher handle is returned.
  267. *
  268. *
  269. * Return: message digest size of cipher
  270. */
  271. static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
  272. {
  273. return crypto_hash_alg_common(tfm)->digestsize;
  274. }
  275. static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
  276. {
  277. return crypto_hash_alg_common(tfm)->statesize;
  278. }
  279. static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
  280. {
  281. return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
  282. }
  283. static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
  284. {
  285. crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
  286. }
  287. static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
  288. {
  289. crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
  290. }
  291. /**
  292. * crypto_ahash_reqtfm() - obtain cipher handle from request
  293. * @req: asynchronous request handle that contains the reference to the ahash
  294. * cipher handle
  295. *
  296. * Return the ahash cipher handle that is registered with the asynchronous
  297. * request handle ahash_request.
  298. *
  299. * Return: ahash cipher handle
  300. */
  301. static inline struct crypto_ahash *crypto_ahash_reqtfm(
  302. struct ahash_request *req)
  303. {
  304. return __crypto_ahash_cast(req->base.tfm);
  305. }
  306. /**
  307. * crypto_ahash_reqsize() - obtain size of the request data structure
  308. * @tfm: cipher handle
  309. *
  310. * Return the size of the ahash state size. With the crypto_ahash_export
  311. * function, the caller can export the state into a buffer whose size is
  312. * defined with this function.
  313. *
  314. * Return: size of the ahash state
  315. */
  316. static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
  317. {
  318. return tfm->reqsize;
  319. }
  320. static inline void *ahash_request_ctx(struct ahash_request *req)
  321. {
  322. return req->__ctx;
  323. }
  324. /**
  325. * crypto_ahash_setkey - set key for cipher handle
  326. * @tfm: cipher handle
  327. * @key: buffer holding the key
  328. * @keylen: length of the key in bytes
  329. *
  330. * The caller provided key is set for the ahash cipher. The cipher
  331. * handle must point to a keyed hash in order for this function to succeed.
  332. *
  333. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  334. */
  335. int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
  336. unsigned int keylen);
  337. static inline bool crypto_ahash_has_setkey(struct crypto_ahash *tfm)
  338. {
  339. return tfm->has_setkey;
  340. }
  341. /**
  342. * crypto_ahash_finup() - update and finalize message digest
  343. * @req: reference to the ahash_request handle that holds all information
  344. * needed to perform the cipher operation
  345. *
  346. * This function is a "short-hand" for the function calls of
  347. * crypto_ahash_update and crypto_shash_final. The parameters have the same
  348. * meaning as discussed for those separate functions.
  349. *
  350. * Return: 0 if the message digest creation was successful; < 0 if an error
  351. * occurred
  352. */
  353. int crypto_ahash_finup(struct ahash_request *req);
  354. /**
  355. * crypto_ahash_final() - calculate message digest
  356. * @req: reference to the ahash_request handle that holds all information
  357. * needed to perform the cipher operation
  358. *
  359. * Finalize the message digest operation and create the message digest
  360. * based on all data added to the cipher handle. The message digest is placed
  361. * into the output buffer registered with the ahash_request handle.
  362. *
  363. * Return: 0 if the message digest creation was successful; < 0 if an error
  364. * occurred
  365. */
  366. int crypto_ahash_final(struct ahash_request *req);
  367. /**
  368. * crypto_ahash_digest() - calculate message digest for a buffer
  369. * @req: reference to the ahash_request handle that holds all information
  370. * needed to perform the cipher operation
  371. *
  372. * This function is a "short-hand" for the function calls of crypto_ahash_init,
  373. * crypto_ahash_update and crypto_ahash_final. The parameters have the same
  374. * meaning as discussed for those separate three functions.
  375. *
  376. * Return: 0 if the message digest creation was successful; < 0 if an error
  377. * occurred
  378. */
  379. int crypto_ahash_digest(struct ahash_request *req);
  380. /**
  381. * crypto_ahash_export() - extract current message digest state
  382. * @req: reference to the ahash_request handle whose state is exported
  383. * @out: output buffer of sufficient size that can hold the hash state
  384. *
  385. * This function exports the hash state of the ahash_request handle into the
  386. * caller-allocated output buffer out which must have sufficient size (e.g. by
  387. * calling crypto_ahash_reqsize).
  388. *
  389. * Return: 0 if the export was successful; < 0 if an error occurred
  390. */
  391. static inline int crypto_ahash_export(struct ahash_request *req, void *out)
  392. {
  393. return crypto_ahash_reqtfm(req)->export(req, out);
  394. }
  395. /**
  396. * crypto_ahash_import() - import message digest state
  397. * @req: reference to ahash_request handle the state is imported into
  398. * @in: buffer holding the state
  399. *
  400. * This function imports the hash state into the ahash_request handle from the
  401. * input buffer. That buffer should have been generated with the
  402. * crypto_ahash_export function.
  403. *
  404. * Return: 0 if the import was successful; < 0 if an error occurred
  405. */
  406. static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
  407. {
  408. return crypto_ahash_reqtfm(req)->import(req, in);
  409. }
  410. /**
  411. * crypto_ahash_init() - (re)initialize message digest handle
  412. * @req: ahash_request handle that already is initialized with all necessary
  413. * data using the ahash_request_* API functions
  414. *
  415. * The call (re-)initializes the message digest referenced by the ahash_request
  416. * handle. Any potentially existing state created by previous operations is
  417. * discarded.
  418. *
  419. * Return: 0 if the message digest initialization was successful; < 0 if an
  420. * error occurred
  421. */
  422. static inline int crypto_ahash_init(struct ahash_request *req)
  423. {
  424. return crypto_ahash_reqtfm(req)->init(req);
  425. }
  426. /**
  427. * crypto_ahash_update() - add data to message digest for processing
  428. * @req: ahash_request handle that was previously initialized with the
  429. * crypto_ahash_init call.
  430. *
  431. * Updates the message digest state of the &ahash_request handle. The input data
  432. * is pointed to by the scatter/gather list registered in the &ahash_request
  433. * handle
  434. *
  435. * Return: 0 if the message digest update was successful; < 0 if an error
  436. * occurred
  437. */
  438. static inline int crypto_ahash_update(struct ahash_request *req)
  439. {
  440. return crypto_ahash_reqtfm(req)->update(req);
  441. }
  442. /**
  443. * DOC: Asynchronous Hash Request Handle
  444. *
  445. * The &ahash_request data structure contains all pointers to data
  446. * required for the asynchronous cipher operation. This includes the cipher
  447. * handle (which can be used by multiple &ahash_request instances), pointer
  448. * to plaintext and the message digest output buffer, asynchronous callback
  449. * function, etc. It acts as a handle to the ahash_request_* API calls in a
  450. * similar way as ahash handle to the crypto_ahash_* API calls.
  451. */
  452. /**
  453. * ahash_request_set_tfm() - update cipher handle reference in request
  454. * @req: request handle to be modified
  455. * @tfm: cipher handle that shall be added to the request handle
  456. *
  457. * Allow the caller to replace the existing ahash handle in the request
  458. * data structure with a different one.
  459. */
  460. static inline void ahash_request_set_tfm(struct ahash_request *req,
  461. struct crypto_ahash *tfm)
  462. {
  463. req->base.tfm = crypto_ahash_tfm(tfm);
  464. }
  465. /**
  466. * ahash_request_alloc() - allocate request data structure
  467. * @tfm: cipher handle to be registered with the request
  468. * @gfp: memory allocation flag that is handed to kmalloc by the API call.
  469. *
  470. * Allocate the request data structure that must be used with the ahash
  471. * message digest API calls. During
  472. * the allocation, the provided ahash handle
  473. * is registered in the request data structure.
  474. *
  475. * Return: allocated request handle in case of success; IS_ERR() is true in case
  476. * of an error, PTR_ERR() returns the error code.
  477. */
  478. static inline struct ahash_request *ahash_request_alloc(
  479. struct crypto_ahash *tfm, gfp_t gfp)
  480. {
  481. struct ahash_request *req;
  482. req = kmalloc(sizeof(struct ahash_request) +
  483. crypto_ahash_reqsize(tfm), gfp);
  484. if (likely(req))
  485. ahash_request_set_tfm(req, tfm);
  486. return req;
  487. }
  488. /**
  489. * ahash_request_free() - zeroize and free the request data structure
  490. * @req: request data structure cipher handle to be freed
  491. */
  492. static inline void ahash_request_free(struct ahash_request *req)
  493. {
  494. kzfree(req);
  495. }
  496. static inline struct ahash_request *ahash_request_cast(
  497. struct crypto_async_request *req)
  498. {
  499. return container_of(req, struct ahash_request, base);
  500. }
  501. /**
  502. * ahash_request_set_callback() - set asynchronous callback function
  503. * @req: request handle
  504. * @flags: specify zero or an ORing of the flags
  505. * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and
  506. * increase the wait queue beyond the initial maximum size;
  507. * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep
  508. * @compl: callback function pointer to be registered with the request handle
  509. * @data: The data pointer refers to memory that is not used by the kernel
  510. * crypto API, but provided to the callback function for it to use. Here,
  511. * the caller can provide a reference to memory the callback function can
  512. * operate on. As the callback function is invoked asynchronously to the
  513. * related functionality, it may need to access data structures of the
  514. * related functionality which can be referenced using this pointer. The
  515. * callback function can access the memory via the "data" field in the
  516. * &crypto_async_request data structure provided to the callback function.
  517. *
  518. * This function allows setting the callback function that is triggered once
  519. * the cipher operation completes.
  520. *
  521. * The callback function is registered with the &ahash_request handle and
  522. * must comply with the following template
  523. *
  524. * void callback_function(struct crypto_async_request *req, int error)
  525. */
  526. static inline void ahash_request_set_callback(struct ahash_request *req,
  527. u32 flags,
  528. crypto_completion_t compl,
  529. void *data)
  530. {
  531. req->base.complete = compl;
  532. req->base.data = data;
  533. req->base.flags = flags;
  534. }
  535. /**
  536. * ahash_request_set_crypt() - set data buffers
  537. * @req: ahash_request handle to be updated
  538. * @src: source scatter/gather list
  539. * @result: buffer that is filled with the message digest -- the caller must
  540. * ensure that the buffer has sufficient space by, for example, calling
  541. * crypto_ahash_digestsize()
  542. * @nbytes: number of bytes to process from the source scatter/gather list
  543. *
  544. * By using this call, the caller references the source scatter/gather list.
  545. * The source scatter/gather list points to the data the message digest is to
  546. * be calculated for.
  547. */
  548. static inline void ahash_request_set_crypt(struct ahash_request *req,
  549. struct scatterlist *src, u8 *result,
  550. unsigned int nbytes)
  551. {
  552. req->src = src;
  553. req->nbytes = nbytes;
  554. req->result = result;
  555. }
  556. /**
  557. * DOC: Synchronous Message Digest API
  558. *
  559. * The synchronous message digest API is used with the ciphers of type
  560. * CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto)
  561. *
  562. * The message digest API is able to maintain state information for the
  563. * caller.
  564. *
  565. * The synchronous message digest API can store user-related context in in its
  566. * shash_desc request data structure.
  567. */
  568. /**
  569. * crypto_alloc_shash() - allocate message digest handle
  570. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  571. * message digest cipher
  572. * @type: specifies the type of the cipher
  573. * @mask: specifies the mask for the cipher
  574. *
  575. * Allocate a cipher handle for a message digest. The returned &struct
  576. * crypto_shash is the cipher handle that is required for any subsequent
  577. * API invocation for that message digest.
  578. *
  579. * Return: allocated cipher handle in case of success; IS_ERR() is true in case
  580. * of an error, PTR_ERR() returns the error code.
  581. */
  582. struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
  583. u32 mask);
  584. static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
  585. {
  586. return &tfm->base;
  587. }
  588. /**
  589. * crypto_free_shash() - zeroize and free the message digest handle
  590. * @tfm: cipher handle to be freed
  591. */
  592. static inline void crypto_free_shash(struct crypto_shash *tfm)
  593. {
  594. crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
  595. }
  596. static inline unsigned int crypto_shash_alignmask(
  597. struct crypto_shash *tfm)
  598. {
  599. return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
  600. }
  601. /**
  602. * crypto_shash_blocksize() - obtain block size for cipher
  603. * @tfm: cipher handle
  604. *
  605. * The block size for the message digest cipher referenced with the cipher
  606. * handle is returned.
  607. *
  608. * Return: block size of cipher
  609. */
  610. static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
  611. {
  612. return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
  613. }
  614. static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
  615. {
  616. return container_of(alg, struct shash_alg, base);
  617. }
  618. static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
  619. {
  620. return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
  621. }
  622. /**
  623. * crypto_shash_digestsize() - obtain message digest size
  624. * @tfm: cipher handle
  625. *
  626. * The size for the message digest created by the message digest cipher
  627. * referenced with the cipher handle is returned.
  628. *
  629. * Return: digest size of cipher
  630. */
  631. static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
  632. {
  633. return crypto_shash_alg(tfm)->digestsize;
  634. }
  635. static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
  636. {
  637. return crypto_shash_alg(tfm)->statesize;
  638. }
  639. static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
  640. {
  641. return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
  642. }
  643. static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
  644. {
  645. crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
  646. }
  647. static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
  648. {
  649. crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
  650. }
  651. /**
  652. * crypto_shash_descsize() - obtain the operational state size
  653. * @tfm: cipher handle
  654. *
  655. * The size of the operational state the cipher needs during operation is
  656. * returned for the hash referenced with the cipher handle. This size is
  657. * required to calculate the memory requirements to allow the caller allocating
  658. * sufficient memory for operational state.
  659. *
  660. * The operational state is defined with struct shash_desc where the size of
  661. * that data structure is to be calculated as
  662. * sizeof(struct shash_desc) + crypto_shash_descsize(alg)
  663. *
  664. * Return: size of the operational state
  665. */
  666. static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
  667. {
  668. return tfm->descsize;
  669. }
  670. static inline void *shash_desc_ctx(struct shash_desc *desc)
  671. {
  672. return desc->__ctx;
  673. }
  674. /**
  675. * crypto_shash_setkey() - set key for message digest
  676. * @tfm: cipher handle
  677. * @key: buffer holding the key
  678. * @keylen: length of the key in bytes
  679. *
  680. * The caller provided key is set for the keyed message digest cipher. The
  681. * cipher handle must point to a keyed message digest cipher in order for this
  682. * function to succeed.
  683. *
  684. * Return: 0 if the setting of the key was successful; < 0 if an error occurred
  685. */
  686. int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
  687. unsigned int keylen);
  688. /**
  689. * crypto_shash_digest() - calculate message digest for buffer
  690. * @desc: see crypto_shash_final()
  691. * @data: see crypto_shash_update()
  692. * @len: see crypto_shash_update()
  693. * @out: see crypto_shash_final()
  694. *
  695. * This function is a "short-hand" for the function calls of crypto_shash_init,
  696. * crypto_shash_update and crypto_shash_final. The parameters have the same
  697. * meaning as discussed for those separate three functions.
  698. *
  699. * Return: 0 if the message digest creation was successful; < 0 if an error
  700. * occurred
  701. */
  702. int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
  703. unsigned int len, u8 *out);
  704. /**
  705. * crypto_shash_export() - extract operational state for message digest
  706. * @desc: reference to the operational state handle whose state is exported
  707. * @out: output buffer of sufficient size that can hold the hash state
  708. *
  709. * This function exports the hash state of the operational state handle into the
  710. * caller-allocated output buffer out which must have sufficient size (e.g. by
  711. * calling crypto_shash_descsize).
  712. *
  713. * Return: 0 if the export creation was successful; < 0 if an error occurred
  714. */
  715. static inline int crypto_shash_export(struct shash_desc *desc, void *out)
  716. {
  717. return crypto_shash_alg(desc->tfm)->export(desc, out);
  718. }
  719. /**
  720. * crypto_shash_import() - import operational state
  721. * @desc: reference to the operational state handle the state imported into
  722. * @in: buffer holding the state
  723. *
  724. * This function imports the hash state into the operational state handle from
  725. * the input buffer. That buffer should have been generated with the
  726. * crypto_ahash_export function.
  727. *
  728. * Return: 0 if the import was successful; < 0 if an error occurred
  729. */
  730. static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
  731. {
  732. return crypto_shash_alg(desc->tfm)->import(desc, in);
  733. }
  734. /**
  735. * crypto_shash_init() - (re)initialize message digest
  736. * @desc: operational state handle that is already filled
  737. *
  738. * The call (re-)initializes the message digest referenced by the
  739. * operational state handle. Any potentially existing state created by
  740. * previous operations is discarded.
  741. *
  742. * Return: 0 if the message digest initialization was successful; < 0 if an
  743. * error occurred
  744. */
  745. static inline int crypto_shash_init(struct shash_desc *desc)
  746. {
  747. return crypto_shash_alg(desc->tfm)->init(desc);
  748. }
  749. /**
  750. * crypto_shash_update() - add data to message digest for processing
  751. * @desc: operational state handle that is already initialized
  752. * @data: input data to be added to the message digest
  753. * @len: length of the input data
  754. *
  755. * Updates the message digest state of the operational state handle.
  756. *
  757. * Return: 0 if the message digest update was successful; < 0 if an error
  758. * occurred
  759. */
  760. int crypto_shash_update(struct shash_desc *desc, const u8 *data,
  761. unsigned int len);
  762. /**
  763. * crypto_shash_final() - calculate message digest
  764. * @desc: operational state handle that is already filled with data
  765. * @out: output buffer filled with the message digest
  766. *
  767. * Finalize the message digest operation and create the message digest
  768. * based on all data added to the cipher handle. The message digest is placed
  769. * into the output buffer. The caller must ensure that the output buffer is
  770. * large enough by using crypto_shash_digestsize.
  771. *
  772. * Return: 0 if the message digest creation was successful; < 0 if an error
  773. * occurred
  774. */
  775. int crypto_shash_final(struct shash_desc *desc, u8 *out);
  776. /**
  777. * crypto_shash_finup() - calculate message digest of buffer
  778. * @desc: see crypto_shash_final()
  779. * @data: see crypto_shash_update()
  780. * @len: see crypto_shash_update()
  781. * @out: see crypto_shash_final()
  782. *
  783. * This function is a "short-hand" for the function calls of
  784. * crypto_shash_update and crypto_shash_final. The parameters have the same
  785. * meaning as discussed for those separate functions.
  786. *
  787. * Return: 0 if the message digest creation was successful; < 0 if an error
  788. * occurred
  789. */
  790. int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
  791. unsigned int len, u8 *out);
  792. #endif /* _CRYPTO_HASH_H */