Skip to content

Commit c7cca84

Browse files
committed
WIP: Address review comments - 1
In a separate commit for better visibility. Removed addressed TODOs, reworded those that should stay for later (magic numbers), rebased to the latest "main". Signed-off-by: alt3r 3go <alt3r.3go@proton.me>
1 parent c360dce commit c7cca84

File tree

8 files changed

+36
-52
lines changed

8 files changed

+36
-52
lines changed

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ default-mechanisms = [
9191
"tdes",
9292
"totp",
9393
"trng",
94-
"rsa2k-pkcs",
95-
"rsa3k",
96-
"rsa4k"
94+
"rsa2k"
9795
]
9896
aes256-cbc = []
9997
chacha8-poly1305 = []
@@ -108,7 +106,7 @@ sha256 = []
108106
tdes = ["des"]
109107
totp = ["sha-1"]
110108
trng = ["sha-1"]
111-
rsa2k-pkcs = ["rsa"]
109+
rsa2k = ["rsa"]
112110
rsa3k = ["rsa"]
113111
rsa4k = ["rsa"]
114112

src/client/mechanisms.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub trait P256: CryptoClient {
245245
}
246246
}
247247

248-
#[cfg(feature = "rsa2k-pkcs")]
248+
#[cfg(feature = "rsa2k")]
249249
impl<S: Syscall> Rsa2kPkcs for ClientImplementation<S> {}
250250

251251
pub trait Rsa2kPkcs: CryptoClient {

src/config.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,21 @@ cfg_if::cfg_if! {
4444
}
4545
pub const MAX_SHORT_DATA_LENGTH: usize = 128;
4646

47-
// TODO:alt3r-3go: Do we want better keylength granularity here?
48-
#[cfg(any(feature = "rsa2k-pkcs", feature = "rsa3k", feature = "rsa4k"))]
47+
#[cfg(any(feature = "rsa2k", feature = "rsa3k", feature = "rsa4k"))]
4948
pub const MAX_SIGNATURE_LENGTH: usize = 512;
50-
#[cfg(any(feature = "rsa2k-pkcs", feature = "rsa3k", feature = "rsa4k"))]
51-
// TODO:alt3r-3go: We use PKCS#8 DER format, this value was found empirically for 2K keys. Need to generalize.
49+
#[cfg(any(feature = "rsa2k", feature = "rsa3k", feature = "rsa4k"))]
50+
// TODO: We use PKCS#8 DER format, this value was found empirically for 2K keys. Need to generalize.
5251
pub const MAX_KEY_MATERIAL_LENGTH: usize = 1217;
53-
#[cfg(any(feature = "rsa2k-pkcs", feature = "rsa3k", feature = "rsa4k"))]
54-
// TODO:alt3r-3go: This is due to the fact that KEY_MATERIAL_LENGTH is now bigger than MESSAGE_LENGTH.
55-
// Double-check this is okay.
52+
#[cfg(any(feature = "rsa2k", feature = "rsa3k", feature = "rsa4k"))]
53+
// This is due to the fact that KEY_MATERIAL_LENGTH is bigger than MESSAGE_LENGTH for RSA.
5654
pub const MAX_MESSAGE_LENGTH: usize = MAX_KEY_MATERIAL_LENGTH;
5755

5856

59-
#[cfg(not(any(feature = "rsa2k-pkcs", feature = "rsa3k", feature = "rsa4k")))]
57+
#[cfg(not(any(feature = "rsa2k", feature = "rsa3k", feature = "rsa4k")))]
6058
pub const MAX_SIGNATURE_LENGTH: usize = 72;
61-
#[cfg(not(any(feature = "rsa2k-pkcs", feature = "rsa3k", feature = "rsa4k")))]
59+
#[cfg(not(any(feature = "rsa2k", feature = "rsa3k", feature = "rsa4k")))]
6260
pub const MAX_KEY_MATERIAL_LENGTH: usize = 128;
63-
#[cfg(not(any(feature = "rsa2k-pkcs", feature = "rsa3k", feature = "rsa4k")))]
61+
#[cfg(not(any(feature = "rsa2k", feature = "rsa3k", feature = "rsa4k")))]
6462
pub const MAX_MESSAGE_LENGTH: usize = 1024;
6563

6664
// must be MAX_KEY_MATERIAL_LENGTH + 4

src/mechanisms.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pub struct P256Prehashed {}
4747
mod p256;
4848

4949
pub struct Rsa2kPkcs {}
50-
mod rsa2kpkcs;
50+
// Later on we'll add: "pub struct Rsa2kPss {}" and so on
51+
mod rsa2k;
5152

5253
pub struct Sha256 {}
5354
mod sha256;
Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ use crate::error::Error;
1212
use crate::service::*;
1313
use crate::types::*;
1414

15-
//TODO:alt3r-3go: sign() and verify() are the only two methods that are actually different between -pkcs and -pss.
16-
// Moreover, the key::Kind::Rsa2K could also probably be parametrized, instead of having a dedicated kind
17-
// for each. Overall this means the class structure can probably be simplified - need to decide.
18-
19-
#[cfg(feature = "rsa2k-pkcs")]
15+
#[cfg(feature = "rsa2k")]
2016
impl DeriveKey for super::Rsa2kPkcs
2117
{
2218
#[inline(never)]
@@ -55,7 +51,7 @@ impl DeriveKey for super::Rsa2kPkcs
5551
}
5652
}
5753

