Skip to content

Commit 48c6d8b

Browse files
nicstangeherbertx
authored andcommitted
crypto: dh - remove struct dh's ->q member
The only current user of the DH KPP algorithm, the keyctl(KEYCTL_DH_COMPUTE) syscall, doesn't set the domain parameter ->q in struct dh. Remove it and any associated (de)serialization code in crypto_dh_encode_key() and crypto_dh_decode_key. Adjust the encoded ->secret values in testmgr's DH test vectors accordingly. Note that the dh-generic implementation would have initialized its struct dh_ctx's ->q from the decoded struct dh's ->q, if present. If this struct dh_ctx's ->q would ever have been non-NULL, it would have enabled a full key validation as specified in NIST SP800-56A in dh_is_pubkey_valid(). However, as outlined above, ->q is always NULL in practice and the full key validation code is effectively dead. A later patch will make dh_is_pubkey_valid() to calculate Q from P on the fly, if possible, so don't remove struct dh_ctx's ->q now, but leave it there until that has happened. Signed-off-by: Nicolai Stange <nstange@suse.de> Reviewed-by: Hannes Reinecke <hare@suse.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 46ed526 commit 48c6d8b

File tree

4 files changed

+10
-33
lines changed

4 files changed

+10
-33
lines changed

crypto/dh.c

-6
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
6262
if (!ctx->p)
6363
return -EINVAL;
6464

65-
if (params->q && params->q_size) {
66-
ctx->q = mpi_read_raw_data(params->q, params->q_size);
67-
if (!ctx->q)
68-
return -EINVAL;
69-
}
70-
7165
ctx->g = mpi_read_raw_data(params->g, params->g_size);
7266
if (!ctx->g)
7367
return -EINVAL;

crypto/dh_helper.c

+4-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <crypto/dh.h>
1111
#include <crypto/kpp.h>
1212

13-
#define DH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 4 * sizeof(int))
13+
#define DH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 3 * sizeof(int))
1414

1515
static inline u8 *dh_pack_data(u8 *dst, u8 *end, const void *src, size_t size)
1616
{
@@ -28,7 +28,7 @@ static inline const u8 *dh_unpack_data(void *dst, const void *src, size_t size)
2828

2929
static inline unsigned int dh_data_size(const struct dh *p)
3030
{
31-
return p->key_size + p->p_size + p->q_size + p->g_size;
31+
return p->key_size + p->p_size + p->g_size;
3232
}
3333

3434
unsigned int crypto_dh_key_len(const struct dh *p)
@@ -53,11 +53,9 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params)
5353
ptr = dh_pack_data(ptr, end, &params->key_size,
5454
sizeof(params->key_size));
5555
ptr = dh_pack_data(ptr, end, &params->p_size, sizeof(params->p_size));
56-
ptr = dh_pack_data(ptr, end, &params->q_size, sizeof(params->q_size));
5756
ptr = dh_pack_data(ptr, end, &params->g_size, sizeof(params->g_size));
5857
ptr = dh_pack_data(ptr, end, params->key, params->key_size);
5958
ptr = dh_pack_data(ptr, end, params->p, params->p_size);
60-
ptr = dh_pack_data(ptr, end, params->q, params->q_size);
6159
ptr = dh_pack_data(ptr, end, params->g, params->g_size);
6260
if (ptr != end)
6361
return -EINVAL;
@@ -79,7 +77,6 @@ int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
7977

8078
ptr = dh_unpack_data(&params->key_size, ptr, sizeof(params->key_size));
8179
ptr = dh_unpack_data(&params->p_size, ptr, sizeof(params->p_size));
82-
ptr = dh_unpack_data(&params->q_size, ptr, sizeof(params->q_size));
8380
ptr = dh_unpack_data(&params->g_size, ptr, sizeof(params->g_size));
8481
if (secret.len != crypto_dh_key_len(params))
8582
return -EINVAL;
@@ -89,17 +86,15 @@ int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
8986
* some drivers assume otherwise.
9087
*/
9188
if (params->key_size > params->p_size ||
92-
params->g_size > params->p_size || params->q_size > params->p_size)
89+
params->g_size > params->p_size)
9390
return -EINVAL;
9491

9592
/* Don't allocate memory. Set pointers to data within
9693
* the given buffer
9794
*/
9895
params->key = (void *)ptr;
9996
params->p = (void *)(ptr + params->key_size);
100-
params->q = (void *)(ptr + params->key_size + params->p_size);
101-
params->g = (void *)(ptr + params->key_size + params->p_size +
102-
params->q_size);
97+
params->g = (void *)(ptr + params->key_size + params->p_size);
10398

