craton-hsm 0.10.5

A production-grade PKCS#11 v3.0-compliant Software HSM written in Rust
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Craton Software Company
//! Lock-free metrics collection for HSM cryptographic operations.
//!
//! Tracks operation counts, latency, and error rates using only `std` atomics —
//! no external observability crates required.

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

// ---------------------------------------------------------------------------
// Operation taxonomy
// ---------------------------------------------------------------------------

/// Operation categories for metrics tracking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MetricOperation {
    Sign,
    Verify,
    Encrypt,
    Decrypt,
    Digest,
    GenerateKey,
    GenerateKeyPair,
    WrapKey,
    UnwrapKey,
    DeriveKey,
    GenerateRandom,
}

impl MetricOperation {
    /// Total number of variants — must stay in sync with the enum definition.
    const COUNT: usize = 11;

    /// Map variant to a fixed index in `[0, COUNT)`.
    #[inline]
    fn index(self) -> usize {
        self as usize
    }

    /// Human-readable label.
    fn name(self) -> &'static str {
        match self {
            Self::Sign => "sign",
            Self::Verify => "verify",
            Self::Encrypt => "encrypt",
            Self::Decrypt => "decrypt",
            Self::Digest => "digest",
            Self::GenerateKey => "generate_key",
            Self::GenerateKeyPair => "generate_key_pair",
            Self::WrapKey => "wrap_key",
            Self::UnwrapKey => "unwrap_key",
            Self::DeriveKey => "derive_key",
            Self::GenerateRandom => "generate_random",
        }
    }

    /// Iterator over all variants (used by `snapshot`).
    fn all() -> &'static [MetricOperation; MetricOperation::COUNT] {
        &[
            Self::Sign,
            Self::Verify,
            Self::Encrypt,
            Self::Decrypt,
            Self::Digest,
            Self::GenerateKey,
            Self::GenerateKeyPair,
            Self::WrapKey,
            Self::UnwrapKey,
            Self::DeriveKey,
            Self::GenerateRandom,
        ]
    }
}

// ---------------------------------------------------------------------------
// Per-operation atomic counters
// ---------------------------------------------------------------------------

struct OperationMetrics {
    total_ops: AtomicU64,
    failed_ops: AtomicU64,
    total_latency_us: AtomicU64,
    max_latency_us: AtomicU64,
}

impl OperationMetrics {
    const fn new() -> Self {
        Self {
            total_ops: AtomicU64::new(0),
            failed_ops: AtomicU64::new(0),
            total_latency_us: AtomicU64::new(0),
            max_latency_us: AtomicU64::new(0),
        }
    }
}

// ---------------------------------------------------------------------------
// Global collector
// ---------------------------------------------------------------------------

/// Lock-free, global metrics collector.
///
/// All mutation methods use `Relaxed` ordering — counters are statistical and
/// do not need sequentially-consistent reads.
pub struct MetricsCollector {
    enabled: bool,
    operations: [OperationMetrics; MetricOperation::COUNT],
    total_sessions_opened: AtomicU64,
    total_sessions_closed: AtomicU64,
    total_logins: AtomicU64,
    total_login_failures: AtomicU64,
    started_at: Instant,
}

impl MetricsCollector {
    /// Create a new collector.  Pass `false` to make every method a no-op.
    pub fn new(enabled: bool) -> Self {
        // Work around the lack of `Copy` on `AtomicU64` — cannot use array
        // repeat syntax, so we initialise each slot manually via a const fn
        // helper.
        #[allow(clippy::declare_interior_mutable_const)]
        const INIT: OperationMetrics = OperationMetrics::new();
        Self {
            enabled,
            operations: [
                INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT, INIT,
            ],
            total_sessions_opened: AtomicU64::new(0),
            total_sessions_closed: AtomicU64::new(0),
            total_logins: AtomicU64::new(0),
            total_login_failures: AtomicU64::new(0),
            started_at: Instant::now(),
        }
    }

    /// Convenience constructor for a disabled (no-op) collector.
    pub fn disabled() -> Self {
        Self::new(false)
    }

