public_key.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <keys/asymmetric-subtype.h>
  20. #include "public_key.h"
  21. MODULE_LICENSE("GPL");
  22. const char *const pkey_algo[PKEY_ALGO__LAST] = {
  23. [PKEY_ALGO_DSA] = "DSA",
  24. [PKEY_ALGO_RSA] = "RSA",
  25. };
  26. EXPORT_SYMBOL_GPL(pkey_algo);
  27. const char *const pkey_hash_algo[PKEY_HASH__LAST] = {
  28. [PKEY_HASH_MD4] = "md4",
  29. [PKEY_HASH_MD5] = "md5",
  30. [PKEY_HASH_SHA1] = "sha1",
  31. [PKEY_HASH_RIPE_MD_160] = "rmd160",
  32. [PKEY_HASH_SHA256] = "sha256",
  33. [PKEY_HASH_SHA384] = "sha384",
  34. [PKEY_HASH_SHA512] = "sha512",
  35. [PKEY_HASH_SHA224] = "sha224",
  36. };
  37. EXPORT_SYMBOL_GPL(pkey_hash_algo);
  38. const char *const pkey_id_type[PKEY_ID_TYPE__LAST] = {
  39. [PKEY_ID_PGP] = "PGP",
  40. [PKEY_ID_X509] = "X509",
  41. };
  42. EXPORT_SYMBOL_GPL(pkey_id_type);
  43. /*
  44. * Provide a part of a description of the key for /proc/keys.
  45. */
  46. static void public_key_describe(const struct key *asymmetric_key,
  47. struct seq_file *m)
  48. {
  49. struct public_key *key = asymmetric_key->payload.data;
  50. if (key)
  51. seq_printf(m, "%s.%s",
  52. pkey_id_type[key->id_type], key->algo->name);
  53. }
  54. /*
  55. * Destroy a public key algorithm key.
  56. */
  57. void public_key_destroy(void *payload)
  58. {
  59. struct public_key *key = payload;
  60. int i;
  61. if (key) {
  62. for (i = 0; i < ARRAY_SIZE(key->mpi); i++)
  63. mpi_free(key->mpi[i]);
  64. kfree(key);
  65. }
  66. }
  67. EXPORT_SYMBOL_GPL(public_key_destroy);
  68. /*
  69. * Verify a signature using a public key.
  70. */
  71. static int public_key_verify_signature(const struct key *key,
  72. const struct public_key_signature *sig)
  73. {
  74. const struct public_key *pk = key->payload.data;
  75. if (!pk->algo->verify_signature)
  76. return -ENOTSUPP;
  77. if (sig->nr_mpi != pk->algo->n_sig_mpi) {
  78. pr_debug("Signature has %u MPI not %u\n",
  79. sig->nr_mpi, pk->algo->n_sig_mpi);
  80. return -EINVAL;
  81. }
  82. return pk->algo->verify_signature(pk, sig);
  83. }
  84. /*
  85. * Public key algorithm asymmetric key subtype
  86. */
  87. struct asymmetric_key_subtype public_key_subtype = {
  88. .owner = THIS_MODULE,
  89. .name = "public_key",
  90. .describe = public_key_describe,
  91. .destroy = public_key_destroy,
  92. .verify_signature = public_key_verify_signature,
  93. };
  94. EXPORT_SYMBOL_GPL(public_key_subtype);