sev 7.1.0

Library for AMD SEV
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
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
// SPDX-License-Identifier: Apache-2.0

#[cfg(feature = "openssl")]
use openssl::error::ErrorStack;
use std::{
    array::TryFromSliceError,
    convert::From,
    error,
    fmt::{Debug, Display},
    io,
};

#[cfg(feature = "openssl")]
use rdrand::ErrorCode;

use std::os::raw::c_int;

#[cfg(feature = "openssl")]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Used for representing known errors when handling snp::Certificates.
pub enum CertFormatError {
    /// Unknown certificate format identified.
    UnknownFormat,
}

#[cfg(feature = "openssl")]
impl std::error::Error for CertFormatError {}

#[cfg(feature = "openssl")]
impl std::fmt::Display for CertFormatError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::UnknownFormat => write!(f, "Unknown Certificate Format Encountered."),
        }
    }
}

/// An error representingthe upper 32 bits of a SW_EXITINFO2 field set by the VMM.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum VmmError {
    /// If there are not enough guest pages to hold the certificate table
    /// and certificate data, the hypervisor will return the required number
    /// of pages needed to hold the certificate table and certificate data
    /// in the RBX register and set the SW_EXITINFO2 field to
    /// 0x0000000100000000.
    InvalidCertificatePageLength = 0x1, // Upper 32 bits.

    /// It is not expected that a guest would issue many Guest Request NAE
    /// events. However, access to the SNP firmware is a sequential and
    /// synchronous operation. To avoid the possibility of a guest creating a
    /// denial-of-service attack against the SNP firmware, it is recommended
    /// that some form of rate limiting be implemented should it be detected
    /// that a high number of Guest Request NAE events are being issued. To
    /// allow for this, the hypervisor may set the SW_EXITINFO2 field to
    /// 0x0000000200000000, which will inform the guest to retry the request.
    RateLimitRetryRequest = 0x2, // Upper 32 bits

    /// Nothing more implemented yet.
    Unknown,
}

/// Use the default implementations for std::error::Error here.
impl error::Error for VmmError {}

impl From<u32> for VmmError {
    /// Takes a raw u32 and translates it into the correlated [VmmError](self::VmmError)
    /// type.
    ///
    /// * value - The raw u32 value which we would like to interpret.
    ///
    /// # Example
    ///
    /// ```
    /// use sev::error::VmmError;
    /// let raw_value: u32 = 0x1u32;
    /// let outcome: VmmError = VmmError::from(raw_value);
    /// assert_eq!(outcome, VmmError::InvalidCertificatePageLength);
    /// ```
    fn from(value: u32) -> Self {
        match value {
            0x1 => VmmError::InvalidCertificatePageLength,
            0x2 => VmmError::RateLimitRetryRequest,
            _ => VmmError::Unknown,
        }
    }
}

impl From<u64> for VmmError {
    /// Takes a raw u64 and translates it into the correlated [VmmError](self::VmmError)
    /// type.
    ///
    /// * value - The raw u64 value which we would like to interpret.
    ///
    /// # Example
    ///
    /// ```
    /// use sev::error::VmmError;
    /// let raw_value: u64 = 0x100000000;
    /// let outcome: VmmError = VmmError::from(raw_value);
    /// assert_eq!(outcome, VmmError::InvalidCertificatePageLength);
    /// ```
    fn from(value: u64) -> Self {
        ((value >> 0x20) as u32).into()
    }
}

impl Display for VmmError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            VmmError::InvalidCertificatePageLength => write!(f, "An invalid number of pages was provided to copy the certificate table and certificate data to userspace."),
            VmmError::RateLimitRetryRequest => write!(f, "The AMD Secure Processor detected a possible denial-of-service. Please retry your request."),
            VmmError::Unknown => write!(f, "An unknown VMM error was encountered!"),
        }
    }
}

/// The raw firmware error.
#[derive(Debug)]
pub(crate) struct RawFwError(pub(crate) u64);

impl std::error::Error for RawFwError {}

impl From<u64> for RawFwError {
    fn from(value: u64) -> Self {
        Self(value)
    }
}

impl PartialEq for RawFwError {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl From<RawFwError> for (u32, u32) {
    fn from(value: RawFwError) -> Self {
        ((value.0 >> 0x20) as u32, value.0 as u32)
    }
}

impl From<RawFwError> for (VmmError, SevError) {
    fn from(value: RawFwError) -> Self {
        (((value.0 >> 0x20) as u32).into(), (value.0 as u32).into())
    }
}

impl Display for RawFwError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "RawFwError: {}", self.0)
    }
}

/// Error conditions returned by the SEV platform or by layers above it
/// (i.e., the Linux kernel).
///
/// These error conditions are documented in the AMD SEV API spec, but
/// their documentation has been copied here for completeness.
#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u32)]
pub enum SevError {
    /// The platform state is invalid for this command.
    InvalidPlatformState = 0x0001,

    /// The guest state is invalid for this command.
    InvalidGuestState = 0x0002,

    /// The platform configuration is invalid.
    InvalidConfig = 0x0003,

    /// A memory buffer is too small.
    InvalidLen = 0x0004,

    /// The platform is already owned.
    AlreadyOwned = 0x0005,

    /// The certificate is invalid.
    InvalidCertificate = 0x0006,

    /// Request is not allowed by guest policy.
    PolicyFailure = 0x0007,

    /// The guest is inactive.
    Inactive = 0x0008,

    /// The address provided is invalid.
    InvalidAddress = 0x0009,

    /// The provided signature is invalid.
    BadSignature = 0x000A,

    /// The provided measurement is invalid.
    BadMeasurement = 0x000B,

    /// The ASID is already owned.
    AsidOwned = 0x000C,

    /// The ASID is invalid.
    InvalidAsid = 0x000D,

    /// WBINVD instruction required.
    WbinvdRequired = 0x000E,

    /// `DF_FLUSH` invocation required.
    DfFlushRequired = 0x000F,

    /// The guest handle is invalid.
    InvalidGuest = 0x0010,

    /// The command issued is invalid.
    InvalidCommand = 0x0011,

    /// The guest is active.
    Active = 0x0012,

