@@ -34,20 +34,18 @@ macro_rules! opt_check {
3434// Import Data
3535// ==========================================================================
3636
37- /// Variant that holds the actual import data
37+ /// Data to be imported, either a [`&JsonValue`](json::JsonValue) or a PEM encoded [`&str`](core::primitive::str).
38+ ///
39+ /// Instances of this struct may be used with the [`FapiContext::import()`](crate::FapiContext::import) function.
3840#[ derive( Clone , Copy , Debug ) ]
3941#[ non_exhaustive]
40- enum ImportVariant < ' a > {
42+ pub enum ImportData < ' a > {
43+ #[ non_exhaustive]
4144 Pem ( & ' a str ) ,
45+ #[ non_exhaustive]
4246 Json ( & ' a JsonValue ) ,
4347}
4448
45- /// Data to be imported, either a [`&JsonValue`](json::JsonValue) or a PEM encoded [`&str`](core::primitive::str).
46- ///
47- /// Instances of this struct may be used with the [`FapiContext::import()`](crate::FapiContext::import) function.
48- #[ derive( Clone , Copy , Debug ) ]
49- pub struct ImportData < ' a > ( ImportVariant < ' a > ) ;
50-
5149impl < ' a > ImportData < ' a > {
5250 /// Attempts to create a new `ImportData` from the given `JsonValue` reference.
5351 ///
@@ -56,7 +54,7 @@ impl<'a> ImportData<'a> {
5654 /// The JSON data will be validated, by the FAPI, when it is actually used.
5755 pub fn from_json ( json_value : & ' a JsonValue ) -> Result < Self , ErrorCode > {
5856 if json_value. is_object ( ) && ( !json_value. is_empty ( ) ) {
59- Ok ( Self ( ImportVariant :: Json ( json_value) ) )
57+ Ok ( Self :: Json ( json_value) )
6058 } else {
6159 Err ( ErrorCode :: InternalError ( InternalError :: InvalidArguments ) )
6260 }
@@ -73,7 +71,7 @@ impl<'a> ImportData<'a> {
7371 || pem_data. starts_with ( "-----BEGIN RSA PRIVATE KEY-----" )
7472 || pem_data. starts_with ( "-----BEGIN EC PRIVATE KEY-----" )
7573 {
76- Ok ( Self ( ImportVariant :: Pem ( pem_data) ) )
74+ Ok ( Self :: Pem ( pem_data) )
7775 } else {
7876 Err ( ERR_INVALID_ARGUMENTS )
7977 }
@@ -84,9 +82,9 @@ impl TryFrom<ImportData<'_>> for CStringHolder {
8482 type Error = ErrorCode ;
8583
8684 fn try_from ( data : ImportData ) -> Result < Self , Self :: Error > {
87- match data. 0 {
88- ImportVariant :: Json ( json_value) => CStringHolder :: try_from ( json_value) ,
89- ImportVariant :: Pem ( pem_data) => CStringHolder :: try_from ( pem_data) ,
85+ match data {
86+ ImportData :: Json ( json_value) => CStringHolder :: try_from ( json_value) ,
87+ ImportData :: Pem ( pem_data) => CStringHolder :: try_from ( pem_data) ,
9088 }
9189 }
9290}
@@ -95,43 +93,40 @@ impl TryFrom<ImportData<'_>> for CStringHolder {
9593// Seal Data
9694// ==========================================================================
9795
98- /// Variant that holds the actual seal data
99- #[ derive( Clone , Copy , Debug ) ]
100- #[ non_exhaustive]
101- enum SealVariant < ' a > {
102- Size ( NonZeroUsize ) ,
103- Data ( & ' a [ u8 ] ) ,
104- }
105-
106- /// The size of the sealed object and, optionally, the initial data
96+ /// The size of the sealed object and, optionally, the initial data.
10797pub type RawSealInfo = ( NonZeroUsize , CBinaryHolder ) ;
10898
10999/// Data to be sealed, either a non-zero size or some explicit data.
110100///
111101/// Instances of this struct may be used with the [`FapiContext::create_seal()`](crate::FapiContext::create_seal) function.
112102#[ derive( Clone , Copy , Debug ) ]
113- pub struct SealData < ' a > ( SealVariant < ' a > ) ;
103+ #[ non_exhaustive]
104+ pub enum SealedData < ' a > {
105+ #[ non_exhaustive]
106+ Data ( & ' a [ u8 ] ) ,
107+ Size ( NonZeroUsize ) ,
108+ }
114109
115- impl < ' a > SealData < ' a > {
110+ impl < ' a > SealedData < ' a > {
116111 /// Creates a new `SealData` with the specified non-zero size.
117112 ///
118113 /// The new sealed object will be created with the specified size and will be initialized by the TPM with random data.
119- pub fn from_size ( size : NonZeroUsize ) -> Self {
120- Self ( SealVariant :: Size ( size) )
114+ pub fn from_size ( size : usize ) -> Result < Self , ErrorCode > {
115+ Ok ( Self :: Size ( NonZeroUsize :: new ( size) . ok_or ( ERR_INVALID_ARGUMENTS ) ? ) )
121116 }
122117
123118 /// Creates a new `SealData` containing the specified data.
124119 ///
125120 /// The new sealed object will be created with a size of `data.len()` and it will be initialized with the given data.
126121 pub fn from_data ( data : & ' a [ u8 ] ) -> Result < Self , ErrorCode > {
127- if !data. is_empty ( ) { Ok ( Self ( SealVariant :: Data ( data) ) ) } else { Err ( ERR_INVALID_ARGUMENTS ) }
122+ if !data. is_empty ( ) { Ok ( Self :: Data ( data) ) } else { Err ( ERR_INVALID_ARGUMENTS ) }
128123 }
129124
130125 /// Returns the actual seal size and the associated data (if any)
131126 pub ( crate ) fn into_raw_data ( self ) -> Result < RawSealInfo , ErrorCode > {
132- match self . 0 {
133- SealVariant :: Size ( size) => Ok ( ( size, CBinaryHolder :: empty ( ) ) ) ,
134- SealVariant :: Data ( data) => {
127+ match self {
128+ Self :: Size ( size) => Ok ( ( size, CBinaryHolder :: empty ( ) ) ) ,
129+ Self :: Data ( data) => {
135130 let cstr_data = CBinaryHolder :: try_from ( data) ?;
136131 let cstr_size = NonZeroUsize :: new ( cstr_data. len ( ) ) . expect ( "Size must not be zero!" ) ;
137132 Ok ( ( cstr_size, cstr_data) )
0 commit comments