akcipher.h 13 KB

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