1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Public Key Encryption
5 * Copyright (c) 2015, Intel Corporation
6 * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
8 #ifndef _CRYPTO_AKCIPHER_H
9 #define _CRYPTO_AKCIPHER_H
11 #include <linux/atomic.h>
12 #include <linux/crypto.h>
15 * struct akcipher_request - public key cipher request
17 * @base: Common attributes for async crypto requests
19 * @dst: Destination data
20 * @src_len: Size of the input buffer
21 * @dst_len: Size of @dst buffer
22 * It needs to be at least as big as the expected result
23 * depending on the operation.
24 * After operation it will be updated with the actual size of the
26 * 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 data
30 struct akcipher_request
{
31 struct crypto_async_request base
;
32 struct scatterlist
*src
;
33 struct scatterlist
*dst
;
36 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
40 * struct crypto_akcipher - user-instantiated objects which encapsulate
41 * algorithms and core processing logic
43 * @reqsize: Request context size required by algorithm implementation
44 * @base: Common crypto API algorithm data structure
46 struct crypto_akcipher
{
49 struct crypto_tfm base
;
53 * struct akcipher_alg - generic public key cipher algorithm
55 * @encrypt: Function performs an encrypt operation as defined by public key
56 * algorithm. In case of error, where the dst_len was insufficient,
57 * the req->dst_len will be updated to the size required for the
59 * @decrypt: Function performs a decrypt operation as defined by public key
60 * algorithm. In case of error, where the dst_len was insufficient,
61 * the req->dst_len will be updated to the size required for the
63 * @set_pub_key: Function invokes the algorithm specific set public key
64 * function, which knows how to decode and interpret
65 * the BER encoded public key and parameters
66 * @set_priv_key: Function invokes the algorithm specific set private key
67 * function, which knows how to decode and interpret
68 * the BER encoded private key and parameters
69 * @max_size: Function returns dest buffer size required for a given key.
70 * @init: Initialize the cryptographic transformation object.
71 * This function is used to initialize the cryptographic
72 * transformation object. This function is called only once at
73 * the instantiation time, right after the transformation context
74 * was allocated. In case the cryptographic hardware has some
75 * special requirements which need to be handled by software, this
76 * function shall check for the precise requirement of the
77 * transformation and put any software fallbacks in place.
78 * @exit: Deinitialize the cryptographic transformation object. This is a
79 * counterpart to @init, used to remove various changes set in
82 * @base: Common crypto API algorithm data structure
85 int (*encrypt
)(struct akcipher_request
*req
);
86 int (*decrypt
)(struct akcipher_request
*req
);
87 int (*set_pub_key
)(struct crypto_akcipher
*tfm
, const void *key
,
89 int (*set_priv_key
)(struct crypto_akcipher
*tfm
, const void *key
,
91 unsigned int (*max_size
)(struct crypto_akcipher
*tfm
);
92 int (*init
)(struct crypto_akcipher
*tfm
);
93 void (*exit
)(struct crypto_akcipher
*tfm
);
95 struct crypto_alg base
;
99 * DOC: Generic Public Key Cipher API
101 * The Public Key Cipher API is used with the algorithms of type
102 * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
106 * crypto_alloc_akcipher() - allocate AKCIPHER tfm handle
107 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
108 * public key algorithm e.g. "rsa"
109 * @type: specifies the type of the algorithm
110 * @mask: specifies the mask for the algorithm
112 * Allocate a handle for public key algorithm. The returned struct
113 * crypto_akcipher is the handle that is required for any subsequent
114 * API invocation for the public key operations.
116 * Return: allocated handle in case of success; IS_ERR() is true in case
117 * of an error, PTR_ERR() returns the error code.
119 struct crypto_akcipher
*crypto_alloc_akcipher(const char *alg_name
, u32 type
,
122 static inline struct crypto_tfm
*crypto_akcipher_tfm(
123 struct crypto_akcipher
*tfm
)
128 static inline struct akcipher_alg
*__crypto_akcipher_alg(struct crypto_alg
*alg
)
130 return container_of(alg
, struct akcipher_alg
, base
);
133 static inline struct crypto_akcipher
*__crypto_akcipher_tfm(
134 struct crypto_tfm
*tfm
)
136 return container_of(tfm
, struct crypto_akcipher
, base
);
139 static inline struct akcipher_alg
*crypto_akcipher_alg(
140 struct crypto_akcipher
*tfm
)
142 return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm
)->__crt_alg
);
145 static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher
*tfm
)
150 static inline void akcipher_request_set_tfm(struct akcipher_request
*req
,
151 struct crypto_akcipher
*tfm
)
153 req
->base
.tfm
= crypto_akcipher_tfm(tfm
);
156 static inline struct crypto_akcipher
*crypto_akcipher_reqtfm(
157 struct akcipher_request
*req
)
159 return __crypto_akcipher_tfm(req
->base
.tfm
);
163 * crypto_free_akcipher() - free AKCIPHER tfm handle
165 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
167 * If @tfm is a NULL or error pointer, this function does nothing.
169 static inline void crypto_free_akcipher(struct crypto_akcipher
*tfm
)
171 crypto_destroy_tfm(tfm
, crypto_akcipher_tfm(tfm
));
175 * akcipher_request_alloc() - allocates public key request
177 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
178 * @gfp: allocation flags
180 * Return: allocated handle in case of success or NULL in case of an error.
182 static inline struct akcipher_request
*akcipher_request_alloc(
183 struct crypto_akcipher
*tfm
, gfp_t gfp
)
185 struct akcipher_request
*req
;
187 req
= kmalloc(sizeof(*req
) + crypto_akcipher_reqsize(tfm
), gfp
);
189 akcipher_request_set_tfm(req
, tfm
);
195 * akcipher_request_free() - zeroize and free public key request
197 * @req: request to free
199 static inline void akcipher_request_free(struct akcipher_request
*req
)
201 kfree_sensitive(req
);
205 * akcipher_request_set_callback() - Sets an asynchronous callback.
207 * Callback will be called when an asynchronous operation on a given
208 * request is finished.
210 * @req: request that the callback will be set for
211 * @flgs: specify for instance if the operation may backlog
212 * @cmpl: callback which will be called
213 * @data: private data used by the caller
215 static inline void akcipher_request_set_callback(struct akcipher_request
*req
,
217 crypto_completion_t cmpl
,
220 req
->base
.complete
= cmpl
;
221 req
->base
.data
= data
;
222 req
->base
.flags
= flgs
;
226 * akcipher_request_set_crypt() - Sets request parameters
228 * Sets parameters required by crypto operation
230 * @req: public key request
231 * @src: ptr to input scatter list
232 * @dst: ptr to output scatter list
233 * @src_len: size of the src input scatter list to be processed
234 * @dst_len: size of the dst output scatter list
236 static inline void akcipher_request_set_crypt(struct akcipher_request
*req
,
237 struct scatterlist
*src
,
238 struct scatterlist
*dst
,
239 unsigned int src_len
,
240 unsigned int dst_len
)
244 req
->src_len
= src_len
;
245 req
->dst_len
= dst_len
;
249 * crypto_akcipher_maxsize() - Get len for output buffer
251 * Function returns the dest buffer size required for a given key.
252 * Function assumes that the key is already set in the transformation. If this
253 * function is called without a setkey or with a failed setkey, you will end up
254 * in a NULL dereference.
256 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
258 static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher
*tfm
)
260 struct akcipher_alg
*alg
= crypto_akcipher_alg(tfm
);
262 return alg
->max_size(tfm
);
266 * crypto_akcipher_encrypt() - Invoke public key encrypt operation
268 * Function invokes the specific public key encrypt operation for a given
269 * public key algorithm
271 * @req: asymmetric key request
273 * Return: zero on success; error code in case of error
275 static inline int crypto_akcipher_encrypt(struct akcipher_request
*req
)
277 struct crypto_akcipher
*tfm
= crypto_akcipher_reqtfm(req
);
279 return crypto_akcipher_alg(tfm
)->encrypt(req
);
283 * crypto_akcipher_decrypt() - Invoke public key decrypt operation
285 * Function invokes the specific public key decrypt operation for a given
286 * public key algorithm
288 * @req: asymmetric key request
290 * Return: zero on success; error code in case of error
292 static inline int crypto_akcipher_decrypt(struct akcipher_request
*req
)
294 struct crypto_akcipher
*tfm
= crypto_akcipher_reqtfm(req
);
296 return crypto_akcipher_alg(tfm
)->decrypt(req
);
300 * crypto_akcipher_sync_encrypt() - Invoke public key encrypt operation
302 * Function invokes the specific public key encrypt operation for a given
303 * public key algorithm
305 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
306 * @src: source buffer
307 * @slen: source length
308 * @dst: destination obuffer
309 * @dlen: destination length
311 * Return: zero on success; error code in case of error
313 int crypto_akcipher_sync_encrypt(struct crypto_akcipher
*tfm
,
314 const void *src
, unsigned int slen
,
315 void *dst
, unsigned int dlen
);
318 * crypto_akcipher_sync_decrypt() - Invoke public key decrypt operation
320 * Function invokes the specific public key decrypt operation for a given
321 * public key algorithm
323 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
324 * @src: source buffer
325 * @slen: source length
326 * @dst: destination obuffer
327 * @dlen: destination length
329 * Return: Output length on success; error code in case of error
331 int crypto_akcipher_sync_decrypt(struct crypto_akcipher
*tfm
,
332 const void *src
, unsigned int slen
,
333 void *dst
, unsigned int dlen
);
336 * crypto_akcipher_set_pub_key() - Invoke set public key operation
338 * Function invokes the algorithm specific set key function, which knows
339 * how to decode and interpret the encoded key and parameters
342 * @key: BER encoded public key, algo OID, paramlen, BER encoded
344 * @keylen: length of the key (not including other data)
346 * Return: zero on success; error code in case of error
348 static inline int crypto_akcipher_set_pub_key(struct crypto_akcipher
*tfm
,
352 struct akcipher_alg
*alg
= crypto_akcipher_alg(tfm
);
354 return alg
->set_pub_key(tfm
, key
, keylen
);
358 * crypto_akcipher_set_priv_key() - Invoke set private key operation
360 * Function invokes the algorithm specific set key function, which knows
361 * how to decode and interpret the encoded key and parameters
364 * @key: BER encoded private key, algo OID, paramlen, BER encoded
366 * @keylen: length of the key (not including other data)
368 * Return: zero on success; error code in case of error
370 static inline int crypto_akcipher_set_priv_key(struct crypto_akcipher
*tfm
,
374 struct akcipher_alg
*alg
= crypto_akcipher_alg(tfm
);
376 return alg
->set_priv_key(tfm
, key
, keylen
);