public_key.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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->s);
  84. if (!sig->digest)
  85. return -ENOPKG;
  86. alg_name = sig->pkey_algo;
  87. if (strcmp(sig->pkey_algo, "rsa") == 0) {
  88. /* The data wangled by the RSA algorithm is typically padded
  89. * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
  90. * sec 8.2].
  91. */
  92. if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
  93. "pkcs1pad(rsa,%s)", sig->hash_algo
  94. ) >= CRYPTO_MAX_ALG_NAME)
  95. return -EINVAL;
  96. alg_name = alg_name_buf;
  97. }
  98. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  99. if (IS_ERR(tfm))
  100. return PTR_ERR(tfm);
  101. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  102. if (!req)
  103. goto error_free_tfm;
  104. ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
  105. if (ret)
  106. goto error_free_req;
  107. outlen = crypto_akcipher_maxsize(tfm);
  108. output = kmalloc(outlen, GFP_KERNEL);
  109. if (!output)
  110. goto error_free_req;
  111. sg_init_one(&sig_sg, sig->s, sig->s_size);
  112. sg_init_one(&digest_sg, output, outlen);
  113. akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
  114. outlen);
  115. init_completion(&compl.completion);
  116. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  117. CRYPTO_TFM_REQ_MAY_SLEEP,
  118. public_key_verify_done, &compl);
  119. /* Perform the verification calculation. This doesn't actually do the
  120. * verification, but rather calculates the hash expected by the
  121. * signature and returns that to us.
  122. */
  123. ret = crypto_akcipher_verify(req);
  124. if ((ret == -EINPROGRESS) || (ret == -EBUSY)) {
  125. wait_for_completion(&compl.completion);
  126. ret = compl.err;
  127. }
  128. if (ret < 0)
  129. goto out_free_output;
  130. /* Do the actual verification step. */
  131. if (req->dst_len != sig->digest_size ||
  132. memcmp(sig->digest, output, sig->digest_size) != 0)
  133. ret = -EKEYREJECTED;
  134. out_free_output:
  135. kfree(output);
  136. error_free_req:
  137. akcipher_request_free(req);
  138. error_free_tfm:
  139. crypto_free_akcipher(tfm);
  140. pr_devel("<==%s() = %d\n", __func__, ret);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL_GPL(public_key_verify_signature);
  144. static int public_key_verify_signature_2(const struct key *key,
  145. const struct public_key_signature *sig)
  146. {
  147. const struct public_key *pk = key->payload.data[asym_crypto];
  148. return public_key_verify_signature(pk, sig);
  149. }
  150. /*
  151. * Public key algorithm asymmetric key subtype
  152. */
  153. struct asymmetric_key_subtype public_key_subtype = {
  154. .owner = THIS_MODULE,
  155. .name = "public_key",
  156. .name_len = sizeof("public_key") - 1,
  157. .describe = public_key_describe,
  158. .destroy = public_key_destroy,
  159. .verify_signature = public_key_verify_signature_2,
  160. };
  161. EXPORT_SYMBOL_GPL(public_key_subtype);