Skip to content

Commit 8552cb0

Browse files
committed
crypto: rsassa-pkcs1 - Copy source data for SG list
As virtual addresses in general may not be suitable for DMA, always perform a copy before using them in an SG list. Fixes: 1e562de ("crypto: rsassa-pkcs1 - Migrate to sig_alg backend") Reported-by: Zorro Lang <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent fac04ef commit 8552cb0

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

crypto/rsassa-pkcs1.c

+14-31
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,6 @@ static int rsassa_pkcs1_sign(struct crypto_sig *tfm,
163163
struct rsassa_pkcs1_inst_ctx *ictx = sig_instance_ctx(inst);
164164
const struct hash_prefix *hash_prefix = ictx->hash_prefix;
165165
struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
166-
unsigned int child_reqsize = crypto_akcipher_reqsize(ctx->child);
167-
struct akcipher_request *child_req __free(kfree_sensitive) = NULL;
168-
struct scatterlist in_sg[3], out_sg;
169-
struct crypto_wait cwait;
170166
unsigned int pad_len;
171167
unsigned int ps_end;
172168
unsigned int len;
@@ -187,37 +183,25 @@ static int rsassa_pkcs1_sign(struct crypto_sig *tfm,
187183

188184
pad_len = ctx->key_size - slen - hash_prefix->size - 1;
189185

190-
child_req = kmalloc(sizeof(*child_req) + child_reqsize + pad_len,
191-
GFP_KERNEL);
192-
if (!child_req)
193-
return -ENOMEM;
194-
195186
/* RFC 8017 sec 8.2.1 step 1 - EMSA-PKCS1-v1_5 encoding generation */
196-
in_buf = (u8 *)(child_req + 1) + child_reqsize;
187+
in_buf = dst;
188+
memmove(in_buf + pad_len + hash_prefix->size, src, slen);
189+
memcpy(in_buf + pad_len, hash_prefix->data, hash_prefix->size);
190+
197191
ps_end = pad_len - 1;
198192
in_buf[0] = 0x01;
199193
memset(in_buf + 1, 0xff, ps_end - 1);
200194
in_buf[ps_end] = 0x00;
201195

202-
/* RFC 8017 sec 8.2.1 step 2 - RSA signature */
203-
crypto_init_wait(&cwait);
204-
sg_init_table(in_sg, 3);
205-
sg_set_buf(&in_sg[0], in_buf, pad_len);
206-
sg_set_buf(&in_sg[1], hash_prefix->data, hash_prefix->size);
207-
sg_set_buf(&in_sg[2], src, slen);
208-
sg_init_one(&out_sg, dst, dlen);
209-
akcipher_request_set_tfm(child_req, ctx->child);
210-
akcipher_request_set_crypt(child_req, in_sg, &out_sg,
211-
ctx->key_size - 1, dlen);
212-
akcipher_request_set_callback(child_req, CRYPTO_TFM_REQ_MAY_SLEEP,
213-
crypto_req_done, &cwait);
214196

215-
err = crypto_akcipher_decrypt(child_req);
216-
err = crypto_wait_req(err, &cwait);
217-
if (err)
197+
/* RFC 8017 sec 8.2.1 step 2 - RSA signature */
198+
err = crypto_akcipher_sync_decrypt(ctx->child, in_buf,
199+
ctx->key_size - 1, in_buf,
200+
ctx->key_size);
201+
if (err < 0)
218202
return err;
219203

220-
len = child_req->dst_len;
204+
len = err;
221205
pad_len = ctx->key_size - len;
222206

223207
/* Four billion to one */
@@ -239,8 +223,8 @@ static int rsassa_pkcs1_verify(struct crypto_sig *tfm,
239223
struct rsassa_pkcs1_ctx *ctx = crypto_sig_ctx(tfm);
240224
unsigned int child_reqsize = crypto_akcipher_reqsize(ctx->child);
241225
struct akcipher_request *child_req __free(kfree_sensitive) = NULL;
242-
struct scatterlist in_sg, out_sg;
243226
struct crypto_wait cwait;
227+
struct scatterlist sg;
244228
unsigned int dst_len;
245229
unsigned int pos;
246230
u8 *out_buf;
@@ -259,13 +243,12 @@ static int rsassa_pkcs1_verify(struct crypto_sig *tfm,
259243
return -ENOMEM;
260244

261245
out_buf = (u8 *)(child_req + 1) + child_reqsize;
246+
memcpy(out_buf, src, slen);
262247

263248
crypto_init_wait(&cwait);
264-
sg_init_one(&in_sg, src, slen);
265-
sg_init_one(&out_sg, out_buf, ctx->key_size);
249+
sg_init_one(&sg, out_buf, slen);
266250
akcipher_request_set_tfm(child_req, ctx->child);
267-
akcipher_request_set_crypt(child_req, &in_sg, &out_sg,
268-
slen, ctx->key_size);
251+
akcipher_request_set_crypt(child_req, &sg, &sg, slen, slen);
269252
akcipher_request_set_callback(child_req, CRYPTO_TFM_REQ_MAY_SLEEP,
270253
crypto_req_done, &cwait);
271254

0 commit comments

Comments
 (0)