espsign 0.1.0

A utility for signing ESP32 firmware images for ESP RSA Secure Boot V2
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
//! Utilities for ESP Secure Boot V2 RSA signature block verification and signing
//!
//! For now, only Secure Boot V2 is supported, with the RSA-based signature block,
//! as this is what seemingly Espressif recommends*. In future, it can be extended
//! with support for ECC signatures, as well as Secure Boot V1.
//!
//! The module is `no_std` (but needs `alloc` because Rust Crypto RSA needs it)
//! so that it can also be used on the chip itself for e.g. verifying image signatures
//! during OTA updates for baremetal apps. Note though that the on-chip verification
//! would be slow(er), because the Esp RSA and SHA peripherals are not utilized yet.
//!
//! * https://docs.espressif.com/projects/esp-idf/en/v5.3.1/esp32h2/security/secure-boot-v2.html#signature-block-format
#![no_std]
#![warn(clippy::large_futures)]

use alloc::boxed::Box;

use core::fmt::{self, Debug, Display};
use core::marker::PhantomData;

use embedded_io_async::{Error, ErrorType, Read, ReadExactError, Write};

use log::{error, info};

use num_bigint::{traits::ModInverse, ToBigUint};
use num_traits::cast::ToPrimitive;

use rand_core::{CryptoRng, RngCore};

use rsa::{traits::PublicKeyParts, BigUint, Pss, RsaPrivateKey, RsaPublicKey};

use sha2::{Digest, Sha256};

extern crate alloc;

#[cfg(feature = "std")]
pub use std::*;

/// The RSA crate is re-exported for user convenience
/// so that users of the lib do not have to explicitly depend on it
pub mod rsa {
    pub use ::rsa::*;
}

/// A null writer that writes to nowhere
/// Implements the `Write` trait from `embedded-io-async`
pub struct NullWrite<E>(PhantomData<fn() -> E>);

impl<E> Default for NullWrite<E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<E> NullWrite<E> {
    /// Create a new null writer
    pub const fn new() -> Self {
        Self(PhantomData::<fn() -> E>)
    }
}

impl<E> ErrorType for NullWrite<E>
where
    E: Error,
{
    type Error = E;
}

impl<E> Write for NullWrite<E>
where
    E: Error,
{
    async fn write(&mut self, data: &[u8]) -> Result<usize, Self::Error> {
        Ok(data.len())
    }
}

/// Errors that can occur during verification
#[derive(Debug)]
pub enum SignError<E> {
    /// IO error
    Io(E),
    /// Signed image is not padded correctly (to 64K for app images and to 4K for bootloader images)
    InvalidImageLen,
}

impl<E> SignError<E> {
    /// Map the IO error to another one
    pub fn map<E2>(self, f: impl FnOnce(E) -> E2) -> SignError<E2> {
        match self {
            SignError::Io(e) => SignError::Io(f(e)),
            SignError::InvalidImageLen => SignError::InvalidImageLen,
        }
    }
}

impl<E> Display for SignError<E>
where
    E: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Io(e) => write!(f, "IO error: {:?}", e),
            Self::InvalidImageLen => write!(f, "Invalid image length"),
        }
    }
}

impl<E> core::error::Error for SignError<E> where E: Debug {}

/// Errors that can occur during verification
#[derive(Debug)]
pub enum VerifyError<E> {
    /// IO error
    Io(E),
    /// Unexpected EOF
    Eof,
    /// Invalid ESP signature block (wrong magic byte, wrong version or CRC does not match)
    InvalidSignatureBlock,
    /// Invalid Sha256 hash
    InvalidHash,
    /// Invalid RSA-PSS signature
    InvalidSignature,
    /// Signed image is not padded correctly (to 64K for app images and to 4K for bootloader images)
    InvalidImageLen,
}

