(&self, nonce: &[u8], encrypted: &[u8])
| 213 | } |
| 214 | |
| 215 | pub fn decrypt(&self, nonce: &[u8], encrypted: &[u8]) -> Result<Vec<u8>, Error> { |
| 216 | let encrypted_len = encrypted.len(); |
| 217 | ensure!( |
| 218 | encrypted_len >= crypto_box_curve25519xchacha20poly1305_MACBYTES as usize, |
| 219 | "Unable to decrypt" |
| 220 | ); |
| 221 | let mut decrypted = |
| 222 | vec![0u8; encrypted_len - crypto_box_curve25519xchacha20poly1305_MACBYTES as usize]; |
| 223 | let res = unsafe { |
| 224 | libsodium_sys::crypto_box_curve25519xchacha20poly1305_open_easy_afternm( |
| 225 | decrypted.as_mut_ptr(), |
| 226 | encrypted.as_ptr(), |
| 227 | encrypted_len as _, |
| 228 | nonce.as_ptr(), |
| 229 | self.0.as_ptr(), |
| 230 | ) |
| 231 | }; |
| 232 | ensure!(res == 0, "Unable to decrypt"); |
| 233 | let idx = decrypted |
| 234 | .iter() |
| 235 | .rposition(|x| *x != 0x00) |
| 236 | .ok_or_else(|| anyhow!("Padding error"))?; |
| 237 | ensure!(decrypted[idx] == 0x80, "Padding error"); |
| 238 | decrypted.truncate(idx); |
| 239 | Ok(decrypted) |
| 240 | } |
| 241 | |
| 242 | pub fn encrypt_into( |
| 243 | &self, |
no outgoing calls
no test coverage detected