akcipher.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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: Pointer to memory containing the input parameters
  21. * The format of the parameter(s) is expeted to be Octet String
  22. * @dst: Pointer to memory whare the result will be stored
  23. * @src_len: Size of the input parameter
  24. * @dst_len: Size of the output buffer. It needs to be at leaset
  25. * as big as the expected result depending on the operation
  26. * After operation it will be updated with the acctual size of the
  27. * result. In case of error, where the dst_len 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. void *src;
  34. void *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 encrytp 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. * @setkey: Function invokes the algorithm specific set key function, which
  68. * knows how to decode and interpret the BER encoded key
  69. * @init: Initialize the cryptographic transformation object.
  70. * This function is used to initialize the cryptographic
  71. * transformation object. This function is called only once at
  72. * the instantiation time, right after the transformation context
  73. * was allocated. In case the cryptographic hardware has some
  74. * special requirements which need to be handled by software, this
  75. * function shall check for the precise requirement of the
  76. * transformation and put any software fallbacks in place.
  77. * @exit: Deinitialize the cryptographic transformation object. This is a
  78. * counterpart to @init, used to remove various changes set in
  79. * @init.
  80. *
  81. * @reqsize: Request context size required by algorithm implementation
  82. * @base: Common crypto API algorithm data structure
  83. */
  84. struct akcipher_alg {
  85. int (*sign)(struct akcipher_request *req);
  86. int (*verify)(struct akcipher_request *req);
  87. int (*encrypt)(struct akcipher_request *req);
  88. int (*decrypt)(struct akcipher_request *req);
  89. int (*setkey)(struct crypto_akcipher *tfm, const void *key,
  90. unsigned int keylen);
  91. int (*init)(struct crypto_akcipher *tfm);
  92. void (*exit)(struct crypto_akcipher *tfm);
  93. unsigned int reqsize;
  94. struct crypto_alg base;
  95. };
  96. /**
  97. * DOC: Generic Public Key API
  98. *
  99. * The Public Key API is used with the algorithms of type
  100. * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
  101. */
  102. /**
  103. * crypto_alloc_akcipher() -- allocate AKCIPHER tfm handle
  104. * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
  105. * public key algorithm e.g. "rsa"
  106. * @type: specifies the type of the algorithm
  107. * @mask: specifies the mask for the algorithm
  108. *
  109. * Allocate a handle for public key algorithm. The returned struct
  110. * crypto_akcipher is the handle that is required for any subsequent
  111. * API invocation for the public key operations.
  112. *
  113. * Return: allocated handle in case of success; IS_ERR() is true in case
  114. * of an error, PTR_ERR() returns the error code.
  115. */
  116. struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
  117. u32 mask);
  118. static inline struct crypto_tfm *crypto_akcipher_tfm(
  119. struct crypto_akcipher *tfm)
  120. {
  121. return &tfm->base;
  122. }
  123. static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
  124. {
  125. return container_of(alg, struct akcipher_alg, base);
  126. }
  127. static inline struct crypto_akcipher *__crypto_akcipher_tfm(
  128. struct crypto_tfm *tfm)
  129. {
  130. return container_of(tfm, struct crypto_akcipher, base);
  131. }
  132. static inline struct akcipher_alg *crypto_akcipher_alg(
  133. struct crypto_akcipher *tfm)
  134. {
  135. return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
  136. }
  137. static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
  138. {
  139. return crypto_akcipher_alg(tfm)->reqsize;
  140. }
  141. static inline void akcipher_request_set_tfm(struct akcipher_request *req,
  142. struct crypto_akcipher *tfm)
  143. {
  144. req->base.tfm = crypto_akcipher_tfm(tfm);
  145. }
  146. static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
  147. struct akcipher_request *req)
  148. {
  149. return __crypto_akcipher_tfm(req->base.tfm);
  150. }
  151. /**
  152. * crypto_free_akcipher() -- free AKCIPHER tfm handle
  153. *
  154. * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
  155. */
  156. static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
  157. {
  158. crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
  159. }
  160. /**
  161. * akcipher_request_alloc() -- allocates public key request
  162. *
  163. * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
  164. * @gfp: allocation flags
  165. *
  166. * Return: allocated handle in case of success or NULL in case of an error.
  167. */
  168. static inline struct akcipher_request *akcipher_request_alloc(
  169. struct crypto_akcipher *tfm, gfp_t gfp)
  170. {
  171. struct akcipher_request *req;
  172. req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
  173. if (likely(req))
  174. akcipher_request_set_tfm(req, tfm);
  175. return req;
  176. }
  177. /**
  178. * akcipher_request_free() -- zeroize and free public key request
  179. *
  180. * @req: request to free
  181. */
  182. static inline void akcipher_request_free(struct akcipher_request *req)
  183. {
  184. kzfree(req);
  185. }
  186. /**
  187. * akcipher_request_set_callback() -- Sets an asynchronous callback.
  188. *
  189. * Callback will be called when an asynchronous operation on a given
  190. * request is finished.
  191. *
  192. * @req: request that the callback will be set for
  193. * @flgs: specify for instance if the operation may backlog
  194. * @cmlp: callback which will be called
  195. * @data: private data used by the caller
  196. */
  197. static inline void akcipher_request_set_callback(struct akcipher_request *req,
  198. u32 flgs,
  199. crypto_completion_t cmpl,
  200. void *data)
  201. {
  202. req->base.complete = cmpl;
  203. req->base.data = data;
  204. req->base.flags = flgs;
  205. }
  206. /**
  207. * akcipher_request_set_crypt() -- Sets reqest parameters
  208. *
  209. * Sets parameters required by crypto operation
  210. *
  211. * @req: public key request
  212. * @src: ptr to input parameter
  213. * @dst: ptr of output parameter
  214. * @src_len: size of the input buffer
  215. * @dst_len: size of the output buffer. It will be updated by the
  216. * implementation to reflect the acctual size of the result
  217. */
  218. static inline void akcipher_request_set_crypt(struct akcipher_request *req,
  219. void *src, void *dst,
  220. unsigned int src_len,
  221. unsigned int dst_len)
  222. {
  223. req->src = src;
  224. req->dst = dst;
  225. req->src_len = src_len;
  226. req->dst_len = dst_len;
  227. }
  228. /**
  229. * crypto_akcipher_encrypt() -- Invoke public key encrypt operation
  230. *
  231. * Function invokes the specific public key encrypt operation for a given
  232. * public key algorithm
  233. *
  234. * @req: asymmetric key request
  235. *
  236. * Return: zero on success; error code in case of error
  237. */
  238. static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
  239. {
  240. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  241. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  242. return alg->encrypt(req);
  243. }
  244. /**
  245. * crypto_akcipher_decrypt() -- Invoke public key decrypt operation
  246. *
  247. * Function invokes the specific public key decrypt operation for a given
  248. * public key algorithm
  249. *
  250. * @req: asymmetric key request
  251. *
  252. * Return: zero on success; error code in case of error
  253. */
  254. static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
  255. {
  256. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  257. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  258. return alg->decrypt(req);
  259. }
  260. /**
  261. * crypto_akcipher_sign() -- Invoke public key sign operation
  262. *
  263. * Function invokes the specific public key sign operation for a given
  264. * public key algorithm
  265. *
  266. * @req: asymmetric key request
  267. *
  268. * Return: zero on success; error code in case of error
  269. */
  270. static inline int crypto_akcipher_sign(struct akcipher_request *req)
  271. {
  272. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  273. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  274. return alg->sign(req);
  275. }
  276. /**
  277. * crypto_akcipher_verify() -- Invoke public key verify operation
  278. *
  279. * Function invokes the specific public key verify 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_verify(struct akcipher_request *req)
  287. {
  288. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  289. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  290. return alg->verify(req);
  291. }
  292. /**
  293. * crypto_akcipher_setkey() -- Invoke public key setkey operation
  294. *
  295. * Function invokes the algorithm specific set key function, which knows
  296. * how to decode and interpret the encoded key
  297. *
  298. * @tfm: tfm handle
  299. * @key: BER encoded private or public key
  300. * @keylen: length of the key
  301. *
  302. * Return: zero on success; error code in case of error
  303. */
  304. static inline int crypto_akcipher_setkey(struct crypto_akcipher *tfm, void *key,
  305. unsigned int keylen)
  306. {
  307. struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
  308. return alg->setkey(tfm, key, keylen);
  309. }
  310. #endif