kpp.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Key-agreement Protocol Primitives (KPP)
  4. *
  5. * Copyright (c) 2016, Intel Corporation
  6. * Authors: Salvatore Benedetto <[email protected]>
  7. */
  8. #ifndef _CRYPTO_KPP_
  9. #define _CRYPTO_KPP_
  10. #include <linux/crypto.h>
  11. /**
  12. * struct kpp_request
  13. *
  14. * @base: Common attributes for async crypto requests
  15. * @src: Source data
  16. * @dst: Destination data
  17. * @src_len: Size of the input buffer
  18. * @dst_len: Size of the output buffer. It needs to be at least
  19. * as big as the expected result depending on the operation
  20. * After operation it will be updated with the actual size of the
  21. * result. In case of error where the dst sgl size was insufficient,
  22. * it will be updated to the size required for the operation.
  23. * @__ctx: Start of private context data
  24. */
  25. struct kpp_request {
  26. struct crypto_async_request base;
  27. struct scatterlist *src;
  28. struct scatterlist *dst;
  29. unsigned int src_len;
  30. unsigned int dst_len;
  31. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  32. };
  33. /**
  34. * struct crypto_kpp - user-instantiated object which encapsulate
  35. * algorithms and core processing logic
  36. *
  37. * @base: Common crypto API algorithm data structure
  38. */
  39. struct crypto_kpp {
  40. struct crypto_tfm base;
  41. };
  42. /**
  43. * struct kpp_alg - generic key-agreement protocol primitives
  44. *
  45. * @set_secret: Function invokes the protocol specific function to
  46. * store the secret private key along with parameters.
  47. * The implementation knows how to decode the buffer
  48. * @generate_public_key: Function generate the public key to be sent to the
  49. * counterpart. In case of error, where output is not big
  50. * enough req->dst_len will be updated to the size
  51. * required
  52. * @compute_shared_secret: Function compute the shared secret as defined by
  53. * the algorithm. The result is given back to the user.
  54. * In case of error, where output is not big enough,
  55. * req->dst_len will be updated to the size required
  56. * @max_size: Function returns the size of the output buffer
  57. * @init: Initialize the object. This is called only once at
  58. * instantiation time. In case the cryptographic hardware
  59. * needs to be initialized. Software fallback should be
  60. * put in place here.
  61. * @exit: Undo everything @init did.
  62. *
  63. * @reqsize: Request context size required by algorithm
  64. * implementation
  65. * @base: Common crypto API algorithm data structure
  66. */
  67. struct kpp_alg {
  68. int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
  69. unsigned int len);
  70. int (*generate_public_key)(struct kpp_request *req);
  71. int (*compute_shared_secret)(struct kpp_request *req);
  72. unsigned int (*max_size)(struct crypto_kpp *tfm);
  73. int (*init)(struct crypto_kpp *tfm);
  74. void (*exit)(struct crypto_kpp *tfm);
  75. unsigned int reqsize;
  76. struct crypto_alg base;
  77. };
  78. /**
  79. * DOC: Generic Key-agreement Protocol Primitives API
  80. *
  81. * The KPP API is used with the algorithm type
  82. * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
  83. */
  84. /**
  85. * crypto_alloc_kpp() - allocate KPP tfm handle
  86. * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
  87. * @type: specifies the type of the algorithm
  88. * @mask: specifies the mask for the algorithm
  89. *
  90. * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
  91. * is required for any following API invocation
  92. *
  93. * Return: allocated handle in case of success; IS_ERR() is true in case of
  94. * an error, PTR_ERR() returns the error code.
  95. */
  96. struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
  97. int crypto_has_kpp(const char *alg_name, u32 type, u32 mask);
  98. static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
  99. {
  100. return &tfm->base;
  101. }
  102. static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
  103. {
  104. return container_of(alg, struct kpp_alg, base);
  105. }
  106. static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
  107. {
  108. return container_of(tfm, struct crypto_kpp, base);
  109. }
  110. static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
  111. {
  112. return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
  113. }
  114. static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
  115. {
  116. return crypto_kpp_alg(tfm)->reqsize;
  117. }
  118. static inline void kpp_request_set_tfm(struct kpp_request *req,
  119. struct crypto_kpp *tfm)
  120. {
  121. req->base.tfm = crypto_kpp_tfm(tfm);
  122. }
  123. static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
  124. {
  125. return __crypto_kpp_tfm(req->base.tfm);
  126. }
  127. static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
  128. {
  129. return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
  130. }
  131. static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
  132. {
  133. crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
  134. }
  135. /**
  136. * crypto_free_kpp() - free KPP tfm handle
  137. *
  138. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
  139. *
  140. * If @tfm is a NULL or error pointer, this function does nothing.
  141. */
  142. static inline void crypto_free_kpp(struct crypto_kpp *tfm)
  143. {
  144. crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
  145. }
  146. /**
  147. * kpp_request_alloc() - allocates kpp request
  148. *
  149. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
  150. * @gfp: allocation flags
  151. *
  152. * Return: allocated handle in case of success or NULL in case of an error.
  153. */
  154. static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
  155. gfp_t gfp)
  156. {
  157. struct kpp_request *req;
  158. req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
  159. if (likely(req))
  160. kpp_request_set_tfm(req, tfm);
  161. return req;
  162. }
  163. /**
  164. * kpp_request_free() - zeroize and free kpp request
  165. *
  166. * @req: request to free
  167. */
  168. static inline void kpp_request_free(struct kpp_request *req)
  169. {
  170. kfree_sensitive(req);
  171. }
  172. /**
  173. * kpp_request_set_callback() - Sets an asynchronous callback.
  174. *
  175. * Callback will be called when an asynchronous operation on a given
  176. * request is finished.
  177. *
  178. * @req: request that the callback will be set for
  179. * @flgs: specify for instance if the operation may backlog
  180. * @cmpl: callback which will be called
  181. * @data: private data used by the caller
  182. */
  183. static inline void kpp_request_set_callback(struct kpp_request *req,
  184. u32 flgs,
  185. crypto_completion_t cmpl,
  186. void *data)
  187. {
  188. req->base.complete = cmpl;
  189. req->base.data = data;
  190. req->base.flags = flgs;
  191. }
  192. /**
  193. * kpp_request_set_input() - Sets input buffer
  194. *
  195. * Sets parameters required by generate_public_key
  196. *
  197. * @req: kpp request
  198. * @input: ptr to input scatter list
  199. * @input_len: size of the input scatter list
  200. */
  201. static inline void kpp_request_set_input(struct kpp_request *req,
  202. struct scatterlist *input,
  203. unsigned int input_len)
  204. {
  205. req->src = input;
  206. req->src_len = input_len;
  207. }
  208. /**
  209. * kpp_request_set_output() - Sets output buffer
  210. *
  211. * Sets parameters required by kpp operation
  212. *
  213. * @req: kpp request
  214. * @output: ptr to output scatter list
  215. * @output_len: size of the output scatter list
  216. */
  217. static inline void kpp_request_set_output(struct kpp_request *req,
  218. struct scatterlist *output,
  219. unsigned int output_len)
  220. {
  221. req->dst = output;
  222. req->dst_len = output_len;
  223. }
  224. enum {
  225. CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
  226. CRYPTO_KPP_SECRET_TYPE_DH,
  227. CRYPTO_KPP_SECRET_TYPE_ECDH,
  228. };
  229. /**
  230. * struct kpp_secret - small header for packing secret buffer
  231. *
  232. * @type: define type of secret. Each kpp type will define its own
  233. * @len: specify the len of the secret, include the header, that
  234. * follows the struct
  235. */
  236. struct kpp_secret {
  237. unsigned short type;
  238. unsigned short len;
  239. };
  240. /**
  241. * crypto_kpp_set_secret() - Invoke kpp operation
  242. *
  243. * Function invokes the specific kpp operation for a given alg.
  244. *
  245. * @tfm: tfm handle
  246. * @buffer: Buffer holding the packet representation of the private
  247. * key. The structure of the packet key depends on the particular
  248. * KPP implementation. Packing and unpacking helpers are provided
  249. * for ECDH and DH (see the respective header files for those
  250. * implementations).
  251. * @len: Length of the packet private key buffer.
  252. *
  253. * Return: zero on success; error code in case of error
  254. */
  255. static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
  256. const void *buffer, unsigned int len)
  257. {
  258. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  259. struct crypto_alg *calg = tfm->base.__crt_alg;
  260. int ret;
  261. crypto_stats_get(calg);
  262. ret = alg->set_secret(tfm, buffer, len);
  263. crypto_stats_kpp_set_secret(calg, ret);
  264. return ret;
  265. }
  266. /**
  267. * crypto_kpp_generate_public_key() - Invoke kpp operation
  268. *
  269. * Function invokes the specific kpp operation for generating the public part
  270. * for a given kpp algorithm.
  271. *
  272. * To generate a private key, the caller should use a random number generator.
  273. * The output of the requested length serves as the private key.
  274. *
  275. * @req: kpp key request
  276. *
  277. * Return: zero on success; error code in case of error
  278. */
  279. static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
  280. {
  281. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  282. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  283. struct crypto_alg *calg = tfm->base.__crt_alg;
  284. int ret;
  285. crypto_stats_get(calg);
  286. ret = alg->generate_public_key(req);
  287. crypto_stats_kpp_generate_public_key(calg, ret);
  288. return ret;
  289. }
  290. /**
  291. * crypto_kpp_compute_shared_secret() - Invoke kpp operation
  292. *
  293. * Function invokes the specific kpp operation for computing the shared secret
  294. * for a given kpp algorithm.
  295. *
  296. * @req: kpp key request
  297. *
  298. * Return: zero on success; error code in case of error
  299. */
  300. static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
  301. {
  302. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  303. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  304. struct crypto_alg *calg = tfm->base.__crt_alg;
  305. int ret;
  306. crypto_stats_get(calg);
  307. ret = alg->compute_shared_secret(req);
  308. crypto_stats_kpp_compute_shared_secret(calg, ret);
  309. return ret;
  310. }
  311. /**
  312. * crypto_kpp_maxsize() - Get len for output buffer
  313. *
  314. * Function returns the output buffer size required for a given key.
  315. * Function assumes that the key is already set in the transformation. If this
  316. * function is called without a setkey or with a failed setkey, you will end up
  317. * in a NULL dereference.
  318. *
  319. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
  320. */
  321. static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
  322. {
  323. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  324. return alg->max_size(tfm);
  325. }
  326. #endif