/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Key-agreement Protocol Primitives (KPP)3*4* Copyright (c) 2016, Intel Corporation5* Authors: Salvatore Benedetto <[email protected]>6*/78#ifndef _CRYPTO_KPP_9#define _CRYPTO_KPP_1011#include <linux/atomic.h>12#include <linux/container_of.h>13#include <linux/crypto.h>14#include <linux/slab.h>1516/**17* struct kpp_request18*19* @base: Common attributes for async crypto requests20* @src: Source data21* @dst: Destination data22* @src_len: Size of the input buffer23* @dst_len: Size of the output buffer. It needs to be at least24* as big as the expected result depending on the operation25* After operation it will be updated with the actual size of the26* 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 data29*/30struct kpp_request {31struct crypto_async_request base;32struct scatterlist *src;33struct scatterlist *dst;34unsigned int src_len;35unsigned int dst_len;36void *__ctx[] CRYPTO_MINALIGN_ATTR;37};3839/**40* struct crypto_kpp - user-instantiated object which encapsulate41* algorithms and core processing logic42*43* @reqsize: Request context size required by algorithm44* implementation45* @base: Common crypto API algorithm data structure46*/47struct crypto_kpp {48unsigned int reqsize;4950struct crypto_tfm base;51};5253/**54* struct kpp_alg - generic key-agreement protocol primitives55*56* @set_secret: Function invokes the protocol specific function to57* store the secret private key along with parameters.58* The implementation knows how to decode the buffer59* @generate_public_key: Function generate the public key to be sent to the60* counterpart. In case of error, where output is not big61* enough req->dst_len will be updated to the size62* required63* @compute_shared_secret: Function compute the shared secret as defined by64* the algorithm. The result is given back to the user.65* In case of error, where output is not big enough,66* req->dst_len will be updated to the size required67* @max_size: Function returns the size of the output buffer68* @init: Initialize the object. This is called only once at69* instantiation time. In case the cryptographic hardware70* needs to be initialized. Software fallback should be71* put in place here.72* @exit: Undo everything @init did.73*74* @base: Common crypto API algorithm data structure75*/76struct kpp_alg {77int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,78unsigned int len);79int (*generate_public_key)(struct kpp_request *req);80int (*compute_shared_secret)(struct kpp_request *req);8182unsigned int (*max_size)(struct crypto_kpp *tfm);8384int (*init)(struct crypto_kpp *tfm);85void (*exit)(struct crypto_kpp *tfm);8687struct crypto_alg base;88};8990/**91* DOC: Generic Key-agreement Protocol Primitives API92*93* The KPP API is used with the algorithm type94* CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)95*/9697/**98* crypto_alloc_kpp() - allocate KPP tfm handle99* @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")100* @type: specifies the type of the algorithm101* @mask: specifies the mask for the algorithm102*103* Allocate a handle for kpp algorithm. The returned struct crypto_kpp104* is required for any following API invocation105*106* Return: allocated handle in case of success; IS_ERR() is true in case of107* an error, PTR_ERR() returns the error code.108*/109struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);110111int crypto_has_kpp(const char *alg_name, u32 type, u32 mask);112113static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)114{115return &tfm->base;116}117118static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)119{120return container_of(alg, struct kpp_alg, base);121}122123static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)124{125return container_of(tfm, struct crypto_kpp, base);126}127128static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)129{130return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);131}132133static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)134{135return tfm->reqsize;136}137138static inline void kpp_request_set_tfm(struct kpp_request *req,139struct crypto_kpp *tfm)140{141req->base.tfm = crypto_kpp_tfm(tfm);142}143144static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)145{146return __crypto_kpp_tfm(req->base.tfm);147}148149static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)150{151return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));152}153154static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)155{156crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);157}158159/**160* crypto_free_kpp() - free KPP tfm handle161*162* @tfm: KPP tfm handle allocated with crypto_alloc_kpp()163*164* If @tfm is a NULL or error pointer, this function does nothing.165*/166static inline void crypto_free_kpp(struct crypto_kpp *tfm)167{168crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));169}170171/**172* kpp_request_alloc() - allocates kpp request173*174* @tfm: KPP tfm handle allocated with crypto_alloc_kpp()175* @gfp: allocation flags176*177* Return: allocated handle in case of success or NULL in case of an error.178*/179static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,180gfp_t gfp)181{182struct kpp_request *req;183184req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);185if (likely(req))186kpp_request_set_tfm(req, tfm);187188return req;189}190191/**192* kpp_request_free() - zeroize and free kpp request193*194* @req: request to free195*/196static inline void kpp_request_free(struct kpp_request *req)197{198kfree_sensitive(req);199}200201/**202* kpp_request_set_callback() - Sets an asynchronous callback.203*204* Callback will be called when an asynchronous operation on a given205* request is finished.206*207* @req: request that the callback will be set for208* @flgs: specify for instance if the operation may backlog209* @cmpl: callback which will be called210* @data: private data used by the caller211*/212static inline void kpp_request_set_callback(struct kpp_request *req,213u32 flgs,214crypto_completion_t cmpl,215void *data)216{217req->base.complete = cmpl;218req->base.data = data;219req->base.flags = flgs;220}221222/**223* kpp_request_set_input() - Sets input buffer224*225* Sets parameters required by generate_public_key226*227* @req: kpp request228* @input: ptr to input scatter list229* @input_len: size of the input scatter list230*/231static inline void kpp_request_set_input(struct kpp_request *req,232struct scatterlist *input,233unsigned int input_len)234{235req->src = input;236req->src_len = input_len;237}238239/**240* kpp_request_set_output() - Sets output buffer241*242* Sets parameters required by kpp operation243*244* @req: kpp request245* @output: ptr to output scatter list246* @output_len: size of the output scatter list247*/248static inline void kpp_request_set_output(struct kpp_request *req,249struct scatterlist *output,250unsigned int output_len)251{252req->dst = output;253req->dst_len = output_len;254}255256enum {257CRYPTO_KPP_SECRET_TYPE_UNKNOWN,258CRYPTO_KPP_SECRET_TYPE_DH,259CRYPTO_KPP_SECRET_TYPE_ECDH,260};261262/**263* struct kpp_secret - small header for packing secret buffer264*265* @type: define type of secret. Each kpp type will define its own266* @len: specify the len of the secret, include the header, that267* follows the struct268*/269struct kpp_secret {270unsigned short type;271unsigned short len;272};273274/**275* crypto_kpp_set_secret() - Invoke kpp operation276*277* Function invokes the specific kpp operation for a given alg.278*279* @tfm: tfm handle280* @buffer: Buffer holding the packet representation of the private281* key. The structure of the packet key depends on the particular282* KPP implementation. Packing and unpacking helpers are provided283* for ECDH and DH (see the respective header files for those284* implementations).285* @len: Length of the packet private key buffer.286*287* Return: zero on success; error code in case of error288*/289static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,290const void *buffer, unsigned int len)291{292return crypto_kpp_alg(tfm)->set_secret(tfm, buffer, len);293}294295/**296* crypto_kpp_generate_public_key() - Invoke kpp operation297*298* Function invokes the specific kpp operation for generating the public part299* for a given kpp algorithm.300*301* To generate a private key, the caller should use a random number generator.302* The output of the requested length serves as the private key.303*304* @req: kpp key request305*306* Return: zero on success; error code in case of error307*/308static inline int crypto_kpp_generate_public_key(struct kpp_request *req)309{310struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);311312return crypto_kpp_alg(tfm)->generate_public_key(req);313}314315/**316* crypto_kpp_compute_shared_secret() - Invoke kpp operation317*318* Function invokes the specific kpp operation for computing the shared secret319* for a given kpp algorithm.320*321* @req: kpp key request322*323* Return: zero on success; error code in case of error324*/325static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)326{327struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);328329return crypto_kpp_alg(tfm)->compute_shared_secret(req);330}331332/**333* crypto_kpp_maxsize() - Get len for output buffer334*335* Function returns the output buffer size required for a given key.336* Function assumes that the key is already set in the transformation. If this337* function is called without a setkey or with a failed setkey, you will end up338* in a NULL dereference.339*340* @tfm: KPP tfm handle allocated with crypto_alloc_kpp()341*/342static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)343{344struct kpp_alg *alg = crypto_kpp_alg(tfm);345346return alg->max_size(tfm);347}348349#endif350351352