Skip to content

Commit ad26959

Browse files
ambarusherbertx
authored andcommitted
crypto: ecc - remove unnecessary casts
ecc software implementation works with chunks of u64 data. There were some unnecessary casts to u8 and then back to u64 for the ecc keys. This patch removes the unnecessary casts. Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 099054d commit ad26959

3 files changed

Lines changed: 22 additions & 25 deletions

File tree

‎crypto/ecc.c‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ static inline void ecc_swap_digits(const u64 *in, u64 *out,
904904
}
905905

906906
int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
907-
const u8 *private_key, unsigned int private_key_len)
907+
const u64 *private_key, unsigned int private_key_len)
908908
{
909909
int nbytes;
910910
const struct ecc_curve *curve = ecc_get_curve(curve_id);
@@ -917,31 +917,30 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
917917
if (private_key_len != nbytes)
918918
return -EINVAL;
919919

920-
if (vli_is_zero((const u64 *)&private_key[0], ndigits))
920+
if (vli_is_zero(private_key, ndigits))
921921
return -EINVAL;
922922

923923
/* Make sure the private key is in the range [1, n-1]. */
924-
if (vli_cmp(curve->n, (const u64 *)&private_key[0], ndigits) != 1)
924+
if (vli_cmp(curve->n, private_key, ndigits) != 1)
925925
return -EINVAL;
926926

927927
return 0;
928928
}
929929

930930
int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits,
931-
const u8 *private_key, u8 *public_key)
931+
const u64 *private_key, u64 *public_key)
932932
{
933933
int ret = 0;
934934
struct ecc_point *pk;
935935
u64 priv[ndigits];
936-
unsigned int nbytes;
937936
const struct ecc_curve *curve = ecc_get_curve(curve_id);
938937

939938
if (!private_key || !curve) {
940939
ret = -EINVAL;
941940
goto out;
942941
}
943942

944-
ecc_swap_digits((const u64 *)private_key, priv, ndigits);
943+
ecc_swap_digits(private_key, priv, ndigits);
945944

946945
pk = ecc_alloc_point(ndigits);
947946
if (!pk) {
@@ -955,9 +954,8 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits,
955954
goto err_free_point;
956955
}
957956

958-
nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
959-
ecc_swap_digits(pk->x, (u64 *)public_key, ndigits);
960-
ecc_swap_digits(pk->y, (u64 *)&public_key[nbytes], ndigits);
957+
ecc_swap_digits(pk->x, public_key, ndigits);
958+
ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
961959

962960
err_free_point:
963961
ecc_free_point(pk);
@@ -966,8 +964,8 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits,
966964
}
967965

968966
int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
969-
const u8 *private_key, const u8 *public_key,
970-
u8 *secret)
967+
const u64 *private_key, const u64 *public_key,
968+
u64 *secret)
971969
{
972970
int ret = 0;
973971
struct ecc_point *product, *pk;
@@ -997,13 +995,13 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
997995
goto err_alloc_product;
998996
}
999997

1000-
ecc_swap_digits((const u64 *)public_key, pk->x, ndigits);
1001-
ecc_swap_digits((const u64 *)&public_key[nbytes], pk->y, ndigits);
1002-
ecc_swap_digits((const u64 *)private_key, priv, ndigits);
998+
ecc_swap_digits(public_key, pk->x, ndigits);
999+
ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1000+
ecc_swap_digits(private_key, priv, ndigits);
10031001

10041002
ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits);
10051003

1006-
ecc_swap_digits(product->x, (u64 *)secret, ndigits);
1004+
ecc_swap_digits(product->x, secret, ndigits);
10071005

10081006
if (ecc_point_is_zero(product))
10091007
ret = -EFAULT;

‎crypto/ecc.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
* Returns 0 if the key is acceptable, a negative value otherwise
4242
*/
4343
int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
44-
const u8 *private_key, unsigned int private_key_len);
44+
const u64 *private_key, unsigned int private_key_len);
4545

4646
/**
4747
* ecdh_make_pub_key() - Compute an ECC public key
@@ -55,7 +55,7 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
5555
* if an error occurred.
5656
*/
5757
int ecdh_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
58-
const u8 *private_key, u8 *public_key);
58+
const u64 *private_key, u64 *public_key);
5959

6060
/**
6161
* crypto_ecdh_shared_secret() - Compute a shared secret
@@ -73,6 +73,6 @@ int ecdh_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
7373
* if an error occurred.
7474
*/
7575
int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
76-
const u8 *private_key, const u8 *public_key,
77-
u8 *secret);
76+
const u64 *private_key, const u64 *public_key,
77+
u64 *secret);
7878
#endif

‎crypto/ecdh.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
5656
ctx->ndigits = ndigits;
5757

5858
if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
59-
(const u8 *)params.key, params.key_size) < 0)
59+
(const u64 *)params.key, params.key_size) < 0)
6060
return -EINVAL;
6161

6262
memcpy(ctx->private_key, params.key, params.key_size);
@@ -81,15 +81,14 @@ static int ecdh_compute_value(struct kpp_request *req)
8181
return -EINVAL;
8282

8383
ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
84-
(const u8 *)ctx->private_key,
85-
(const u8 *)ctx->public_key,
86-
(u8 *)ctx->shared_secret);
84+
ctx->private_key,
85+
ctx->public_key,
86+
ctx->shared_secret);
8787

8888
buf = ctx->shared_secret;
8989
} else {
9090
ret = ecdh_make_pub_key(ctx->curve_id, ctx->ndigits,
91-
(const u8 *)ctx->private_key,
92-
(u8 *)ctx->public_key);
91+
ctx->private_key, ctx->public_key);
9392
buf = ctx->public_key;
9493
/* Public part is a point thus it has both coordinates */
9594
nbytes *= 2;

0 commit comments

Comments
 (0)