xref: /openbmc/linux/include/crypto/kpp.h (revision e2950bf1)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
24e5f2c40SSalvatore Benedetto /*
34e5f2c40SSalvatore Benedetto  * Key-agreement Protocol Primitives (KPP)
44e5f2c40SSalvatore Benedetto  *
54e5f2c40SSalvatore Benedetto  * Copyright (c) 2016, Intel Corporation
64e5f2c40SSalvatore Benedetto  * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
74e5f2c40SSalvatore Benedetto  */
84e5f2c40SSalvatore Benedetto 
94e5f2c40SSalvatore Benedetto #ifndef _CRYPTO_KPP_
104e5f2c40SSalvatore Benedetto #define _CRYPTO_KPP_
11*e2950bf1SHerbert Xu 
12*e2950bf1SHerbert Xu #include <linux/atomic.h>
13*e2950bf1SHerbert Xu #include <linux/container_of.h>
144e5f2c40SSalvatore Benedetto #include <linux/crypto.h>
15*e2950bf1SHerbert Xu #include <linux/slab.h>
164e5f2c40SSalvatore Benedetto 
174e5f2c40SSalvatore Benedetto /**
184e5f2c40SSalvatore Benedetto  * struct kpp_request
194e5f2c40SSalvatore Benedetto  *
204e5f2c40SSalvatore Benedetto  * @base:	Common attributes for async crypto requests
214e5f2c40SSalvatore Benedetto  * @src:	Source data
224e5f2c40SSalvatore Benedetto  * @dst:	Destination data
234e5f2c40SSalvatore Benedetto  * @src_len:	Size of the input buffer
244e5f2c40SSalvatore Benedetto  * @dst_len:	Size of the output buffer. It needs to be at least
254e5f2c40SSalvatore Benedetto  *		as big as the expected result depending	on the operation
264e5f2c40SSalvatore Benedetto  *		After operation it will be updated with the actual size of the
274e5f2c40SSalvatore Benedetto  *		result. In case of error where the dst sgl size was insufficient,
284e5f2c40SSalvatore Benedetto  *		it will be updated to the size required for the operation.
294e5f2c40SSalvatore Benedetto  * @__ctx:	Start of private context data
304e5f2c40SSalvatore Benedetto  */
314e5f2c40SSalvatore Benedetto struct kpp_request {
324e5f2c40SSalvatore Benedetto 	struct crypto_async_request base;
334e5f2c40SSalvatore Benedetto 	struct scatterlist *src;
344e5f2c40SSalvatore Benedetto 	struct scatterlist *dst;
354e5f2c40SSalvatore Benedetto 	unsigned int src_len;
364e5f2c40SSalvatore Benedetto 	unsigned int dst_len;
374e5f2c40SSalvatore Benedetto 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
384e5f2c40SSalvatore Benedetto };
394e5f2c40SSalvatore Benedetto 
404e5f2c40SSalvatore Benedetto /**
414e5f2c40SSalvatore Benedetto  * struct crypto_kpp - user-instantiated object which encapsulate
424e5f2c40SSalvatore Benedetto  * algorithms and core processing logic
434e5f2c40SSalvatore Benedetto  *
444d2b225aSHerbert Xu  * @reqsize:		Request context size required by algorithm
454d2b225aSHerbert Xu  *			implementation
464e5f2c40SSalvatore Benedetto  * @base:	Common crypto API algorithm data structure
474e5f2c40SSalvatore Benedetto  */
484e5f2c40SSalvatore Benedetto struct crypto_kpp {
494d2b225aSHerbert Xu 	unsigned int reqsize;
504d2b225aSHerbert Xu 
514e5f2c40SSalvatore Benedetto 	struct crypto_tfm base;
524e5f2c40SSalvatore Benedetto };
534e5f2c40SSalvatore Benedetto 
54*e2950bf1SHerbert Xu /*
55*e2950bf1SHerbert Xu  * struct crypto_istat_kpp - statistics for KPP algorithm
56*e2950bf1SHerbert Xu  * @setsecret_cnt:		number of setsecrey operation
57*e2950bf1SHerbert Xu  * @generate_public_key_cnt:	number of generate_public_key operation
58*e2950bf1SHerbert Xu  * @compute_shared_secret_cnt:	number of compute_shared_secret operation
59*e2950bf1SHerbert Xu  * @err_cnt:			number of error for KPP requests
60*e2950bf1SHerbert Xu  */
61*e2950bf1SHerbert Xu struct crypto_istat_kpp {
62*e2950bf1SHerbert Xu 	atomic64_t setsecret_cnt;
63*e2950bf1SHerbert Xu 	atomic64_t generate_public_key_cnt;
64*e2950bf1SHerbert Xu 	atomic64_t compute_shared_secret_cnt;
65*e2950bf1SHerbert Xu 	atomic64_t err_cnt;
66*e2950bf1SHerbert Xu };
67*e2950bf1SHerbert Xu 
684e5f2c40SSalvatore Benedetto /**
694e5f2c40SSalvatore Benedetto  * struct kpp_alg - generic key-agreement protocol primitives
704e5f2c40SSalvatore Benedetto  *
714e5f2c40SSalvatore Benedetto  * @set_secret:		Function invokes the protocol specific function to
724e5f2c40SSalvatore Benedetto  *			store the secret private key along with parameters.
73c0ca1215STudor-Dan Ambarus  *			The implementation knows how to decode the buffer
744e5f2c40SSalvatore Benedetto  * @generate_public_key: Function generate the public key to be sent to the
754e5f2c40SSalvatore Benedetto  *			counterpart. In case of error, where output is not big
764e5f2c40SSalvatore Benedetto  *			enough req->dst_len will be updated to the size
774e5f2c40SSalvatore Benedetto  *			required
784e5f2c40SSalvatore Benedetto  * @compute_shared_secret: Function compute the shared secret as defined by
794e5f2c40SSalvatore Benedetto  *			the algorithm. The result is given back to the user.
804e5f2c40SSalvatore Benedetto  *			In case of error, where output is not big enough,
814e5f2c40SSalvatore Benedetto  *			req->dst_len will be updated to the size required
824e5f2c40SSalvatore Benedetto  * @max_size:		Function returns the size of the output buffer
834e5f2c40SSalvatore Benedetto  * @init:		Initialize the object. This is called only once at
844e5f2c40SSalvatore Benedetto  *			instantiation time. In case the cryptographic hardware
854e5f2c40SSalvatore Benedetto  *			needs to be initialized. Software fallback should be
864e5f2c40SSalvatore Benedetto  *			put in place here.
874e5f2c40SSalvatore Benedetto  * @exit:		Undo everything @init did.
884e5f2c40SSalvatore Benedetto  *
898d23da22SStephan Mueller  * @base:		Common crypto API algorithm data structure
90*e2950bf1SHerbert Xu  * @stat:		Statistics for KPP algorithm
914e5f2c40SSalvatore Benedetto  */
924e5f2c40SSalvatore Benedetto struct kpp_alg {
935527dfb6SEric Biggers 	int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
944e5f2c40SSalvatore Benedetto 			  unsigned int len);
954e5f2c40SSalvatore Benedetto 	int (*generate_public_key)(struct kpp_request *req);
964e5f2c40SSalvatore Benedetto 	int (*compute_shared_secret)(struct kpp_request *req);
974e5f2c40SSalvatore Benedetto 
98c444b8daSTudor-Dan Ambarus 	unsigned int (*max_size)(struct crypto_kpp *tfm);
994e5f2c40SSalvatore Benedetto 
1004e5f2c40SSalvatore Benedetto 	int (*init)(struct crypto_kpp *tfm);
1014e5f2c40SSalvatore Benedetto 	void (*exit)(struct crypto_kpp *tfm);
1024e5f2c40SSalvatore Benedetto 
103*e2950bf1SHerbert Xu #ifdef CONFIG_CRYPTO_STATS
104*e2950bf1SHerbert Xu 	struct crypto_istat_kpp stat;
105*e2950bf1SHerbert Xu #endif
106*e2950bf1SHerbert Xu 
1074e5f2c40SSalvatore Benedetto 	struct crypto_alg base;
1084e5f2c40SSalvatore Benedetto };
1094e5f2c40SSalvatore Benedetto 
1104e5f2c40SSalvatore Benedetto /**
1118d23da22SStephan Mueller  * DOC: Generic Key-agreement Protocol Primitives API
1124e5f2c40SSalvatore Benedetto  *
1134e5f2c40SSalvatore Benedetto  * The KPP API is used with the algorithm type
1144e5f2c40SSalvatore Benedetto  * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
1154e5f2c40SSalvatore Benedetto  */
1164e5f2c40SSalvatore Benedetto 
1174e5f2c40SSalvatore Benedetto /**
1184e5f2c40SSalvatore Benedetto  * crypto_alloc_kpp() - allocate KPP tfm handle
1194e5f2c40SSalvatore Benedetto  * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
1204e5f2c40SSalvatore Benedetto  * @type: specifies the type of the algorithm
1214e5f2c40SSalvatore Benedetto  * @mask: specifies the mask for the algorithm
1224e5f2c40SSalvatore Benedetto  *
1234e5f2c40SSalvatore Benedetto  * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
124c0ca1215STudor-Dan Ambarus  * is required for any following API invocation
1254e5f2c40SSalvatore Benedetto  *
1264e5f2c40SSalvatore Benedetto  * Return: allocated handle in case of success; IS_ERR() is true in case of
1274e5f2c40SSalvatore Benedetto  *	   an error, PTR_ERR() returns the error code.
1284e5f2c40SSalvatore Benedetto  */
1294e5f2c40SSalvatore Benedetto struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
1304e5f2c40SSalvatore Benedetto 
1319e2f284eSHannes Reinecke int crypto_has_kpp(const char *alg_name, u32 type, u32 mask);
1329e2f284eSHannes Reinecke 
crypto_kpp_tfm(struct crypto_kpp * tfm)1334e5f2c40SSalvatore Benedetto static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
1344e5f2c40SSalvatore Benedetto {
1354e5f2c40SSalvatore Benedetto 	return &tfm->base;
1364e5f2c40SSalvatore Benedetto }
1374e5f2c40SSalvatore Benedetto 
__crypto_kpp_alg(struct crypto_alg * alg)1384e5f2c40SSalvatore Benedetto static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
1394e5f2c40SSalvatore Benedetto {
1404e5f2c40SSalvatore Benedetto 	return container_of(alg, struct kpp_alg, base);
1414e5f2c40SSalvatore Benedetto }
1424e5f2c40SSalvatore Benedetto 
__crypto_kpp_tfm(struct crypto_tfm * tfm)1434e5f2c40SSalvatore Benedetto static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
1444e5f2c40SSalvatore Benedetto {
1454e5f2c40SSalvatore Benedetto 	return container_of(tfm, struct crypto_kpp, base);
1464e5f2c40SSalvatore Benedetto }
1474e5f2c40SSalvatore Benedetto 
crypto_kpp_alg(struct crypto_kpp * tfm)1484e5f2c40SSalvatore Benedetto static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
1494e5f2c40SSalvatore Benedetto {
1504e5f2c40SSalvatore Benedetto 	return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
1514e5f2c40SSalvatore Benedetto }
1524e5f2c40SSalvatore Benedetto 
crypto_kpp_reqsize(struct crypto_kpp * tfm)1534e5f2c40SSalvatore Benedetto static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
1544e5f2c40SSalvatore Benedetto {
1554d2b225aSHerbert Xu 	return tfm->reqsize;
1564e5f2c40SSalvatore Benedetto }
1574e5f2c40SSalvatore Benedetto 
kpp_request_set_tfm(struct kpp_request * req,struct crypto_kpp * tfm)1584e5f2c40SSalvatore Benedetto static inline void kpp_request_set_tfm(struct kpp_request *req,
1594e5f2c40SSalvatore Benedetto 				       struct crypto_kpp *tfm)
1604e5f2c40SSalvatore Benedetto {
1614e5f2c40SSalvatore Benedetto 	req->base.tfm = crypto_kpp_tfm(tfm);
1624e5f2c40SSalvatore Benedetto }
1634e5f2c40SSalvatore Benedetto 
crypto_kpp_reqtfm(struct kpp_request * req)1644e5f2c40SSalvatore Benedetto static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
1654e5f2c40SSalvatore Benedetto {
1664e5f2c40SSalvatore Benedetto 	return __crypto_kpp_tfm(req->base.tfm);
1674e5f2c40SSalvatore Benedetto }
1684e5f2c40SSalvatore Benedetto 
crypto_kpp_get_flags(struct crypto_kpp * tfm)16966d3994cSTudor-Dan Ambarus static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
17066d3994cSTudor-Dan Ambarus {
17166d3994cSTudor-Dan Ambarus 	return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
17266d3994cSTudor-Dan Ambarus }
17366d3994cSTudor-Dan Ambarus 
crypto_kpp_set_flags(struct crypto_kpp * tfm,u32 flags)17466d3994cSTudor-Dan Ambarus static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
17566d3994cSTudor-Dan Ambarus {
17666d3994cSTudor-Dan Ambarus 	crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
17766d3994cSTudor-Dan Ambarus }
17866d3994cSTudor-Dan Ambarus 
1794e5f2c40SSalvatore Benedetto /**
1804e5f2c40SSalvatore Benedetto  * crypto_free_kpp() - free KPP tfm handle
1814e5f2c40SSalvatore Benedetto  *
1824e5f2c40SSalvatore Benedetto  * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
18383681f2bSArd Biesheuvel  *
18483681f2bSArd Biesheuvel  * If @tfm is a NULL or error pointer, this function does nothing.
1854e5f2c40SSalvatore Benedetto  */
crypto_free_kpp(struct crypto_kpp * tfm)1864e5f2c40SSalvatore Benedetto static inline void crypto_free_kpp(struct crypto_kpp *tfm)
1874e5f2c40SSalvatore Benedetto {
1884e5f2c40SSalvatore Benedetto 	crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
1894e5f2c40SSalvatore Benedetto }
1904e5f2c40SSalvatore Benedetto 
1914e5f2c40SSalvatore Benedetto /**
1924e5f2c40SSalvatore Benedetto  * kpp_request_alloc() - allocates kpp request
1934e5f2c40SSalvatore Benedetto  *
1944e5f2c40SSalvatore Benedetto  * @tfm:	KPP tfm handle allocated with crypto_alloc_kpp()
1954e5f2c40SSalvatore Benedetto  * @gfp:	allocation flags
1964e5f2c40SSalvatore Benedetto  *
1974e5f2c40SSalvatore Benedetto  * Return: allocated handle in case of success or NULL in case of an error.
1984e5f2c40SSalvatore Benedetto  */
kpp_request_alloc(struct crypto_kpp * tfm,gfp_t gfp)1994e5f2c40SSalvatore Benedetto static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
2004e5f2c40SSalvatore Benedetto 						    gfp_t gfp)
2014e5f2c40SSalvatore Benedetto {
2024e5f2c40SSalvatore Benedetto 	struct kpp_request *req;
2034e5f2c40SSalvatore Benedetto 
2044e5f2c40SSalvatore Benedetto 	req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
2054e5f2c40SSalvatore Benedetto 	if (likely(req))
2064e5f2c40SSalvatore Benedetto 		kpp_request_set_tfm(req, tfm);
2074e5f2c40SSalvatore Benedetto 
2084e5f2c40SSalvatore Benedetto 	return req;
2094e5f2c40SSalvatore Benedetto }
2104e5f2c40SSalvatore Benedetto 
2114e5f2c40SSalvatore Benedetto /**
2124e5f2c40SSalvatore Benedetto  * kpp_request_free() - zeroize and free kpp request
2134e5f2c40SSalvatore Benedetto  *
2144e5f2c40SSalvatore Benedetto  * @req:	request to free
2154e5f2c40SSalvatore Benedetto  */
kpp_request_free(struct kpp_request * req)2164e5f2c40SSalvatore Benedetto static inline void kpp_request_free(struct kpp_request *req)
2174e5f2c40SSalvatore Benedetto {
218453431a5SWaiman Long 	kfree_sensitive(req);
2194e5f2c40SSalvatore Benedetto }
2204e5f2c40SSalvatore Benedetto 
2214e5f2c40SSalvatore Benedetto /**
2224e5f2c40SSalvatore Benedetto  * kpp_request_set_callback() - Sets an asynchronous callback.
2234e5f2c40SSalvatore Benedetto  *
2244e5f2c40SSalvatore Benedetto  * Callback will be called when an asynchronous operation on a given
2254e5f2c40SSalvatore Benedetto  * request is finished.
2264e5f2c40SSalvatore Benedetto  *
2274e5f2c40SSalvatore Benedetto  * @req:	request that the callback will be set for
2284e5f2c40SSalvatore Benedetto  * @flgs:	specify for instance if the operation may backlog
2294e5f2c40SSalvatore Benedetto  * @cmpl:	callback which will be called
2304e5f2c40SSalvatore Benedetto  * @data:	private data used by the caller
2314e5f2c40SSalvatore Benedetto  */
kpp_request_set_callback(struct kpp_request * req,u32 flgs,crypto_completion_t cmpl,void * data)2324e5f2c40SSalvatore Benedetto static inline void kpp_request_set_callback(struct kpp_request *req,
2334e5f2c40SSalvatore Benedetto 					    u32 flgs,
2344e5f2c40SSalvatore Benedetto 					    crypto_completion_t cmpl,
2354e5f2c40SSalvatore Benedetto 					    void *data)
2364e5f2c40SSalvatore Benedetto {
2374e5f2c40SSalvatore Benedetto 	req->base.complete = cmpl;
2384e5f2c40SSalvatore Benedetto 	req->base.data = data;
2394e5f2c40SSalvatore Benedetto 	req->base.flags = flgs;
2404e5f2c40SSalvatore Benedetto }
2414e5f2c40SSalvatore Benedetto 
2424e5f2c40SSalvatore Benedetto /**
2434e5f2c40SSalvatore Benedetto  * kpp_request_set_input() - Sets input buffer
2444e5f2c40SSalvatore Benedetto  *
2454e5f2c40SSalvatore Benedetto  * Sets parameters required by generate_public_key
2464e5f2c40SSalvatore Benedetto  *
2474e5f2c40SSalvatore Benedetto  * @req:	kpp request
2484e5f2c40SSalvatore Benedetto  * @input:	ptr to input scatter list
2494e5f2c40SSalvatore Benedetto  * @input_len:	size of the input scatter list
2504e5f2c40SSalvatore Benedetto  */
kpp_request_set_input(struct kpp_request * req,struct scatterlist * input,unsigned int input_len)2514e5f2c40SSalvatore Benedetto static inline void kpp_request_set_input(struct kpp_request *req,
2524e5f2c40SSalvatore Benedetto 					 struct scatterlist *input,
2534e5f2c40SSalvatore Benedetto 					 unsigned int input_len)
2544e5f2c40SSalvatore Benedetto {
2554e5f2c40SSalvatore Benedetto 	req->src = input;
2564e5f2c40SSalvatore Benedetto 	req->src_len = input_len;
2574e5f2c40SSalvatore Benedetto }
2584e5f2c40SSalvatore Benedetto 
2594e5f2c40SSalvatore Benedetto /**
2604e5f2c40SSalvatore Benedetto  * kpp_request_set_output() - Sets output buffer
2614e5f2c40SSalvatore Benedetto  *
2624e5f2c40SSalvatore Benedetto  * Sets parameters required by kpp operation
2634e5f2c40SSalvatore Benedetto  *
2644e5f2c40SSalvatore Benedetto  * @req:	kpp request
2654e5f2c40SSalvatore Benedetto  * @output:	ptr to output scatter list
2664e5f2c40SSalvatore Benedetto  * @output_len:	size of the output scatter list
2674e5f2c40SSalvatore Benedetto  */
kpp_request_set_output(struct kpp_request * req,struct scatterlist * output,unsigned int output_len)2684e5f2c40SSalvatore Benedetto static inline void kpp_request_set_output(struct kpp_request *req,
2694e5f2c40SSalvatore Benedetto 					  struct scatterlist *output,
2704e5f2c40SSalvatore Benedetto 					  unsigned int output_len)
2714e5f2c40SSalvatore Benedetto {
2724e5f2c40SSalvatore Benedetto 	req->dst = output;
2734e5f2c40SSalvatore Benedetto 	req->dst_len = output_len;
2744e5f2c40SSalvatore Benedetto }
2754e5f2c40SSalvatore Benedetto 
2764e5f2c40SSalvatore Benedetto enum {
2774e5f2c40SSalvatore Benedetto 	CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
278802c7f1cSSalvatore Benedetto 	CRYPTO_KPP_SECRET_TYPE_DH,
2793c4b2390SSalvatore Benedetto 	CRYPTO_KPP_SECRET_TYPE_ECDH,
2804e5f2c40SSalvatore Benedetto };
2814e5f2c40SSalvatore Benedetto 
2824e5f2c40SSalvatore Benedetto /**
2834e5f2c40SSalvatore Benedetto  * struct kpp_secret - small header for packing secret buffer
2844e5f2c40SSalvatore Benedetto  *
2854e5f2c40SSalvatore Benedetto  * @type:	define type of secret. Each kpp type will define its own
2864e5f2c40SSalvatore Benedetto  * @len:	specify the len of the secret, include the header, that
2874e5f2c40SSalvatore Benedetto  *		follows the struct
2884e5f2c40SSalvatore Benedetto  */
2894e5f2c40SSalvatore Benedetto struct kpp_secret {
2904e5f2c40SSalvatore Benedetto 	unsigned short type;
2914e5f2c40SSalvatore Benedetto 	unsigned short len;
2924e5f2c40SSalvatore Benedetto };
2934e5f2c40SSalvatore Benedetto 
kpp_get_stat(struct kpp_alg * alg)294*e2950bf1SHerbert Xu static inline struct crypto_istat_kpp *kpp_get_stat(struct kpp_alg *alg)
295*e2950bf1SHerbert Xu {
296*e2950bf1SHerbert Xu #ifdef CONFIG_CRYPTO_STATS
297*e2950bf1SHerbert Xu 	return &alg->stat;
298*e2950bf1SHerbert Xu #else
299*e2950bf1SHerbert Xu 	return NULL;
300*e2950bf1SHerbert Xu #endif
301*e2950bf1SHerbert Xu }
302*e2950bf1SHerbert Xu 
crypto_kpp_errstat(struct kpp_alg * alg,int err)303*e2950bf1SHerbert Xu static inline int crypto_kpp_errstat(struct kpp_alg *alg, int err)
304*e2950bf1SHerbert Xu {
305*e2950bf1SHerbert Xu 	if (!IS_ENABLED(CONFIG_CRYPTO_STATS))
306*e2950bf1SHerbert Xu 		return err;
307*e2950bf1SHerbert Xu 
308*e2950bf1SHerbert Xu 	if (err && err != -EINPROGRESS && err != -EBUSY)
309*e2950bf1SHerbert Xu 		atomic64_inc(&kpp_get_stat(alg)->err_cnt);
310*e2950bf1SHerbert Xu 
311*e2950bf1SHerbert Xu 	return err;
312*e2950bf1SHerbert Xu }
313*e2950bf1SHerbert Xu 
3144e5f2c40SSalvatore Benedetto /**
3154e5f2c40SSalvatore Benedetto  * crypto_kpp_set_secret() - Invoke kpp operation
3164e5f2c40SSalvatore Benedetto  *
3174e5f2c40SSalvatore Benedetto  * Function invokes the specific kpp operation for a given alg.
3184e5f2c40SSalvatore Benedetto  *
3194e5f2c40SSalvatore Benedetto  * @tfm:	tfm handle
3208d23da22SStephan Mueller  * @buffer:	Buffer holding the packet representation of the private
3218d23da22SStephan Mueller  *		key. The structure of the packet key depends on the particular
3228d23da22SStephan Mueller  *		KPP implementation. Packing and unpacking helpers are provided
3238d23da22SStephan Mueller  *		for ECDH and DH (see the respective header files for those
3248d23da22SStephan Mueller  *		implementations).
3258d23da22SStephan Mueller  * @len:	Length of the packet private key buffer.
3264e5f2c40SSalvatore Benedetto  *
3274e5f2c40SSalvatore Benedetto  * Return: zero on success; error code in case of error
3284e5f2c40SSalvatore Benedetto  */
crypto_kpp_set_secret(struct crypto_kpp * tfm,const void * buffer,unsigned int len)3295527dfb6SEric Biggers static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
3305527dfb6SEric Biggers 					const void *buffer, unsigned int len)
3314e5f2c40SSalvatore Benedetto {
3324e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
3334e5f2c40SSalvatore Benedetto 
334*e2950bf1SHerbert Xu 	if (IS_ENABLED(CONFIG_CRYPTO_STATS))
335*e2950bf1SHerbert Xu 		atomic64_inc(&kpp_get_stat(alg)->setsecret_cnt);
336*e2950bf1SHerbert Xu 
337*e2950bf1SHerbert Xu 	return crypto_kpp_errstat(alg, alg->set_secret(tfm, buffer, len));
3384e5f2c40SSalvatore Benedetto }
3394e5f2c40SSalvatore Benedetto 
3404e5f2c40SSalvatore Benedetto /**
3414e5f2c40SSalvatore Benedetto  * crypto_kpp_generate_public_key() - Invoke kpp operation
3424e5f2c40SSalvatore Benedetto  *
3434e5f2c40SSalvatore Benedetto  * Function invokes the specific kpp operation for generating the public part
3448d23da22SStephan Mueller  * for a given kpp algorithm.
3458d23da22SStephan Mueller  *
3468d23da22SStephan Mueller  * To generate a private key, the caller should use a random number generator.
3478d23da22SStephan Mueller  * The output of the requested length serves as the private key.
3484e5f2c40SSalvatore Benedetto  *
3494e5f2c40SSalvatore Benedetto  * @req:	kpp key request
3504e5f2c40SSalvatore Benedetto  *
3514e5f2c40SSalvatore Benedetto  * Return: zero on success; error code in case of error
3524e5f2c40SSalvatore Benedetto  */
crypto_kpp_generate_public_key(struct kpp_request * req)3534e5f2c40SSalvatore Benedetto static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
3544e5f2c40SSalvatore Benedetto {
3554e5f2c40SSalvatore Benedetto 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
3564e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
3574e5f2c40SSalvatore Benedetto 
358*e2950bf1SHerbert Xu 	if (IS_ENABLED(CONFIG_CRYPTO_STATS))
359*e2950bf1SHerbert Xu 		atomic64_inc(&kpp_get_stat(alg)->generate_public_key_cnt);
360*e2950bf1SHerbert Xu 
361*e2950bf1SHerbert Xu 	return crypto_kpp_errstat(alg, alg->generate_public_key(req));
3624e5f2c40SSalvatore Benedetto }
3634e5f2c40SSalvatore Benedetto 
3644e5f2c40SSalvatore Benedetto /**
3654e5f2c40SSalvatore Benedetto  * crypto_kpp_compute_shared_secret() - Invoke kpp operation
3664e5f2c40SSalvatore Benedetto  *
3674e5f2c40SSalvatore Benedetto  * Function invokes the specific kpp operation for computing the shared secret
3684e5f2c40SSalvatore Benedetto  * for a given kpp algorithm.
3694e5f2c40SSalvatore Benedetto  *
3704e5f2c40SSalvatore Benedetto  * @req:	kpp key request
3714e5f2c40SSalvatore Benedetto  *
3724e5f2c40SSalvatore Benedetto  * Return: zero on success; error code in case of error
3734e5f2c40SSalvatore Benedetto  */
crypto_kpp_compute_shared_secret(struct kpp_request * req)3744e5f2c40SSalvatore Benedetto static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
3754e5f2c40SSalvatore Benedetto {
3764e5f2c40SSalvatore Benedetto 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
3774e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
3784e5f2c40SSalvatore Benedetto 
379*e2950bf1SHerbert Xu 	if (IS_ENABLED(CONFIG_CRYPTO_STATS))
380*e2950bf1SHerbert Xu 		atomic64_inc(&kpp_get_stat(alg)->compute_shared_secret_cnt);
381*e2950bf1SHerbert Xu 
382*e2950bf1SHerbert Xu 	return crypto_kpp_errstat(alg, alg->compute_shared_secret(req));
3834e5f2c40SSalvatore Benedetto }
3844e5f2c40SSalvatore Benedetto 
3854e5f2c40SSalvatore Benedetto /**
3864e5f2c40SSalvatore Benedetto  * crypto_kpp_maxsize() - Get len for output buffer
3874e5f2c40SSalvatore Benedetto  *
388c444b8daSTudor-Dan Ambarus  * Function returns the output buffer size required for a given key.
389c444b8daSTudor-Dan Ambarus  * Function assumes that the key is already set in the transformation. If this
390c444b8daSTudor-Dan Ambarus  * function is called without a setkey or with a failed setkey, you will end up
391c444b8daSTudor-Dan Ambarus  * in a NULL dereference.
3924e5f2c40SSalvatore Benedetto  *
3934e5f2c40SSalvatore Benedetto  * @tfm:	KPP tfm handle allocated with crypto_alloc_kpp()
3944e5f2c40SSalvatore Benedetto  */
crypto_kpp_maxsize(struct crypto_kpp * tfm)395c444b8daSTudor-Dan Ambarus static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
3964e5f2c40SSalvatore Benedetto {
3974e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
3984e5f2c40SSalvatore Benedetto 
3994e5f2c40SSalvatore Benedetto 	return alg->max_size(tfm);
4004e5f2c40SSalvatore Benedetto }
4014e5f2c40SSalvatore Benedetto 
4024e5f2c40SSalvatore Benedetto #endif
403