openentropy-core 0.12.3

Core entropy harvesting library — hardware noise sources, raw or SHA-256 conditioned
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Kqueue event timing — entropy from BSD kernel event notification multiplexing.

#[cfg(any(
    target_os = "macos",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
use std::sync::Arc;
#[cfg(any(
    target_os = "macos",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
use std::sync::atomic::{AtomicBool, Ordering};
#[cfg(any(
    target_os = "macos",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
use std::thread;

use crate::source::{EntropySource, Platform, SourceCategory, SourceInfo};
#[cfg(any(
    target_os = "macos",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
use crate::sources::helpers::extract_timing_entropy;
#[cfg(any(
    target_os = "macos",
    target_os = "freebsd",
    target_os = "netbsd",
    target_os = "openbsd"
))]
use crate::sources::helpers::mach_time;

/// Configuration for kqueue events entropy collection.
///
/// # Example
/// ```
/// # use openentropy_core::sources::ipc::KqueueEventsConfig;
/// let config = KqueueEventsConfig {
///     num_file_watchers: 2,   // fewer watchers
///     num_timers: 16,         // more timers for richer interference
///     num_sockets: 4,         // default socket pairs
///     timeout_ms: 2,          // slightly longer timeout
/// };
/// ```
#[derive(Debug, Clone)]
pub struct KqueueEventsConfig {
    /// Number of file watchers to register via `EVFILT_VNODE`.
    ///
    /// Each watcher monitors a temp file for write/attribute changes.
    /// The filesystem notification path traverses VFS, APFS/HFS event queues,
    /// and the kqueue knote hash table.
    ///
    /// **Range:** 0+. **Default:** `4`
    pub num_file_watchers: usize,

    /// Number of timer events to register via `EVFILT_TIMER`.
    ///
    /// Each timer fires at a different interval (1-10ms). Multiple timers
    /// create scheduling contention and exercise kernel timer coalescing.
    /// Timer delivery is affected by interrupt handling and power management.
    ///
    /// **Range:** 0+. **Default:** `8`
    pub num_timers: usize,

    /// Number of socket pairs for `EVFILT_READ`/`EVFILT_WRITE` monitoring.
    ///
    /// Socket buffer management interacts with the network stack's mbuf
    /// allocator. A background thread periodically writes to sockets to
    /// generate asynchronous events.
    ///
    /// **Range:** 0+. **Default:** `4`
    pub num_sockets: usize,

    /// Timeout in milliseconds for `kevent()` calls.
    ///
    /// Controls how long each `kevent()` waits for events. Shorter timeouts
    /// capture more frequent timing samples; longer timeouts allow more
    /// events to accumulate per call.
    ///
    /// **Range:** 1+. **Default:** `1`
    pub timeout_ms: u32,
}

impl Default for KqueueEventsConfig {
    fn default() -> Self {
        Self {
            num_file_watchers: 4,
            num_timers: 8,
            num_sockets: 4,
            timeout_ms: 1,
        }
    }
}

/// Harvests timing jitter from kqueue event notification multiplexing.
///
/// # What it measures
/// Nanosecond timing of `kevent()` calls with multiple registered event
/// types (timers, file watchers, socket monitors) firing concurrently.
///
/// # Why it's entropic
/// kqueue is the macOS/BSD kernel event notification system. Registering
/// diverse event types simultaneously creates rich interference:
/// - **Timer events** — `EVFILT_TIMER` with different intervals fire at
///   kernel-determined times affected by timer coalescing, interrupt handling,
///   and power management state
/// - **File watchers** — `EVFILT_VNODE` on temp files monitors inode changes;
///   traverses VFS, APFS/HFS event queues, and the kqueue knote hash table
/// - **Socket events** — `EVFILT_READ`/`EVFILT_WRITE` on socket pairs monitors
///   buffer state; interacts with the network stack's mbuf allocator
/// - **Knote lock contention** — many registered watchers all compete for the
///   kqueue's internal knote lock and dispatch queue
///
/// # What makes it unique
/// No prior work has combined multiple kqueue event types as an entropy source.
/// The cross-event-type interference (timer delivery affecting socket
/// notification timing) produces entropy that is independent from any single
/// event source.
///
/// # Configuration
/// See [`KqueueEventsConfig`] for tunable parameters. Key options:
/// - `num_timers`: controls timer coalescing interference
/// - `num_sockets`: controls mbuf allocator contention
/// - `num_file_watchers`: controls VFS notification path diversity
/// - `timeout_ms`: controls `kevent()` wait duration
#[derive(Default)]
pub struct KqueueEventsSource {
    /// Source configuration. Use `Default::default()` for recommended settings.
    pub config: KqueueEventsConfig,
}

