hash.h 34 KB

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