Skip to content

Commit

Permalink
align all the precisions (its ugly)
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jul 23, 2024
1 parent d584735 commit ab0d36e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
9 changes: 6 additions & 3 deletions dsa/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ impl<'a> DecodeValue<'a> for Components {
let q = reader.decode::<UintRef<'_>>()?;
let g = reader.decode::<UintRef<'_>>()?;

let p = BoxedUint::from_be_slice(p.as_bytes(), (p.as_bytes().len() * 8) as u32).unwrap();
let q = BoxedUint::from_be_slice(q.as_bytes(), (q.as_bytes().len() * 8) as u32).unwrap();
let g = BoxedUint::from_be_slice(g.as_bytes(), (g.as_bytes().len() * 8) as u32).unwrap();
// Just use the precision of `p` since `p` will be the largest value and all values need to have the same precision
let precision = (p.as_bytes().len() * 8) as u32;

let p = BoxedUint::from_be_slice(p.as_bytes(), precision).unwrap();
let q = BoxedUint::from_be_slice(q.as_bytes(), precision).unwrap();
let g = BoxedUint::from_be_slice(g.as_bytes(), precision).unwrap();

let p = NonZero::new(p).unwrap();
let q = NonZero::new(q).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions dsa/src/generate/secret_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
let size = (q.bits() / 8) as usize;

// Reduce hash mod q
let hash = BoxedUint::from_be_slice(hash, (hash.len() * 8) as u32).unwrap();
let hash = BoxedUint::from_be_slice(hash, q.bits_precision()).unwrap();
let hash = (hash % q).to_be_bytes();
let hash = strip_leading_zeros(&hash, size);

Expand All @@ -41,7 +41,7 @@ where
loop {
rfc6979::generate_k_mut::<D>(x_bytes, q_bytes, hash, &[], &mut buffer);

let k = BoxedUint::from_be_slice(&buffer, (buffer.len() * 8) as u32).unwrap();
let k = BoxedUint::from_be_slice(&buffer, q.bits_precision()).unwrap();
if let Some(inv_k) = k.inv_mod(q).into() {
if (bool::from(k.is_nonzero())) & (k < **q) {
debug_assert!(bool::from(k.is_odd()));
Expand Down
14 changes: 12 additions & 2 deletions dsa/src/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ impl SigningKey {
let (p, q, g) = (components.p(), components.q(), components.g());
let x = self.x();

// Verify all the precisions check out. Otherwise the math operations will fail
debug_assert_eq!(p.bits_precision(), q.bits_precision());
debug_assert_eq!(q.bits_precision(), g.bits_precision());
debug_assert_eq!(g.bits_precision(), x.bits_precision());
debug_assert_eq!(x.bits_precision(), k.bits_precision());

let params = BoxedMontyParams::new(Odd::new(k).unwrap());
let form = BoxedMontyForm::new((**g).clone(), params);
let r = NonZero::new(form.pow(p).retrieve() % q).unwrap();
Expand Down Expand Up @@ -209,14 +215,18 @@ impl<'a> TryFrom<PrivateKeyInfo<'a>> for SigningKey {
let parameters = value.algorithm.parameters_any()?;
let components = parameters.decode_as::<Components>()?;

// Use the precision of `p`. `p` will always have the largest precision.
// Every operation is mod `p` anyway, so it will always fit.
let precision = components.p().bits_precision();

let x = UintRef::from_der(value.private_key)?;
let x = BoxedUint::from_be_slice(x.as_bytes(), x.as_bytes().len() as u32 * 8).unwrap();
let x = BoxedUint::from_be_slice(x.as_bytes(), precision).unwrap();
let x = NonZero::new(x).unwrap();

let y = if let Some(y_bytes) = value.public_key {
let y = UintRef::from_der(y_bytes)?;
NonZero::new(
BoxedUint::from_be_slice(y.as_bytes(), y.as_bytes().len() as u32 * 8).unwrap(),
BoxedUint::from_be_slice(y.as_bytes(), precision).unwrap(),
)
.unwrap()
} else {
Expand Down
29 changes: 18 additions & 11 deletions dsa/tests/deterministic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ use signature::DigestSigner;
const MESSAGE: &[u8] = b"sample";
const MESSAGE_2: &[u8] = b"test";

fn decode_hex_number(txt: &str) -> BoxedUint {
fn decode_hex_number(txt: &str, precision: u32) -> BoxedUint {
let val = hex::decode(txt).unwrap();
BoxedUint::from_be_slice(&val, val.len() as u32 * 8).unwrap()
BoxedUint::from_be_slice(&val, precision).unwrap()
}

fn dsa_1024_signing_key() -> SigningKey {
let p_str = "86F5CA03DCFEB225063FF830A0C769B9DD9D6153AD91D7CE27F787C43278B447\
E6533B86B18BED6E8A48B784A14C252C5BE0DBF60B86D6385BD2F12FB763ED88\
73ABFD3F5BA2E0A8C0A59082EAC056935E529DAF7C610467899C77ADEDFC846C\
881870B7B19B2B58F9BE0521A17002E3BDD6B86685EE90B3D9A1B02B782B1779";
let p = decode_hex_number(p_str);
let p = decode_hex_number(p_str, 1024);

let q_str = "996F967F6C8E388D9E28D01E205FBA957A5698B1";
let q = decode_hex_number(q_str);
let q = decode_hex_number(q_str, 1024);

let g_str = "07B0F92546150B62514BB771E2A0C0CE387F03BDA6C56B505209FF25FD3C133D\
89BBCD97E904E09114D9A7DEFDEADFC9078EA544D2E401AEECC40BB9FBBF78FD\
87995A10A1C27CB7789B594BA7EFB5C4326A9FE59A070E136DB77175464ADCA4\
17BE5DCE2F40D10A46A3A3943F26AB7FD9C0398FF8C76EE0A56826A8A88F1DBD";
let g = decode_hex_number(g_str);
let g = decode_hex_number(g_str, 1024);

let x_str = "411602CB19A6CCC34494D79D98EF1E7ED5AF25F7";
let x = decode_hex_number(x_str);
let x = decode_hex_number(x_str, 1024);

let y_str = "5DF5E01DED31D0297E274E1691C192FE5868FEF9E19A84776454B100CF16F653\
92195A38B90523E2542EE61871C0440CB87C322FC4B4D2EC5E1E7EC766E1BE8D\
4CE935437DC11C3C8FD426338933EBFE739CB3465F4D3668C5E473508253B1E6\
82F65CBDC4FAE93C2EA212390E54905A86E2223170B44EAA7DA5DD9FFCFB7F3B";
let y = decode_hex_number(y_str);
let y = decode_hex_number(y_str, 1024);

let (p, q, g, y, x) = (
NonZero::new(p).unwrap(),
Expand All @@ -63,8 +63,9 @@ fn dsa_2048_signing_key() -> SigningKey {
F1BF14D4BB4563CA28371621CAD3324B6A2D392145BEBFAC748805236F5CA2FE\
92B871CD8F9C36D3292B5509CA8CAA77A2ADFC7BFD77DDA6F71125A7456FEA15\
3E433256A2261C6A06ED3693797E7995FAD5AABBCFBE3EDA2741E375404AE25B",
2048,
);
let q = decode_hex_number("F2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F");
let q = decode_hex_number("F2C3119374CE76C9356990B465374A17F23F9ED35089BD969F61C6DDE9998C1F", 2048);
let g = decode_hex_number(
"5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E24809670716C613\
D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D1AA58C4328A06C4\
Expand All @@ -74,9 +75,10 @@ fn dsa_2048_signing_key() -> SigningKey {
3A10B1C4D203CC76A470A33AFDCBDD92959859ABD8B56E1725252D78EAC66E71\
BA9AE3F1DD2487199874393CD4D832186800654760E1E34C09E4D155179F9EC0\
DC4473F996BDCE6EED1CABED8B6F116F7AD9CF505DF0F998E34AB27514B0FFE7",
2048,
);

let x = decode_hex_number("69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC");
let x = decode_hex_number("69C7548C21D0DFEA6B9A51C9EAD4E27C33D3B3F180316E5BCAB92C933F0E4DBC", 2048);
let y = decode_hex_number(
"667098C654426C78D7F8201EAC6C203EF030D43605032C2F1FA937E5237DBD94\
9F34A0A2564FE126DC8B715C5141802CE0979C8246463C40E6B6BDAA2513FA61\
Expand All @@ -86,6 +88,7 @@ fn dsa_2048_signing_key() -> SigningKey {
687972A2D382599C9BAC4E0ED7998193078913032558134976410B89D2C171D1\
23AC35FD977219597AA7D15C1A9A428E59194F75C721EBCBCFAE44696A499AFA\
74E04299F132026601638CB87AB79190D4A0986315DA8EEC6561C938996BEADF",
2048,
);

let (p, q, g, y, x) = (
Expand Down Expand Up @@ -129,9 +132,13 @@ where

/// Create a signature container from the two components in their textual hexadecimal form
fn from_str_signature(r: &str, s: &str) -> Signature {
assert_eq!(r.len(), s.len());

let precision = (r.len() * 8) as u32;

Signature::from_components(
NonZero::new(decode_hex_number(r)).unwrap(),
NonZero::new(decode_hex_number(s)).unwrap(),
NonZero::new(decode_hex_number(r, precision)).unwrap(),
NonZero::new(decode_hex_number(s, precision)).unwrap(),
)
}

Expand Down

0 comments on commit ab0d36e

Please # to comment.