1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Crypto operations using stored keys
4 * Copyright (c) 2016, Intel Corporation
7 #include <linux/slab.h>
8 #include <linux/uaccess.h>
9 #include <linux/scatterlist.h>
10 #include <linux/crypto.h>
11 #include <crypto/hash.h>
12 #include <crypto/kpp.h>
13 #include <crypto/dh.h>
14 #include <crypto/kdf_sp800108.h>
15 #include <keys/user-type.h>
18 static ssize_t
dh_data_from_key(key_serial_t keyid
, const void **data
)
25 key_ref
= lookup_user_key(keyid
, 0, KEY_NEED_READ
);
26 if (IS_ERR(key_ref
)) {
31 key
= key_ref_to_ptr(key_ref
);
34 if (key
->type
== &key_type_user
) {
36 status
= key_validate(key
);
38 const struct user_key_payload
*payload
;
41 payload
= user_key_payload_locked(key
);
43 duplicate
= kmemdup(payload
->data
, payload
->datalen
,
47 ret
= payload
->datalen
;
60 static void dh_free_data(struct dh
*dh
)
62 kfree_sensitive(dh
->key
);
63 kfree_sensitive(dh
->p
);
64 kfree_sensitive(dh
->g
);
67 static int kdf_alloc(struct crypto_shash
**hash
, char *hashname
)
69 struct crypto_shash
*tfm
;
71 /* allocate synchronous hash */
72 tfm
= crypto_alloc_shash(hashname
, 0, 0);
74 pr_info("could not allocate digest TFM handle %s\n", hashname
);
78 if (crypto_shash_digestsize(tfm
) == 0) {
79 crypto_free_shash(tfm
);
88 static void kdf_dealloc(struct crypto_shash
*hash
)
91 crypto_free_shash(hash
);
94 static int keyctl_dh_compute_kdf(struct crypto_shash
*hash
,
95 char __user
*buffer
, size_t buflen
,
96 uint8_t *kbuf
, size_t kbuflen
)
98 struct kvec kbuf_iov
= { .iov_base
= kbuf
, .iov_len
= kbuflen
};
99 uint8_t *outbuf
= NULL
;
101 size_t outbuf_len
= roundup(buflen
, crypto_shash_digestsize(hash
));
103 outbuf
= kmalloc(outbuf_len
, GFP_KERNEL
);
109 ret
= crypto_kdf108_ctr_generate(hash
, &kbuf_iov
, 1, outbuf
, outbuf_len
);
114 if (copy_to_user(buffer
, outbuf
, buflen
) != 0)
118 kfree_sensitive(outbuf
);
122 long __keyctl_dh_compute(struct keyctl_dh_params __user
*params
,
123 char __user
*buffer
, size_t buflen
,
124 struct keyctl_kdf_params
*kdfcopy
)
130 struct keyctl_dh_params pcopy
;
132 struct scatterlist outsg
;
133 DECLARE_CRYPTO_WAIT(compl);
134 struct crypto_kpp
*tfm
;
135 struct kpp_request
*req
;
138 struct crypto_shash
*hash
= NULL
;
140 if (!params
|| (!buffer
&& buflen
)) {
144 if (copy_from_user(&pcopy
, params
, sizeof(pcopy
)) != 0) {
152 if (memchr_inv(kdfcopy
->__spare
, 0, sizeof(kdfcopy
->__spare
))) {
157 if (buflen
> KEYCTL_KDF_MAX_OUTPUT_LEN
||
158 kdfcopy
->otherinfolen
> KEYCTL_KDF_MAX_OI_LEN
) {
163 /* get KDF name string */
164 hashname
= strndup_user(kdfcopy
->hashname
, CRYPTO_MAX_ALG_NAME
);
165 if (IS_ERR(hashname
)) {
166 ret
= PTR_ERR(hashname
);
170 /* allocate KDF from the kernel crypto API */
171 ret
= kdf_alloc(&hash
, hashname
);
177 memset(&dh_inputs
, 0, sizeof(dh_inputs
));
179 dlen
= dh_data_from_key(pcopy
.prime
, &dh_inputs
.p
);
184 dh_inputs
.p_size
= dlen
;
186 dlen
= dh_data_from_key(pcopy
.base
, &dh_inputs
.g
);
191 dh_inputs
.g_size
= dlen
;
193 dlen
= dh_data_from_key(pcopy
.private, &dh_inputs
.key
);
198 dh_inputs
.key_size
= dlen
;
200 secretlen
= crypto_dh_key_len(&dh_inputs
);
201 secret
= kmalloc(secretlen
, GFP_KERNEL
);
206 ret
= crypto_dh_encode_key(secret
, secretlen
, &dh_inputs
);
210 tfm
= crypto_alloc_kpp("dh", 0, 0);
216 ret
= crypto_kpp_set_secret(tfm
, secret
, secretlen
);
220 outlen
= crypto_kpp_maxsize(tfm
);
224 * When not using a KDF, buflen 0 is used to read the
225 * required buffer length
230 } else if (outlen
> buflen
) {
236 outbuf
= kzalloc(kdfcopy
? (outlen
+ kdfcopy
->otherinfolen
) : outlen
,
243 sg_init_one(&outsg
, outbuf
, outlen
);
245 req
= kpp_request_alloc(tfm
, GFP_KERNEL
);
251 kpp_request_set_input(req
, NULL
, 0);
252 kpp_request_set_output(req
, &outsg
, outlen
);
253 kpp_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
|
254 CRYPTO_TFM_REQ_MAY_SLEEP
,
255 crypto_req_done
, &compl);
258 * For DH, generate_public_key and generate_shared_secret are
259 * the same calculation
261 ret
= crypto_kpp_generate_public_key(req
);
262 ret
= crypto_wait_req(ret
, &compl);
268 * Concatenate SP800-56A otherinfo past DH shared secret -- the
269 * input to the KDF is (DH shared secret || otherinfo)
271 if (copy_from_user(outbuf
+ req
->dst_len
, kdfcopy
->otherinfo
,
272 kdfcopy
->otherinfolen
) != 0) {
277 ret
= keyctl_dh_compute_kdf(hash
, buffer
, buflen
, outbuf
,
278 req
->dst_len
+ kdfcopy
->otherinfolen
);
279 } else if (copy_to_user(buffer
, outbuf
, req
->dst_len
) == 0) {
286 kpp_request_free(req
);
288 kfree_sensitive(outbuf
);
290 crypto_free_kpp(tfm
);
292 kfree_sensitive(secret
);
294 dh_free_data(&dh_inputs
);
300 long keyctl_dh_compute(struct keyctl_dh_params __user
*params
,
301 char __user
*buffer
, size_t buflen
,
302 struct keyctl_kdf_params __user
*kdf
)
304 struct keyctl_kdf_params kdfcopy
;
307 return __keyctl_dh_compute(params
, buffer
, buflen
, NULL
);
309 if (copy_from_user(&kdfcopy
, kdf
, sizeof(kdfcopy
)) != 0)
312 return __keyctl_dh_compute(params
, buffer
, buflen
, &kdfcopy
);