@@ -56,6 +56,68 @@ impl Session {
5656 Ok ( signature)
5757 }
5858
59+ /// Starts new multi-part signing operation
60+ pub fn sign_init ( & self , mechanism : & Mechanism , key : ObjectHandle ) -> Result < ( ) > {
61+ let mut mechanism: CK_MECHANISM = mechanism. into ( ) ;
62+
63+ unsafe {
64+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_SignInit ) (
65+ self . handle ( ) ,
66+ & mut mechanism as CK_MECHANISM_PTR ,
67+ key. handle ( ) ,
68+ ) )
69+ . into_result ( Function :: SignInit ) ?;
70+ }
71+
72+ Ok ( ( ) )
73+ }
74+
75+ /// Continues an ongoing multi-part signing operation,
76+ /// taking in the next part of the data to sign
77+ pub fn sign_update ( & self , data : & [ u8 ] ) -> Result < ( ) > {
78+ unsafe {
79+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_SignUpdate ) (
80+ self . handle ( ) ,
81+ data. as_ptr ( ) as * mut u8 ,
82+ data. len ( ) . try_into ( ) ?,
83+ ) )
84+ . into_result ( Function :: SignUpdate ) ?;
85+ }
86+
87+ Ok ( ( ) )
88+ }
89+
90+ /// Finalizes ongoing multi-part signing operation,
91+ /// returning the signature
92+ pub fn sign_final ( & self ) -> Result < Vec < u8 > > {
93+ let mut signature_len = 0 ;
94+
95+ // Get the output buffer length
96+ unsafe {
97+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_SignFinal ) (
98+ self . handle ( ) ,
99+ std:: ptr:: null_mut ( ) ,
100+ & mut signature_len,
101+ ) )
102+ . into_result ( Function :: SignFinal ) ?;
103+ }
104+
105+ let mut signature = vec ! [ 0 ; signature_len. try_into( ) ?] ;
106+
107+ unsafe {
108+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_SignFinal ) (
109+ self . handle ( ) ,
110+ signature. as_mut_ptr ( ) ,
111+ & mut signature_len,
112+ ) )
113+ . into_result ( Function :: SignFinal ) ?;
114+ }
115+
116+ signature. resize ( signature_len. try_into ( ) ?, 0 ) ;
117+
118+ Ok ( signature)
119+ }
120+
59121 /// Verify data in single-part
60122 pub fn verify (
61123 & self ,
@@ -86,4 +148,50 @@ impl Session {
86148 . into_result ( Function :: Verify )
87149 }
88150 }
151+
152+ /// Starts new multi-part verifying operation
153+ pub fn verify_init ( & self , mechanism : & Mechanism , key : ObjectHandle ) -> Result < ( ) > {
154+ let mut mechanism: CK_MECHANISM = mechanism. into ( ) ;
155+
156+ unsafe {
157+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_VerifyInit ) (
158+ self . handle ( ) ,
159+ & mut mechanism as CK_MECHANISM_PTR ,
160+ key. handle ( ) ,
161+ ) )
162+ . into_result ( Function :: VerifyInit ) ?;
163+ }
164+
165+ Ok ( ( ) )
166+ }
167+
168+ /// Continues an ongoing multi-part verifying operation,
169+ /// taking in the next part of the data to verify
170+ pub fn verify_update ( & self , data : & [ u8 ] ) -> Result < ( ) > {
171+ unsafe {
172+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_VerifyUpdate ) (
173+ self . handle ( ) ,
174+ data. as_ptr ( ) as * mut u8 ,
175+ data. len ( ) . try_into ( ) ?,
176+ ) )
177+ . into_result ( Function :: VerifyUpdate ) ?;
178+ }
179+
180+ Ok ( ( ) )
181+ }
182+
183+ /// Finalizes ongoing multi-part verifying operation,
184+ /// returning Ok only if the signature verifies
185+ pub fn verify_final ( & self , signature : & [ u8 ] ) -> Result < ( ) > {
186+ unsafe {
187+ Rv :: from ( get_pkcs11 ! ( self . client( ) , C_VerifyFinal ) (
188+ self . handle ( ) ,
189+ signature. as_ptr ( ) as * mut u8 ,
190+ signature. len ( ) . try_into ( ) ?,
191+ ) )
192+ . into_result ( Function :: VerifyFinal ) ?;
193+ }
194+
195+ Ok ( ( ) )
196+ }
89197}
0 commit comments