MCPcopy Create free account
hub / github.com/Dstack-TEE/dstack / dh_decrypt

Function dh_decrypt

dstack-util/src/crypto.rs:19–42  ·  view source on GitHub ↗
(secret: [u8; 32], ciphertext: &[u8])

Source from the content-addressed store, hash-verified

17}
18
19pub fn dh_decrypt(secret: [u8; 32], ciphertext: &[u8]) -> Result<Vec<u8>> {
20 // Extract components (matching JS implementation)
21 let ephemeral_pubkey = ciphertext
22 .get(..32)
23 .ok_or(anyhow!("Invalid ephemeral public key length"))?
24 .try_into()
25 .map_err(|_| anyhow!("Invalid ephemeral public key length"))?;
26 let iv = &ciphertext.get(32..44).ok_or(anyhow!("Invalid IV length"))?;
27 let ciphertext = &ciphertext
28 .get(44..)
29 .ok_or(anyhow!("Invalid ciphertext length"))?;
30
31 // Derive shared secret using X25519
32 let shared_secret = dh_agree(secret, ephemeral_pubkey);
33
34 // Create AES-GCM cipher
35 let cipher = Aes256Gcm::new_from_slice(&shared_secret)
36 .map_err(|e| anyhow!("Failed to create cipher: {}", e))?;
37
38 // Decrypt using AES-GCM
39 cipher
40 .decrypt(Nonce::<Aes256Gcm>::from_slice(iv), ciphertext.as_ref())
41 .map_err(|e| anyhow!("Decryption failed: {}", e))
42}
43
44#[cfg(test)]
45mod tests {

Callers 2

test_dh_decryptFunction · 0.85
decrypt_env_varsMethod · 0.85

Calls 3

dh_agreeFunction · 0.85
getMethod · 0.45
as_refMethod · 0.45

Tested by 1

test_dh_decryptFunction · 0.68