impl<E> VerifyError<E> {
    /// Map the IO error to another one
    pub fn map<E2>(self, f: impl FnOnce(E) -> E2) -> VerifyError<E2> {
        match self {
            VerifyError::Io(e) => VerifyError::Io(f(e)),
            VerifyError::Eof => VerifyError::Eof,
            VerifyError::InvalidSignatureBlock => VerifyError::InvalidSignatureBlock,
            VerifyError::InvalidHash => VerifyError::InvalidHash,
            VerifyError::InvalidSignature => VerifyError::InvalidSignature,
            VerifyError::InvalidImageLen => VerifyError::InvalidImageLen,
        }
    }
}

impl<E> From<ReadExactError<E>> for VerifyError<E> {
    fn from(e: ReadExactError<E>) -> Self {
        match e {
            ReadExactError::UnexpectedEof => Self::Eof,
            ReadExactError::Other(e) => Self::Io(e),
        }
    }
}

impl<E> Display for VerifyError<E>
where
    E: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Io(e) => write!(f, "IO error: {:?}", e),
            Self::Eof => write!(f, "Unexpected EOF"),
            Self::InvalidSignatureBlock => write!(f, "Invalid signature block"),
            Self::InvalidHash => write!(f, "Invalid hash"),
            Self::InvalidSignature => write!(f, "Invalid signature"),
            Self::InvalidImageLen => write!(f, "Invalid image length"),
        }
    }
}

impl<E> core::error::Error for VerifyError<E> where E: Debug {}

/// Type of image to sign or verify
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ImageType {
    /// Bootloader image (image will/should be padded to 4K boundary)
    Bootloader,
    /// Application image (image will/should be padded to 64K boundary)
    App,
}

impl ImageType {
    /// Get the alignment for the image type
    pub fn align(&self) -> usize {
        match self {
            ImageType::Bootloader => 4096,
            ImageType::App => 65536,
        }
    }
}

/// ESP Secure Boot V2 RSA Public key
///
/// Embedded in the signature block below, as well as used as-is for
/// generating the public key SHA-256 signature that needs to be burned into E-FUSE
///
/// This type is `repr(C)` and `repr(packed)` so that it can be directly transmuted from a memory-mapped
/// region of the ESP32 flash memory, if necessary. In other words, its memory layout represents exactly the
/// layout of a serialized ESP Secure Boot V2 RSA public key.
#[repr(C)]
#[repr(packed)]
pub struct SBV2RsaPubKey {
    /// RSA Public Modulus used for signature verification. (value ‘n’ in RFC8017).
    ///
    /// NOTE: LE order instead of (regular) BE order, as the Esp RSA peripheral uses LE.
    rsa_public_modulus: [u8; 384],
    /// RSA Public Exponent used for signature verification (value ‘e’ in RFC8017).
    ///
    /// NOTE: LE order instead of (regular) BE order, as the Esp RSA peripheral uses LE.
    rsa_public_exponent: [u8; 4],
    /// Pre-calculated `R`, derived from ‘n’.
    ///
    /// NOTE: LE order instead of (regular) BE order, as the Esp RSA peripheral uses LE.
    rsa_precalc_r: [u8; 384],
    /// Pre-calculated `M`, derived from ‘n’
    /// NOTE: LE order instead of (regular) BE order, as the Esp RSA peripheral uses LE.
    rsa_precalc_m: [u8; 4],
}

impl SBV2RsaPubKey {
    /// Create a new Secure Boot V2 RSA public key from the given RSA public key
    pub fn create(pub_key: &RsaPublicKey) -> Self {
        let mut this = Self::new_empty();

        this.fill(pub_key);

        this
    }

    /// Save the Sha256 hash of the public key to the output
    ///
    /// The saved hash should then be burned into E-FUSE with the `espefuse` tool
    /// when Secure Boot V2 is being enabled for the Esp chip
    ///
    /// # Arguments
    /// * `out` - Output to write the hash to
    pub async fn save_hash<W>(&self, mut out: W) -> Result<(), W::Error>
    where
        W: Write,
    {
        let mut hasher = Sha256::new();

        self.fill_hash(&mut hasher);

        out.write_all(&hasher.finalize()).await?;

        Ok(())
    }

