Keystore 2.0: add entropy feeder on idle
In AsyncTask, if both the high and low priority job queues are empty
then run any registered idle callbacks once, passing them the shelf
to play with. After this, the idle callbacks will only be called again
when some other job is queued.
Register an idle callback that feeds entropy to all known KeyMint
devices, provided that sufficient time (30s) has elapsed since the
last time entropy was fed.
Bug: 171703867
Test: keystore2_test, subset of CtsKeystoreTestCases with extra logging
Change-Id: Ic21cd1906ee24bb6c050ce17b104d8000c6aed14
diff --git a/keystore2/src/crypto/lib.rs b/keystore2/src/crypto/lib.rs
index 77dab67..bd5906c 100644
--- a/keystore2/src/crypto/lib.rs
+++ b/keystore2/src/crypto/lib.rs
@@ -58,10 +58,15 @@
/// Generate a salt.
pub fn generate_salt() -> Result<Vec<u8>, Error> {
- // Safety: salt has the same length as the requested number of random bytes.
- let mut salt = vec![0; SALT_LENGTH];
- if unsafe { randomBytes(salt.as_mut_ptr(), SALT_LENGTH) } {
- Ok(salt)
+ generate_random_data(SALT_LENGTH)
+}
+
+/// Generate random data of the given size.
+pub fn generate_random_data(size: usize) -> Result<Vec<u8>, Error> {
+ // Safety: data has the same length as the requested number of random bytes.
+ let mut data = vec![0; size];
+ if unsafe { randomBytes(data.as_mut_ptr(), size) } {
+ Ok(data)
} else {
Err(Error::RandomNumberGenerationFailed)
}