forked from seemoo-lab/openhaystack
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This should minimize the overhead and add parallelization to web.
- Loading branch information
1 parent
b17324d
commit 49a4b2a
Showing
5 changed files
with
115 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
use p224::{SecretKey, PublicKey, ecdh::diffie_hellman}; | ||
use rayon::prelude::*; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
const PRIVATE_LEN : usize = 28; | ||
const PUBLIC_LEN : usize = 57; | ||
|
||
pub fn ecdh(public_key_blob : Vec<u8>, private_key : Vec<u8>) -> Vec<u8> { | ||
let num_keys = public_key_blob.len() / PUBLIC_LEN; | ||
let mut vec_shared_secret = vec![0u8; num_keys*PRIVATE_LEN]; | ||
let vec_shared_secret = Arc::new(Mutex::new(vec![0u8; num_keys*PRIVATE_LEN])); | ||
|
||
let private_key = SecretKey::from_slice(&private_key).unwrap(); | ||
let secret_scalar = private_key.to_nonzero_scalar(); | ||
|
||
let mut i = 0; | ||
let mut j = 0; | ||
|
||
for _i in 0..num_keys { | ||
let public_key = PublicKey::from_sec1_bytes(&public_key_blob[i..i+PUBLIC_LEN]).unwrap(); | ||
(0..num_keys).into_par_iter().for_each(|i| { | ||
let start = i * PUBLIC_LEN; | ||
let end = start + PUBLIC_LEN; | ||
let public_key = PublicKey::from_sec1_bytes(&public_key_blob[start..end]).unwrap(); | ||
let public_affine = public_key.as_affine(); | ||
let shared_secret = diffie_hellman(secret_scalar, public_affine); | ||
|
||
let shared_secret = diffie_hellman(secret_scalar, public_affine); | ||
let shared_secret_ref = shared_secret.raw_secret_bytes().as_ref(); | ||
|
||
let start = i * PRIVATE_LEN; | ||
let end = start + PRIVATE_LEN; | ||
|
||
vec_shared_secret[j..j+PRIVATE_LEN].copy_from_slice(shared_secret_ref); | ||
let mut vec_shared_secret = vec_shared_secret.lock().unwrap(); | ||
vec_shared_secret[start..end].copy_from_slice(shared_secret_ref); | ||
}); | ||
|
||
i += PUBLIC_LEN; | ||
j += PRIVATE_LEN; | ||
} | ||
|
||
return vec_shared_secret; | ||
Arc::try_unwrap(vec_shared_secret).unwrap().into_inner().unwrap() | ||
} |