akcipher.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Public Key Encryption
  4. *
  5. * Copyright (c) 2015, Intel Corporation
  6. * Authors: Tadeusz Struk <[email protected]>
  7. */
  8. #ifndef _CRYPTO_AKCIPHER_H
  9. #define _CRYPTO_AKCIPHER_H
  10. #include <linux/crypto.h>
  11. /**
  12. * struct akcipher_request - public key request
  13. *
  14. * @base: Common attributes for async crypto requests
  15. * @src: Source data
  16. * For verify op this is signature + digest, in that case
  17. * total size of @src is @src_len + @dst_len.
  18. * @dst: Destination data (Should be NULL for verify op)
  19. * @src_len: Size of the input buffer
  20. * For verify op it's size of signature part of @src, this part
  21. * is supposed to be operated by cipher.
  22. * @dst_len: Size of @dst buffer (for all ops except verify).
  23. * It needs to be at least as big as the expected result
  24. * depending on the operation.
  25. * After operation it will be updated with the actual size of the
  26. * result.
  27. * In case of error where the dst sgl size was insufficient,
  28. * it will be updated to the size required for the operation.
  29. * For verify op this is size of digest part in @src.
  30. * @__ctx: Start of private context data
  31. */
  32. struct akcipher_request {
  33. struct crypto_async_request base;
  34. struct scatterlist *src;
  35. struct scatterlist *dst;
  36. unsigned int src_len;
  37. unsigned int dst_len;
  38. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  39. };
  40. /**
  41. * struct crypto_akcipher - user-instantiated objects which encapsulate
  42. * algorithms and core processing logic
  43. *
  44. * @base: Common crypto API algorithm data structure
  45. */
  46. struct crypto_akcipher {
  47. struct crypto_tfm base;
  48. };
  49. /**
  50. * struct akcipher_alg - generic public key algorithm
  51. *
  52. * @sign: Function performs a sign operation as defined by public key
  53. * algorithm. In case of error, where the dst_len was insufficient,
  54. * the req->dst_len will be updated to the size required for the
  55. * operation
  56. * @verify: Function performs a complete verify operation as defined by
  57. * public key algorithm, returning verification status. Requires
  58. * digest value as input parameter.
  59. * @encrypt: Function performs an encrypt operation as defined by public key
  60. * algorithm. In case of error, where the dst_len was insufficient,
  61. * the req->dst_len will be updated to the size required for the
  62. * operation
  63. * @decrypt: Function performs a decrypt operation as defined by public key
  64. * algorithm. In case of error, where the dst_len was insufficient,
  65. * the req->dst_len will be updated to the size required for the
  66. * operation
  67. * @set_pub_key: Function invokes the algorithm specific set public key
  68. * function, which knows how to decode and interpret
  69. * the BER encoded public key and parameters
  70. * @set_priv_key: Function invokes the algorithm specific set private key
  71. * function, which knows how to decode and interpret
  72. * the BER encoded private key and parameters
  73. * @max_size: Function returns dest buffer size required for a given key.
  74. * @init: Initialize the cryptographic transformation object.
  75. * This function is used to initialize the cryptographic
  76. * transformation object. This function is called only once at
  77. * the instantiation time, right after the transformation context
  78. * was allocated. In case the cryptographic hardware has some
  79. * special requirements which need to be handled by software, this
  80. * function shall check for the precise requirement of the
  81. * transformation and put any software fallbacks in place.
  82. * @exit: Deinitialize the cryptographic transformation object. This is a
  83. * counterpart to @init, used to remove various changes set in
  84. * @init.
  85. *
  86. * @reqsize: Request context size required by algorithm implementation
  87. * @base: Common crypto API algorithm data structure
  88. */
  89. struct akcipher_alg {
  90. int (*sign)(struct akcipher_request *req);
  91. int (*verify)(struct akcipher_request *req);
  92. int (*encrypt)(struct akcipher_request *req);
  93. int (*decrypt)(struct akcipher_request *req);
  94. int (*set_pub_key)(struct crypto_akcipher *tfm, const void *key,
  95. unsigned int keylen);
  96. int (*set_priv_key)(struct crypto_akcipher *tfm, const void *key,
  97. unsigned int keylen);
  98. unsigned int (*max_size)(struct crypto_akcipher *tfm);
  99. int (*init)(struct crypto_akcipher *tfm);
  100. void (*exit)(struct crypto_akcipher *tfm);
  101. unsigned int reqsize;
  102. struct crypto_alg base;
  103. };
  104. /**
  105. * DOC: Generic Public Key API
  106. *
  107. * The Public Key API is used with the algorithms of type
  108. * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
  109. */
  110. /**
  111. * crypto_alloc_akcipher() - allocate AKCIPHER tfm handle
  112. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  113. * public key algorithm e.g. "rsa"
  114. * @type: specifies the type of the algorithm
  115. * @mask: specifies the mask for the algorithm
  116. *
  117. * Allocate a handle for public key algorithm. The returned struct
  118. * crypto_akcipher is the handle that is required for any subsequent
  119. * API invocation for the public key operations.
  120. *
  121. * Return: allocated handle in case of success; IS_ERR() is true in case
  122. * of an error, PTR_ERR() returns the error code.
  123. */
  124. struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
  125. u32 mask);
  126. static inline struct crypto_tfm *crypto_akcipher_tfm(
  127. struct crypto_akcipher *tfm)
  128. {
  129. return &tfm->base;
  130. }
  131. static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
  132. {
  133. return container_of(alg, struct akcipher_alg, base);
  134. }
  135. static inline struct crypto_akcipher *__crypto_akcipher_tfm(
  136. struct crypto_tfm *tfm)
  137. {
  138. return container_of(tfm, struct crypto_akcipher, base);
  139. }
  140. static inline struct akcipher_alg *crypto_akcipher_alg(
  141. struct crypto_akcipher *tfm)
  142. {
  143. return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
  144. }
  145. static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
  146. {
  147. return crypto_akcipher_alg(tfm)->reqsize;
  148. }
  149. static inline void akcipher_request_set_tfm(struct akcipher_request *req,
  150. struct crypto_akcipher *tfm)
  151. {
  152. req->base.tfm = crypto_akcipher_tfm(tfm);
  153. }
  154. static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
  155. struct akcipher_request *req)
  156. {
  157. return __crypto_akcipher_tfm(req->base.tfm);
  158. }
  159. /**
  160. * crypto_free_akcipher() - free AKCIPHER tfm handle
  161. *
  162. * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
  163. *
  164. * If @tfm is a NULL or error pointer, this function does nothing.
  165. */
  166. static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
  167. {
  168. crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
  169. }
  170. /**
  171. * akcipher_request_alloc() - allocates public key request
  172. *
  173. * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
  174. * @gfp: allocation flags
  175. *
  176. * Return: allocated handle in case of success or NULL in case of an error.
  177. */
  178. static inline struct akcipher_request *akcipher_request_alloc(
  179. struct crypto_akcipher *tfm, gfp_t gfp)
  180. {
  181. struct akcipher_request *req;
  182. req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
  183. if (likely(req))
  184. akcipher_request_set_tfm(req, tfm);
  185. return req;
  186. }
  187. /**
  188. * akcipher_request_free() - zeroize and free public key request
  189. *
  190. * @req: request to free
  191. */
  192. static inline void akcipher_request_free(struct akcipher_request *req)
  193. {
  194. kfree_sensitive(req);
  195. }
  196. /**
  197. * akcipher_request_set_callback() - Sets an asynchronous callback.
  198. *
  199. * Callback will be called when an asynchronous operation on a given
  200. * request is finished.
  201. *
  202. * @req: request that the callback will be set for
  203. * @flgs: specify for instance if the operation may backlog
  204. * @cmpl: callback which will be called
  205. * @data: private data used by the caller
  206. */
  207. static inline void akcipher_request_set_callback(struct akcipher_request *req,
  208. u32 flgs,
  209. crypto_completion_t cmpl,
  210. void *data)
  211. {
  212. req->base.complete = cmpl;
  213. req->base.data = data;
  214. req->base.flags = flgs;
  215. }
  216. /**
  217. * akcipher_request_set_crypt() - Sets request parameters
  218. *
  219. * Sets parameters required by crypto operation
  220. *
  221. * @req: public key request
  222. * @src: ptr to input scatter list
  223. * @dst: ptr to output scatter list or NULL for verify op
  224. * @src_len: size of the src input scatter list to be processed
  225. * @dst_len: size of the dst output scatter list or size of signature
  226. * portion in @src for verify op
  227. */
  228. static inline void akcipher_request_set_crypt(struct akcipher_request *req,
  229. struct scatterlist *src,
  230. struct scatterlist *dst,
  231. unsigned int src_len,
  232. unsigned int dst_len)
  233. {
  234. req->src = src;
  235. req->dst = dst;
  236. req->src_len = src_len;
  237. req->dst_len = dst_len;
  238. }
  239. /**
  240. * crypto_akcipher_maxsize() - Get len for output buffer
  241. *
  242. * Function returns the dest buffer size required for a given key.
  243. * Function assumes that the key is already set in the transformation. If this
  244. * function is called without a setkey or with a failed setkey, you will end up
  245. * in a NULL dereference.
  246. *
  247. * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
  248. */
  249. static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
  250. {
  251. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  252. return alg->max_size(tfm);
  253. }
  254. /**
  255. * crypto_akcipher_encrypt() - Invoke public key encrypt operation
  256. *
  257. * Function invokes the specific public key encrypt operation for a given
  258. * public key algorithm
  259. *
  260. * @req: asymmetric key request
  261. *
  262. * Return: zero on success; error code in case of error
  263. */
  264. static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
  265. {
  266. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  267. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  268. struct crypto_alg *calg = tfm->base.__crt_alg;
  269. unsigned int src_len = req->src_len;
  270. int ret;
  271. crypto_stats_get(calg);
  272. ret = alg->encrypt(req);
  273. crypto_stats_akcipher_encrypt(src_len, ret, calg);
  274. return ret;
  275. }
  276. /**
  277. * crypto_akcipher_decrypt() - Invoke public key decrypt operation
  278. *
  279. * Function invokes the specific public key decrypt operation for a given
  280. * public key algorithm
  281. *
  282. * @req: asymmetric key request
  283. *
  284. * Return: zero on success; error code in case of error
  285. */
  286. static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
  287. {
  288. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  289. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  290. struct crypto_alg *calg = tfm->base.__crt_alg;
  291. unsigned int src_len = req->src_len;
  292. int ret;
  293. crypto_stats_get(calg);
  294. ret = alg->decrypt(req);
  295. crypto_stats_akcipher_decrypt(src_len, ret, calg);
  296. return ret;
  297. }
  298. /**
  299. * crypto_akcipher_sign() - Invoke public key sign operation
  300. *
  301. * Function invokes the specific public key sign operation for a given
  302. * public key algorithm
  303. *
  304. * @req: asymmetric key request
  305. *
  306. * Return: zero on success; error code in case of error
  307. */
  308. static inline int crypto_akcipher_sign(struct akcipher_request *req)
  309. {
  310. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  311. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  312. struct crypto_alg *calg = tfm->base.__crt_alg;
  313. int ret;
  314. crypto_stats_get(calg);
  315. ret = alg->sign(req);
  316. crypto_stats_akcipher_sign(ret, calg);
  317. return ret;
  318. }
  319. /**
  320. * crypto_akcipher_verify() - Invoke public key signature verification
  321. *
  322. * Function invokes the specific public key signature verification operation
  323. * for a given public key algorithm.
  324. *
  325. * @req: asymmetric key request
  326. *
  327. * Note: req->dst should be NULL, req->src should point to SG of size
  328. * (req->src_size + req->dst_size), containing signature (of req->src_size
  329. * length) with appended digest (of req->dst_size length).
  330. *
  331. * Return: zero on verification success; error code in case of error.
  332. */
  333. static inline int crypto_akcipher_verify(struct akcipher_request *req)
  334. {
  335. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  336. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  337. struct crypto_alg *calg = tfm->base.__crt_alg;
  338. int ret;
  339. crypto_stats_get(calg);
  340. ret = alg->verify(req);
  341. crypto_stats_akcipher_verify(ret, calg);
  342. return ret;
  343. }
  344. /**
  345. * crypto_akcipher_set_pub_key() - Invoke set public key operation
  346. *
  347. * Function invokes the algorithm specific set key function, which knows
  348. * how to decode and interpret the encoded key and parameters
  349. *
  350. * @tfm: tfm handle
  351. * @key: BER encoded public key, algo OID, paramlen, BER encoded
  352. * parameters
  353. * @keylen: length of the key (not including other data)
  354. *
  355. * Return: zero on success; error code in case of error
  356. */
  357. static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,
  358. const void *key,
  359. unsigned int keylen)
  360. {
  361. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  362. return alg->set_pub_key(tfm, key, keylen);
  363. }
  364. /**
  365. * crypto_akcipher_set_priv_key() - Invoke set private key operation
  366. *
  367. * Function invokes the algorithm specific set key function, which knows
  368. * how to decode and interpret the encoded key and parameters
  369. *
  370. * @tfm: tfm handle
  371. * @key: BER encoded private key, algo OID, paramlen, BER encoded
  372. * parameters
  373. * @keylen: length of the key (not including other data)
  374. *
  375. * Return: zero on success; error code in case of error
  376. */
  377. static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
  378. const void *key,
  379. unsigned int keylen)
  380. {
  381. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  382. return alg->set_priv_key(tfm, key, keylen);
  383. }
  384. #endif