    /// A hardware condition has occurred affecting the platform. It is safe
    /// to re-allocate parameter buffers.
    HardwarePlatform = 0x0013,

    /// A hardware condition has occurred affecting the platform. Re-allocating
    /// parameter buffers is not safe.
    HardwareUnsafe = 0x0014,

    /// Feature is unsupported.
    Unsupported = 0x0015,

    /// A given parameter is invalid.
    InvalidParam = 0x0016,

    /// The SEV firmware has run out of a resource required to carry out the
    /// command.
    ResourceLimit = 0x0017,

    /// The SEV platform observed a failed integrity check.
    SecureDataInvalid = 0x0018,

    /// The RMP page size is incorrect.
    InvalidPageSize = 0x0019,

    /// The RMP page state is incorrect
    InvalidPageState = 0x001A,

    /// The metadata entry is invalid.
    InvalidMdataEntry = 0x001B,

    /// The page ownership is incorrect
    InvalidPageOwner = 0x001C,

    /// The AEAD algorithm would have overflowed
    AEADOFlow = 0x001D,

    /// A Mailbox mode command was sent while the SEV FW was in Ring Buffer
    /// mode. Ring Buffer mode has been exited; the Mailbox mode command
    /// has been ignored. Retry is recommended.
    RbModeExited = 0x001F, // 0x001F

    /// The RMP must be reinitialized.
    RMPInitRequired = 0x0020, // 0x0020

    /// SVN of provided image is lower than the committed SVN.
    BadSvn = 0x0021,

    /// Firmware version anti-rollback.
    BadVersion = 0x0022,

    /// An invocation of SNP_SHUTDOWN is required to complete this action.
    ShutdownRequired = 0x0023,

    /// Update of the firmware internal state or a guest context page has failed.
    UpdateFailed = 0x0024,

    /// Installation of the committed firmware image required
    RestoreRequired = 0x0025,

    /// The RMP initialization failed.
    RMPInitFailed = 0x0026,

    /// The key requested is invalid, not present, or not allowed.
    InvalidKey = 0x0027,

    /// Unknown status code
    UnknownError = 0x0000,
}

impl std::fmt::Display for SevError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let code = *self as u32;
        match self {
            SevError::InvalidPlatformState => write!(f,"Status Code: 0x{:x}: Invalid platform state.", code),
            SevError::InvalidGuestState => write!(f,"Status Code: 0x{:x}: Invalid guest state.", code),
            SevError::InvalidConfig => write!(f,"Status Code: 0x{:x}: Platform configuration invalid.", code),
            SevError::InvalidLen => write!(f,"Status Code: 0x{:x}: Memory buffer too small.", code),
            SevError::AlreadyOwned => write!(f,"Status Code: 0x{:x}: Platform is already owned.", code),
            SevError::InvalidCertificate => write!(f,"Status Code: 0x{:x}: Invalid certificate.", code),
            SevError::PolicyFailure => write!(f,"Status Code: 0x{:x}: Policy failure.", code),
            SevError::Inactive => write!(f,"Status Code: 0x{:x}: Guest is inactive.", code),
            SevError::InvalidAddress => write!(f,"Status Code: 0x{:x}: Provided address is invalid.", code),
            SevError::BadSignature => write!(f,"Status Code: 0x{:x}: Provided signature is invalid.", code),
            SevError::BadMeasurement => write!(f,"Status Code: 0x{:x}: Provided measurement is invalid.", code),
            SevError::AsidOwned => write!(f,"Status Code: 0x{:x}: ASID is already owned.", code),
            SevError::InvalidAsid => write!(f,"Status Code: 0x{:x}: ASID is invalid.", code),
            SevError::WbinvdRequired => write!(f,"Status Code: 0x{:x}: WBINVD instruction required.", code),
            SevError::DfFlushRequired => write!(f,"Status Code: 0x{:x}: DF_FLUSH invocation required.", code),
            SevError::InvalidGuest => write!(f,"Status Code: 0x{:x}: Guest handle is invalid.", code),
            SevError::InvalidCommand => write!(f,"Status Code: 0x{:x}: Issued command is invalid.", code),
            SevError::Active => write!(f,"Status Code: 0x{:x}: Guest is active.", code),
            SevError::HardwarePlatform => {
                write!(f,"Status Code: 0x{:x}: Hardware condition occured, safe to re-allocate parameter buffers.", code)
            }
            SevError::HardwareUnsafe => {
                write!(f,"Status Code: 0x{:x}: Hardware condition occured, unsafe to re-allocate parameter buffers.", code)
            }
            SevError::Unsupported => write!(f,"Status Code: 0x{:x}: Feature is unsupported.", code),
            SevError::InvalidParam => write!(f,"Status Code: 0x{:x}: Given parameter is invalid.", code),
            SevError::ResourceLimit => {
                write!(f,"Status Code: 0x{:x}: SEV firmware has run out of required resources to carry out command.", code)
            }
            SevError::SecureDataInvalid => write!(f,"Status Code: 0x{:x}: SEV platform observed a failed integrity check.", code),
            SevError::InvalidPageSize => write!(f,"Status Code: 0x{:x}: The RMP page size is incorrect.", code),
            SevError::InvalidPageState => write!(f,"Status Code: 0x{:x}: The RMP page state is incorrect.", code),
            SevError::InvalidMdataEntry => write!(f,"Status Code: 0x{:x}: The metadata entry is invalid.", code),
            SevError::InvalidPageOwner => write!(f,"Status Code: 0x{:x}: The page ownership is incorrect.", code),
            SevError::AEADOFlow => write!(f,"Status Code: 0x{:x}: The AEAD algorithm would have overflowed.", code),
            SevError::RbModeExited => write!(f,"Status Code: 0x{:x}: A Mailbox mode command was sent while the SEV FW was in Ring Buffer \
                                    mode. Ring Buffer mode has been exited; the Mailbox mode command has \
                                    been ignored. Retry is recommended.", code),
            SevError::RMPInitRequired => write!(f,"Status Code: 0x{:x}: The RMP must be reinitialized.", code),
            SevError::BadSvn => write!(f,"Status Code: 0x{:x}: SVN of provided image is lower than the committed SVN.", code),
            SevError::BadVersion => write!(f,"Status Code: 0x{:x}: Firmware version anti-rollback.", code),
            SevError::ShutdownRequired => write!(f,"Status Code: 0x{:x}: An invocation of SNP_SHUTDOWN is required to complete this action.", code),
            SevError::UpdateFailed => write!(f,"Status Code: 0x{:x}: Update of the firmware internal state or a guest context page has failed.", code),
            SevError::RestoreRequired => write!(f,"Status Code: 0x{:x}: Installation of the committed firmware image required.", code),
            SevError::RMPInitFailed => write!(f,"Status Code: 0x{:x}: The RMP initialization failed.", code),
            SevError::InvalidKey => write!(f,"Status Code: 0x{:x}: The key requested is invalid, not present, or not allowed.", code),
            SevError::UnknownError => write!(f,"Unknown SEV Error"),
        }
    }
}

