pith-digest 0.1.0

pith foundation: digest (zero-dep 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! The C ABI surface of `pith-digest`: the entry points the Python
//! (ctypes), Node (koffi) and Go (cgo) SDKs bind through.
//!
//! The suite's FFI convention, defined by this module and mirrored by
//! every `pith-*` cdylib:
//!
//! * one flat set of `#[unsafe(no_mangle)] pub unsafe extern "C"`
//!   functions — raw pointers plus lengths, no structs across the
//!   boundary;
//! * every function returns a status code (see the constants below),
//!   never a `Result`, never a panic: a `panic = "abort"` cdylib must
//!   not be reachable from a foreign caller;
//! * every operation here writes into caller-provided out-parameters
//!   and allocates nothing, so the crate ships no `_free` — there is
//!   no buffer this side ever hands out;
//! * the `unsafe` allowance is confined to this module; every core
//!   module stays unsafe-free behind the crate-root `#![deny]`.

#![allow(unsafe_code)]

use crate::error::Result;
use crate::{SplitMix64, adler32, crc32, fnv1a64, sha256};

/// Borrows `data` as a byte slice; a null pointer (only legal with the
/// zero length the callers validate first) borrows the empty slice —
/// `from_raw_parts` itself would panic on the null even at length 0.
fn borrow<'a>(data: *const u8, len: usize) -> &'a [u8] {
    if data.is_null() {
        &[]
    } else {
        unsafe { core::slice::from_raw_parts(data, len) }
    }
}

/// The mutable counterpart of [`borrow`] for out-slots.
fn borrow_mut<'a>(out: *mut u64, count: usize) -> &'a mut [u64] {
    if out.is_null() {
        &mut []
    } else {
        unsafe { core::slice::from_raw_parts_mut(out, count) }
    }
}

/// Status: success.
pub const PITH_OK: i32 = 0;
/// Status: a caller argument is invalid — a null out-slot, or a null
/// data pointer that claims a nonzero length.
pub const PITH_E_INVALID: i32 = -1;
/// Status: the core primitive refused the input. With `pith-digest`
/// this is reachable for a SHA-256/SHA-1 input beyond the padding
/// format's length ceiling — unreachable for any buffer a real caller
/// can pass — and for the base64 pair (an output buffer smaller than
/// the operation needs, or a non-canonical decode input), but the
/// code exists so a foreign caller never has to reason about a panic.
pub const PITH_E_REJECTED: i32 = -2;

/// The SHA-256 digest of `data`, written as 32 raw bytes through `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to 32 writable
/// bytes; both must stay valid for the duration of the call. The
/// function retains nothing.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_sha256(data: *const u8, len: usize, out: *mut u8) -> i32 {
    // A null `data` with `len == 0` is a valid empty preimage; only a
    // null out-slot, or a null data pointer that claims length, is a
    // caller bug.
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    match sha256(bytes) {
        Ok(digest) => {
            unsafe { core::ptr::copy_nonoverlapping(digest.as_bytes().as_ptr(), out, 32) };
            PITH_OK
        }
        Err(_) => PITH_E_REJECTED,
    }
}

/// The CRC-32 (IEEE 802.3, reflected) of `data`, written through `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to one writable
/// `u32`; both must stay valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_crc32(data: *const u8, len: usize, out: *mut u32) -> i32 {
    // A null `data` with `len == 0` is a valid empty preimage; only a
    // null out-slot, or a null data pointer that claims length, is a
    // caller bug.
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    unsafe { *out = crc32(bytes) };
    PITH_OK
}

/// The Adler-32 (RFC 1950) of `data`, written through `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to one writable
/// `u32`; both must stay valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_adler32(data: *const u8, len: usize, out: *mut u32) -> i32 {
    // A null `data` with `len == 0` is a valid empty preimage; only a
    // null out-slot, or a null data pointer that claims length, is a
    // caller bug.
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    unsafe { *out = adler32(bytes) };
    PITH_OK
}

/// The FNV-1a 64 of `data`, written through `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to one writable
/// `u64`; both must stay valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_fnv1a64(data: *const u8, len: usize, out: *mut u64) -> i32 {
    // A null `data` with `len == 0` is a valid empty preimage; only a
    // null out-slot, or a null data pointer that claims length, is a
    // caller bug.
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    unsafe { *out = fnv1a64(bytes) };
    PITH_OK
}

