diff options
Diffstat (limited to 'src/shamir/secret.rs')
| -rw-r--r-- | src/shamir/secret.rs | 199 |
1 files changed, 160 insertions, 39 deletions
diff --git a/src/shamir/secret.rs b/src/shamir/secret.rs index d2e8887..db2c84f 100644 --- a/src/shamir/secret.rs +++ b/src/shamir/secret.rs @@ -1,49 +1,115 @@ -//! Implementation of SLIP-0039 secret encryption. +//! SLIP-0039 secret splitting and restoring. -use hmac::Hmac; +use super::ff; +use crate::rand; +use anyhow::{ensure, Result}; +use hmac::{Hmac, Mac as _}; use sha2::Sha256; -use std::iter; -/// Encrypts a secret using a 4-round Feistel cypher. -pub fn encrypt(s: &[u8], p: &[u8], e: u32, id: i16) -> Vec<u8> { - feistel(s, p, e, id, [0, 1, 2, 3]) -} +/// Lagrange interpolation of a polynomial at `x` described by the specified +/// points. +fn interpolate(ps: &[(u8, &[u8])], x: u8, y: &mut [u8]) { + assert_lengths(y.len(), ps.iter().map(|(_, yi)| *yi)); -/// Decrypts a secret using a 4-round Feistel cypher. -pub fn decrypt(es: &[u8], p: &[u8], e: u32, id: i16) -> Vec<u8> { - feistel(es, p, e, id, [3, 2, 1, 0]) + let ps = || ps.iter().copied().enumerate(); + for (i, (xi, yi)) in ps() { + let mut pi = 1; + for (j, (xj, _)) in ps().filter(|(j, _)| *j != i) { + debug_assert!(xi != xj, "duplicate point"); + pi = ff::mul(pi, ff::div(ff::sub(x, xj), ff::sub(xi, xj))); + } + for (yk, yik) in y.iter_mut().zip(yi) { + *yk = ff::add(*yk, ff::mul(*yik, pi)); + } + } } -fn feistel(m: &[u8], p: &[u8], e: u32, id: i16, instances: [u8; 4]) -> Vec<u8> { - let n = m.len() / 2; - debug_assert!(n * 2 == m.len(), "secret must be even number of bytes"); +/// Splits a secret into `n` shares, with a threshold recovery `t`. Returns a +/// `Vec` with `n` elements representing each of the shares. +pub fn split(t: usize, n: usize, s: &[u8]) -> Vec<Vec<u8>> { + let m = s.len(); + debug_assert!( + 0 < t && t <= n && n <= 16 && m >= 16 && m % 2 == 0, + "invalid shamir parameters", + ); + + if t == 1 { + return (0..n).map(|_| s.to_vec()).collect(); + } + + let d = { + let mut buf = vec![0; m]; + + let r = &mut buf[4..]; + rand::fill(r); + + let code = hmac_sha256(r, s); + buf[..4].copy_from_slice(&code[..4]); + + buf + }; - let (l, r) = m.split_at(n); - let mut output = [r, l].concat(); - let (mut r, mut l) = output.split_at_mut(n); + let mut ys = vec![vec![0; m]; n]; + let (ysr, ysi) = ys.split_at_mut(t - 2); - let mut key = [&[0], p].concat(); - let mut salt = iter::empty() - .chain(*b"shamir") - .chain(id.to_be_bytes()) - .chain(iter::repeat(0).take(n)) + for y in ysr.iter_mut() { + rand::fill(y.as_mut()); + } + + let ps = ysr + .iter() + .enumerate() + .map(|(x, y)| (x as u8, &y[..])) + .chain([(254, &d[..]), (255, s)]) .collect::<Vec<_>>(); - let rounds = 2500 << e; - let mut buf = vec![0; n]; - - for i in instances { - key[0] = i; - salt[8..].copy_from_slice(r); - pbkdf2::pbkdf2::<Hmac<Sha256>>(&key, &salt, rounds, &mut buf) - .expect("HMAC can be initialized with any key length"); - for (l, b) in l.iter_mut().zip(&buf) { - *l ^= *b; - } + for (i, y) in ysi.iter_mut().enumerate() { + let x = i + t - 2; + interpolate(&ps, x as _, y); + } - (l, r) = (r, l); + ys +} + +/// Recovers a secret from a list of shares for a threshold `t`. +pub fn recover(ps: &[(u8, &[u8])]) -> Result<Vec<u8>> { + let t = ps.len(); + let m = ps.first().map(|(_, y)| y.len()).unwrap_or_default(); + assert_lengths(m, ps.iter().map(|(_, y)| *y)); + debug_assert!( + 0 < t && t <= 16 && m >= 16 && m % 2 == 0, + "invalid shamir parameters" + ); + + if t == 1 { + return Ok(ps[0].1.to_vec()); } - output + let (mut s, mut d) = (vec![0; m], vec![0; m]); + + interpolate(ps, 255, &mut s); + interpolate(ps, 254, &mut d); + + let r = &d[4..]; + let code = hmac_sha256(r, &s[..]); + + ensure!(d[..4] == code[..4], "secret checksum mismatch"); + Ok(s) +} + +fn hmac_sha256(r: &[u8], s: &[u8]) -> [u8; 32] { + Hmac::<Sha256>::new_from_slice(r) + .expect("HMAC should accept arbitrary key sizes") + .chain_update(&s) + .finalize() + .into_bytes() + .into() +} + +fn assert_lengths<'a>(m: usize, mut ys: impl Iterator<Item = &'a [u8]>) { + debug_assert!( + ys.all(|y| y.len() == m), + "secret and shares have different lengths", + ); } #[cfg(test)] @@ -51,10 +117,65 @@ mod tests { use super::*; #[test] - fn feistel_ecryption() { - let secret = b"hello shamir"; - let enc = encrypt(secret, b"password", 0, 0x1337); - let dec = decrypt(&enc, b"password", 0, 0x1337); - assert_eq!(secret[..], dec); + fn lagrange_interpolation() { + let mut y = [0, 0]; + interpolate(&[(1, &[118, 56]), (2, &[146, 14])], 0, &mut y); + + assert_eq!(y, [42, 42]); + } + + #[test] + fn split_and_recover() { + let secret = { + let mut buf = [0; 32]; + for (i, b) in buf.iter_mut().enumerate() { + *b = (i + 1) as u8; + } + buf + }; + + let shares = split(3, 5, &secret); + + // Insufficient shares + assert!(recover(&[(0, &shares[0]), (1, &shares[1])]).is_err()); + + // Exact amount of shares + for i in 0..5 { + for j in 0..5 { + for k in 0..5 { + if i == j || i == k || j == k { + continue; + } + + let exact = recover(&[ + (i as _, &shares[i]), + (j as _, &shares[j]), + (k as _, &shares[k]), + ]) + .unwrap(); + assert_eq!(secret[..], exact); + } + } + } + + // Extra shares + let extra = recover(&[ + (0, &shares[0]), + (1, &shares[1]), + (2, &shares[2]), + (3, &shares[3]), + (4, &shares[4]), + ]) + .unwrap(); + assert_eq!(secret[..], extra); + } + + #[test] + fn very_large_secret() { + let secret = [42; 8192]; + let shares = split(2, 2, &secret); + let recovered = recover(&[(0, &shares[0]), (1, &shares[1])]).unwrap(); + + assert_eq!(&secret[..], recovered); } } |
