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
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use serde::{Deserialize, Serialize};
use serde_annotate::Annotate;
use std::convert::TryFrom;
use std::io::{Read, Write};

use super::misc::{KeyMaterial, OwnershipKeyAlg, TlvHeader, TlvTag};
use super::{OwnerApplicationKey, OwnerFlashConfig, OwnerFlashInfoConfig, OwnerRescueConfig};
use crate::crypto::ecdsa::{EcdsaPrivateKey, EcdsaRawSignature};
use crate::with_unknown;

with_unknown! {
    pub enum SramExecMode: u32 [default = Self::DisabledLocked] {
        DisabledLocked = u32::from_le_bytes(*b"LNEX"),
        Disabled = u32::from_le_bytes(*b"NOEX"),
        Enabled = u32::from_le_bytes(*b"EXEC"),
    }
}

/// Describes the owner configuration and key material.
#[derive(Debug, Serialize, Deserialize, Annotate)]
pub struct OwnerBlock {
    /// Header identifying this struct.
    #[serde(default)]
    pub header: TlvHeader,
    /// Version of this structure (ie: currently, zero).
    #[serde(default)]
    pub version: u32,
    /// Whether the owner wants to permit code execution in SRAM.
    #[serde(default)]
    pub sram_exec: SramExecMode,
    /// The key algorithm of the ownership keys.
    pub ownership_key_alg: OwnershipKeyAlg,
    #[serde(default)]
    #[annotate(format=hex)]
    pub reserved: [u32; 3],
    /// The owner identity key.
    pub owner_key: KeyMaterial,
    /// The owner activation key.
    pub activate_key: KeyMaterial,
    /// The owner unlock key.
    pub unlock_key: KeyMaterial,
    /// A list of other configuration items (application keys, flash configuration, etc).
    #[serde(default)]
    pub data: Vec<OwnerConfigItem>,
    #[serde(default, skip_serializing_if = "EcdsaRawSignature::is_empty")]
    #[annotate(format=hex)]
    /// A signature over this block with the owner key.
    pub signature: EcdsaRawSignature,
    /// A sealing value that locks a configuration to a particular device.
    #[serde(default, skip_serializing_if = "Vec::is_empty", with = "serde_bytes")]
    #[annotate(format = hexstr)]
    pub seal: Vec<u8>,
}

impl Default for OwnerBlock {
    fn default() -> Self {
        Self {
            header: TlvHeader::new(TlvTag::Owner, 0),
            version: 0,
            sram_exec: SramExecMode::default(),
            ownership_key_alg: OwnershipKeyAlg::default(),
            reserved: [0u32; 3],
            owner_key: KeyMaterial::default(),
            activate_key: KeyMaterial::default(),
            unlock_key: KeyMaterial::default(),
            data: Vec::new(),
            signature: EcdsaRawSignature::default(),
            seal: vec![0xffu8; 32],
        }
    }
}

impl OwnerBlock {
    const SIZE: usize = 2048;
    const DATA_SIZE: usize = 1728;
    const SIGNATURE_OFFSET: usize = 1952;
    // The not present value must be reflected in the TlvTag::NotPresent value.
    const NOT_PRESENT: u8 = 0x5a;

    pub fn basic() -> Self {
        Self {
            data: vec![
                OwnerConfigItem::ApplicationKey(OwnerApplicationKey::default()),
                OwnerConfigItem::FlashConfig(OwnerFlashConfig::basic()),
                OwnerConfigItem::FlashInfoConfig(OwnerFlashInfoConfig::basic()),
                OwnerConfigItem::RescueConfig(OwnerRescueConfig::all()),
            ],
            ..Default::default()
        }
    }

    pub fn write(&self, dest: &mut impl Write) -> Result<()> {
        let header = TlvHeader::new(TlvTag::Owner, Self::SIZE);
        header.write(dest)?;
        dest.write_u32::<LittleEndian>(self.version)?;
        dest.write_u32::<LittleEndian>(u32::from(self.sram_exec))?;
        dest.write_u32::<LittleEndian>(u32::from(self.ownership_key_alg))?;
        for x in &self.reserved {
            dest.write_u32::<LittleEndian>(*x)?;
        }
        self.owner_key.write_length(dest, 64)?;
        self.activate_key.write_length(dest, 64)?;
        self.unlock_key.write_length(dest, 64)?;
        let mut data = Vec::new();
        for item in &self.data {
            item.write(&mut data)?;
        }
        data.resize(Self::DATA_SIZE, Self::NOT_PRESENT);
        dest.write_all(&data)?;
        self.signature.write(dest)?;
        dest.write_all(&self.seal)?;
        Ok(())
    }

