hash.h 32 KB

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