static KQUEUE_EVENTS_INFO: SourceInfo = SourceInfo {
    name: "kqueue_events",
    description: "Kqueue event multiplexing timing from timers, files, and sockets",
    physics: "Registers diverse kqueue event types (timers, file watchers, socket monitors) \
              and measures kevent() notification timing. Timer events capture kernel timer \
              coalescing and interrupt jitter. File watchers exercise VFS/APFS notification \
              paths. Socket events capture mbuf allocator timing. Multiple simultaneous watchers \
              create knote lock contention and dispatch queue interference. The combination of \
              independent event sources produces high min-entropy.",
    category: SourceCategory::IPC,
    platform: Platform::Any, // macOS + FreeBSD/NetBSD/OpenBSD (all BSDs with kqueue)
    requirements: &[],
    entropy_rate_estimate: 2.5,
    composite: false,
    is_fast: true,
};

impl EntropySource for KqueueEventsSource {
    fn info(&self) -> &SourceInfo {
        &KQUEUE_EVENTS_INFO
    }

    fn is_available(&self) -> bool {
        cfg!(any(
            target_os = "macos",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ))
    }

    #[cfg(any(
        target_os = "macos",
        target_os = "freebsd",
        target_os = "netbsd",
        target_os = "openbsd"
    ))]
    fn collect(&self, n_samples: usize) -> Vec<u8> {
        // Each kevent() call costs ~timeout_ms. Keep raw_count reasonable
        // so collection finishes well within the pool's per-source timeout.
        // With timeout_ms=1, raw_count=n+64 takes ~(n+64)ms which is fine
        // for typical n_samples (64-4096).
        let raw_count = n_samples + 64;
        let mut timings: Vec<u64> = Vec::with_capacity(raw_count);

        // SAFETY: kqueue() creates a new kernel event queue (always safe).
        let kq = unsafe { libc::kqueue() };
        if kq < 0 {
            return Vec::new();
        }

        let mut changes: Vec<libc::kevent> = Vec::new();
        let mut cleanup_fds: Vec<i32> = Vec::new();

        // Register timer events with different intervals (1-10ms).
        for i in 0..self.config.num_timers {
            let interval_ms = 1 + (i % 10);
            let mut ev: libc::kevent = unsafe { std::mem::zeroed() };
            ev.ident = i;
            ev.filter = libc::EVFILT_TIMER;
            ev.flags = libc::EV_ADD | libc::EV_ENABLE;
            ev.fflags = 0;
            ev.data = interval_ms as isize;
            ev.udata = std::ptr::null_mut();
            changes.push(ev);
        }

        // Register socket pair events.
        for _i in 0..self.config.num_sockets {
            let mut sv: [i32; 2] = [0; 2];
            let ret =
                unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, sv.as_mut_ptr()) };
            if ret == 0 {
                cleanup_fds.push(sv[0]);
                cleanup_fds.push(sv[1]);

                let mut ev: libc::kevent = unsafe { std::mem::zeroed() };
                ev.ident = sv[0] as usize;
                ev.filter = libc::EVFILT_READ;
                ev.flags = libc::EV_ADD | libc::EV_ENABLE;
                ev.udata = std::ptr::null_mut();
                changes.push(ev);

                let byte = [0xAAu8];
                unsafe {
                    libc::write(sv[1], byte.as_ptr() as *const _, 1);
                }

                let mut ev2: libc::kevent = unsafe { std::mem::zeroed() };
                ev2.ident = sv[1] as usize;
                ev2.filter = libc::EVFILT_WRITE;
                ev2.flags = libc::EV_ADD | libc::EV_ENABLE;
                ev2.udata = std::ptr::null_mut();
                changes.push(ev2);
            }
        }

        // Register file watchers on temp files.
        let mut temp_files: Vec<(i32, std::path::PathBuf)> = Vec::new();
        for i in 0..self.config.num_file_watchers {
            let path = std::env::temp_dir().join(format!("oe_kq_{i}_{}", std::process::id()));
            if std::fs::write(&path, b"entropy").is_ok() {
                let path_str = path.to_str().unwrap_or("");
                let c_path = match std::ffi::CString::new(path_str) {
                    Ok(c) => c,
                    Err(_) => continue, // skip paths with null bytes
                };
                let fd = unsafe { libc::open(c_path.as_ptr(), libc::O_RDONLY) };
                if fd >= 0 {
                    let mut ev: libc::kevent = unsafe { std::mem::zeroed() };
                    ev.ident = fd as usize;
                    ev.filter = libc::EVFILT_VNODE;
                    ev.flags = libc::EV_ADD | libc::EV_ENABLE | libc::EV_CLEAR;
                    ev.fflags = libc::NOTE_WRITE | libc::NOTE_ATTRIB;
                    ev.udata = std::ptr::null_mut();
                    changes.push(ev);
                    temp_files.push((fd, path));
                }
            }
        }

        // Register all changes.
        if !changes.is_empty() {
            unsafe {
                libc::kevent(
                    kq,
                    changes.as_ptr(),
                    changes.len() as i32,
                    std::ptr::null_mut(),
                    0,
                    std::ptr::null(),
                );
            }
        }

        // Spawn a thread to periodically poke watched files and sockets.
        let stop = Arc::new(AtomicBool::new(false));
        let stop2 = stop.clone();
        let socket_write_fds: Vec<i32> = cleanup_fds.iter().skip(1).step_by(2).copied().collect();
        let file_paths: Vec<std::path::PathBuf> =
            temp_files.iter().map(|(_, p)| p.clone()).collect();

        let poker = thread::spawn(move || {
            let byte = [0xBBu8];
            while !stop2.load(Ordering::Relaxed) {
                for &fd in &socket_write_fds {
                    unsafe {
                        libc::write(fd, byte.as_ptr() as *const _, 1);
                    }
                }
                for path in &file_paths {
                    let _ = std::fs::write(path, b"poke");
                }
                std::thread::sleep(std::time::Duration::from_micros(500));
            }
        });

        // Collect timing samples.
        let timeout = libc::timespec {
            tv_sec: 0,
            tv_nsec: self.config.timeout_ms as i64 * 1_000_000,
        };
        let mut events: Vec<libc::kevent> =
            vec![unsafe { std::mem::zeroed() }; changes.len().max(16)];

        let socket_read_fds: Vec<i32> = cleanup_fds.iter().step_by(2).copied().collect();
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(4);

        for iter in 0..raw_count {
            if iter % 64 == 0 && std::time::Instant::now() >= deadline {
                break;
            }
            let t0 = mach_time();

            let n = unsafe {
                libc::kevent(
                    kq,
                    std::ptr::null(),
                    0,
                    events.as_mut_ptr(),
                    events.len() as i32,
                    &timeout,
                )
            };

            let t1 = mach_time();

            // Drain socket read buffers to prevent saturation.
            if n > 0 {
                let mut drain = [0u8; 64];
                for &fd in &socket_read_fds {
                    unsafe {
                        libc::read(fd, drain.as_mut_ptr() as *mut _, drain.len());
                    }
                }
            }

            timings.push(t1.wrapping_sub(t0));
        }

        // Shutdown poker thread.
        stop.store(true, Ordering::Relaxed);
        let _ = poker.join();

        // Cleanup.
        for (fd, path) in &temp_files {
            unsafe {
                libc::close(*fd);
            }
            let _ = std::fs::remove_file(path);
        }
        for &fd in &cleanup_fds {
            unsafe {
                libc::close(fd);
            }
        }
        unsafe {
            libc::close(kq);
        }

        extract_timing_entropy(&timings, n_samples)
    }

    #[cfg(not(any(
        target_os = "macos",
        target_os = "freebsd",
        target_os = "netbsd",
        target_os = "openbsd"
    )))]
    fn collect(&self, _n_samples: usize) -> Vec<u8> {
        Vec::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn info() {
        let src = KqueueEventsSource::default();
        assert_eq!(src.name(), "kqueue_events");
        assert_eq!(src.info().category, SourceCategory::IPC);
        assert!(!src.info().composite);
    }

    #[test]
    fn default_config() {
        let config = KqueueEventsConfig::default();
        assert_eq!(config.num_file_watchers, 4);
        assert_eq!(config.num_timers, 8);
        assert_eq!(config.num_sockets, 4);
        assert_eq!(config.timeout_ms, 1);
    }

    #[test]
    fn custom_config() {
        let src = KqueueEventsSource {
            config: KqueueEventsConfig {
                num_file_watchers: 2,
                num_timers: 4,
                num_sockets: 2,
                timeout_ms: 5,
            },
        };
        assert_eq!(src.config.num_timers, 4);
    }

    #[test]
    #[ignore] // Uses kqueue syscall
    fn collects_bytes() {
        let src = KqueueEventsSource::default();
        if src.is_available() {
            let data = src.collect(64);
            assert!(!data.is_empty());
            assert!(data.len() <= 64);
        }
    }
}