Skip to content

Commit d0fd8df

Browse files
committed
hil: pub key: add metadata to select key done
This allows for the layer that handles selecting keys to provide an opaque usize metadata that can later be used as part of appid. The idea is this: the key selector might not want to use index as the metadata. For example, keys stored in flash or in some hardware chip might be set in any order, and instead might have some other way to assig metadata. This allows the key selection layer to decide. For in_memory_keys, we just use the index because the array of keys is well known.
1 parent c7ac3a4 commit d0fd8df

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

capsules/extra/src/signature_verify_in_memory_keys.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ impl<
186186
}
187187

188188
self.client_key_select.map(|client| {
189-
client.select_key_done(self.active_key.get().unwrap_or(0), error);
189+
let key_index = self.active_key.get().unwrap_or(0);
190+
client.select_key_done(key_index, key_index, error);
190191
});
191192
}
192193
}

capsules/system/src/process_checker/signature.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct AppCheckerSignature<
3939
credential_type: TbfFooterV2CredentialsType,
4040
credentials: OptionalCell<TbfFooterV2Credentials>,
4141
binary: OptionalCell<&'static [u8]>,
42-
active_key_index: Cell<(usize, usize)>,
42+
active_key_index: Cell<(usize, usize, usize)>,
4343
}
4444

4545
impl<
@@ -67,7 +67,7 @@ impl<
6767
credential_type,
6868
credentials: OptionalCell::empty(),
6969
binary: OptionalCell::empty(),
70-
active_key_index: Cell::new((0, 0)),
70+
active_key_index: Cell::new((0, 0, 0)),
7171
}
7272
}
7373

@@ -154,7 +154,7 @@ impl<
154154
fn get_key_count_done(&self, count: usize) {
155155
// We have the hash, we know how many keys, now we need to select the
156156
// first key to check. Activate the first key.
157-
self.active_key_index.set((0, count));
157+
self.active_key_index.set((0, 0, count));
158158
if let Err(_e) = self.verifier.select_key(0) {
159159
self.client.map(|c| {
160160
let binary = self.binary.take().unwrap();
@@ -164,7 +164,7 @@ impl<
164164
}
165165
}
166166

167-
fn select_key_done(&self, _index: usize, error: Result<(), ErrorCode>) {
167+
fn select_key_done(&self, _index: usize, metadata: usize, error: Result<(), ErrorCode>) {
168168
match error {
169169
Err(e) => {
170170
// Could not switch to the requested key.
@@ -174,7 +174,14 @@ impl<
174174
c.check_done(Err(e), cred, binary)
175175
});
176176
}
177-
Ok(()) => self.do_verify(),
177+
Ok(()) => {
178+
// Save the metadata for the newly selected key.
179+
let (current_key, _, number_keys) = self.active_key_index.get();
180+
self.active_key_index
181+
.set((current_key, metadata, number_keys));
182+
183+
self.do_verify()
184+
}
178185
}
179186
}
180187
}
@@ -257,7 +264,7 @@ impl<
257264
self.hash.replace(hash);
258265
self.signature.replace(signature);
259266

260-
let (current_key, number_keys) = self.active_key_index.get();
267+
let (current_key, key_metadata, number_keys) = self.active_key_index.get();
261268

262269
// Check if the verification was successful. If so, we can issue the
263270
// callback.
@@ -268,7 +275,7 @@ impl<
268275
c.check_done(
269276
Ok(CheckResult::Accept(Some(
270277
kernel::process_checker::CheckResultAcceptMetadata {
271-
metadata: current_key,
278+
metadata: key_metadata,
272279
},
273280
))),
274281
cred,
@@ -290,7 +297,7 @@ impl<
290297
});
291298
} else {
292299
// Activate the next key.
293-
self.active_key_index.set((next_key, number_keys));
300+
self.active_key_index.set((next_key, 0, number_keys));
294301
if self.verifier.select_key(next_key).is_err() {
295302
self.client.map(|c| {
296303
let binary = self.binary.take().unwrap();

kernel/src/hil/public_key_crypto/keys.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,21 @@ pub trait SelectKeyClient {
292292
/// Called when the specified key is active and ready to use for the next
293293
/// cryptographic operation.
294294
///
295+
/// ### Arguments
296+
///
297+
/// - `index`: The index of the key that was selected.
298+
/// - `metadata`: An opaque usize of metadata associated with the key. This
299+
/// can be used with
300+
/// [`CheckResultAcceptMetadata`](crate::process_checker::CheckResultAcceptMetadata)
301+
/// to assign metadata with the accepted credential.
302+
///
295303
/// ### `error`:
296304
///
297305
/// - `Ok(())`: The key was selected successfully.
298306
/// - `Err(())`: The key was selected set successfully.
299307
/// - `ErrorCode::INVAL`: The index was not valid.
300308
/// - `ErrorCode::FAIL`: The key could not be set.
301-
fn select_key_done(&self, index: usize, error: Result<(), ErrorCode>);
309+
fn select_key_done(&self, index: usize, metadata: usize, error: Result<(), ErrorCode>);
302310
}
303311

304312
/// Interface for selecting an active key among the number of available keys.

0 commit comments

Comments
 (0)