Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Key-agreement Protocol Primitives (KPP)
0004  *
0005  * Copyright (c) 2016, Intel Corporation
0006  * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
0007  */
0008 
0009 #ifndef _CRYPTO_KPP_
0010 #define _CRYPTO_KPP_
0011 #include <linux/crypto.h>
0012 
0013 /**
0014  * struct kpp_request
0015  *
0016  * @base:   Common attributes for async crypto requests
0017  * @src:    Source data
0018  * @dst:    Destination data
0019  * @src_len:    Size of the input buffer
0020  * @dst_len:    Size of the output buffer. It needs to be at least
0021  *      as big as the expected result depending on the operation
0022  *      After operation it will be updated with the actual size of the
0023  *      result. In case of error where the dst sgl size was insufficient,
0024  *      it will be updated to the size required for the operation.
0025  * @__ctx:  Start of private context data
0026  */
0027 struct kpp_request {
0028     struct crypto_async_request base;
0029     struct scatterlist *src;
0030     struct scatterlist *dst;
0031     unsigned int src_len;
0032     unsigned int dst_len;
0033     void *__ctx[] CRYPTO_MINALIGN_ATTR;
0034 };
0035 
0036 /**
0037  * struct crypto_kpp - user-instantiated object which encapsulate
0038  * algorithms and core processing logic
0039  *
0040  * @base:   Common crypto API algorithm data structure
0041  */
0042 struct crypto_kpp {
0043     struct crypto_tfm base;
0044 };
0045 
0046 /**
0047  * struct kpp_alg - generic key-agreement protocol primitives
0048  *
0049  * @set_secret:     Function invokes the protocol specific function to
0050  *          store the secret private key along with parameters.
0051  *          The implementation knows how to decode the buffer
0052  * @generate_public_key: Function generate the public key to be sent to the
0053  *          counterpart. In case of error, where output is not big
0054  *          enough req->dst_len will be updated to the size
0055  *          required
0056  * @compute_shared_secret: Function compute the shared secret as defined by
0057  *          the algorithm. The result is given back to the user.
0058  *          In case of error, where output is not big enough,
0059  *          req->dst_len will be updated to the size required
0060  * @max_size:       Function returns the size of the output buffer
0061  * @init:       Initialize the object. This is called only once at
0062  *          instantiation time. In case the cryptographic hardware
0063  *          needs to be initialized. Software fallback should be
0064  *          put in place here.
0065  * @exit:       Undo everything @init did.
0066  *
0067  * @reqsize:        Request context size required by algorithm
0068  *          implementation
0069  * @base:       Common crypto API algorithm data structure
0070  */
0071 struct kpp_alg {
0072     int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
0073               unsigned int len);
0074     int (*generate_public_key)(struct kpp_request *req);
0075     int (*compute_shared_secret)(struct kpp_request *req);
0076 
0077     unsigned int (*max_size)(struct crypto_kpp *tfm);
0078 
0079     int (*init)(struct crypto_kpp *tfm);
0080     void (*exit)(struct crypto_kpp *tfm);
0081 
0082     unsigned int reqsize;
0083     struct crypto_alg base;
0084 };
0085 
0086 /**
0087  * DOC: Generic Key-agreement Protocol Primitives API
0088  *
0089  * The KPP API is used with the algorithm type
0090  * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
0091  */
0092 
0093 /**
0094  * crypto_alloc_kpp() - allocate KPP tfm handle
0095  * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
0096  * @type: specifies the type of the algorithm
0097  * @mask: specifies the mask for the algorithm
0098  *
0099  * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
0100  * is required for any following API invocation
0101  *
0102  * Return: allocated handle in case of success; IS_ERR() is true in case of
0103  *     an error, PTR_ERR() returns the error code.
0104  */
0105 struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
0106 
0107 int crypto_has_kpp(const char *alg_name, u32 type, u32 mask);
0108 
0109 static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
0110 {
0111     return &tfm->base;
0112 }
0113 
0114 static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
0115 {
0116     return container_of(alg, struct kpp_alg, base);
0117 }
0118 
0119 static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
0120 {
0121     return container_of(tfm, struct crypto_kpp, base);
0122 }
0123 
0124 static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
0125 {
0126     return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
0127 }
0128 
0129 static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
0130 {
0131     return crypto_kpp_alg(tfm)->reqsize;
0132 }
0133 
0134 static inline void kpp_request_set_tfm(struct kpp_request *req,
0135                        struct crypto_kpp *tfm)
0136 {
0137     req->base.tfm = crypto_kpp_tfm(tfm);
0138 }
0139 
0140 static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
0141 {
0142     return __crypto_kpp_tfm(req->base.tfm);
0143 }
0144 
0145 static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
0146 {
0147     return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
0148 }
0149 
0150 static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
0151 {
0152     crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
0153 }
0154 
0155 /**
0156  * crypto_free_kpp() - free KPP tfm handle
0157  *
0158  * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
0159  *
0160  * If @tfm is a NULL or error pointer, this function does nothing.
0161  */
0162 static inline void crypto_free_kpp(struct crypto_kpp *tfm)
0163 {
0164     crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
0165 }
0166 
0167 /**
0168  * kpp_request_alloc() - allocates kpp request
0169  *
0170  * @tfm:    KPP tfm handle allocated with crypto_alloc_kpp()
0171  * @gfp:    allocation flags
0172  *
0173  * Return: allocated handle in case of success or NULL in case of an error.
0174  */
0175 static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
0176                             gfp_t gfp)
0177 {
0178     struct kpp_request *req;
0179 
0180     req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
0181     if (likely(req))
0182         kpp_request_set_tfm(req, tfm);
0183 
0184     return req;
0185 }
0186 
0187 /**
0188  * kpp_request_free() - zeroize and free kpp request
0189  *
0190  * @req:    request to free
0191  */
0192 static inline void kpp_request_free(struct kpp_request *req)
0193 {
0194     kfree_sensitive(req);
0195 }
0196 
0197 /**
0198  * kpp_request_set_callback() - Sets an asynchronous callback.
0199  *
0200  * Callback will be called when an asynchronous operation on a given
0201  * request is finished.
0202  *
0203  * @req:    request that the callback will be set for
0204  * @flgs:   specify for instance if the operation may backlog
0205  * @cmpl:   callback which will be called
0206  * @data:   private data used by the caller
0207  */
0208 static inline void kpp_request_set_callback(struct kpp_request *req,
0209                         u32 flgs,
0210                         crypto_completion_t cmpl,
0211                         void *data)
0212 {
0213     req->base.complete = cmpl;
0214     req->base.data = data;
0215     req->base.flags = flgs;
0216 }
0217 
0218 /**
0219  * kpp_request_set_input() - Sets input buffer
0220  *
0221  * Sets parameters required by generate_public_key
0222  *
0223  * @req:    kpp request
0224  * @input:  ptr to input scatter list
0225  * @input_len:  size of the input scatter list
0226  */
0227 static inline void kpp_request_set_input(struct kpp_request *req,
0228                      struct scatterlist *input,
0229                      unsigned int input_len)
0230 {
0231     req->src = input;
0232     req->src_len = input_len;
0233 }
0234 
0235 /**
0236  * kpp_request_set_output() - Sets output buffer
0237  *
0238  * Sets parameters required by kpp operation
0239  *
0240  * @req:    kpp request
0241  * @output: ptr to output scatter list
0242  * @output_len: size of the output scatter list
0243  */
0244 static inline void kpp_request_set_output(struct kpp_request *req,
0245                       struct scatterlist *output,
0246                       unsigned int output_len)
0247 {
0248     req->dst = output;
0249     req->dst_len = output_len;
0250 }
0251 
0252 enum {
0253     CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
0254     CRYPTO_KPP_SECRET_TYPE_DH,
0255     CRYPTO_KPP_SECRET_TYPE_ECDH,
0256 };
0257 
0258 /**
0259  * struct kpp_secret - small header for packing secret buffer
0260  *
0261  * @type:   define type of secret. Each kpp type will define its own
0262  * @len:    specify the len of the secret, include the header, that
0263  *      follows the struct
0264  */
0265 struct kpp_secret {
0266     unsigned short type;
0267     unsigned short len;
0268 };
0269 
0270 /**
0271  * crypto_kpp_set_secret() - Invoke kpp operation
0272  *
0273  * Function invokes the specific kpp operation for a given alg.
0274  *
0275  * @tfm:    tfm handle
0276  * @buffer: Buffer holding the packet representation of the private
0277  *      key. The structure of the packet key depends on the particular
0278  *      KPP implementation. Packing and unpacking helpers are provided
0279  *      for ECDH and DH (see the respective header files for those
0280  *      implementations).
0281  * @len:    Length of the packet private key buffer.
0282  *
0283  * Return: zero on success; error code in case of error
0284  */
0285 static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
0286                     const void *buffer, unsigned int len)
0287 {
0288     struct kpp_alg *alg = crypto_kpp_alg(tfm);
0289     struct crypto_alg *calg = tfm->base.__crt_alg;
0290     int ret;
0291 
0292     crypto_stats_get(calg);
0293     ret = alg->set_secret(tfm, buffer, len);
0294     crypto_stats_kpp_set_secret(calg, ret);
0295     return ret;
0296 }
0297 
0298 /**
0299  * crypto_kpp_generate_public_key() - Invoke kpp operation
0300  *
0301  * Function invokes the specific kpp operation for generating the public part
0302  * for a given kpp algorithm.
0303  *
0304  * To generate a private key, the caller should use a random number generator.
0305  * The output of the requested length serves as the private key.
0306  *
0307  * @req:    kpp key request
0308  *
0309  * Return: zero on success; error code in case of error
0310  */
0311 static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
0312 {
0313     struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
0314     struct kpp_alg *alg = crypto_kpp_alg(tfm);
0315     struct crypto_alg *calg = tfm->base.__crt_alg;
0316     int ret;
0317 
0318     crypto_stats_get(calg);
0319     ret = alg->generate_public_key(req);
0320     crypto_stats_kpp_generate_public_key(calg, ret);
0321     return ret;
0322 }
0323 
0324 /**
0325  * crypto_kpp_compute_shared_secret() - Invoke kpp operation
0326  *
0327  * Function invokes the specific kpp operation for computing the shared secret
0328  * for a given kpp algorithm.
0329  *
0330  * @req:    kpp key request
0331  *
0332  * Return: zero on success; error code in case of error
0333  */
0334 static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
0335 {
0336     struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
0337     struct kpp_alg *alg = crypto_kpp_alg(tfm);
0338     struct crypto_alg *calg = tfm->base.__crt_alg;
0339     int ret;
0340 
0341     crypto_stats_get(calg);
0342     ret = alg->compute_shared_secret(req);
0343     crypto_stats_kpp_compute_shared_secret(calg, ret);
0344     return ret;
0345 }
0346 
0347 /**
0348  * crypto_kpp_maxsize() - Get len for output buffer
0349  *
0350  * Function returns the output buffer size required for a given key.
0351  * Function assumes that the key is already set in the transformation. If this
0352  * function is called without a setkey or with a failed setkey, you will end up
0353  * in a NULL dereference.
0354  *
0355  * @tfm:    KPP tfm handle allocated with crypto_alloc_kpp()
0356  */
0357 static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
0358 {
0359     struct kpp_alg *alg = crypto_kpp_alg(tfm);
0360 
0361     return alg->max_size(tfm);
0362 }
0363 
0364 #endif