    /// Load the Secure Boot V2 RSA public key from the input
    ///
    /// # Arguments
    /// * `read` - Input to read the public key from
    async fn load<R>(&mut self, mut read: R) -> Result<(), ReadExactError<R::Error>>
    where
        R: Read,
    {
        read.read_exact(&mut self.rsa_public_modulus).await?;
        read.read_exact(&mut self.rsa_public_exponent).await?;
        read.read_exact(&mut self.rsa_precalc_r).await?;
        read.read_exact(&mut self.rsa_precalc_m).await?;

        Ok(())
    }

    /// Save the Secure Boot V2 RSA public key to the output
    ///
    /// # Arguments
    /// * `out` - Output to write the public key to
    async fn save<W>(&self, mut out: W) -> Result<(), W::Error>
    where
        W: Write,
    {
        out.write_all(&self.rsa_public_modulus).await?;
        out.write_all(&self.rsa_public_exponent).await?;
        out.write_all(&self.rsa_precalc_r).await?;
        out.write_all(&self.rsa_precalc_m).await?;

        Ok(())
    }

    /// Convert the Secure Boot V2 RSA public key to an RSA public key
    fn pub_key(&self) -> RsaPublicKey {
        RsaPublicKey::new(
            BigUint::from_bytes_le(&self.rsa_public_modulus),
            BigUint::from_bytes_le(&self.rsa_public_exponent),
        )
        .unwrap()
    }

    /// Fill the Sha256 hash of the public key into the provided hasher
    fn fill_hash(&self, hasher: &mut Sha256) {
        hasher.update(self.rsa_public_modulus);
        hasher.update(self.rsa_public_exponent);
        hasher.update(self.rsa_precalc_r);
        hasher.update(self.rsa_precalc_m);
    }

    /// Create a new empty Secure Boot V2 RSA public key in uninitialized state
    const fn new_empty() -> Self {
        Self {
            rsa_public_modulus: [0; 384],
            rsa_public_exponent: [0; 4],
            rsa_precalc_r: [0; 384],
            rsa_precalc_m: [0; 4],
        }
    }

    /// Fill (initialize) the Secure Boot V2 RSA public key with the given RSA public key
    fn fill(&mut self, pub_key: &RsaPublicKey) {
        // https://github.com/espressif/esptool/blob/6cc002c4bd0de6a5b1afa630a59c7cbaac44ab86/espsecure/__init__.py#L325
        let m = -pub_key
            .n()
            .mod_inverse(1.to_biguint().unwrap() << 32)
            .unwrap();

        // https://github.com/espressif/esptool/blob/6cc002c4bd0de6a5b1afa630a59c7cbaac44ab86/espsecure/__init__.py#L327
        let rr = 1.to_biguint().unwrap() << (pub_key.n().bits() * 2);
        let rinv = rr % pub_key.n();

        // https://github.com/espressif/esptool/blob/6cc002c4bd0de6a5b1afa630a59c7cbaac44ab86/espsecure/__init__.py#L1066
        self.rsa_public_modulus
            .copy_from_slice(&pub_key.n().to_bytes_le());
        self.rsa_public_exponent
            .copy_from_slice(&pub_key.e().to_i32().unwrap().to_le_bytes());
        self.rsa_precalc_r.copy_from_slice(&rinv.to_bytes_le());
        self.rsa_precalc_m
            .copy_from_slice(&((m.to_i64().unwrap() & 0xffffffff) as i32).to_le_bytes());
    }
}

