summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTudor-Dan Ambarus <tudor.ambarus@microchip.com>2017-05-25 03:18:05 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2017-06-10 00:04:26 -0400
commitad269597107e6dde4810987eebbd075778d0262c (patch)
treeb0f5fa3416adfa983dff1bfefce13a793a03a883
parent099054d735a5e4cfc8e90b03b7422c9b48209d8b (diff)
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>
-rw-r--r--crypto/ecc.c28
-rw-r--r--crypto/ecc.h8
-rw-r--r--crypto/ecdh.c11
3 files changed, 22 insertions, 25 deletions
diff --git a/crypto/ecc.c b/crypto/ecc.c
index 69b4cc4303aa..e3a2b8f78a38 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -904,7 +904,7 @@ static inline void ecc_swap_digits(const u64 *in, u64 *out,
904} 904}
905 905
906int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, 906int 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)
908{ 908{
909 int nbytes; 909 int nbytes;
910 const struct ecc_curve *curve = ecc_get_curve(curve_id); 910 const struct ecc_curve *curve = ecc_get_curve(curve_id);
@@ -917,23 +917,22 @@ int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
917 if (private_key_len != nbytes) 917 if (private_key_len != nbytes)
918 return -EINVAL; 918 return -EINVAL;
919 919
920 if (vli_is_zero((const u64 *)&private_key[0], ndigits)) 920 if (vli_is_zero(private_key, ndigits))
921 return -EINVAL; 921 return -EINVAL;
922 922
923 /* Make sure the private key is in the range [1, n-1]. */ 923 /* 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)
925 return -EINVAL; 925 return -EINVAL;
926 926
927 return 0; 927 return 0;
928} 928}
929 929
930int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits, 930int 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)
932{ 932{
933 int ret = 0; 933 int ret = 0;
934 struct ecc_point *pk; 934 struct ecc_point *pk;
935 u64 priv[ndigits]; 935 u64 priv[ndigits];
936 unsigned int nbytes;
937 const struct ecc_curve *curve = ecc_get_curve(curve_id); 936 const struct ecc_curve *curve = ecc_get_curve(curve_id);
938 937
939 if (!private_key || !curve) { 938 if (!private_key || !curve) {
@@ -941,7 +940,7 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits,
941 goto out; 940 goto out;
942 } 941 }
943 942
944 ecc_swap_digits((const u64 *)private_key, priv, ndigits); 943 ecc_swap_digits(private_key, priv, ndigits);
945 944
946 pk = ecc_alloc_point(ndigits); 945 pk = ecc_alloc_point(ndigits);
947 if (!pk) { 946 if (!pk) {
@@ -955,9 +954,8 @@ int ecdh_make_pub_key(unsigned int curve_id, unsigned int ndigits,
955 goto err_free_point; 954 goto err_free_point;
956 } 955 }
957 956
958 nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT; 957 ecc_swap_digits(pk->x, public_key, ndigits);
959 ecc_swap_digits(pk->x, (u64 *)public_key, ndigits); 958 ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
960 ecc_swap_digits(pk->y, (u64 *)&public_key[nbytes], ndigits);
961 959
962err_free_point: 960err_free_point:
963 ecc_free_point(pk); 961 ecc_free_point(pk);
@@ -966,8 +964,8 @@ out:
966} 964}
967 965
968int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, 966int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
969 const u8 *private_key, const u8 *public_key, 967 const u64 *private_key, const u64 *public_key,
970 u8 *secret) 968 u64 *secret)
971{ 969{
972 int ret = 0; 970 int ret = 0;
973 struct ecc_point *product, *pk; 971 struct ecc_point *product, *pk;
@@ -997,13 +995,13 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
997 goto err_alloc_product; 995 goto err_alloc_product;
998 } 996 }
999 997
1000 ecc_swap_digits((const u64 *)public_key, pk->x, ndigits); 998 ecc_swap_digits(public_key, pk->x, ndigits);
1001 ecc_swap_digits((const u64 *)&public_key[nbytes], pk->y, ndigits); 999 ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1002 ecc_swap_digits((const u64 *)private_key, priv, ndigits); 1000 ecc_swap_digits(private_key, priv, ndigits);
1003 1001
1004 ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits); 1002 ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits);
1005 1003
1006 ecc_swap_digits(product->x, (u64 *)secret, ndigits); 1004 ecc_swap_digits(product->x, secret, ndigits);
1007 1005
1008 if (ecc_point_is_zero(product)) 1006 if (ecc_point_is_zero(product))
1009 ret = -EFAULT; 1007 ret = -EFAULT;
diff --git a/crypto/ecc.h b/crypto/ecc.h
index 1ca9bf7437d7..af2ffdb2dcd6 100644
--- a/crypto/ecc.h
+++ b/crypto/ecc.h
@@ -41,7 +41,7 @@
41 * Returns 0 if the key is acceptable, a negative value otherwise 41 * Returns 0 if the key is acceptable, a negative value otherwise
42 */ 42 */
43int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits, 43int 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);
45 45
46/** 46/**
47 * ecdh_make_pub_key() - Compute an ECC public key 47 * 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,
55 * if an error occurred. 55 * if an error occurred.
56 */ 56 */
57int ecdh_make_pub_key(const unsigned int curve_id, unsigned int ndigits, 57int 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);
59 59
60/** 60/**
61 * crypto_ecdh_shared_secret() - Compute a shared secret 61 * 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,
73 * if an error occurred. 73 * if an error occurred.
74 */ 74 */
75int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits, 75int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
76 const u8 *private_key, const u8 *public_key, 76 const u64 *private_key, const u64 *public_key,
77 u8 *secret); 77 u64 *secret);
78#endif 78#endif
diff --git a/crypto/ecdh.c b/crypto/ecdh.c
index 69c3951f0a03..c1f01630faad 100644
--- a/crypto/ecdh.c
+++ b/crypto/ecdh.c
@@ -56,7 +56,7 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
56 ctx->ndigits = ndigits; 56 ctx->ndigits = ndigits;
57 57
58 if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits, 58 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)
60 return -EINVAL; 60 return -EINVAL;
61 61
62 memcpy(ctx->private_key, params.key, params.key_size); 62 memcpy(ctx->private_key, params.key, params.key_size);
@@ -81,15 +81,14 @@ static int ecdh_compute_value(struct kpp_request *req)
81 return -EINVAL; 81 return -EINVAL;
82 82
83 ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits, 83 ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
84 (const u8 *)ctx->private_key, 84 ctx->private_key,
85 (const u8 *)ctx->public_key, 85 ctx->public_key,
86 (u8 *)ctx->shared_secret); 86 ctx->shared_secret);
87 87
88 buf = ctx->shared_secret; 88 buf = ctx->shared_secret;
89 } else { 89 } else {
90 ret = ecdh_make_pub_key(ctx->curve_id, ctx->ndigits, 90 ret = ecdh_make_pub_key(ctx->curve_id, ctx->ndigits,
91 (const u8 *)ctx->private_key, 91 ctx->private_key, ctx->public_key);
92 (u8 *)ctx->public_key);
93 buf = ctx->public_key; 92 buf = ctx->public_key;
94 /* Public part is a point thus it has both coordinates */ 93 /* Public part is a point thus it has both coordinates */
95 nbytes *= 2; 94 nbytes *= 2;