Start with challenge 44 by parsing text file

This commit is contained in:
2023-01-02 15:07:28 -05:00
parent e3ab3af101
commit ec5a19f54e
3 changed files with 75 additions and 6 deletions

View File

@@ -4,6 +4,7 @@
#![allow(clippy::items_after_statements)]
#![allow(clippy::many_single_char_names)]
#![allow(clippy::module_name_repetitions)]
#![feature(string_remove_matches)]
mod bytes;
mod bytes_base64;
mod cbc;
@@ -70,7 +71,7 @@ fn main() {
set5::challenge40().unwrap_or_else(|| println!("[fail] challenge 40"));
set6::challenge41().unwrap_or_else(|| println!("[fail] challenge 41"));
set6::challenge42().unwrap_or_else(|| println!("[fail] challenge 42"));
set6::challenge43().unwrap_or_else(|| println!("[fail] challenge 43"));
}
set6::challenge43().unwrap_or_else(|| println!("[fail] challenge 43"));
set6::challenge44().unwrap_or_else(|| println!("[fail] challenge 44"));
}

View File

@@ -117,12 +117,11 @@ pub mod challenge43 {
BigNum::from_hex_str("d2d0714f014a9784047eaeccf956520045c45265")?
);
// They provide s and r as decimal integers and not hex strings. I
// converted the decimals to hex. I took me a couple of hours to figure
// that out.
// They provide s and r as decimal integers and not hex strings. Took me a while
// to notice that.
let mut sig = dsa::DsaSig {
r: BigNum::from_hex_str("60019cacdc56eedf8e080984bfa898c8c5c419a8")?,
s: BigNum::from_hex_str("961f2062efc3c68db965a90c924cf76580ec1bbc")?,
r: BigNum::from_dec_str("548099063082341131477253921760299949438196259240")?,
s: BigNum::from_dec_str("857042759984254168557880549501802188789837994940")?,
k: BigNum::from_u32(0)?,
};
let msg_h = dsa::h(&msg)?;
@@ -173,6 +172,30 @@ pub fn challenge43() -> Option<()> {
Some(())
}
pub mod challenge44 {
use std::io::{BufReader, BufRead};
use crate::bytes::Bytes;
use openssl::bn::BigNum;
use crate::dsa;
pub fn read_dsa_signed_messages() {
let file = std::fs::File::open("data/44.txt").unwrap();
let mut lines: Vec<String> = BufReader::new(file).lines().map(|l| l.unwrap()).collect();
// each message cosists of four lines: msg, s, r, m (sha1 hash of msg)
for line in lines.chunks_mut(4) {
line[0].remove_matches("msg: ");
line[1].remove_matches("s: ");
line[2].remove_matches("r: ");
line[3].remove_matches("m: ");
let msg = Bytes::from_utf8(&line[0]);
let m = BigNum::from_hex_str(&line[3]).unwrap();
assert_eq!(dsa::h(&msg).unwrap(), m, "Message hash from data/44.txt does not match");
}
}
}
pub fn challenge44() -> Option<()> {
println!("[xxxx] Challenge 44: DSA nonce recovery from repeated nonce");
let msg = Bytes::from_utf8("hello, world!");
@@ -181,5 +204,6 @@ pub fn challenge44() -> Option<()> {
let sig = keys.sign(&params, &msg).ok()?;
let result = keys.verify(&params, &msg, &sig).ok()?;
assert!(result, "verify failed unexpectedly");
challenge44::read_dsa_signed_messages();
None
}