akcipher.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Public Key Encryption
  3. *
  4. * Copyright (c) 2015, Intel Corporation
  5. * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. */
  13. #ifndef _CRYPTO_AKCIPHER_H
  14. #define _CRYPTO_AKCIPHER_H
  15. #include <linux/crypto.h>
  16. /**
  17. * struct akcipher_request - public key request
  18. *
  19. * @base: Common attributes for async crypto requests
  20. * @src: Source data
  21. * @dst: Destination data
  22. * @src_len: Size of the input buffer
  23. * @dst_len: Size of the output buffer. It needs to be at least
  24. * as big as the expected result 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. * @__ctx: Start of private context data
  30. */
  31. struct akcipher_request {
  32. struct crypto_async_request base;
  33. struct scatterlist *src;
  34. struct scatterlist *dst;
  35. unsigned int src_len;
  36. unsigned int dst_len;
  37. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  38. };
  39. /**
  40. * struct crypto_akcipher - user-instantiated objects which encapsulate
  41. * algorithms and core processing logic
  42. *
  43. * @base: Common crypto API algorithm data structure
  44. */
  45. struct crypto_akcipher {
  46. struct crypto_tfm base;
  47. };
  48. /**
  49. * struct akcipher_alg - generic public key algorithm
  50. *
  51. * @sign: Function performs a sign operation as defined by public key
  52. * algorithm. In case of error, where the dst_len was insufficient,
  53. * the req->dst_len will be updated to the size required for the
  54. * operation
  55. * @verify: 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. * @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
  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
  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. kzfree(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
  224. * @src_len: size of the src input scatter list to be processed
  225. * @dst_len: size of the dst output scatter list
  226. */
  227. static inline void akcipher_request_set_crypt(struct akcipher_request *req,
  228. struct scatterlist *src,
  229. struct scatterlist *dst,
  230. unsigned int src_len,
  231. unsigned int dst_len)
  232. {
  233. req->src = src;
  234. req->dst = dst;
  235. req->src_len = src_len;
  236. req->dst_len = dst_len;
  237. }
  238. /**
  239. * crypto_akcipher_maxsize() - Get len for output buffer
  240. *
  241. * Function returns the dest buffer size required for a given key.
  242. * Function assumes that the key is already set in the transformation. If this
  243. * function is called without a setkey or with a failed setkey, you will end up
  244. * in a NULL dereference.
  245. *
  246. * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
  247. */
  248. static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)
  249. {
  250. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  251. return alg->max_size(tfm);
  252. }
  253. /**
  254. * crypto_akcipher_encrypt() - Invoke public key encrypt operation
  255. *
  256. * Function invokes the specific public key encrypt operation for a given
  257. * public key algorithm
  258. *
  259. * @req: asymmetric key request
  260. *
  261. * Return: zero on success; error code in case of error
  262. */
  263. static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
  264. {
  265. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  266. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  267. return alg->encrypt(req);
  268. }
  269. /**
  270. * crypto_akcipher_decrypt() - Invoke public key decrypt operation
  271. *
  272. * Function invokes the specific public key decrypt operation for a given
  273. * public key algorithm
  274. *
  275. * @req: asymmetric key request
  276. *
  277. * Return: zero on success; error code in case of error
  278. */
  279. static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
  280. {
  281. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  282. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  283. return alg->decrypt(req);
  284. }
  285. /**
  286. * crypto_akcipher_sign() - Invoke public key sign operation
  287. *
  288. * Function invokes the specific public key sign operation for a given
  289. * public key algorithm
  290. *
  291. * @req: asymmetric key request
  292. *
  293. * Return: zero on success; error code in case of error
  294. */
  295. static inline int crypto_akcipher_sign(struct akcipher_request *req)
  296. {
  297. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  298. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  299. return alg->sign(req);
  300. }
  301. /**
  302. * crypto_akcipher_verify() - Invoke public key verify operation
  303. *
  304. * Function invokes the specific public key verify operation for a given
  305. * public key algorithm
  306. *
  307. * @req: asymmetric key request
  308. *
  309. * Return: zero on success; error code in case of error
  310. */
  311. static inline int crypto_akcipher_verify(struct akcipher_request *req)
  312. {
  313. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  314. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  315. return alg->verify(req);
  316. }
  317. /**
  318. * crypto_akcipher_set_pub_key() - Invoke set public key operation
  319. *
  320. * Function invokes the algorithm specific set key function, which knows
  321. * how to decode and interpret the encoded key
  322. *
  323. * @tfm: tfm handle
  324. * @key: BER encoded public key
  325. * @keylen: length of the key
  326. *
  327. * Return: zero on success; error code in case of error
  328. */
  329. static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher *tfm,
  330. const void *key,
  331. unsigned int keylen)
  332. {
  333. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  334. return alg->set_pub_key(tfm, key, keylen);
  335. }
  336. /**
  337. * crypto_akcipher_set_priv_key() - Invoke set private key operation
  338. *
  339. * Function invokes the algorithm specific set key function, which knows
  340. * how to decode and interpret the encoded key
  341. *
  342. * @tfm: tfm handle
  343. * @key: BER encoded private key
  344. * @keylen: length of the key
  345. *
  346. * Return: zero on success; error code in case of error
  347. */
  348. static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher *tfm,
  349. const void *key,
  350. unsigned int keylen)
  351. {
  352. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  353. return alg->set_priv_key(tfm, key, keylen);
  354. }
  355. #endif