Merge tag 'rtc-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux
[linux-2.6-block.git] / security / apparmor / crypto.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
f8eb8a13
JJ
2/*
3 * AppArmor security module
4 *
5 * This file contains AppArmor policy loading interface function definitions.
6 *
7 * Copyright 2013 Canonical Ltd.
8 *
f8eb8a13
JJ
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
71ac7f62 14#include <crypto/hash.h>
f8eb8a13
JJ
15
16#include "include/apparmor.h"
17#include "include/crypto.h"
18
19static unsigned int apparmor_hash_size;
20
71ac7f62 21static struct crypto_shash *apparmor_tfm;
f8eb8a13
JJ
22
23unsigned int aa_hash_size(void)
24{
25 return apparmor_hash_size;
26}
27
5ac8c355
JJ
28char *aa_calc_hash(void *data, size_t len)
29{
9814448d 30 SHASH_DESC_ON_STACK(desc, apparmor_tfm);
5ac8c355
JJ
31 char *hash = NULL;
32 int error = -ENOMEM;
33
34 if (!apparmor_tfm)
35 return NULL;
36
37 hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
38 if (!hash)
39 goto fail;
40
9814448d 41 desc->tfm = apparmor_tfm;
5ac8c355 42
9814448d 43 error = crypto_shash_init(desc);
5ac8c355
JJ
44 if (error)
45 goto fail;
9814448d 46 error = crypto_shash_update(desc, (u8 *) data, len);
5ac8c355
JJ
47 if (error)
48 goto fail;
9814448d 49 error = crypto_shash_final(desc, hash);
5ac8c355
JJ
50 if (error)
51 goto fail;
52
53 return hash;
54
55fail:
56 kfree(hash);
57
58 return ERR_PTR(error);
59}
60
f8eb8a13
JJ
61int aa_calc_profile_hash(struct aa_profile *profile, u32 version, void *start,
62 size_t len)
63{
9814448d 64 SHASH_DESC_ON_STACK(desc, apparmor_tfm);
f8eb8a13 65 int error = -ENOMEM;
5ac8c355 66 __le32 le32_version = cpu_to_le32(version);
f8eb8a13 67
7616ac70
AB
68 if (!aa_g_hash_policy)
69 return 0;
70
f8eb8a13
JJ
71 if (!apparmor_tfm)
72 return 0;
73
f8eb8a13
JJ
74 profile->hash = kzalloc(apparmor_hash_size, GFP_KERNEL);
75 if (!profile->hash)
76 goto fail;
77
9814448d 78 desc->tfm = apparmor_tfm;
71ac7f62 79
9814448d 80 error = crypto_shash_init(desc);
f8eb8a13
JJ
81 if (error)
82 goto fail;
9814448d 83 error = crypto_shash_update(desc, (u8 *) &le32_version, 4);
f8eb8a13
JJ
84 if (error)
85 goto fail;
9814448d 86 error = crypto_shash_update(desc, (u8 *) start, len);
f8eb8a13
JJ
87 if (error)
88 goto fail;
9814448d 89 error = crypto_shash_final(desc, profile->hash);
f8eb8a13
JJ
90 if (error)
91 goto fail;
92
93 return 0;
94
95fail:
96 kfree(profile->hash);
97 profile->hash = NULL;
98
99 return error;
100}
101
102static int __init init_profile_hash(void)
103{
71ac7f62 104 struct crypto_shash *tfm;
f8eb8a13
JJ
105
106 if (!apparmor_initialized)
107 return 0;
108
3d234b33 109 tfm = crypto_alloc_shash("sha1", 0, 0);
f8eb8a13
JJ
110 if (IS_ERR(tfm)) {
111 int error = PTR_ERR(tfm);
112 AA_ERROR("failed to setup profile sha1 hashing: %d\n", error);
113 return error;
114 }
115 apparmor_tfm = tfm;
71ac7f62 116 apparmor_hash_size = crypto_shash_digestsize(apparmor_tfm);
f8eb8a13
JJ
117
118 aa_info_message("AppArmor sha1 policy hashing enabled");
119
120 return 0;
121}
122
123late_initcall(init_profile_hash);