Skip to content

Commit ea169a3

Browse files
smuellerDDherbertx
authored andcommitted
crypto: ecdh - add public key verification test
According to SP800-56A section 5.6.2.1, the public key to be processed for the ECDH operation shall be checked for appropriateness. When the public key is considered to be an ephemeral key, the partial validation test as defined in SP800-56A section 5.6.2.3.4 can be applied. The partial verification test requires the presence of the field elements of a and b. For the implemented NIST curves, b is defined in FIPS 186-4 appendix D.1.2. The element a is implicitly given with the Weierstrass equation given in D.1.2 where a = p - 3. Without the test, the NIST ACVP testing fails. After adding this check, the NIST ACVP testing passes. Signed-off-by: Stephan Mueller <smueller@chronox.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent e4e4730 commit ea169a3

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎crypto/ecc.c‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,36 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
10191019
return ret;
10201020
}
10211021

1022+
/* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
1023+
static int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
1024+
struct ecc_point *pk)
1025+
{
1026+
u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
1027+
1028+
/* Check 1: Verify key is not the zero point. */
1029+
if (ecc_point_is_zero(pk))
1030+
return -EINVAL;
1031+
1032+
/* Check 2: Verify key is in the range [1, p-1]. */
1033+
if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
1034+
return -EINVAL;
1035+
if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
1036+
return -EINVAL;
1037+
1038+
/* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
1039+
vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits); /* y^2 */
1040+
vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits); /* x^2 */
1041+
vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits); /* x^3 */
1042+
vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits); /* a·x */
1043+
vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
1044+
vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
1045+
if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
1046+
return -EINVAL;
1047+
1048+
return 0;
1049+
1050+
}
1051+
10221052
int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
10231053
const u64 *private_key, const u64 *public_key,
10241054
u64 *secret)
@@ -1046,16 +1076,20 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
10461076
goto out;
10471077
}
10481078

1079+
ecc_swap_digits(public_key, pk->x, ndigits);
1080+
ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1081+
ret = ecc_is_pubkey_valid_partial(curve, pk);
1082+
if (ret)
1083+
goto err_alloc_product;
1084+
1085+
ecc_swap_digits(private_key, priv, ndigits);
1086+
10491087
product = ecc_alloc_point(ndigits);
10501088
if (!product) {
10511089
ret = -ENOMEM;
10521090
goto err_alloc_product;
10531091
}
10541092

1055-
ecc_swap_digits(public_key, pk->x, ndigits);
1056-
ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1057-
ecc_swap_digits(private_key, priv, ndigits);
1058-
10591093
ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits);
10601094

10611095
ecc_swap_digits(product->x, secret, ndigits);

‎crypto/ecc_curve_defs.h‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ struct ecc_curve {
1313
struct ecc_point g;
1414
u64 *p;
1515
u64 *n;
16+
u64 *a;
17+
u64 *b;
1618
};
1719

18-
/* NIST P-192 */
20+
/* NIST P-192: a = p - 3 */
1921
static u64 nist_p192_g_x[] = { 0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull,
2022
0x188DA80EB03090F6ull };
2123
static u64 nist_p192_g_y[] = { 0x73F977A11E794811ull, 0x631011ED6B24CDD5ull,
@@ -24,6 +26,10 @@ static u64 nist_p192_p[] = { 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull,
2426
0xFFFFFFFFFFFFFFFFull };
2527
static u64 nist_p192_n[] = { 0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull,
2628
0xFFFFFFFFFFFFFFFFull };
29+
static u64 nist_p192_a[] = { 0xFFFFFFFFFFFFFFFCull, 0xFFFFFFFFFFFFFFFEull,
30+
0xFFFFFFFFFFFFFFFEull };
31+
static u64 nist_p192_b[] = { 0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull,
32+
0x64210519E59C80E7ull };
2733
static struct ecc_curve nist_p192 = {
2834
.name = "nist_192",
2935
.g = {
@@ -32,10 +38,12 @@ static struct ecc_curve nist_p192 = {
3238
.ndigits = 3,
3339
},
3440
.p = nist_p192_p,
35-
.n = nist_p192_n
41+
.n = nist_p192_n,
42+
.a = nist_p192_a,
43+
.b = nist_p192_b
3644
};
3745

38-
/* NIST P-256 */
46+
/* NIST P-256: a = p - 3 */
3947
static u64 nist_p256_g_x[] = { 0xF4A13945D898C296ull, 0x77037D812DEB33A0ull,
4048
0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull };
4149
static u64 nist_p256_g_y[] = { 0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull,
@@ -44,6 +52,10 @@ static u64 nist_p256_p[] = { 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull,
4452
0x0000000000000000ull, 0xFFFFFFFF00000001ull };
4553
static u64 nist_p256_n[] = { 0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull,
4654
0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull };
55+
static u64 nist_p256_a[] = { 0xFFFFFFFFFFFFFFFCull, 0x00000000FFFFFFFFull,
56+
0x0000000000000000ull, 0xFFFFFFFF00000001ull };
57+
static u64 nist_p256_b[] = { 0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull,
58+
0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull };
4759
static struct ecc_curve nist_p256 = {
4860
.name = "nist_256",
4961
.g = {
@@ -52,7 +64,9 @@ static struct ecc_curve nist_p256 = {
5264
.ndigits = 4,
5365
},
5466
.p = nist_p256_p,
55-
.n = nist_p256_n
67+
.n = nist_p256_n,
68+
.a = nist_p256_a,
69+
.b = nist_p256_b
5670
};
5771

5872
#endif

0 commit comments

Comments
 (0)