    pub fn read(src: &mut impl Read, header: TlvHeader) -> Result<Self> {
        let version = src.read_u32::<LittleEndian>()?;
        let sram_exec = SramExecMode(src.read_u32::<LittleEndian>()?);
        let ownership_key_alg = OwnershipKeyAlg(src.read_u32::<LittleEndian>()?);
        let mut reserved = [0u32; 3];
        src.read_u32_into::<LittleEndian>(&mut reserved)?;
        let owner_key = KeyMaterial::read_length(src, ownership_key_alg, 64)?;
        let activate_key = KeyMaterial::read_length(src, ownership_key_alg, 64)?;
        let unlock_key = KeyMaterial::read_length(src, ownership_key_alg, 64)?;
        let mut bytes = vec![0u8; Self::DATA_SIZE];
        src.read_exact(&mut bytes)?;
        let mut cursor = std::io::Cursor::new(&bytes);
        let mut data = Vec::new();
        while cursor.position() as usize != Self::DATA_SIZE {
            match OwnerConfigItem::read(&mut cursor)? {
                Some(item) => data.push(item),
                None => break,
            }
        }
        let signature = EcdsaRawSignature::read(src)?;
        let mut seal = vec![0u8; 32];
        src.read_exact(&mut seal)?;
        Ok(Self {
            header,
            version,
            sram_exec,
            ownership_key_alg,
            reserved,
            owner_key,
            activate_key,
            unlock_key,
            data,
            signature,
            seal,
        })
    }
    pub fn sign(&mut self, key: &EcdsaPrivateKey) -> Result<()> {
        let mut data = Vec::new();
        self.write(&mut data)?;
        self.signature = key.digest_and_sign(&data[..Self::SIGNATURE_OFFSET])?;
        Ok(())
    }
}

#[derive(Debug, Serialize, Deserialize, Annotate)]
pub enum OwnerConfigItem {
    ApplicationKey(OwnerApplicationKey),
    FlashInfoConfig(OwnerFlashInfoConfig),
    FlashConfig(OwnerFlashConfig),
    RescueConfig(OwnerRescueConfig),
    Raw(
        #[serde(with = "serde_bytes")]
        #[annotate(format = hexdump)]
        Vec<u8>,
    ),
}

impl OwnerConfigItem {
    pub fn write(&self, dest: &mut impl Write) -> Result<()> {
        match self {
            Self::ApplicationKey(x) => x.write(dest)?,
            Self::FlashInfoConfig(x) => x.write(dest)?,
            Self::FlashConfig(x) => x.write(dest)?,
            Self::RescueConfig(x) => x.write(dest)?,
            Self::Raw(x) => dest.write_all(x)?,
        }
        Ok(())
    }

