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
// Copyright (c) 2018-2023 The MobileCoin Foundation

#[cfg(feature = "alloc")]
pub(crate) use alloc::vec::Vec;

/// Boilerplate macro to fill in any trait implementations required by
/// an SgxWrapperType that don't depend on the contents of the inner
/// type.
#[macro_export]
macro_rules! newtype_accessors_impls {
    ($($wrapper:ident, $inner:ty;)*) => {$(
        impl AsMut<$inner> for $wrapper {
            fn as_mut(&mut self) -> &mut $inner {
                &mut self.0
            }
        }

        impl AsRef<$inner> for $wrapper {
            fn as_ref(&self) -> &$inner {
                &self.0
            }
        }

        impl From<$inner> for $wrapper {
            fn from(src: $inner) -> Self {
                Self(src)
            }
        }

        impl<'src> From<&'src $inner> for $wrapper {
            fn from(src: &$inner) -> Self {
                Self(src.clone())
            }
        }

        impl From<$wrapper> for $inner {
            fn from(src: $wrapper) -> $inner {
                src.0
            }
        }

    )*}
}

#[macro_export]
/// Newtype wrapper without a display implementation.
/// TODO: Remove once every type has a Display impl.
macro_rules! impl_newtype_no_display {
    ($($wrapper:ident, $inner:ty;)*) => {$(
        $crate::newtype_accessors_impls! {
            $wrapper, $inner;
        }
    )*}
}
/// Newtype wrapper for a primitive or struct type
#[macro_export]
macro_rules! impl_newtype {
    ($($wrapper:ident, $inner:ty;)*) => {$(
        $crate::newtype_accessors_impls! {
            $wrapper, $inner;
        }

        impl ::core::fmt::Display for $wrapper {
            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                ::core::fmt::Debug::fmt(&self.0, f)
            }
        }
    )*}
}

/// This macro provides common byte-handling operations when the type being
/// wrapped is a struct containing a single fixed-size array of bytes.
///
/// This should be called from within a private submodule.
#[macro_export]
macro_rules! impl_newtype_for_bytestruct {
    ($($wrapper:ident, $inner:ident, $size:ident, $fieldname:ident;)*) => {$(

        $crate::newtype_accessors_impls! {
            $wrapper, $inner;
        }

        impl $wrapper {
            #[doc="Size of the internal array"]
            pub const SIZE: usize = $size;
        }

        impl AsRef<[u8]> for $wrapper {
            fn as_ref(&self) -> &[u8] {
                &(self.0).$fieldname[..]
            }
        }

        impl AsMut<[u8]> for $wrapper {
            fn as_mut(&mut self) -> &mut [u8] {
                &mut (self.0).$fieldname[..]
            }
        }

        impl Ord for $wrapper {
            fn cmp(&self, other: &$wrapper) -> core::cmp::Ordering {
                self.0.$fieldname.cmp(&other.0.$fieldname)
            }
        }

        impl PartialOrd for $wrapper {
            fn partial_cmp(&self, other: &$wrapper) -> Option<core::cmp::Ordering> {
                Some(self.0.$fieldname.cmp(&other.0.$fieldname))
            }
        }

        impl<'bytes> TryFrom<&'bytes [u8]> for $wrapper {
            type Error = $crate::FfiError;

            fn try_from(src: &[u8]) -> ::core::result::Result<Self, Self::Error> {
                if src.len() < $size {
                    return Err($crate::FfiError::InvalidInputLength);
                }

                let mut retval = $wrapper::default();
                (retval.0).$fieldname[..].copy_from_slice(&src[..$size]);
                Ok(retval)
            }
        }

        #[cfg(feature="alloc")]
        impl TryFrom<$crate::macros::Vec<u8>> for $wrapper {
            type Error = $crate::FfiError;

            fn try_from(src: $crate::macros::Vec<u8>) -> ::core::result::Result<Self, Self::Error> {
                Self::try_from(src.as_slice())
            }
        }

        impl From<[u8; $size]> for $wrapper {
            fn from($fieldname: [u8; $size]) -> Self {
                Self($inner { $fieldname })
            }
        }

    )*}
}

/// Derive [UpperHex] and [LowerHex] from [AsRef<T>] to render as a hexadecimal
/// string.
///
/// This is not connected to [ReprBytes] but it is a macro like the above macros
/// that is often needed for structs holding bytes.
#[macro_export]
macro_rules! derive_measurement_hex_from_as_ref {
    ($mytype:ty) => {
        $crate::derive_measurement_hex_from_as_ref!($mytype, [u8]);
    };
    ($mytype:ty, $asref:ty) => {
        impl core::fmt::LowerHex for $mytype {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                let data: &$asref = self.as_ref();
                for d in data {
                    write!(f, "{:02x}", d)?;
                }
                Ok(())
            }
        }

        impl core::fmt::UpperHex for $mytype {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                let data: &$asref = self.as_ref();
                for d in data {
                    write!(f, "{:02X}", d)?;
                }
                Ok(())
            }
        }

        impl hex::FromHex for $mytype {
            type Error = hex::FromHexError;
            fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
                let bytes = <[u8; <$mytype>::SIZE]>::from_hex(hex)?;
                Ok(bytes.into())
            }
        }
    };
}