    /// Whether metrics collection is active.
    #[inline]
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    // -- operation recording -------------------------------------------------

    /// Record a single cryptographic operation.
    ///
    /// `latency` is the wall-clock duration of the operation.
    /// `success` indicates whether it completed without error.
    pub fn record_operation(&self, op: MetricOperation, latency: Duration, success: bool) {
        if !self.enabled {
            return;
        }
        let m = &self.operations[op.index()];
        m.total_ops.fetch_add(1, Ordering::Relaxed);
        if !success {
            m.failed_ops.fetch_add(1, Ordering::Relaxed);
        }
        let us = latency.as_micros() as u64;
        m.total_latency_us.fetch_add(us, Ordering::Relaxed);

        // Update max using a CAS loop.
        let mut cur = m.max_latency_us.load(Ordering::Relaxed);
        while us > cur {
            match m.max_latency_us.compare_exchange_weak(
                cur,
                us,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(actual) => cur = actual,
            }
        }
    }

    // -- session recording ---------------------------------------------------

    /// Record a session being opened.
    pub fn record_session_open(&self) {
        if !self.enabled {
            return;
        }
        self.total_sessions_opened.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a session being closed.
    pub fn record_session_close(&self) {
        if !self.enabled {
            return;
        }
        self.total_sessions_closed.fetch_add(1, Ordering::Relaxed);
    }

    // -- login recording -----------------------------------------------------

    /// Record a login attempt.
    pub fn record_login(&self, success: bool) {
        if !self.enabled {
            return;
        }
        self.total_logins.fetch_add(1, Ordering::Relaxed);
        if !success {
            self.total_login_failures.fetch_add(1, Ordering::Relaxed);
        }
    }

    // -- snapshot ------------------------------------------------------------

    /// Capture a point-in-time copy of every metric.
    pub fn snapshot(&self) -> MetricsSnapshot {
        let uptime_secs = self.started_at.elapsed().as_secs();
        let opened = self.total_sessions_opened.load(Ordering::Relaxed);
        let closed = self.total_sessions_closed.load(Ordering::Relaxed);

        let operations = MetricOperation::all()
            .iter()
            .map(|&op| {
                let m = &self.operations[op.index()];
                let total = m.total_ops.load(Ordering::Relaxed);
                let failed = m.failed_ops.load(Ordering::Relaxed);
                let total_lat = m.total_latency_us.load(Ordering::Relaxed);
                let max_lat = m.max_latency_us.load(Ordering::Relaxed);
                let avg = total_lat.checked_div(total).unwrap_or(0);
                OperationSnapshot {
                    name: op.name(),
                    total,
                    failed,
                    avg_latency_us: avg,
                    max_latency_us: max_lat,
                }
            })
            .collect();

        MetricsSnapshot {
            uptime_secs,
            operations,
            total_sessions_opened: opened,
            total_sessions_closed: closed,
            active_sessions: opened.saturating_sub(closed),
            total_logins: self.total_logins.load(Ordering::Relaxed),
            total_login_failures: self.total_login_failures.load(Ordering::Relaxed),
        }
    }
}

// ---------------------------------------------------------------------------
// Snapshot types
// ---------------------------------------------------------------------------

/// Point-in-time copy of all metrics.
#[derive(Debug, Clone)]
pub struct MetricsSnapshot {
    pub uptime_secs: u64,
    pub operations: Vec<OperationSnapshot>,
    pub total_sessions_opened: u64,
    pub total_sessions_closed: u64,
    pub active_sessions: u64,
    pub total_logins: u64,
    pub total_login_failures: u64,
}

/// Snapshot of a single operation category.
#[derive(Debug, Clone)]
pub struct OperationSnapshot {
    pub name: &'static str,
    pub total: u64,
    pub failed: u64,
    pub avg_latency_us: u64,
    pub max_latency_us: u64,
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn disabled_collector_does_not_panic() {
        let c = MetricsCollector::disabled();
        assert!(!c.is_enabled());
        c.record_operation(MetricOperation::Sign, Duration::from_millis(5), true);
        c.record_operation(MetricOperation::Encrypt, Duration::from_millis(2), false);
        c.record_session_open();
        c.record_session_close();
        c.record_login(true);
        c.record_login(false);

        // Snapshot should return zeroes since collection is disabled.
        let snap = c.snapshot();
        for op in &snap.operations {
            assert_eq!(op.total, 0);
            assert_eq!(op.failed, 0);
        }
        assert_eq!(snap.total_sessions_opened, 0);
        assert_eq!(snap.total_logins, 0);
    }

