public_key.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_DESCRIPTION("In-software asymmetric public-key subtype");
  24. MODULE_AUTHOR("Red Hat, Inc.");
  25. MODULE_LICENSE("GPL");
  26. /*
  27. * Provide a part of a description of the key for /proc/keys.
  28. */
  29. static void public_key_describe(const struct key *asymmetric_key,
  30. struct seq_file *m)
  31. {
  32. struct public_key *key = asymmetric_key->payload.data[asym_crypto];
  33. if (key)
  34. seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
  35. }
  36. /*
  37. * Destroy a public key algorithm key.
  38. */
  39. void public_key_free(struct public_key *key)
  40. {
  41. if (key) {
  42. kfree(key->key);
  43. kfree(key);
  44. }
  45. }
  46. EXPORT_SYMBOL_GPL(public_key_free);
  47. /*
  48. * Destroy a public key algorithm key.
  49. */
  50. static void public_key_destroy(void *payload0, void *payload3)
  51. {
  52. public_key_free(payload0);
  53. public_key_signature_free(payload3);
  54. }
  55. /*
  56. * Verify a signature using a public key.
  57. */
  58. int public_key_verify_signature(const struct public_key *pkey,
  59. const struct public_key_signature *sig)
  60. {
  61. struct crypto_wait cwait;
  62. struct crypto_akcipher *tfm;
  63. struct akcipher_request *req;
  64. struct scatterlist sig_sg, digest_sg;
  65. const char *alg_name;
  66. char alg_name_buf[CRYPTO_MAX_ALG_NAME];
  67. void *output;
  68. unsigned int outlen;
  69. int ret;
  70. pr_devel("==>%s()\n", __func__);
  71. BUG_ON(!pkey);
  72. BUG_ON(!sig);
  73. BUG_ON(!sig->s);
  74. if (!sig->digest)
  75. return -ENOPKG;
  76. alg_name = sig->pkey_algo;
  77. if (strcmp(sig->pkey_algo, "rsa") == 0) {
  78. /* The data wangled by the RSA algorithm is typically padded
  79. * and encoded in some manner, such as EMSA-PKCS1-1_5 [RFC3447
  80. * sec 8.2].
  81. */
  82. if (snprintf(alg_name_buf, CRYPTO_MAX_ALG_NAME,
  83. "pkcs1pad(rsa,%s)", sig->hash_algo
  84. ) >= CRYPTO_MAX_ALG_NAME)
  85. return -EINVAL;
  86. alg_name = alg_name_buf;
  87. }
  88. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  89. if (IS_ERR(tfm))
  90. return PTR_ERR(tfm);
  91. ret = -ENOMEM;
  92. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  93. if (!req)
  94. goto error_free_tfm;
  95. ret = crypto_akcipher_set_pub_key(tfm, pkey->key, pkey->keylen);
  96. if (ret)
  97. goto error_free_req;
  98. ret = -ENOMEM;
  99. outlen = crypto_akcipher_maxsize(tfm);
  100. output = kmalloc(outlen, GFP_KERNEL);
  101. if (!output)
  102. goto error_free_req;
  103. sg_init_one(&sig_sg, sig->s, sig->s_size);
  104. sg_init_one(&digest_sg, output, outlen);
  105. akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
  106. outlen);
  107. crypto_init_wait(&cwait);
  108. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  109. CRYPTO_TFM_REQ_MAY_SLEEP,
  110. crypto_req_done, &cwait);
  111. /* Perform the verification calculation. This doesn't actually do the
  112. * verification, but rather calculates the hash expected by the
  113. * signature and returns that to us.
  114. */
  115. ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
  116. if (ret)
  117. goto out_free_output;
  118. /* Do the actual verification step. */
  119. if (req->dst_len != sig->digest_size ||
  120. memcmp(sig->digest, output, sig->digest_size) != 0)
  121. ret = -EKEYREJECTED;
  122. out_free_output:
  123. kfree(output);
  124. error_free_req:
  125. akcipher_request_free(req);
  126. error_free_tfm:
  127. crypto_free_akcipher(tfm);
  128. pr_devel("<==%s() = %d\n", __func__, ret);
  129. if (WARN_ON_ONCE(ret > 0))
  130. ret = -EINVAL;
  131. return ret;
  132. }
  133. EXPORT_SYMBOL_GPL(public_key_verify_signature);
  134. static int public_key_verify_signature_2(const struct key *key,
  135. const struct public_key_signature *sig)
  136. {
  137. const struct public_key *pk = key->payload.data[asym_crypto];
  138. return public_key_verify_signature(pk, sig);
  139. }
  140. /*
  141. * Public key algorithm asymmetric key subtype
  142. */
  143. struct asymmetric_key_subtype public_key_subtype = {
  144. .owner = THIS_MODULE,
  145. .name = "public_key",
  146. .name_len = sizeof("public_key") - 1,
  147. .describe = public_key_describe,
  148. .destroy = public_key_destroy,
  149. .verify_signature = public_key_verify_signature_2,
  150. };
  151. EXPORT_SYMBOL_GPL(public_key_subtype);