diff options
author | Josh Brown <josh9051@gmail.com> | 2022-01-22 09:38:33 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-22 07:38:33 -0700 |
commit | 6d963225520f0d8e5948457b8ba25bd563382f5e (patch) | |
tree | 12c05900061a93c74242d37f5dc1935977c9bd8c /srp/src/utils.rs | |
parent | 689dc0ab6af950b027b4bab96f73c427d2c42d6e (diff) | |
download | PAKEs-6d963225520f0d8e5948457b8ba25bd563382f5e.tar.xz |
srp: rebuild library (#79)
Complete rewrite of the SRP library.
Includes many improvements over the old library:
- Improved file and code organization
- Access to individual SRP computations
- Consistent sever and client API
- Simpler API
- Improved documentation with tests in documentation
- New tests for compatibility with the RFC
- Bumps dependencies
- Timing safe verification comparisons
- Modernized error handling
Diffstat (limited to 'srp/src/utils.rs')
-rw-r--r-- | srp/src/utils.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/srp/src/utils.rs b/srp/src/utils.rs new file mode 100644 index 0000000..a9372bd --- /dev/null +++ b/srp/src/utils.rs @@ -0,0 +1,45 @@ +use digest::{Digest, Output}; +use num_bigint::BigUint; + +use crate::types::SrpGroup; + +// u = H(PAD(A) | PAD(B)) +pub fn compute_u<D: Digest>(a_pub: &[u8], b_pub: &[u8]) -> BigUint { + let mut u = D::new(); + u.update(a_pub); + u.update(b_pub); + BigUint::from_bytes_be(&u.finalize()) +} + +// k = H(N | PAD(g)) +pub fn compute_k<D: Digest>(params: &SrpGroup) -> BigUint { + let n = params.n.to_bytes_be(); + let g_bytes = params.g.to_bytes_be(); + let mut buf = vec![0u8; n.len()]; + let l = n.len() - g_bytes.len(); + buf[l..].copy_from_slice(&g_bytes); + + let mut d = D::new(); + d.update(&n); + d.update(&buf); + BigUint::from_bytes_be(d.finalize().as_slice()) +} + +// M1 = H(A, B, K) this doesn't follow the spec but apparently no one does for M1 +// M1 should equal = H(H(N) XOR H(g) | H(U) | s | A | B | K) according to the spec +pub fn compute_m1<D: Digest>(a_pub: &[u8], b_pub: &[u8], key: &[u8]) -> Output<D> { + let mut d = D::new(); + d.update(a_pub); + d.update(b_pub); + d.update(key); + d.finalize() +} + +// M2 = H(A, M1, K) +pub fn compute_m2<D: Digest>(a_pub: &[u8], m1: &Output<D>, key: &[u8]) -> Output<D> { + let mut d = D::new(); + d.update(&a_pub); + d.update(&m1); + d.update(&key); + d.finalize() +} |