hash.h 29 KB

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