hash.h 33 KB

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