    pub fn read(src: &mut impl Read) -> Result<Option<Self>> {
        let header = TlvHeader::read(src)?;
        let item = match header.identifier {
            TlvTag::ApplicationKey => Self::ApplicationKey(OwnerApplicationKey::read(src, header)?),
            TlvTag::FlashConfig => Self::FlashConfig(OwnerFlashConfig::read(src, header)?),
            TlvTag::FlashInfoConfig => {
                Self::FlashInfoConfig(OwnerFlashInfoConfig::read(src, header)?)
            }
            TlvTag::Rescue => Self::RescueConfig(OwnerRescueConfig::read(src, header)?),
            TlvTag::NotPresent => return Ok(None),
            _ => {
                let mut data = Vec::new();
                header.write(&mut data)?;
                if header.length >= TlvHeader::SIZE && header.length < OwnerBlock::DATA_SIZE {
                    data.resize(header.length, 0);
                    let len = src.read(&mut data)?;
                    data.resize(len, 0);
                } else {
                    src.read_to_end(&mut data)?;
                }
                Self::Raw(data)
            }
        };
        Ok(Some(item))
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::crypto::ecdsa::EcdsaRawPublicKey;
    use crate::ownership::{
        ApplicationKeyDomain, FlashFlags, OwnerFlashInfoConfig, OwnerFlashRegion, OwnerInfoPage,
        OwnerRescueConfig,
    };
    use crate::util::hexdump::{hexdump_parse, hexdump_string};

    #[rustfmt::skip]
    const OWNER_BIN: &str =
r#"00000000: 4f 57 4e 52 00 08 00 00 00 00 00 00 4c 4e 45 58  OWNR........LNEX
00000010: 50 32 35 36 00 00 00 00 00 00 00 00 00 00 00 00  P256............
00000020: 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11  ................
00000030: 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11  ................
00000040: 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  !!!!!!!!!!!!!!!!
00000050: 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21  !!!!!!!!!!!!!!!!
00000060: 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33  3333333333333333
00000070: 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33 33  3333333333333333
00000080: 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44  DDDDDDDDDDDDDDDD
00000090: 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44 44  DDDDDDDDDDDDDDDD
000000a0: 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55  UUUUUUUUUUUUUUUU
000000b0: 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55  UUUUUUUUUUUUUUUU
000000c0: 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
000000d0: 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
000000e0: 41 50 50 4b 70 00 00 00 50 32 35 36 70 72 6f 64  APPKp...P256prod
000000f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000110: aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa  ................
00000120: aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa  ................
00000130: bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb  ................
00000140: bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb  ................
00000150: 46 4c 53 48 20 00 00 00 00 00 00 01 66 06 00 99  FLSH .......f...
00000160: 66 06 00 00 00 01 00 02 77 17 11 88 77 17 11 11  f.......w...w...
00000170: 49 4e 46 4f 20 00 00 00 00 01 00 00 66 06 00 99  INFO .......f...
00000180: 66 06 00 00 01 05 00 00 77 17 11 88 77 17 11 11  f.......w...w...
00000190: 52 45 53 51 38 00 00 00 58 4d 44 4d 20 00 e0 00  RESQ8...XMDM ...
000001a0: 45 4d 50 54 4d 53 45 43 4e 45 58 54 55 4e 4c 4b  EMPTMSECNEX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5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000710: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000720: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000730: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000740: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000750: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000760: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000770: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000780: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
00000790: 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a 5a  ZZZZZZZZZZZZZZZZ
000007a0: 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77  wwwwwwwwwwwwwwww
000007b0: 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77  wwwwwwwwwwwwwwww
000007c0: 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88  ................
000007d0: 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88  ................
000007e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ................
000007f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff  ................
"#;

    const OWNER_JSON: &str = r#"{
  header: {
    identifier: "Owner",
    length: 2048
  },
  version: 0,
  sram_exec: "DisabledLocked",
  ownership_key_alg: "EcdsaP256",
  reserved: [
    0x0,
    0x0,
    0x0
  ],
  owner_key: {
    Ecdsa: {
      x: "1111111111111111111111111111111111111111111111111111111111111111",
      y: "2121212121212121212121212121212121212121212121212121212121212121"
    }
  },
  activate_key: {
    Ecdsa: {
      x: "3333333333333333333333333333333333333333333333333333333333333333",
      y: "4444444444444444444444444444444444444444444444444444444444444444"
    }
  },
  unlock_key: {
    Ecdsa: {
      x: "5555555555555555555555555555555555555555555555555555555555555555",
      y: "6666666666666666666666666666666666666666666666666666666666666666"
    }
  },
  data: [
    {
      ApplicationKey: {
        header: {
          identifier: "ApplicationKey",
          length: 112
        },
        key_alg: "EcdsaP256",
        key_domain: "Prod",
        key_diversifier: [
          0x0,
          0x0,
          0x0,
          0x0,
          0x0,
          0x0,
          0x0
        ],
        usage_constraint: 0x0,
        key: {
          Ecdsa: {
            x: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            y: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
          }
        }
      }
    },
    {
      FlashConfig: {
        header: {
          identifier: "FlashConfig",
          length: 32
        },
        config: [
          {
            start: 0,
            size: 256,
            read: true,
            program: true,
            erase: true,
            scramble: true,
            ecc: true,
            high_endurance: true,
            protect_when_primary: false,
            lock: false
          },
          {
            start: 256,
            size: 512,
            read: true,
            program: true,
            erase: true,
            scramble: true,
            ecc: true,
            high_endurance: true,
            protect_when_primary: false,
            lock: false
          }
        ]
      }
    },
    {
      FlashInfoConfig: {
        header: {
          identifier: "FlashInfoConfig",
          length: 32
        },
        config: [
          {
            bank: 0,
            page: 1,
            pad: 0,
            read: true,
            program: true,
            erase: true,
            scramble: true,
            ecc: true,
            high_endurance: true,
            protect_when_primary: false,
            lock: false
          },
          {
            bank: 1,
            page: 5,
            pad: 0,
            read: true,
            program: true,
            erase: true,
            scramble: true,
            ecc: true,
            high_endurance: true,
            protect_when_primary: false,
            lock: false
          }
        ]
      }
    },
    {
      RescueConfig: {
        header: {
          identifier: "Rescue",
          length: 56
        },
        rescue_type: "Xmodem",
        start: 32,
        size: 224,
        command_allow: [
          "Empty",
          "MinBl0SecVerRequest",
          "NextBl0SlotRequest",
          "OwnershipUnlockRequest",
          "OwnershipActivateRequest",
          "Rescue",
          "GetBootLog",
          "BootSvcReq",
          "BootSvcRsp",
          "OwnerBlock"
        ]
      }
    }
  ],
  signature: {
    r: "7777777777777777777777777777777777777777777777777777777777777777",
    s: "8888888888888888888888888888888888888888888888888888888888888888"
  },
  seal: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
}"#;

