public_key.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. int ret;
  93. pr_devel("==>%s()\n", __func__);
  94. if (!pkey || !sig)
  95. return -EINVAL;
  96. if (pkey->key_is_private)
  97. return -EINVAL;
  98. memset(&info, '\0', sizeof(info));
  99. info.padding = image_get_padding_algo("pkcs-1.5");
  100. /*
  101. * Note: image_get_[checksum|crypto]_algo takes a string
  102. * argument like "<checksum>,<crypto>"
  103. * TODO: support other hash algorithms
  104. */
  105. if (strcmp(sig->pkey_algo, "rsa") || (sig->s_size * 8) != 2048) {
  106. pr_warn("Encryption is not RSA2048: %s%d\n",
  107. sig->pkey_algo, sig->s_size * 8);
  108. return -ENOPKG;
  109. }
  110. if (!strcmp(sig->hash_algo, "sha1")) {
  111. info.checksum = image_get_checksum_algo("sha1,rsa2048");
  112. info.name = "sha1,rsa2048";
  113. } else if (!strcmp(sig->hash_algo, "sha256")) {
  114. info.checksum = image_get_checksum_algo("sha256,rsa2048");
  115. info.name = "sha256,rsa2048";
  116. } else {
  117. pr_warn("unknown msg digest algo: %s\n", sig->hash_algo);
  118. return -ENOPKG;
  119. }
  120. info.crypto = image_get_crypto_algo(info.name);
  121. if (IS_ERR(info.checksum) || IS_ERR(info.crypto))
  122. return -ENOPKG;
  123. info.key = pkey->key;
  124. info.keylen = pkey->keylen;
  125. if (rsa_verify_with_pkey(&info, sig->digest, sig->s, sig->s_size))
  126. ret = -EKEYREJECTED;
  127. else
  128. ret = 0;
  129. pr_devel("<==%s() = %d\n", __func__, ret);
  130. return ret;
  131. }
  132. #else
  133. /*
  134. * Destroy a public key algorithm key.
  135. */
  136. static void public_key_destroy(void *payload0, void *payload3)
  137. {
  138. public_key_free(payload0);
  139. public_key_signature_free(payload3);
  140. }
  141. /*
  142. * Determine the crypto algorithm name.
  143. */
  144. static
  145. int software_key_determine_akcipher(const char *encoding,
  146. const char *hash_algo,
  147. const struct public_key *pkey,
  148. char alg_name[CRYPTO_MAX_ALG_NAME])
  149. {
  150. int n;
  151. if (strcmp(encoding, "pkcs1") == 0) {
  152. /* The data wangled by the RSA algorithm is typically padded
  153. * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
  154. * sec 8.2].
  155. */
  156. if (!hash_algo)
  157. n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
  158. "pkcs1pad(%s)",
  159. pkey->pkey_algo);
  160. else
  161. n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
  162. "pkcs1pad(%s,%s)",
  163. pkey->pkey_algo, hash_algo);
  164. return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
  165. }
  166. if (strcmp(encoding, "raw") == 0) {
  167. strcpy(alg_name, pkey->pkey_algo);
  168. return 0;
  169. }
  170. return -ENOPKG;
  171. }
  172. static u8 *pkey_pack_u32(u8 *dst, u32 val)
  173. {
  174. memcpy(dst, &val, sizeof(val));
  175. return dst + sizeof(val);
  176. }
  177. /*
  178. * Query information about a key.
  179. */
  180. static int software_key_query(const struct kernel_pkey_params *params,
  181. struct kernel_pkey_query *info)
  182. {
  183. struct crypto_akcipher *tfm;
  184. struct public_key *pkey = params->key->payload.data[asym_crypto];
  185. char alg_name[CRYPTO_MAX_ALG_NAME];
  186. u8 *key, *ptr;
  187. int ret, len;
  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. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  197. GFP_KERNEL);
  198. if (!key)
  199. goto error_free_tfm;
  200. memcpy(key, pkey->key, pkey->keylen);
  201. ptr = key + pkey->keylen;
  202. ptr = pkey_pack_u32(ptr, pkey->algo);
  203. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  204. memcpy(ptr, pkey->params, pkey->paramlen);
  205. if (pkey->key_is_private)
  206. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  207. else
  208. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  209. if (ret < 0)
  210. goto error_free_key;
  211. len = crypto_akcipher_maxsize(tfm);
  212. info->key_size = len * 8;
  213. info->max_data_size = len;
  214. info->max_sig_size = len;
  215. info->max_enc_size = len;
  216. info->max_dec_size = len;
  217. info->supported_ops = (KEYCTL_SUPPORTS_ENCRYPT |
  218. KEYCTL_SUPPORTS_VERIFY);
  219. if (pkey->key_is_private)
  220. info->supported_ops |= (KEYCTL_SUPPORTS_DECRYPT |
  221. KEYCTL_SUPPORTS_SIGN);
  222. ret = 0;
  223. error_free_key:
  224. kfree(key);
  225. error_free_tfm:
  226. crypto_free_akcipher(tfm);
  227. pr_devel("<==%s() = %d\n", __func__, ret);
  228. return ret;
  229. }
  230. /*
  231. * Do encryption, decryption and signing ops.
  232. */
  233. static int software_key_eds_op(struct kernel_pkey_params *params,
  234. const void *in, void *out)
  235. {
  236. const struct public_key *pkey = params->key->payload.data[asym_crypto];
  237. struct akcipher_request *req;
  238. struct crypto_akcipher *tfm;
  239. struct crypto_wait cwait;
  240. struct scatterlist in_sg, out_sg;
  241. char alg_name[CRYPTO_MAX_ALG_NAME];
  242. char *key, *ptr;
  243. int ret;
  244. pr_devel("==>%s()\n", __func__);
  245. ret = software_key_determine_akcipher(params->encoding,
  246. params->hash_algo,
  247. pkey, alg_name);
  248. if (ret < 0)
  249. return ret;
  250. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  251. if (IS_ERR(tfm))
  252. return PTR_ERR(tfm);
  253. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  254. if (!req)
  255. goto error_free_tfm;
  256. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  257. GFP_KERNEL);
  258. if (!key)
  259. goto error_free_req;
  260. memcpy(key, pkey->key, pkey->keylen);
  261. ptr = key + pkey->keylen;
  262. ptr = pkey_pack_u32(ptr, pkey->algo);
  263. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  264. memcpy(ptr, pkey->params, pkey->paramlen);
  265. if (pkey->key_is_private)
  266. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  267. else
  268. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  269. if (ret)
  270. goto error_free_key;
  271. sg_init_one(&in_sg, in, params->in_len);
  272. sg_init_one(&out_sg, out, params->out_len);
  273. akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
  274. params->out_len);
  275. crypto_init_wait(&cwait);
  276. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  277. CRYPTO_TFM_REQ_MAY_SLEEP,
  278. crypto_req_done, &cwait);
  279. /* Perform the encryption calculation. */
  280. switch (params->op) {
  281. case kernel_pkey_encrypt:
  282. ret = crypto_akcipher_encrypt(req);
  283. break;
  284. case kernel_pkey_decrypt:
  285. ret = crypto_akcipher_decrypt(req);
  286. break;
  287. case kernel_pkey_sign:
  288. ret = crypto_akcipher_sign(req);
  289. break;
  290. default:
  291. BUG();
  292. }
  293. ret = crypto_wait_req(ret, &cwait);
  294. if (ret == 0)
  295. ret = req->dst_len;
  296. error_free_key:
  297. kfree(key);
  298. error_free_req:
  299. akcipher_request_free(req);
  300. error_free_tfm:
  301. crypto_free_akcipher(tfm);
  302. pr_devel("<==%s() = %d\n", __func__, ret);
  303. return ret;
  304. }
  305. /*
  306. * Verify a signature using a public key.
  307. */
  308. int public_key_verify_signature(const struct public_key *pkey,
  309. const struct public_key_signature *sig)
  310. {
  311. struct crypto_wait cwait;
  312. struct crypto_akcipher *tfm;
  313. struct akcipher_request *req;
  314. struct scatterlist src_sg[2];
  315. char alg_name[CRYPTO_MAX_ALG_NAME];
  316. char *key, *ptr;
  317. int ret;
  318. pr_devel("==>%s()\n", __func__);
  319. BUG_ON(!pkey);
  320. BUG_ON(!sig);
  321. BUG_ON(!sig->s);
  322. ret = software_key_determine_akcipher(sig->encoding,
  323. sig->hash_algo,
  324. pkey, alg_name);
  325. if (ret < 0)
  326. return ret;
  327. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  328. if (IS_ERR(tfm))
  329. return PTR_ERR(tfm);
  330. ret = -ENOMEM;
  331. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  332. if (!req)
  333. goto error_free_tfm;
  334. key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
  335. GFP_KERNEL);
  336. if (!key)
  337. goto error_free_req;
  338. memcpy(key, pkey->key, pkey->keylen);
  339. ptr = key + pkey->keylen;
  340. ptr = pkey_pack_u32(ptr, pkey->algo);
  341. ptr = pkey_pack_u32(ptr, pkey->paramlen);
  342. memcpy(ptr, pkey->params, pkey->paramlen);
  343. if (pkey->key_is_private)
  344. ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
  345. else
  346. ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
  347. if (ret)
  348. goto error_free_key;
  349. sg_init_table(src_sg, 2);
  350. sg_set_buf(&src_sg[0], sig->s, sig->s_size);
  351. sg_set_buf(&src_sg[1], sig->digest, sig->digest_size);
  352. akcipher_request_set_crypt(req, src_sg, NULL, sig->s_size,
  353. sig->digest_size);
  354. crypto_init_wait(&cwait);
  355. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  356. CRYPTO_TFM_REQ_MAY_SLEEP,
  357. crypto_req_done, &cwait);
  358. ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
  359. error_free_key:
  360. kfree(key);
  361. error_free_req:
  362. akcipher_request_free(req);
  363. error_free_tfm:
  364. crypto_free_akcipher(tfm);
  365. pr_devel("<==%s() = %d\n", __func__, ret);
  366. if (WARN_ON_ONCE(ret > 0))
  367. ret = -EINVAL;
  368. return ret;
  369. }
  370. EXPORT_SYMBOL_GPL(public_key_verify_signature);
  371. static int public_key_verify_signature_2(const struct key *key,
  372. const struct public_key_signature *sig)
  373. {
  374. const struct public_key *pk = key->payload.data[asym_crypto];
  375. return public_key_verify_signature(pk, sig);
  376. }
  377. /*
  378. * Public key algorithm asymmetric key subtype
  379. */
  380. struct asymmetric_key_subtype public_key_subtype = {
  381. .owner = THIS_MODULE,
  382. .name = "public_key",
  383. .name_len = sizeof("public_key") - 1,
  384. .describe = public_key_describe,
  385. .destroy = public_key_destroy,
  386. .query = software_key_query,
  387. .eds_op = software_key_eds_op,
  388. .verify_signature = public_key_verify_signature_2,
  389. };
  390. EXPORT_SYMBOL_GPL(public_key_subtype);
  391. #endif /* !__UBOOT__ */