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
//! C ABI for `purecrypto` (the `ffi` feature).
//!
//! This is the only module permitted to use `unsafe` (the crate sets
//! `unsafe_code = "deny"`, not `forbid`, for exactly this purpose). It exposes
//! `extern "C"` entry points — hashing, HMAC, randomness, RSA/ECDSA keys and
//! signatures, and X.509 parsing/verification — declared in
//! `include/purecrypto.h`.
//!
//! ## Conventions
//! - Functions return [`PcStatus`] (`0` = success, negative = error). Fallible
//! constructors instead return an opaque pointer that is NULL on failure.
//! - Variable-length output uses the in/out length convention: pass a buffer and
//! a `*out_len` holding its capacity; on return `*out_len` is the actual (or,
//! on [`PcStatus::BufferTooSmall`], the required) length. Call with a zero
//! capacity to query the length first.
//! - Opaque handles (`PcHash`, `PcRsaKey`, `PcEcKey`, `PcCert`) are created and
//! freed by the library; every `*_new`/`*_generate`/`*_from_*` is paired with
//! a `*_free`.
//! - Every entry point catches panics, so a Rust panic surfaces as
//! [`PcStatus::Internal`] rather than unwinding across the boundary.
//!
//! Build a C library with, e.g.:
//! `cargo rustc --release --features ffi --crate-type staticlib`
//! (or `--crate-type cdylib`).
pub use PcStatus;
/// Allocates `size` uninitialized bytes in the module's heap and returns a
/// pointer to them (NULL if `size` is 0 or the allocation fails).
///
/// Exposed chiefly for **WebAssembly** hosts: JavaScript cannot allocate inside
/// the wasm linear memory on its own, so it calls `pc_malloc` to reserve space
/// for the input/output buffers the `pc_*` entry points read and write, then
/// releases it with [`pc_free`]. Native C callers can use their own `malloc`.
///
/// The allocation is untracked (aligned for any byte buffer), so the caller
/// must remember `size` and pass the SAME value to [`pc_free`].
pub extern "C"
/// Frees a buffer returned by [`pc_malloc`]. `ptr` must have come from
/// `pc_malloc(size)` with the identical `size`; a NULL `ptr` or zero `size` is
/// a no-op. The buffer must not be used after this call.
///
/// # Safety
/// `ptr` must be a live allocation produced by `pc_malloc(size)` and `size`
/// must match that call exactly.
pub unsafe extern "C"