kpp.h 9.5 KB

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