Skip to content

Commit 90fa9ae

Browse files
smuellerDDherbertx
authored andcommitted
crypto: dh - check validity of Z before export
SP800-56A rev3 section 5.7.1.1 step 2 mandates that the validity of the calculated shared secret is verified before the data is returned to the caller. This patch adds the validation check. Signed-off-by: Stephan Mueller <[email protected]> Acked-by: Neil Horman <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 4278e9d commit 90fa9ae

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

crypto/dh.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <crypto/internal/kpp.h>
1010
#include <crypto/kpp.h>
1111
#include <crypto/dh.h>
12+
#include <linux/fips.h>
1213
#include <linux/mpi.h>
1314

1415
struct dh_ctx {
@@ -179,6 +180,34 @@ static int dh_compute_value(struct kpp_request *req)
179180
if (ret)
180181
goto err_free_base;
181182

183+
/* SP800-56A rev3 5.7.1.1 check: Validation of shared secret */
184+
if (fips_enabled && req->src) {
185+
MPI pone;
186+
187+
/* z <= 1 */
188+
if (mpi_cmp_ui(val, 1) < 1) {
189+
ret = -EBADMSG;
190+
goto err_free_base;
191+
}
192+
193+
/* z == p - 1 */
194+
pone = mpi_alloc(0);
195+
196+
if (!pone) {
197+
ret = -ENOMEM;
198+
goto err_free_base;
199+
}
200+
201+
ret = mpi_sub_ui(pone, ctx->p, 1);
202+
if (!ret && !mpi_cmp(pone, val))
203+
ret = -EBADMSG;
204+
205+
mpi_free(pone);
206+
207+
if (ret)
208+
goto err_free_base;
209+
}
210+
182211
ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign);
183212
if (ret)
184213
goto err_free_base;

0 commit comments

Comments
 (0)