dh.c 8.6 KB

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