public_key.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* In-software asymmetric public-key crypto subtype
  3. *
  4. * See Documentation/crypto/asymmetric-keys.txt
  5. *
  6. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. */
  9. #define pr_fmt(fmt) "PKEY: "fmt
  10. #ifdef __UBOOT__
  11. #include <dm/devres.h>
  12. #include <linux/bug.h>
  13. #include <linux/compat.h>
  14. #include <linux/err.h>
  15. #else
  16. #include <linux/module.h>
  17. #include <linux/export.h>
  18. #endif
  19. #include <linux/kernel.h>
  20. #ifndef __UBOOT__
  21. #include <linux/slab.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/scatterlist.h>
  24. #include <keys/asymmetric-subtype.h>
  25. #endif
  26. #include <crypto/public_key.h>
  27. #ifndef __UBOOT__
  28. #include <crypto/akcipher.h>
  29. #endif
  30. MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
  31. MODULE_AUTHOR("Red Hat, Inc.");
  32. MODULE_LICENSE("GPL");
  33. #ifndef __UBOOT__
  34. /*
  35. * Provide a part of a description of the key for /proc/keys.
  36. */
  37. static void public_key_describe(const struct key *asymmetric_key,
  38. struct seq_file *m)
  39. {
  40. struct public_key *key = asymmetric_key->payload.data[asym_crypto];
  41. if (key)
  42. seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
  43. }
  44. #endif
  45. /*
  46. * Destroy a public key algorithm key.
  47. */
  48. void public_key_free(struct public_key *key)
  49. {
  50. if (key) {
  51. kfree(key->key);
  52. kfree(key->params);
  53. kfree(key);
  54. }
  55. }
  56. EXPORT_SYMBOL_GPL(public_key_free);
  57. #ifdef __UBOOT__
  58. /*
  59. * from <linux>/crypto/asymmetric_keys/signature.c
  60. *
  61. * Destroy a public key signature.
  62. */
  63. void public_key_signature_free(struct public_key_signature *sig)
  64. {
  65. int i;
  66. if (sig) {
  67. for (i = 0; i < ARRAY_SIZE(sig->auth_ids); i++)
  68. free(sig->auth_ids[i]);
  69. free(sig->s);
  70. free(sig->digest);
  71. free(sig);
  72. }
  73. }
  74. EXPORT_SYMBOL_GPL(public_key_signature_free);
  75. #else
  76. /*
  77. * Destroy a public key algorithm key.
  78. */
  79. static void public_key_destroy(void *payload0, void *payload3)
  80. {
  81. public_key_free(payload0);
  82. public_key_signature_free(payload3);
  83. }
  84. /*
  85. * Determine the crypto algorithm name.
  86. */
  87. static
  88. int software_key_determine_akcipher(const char *encoding,
  89. const char *hash_algo,
  90. const struct public_key *pkey,
  91. char alg_name[CRYPTO_MAX_ALG_NAME])
  92. {
  93. int n;
  94. if (strcmp(encoding, "pkcs1") == 0) {
  95. /* The data wangled by the RSA algorithm is typically padded
  96. * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
  97. * sec 8.2].
  98. */
  99. if (!hash_algo)
  100. n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
  101. "pkcs1pad(%s)",
  102. pkey->pkey_algo);
  103. else
  104. n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
  105. "pkcs1pad(%s,%s)",
  106. pkey->pkey_algo, hash_algo);
  107. return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
  108. }
  109. if (strcmp(encoding, "raw") == 0) {
  110. strcpy(alg_name, pkey->pkey_algo);
  111. return 0;
  112. }
  113. return -ENOPKG;
  114. }
  115. static u8 *pkey_pack_u32(u8 *dst, u32 val)
  116. {
  117. memcpy(dst, &val, sizeof(val));
  118. return dst + sizeof(val);
  119. }
  120. /*
  121. * Query information about a key.
  122. */
  123. static int software_key_query(const struct kernel_pkey_params *params,
  124. struct kernel_pkey_query *info)
  125. {
  126. struct crypto_akcipher *tfm;
  127. struct public_key *pkey = params->key->payload.data[asym_crypto];
  128. char alg_name[CRYPTO_MAX_ALG_NAME];
  129. u8 *key, *ptr;
  130. int ret, len;
  131. ret = software_key_determine_akcipher(params->encoding,
  132. params->hash_algo,
  133. pkey, alg_name);
  134. if (ret < 0)
  135. return ret;
  136. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  137. if (IS_ERR(tfm))
  138. return PTR_ERR(tfm);
  139. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  140. GFP_KERNEL);
  141. if (!key)
  142. goto error_free_tfm;
  143. memcpy(key, pkey->key, pkey->keylen);
  144. ptr = key + pkey->keylen;
  145. ptr = pkey_pack_u32(ptr, pkey->algo);
  146. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  147. memcpy(ptr, pkey->params, pkey->paramlen);
  148. if (pkey->key_is_private)
  149. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  150. else
  151. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  152. if (ret < 0)
  153. goto error_free_key;
  154. len = crypto_akcipher_maxsize(tfm);
  155. info->key_size = len * 8;
  156. info->max_data_size = len;
  157. info->max_sig_size = len;
  158. info->max_enc_size = len;
  159. info->max_dec_size = len;
  160. info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
  161. KEYCTL_SUPPORTS_VERIFY);
  162. if (pkey->key_is_private)
  163. info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
  164. KEYCTL_SUPPORTS_SIGN);
  165. ret = 0;
  166. error_free_key:
  167. kfree(key);
  168. error_free_tfm:
  169. crypto_free_akcipher(tfm);
  170. pr_devel("<==%s() = %d\n", __func__, ret);
  171. return ret;
  172. }
  173. /*
  174. * Do encryption, decryption and signing ops.
  175. */
  176. static int software_key_eds_op(struct kernel_pkey_params *params,
  177. const void *in, void *out)
  178. {
  179. const struct public_key *pkey = params->key->payload.data[asym_crypto];
  180. struct akcipher_request *req;
  181. struct crypto_akcipher *tfm;
  182. struct crypto_wait cwait;
  183. struct scatterlist in_sg, out_sg;
  184. char alg_name[CRYPTO_MAX_ALG_NAME];
  185. char *key, *ptr;
  186. int ret;
  187. pr_devel("==>%s()\n", __func__);
  188. ret = software_key_determine_akcipher(params->encoding,
  189. params->hash_algo,
  190. pkey, alg_name);
  191. if (ret < 0)
  192. return ret;
  193. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  194. if (IS_ERR(tfm))
  195. return PTR_ERR(tfm);
  196. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  197. if (!req)
  198. goto error_free_tfm;
  199. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  200. GFP_KERNEL);
  201. if (!key)
  202. goto error_free_req;
  203. memcpy(key, pkey->key, pkey->keylen);
  204. ptr = key + pkey->keylen;
  205. ptr = pkey_pack_u32(ptr, pkey->algo);
  206. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  207. memcpy(ptr, pkey->params, pkey->paramlen);
  208. if (pkey->key_is_private)
  209. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  210. else
  211. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  212. if (ret)
  213. goto error_free_key;
  214. sg_init_one(&in_sg, in, params->in_len);
  215. sg_init_one(&out_sg, out, params->out_len);
  216. akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
  217. params->out_len);
  218. crypto_init_wait(&cwait);
  219. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  220. CRYPTO_TFM_REQ_MAY_SLEEP,
  221. crypto_req_done, &cwait);
  222. /* Perform the encryption calculation. */
  223. switch (params->op) {
  224. case kernel_pkey_encrypt:
  225. ret = crypto_akcipher_encrypt(req);
  226. break;
  227. case kernel_pkey_decrypt:
  228. ret = crypto_akcipher_decrypt(req);
  229. break;
  230. case kernel_pkey_sign:
  231. ret = crypto_akcipher_sign(req);
  232. break;
  233. default:
  234. BUG();
  235. }
  236. ret = crypto_wait_req(ret, &cwait);
  237. if (ret == 0)
  238. ret = req->dst_len;
  239. error_free_key:
  240. kfree(key);
  241. error_free_req:
  242. akcipher_request_free(req);
  243. error_free_tfm:
  244. crypto_free_akcipher(tfm);
  245. pr_devel("<==%s() = %d\n", __func__, ret);
  246. return ret;
  247. }
  248. /*
  249. * Verify a signature using a public key.
  250. */
  251. int public_key_verify_signature(const struct public_key *pkey,
  252. const struct public_key_signature *sig)
  253. {
  254. struct crypto_wait cwait;
  255. struct crypto_akcipher *tfm;
  256. struct akcipher_request *req;
  257. struct scatterlist src_sg[2];
  258. char alg_name[CRYPTO_MAX_ALG_NAME];
  259. char *key, *ptr;
  260. int ret;
  261. pr_devel("==>%s()\n", __func__);
  262. BUG_ON(!pkey);
  263. BUG_ON(!sig);
  264. BUG_ON(!sig->s);
  265. ret = software_key_determine_akcipher(sig->encoding,
  266. sig->hash_algo,
  267. pkey, alg_name);
  268. if (ret < 0)
  269. return ret;
  270. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  271. if (IS_ERR(tfm))
  272. return PTR_ERR(tfm);
  273. ret = -ENOMEM;
  274. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  275. if (!req)
  276. goto error_free_tfm;
  277. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  278. GFP_KERNEL);
  279. if (!key)
  280. goto error_free_req;
  281. memcpy(key, pkey->key, pkey->keylen);
  282. ptr = key + pkey->keylen;
  283. ptr = pkey_pack_u32(ptr, pkey->algo);
  284. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  285. memcpy(ptr, pkey->params, pkey->paramlen);
  286. if (pkey->key_is_private)
  287. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  288. else
  289. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  290. if (ret)
  291. goto error_free_key;
  292. sg_init_table(src_sg, 2);
  293. sg_set_buf(&src_sg[0], sig->s, sig->s_size);
  294. sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
  295. akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
  296. sig->digest_size);
  297. crypto_init_wait(&cwait);
  298. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  299. CRYPTO_TFM_REQ_MAY_SLEEP,
  300. crypto_req_done, &cwait);
  301. ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
  302. error_free_key:
  303. kfree(key);
  304. error_free_req:
  305. akcipher_request_free(req);
  306. error_free_tfm:
  307. crypto_free_akcipher(tfm);
  308. pr_devel("<==%s() = %d\n", __func__, ret);
  309. if (WARN_ON_ONCE(ret > 0))
  310. ret = -EINVAL;
  311. return ret;
  312. }
  313. EXPORT_SYMBOL_GPL(public_key_verify_signature);
  314. static int public_key_verify_signature_2(const struct key *key,
  315. const struct public_key_signature *sig)
  316. {
  317. const struct public_key *pk = key->payload.data[asym_crypto];
  318. return public_key_verify_signature(pk, sig);
  319. }
  320. /*
  321. * Public key algorithm asymmetric key subtype
  322. */
  323. struct asymmetric_key_subtype public_key_subtype = {
  324. .owner = THIS_MODULE,
  325. .name = "public_key",
  326. .name_len = sizeof("public_key") - 1,
  327. .describe = public_key_describe,
  328. .destroy = public_key_destroy,
  329. .query = software_key_query,
  330. .eds_op = software_key_eds_op,
  331. .verify_signature = public_key_verify_signature_2,
  332. };
  333. EXPORT_SYMBOL_GPL(public_key_subtype);
  334. #endif /* !__UBOOT__ */