Skip to content

Commit da9426c

Browse files
committed
crypto: atmel-ecc - fix to allow multi segment scatterlists
Remove the limitation of single element scatterlists. ECDH with multi-element scatterlists is needed by TPM. Similar to 'commit 95ec01b ("crypto: ecdh - fix to allow multi segment scatterlists")'. Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> (cherry picked from commit e9440ff)
1 parent 1403789 commit da9426c

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

drivers/crypto/atmel-ecc.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ static int atmel_ecc_init_ecdh_cmd(struct atmel_ecc_cmd *cmd,
186186
* always be the same. Use a macro for the key size to avoid unnecessary
187187
* computations.
188188
*/
189-
copied = sg_copy_to_buffer(pubkey, 1, cmd->data, ATMEL_ECC_PUBKEY_SIZE);
189+
copied = sg_copy_to_buffer(pubkey,
190+
sg_nents_for_len(pubkey,
191+
ATMEL_ECC_PUBKEY_SIZE),
192+
cmd->data, ATMEL_ECC_PUBKEY_SIZE);
190193
if (copied != ATMEL_ECC_PUBKEY_SIZE)
191194
return -EINVAL;
192195

@@ -268,15 +271,17 @@ static void atmel_ecdh_done(struct atmel_ecc_work_data *work_data, void *areq,
268271
struct kpp_request *req = areq;
269272
struct atmel_ecdh_ctx *ctx = work_data->ctx;
270273
struct atmel_ecc_cmd *cmd = &work_data->cmd;
271-
size_t copied;
272-
size_t n_sz = ctx->n_sz;
274+
size_t copied, n_sz;
273275

274276
if (status)
275277
goto free_work_data;
276278

279+
/* might want less than we've got */
280+
n_sz = min_t(size_t, ctx->n_sz, req->dst_len);
281+
277282
/* copy the shared secret */
278-
copied = sg_copy_from_buffer(req->dst, 1, &cmd->data[RSP_DATA_IDX],
279-
n_sz);
283+
copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz),
284+
&cmd->data[RSP_DATA_IDX], n_sz);
280285
if (copied != n_sz)
281286
status = -EINVAL;
282287

@@ -440,18 +445,22 @@ static int atmel_ecdh_generate_public_key(struct kpp_request *req)
440445
{
441446
struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
442447
struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
443-
size_t copied;
448+
size_t copied, nbytes;
444449
int ret = 0;
445450

446451
if (ctx->do_fallback) {
447452
kpp_request_set_tfm(req, ctx->fallback);
448453
return crypto_kpp_generate_public_key(req);
449454
}
450455

456+
/* might want less than we've got */
457+
nbytes = min_t(size_t, ATMEL_ECC_PUBKEY_SIZE, req->dst_len);
458+
451459
/* public key was saved at private key generation */
452-
copied = sg_copy_from_buffer(req->dst, 1, ctx->public_key,
453-
ATMEL_ECC_PUBKEY_SIZE);
454-
if (copied != ATMEL_ECC_PUBKEY_SIZE)
460+
copied = sg_copy_from_buffer(req->dst,
461+
sg_nents_for_len(req->dst, nbytes),
462+
ctx->public_key, nbytes);
463+
if (copied != nbytes)
455464
ret = -EINVAL;
456465

457466
return ret;
@@ -470,6 +479,10 @@ static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
470479
return crypto_kpp_compute_shared_secret(req);
471480
}
472481

482+
/* must have exactly two points to be on the curve */
483+
if (req->src_len != ATMEL_ECC_PUBKEY_SIZE)
484+
return -EINVAL;
485+
473486
gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :
474487
GFP_ATOMIC;
475488

0 commit comments

Comments
 (0)