crypto.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor policy loading interface function definitions.
  6. *
  7. * Copyright 2013 Canonical Ltd.
  8. *
  9. * Fns to provide a checksum of policy that has been loaded this can be
  10. * compared to userspace policy compiles to check loaded policy is what
  11. * it should be.
  12. */
  13. #include <crypto/sha2.h>
  14. #include "include/apparmor.h"
  15. #include "include/crypto.h"
  16. unsigned int aa_hash_size(void)
  17. {
  18. return SHA256_DIGEST_SIZE;
  19. }
  20. char *aa_calc_hash(void *data, size_t len)
  21. {
  22. char *hash;
  23. hash = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  24. if (!hash)
  25. return ERR_PTR(-ENOMEM);
  26. sha256(data, len, hash);
  27. return hash;
  28. }
  29. int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
  30. size_t len)
  31. {
  32. struct sha256_ctx sctx;
  33. __le32 le32_version = cpu_to_le32(version);
  34. if (!aa_g_hash_policy)
  35. return 0;
  36. profile->hash = kzalloc(SHA256_DIGEST_SIZE, GFP_KERNEL);
  37. if (!profile->hash)
  38. return -ENOMEM;
  39. sha256_init(&sctx);
  40. sha256_update(&sctx, (u8 *)&le32_version, 4);
  41. sha256_update(&sctx, (u8 *)start, len);
  42. sha256_final(&sctx, profile->hash);
  43. return 0;
  44. }
  45. int __init init_profile_hash(void)
  46. {
  47. if (apparmor_initialized)
  48. aa_info_message("AppArmor sha256 policy hashing enabled");
  49. return 0;
  50. }