dh.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* Crypto operations using stored keys
  2. *
  3. * Copyright (c) 2016, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/crypto.h>
  14. #include <crypto/hash.h>
  15. #include <crypto/kpp.h>
  16. #include <crypto/dh.h>
  17. #include <keys/user-type.h>
  18. #include "internal.h"
  19. static ssize_t dh_data_from_key(key_serial_t keyid, void **data)
  20. {
  21. struct key *key;
  22. key_ref_t key_ref;
  23. long status;
  24. ssize_t ret;
  25. key_ref = lookup_user_key(keyid, 0, KEY_NEED_READ);
  26. if (IS_ERR(key_ref)) {
  27. ret = -ENOKEY;
  28. goto error;
  29. }
  30. key = key_ref_to_ptr(key_ref);
  31. ret = -EOPNOTSUPP;
  32. if (key->type == &key_type_user) {
  33. down_read(&key->sem);
  34. status = key_validate(key);
  35. if (status == 0) {
  36. const struct user_key_payload *payload;
  37. uint8_t *duplicate;
  38. payload = user_key_payload_locked(key);
  39. duplicate = kmemdup(payload->data, payload->datalen,
  40. GFP_KERNEL);
  41. if (duplicate) {
  42. *data = duplicate;
  43. ret = payload->datalen;
  44. } else {
  45. ret = -ENOMEM;
  46. }
  47. }
  48. up_read(&key->sem);
  49. }
  50. key_put(key);
  51. error:
  52. return ret;
  53. }
  54. static void dh_free_data(struct dh *dh)
  55. {
  56. kzfree(dh->key);
  57. kzfree(dh->p);
  58. kzfree(dh->g);
  59. }
  60. struct dh_completion {
  61. struct completion completion;
  62. int err;
  63. };
  64. static void dh_crypto_done(struct crypto_async_request *req, int err)
  65. {
  66. struct dh_completion *compl = req->data;
  67. if (err == -EINPROGRESS)
  68. return;
  69. compl->err = err;
  70. complete(&compl->completion);
  71. }
  72. struct kdf_sdesc {
  73. struct shash_desc shash;
  74. char ctx[];
  75. };
  76. static int kdf_alloc(struct kdf_sdesc **sdesc_ret, char *hashname)
  77. {
  78. struct crypto_shash *tfm;
  79. struct kdf_sdesc *sdesc;
  80. int size;
  81. int err;
  82. /* allocate synchronous hash */
  83. tfm = crypto_alloc_shash(hashname, 0, 0);
  84. if (IS_ERR(tfm)) {
  85. pr_info("could not allocate digest TFM handle %s\n", hashname);
  86. return PTR_ERR(tfm);
  87. }
  88. err = -EINVAL;
  89. if (crypto_shash_digestsize(tfm) == 0)
  90. goto out_free_tfm;
  91. err = -ENOMEM;
  92. size = sizeof(struct shash_desc) + crypto_shash_descsize(tfm);
  93. sdesc = kmalloc(size, GFP_KERNEL);
  94. if (!sdesc)
  95. goto out_free_tfm;
  96. sdesc->shash.tfm = tfm;
  97. sdesc->shash.flags = 0x0;
  98. *sdesc_ret = sdesc;
  99. return 0;
  100. out_free_tfm:
  101. crypto_free_shash(tfm);
  102. return err;
  103. }
  104. static void kdf_dealloc(struct kdf_sdesc *sdesc)
  105. {
  106. if (!sdesc)
  107. return;
  108. if (sdesc->shash.tfm)
  109. crypto_free_shash(sdesc->shash.tfm);
  110. kzfree(sdesc);
  111. }
  112. /*
  113. * Implementation of the KDF in counter mode according to SP800-108 section 5.1
  114. * as well as SP800-56A section 5.8.1 (Single-step KDF).
  115. *
  116. * SP800-56A:
  117. * The src pointer is defined as Z || other info where Z is the shared secret
  118. * from DH and other info is an arbitrary string (see SP800-56A section
  119. * 5.8.1.2).
  120. */
  121. static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
  122. u8 *dst, unsigned int dlen, unsigned int zlen)
  123. {
  124. struct shash_desc *desc = &sdesc->shash;
  125. unsigned int h = crypto_shash_digestsize(desc->tfm);
  126. int err = 0;
  127. u8 *dst_orig = dst;
  128. __be32 counter = cpu_to_be32(1);
  129. while (dlen) {
  130. err = crypto_shash_init(desc);
  131. if (err)
  132. goto err;
  133. err = crypto_shash_update(desc, (u8 *)&counter, sizeof(__be32));
  134. if (err)
  135. goto err;
  136. if (zlen && h) {
  137. u8 tmpbuffer[h];
  138. size_t chunk = min_t(size_t, zlen, h);
  139. memset(tmpbuffer, 0, chunk);
  140. do {
  141. err = crypto_shash_update(desc, tmpbuffer,
  142. chunk);
  143. if (err)
  144. goto err;
  145. zlen -= chunk;
  146. chunk = min_t(size_t, zlen, h);
  147. } while (zlen);
  148. }
  149. if (src && slen) {
  150. err = crypto_shash_update(desc, src, slen);
  151. if (err)
  152. goto err;
  153. }
  154. if (dlen < h) {
  155. u8 tmpbuffer[h];
  156. err = crypto_shash_final(desc, tmpbuffer);
  157. if (err)
  158. goto err;
  159. memcpy(dst, tmpbuffer, dlen);
  160. memzero_explicit(tmpbuffer, h);
  161. return 0;
  162. } else {
  163. err = crypto_shash_final(desc, dst);
  164. if (err)
  165. goto err;
  166. dlen -= h;
  167. dst += h;
  168. counter = cpu_to_be32(be32_to_cpu(counter) + 1);
  169. }
  170. }
  171. return 0;
  172. err:
  173. memzero_explicit(dst_orig, dlen);
  174. return err;
  175. }
  176. static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc,
  177. char __user *buffer, size_t buflen,
  178. uint8_t *kbuf, size_t kbuflen, size_t lzero)
  179. {
  180. uint8_t *outbuf = NULL;
  181. int ret;
  182. outbuf = kmalloc(buflen, GFP_KERNEL);
  183. if (!outbuf) {
  184. ret = -ENOMEM;
  185. goto err;
  186. }
  187. ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, buflen, lzero);
  188. if (ret)
  189. goto err;
  190. ret = buflen;
  191. if (copy_to_user(buffer, outbuf, buflen) != 0)
  192. ret = -EFAULT;
  193. err:
  194. kzfree(outbuf);
  195. return ret;
  196. }
  197. long __keyctl_dh_compute(struct keyctl_dh_params __user *params,
  198. char __user *buffer, size_t buflen,
  199. struct keyctl_kdf_params *kdfcopy)
  200. {
  201. long ret;
  202. ssize_t dlen;
  203. int secretlen;
  204. int outlen;
  205. struct keyctl_dh_params pcopy;
  206. struct dh dh_inputs;
  207. struct scatterlist outsg;
  208. struct dh_completion compl;
  209. struct crypto_kpp *tfm;
  210. struct kpp_request *req;
  211. uint8_t *secret;
  212. uint8_t *outbuf;
  213. struct kdf_sdesc *sdesc = NULL;
  214. if (!params || (!buffer && buflen)) {
  215. ret = -EINVAL;
  216. goto out1;
  217. }
  218. if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) {
  219. ret = -EFAULT;
  220. goto out1;
  221. }
  222. if (kdfcopy) {
  223. char *hashname;
  224. if (memchr_inv(kdfcopy->__spare, 0, sizeof(kdfcopy->__spare))) {
  225. ret = -EINVAL;
  226. goto out1;
  227. }
  228. if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN ||
  229. kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) {
  230. ret = -EMSGSIZE;
  231. goto out1;
  232. }
  233. /* get KDF name string */
  234. hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME);
  235. if (IS_ERR(hashname)) {
  236. ret = PTR_ERR(hashname);
  237. goto out1;
  238. }
  239. /* allocate KDF from the kernel crypto API */
  240. ret = kdf_alloc(&sdesc, hashname);
  241. kfree(hashname);
  242. if (ret)
  243. goto out1;
  244. }
  245. memset(&dh_inputs, 0, sizeof(dh_inputs));
  246. dlen = dh_data_from_key(pcopy.prime, &dh_inputs.p);
  247. if (dlen < 0) {
  248. ret = dlen;
  249. goto out1;
  250. }
  251. dh_inputs.p_size = dlen;
  252. dlen = dh_data_from_key(pcopy.base, &dh_inputs.g);
  253. if (dlen < 0) {
  254. ret = dlen;
  255. goto out2;
  256. }
  257. dh_inputs.g_size = dlen;
  258. dlen = dh_data_from_key(pcopy.private, &dh_inputs.key);
  259. if (dlen < 0) {
  260. ret = dlen;
  261. goto out2;
  262. }
  263. dh_inputs.key_size = dlen;
  264. secretlen = crypto_dh_key_len(&dh_inputs);
  265. secret = kmalloc(secretlen, GFP_KERNEL);
  266. if (!secret) {
  267. ret = -ENOMEM;
  268. goto out2;
  269. }
  270. ret = crypto_dh_encode_key(secret, secretlen, &dh_inputs);
  271. if (ret)
  272. goto out3;
  273. tfm = crypto_alloc_kpp("dh", CRYPTO_ALG_TYPE_KPP, 0);
  274. if (IS_ERR(tfm)) {
  275. ret = PTR_ERR(tfm);
  276. goto out3;
  277. }
  278. ret = crypto_kpp_set_secret(tfm, secret, secretlen);
  279. if (ret)
  280. goto out4;
  281. outlen = crypto_kpp_maxsize(tfm);
  282. if (!kdfcopy) {
  283. /*
  284. * When not using a KDF, buflen 0 is used to read the
  285. * required buffer length
  286. */
  287. if (buflen == 0) {
  288. ret = outlen;
  289. goto out4;
  290. } else if (outlen > buflen) {
  291. ret = -EOVERFLOW;
  292. goto out4;
  293. }
  294. }
  295. outbuf = kzalloc(kdfcopy ? (outlen + kdfcopy->otherinfolen) : outlen,
  296. GFP_KERNEL);
  297. if (!outbuf) {
  298. ret = -ENOMEM;
  299. goto out4;
  300. }
  301. sg_init_one(&outsg, outbuf, outlen);
  302. req = kpp_request_alloc(tfm, GFP_KERNEL);
  303. if (!req) {
  304. ret = -ENOMEM;
  305. goto out5;
  306. }
  307. kpp_request_set_input(req, NULL, 0);
  308. kpp_request_set_output(req, &outsg, outlen);
  309. init_completion(&compl.completion);
  310. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  311. CRYPTO_TFM_REQ_MAY_SLEEP,
  312. dh_crypto_done, &compl);
  313. /*
  314. * For DH, generate_public_key and generate_shared_secret are
  315. * the same calculation
  316. */
  317. ret = crypto_kpp_generate_public_key(req);
  318. if (ret == -EINPROGRESS) {
  319. wait_for_completion(&compl.completion);
  320. ret = compl.err;
  321. if (ret)
  322. goto out6;
  323. }
  324. if (kdfcopy) {
  325. /*
  326. * Concatenate SP800-56A otherinfo past DH shared secret -- the
  327. * input to the KDF is (DH shared secret || otherinfo)
  328. */
  329. if (copy_from_user(outbuf + req->dst_len, kdfcopy->otherinfo,
  330. kdfcopy->otherinfolen) != 0) {
  331. ret = -EFAULT;
  332. goto out6;
  333. }
  334. ret = keyctl_dh_compute_kdf(sdesc, buffer, buflen, outbuf,
  335. req->dst_len + kdfcopy->otherinfolen,
  336. outlen - req->dst_len);
  337. } else if (copy_to_user(buffer, outbuf, req->dst_len) == 0) {
  338. ret = req->dst_len;
  339. } else {
  340. ret = -EFAULT;
  341. }
  342. out6:
  343. kpp_request_free(req);
  344. out5:
  345. kzfree(outbuf);
  346. out4:
  347. crypto_free_kpp(tfm);
  348. out3:
  349. kzfree(secret);
  350. out2:
  351. dh_free_data(&dh_inputs);
  352. out1:
  353. kdf_dealloc(sdesc);
  354. return ret;
  355. }
  356. long keyctl_dh_compute(struct keyctl_dh_params __user *params,
  357. char __user *buffer, size_t buflen,
  358. struct keyctl_kdf_params __user *kdf)
  359. {
  360. struct keyctl_kdf_params kdfcopy;
  361. if (!kdf)
  362. return __keyctl_dh_compute(params, buffer, buflen, NULL);
  363. if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0)
  364. return -EFAULT;
  365. return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
  366. }