Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virt: adapt to trussed-dev/littlefs2#24 #12

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
2909643
Update postcard dependency
sosthene-nitrokey Oct 6, 2022
e64e1f1
Update aes dependency and replace block-modes by cbc
sosthene-nitrokey Oct 6, 2022
ccbcf68
Update chacha dependency
sosthene-nitrokey Oct 6, 2022
31957a5
Update des dependency
sosthene-nitrokey Oct 6, 2022
142d199
Update blake2 dependency
sosthene-nitrokey Oct 6, 2022
0f82285
Update hmac and sha dependencies
sosthene-nitrokey Oct 6, 2022
eb7ef0e
Update serial_test
sosthene-nitrokey Oct 6, 2022
6aa1b26
Update p256 dependency
sosthene-nitrokey Oct 6, 2022
df1d890
WIP: RSA lib integration - part 1 (very first steps, incomplete)
alt3r-3go Nov 1, 2021
195a2f6
WIP: RSA lib integration - part 2 (more plumbing, still incomplete)
alt3r-3go Nov 21, 2021
6c389cd
WIP: RSA lib integration - part 3 (RSA2K GenerateKey+test)
alt3r-3go Nov 28, 2021
5756f27
WIP: RSA lib integration - part 4 (DeriveKey+test)
alt3r-3go Dec 12, 2021
22478ef
WIP: RSA lib integration - part 5 (Exists/{De}SerializeKey+tests)
alt3r-3go Dec 19, 2021
485e780
WIP: RSA lib integration - part 6 (Sign/Verify+tests)
alt3r-3go Jan 23, 2022
1dfe0b6
WIP: remove unneeded code, mark my TODOs for simpler discovery
alt3r-3go Jul 11, 2022
56c9c71
WIP: Rebase to current trussed/main, cleanup
alt3r-3go Jul 17, 2022
5cd3b6d
WIP: update RSA traits after updating to 0.6.0
alt3r-3go Jul 17, 2022
fcdf318
WIP: Address review comments - 1
alt3r-3go Jul 31, 2022
0ae49ce
Fix test with latest main
sosthene-nitrokey Oct 6, 2022
9266f6f
RSA: Serialiaze only public keys with E and N serialization formats
sosthene-nitrokey Oct 7, 2022
dd7ec62
Add logging
sosthene-nitrokey Oct 7, 2022
70f1576
Fix max key material length
sosthene-nitrokey Oct 7, 2022
400df2d
Add decryption support
sosthene-nitrokey Oct 10, 2022
de72d3e
Deserialize public key rather than private keys
sosthene-nitrokey Oct 10, 2022
183ac33
Use None as the hash
sosthene-nitrokey Oct 10, 2022
70decec
Fix max serialized key length
sosthene-nitrokey Oct 10, 2022
4afba61
Add Pkcs8Der serialzation format
sosthene-nitrokey Oct 12, 2022
c46a766
Fix rsa deserialization secrecy
sosthene-nitrokey Oct 12, 2022
7f23c4a
Rename rsa with full bit size
sosthene-nitrokey Oct 14, 2022
5292134
Remove RSA std dependency
sosthene-nitrokey Oct 14, 2022
d8a5cae
Restore old Message length
sosthene-nitrokey Nov 14, 2022
4042206
WIP: Add RSA key import
sosthene-nitrokey Oct 13, 2022
9d9dd18
Add rsa import support
sosthene-nitrokey Oct 14, 2022
4bc3394
Remove the requirement for N in rsa key import
sosthene-nitrokey Oct 14, 2022
45d57e7
Improve naming
sosthene-nitrokey Oct 14, 2022
3ce8260
Fix RSA capitalisation in logs
sosthene-nitrokey Oct 14, 2022
b585d9b
Remove RSA from default mechanism
sosthene-nitrokey Nov 16, 2022
dec0247
Add support for RSA 4096 bits
sosthene-nitrokey Nov 17, 2022
4fd0c06
Add tests for RSA key import
sosthene-nitrokey Nov 17, 2022
fa7610c
Fix bad import
sosthene-nitrokey Nov 17, 2022
0a4a269
Fix key length
sosthene-nitrokey Nov 17, 2022
65a83bc
Revert "Update postcard dependency"
sosthene-nitrokey Nov 30, 2022
ccdbf99
Relax key kind check in derive_key for hmacsha256
robin-nitrokey Dec 1, 2022
eb4060e
Drop firmwares check from CI
robin-nitrokey Dec 14, 2022
e0d6a62
Fix aes256cbc test
robin-nitrokey Dec 14, 2022
eceb048
Merge pull request #7 from Nitrokey/rsa-import
robin-nitrokey Dec 14, 2022
0ee7691
Patch littlefs2 dependency
robin-nitrokey Dec 15, 2022
c643432
Merge pull request #8 from Nitrokey/littlefs2
robin-nitrokey Dec 15, 2022
238b7b2
Merge remote-tracking branch 'upstream/main'
robin-nitrokey Dec 15, 2022
85d78dc
virt: adapt to trussed-dev/littlefs2#24
daringer Feb 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions .github/workflows/firmwares.yml

