Untitled

 avatar
unknown
rust
2 months ago
2.9 kB
8
Indexable
pub fn new_filler(hops_data: &Vec<Hop>,hop_keys: HashMap<Hop,KeyForHop>) -> Vec<u8> {
    let mut filler_size: usize=0;
    for hop in hops_data.iter().take(hops_data.len() -1) {
        filler_size +=hop.payload.len();
    }

    let mut filler = vec![0u8; filler_size];

    for i in 0..(hops_data.len()) {
        let mut filler_start = ONION_PAYLOAD_LENGTH;

        for p in &hops_data[..i]  {
            filler_start -= p.payload.len();
        }

        let filler_end = ONION_PAYLOAD_LENGTH + hops_data[i].payload.len();

        if let Some(data) = hop_keys.get(&hops_data[i]) {
            println!("{}",hex::encode(&data.mu));
            let mut stream_key =data.rho.clone();
            let mut stream_bytes =produce_cipher_steam(&stream_key, ONION_PAYLOAD_LENGTH*2);
            let filler_copy =filler.clone();
            xor(&mut filler, &filler_copy, &stream_bytes[filler_start..filler_end]);

        }
    }

    return filler;

}

fn xor(dst: &mut [u8], a: &[u8], b: &[u8]) -> usize {
    let n = a.len().min(b.len());
    for i in 0..n {
        dst[i] = a[i] ^ b[i];
    }
    n
}

fn right_shift(slice: &mut [u8], num: usize) {
    // Shift elements to the right
    for i in (0..slice.len() - num).rev() {
        slice[num + i] = slice[i];
    }

    // Fill the first num elements with zeros
    for i in 0..num {
        slice[i] = 0;
    }
}

pub fn build_onion(keys: Input, hop_keys: HashMap<Hop,KeyForHop>,hops: Vec<Hop>) -> Vec<u8> {
   let num_hops = hops.len();
   //let mut final_packet = Vec::new();
   

   let mut allhmacs = Vec::new();

     let filler = new_filler(&hops, hop_keys.clone());

     let initial_padkey = get_pad_key(&keys.session_key);

     let mut mixheader = produce_cipher_steam(&initial_padkey, ONION_PAYLOAD_LENGTH);
     let mut nexthmac =vec![0u8; HMAC_SIZE];

     for i in (0..num_hops).rev() {
        if let Some(hop_data) = hop_keys.get(&hops[i]) {
            let rho_key = hop_data.rho.clone();
            let mu_key = hop_data.mu.clone();
            allhmacs.push(nexthmac.clone());

            let stream_bytes =produce_cipher_steam(&rho_key, ONION_PAYLOAD_LENGTH);
            let paylaod = hops[i].payload.clone();
            let shiftsize = paylaod.len();
            right_shift(&mut mixheader[..], shiftsize);

            let mixh = mixheader.clone();

            xor(&mut mixheader[..], &mixh[..], &stream_bytes[..]);

            if  i == 0 {
             
                    let start_idx = mixheader.len() - filler.len();
                    mixheader[start_idx..].copy_from_slice(&filler);
        
            }
            let mut packet = mixheader.to_vec();
             packet.extend_from_slice(&keys.associated_data);
             nexthmac = calculate_hmac(&mu_key, &packet,);
        }

         
     }

    // println!("{:?}",allhmacs);
     //println!("{:?}",final_packet);

    
     return mixheader;

}

Editor is loading...
Leave a Comment