kpp.h 9.7 KB

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