    #[test]
    fn operation_recording_increments_counters() {
        let c = MetricsCollector::new(true);
        c.record_operation(MetricOperation::Sign, Duration::from_micros(100), true);
        c.record_operation(MetricOperation::Sign, Duration::from_micros(200), true);
        c.record_operation(MetricOperation::Sign, Duration::from_micros(300), false);

        let snap = c.snapshot();
        let sign = snap.operations.iter().find(|o| o.name == "sign").unwrap();
        assert_eq!(sign.total, 3);
        assert_eq!(sign.failed, 1);
    }

    #[test]
    fn snapshot_returns_correct_values() {
        let c = MetricsCollector::new(true);
        c.record_operation(MetricOperation::Encrypt, Duration::from_micros(500), true);
        c.record_session_open();
        c.record_session_open();
        c.record_session_close();
        c.record_login(true);
        c.record_login(false);

        let snap = c.snapshot();
        assert_eq!(snap.total_sessions_opened, 2);
        assert_eq!(snap.total_sessions_closed, 1);
        assert_eq!(snap.active_sessions, 1);
        assert_eq!(snap.total_logins, 2);
        assert_eq!(snap.total_login_failures, 1);

        let enc = snap
            .operations
            .iter()
            .find(|o| o.name == "encrypt")
            .unwrap();
        assert_eq!(enc.total, 1);
        assert_eq!(enc.avg_latency_us, 500);
        assert_eq!(enc.max_latency_us, 500);
    }

    #[test]
    fn concurrent_recording() {
        use std::sync::Arc;
        use std::thread;

        let c = Arc::new(MetricsCollector::new(true));
        let mut handles = Vec::new();

        for _ in 0..8 {
            let c = Arc::clone(&c);
            handles.push(thread::spawn(move || {
                for _ in 0..1000 {
                    c.record_operation(MetricOperation::Verify, Duration::from_micros(10), true);
                }
            }));
        }

        for h in handles {
            h.join().unwrap();
        }

        let snap = c.snapshot();
        let verify = snap.operations.iter().find(|o| o.name == "verify").unwrap();
        assert_eq!(verify.total, 8000);
        assert_eq!(verify.failed, 0);
    }

    #[test]
    fn latency_tracking_avg_and_max() {
        let c = MetricsCollector::new(true);
        // 100, 200, 300 => avg 200, max 300
        c.record_operation(MetricOperation::Digest, Duration::from_micros(100), true);
        c.record_operation(MetricOperation::Digest, Duration::from_micros(200), true);
        c.record_operation(MetricOperation::Digest, Duration::from_micros(300), true);

        let snap = c.snapshot();
        let dig = snap.operations.iter().find(|o| o.name == "digest").unwrap();
        assert_eq!(dig.avg_latency_us, 200);
        assert_eq!(dig.max_latency_us, 300);
    }

    #[test]
    fn session_open_close_counting() {
        let c = MetricsCollector::new(true);
        for _ in 0..5 {
            c.record_session_open();
        }
        for _ in 0..3 {
            c.record_session_close();
        }

        let snap = c.snapshot();
        assert_eq!(snap.total_sessions_opened, 5);
        assert_eq!(snap.total_sessions_closed, 3);
        assert_eq!(snap.active_sessions, 2);
    }

    #[test]
    fn login_success_failure_counting() {
        let c = MetricsCollector::new(true);
        c.record_login(true);
        c.record_login(true);
        c.record_login(false);
        c.record_login(true);
        c.record_login(false);

        let snap = c.snapshot();
        assert_eq!(snap.total_logins, 5);
        assert_eq!(snap.total_login_failures, 2);
    }
}