Skip to content

Commit 17f4af3

Browse files
committed
rust: kernel: Add simple siphash abstraction
This allows Rust code to use the Hasher interface with the kernel siphash implementation. Signed-off-by: Asahi Lina <[email protected]>
1 parent 07b4a16 commit 17f4af3

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <linux/platform_device.h>
3030
#include <linux/refcount.h>
3131
#include <linux/wait.h>
32+
#include <linux/siphash.h>
3233
#include <linux/sched.h>
3334
#include <linux/soc/apple/rtkit.h>
3435
#include <linux/timekeeping.h>

rust/helpers.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/platform_device.h>
3535
#include <linux/refcount.h>
3636
#include <linux/mutex.h>
37+
#include <linux/siphash.h>
3738
#include <linux/spinlock.h>
3839
#include <linux/sched/signal.h>
3940
#include <linux/wait.h>
@@ -151,6 +152,13 @@ const char *rust_helper_errname(int err)
151152
}
152153
EXPORT_SYMBOL_GPL(rust_helper_errname);
153154

155+
u64 rust_helper_siphash(const void *data, size_t len,
156+
const siphash_key_t *key)
157+
{
158+
return siphash(data, len, key);
159+
}
160+
EXPORT_SYMBOL_GPL(rust_helper_siphash);
161+
154162
void rust_helper_xa_init_flags(struct xarray *xa, gfp_t flags)
155163
{
156164
xa_init_flags(xa, flags);

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub mod of;
5757
pub mod platform;
5858
pub mod prelude;
5959
pub mod print;
60+
pub mod siphash;
6061
pub mod soc;
6162
mod static_assert;
6263
#[doc(hidden)]

rust/kernel/siphash.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! A core::hash::Hasher wrapper for the kernel siphash implementation.
4+
//!
5+
//! This module allows Rust code to use the kernel's siphash implementation
6+
//! to hash Rust objects.
7+
8+
use core::hash::Hasher;
9+
10+
/// A Hasher implementation that uses the kernel siphash implementation.
11+
#[derive(Default)]
12+
pub struct SipHasher {
13+
// SipHash state is 4xu64, but the Linux implementation
14+
// doesn't expose incremental hashing so let's just chain
15+
// individual SipHash calls for now, which return a u64
16+
// hash.
17+
state: u64,
18+
}
19+
20+
impl SipHasher {
21+
/// Create a new SipHasher with zeroed state.
22+
pub fn new() -> Self {
23+
SipHasher { state: 0 }
24+
}
25+
}
26+
27+
impl Hasher for SipHasher {
28+
fn finish(&self) -> u64 {
29+
self.state
30+
}
31+
32+
fn write(&mut self, bytes: &[u8]) {
33+
let key = bindings::siphash_key_t {
34+
key: [self.state, 0],
35+
};
36+
37+
self.state = unsafe { bindings::siphash(bytes.as_ptr() as *const _, bytes.len(), &key) };
38+
}
39+
}

0 commit comments

Comments
 (0)