This file was deleted.

39 changes: 27 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ postcard = "0.7.0"
rand_core = "0.6"
serde = { version = "1.0", default-features = false }
zeroize = { version = "1.2", default-features = false, features = ["zeroize_derive"] }
rand_chacha = { version = "0.3.1", default-features = false }

# RustCrypto
aes = { version = "0.7", default-features = false }
blake2 = { version = "0.9", default-features = false, optional = true }
block-modes = { version = "0.8", default-features = false }
chacha20 = { version = "0.7", default-features = false, features = ["rng"] }
chacha20poly1305 = { version = "0.8", default-features = false, features = ["heapless", "reduced-round"] }
des = { version = "0.7", optional = true }
hmac = "0.11"
sha-1 = { version = "0.9", default-features = false, optional = true }
sha2 = { version = "0.9", default-features = false }
aes = { version = "0.8", default-features = false }
cbc = "0.1.2"
blake2 = { version = "0.10", default-features = false, optional = true }
chacha20 = { version = "0.9", default-features = false }
chacha20poly1305 = { version = "0.10", default-features = false, features = ["heapless", "reduced-round"] }
des = { version = "0.8", optional = true }
hmac = "0.12"
sha-1 = { version = "0.10", default-features = false, optional = true }
sha2 = { version = "0.10", default-features = false }
rsa = { version = "0.6.0", optional = true , default-features = false }
num-bigint-dig = { version = "0.8.1", optional = true , default-features = false}

# ours
cosey = "0.3"
Expand All @@ -43,13 +46,13 @@ cbor-smol = "0.4"
heapless-bytes = { version = "0.3.0", features = ["cbor"] }
interchange = "0.2.1"
littlefs2 = "0.3.1"
p256-cortex-m4 = { version = "0.1.0-alpha.5", features = ["prehash", "sec1-signatures"] }
p256-cortex-m4 = { version = "0.1.0-alpha.6", features = ["prehash", "sec1-signatures"] }
salty = { version = "0.2.0", features = ["cose"] }
serde-indexed = "0.1.0"

[dev-dependencies]
# Testing
serial_test = { version = "0.6" }
serial_test = { version = "0.9" }
entropy = "0.4.0"
once_cell = "1.13.0"
# Somehow, this is causing a regression.
Expand Down Expand Up @@ -101,6 +104,10 @@ sha256 = []
tdes = ["des"]
totp = ["sha-1"]
trng = ["sha-1"]
rsa2048 = ["rsa", "alloc", "num-bigint-dig"]
rsa3072 = ["rsa", "alloc", "num-bigint-dig"]
rsa4096 = ["rsa", "alloc", "num-bigint-dig"]
alloc = []

clients-1 = []
clients-2 = []
Expand All @@ -116,9 +123,17 @@ clients-11 = []
clients-12 = []

test-attestation-cert-ids = []
# [patch.crates-io]

[patch.crates-io]
# interchange = { git = "https://github.com/trussed-dev/interchange", branch = "main" }
littlefs2 = { git = "https://github.com/Nitrokey/littlefs2", tag = "v0.3.2-nitrokey-1" }

[package.metadata.docs.rs]
features = ["virt"]
rustdoc-args = ["--cfg", "docsrs"]

[profile.dev.package.rsa]
opt-level = 2

