Skip to content

Commit feb07ed

Browse files
Added tests for multi-part signing/verification
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent 7e2b18f commit feb07ed

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

cryptoki/tests/basic.rs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,170 @@ fn sign_verify_eddsa_with_ed448_schemes() -> TestResult {
213213
Ok(())
214214
}
215215

216+
#[test]
217+
#[serial]
218+
fn sign_verify_multipart() -> TestResult {
219+
let (pkcs11, slot) = init_pins();
220+
221+
// Open a session and log in
222+
let session = pkcs11.open_rw_session(slot)?;
223+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
224+
225+
// Define parameters for keypair
226+
let public_exponent = vec![0x01, 0x00, 0x01];
227+
let modulus_bits = 1024;
228+
229+
let pub_key_template = vec![
230+
Attribute::Token(true),
231+
Attribute::Private(false),
232+
Attribute::PublicExponent(public_exponent),
233+
Attribute::ModulusBits(modulus_bits.into()),
234+
Attribute::Verify(true),
235+
];
236+
let priv_key_template = vec![Attribute::Token(true), Attribute::Sign(true)];
237+
238+
// Generate keypair
239+
let (pub_key, priv_key) = session.generate_key_pair(
240+
&Mechanism::RsaPkcsKeyPairGen,
241+
&pub_key_template,
242+
&priv_key_template,
243+
)?;
244+
245+
// Data to sign
246+
let data = vec![0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33];
247+
248+
// Sign data in parts (standard RsaPkcs doesn't support this)
249+
session.sign_initialize(&Mechanism::Sha256RsaPkcs, priv_key)?;
250+
for part in data.chunks(3) {
251+
session.sign_update(part)?;
252+
}
253+
let signature = session.sign_finalize()?;
254+
255+
// Verify signature in parts (standard RsaPkcs doesn't support this)
256+
session.verify_initialize(&Mechanism::Sha256RsaPkcs, pub_key)?;
257+
for part in data.chunks(3) {
258+
session.verify_update(part)?;
259+
}
260+
session.verify_finalize(&signature)?;
261+
262+
// Delete keys
263+
session.destroy_object(pub_key)?;
264+
session.destroy_object(priv_key)?;
265+
266+
Ok(())
267+
}
268+
269+
#[test]
270+
#[serial]
271+
fn sign_verify_multipart_not_initialized() -> TestResult {
272+
let (pkcs11, slot) = init_pins();
273+
274+
// Open a session and log in
275+
let session = pkcs11.open_ro_session(slot)?;
276+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
277+
278+
// Data to sign/verify
279+
let data = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
280+
let signature = vec![0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
281+
282+
// Attempt to update signing without an operation having been initialized
283+
let result = session.sign_update(&data);
284+
285+
assert!(result.is_err());
286+
assert!(matches!(
287+
result.unwrap_err(),
288+
Error::Pkcs11(RvError::OperationNotInitialized, Function::SignUpdate)
289+
));
290+
291+
// Attempt to finalize signing without an operation having been initialized
292+
let result = session.sign_finalize();
293+
294+
assert!(result.is_err());
295+
assert!(matches!(
296+
result.unwrap_err(),
297+
Error::Pkcs11(RvError::OperationNotInitialized, Function::SignFinal)
298+
));
299+
300+
// Attempt to update verification without an operation having been initialized
301+
let result = session.verify_update(&data);
302+
303+
assert!(result.is_err());
304+
assert!(matches!(
305+
result.unwrap_err(),
306+
Error::Pkcs11(RvError::OperationNotInitialized, Function::VerifyUpdate)
307+
));
308+
309+
// Attempt to finalize verification without an operation having been initialized
310+
let result = session.verify_finalize(&signature);
311+
312+
assert!(result.is_err());
313+
assert!(matches!(
314+
result.unwrap_err(),
315+
Error::Pkcs11(RvError::OperationNotInitialized, Function::VerifyFinal)
316+
));
317+
318+
Ok(())
319+
}
320+
321+
#[test]
322+
#[serial]
323+
fn sign_verify_multipart_already_initialized() -> TestResult {
324+
let (pkcs11, slot) = init_pins();
325+
326+
// Open a session and log in
327+
let session = pkcs11.open_rw_session(slot)?;
328+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
329+
330+
// Define parameters for keypair
331+
let public_exponent = vec![0x01, 0x00, 0x01];
332+
let modulus_bits = 1024;
333+
334+
let pub_key_template = vec![
335+
Attribute::Token(true),
336+
Attribute::Private(false),
337+
Attribute::PublicExponent(public_exponent),
338+
Attribute::ModulusBits(modulus_bits.into()),
339+
Attribute::Verify(true),
340+
];
341+
let priv_key_template = vec![Attribute::Token(true), Attribute::Sign(true)];
342+
343+
// Generate keypair
344+
let (pub_key, priv_key) = session.generate_key_pair(
345+
&Mechanism::RsaPkcsKeyPairGen,
346+
&pub_key_template,
347+
&priv_key_template,
348+
)?;
349+
350+
// Initialize signing operation twice in a row
351+
session.sign_initialize(&Mechanism::Sha256RsaPkcs, priv_key)?;
352+
let result = session.sign_initialize(&Mechanism::Sha256RsaPkcs, priv_key);
353+
354+
assert!(result.is_err());
355+
assert!(matches!(
356+
result.unwrap_err(),
357+
Error::Pkcs11(RvError::OperationActive, Function::SignInit)
358+
));
359+
360+
// Make sure signing operation is over before trying same with verification
361+
session.sign_finalize()?;
362+
363+
// Initialize verification operation twice in a row
364+
session.verify_initialize(&Mechanism::Sha256RsaPkcs, pub_key)?;
365+
let result = session.verify_initialize(&Mechanism::Sha256RsaPkcs, pub_key);
366+
367+
assert!(result.is_err());
368+
assert!(matches!(
369+
result.unwrap_err(),
370+
Error::Pkcs11(RvError::OperationActive, Function::VerifyInit)
371+
));
372+
373+
// Delete keys
374+
session.destroy_object(pub_key)?;
375+
session.destroy_object(priv_key)?;
376+
377+
Ok(())
378+
}
379+
216380
#[test]
217381
#[serial]
218382
fn encrypt_decrypt() -> TestResult {

0 commit comments

Comments
 (0)