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

44
data/44.txt Normal file
View File

@@ -0,0 +1,44 @@
msg: Listen for me, you better listen for me now.
s: 1267396447369736888040262262183731677867615804316
r: 1105520928110492191417703162650245113664610474875
m: a4db3de27e2db3e5ef085ced2bced91b82e0df19
msg: Listen for me, you better listen for me now.
s: 29097472083055673620219739525237952924429516683
r: 51241962016175933742870323080382366896234169532
m: a4db3de27e2db3e5ef085ced2bced91b82e0df19
msg: When me rockin' the microphone me rock on steady,
s: 277954141006005142760672187124679727147013405915
r: 228998983350752111397582948403934722619745721541
m: 21194f72fe39a80c9c20689b8cf6ce9b0e7e52d4
msg: Yes a Daddy me Snow me are de article dan.
s: 1013310051748123261520038320957902085950122277350
r: 1099349585689717635654222811555852075108857446485
m: 1d7aaaa05d2dee2f7dabdc6fa70b6ddab9c051c5
msg: But in a in an' a out de dance em
s: 203941148183364719753516612269608665183595279549
r: 425320991325990345751346113277224109611205133736
m: 6bc188db6e9e6c7d796f7fdd7fa411776d7a9ff
msg: Aye say where you come from a,
s: 502033987625712840101435170279955665681605114553
r: 486260321619055468276539425880393574698069264007
m: 5ff4d4e8be2f8aae8a5bfaabf7408bd7628f43c9
msg: People em say ya come from Jamaica,
s: 1133410958677785175751131958546453870649059955513
r: 537050122560927032962561247064393639163940220795
m: 7d9abd18bbecdaa93650ecc4da1b9fcae911412
msg: But me born an' raised in the ghetto that I want yas to know,
s: 559339368782867010304266546527989050544914568162
r: 826843595826780327326695197394862356805575316699
m: 88b9e184393408b133efef59fcef85576d69e249
msg: Pure black people mon is all I mon know.
s: 1021643638653719618255840562522049391608552714967
r: 1105520928110492191417703162650245113664610474875
m: d22804c4899b522b23eda34d2137cd8cc22b9ce8
msg: Yeah me shoes a an tear up an' now me toes is a show a
s: 506591325247687166499867321330657300306462367256
r: 51241962016175933742870323080382366896234169532
m: bc7ec371d951977cba10381da08fe934dea80314
msg: Where me a born in are de one Toronto, so
s: 458429062067186207052865988429747640462282138703
r: 228998983350752111397582948403934722619745721541
m: d6340bfcda59b6b75b59ca634813d572de800e8f

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
}