evidence 0.1.0

Type-level tags for cryptographic primitives
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
//! Message Authentication Codes with type-level tracking.
//!
//! [`Mac<T, M, C>`] represents data with a MAC tag attached. Like [`Signed`](crate::signed::Signed),
//! the payload is inaccessible until verification succeeds — but MACs use symmetric
//! keys rather than public-key cryptography.
//!
//! # Type Parameters
//!
//! - `T`: The payload type that was authenticated
//! - `M`: The MAC primitive (e.g., [`HmacSha256`](hmac::HmacSha256))
//! - `C`: The codec used to serialize the payload
//!
//! # Example
//!
//! ```
//! # #[cfg(feature = "hmac")]
//! # {
//! use evidence::{codec::Identity, mac::{Mac, hmac::HmacSha256}};
//!
//! let key = hmac::digest::Key::<hmac::Hmac<sha2::Sha256>>::from_slice(&[0u8; 64]);
//! let data = b"hello world";
//!
//! let mac: Mac<[u8; 11], HmacSha256, Identity> = Mac::tag(key, data);
//!
//! // Payload is NOT accessible here — must verify first
//! let payload = mac.try_verify(key).unwrap();
//! assert_eq!(payload, *data);
//! # }
//! ```

use alloc::vec::Vec;
use core::{fmt::Debug, marker::PhantomData};

use crate::codec::{Decode, Encode};

/// A MAC primitive that can authenticate and verify messages.
///
/// This trait abstracts over different MAC schemes (HMAC, KMAC, Poly1305, etc.)
/// similar to how [`SignaturePrimitive`](crate::signature::SignaturePrimitive) abstracts
/// over signature algorithms — but MACs use symmetric keys.
pub trait MacPrimitive {
    /// The secret key type (same key for tagging and verification).
    type Key;

    /// The MAC tag type.
    type Tag: Clone + Eq;

    /// Error type returned when verification fails.
    type Error: Debug;

    /// Compute a MAC tag over the message.
    fn mac(key: &Self::Key, message: &[u8]) -> Self::Tag;

    /// Verify a MAC tag against a message.
    ///
    /// # Errors
    ///
    /// Returns an error if the MAC tag does not match.
    fn verify(key: &Self::Key, message: &[u8], tag: &Self::Tag) -> Result<(), Self::Error>;
}

/// Authenticated data with a MAC tag.
///
/// This type deliberately does _not_ provide access to the payload.
/// You must call [`try_verify`](Self::try_verify) to obtain the payload,
/// which ensures you cannot accidentally use unauthenticated data.
///
/// # Construction
///
/// Use [`tag`](Self::tag) to create an authenticated payload.
///
/// For deserialization from untrusted sources, import the
/// [`MacUnchecked`] extension trait.
pub struct Mac<T, M: MacPrimitive, C> {
    tag: M::Tag,
    encoded_payload: Vec<u8>,
    _marker: PhantomData<fn() -> (T, C)>,
}

impl<T, M: MacPrimitive, C> Clone for Mac<T, M, C>
where
    M::Tag: Clone,
{
    fn clone(&self) -> Self {
        Self {
            tag: self.tag.clone(),
            encoded_payload: self.encoded_payload.clone(),
            _marker: PhantomData,
        }
    }
}

impl<T, M: MacPrimitive, C> PartialEq for Mac<T, M, C>
where
    M::Tag: PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.tag == other.tag && self.encoded_payload == other.encoded_payload
    }
}

impl<T, M: MacPrimitive, C> Eq for Mac<T, M, C> where M::Tag: Eq {}

impl<T, M: MacPrimitive, C> core::fmt::Debug for Mac<T, M, C>
where
    M::Tag: core::fmt::Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Mac")
            .field("tag", &self.tag)
            .field("encoded_payload", &self.encoded_payload)
            .finish()
    }
}

impl<T, M: MacPrimitive, C> Mac<T, M, C> {
    /// Create a MAC from its components.
    ///
    /// This is `pub(crate)` — external users should use [`tag`](Self::tag)
    /// or the [`MacUnchecked`] extension trait.
    #[must_use]
    pub(crate) fn new(tag: M::Tag, encoded_payload: Vec<u8>) -> Self {
        Self {
            tag,
            encoded_payload,
            _marker: PhantomData,
        }
    }

    /// Compute a MAC tag over a payload.
    ///
    /// The payload is encoded using codec `C`, then authenticated with key.
    ///
    /// # Panics
    ///
    /// Panics if the codec fails to encode the payload.
    #[must_use]
    #[allow(clippy::expect_used)] // documented panic on encode failure
    pub fn tag(key: &M::Key, payload: &T) -> Self
    where
        C: Encode<T>,
    {
        let encoded = C::encode(payload).expect("encoding failed");
        let tag = M::mac(key, &encoded);
        Self::new(tag, encoded)
    }

