hash.h 31 KB

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