/// ESP Secure Boot V2 RSA Signature Block
/// https://docs.espressif.com/projects/esp-idf/en/stable/esp32/security/secure-boot-v2.html#signature-block-format
///
/// This type is `repr(C)` and `repr(packed)` so that it can be directly transmuted from a memory-mapped
/// region of the ESP32 flash memory, if necessary. In other words, its memory layout represents exactly the
/// layout of a serialized ESP Secure Boot V2 RSA signature block.
///
/// Algorithms based on https://github.com/espressif/esptool/blob/master/espsecure
///
/// Note 1: The rest of the 4K page/sector containing the signature should be filled with 0xFF.
/// Note 2: App images SHOULD be padded to 64K (!) boundary in advance, so the signature block is the
///         first 4K block following the 64K aligned and padded app image
///         ESP IDF bootloader images need to only be padded to 4K boundary and this is done by `espsign` automatically
/// Note 3: The partition table - at least with Secure Boot V2 - seems unsigned
#[repr(C)]
#[repr(packed)]
pub struct SBV2RsaSignatureBlock {
    /// Magic byte. Always 0xe7
    magic: u8,
    /// Version number byte. Always 0x02
    version: u8,
    /// Padding bytes. Reserved. Should be zero.
    padding: [u8; 2],
    /// SHA-256 hash of only the image content, not including the signature block.
    sha256: [u8; 32],
    /// RSA public key
    rsa_pub_key: SBV2RsaPubKey,
    /// RSA-PSS Signature result (section 8.1.1 of RFC8017) of image content,
    /// computed using following PSS parameters:
    /// SHA256 hash, MGF1 function, salt length 32 bytes, default trailer field (0xBC).
    ///
    /// NOTE: LE order instead of (regular) BE order, as the Esp RSA peripheral uses LE.
    rsa_pss_signature: [u8; 384],
    /// CRC32 of the preceding 1196 bytes. LE order.
    crc32: [u8; 4],
    /// Zero padding to length 1216 bytes.
    padding2: [u8; 16],
}

impl SBV2RsaSignatureBlock {
    /// The size of a SBV2 RSA signature block (padded with 0xFF to 4K)
    const PAGE_SIZE: usize = 4096;

    /// The magic byte for the signature block (at offset 0)
    const MAGIC_BYTE: u8 = 0xe7;

    /// The version byte for the signature block (at offset 1)
    const VERSION: u8 = 2;

    /// Sign an image and write the padded image and the signature block to the output
    ///
    /// # Arguments
    /// * `priv_key` - RSA private key to sign the image with
    /// * `rng` - Random number generator
    /// * `buf` - Buffer to use for reading the image.
    /// * `image` - Image to sign
    /// * `image_type` - Type of image to sign
    /// * `out` - Output to write the padded image and the signature block to
    pub async fn sign<C, R, W>(
        priv_key: &RsaPrivateKey,
        rng: &mut C,
        buf: &mut [u8],
        image: R,
        image_type: ImageType,
        mut out: W,
    ) -> Result<Self, SignError<R::Error>>
    where
        C: RngCore + CryptoRng,
        R: Read,
        W: Write,
        W::Error: Into<R::Error>,
    {
        let block = Self::create(priv_key, rng, buf, image, image_type, Some(&mut out)).await?;

        block
            .save(out, Some(buf))
            .await
            .map_err(Into::<R::Error>::into)
            .map_err(SignError::Io)?;

        Ok(block)
    }

    /// Load and verify an image and its signature block
    ///
    /// # Arguments
    /// * `buf` - Buffer to use for reading the image.
    ///   NOTE: The buffer should be bigger than 4096 bytes, or else this method would panic!
    /// * `image` - Image to verify
    /// * `image_type` - Type of image to verify
    pub async fn load_and_verify<R>(
        buf: &mut [u8],
        mut image: R,
        image_type: ImageType,
    ) -> Result<&Self, VerifyError<R::Error>>
    where
        R: Read,
    {
        if buf.len() <= Self::PAGE_SIZE {
            panic!("Buffer too small; should be > {}B", Self::PAGE_SIZE);
        }

        let mut size = 0;
        let mut offset = 0;

        let mut hasher = Sha256::new();

        loop {
            let read = image.read(&mut buf[offset..]).await.unwrap();

            offset += read;
            size += read;

            if buf.len() == offset || read == 0 {
                hasher.update(&buf[..offset - Self::PAGE_SIZE]);

                buf.copy_within(offset - Self::PAGE_SIZE.., 0);
                offset = Self::PAGE_SIZE;
            }

            if read == 0 {
                break;
            }
        }

        if offset != Self::PAGE_SIZE || (size - Self::PAGE_SIZE) % image_type.align() != 0 {
            Err(VerifyError::InvalidImageLen)?;
        }

        // Transmute the last page into the signature block to save memory
        // Possible, because we are `repr(C)` and `repr(packed)`
        let block = unsafe { (buf.as_ptr() as *const Self).as_ref() }.unwrap();
        // let mut block = Self::new_empty();
        // block
        //     .load(&mut &buf[..Self::PAGE_SIZE])
        //     .await
        //     .map_err(|e| e.map(|e| match e {}))?;

        let digest = hasher.finalize();

        block.verify_image_hash(digest.as_ref())?;

        Ok(block)
    }

