Skip to content

Commit 47d3fd3

Browse files
ambarusherbertx
authored andcommitted
crypto: testmgr - add genkey kpp test
The test considers a party that already has a private-public key pair and a party that provides a NULL key. The kernel will generate the private-public key pair for the latter, computes the shared secret on both ends and verifies if it's the same. The explicit private-public key pair was copied from the previous test vector. Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 6755fd2 commit 47d3fd3

2 files changed

Lines changed: 112 additions & 11 deletions

File tree

‎crypto/testmgr.c‎

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,9 @@ static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
19971997
struct kpp_request *req;
19981998
void *input_buf = NULL;
19991999
void *output_buf = NULL;
2000+
void *a_public = NULL;
2001+
void *a_ss = NULL;
2002+
void *shared_secret = NULL;
20002003
struct tcrypt_result result;
20012004
unsigned int out_len_max;
20022005
int err = -ENOMEM;
@@ -2026,20 +2029,31 @@ static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
20262029
kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
20272030
tcrypt_complete, &result);
20282031

2029-
/* Compute public key */
2032+
/* Compute party A's public key */
20302033
err = wait_async_op(&result, crypto_kpp_generate_public_key(req));
20312034
if (err) {
2032-
pr_err("alg: %s: generate public key test failed. err %d\n",
2035+
pr_err("alg: %s: Party A: generate public key test failed. err %d\n",
20332036
alg, err);
20342037
goto free_output;
20352038
}
2036-
/* Verify calculated public key */
2037-
if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2038-
vec->expected_a_public_size)) {
2039-
pr_err("alg: %s: generate public key test failed. Invalid output\n",
2040-
alg);
2041-
err = -EINVAL;
2042-
goto free_output;
2039+
2040+
if (vec->genkey) {
2041+
/* Save party A's public key */
2042+
a_public = kzalloc(out_len_max, GFP_KERNEL);
2043+
if (!a_public) {
2044+
err = -ENOMEM;
2045+
goto free_output;
2046+
}
2047+
memcpy(a_public, sg_virt(req->dst), out_len_max);
2048+
} else {
2049+
/* Verify calculated public key */
2050+
if (memcmp(vec->expected_a_public, sg_virt(req->dst),
2051+
vec->expected_a_public_size)) {
2052+
pr_err("alg: %s: Party A: generate public key test failed. Invalid output\n",
2053+
alg);
2054+
err = -EINVAL;
2055+
goto free_output;
2056+
}
20432057
}
20442058

20452059
/* Calculate shared secret key by using counter part (b) public key. */
@@ -2058,24 +2072,64 @@ static int do_test_kpp(struct crypto_kpp *tfm, const struct kpp_testvec *vec,
20582072
tcrypt_complete, &result);
20592073
err = wait_async_op(&result, crypto_kpp_compute_shared_secret(req));
20602074
if (err) {
2061-
pr_err("alg: %s: compute shard secret test failed. err %d\n",
2075+
pr_err("alg: %s: Party A: compute shared secret test failed. err %d\n",
20622076
alg, err);
20632077
goto free_all;
20642078
}
2079+
2080+
if (vec->genkey) {
2081+
/* Save the shared secret obtained by party A */
2082+
a_ss = kzalloc(vec->expected_ss_size, GFP_KERNEL);
2083+
if (!a_ss) {
2084+
err = -ENOMEM;
2085+
goto free_all;
2086+
}
2087+
memcpy(a_ss, sg_virt(req->dst), vec->expected_ss_size);
2088+
2089+
/*
2090+
* Calculate party B's shared secret by using party A's
2091+
* public key.
2092+
*/
2093+
err = crypto_kpp_set_secret(tfm, vec->b_secret,
2094+
vec->b_secret_size);
2095+
if (err < 0)
2096+
goto free_all;
2097+
2098+
sg_init_one(&src, a_public, vec->expected_a_public_size);
2099+
sg_init_one(&dst, output_buf, out_len_max);
2100+
kpp_request_set_input(req, &src, vec->expected_a_public_size);
2101+
kpp_request_set_output(req, &dst, out_len_max);
2102+
kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2103+
tcrypt_complete, &result);
2104+
err = wait_async_op(&result,
2105+
crypto_kpp_compute_shared_secret(req));
2106+
if (err) {
2107+
pr_err("alg: %s: Party B: compute shared secret failed. err %d\n",
2108+
alg, err);
2109+
goto free_all;
2110+
}
2111+
2112+
shared_secret = a_ss;
2113+
} else {
2114+
shared_secret = (void *)vec->expected_ss;
2115+
}
2116+
20652117
/*
20662118
* verify shared secret from which the user will derive
20672119
* secret key by executing whatever hash it has chosen
20682120
*/
2069-
if (memcmp(vec->expected_ss, sg_virt(req->dst),
2121+
if (memcmp(shared_secret, sg_virt(req->dst),
20702122
vec->expected_ss_size)) {
20712123
pr_err("alg: %s: compute shared secret test failed. Invalid output\n",
20722124
alg);
20732125
err = -EINVAL;
20742126
}
20752127

20762128
free_all:
2129+
kfree(a_ss);
20772130
kfree(input_buf);
20782131
free_output:
2132+
kfree(a_public);
20792133
kfree(output_buf);
20802134
free_req:
20812135
kpp_request_free(req);

‎crypto/testmgr.h‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,16 @@ struct akcipher_testvec {
137137

138138
struct kpp_testvec {
139139
const unsigned char *secret;
140+
const unsigned char *b_secret;
140141
const unsigned char *b_public;
141142
const unsigned char *expected_a_public;
142143
const unsigned char *expected_ss;
143144
unsigned short secret_size;
145+
unsigned short b_secret_size;
144146
unsigned short b_public_size;
145147
unsigned short expected_a_public_size;
146148
unsigned short expected_ss_size;
149+
bool genkey;
147150
};
148151

149152
static const char zeroed_string[48];
@@ -840,6 +843,50 @@ static const struct kpp_testvec ecdh_tv_template[] = {
840843
.b_public_size = 64,
841844
.expected_a_public_size = 64,
842845
.expected_ss_size = 32
846+
}, {
847+
.secret =
848+
#ifdef __LITTLE_ENDIAN
849+
"\x02\x00" /* type */
850+
"\x08\x00" /* len */
851+
"\x02\x00" /* curve_id */
852+
"\x00\x00", /* key_size */
853+
#else
854+
"\x00\x02" /* type */
855+
"\x00\x08" /* len */
856+
"\x00\x02" /* curve_id */
857+
"\x00\x00", /* key_size */
858+
#endif
859+
.b_secret =
860+
#ifdef __LITTLE_ENDIAN
861+
"\x02\x00" /* type */
862+
"\x28\x00" /* len */
863+
"\x02\x00" /* curve_id */
864+
"\x20\x00" /* key_size */
865+
#else
866+
"\x00\x02" /* type */
867+
"\x00\x28" /* len */
868+
"\x00\x02" /* curve_id */
869+
"\x00\x20" /* key_size */
870+
#endif
871+
"\x24\xd1\x21\xeb\xe5\xcf\x2d\x83"
872+
"\xf6\x62\x1b\x6e\x43\x84\x3a\xa3"
873+
"\x8b\xe0\x86\xc3\x20\x19\xda\x92"
874+
"\x50\x53\x03\xe1\xc0\xea\xb8\x82",
875+
.b_public =
876+
"\x1a\x7f\xeb\x52\x00\xbd\x3c\x31"
877+
"\x7d\xb6\x70\xc1\x86\xa6\xc7\xc4"
878+
"\x3b\xc5\x5f\x6c\x6f\x58\x3c\xf5"
879+
"\xb6\x63\x82\x77\x33\x24\xa1\x5f"
880+
"\x6a\xca\x43\x6f\xf7\x7e\xff\x02"
881+
"\x37\x08\xcc\x40\x5e\x7a\xfd\x6a"
882+
"\x6a\x02\x6e\x41\x87\x68\x38\x77"
883+
"\xfa\xa9\x44\x43\x2d\xef\x09\xdf",
884+
.secret_size = 8,
885+
.b_secret_size = 40,
886+
.b_public_size = 64,
887+
.expected_a_public_size = 64,
888+
.expected_ss_size = 32,
889+
.genkey = true,
843890
}
844891
};
845892

0 commit comments

Comments
 (0)