kpp.h 8.8 KB

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