1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Key-agreement Protocol Primitives (KPP)
5 * Copyright (c) 2016, Intel Corporation
6 * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
12 #include <linux/atomic.h>
13 #include <linux/container_of.h>
14 #include <linux/crypto.h>
15 #include <linux/slab.h>
20 * @base: Common attributes for async crypto requests
22 * @dst: Destination data
23 * @src_len: Size of the input buffer
24 * @dst_len: Size of the output buffer. It needs to be at least
25 * as big as the expected result depending on the operation
26 * After operation it will be updated with the actual size of the
27 * result. In case of error where the dst sgl size was insufficient,
28 * it will be updated to the size required for the operation.
29 * @__ctx: Start of private context data
32 struct crypto_async_request base
;
33 struct scatterlist
*src
;
34 struct scatterlist
*dst
;
37 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
41 * struct crypto_kpp - user-instantiated object which encapsulate
42 * algorithms and core processing logic
44 * @reqsize: Request context size required by algorithm
46 * @base: Common crypto API algorithm data structure
51 struct crypto_tfm base
;
55 * struct kpp_alg - generic key-agreement protocol primitives
57 * @set_secret: Function invokes the protocol specific function to
58 * store the secret private key along with parameters.
59 * The implementation knows how to decode the buffer
60 * @generate_public_key: Function generate the public key to be sent to the
61 * counterpart. In case of error, where output is not big
62 * enough req->dst_len will be updated to the size
64 * @compute_shared_secret: Function compute the shared secret as defined by
65 * the algorithm. The result is given back to the user.
66 * In case of error, where output is not big enough,
67 * req->dst_len will be updated to the size required
68 * @max_size: Function returns the size of the output buffer
69 * @init: Initialize the object. This is called only once at
70 * instantiation time. In case the cryptographic hardware
71 * needs to be initialized. Software fallback should be
73 * @exit: Undo everything @init did.
75 * @base: Common crypto API algorithm data structure
78 int (*set_secret
)(struct crypto_kpp
*tfm
, const void *buffer
,
80 int (*generate_public_key
)(struct kpp_request
*req
);
81 int (*compute_shared_secret
)(struct kpp_request
*req
);
83 unsigned int (*max_size
)(struct crypto_kpp
*tfm
);
85 int (*init
)(struct crypto_kpp
*tfm
);
86 void (*exit
)(struct crypto_kpp
*tfm
);
88 struct crypto_alg base
;
92 * DOC: Generic Key-agreement Protocol Primitives API
94 * The KPP API is used with the algorithm type
95 * CRYPTO_ALG_TYPE_KPP (listed as type "kpp" in /proc/crypto)
99 * crypto_alloc_kpp() - allocate KPP tfm handle
100 * @alg_name: is the name of the kpp algorithm (e.g. "dh", "ecdh")
101 * @type: specifies the type of the algorithm
102 * @mask: specifies the mask for the algorithm
104 * Allocate a handle for kpp algorithm. The returned struct crypto_kpp
105 * is required for any following API invocation
107 * Return: allocated handle in case of success; IS_ERR() is true in case of
108 * an error, PTR_ERR() returns the error code.
110 struct crypto_kpp
*crypto_alloc_kpp(const char *alg_name
, u32 type
, u32 mask
);
112 int crypto_has_kpp(const char *alg_name
, u32 type
, u32 mask
);
114 static inline struct crypto_tfm
*crypto_kpp_tfm(struct crypto_kpp
*tfm
)
119 static inline struct kpp_alg
*__crypto_kpp_alg(struct crypto_alg
*alg
)
121 return container_of(alg
, struct kpp_alg
, base
);
124 static inline struct crypto_kpp
*__crypto_kpp_tfm(struct crypto_tfm
*tfm
)
126 return container_of(tfm
, struct crypto_kpp
, base
);
129 static inline struct kpp_alg
*crypto_kpp_alg(struct crypto_kpp
*tfm
)
131 return __crypto_kpp_alg(crypto_kpp_tfm(tfm
)->__crt_alg
);
134 static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp
*tfm
)
139 static inline void kpp_request_set_tfm(struct kpp_request
*req
,
140 struct crypto_kpp
*tfm
)
142 req
->base
.tfm
= crypto_kpp_tfm(tfm
);
145 static inline struct crypto_kpp
*crypto_kpp_reqtfm(struct kpp_request
*req
)
147 return __crypto_kpp_tfm(req
->base
.tfm
);
150 static inline u32
crypto_kpp_get_flags(struct crypto_kpp
*tfm
)
152 return crypto_tfm_get_flags(crypto_kpp_tfm(tfm
));
155 static inline void crypto_kpp_set_flags(struct crypto_kpp
*tfm
, u32 flags
)
157 crypto_tfm_set_flags(crypto_kpp_tfm(tfm
), flags
);
161 * crypto_free_kpp() - free KPP tfm handle
163 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
165 * If @tfm is a NULL or error pointer, this function does nothing.
167 static inline void crypto_free_kpp(struct crypto_kpp
*tfm
)
169 crypto_destroy_tfm(tfm
, crypto_kpp_tfm(tfm
));
173 * kpp_request_alloc() - allocates kpp request
175 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
176 * @gfp: allocation flags
178 * Return: allocated handle in case of success or NULL in case of an error.
180 static inline struct kpp_request
*kpp_request_alloc(struct crypto_kpp
*tfm
,
183 struct kpp_request
*req
;
185 req
= kmalloc(sizeof(*req
) + crypto_kpp_reqsize(tfm
), gfp
);
187 kpp_request_set_tfm(req
, tfm
);
193 * kpp_request_free() - zeroize and free kpp request
195 * @req: request to free
197 static inline void kpp_request_free(struct kpp_request
*req
)
199 kfree_sensitive(req
);
203 * kpp_request_set_callback() - Sets an asynchronous callback.
205 * Callback will be called when an asynchronous operation on a given
206 * request is finished.
208 * @req: request that the callback will be set for
209 * @flgs: specify for instance if the operation may backlog
210 * @cmpl: callback which will be called
211 * @data: private data used by the caller
213 static inline void kpp_request_set_callback(struct kpp_request
*req
,
215 crypto_completion_t cmpl
,
218 req
->base
.complete
= cmpl
;
219 req
->base
.data
= data
;
220 req
->base
.flags
= flgs
;
224 * kpp_request_set_input() - Sets input buffer
226 * Sets parameters required by generate_public_key
229 * @input: ptr to input scatter list
230 * @input_len: size of the input scatter list
232 static inline void kpp_request_set_input(struct kpp_request
*req
,
233 struct scatterlist
*input
,
234 unsigned int input_len
)
237 req
->src_len
= input_len
;
241 * kpp_request_set_output() - Sets output buffer
243 * Sets parameters required by kpp operation
246 * @output: ptr to output scatter list
247 * @output_len: size of the output scatter list
249 static inline void kpp_request_set_output(struct kpp_request
*req
,
250 struct scatterlist
*output
,
251 unsigned int output_len
)
254 req
->dst_len
= output_len
;
258 CRYPTO_KPP_SECRET_TYPE_UNKNOWN
,
259 CRYPTO_KPP_SECRET_TYPE_DH
,
260 CRYPTO_KPP_SECRET_TYPE_ECDH
,
264 * struct kpp_secret - small header for packing secret buffer
266 * @type: define type of secret. Each kpp type will define its own
267 * @len: specify the len of the secret, include the header, that
276 * crypto_kpp_set_secret() - Invoke kpp operation
278 * Function invokes the specific kpp operation for a given alg.
281 * @buffer: Buffer holding the packet representation of the private
282 * key. The structure of the packet key depends on the particular
283 * KPP implementation. Packing and unpacking helpers are provided
284 * for ECDH and DH (see the respective header files for those
286 * @len: Length of the packet private key buffer.
288 * Return: zero on success; error code in case of error
290 static inline int crypto_kpp_set_secret(struct crypto_kpp
*tfm
,
291 const void *buffer
, unsigned int len
)
293 return crypto_kpp_alg(tfm
)->set_secret(tfm
, buffer
, len
);
297 * crypto_kpp_generate_public_key() - Invoke kpp operation
299 * Function invokes the specific kpp operation for generating the public part
300 * for a given kpp algorithm.
302 * To generate a private key, the caller should use a random number generator.
303 * The output of the requested length serves as the private key.
305 * @req: kpp key request
307 * Return: zero on success; error code in case of error
309 static inline int crypto_kpp_generate_public_key(struct kpp_request
*req
)
311 struct crypto_kpp
*tfm
= crypto_kpp_reqtfm(req
);
313 return crypto_kpp_alg(tfm
)->generate_public_key(req
);
317 * crypto_kpp_compute_shared_secret() - Invoke kpp operation
319 * Function invokes the specific kpp operation for computing the shared secret
320 * for a given kpp algorithm.
322 * @req: kpp key request
324 * Return: zero on success; error code in case of error
326 static inline int crypto_kpp_compute_shared_secret(struct kpp_request
*req
)
328 struct crypto_kpp
*tfm
= crypto_kpp_reqtfm(req
);
330 return crypto_kpp_alg(tfm
)->compute_shared_secret(req
);
334 * crypto_kpp_maxsize() - Get len for output buffer
336 * Function returns the output buffer size required for a given key.
337 * Function assumes that the key is already set in the transformation. If this
338 * function is called without a setkey or with a failed setkey, you will end up
339 * in a NULL dereference.
341 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
343 static inline unsigned int crypto_kpp_maxsize(struct crypto_kpp
*tfm
)
345 struct kpp_alg
*alg
= crypto_kpp_alg(tfm
);
347 return alg
->max_size(tfm
);