@@ -10,7 +10,7 @@ use tss_esapi::{
10
10
interface_types:: {
11
11
algorithm:: { HashingAlgorithm , PublicAlgorithm , SymmetricMode } ,
12
12
key_bits:: AesKeyBits ,
13
- resource_handles :: Hierarchy ,
13
+ reserved_handles :: Hierarchy ,
14
14
} ,
15
15
structures:: {
16
16
CreatePrimaryKeyResult , Digest , InitialValue , MaxBuffer , PublicBuilder ,
@@ -73,27 +73,25 @@ fn main() {
73
73
74
74
// The data we wish to encrypt. Be aware that there is a limit to the size of this data
75
75
// 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
77
77
// the actual data in question outside of the TPM.
78
78
//
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
80
80
// CEK pattern for performance reasons.
81
81
let data_to_encrypt = "TPMs are super cool, you should use them!"
82
82
. as_bytes ( )
83
83
. to_vec ( ) ;
84
84
85
85
eprintln ! ( "{:?}" , data_to_encrypt. len( ) ) ;
86
86
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.
89
90
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.
92
93
const AES_BLOCK_SIZE : usize = 16 ;
93
94
94
- // REVIEW NOTE: Should we added PKCS7 padding as a function to MaxBuffer to prevent
95
- // people needing to "roll their own"?
96
-
97
95
let need_k_bytes = AES_BLOCK_SIZE - ( data_to_encrypt. len ( ) % AES_BLOCK_SIZE ) ;
98
96
// PKCS7 always pads to remove ambiguous situations.
99
97
let need_k_bytes = if need_k_bytes == 0 {
@@ -109,11 +107,12 @@ fn main() {
109
107
110
108
let padded_data_to_encrypt = MaxBuffer :: try_from ( padded_data_to_encrypt) . unwrap ( ) ;
111
109
112
- // Padding always has to be added.
110
+ // Padding always has to be added in pkcs7 to make it unambiguous .
113
111
assert_ne ! (
114
112
data_to_encrypt. as_slice( ) ,
115
113
padded_data_to_encrypt. as_slice( )
116
114
) ;
115
+ // END WARNING
117
116
118
117
// AES requires a random initial_value before any encryption or decryption. This must
119
118
// be persisted with the encrypted data, else decryption can not be performed.
@@ -174,6 +173,9 @@ fn main() {
174
173
panic ! ( "Should not be empty" ) ;
175
174
}
176
175
176
+ // WARNING: Manually implemented pkcs7 follows. This has not been audited. Don't use this
177
+ // in production.
178
+
177
179
let last_byte = decrypted_data. len ( ) - 1 ;
178
180
let k_byte = decrypted_data[ last_byte] ;
179
181
// Since pkcs7 padding repeats this byte k times, we check that this byte
@@ -194,6 +196,8 @@ fn main() {
194
196
let mut decrypted_data = decrypted_data. to_vec ( ) ;
195
197
decrypted_data. truncate ( truncate_to) ;
196
198
199
+ // END WARNING
200
+
197
201
println ! ( "data_to_encrypt = {:?}" , data_to_encrypt) ;
198
202
println ! ( "decrypted_data = {:?}" , decrypted_data) ;
199
203
// They are the same!
0 commit comments