Blame view

crypto/dh.c 5.37 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
2
3
4
5
  /*  Diffie-Hellman Key Agreement Method [RFC2631]
   *
   * Copyright (c) 2016, Intel Corporation
   * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
6
7
8
9
10
11
   */
  
  #include <linux/module.h>
  #include <crypto/internal/kpp.h>
  #include <crypto/kpp.h>
  #include <crypto/dh.h>
90fa9ae51   Stephan Müller   crypto: dh - chec...
12
  #include <linux/fips.h>
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
13
14
15
  #include <linux/mpi.h>
  
  struct dh_ctx {
e3fe0ae12   Stephan Mueller   crypto: dh - add ...
16
17
18
19
  	MPI p;	/* Value is guaranteed to be set. */
  	MPI q;	/* Value is optional. */
  	MPI g;	/* Value is guaranteed to be set. */
  	MPI xa;	/* Value is guaranteed to be set. */
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
20
  };
12d41a023   Eric Biggers   crypto: dh - Fix ...
21
  static void dh_clear_ctx(struct dh_ctx *ctx)
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
22
23
  {
  	mpi_free(ctx->p);
e3fe0ae12   Stephan Mueller   crypto: dh - add ...
24
  	mpi_free(ctx->q);
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
25
  	mpi_free(ctx->g);
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
26
  	mpi_free(ctx->xa);
12d41a023   Eric Biggers   crypto: dh - Fix ...
27
  	memset(ctx, 0, sizeof(*ctx));
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  }
  
  /*
   * If base is g we compute the public key
   *	ya = g^xa mod p; [RFC2631 sec 2.1.1]
   * else if base if the counterpart public key we compute the shared secret
   *	ZZ = yb^xa mod p; [RFC2631 sec 2.1.1]
   */
  static int _compute_val(const struct dh_ctx *ctx, MPI base, MPI val)
  {
  	/* val = base^xa mod p */
  	return mpi_powm(val, base, ctx->xa, ctx->p);
  }
  
  static inline struct dh_ctx *dh_get_ctx(struct crypto_kpp *tfm)
  {
  	return kpp_tfm_ctx(tfm);
  }
  
  static int dh_check_params_length(unsigned int p_len)
  {
  	return (p_len < 1536) ? -EINVAL : 0;
  }
  
  static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
  {
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
54
55
56
57
58
59
  	if (dh_check_params_length(params->p_size << 3))
  		return -EINVAL;
  
  	ctx->p = mpi_read_raw_data(params->p, params->p_size);
  	if (!ctx->p)
  		return -EINVAL;
e3fe0ae12   Stephan Mueller   crypto: dh - add ...
60
61
62
63
64
  	if (params->q && params->q_size) {
  		ctx->q = mpi_read_raw_data(params->q, params->q_size);
  		if (!ctx->q)
  			return -EINVAL;
  	}
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
65
  	ctx->g = mpi_read_raw_data(params->g, params->g_size);
12d41a023   Eric Biggers   crypto: dh - Fix ...
66
  	if (!ctx->g)
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
67
  		return -EINVAL;
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
68
69
70
  
  	return 0;
  }
5527dfb6d   Eric Biggers   crypto: kpp - con...
71
72
  static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
  			 unsigned int len)
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
73
74
75
  {
  	struct dh_ctx *ctx = dh_get_ctx(tfm);
  	struct dh params;
ee34e2644   Tudor-Dan Ambarus   crypto: dh - fix ...
76
  	/* Free the old MPI key if any */
12d41a023   Eric Biggers   crypto: dh - Fix ...
77
  	dh_clear_ctx(ctx);
ee34e2644   Tudor-Dan Ambarus   crypto: dh - fix ...
78

802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
79
  	if (crypto_dh_decode_key(buf, len, &params) < 0)
12d41a023   Eric Biggers   crypto: dh - Fix ...
80
  		goto err_clear_ctx;
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
81
82
  
  	if (dh_set_params(ctx, &params) < 0)
12d41a023   Eric Biggers   crypto: dh - Fix ...
83
  		goto err_clear_ctx;
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
84
85
  
  	ctx->xa = mpi_read_raw_data(params.key, params.key_size);
12d41a023   Eric Biggers   crypto: dh - Fix ...
86
87
  	if (!ctx->xa)
  		goto err_clear_ctx;
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
88
89
  
  	return 0;
12d41a023   Eric Biggers   crypto: dh - Fix ...
90
91
92
93
  
  err_clear_ctx:
  	dh_clear_ctx(ctx);
  	return -EINVAL;
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
94
  }