    /// Create a new Secure Boot V2 RSA signature block from the provided RSA private key and image
    ///
    /// The creation of the signature block means the provided image will be signed.
    ///
    /// # Arguments
    /// * `priv_key` - RSA private key to sign the image with
    /// * `rng` - Random number generator
    /// * `buf` - Buffer to use for reading the image.
    /// * `image` - Image to sign
    /// * `image_type` - Type of image to sign
    /// * `out` - Optional output to write the padded image to
    pub async fn create<C, R, W>(
        priv_key: &RsaPrivateKey,
        rng: &mut C,
        buf: &mut [u8],
        image: R,
        image_type: ImageType,
        out: Option<W>,
    ) -> Result<Self, SignError<R::Error>>
    where
        C: RngCore + CryptoRng,
        R: Read,
        W: Write,
        W::Error: Into<R::Error>,
    {
        let mut block = Self::new_empty();

        block
            .fill(priv_key, rng, buf, image, out, image_type)
            .await?;

        Ok(block)
    }

    /// Load a Secure Boot V2 RSA signature block from the input
    ///
    /// # Arguments
    /// * `read` - Input to read the signature block from
    pub async fn load<R>(&mut self, mut read: R) -> Result<(), VerifyError<R::Error>>
    where
        R: Read,
    {
        self.clear();

        let mut bt = [0; 2];
        read.read_exact(&mut bt).await?;

        if bt[0] != Self::MAGIC_BYTE || bt[1] != Self::VERSION {
            Err(VerifyError::InvalidSignatureBlock)?;
        }

        self.magic = bt[0];
        self.version = bt[1];

        read.read_exact(&mut self.padding).await?;
        read.read_exact(&mut self.sha256).await?;
        self.rsa_pub_key.load(&mut read).await?;
        read.read_exact(&mut self.rsa_pss_signature).await?;
        read.read_exact(&mut self.crc32).await?;
        read.read_exact(&mut self.padding2).await?;

        if self.crc32 != self.crc32() {
            Err(VerifyError::InvalidSignatureBlock)?;
        }

        Ok(())
    }

    /// Save the Secure Boot V2 RSA signature block to the output
    ///
    /// # Arguments
    /// * `out` - Output to write the signature block to
    /// * `padded` - Whether to pad the output to 4K
    ///   In that case, the function needs a non-zero buffer to perform the padding
    pub async fn save<W>(&self, mut out: W, padded: Option<&mut [u8]>) -> Result<(), W::Error>
    where
        W: Write,
    {
        out.write_all(&[self.magic]).await?;
        out.write_all(&[self.version]).await?;
        out.write_all(&self.padding).await?;
        out.write_all(&self.sha256).await?;
        self.rsa_pub_key.save(&mut out).await?;
        out.write_all(&self.rsa_pss_signature).await?;
        out.write_all(&self.crc32).await?;
        out.write_all(&self.padding2).await?;

        if let Some(buf) = padded {
            let mut remainder = Self::PAGE_SIZE - core::mem::size_of_val(self);

            if remainder != 0 {
                buf.fill(0xff);

                while remainder > 0 {
                    let len = core::cmp::min(remainder, buf.len());
                    out.write_all(&buf[..len]).await?;
                    remainder -= len;
                }
            }
        }

        Ok(())
    }

    /// Save the Sha256 hash of the public key to the output
    ///
    /// The saved hash should then be burned into E-FUSE with the `espefuse` tool
    /// when Secure Boot V2 is being enabled for the Esp chip
    ///
    /// # Arguments
    /// * `out` - Output to write the hash to
    pub async fn save_pubkey_hash<W>(&self, out: W) -> Result<(), W::Error>
    where
        W: Write,
    {
        self.rsa_pub_key.save_hash(out).await
    }