[profile.dev.package.num-bigint-dig]
opt-level = 2
6 changes: 3 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub mod request {

DeserializeKey:
- mechanism: Mechanism
- serialized_key: Message
- serialized_key: SerializedKey
- format: KeySerialization
- attributes: StorageAttributes

Expand Down Expand Up @@ -275,7 +275,7 @@ pub mod request {

UnsafeInjectKey:
- mechanism: Mechanism // -> implies key type
- raw_key: ShortData
- raw_key: SerializedKey
- attributes: StorageAttributes
- format: KeySerialization

Expand Down Expand Up @@ -436,7 +436,7 @@ pub mod reply {
- bytes: Message

SerializeKey:
- serialized_key: Message
- serialized_key: SerializedKey

Sign:
- signature: Signature
Expand Down
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub trait CryptoClient: PollClient {
attributes: StorageAttributes,
) -> ClientResult<'c, reply::DeserializeKey, Self> {
let serialized_key =
Message::from_slice(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
SerializedKey::from_slice(serialized_key).map_err(|_| ClientError::DataTooLarge)?;
let r = self.request(request::DeserializeKey {
mechanism,
serialized_key,
Expand Down Expand Up @@ -527,7 +527,7 @@ pub trait CryptoClient: PollClient {
) -> ClientResult<'_, reply::UnsafeInjectKey, Self> {
let r = self.request(request::UnsafeInjectKey {
mechanism,
raw_key: ShortData::from_slice(raw_key).unwrap(),
raw_key: SerializedKey::from_slice(raw_key).unwrap(),
attributes: StorageAttributes::new().set_persistence(persistence),
format,
})?;
Expand Down
146 changes: 146 additions & 0 deletions src/client/mechanisms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,152 @@ pub trait P256: CryptoClient {
}
}

#[cfg(feature = "rsa2048")]
impl<S: Syscall> Rsa2048Pkcs for ClientImplementation<S> {}

pub trait Rsa2048Pkcs: CryptoClient {
fn generate_rsa2048pkcs_private_key(
&mut self,
persistence: Location,
) -> ClientResult<'_, reply::GenerateKey, Self> {
self.generate_key(
Mechanism::Rsa2048Pkcs,
StorageAttributes::new().set_persistence(persistence),
)
}

fn derive_rsa2048pkcs_public_key(
&mut self,
shared_key: KeyId,
persistence: Location,
) -> ClientResult<'_, reply::DeriveKey, Self> {
self.derive_key(
Mechanism::Rsa2048Pkcs,
shared_key,
None,
StorageAttributes::new().set_persistence(persistence),
)
}

fn serialize_rsa2048pkcs_key(
&mut self,
key: KeyId,
format: KeySerialization,
) -> ClientResult<'_, reply::SerializeKey, Self> {
self.serialize_key(Mechanism::Rsa2048Pkcs, key, format)
}

fn deserialize_rsa2048pkcs_key<'c>(
&'c mut self,
serialized_key: &[u8],
format: KeySerialization,
attributes: StorageAttributes,
) -> ClientResult<'c, reply::DeserializeKey, Self> {
self.deserialize_key(Mechanism::Rsa2048Pkcs, serialized_key, format, attributes)
}

fn sign_rsa2048pkcs<'c>(
&'c mut self,
key: KeyId,
message: &[u8],
) -> ClientResult<'c, reply::Sign, Self> {
self.sign(
Mechanism::Rsa2048Pkcs,
key,
message,
SignatureSerialization::Raw,
)
}

fn verify_rsa2048pkcs<'c>(
&'c mut self,
key: KeyId,
message: &[u8],
signature: &[u8],
) -> ClientResult<'c, reply::Verify, Self> {
self.verify(
Mechanism::Rsa2048Pkcs,
key,
message,
signature,
SignatureSerialization::Raw,
)
}
}

#[cfg(feature = "rsa4096")]
impl<S: Syscall> Rsa4096Pkcs for ClientImplementation<S> {}

pub trait Rsa4096Pkcs: CryptoClient {
fn generate_rsa4096pkcs_private_key(
&mut self,
persistence: Location,
) -> ClientResult<'_, reply::GenerateKey, Self> {
self.generate_key(
Mechanism::Rsa4096Pkcs,
StorageAttributes::new().set_persistence(persistence),
)
}

fn derive_rsa4096pkcs_public_key(
&mut self,
shared_key: KeyId,
persistence: Location,
) -> ClientResult<'_, reply::DeriveKey, Self> {
self.derive_key(
Mechanism::Rsa4096Pkcs,
shared_key,
None,
StorageAttributes::new().set_persistence(persistence),
)
}

