Debian Codegraph @

kernel-patches/4.15-rc3/kpp/v3-0006-crypto-DH-add-PKCS-3-parameter-handling.patch

raw
1 From c9d9dfe9a3a9262841322ac85483b31b849b1bfd Mon Sep 17 00:00:00 2001
2 From: Stephan Mueller <[email protected]>
3 Date: Sun, 7 Jan 2018 19:26:23 +0100
4 Subject: [PATCH v3 6/8] crypto: DH - add PKCS#3 parameter handling
5
6 DH parameters are commonly handled in PKCS#3 ASN.1 DER encoded files.
7 The addition adds support for the parsing of such DER encoded parameter
8 sets. After parsing, the data is added to the DH context data structure.
9
10 This support allows using of parameter sets generated with the openssl
11 dhparam command.
12
13 Note, the privateValueLength parameter defined in PKCS#3 is not parsed
14 as it is not required for the DH operation.
15
16 The PKCS#3 parser is able to parse data created by other tools, such as:
17
18 openssl dhparam -outform DER -out dhparam.der 2048
19
20 Signed-off-by: Stephan Mueller <[email protected]>
21 ---
22 crypto/Makefile | 7 ++++++-
23 crypto/dh.c | 33 +++++++++++++++++++++++++++---
24 crypto/dh_helper.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++---
25 crypto/dhparameter.asn1 | 4 ++++
26 include/crypto/dh.h | 21 +++++++++++++++++---
27 5 files changed, 108 insertions(+), 10 deletions(-)
28 create mode 100644 crypto/dhparameter.asn1
29
30 diff --git a/crypto/Makefile b/crypto/Makefile
31 index 526a4e5336af..c58f1fe2176a 100644
32 --- a/crypto/Makefile
33 +++ b/crypto/Makefile
34 @@ -31,7 +31,12 @@ obj-$(CONFIG_CRYPTO_HASH2) += crypto_hash.o
35 obj-$(CONFIG_CRYPTO_AKCIPHER2) += akcipher.o
36 obj-$(CONFIG_CRYPTO_KPP2) += kpp.o
37
38 -dh_generic-y := dh.o
39 +$(obj)/dhparameter-asn1.o: $(obj)/dhparameter-asn1.c $(obj)/dhparameter-asn1.h
40 +$(obj)/dh_helper.o: $(obj)/dhparameter-asn1.h
41 +clean-files += dhparameter-asn1.c dhparameter-asn1.h
42 +
43 +dh_generic-y := dhparameter-asn1.o
44 +dh_generic-y += dh.o
45 dh_generic-y += dh_helper.o
46 obj-$(CONFIG_CRYPTO_DH) += dh_generic.o
47
48 diff --git a/crypto/dh.c b/crypto/dh.c
49 index 7d3485682edc..0fcc55c93dff 100644
50 --- a/crypto/dh.c
51 +++ b/crypto/dh.c
52 @@ -21,14 +21,24 @@ struct dh_ctx {
53 MPI xa;
54 };
55
56 -static void dh_clear_ctx(struct dh_ctx *ctx)
57 +static inline void dh_clear_params(struct dh_ctx *ctx)
58 {
59 mpi_free(ctx->p);
60 mpi_free(ctx->g);
61 +}
62 +
63 +static inline void dh_clear_key(struct dh_ctx *ctx)
64 +{
65 mpi_free(ctx->xa);
66 memset(ctx, 0, sizeof(*ctx));
67 }
68
69 +static void dh_clear_ctx(struct dh_ctx *ctx)
70 +{
71 + dh_clear_params(ctx);
72 + dh_clear_key(ctx);
73 +}
74 +
75 /*
76 * If base is g we compute the public key
77 * ya = g^xa mod p; [RFC2631 sec 2.1.1]
78 @@ -71,6 +81,23 @@ static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
79 return 0;
80 }
81
82 +static int dh_set_params_pkcs3(struct crypto_kpp *tfm, const void *param,
83 + unsigned int param_len)
84 +{
85 + struct dh_ctx *ctx = dh_get_ctx(tfm);
86 + struct dh parsed_params;
87 + int ret;
88 +
89 + /* Free the old parameter if any */
90 + dh_clear_params(ctx);
91 +
92 + ret = dh_parse_params_pkcs3(&parsed_params, param, param_len);
93 + if (ret)
94 + return ret;
95 +
96 + return dh_set_params(ctx, &parsed_params);
97 +}
98 +
99 static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
100 unsigned int len)
101 {
102 @@ -78,7 +105,7 @@ static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
103 struct dh params;
104
105 /* Free the old MPI key if any */
106 - dh_clear_ctx(ctx);
107 + dh_clear_key(ctx);
108
109 if (crypto_dh_decode_key(buf, len, &params) < 0)
110 goto err_clear_ctx;
111 @@ -104,7 +131,6 @@ static int dh_compute_value(struct kpp_request *req)
112 MPI base, val = mpi_alloc(0);
113 int ret = 0;
114 int sign;
115 -
116 if (!val)
117 return -ENOMEM;
118
119 @@ -156,6 +182,7 @@ static void dh_exit_tfm(struct crypto_kpp *tfm)
120 }
121
122 static struct kpp_alg dh = {
123 + .set_params = dh_set_params_pkcs3,
124 .set_secret = dh_set_secret,
125 .generate_public_key = dh_compute_value,
126 .compute_shared_secret = dh_compute_value,
127 diff --git a/crypto/dh_helper.c b/crypto/dh_helper.c
128 index 24fdb2ecaa85..0c0a40e377fc 100644
129 --- a/crypto/dh_helper.c
130 +++ b/crypto/dh_helper.c
131 @@ -13,6 +13,7 @@
132 #include <linux/string.h>
133 #include <crypto/dh.h>
134 #include <crypto/kpp.h>
135 +#include "dhparameter-asn1.h"
136
137 #define DH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 3 * sizeof(int))
138
139 @@ -53,13 +54,22 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params)
140 if (len != crypto_dh_key_len(params))
141 return -EINVAL;
142
143 + /* Prevention of out-of-bounds access in decode code path */
144 + if ((!params->key && params->key_size) ||
145 + (!params->p && params->p_size) ||
146 + (!params->g && params->g_size))
147 + return -EINVAL;
148 +
149 ptr = dh_pack_data(ptr, &secret, sizeof(secret));
150 ptr = dh_pack_data(ptr, &params->key_size, sizeof(params->key_size));
151 ptr = dh_pack_data(ptr, &params->p_size, sizeof(params->p_size));
152 ptr = dh_pack_data(ptr, &params->g_size, sizeof(params->g_size));
153 - ptr = dh_pack_data(ptr, params->key, params->key_size);
154 - ptr = dh_pack_data(ptr, params->p, params->p_size);
155 - dh_pack_data(ptr, params->g, params->g_size);
156 + if (params->key)
157 + ptr = dh_pack_data(ptr, params->key, params->key_size);
158 + if (params->p)
159 + ptr = dh_pack_data(ptr, params->p, params->p_size);
160 + if (params->g)
161 + dh_pack_data(ptr, params->g, params->g_size);
162
163 return 0;
164 }
165 @@ -109,3 +119,40 @@ int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
166 return 0;
167 }
168 EXPORT_SYMBOL_GPL(crypto_dh_decode_key);
169 +
170 +int dh_get_p(void *context, size_t hdrlen, unsigned char tag, const void *value,
171 + size_t vlen)
172 +{
173 + struct dh *dh = context;
174 +
175 + /* invalid key provided */
176 + if (!value || !vlen)
177 + return -EINVAL;
178 +
179 + dh->p = value;
180 + dh->p_size = vlen;
181 +
182 + return 0;
183 +}
184 +
185 +int dh_get_g(void *context, size_t hdrlen, unsigned char tag,
186 + const void *value, size_t vlen)
187 +{
188 + struct dh *dh = context;
189 +
190 + /* invalid base provided */
191 + if (!value || !dh->p_size || !vlen || vlen > dh->p_size)
192 + return -EINVAL;
193 +
194 + dh->g = value;
195 + dh->g_size = vlen;
196 +
197 + return 0;
198 +}
199 +
200 +int dh_parse_params_pkcs3(struct dh *dh, const void *param,
201 + unsigned int param_len)
202 +{
203 + return asn1_ber_decoder(&dhparameter_decoder, dh, param, param_len);
204 +}
205 +EXPORT_SYMBOL_GPL(dh_parse_params_pkcs3);
206 diff --git a/crypto/dhparameter.asn1 b/crypto/dhparameter.asn1
207 new file mode 100644
208 index 000000000000..e9107c80478d
209 --- /dev/null
210 +++ b/crypto/dhparameter.asn1
211 @@ -0,0 +1,4 @@
212 +DHParameter ::= SEQUENCE {
213 + prime INTEGER ({ dh_get_p }),
214 + base INTEGER ({ dh_get_g })
215 +}
216 diff --git a/include/crypto/dh.h b/include/crypto/dh.h
217 index 71e1bb24d79f..80b36843c213 100644
218 --- a/include/crypto/dh.h
219 +++ b/include/crypto/dh.h
220 @@ -35,9 +35,9 @@
221 * @g_size: Size of DH generator G
222 */
223 struct dh {
224 - void *key;
225 - void *p;
226 - void *g;
227 + const void *key;
228 + const void *p;
229 + const void *g;
230 unsigned int key_size;
231 unsigned int p_size;
232 unsigned int g_size;
233 @@ -84,4 +84,19 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params);
234 */
235 int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params);
236
237 +/**
238 + * dh_parse_params_pkcs3() - decodes the PKCS#3 BER encoded buffer of the DH
239 + * parameters and stores in the provided struct dh,
240 + * pointers to the raw p and g parameters as is, so
241 + * that the caller can copy it or MPI parse it, etc.
242 + *
243 + * @dh: struct dh representation
244 + * @param: DH parameters in BER format following PKCS#3
245 + * @param_len: length of parameter buffer
246 + *
247 + * Return: 0 on success or error code in case of error
248 + */
249 +int dh_parse_params_pkcs3(struct dh *dh, const void *param,
250 + unsigned int param_len);
251 +
252 #endif
253 --
254 2.14.3
255