    /// Verify an image assuming this signature block is for that image
    ///
    /// # Arguments
    /// * `buf` - Buffer to use for reading the image.
    /// * `image` - Image to verify
    /// * `image_type` - Type of image to verify
    pub async fn verify<R>(
        &self,
        buf: &mut [u8],
        image: R,
        image_type: ImageType,
    ) -> Result<(), VerifyError<R::Error>>
    where
        R: Read,
    {
        let mut hasher = Sha256::new();

        let aligned = Self::read_write_hash(
            &mut hasher,
            buf,
            image,
            Option::<NullWrite<R::Error>>::None,
            image_type,
            false, /*pad*/
        )
        .await
        .map_err(VerifyError::Io)?;

        if !aligned {
            Err(VerifyError::InvalidImageLen)?;
        }

        let digest = hasher.finalize();
        let hashed = digest.as_ref();

        self.verify_image_hash(hashed)?;

        Ok(())
    }

    /// Verify the Sha-256 hash of an image against this signature block
    ///
    /// Arguments
    /// * `hash` - Sha-256 hash to verify
    pub fn verify_image_hash<E>(&self, hash: &[u8]) -> Result<(), VerifyError<E>> {
        if hash != self.sha256 {
            Err(VerifyError::InvalidHash)?;
        }

        let pub_key = self.rsa_pub_key.pub_key();
        let pss = Pss::new_with_salt::<Sha256>(32);

        let mut signature = [0; 384];
        signature.copy_from_slice(&self.rsa_pss_signature);
        signature.reverse(); // To BE

        pub_key
            .verify(pss, hash, &signature)
            .map_err(|_| VerifyError::InvalidSignature)?;

        Ok(())
    }

    /// Create an empty Secure Boot V2 RSA signature block in uninitialized state
    const fn new_empty() -> Self {
        Self {
            magic: Self::MAGIC_BYTE,
            version: Self::VERSION,
            padding: [0; 2],
            sha256: [0; 32],
            rsa_pub_key: SBV2RsaPubKey::new_empty(),
            rsa_pss_signature: [0; 384],
            crc32: [0; 4],
            padding2: [0; 16],
        }
    }

    /// Fill the Secure Boot V2 RSA signature block with the provided RSA private key and image
    /// essentially signing the image this way.
    ///
    /// # Arguments
    /// * `priv_key` - RSA private key to sign the image with
    /// * `rng` - Random number generator
    /// * `buf` - Buffer to use for reading the image.
    /// * `image` - Image to sign
    /// * `out` - Optional output to write the padded image to
    /// * `align` - Alignment for the output image
    async fn fill<C, R, W>(
        &mut self,
        priv_key: &RsaPrivateKey,
        rng: &mut C,
        buf: &mut [u8],
        image: R,
        out: Option<W>,
        image_type: ImageType,
    ) -> Result<(), SignError<R::Error>>
    where
        C: RngCore + CryptoRng,
        R: Read,
        W: Write,
        W::Error: Into<R::Error>,
    {
        self.clear();
        self.fill_pub_key(&priv_key.to_public_key());
        self.fill_hash(buf, image, out, image_type, true).await?;
        self.fill_signature(rng, priv_key);
        self.fill_crc32();

        Ok(())
    }

    /// Reset the Secure Boot V2 RSA signature block to its initial uninitialized state
    fn clear(&mut self) {
        *self = Self::new_empty();
    }

    /// Fill the Secure Boot V2 RSA public key fuilds with the provided RSA public key
    fn fill_pub_key(&mut self, pub_key: &RsaPublicKey) {
        self.rsa_pub_key.fill(pub_key);
    }

    /// Fill the Secure Boot V2 RSA-PSS signature field with the signature of the Sha256 hash
    fn fill_signature<C: RngCore + CryptoRng>(&mut self, rng: &mut C, priv_key: &RsaPrivateKey) {
        let pss = Pss::new_with_salt::<Sha256>(32);

        let signature: Box<[u8]> = priv_key
            .sign_with_rng(rng, pss, &self.sha256)
            .unwrap()
            .into();

        self.rsa_pss_signature.copy_from_slice(signature.as_ref());
        self.rsa_pss_signature.reverse(); // To LE
    }