impl From<u64> for SevError {
    fn from(value: u64) -> Self {
        Self::from(value as u32)
    }
}

impl From<u32> for SevError {
    #[inline]
    fn from(error: u32) -> SevError {
        match error {
            0x01 => SevError::InvalidPlatformState,
            0x02 => SevError::InvalidGuestState,
            0x03 => SevError::InvalidConfig,
            0x04 => SevError::InvalidLen,
            0x05 => SevError::AlreadyOwned,
            0x06 => SevError::InvalidCertificate,
            0x07 => SevError::PolicyFailure,
            0x08 => SevError::Inactive,
            0x09 => SevError::InvalidAddress,
            0x0A => SevError::BadSignature,
            0x0B => SevError::BadMeasurement,
            0x0C => SevError::AsidOwned,
            0x0D => SevError::InvalidAsid,
            0x0E => SevError::WbinvdRequired,
            0x0F => SevError::DfFlushRequired,
            0x10 => SevError::InvalidGuest,
            0x11 => SevError::InvalidCommand,
            0x12 => SevError::Active,
            0x13 => SevError::HardwarePlatform,
            0x14 => SevError::HardwareUnsafe,
            0x15 => SevError::Unsupported,
            0x16 => SevError::InvalidParam,
            0x17 => SevError::ResourceLimit,
            0x18 => SevError::SecureDataInvalid,
            0x19 => SevError::InvalidPageSize,
            0x1A => SevError::InvalidPageState,
            0x1B => SevError::InvalidMdataEntry,
            0x1C => SevError::InvalidPageOwner,
            0x1D => SevError::AEADOFlow,
            0x1F => SevError::RbModeExited,
            0x20 => SevError::RMPInitRequired,
            0x21 => SevError::BadSvn,
            0x22 => SevError::BadVersion,
            0x23 => SevError::ShutdownRequired,
            0x24 => SevError::UpdateFailed,
            0x25 => SevError::RestoreRequired,
            0x26 => SevError::RMPInitFailed,
            0x27 => SevError::InvalidKey,
            _ => SevError::UnknownError,
        }
    }
}

impl From<SevError> for c_int {
    fn from(err: SevError) -> Self {
        match err {
            SevError::InvalidPlatformState => 0x01,
            SevError::InvalidGuestState => 0x02,
            SevError::InvalidConfig => 0x03,
            SevError::InvalidLen => 0x04,
            SevError::AlreadyOwned => 0x05,
            SevError::InvalidCertificate => 0x06,
            SevError::PolicyFailure => 0x07,
            SevError::Inactive => 0x08,
            SevError::InvalidAddress => 0x09,
            SevError::BadSignature => 0x0A,
            SevError::BadMeasurement => 0x0B,
            SevError::AsidOwned => 0x0C,
            SevError::InvalidAsid => 0x0D,
            SevError::WbinvdRequired => 0x0E,
            SevError::DfFlushRequired => 0x0F,
            SevError::InvalidGuest => 0x10,
            SevError::InvalidCommand => 0x11,
            SevError::Active => 0x12,
            SevError::HardwarePlatform => 0x13,
            SevError::HardwareUnsafe => 0x14,
            SevError::Unsupported => 0x15,
            SevError::InvalidParam => 0x16,
            SevError::ResourceLimit => 0x17,
            SevError::SecureDataInvalid => 0x18,
            SevError::InvalidPageSize => 0x19,
            SevError::InvalidPageState => 0x1A,
            SevError::InvalidMdataEntry => 0x1B,
            SevError::InvalidPageOwner => 0x1C,
            SevError::AEADOFlow => 0x1D,
            SevError::RbModeExited => 0x1F,
            SevError::RMPInitRequired => 0x20,
            SevError::BadSvn => 0x21,
            SevError::BadVersion => 0x22,
            SevError::ShutdownRequired => 0x23,
            SevError::UpdateFailed => 0x24,
            SevError::RestoreRequired => 0x25,
            SevError::RMPInitFailed => 0x26,
            SevError::InvalidKey => 0x27,
            SevError::UnknownError => -1,
        }
    }
}

impl std::error::Error for SevError {}

/// There are a number of error conditions that can occur between this
/// layer all the way down to the SEV platform. Most of these cases have
/// been enumerated; however, there is a possibility that some error
/// conditions are not encapsulated here.
#[derive(Debug)]
pub enum FirmwareError {
    /// The error condition is known.
    KnownSevError(SevError),

    /// The error condition is unknown.
    UnknownSevError(u32),

    /// IO Error
    IoError(std::io::Error),
}

impl error::Error for FirmwareError {}

impl Display for FirmwareError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let err_description = match self {
            FirmwareError::KnownSevError(error) => format!("Known SEV FW Error: {error}"),
            FirmwareError::UnknownSevError(code) => {
                format!("Unknown SEV FW Error Encountered: {code}")
            }
            FirmwareError::IoError(error) => format!("IO Error Encountered: {error}"),
        };

        write!(f, "{err_description}")
    }
}

impl std::convert::From<SevError> for FirmwareError {
    fn from(sev_error: SevError) -> Self {
        match sev_error {
            SevError::UnknownError => FirmwareError::UnknownSevError(sev_error as u32),
            _ => FirmwareError::KnownSevError(sev_error),
        }
    }
}

