aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--crypto/dh.c33
1 files changed, 13 insertions, 20 deletions
diff --git a/crypto/dh.c b/crypto/dh.c
index b1032a5c1bfa..aadaf36fb56f 100644
--- a/crypto/dh.c
+++ b/crypto/dh.c
@@ -21,19 +21,12 @@ struct dh_ctx {
21 MPI xa; 21 MPI xa;
22}; 22};
23 23
24static inline void dh_clear_params(struct dh_ctx *ctx) 24static void dh_clear_ctx(struct dh_ctx *ctx)
25{ 25{
26 mpi_free(ctx->p); 26 mpi_free(ctx->p);
27 mpi_free(ctx->g); 27 mpi_free(ctx->g);
28 ctx->p = NULL;
29 ctx->g = NULL;
30}
31
32static void dh_free_ctx(struct dh_ctx *ctx)
33{
34 dh_clear_params(ctx);
35 mpi_free(ctx->xa); 28 mpi_free(ctx->xa);
36 ctx->xa = NULL; 29 memset(ctx, 0, sizeof(*ctx));
37} 30}
38 31
39/* 32/*
@@ -71,10 +64,8 @@ static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
71 return -EINVAL; 64 return -EINVAL;
72 65
73 ctx->g = mpi_read_raw_data(params->g, params->g_size); 66 ctx->g = mpi_read_raw_data(params->g, params->g_size);
74 if (!ctx->g) { 67 if (!ctx->g)
75 mpi_free(ctx->p);
76 return -EINVAL; 68 return -EINVAL;
77 }
78 69
79 return 0; 70 return 0;
80} 71}
@@ -86,21 +77,23 @@ static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
86 struct dh params; 77 struct dh params;
87 78
88 /* Free the old MPI key if any */ 79 /* Free the old MPI key if any */
89 dh_free_ctx(ctx); 80 dh_clear_ctx(ctx);
90 81
91 if (crypto_dh_decode_key(buf, len, &params) < 0) 82 if (crypto_dh_decode_key(buf, len, &params) < 0)
92 return -EINVAL; 83 goto err_clear_ctx;
93 84
94 if (dh_set_params(ctx, &params) < 0) 85 if (dh_set_params(ctx, &params) < 0)
95 return -EINVAL; 86 goto err_clear_ctx;
96 87
97 ctx->xa = mpi_read_raw_data(params.key, params.key_size); 88 ctx->xa = mpi_read_raw_data(params.key, params.key_size);
98 if (!ctx->xa) { 89 if (!ctx->xa)
99 dh_clear_params(ctx); 90 goto err_clear_ctx;
100 return -EINVAL;
101 }
102 91
103 return 0; 92 return 0;
93
94err_clear_ctx:
95 dh_clear_ctx(ctx);
96 return -EINVAL;
104} 97}
105 98
106static int dh_compute_value(struct kpp_request *req) 99static int dh_compute_value(struct kpp_request *req)
@@ -158,7 +151,7 @@ static void dh_exit_tfm(struct crypto_kpp *tfm)
158{ 151{
159 struct dh_ctx *ctx = dh_get_ctx(tfm); 152 struct dh_ctx *ctx = dh_get_ctx(tfm);
160 153
161 dh_free_ctx(ctx); 154 dh_clear_ctx(ctx);
162} 155}
163 156
164static struct kpp_alg dh = { 157static struct kpp_alg dh = {