2 * Key-agreement Protocol Primitives (KPP)
4 * Copyright (c) 2016, Intel Corporation
5 * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <linux/crypto.h>
21 * @base: Common attributes for async crypto requests
23 * @dst: Destination data
24 * @src_len: Size of the input buffer
25 * @dst_len: Size of the output buffer. It needs to be at least
26 * as big as the expected result depending on the operation
27 * After operation it will be updated with the actual size of the
28 * result. In case of error where the dst sgl size was insufficient,
29 * it will be updated to the size required for the operation.
30 * @__ctx: Start of private context data
33 struct crypto_async_request base
;
34 struct scatterlist
*src
;
35 struct scatterlist
*dst
;
38 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
42 * struct crypto_kpp - user-instantiated object which encapsulate
43 * algorithms and core processing logic
45 * @base: Common crypto API algorithm data structure
48 struct crypto_tfm base
;
52 * struct kpp_alg - generic key-agreement protocol primitives
54 * @set_secret: Function invokes the protocol specific function to
55 * store the secret private key along with parameters.
56 * The implementation knows how to decode thie buffer
57 * @generate_public_key: Function generate the public key to be sent to the
58 * counterpart. In case of error, where output is not big
59 * enough req->dst_len will be updated to the size
61 * @compute_shared_secret: Function compute the shared secret as defined by
62 * the algorithm. The result is given back to the user.
63 * In case of error, where output is not big enough,
64 * req->dst_len will be updated to the size required
65 * @max_size: Function returns the size of the output buffer
66 * @init: Initialize the object. This is called only once at
67 * instantiation time. In case the cryptographic hardware
68 * needs to be initialized. Software fallback should be
70 * @exit: Undo everything @init did.
72 * @reqsize: Request context size required by algorithm
74 * @base Common crypto API algorithm data structure
77 int (*set_secret
)(struct crypto_kpp
*tfm
, void *buffer
,
79 int (*generate_public_key
)(struct kpp_request
*req
);
80 int (*compute_shared_secret
)(struct kpp_request
*req
);
82 int (*max_size
)(struct crypto_kpp
*tfm
);
84 int (*init
)(struct crypto_kpp
*tfm
);
85 void (*exit
)(struct crypto_kpp
*tfm
);
88 struct crypto_alg base
;
92 * DOC: Generic Key-agreement Protocol Primitevs 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 requeried 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 static inline struct crypto_tfm
*crypto_kpp_tfm(struct crypto_kpp
*tfm
)
117 static inline struct kpp_alg
*__crypto_kpp_alg(struct crypto_alg
*alg
)
119 return container_of(alg
, struct kpp_alg
, base
);
122 static inline struct crypto_kpp
*__crypto_kpp_tfm(struct crypto_tfm
*tfm
)
124 return container_of(tfm
, struct crypto_kpp
, base
);
127 static inline struct kpp_alg
*crypto_kpp_alg(struct crypto_kpp
*tfm
)
129 return __crypto_kpp_alg(crypto_kpp_tfm(tfm
)->__crt_alg
);
132 static inline unsigned int crypto_kpp_reqsize(struct crypto_kpp
*tfm
)
134 return crypto_kpp_alg(tfm
)->reqsize
;
137 static inline void kpp_request_set_tfm(struct kpp_request
*req
,
138 struct crypto_kpp
*tfm
)
140 req
->base
.tfm
= crypto_kpp_tfm(tfm
);
143 static inline struct crypto_kpp
*crypto_kpp_reqtfm(struct kpp_request
*req
)
145 return __crypto_kpp_tfm(req
->base
.tfm
);
149 * crypto_free_kpp() - free KPP tfm handle
151 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
153 static inline void crypto_free_kpp(struct crypto_kpp
*tfm
)
155 crypto_destroy_tfm(tfm
, crypto_kpp_tfm(tfm
));
159 * kpp_request_alloc() - allocates kpp request
161 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
162 * @gfp: allocation flags
164 * Return: allocated handle in case of success or NULL in case of an error.
166 static inline struct kpp_request
*kpp_request_alloc(struct crypto_kpp
*tfm
,
169 struct kpp_request
*req
;
171 req
= kmalloc(sizeof(*req
) + crypto_kpp_reqsize(tfm
), gfp
);
173 kpp_request_set_tfm(req
, tfm
);
179 * kpp_request_free() - zeroize and free kpp request
181 * @req: request to free
183 static inline void kpp_request_free(struct kpp_request
*req
)
189 * kpp_request_set_callback() - Sets an asynchronous callback.
191 * Callback will be called when an asynchronous operation on a given
192 * request is finished.
194 * @req: request that the callback will be set for
195 * @flgs: specify for instance if the operation may backlog
196 * @cmpl: callback which will be called
197 * @data: private data used by the caller
199 static inline void kpp_request_set_callback(struct kpp_request
*req
,
201 crypto_completion_t cmpl
,
204 req
->base
.complete
= cmpl
;
205 req
->base
.data
= data
;
206 req
->base
.flags
= flgs
;
210 * kpp_request_set_input() - Sets input buffer
212 * Sets parameters required by generate_public_key
215 * @input: ptr to input scatter list
216 * @input_len: size of the input scatter list
218 static inline void kpp_request_set_input(struct kpp_request
*req
,
219 struct scatterlist
*input
,
220 unsigned int input_len
)
223 req
->src_len
= input_len
;
227 * kpp_request_set_output() - Sets output buffer
229 * Sets parameters required by kpp operation
232 * @output: ptr to output scatter list
233 * @output_len: size of the output scatter list
235 static inline void kpp_request_set_output(struct kpp_request
*req
,
236 struct scatterlist
*output
,
237 unsigned int output_len
)
240 req
->dst_len
= output_len
;
244 CRYPTO_KPP_SECRET_TYPE_UNKNOWN
,
245 CRYPTO_KPP_SECRET_TYPE_DH
,
246 CRYPTO_KPP_SECRET_TYPE_ECDH
,
250 * struct kpp_secret - small header for packing secret buffer
252 * @type: define type of secret. Each kpp type will define its own
253 * @len: specify the len of the secret, include the header, that
262 * crypto_kpp_set_secret() - Invoke kpp operation
264 * Function invokes the specific kpp operation for a given alg.
268 * Return: zero on success; error code in case of error
270 static inline int crypto_kpp_set_secret(struct crypto_kpp
*tfm
, void *buffer
,
273 struct kpp_alg
*alg
= crypto_kpp_alg(tfm
);
275 return alg
->set_secret(tfm
, buffer
, len
);
279 * crypto_kpp_generate_public_key() - Invoke kpp operation
281 * Function invokes the specific kpp operation for generating the public part
282 * for a given kpp algorithm
284 * @req: kpp key request
286 * Return: zero on success; error code in case of error
288 static inline int crypto_kpp_generate_public_key(struct kpp_request
*req
)
290 struct crypto_kpp
*tfm
= crypto_kpp_reqtfm(req
);
291 struct kpp_alg
*alg
= crypto_kpp_alg(tfm
);
293 return alg
->generate_public_key(req
);
297 * crypto_kpp_compute_shared_secret() - Invoke kpp operation
299 * Function invokes the specific kpp operation for computing the shared secret
300 * for a given kpp algorithm.
302 * @req: kpp key request
304 * Return: zero on success; error code in case of error
306 static inline int crypto_kpp_compute_shared_secret(struct kpp_request
*req
)
308 struct crypto_kpp
*tfm
= crypto_kpp_reqtfm(req
);
309 struct kpp_alg
*alg
= crypto_kpp_alg(tfm
);
311 return alg
->compute_shared_secret(req
);
315 * crypto_kpp_maxsize() - Get len for output buffer
317 * Function returns the output buffer size required
319 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp()
321 * Return: minimum len for output buffer or error code if key hasn't been set
323 static inline int crypto_kpp_maxsize(struct crypto_kpp
*tfm
)
325 struct kpp_alg
*alg
= crypto_kpp_alg(tfm
);
327 return alg
->max_size(tfm
);