/// Fills `out` with the first `count` sequential splitmix64 outputs of
/// a generator seeded with `seed` — exactly the byte stream
/// [`SplitMix64::fill_bytes`] derives its words from, one `u64` per
/// step.
///
/// # Safety
///
/// `out` must point to `count` writable `u64`s and stay valid for the
/// duration of the call. `count` may be zero (a no-op).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_splitmix64_fill(
    seed: u64,
    out: *mut u64,
    count: usize,
) -> i32 {
    // A null `out` with `count == 0` is a no-op, not a refusal.
    if out.is_null() && count > 0 {
        return PITH_E_INVALID;
    }
    let slots = borrow_mut(out, count);
    let mut rng = SplitMix64::new(seed);
    for slot in slots {
        *slot = rng.next_u64();
    }
    PITH_OK
}

/// The mutable byte-buffer counterpart of [`borrow`] for sized
/// out-buffers (the base64 pair).
fn borrow_mut_bytes<'a>(out: *mut u8, cap: usize) -> &'a mut [u8] {
    if out.is_null() {
        &mut []
    } else {
        unsafe { core::slice::from_raw_parts_mut(out, cap) }
    }
}

/// The SHA-1 digest of `data`, written as 20 raw bytes through `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to 20 writable
/// bytes; both must stay valid for the duration of the call. The
/// function retains nothing.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_sha1(data: *const u8, len: usize, out: *mut u8) -> i32 {
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    match crate::sha1(bytes) {
        Ok(digest) => {
            unsafe { core::ptr::copy_nonoverlapping(digest.as_bytes().as_ptr(), out, 20) };
            PITH_OK
        }
        Err(_) => PITH_E_REJECTED,
    }
}

/// The SHA-512 digest of `data`, written as 64 raw bytes through
/// `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to 64 writable
/// bytes; both must stay valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_sha512(data: *const u8, len: usize, out: *mut u8) -> i32 {
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    let digest = crate::sha512(bytes);
    unsafe { core::ptr::copy_nonoverlapping(digest.as_bytes().as_ptr(), out, 64) };
    PITH_OK
}

/// The HMAC-SHA-1 of `data` under `key`, written as 20 raw bytes
/// through `out`.
///
/// # Safety
///
/// `key` must point to `key_len` readable bytes, `data` to `len`
/// readable bytes and `out` to 20 writable bytes; all must stay valid
/// for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_hmac_sha1(
    key: *const u8,
    key_len: usize,
    data: *const u8,
    len: usize,
    out: *mut u8,
) -> i32 {
    unsafe {
        hmac_export(key, key_len, data, len, out, |k, d| {
            crate::hmac_sha1(k, d).map(|digest| *digest.as_bytes())
        })
    }
}

/// The HMAC-SHA-256 of `data` under `key`, written as 32 raw bytes
/// through `out`.
///
/// # Safety
///
/// `key` must point to `key_len` readable bytes, `data` to `len`
/// readable bytes and `out` to 32 writable bytes; all must stay valid
/// for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_hmac_sha256(
    key: *const u8,
    key_len: usize,
    data: *const u8,
    len: usize,
    out: *mut u8,
) -> i32 {
    unsafe {
        hmac_export(key, key_len, data, len, out, |k, d| {
            crate::hmac_sha256(k, d).map(|digest| *digest.as_bytes())
        })
    }
}

/// The HMAC-SHA-512 of `data` under `key`, written as 64 raw bytes
/// through `out`.
///
/// # Safety
///
/// `key` must point to `key_len` readable bytes, `data` to `len`
/// readable bytes and `out` to 64 writable bytes; all must stay valid
/// for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_hmac_sha512(
    key: *const u8,
    key_len: usize,
    data: *const u8,
    len: usize,
    out: *mut u8,
) -> i32 {
    unsafe {
        hmac_export(key, key_len, data, len, out, |k, d| {
            Ok(*crate::hmac_sha512(k, d).as_bytes())
        })
    }
}

/// The shared body of the three HMAC exports: validates the pointers,
/// runs `hmac`, copies the `N`-byte digest.
///
/// # Safety
///
/// The caller has already validated every pointer's writability
/// contract; this helper only centralizes the null checks and the
/// copy.
unsafe fn hmac_export<const N: usize>(
    key: *const u8,
    key_len: usize,
    data: *const u8,
    len: usize,
    out: *mut u8,
    hmac: impl FnOnce(&[u8], &[u8]) -> Result<[u8; N]>,
) -> i32 {
    if out.is_null() || (key.is_null() && key_len > 0) || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let key_bytes = borrow(key, key_len);
    let data_bytes = borrow(data, len);
    match hmac(key_bytes, data_bytes) {
        Ok(digest) => {
            unsafe { core::ptr::copy_nonoverlapping(digest.as_ptr(), out, N) };
            PITH_OK
        }
        Err(_) => PITH_E_REJECTED,
    }
}

