@@ -13,6 +13,130 @@ use std::ptr::null_mut;
1313
1414impl Context {
1515 /// Perform an asymmetric RSA encryption.
16+ ///
17+ /// # Details
18+ /// "This command performs RSA encryption using the indicated padding scheme according to IETF RFC
19+ /// 8017."
20+ ///
21+ /// # Arguments
22+ /// * `key_handle` - A [KeyHandle] to to public portion of RSA key to use for
23+ /// encryption.
24+ /// * `message` - The message to be encrypted.
25+ /// * `in_scheme` - The padding scheme to use if scheme associated with
26+ /// the `key_handle` is [RsaDecryptionScheme::Null].
27+ /// * `label` - A label whose association with the message is to be
28+ /// verified.
29+ /// # Returns
30+ /// The encrypted output.
31+ ///
32+ /// # Example
33+ ///
34+ /// ```rust
35+ /// # use tss_esapi::{
36+ /// # Context, TctiNameConf,
37+ /// # attributes::{SessionAttributesBuilder, ObjectAttributesBuilder},
38+ /// # constants::SessionType,
39+ /// # interface_types::{
40+ /// # algorithm::{
41+ /// # HashingAlgorithm, PublicAlgorithm, RsaDecryptAlgorithm,
42+ /// # },
43+ /// # key_bits::RsaKeyBits,
44+ /// # reserved_handles::Hierarchy,
45+ /// # },
46+ /// # structures::{
47+ /// # Auth, Data, RsaScheme, PublicBuilder, PublicRsaParametersBuilder, PublicKeyRsa,
48+ /// # RsaDecryptionScheme, HashScheme, SymmetricDefinition, RsaExponent,
49+ /// # },
50+ /// # };
51+ /// # use std::{env, str::FromStr, convert::TryFrom};
52+ /// # // Create context
53+ /// # let mut context =
54+ /// # Context::new(
55+ /// # TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
56+ /// # ).expect("Failed to create Context");
57+ /// #
58+ /// # let session = context
59+ /// # .start_auth_session(
60+ /// # None,
61+ /// # None,
62+ /// # None,
63+ /// # SessionType::Hmac,
64+ /// # SymmetricDefinition::AES_256_CFB,
65+ /// # tss_esapi::interface_types::algorithm::HashingAlgorithm::Sha256,
66+ /// # )
67+ /// # .expect("Failed to create session")
68+ /// # .expect("Received invalid handle");
69+ /// # let (session_attributes, session_attributes_mask) = SessionAttributesBuilder::new()
70+ /// # .with_decrypt(true)
71+ /// # .with_encrypt(true)
72+ /// # .build();
73+ /// # context.tr_sess_set_attributes(session, session_attributes, session_attributes_mask)
74+ /// # .expect("Failed to set attributes on session");
75+ /// # context.set_sessions((Some(session), None, None));
76+ /// # let mut random_digest = vec![0u8; 16];
77+ /// # getrandom::getrandom(&mut random_digest).unwrap();
78+ /// # let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
79+ /// #
80+ /// # let object_attributes = ObjectAttributesBuilder::new()
81+ /// # .with_fixed_tpm(true)
82+ /// # .with_fixed_parent(true)
83+ /// # .with_sensitive_data_origin(true)
84+ /// # .with_user_with_auth(true)
85+ /// # .with_decrypt(true)
86+ /// # .with_sign_encrypt(true)
87+ /// # .with_restricted(false)
88+ /// # .build()
89+ /// # .expect("Should be able to build object attributes when the attributes are not conflicting.");
90+ /// #
91+ /// # let key_pub = PublicBuilder::new()
92+ /// # .with_public_algorithm(PublicAlgorithm::Rsa)
93+ /// # .with_name_hashing_algorithm(HashingAlgorithm::Sha256)
94+ /// # .with_object_attributes(object_attributes)
95+ /// # .with_rsa_parameters(
96+ /// # PublicRsaParametersBuilder::new()
97+ /// # .with_scheme(RsaScheme::Null)
98+ /// # .with_key_bits(RsaKeyBits::Rsa2048)
99+ /// # .with_exponent(RsaExponent::default())
100+ /// # .with_is_signing_key(true)
101+ /// # .with_is_decryption_key(true)
102+ /// # .with_restricted(false)
103+ /// # .build()
104+ /// # .expect("Should be possible to build valid RSA parameters")
105+ /// # )
106+ /// # .with_rsa_unique_identifier(PublicKeyRsa::default())
107+ /// # .build()
108+ /// # .expect("Should be possible to build a valid Public object.");
109+ /// #
110+ /// # let key_handle = context
111+ /// # .create_primary(
112+ /// # Hierarchy::Owner,
113+ /// # key_pub,
114+ /// # None,
115+ /// # None,
116+ /// # None,
117+ /// # None,
118+ /// # )
119+ /// # .expect("Should be possible to create primary key from using valid Public object.")
120+ /// # .key_handle;
121+ /// // Because the key was created RsaScheme::Null it is possible to
122+ /// // provide a scheme for the rsa_encrypt function to use.
123+ /// let scheme =
124+ /// RsaDecryptionScheme::create(RsaDecryptAlgorithm::Oaep, Some(HashingAlgorithm::Sha256))
125+ /// .expect("Failed to create rsa decryption scheme");
126+ /// let plain_text = String::from("Plain Text");
127+ /// let plain_text_bytes = plain_text.as_bytes().to_vec();
128+ /// let message_in = PublicKeyRsa::try_from(plain_text_bytes)
129+ /// .expect("Should be possible to create a PublicKeyRsa object from valid bytes.");
130+ /// let label = Data::default();
131+ /// let cipher_text = context.rsa_encrypt(key_handle, message_in, scheme, label.clone())
132+ /// .expect("Should be possible to call rsa_encrypt using valid arguments.");
133+ /// # let message_out = context.rsa_decrypt(key_handle, cipher_text, scheme, label)
134+ /// # .expect("Should be possible to call rsa_decrypt using valid arguments.");
135+ /// # let decrypted_bytes = message_out.as_bytes().to_vec();
136+ /// # let decrypted_text = String::from_utf8(decrypted_bytes)
137+ /// # .expect("Should be possible to turn the decrypted data into an utf-8 string.");
138+ /// # assert_eq!(plain_text, decrypted_text);
139+ /// ```
16140 pub fn rsa_encrypt (
17141 & mut self ,
18142 key_handle : KeyHandle ,
@@ -43,6 +167,127 @@ impl Context {
43167 }
44168
45169 /// Perform an asymmetric RSA decryption.
170+ ///
171+ /// # Details
172+ /// "This command performs RSA decryption using the indicated padding scheme according to IETF RFC
173+ /// 8017 ((PKCS#1)."
174+ ///
175+ /// # Arguments
176+ /// * `key_handle` - A [KeyHandle] of the RSA key to use for decryption.
177+ /// * `cipher_test` - The cipher text to be decrypted.
178+ /// * `in_sceheme` - The padding scheme to use if scheme associated with
179+ /// the `key_handle` is [RsaDecryptionScheme::Null].
180+ /// * `label` - A label whose association with the message is to be
181+ /// verified.
182+ /// # Returns
183+ /// The decrypted output.
184+ ///
185+ /// # Example
186+ ///
187+ /// ```rust
188+ /// # use tss_esapi::{
189+ /// # Context, TctiNameConf,
190+ /// # attributes::{SessionAttributesBuilder, ObjectAttributesBuilder},
191+ /// # constants::SessionType,
192+ /// # interface_types::{
193+ /// # algorithm::{
194+ /// # HashingAlgorithm, PublicAlgorithm, RsaDecryptAlgorithm,
195+ /// # },
196+ /// # key_bits::RsaKeyBits,
197+ /// # reserved_handles::Hierarchy,
198+ /// # },
199+ /// # structures::{
200+ /// # Auth, Data, RsaScheme, PublicBuilder, PublicRsaParametersBuilder, PublicKeyRsa,
201+ /// # RsaDecryptionScheme, HashScheme, SymmetricDefinition, RsaExponent,
202+ /// # },
203+ /// # };
204+ /// # use std::{env, str::FromStr, convert::TryFrom};
205+ /// # // Create context
206+ /// # let mut context =
207+ /// # Context::new(
208+ /// # TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
209+ /// # ).expect("Failed to create Context");
210+ /// #
211+ /// # let session = context
212+ /// # .start_auth_session(
213+ /// # None,
214+ /// # None,
215+ /// # None,
216+ /// # SessionType::Hmac,
217+ /// # SymmetricDefinition::AES_256_CFB,
218+ /// # tss_esapi::interface_types::algorithm::HashingAlgorithm::Sha256,
219+ /// # )
220+ /// # .expect("Failed to create session")
221+ /// # .expect("Received invalid handle");
222+ /// # let (session_attributes, session_attributes_mask) = SessionAttributesBuilder::new()
223+ /// # .with_decrypt(true)
224+ /// # .with_encrypt(true)
225+ /// # .build();
226+ /// # context.tr_sess_set_attributes(session, session_attributes, session_attributes_mask)
227+ /// # .expect("Failed to set attributes on session");
228+ /// # context.set_sessions((Some(session), None, None));
229+ /// # let mut random_digest = vec![0u8; 16];
230+ /// # getrandom::getrandom(&mut random_digest).unwrap();
231+ /// # let key_auth = Auth::from_bytes(random_digest.as_slice()).unwrap();
232+ /// #
233+ /// # let object_attributes = ObjectAttributesBuilder::new()
234+ /// # .with_fixed_tpm(true)
235+ /// # .with_fixed_parent(true)
236+ /// # .with_sensitive_data_origin(true)
237+ /// # .with_user_with_auth(true)
238+ /// # .with_decrypt(true)
239+ /// # .with_sign_encrypt(true)
240+ /// # .with_restricted(false)
241+ /// # .build()
242+ /// # .expect("Should be able to build object attributes when the attributes are not conflicting.");
243+ /// #
244+ /// # let key_pub = PublicBuilder::new()
245+ /// # .with_public_algorithm(PublicAlgorithm::Rsa)
246+ /// # .with_name_hashing_algorithm(HashingAlgorithm::Sha256)
247+ /// # .with_object_attributes(object_attributes)
248+ /// # .with_rsa_parameters(
249+ /// # PublicRsaParametersBuilder::new()
250+ /// # .with_scheme(RsaScheme::Null)
251+ /// # .with_key_bits(RsaKeyBits::Rsa2048)
252+ /// # .with_exponent(RsaExponent::default())
253+ /// # .with_is_signing_key(true)
254+ /// # .with_is_decryption_key(true)
255+ /// # .with_restricted(false)
256+ /// # .build()
257+ /// # .expect("Should be possible to build valid RSA parameters")
258+ /// # )
259+ /// # .with_rsa_unique_identifier(PublicKeyRsa::default())
260+ /// # .build()
261+ /// # .expect("Should be possible to build a valid Public object.");
262+ /// #
263+ /// # let key_handle = context
264+ /// # .create_primary(
265+ /// # Hierarchy::Owner,
266+ /// # key_pub,
267+ /// # None,
268+ /// # None,
269+ /// # None,
270+ /// # None,
271+ /// # )
272+ /// # .expect("Should be possible to create primary key from using valid Public object.")
273+ /// # .key_handle;
274+ /// # let scheme =
275+ /// # RsaDecryptionScheme::create(RsaDecryptAlgorithm::RsaEs, None)
276+ /// # .expect("Failed to create rsa decryption scheme");
277+ /// # let plain_text = String::from("Plain Text");
278+ /// # let plain_text_bytes = plain_text.as_bytes().to_vec();
279+ /// # let message_in = PublicKeyRsa::try_from(plain_text_bytes)
280+ /// # .expect("Should be possible to create a PublicKeyRsa object from valid bytes.");
281+ /// # let label = Data::default();
282+ /// # let cipher_text = context.rsa_encrypt(key_handle, message_in, scheme, label.clone())
283+ /// # .expect("Should be possible to call rsa_encrypt using valid arguments.");
284+ /// let message_out = context.rsa_decrypt(key_handle, cipher_text, scheme, label)
285+ /// .expect("Should be possible to call rsa_decrypt using valid arguments.");
286+ /// let decrypted_bytes = message_out.as_bytes().to_vec();
287+ /// let decrypted_text = String::from_utf8(decrypted_bytes)
288+ /// .expect("Should be possible to turn the decrypted data into an utf-8 string.");
289+ /// # assert_eq!(plain_text, decrypted_text);
290+ /// ```
46291 pub fn rsa_decrypt (
47292 & mut self ,
48293 key_handle : KeyHandle ,
0 commit comments