public_key.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #ifdef __UBOOT__
  28. #include <image.h>
  29. #include <u-boot/rsa.h>
  30. #else
  31. #include <crypto/akcipher.h>
  32. #endif
  33. MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
  34. MODULE_AUTHOR("Red Hat, Inc.");
  35. MODULE_LICENSE("GPL");
  36. #ifndef __UBOOT__
  37. /*
  38. * Provide a part of a description of the key for /proc/keys.
  39. */
  40. static void public_key_describe(const struct key *asymmetric_key,
  41. struct seq_file *m)
  42. {
  43. struct public_key *key = asymmetric_key->payload.data[asym_crypto];
  44. if (key)
  45. seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
  46. }
  47. #endif
  48. /*
  49. * Destroy a public key algorithm key.
  50. */
  51. void public_key_free(struct public_key *key)
  52. {
  53. if (key) {
  54. kfree(key->key);
  55. kfree(key->params);
  56. kfree(key);
  57. }
  58. }
  59. EXPORT_SYMBOL_GPL(public_key_free);
  60. #ifdef __UBOOT__
  61. /*
  62. * from <linux>/crypto/asymmetric_keys/signature.c
  63. *
  64. * Destroy a public key signature.
  65. */
  66. void public_key_signature_free(struct public_key_signature *sig)
  67. {
  68. int i;
  69. if (sig) {
  70. for (i = 0; i < ARRAY_SIZE(sig->auth_ids); i++)
  71. free(sig->auth_ids[i]);
  72. free(sig->s);
  73. free(sig->digest);
  74. free(sig);
  75. }
  76. }
  77. EXPORT_SYMBOL_GPL(public_key_signature_free);
  78. /**
  79. * public_key_verify_signature - Verify a signature using a public key.
  80. *
  81. * @pkey: Public key
  82. * @sig: Signature
  83. *
  84. * Verify a signature, @sig, using a RSA public key, @pkey.
  85. *
  86. * Return: 0 - verified, non-zero error code - otherwise
  87. */
  88. int public_key_verify_signature(const struct public_key *pkey,
  89. const struct public_key_signature *sig)
  90. {
  91. struct image_sign_info info;
  92. char algo[256];
  93. int ret;
  94. pr_devel("==>%s()\n", __func__);
  95. if (!pkey || !sig)
  96. return -EINVAL;
  97. if (pkey->key_is_private)
  98. return -EINVAL;
  99. memset(&info, '\0', sizeof(info));
  100. memset(algo, 0, sizeof(algo));
  101. info.padding = image_get_padding_algo("pkcs-1.5");
  102. if (strcmp(sig->pkey_algo, "rsa")) {
  103. pr_err("Encryption is not RSA: %s\n", sig->pkey_algo);
  104. return -ENOPKG;
  105. }
  106. ret = snprintf(algo, sizeof(algo), "%s,%s%d", sig->hash_algo,
  107. sig->pkey_algo, sig->s_size * 8);
  108. if (ret >= sizeof(algo))
  109. return -EINVAL;
  110. info.checksum = image_get_checksum_algo((const char *)algo);
  111. info.name = (const char *)algo;
  112. info.crypto = image_get_crypto_algo(info.name);
  113. if (!info.checksum || !info.crypto) {
  114. pr_err("<%s> not supported on image_get_(checksum|crypto)_algo()\n",
  115. algo);
  116. return -ENOPKG;
  117. }
  118. info.key = pkey->key;
  119. info.keylen = pkey->keylen;
  120. if (rsa_verify_with_pkey(&info, sig->digest, sig->s, sig->s_size))
  121. ret = -EKEYREJECTED;
  122. else
  123. ret = 0;
  124. pr_devel("<==%s() = %d\n", __func__, ret);
  125. return ret;
  126. }
  127. #else
  128. /*
  129. * Destroy a public key algorithm key.
  130. */
  131. static void public_key_destroy(void *payload0, void *payload3)
  132. {
  133. public_key_free(payload0);
  134. public_key_signature_free(payload3);
  135. }
  136. /*
  137. * Determine the crypto algorithm name.
  138. */
  139. static
  140. int software_key_determine_akcipher(const char *encoding,
  141. const char *hash_algo,
  142. const struct public_key *pkey,
  143. char alg_name[CRYPTO_MAX_ALG_NAME])
  144. {
  145. int n;
  146. if (strcmp(encoding, "pkcs1") == 0) {
  147. /* The data wangled by the RSA algorithm is typically padded
  148. * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
  149. * sec 8.2].
  150. */
  151. if (!hash_algo)
  152. n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
  153. "pkcs1pad(%s)",
  154. pkey->pkey_algo);
  155. else
  156. n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
  157. "pkcs1pad(%s,%s)",
  158. pkey->pkey_algo, hash_algo);
  159. return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
  160. }
  161. if (strcmp(encoding, "raw") == 0) {
  162. strcpy(alg_name, pkey->pkey_algo);
  163. return 0;
  164. }
  165. return -ENOPKG;
  166. }
  167. static u8 *pkey_pack_u32(u8 *dst, u32 val)
  168. {
  169. memcpy(dst, &val, sizeof(val));
  170. return dst + sizeof(val);
  171. }
  172. /*
  173. * Query information about a key.
  174. */
  175. static int software_key_query(const struct kernel_pkey_params *params,
  176. struct kernel_pkey_query *info)
  177. {
  178. struct crypto_akcipher *tfm;
  179. struct public_key *pkey = params->key->payload.data[asym_crypto];
  180. char alg_name[CRYPTO_MAX_ALG_NAME];
  181. u8 *key, *ptr;
  182. int ret, len;
  183. ret = software_key_determine_akcipher(params->encoding,
  184. params->hash_algo,
  185. pkey, alg_name);
  186. if (ret < 0)
  187. return ret;
  188. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  189. if (IS_ERR(tfm))
  190. return PTR_ERR(tfm);
  191. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  192. GFP_KERNEL);
  193. if (!key)
  194. goto error_free_tfm;
  195. memcpy(key, pkey->key, pkey->keylen);
  196. ptr = key + pkey->keylen;
  197. ptr = pkey_pack_u32(ptr, pkey->algo);
  198. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  199. memcpy(ptr, pkey->params, pkey->paramlen);
  200. if (pkey->key_is_private)
  201. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  202. else
  203. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  204. if (ret < 0)
  205. goto error_free_key;
  206. len = crypto_akcipher_maxsize(tfm);
  207. info->key_size = len * 8;
  208. info->max_data_size = len;
  209. info->max_sig_size = len;
  210. info->max_enc_size = len;
  211. info->max_dec_size = len;
  212. info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
  213. KEYCTL_SUPPORTS_VERIFY);
  214. if (pkey->key_is_private)
  215. info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
  216. KEYCTL_SUPPORTS_SIGN);
  217. ret = 0;
  218. error_free_key:
  219. kfree(key);
  220. error_free_tfm:
  221. crypto_free_akcipher(tfm);
  222. pr_devel("<==%s() = %d\n", __func__, ret);
  223. return ret;
  224. }
  225. /*
  226. * Do encryption, decryption and signing ops.
  227. */
  228. static int software_key_eds_op(struct kernel_pkey_params *params,
  229. const void *in, void *out)
  230. {
  231. const struct public_key *pkey = params->key->payload.data[asym_crypto];
  232. struct akcipher_request *req;
  233. struct crypto_akcipher *tfm;
  234. struct crypto_wait cwait;
  235. struct scatterlist in_sg, out_sg;
  236. char alg_name[CRYPTO_MAX_ALG_NAME];
  237. char *key, *ptr;
  238. int ret;
  239. pr_devel("==>%s()\n", __func__);
  240. ret = software_key_determine_akcipher(params->encoding,
  241. params->hash_algo,
  242. pkey, alg_name);
  243. if (ret < 0)
  244. return ret;
  245. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  246. if (IS_ERR(tfm))
  247. return PTR_ERR(tfm);
  248. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  249. if (!req)
  250. goto error_free_tfm;
  251. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  252. GFP_KERNEL);
  253. if (!key)
  254. goto error_free_req;
  255. memcpy(key, pkey->key, pkey->keylen);
  256. ptr = key + pkey->keylen;
  257. ptr = pkey_pack_u32(ptr, pkey->algo);
  258. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  259. memcpy(ptr, pkey->params, pkey->paramlen);
  260. if (pkey->key_is_private)
  261. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  262. else
  263. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  264. if (ret)
  265. goto error_free_key;
  266. sg_init_one(&in_sg, in, params->in_len);
  267. sg_init_one(&out_sg, out, params->out_len);
  268. akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
  269. params->out_len);
  270. crypto_init_wait(&cwait);
  271. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  272. CRYPTO_TFM_REQ_MAY_SLEEP,
  273. crypto_req_done, &cwait);
  274. /* Perform the encryption calculation. */
  275. switch (params->op) {
  276. case kernel_pkey_encrypt:
  277. ret = crypto_akcipher_encrypt(req);
  278. break;
  279. case kernel_pkey_decrypt:
  280. ret = crypto_akcipher_decrypt(req);
  281. break;
  282. case kernel_pkey_sign:
  283. ret = crypto_akcipher_sign(req);
  284. break;
  285. default:
  286. BUG();
  287. }
  288. ret = crypto_wait_req(ret, &cwait);
  289. if (ret == 0)
  290. ret = req->dst_len;
  291. error_free_key:
  292. kfree(key);
  293. error_free_req:
  294. akcipher_request_free(req);
  295. error_free_tfm:
  296. crypto_free_akcipher(tfm);
  297. pr_devel("<==%s() = %d\n", __func__, ret);
  298. return ret;
  299. }
  300. /*
  301. * Verify a signature using a public key.
  302. */
  303. int public_key_verify_signature(const struct public_key *pkey,
  304. const struct public_key_signature *sig)
  305. {
  306. struct crypto_wait cwait;
  307. struct crypto_akcipher *tfm;
  308. struct akcipher_request *req;
  309. struct scatterlist src_sg[2];
  310. char alg_name[CRYPTO_MAX_ALG_NAME];
  311. char *key, *ptr;
  312. int ret;
  313. pr_devel("==>%s()\n", __func__);
  314. BUG_ON(!pkey);
  315. BUG_ON(!sig);
  316. BUG_ON(!sig->s);
  317. ret = software_key_determine_akcipher(sig->encoding,
  318. sig->hash_algo,
  319. pkey, alg_name);
  320. if (ret < 0)
  321. return ret;
  322. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  323. if (IS_ERR(tfm))
  324. return PTR_ERR(tfm);
  325. ret = -ENOMEM;
  326. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  327. if (!req)
  328. goto error_free_tfm;
  329. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  330. GFP_KERNEL);
  331. if (!key)
  332. goto error_free_req;
  333. memcpy(key, pkey->key, pkey->keylen);
  334. ptr = key + pkey->keylen;
  335. ptr = pkey_pack_u32(ptr, pkey->algo);
  336. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  337. memcpy(ptr, pkey->params, pkey->paramlen);
  338. if (pkey->key_is_private)
  339. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  340. else
  341. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  342. if (ret)
  343. goto error_free_key;
  344. sg_init_table(src_sg, 2);
  345. sg_set_buf(&src_sg[0], sig->s, sig->s_size);
  346. sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
  347. akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
  348. sig->digest_size);
  349. crypto_init_wait(&cwait);
  350. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  351. CRYPTO_TFM_REQ_MAY_SLEEP,
  352. crypto_req_done, &cwait);
  353. ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
  354. error_free_key:
  355. kfree(key);
  356. error_free_req:
  357. akcipher_request_free(req);
  358. error_free_tfm:
  359. crypto_free_akcipher(tfm);
  360. pr_devel("<==%s() = %d\n", __func__, ret);
  361. if (WARN_ON_ONCE(ret > 0))
  362. ret = -EINVAL;
  363. return ret;
  364. }
  365. EXPORT_SYMBOL_GPL(public_key_verify_signature);
  366. static int public_key_verify_signature_2(const struct key *key,
  367. const struct public_key_signature *sig)
  368. {
  369. const struct public_key *pk = key->payload.data[asym_crypto];
  370. return public_key_verify_signature(pk, sig);
  371. }
  372. /*
  373. * Public key algorithm asymmetric key subtype
  374. */
  375. struct asymmetric_key_subtype public_key_subtype = {
  376. .owner = THIS_MODULE,
  377. .name = "public_key",
  378. .name_len = sizeof("public_key") - 1,
  379. .describe = public_key_describe,
  380. .destroy = public_key_destroy,
  381. .query = software_key_query,
  382. .eds_op = software_key_eds_op,
  383. .verify_signature = public_key_verify_signature_2,
  384. };
  385. EXPORT_SYMBOL_GPL(public_key_subtype);
  386. #endif /* !__UBOOT__ */