hash.h 29 KB

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