58-
#[cfg(feature = "rsa2k-pkcs")]
54+
#[cfg(feature = "rsa2k")]
5955
impl DeserializeKey for super::Rsa2kPkcs
6056
{
6157
#[inline(never)]
@@ -73,7 +69,7 @@ impl DeserializeKey for super::Rsa2kPkcs
7369
let private_key: RsaPrivateKey = DecodePrivateKey::from_pkcs8_der(&request.serialized_key)
7470
.map_err(|_| Error::InvalidSerializedKey)?;
7571

76-
// We store our keys in PKCS#8 DER format as well
72+
// We store our keys in PKCS#8 DER format
7773
let private_key_der = private_key.to_pkcs8_der()
7874
.expect("Failed to serialize an RSA 2K private key to PKCS#8 DER");
7975

@@ -88,7 +84,7 @@ impl DeserializeKey for super::Rsa2kPkcs
8884
}
8985
}
9086

91-
#[cfg(feature = "rsa2k-pkcs")]
87+
#[cfg(feature = "rsa2k")]
9288
impl GenerateKey for super::Rsa2kPkcs
9389
{
9490
#[inline(never)]
@@ -125,7 +121,7 @@ impl GenerateKey for super::Rsa2kPkcs
125121
}
126122
}
127123

128-
#[cfg(feature = "rsa2k-pkcs")]
124+
#[cfg(feature = "rsa2k")]
129125
impl SerializeKey for super::Rsa2kPkcs
130126
{
131127
#[inline(never)]
@@ -140,8 +136,6 @@ impl SerializeKey for super::Rsa2kPkcs
140136
.material;
141137

142138
let serialized_key = match request.format {
143-
// TODO:alt3r-3go: There are "Der" and "Asn1Der" commented out in KeySerialization enum,
144-
// should those be used instead?
145139
KeySerialization::Raw => {
146140
let mut serialized_key = Message::new();
147141
serialized_key.extend_from_slice(&priv_key_der).map_err(|_| Error::InternalError)?;
@@ -155,7 +149,7 @@ impl SerializeKey for super::Rsa2kPkcs
155149
}
156150
}
157151

158-
#[cfg(feature = "rsa2k-pkcs")]
152+
#[cfg(feature = "rsa2k")]
159153
impl Exists for super::Rsa2kPkcs
160154
{
161155
#[inline(never)]
@@ -169,7 +163,7 @@ impl Exists for super::Rsa2kPkcs
169163
}
170164
}
171165

172-
#[cfg(feature = "rsa2k-pkcs")]
166+
#[cfg(feature = "rsa2k")]
173167
impl Sign for super::Rsa2kPkcs
174168
{
175169
#[inline(never)]
@@ -188,12 +182,9 @@ impl Sign for super::Rsa2kPkcs
188182
.expect("Failed to deserialize an RSA 2K private key from PKCS#8 DER");
189183

190184
// RSA lib takes in a hash value to sign, not raw data.
191-
// TODO:alt3r-3go: Do we assume we get digest into this function, or we calculate it ourselves?
192-
// use sha2::digest::Digest;
193-
// let digest_to_sign: [u8; 32] = sha2::Sha256::digest(&request.message).into();
185+
// We assume we get digest into this function, too.
194186

195-
// TODO:alt3r-3go: There's also .sign_blinded(), which is supposed to protect the private key from timing side channels,
196-
// but requires an RNG instance - decide if we want to always use it.
187+
// TODO: Consider using .sign_blinded(), which is supposed to protect the private key from timing side channels
197188
use rsa::padding::PaddingScheme;
198189
use rsa::hash::Hash;
199190
let native_signature = priv_key
@@ -211,7 +202,7 @@ impl Sign for super::Rsa2kPkcs
211202
}
212203
}
213204

