CRIS: UAPI: use generic sembuf.h
[linux-2.6/btrfs-unstable.git] / include / crypto / akcipher.h
blob69d163e39101bf46d6aae184d18e1938e109044c
1 /*
2 * Public Key Encryption
4 * Copyright (c) 2015, Intel Corporation
5 * Authors: Tadeusz Struk <tadeusz.struk@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)
10 * any later version.
13 #ifndef _CRYPTO_AKCIPHER_H
14 #define _CRYPTO_AKCIPHER_H
15 #include <linux/crypto.h>
17 /**
18 * struct akcipher_request - public key request
20 * @base: Common attributes for async crypto requests
21 * @src: Pointer to memory containing the input parameters
22 * The format of the parameter(s) is expeted to be Octet String
23 * @dst: Pointer to memory whare the result will be stored
24 * @src_len: Size of the input parameter
25 * @dst_len: Size of the output buffer. It needs to be at leaset
26 * as big as the expected result depending on the operation
27 * After operation it will be updated with the acctual size of the
28 * result. In case of error, where the dst_len was insufficient,
29 * it will be updated to the size required for the operation.
30 * @__ctx: Start of private context data
32 struct akcipher_request {
33 struct crypto_async_request base;
34 void *src;
35 void *dst;
36 unsigned int src_len;
37 unsigned int dst_len;
38 void *__ctx[] CRYPTO_MINALIGN_ATTR;
41 /**
42 * struct crypto_akcipher - user-instantiated objects which encapsulate
43 * algorithms and core processing logic
45 * @base: Common crypto API algorithm data structure
47 struct crypto_akcipher {
48 struct crypto_tfm base;
51 /**
52 * struct akcipher_alg - generic public key algorithm
54 * @sign: Function performs a sign operation as defined by public key
55 * algorithm. In case of error, where the dst_len was insufficient,
56 * the req->dst_len will be updated to the size required for the
57 * operation
58 * @verify: Function performs a sign operation as defined by public key
59 * algorithm. In case of error, where the dst_len was insufficient,
60 * the req->dst_len will be updated to the size required for the
61 * operation
62 * @encrypt: Function performs an encrytp operation as defined by public key
63 * algorithm. In case of error, where the dst_len was insufficient,
64 * the req->dst_len will be updated to the size required for the
65 * operation
66 * @decrypt: Function performs a decrypt operation as defined by public key
67 * algorithm. In case of error, where the dst_len was insufficient,
68 * the req->dst_len will be updated to the size required for the
69 * operation
70 * @setkey: Function invokes the algorithm specific set key function, which
71 * knows how to decode and interpret the BER encoded key
72 * @init: Initialize the cryptographic transformation object.
73 * This function is used to initialize the cryptographic
74 * transformation object. This function is called only once at
75 * the instantiation time, right after the transformation context
76 * was allocated. In case the cryptographic hardware has some
77 * special requirements which need to be handled by software, this
78 * function shall check for the precise requirement of the
79 * transformation and put any software fallbacks in place.
80 * @exit: Deinitialize the cryptographic transformation object. This is a
81 * counterpart to @init, used to remove various changes set in
82 * @init.
84 * @reqsize: Request context size required by algorithm implementation
85 * @base: Common crypto API algorithm data structure
87 struct akcipher_alg {
88 int (*sign)(struct akcipher_request *req);
89 int (*verify)(struct akcipher_request *req);
90 int (*encrypt)(struct akcipher_request *req);
91 int (*decrypt)(struct akcipher_request *req);
92 int (*setkey)(struct crypto_akcipher *tfm, const void *key,
93 unsigned int keylen);
94 int (*init)(struct crypto_akcipher *tfm);
95 void (*exit)(struct crypto_akcipher *tfm);
97 unsigned int reqsize;
98 struct crypto_alg base;
102 * DOC: Generic Public Key API
104 * The Public Key API is used with the algorithms of type
105 * CRYPTO_ALG_TYPE_AKCIPHER (listed as type "akcipher" in /proc/crypto)
109 * crypto_alloc_akcipher() -- allocate AKCIPHER tfm handle
110 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the
111 * public key algorithm e.g. "rsa"
112 * @type: specifies the type of the algorithm
113 * @mask: specifies the mask for the algorithm
115 * Allocate a handle for public key algorithm. The returned struct
116 * crypto_akcipher is the handle that is required for any subsequent
117 * API invocation for the public key operations.
119 * Return: allocated handle in case of success; IS_ERR() is true in case
120 * of an error, PTR_ERR() returns the error code.
122 struct crypto_akcipher *crypto_alloc_akcipher(const char *alg_name, u32 type,
123 u32 mask);
125 static inline struct crypto_tfm *crypto_akcipher_tfm(
126 struct crypto_akcipher *tfm)
128 return &tfm->base;
131 static inline struct akcipher_alg *__crypto_akcipher_alg(struct crypto_alg *alg)
133 return container_of(alg, struct akcipher_alg, base);
136 static inline struct crypto_akcipher *__crypto_akcipher_tfm(
137 struct crypto_tfm *tfm)
139 return container_of(tfm, struct crypto_akcipher, base);
142 static inline struct akcipher_alg *crypto_akcipher_alg(
143 struct crypto_akcipher *tfm)
145 return __crypto_akcipher_alg(crypto_akcipher_tfm(tfm)->__crt_alg);
148 static inline unsigned int crypto_akcipher_reqsize(struct crypto_akcipher *tfm)
150 return crypto_akcipher_alg(tfm)->reqsize;
153 static inline void akcipher_request_set_tfm(struct akcipher_request *req,
154 struct crypto_akcipher *tfm)
156 req->base.tfm = crypto_akcipher_tfm(tfm);
159 static inline struct crypto_akcipher *crypto_akcipher_reqtfm(
160 struct akcipher_request *req)
162 return __crypto_akcipher_tfm(req->base.tfm);
166 * crypto_free_akcipher() -- free AKCIPHER tfm handle
168 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
170 static inline void crypto_free_akcipher(struct crypto_akcipher *tfm)
172 crypto_destroy_tfm(tfm, crypto_akcipher_tfm(tfm));
176 * akcipher_request_alloc() -- allocates public key request
178 * @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
179 * @gfp: allocation flags
181 * Return: allocated handle in case of success or NULL in case of an error.
183 static inline struct akcipher_request *akcipher_request_alloc(
184 struct crypto_akcipher *tfm, gfp_t gfp)
186 struct akcipher_request *req;
188 req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
189 if (likely(req))
190 akcipher_request_set_tfm(req, tfm);
192 return req;
196 * akcipher_request_free() -- zeroize and free public key request
198 * @req: request to free
200 static inline void akcipher_request_free(struct akcipher_request *req)
202 kzfree(req);
206 * akcipher_request_set_callback() -- Sets an asynchronous callback.
208 * Callback will be called when an asynchronous operation on a given
209 * request is finished.
211 * @req: request that the callback will be set for
212 * @flgs: specify for instance if the operation may backlog
213 * @cmlp: callback which will be called
214 * @data: private data used by the caller
216 static inline void akcipher_request_set_callback(struct akcipher_request *req,
217 u32 flgs,
218 crypto_completion_t cmpl,
219 void *data)
221 req->base.complete = cmpl;
222 req->base.data = data;
223 req->base.flags = flgs;
227 * akcipher_request_set_crypt() -- Sets reqest parameters
229 * Sets parameters required by crypto operation
231 * @req: public key request
232 * @src: ptr to input parameter
233 * @dst: ptr of output parameter
234 * @src_len: size of the input buffer
235 * @dst_len: size of the output buffer. It will be updated by the
236 * implementation to reflect the acctual size of the result
238 static inline void akcipher_request_set_crypt(struct akcipher_request *req,
239 void *src, void *dst,
240 unsigned int src_len,
241 unsigned int dst_len)
243 req->src = src;
244 req->dst = dst;
245 req->src_len = src_len;
246 req->dst_len = dst_len;
250 * crypto_akcipher_encrypt() -- Invoke public key encrypt operation
252 * Function invokes the specific public key encrypt operation for a given
253 * public key algorithm
255 * @req: asymmetric key request
257 * Return: zero on success; error code in case of error
259 static inline int crypto_akcipher_encrypt(struct akcipher_request *req)
261 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
262 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
264 return alg->encrypt(req);
268 * crypto_akcipher_decrypt() -- Invoke public key decrypt operation
270 * Function invokes the specific public key decrypt operation for a given
271 * public key algorithm
273 * @req: asymmetric key request
275 * Return: zero on success; error code in case of error
277 static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
279 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
280 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
282 return alg->decrypt(req);
286 * crypto_akcipher_sign() -- Invoke public key sign operation
288 * Function invokes the specific public key sign operation for a given
289 * public key algorithm
291 * @req: asymmetric key request
293 * Return: zero on success; error code in case of error
295 static inline int crypto_akcipher_sign(struct akcipher_request *req)
297 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
298 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
300 return alg->sign(req);
304 * crypto_akcipher_verify() -- Invoke public key verify operation
306 * Function invokes the specific public key verify operation for a given
307 * public key algorithm
309 * @req: asymmetric key request
311 * Return: zero on success; error code in case of error
313 static inline int crypto_akcipher_verify(struct akcipher_request *req)
315 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
316 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
318 return alg->verify(req);
322 * crypto_akcipher_setkey() -- Invoke public key setkey operation
324 * Function invokes the algorithm specific set key function, which knows
325 * how to decode and interpret the encoded key
327 * @tfm: tfm handle
328 * @key: BER encoded private or public key
329 * @keylen: length of the key
331 * Return: zero on success; error code in case of error
333 static inline int crypto_akcipher_setkey(struct crypto_akcipher *tfm, void *key,
334 unsigned int keylen)
336 struct akcipher_alg *alg = crypto_akcipher_alg(tfm);
338 return alg->setkey(tfm, key, keylen);
340 #endif