Skip to content

Commit 32a3338

Browse files
committed
Latest Updates
1 parent 62567cf commit 32a3338

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

Diff for: tss-esapi/examples/aes_encrypt_decrypt.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use tss_esapi::{
1010
interface_types::{
1111
algorithm::{HashingAlgorithm, PublicAlgorithm, SymmetricMode},
1212
key_bits::AesKeyBits,
13-
resource_handles::Hierarchy,
13+
reserved_handles::Hierarchy,
1414
},
1515
structures::{
1616
CreatePrimaryKeyResult, Digest, InitialValue, MaxBuffer, PublicBuilder,
@@ -73,27 +73,25 @@ fn main() {
7373

7474
// The data we wish to encrypt. Be aware that there is a limit to the size of this data
7575
// that can be encrypted or decrypted (1024 bytes). In some cases you may need to encrypt a
76-
// "content encryption key", which can be decrypted and released and then used to decrypt
76+
// content encryption key (CEK), which can be decrypted and released and then used to decrypt
7777
// the actual data in question outside of the TPM.
7878
//
79-
// TPMs also tend to be "slower" for encryption/decryption, so you may consider the
79+
// TPMs also tend to be "slower" for encryption/decryption, so you should consider the
8080
// CEK pattern for performance reasons.
8181
let data_to_encrypt = "TPMs are super cool, you should use them!"
8282
.as_bytes()
8383
.to_vec();
8484

8585
eprintln!("{:?}", data_to_encrypt.len());
8686

87-
// Input data needs to always be a multiple of AES_BLOCK_SIZE, so we implement PKCS7 padding
88-
// to achieve this.
87+
// Input data needs to always be a multiple of the AES block size, in this case which is 16
88+
// bytes for AES-128-CBC. Normally you *MUST* implement a secure padding scheme such as pkcs7
89+
// but in this example we will *manually* pad the data.
8990

90-
// REVIEW NOTE: Tss-esapi likely should expose these as constants from AesKeyBits::Aes128
91-
// to prevent ambiguity!
91+
// WARNING: Manually implemented pkcs7 follows. This has not been audited. Don't use this
92+
// in production.
9293
const AES_BLOCK_SIZE: usize = 16;
9394

94-
// REVIEW NOTE: Should we added PKCS7 padding as a function to MaxBuffer to prevent
95-
// people needing to "roll their own"?
96-
9795
let need_k_bytes = AES_BLOCK_SIZE - (data_to_encrypt.len() % AES_BLOCK_SIZE);
9896
// PKCS7 always pads to remove ambiguous situations.
9997
let need_k_bytes = if need_k_bytes == 0 {
@@ -109,11 +107,12 @@ fn main() {
109107

110108
let padded_data_to_encrypt = MaxBuffer::try_from(padded_data_to_encrypt).unwrap();
111109

112-
// Padding always has to be added.
110+
// Padding always has to be added in pkcs7 to make it unambiguous.
113111
assert_ne!(
114112
data_to_encrypt.as_slice(),
115113
padded_data_to_encrypt.as_slice()
116114
);
115+
// END WARNING
117116

118117
// AES requires a random initial_value before any encryption or decryption. This must
119118
// be persisted with the encrypted data, else decryption can not be performed.
@@ -174,6 +173,9 @@ fn main() {
174173
panic!("Should not be empty");
175174
}
176175

176+
// WARNING: Manually implemented pkcs7 follows. This has not been audited. Don't use this
177+
// in production.
178+
177179
let last_byte = decrypted_data.len() - 1;
178180
let k_byte = decrypted_data[last_byte];
179181
// Since pkcs7 padding repeats this byte k times, we check that this byte
@@ -194,6 +196,8 @@ fn main() {
194196
let mut decrypted_data = decrypted_data.to_vec();
195197
decrypted_data.truncate(truncate_to);
196198

199+
// END WARNING
200+
197201
println!("data_to_encrypt = {:?}", data_to_encrypt);
198202
println!("decrypted_data = {:?}", decrypted_data);
199203
// They are the same!

0 commit comments

Comments
 (0)