Skip to content

Commit e6e7da4

Browse files
Updated naming of multi-part functions to match the spec
Signed-off-by: Jacob Prud'homme <2160185+jacobprudhomme@users.noreply.github.com>
1 parent 63fc6eb commit e6e7da4

File tree

5 files changed

+61
-50
lines changed

5 files changed

+61
-50
lines changed

cryptoki/src/session/decryption.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Session {
6262
}
6363

6464
/// Starts new multi-part decryption operation
65-
pub fn decrypt_initialize(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
65+
pub fn decrypt_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
6666
let mut mechanism: CK_MECHANISM = mechanism.into();
6767

6868
unsafe {
@@ -77,7 +77,8 @@ impl Session {
7777
Ok(())
7878
}
7979

80-
/// Continues an ongoing multi-part decryption operation
80+
/// Continues an ongoing multi-part decryption operation,
81+
/// taking in the next part of the encrypted data and returning its decryption
8182
pub fn decrypt_update(&self, encrypted_data: &[u8]) -> Result<Vec<u8>> {
8283
let mut data_len = 0;
8384

@@ -109,8 +110,9 @@ impl Session {
109110
Ok(data)
110111
}
111112

112-
/// Finalizes ongoing multi-part decryption operation
113-
pub fn decrypt_finalize(&self) -> Result<Vec<u8>> {
113+
/// Finalizes ongoing multi-part decryption operation,
114+
/// returning any remaining bytes in the decrypted data
115+
pub fn decrypt_final(&self) -> Result<Vec<u8>> {
114116
let mut data_len = 0;
115117

116118
// Get the output buffer length

cryptoki/src/session/digesting.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Session {
5555
}
5656

5757
/// Starts new multi-part digesting operation
58-
pub fn digest_initialize(&self, m: &Mechanism) -> Result<()> {
58+
pub fn digest_init(&self, m: &Mechanism) -> Result<()> {
5959
let mut mechanism: CK_MECHANISM = m.into();
6060

6161
unsafe {
@@ -69,7 +69,8 @@ impl Session {
6969
Ok(())
7070
}
7171

72-
/// Continues an ongoing multi-part digesting operation
72+
/// Continues an ongoing multi-part digesting operation,
73+
/// taking in the next part of the data to digest
7374
pub fn digest_update(&self, data: &[u8]) -> Result<()> {
7475
unsafe {
7576
Rv::from(get_pkcs11!(self.client(), C_DigestUpdate)(
@@ -83,7 +84,8 @@ impl Session {
8384
Ok(())
8485
}
8586

86-
/// Continues an ongoing multi-part digesting operation, using the value of a secret key as input
87+
/// Continues an ongoing multi-part digesting operation,
88+
/// using the value of a secret key as input
8789
pub fn digest_key(&self, key: ObjectHandle) -> Result<()> {
8890
unsafe {
8991
Rv::from(get_pkcs11!(self.client(), C_DigestKey)(
@@ -96,8 +98,9 @@ impl Session {
9698
Ok(())
9799
}
98100

99-
/// Finalizes ongoing multi-part digest operation
100-
pub fn digest_finalize(&self) -> Result<Vec<u8>> {
101+
/// Finalizes ongoing multi-part digest operation,
102+
/// returning the digest
103+
pub fn digest_final(&self) -> Result<Vec<u8>> {
101104
let mut digest_len = 0;
102105

103106
// Get the output buffer length

cryptoki/src/session/encryption.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Session {
6161
}
6262

6363
/// Starts new multi-part encryption operation
64-
pub fn encrypt_initialize(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
64+
pub fn encrypt_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
6565
let mut mechanism: CK_MECHANISM = mechanism.into();
6666

6767
unsafe {
@@ -76,7 +76,8 @@ impl Session {
7676
Ok(())
7777
}
7878

79-
/// Continues an ongoing multi-part encryption operation
79+
/// Continues an ongoing multi-part encryption operation,
80+
/// taking in the next part of the data and returning its encryption
8081
pub fn encrypt_update(&self, data: &[u8]) -> Result<Vec<u8>> {
8182
let mut encrypted_data_len = 0;
8283

@@ -108,8 +109,9 @@ impl Session {
108109
Ok(encrypted_data)
109110
}
110111

111-
/// Finalizes ongoing multi-part encryption operation
112-
pub fn encrypt_finalize(&self) -> Result<Vec<u8>> {
112+
/// Finalizes ongoing multi-part encryption operation,
113+
/// returning any remaining bytes in the encrypted data
114+
pub fn encrypt_final(&self) -> Result<Vec<u8>> {
113115
let mut encrypted_data_len = 0;
114116

115117
// Get the output buffer length

cryptoki/src/session/signing_macing.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl Session {
5757
}
5858

5959
/// Starts new multi-part signing operation
60-
pub fn sign_initialize(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
60+
pub fn sign_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
6161
let mut mechanism: CK_MECHANISM = mechanism.into();
6262

6363
unsafe {
@@ -72,7 +72,8 @@ impl Session {
7272
Ok(())
7373
}
7474

75-
/// Continues an ongoing multi-part signing operation
75+
/// Continues an ongoing multi-part signing operation,
76+
/// taking in the next part of the data to sign
7677
pub fn sign_update(&self, data: &[u8]) -> Result<()> {
7778
unsafe {
7879
Rv::from(get_pkcs11!(self.client(), C_SignUpdate)(
@@ -86,8 +87,9 @@ impl Session {
8687
Ok(())
8788
}
8889

89-
/// Finalizes ongoing multi-part signing operation
90-
pub fn sign_finalize(&self) -> Result<Vec<u8>> {
90+
/// Finalizes ongoing multi-part signing operation,
91+
/// returning the signature
92+
pub fn sign_final(&self) -> Result<Vec<u8>> {
9193
let mut signature_len = 0;
9294

9395
// Get the output buffer length
@@ -148,7 +150,7 @@ impl Session {
148150
}
149151

150152
/// Starts new multi-part verifying operation
151-
pub fn verify_initialize(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
153+
pub fn verify_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
152154
let mut mechanism: CK_MECHANISM = mechanism.into();
153155

154156
unsafe {
@@ -163,7 +165,8 @@ impl Session {
163165
Ok(())
164166
}
165167

166-
/// Continues an ongoing multi-part verifying operation
168+
/// Continues an ongoing multi-part verifying operation,
169+
/// taking in the next part of the data to verify
167170
pub fn verify_update(&self, data: &[u8]) -> Result<()> {
168171
unsafe {
169172
Rv::from(get_pkcs11!(self.client(), C_VerifyUpdate)(
@@ -177,8 +180,9 @@ impl Session {
177180
Ok(())
178181
}
179182

180-
/// Finalizes ongoing multi-part verifying operation
181-
pub fn verify_finalize(&self, signature: &[u8]) -> Result<()> {
183+
/// Finalizes ongoing multi-part verifying operation,
184+
/// returning Ok only if the signature verifies
185+
pub fn verify_final(&self, signature: &[u8]) -> Result<()> {
182186
unsafe {
183187
Rv::from(get_pkcs11!(self.client(), C_VerifyFinal)(
184188
self.handle(),

cryptoki/tests/basic.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,18 @@ fn sign_verify_multipart() -> TestResult {
244244
let data = vec![0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33];
245245

246246
// Sign data in parts (standard RsaPkcs doesn't support this)
247-
session.sign_initialize(&Mechanism::Sha256RsaPkcs, priv_key)?;
247+
session.sign_init(&Mechanism::Sha256RsaPkcs, priv_key)?;
248248
for part in data.chunks(3) {
249249
session.sign_update(part)?;
250250
}
251-
let signature = session.sign_finalize()?;
251+
let signature = session.sign_final()?;
252252

253253
// Verify signature in parts (standard RsaPkcs doesn't support this)
254-
session.verify_initialize(&Mechanism::Sha256RsaPkcs, pub_key)?;
254+
session.verify_init(&Mechanism::Sha256RsaPkcs, pub_key)?;
255255
for part in data.chunks(3) {
256256
session.verify_update(part)?;
257257
}
258-
session.verify_finalize(&signature)?;
258+
session.verify_final(&signature)?;
259259

260260
// Delete keys
261261
session.destroy_object(pub_key)?;
@@ -287,7 +287,7 @@ fn sign_verify_multipart_not_initialized() -> TestResult {
287287
));
288288

289289
// Attempt to finalize signing without an operation having been initialized
290-
let result = session.sign_finalize();
290+
let result = session.sign_final();
291291

292292
assert!(result.is_err());
293293
assert!(matches!(
@@ -305,7 +305,7 @@ fn sign_verify_multipart_not_initialized() -> TestResult {
305305
));
306306

307307
// Attempt to finalize verification without an operation having been initialized
308-
let result = session.verify_finalize(&signature);
308+
let result = session.verify_final(&signature);
309309

310310
assert!(result.is_err());
311311
assert!(matches!(
@@ -344,8 +344,8 @@ fn sign_verify_multipart_already_initialized() -> TestResult {
344344
)?;
345345

346346
// Initialize signing operation twice in a row
347-
session.sign_initialize(&Mechanism::Sha256RsaPkcs, priv_key)?;
348-
let result = session.sign_initialize(&Mechanism::Sha256RsaPkcs, priv_key);
347+
session.sign_init(&Mechanism::Sha256RsaPkcs, priv_key)?;
348+
let result = session.sign_init(&Mechanism::Sha256RsaPkcs, priv_key);
349349

350350
assert!(result.is_err());
351351
assert!(matches!(
@@ -354,11 +354,11 @@ fn sign_verify_multipart_already_initialized() -> TestResult {
354354
));
355355

356356
// Make sure signing operation is over before trying same with verification
357-
session.sign_finalize()?;
357+
session.sign_final()?;
358358

359359
// Initialize verification operation twice in a row
360-
session.verify_initialize(&Mechanism::Sha256RsaPkcs, pub_key)?;
361-
let result = session.verify_initialize(&Mechanism::Sha256RsaPkcs, pub_key);
360+
session.verify_init(&Mechanism::Sha256RsaPkcs, pub_key)?;
361+
let result = session.verify_init(&Mechanism::Sha256RsaPkcs, pub_key);
362362

363363
assert!(result.is_err());
364364
assert!(matches!(
@@ -448,22 +448,22 @@ fn encrypt_decrypt_multipart() -> TestResult {
448448
];
449449

450450
// Encrypt data in parts
451-
session.encrypt_initialize(&Mechanism::AesEcb, key)?;
451+
session.encrypt_init(&Mechanism::AesEcb, key)?;
452452

453453
let mut encrypted_data = vec![];
454454
for part in data.chunks(3) {
455455
encrypted_data.extend(session.encrypt_update(part)?);
456456
}
457-
encrypted_data.extend(session.encrypt_finalize()?);
457+
encrypted_data.extend(session.encrypt_final()?);
458458

459459
// Decrypt data in parts
460-
session.decrypt_initialize(&Mechanism::AesEcb, key)?;
460+
session.decrypt_init(&Mechanism::AesEcb, key)?;
461461

462462
let mut decrypted_data = vec![];
463463
for part in encrypted_data.chunks(3) {
464464
decrypted_data.extend(session.decrypt_update(part)?);
465465
}
466-
decrypted_data.extend(session.decrypt_finalize()?);
466+
decrypted_data.extend(session.decrypt_final()?);
467467

468468
assert_eq!(data, decrypted_data);
469469

@@ -498,7 +498,7 @@ fn encrypt_decrypt_multipart_not_initialized() -> TestResult {
498498
));
499499

500500
// Attempt to finalize encryption without an operation having been initialized
501-
let result = session.encrypt_finalize();
501+
let result = session.encrypt_final();
502502

503503
assert!(result.is_err());
504504
assert!(matches!(
@@ -516,7 +516,7 @@ fn encrypt_decrypt_multipart_not_initialized() -> TestResult {
516516
));
517517

518518
// Attempt to finalize decryption without an operation having been initialized
519-
let result = session.decrypt_finalize();
519+
let result = session.decrypt_final();
520520

521521
assert!(result.is_err());
522522
assert!(matches!(
@@ -544,8 +544,8 @@ fn encrypt_decrypt_multipart_already_initialized() -> TestResult {
544544
let key = session.generate_key(&Mechanism::AesKeyGen, &template)?;
545545

546546
// Initialize encryption operation twice in a row
547-
session.encrypt_initialize(&Mechanism::AesEcb, key)?;
548-
let result = session.encrypt_initialize(&Mechanism::AesEcb, key);
547+
session.encrypt_init(&Mechanism::AesEcb, key)?;
548+
let result = session.encrypt_init(&Mechanism::AesEcb, key);
549549

550550
assert!(result.is_err());
551551
assert!(matches!(
@@ -554,11 +554,11 @@ fn encrypt_decrypt_multipart_already_initialized() -> TestResult {
554554
));
555555

556556
// Make sure encryption operation is over before trying same with decryption
557-
session.encrypt_finalize()?;
557+
session.encrypt_final()?;
558558

559559
// Initialize encryption operation twice in a row
560-
session.decrypt_initialize(&Mechanism::AesEcb, key)?;
561-
let result = session.decrypt_initialize(&Mechanism::AesEcb, key);
560+
session.decrypt_init(&Mechanism::AesEcb, key)?;
561+
let result = session.decrypt_init(&Mechanism::AesEcb, key);
562562

563563
assert!(result.is_err());
564564
assert!(matches!(
@@ -1626,12 +1626,12 @@ fn sha256_digest_multipart() -> TestResult {
16261626
];
16271627

16281628
// Digest data in parts
1629-
session.digest_initialize(&Mechanism::Sha256)?;
1629+
session.digest_init(&Mechanism::Sha256)?;
16301630
for part in data.chunks(3) {
16311631
session.digest_update(part)?;
16321632
}
16331633

1634-
let have = session.digest_finalize()?;
1634+
let have = session.digest_final()?;
16351635
let want = vec![
16361636
0x8c, 0x18, 0xb1, 0x5f, 0x01, 0x47, 0x13, 0x2a, 0x03, 0xc2, 0xe3, 0xfd, 0x4f, 0x29, 0xb7,
16371637
0x75, 0x80, 0x19, 0xb5, 0x58, 0x5e, 0xfc, 0xeb, 0x45, 0x18, 0x33, 0x2b, 0x2f, 0xa7, 0xa4,
@@ -1673,12 +1673,12 @@ fn sha256_digest_multipart_with_key() -> TestResult {
16731673
};
16741674

16751675
// Digest data in parts
1676-
session.digest_initialize(&Mechanism::Sha256)?;
1676+
session.digest_init(&Mechanism::Sha256)?;
16771677
session.digest_update(&data)?;
16781678
session.digest_key(key)?;
16791679

16801680
// Create digests to compare
1681-
let have = session.digest_finalize()?;
1681+
let have = session.digest_final()?;
16821682

16831683
data.append(&mut key_data);
16841684
let want = session.digest(&Mechanism::Sha256, &data)?;
@@ -1713,7 +1713,7 @@ fn sha256_digest_multipart_not_initialized() -> TestResult {
17131713
));
17141714

17151715
// Attempt to finalize digest without an operation having been initialized
1716-
let result = session.digest_finalize();
1716+
let result = session.digest_final();
17171717

17181718
assert!(result.is_err());
17191719
assert!(matches!(
@@ -1734,8 +1734,8 @@ fn sha256_digest_multipart_already_initialized() -> TestResult {
17341734
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
17351735

17361736
// Initialize digesting operation twice in a row
1737-
session.digest_initialize(&Mechanism::Sha256)?;
1738-
let result = session.digest_initialize(&Mechanism::Sha256);
1737+
session.digest_init(&Mechanism::Sha256)?;
1738+
let result = session.digest_init(&Mechanism::Sha256);
17391739

17401740
assert!(result.is_err());
17411741
assert!(matches!(

0 commit comments

Comments
 (0)