dh.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. * 'dlen' must be a multiple of the digest size.
  122. */
  123. static int kdf_ctr(struct kdf_sdesc *sdesc, const u8 *src, unsigned int slen,
  124. u8 *dst, unsigned int dlen, unsigned int zlen)
  125. {
  126. struct shash_desc *desc = &sdesc->shash;
  127. unsigned int h = crypto_shash_digestsize(desc->tfm);
  128. int err = 0;
  129. u8 *dst_orig = dst;
  130. __be32 counter = cpu_to_be32(1);
  131. while (dlen) {
  132. err = crypto_shash_init(desc);
  133. if (err)
  134. goto err;
  135. err = crypto_shash_update(desc, (u8 *)&counter, sizeof(__be32));
  136. if (err)
  137. goto err;
  138. if (zlen && h) {
  139. u8 tmpbuffer[32];
  140. size_t chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
  141. memset(tmpbuffer, 0, chunk);
  142. do {
  143. err = crypto_shash_update(desc, tmpbuffer,
  144. chunk);
  145. if (err)
  146. goto err;
  147. zlen -= chunk;
  148. chunk = min_t(size_t, zlen, sizeof(tmpbuffer));
  149. } while (zlen);
  150. }
  151. if (src && slen) {
  152. err = crypto_shash_update(desc, src, slen);
  153. if (err)
  154. goto err;
  155. }
  156. err = crypto_shash_final(desc, dst);
  157. if (err)
  158. goto err;
  159. dlen -= h;
  160. dst += h;
  161. counter = cpu_to_be32(be32_to_cpu(counter) + 1);
  162. }
  163. return 0;
  164. err:
  165. memzero_explicit(dst_orig, dlen);
  166. return err;
  167. }
  168. static int keyctl_dh_compute_kdf(struct kdf_sdesc *sdesc,
  169. char __user *buffer, size_t buflen,
  170. uint8_t *kbuf, size_t kbuflen, size_t lzero)
  171. {
  172. uint8_t *outbuf = NULL;
  173. int ret;
  174. size_t outbuf_len = roundup(buflen,
  175. crypto_shash_digestsize(sdesc->shash.tfm));
  176. outbuf = kmalloc(outbuf_len, GFP_KERNEL);
  177. if (!outbuf) {
  178. ret = -ENOMEM;
  179. goto err;
  180. }
  181. ret = kdf_ctr(sdesc, kbuf, kbuflen, outbuf, outbuf_len, lzero);
  182. if (ret)
  183. goto err;
  184. ret = buflen;
  185. if (copy_to_user(buffer, outbuf, buflen) != 0)
  186. ret = -EFAULT;
  187. err:
  188. kzfree(outbuf);
  189. return ret;
  190. }
  191. long __keyctl_dh_compute(struct keyctl_dh_params __user *params,
  192. char __user *buffer, size_t buflen,
  193. struct keyctl_kdf_params *kdfcopy)
  194. {
  195. long ret;
  196. ssize_t dlen;
  197. int secretlen;
  198. int outlen;
  199. struct keyctl_dh_params pcopy;
  200. struct dh dh_inputs;
  201. struct scatterlist outsg;
  202. struct dh_completion compl;
  203. struct crypto_kpp *tfm;
  204. struct kpp_request *req;
  205. uint8_t *secret;
  206. uint8_t *outbuf;
  207. struct kdf_sdesc *sdesc = NULL;
  208. if (!params || (!buffer && buflen)) {
  209. ret = -EINVAL;
  210. goto out1;
  211. }
  212. if (copy_from_user(&pcopy, params, sizeof(pcopy)) != 0) {
  213. ret = -EFAULT;
  214. goto out1;
  215. }
  216. if (kdfcopy) {
  217. char *hashname;
  218. if (memchr_inv(kdfcopy->__spare, 0, sizeof(kdfcopy->__spare))) {
  219. ret = -EINVAL;
  220. goto out1;
  221. }
  222. if (buflen > KEYCTL_KDF_MAX_OUTPUT_LEN ||
  223. kdfcopy->otherinfolen > KEYCTL_KDF_MAX_OI_LEN) {
  224. ret = -EMSGSIZE;
  225. goto out1;
  226. }
  227. /* get KDF name string */
  228. hashname = strndup_user(kdfcopy->hashname, CRYPTO_MAX_ALG_NAME);
  229. if (IS_ERR(hashname)) {
  230. ret = PTR_ERR(hashname);
  231. goto out1;
  232. }
  233. /* allocate KDF from the kernel crypto API */
  234. ret = kdf_alloc(&sdesc, hashname);
  235. kfree(hashname);
  236. if (ret)
  237. goto out1;
  238. }
  239. memset(&dh_inputs, 0, sizeof(dh_inputs));
  240. dlen = dh_data_from_key(pcopy.prime, &dh_inputs.p);
  241. if (dlen < 0) {
  242. ret = dlen;
  243. goto out1;
  244. }
  245. dh_inputs.p_size = dlen;
  246. dlen = dh_data_from_key(pcopy.base, &dh_inputs.g);
  247. if (dlen < 0) {
  248. ret = dlen;
  249. goto out2;
  250. }
  251. dh_inputs.g_size = dlen;
  252. dlen = dh_data_from_key(pcopy.private, &dh_inputs.key);
  253. if (dlen < 0) {
  254. ret = dlen;
  255. goto out2;
  256. }
  257. dh_inputs.key_size = dlen;
  258. secretlen = crypto_dh_key_len(&dh_inputs);
  259. secret = kmalloc(secretlen, GFP_KERNEL);
  260. if (!secret) {
  261. ret = -ENOMEM;
  262. goto out2;
  263. }
  264. ret = crypto_dh_encode_key(secret, secretlen, &dh_inputs);
  265. if (ret)
  266. goto out3;
  267. tfm = crypto_alloc_kpp("dh", 0, 0);
  268. if (IS_ERR(tfm)) {
  269. ret = PTR_ERR(tfm);
  270. goto out3;
  271. }
  272. ret = crypto_kpp_set_secret(tfm, secret, secretlen);
  273. if (ret)
  274. goto out4;
  275. outlen = crypto_kpp_maxsize(tfm);
  276. if (!kdfcopy) {
  277. /*
  278. * When not using a KDF, buflen 0 is used to read the
  279. * required buffer length
  280. */
  281. if (buflen == 0) {
  282. ret = outlen;
  283. goto out4;
  284. } else if (outlen > buflen) {
  285. ret = -EOVERFLOW;
  286. goto out4;
  287. }
  288. }
  289. outbuf = kzalloc(kdfcopy ? (outlen + kdfcopy->otherinfolen) : outlen,
  290. GFP_KERNEL);
  291. if (!outbuf) {
  292. ret = -ENOMEM;
  293. goto out4;
  294. }
  295. sg_init_one(&outsg, outbuf, outlen);
  296. req = kpp_request_alloc(tfm, GFP_KERNEL);
  297. if (!req) {
  298. ret = -ENOMEM;
  299. goto out5;
  300. }
  301. kpp_request_set_input(req, NULL, 0);
  302. kpp_request_set_output(req, &outsg, outlen);
  303. init_completion(&compl.completion);
  304. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  305. CRYPTO_TFM_REQ_MAY_SLEEP,
  306. dh_crypto_done, &compl);
  307. /*
  308. * For DH, generate_public_key and generate_shared_secret are
  309. * the same calculation
  310. */
  311. ret = crypto_kpp_generate_public_key(req);
  312. if (ret == -EINPROGRESS) {
  313. wait_for_completion(&compl.completion);
  314. ret = compl.err;
  315. if (ret)
  316. goto out6;
  317. }
  318. if (kdfcopy) {
  319. /*
  320. * Concatenate SP800-56A otherinfo past DH shared secret -- the
  321. * input to the KDF is (DH shared secret || otherinfo)
  322. */
  323. if (copy_from_user(outbuf + req->dst_len, kdfcopy->otherinfo,
  324. kdfcopy->otherinfolen) != 0) {
  325. ret = -EFAULT;
  326. goto out6;
  327. }
  328. ret = keyctl_dh_compute_kdf(sdesc, buffer, buflen, outbuf,
  329. req->dst_len + kdfcopy->otherinfolen,
  330. outlen - req->dst_len);
  331. } else if (copy_to_user(buffer, outbuf, req->dst_len) == 0) {
  332. ret = req->dst_len;
  333. } else {
  334. ret = -EFAULT;
  335. }
  336. out6:
  337. kpp_request_free(req);
  338. out5:
  339. kzfree(outbuf);
  340. out4:
  341. crypto_free_kpp(tfm);
  342. out3:
  343. kzfree(secret);
  344. out2:
  345. dh_free_data(&dh_inputs);
  346. out1:
  347. kdf_dealloc(sdesc);
  348. return ret;
  349. }
  350. long keyctl_dh_compute(struct keyctl_dh_params __user *params,
  351. char __user *buffer, size_t buflen,
  352. struct keyctl_kdf_params __user *kdf)
  353. {
  354. struct keyctl_kdf_params kdfcopy;
  355. if (!kdf)
  356. return __keyctl_dh_compute(params, buffer, buflen, NULL);
  357. if (copy_from_user(&kdfcopy, kdf, sizeof(kdfcopy)) != 0)
  358. return -EFAULT;
  359. return __keyctl_dh_compute(params, buffer, buflen, &kdfcopy);
  360. }