]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - security/apparmor/crypto.c
2 * AppArmor security module
4 * This file contains AppArmor policy loading interface function definitions.
6 * Copyright 2013 Canonical Ltd.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
13 * Fns to provide a checksum of policy that has been loaded this can be
14 * compared to userspace policy compiles to check loaded policy is what
18 #include <crypto/hash.h>
20 #include "include/apparmor.h"
21 #include "include/crypto.h"
23 static unsigned int apparmor_hash_size
;
25 static struct crypto_shash
*apparmor_tfm
;
27 unsigned int aa_hash_size(void)
29 return apparmor_hash_size
;
32 void aa_snprint_hashstr(char *out
, unsigned char *hash
, unsigned int hsize
)
36 for (i
= 0; i
< hsize
; i
++)
37 sprintf(out
+ i
*2, "%.2x", hash
[i
]);
41 char *aa_asprint_hashstr(unsigned char *hash
, unsigned int hsize
, gfp_t gfp
)
43 char *buffer
= kmalloc(hsize
*2 + 1, gfp
);
46 aa_snprint_hashstr(buffer
, hash
, hsize
);
51 char *aa_calc_hash(void *data
, size_t len
)
53 SHASH_DESC_ON_STACK(desc
, apparmor_tfm
);
60 hash
= kzalloc(apparmor_hash_size
, GFP_KERNEL
);
64 desc
->tfm
= apparmor_tfm
;
67 error
= crypto_shash_init(desc
);
70 error
= crypto_shash_update(desc
, (u8
*) data
, len
);
73 error
= crypto_shash_final(desc
, hash
);
82 return ERR_PTR(error
);
85 int aa_calc_profile_hash(struct aa_profile
*profile
, u32 version
, void *start
,
88 SHASH_DESC_ON_STACK(desc
, apparmor_tfm
);
90 __le32 le32_version
= cpu_to_le32(version
);
92 if (!aa_g_hash_policy
)
98 profile
->hash
= kzalloc(apparmor_hash_size
, GFP_KERNEL
);
102 desc
->tfm
= apparmor_tfm
;
105 error
= crypto_shash_init(desc
);
108 error
= crypto_shash_update(desc
, (u8
*) &le32_version
, 4);
111 error
= crypto_shash_update(desc
, (u8
*) start
, len
);
114 error
= crypto_shash_final(desc
, profile
->hash
);
121 kfree(profile
->hash
);
122 profile
->hash
= NULL
;
127 static int __init
init_profile_hash(void)
129 struct crypto_shash
*tfm
;
131 if (!apparmor_initialized
)
134 tfm
= crypto_alloc_shash("sha1", 0, CRYPTO_ALG_ASYNC
);
136 int error
= PTR_ERR(tfm
);
137 AA_ERROR("failed to setup profile sha1 hashing: %d\n", error
);
141 apparmor_hash_size
= crypto_shash_digestsize(apparmor_tfm
);
143 aa_info_message("AppArmor sha1 policy hashing enabled");
148 late_initcall(init_profile_hash
);