tpm2-protocol 0.19.0

TPM 2.0 marshaler/unmarshaler
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2025 Opinsys Oy
// Copyright (c) 2024-2025 Jarkko Sakkinen

#[macro_export]
macro_rules! tpm_struct {
    (@wire_field_methods [$($prev_type:ty,)*],) => {};

    (@wire_field_methods [$($prev_type:ty,)*], pub $field_name:ident: $field_type:ty $(, pub $rest_field:ident: $rest_type:ty)* $(,)?) => {
        /// Returns a borrowed field view.
        ///
        /// # Errors
        ///
        /// Returns `Err(TpmError)` when preceding fields or this field are malformed.
        #[allow(unused_mut)]
        pub fn $field_name(&self) -> $crate::TpmResult<&$field_type>
        where
            $($prev_type: for<'a> $crate::TpmField<'a>,)*
            $field_type: for<'a> $crate::TpmField<'a, View = &'a $field_type>,
        {
            let mut cursor = &self.0;
            $(
                let (_, tail) = <$prev_type as $crate::TpmField>::cast_prefix_field(cursor)?;
                cursor = tail;
            )*

            let (value, _) = <$field_type as $crate::TpmField>::cast_prefix_field(cursor)?;

            Ok(value)
        }

        $crate::tpm_struct!(@wire_field_methods [$($prev_type,)* $field_type,], $(pub $rest_field: $rest_type),*);
    };

    (
        $(#[$meta:meta])*
        kind: Command,
        name: $name:ident,
        cc: $cc:expr,
        handles: $count:literal,
        parameters: {
            $(pub $param_field:ident: $param_type:ty),*
            $(,)?
        }
    ) => {
        $(#[$meta])*
        pub struct $name {
            pub handles: [$crate::basic::TpmHandle; $count],
            $(pub $param_field: $param_type,)*
        }

        impl $crate::frame::TpmHeader for $name {
            const CC: $crate::data::TpmCc = $cc;
            const HANDLES: usize = $count;
        }

        impl $name {
            /// Casts a command frame into a typed wire view for this command.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when the frame is malformed or
            /// has a different command code.
            pub fn cast_frame(buf: &[u8]) -> $crate::TpmResult<&$crate::frame::TpmCommand> {
                let command = <$crate::frame::TpmCommand>::cast(buf)?;

                let cc = command.cc()?;
                if cc != Self::CC {
                    return Err($crate::TpmError::InvalidCc(
                        $crate::TpmErrorValue::new(6).value(u64::from(cc.value())),
                    ));
                }

                Ok(command)
            }

            /// Casts a mutable command frame into a typed mutable wire view for this command.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when the frame is malformed or
            /// has a different command code.
            pub fn cast_frame_mut(
                buf: &mut [u8],
            ) -> $crate::TpmResult<&mut $crate::frame::TpmCommand> {
                let command = <$crate::frame::TpmCommand>::cast_mut(buf)?;

                let cc = command.cc()?;
                if cc != Self::CC {
                    return Err($crate::TpmError::InvalidCc(
                        $crate::TpmErrorValue::new(6).value(u64::from(cc.value())),
                    ));
                }

                Ok(command)
            }
        }

        impl $crate::frame::TpmFrame for $name {
            fn cc(&self) -> $crate::data::TpmCc {
                Self::CC
            }
            fn handles(&self) -> usize {
                Self::HANDLES
            }
        }

        impl $crate::TpmSized for $name {
            const SIZE: usize = (Self::HANDLES * <$crate::basic::TpmHandle>::SIZE) $(+ <$param_type>::SIZE)*;
            fn len(&self) -> usize {
                (self.handles.len() * <$crate::basic::TpmHandle>::SIZE) $(+ $crate::TpmSized::len(&self.$param_field))*
            }
        }

        impl $crate::TpmMarshal for $name {
            #[allow(unused_variables)]
            fn marshal(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                <Self as $crate::frame::TpmMarshalBody>::marshal_handles(self, writer)?;
                <Self as $crate::frame::TpmMarshalBody>::marshal_parameters(self, writer)
            }
        }

        impl $crate::frame::TpmMarshalBody for $name {
            #[allow(unused_variables)]
            fn marshal_handles(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                for handle in &self.handles {
                    $crate::TpmMarshal::marshal(handle, writer)?;
                }
                Ok(())
            }

            #[allow(unused_variables)]
            fn marshal_parameters(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                $($crate::TpmMarshal::marshal(&self.$param_field, writer)?;)*
                Ok(())
            }
        }
    };

    (
        $(#[$meta:meta])*
        kind: Response,
        name: $name:ident,
        cc: $cc:expr,
        handles: $count:literal,
        parameters: {
            $(pub $param_field:ident: $param_type:ty),*
            $(,)?
        }
    ) => {
        $(#[$meta])*
        pub struct $name {
            pub handles: [$crate::basic::TpmHandle; $count],
            $(pub $param_field: $param_type,)*
        }

        impl $crate::frame::TpmHeader for $name {
            const CC: $crate::data::TpmCc = $cc;
            const HANDLES: usize = $count;
        }

        impl $name {
            /// Casts a response frame into a typed wire view for this response.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when the frame envelope is malformed.
            pub fn cast_frame(buf: &[u8]) -> $crate::TpmResult<&$crate::frame::TpmResponse> {
                <$crate::frame::TpmResponse>::cast(buf)
            }

            /// Casts a mutable response frame into a typed mutable wire view for this response.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when the frame envelope is malformed.
            pub fn cast_frame_mut(
                buf: &mut [u8],
            ) -> $crate::TpmResult<&mut $crate::frame::TpmResponse> {
                <$crate::frame::TpmResponse>::cast_mut(buf)
            }
        }

        impl $crate::frame::TpmFrame for $name {
            fn cc(&self) -> $crate::data::TpmCc {
                Self::CC
            }
            fn handles(&self) -> usize {
                Self::HANDLES
            }
        }

        impl $crate::frame::TpmMarshalBody for $name {
            #[allow(unused_variables)]
            fn marshal_handles(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                for handle in &self.handles {
                    $crate::TpmMarshal::marshal(handle, writer)?;
                }
                Ok(())
            }
            #[allow(unused_variables)]
            fn marshal_parameters(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                $($crate::TpmMarshal::marshal(&self.$param_field, writer)?;)*
                Ok(())
            }
        }

        impl $crate::TpmSized for $name {
            const SIZE: usize = (Self::HANDLES * <$crate::basic::TpmHandle>::SIZE) $(+ <$param_type>::SIZE)*;
            fn len(&self) -> usize {
                (self.handles.len() * <$crate::basic::TpmHandle>::SIZE) $(+ $crate::TpmSized::len(&self.$param_field))*
            }
        }

        impl $crate::TpmMarshal for $name {
            fn marshal(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                <Self as $crate::frame::TpmMarshalBody>::marshal_handles(self, writer)?;
                <Self as $crate::frame::TpmMarshalBody>::marshal_parameters(self, writer)
            }
        }
    };

    (
        $(#[$meta:meta])*
        wire: $wire:ident,
        $vis:vis struct $name:ident {
            $(pub $field_name:ident: $field_type:ty),*
            $(,)?
        }
    ) => {
        $(#[$meta])*
        $vis struct $name {
            $(pub $field_name: $field_type,)*
        }

        #[repr(transparent)]
        $vis struct $wire([u8]);

        impl $wire {
            /// Casts bytes into a typed wire structure view.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` is not exactly one valid wire
            /// structure.
            pub fn cast(buf: &[u8]) -> $crate::TpmResult<&Self>
            where
                $($field_type: for<'a> $crate::TpmField<'a>,)*
            {
                Self::validate(buf)?;

                // SAFETY: `validate` checked the complete wire structure.
                Ok(unsafe { Self::cast_unchecked(buf) })
            }

            /// Casts the first typed wire structure from `buf` and returns the remainder.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` does not start with a valid wire
            /// structure.
            pub fn cast_prefix(buf: &[u8]) -> $crate::TpmResult<(&Self, &[u8])>
            where
                $($field_type: for<'a> $crate::TpmField<'a>,)*
            {
                let wire_len = Self::validate_prefix(buf)?;
                if buf.len() < wire_len {
                    return Err($crate::TpmError::UnexpectedEnd(
                        $crate::TpmErrorValue::new(0).size(wire_len, buf.len()),
                    ));
                }

                let (head, tail) = buf.split_at(wire_len);

                // SAFETY: `validate_prefix` checked the complete wire structure.
                Ok((unsafe { Self::cast_unchecked(head) }, tail))
            }

            /// Casts bytes into a typed wire structure view without validation.
            ///
            /// # Safety
            ///
            /// The caller must ensure `buf` is exactly one valid wire structure.
            #[must_use]
            pub unsafe fn cast_unchecked(buf: &[u8]) -> &Self {
                let ptr = core::ptr::from_ref(buf) as *const Self;

                // SAFETY: `$wire` is `repr(transparent)` over `[u8]`, so it has
                // the same layout, metadata, and alignment as the referenced slice.
                unsafe { &*ptr }
            }

            /// Returns the complete wire structure bytes.
            #[must_use]
            pub const fn as_bytes(&self) -> &[u8] {
                &self.0
            }

            /// Validates an exact typed wire structure view.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` is not exactly one valid wire
            /// structure.
            pub fn validate(buf: &[u8]) -> $crate::TpmResult<()>
            where
                $($field_type: for<'a> $crate::TpmField<'a>,)*
            {
                let wire_len = Self::validate_prefix(buf)?;

                if buf.len() > wire_len {
                    return Err($crate::TpmError::TrailingData(
                        $crate::TpmErrorValue::new(wire_len).actual(buf.len() - wire_len),
                    ));
                }

                Ok(())
            }

            #[allow(unused_assignments, unused_mut, unused_variables)]
            /// Validates a typed wire structure prefix.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` does not start with a valid wire
            /// structure.
            pub fn validate_prefix(buf: &[u8]) -> $crate::TpmResult<usize>
            where
                $($field_type: for<'a> $crate::TpmField<'a>,)*
            {
                let mut cursor = buf;
                let mut consumed = 0usize;

                $(
                    let before = cursor.len();
                    let (_, tail) = <$field_type as $crate::TpmField>::cast_prefix_field(cursor)?;
                    let field_len = before.checked_sub(tail.len()).ok_or(
                        $crate::TpmError::IntegerTooLarge(
                            $crate::TpmErrorValue::new(consumed).value_usize(tail.len()),
                        ),
                    )?;
                    consumed = consumed.checked_add(field_len).ok_or(
                        $crate::TpmError::IntegerTooLarge(
                            $crate::TpmErrorValue::new(consumed).value_usize(before),
                        ),
                    )?;
                    cursor = tail;
                )*

                Ok(consumed)
            }

            $crate::tpm_struct!(@wire_field_methods [], $(pub $field_name: $field_type),*);
        }

        impl AsRef<[u8]> for $wire {
            fn as_ref(&self) -> &[u8] {
                self.as_bytes()
            }
        }

        impl<'a> $crate::TpmField<'a> for $name
        where
            $($field_type: for<'b> $crate::TpmField<'b>,)*
        {
            type View = &'a $wire;

            fn cast_prefix_field(buf: &'a [u8]) -> $crate::TpmResult<(Self::View, &'a [u8])> {
                $wire::cast_prefix(buf)
            }
        }

        impl $crate::TpmSized for $name {
            const SIZE: usize = 0 $(+ <$field_type>::SIZE)*;
            fn len(&self) -> usize {
                0 $(+ $crate::TpmSized::len(&self.$field_name))*
            }
        }

        impl $crate::TpmMarshal for $name {
            #[allow(unused_variables)]
            fn marshal(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                $( $crate::TpmMarshal::marshal(&self.$field_name, writer)?; )*
                Ok(())
            }
        }

    };
}

#[macro_export]
macro_rules! tpm2b {
    ($name:ident, $capacity:expr) => {
        pub type $name = $crate::basic::TpmBuffer<$capacity>;
    };
}

#[macro_export]
macro_rules! tpm2b_struct {
    (
        $(#[$meta:meta])*
        wire: $wire_ty:ident,
        $wrapper_ty:ident, $inner_ty:ty) => {
        $(#[$meta])*
        pub struct $wrapper_ty {
            pub inner: $inner_ty,
        }

        #[repr(transparent)]
        pub struct $wire_ty([u8]);

        impl $wire_ty {
            /// Casts bytes into a typed TPM2B wire view.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` is not exactly one valid TPM2B
            /// wrapper for the inner type.
            pub fn cast(buf: &[u8]) -> $crate::TpmResult<&Self>
            where
                $inner_ty: for<'a> $crate::TpmField<'a>,
            {
                Self::validate(buf)?;

                // SAFETY: `validate` checked the complete TPM2B wrapper.
                Ok(unsafe { Self::cast_unchecked(buf) })
            }

            /// Casts the first TPM2B wire view from `buf` and returns the remainder.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` does not start with a valid TPM2B
            /// wrapper for the inner type.
            pub fn cast_prefix(buf: &[u8]) -> $crate::TpmResult<(&Self, &[u8])>
            where
                $inner_ty: for<'a> $crate::TpmField<'a>,
            {
                let wire_len = Self::validate_prefix(buf)?;
                let (head, tail) = buf.split_at(wire_len);

                // SAFETY: `validate_prefix` checked the complete TPM2B wrapper.
                Ok((unsafe { Self::cast_unchecked(head) }, tail))
            }

            /// Casts bytes into a typed TPM2B wire view without validation.
            ///
            /// # Safety
            ///
            /// The caller must ensure `buf` is exactly one valid TPM2B wrapper for
            /// the inner type.
            #[must_use]
            pub unsafe fn cast_unchecked(buf: &[u8]) -> &Self {
                let ptr = core::ptr::from_ref(buf) as *const Self;

                // SAFETY: `$wire_ty` is `repr(transparent)` over `[u8]`, so it has
                // the same layout, metadata, and alignment as the referenced slice.
                unsafe { &*ptr }
            }

            /// Returns the complete TPM2B wrapper bytes.
            #[must_use]
            pub const fn as_bytes(&self) -> &[u8] {
                &self.0
            }

            /// Returns the borrowed inner structure view.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when the TPM2B payload is not exactly one
            /// valid inner structure.
            pub fn inner(&self) -> $crate::TpmResult<<$inner_ty as $crate::TpmField<'_>>::View>
            where
                $inner_ty: for<'a> $crate::TpmField<'a>,
            {
                let inner_bytes = &self.0[<$crate::basic::TpmUint16 as $crate::TpmSized>::SIZE..];
                let (inner, tail) = <$inner_ty as $crate::TpmField>::cast_prefix_field(inner_bytes)?;

                if !tail.is_empty() {
                    return Err($crate::TpmError::TrailingData(
                        $crate::TpmErrorValue::at(&self.0, tail).actual(tail.len()),
                    ));
                }

                Ok(inner)
            }

            /// Validates an exact typed TPM2B wire view.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` is not exactly one valid TPM2B
            /// wrapper for the inner type.
            pub fn validate(buf: &[u8]) -> $crate::TpmResult<()>
            where
                $inner_ty: for<'a> $crate::TpmField<'a>,
            {
                let wire_len = Self::validate_prefix(buf)?;

                if buf.len() > wire_len {
                    return Err($crate::TpmError::TrailingData(
                        $crate::TpmErrorValue::new(wire_len).actual(buf.len() - wire_len),
                    ));
                }

                Ok(())
            }

            /// Validates a typed TPM2B wire view prefix.
            ///
            /// # Errors
            ///
            /// Returns `Err(TpmError)` when `buf` does not start with a valid TPM2B
            /// wrapper for the inner type.
            pub fn validate_prefix(buf: &[u8]) -> $crate::TpmResult<usize>
            where
                $inner_ty: for<'a> $crate::TpmField<'a>,
            {
                let (size_field, payload) = <$crate::basic::TpmUint16 as $crate::TpmCast>::cast_prefix(buf)?;
                let payload_len = size_field.get() as usize;

                if payload.len() < payload_len {
                    return Err($crate::TpmError::UnexpectedEnd(
                        $crate::TpmErrorValue::at(buf, payload).size(payload_len, payload.len()),
                    ));
                }

                let (inner_bytes, _) = payload.split_at(payload_len);
                let (_, tail) = <$inner_ty as $crate::TpmField>::cast_prefix_field(inner_bytes)?;

                if !tail.is_empty() {
                    return Err($crate::TpmError::TrailingData(
                        $crate::TpmErrorValue::at(buf, tail).actual(tail.len()),
                    ));
                }

                Ok(<$crate::basic::TpmUint16 as $crate::TpmSized>::SIZE + payload_len)
            }
        }

        impl AsRef<[u8]> for $wire_ty {
            fn as_ref(&self) -> &[u8] {
                self.as_bytes()
            }
        }

        impl<'a> $crate::TpmField<'a> for $wrapper_ty
        where
            $inner_ty: for<'b> $crate::TpmField<'b>,
        {
            type View = &'a $wire_ty;

            fn cast_prefix_field(buf: &'a [u8]) -> $crate::TpmResult<(Self::View, &'a [u8])> {
                $wire_ty::cast_prefix(buf)
            }
        }

        impl $crate::TpmSized for $wrapper_ty {
            const SIZE: usize = $crate::basic::TpmUint16::SIZE + <$inner_ty>::SIZE;
            fn len(&self) -> usize {
                $crate::basic::TpmUint16::SIZE + $crate::TpmSized::len(&self.inner)
            }
        }

        impl $crate::TpmMarshal for $wrapper_ty
        where
            $inner_ty: $crate::TpmSized,
        {
            fn marshal(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
                let inner_len = $crate::TpmSized::len(&self.inner);
                let len_field = <$crate::basic::TpmUint16>::try_from(inner_len)
                    .map_err(|_| $crate::TpmError::IntegerTooLarge(
                        $crate::TpmErrorValue::new(writer.len()).value_usize(inner_len),
                    ))?;
                len_field.marshal(writer)?;
                $crate::TpmMarshal::marshal(&self.inner, writer)
            }
        }

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

        impl core::ops::Deref for $wrapper_ty {
            type Target = $inner_ty;
            fn deref(&self) -> &Self::Target {
                &self.inner
            }
        }

        impl core::ops::DerefMut for $wrapper_ty {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.inner
            }
        }
    };
}

#[macro_export]
macro_rules! tpml {
    ($name:ident, $inner_ty:ty, $capacity:expr) => {
        pub type $name = $crate::basic::TpmList<$inner_ty, $capacity>;
    };
}