    /// Fill the Sha256 hash of the image into the Secure Boot V2 RSA signature block
    async fn fill_hash<R, W>(
        &mut self,
        buf: &mut [u8],
        image: R,
        out: Option<W>,
        image_type: ImageType,
        pad: bool,
    ) -> Result<(), SignError<R::Error>>
    where
        R: Read,
        W: Write,
        W::Error: Into<R::Error>,
    {
        let mut hasher = Sha256::new();

        if !Self::read_write_hash(&mut hasher, buf, image, out, image_type, pad)
            .await
            .map_err(SignError::Io)?
        {
            Err(SignError::InvalidImageLen)?; // Image size is not a multiple of align
        }

        self.sha256.copy_from_slice(hasher.finalize().as_ref());

        Ok(())
    }

    /// Fill the CRC32 field of the Secure Boot V2 RSA signature block
    fn fill_crc32(&mut self) {
        let crc32 = self.crc32();

        self.crc32.copy_from_slice(&crc32);
    }

    /// Calculate the CRC32 of the Secure Boot V2 RSA signature block
    fn crc32(&self) -> [u8; 4] {
        // CRC-32/ISO-HDLC
        const CRC32: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);

        let mut crc = CRC32.digest();
        crc.update(&[self.magic]);
        crc.update(&[self.version]);
        crc.update(&self.padding);
        crc.update(&self.sha256);
        crc.update(&self.rsa_pub_key.rsa_public_modulus);
        crc.update(&self.rsa_pub_key.rsa_public_exponent);
        crc.update(&self.rsa_pub_key.rsa_precalc_r);
        crc.update(&self.rsa_pub_key.rsa_precalc_m);
        crc.update(&self.rsa_pss_signature);

        let checksum = crc.finalize();

        checksum.to_le_bytes()
    }

    /// Read and write and image to the hasher and to the optional output
    async fn read_write_hash<R, W>(
        hasher: &mut Sha256,
        buf: &mut [u8],
        mut image: R,
        mut out: Option<W>,
        image_type: ImageType,
        pad: bool,
    ) -> Result<bool, R::Error>
    where
        R: Read,
        W: Write,
        W::Error: Into<R::Error>,
    {
        let mut size: usize = 0;

        loop {
            let read = image.read(buf).await?;
            if read == 0 {
                break;
            }

            Self::write_hash(hasher, &buf[..read], out.as_mut())
                .await
                .map_err(Into::into)?;

            size += read;
        }

        let align = image_type.align();
        let mut remainder = align - size % align;

        if remainder != align {
            if pad {
                if matches!(image_type, ImageType::App) {
                    error!("App image size ({size}B) is not a multiple of {align}B. Padding requested, but can't pad App images.");
                    Ok(false)
                } else {
                    info!("Bootloader image size ({size}B) is not a multiple of {align}B. Padding {remainder}B with 0xFF");

                    buf.fill(0xff);

                    while remainder > 0 {
                        let to_write = remainder.min(buf.len());

                        Self::write_hash(hasher, &buf[..to_write], out.as_mut())
                            .await
                            .map_err(Into::into)?;

                        remainder -= to_write;
                    }

                    Ok(true)
                }
            } else {
                Ok(false)
            }
        } else {
            Ok(true)
        }
    }

    /// Write data to the hasher and to the optional output
    async fn write_hash<W>(
        hasher: &mut Sha256,
        data: &[u8],
        mut out: Option<W>,
    ) -> Result<(), W::Error>
    where
        W: Write,
    {
        if let Some(write) = out.as_mut() {
            write.write_all(data).await.map_err(Into::into)?;
        }

        hasher.update(data);

        Ok(())
    }
}

#[cfg(feature = "std")]
mod std {
    use std::io;

    use embedded_io_async::{ErrorType, Read, Write};

    extern crate std;

    /// A blocking wrapper for types implementing `std::io::Read` and `std::io::Write` to implement `Read` and `Write` for async I/O.
    pub struct AsyncIo<T>(T);