/// The XXH64 hash of `data` under `seed`, written through `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to one writable
/// `u64`; both must stay valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_xxh64(
    data: *const u8,
    len: usize,
    seed: u64,
    out: *mut u64,
) -> i32 {
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    unsafe { *out = crate::xxh64(bytes, seed) };
    PITH_OK
}

/// The MurmurHash3 x64 128-bit digest of `data` under `seed`, written
/// as 16 raw bytes (h1 little-endian, then h2 little-endian) through
/// `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to 16 writable
/// bytes; both must stay valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_murmur3_x64_128(
    data: *const u8,
    len: usize,
    seed: u32,
    out: *mut u8,
) -> i32 {
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    let digest = crate::murmur3_x64_128(bytes, seed);
    unsafe { core::ptr::copy_nonoverlapping(digest.as_bytes().as_ptr(), out, 16) };
    PITH_OK
}

/// The CRC-32C (Castagnoli, RFC 3720 B.4) of `data`, written through
/// `out`.
///
/// # Safety
///
/// `data` must point to `len` readable bytes and `out` to one writable
/// `u32`; both must stay valid for the duration of the call.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_crc32c(data: *const u8, len: usize, out: *mut u32) -> i32 {
    if out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    unsafe { *out = crate::crc32c(bytes) };
    PITH_OK
}

/// Encodes `data` as canonical padded base64 into `out`, writing the
/// encoded length through `out_len` (RFC 4648 §4).
///
/// # Safety
///
/// `data` must point to `len` readable bytes, `out` to `cap` writable
/// bytes and `out_len` to one writable `usize`; all must stay valid
/// for the duration of the call. [`PITH_E_REJECTED`] means `cap` is
/// below [`crate::base64::encoded_len`]`(len)` and nothing was
/// written.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_base64_encode(
    data: *const u8,
    len: usize,
    out: *mut u8,
    cap: usize,
    out_len: *mut usize,
) -> i32 {
    if out_len.is_null() || out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    let buffer = borrow_mut_bytes(out, cap);
    match crate::base64::base64_encode(bytes, buffer) {
        Ok(()) => {
            unsafe { *out_len = crate::base64::encoded_len(len) };
            PITH_OK
        }
        Err(_) => PITH_E_REJECTED,
    }
}

/// Decodes canonical padded base64 from `data` into `out`, writing
/// the decoded length through `out_len` (RFC 4648 §4).
///
/// # Safety
///
/// `data` must point to `len` readable bytes, `out` to `cap` writable
/// bytes and `out_len` to one writable `usize`; all must stay valid
/// for the duration of the call. [`PITH_E_REJECTED`] means `cap` is
/// below `len / 4 * 3` or the input is not canonical base64; nothing
/// was written.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_base64_decode(
    data: *const u8,
    len: usize,
    out: *mut u8,
    cap: usize,
    out_len: *mut usize,
) -> i32 {
    if out_len.is_null() || out.is_null() || (data.is_null() && len > 0) {
        return PITH_E_INVALID;
    }
    let bytes = borrow(data, len);
    let buffer = borrow_mut_bytes(out, cap);
    match crate::base64::base64_decode(bytes, buffer) {
        Ok(written) => {
            unsafe { *out_len = written };
            PITH_OK
        }
        Err(_) => PITH_E_REJECTED,
    }
}

/// Fills `out` with the first `count` sequential xoshiro256** outputs
/// of a generator seeded with `seed` (the splitmix64 expansion the
/// reference implementation recommends).
///
/// # Safety
///
/// `out` must point to `count` writable `u64`s and stay valid for the
/// duration of the call. `count` may be zero (a no-op).
#[unsafe(no_mangle)]
pub unsafe extern "C" fn pith_digest_xoshiro256_fill(
    seed: u64,
    out: *mut u64,
    count: usize,
) -> i32 {
    if out.is_null() && count > 0 {
        return PITH_E_INVALID;
    }
    let slots = borrow_mut(out, count);
    let mut rng = crate::Xoshiro256StarStar::from_seed(seed);
    for slot in slots {
        *slot = rng.next_u64();
    }
    PITH_OK
}

