|
| 1 | +#![no_main] |
| 2 | +#![no_std] |
| 3 | + |
| 4 | +/// |
| 5 | +/// Compare also with: https://github.com/Ko-/aes-armcortexm |
| 6 | +/// |
| 7 | +// extern crate panic_halt; |
| 8 | +extern crate panic_semihosting; |
| 9 | +use cortex_m_rt::entry; |
| 10 | + |
| 11 | +#[allow(unused_imports)] |
| 12 | +use hal::prelude::*; |
| 13 | +#[allow(unused_imports)] |
| 14 | +use lpc55_hal as hal; |
| 15 | + |
| 16 | +use salty::{Keypair, Sha512}; |
| 17 | + |
| 18 | +use cortex_m_semihosting::hprintln; |
| 19 | + |
| 20 | +fn test_ed25519ph_with_rfc_8032_test_vector() { |
| 21 | + let seed: [u8; 32] = [ |
| 22 | + 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, |
| 23 | + 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e, |
| 24 | + 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b, |
| 25 | + 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, |
| 26 | + ]; |
| 27 | + |
| 28 | + let keypair = Keypair::from(&seed); |
| 29 | + |
| 30 | + let message: [u8; 3] = [0x61, 0x62, 0x63]; |
| 31 | + |
| 32 | + let prehashed_message = Sha512::new().updated(&message).finalize(); |
| 33 | + |
| 34 | + let mut signature: salty::Signature = salty::Signature::default(); |
| 35 | + for _ in 1..=3 { |
| 36 | + let cycles_before = hal::get_cycle_count(); |
| 37 | + signature = keypair.sign_prehashed(&prehashed_message, None); |
| 38 | + let cycles_after = hal::get_cycle_count(); |
| 39 | + hprintln!("signing took {} cycles", cycles_after - cycles_before).ok(); |
| 40 | + } |
| 41 | + |
| 42 | + let expected_r = [ |
| 43 | + 0x98, 0xa7, 0x02, 0x22, 0xf0, 0xb8, 0x12, 0x1a, |
| 44 | + 0xa9, 0xd3, 0x0f, 0x81, 0x3d, 0x68, 0x3f, 0x80, |
| 45 | + 0x9e, 0x46, 0x2b, 0x46, 0x9c, 0x7f, 0xf8, 0x76, |
| 46 | + 0x39, 0x49, 0x9b, 0xb9, 0x4e, 0x6d, 0xae, 0x41, |
| 47 | + ]; |
| 48 | + |
| 49 | + let expected_s = [ |
| 50 | + 0x31, 0xf8, 0x50, 0x42, 0x46, 0x3c, 0x2a, 0x35, |
| 51 | + 0x5a, 0x20, 0x03, 0xd0, 0x62, 0xad, 0xf5, 0xaa, |
| 52 | + 0xa1, 0x0b, 0x8c, 0x61, 0xe6, 0x36, 0x06, 0x2a, |
| 53 | + 0xaa, 0xd1, 0x1c, 0x2a, 0x26, 0x08, 0x34, 0x06, |
| 54 | + ]; |
| 55 | + |
| 56 | + assert_eq!(signature.r.to_bytes(), expected_r); |
| 57 | + assert_eq!(signature.s.to_bytes(), expected_s); |
| 58 | + |
| 59 | + let public_key = keypair.public; |
| 60 | + let cycles_before = hal::get_cycle_count(); |
| 61 | + let verification = public_key.verify_prehashed(&prehashed_message, &signature, None); |
| 62 | + let cycles_after = hal::get_cycle_count(); |
| 63 | + hprintln!("verification took {} cycles", cycles_after - cycles_before).ok(); |
| 64 | + assert!(verification.is_ok()); |
| 65 | +} |
| 66 | + |
| 67 | +impl AsRef<[u8]> for &[u8; 64] |
| 68 | +{ |
| 69 | + #[inline] |
| 70 | + fn as_ref(&self) -> &[u8] { |
| 71 | + self |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[entry] |
| 76 | +fn main() -> ! { |
| 77 | + |
| 78 | + let hal = hal::Peripherals::take().unwrap(); |
| 79 | + |
| 80 | + let mut anactrl = hal.anactrl; |
| 81 | + let mut pmc = hal.pmc; |
| 82 | + let mut syscon = hal.syscon; |
| 83 | + |
| 84 | + |
| 85 | + let seed: [u8; 32] = [ |
| 86 | + 0x83, 0x3f, 0xe6, 0x24, 0x09, 0x23, 0x7b, 0x9d, |
| 87 | + 0x62, 0xec, 0x77, 0x58, 0x75, 0x20, 0x91, 0x1e, |
| 88 | + 0x9a, 0x75, 0x9c, 0xec, 0x1d, 0x19, 0x75, 0x5b, |
| 89 | + 0x7d, 0xa9, 0x01, 0xb9, 0x6d, 0xca, 0x3d, 0x42, |
| 90 | + ]; |
| 91 | + |
| 92 | + let keypair = Keypair::from(&seed); |
| 93 | + let context = b""; |
| 94 | + let message: [u8; 3] = [0x61, 0x62, 0x63]; |
| 95 | + let prehashed_message = &salty::Sha512::new().updated(&message).finalize(); |
| 96 | + // let prehashed_message = &Sha512::new().chain(&message).result(); |
| 97 | + use sha2::{Sha512, Digest}; |
| 98 | + for _ in 1..=20 { |
| 99 | + let cycles_before = hal::get_cycle_count(); |
| 100 | + let first_hash = Sha512::new() |
| 101 | + // Ed25519ph parts |
| 102 | + .chain(b"SigEd25519 no Ed25519 collisions") |
| 103 | + .chain(&[1]) |
| 104 | + // context parts |
| 105 | + .chain(&[context.len() as u8]) |
| 106 | + .chain(context) |
| 107 | + // usual parts |
| 108 | + .chain(&keypair.secret.nonce) |
| 109 | + .chain(&prehashed_message) |
| 110 | + // .finalize(); |
| 111 | + .result() |
| 112 | + ; |
| 113 | + // let first_hash = salty::Sha512::new() |
| 114 | + // // Ed25519ph parts |
| 115 | + // .updated(b"SigEd25519 no Ed25519 collisions") |
| 116 | + // .updated(&[1]) |
| 117 | + // // context parts |
| 118 | + // .updated(&[context.len() as u8]) |
| 119 | + // .updated(context) |
| 120 | + // // usual parts |
| 121 | + // .updated(&keypair.secret.nonce) |
| 122 | + // .updated(prehashed_message) |
| 123 | + // .finalize(); |
| 124 | + // // .result() |
| 125 | + // ; |
| 126 | + let cycles_after = hal::get_cycle_count(); |
| 127 | + hprintln!("first hash took {} cycles", cycles_after - cycles_before).ok(); |
| 128 | + } |
| 129 | + for _ in 1..=20 { |
| 130 | + let cycles_before = hal::get_cycle_count(); |
| 131 | + let first_hash = salty::Sha512::new() |
| 132 | + // Ed25519ph parts |
| 133 | + .updated(b"SigEd25519 no Ed25519 collisions") |
| 134 | + .updated(&[1]) |
| 135 | + // context parts |
| 136 | + // .updated(&[context.len() as u8]) |
| 137 | + .updated(context) |
| 138 | + // usual parts |
| 139 | + .updated(&keypair.secret.nonce) |
| 140 | + .updated(prehashed_message) |
| 141 | + // .finalize(); |
| 142 | + ; |
| 143 | + let cycles_after = hal::get_cycle_count(); |
| 144 | + hprintln!("first hash took {} cycles", cycles_after - cycles_before).ok(); |
| 145 | + } |
| 146 | + |
| 147 | + hal::enable_cycle_counter(); |
| 148 | + let cycles_before = hal::get_cycle_count(); |
| 149 | + hal::ClockRequirements::default() |
| 150 | + .system_frequency(96.mhz()) |
| 151 | + .configure(&mut anactrl, &mut pmc, &mut syscon) |
| 152 | + .unwrap(); |
| 153 | + let cycles_after = hal::get_cycle_count(); |
| 154 | + hprintln!("clocks configured in {} cycles", cycles_after - cycles_before).ok(); |
| 155 | + |
| 156 | + for _ in 1..=3 { |
| 157 | + let cycles_before = hal::get_cycle_count(); |
| 158 | + test_ed25519ph_with_rfc_8032_test_vector(); |
| 159 | + let cycles_after = hal::get_cycle_count(); |
| 160 | + hprintln!("signing and verification took {} cycles", cycles_after - cycles_before).ok(); |
| 161 | + } |
| 162 | + |
| 163 | + hprintln!("DONE").ok(); |
| 164 | + |
| 165 | + loop { continue; } |
| 166 | +} |
0 commit comments