    impl<T> AsyncIo<T> {
        /// Create a new `FileAsyncIo` from the inner type.
        pub const fn new(inner: T) -> Self {
            Self(inner)
        }
    }

    impl<T> ErrorType for AsyncIo<T> {
        type Error = io::Error;
    }

    impl<T> Read for AsyncIo<T>
    where
        T: io::Read,
    {
        async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
            self.0.read(buf)
        }
    }

    impl<T> Write for AsyncIo<T>
    where
        T: io::Write,
    {
        async fn write(&mut self, data: &[u8]) -> Result<usize, Self::Error> {
            self.0.write(data)
        }
    }
}

#[cfg(all(test, feature = "pks"))]
mod test {
    use core::convert::Infallible;

    use alloc::vec::Vec;

    use embedded_io_async::{ErrorType, Write};

    use super::ImageType;

    use rand_core::{CryptoRng, RngCore};

    use rsa::pkcs8::DecodePrivateKey;

    extern crate alloc;

    static PRIV_KEY: &str = r#"
-----BEGIN PRIVATE KEY-----
MIIG/gIBADANBgkqhkiG9w0BAQEFAASCBugwggbkAgEAAoIBgQDBBSES+nnQWeNg
hCSpGcORxXbK5G7bXsalqLy/edCa7snBsqVyq+qUqM+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/A+yu
xjKaJuYoju9e7ZQmjpY7FD7QgCG9zW2jcrBpTqMPL58IHioLszBvj3t8QASfMwta
PJawyWpGY6fVrQzlc56r/fCGXAyVyK79qb3A50yPIBpJAF3EGXVeY235jJAnT7mQ
HLi+/wQ5736LzHUphwOfBDZZ
-----END PRIVATE KEY-----
"#;

    static IMAGE: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

    struct Rng(u8);

    impl RngCore for Rng {
        fn next_u32(&mut self) -> u32 {
            let mut result = [0; 4];
            self.fill_bytes(&mut result);

            u32::from_le_bytes(result)
        }

        fn next_u64(&mut self) -> u64 {
            let mut result = [0; 8];
            self.fill_bytes(&mut result);

            u64::from_le_bytes(result)
        }

        fn fill_bytes(&mut self, dest: &mut [u8]) {
            for i in dest {
                *i = self.0;
                self.0 += 1;
            }
        }

        fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
            self.fill_bytes(dest);
            Ok(())
        }
    }

    impl CryptoRng for Rng {}

    struct AsyncIo<T>(T);

    impl<T> ErrorType for AsyncIo<T> {
        type Error = Infallible;
    }

    impl Write for AsyncIo<&mut Vec<u8>> {
        async fn write(&mut self, data: &[u8]) -> Result<usize, Self::Error> {
            self.0.extend_from_slice(data);

            Ok(data.len())
        }
    }

    /// A test that signs and then verifies an image
    #[test]
    fn test() {
        let priv_key = super::rsa::RsaPrivateKey::from_pkcs8_pem(PRIV_KEY).unwrap();

        let mut buf = [0; 5000];

        let mut out = Vec::new();
        let mut sha = Vec::new();

        let mut rng = Rng(0);

        embassy_futures::block_on(async {
            let signature = super::SBV2RsaSignatureBlock::sign(
                &priv_key,
                &mut rng,
                &mut buf,
                IMAGE,
                ImageType::Bootloader,
                AsyncIo(&mut out),
            )
            .await
            .unwrap();

            signature.save_pubkey_hash(AsyncIo(&mut sha)).await.unwrap();
            assert_eq!(
                &sha,
                &[
                    66, 75, 181, 110, 45, 200, 254, 51, 193, 128, 186, 133, 116, 47, 246, 223, 131,
                    205, 201, 187, 29, 16, 233, 45, 4, 113, 121, 145, 220, 211, 91, 77
                ]
            );

            super::SBV2RsaSignatureBlock::load_and_verify(
                &mut buf,
                &mut out.as_ref(),
                ImageType::Bootloader,
            )
            .await
            .unwrap();
        });
    }
}