Skip to content

Commit

Permalink
crypto: dh - check validity of Z before export
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
smuellerDD authored and herbertx committed Jul 31, 2020
1 parent 4278e9d commit 90fa9ae
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions crypto/dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <crypto/internal/kpp.h>
#include <crypto/kpp.h>
#include <crypto/dh.h>
#include <linux/fips.h>
#include <linux/mpi.h>

struct dh_ctx {
Expand Down Expand Up @@ -179,6 +180,34 @@ static int dh_compute_value(struct kpp_request *req)
if (ret)
goto err_free_base;

/* SP800-56A rev3 5.7.1.1 check: Validation of shared secret */
if (fips_enabled && req->src) {
MPI pone;

/* z <= 1 */
if (mpi_cmp_ui(val, 1) < 1) {
ret = -EBADMSG;
goto err_free_base;
}

/* z == p - 1 */
pone = mpi_alloc(0);

if (!pone) {
ret = -ENOMEM;
goto err_free_base;
}

ret = mpi_sub_ui(pone, ctx->p, 1);
if (!ret && !mpi_cmp(pone, val))
ret = -EBADMSG;

mpi_free(pone);

if (ret)
goto err_free_base;
}

ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign);
if (ret)
goto err_free_base;
Expand Down

0 comments on commit 90fa9ae

Please sign in to comment.