e3fe0ae12   Stephan Mueller   crypto: dh - add ...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  /*
   * SP800-56A public key verification:
   *
   * * If Q is provided as part of the domain paramenters, a full validation
   *   according to SP800-56A section 5.6.2.3.1 is performed.
   *
   * * If Q is not provided, a partial validation according to SP800-56A section
   *   5.6.2.3.2 is performed.
   */
  static int dh_is_pubkey_valid(struct dh_ctx *ctx, MPI y)
  {
  	if (unlikely(!ctx->p))
  		return -EINVAL;
  
  	/*
  	 * Step 1: Verify that 2 <= y <= p - 2.
  	 *
  	 * The upper limit check is actually y < p instead of y < p - 1
  	 * as the mpi_sub_ui function is yet missing.
  	 */
  	if (mpi_cmp_ui(y, 1) < 1 || mpi_cmp(y, ctx->p) >= 0)
  		return -EINVAL;
  
  	/* Step 2: Verify that 1 = y^q mod p */
  	if (ctx->q) {
  		MPI val = mpi_alloc(0);
  		int ret;
  
  		if (!val)
  			return -ENOMEM;
  
  		ret = mpi_powm(val, y, ctx->q, ctx->p);
  
  		if (ret) {
  			mpi_free(val);
  			return ret;
  		}
  
  		ret = mpi_cmp_ui(val, 1);
  
  		mpi_free(val);
  
  		if (ret != 0)
  			return -EINVAL;
  	}
  
  	return 0;
  }
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  static int dh_compute_value(struct kpp_request *req)
  {
  	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  	struct dh_ctx *ctx = dh_get_ctx(tfm);
  	MPI base, val = mpi_alloc(0);
  	int ret = 0;
  	int sign;
  
  	if (!val)
  		return -ENOMEM;
  
  	if (unlikely(!ctx->xa)) {
  		ret = -EINVAL;
  		goto err_free_val;
  	}
  
  	if (req->src) {
  		base = mpi_read_raw_from_sgl(req->src, req->src_len);
  		if (!base) {
8edda7d22   Mat Martineau   crypto: dh - Cons...
162
  			ret = -EINVAL;
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
163
164
  			goto err_free_val;
  		}
e3fe0ae12   Stephan Mueller   crypto: dh - add ...
165
166
  		ret = dh_is_pubkey_valid(ctx, base);
  		if (ret)
3fd8093b4   Gustavo A. R. Silva   crypto: dh - fix ...
167
  			goto err_free_base;
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
168
169
170
171
172
173
174
  	} else {
  		base = ctx->g;
  	}
  
  	ret = _compute_val(ctx, base, val);
  	if (ret)
  		goto err_free_base;
2ed5ba61c   Stephan Müller   crypto: dh - SP80...
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  	if (fips_enabled) {
  		/* SP800-56A rev3 5.7.1.1 check: Validation of shared secret */
  		if (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;
  
  		/* SP800-56A rev 3 5.6.2.1.3 key check */
  		} else {
  			if (dh_is_pubkey_valid(ctx, val)) {
  				ret = -EAGAIN;
  				goto err_free_val;
  			}
90fa9ae51   Stephan Müller   crypto: dh - chec...
209
  		}
90fa9ae51   Stephan Müller   crypto: dh - chec...
210
  	}
9b45b7bba   Herbert Xu   crypto: rsa - Gen...
211
  	ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign);
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
212
213
214
215
216
217
218
219
220
221
222
223
  	if (ret)
  		goto err_free_base;
  
  	if (sign < 0)
  		ret = -EBADMSG;
  err_free_base:
  	if (req->src)
  		mpi_free(base);
  err_free_val:
  	mpi_free(val);
  	return ret;
  }
7f6910507   Tudor-Dan Ambarus   crypto: dh - comp...
224
  static unsigned int dh_max_size(struct crypto_kpp *tfm)
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
225
226
227
228
229
230
231
232
233
  {
  	struct dh_ctx *ctx = dh_get_ctx(tfm);
  
  	return mpi_get_size(ctx->p);
  }
  
  static void dh_exit_tfm(struct crypto_kpp *tfm)
  {
  	struct dh_ctx *ctx = dh_get_ctx(tfm);
12d41a023   Eric Biggers   crypto: dh - Fix ...
234
  	dh_clear_ctx(ctx);
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  }
  
  static struct kpp_alg dh = {
  	.set_secret = dh_set_secret,
  	.generate_public_key = dh_compute_value,
  	.compute_shared_secret = dh_compute_value,
  	.max_size = dh_max_size,
  	.exit = dh_exit_tfm,
  	.base = {
  		.cra_name = "dh",
  		.cra_driver_name = "dh-generic",
  		.cra_priority = 100,
  		.cra_module = THIS_MODULE,
  		.cra_ctxsize = sizeof(struct dh_ctx),
  	},
  };
  
  static int dh_init(void)
  {
  	return crypto_register_kpp(&dh);
  }
  
  static void dh_exit(void)
  {
  	crypto_unregister_kpp(&dh);
  }
c4741b230   Eric Biggers   crypto: run initc...
261
  subsys_initcall(dh_init);
802c7f1c8   Salvatore Benedetto   crypto: dh - Add ...
262
263
264
265
  module_exit(dh_exit);
  MODULE_ALIAS_CRYPTO("dh");
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("DH generic algorithm");