Skip to content

Commit addde1f

Browse files
committed
crypto: akcipher - Add sync interface without SG lists
The only user of akcipher does not use SG lists. Therefore forcing users to use SG lists only results unnecessary overhead. Add a new interface that supports arbitrary kernel pointers. For the time being the copy will be performed unconditionally. But this will go away once the underlying interface is updated. Note also that only encryption and decryption is addressed by this patch as sign/verify will go into a new interface (sig). Signed-off-by: Herbert Xu <[email protected]>
1 parent 9979c6e commit addde1f

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

crypto/akcipher.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@
1010
#include <linux/errno.h>
1111
#include <linux/kernel.h>
1212
#include <linux/module.h>
13+
#include <linux/scatterlist.h>
1314
#include <linux/seq_file.h>
1415
#include <linux/slab.h>
1516
#include <linux/string.h>
1617
#include <net/netlink.h>
1718

1819
#include "internal.h"
1920

21+
struct crypto_akcipher_sync_data {
22+
struct crypto_akcipher *tfm;
23+
const void *src;
24+
void *dst;
25+
unsigned int slen;
26+
unsigned int dlen;
27+
28+
struct akcipher_request *req;
29+
struct crypto_wait cwait;
30+
struct scatterlist sg;
31+
u8 *buf;
32+
};
33+
2034
static int __maybe_unused crypto_akcipher_report(
2135
struct sk_buff *skb, struct crypto_alg *alg)
2236
{
@@ -186,5 +200,86 @@ int akcipher_register_instance(struct crypto_template *tmpl,
186200
}
187201
EXPORT_SYMBOL_GPL(akcipher_register_instance);
188202

203+
static int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data)
204+
{
205+
unsigned int reqsize = crypto_akcipher_reqsize(data->tfm);
206+
unsigned int mlen = max(data->slen, data->dlen);
207+
struct akcipher_request *req;
208+
struct scatterlist *sg;
209+
unsigned int len;
210+
u8 *buf;
211+
212+
len = sizeof(*req) + reqsize + mlen;
213+
if (len < mlen)
214+
return -EOVERFLOW;
215+
216+
req = kzalloc(len, GFP_KERNEL);
217+
if (!req)
218+
return -ENOMEM;
219+
220+
data->req = req;
221+
222+
buf = (u8 *)(req + 1) + reqsize;
223+
data->buf = buf;
224+
memcpy(buf, data->src, data->slen);
225+
226+
sg = &data->sg;
227+
sg_init_one(sg, buf, mlen);
228+
akcipher_request_set_crypt(req, sg, sg, data->slen, data->dlen);
229+
230+
crypto_init_wait(&data->cwait);
231+
akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP,
232+
crypto_req_done, &data->cwait);
233+
234+
return 0;
235+
}
236+
237+
static int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data,
238+
int err)
239+
{
240+
err = crypto_wait_req(err, &data->cwait);
241+
memcpy(data->dst, data->buf, data->dlen);
242+
data->dlen = data->req->dst_len;
243+
kfree_sensitive(data->req);
244+
return err;
245+
}
246+
247+
int crypto_akcipher_sync_encrypt(struct crypto_akcipher *tfm,
248+
const void *src, unsigned int slen,
249+
void *dst, unsigned int dlen)
250+
{
251+
struct crypto_akcipher_sync_data data = {
252+
.tfm = tfm,
253+
.src = src,
254+
.dst = dst,
255+
.slen = slen,
256+
.dlen = dlen,
257+
};
258+
259+
return crypto_akcipher_sync_prep(&data) ?:
260+
crypto_akcipher_sync_post(&data,
261+
crypto_akcipher_encrypt(data.req));
262+
}
263+
EXPORT_SYMBOL_GPL(crypto_akcipher_sync_encrypt);
264+
265+
int crypto_akcipher_sync_decrypt(struct crypto_akcipher *tfm,
266+
const void *src, unsigned int slen,
267+
void *dst, unsigned int dlen)
268+
{
269+
struct crypto_akcipher_sync_data data = {
270+
.tfm = tfm,
271+
.src = src,
272+
.dst = dst,
273+
.slen = slen,
274+
.dlen = dlen,
275+
};
276+
277+
return crypto_akcipher_sync_prep(&data) ?:
278+
crypto_akcipher_sync_post(&data,
279+
crypto_akcipher_decrypt(data.req)) ?:
280+
data.dlen;
281+
}
282+
EXPORT_SYMBOL_GPL(crypto_akcipher_sync_decrypt);
283+
189284
MODULE_LICENSE("GPL");
190285
MODULE_DESCRIPTION("Generic public key cipher type");

include/crypto/akcipher.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,42 @@ static inline int crypto_akcipher_decrypt(struct akcipher_request *req)
373373
return crypto_akcipher_errstat(alg, alg->decrypt(req));
374374
}
375375

376+
/**
377+
* crypto_akcipher_sync_encrypt() - Invoke public key encrypt operation
378+
*
379+
* Function invokes the specific public key encrypt operation for a given
380+
* public key algorithm
381+
*
382+
* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
383+
* @src: source buffer
384+
* @slen: source length
385+
* @dst: destinatino obuffer
386+
* @dlen: destination length
387+
*
388+
* Return: zero on success; error code in case of error
389+
*/
390+
int crypto_akcipher_sync_encrypt(struct crypto_akcipher *tfm,
391+
const void *src, unsigned int slen,
392+
void *dst, unsigned int dlen);
393+
394+
/**
395+
* crypto_akcipher_sync_decrypt() - Invoke public key decrypt operation
396+
*
397+
* Function invokes the specific public key decrypt operation for a given
398+
* public key algorithm
399+
*
400+
* @tfm: AKCIPHER tfm handle allocated with crypto_alloc_akcipher()
401+
* @src: source buffer
402+
* @slen: source length
403+
* @dst: destinatino obuffer
404+
* @dlen: destination length
405+
*
406+
* Return: Output length on success; error code in case of error
407+
*/
408+
int crypto_akcipher_sync_decrypt(struct crypto_akcipher *tfm,
409+
const void *src, unsigned int slen,
410+
void *dst, unsigned int dlen);
411+
376412
/**
377413
* crypto_akcipher_sign() - Invoke public key sign operation
378414
*

0 commit comments

Comments
 (0)