public_key.c 9.0 KB

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