@@ -13,6 +13,65 @@ use log::error;
1313use std:: ptr:: null_mut;
1414
1515impl Context {
16+ /// Starts HMAC sequence of large data (larger than MaxBuffer::MAX_SIZE) using the specified algorithm.
17+ ///
18+ /// # Details
19+ /// When the amount of data to be included in a digest cannot be sent to the TPM in one atomic HMAC
20+ /// command then a sequence of commands may be used to provide incremental updates to the digest.
21+ /// Follow the pattern:
22+ /// - Initialize sequence with `hmac_sequence_start()`
23+ /// - Send data to calculate the hash with `sequence_update()`
24+ /// - Finish hash calculation with call to `sequence_complete()`
25+ ///
26+ /// # Example
27+ ///
28+ /// ```rust
29+ /// # Create context with session.
30+ /// # let mut context = create_ctx_with_session();
31+ ///
32+ /// let object_attributes = ObjectAttributesBuilder::new()
33+ /// .with_sign_encrypt(true)
34+ /// .with_sensitive_data_origin(true)
35+ /// .with_user_with_auth(true)
36+ /// .build()
37+ /// .expect("Failed to build object attributes");
38+ ///
39+ /// let key_pub = PublicBuilder::new()
40+ /// .with_public_algorithm(PublicAlgorithm::KeyedHash)
41+ /// .with_name_hashing_algorithm(HashingAlgorithm::Sha256)
42+ /// .with_object_attributes(object_attributes)
43+ /// .with_keyed_hash_parameters(PublicKeyedHashParameters::new(
44+ /// KeyedHashScheme::HMAC_SHA_256,
45+ /// ))
46+ /// .with_keyed_hash_unique_identifier(Default::default())
47+ /// .build()
48+ /// .expect("Failed to build public structure for key.");
49+ ///
50+ /// let key = context
51+ /// .create_primary(Hierarchy::Owner, key_pub, None, None, None, None)
52+ /// .unwrap();
53+ ///
54+ /// let data = [0xEE; 5000];
55+ ///
56+ /// let handle = context
57+ /// .hmac_sequence_start(key.key_handle.into(), HashingAlgorithm::Sha256, None)
58+ /// .unwrap();
59+ ///
60+ /// let chunks = data.chunks_exact(MaxBuffer::MAX_SIZE);
61+ /// let last_chunk = chunks.remainder();
62+ /// for chunk in chunks {
63+ /// context
64+ /// .sequence_update(handle, MaxBuffer::from_bytes(&chunk).unwrap())
65+ /// .unwrap();
66+ /// }
67+ /// let (actual_hashed_data, ticket) = context
68+ /// .sequence_complete(
69+ /// handle,
70+ /// MaxBuffer::from_bytes(&last_chunk).unwrap(),
71+ /// Hierarchy::Null,
72+ /// )
73+ /// .unwrap();
74+ /// ```
1675 pub fn hmac_sequence_start (
1776 & mut self ,
1877 handle : ObjectHandle ,
@@ -45,6 +104,43 @@ impl Context {
45104
46105 // Missing function: MAC_Start
47106
107+ /// Starts hash sequence of large data (larger than MaxBuffer::MAX_SIZE) using the specified algorithm.
108+ ///
109+ /// # Details
110+ /// When the amount of data to be included in a digest cannot be sent to the TPM in one atomic hash
111+ /// command then a sequence of commands may be used to provide incremental updates to the digest.
112+ /// Follow the pattern:
113+ /// - Initialize sequence with `hash_sequence_start()`
114+ /// - Send data to calculate the hash with `sequence_update()`
115+ /// - Finish hash calculation with call to `sequence_complete()`
116+ ///
117+ /// # Example
118+ ///
119+ /// ```rust
120+ /// # Create context with session.
121+ /// # let mut context = create_ctx_with_session();
122+ ///
123+ /// let data = [0xEE; 2*1025];
124+ ///
125+ /// let handle = context
126+ /// .hash_sequence_start(HashingAlgorithm::Sha256, None)
127+ /// .unwrap();
128+ ///
129+ /// let chunks = data.chunks_exact(MaxBuffer::MAX_SIZE);
130+ /// let last_chung = chunks.remainder();
131+ /// for chunk in chunks {
132+ /// context
133+ /// .sequence_update(handle, MaxBuffer::from_bytes(&chunk).unwrap())
134+ /// .unwrap();
135+ /// }
136+ /// let (actual_hashed_data, ticket) = context
137+ /// .sequence_complete(
138+ /// handle,
139+ /// MaxBuffer::from_bytes(&last_chung).unwrap(),
140+ /// expected_hierarchy,
141+ /// )
142+ /// .unwrap();
143+ /// ```
48144 pub fn hash_sequence_start (
49145 & mut self ,
50146 hashing_algorithm : HashingAlgorithm ,
@@ -73,6 +169,9 @@ impl Context {
73169 Ok ( ObjectHandle :: from ( sequence_handle) )
74170 }
75171
172+ /// Continues hash or HMAC sequence.
173+ ///
174+ /// @see hash_sequence_start(), hmac_sequence_start()
76175 pub fn sequence_update (
77176 & mut self ,
78177 sequence_handle : ObjectHandle ,
@@ -98,6 +197,9 @@ impl Context {
98197 )
99198 }
100199
200+ /// Finishes hash or HMAC sequence.
201+ ///
202+ /// @see hash_sequence_start(), hmac_sequence_start()
101203 pub fn sequence_complete (
102204 & mut self ,
103205 sequence_handle : ObjectHandle ,
0 commit comments