/// Implement [Debug] and [Display] for enclave measurement types.
/// Measurement types are usually stored/shared a lowercase hex strings with no byte
/// delimiters and no leading '0x'
/// This implementation differs from other bytestruct types.
#[macro_export]
macro_rules! impl_display_and_debug_for_measurement {
    ($($wrapper:ident),*) => {$(
        $crate::derive_measurement_hex_from_as_ref!($wrapper);
        impl core::fmt::Debug for $wrapper {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{}({})", stringify!($wrapper), self)
            }
        }

        impl core::fmt::Display for $wrapper {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{:x}", self)
            }
        }
    )*}
}

/// Implement [Display] for bytestruct types.
/// bytestruct types should display as an uppercase, two-byte underscore-delimited
/// hex string prefixed with a '0x'
/// Non-measurement bytestruct types derive [Debug], so it is not implemented here.
#[macro_export]
macro_rules! impl_display_for_bytestruct {
    ($($wrapper:ident)*) => {$(
        impl ::core::fmt::UpperHex for $wrapper {
            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                let inner: &[u8] = self.as_ref();
                mc_sgx_util::fmt_hex(inner, f)
            }
        }
        impl ::core::fmt::Display for $wrapper {
            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                write!(f, "{:#X}", self)
            }
        }
    )*}
}

#[cfg(test)]
mod test {
    extern crate std;

    use crate::FfiError;
    use std::format;
    use std::string::ToString;
    use yare::parameterized;

    const FIELD_SIZE: usize = 24;

    #[derive(Debug, Default, Eq, Clone, Copy, PartialEq)]
    struct Inner {
        field: [u8; FIELD_SIZE],
    }

    #[derive(Debug, Default, Eq, Clone, PartialEq)]
    #[repr(transparent)]
    struct Outer(Inner);

    impl_newtype_for_bytestruct! {
    Outer, Inner, FIELD_SIZE, field;
    }
    impl_display_for_bytestruct!(Outer);

    #[test]
    fn outer_from_inner() {
        let inner = Inner {
            field: [4u8; Outer::SIZE],
        };
        let outer: Outer = inner.into();
        assert_eq!(outer.0, inner);
    }

    #[parameterized(
    correct_size = { FIELD_SIZE, Ok(Outer::default()) },
    extra_large = { FIELD_SIZE + 1, Ok(Outer::default()) },
    too_small = { FIELD_SIZE - 1, Err(FfiError::InvalidInputLength) },
    )]
    fn try_from(size: usize, result: Result<Outer, FfiError>) {
        let buffer = [0; FIELD_SIZE * 2];
        let slice = &buffer[..size];
        assert_eq!(Outer::try_from(slice), result);

        #[cfg(feature = "alloc")]
        {
            use alloc::vec;
            let zero_vec = vec![0; size];
            assert_eq!(Outer::try_from(zero_vec), result);
        }
    }

    #[test]
    fn from_array() {
        let raw_array = [5u8; Outer::SIZE];
        let outer: Outer = raw_array.try_into().unwrap();
        assert_eq!(outer.0.field, raw_array);
    }

    #[test]
    fn as_ref() {
        let raw_array = [9u8; Outer::SIZE];
        let outer: Outer = raw_array.try_into().unwrap();
        assert_eq!(outer.as_ref(), raw_array);
    }

    #[test]
    fn as_mut() {
        let mut outer = Outer::default();
        let replacement = [11u8; Outer::SIZE];
        let mut_ref: &mut [u8] = outer.as_mut();
        mut_ref.copy_from_slice(&replacement);
        assert_eq!(outer.0.field, replacement);
    }

    #[test]
    fn default() {
        let outer = Outer::default();
        assert_eq!(outer.0.field, [0u8; Outer::SIZE]);
    }

    #[test]
    fn newtype_byte_array_display() {
        let outer = Outer::from([
            0xABu8, 0x00, 0xcd, 0x12, 0xfe, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0a,
            0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13,
        ]);
        assert_eq!(
            outer.to_string(),
            "0xAB00_CD12_FE01_0203_0405_0607_0809_0A0B_0C0D_0E0F_1011_1213"
        );
    }

    #[derive(Debug, Clone, Copy, PartialEq)]
    struct StructInner {
        field: u32,
    }

    #[repr(transparent)]
    struct StructOuter(StructInner);
    impl_newtype! {
        StructOuter, StructInner;
    }

    #[repr(transparent)]
    struct PrimitiveOuter(u32);
    impl_newtype! {
        PrimitiveOuter, u32;
    }

    #[test]
    fn newtype_for_struct() {
        let inner = StructInner { field: 30 };
        let outer: StructOuter = inner.into();
        assert_eq!(outer.0, inner);
    }

    #[test]
    fn display_newtype_for_struct() {
        let inner = StructInner { field: 20 };
        let outer: StructOuter = inner.into();
        assert_eq!(outer.to_string(), "StructInner { field: 20 }");
    }

    #[test]
    fn display_newtype_for_struct_alternate() {
        let inner = StructInner { field: 20 };
        let outer: StructOuter = inner.into();
        let expected = r#"
            StructInner {
                field: 20,
            }"#;
        assert_eq!(format!("\n{outer:#}"), textwrap::dedent(expected));
    }

    #[test]
    fn display_newtype_for_primitive() {
        let inner = 42;
        let outer: PrimitiveOuter = inner.into();
        assert_eq!(outer.to_string(), "42");
    }
}