fs/verity/hash_algs.c
Source file repositories/reference/linux-study-clean/fs/verity/hash_algs.c
File Facts
- System
- Linux kernel
- Corpus path
fs/verity/hash_algs.c- Extension
.c- Size
- 5289 bytes
- Lines
- 189
- Domain
- Core OS
- Bucket
- VFS And Filesystem Core
- Inferred role
- Core OS: implementation source
- Status
- source implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
fsverity_private.h
Detected Declarations
function fsverity_get_hash_algfunction fsverity_prepare_hash_statefunction fsverity_hash_blockfunction fsverity_hash_bufferfunction fsverity_check_hash_algs
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* fs-verity hash algorithms
*
* Copyright 2019 Google LLC
*/
#include "fsverity_private.h"
/* The hash algorithms supported by fs-verity */
const struct fsverity_hash_alg fsverity_hash_algs[] = {
[FS_VERITY_HASH_ALG_SHA256] = {
.name = "sha256",
.digest_size = SHA256_DIGEST_SIZE,
.block_size = SHA256_BLOCK_SIZE,
.algo_id = HASH_ALGO_SHA256,
},
[FS_VERITY_HASH_ALG_SHA512] = {
.name = "sha512",
.digest_size = SHA512_DIGEST_SIZE,
.block_size = SHA512_BLOCK_SIZE,
.algo_id = HASH_ALGO_SHA512,
},
};
/**
* fsverity_get_hash_alg() - get a hash algorithm by number
* @inode: optional inode for logging purposes
* @num: the hash algorithm number
*
* Get the struct fsverity_hash_alg for the given hash algorithm number.
*
* Return: pointer to the hash alg if it's known, otherwise NULL.
*/
const struct fsverity_hash_alg *fsverity_get_hash_alg(const struct inode *inode,
unsigned int num)
{
if (num >= ARRAY_SIZE(fsverity_hash_algs) ||
!fsverity_hash_algs[num].name) {
fsverity_warn(inode, "Unknown hash algorithm number: %u", num);
return NULL;
}
return &fsverity_hash_algs[num];
}
/**
* fsverity_prepare_hash_state() - precompute the initial hash state
* @alg: hash algorithm
* @salt: a salt which is to be prepended to all data to be hashed
* @salt_size: salt size in bytes
*
* Return: the kmalloc()'ed initial hash state, or NULL if out of memory.
*/
union fsverity_hash_ctx *
fsverity_prepare_hash_state(const struct fsverity_hash_alg *alg,
const u8 *salt, size_t salt_size)
{
u8 *padded_salt = NULL;
size_t padded_salt_size;
union fsverity_hash_ctx ctx;
void *res = NULL;
/*
* Zero-pad the salt to the next multiple of the input size of the hash
* algorithm's compression function, e.g. 64 bytes for SHA-256 or 128
* bytes for SHA-512. This ensures that the hash algorithm won't have
* any bytes buffered internally after processing the salt, thus making
* salted hashing just as fast as unsalted hashing.
*/
padded_salt_size = round_up(salt_size, alg->block_size);
padded_salt = kzalloc(padded_salt_size, GFP_KERNEL);
if (!padded_salt)
return NULL;
memcpy(padded_salt, salt, salt_size);
switch (alg->algo_id) {
case HASH_ALGO_SHA256:
sha256_init(&ctx.sha256);
sha256_update(&ctx.sha256, padded_salt, padded_salt_size);
res = kmemdup(&ctx.sha256, sizeof(ctx.sha256), GFP_KERNEL);
break;
case HASH_ALGO_SHA512:
sha512_init(&ctx.sha512);
sha512_update(&ctx.sha512, padded_salt, padded_salt_size);
res = kmemdup(&ctx.sha512, sizeof(ctx.sha512), GFP_KERNEL);
break;
default:
WARN_ON_ONCE(1);
}
kfree(padded_salt);
Annotation
- Immediate include surface: `fsverity_private.h`.
- Detected declarations: `function fsverity_get_hash_alg`, `function fsverity_prepare_hash_state`, `function fsverity_hash_block`, `function fsverity_hash_buffer`, `function fsverity_check_hash_algs`.
- Atlas domain: Core OS / VFS And Filesystem Core.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.