Skip to content

Commit e2955dc

Browse files
committed
session: Truncate returned buffer from operation
All these operations follow Section 5.2 semantics, which allow the size query to return larger buffer and adjust the returned length to the real length during the operation. Previously, the `resize()` was used, but it was discussed several times that it can not extend the buffer (unless the pkcs11 module misbehaves) so changing all the places to `truncate()` instead. Signed-off-by: Jakub Jelen <[email protected]>
1 parent 31894b4 commit e2955dc

File tree

7 files changed

+13
-13
lines changed

7 files changed

+13
-13
lines changed

cryptoki/src/session/decryption.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Session {
5656
.into_result(Function::Decrypt)?;
5757
}
5858

59-
data.resize(data_len.try_into()?, 0);
59+
data.truncate(data_len.try_into()?);
6060

6161
Ok(data)
6262
}
@@ -136,7 +136,7 @@ impl Session {
136136
.into_result(Function::DecryptFinal)?;
137137
}
138138

139-
data.resize(data_len.try_into()?, 0);
139+
data.truncate(data_len.try_into()?);
140140

141141
Ok(data)
142142
}

cryptoki/src/session/digesting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Session {
4949
.into_result(Function::Digest)?;
5050
}
5151

52-
digest.resize(digest_len.try_into()?, 0);
52+
digest.truncate(digest_len.try_into()?);
5353

5454
Ok(digest)
5555
}
@@ -124,7 +124,7 @@ impl Session {
124124
.into_result(Function::DigestFinal)?;
125125
}
126126

127-
digest.resize(digest_len.try_into()?, 0);
127+
digest.truncate(digest_len.try_into()?);
128128

129129
Ok(digest)
130130
}

cryptoki/src/session/encapsulation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl Session {
5454
.into_result(Function::EncapsulateKey)?;
5555
}
5656

57-
encapsulated.resize(encapsulated_len.try_into()?, 0);
57+
encapsulated.truncate(encapsulated_len.try_into()?);
5858

5959
Ok((encapsulated, ObjectHandle::new(handle)))
6060
}

cryptoki/src/session/encryption.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Session {
5555
.into_result(Function::Encrypt)?;
5656
}
5757

58-
encrypted_data.resize(encrypted_data_len.try_into()?, 0);
58+
encrypted_data.truncate(encrypted_data_len.try_into()?);
5959

6060
Ok(encrypted_data)
6161
}
@@ -135,7 +135,7 @@ impl Session {
135135
.into_result(Function::EncryptFinal)?;
136136
}
137137

138-
encrypted_data.resize(encrypted_data_len.try_into()?, 0);
138+
encrypted_data.truncate(encrypted_data_len.try_into()?);
139139

140140
Ok(encrypted_data)
141141
}

cryptoki/src/session/message_decryption.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Session {
6868
.into_result(Function::DecryptMessage)?;
6969
}
7070

71-
data.resize(data_len.try_into()?, 0);
71+
data.truncate(data_len.try_into()?);
7272

7373
Ok(data)
7474
}
@@ -123,7 +123,7 @@ impl Session {
123123
))
124124
.into_result(Function::DecryptMessageNext)?;
125125
}
126-
data.resize(data_len.try_into()?, 0);
126+
data.truncate(data_len.try_into()?);
127127

128128
Ok(data)
129129
}

cryptoki/src/session/message_encryption.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Session {
6868
.into_result(Function::EncryptMessage)?;
6969
}
7070

71-
encrypted_data.resize(encrypted_data_len.try_into()?, 0);
71+
encrypted_data.truncate(encrypted_data_len.try_into()?);
7272

7373
Ok(encrypted_data)
7474
}
@@ -123,7 +123,7 @@ impl Session {
123123
))
124124
.into_result(Function::EncryptMessageNext)?;
125125
}
126-
encrypted_data.resize(encrypted_data_len.try_into()?, 0);
126+
encrypted_data.truncate(encrypted_data_len.try_into()?);
127127

128128
Ok(encrypted_data)
129129
}

cryptoki/src/session/signing_macing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Session {
5151
.into_result(Function::Sign)?;
5252
}
5353

54-
signature.resize(signature_len.try_into()?, 0);
54+
signature.truncate(signature_len.try_into()?);
5555

5656
Ok(signature)
5757
}
@@ -113,7 +113,7 @@ impl Session {
113113
.into_result(Function::SignFinal)?;
114114
}
115115

116-
signature.resize(signature_len.try_into()?, 0);
116+
signature.truncate(signature_len.try_into()?);
117117

118118
Ok(signature)
119119
}

0 commit comments

Comments
 (0)