impl From<io::Error> for FirmwareError {
    #[inline]
    fn from(error: io::Error) -> FirmwareError {
        FirmwareError::IoError(error)
    }
}

impl From<u64> for FirmwareError {
    fn from(value: u64) -> Self {
        Self::from(value as u32)
    }
}

impl From<u32> for FirmwareError {
    #[inline]
    fn from(error: u32) -> FirmwareError {
        match error {
            0x00 => FirmwareError::IoError(io::Error::last_os_error()),
            0x01..0x027 => FirmwareError::KnownSevError(error.into()),
            _ => FirmwareError::UnknownSevError(error),
        }
    }
}

impl From<FirmwareError> for c_int {
    fn from(err: FirmwareError) -> Self {
        match err {
            FirmwareError::UnknownSevError(_) | FirmwareError::IoError(_) => -0x01,
            FirmwareError::KnownSevError(e) => e.into(),
        }
    }
}

#[derive(Debug)]
/// Wrapper Error for Firmware or User API Errors
pub enum UserApiError {
    /// Sev Firmware related errors.
    FirmwareError(FirmwareError),

    /// IO related errors
    IOError(io::Error),

    /// User API related errors.
    ApiError(CertError),

    /// Errors returned by the VMM via ioctl().
    VmmError(VmmError),

    /// Uuid parsing errors.
    UuidError(uuid::Error),

    /// VLEK Hashstick errors.
    HashstickError(HashstickError),

    /// Invalid VMPL.
    VmplError,

    /// Attestation Report Error
    AttestationReportError(AttestationReportError),

    /// Unknown error
    Unknown,
}

impl error::Error for UserApiError {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Self::ApiError(uapi_error) => Some(uapi_error),
            Self::FirmwareError(firmware_error) => Some(firmware_error),
            Self::IOError(io_error) => Some(io_error),
            Self::UuidError(uuid_error) => Some(uuid_error),
            Self::VmmError(vmm_error) => Some(vmm_error),
            Self::HashstickError(hashstick_error) => Some(hashstick_error),
            Self::VmplError => None,
            Self::AttestationReportError(attestation_error) => Some(attestation_error),
            Self::Unknown => None,
        }
    }
}

impl std::fmt::Display for UserApiError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let err_msg: String = match self {
            Self::FirmwareError(error) => format!("Firmware Error Encountered: {error}"),
            Self::IOError(error) => format!("I/O Error Encountered: {error}"),
            Self::ApiError(error) => format!("Certificate Error Encountered: {error}"),
            Self::UuidError(error) => format!("UUID Error Encountered: {error}"),
            Self::VmmError(error) => format!("VMM Error Encountered: {error}"),
            Self::HashstickError(error) => format!("VLEK Hashstick Error Encountered: {error}"),
            Self::VmplError => "Invalid VM Permission Level (VMPL)".to_string(),
            Self::AttestationReportError(error) => {
                format!("Attestation Report Error Encountered: {error}")
            }
            Self::Unknown => "Unknown Error Encountered!".to_string(),
        };
        write!(f, "{err_msg}")
    }
}

impl std::convert::From<HashstickError> for UserApiError {
    fn from(value: HashstickError) -> Self {
        Self::HashstickError(value)
    }
}

impl std::convert::From<VmmError> for UserApiError {
    fn from(value: VmmError) -> Self {
        Self::VmmError(value)
    }
}

impl std::convert::From<uuid::Error> for UserApiError {
    fn from(uuid_error: uuid::Error) -> Self {
        Self::UuidError(uuid_error)
    }
}

impl std::convert::From<FirmwareError> for UserApiError {
    fn from(firmware_error: FirmwareError) -> Self {
        Self::FirmwareError(firmware_error)
    }
}

impl std::convert::From<SevError> for UserApiError {
    fn from(sev_error: SevError) -> Self {
        Self::FirmwareError(sev_error.into())
    }
}

impl std::convert::From<std::io::Error> for UserApiError {
    fn from(io_error: std::io::Error) -> Self {
        Self::IOError(io_error)
    }
}

impl From<UserApiError> for io::Error {
    fn from(value: UserApiError) -> Self {
        io::Error::new(io::ErrorKind::Other, value)
    }
}

impl std::convert::From<CertError> for UserApiError {
    fn from(cert_error: CertError) -> Self {
        Self::ApiError(cert_error)
    }
}

impl std::convert::From<AttestationReportError> for UserApiError {
    fn from(attestation_error: AttestationReportError) -> Self {
        Self::AttestationReportError(attestation_error)
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
/// Errors which may be encountered when handling Version Loaded Endorsement Keys
/// (VLEK) Hashsticks.
pub enum HashstickError {
    /// Hashstick length does not match what was specified in the buffer.
    InvalidLength,

    /// No hashstick was provided
    EmptyHashstickBuffer,

    /// Invalid reserved field in the hashstick.
    InvalidReservedField,

    /// Unknown Error.
    UnknownError,
}

impl std::error::Error for HashstickError {}

impl std::fmt::Display for HashstickError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HashstickError::InvalidLength => {
                write!(
                    f,
                    "VLEK hashstick is an invalid length. Should be 432 bytes in size."
                )
            }
            HashstickError::EmptyHashstickBuffer => write!(f, "Hashstick buffer is empty."),
            HashstickError::InvalidReservedField => {
                write!(f, "Reserved field in the VLEK Hashstick is invalid.")
            }
            HashstickError::UnknownError => {
                write!(
                    f,
                    "Unknown error encountered when handling the VLEK Hashstick."
                )
            }
        }
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
/// Errors which may be encountered through misuse of the User API.
pub enum CertError {
    /// Malformed GUID.
    InvalidGUID,

    /// Malformed Page Alignment
    PageMisalignment,

    /// Invalid Buffer Size
    BufferOverflow,

    /// No certificates were set by the Host.
    EmptyCertBuffer,

