1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Key-agreement Protocol Primitives (KPP)
5 * Copyright (c) 2016, Intel Corporation
11 #include <linux/crypto.h>
16 * @base: Common attributes for async crypto requests
18 * @dst: Destination data
19 * @src_len: Size of the input buffer
20 * @dst_len: Size of the output buffer. It needs to be at least
21 * as big as the expected result depending on the operation
22 * After operation it will be updated with the actual size of the
23 * result. In case of error where the dst sgl size was insufficient,
24 * it will be updated to the size required for the operation.
25 * @__ctx: Start of private context data
28 struct crypto_async_request base;
29 struct scatterlist *src;
30 struct scatterlist *dst;
33 void *__ctx[] CRYPTO_MINALIGN_ATTR;
37 * struct crypto_kpp - user-instantiated object which encapsulate
38 * algorithms and core processing logic
40 * @base: Common crypto API algorithm data structure
43 struct crypto_tfm base;
47 * struct kpp_alg - generic key-agreement protocol primitives
49 * @set_secret: Function invokes the protocol specific function to
50 * store the secret private key along with parameters.
51 * The implementation knows how to decode the buffer
52 * @generate_public_key: Function generate the public key to be sent to the
53 * counterpart. In case of error, where output is not big
54 * enough req->dst_len will be updated to the size
56 * @compute_shared_secret: Function compute the shared secret as defined by
57 * the algorithm. The result is given back to the user.
58 * In case of error, where output is not big enough,
59 * req->dst_len will be updated to the size required
60 * @max_size: Function returns the size of the output buffer
61 * @init: Initialize the object. This is called only once at
62 * instantiation time. In case the cryptographic hardware
63 * needs to be initialized. Software fallback should be
65 * @exit: Undo everything @init did.
67 * @reqsize: Request context size required by algorithm
69 * @base: Common crypto API algorithm data structure
72 int (*set_secret)(struct crypto_kpp *tfm, const void *buffer,
74 int (*generate_public_key)(struct kpp_request *req);
75 int (*compute_shared_secret)(struct kpp_request *req);
77 unsigned int (*max_size)(struct crypto_kpp *tfm);
79 int (*init)(struct crypto_kpp *tfm);
80 void (*exit)(struct crypto_kpp *tfm);
83 struct crypto_alg base;
87 * DOC: Generic Key-agreement Protocol Primitives API
89 * The KPP API is used with the algorithm type
90 * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
94 * crypto_alloc_kpp() - allocate KPP tfm handle
95 * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
96 * @type: specifies the type of the algorithm
97 * @mask: specifies the mask for the algorithm
99 * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
100 * is required for any following API invocation
102 * Return: allocated handle in case of success; IS_ERR() is true in case of
103 * an error, PTR_ERR() returns the error code.
105 struct crypto_kpp *crypto_alloc_kpp(const char *alg_name, u32 type, u32 mask);
107 int crypto_has_kpp(const char *alg_name, u32 type, u32 mask);
109 static inline struct crypto_tfm *crypto_kpp_tfm(struct crypto_kpp *tfm)
114 static inline struct kpp_alg *__crypto_kpp_alg(struct crypto_alg *alg)
116 return container_of(alg, struct kpp_alg, base);
119 static inline struct crypto_kpp *__crypto_kpp_tfm(struct crypto_tfm *tfm)
121 return container_of(tfm, struct crypto_kpp, base);
124 static inline struct kpp_alg *crypto_kpp_alg(struct crypto_kpp *tfm)
126 return __crypto_kpp_alg(crypto_kpp_tfm(tfm)->__crt_alg);
129 static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp *tfm)
131 return crypto_kpp_alg(tfm)->reqsize;
134 static inline void kpp_request_set_tfm(struct kpp_request *req,
135 struct crypto_kpp *tfm)
137 req->base.tfm = crypto_kpp_tfm(tfm);
140 static inline struct crypto_kpp *crypto_kpp_reqtfm(struct kpp_request *req)
142 return __crypto_kpp_tfm(req->base.tfm);
145 static inline u32 crypto_kpp_get_flags(struct crypto_kpp *tfm)
147 return crypto_tfm_get_flags(crypto_kpp_tfm(tfm));
150 static inline void crypto_kpp_set_flags(struct crypto_kpp *tfm, u32 flags)
152 crypto_tfm_set_flags(crypto_kpp_tfm(tfm), flags);
156 * crypto_free_kpp() - free KPP tfm handle
158 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
160 * If @tfm is a NULL or error pointer, this function does nothing.
162 static inline void crypto_free_kpp(struct crypto_kpp *tfm)
164 crypto_destroy_tfm(tfm, crypto_kpp_tfm(tfm));
168 * kpp_request_alloc() - allocates kpp request
170 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
171 * @gfp: allocation flags
173 * Return: allocated handle in case of success or NULL in case of an error.
175 static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
178 struct kpp_request *req;
180 req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
182 kpp_request_set_tfm(req, tfm);
188 * kpp_request_free() - zeroize and free kpp request
190 * @req: request to free
192 static inline void kpp_request_free(struct kpp_request *req)
194 kfree_sensitive(req);
198 * kpp_request_set_callback() - Sets an asynchronous callback.
200 * Callback will be called when an asynchronous operation on a given
201 * request is finished.
203 * @req: request that the callback will be set for
204 * @flgs: specify for instance if the operation may backlog
205 * @cmpl: callback which will be called
206 * @data: private data used by the caller
208 static inline void kpp_request_set_callback(struct kpp_request *req,
210 crypto_completion_t cmpl,
213 req->base.complete = cmpl;
214 req->base.data = data;
215 req->base.flags = flgs;
219 * kpp_request_set_input() - Sets input buffer
221 * Sets parameters required by generate_public_key
224 * @input: ptr to input scatter list
225 * @input_len: size of the input scatter list
227 static inline void kpp_request_set_input(struct kpp_request *req,
228 struct scatterlist *input,
229 unsigned int input_len)
232 req->src_len = input_len;
236 * kpp_request_set_output() - Sets output buffer
238 * Sets parameters required by kpp operation
241 * @output: ptr to output scatter list
242 * @output_len: size of the output scatter list
244 static inline void kpp_request_set_output(struct kpp_request *req,
245 struct scatterlist *output,
246 unsigned int output_len)
249 req->dst_len = output_len;
253 CRYPTO_KPP_SECRET_TYPE_UNKNOWN,
254 CRYPTO_KPP_SECRET_TYPE_DH,
255 CRYPTO_KPP_SECRET_TYPE_ECDH,
259 * struct kpp_secret - small header for packing secret buffer
261 * @type: define type of secret. Each kpp type will define its own
262 * @len: specify the len of the secret, include the header, that
271 * crypto_kpp_set_secret() - Invoke kpp operation
273 * Function invokes the specific kpp operation for a given alg.
276 * @buffer: Buffer holding the packet representation of the private
277 * key. The structure of the packet key depends on the particular
278 * KPP implementation. Packing and unpacking helpers are provided
279 * for ECDH and DH (see the respective header files for those
281 * @len: Length of the packet private key buffer.
283 * Return: zero on success; error code in case of error
285 static inline int crypto_kpp_set_secret(struct crypto_kpp *tfm,
286 const void *buffer, unsigned int len)
288 struct kpp_alg *alg = crypto_kpp_alg(tfm);
289 struct crypto_alg *calg = tfm->base.__crt_alg;
292 crypto_stats_get(calg);
293 ret = alg->set_secret(tfm, buffer, len);
294 crypto_stats_kpp_set_secret(calg, ret);
299 * crypto_kpp_generate_public_key() - Invoke kpp operation
301 * Function invokes the specific kpp operation for generating the public part
302 * for a given kpp algorithm.
304 * To generate a private key, the caller should use a random number generator.
305 * The output of the requested length serves as the private key.
307 * @req: kpp key request
309 * Return: zero on success; error code in case of error
311 static inline int crypto_kpp_generate_public_key(struct kpp_request *req)
313 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
314 struct kpp_alg *alg = crypto_kpp_alg(tfm);
315 struct crypto_alg *calg = tfm->base.__crt_alg;
318 crypto_stats_get(calg);
319 ret = alg->generate_public_key(req);
320 crypto_stats_kpp_generate_public_key(calg, ret);
325 * crypto_kpp_compute_shared_secret() - Invoke kpp operation
327 * Function invokes the specific kpp operation for computing the shared secret
328 * for a given kpp algorithm.
330 * @req: kpp key request
332 * Return: zero on success; error code in case of error
334 static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req)
336 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
337 struct kpp_alg *alg = crypto_kpp_alg(tfm);
338 struct crypto_alg *calg = tfm->base.__crt_alg;
341 crypto_stats_get(calg);
342 ret = alg->compute_shared_secret(req);
343 crypto_stats_kpp_compute_shared_secret(calg, ret);
348 * crypto_kpp_maxsize() - Get len for output buffer
350 * Function returns the output buffer size required for a given key.
351 * Function assumes that the key is already set in the transformation. If this
352 * function is called without a setkey or with a failed setkey, you will end up
353 * in a NULL dereference.
355 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
357 static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp *tfm)
359 struct kpp_alg *alg = crypto_kpp_alg(tfm);
361 return alg->max_size(tfm);