214-
#[cfg(feature = "rsa2k-pkcs")]
205+
#[cfg(feature = "rsa2k")]
215206
impl Verify for super::Rsa2kPkcs
216207
{
217208
#[inline(never)]
@@ -223,7 +214,7 @@ impl Verify for super::Rsa2kPkcs
223214
return Err(Error::InvalidSerializationFormat);
224215
}
225216

226-
// TODO:alt3r-3go: This must not be a hardcoded magic number, need to generalize
217+
// TODO: This must not be a hardcoded magic number, convert when a common mechanism is available
227218
if request.signature.len() != 256 {
228219
return Err(Error::WrongSignatureLength);
229220
}
@@ -250,11 +241,11 @@ impl Verify for super::Rsa2kPkcs
250241
}
251242
}
252243

253-
#[cfg(not(feature = "rsa2k-pkcs"))]
244+
#[cfg(not(feature = "rsa2k"))]
254245
impl DeriveKey for super::Rsa2kPkcs {}
255-
#[cfg(not(feature = "rsa2k-pkcs"))]
246+
#[cfg(not(feature = "rsa2k"))]
256247
impl GenerateKey for super::Rsa2kPkcs {}
257-
#[cfg(not(feature = "rsa2k-pkcs"))]
248+
#[cfg(not(feature = "rsa2k"))]
258249
impl Sign for super::Rsa2kPkcs {}
259-
#[cfg(not(feature = "rsa2k-pkcs"))]
250+
#[cfg(not(feature = "rsa2k"))]
260251
impl Verify for super::Rsa2kPkcs {}

src/store/keystore.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,9 @@ impl<P: Platform> Keystore for ClientKeystore<P> {
147147

148148
let location = self.location(secrecy, id).ok_or(Error::NoSuchKey)?;
149149

150-
//TODO:alt3r-3go: This should better be defined in some way, instead of hardcoding.
151-
// I've tried referring to MAX_SERIALIZED_KEY_LENGTH, is this a good idea?
152-
#[cfg(not(feature = "rsa2k-pkcs"))]
150+
#[cfg(not(feature = "rsa2k"))]
153151
let bytes: Bytes<128> = store::read(self.store, location, &path)?;
154-
#[cfg(feature = "rsa2k-pkcs")]
152+
#[cfg(feature = "rsa2k")]
155153
let bytes: Bytes<MAX_SERIALIZED_KEY_LENGTH> = store::read(self.store, location, &path)?;
156154

157155
let key = key::Key::try_deserialize(&bytes)?;

src/types.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,11 @@ pub enum Mechanism {
509509
Trng,
510510
X255,
511511
Rsa2kPkcs,
512-
Rsa3k,
513-
Rsa4k,
512+
Rsa2kPss,
513+
Rsa3kPkcs,
514+
Rsa3kPss,
515+
Rsa4kPkcs,
516+
Rsa4kPss,
514517
}
515518

516519
pub type LongData = Bytes<MAX_LONG_DATA_LENGTH>;

tests/rsa2kpkcs.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,14 @@ use trussed::types::KeySerialization;
99
use trussed::types::Location::*;
1010
use trussed::types::StorageAttributes;
1111

12-
// TODO:alt3r-3go: Looks like the test infra is not supposed to be used with several tests like below -
13-
// right now it randomly fails with SIGSERV on either of the two, when run together,
14-
// but never when run separately. Need to investigate and fix.
12+
// Tests below can be run on a PC using the "virt" feature
1513

1614
#[test]
1715
fn rsa2kpkcs_generate_key() {
1816
client::get(|client| {
1917
let sk = syscall!(client.generate_rsa2kpkcs_private_key(Internal)).key;
2018

2119
// This assumes we don't ever get a key with ID 0
22-
// TODO:alt3r-3go: make sure the above always holds or find a better way to check for success
2320
assert_ne!(sk, KeyId::from_special(0));
2421
})
2522
}
@@ -31,7 +28,6 @@ fn rsa2kpkcs_derive_key() {
3128
let pk = syscall!(client.derive_rsa2kpkcs_public_key(sk, Volatile)).key;
3229

3330
// This assumes we don't ever get a key with ID 0
34-
// TODO:alt3r-3go: make sure the above always holds or find a better way to check for success
3531
assert_ne!(pk, KeyId::from_special(0));
3632
})
3733
}
@@ -67,7 +63,6 @@ fn rsa2kpkcs_deserialize_key() {
6763
let deserialized_key_id = syscall!(client.deserialize_rsa2kpkcs_key(&serialized_key, KeySerialization::Raw, location)).key;
6864

6965
// This assumes we don't ever get a key with ID 0
70-
// TODO:alt3r-3go: make sure the above always holds or find a better way to check for success
7166
assert_ne!(deserialized_key_id, KeyId::from_special(0));
7267
})
7368
}

0 commit comments

Comments
 (0)