10499
/*
105100
* Don't permit 'p' to be 0. It's not a prime number, and it's subject
@@ -109,10 +104,6 @@ int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
109104
if (memchr_inv(params->p, 0, params->p_size) == NULL)
110105
return -EINVAL;
111106

112-
/* It is permissible to not provide Q. */
113-
if (params->q_size == 0)
114-
params->q = NULL;
115-
116107
return 0;
117108
}
118109
EXPORT_SYMBOL_GPL(crypto_dh_decode_key);

crypto/testmgr.h

+6-10
Original file line numberDiff line numberDiff line change
@@ -1246,17 +1246,15 @@ static const struct kpp_testvec dh_tv_template[] = {
12461246
.secret =
12471247
#ifdef __LITTLE_ENDIAN
12481248
"\x01\x00" /* type */
1249-
"\x15\x02" /* len */
1249+
"\x11\x02" /* len */
12501250
"\x00\x01\x00\x00" /* key_size */
12511251
"\x00\x01\x00\x00" /* p_size */
1252-
"\x00\x00\x00\x00" /* q_size */
12531252
"\x01\x00\x00\x00" /* g_size */
12541253
#else
12551254
"\x00\x01" /* type */
1256-
"\x02\x15" /* len */
1255+
"\x02\x11" /* len */
12571256
"\x00\x00\x01\x00" /* key_size */
12581257
"\x00\x00\x01\x00" /* p_size */
1259-
"\x00\x00\x00\x00" /* q_size */
12601258
"\x00\x00\x00\x01" /* g_size */
12611259
#endif
12621260
/* xa */
@@ -1346,7 +1344,7 @@ static const struct kpp_testvec dh_tv_template[] = {
13461344
"\xd3\x34\x49\xad\x64\xa6\xb1\xc0\x59\x28\x75\x60\xa7\x8a\xb0\x11"
13471345
"\x56\x89\x42\x74\x11\xf5\xf6\x5e\x6f\x16\x54\x6a\xb1\x76\x4d\x50"
13481346
"\x8a\x68\xc1\x5b\x82\xb9\x0d\x00\x32\x50\xed\x88\x87\x48\x92\x17",
1349-
.secret_size = 533,
1347+
.secret_size = 529,
13501348
.b_public_size = 256,
13511349
.expected_a_public_size = 256,
13521350
.expected_ss_size = 256,
@@ -1355,17 +1353,15 @@ static const struct kpp_testvec dh_tv_template[] = {
13551353
.secret =
13561354
#ifdef __LITTLE_ENDIAN
13571355
"\x01\x00" /* type */
1358-
"\x15\x02" /* len */
1356+
"\x11\x02" /* len */
13591357
"\x00\x01\x00\x00" /* key_size */
13601358
"\x00\x01\x00\x00" /* p_size */
1361-
"\x00\x00\x00\x00" /* q_size */
13621359
"\x01\x00\x00\x00" /* g_size */
13631360
#else
13641361
"\x00\x01" /* type */
1365-
"\x02\x15" /* len */
1362+
"\x02\x11" /* len */
13661363
"\x00\x00\x01\x00" /* key_size */
13671364
"\x00\x00\x01\x00" /* p_size */
1368-
"\x00\x00\x00\x00" /* q_size */
13691365
"\x00\x00\x00\x01" /* g_size */
13701366
#endif
13711367
/* xa */
@@ -1455,7 +1451,7 @@ static const struct kpp_testvec dh_tv_template[] = {
14551451
"\x5e\x5a\x64\xbd\xf6\x85\x04\xe8\x28\x6a\xac\xef\xce\x19\x8e\x9a"
14561452
"\xfe\x75\xc0\x27\x69\xe3\xb3\x7b\x21\xa7\xb1\x16\xa4\x85\x23\xee"
14571453
"\xb0\x1b\x04\x6e\xbd\xab\x16\xde\xfd\x86\x6b\xa9\x95\xd7\x0b\xfd",
1458-
.secret_size = 533,
1454+
.secret_size = 529,
14591455
.b_public_size = 256,
14601456
.expected_a_public_size = 256,
14611457
.expected_ss_size = 256,

include/crypto/dh.h

-4
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,17 @@
2424
*
2525
* @key: Private DH key
2626
* @p: Diffie-Hellman parameter P
27-
* @q: Diffie-Hellman parameter Q
2827
* @g: Diffie-Hellman generator G
2928
* @key_size: Size of the private DH key
3029
* @p_size: Size of DH parameter P
31-
* @q_size: Size of DH parameter Q
3230
* @g_size: Size of DH generator G
3331
*/
3432
struct dh {
3533
void *key;
3634
void *p;
37-
void *q;
3835
void *g;
3936
unsigned int key_size;
4037
unsigned int p_size;
41-
unsigned int q_size;
4238
unsigned int g_size;
4339
};
4440

0 commit comments

Comments
 (0)