    /// Unknown Error.
    UnknownError,
}

impl std::fmt::Display for CertError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            CertError::InvalidGUID => write!(f, "Invalid GUID provided in certificate chain."),
            CertError::PageMisalignment => {
                write!(f, "Certificate Buffer not aligned with 4K Pages.")
            }
            CertError::BufferOverflow => {
                write!(f, "Buffer overflow prevented: Bytes provided exceed space allocated for the buffer provided.")
            }
            CertError::UnknownError => {
                write!(f, "Unknown Error encountered within the certificate chain.")
            }
            CertError::EmptyCertBuffer => {
                write!(
                    f,
                    "No certificates were provided by the host, please contact your CSP."
                )
            }
        }
    }
}

impl error::Error for CertError {}

#[derive(Debug)]
/// Errors which may be encountered when handling attestation reports
pub enum AttestationReportError {
    /// Unsuported Attestation Report Version
    UnsupportedReportVersion(u32),

    /// Field is not supported in the current version of the Attestation Report
    UnsupportedField(String),

    /// MASK_CHIP_ID enabled
    MaskedChipId,
}

impl std::fmt::Display for AttestationReportError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            AttestationReportError::MaskedChipId => write!(f, "MASK_CHIP_ID is enabled, preventing the identification of the CPU generation."),
            AttestationReportError::UnsupportedReportVersion(version) => write!(f, "The encountered Attestation Report version {version} is not supported by the library yet."),
            AttestationReportError::UnsupportedField(field) => write!(f,"The field {field} is not supported in the provided Attestation Report version"),
        }
    }
}

impl From<AttestationReportError> for std::io::Error {
    fn from(value: AttestationReportError) -> Self {
        std::io::Error::new(std::io::ErrorKind::Other, value)
    }
}

impl error::Error for AttestationReportError {}

#[derive(Debug)]
/// Errors which may be encountered when building custom guest context.
pub enum GCTXError {
    /// Malformed guest context page.
    InvalidPageSize(usize, usize),

    /// Block size data was the incorrect size
    InvalidBlockSize,

    /// Missing data to do page update
    MissingData,

    /// Missing block size
    MissingBlockSize,

    /// Unknown Error.
    UnknownError,
}

impl std::fmt::Display for GCTXError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            GCTXError::InvalidPageSize(actual, expected) => write!(
                f,
                "Page information was not the correct length ({actual} vs {expected})"
            ),
            GCTXError::InvalidBlockSize => {
                write!(f, "Provided data does not conform to a 4096 block size")
            }
            GCTXError::MissingData => {
                write!(f, "Did not provide data to perform page update")
            }
            GCTXError::MissingBlockSize => {
                write!(f, "Did not provide block size to perform page update")
            }
            GCTXError::UnknownError => write!(f, "An unknown Guest Context error encountered"),
        }
    }
}

impl std::error::Error for GCTXError {}

#[derive(Debug)]
/// Errors which may be encountered when handling OVMF data
pub enum OVMFError {
    /// An invalid section type was provided for OVMF METADATA
    InvalidSectionType,

    /// Part of the SEV METADATA failed verification
    SEVMetadataVerification(String),

    /// Desired entry is missing from table
    EntryMissingInTable(String),

    /// Failed to get item from table
    GetTableItemError,

    /// Invalid Entry Size was provided
    InvalidSize(String, usize, usize),

    /// GUID doesn't match expected GUID
    MismatchingGUID,

    /// Unknown Error.
    UnknownError,
}

impl std::fmt::Display for OVMFError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            OVMFError::InvalidSectionType => write!(f, "An invalid section type was found"),
            OVMFError::SEVMetadataVerification(section) => {
                write!(f, "Wrong SEV metadata {section}")
            }
            OVMFError::EntryMissingInTable(entry) => {
                write!(f, "Can't find {entry} entry in OVMF table")
            }
            OVMFError::GetTableItemError => {
                write!(f, "OVMF table failed to return item")
            }
            OVMFError::InvalidSize(entry, actual, expected) => {
                write!(f, "Invalid size of {entry}: {actual} < {expected}")
            }
            OVMFError::MismatchingGUID => {
                write!(f, "OVMF table footer GUID does not match expected GUID")
            }
            OVMFError::UnknownError => write!(f, "An unknown OVMF error encountered"),
        }
    }
}

impl std::error::Error for OVMFError {}

/// Errors which may be encountered when building SEV hashes.
#[derive(Debug)]
pub enum SevHashError {
    /// Provided page has invalid size
    InvalidSize(usize, usize),

    /// Provided page has invalid offset
    InvalidOffset(usize, usize),

    /// Unknown Error.
    UnknownError,
}

impl std::fmt::Display for SevHashError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            SevHashError::InvalidOffset(actual, expected) => {
                write!(f, "Invalid page Offset: {actual} vs {expected}")
            }
            SevHashError::InvalidSize(actual, expected) => {
                write!(f, "Invalid page Size: {actual} vs {expected}")
            }
            SevHashError::UnknownError => write!(f, "An unknown SEV Hashing error encountered"),
        }
    }
}

impl std::error::Error for SevHashError {}

/// Possible errors when working with the large array type
#[derive(Debug)]
pub enum ArrayError {
    /// Error when trying from slice
    SliceError(TryFromSliceError),

    /// Error when converting from vector
    VectorError(String),
}

impl std::fmt::Display for ArrayError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            ArrayError::SliceError(error) => {
                write!(f, "Error when trying from slice: {error}")
            }
            ArrayError::VectorError(error) => {
                write!(f, "Error when trying from vector: {error}")
            }
        }
    }
}

impl std::error::Error for ArrayError {}

impl std::convert::From<TryFromSliceError> for ArrayError {
    fn from(value: TryFromSliceError) -> Self {
        Self::SliceError(value)
    }
}

/// Errors when calculating the ID BLOCK
#[derive(Debug)]
pub enum IdBlockError {
    #[cfg(all(feature = "snp", feature = "openssl"))]
    /// TryFrom Slice Error handling
    CryptoErrorStack(openssl::error::ErrorStack),

    /// Large Array Error handling
    LargeArrayError(ArrayError),

    /// File Error Handling
    FileError(std::io::Error),

    /// TryFrom Slice Error handling
    FromSliceError(TryFromSliceError),

    /// Error from when handling SEV Curve algorithm
    SevCurveError(),

    /// Error when handling SEV ECDSA Signature
    SevEcsdsaSigError(String),

    /// Error when converting vector into array
    BadVectorError(usize, usize),
}

