MCPcopy Create free account
hub / github.com/evilsocket/cake / authenticate_as_worker

Function authenticate_as_worker

cake-core/src/cake/sharding/auth.rs:88–118  ·  view source on GitHub ↗

Worker side of the mutual authentication. Called on the accepted TCP stream before reading any Cake protocol messages.

(stream: &mut S, key: &str)

Source from the content-addressed store, hash-verified

86///
87/// Called on the accepted TCP stream before reading any Cake protocol messages.
88pub async fn authenticate_as_worker<S>(stream: &mut S, key: &str) -> Result<()>
89where
90 S: AsyncReadExt + AsyncWriteExt + Unpin,
91{
92 let key_bytes = key.as_bytes();
93
94 // Step 1: read master's nonce
95 let mut master_nonce = [0u8; NONCE_SIZE];
96 stream.read_exact(&mut master_nonce).await?;
97
98 // Step 2: send HMAC response + our nonce in one write
99 let worker_hmac = compute_hmac(key_bytes, &master_nonce);
100 let worker_nonce = random_nonce();
101 let mut response = [0u8; HMAC_SIZE + NONCE_SIZE];
102 response[..HMAC_SIZE].copy_from_slice(&worker_hmac);
103 response[HMAC_SIZE..].copy_from_slice(&worker_nonce);
104 stream.write_all(&response).await?;
105 stream.flush().await?;
106
107 // Step 3: read master's HMAC response
108 let mut master_hmac = [0u8; HMAC_SIZE];
109 stream.read_exact(&mut master_hmac).await?;
110
111 // Step 4: verify master's HMAC
112 let expected = compute_hmac(key_bytes, &worker_nonce);
113 if !constant_time_eq(&master_hmac, &expected) {
114 return Err(anyhow!("master authentication failed: invalid HMAC"));
115 }
116
117 Ok(())
118}

Callers 4

handle_master_clientMethod · 0.85
handle_oneMethod · 0.85
run_mock_workerFunction · 0.85

Calls 3

compute_hmacFunction · 0.85
random_nonceFunction · 0.85
constant_time_eqFunction · 0.85

Tested by 2

handle_oneMethod · 0.68
run_mock_workerFunction · 0.68