#[cfg(test)]
mod tests {
    use super::{
        PITH_E_INVALID, PITH_OK, pith_digest_adler32, pith_digest_crc32, pith_digest_fnv1a64,
        pith_digest_sha256, pith_digest_splitmix64_fill,
    };
    use crate::sha256;

    /// The empty SHA-256 through raw pointers: the FIPS 180-2 vector.
    #[test]
    fn ffi_sha256_reproduces_the_empty_digest() {
        let mut out = [0u8; 32];
        let status = unsafe { pith_digest_sha256(b"abc".as_ptr(), 3, out.as_mut_ptr()) };
        assert_eq!(status, PITH_OK);
        let expect = sha256(b"abc").expect("within size limit");
        assert_eq!(out, *expect.as_bytes());
    }

    /// The checksums through raw pointers against their published
    /// vectors: CRC-32 of "123456789", Adler-32 of "Wikipedia",
    /// FNV-1a 64 of "foobar".
    #[test]
    fn ffi_checksums_reproduce_published_vectors() {
        let mut c = 0u32;
        assert_eq!(
            unsafe { pith_digest_crc32(b"123456789".as_ptr(), 9, &mut c) },
            PITH_OK
        );
        assert_eq!(c, 0xcbf4_3926);

        let mut a = 0u32;
        assert_eq!(
            unsafe { pith_digest_adler32(b"Wikipedia".as_ptr(), 9, &mut a) },
            PITH_OK
        );
        assert_eq!(a, 0x11e6_0398);

        let mut f = 0u64;
        assert_eq!(
            unsafe { pith_digest_fnv1a64(b"foobar".as_ptr(), 6, &mut f) },
            PITH_OK
        );
        assert_eq!(f, 0x8594_4171_f739_67e8);
    }

    /// splitmix64 through raw pointers: seed 0's first two outputs,
    /// a zero count no-op and the null refusal.
    #[test]
    fn ffi_splitmix64_fill_reproduces_the_paper_sequence() {
        let mut out = [0u64; 2];
        assert_eq!(
            unsafe { pith_digest_splitmix64_fill(0, out.as_mut_ptr(), 2) },
            PITH_OK
        );
        assert_eq!(out, [0xe220_a839_7b1d_cdaf, 0x6e78_9e6a_a1b9_65f4]);

        let mut zero: [u64; 0] = [];
        assert_eq!(
            unsafe { pith_digest_splitmix64_fill(0, zero.as_mut_ptr(), 0) },
            PITH_OK
        );
        assert_eq!(
            unsafe { pith_digest_splitmix64_fill(0, core::ptr::null_mut(), 1) },
            PITH_E_INVALID
        );
    }

    /// A null `data` with `len == 0` is the valid empty preimage: the
    /// FIPS 180-2 empty digest through the null-pointer path.
    #[test]
    fn ffi_null_data_with_zero_len_is_the_empty_preimage() {
        let mut out = [0u8; 32];
        let status = unsafe { pith_digest_sha256(core::ptr::null(), 0, out.as_mut_ptr()) };
        assert_eq!(status, PITH_OK);
        let expect = sha256(b"").expect("within size limit");
        assert_eq!(out, *expect.as_bytes());

        let mut c = 0u32;
        assert_eq!(
            unsafe { pith_digest_crc32(core::ptr::null(), 0, &mut c) },
            PITH_OK
        );
        assert_eq!(c, 0);
    }

    /// A null `data` that claims a nonzero length is a refusal.
    #[test]
    fn ffi_null_data_with_length_is_refused() {
        let mut out = [0u8; 32];
        let status = unsafe { pith_digest_sha256(core::ptr::null(), 1, out.as_mut_ptr()) };
        assert_eq!(status, PITH_E_INVALID);
    }

    /// Null output slots are refusals, never a write through null.
    #[test]
    fn ffi_null_out_slots_are_refused() {
        let data = b"x";
        unsafe {
            assert_eq!(
                pith_digest_sha256(data.as_ptr(), 1, core::ptr::null_mut()),
                PITH_E_INVALID
            );
            assert_eq!(
                pith_digest_crc32(data.as_ptr(), 1, core::ptr::null_mut()),
                PITH_E_INVALID
            );
            assert_eq!(
                pith_digest_adler32(data.as_ptr(), 1, core::ptr::null_mut()),
                PITH_E_INVALID
            );
            assert_eq!(
                pith_digest_fnv1a64(data.as_ptr(), 1, core::ptr::null_mut()),
                PITH_E_INVALID
            );
        }
    }
}