    /// Verify the MAC and decode the payload.
    ///
    /// # Errors
    ///
    /// Returns [`MacError::InvalidMac`] if the tag does not verify.
    ///
    /// Returns [`MacError::DecodeError`] if the payload cannot be decoded.
    pub fn try_verify(&self, key: &M::Key) -> Result<T, MacError>
    where
        C: Decode<T>,
    {
        M::verify(key, &self.encoded_payload, &self.tag).map_err(|_| MacError::InvalidMac)?;

        C::decode(&self.encoded_payload).map_err(|_| MacError::DecodeError)
    }

    /// Get the MAC tag.
    #[must_use]
    pub const fn mac_tag(&self) -> &M::Tag {
        &self.tag
    }

    /// Get the encoded payload bytes.
    ///
    /// Note: This returns the raw encoded bytes, _not_ the decoded payload.
    /// The decoded payload is only accessible after verification via
    /// [`try_verify`](Self::try_verify).
    #[must_use]
    pub fn encoded_payload(&self) -> &[u8] {
        &self.encoded_payload
    }
}

/// Error returned when MAC verification fails.
///
/// Details of _why_ verification failed are intentionally hidden
/// to avoid leaking information.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacError {
    /// MAC tag did not verify.
    InvalidMac,

    /// Payload could not be decoded.
    DecodeError,
}

impl core::fmt::Display for MacError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::InvalidMac => write!(f, "invalid MAC"),
            Self::DecodeError => write!(f, "payload decode error"),
        }
    }
}

/// Extension trait for constructing [`Mac`] from raw components.
///
/// This trait is _intentionally_ not in the prelude. Importing it is an explicit
/// acknowledgment that you are bypassing the normal authentication flow.
///
/// # When to use
///
/// - Deserializing authenticated data from storage or network
/// - Interoperating with external systems
/// - Testing
///
/// # Example
///
/// ```
/// # #[cfg(feature = "hmac")]
/// # {
/// use evidence::{codec::Identity, mac::{Mac, MacUnchecked, hmac::HmacSha256}};
///
/// // Reconstruct from deserialized components
/// let tag: [u8; 32] = [0u8; 32];
/// let encoded = vec![1, 2, 3, 4];
///
/// let mac: Mac<Vec<u8>, HmacSha256, Identity> = Mac::from_unchecked_parts(tag, encoded);
/// # }
/// ```
pub trait MacUnchecked<T, M: MacPrimitive, C> {
    /// Create a MAC from raw components.
    ///
    /// # Safety (logical)
    ///
    /// This does not perform any verification. The caller must ensure
    /// the components represent valid authenticated data.
    fn from_unchecked_parts(tag: M::Tag, encoded_payload: Vec<u8>) -> Self;
}

impl<T, M: MacPrimitive, C> MacUnchecked<T, M, C> for Mac<T, M, C> {
    fn from_unchecked_parts(tag: M::Tag, encoded_payload: Vec<u8>) -> Self {
        Self::new(tag, encoded_payload)
    }
}

#[cfg(feature = "serde")]
impl<T, M: MacPrimitive, C> serde::Serialize for Mac<T, M, C>
where
    M::Tag: serde::Serialize,
{
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("Mac", 2)?;
        state.serialize_field("tag", &self.tag)?;
        state.serialize_field("encoded_payload", &self.encoded_payload)?;
        state.end()
    }
}

#[cfg(feature = "serde")]
impl<'de, T, M: MacPrimitive, C> serde::Deserialize<'de> for Mac<T, M, C>
where
    M::Tag: serde::Deserialize<'de>,
{
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        use serde::de::{MapAccess, Visitor};

        struct MacVisitor<T, M: MacPrimitive, C>(PhantomData<(T, M, C)>);

        impl<'de, T, M: MacPrimitive, C> Visitor<'de> for MacVisitor<T, M, C>
        where
            M::Tag: serde::Deserialize<'de>,
        {
            type Value = Mac<T, M, C>;

            fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                formatter.write_str("struct Mac")
            }

            fn visit_map<V: MapAccess<'de>>(self, mut map: V) -> Result<Mac<T, M, C>, V::Error> {
                let mut tag = None;
                let mut encoded_payload = None;

                while let Some(key) = map.next_key::<&str>()? {
                    match key {
                        "tag" => tag = Some(map.next_value()?),
                        "encoded_payload" => encoded_payload = Some(map.next_value()?),
                        _ => {
                            let _: serde::de::IgnoredAny = map.next_value()?;
                        }
                    }
                }

                let tag = tag.ok_or_else(|| serde::de::Error::missing_field("tag"))?;
                let encoded_payload = encoded_payload
                    .ok_or_else(|| serde::de::Error::missing_field("encoded_payload"))?;

                Ok(Mac::new(tag, encoded_payload))
            }
        }

        const FIELDS: &[&str] = &["tag", "encoded_payload"];
        deserializer.deserialize_struct("Mac", FIELDS, MacVisitor(PhantomData))
    }
}

