dh.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Crypto operations using stored keys
  3. *
  4. * Copyright (c) 2016, Intel Corporation
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/scatterlist.h>
  9. #include <linux/crypto.h>
  10. #include <crypto/hash.h>
  11. #include <crypto/kpp.h>
  12. #include <crypto/dh.h>
  13. #include <crypto/kdf_sp800108.h>
  14. #include <keys/user-type.h>
  15. #include "internal.h"
  16. static ssize_t dh_data_from_key(key_serial_t keyid, const void **data)
  17. {
  18. struct key *key;
  19. key_ref_t key_ref;
  20. long status;
  21. ssize_t ret;
  22. key_ref = lookup_user_key(keyid, 0, KEY_NEED_READ);
  23. if (IS_ERR(key_ref)) {
  24. ret = -ENOKEY;
  25. goto error;
  26. }
  27. key = key_ref_to_ptr(key_ref);
  28. ret = -EOPNOTSUPP;
  29. if (key->type == &key_type_user) {
  30. down_read(&key->sem);
  31. status = key_validate(key);
  32. if (status == 0) {
  33. const struct user_key_payload *payload;
  34. uint8_t *duplicate;
  35. payload = user_key_payload_locked(key);
  36. duplicate = kmemdup(payload->data, payload->datalen,
  37. GFP_KERNEL);
  38. if (duplicate) {
  39. *data = duplicate;
  40. ret = payload->datalen;
  41. } else {
  42. ret = -ENOMEM;
  43. }
  44. }
  45. up_read(&key->sem);
  46. }
  47. key_put(key);
  48. error:
  49. return ret;
  50. }
  51. static void dh_free_data(struct dh *dh)
  52. {
  53. kfree_sensitive(dh->key);
  54. kfree_sensitive(dh->p);
  55. kfree_sensitive(dh->g);
  56. }
  57. struct dh_completion {
  58. struct completion completion;
  59. int err;
  60. };
  61. static void dh_crypto_done(struct crypto_async_request *req, int err)
  62. {
  63. struct dh_completion *compl = req->data;
  64. if (err == -EINPROGRESS)
  65. return;
  66. compl->err = err;
  67. complete(&compl->completion);
  68. }
  69. static int kdf_alloc(struct crypto_shash **hash, char *hashname)
  70. {
  71. struct crypto_shash *tfm;
  72. /* allocate synchronous hash */
  73. tfm = crypto_alloc_shash(hashname, 0, 0);
  74. if (IS_ERR(tfm)) {
  75. pr_info("could not allocate digest TFM handle %s\n", hashname);
  76. return PTR_ERR(tfm);
  77. }
  78. if (crypto_shash_digestsize(tfm) == 0) {
  79. crypto_free_shash(tfm);
  80. return -EINVAL;
  81. }
  82. *hash = tfm;
  83. return 0;
  84. }
  85. static void kdf_dealloc(struct crypto_shash *hash)
  86. {
  87. if (hash)
  88. crypto_free_shash(hash);
  89. }
  90. static int keyctl_dh_compute_kdf(struct crypto_shash *hash,
  91. char __user *buffer, size_t buflen,
  92. uint8_t *kbuf, size_t kbuflen)
  93. {
  94. struct kvec kbuf_iov = { .iov_base = kbuf, .iov_len = kbuflen };
  95. uint8_t *outbuf = NULL;
  96. int ret;
  97. size_t outbuf_len = roundup(buflen, crypto_shash_digestsize(hash));
  98. outbuf = kmalloc(outbuf_len, GFP_KERNEL);
  99. if (!outbuf) {
  100. ret = -ENOMEM;
  101. goto err;
  102. }
  103. ret = crypto_kdf108_ctr_generate(hash, &kbuf_iov, 1, outbuf, outbuf_len);
  104. if (ret)
  105. goto err;
  106. ret = buflen;
  107. if (copy_to_user(buffer, outbuf, buflen) != 0)
  108. ret = -EFAULT;
  109. err:
  110. kfree_sensitive(outbuf);
  111. return ret;
  112. }
  113. long __keyctl_dh_compute(struct keyctl_dh_params __user *params,
  114. char __user *buffer, size_t buflen,
  115. struct keyctl_kdf_params *kdfcopy)
  116. {
  117. long ret;
  118. ssize_t dlen;
  119. int secretlen;
  120. int outlen;
  121. struct keyctl_dh_params pcopy;
  122. struct dh dh_inputs;
  123. struct scatterlist outsg;
  124. struct dh_completion compl;
  125. struct crypto_kpp *tfm;
  126. struct kpp_request *req;
  127. uint8_t *secret;
  128. uint8_t *outbuf;
  129. struct crypto_shash *hash = NULL;
  130. if (!params || (!buffer && buflen)) {
  131. ret = -EINVAL;
  132. goto out1;
  133. }
  134. if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) {
  135. ret = -EFAULT;
  136. goto out1;
  137. }
  138. if (kdfcopy) {
  139. char *hashname;
  140. if (memchr_inv(kdfcopy->__spare, 0, sizeof(kdfcopy->__spare))) {
  141. ret = -EINVAL;
  142. goto out1;
  143. }
  144. if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN ||
  145. kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) {
  146. ret = -EMSGSIZE;
  147. goto out1;
  148. }
  149. /* get KDF name string */
  150. hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME);
  151. if (IS_ERR(hashname)) {
  152. ret = PTR_ERR(hashname);
  153. goto out1;
  154. }
  155. /* allocate KDF from the kernel crypto API */
  156. ret = kdf_alloc(&hash, hashname);
  157. kfree(hashname);
  158. if (ret)
  159. goto out1;
  160. }
  161. memset(&dh_inputs, 0, sizeof(dh_inputs));
  162. dlen = dh_data_from_key(pcopy.prime, &dh_inputs.p);
  163. if (dlen < 0) {
  164. ret = dlen;
  165. goto out1;
  166. }
  167. dh_inputs.p_size = dlen;
  168. dlen = dh_data_from_key(pcopy.base, &dh_inputs.g);
  169. if (dlen < 0) {
  170. ret = dlen;
  171. goto out2;
  172. }
  173. dh_inputs.g_size = dlen;
  174. dlen = dh_data_from_key(pcopy.private, &dh_inputs.key);
  175. if (dlen < 0) {
  176. ret = dlen;
  177. goto out2;
  178. }
  179. dh_inputs.key_size = dlen;
  180. secretlen = crypto_dh_key_len(&dh_inputs);
  181. secret = kmalloc(secretlen, GFP_KERNEL);
  182. if (!secret) {
  183. ret = -ENOMEM;
  184. goto out2;
  185. }
  186. ret = crypto_dh_encode_key(secret, secretlen, &dh_inputs);
  187. if (ret)
  188. goto out3;
  189. tfm = crypto_alloc_kpp("dh", 0, 0);
  190. if (IS_ERR(tfm)) {
  191. ret = PTR_ERR(tfm);
  192. goto out3;
  193. }
  194. ret = crypto_kpp_set_secret(tfm, secret, secretlen);
  195. if (ret)
  196. goto out4;
  197. outlen = crypto_kpp_maxsize(tfm);
  198. if (!kdfcopy) {
  199. /*
  200. * When not using a KDF, buflen 0 is used to read the
  201. * required buffer length
  202. */
  203. if (buflen == 0) {
  204. ret = outlen;
  205. goto out4;
  206. } else if (outlen > buflen) {
  207. ret = -EOVERFLOW;
  208. goto out4;
  209. }
  210. }
  211. outbuf = kzalloc(kdfcopy ? (outlen + kdfcopy->otherinfolen) : outlen,
  212. GFP_KERNEL);
  213. if (!outbuf) {
  214. ret = -ENOMEM;
  215. goto out4;
  216. }
  217. sg_init_one(&outsg, outbuf, outlen);
  218. req = kpp_request_alloc(tfm, GFP_KERNEL);
  219. if (!req) {
  220. ret = -ENOMEM;
  221. goto out5;
  222. }
  223. kpp_request_set_input(req, NULL, 0);
  224. kpp_request_set_output(req, &outsg, outlen);
  225. init_completion(&compl.completion);
  226. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  227. CRYPTO_TFM_REQ_MAY_SLEEP,
  228. dh_crypto_done, &compl);
  229. /*
  230. * For DH, generate_public_key and generate_shared_secret are
  231. * the same calculation
  232. */
  233. ret = crypto_kpp_generate_public_key(req);
  234. if (ret == -EINPROGRESS) {
  235. wait_for_completion(&compl.completion);
  236. ret = compl.err;
  237. if (ret)
  238. goto out6;
  239. }
  240. if (kdfcopy) {
  241. /*
  242. * Concatenate SP800-56A otherinfo past DH shared secret -- the
  243. * input to the KDF is (DH shared secret || otherinfo)
  244. */
  245. if (copy_from_user(outbuf + req->dst_len, kdfcopy->otherinfo,
  246. kdfcopy->otherinfolen) != 0) {
  247. ret = -EFAULT;
  248. goto out6;
  249. }
  250. ret = keyctl_dh_compute_kdf(hash, buffer, buflen, outbuf,
  251. req->dst_len + kdfcopy->otherinfolen);
  252. } else if (copy_to_user(buffer, outbuf, req->dst_len) == 0) {
  253. ret = req->dst_len;
  254. } else {
  255. ret = -EFAULT;
  256. }
  257. out6:
  258. kpp_request_free(req);
  259. out5:
  260. kfree_sensitive(outbuf);
  261. out4:
  262. crypto_free_kpp(tfm);
  263. out3:
  264. kfree_sensitive(secret);
  265. out2:
  266. dh_free_data(&dh_inputs);
  267. out1:
  268. kdf_dealloc(hash);
  269. return ret;
  270. }
  271. long keyctl_dh_compute(struct keyctl_dh_params __user *params,
  272. char __user *buffer, size_t buflen,
  273. struct keyctl_kdf_params __user *kdf)
  274. {
  275. struct keyctl_kdf_params kdfcopy;
  276. if (!kdf)
  277. return __keyctl_dh_compute(params, buffer, buflen, NULL);
  278. if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0)
  279. return -EFAULT;
  280. return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
  281. }