impl std::fmt::Display for IdBlockError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            #[cfg(all(feature = "snp", feature = "openssl"))]
            IdBlockError::CryptoErrorStack(e) => write!(f, "Error when with OPENSSL: {e}"),
            IdBlockError::LargeArrayError(e) => write!(f, "{e}"),
            IdBlockError::FileError(e) => write!(f, "Failed handling file: {e}"),
            IdBlockError::FromSliceError(e) => write!(f, "Error converting slice: {e}"),
            IdBlockError::SevCurveError() => {
                write!(f, "Wrong curve used in the provided private key")
            }
            IdBlockError::SevEcsdsaSigError(msg) => {
                write!(f, "Error validation SEV signature: {msg}")
            }
            IdBlockError::BadVectorError(vec_size, expected) => write!(
                f,
                "Bad vector length: Got {vec_size}, and expected: {expected}"
            ),
        }
    }
}

impl std::error::Error for IdBlockError {}

#[cfg(all(feature = "snp", feature = "openssl"))]
impl std::convert::From<openssl::error::ErrorStack> for IdBlockError {
    fn from(value: openssl::error::ErrorStack) -> Self {
        Self::CryptoErrorStack(value)
    }
}

impl std::convert::From<ArrayError> for IdBlockError {
    fn from(value: ArrayError) -> Self {
        Self::LargeArrayError(value)
    }
}

impl std::convert::From<std::io::Error> for IdBlockError {
    fn from(value: std::io::Error) -> Self {
        Self::FileError(value)
    }
}

impl std::convert::From<TryFromSliceError> for IdBlockError {
    fn from(value: TryFromSliceError) -> Self {
        Self::FromSliceError(value)
    }
}

/// Errors which may be encountered when calculating the guest measurement.
#[derive(Debug)]
pub enum MeasurementError {
    /// TryFrom Slice Error handling
    FromSliceError(TryFromSliceError),

    /// UUID Error handling
    UUIDError(uuid::Error),

    /// File Error Handling
    FileError(std::io::Error),

    /// Vec from hex Error Handling
    FromHexError(hex::FromHexError),

    /// Guest Context Error Handling
    GCTXError(GCTXError),

    /// OVMF Error Handling
    OVMFError(OVMFError),

    /// SEV Hash Error Handling
    SevHashError(SevHashError),

    /// Id Block Error Handling
    IdBlockError(IdBlockError),

    /// Large Array Error handling
    LargeArrayError(ArrayError),

    /// Invalid VCPU provided
    InvalidVcpuTypeError(String),

    /// Invalid VCPU Signature provided
    InvalidVcpuSignatureError(String),

    /// Invalid VMM Provided
    InvalidVmmError(String),

    /// Invalid SEV Mode provided
    InvalidSevModeError(String),

    /// OVMF doesn't support kernel measurement
    InvalidOvmfKernelError,

    /// OVMF is missing required section with kernel specified
    MissingSection(String),
}

impl std::fmt::Display for MeasurementError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            MeasurementError::FromSliceError(e) => write!(f, "Error converting slice: {e}"),
            MeasurementError::UUIDError(e) => write!(f, "UUID Error encountered: {e}"),
            MeasurementError::FileError(e) => write!(f, "Failed handling file: {e}"),
            MeasurementError::FromHexError(e) => write!(f, "Converting hex to vector error: {e}"),
            MeasurementError::GCTXError(e) => write!(f, "GCTX Error Encountered: {e}"),
            MeasurementError::OVMFError(e) => write!(f, "OVMF Error Encountered: {e}"),
            MeasurementError::SevHashError(e) => write!(f, "Sev hash Error Encountered: {e}"),
            MeasurementError::IdBlockError(e) => write!(f, "Id Block Error Encountered: {e}"),
            MeasurementError::LargeArrayError(e) => {
                write!(f, "Error when handling Large arrays: {e}")
            }
            MeasurementError::InvalidVcpuTypeError(value) => {
                write!(f, "Invalid VCPU type value provided: {value}")
            }
            MeasurementError::InvalidVcpuSignatureError(value) => {
                write!(f, "Invalid VCPU signature provided: {value}")
            }
            MeasurementError::InvalidVmmError(value) => {
                write!(f, "Invalid VMM type provided: {value}")
            }
            MeasurementError::InvalidSevModeError(value) => {
                write!(f, "Invalid SEV mode provided: {value}")
            }
            MeasurementError::InvalidOvmfKernelError => write!(
                f,
                "Kernel specified but OVMF doesn't support kernel/initrd/cmdline measurement"
            ),
            MeasurementError::MissingSection(section) => write!(
                f,
                "Kernel specified but OVMF metadata doesn't include {section} section"
            ),
        }
    }
}

impl std::error::Error for MeasurementError {}

impl std::convert::From<TryFromSliceError> for MeasurementError {
    fn from(value: TryFromSliceError) -> Self {
        Self::FromSliceError(value)
    }
}

impl std::convert::From<uuid::Error> for MeasurementError {
    fn from(value: uuid::Error) -> Self {
        Self::UUIDError(value)
    }
}

impl std::convert::From<std::io::Error> for MeasurementError {
    fn from(value: std::io::Error) -> Self {
        Self::FileError(value)
    }
}

impl std::convert::From<hex::FromHexError> for MeasurementError {
    fn from(value: hex::FromHexError) -> Self {
        Self::FromHexError(value)
    }
}

impl std::convert::From<GCTXError> for MeasurementError {
    fn from(value: GCTXError) -> Self {
        Self::GCTXError(value)
    }
}

impl std::convert::From<OVMFError> for MeasurementError {
    fn from(value: OVMFError) -> Self {
        Self::OVMFError(value)
    }
}

impl std::convert::From<SevHashError> for MeasurementError {
    fn from(value: SevHashError) -> Self {
        Self::SevHashError(value)
    }
}

impl std::convert::From<IdBlockError> for MeasurementError {
    fn from(value: IdBlockError) -> Self {
        Self::IdBlockError(value)
    }
}

impl std::convert::From<ArrayError> for MeasurementError {
    fn from(value: ArrayError) -> Self {
        Self::LargeArrayError(value)
    }
}