    #[test]
    fn test_owner_write() -> Result<()> {
        let own = OwnerBlock {
            version: 0,
            sram_exec: SramExecMode::default(),
            ownership_key_alg: OwnershipKeyAlg::EcdsaP256,
            owner_key: KeyMaterial::Ecdsa(EcdsaRawPublicKey {
                x: hex::decode("1111111111111111111111111111111111111111111111111111111111111111")?,
                y: hex::decode("2121212121212121212121212121212121212121212121212121212121212121")?,
            }),
            activate_key: KeyMaterial::Ecdsa(EcdsaRawPublicKey {
                x: hex::decode("3333333333333333333333333333333333333333333333333333333333333333")?,
                y: hex::decode("4444444444444444444444444444444444444444444444444444444444444444")?,
            }),
            unlock_key: KeyMaterial::Ecdsa(EcdsaRawPublicKey {
                x: hex::decode("5555555555555555555555555555555555555555555555555555555555555555")?,
                y: hex::decode("6666666666666666666666666666666666666666666666666666666666666666")?,
            }),
            data: vec![
                OwnerConfigItem::ApplicationKey(OwnerApplicationKey {
                    key_alg: OwnershipKeyAlg::EcdsaP256,
                    key_domain: ApplicationKeyDomain::Prod,
                    key: KeyMaterial::Ecdsa(EcdsaRawPublicKey {
                        x: hex::decode(
                            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                        )?,
                        y: hex::decode(
                            "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
                        )?,
                    }),
                    ..Default::default()
                }),
                OwnerConfigItem::FlashConfig(OwnerFlashConfig {
                    config: vec![
                        OwnerFlashRegion {
                            start: 0x000,
                            size: 0x100,
                            flags: FlashFlags::basic(),
                        },
                        OwnerFlashRegion {
                            start: 0x100,
                            size: 0x200,
                            flags: FlashFlags::basic(),
                        },
                    ],
                    ..Default::default()
                }),
                OwnerConfigItem::FlashInfoConfig(OwnerFlashInfoConfig {
                    config: vec![
                        OwnerInfoPage::new(0, 1, FlashFlags::basic()),
                        OwnerInfoPage::new(1, 5, FlashFlags::basic()),
                    ],
                    ..Default::default()
                }),
                OwnerConfigItem::RescueConfig(OwnerRescueConfig::all()),
            ],
            signature: EcdsaRawSignature {
                r: hex::decode("7777777777777777777777777777777777777777777777777777777777777777")?,
                s: hex::decode("8888888888888888888888888888888888888888888888888888888888888888")?,
            },
            seal: hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")?,
            ..Default::default()
        };
        let mut bin = Vec::new();
        own.write(&mut bin)?;
        eprintln!("{}", hexdump_string(&bin)?);
        assert_eq!(hexdump_string(&bin)?, OWNER_BIN);
        Ok(())
    }

    #[test]
    fn test_owner_read() -> Result<()> {
        let buf = hexdump_parse(OWNER_BIN)?;
        let mut cur = std::io::Cursor::new(&buf);
        let header = TlvHeader::read(&mut cur)?;
        let own = OwnerBlock::read(&mut cur, header)?;
        let doc = serde_annotate::serialize(&own)?.to_json5().to_string();
        eprintln!("{}", doc);
        assert_eq!(doc, OWNER_JSON);
        Ok(())
    }
}