Skip to content

Commit f66a211

Browse files
committed
crypto: dh - Make public key test FIPS-only
The function dh_is_pubkey_valid was added to for FIPS but it was only partially conditional to fips_enabled. In particular, the first test in the function relies on the last test to work properly, but the last test is only run in FIPS mode. Fix this inconsistency by making the whole function conditional on fips_enabled. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent a24e3b5 commit f66a211

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

crypto/dh.c

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
106106
*/
107107
static int dh_is_pubkey_valid(struct dh_ctx *ctx, MPI y)
108108
{
109+
MPI val, q;
110+
int ret;
111+
112+
if (!fips_enabled)
113+
return 0;
114+
109115
if (unlikely(!ctx->p))
110116
return -EINVAL;
111117

@@ -125,40 +131,35 @@ static int dh_is_pubkey_valid(struct dh_ctx *ctx, MPI y)
125131
*
126132
* For the safe-prime groups q = (p - 1)/2.
127133
*/
128-
if (fips_enabled) {
129-
MPI val, q;
130-
int ret;
134+
val = mpi_alloc(0);
135+
if (!val)
136+
return -ENOMEM;
131137

132-
val = mpi_alloc(0);
133-
if (!val)
134-
return -ENOMEM;
138+
q = mpi_alloc(mpi_get_nlimbs(ctx->p));
139+
if (!q) {
140+
mpi_free(val);
141+
return -ENOMEM;
142+
}
135143

136-
q = mpi_alloc(mpi_get_nlimbs(ctx->p));
137-
if (!q) {
138-
mpi_free(val);
139-
return -ENOMEM;
140-
}
144+
/*
145+
* ->p is odd, so no need to explicitly subtract one
146+
* from it before shifting to the right.
147+
*/
148+
mpi_rshift(q, ctx->p, 1);
141149

142-
/*
143-
* ->p is odd, so no need to explicitly subtract one
144-
* from it before shifting to the right.
145-
*/
146-
mpi_rshift(q, ctx->p, 1);
147-
148-
ret = mpi_powm(val, y, q, ctx->p);
149-
mpi_free(q);
150-
if (ret) {
151-
mpi_free(val);
152-
return ret;
153-
}
150+
ret = mpi_powm(val, y, q, ctx->p);
151+
mpi_free(q);
152+
if (ret) {
153+
mpi_free(val);
154+
return ret;
155+
}
154156

155-
ret = mpi_cmp_ui(val, 1);
157+
ret = mpi_cmp_ui(val, 1);
156158

157-
mpi_free(val);
159+
mpi_free(val);
158160

159-
if (ret != 0)
160-
return -EINVAL;
161-
}
161+
if (ret != 0)
162+
return -EINVAL;
162163

163164
return 0;
164165
}

0 commit comments

Comments
 (0)