#[cfg(feature = "openssl")]
#[derive(Debug)]
/// Used to describe errors related to SEV-ES "Sessions".
pub enum SessionError {
    /// Errors which occur from using the rdrand crate.
    RandError(ErrorCode),

    /// OpenSSL Error Stack
    OpenSSLStack(ErrorStack),

    /// Errors occuring from IO operations.
    IOError(std::io::Error),
}

#[cfg(feature = "openssl")]
impl From<ErrorCode> for SessionError {
    fn from(value: ErrorCode) -> Self {
        Self::RandError(value)
    }
}

#[cfg(feature = "openssl")]
impl From<std::io::Error> for SessionError {
    fn from(value: std::io::Error) -> Self {
        Self::IOError(value)
    }
}

#[cfg(feature = "openssl")]
impl From<ErrorStack> for SessionError {
    fn from(value: ErrorStack) -> Self {
        Self::OpenSSLStack(value)
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use std::{
        convert::{TryFrom, TryInto},
        error::Error,
    };

    #[test]
    fn test_vmm_error_complete() {
        // Test all variants
        let variants = vec![
            (1u32, VmmError::InvalidCertificatePageLength),
            (2u32, VmmError::RateLimitRetryRequest),
            (999u32, VmmError::Unknown),
        ];

        for (code, expected) in variants {
            // Test u32 conversion
            assert_eq!(VmmError::from(code), expected);
            // Test u64 conversion
            assert_eq!(VmmError::from((code as u64) << 32), expected);
            // Test display
            assert!(!expected.to_string().is_empty());
            // Test error trait
            assert!(std::error::Error::source(&expected).is_none());
        }
    }

    #[test]
    fn test_sev_error_complete() {
        // Test all valid codes
        for code in 0x01..=0x27u32 {
            if code == 0x1E {
                continue;
            } // Skip gap
            let err = SevError::from(code);
            assert!(!matches!(err, SevError::UnknownError));

            // Test u64 conversion
            let err64 = SevError::from(code as u64);
            assert_eq!(err, err64);

            // Test display
            assert!(!err.to_string().is_empty());

            // Test c_int conversion
            let c_val: c_int = err.into();
            assert_eq!(c_val as u32, code);
        }

        // Test invalid codes
        assert_eq!(SevError::from(0u32), SevError::UnknownError);
        assert_eq!(SevError::from(0x28u32), SevError::UnknownError);
        assert!(!SevError::from(0u32).to_string().is_empty());
        assert!(!SevError::from(0x28u32).to_string().is_empty());
        let err: SevError = SevError::UnknownError;
        let c_val: c_int = err.into();
        assert_eq!(c_val as u32, u32::MAX);
    }

    #[test]
    fn test_raw_fw_error_complete() {
        let raw = RawFwError(0x100000000u64);

        // Test display and debug
        assert!(raw.to_string().contains("RawFwError: 4294967296"));
        assert!(format!("{:?}", raw).contains("RawFwError"));

        // Test From<u64>
        assert_eq!(RawFwError::from(0x100000000u64), raw);

        // Test tuple conversions
        let (upper, lower): (u32, u32) = raw.into();
        assert_eq!(upper, 1);
        assert_eq!(lower, 0);

        let raw2 = RawFwError(0x100000000u64);
        let (vmm, _sev): (VmmError, SevError) = raw2.into();
        assert_eq!(vmm, VmmError::InvalidCertificatePageLength);
    }

    #[test]
    fn test_firmware_error_complete() {
        let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
        let variants = vec![
            FirmwareError::IoError(io_err),
            FirmwareError::KnownSevError(SevError::InvalidPlatformState),
            FirmwareError::UnknownSevError(999),
        ];

        for err in variants {
            // Test display
            assert!(!err.to_string().is_empty());

            // Test c_int conversion
            let c_val: c_int = err.into();
            assert!(c_val == -1 || c_val > 0);
        }

        // Test conversions
        let from_u32: FirmwareError = 0x0u32.into();
        assert!(matches!(from_u32, FirmwareError::IoError(_)));
        let from_u32: FirmwareError = 0x1u32.into();
        assert!(matches!(from_u32, FirmwareError::KnownSevError(_)));
        let from_u32: FirmwareError = 0x28u32.into();
        assert!(matches!(from_u32, FirmwareError::UnknownSevError(_)));
        let from_u64: FirmwareError = 0x1u64.into();
        assert!(matches!(from_u64, FirmwareError::KnownSevError(_)));
    }

    #[test]
    fn test_firmware_error_conversions() {
        // Test From<SevError>
        let sev_err = SevError::InvalidPlatformState;
        let fw_err = FirmwareError::from(sev_err);
        assert!(matches!(
            fw_err,
            FirmwareError::KnownSevError(SevError::InvalidPlatformState)
        ));

        let unknown_sev = SevError::UnknownError;
        let fw_err = FirmwareError::from(unknown_sev);
        assert!(matches!(fw_err, FirmwareError::UnknownSevError(_)));

        // Test From<io::Error>
        let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
        let fw_err = FirmwareError::from(io_err);
        assert!(matches!(fw_err, FirmwareError::IoError(_)));
    }

    #[test]
    fn test_user_api_error_complete() {
        let variants = vec![
            FirmwareError::UnknownSevError(0).into(),
            std::io::Error::new(std::io::ErrorKind::Other, "test").into(),
            CertError::UnknownError.into(),
            VmmError::Unknown.into(),
            uuid::Uuid::try_from("").unwrap_err().into(),
            HashstickError::UnknownError.into(),
            UserApiError::VmplError, // No From impl
            UserApiError::Unknown,   // No From impl
        ];

        for err in variants {
            // Test display
            assert!(!err.to_string().is_empty());
            // Test error source
            match &err {
                UserApiError::VmplError | UserApiError::Unknown => assert!(err.source().is_none()),
                _ => assert!(err.source().is_some()),
            }
            // Test io::Error conversion
            let _: std::io::Error = err.into();
        }

        let sev_error: SevError = SevError::InvalidPlatformState;
        let uapi_error: UserApiError = sev_error.into();
        assert!(matches!(uapi_error, UserApiError::FirmwareError(_)));
    }

    #[test]
    fn test_hashstick_error_complete() {
        let variants = vec![
            HashstickError::InvalidLength,
            HashstickError::EmptyHashstickBuffer,
            HashstickError::UnknownError,
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(std::error::Error::source(&err).is_none());
        }
    }

    #[test]
    fn test_cert_error_complete() {
        let variants = vec![
            CertError::InvalidGUID,
            CertError::PageMisalignment,
            CertError::BufferOverflow,
            CertError::EmptyCertBuffer,
            CertError::UnknownError,
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(std::error::Error::source(&err).is_none());
        }
    }

    #[test]
    fn test_gctx_error_complete() {
        let variants = vec![
            GCTXError::InvalidPageSize(100, 200),
            GCTXError::InvalidBlockSize,
            GCTXError::MissingData,
            GCTXError::MissingBlockSize,
            GCTXError::UnknownError,
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(std::error::Error::source(&err).is_none());
        }
    }

    #[test]
    fn test_ovmf_error_complete() {
        let variants = vec![
            OVMFError::InvalidSectionType,
            OVMFError::SEVMetadataVerification("test".into()),
            OVMFError::EntryMissingInTable("test".into()),
            OVMFError::GetTableItemError,
            OVMFError::InvalidSize("test".into(), 1, 2),
            OVMFError::MismatchingGUID,
            OVMFError::UnknownError,
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(std::error::Error::source(&err).is_none());
        }
    }

    #[test]
    fn test_sev_hash_error_complete() {
        let variants = vec![
            SevHashError::InvalidSize(1, 2),
            SevHashError::InvalidOffset(1, 2),
            SevHashError::UnknownError,
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(std::error::Error::source(&err).is_none());
        }
    }

    #[test]
    fn test_large_array_error_complete() {
        let slice_err: Result<[u8; 2], TryFromSliceError> = vec![1u8].as_slice().try_into();
        let variants = vec![
            slice_err.unwrap_err().into(),
            ArrayError::VectorError("test".into()),
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(std::error::Error::source(&err).is_none());
        }
    }

    #[test]
    fn test_id_block_error_complete() {
        let slice_err: Result<[u8; 2], TryFromSliceError> = vec![1u8].as_slice().try_into();

        let variants = vec![
            ArrayError::VectorError("test".into()).into(),
            std::io::Error::new(std::io::ErrorKind::Other, "test").into(),
            slice_err.unwrap_err().into(),
            IdBlockError::SevCurveError(),
            IdBlockError::SevEcsdsaSigError("test".into()),
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(std::error::Error::source(&err).is_none());
        }

        // Test conversions
        let arr_err = ArrayError::VectorError("test".into());
        assert!(matches!(
            IdBlockError::from(arr_err),
            IdBlockError::LargeArrayError(_)
        ));
    }

    #[test]
    fn test_measurement_error_complete() {
        let slice_err: Result<[u8; 2], TryFromSliceError> = vec![1u8].as_slice().try_into();

        let uuid_err = uuid::Uuid::try_from("").unwrap_err();

        let variants = vec![
            slice_err.unwrap_err().into(),
            uuid_err.into(),
            std::io::Error::new(std::io::ErrorKind::Other, "test").into(),
            hex::FromHexError::OddLength.into(),
            GCTXError::UnknownError.into(),
            OVMFError::UnknownError.into(),
            SevHashError::UnknownError.into(),
            IdBlockError::SevCurveError().into(),
            ArrayError::VectorError("test".into()).into(),
            MeasurementError::InvalidVcpuTypeError("test".into()),
            MeasurementError::InvalidVcpuSignatureError("test".into()),
            MeasurementError::InvalidVmmError("test".into()),
            MeasurementError::InvalidSevModeError("test".into()),
            MeasurementError::InvalidOvmfKernelError,
            MeasurementError::MissingSection("test".into()),
        ];

        for err in variants {
            assert!(!err.to_string().is_empty());
            assert!(
                err.source().is_some()
                    || matches!(
                        err,
                        MeasurementError::FromSliceError(_)
                            | MeasurementError::UUIDError(_)
                            | MeasurementError::FileError(_)
                            | MeasurementError::FromHexError(_)
                            | MeasurementError::GCTXError(_)
                            | MeasurementError::OVMFError(_)
                            | MeasurementError::SevHashError(_)
                            | MeasurementError::IdBlockError(_)
                            | MeasurementError::LargeArrayError(_)
                            | MeasurementError::InvalidVcpuTypeError(_)
                            | MeasurementError::InvalidVcpuSignatureError(_)
                            | MeasurementError::InvalidVmmError(_)
                            | MeasurementError::InvalidSevModeError(_)
                            | MeasurementError::InvalidOvmfKernelError
                            | MeasurementError::MissingSection(_)
                    )
            );
        }
    }

    #[cfg(feature = "openssl")]
    #[test]
    fn test_openssl_features_complete() {
        // Test CertFormatError
        let cert_err = CertFormatError::UnknownFormat;
        assert!(!cert_err.to_string().is_empty());
        assert!(std::error::Error::source(&cert_err).is_none());

        // Test SessionError
        let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
        let variants = vec![
            SessionError::RandError(ErrorCode::HardwareFailure),
            SessionError::IOError(io_err),
            SessionError::OpenSSLStack(ErrorStack::get()),
        ];

        for err in variants {
            let debug_str = format!("{:?}", err);
            match err {
                SessionError::RandError(_) => assert!(debug_str.contains("RandError")),
                SessionError::IOError(_) => assert!(debug_str.contains("IOError")),
                SessionError::OpenSSLStack(_) => assert!(debug_str.contains("OpenSSLStack")),
            }
        }

        // Test conversions
        let from_io = SessionError::from(std::io::Error::new(std::io::ErrorKind::Other, "test"));
        assert!(matches!(from_io, SessionError::IOError(_)));

        let from_code = SessionError::from(ErrorCode::HardwareFailure);
        assert!(matches!(from_code, SessionError::RandError(_)));

        let from_stack = SessionError::from(ErrorStack::get());
        assert!(matches!(from_stack, SessionError::OpenSSLStack(_)));
    }
}