tock / elf2tab

Tool to create Tock Application Bundles from ELF files.
MIT License
15 stars 33 forks source link

Fix clippy warnings #59

Closed lhvy closed 1 year ago

lhvy commented 2 years ago

General purpose PR to fix many clippy warnings encountered whilst working on another issue. Changes should be very minimal but code review and extra testing would be appreciated as always! There are 3-4 warnings which I have not fixed as they would require changes I was not comfortable making without input:


I'm participating in Hacktoberfest 2022 and if this PR is approved it, it would be greatly appreciated if the PR could be labelled hacktoberfest-accepted, or even the repository labelled as hacktoberfest to allow PRs in this repository to count towards the competition!

lhvy commented 2 years ago

The final warning I am receiving is around manually copying slices. I was working on a fix and noticed a potential optimisation, but I could be incorrectly understanding how this section of the code works as I haven't worked with this codebase much.

@@ -829,7 +829,7 @@ pub fn elf_to_tbf(
         if key_pair.public_modulus_len() != 512 {
            // A 4096-bit key should have a 512-byte modulus
            panic!(
                "RSA4096 signature requested but key {:?} is not 4096 bits, it is {} bits",
                private_key_path,
                private_key_der.len() * 8
             );
         }
         let rng = rand::SystemRandom::new();
-        let mut signature = vec![0; key_pair.public_modulus_len()];
+        let mut signature = vec![0; 512];
         let _res = key_pair
             .sign(
                 &signature::RSA_PKCS1_SHA512,
                &rng,
                &output[0..tbfheader.binary_end_offset() as usize],
                &mut signature,
            )
            .map_err(|e| {
                 panic!("Could not generate RSA4096 signature: {:?}", e);
             });
         let mut credentials = vec![0; 1024];
-        for i in 0..key_pair.public_modulus_len() {
-            credentials[i] = public_modulus[i];
-        }
-        for i in 0..signature.len() {
-            let index = i + key_pair.public_modulus_len();
-            credentials[index] = signature[i];
-        }
+        credentials[..512].copy_from_slice(&public_modulus);
+        credentials[512..].copy_from_slice(&signature);

         let rsa4096_credentials = header::TbfFooterCredentials {
             base: header::TbfHeaderTlv {

It is to my understanding that the length of public_modulus is 512, and signature is created with the same length, so I can assume they are both 512 and will fit perfectly into credential which is of length 1024. The only thing confusing me is the difference between key_pair.public_modulus_len() and public_modulus. Can I assume that these are the same and both of length 512?

lhvy commented 2 years ago

Just had an attempt at clippy CI in e2dde85. No luck though if the CI is being run on a fork, it fails because it doesn't have access to add annotations to the commit on my fork. Commit has been force-pushed away and someone can pick up the changes if they want to.

bradjc commented 1 year ago

We didn't get this to and there are significant changes, updated version in #71.