]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - security/apparmor/crypto.c
1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor policy loading interface function definitions.
7 * Copyright 2013 Canonical Ltd.
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
14 #include <crypto/hash.h>
16 #include "include/apparmor.h"
17 #include "include/crypto.h"
19 static unsigned int apparmor_hash_size
;
21 static struct crypto_shash
*apparmor_tfm
;
23 unsigned int aa_hash_size(void)
25 return apparmor_hash_size
;
28 void aa_snprint_hashstr(char *out
, unsigned char *hash
, unsigned int hsize
)
32 for (i
= 0; i
< hsize
; i
++)
33 sprintf(out
+ i
*2, "%.2x", hash
[i
]);
37 char *aa_asprint_hashstr(unsigned char *hash
, unsigned int hsize
, gfp_t gfp
)
39 char *buffer
= kmalloc(hsize
*2 + 1, gfp
);
42 aa_snprint_hashstr(buffer
, hash
, hsize
);
47 char *aa_calc_hash(void *data
, size_t len
)
49 SHASH_DESC_ON_STACK(desc
, apparmor_tfm
);
56 hash
= kzalloc(apparmor_hash_size
, GFP_KERNEL
);
60 desc
->tfm
= apparmor_tfm
;
62 error
= crypto_shash_init(desc
);
65 error
= crypto_shash_update(desc
, (u8
*) data
, len
);
68 error
= crypto_shash_final(desc
, hash
);
77 return ERR_PTR(error
);
80 int aa_calc_profile_hash(struct aa_profile
*profile
, u32 version
, void *start
,
83 SHASH_DESC_ON_STACK(desc
, apparmor_tfm
);
85 __le32 le32_version
= cpu_to_le32(version
);
87 if (!aa_g_hash_policy
)
93 profile
->hash
= kzalloc(apparmor_hash_size
, GFP_KERNEL
);
97 desc
->tfm
= apparmor_tfm
;
99 error
= crypto_shash_init(desc
);
102 error
= crypto_shash_update(desc
, (u8
*) &le32_version
, 4);
105 error
= crypto_shash_update(desc
, (u8
*) start
, len
);
108 error
= crypto_shash_final(desc
, profile
->hash
);
115 kfree(profile
->hash
);
116 profile
->hash
= NULL
;
121 static int __init
init_profile_hash(void)
123 struct crypto_shash
*tfm
;
125 if (!apparmor_initialized
)
128 tfm
= crypto_alloc_shash("sha1", 0, 0);
130 int error
= PTR_ERR(tfm
);
131 AA_ERROR("failed to setup profile sha1 hashing: %d\n", error
);
135 apparmor_hash_size
= crypto_shash_digestsize(apparmor_tfm
);
137 aa_info_message("AppArmor sha1 policy hashing enabled");
142 late_initcall(init_profile_hash
);