kpp.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 the 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, const 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. unsigned 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 Primitives 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 required 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. static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
  132. {
  133. return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
  134. }
  135. static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
  136. {
  137. crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
  138. }
  139. /**
  140. * crypto_free_kpp() - free KPP tfm handle
  141. *
  142. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
  143. */
  144. static inline void crypto_free_kpp(struct crypto_kpp *tfm)
  145. {
  146. crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
  147. }
  148. /**
  149. * kpp_request_alloc() - allocates kpp request
  150. *
  151. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
  152. * @gfp: allocation flags
  153. *
  154. * Return: allocated handle in case of success or NULL in case of an error.
  155. */
  156. static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
  157. gfp_t gfp)
  158. {
  159. struct kpp_request *req;
  160. req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
  161. if (likely(req))
  162. kpp_request_set_tfm(req, tfm);
  163. return req;
  164. }
  165. /**
  166. * kpp_request_free() - zeroize and free kpp request
  167. *
  168. * @req: request to free
  169. */
  170. static inline void kpp_request_free(struct kpp_request *req)
  171. {
  172. kzfree(req);
  173. }
  174. /**
  175. * kpp_request_set_callback() - Sets an asynchronous callback.
  176. *
  177. * Callback will be called when an asynchronous operation on a given
  178. * request is finished.
  179. *
  180. * @req: request that the callback will be set for
  181. * @flgs: specify for instance if the operation may backlog
  182. * @cmpl: callback which will be called
  183. * @data: private data used by the caller
  184. */
  185. static inline void kpp_request_set_callback(struct kpp_request *req,
  186. u32 flgs,
  187. crypto_completion_t cmpl,
  188. void *data)
  189. {
  190. req->base.complete = cmpl;
  191. req->base.data = data;
  192. req->base.flags = flgs;
  193. }
  194. /**
  195. * kpp_request_set_input() - Sets input buffer
  196. *
  197. * Sets parameters required by generate_public_key
  198. *
  199. * @req: kpp request
  200. * @input: ptr to input scatter list
  201. * @input_len: size of the input scatter list
  202. */
  203. static inline void kpp_request_set_input(struct kpp_request *req,
  204. struct scatterlist *input,
  205. unsigned int input_len)
  206. {
  207. req->src = input;
  208. req->src_len = input_len;
  209. }
  210. /**
  211. * kpp_request_set_output() - Sets output buffer
  212. *
  213. * Sets parameters required by kpp operation
  214. *
  215. * @req: kpp request
  216. * @output: ptr to output scatter list
  217. * @output_len: size of the output scatter list
  218. */
  219. static inline void kpp_request_set_output(struct kpp_request *req,
  220. struct scatterlist *output,
  221. unsigned int output_len)
  222. {
  223. req->dst = output;
  224. req->dst_len = output_len;
  225. }
  226. enum {
  227. CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
  228. CRYPTO_KPP_SECRET_TYPE_DH,
  229. CRYPTO_KPP_SECRET_TYPE_ECDH,
  230. };
  231. /**
  232. * struct kpp_secret - small header for packing secret buffer
  233. *
  234. * @type: define type of secret. Each kpp type will define its own
  235. * @len: specify the len of the secret, include the header, that
  236. * follows the struct
  237. */
  238. struct kpp_secret {
  239. unsigned short type;
  240. unsigned short len;
  241. };
  242. /**
  243. * crypto_kpp_set_secret() - Invoke kpp operation
  244. *
  245. * Function invokes the specific kpp operation for a given alg.
  246. *
  247. * @tfm: tfm handle
  248. * @buffer: Buffer holding the packet representation of the private
  249. * key. The structure of the packet key depends on the particular
  250. * KPP implementation. Packing and unpacking helpers are provided
  251. * for ECDH and DH (see the respective header files for those
  252. * implementations).
  253. * @len: Length of the packet private key buffer.
  254. *
  255. * Return: zero on success; error code in case of error
  256. */
  257. static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
  258. const void *buffer, unsigned int len)
  259. {
  260. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  261. return alg->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. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  280. return alg->generate_public_key(req);
  281. }
  282. /**
  283. * crypto_kpp_compute_shared_secret() - Invoke kpp operation
  284. *
  285. * Function invokes the specific kpp operation for computing the shared secret
  286. * for a given kpp algorithm.
  287. *
  288. * @req: kpp key request
  289. *
  290. * Return: zero on success; error code in case of error
  291. */
  292. static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
  293. {
  294. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  295. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  296. return alg->compute_shared_secret(req);
  297. }
  298. /**
  299. * crypto_kpp_maxsize() - Get len for output buffer
  300. *
  301. * Function returns the output buffer size required for a given key.
  302. * Function assumes that the key is already set in the transformation. If this
  303. * function is called without a setkey or with a failed setkey, you will end up
  304. * in a NULL dereference.
  305. *
  306. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
  307. */
  308. static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
  309. {
  310. struct kpp_alg *alg = crypto_kpp_alg(tfm);
  311. return alg->max_size(tfm);
  312. }
  313. #endif