|
| 1 | +use pyo3::{ |
| 2 | + pyclass, pymethods, |
| 3 | + types::{PyBytes, PyType}, |
| 4 | + Bound, Py, Python, |
| 5 | +}; |
| 6 | +use vodozemac::Curve25519PublicKey; |
| 7 | + |
| 8 | +use crate::PkEncryptionError; |
| 9 | + |
| 10 | +#[pyclass] |
| 11 | +pub struct Message { |
| 12 | + ciphertext: Vec<u8>, |
| 13 | + mac: Vec<u8>, |
| 14 | + ephemeral_key: Vec<u8>, |
| 15 | +} |
| 16 | + |
| 17 | +/// The decryption component of the PkEncryption support. |
| 18 | +#[pyclass] |
| 19 | +pub struct PkDecryption { |
| 20 | + inner: vodozemac::pk_encryption::PkDecryption, |
| 21 | +} |
| 22 | + |
| 23 | +#[pymethods] |
| 24 | +impl PkDecryption { |
| 25 | + /// Create a new random PkDecryption object. |
| 26 | + #[new] |
| 27 | + fn new() -> Self { |
| 28 | + Self { |
| 29 | + inner: vodozemac::pk_encryption::PkDecryption::new(), |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /// Create a PkDecryption object from the secret key bytes. |
| 34 | + #[classmethod] |
| 35 | + fn from_bytes(_cls: &Bound<'_, PyType>, bytes: &[u8]) -> Result<Self, PkEncryptionError> { |
| 36 | + let key: &[u8; 32] = bytes |
| 37 | + .try_into() |
| 38 | + .map_err(|_| PkEncryptionError::InvalidKeySize(bytes.len()))?; |
| 39 | + |
| 40 | + Ok(Self { |
| 41 | + inner: vodozemac::pk_encryption::PkDecryption::from_bytes(&key), |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + // The secret key used to decrypt messages. |
| 46 | + #[getter] |
| 47 | + pub fn key(&self) -> Py<PyBytes> { |
| 48 | + Python::with_gil(|py| PyBytes::new_bound(py, self.inner.to_bytes().as_slice()).into()) |
| 49 | + } |
| 50 | + |
| 51 | + // The public key used to encrypt messages for this decryption object. |
| 52 | + #[getter] |
| 53 | + pub fn public_key(&self) -> Py<PyBytes> { |
| 54 | + Python::with_gil(|py| PyBytes::new_bound(py, self.inner.public_key().as_bytes()).into()) |
| 55 | + } |
| 56 | + |
| 57 | + /// Decrypt a ciphertext. See the PkEncryption::encrypt function |
| 58 | + /// for descriptions of the ephemeral_key and mac arguments. |
| 59 | + pub fn decrypt(&self, message: &Message) -> Result<Py<PyBytes>, PkEncryptionError> { |
| 60 | + let ephemeral_key_bytes: [u8; 32] = message |
| 61 | + .ephemeral_key |
| 62 | + .as_slice() |
| 63 | + .try_into() |
| 64 | + .map_err(|_| PkEncryptionError::InvalidKeySize(message.ephemeral_key.len()))?; |
| 65 | + |
| 66 | + let message = vodozemac::pk_encryption::Message { |
| 67 | + ciphertext: message.ciphertext.clone(), |
| 68 | + mac: message.mac.clone(), |
| 69 | + ephemeral_key: Curve25519PublicKey::from_bytes(ephemeral_key_bytes), |
| 70 | + }; |
| 71 | + |
| 72 | + self.inner |
| 73 | + .decrypt(&message) |
| 74 | + .map(|vec| Python::with_gil(|py| PyBytes::new_bound(py, vec.as_slice()).into())) |
| 75 | + .map_err(|e| PkEncryptionError::Decode(e)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// The encryption component of PkEncryption support. |
| 80 | +#[pyclass] |
| 81 | +pub struct PkEncryption { |
| 82 | + inner: vodozemac::pk_encryption::PkEncryption, |
| 83 | +} |
| 84 | + |
| 85 | +#[pymethods] |
| 86 | +impl PkEncryption { |
| 87 | + /// Create a new PkEncryption object from public key bytes. |
| 88 | + #[classmethod] |
| 89 | + fn from_public_key(_cls: &Bound<'_, PyType>, bytes: &[u8]) -> Result<Self, PkEncryptionError> { |
| 90 | + let key: &[u8; 32] = bytes |
| 91 | + .try_into() |
| 92 | + .map_err(|_| PkEncryptionError::InvalidKeySize(bytes.len()))?; |
| 93 | + |
| 94 | + Ok(Self { |
| 95 | + inner: vodozemac::pk_encryption::PkEncryption::from_key( |
| 96 | + Curve25519PublicKey::from_bytes(*key), |
| 97 | + ), |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + /// Encrypt a plaintext for the recipient. Writes to the ciphertext, mac, and |
| 102 | + /// ephemeral_key buffers, whose values should be sent to the recipient. mac is |
| 103 | + /// a Message Authentication Code to ensure that the data is received and |
| 104 | + /// decrypted properly. ephemeral_key is the public part of the ephemeral key |
| 105 | + /// used (together with the recipient's key) to generate a symmetric encryption |
| 106 | + /// key. |
| 107 | + pub fn encrypt(&self, message: &[u8]) -> Message { |
| 108 | + let msg = self.inner.encrypt(message); |
| 109 | + Message { |
| 110 | + ciphertext: msg.ciphertext.to_vec(), |
| 111 | + mac: msg.mac.to_vec(), |
| 112 | + ephemeral_key: msg.ephemeral_key.to_vec(), |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments