@@ -7,71 +7,51 @@ use log::error;
7
7
use parsec_interface:: operations:: psa_algorithm:: * ;
8
8
use parsec_interface:: operations:: { psa_sign_hash, psa_verify_hash} ;
9
9
use parsec_interface:: requests:: { ProviderID , ResponseStatus , Result } ;
10
- use tss_esapi:: { constants:: TPM2_ALG_SHA256 , utils:: AsymSchemeUnion , utils:: Signature } ;
11
10
12
11
impl TpmProvider {
13
12
pub ( super ) fn psa_sign_hash_internal (
14
13
& self ,
15
14
app_name : ApplicationName ,
16
15
op : psa_sign_hash:: Operation ,
17
16
) -> Result < psa_sign_hash:: Result > {
18
- let key_name = op. key_name ;
19
- let hash = op. hash ;
20
- let alg = op. alg ;
21
- let key_triple = KeyTriple :: new ( app_name, ProviderID :: Tpm , key_name) ;
17
+ let key_triple = KeyTriple :: new ( app_name, ProviderID :: Tpm , op. key_name . clone ( ) ) ;
22
18
23
19
let store_handle = self . key_info_store . read ( ) . expect ( "Key store lock poisoned" ) ;
24
20
let mut esapi_context = self
25
21
. esapi_context
26
22
. lock ( )
27
23
. expect ( "ESAPI Context lock poisoned" ) ;
28
24
29
- if alg
30
- != ( AsymmetricSignature :: RsaPkcs1v15Sign {
31
- hash_alg : Hash :: Sha256 ,
32
- } )
33
- {
34
- error ! (
35
- "The TPM provider currently only supports signature algorithm to be RSA PKCS#1 v1.5 and the text hashed with SHA-256." ) ;
36
- return Err ( ResponseStatus :: PsaErrorNotSupported ) ;
37
- }
38
-
39
- if hash. len ( ) != 32 {
40
- error ! ( "The SHA-256 hash must be 32 bytes long." ) ;
41
- return Err ( ResponseStatus :: PsaErrorInvalidArgument ) ;
42
- }
43
-
44
25
let ( password_context, key_attributes) =
45
26
key_management:: get_password_context ( & * store_handle, key_triple) ?;
46
27
47
- key_attributes. can_sign_hash ( ) ?;
48
- key_attributes. permits_alg ( alg. into ( ) ) ?;
49
- key_attributes. compatible_with_alg ( alg. into ( ) ) ?;
50
-
51
- match alg {
52
- AsymmetricSignature :: RsaPkcs1v15Sign {
53
- hash_alg : Hash :: Sha256 ,
54
- } => ( ) ,
28
+ match op. alg {
29
+ AsymmetricSignature :: RsaPkcs1v15Sign { .. } => ( ) ,
30
+ AsymmetricSignature :: Ecdsa { .. } => ( ) ,
55
31
_ => {
56
32
error ! (
57
- "The TPM provider currently only supports \" RSA PKCS#1 v1.5 signature with hashing\" algorithm with SHA-256 as hashing algorithm for the PsaSignHash operation." ) ;
33
+ "Requested algorithm is not supported by the TPM provider: {:?}" ,
34
+ op. alg
35
+ ) ;
58
36
return Err ( ResponseStatus :: PsaErrorNotSupported ) ;
59
37
}
60
38
}
61
39
40
+ op. validate ( key_attributes) ?;
41
+
62
42
let signature = esapi_context
63
43
. sign (
64
44
password_context. context ,
65
45
& password_context. auth_value ,
66
- & hash,
46
+ & op . hash ,
67
47
)
68
48
. or_else ( |e| {
69
49
error ! ( "Error signing: {}." , e) ;
70
50
Err ( utils:: to_response_status ( e) )
71
51
} ) ?;
72
52
73
53
Ok ( psa_sign_hash:: Result {
74
- signature : signature. signature ,
54
+ signature : utils :: signature_data_to_bytes ( signature. signature , key_attributes ) ? ,
75
55
} )
76
56
}
77
57
@@ -80,59 +60,36 @@ impl TpmProvider {
80
60
app_name : ApplicationName ,
81
61
op : psa_verify_hash:: Operation ,
82
62
) -> Result < psa_verify_hash:: Result > {
83
- let key_name = op. key_name ;
84
- let hash = op. hash ;
85
- let alg = op. alg ;
86
- let signature = op. signature ;
87
- let key_triple = KeyTriple :: new ( app_name, ProviderID :: Tpm , key_name) ;
63
+ let key_triple = KeyTriple :: new ( app_name, ProviderID :: Tpm , op. key_name . clone ( ) ) ;
88
64
89
65
let store_handle = self . key_info_store . read ( ) . expect ( "Key store lock poisoned" ) ;
90
66
let mut esapi_context = self
91
67
. esapi_context
92
68
. lock ( )
93
69
. expect ( "ESAPI Context lock poisoned" ) ;
94
70
95
- if alg
96
- != ( AsymmetricSignature :: RsaPkcs1v15Sign {
97
- hash_alg : Hash :: Sha256 ,
98
- } )
99
- {
100
- error ! (
101
- "The TPM provider currently only supports signature algorithm to be RSA PKCS#1 v1.5 and the text hashed with SHA-256." ) ;
102
- return Err ( ResponseStatus :: PsaErrorNotSupported ) ;
103
- }
104
-
105
- if hash. len ( ) != 32 {
106
- error ! ( "The SHA-256 hash must be 32 bytes long." ) ;
107
- return Err ( ResponseStatus :: PsaErrorInvalidArgument ) ;
108
- }
109
-
110
- let signature = Signature {
111
- scheme : AsymSchemeUnion :: RSASSA ( TPM2_ALG_SHA256 ) ,
112
- signature,
113
- } ;
114
-
115
71
let ( password_context, key_attributes) =
116
72
key_management:: get_password_context ( & * store_handle, key_triple) ?;
117
73
118
- key_attributes. can_verify_hash ( ) ?;
119
- key_attributes. permits_alg ( alg. into ( ) ) ?;
120
- key_attributes. compatible_with_alg ( alg. into ( ) ) ?;
121
-
122
- match alg {
123
- AsymmetricSignature :: RsaPkcs1v15Sign {
124
- hash_alg : Hash :: Sha256 ,
125
- } => ( ) ,
74
+ match op. alg {
75
+ AsymmetricSignature :: RsaPkcs1v15Sign { .. } => ( ) ,
76
+ AsymmetricSignature :: Ecdsa { .. } => ( ) ,
126
77
_ => {
127
78
error ! (
128
- "The TPM provider currently only supports \" RSA PKCS#1 v1.5 signature with hashing\" algorithm with SHA-256 as hashing algorithm for the PsaVerifyHash operation." ) ;
79
+ "Requested algorithm is not supported by the TPM provider: {:?}" ,
80
+ op. alg
81
+ ) ;
129
82
return Err ( ResponseStatus :: PsaErrorNotSupported ) ;
130
83
}
131
84
}
132
85
86
+ op. validate ( key_attributes) ?;
87
+
88
+ let signature = utils:: parsec_to_tpm_signature ( op. signature , key_attributes, op. alg ) ?;
89
+
133
90
let _ = esapi_context
134
- . verify_signature ( password_context. context , & hash, signature)
135
- . or_else ( |e| Err ( utils:: to_response_status ( e ) ) ) ?;
91
+ . verify_signature ( password_context. context , & op . hash , signature)
92
+ . map_err ( utils:: to_response_status) ?;
136
93
137
94
Ok ( psa_verify_hash:: Result { } )
138
95
}
0 commit comments