fn serialize_rsa4096pkcs_key(
&mut self,
key: KeyId,
format: KeySerialization,
) -> ClientResult<'_, reply::SerializeKey, Self> {
self.serialize_key(Mechanism::Rsa4096Pkcs, key, format)
}

fn deserialize_rsa4096pkcs_key<'c>(
&'c mut self,
serialized_key: &[u8],
format: KeySerialization,
attributes: StorageAttributes,
) -> ClientResult<'c, reply::DeserializeKey, Self> {
self.deserialize_key(Mechanism::Rsa4096Pkcs, serialized_key, format, attributes)
}

fn sign_rsa4096pkcs<'c>(
&'c mut self,
key: KeyId,
message: &[u8],
) -> ClientResult<'c, reply::Sign, Self> {
self.sign(
Mechanism::Rsa4096Pkcs,
key,
message,
SignatureSerialization::Raw,
)
}

fn verify_rsa4096pkcs<'c>(
&'c mut self,
key: KeyId,
message: &[u8],
signature: &[u8],
) -> ClientResult<'c, reply::Verify, Self> {
self.verify(
Mechanism::Rsa4096Pkcs,
key,
message,
signature,
SignatureSerialization::Raw,
)
}
}

#[cfg(feature = "sha256")]
impl<S: Syscall> Sha256 for ClientImplementation<S> {}

Expand Down
23 changes: 20 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ use littlefs2::consts;

pub type MAX_APPLICATION_NAME_LENGTH = consts::U256;
pub const MAX_LONG_DATA_LENGTH: usize = 1024;
pub const MAX_MESSAGE_LENGTH: usize = 1024;
pub type MAX_OBJECT_HANDLES = consts::U16;
pub type MAX_LABEL_LENGTH = consts::U256;
pub const MAX_MEDIUM_DATA_LENGTH: usize = 256;
pub type MAX_PATH_LENGTH = consts::U256;
pub const MAX_KEY_MATERIAL_LENGTH: usize = 128;
//pub const MAX_KEY_MATERIAL_LENGTH: usize = 128;
// must be above + 4
pub const MAX_SERIALIZED_KEY_LENGTH: usize = 132;
//pub const MAX_SERIALIZED_KEY_LENGTH: usize = 132;
cfg_if::cfg_if! {
if #[cfg(feature = "clients-12")] {
pub type MAX_SERVICE_CLIENTS = consts::U12;
Expand Down Expand Up @@ -44,7 +43,25 @@ cfg_if::cfg_if! {
}
}
pub const MAX_SHORT_DATA_LENGTH: usize = 128;

#[cfg(any(feature = "rsa2048", feature = "rsa3072", feature = "rsa4096"))]
pub const MAX_SIGNATURE_LENGTH: usize = 512 * 2;
#[cfg(any(feature = "rsa2048", feature = "rsa3072", feature = "rsa4096"))]
// FIXME: Value from https://stackoverflow.com/questions/5403808/private-key-length-bytes for Rsa2048 Private key
pub const MAX_KEY_MATERIAL_LENGTH: usize = 1160 * 2 + 72;
#[cfg(any(feature = "rsa2048", feature = "rsa3072", feature = "rsa4096"))]
// This is due to the fact that KEY_MATERIAL_LENGTH is bigger than MESSAGE_LENGTH for RSA.
pub const MAX_MESSAGE_LENGTH: usize = 1024;

#[cfg(not(any(feature = "rsa2048", feature = "rsa3072", feature = "rsa4096")))]
pub const MAX_SIGNATURE_LENGTH: usize = 72;
#[cfg(not(any(feature = "rsa2048", feature = "rsa3072", feature = "rsa4096")))]
pub const MAX_KEY_MATERIAL_LENGTH: usize = 128;
#[cfg(not(any(feature = "rsa2048", feature = "rsa3072", feature = "rsa4096")))]
pub const MAX_MESSAGE_LENGTH: usize = 1024;

// must be MAX_KEY_MATERIAL_LENGTH + 4
pub const MAX_SERIALIZED_KEY_LENGTH: usize = MAX_KEY_MATERIAL_LENGTH + 4;
pub const MAX_USER_ATTRIBUTE_LENGTH: usize = 256;

pub const USER_ATTRIBUTE_NUMBER: u8 = 37;
Loading