xref: /openbmc/linux/include/crypto/kpp.h (revision 9e2f284e)
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_
114e5f2c40SSalvatore Benedetto #include <linux/crypto.h>
124e5f2c40SSalvatore Benedetto 
134e5f2c40SSalvatore Benedetto /**
144e5f2c40SSalvatore Benedetto  * struct kpp_request
154e5f2c40SSalvatore Benedetto  *
164e5f2c40SSalvatore Benedetto  * @base:	Common attributes for async crypto requests
174e5f2c40SSalvatore Benedetto  * @src:	Source data
184e5f2c40SSalvatore Benedetto  * @dst:	Destination data
194e5f2c40SSalvatore Benedetto  * @src_len:	Size of the input buffer
204e5f2c40SSalvatore Benedetto  * @dst_len:	Size of the output buffer. It needs to be at least
214e5f2c40SSalvatore Benedetto  *		as big as the expected result depending	on the operation
224e5f2c40SSalvatore Benedetto  *		After operation it will be updated with the actual size of the
234e5f2c40SSalvatore Benedetto  *		result. In case of error where the dst sgl size was insufficient,
244e5f2c40SSalvatore Benedetto  *		it will be updated to the size required for the operation.
254e5f2c40SSalvatore Benedetto  * @__ctx:	Start of private context data
264e5f2c40SSalvatore Benedetto  */
274e5f2c40SSalvatore Benedetto struct kpp_request {
284e5f2c40SSalvatore Benedetto 	struct crypto_async_request base;
294e5f2c40SSalvatore Benedetto 	struct scatterlist *src;
304e5f2c40SSalvatore Benedetto 	struct scatterlist *dst;
314e5f2c40SSalvatore Benedetto 	unsigned int src_len;
324e5f2c40SSalvatore Benedetto 	unsigned int dst_len;
334e5f2c40SSalvatore Benedetto 	void *__ctx[] CRYPTO_MINALIGN_ATTR;
344e5f2c40SSalvatore Benedetto };
354e5f2c40SSalvatore Benedetto 
364e5f2c40SSalvatore Benedetto /**
374e5f2c40SSalvatore Benedetto  * struct crypto_kpp - user-instantiated object which encapsulate
384e5f2c40SSalvatore Benedetto  * algorithms and core processing logic
394e5f2c40SSalvatore Benedetto  *
404e5f2c40SSalvatore Benedetto  * @base:	Common crypto API algorithm data structure
414e5f2c40SSalvatore Benedetto  */
424e5f2c40SSalvatore Benedetto struct crypto_kpp {
434e5f2c40SSalvatore Benedetto 	struct crypto_tfm base;
444e5f2c40SSalvatore Benedetto };
454e5f2c40SSalvatore Benedetto 
464e5f2c40SSalvatore Benedetto /**
474e5f2c40SSalvatore Benedetto  * struct kpp_alg - generic key-agreement protocol primitives
484e5f2c40SSalvatore Benedetto  *
494e5f2c40SSalvatore Benedetto  * @set_secret:		Function invokes the protocol specific function to
504e5f2c40SSalvatore Benedetto  *			store the secret private key along with parameters.
51c0ca1215STudor-Dan Ambarus  *			The implementation knows how to decode the buffer
524e5f2c40SSalvatore Benedetto  * @generate_public_key: Function generate the public key to be sent to the
534e5f2c40SSalvatore Benedetto  *			counterpart. In case of error, where output is not big
544e5f2c40SSalvatore Benedetto  *			enough req->dst_len will be updated to the size
554e5f2c40SSalvatore Benedetto  *			required
564e5f2c40SSalvatore Benedetto  * @compute_shared_secret: Function compute the shared secret as defined by
574e5f2c40SSalvatore Benedetto  *			the algorithm. The result is given back to the user.
584e5f2c40SSalvatore Benedetto  *			In case of error, where output is not big enough,
594e5f2c40SSalvatore Benedetto  *			req->dst_len will be updated to the size required
604e5f2c40SSalvatore Benedetto  * @max_size:		Function returns the size of the output buffer
614e5f2c40SSalvatore Benedetto  * @init:		Initialize the object. This is called only once at
624e5f2c40SSalvatore Benedetto  *			instantiation time. In case the cryptographic hardware
634e5f2c40SSalvatore Benedetto  *			needs to be initialized. Software fallback should be
644e5f2c40SSalvatore Benedetto  *			put in place here.
654e5f2c40SSalvatore Benedetto  * @exit:		Undo everything @init did.
664e5f2c40SSalvatore Benedetto  *
674e5f2c40SSalvatore Benedetto  * @reqsize:		Request context size required by algorithm
684e5f2c40SSalvatore Benedetto  *			implementation
698d23da22SStephan Mueller  * @base:		Common crypto API algorithm data structure
704e5f2c40SSalvatore Benedetto  */
714e5f2c40SSalvatore Benedetto struct kpp_alg {
725527dfb6SEric Biggers 	int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
734e5f2c40SSalvatore Benedetto 			  unsigned int len);
744e5f2c40SSalvatore Benedetto 	int (*generate_public_key)(struct kpp_request *req);
754e5f2c40SSalvatore Benedetto 	int (*compute_shared_secret)(struct kpp_request *req);
764e5f2c40SSalvatore Benedetto 
77c444b8daSTudor-Dan Ambarus 	unsigned int (*max_size)(struct crypto_kpp *tfm);
784e5f2c40SSalvatore Benedetto 
794e5f2c40SSalvatore Benedetto 	int (*init)(struct crypto_kpp *tfm);
804e5f2c40SSalvatore Benedetto 	void (*exit)(struct crypto_kpp *tfm);
814e5f2c40SSalvatore Benedetto 
824e5f2c40SSalvatore Benedetto 	unsigned int reqsize;
834e5f2c40SSalvatore Benedetto 	struct crypto_alg base;
844e5f2c40SSalvatore Benedetto };
854e5f2c40SSalvatore Benedetto 
864e5f2c40SSalvatore Benedetto /**
878d23da22SStephan Mueller  * DOC: Generic Key-agreement Protocol Primitives API
884e5f2c40SSalvatore Benedetto  *
894e5f2c40SSalvatore Benedetto  * The KPP API is used with the algorithm type
904e5f2c40SSalvatore Benedetto  * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
914e5f2c40SSalvatore Benedetto  */
924e5f2c40SSalvatore Benedetto 
934e5f2c40SSalvatore Benedetto /**
944e5f2c40SSalvatore Benedetto  * crypto_alloc_kpp() - allocate KPP tfm handle
954e5f2c40SSalvatore Benedetto  * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
964e5f2c40SSalvatore Benedetto  * @type: specifies the type of the algorithm
974e5f2c40SSalvatore Benedetto  * @mask: specifies the mask for the algorithm
984e5f2c40SSalvatore Benedetto  *
994e5f2c40SSalvatore Benedetto  * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
100c0ca1215STudor-Dan Ambarus  * is required for any following API invocation
1014e5f2c40SSalvatore Benedetto  *
1024e5f2c40SSalvatore Benedetto  * Return: allocated handle in case of success; IS_ERR() is true in case of
1034e5f2c40SSalvatore Benedetto  *	   an error, PTR_ERR() returns the error code.
1044e5f2c40SSalvatore Benedetto  */
1054e5f2c40SSalvatore Benedetto struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
1064e5f2c40SSalvatore Benedetto 
107*9e2f284eSHannes Reinecke int crypto_has_kpp(const char *alg_name, u32 type, u32 mask);
108*9e2f284eSHannes Reinecke 
1094e5f2c40SSalvatore Benedetto static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
1104e5f2c40SSalvatore Benedetto {
1114e5f2c40SSalvatore Benedetto 	return &tfm->base;
1124e5f2c40SSalvatore Benedetto }
1134e5f2c40SSalvatore Benedetto 
1144e5f2c40SSalvatore Benedetto static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
1154e5f2c40SSalvatore Benedetto {
1164e5f2c40SSalvatore Benedetto 	return container_of(alg, struct kpp_alg, base);
1174e5f2c40SSalvatore Benedetto }
1184e5f2c40SSalvatore Benedetto 
1194e5f2c40SSalvatore Benedetto static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
1204e5f2c40SSalvatore Benedetto {
1214e5f2c40SSalvatore Benedetto 	return container_of(tfm, struct crypto_kpp, base);
1224e5f2c40SSalvatore Benedetto }
1234e5f2c40SSalvatore Benedetto 
1244e5f2c40SSalvatore Benedetto static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
1254e5f2c40SSalvatore Benedetto {
1264e5f2c40SSalvatore Benedetto 	return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
1274e5f2c40SSalvatore Benedetto }
1284e5f2c40SSalvatore Benedetto 
1294e5f2c40SSalvatore Benedetto static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
1304e5f2c40SSalvatore Benedetto {
1314e5f2c40SSalvatore Benedetto 	return crypto_kpp_alg(tfm)->reqsize;
1324e5f2c40SSalvatore Benedetto }
1334e5f2c40SSalvatore Benedetto 
1344e5f2c40SSalvatore Benedetto static inline void kpp_request_set_tfm(struct kpp_request *req,
1354e5f2c40SSalvatore Benedetto 				       struct crypto_kpp *tfm)
1364e5f2c40SSalvatore Benedetto {
1374e5f2c40SSalvatore Benedetto 	req->base.tfm = crypto_kpp_tfm(tfm);
1384e5f2c40SSalvatore Benedetto }
1394e5f2c40SSalvatore Benedetto 
1404e5f2c40SSalvatore Benedetto static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
1414e5f2c40SSalvatore Benedetto {
1424e5f2c40SSalvatore Benedetto 	return __crypto_kpp_tfm(req->base.tfm);
1434e5f2c40SSalvatore Benedetto }
1444e5f2c40SSalvatore Benedetto 
14566d3994cSTudor-Dan Ambarus static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
14666d3994cSTudor-Dan Ambarus {
14766d3994cSTudor-Dan Ambarus 	return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
14866d3994cSTudor-Dan Ambarus }
14966d3994cSTudor-Dan Ambarus 
15066d3994cSTudor-Dan Ambarus static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
15166d3994cSTudor-Dan Ambarus {
15266d3994cSTudor-Dan Ambarus 	crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
15366d3994cSTudor-Dan Ambarus }
15466d3994cSTudor-Dan Ambarus 
1554e5f2c40SSalvatore Benedetto /**
1564e5f2c40SSalvatore Benedetto  * crypto_free_kpp() - free KPP tfm handle
1574e5f2c40SSalvatore Benedetto  *
1584e5f2c40SSalvatore Benedetto  * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
15983681f2bSArd Biesheuvel  *
16083681f2bSArd Biesheuvel  * If @tfm is a NULL or error pointer, this function does nothing.
1614e5f2c40SSalvatore Benedetto  */
1624e5f2c40SSalvatore Benedetto static inline void crypto_free_kpp(struct crypto_kpp *tfm)
1634e5f2c40SSalvatore Benedetto {
1644e5f2c40SSalvatore Benedetto 	crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
1654e5f2c40SSalvatore Benedetto }
1664e5f2c40SSalvatore Benedetto 
1674e5f2c40SSalvatore Benedetto /**
1684e5f2c40SSalvatore Benedetto  * kpp_request_alloc() - allocates kpp request
1694e5f2c40SSalvatore Benedetto  *
1704e5f2c40SSalvatore Benedetto  * @tfm:	KPP tfm handle allocated with crypto_alloc_kpp()
1714e5f2c40SSalvatore Benedetto  * @gfp:	allocation flags
1724e5f2c40SSalvatore Benedetto  *
1734e5f2c40SSalvatore Benedetto  * Return: allocated handle in case of success or NULL in case of an error.
1744e5f2c40SSalvatore Benedetto  */
1754e5f2c40SSalvatore Benedetto static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
1764e5f2c40SSalvatore Benedetto 						    gfp_t gfp)
1774e5f2c40SSalvatore Benedetto {
1784e5f2c40SSalvatore Benedetto 	struct kpp_request *req;
1794e5f2c40SSalvatore Benedetto 
1804e5f2c40SSalvatore Benedetto 	req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
1814e5f2c40SSalvatore Benedetto 	if (likely(req))
1824e5f2c40SSalvatore Benedetto 		kpp_request_set_tfm(req, tfm);
1834e5f2c40SSalvatore Benedetto 
1844e5f2c40SSalvatore Benedetto 	return req;
1854e5f2c40SSalvatore Benedetto }
1864e5f2c40SSalvatore Benedetto 
1874e5f2c40SSalvatore Benedetto /**
1884e5f2c40SSalvatore Benedetto  * kpp_request_free() - zeroize and free kpp request
1894e5f2c40SSalvatore Benedetto  *
1904e5f2c40SSalvatore Benedetto  * @req:	request to free
1914e5f2c40SSalvatore Benedetto  */
1924e5f2c40SSalvatore Benedetto static inline void kpp_request_free(struct kpp_request *req)
1934e5f2c40SSalvatore Benedetto {
194453431a5SWaiman Long 	kfree_sensitive(req);
1954e5f2c40SSalvatore Benedetto }
1964e5f2c40SSalvatore Benedetto 
1974e5f2c40SSalvatore Benedetto /**
1984e5f2c40SSalvatore Benedetto  * kpp_request_set_callback() - Sets an asynchronous callback.
1994e5f2c40SSalvatore Benedetto  *
2004e5f2c40SSalvatore Benedetto  * Callback will be called when an asynchronous operation on a given
2014e5f2c40SSalvatore Benedetto  * request is finished.
2024e5f2c40SSalvatore Benedetto  *
2034e5f2c40SSalvatore Benedetto  * @req:	request that the callback will be set for
2044e5f2c40SSalvatore Benedetto  * @flgs:	specify for instance if the operation may backlog
2054e5f2c40SSalvatore Benedetto  * @cmpl:	callback which will be called
2064e5f2c40SSalvatore Benedetto  * @data:	private data used by the caller
2074e5f2c40SSalvatore Benedetto  */
2084e5f2c40SSalvatore Benedetto static inline void kpp_request_set_callback(struct kpp_request *req,
2094e5f2c40SSalvatore Benedetto 					    u32 flgs,
2104e5f2c40SSalvatore Benedetto 					    crypto_completion_t cmpl,
2114e5f2c40SSalvatore Benedetto 					    void *data)
2124e5f2c40SSalvatore Benedetto {
2134e5f2c40SSalvatore Benedetto 	req->base.complete = cmpl;
2144e5f2c40SSalvatore Benedetto 	req->base.data = data;
2154e5f2c40SSalvatore Benedetto 	req->base.flags = flgs;
2164e5f2c40SSalvatore Benedetto }
2174e5f2c40SSalvatore Benedetto 
2184e5f2c40SSalvatore Benedetto /**
2194e5f2c40SSalvatore Benedetto  * kpp_request_set_input() - Sets input buffer
2204e5f2c40SSalvatore Benedetto  *
2214e5f2c40SSalvatore Benedetto  * Sets parameters required by generate_public_key
2224e5f2c40SSalvatore Benedetto  *
2234e5f2c40SSalvatore Benedetto  * @req:	kpp request
2244e5f2c40SSalvatore Benedetto  * @input:	ptr to input scatter list
2254e5f2c40SSalvatore Benedetto  * @input_len:	size of the input scatter list
2264e5f2c40SSalvatore Benedetto  */
2274e5f2c40SSalvatore Benedetto static inline void kpp_request_set_input(struct kpp_request *req,
2284e5f2c40SSalvatore Benedetto 					 struct scatterlist *input,
2294e5f2c40SSalvatore Benedetto 					 unsigned int input_len)
2304e5f2c40SSalvatore Benedetto {
2314e5f2c40SSalvatore Benedetto 	req->src = input;
2324e5f2c40SSalvatore Benedetto 	req->src_len = input_len;
2334e5f2c40SSalvatore Benedetto }
2344e5f2c40SSalvatore Benedetto 
2354e5f2c40SSalvatore Benedetto /**
2364e5f2c40SSalvatore Benedetto  * kpp_request_set_output() - Sets output buffer
2374e5f2c40SSalvatore Benedetto  *
2384e5f2c40SSalvatore Benedetto  * Sets parameters required by kpp operation
2394e5f2c40SSalvatore Benedetto  *
2404e5f2c40SSalvatore Benedetto  * @req:	kpp request
2414e5f2c40SSalvatore Benedetto  * @output:	ptr to output scatter list
2424e5f2c40SSalvatore Benedetto  * @output_len:	size of the output scatter list
2434e5f2c40SSalvatore Benedetto  */
2444e5f2c40SSalvatore Benedetto static inline void kpp_request_set_output(struct kpp_request *req,
2454e5f2c40SSalvatore Benedetto 					  struct scatterlist *output,
2464e5f2c40SSalvatore Benedetto 					  unsigned int output_len)
2474e5f2c40SSalvatore Benedetto {
2484e5f2c40SSalvatore Benedetto 	req->dst = output;
2494e5f2c40SSalvatore Benedetto 	req->dst_len = output_len;
2504e5f2c40SSalvatore Benedetto }
2514e5f2c40SSalvatore Benedetto 
2524e5f2c40SSalvatore Benedetto enum {
2534e5f2c40SSalvatore Benedetto 	CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
254802c7f1cSSalvatore Benedetto 	CRYPTO_KPP_SECRET_TYPE_DH,
2553c4b2390SSalvatore Benedetto 	CRYPTO_KPP_SECRET_TYPE_ECDH,
2564e5f2c40SSalvatore Benedetto };
2574e5f2c40SSalvatore Benedetto 
2584e5f2c40SSalvatore Benedetto /**
2594e5f2c40SSalvatore Benedetto  * struct kpp_secret - small header for packing secret buffer
2604e5f2c40SSalvatore Benedetto  *
2614e5f2c40SSalvatore Benedetto  * @type:	define type of secret. Each kpp type will define its own
2624e5f2c40SSalvatore Benedetto  * @len:	specify the len of the secret, include the header, that
2634e5f2c40SSalvatore Benedetto  *		follows the struct
2644e5f2c40SSalvatore Benedetto  */
2654e5f2c40SSalvatore Benedetto struct kpp_secret {
2664e5f2c40SSalvatore Benedetto 	unsigned short type;
2674e5f2c40SSalvatore Benedetto 	unsigned short len;
2684e5f2c40SSalvatore Benedetto };
2694e5f2c40SSalvatore Benedetto 
2704e5f2c40SSalvatore Benedetto /**
2714e5f2c40SSalvatore Benedetto  * crypto_kpp_set_secret() - Invoke kpp operation
2724e5f2c40SSalvatore Benedetto  *
2734e5f2c40SSalvatore Benedetto  * Function invokes the specific kpp operation for a given alg.
2744e5f2c40SSalvatore Benedetto  *
2754e5f2c40SSalvatore Benedetto  * @tfm:	tfm handle
2768d23da22SStephan Mueller  * @buffer:	Buffer holding the packet representation of the private
2778d23da22SStephan Mueller  *		key. The structure of the packet key depends on the particular
2788d23da22SStephan Mueller  *		KPP implementation. Packing and unpacking helpers are provided
2798d23da22SStephan Mueller  *		for ECDH and DH (see the respective header files for those
2808d23da22SStephan Mueller  *		implementations).
2818d23da22SStephan Mueller  * @len:	Length of the packet private key buffer.
2824e5f2c40SSalvatore Benedetto  *
2834e5f2c40SSalvatore Benedetto  * Return: zero on success; error code in case of error
2844e5f2c40SSalvatore Benedetto  */
2855527dfb6SEric Biggers static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
2865527dfb6SEric Biggers 					const void *buffer, unsigned int len)
2874e5f2c40SSalvatore Benedetto {
2884e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
289f7d76e05SCorentin Labbe 	struct crypto_alg *calg = tfm->base.__crt_alg;
290cac5818cSCorentin Labbe 	int ret;
2914e5f2c40SSalvatore Benedetto 
292f7d76e05SCorentin Labbe 	crypto_stats_get(calg);
293cac5818cSCorentin Labbe 	ret = alg->set_secret(tfm, buffer, len);
294f7d76e05SCorentin Labbe 	crypto_stats_kpp_set_secret(calg, ret);
295cac5818cSCorentin Labbe 	return ret;
2964e5f2c40SSalvatore Benedetto }
2974e5f2c40SSalvatore Benedetto 
2984e5f2c40SSalvatore Benedetto /**
2994e5f2c40SSalvatore Benedetto  * crypto_kpp_generate_public_key() - Invoke kpp operation
3004e5f2c40SSalvatore Benedetto  *
3014e5f2c40SSalvatore Benedetto  * Function invokes the specific kpp operation for generating the public part
3028d23da22SStephan Mueller  * for a given kpp algorithm.
3038d23da22SStephan Mueller  *
3048d23da22SStephan Mueller  * To generate a private key, the caller should use a random number generator.
3058d23da22SStephan Mueller  * The output of the requested length serves as the private key.
3064e5f2c40SSalvatore Benedetto  *
3074e5f2c40SSalvatore Benedetto  * @req:	kpp key request
3084e5f2c40SSalvatore Benedetto  *
3094e5f2c40SSalvatore Benedetto  * Return: zero on success; error code in case of error
3104e5f2c40SSalvatore Benedetto  */
3114e5f2c40SSalvatore Benedetto static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
3124e5f2c40SSalvatore Benedetto {
3134e5f2c40SSalvatore Benedetto 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
3144e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
315f7d76e05SCorentin Labbe 	struct crypto_alg *calg = tfm->base.__crt_alg;
316cac5818cSCorentin Labbe 	int ret;
3174e5f2c40SSalvatore Benedetto 
318f7d76e05SCorentin Labbe 	crypto_stats_get(calg);
319cac5818cSCorentin Labbe 	ret = alg->generate_public_key(req);
320f7d76e05SCorentin Labbe 	crypto_stats_kpp_generate_public_key(calg, ret);
321cac5818cSCorentin Labbe 	return ret;
3224e5f2c40SSalvatore Benedetto }
3234e5f2c40SSalvatore Benedetto 
3244e5f2c40SSalvatore Benedetto /**
3254e5f2c40SSalvatore Benedetto  * crypto_kpp_compute_shared_secret() - Invoke kpp operation
3264e5f2c40SSalvatore Benedetto  *
3274e5f2c40SSalvatore Benedetto  * Function invokes the specific kpp operation for computing the shared secret
3284e5f2c40SSalvatore Benedetto  * for a given kpp algorithm.
3294e5f2c40SSalvatore Benedetto  *
3304e5f2c40SSalvatore Benedetto  * @req:	kpp key request
3314e5f2c40SSalvatore Benedetto  *
3324e5f2c40SSalvatore Benedetto  * Return: zero on success; error code in case of error
3334e5f2c40SSalvatore Benedetto  */
3344e5f2c40SSalvatore Benedetto static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
3354e5f2c40SSalvatore Benedetto {
3364e5f2c40SSalvatore Benedetto 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
3374e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
338f7d76e05SCorentin Labbe 	struct crypto_alg *calg = tfm->base.__crt_alg;
339cac5818cSCorentin Labbe 	int ret;
3404e5f2c40SSalvatore Benedetto 
341f7d76e05SCorentin Labbe 	crypto_stats_get(calg);
342cac5818cSCorentin Labbe 	ret = alg->compute_shared_secret(req);
343f7d76e05SCorentin Labbe 	crypto_stats_kpp_compute_shared_secret(calg, ret);
344cac5818cSCorentin Labbe 	return ret;
3454e5f2c40SSalvatore Benedetto }
3464e5f2c40SSalvatore Benedetto 
3474e5f2c40SSalvatore Benedetto /**
3484e5f2c40SSalvatore Benedetto  * crypto_kpp_maxsize() - Get len for output buffer
3494e5f2c40SSalvatore Benedetto  *
350c444b8daSTudor-Dan Ambarus  * Function returns the output buffer size required for a given key.
351c444b8daSTudor-Dan Ambarus  * Function assumes that the key is already set in the transformation. If this
352c444b8daSTudor-Dan Ambarus  * function is called without a setkey or with a failed setkey, you will end up
353c444b8daSTudor-Dan Ambarus  * in a NULL dereference.
3544e5f2c40SSalvatore Benedetto  *
3554e5f2c40SSalvatore Benedetto  * @tfm:	KPP tfm handle allocated with crypto_alloc_kpp()
3564e5f2c40SSalvatore Benedetto  */
357c444b8daSTudor-Dan Ambarus static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
3584e5f2c40SSalvatore Benedetto {
3594e5f2c40SSalvatore Benedetto 	struct kpp_alg *alg = crypto_kpp_alg(tfm);
3604e5f2c40SSalvatore Benedetto 
3614e5f2c40SSalvatore Benedetto 	return alg->max_size(tfm);
3624e5f2c40SSalvatore Benedetto }
3634e5f2c40SSalvatore Benedetto 
3644e5f2c40SSalvatore Benedetto #endif
365