#[cfg(feature = "arbitrary")]
impl<'a, T, M: MacPrimitive, C> arbitrary::Arbitrary<'a> for Mac<T, M, C>
where
    M::Tag: arbitrary::Arbitrary<'a>,
{
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let tag = M::Tag::arbitrary(u)?;
        let encoded_payload = Vec::arbitrary(u)?;
        Ok(Self::new(tag, encoded_payload))
    }
}

#[cfg(feature = "bolero")]
impl<T: 'static, M: MacPrimitive + 'static, C: 'static> bolero_generator::TypeGenerator
    for Mac<T, M, C>
where
    M::Tag: bolero_generator::TypeGenerator,
{
    fn generate<D: bolero_generator::Driver>(driver: &mut D) -> Option<Self> {
        let tag = M::Tag::generate(driver)?;
        let encoded_payload = Vec::generate(driver)?;
        Some(Self::new(tag, encoded_payload))
    }
}

#[cfg(feature = "proptest")]
impl<T: 'static, M: MacPrimitive + 'static, C: 'static> proptest::arbitrary::Arbitrary
    for Mac<T, M, C>
where
    M::Tag: proptest::arbitrary::Arbitrary + 'static,
{
    type Parameters = ();
    type Strategy = proptest::strategy::BoxedStrategy<Self>;

    fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
        use proptest::prelude::*;
        (
            any::<M::Tag>(),
            proptest::collection::vec(any::<u8>(), 0..256),
        )
            .prop_map(|(tag, encoded_payload)| Self::new(tag, encoded_payload))
            .boxed()
    }
}

#[cfg(feature = "rkyv")]
/// Zero-copy [`rkyv`] serialization support for [`Mac`].
pub mod archive {
    use super::{Mac, MacPrimitive};
    use alloc::vec::Vec;
    use rkyv::{Archive, Archived, Deserialize, Serialize, rancor::Fallible};

    impl<T, M: MacPrimitive, C> Archive for Mac<T, M, C>
    where
        M::Tag: Archive,
    {
        type Archived = ArchivedMac<M::Tag>;
        type Resolver = MacResolver<M::Tag>;

        fn resolve(&self, resolver: Self::Resolver, out: rkyv::Place<Self::Archived>) {
            let helper = MacHelper {
                tag: self.tag.clone(),
                encoded_payload: self.encoded_payload.clone(),
            };
            helper.resolve(resolver, out);
        }
    }

    impl<T, M: MacPrimitive, C, S> Serialize<S> for Mac<T, M, C>
    where
        M::Tag: Serialize<S>,
        S: Fallible + rkyv::ser::Allocator + rkyv::ser::Writer + ?Sized,
    {
        fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
            let helper = MacHelper {
                tag: self.tag.clone(),
                encoded_payload: self.encoded_payload.clone(),
            };
            helper.serialize(serializer)
        }
    }

    impl<T, M: MacPrimitive, C, D> Deserialize<Mac<T, M, C>, D> for ArchivedMac<M::Tag>
    where
        M::Tag: Archive,
        Archived<M::Tag>: Deserialize<M::Tag, D>,
        D: Fallible + ?Sized,
        D::Error: rkyv::rancor::Source,
    {
        fn deserialize(&self, deserializer: &mut D) -> Result<Mac<T, M, C>, D::Error> {
            let helper: MacHelper<M::Tag> = <ArchivedMac<M::Tag> as Deserialize<
                MacHelper<M::Tag>,
                D,
            >>::deserialize(self, deserializer)?;
            Ok(Mac::new(helper.tag, helper.encoded_payload))
        }
    }

    /// Helper struct for rkyv serialization.
    ///
    /// The phantom type parameters from [`Mac`] are erased in the archived
    /// form since they only matter at compile time.
    #[derive(Debug, Archive, Serialize, Deserialize)]
    pub struct MacHelper<Tag> {
        tag: Tag,
        encoded_payload: Vec<u8>,
    }

    /// Type alias for the archived form of [`Mac`].
    pub type ArchivedMac<Tag> = ArchivedMacHelper<Tag>;

    /// Type alias for the resolver of [`Mac`].
    pub type MacResolver<Tag> = MacHelperResolver<Tag>;
}

#[cfg(feature = "hmac")]
pub mod hmac;