signature.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Signature verification with an asymmetric key
  2. *
  3. * See Documentation/security/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) "SIG: "fmt
  14. #include <keys/asymmetric-subtype.h>
  15. #include <linux/export.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <crypto/public_key.h>
  19. #include "asymmetric_keys.h"
  20. /*
  21. * Destroy a public key signature.
  22. */
  23. void public_key_signature_free(struct public_key_signature *sig)
  24. {
  25. int i;
  26. if (sig) {
  27. for (i = 0; i < ARRAY_SIZE(sig->auth_ids); i++)
  28. kfree(sig->auth_ids[i]);
  29. kfree(sig->s);
  30. kfree(sig->digest);
  31. kfree(sig);
  32. }
  33. }
  34. EXPORT_SYMBOL_GPL(public_key_signature_free);
  35. /**
  36. * verify_signature - Initiate the use of an asymmetric key to verify a signature
  37. * @key: The asymmetric key to verify against
  38. * @sig: The signature to check
  39. *
  40. * Returns 0 if successful or else an error.
  41. */
  42. int verify_signature(const struct key *key,
  43. const struct public_key_signature *sig)
  44. {
  45. const struct asymmetric_key_subtype *subtype;
  46. int ret;
  47. pr_devel("==>%s()\n", __func__);
  48. if (key->type != &key_type_asymmetric)
  49. return -EINVAL;
  50. subtype = asymmetric_key_subtype(key);
  51. if (!subtype ||
  52. !key->payload.data[0])
  53. return -EINVAL;
  54. if (!subtype->verify_signature)
  55. return -ENOTSUPP;
  56. ret = subtype->verify_signature(key, sig);
  57. pr_devel("<==%s() = %d\n", __func__, ret);
  58. return ret;
  59. }
  60. EXPORT_SYMBOL_GPL(verify_signature);