fscrypt: use sha256() instead of open coding

Now that there's a library function that calculates the SHA-256 digest
of a buffer in one step, use it instead of sha256_init() +
sha256_update() + sha256_final().

Link: https://lore.kernel.org/r/20200917045341.324996-1-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@google.com>
This commit is contained in:
Eric Biggers 2020-09-16 21:53:41 -07:00
parent c8c868abc9
commit 0c6a113b24
1 changed files with 7 additions and 16 deletions

View File

@ -61,15 +61,6 @@ struct fscrypt_nokey_name {
*/ */
#define FSCRYPT_NOKEY_NAME_MAX offsetofend(struct fscrypt_nokey_name, sha256) #define FSCRYPT_NOKEY_NAME_MAX offsetofend(struct fscrypt_nokey_name, sha256)
static void fscrypt_do_sha256(const u8 *data, unsigned int data_len, u8 *result)
{
struct sha256_state sctx;
sha256_init(&sctx);
sha256_update(&sctx, data, data_len);
sha256_final(&sctx, result);
}
static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
{ {
if (str->len == 1 && str->name[0] == '.') if (str->len == 1 && str->name[0] == '.')
@ -366,9 +357,9 @@ int fscrypt_fname_disk_to_usr(const struct inode *inode,
} else { } else {
memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes)); memcpy(nokey_name.bytes, iname->name, sizeof(nokey_name.bytes));
/* Compute strong hash of remaining part of name. */ /* Compute strong hash of remaining part of name. */
fscrypt_do_sha256(&iname->name[sizeof(nokey_name.bytes)], sha256(&iname->name[sizeof(nokey_name.bytes)],
iname->len - sizeof(nokey_name.bytes), iname->len - sizeof(nokey_name.bytes),
nokey_name.sha256); nokey_name.sha256);
size = FSCRYPT_NOKEY_NAME_MAX; size = FSCRYPT_NOKEY_NAME_MAX;
} }
oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name); oname->len = base64_encode((const u8 *)&nokey_name, size, oname->name);
@ -497,7 +488,7 @@ bool fscrypt_match_name(const struct fscrypt_name *fname,
{ {
const struct fscrypt_nokey_name *nokey_name = const struct fscrypt_nokey_name *nokey_name =
(const void *)fname->crypto_buf.name; (const void *)fname->crypto_buf.name;
u8 sha256[SHA256_DIGEST_SIZE]; u8 digest[SHA256_DIGEST_SIZE];
if (likely(fname->disk_name.name)) { if (likely(fname->disk_name.name)) {
if (de_name_len != fname->disk_name.len) if (de_name_len != fname->disk_name.len)
@ -508,9 +499,9 @@ bool fscrypt_match_name(const struct fscrypt_name *fname,
return false; return false;
if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes))) if (memcmp(de_name, nokey_name->bytes, sizeof(nokey_name->bytes)))
return false; return false;
fscrypt_do_sha256(&de_name[sizeof(nokey_name->bytes)], sha256(&de_name[sizeof(nokey_name->bytes)],
de_name_len - sizeof(nokey_name->bytes), sha256); de_name_len - sizeof(nokey_name->bytes), digest);
return !memcmp(sha256, nokey_name->sha256, sizeof(sha256)); return !memcmp(digest, nokey_name->sha256, sizeof(digest));
} }
EXPORT_SYMBOL_GPL(fscrypt_match_name); EXPORT_SYMBOL_GPL(fscrypt_match_name);