hash.h 33 KB

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