public_key.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* In-software asymmetric public-key crypto subtype
  2. *
  3. * See Documentation/crypto/asymmetric-keys.txt
  4. *
  5. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  6. * Written by David Howells (dhowells@redhat.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public Licence
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the Licence, or (at your option) any later version.
  12. */
  13. #define pr_fmt(fmt) "PKEY: "fmt
  14. #include <linux/module.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/scatterlist.h>
  20. #include <keys/asymmetric-subtype.h>
  21. #include <crypto/public_key.h>
  22. #include <crypto/akcipher.h>
  23. MODULE_LICENSE("GPL");
  24. /*
  25. * Provide a part of a description of the key for /proc/keys.
  26. */
  27. static void public_key_describe(const struct key *asymmetric_key,
  28. struct seq_file *m)
  29. {
  30. struct public_key *key = asymmetric_key->payload.data[asym_crypto];
  31. if (key)
  32. seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
  33. }
  34. /*
  35. * Destroy a public key algorithm key.
  36. */
  37. void public_key_free(struct public_key *key)
  38. {
  39. if (key) {
  40. kfree(key->key);
  41. kfree(key);
  42. }
  43. }
  44. EXPORT_SYMBOL_GPL(public_key_free);
  45. /*
  46. * Destroy a public key algorithm key.
  47. */
  48. static void public_key_destroy(void *payload0, void *payload3)
  49. {
  50. public_key_free(payload0);
  51. public_key_signature_free(payload3);
  52. }
  53. struct public_key_completion {
  54. struct completion completion;
  55. int err;
  56. };
  57. static void public_key_verify_done(struct crypto_async_request *req, int err)
  58. {
  59. struct public_key_completion *compl = req->data;
  60. if (err == -EINPROGRESS)
  61. return;
  62. compl->err = err;
  63. complete(&compl->completion);
  64. }
  65. /*
  66. * Verify a signature using a public key.
  67. */
  68. int public_key_verify_signature(const struct public_key *pkey,
  69. const struct public_key_signature *sig)
  70. {
  71. struct public_key_completion compl;
  72. struct crypto_akcipher *tfm;
  73. struct akcipher_request *req;
  74. struct scatterlist sig_sg, digest_sg;
  75. const char *alg_name;
  76. char alg_name_buf[CRYPTO_MAX_ALG_NAME];
  77. void *output;
  78. unsigned int outlen;
  79. int ret = -ENOMEM;
  80. pr_devel("==>%s()\n", __func__);
  81. BUG_ON(!pkey);
  82. BUG_ON(!sig);
  83. BUG_ON(!sig->digest);
  84. BUG_ON(!sig->s);
  85. alg_name = sig->pkey_algo;
  86. if (strcmp(sig->pkey_algo, "rsa") == 0) {
  87. /* The data wangled by the RSA algorithm is typically padded
  88. * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
  89. * sec 8.2].
  90. */
  91. if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
  92. "pkcs1pad(rsa,%s)", sig->hash_algo
  93. ) >= CRYPTO_MAX_ALG_NAME)
  94. return -EINVAL;
  95. alg_name = alg_name_buf;
  96. }
  97. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  98. if (IS_ERR(tfm))
  99. return PTR_ERR(tfm);
  100. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  101. if (!req)
  102. goto error_free_tfm;
  103. ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
  104. if (ret)
  105. goto error_free_req;
  106. outlen = crypto_akcipher_maxsize(tfm);
  107. output = kmalloc(outlen, GFP_KERNEL);
  108. if (!output)
  109. goto error_free_req;
  110. sg_init_one(&sig_sg, sig->s, sig->s_size);
  111. sg_init_one(&digest_sg, output, outlen);
  112. akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
  113. outlen);
  114. init_completion(&compl.completion);
  115. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  116. CRYPTO_TFM_REQ_MAY_SLEEP,
  117. public_key_verify_done, &compl);
  118. /* Perform the verification calculation. This doesn't actually do the
  119. * verification, but rather calculates the hash expected by the
  120. * signature and returns that to us.
  121. */
  122. ret = crypto_akcipher_verify(req);
  123. if ((ret == -EINPROGRESS) || (ret == -EBUSY)) {
  124. wait_for_completion(&compl.completion);
  125. ret = compl.err;
  126. }
  127. if (ret < 0)
  128. goto out_free_output;
  129. /* Do the actual verification step. */
  130. if (req->dst_len != sig->digest_size ||
  131. memcmp(sig->digest, output, sig->digest_size) != 0)
  132. ret = -EKEYREJECTED;
  133. out_free_output:
  134. kfree(output);
  135. error_free_req:
  136. akcipher_request_free(req);
  137. error_free_tfm:
  138. crypto_free_akcipher(tfm);
  139. pr_devel("<==%s() = %d\n", __func__, ret);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL_GPL(public_key_verify_signature);
  143. static int public_key_verify_signature_2(const struct key *key,
  144. const struct public_key_signature *sig)
  145. {
  146. const struct public_key *pk = key->payload.data[asym_crypto];
  147. return public_key_verify_signature(pk, sig);
  148. }
  149. /*
  150. * Public key algorithm asymmetric key subtype
  151. */
  152. struct asymmetric_key_subtype public_key_subtype = {
  153. .owner = THIS_MODULE,
  154. .name = "public_key",
  155. .name_len = sizeof("public_key") - 1,
  156. .describe = public_key_describe,
  157. .destroy = public_key_destroy,
  158. .verify_signature = public_key_verify_signature_2,
  159. };
  160. EXPORT_SYMBOL_GPL(public_key_subtype);