1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
//! Module for symmetric/asymmetric encryption/decryption.
//!
//! This module contains everything related to encryption. You can use it to encrypt and decrypt data using either a shared key of a keypair.
//! Either way, the encryption will give you a `Ciphertext`, which has a method to decrypt it.
//!
//! ### Symmetric
//!
//! ```rust
//! use devolutions_crypto::utils::generate_key;
//! use devolutions_crypto::ciphertext::{ encrypt, CiphertextVersion, Ciphertext };
//!
//! let key: Vec<u8> = generate_key(32);
//!
//! let data = b"somesecretdata";
//!
//! let encrypted_data: Ciphertext = encrypt(data, &key, CiphertextVersion::Latest).expect("encryption shouldn't fail");
//!
//! let decrypted_data = encrypted_data.decrypt(&key).expect("The decryption shouldn't fail");
//!
//! assert_eq!(decrypted_data, data);
//! ```
//!
//! ### Asymmetric
//! Here, you will need a `PublicKey` to encrypt data and the corresponding
//! `PrivateKey` to decrypt it. You can generate them by using `generate_keypair`
//! in the [Key module](#key).
//!
//! ```rust
//! use devolutions_crypto::key::{generate_keypair, KeyVersion, KeyPair};
//! use devolutions_crypto::ciphertext::{ encrypt_asymmetric, CiphertextVersion, Ciphertext };
//!
//! let keypair: KeyPair = generate_keypair(KeyVersion::Latest);
//!
//! let data = b"somesecretdata";
//!
//! let encrypted_data: Ciphertext = encrypt_asymmetric(data, &keypair.public_key, CiphertextVersion::Latest).expect("encryption shouldn't fail");
//!
//! let decrypted_data = encrypted_data.decrypt_asymmetric(&keypair.private_key).expect("The decryption shouldn't fail");
//!
//! assert_eq!(decrypted_data, data);
//! ```
mod ciphertext_v1;
mod ciphertext_v2;
use super::CiphertextSubtype;
pub use super::CiphertextVersion;
use super::DataType;
use super::Error;
use super::Header;
use super::HeaderType;
use super::Result;
use super::key::{PrivateKey, PublicKey};
use ciphertext_v1::CiphertextV1;
use ciphertext_v2::{CiphertextV2Asymmetric, CiphertextV2Symmetric};
use std::convert::TryFrom;
#[cfg(feature = "fuzz")]
use arbitrary::Arbitrary;
/// A versionned ciphertext. Can be either symmetric or asymmetric.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz", derive(Arbitrary))]
pub struct Ciphertext {
pub(crate) header: Header<Ciphertext>,
payload: CiphertextPayload,
}
impl HeaderType for Ciphertext {
type Version = CiphertextVersion;
type Subtype = CiphertextSubtype;
fn data_type() -> DataType {
DataType::Ciphertext
}
}
#[derive(Clone, Debug)]
#[cfg_attr(feature = "fuzz", derive(Arbitrary))]
enum CiphertextPayload {
V1(CiphertextV1),
V2Symmetric(CiphertextV2Symmetric),
V2Asymmetric(CiphertextV2Asymmetric),
}
/// Returns an `Ciphertext` from cleartext data and a key.
/// # Arguments
/// * `data` - The data to encrypt.
/// * `key` - The key to use. The recommended size is 32 bytes.
/// * `version` - Version of the library to encrypt with. Use `CiphertTextVersion::Latest` if you're not dealing with shared data.
/// # Returns
/// Returns a `Ciphertext` containing the encrypted data.
/// # Example
/// ```rust
/// use devolutions_crypto::ciphertext::{ encrypt, CiphertextVersion };
///
/// let data = b"somesecretdata";
/// let key = b"somesecretkey";
///
/// let encrypted_data = encrypt(data, key, CiphertextVersion::Latest).unwrap();
/// ```
pub fn encrypt(data: &[u8], key: &[u8], version: CiphertextVersion) -> Result<Ciphertext> {
let mut header = Header::default();
header.data_subtype = CiphertextSubtype::Symmetric;
let payload = match version {
CiphertextVersion::V1 => {
header.version = CiphertextVersion::V1;
CiphertextPayload::V1(CiphertextV1::encrypt(data, key, &header)?)
}
CiphertextVersion::V2 | CiphertextVersion::Latest => {
header.version = CiphertextVersion::V2;
CiphertextPayload::V2Symmetric(CiphertextV2Symmetric::encrypt(data, key, &header)?)
} //_ => return Err(DevoCryptoError::UnknownVersion),
};
Ok(Ciphertext { header, payload })
}
/// Returns an `Ciphertext` from cleartext data and a `PublicKey`.
/// You will need the corresponding `PrivateKey` to decrypt it.
/// # Arguments
/// * `data` - The data to encrypt.
/// * `public_key` - The `PublicKey` to use. Use `generate_keypair` to generate a keypair.
/// * `version` - Version of the library to encrypt with. Use `CiphertTextVersion::Latest` if you're not dealing with shared data.
/// # Returns
/// Returns a `Ciphertext` containing the encrypted data.
/// # Example
/// ```rust
/// use devolutions_crypto::ciphertext::{ encrypt_asymmetric, CiphertextVersion };
/// use devolutions_crypto::key::{ generate_keypair, KeyVersion };
///
/// let data = b"somesecretdata";
/// let keypair = generate_keypair(KeyVersion::Latest);
///
/// let encrypted_data = encrypt_asymmetric(data, &keypair.public_key, CiphertextVersion::Latest).unwrap();
/// ```
pub fn encrypt_asymmetric(
data: &[u8],
public_key: &PublicKey,
version: CiphertextVersion,
) -> Result<Ciphertext> {
let mut header = Header::default();
header.data_subtype = CiphertextSubtype::Asymmetric;
let payload = match version {
CiphertextVersion::V2 | CiphertextVersion::Latest => {
header.version = CiphertextVersion::V2;
CiphertextPayload::V2Asymmetric(CiphertextV2Asymmetric::encrypt(
data, public_key, &header,
)?)
}
_ => return Err(Error::UnknownVersion),
};
Ok(Ciphertext { header, payload })
}
impl Ciphertext {
/// Decrypt the data blob using a key.
/// # Arguments
/// * `key` - Key to use. The recommended size is 32 bytes.
/// # Returns
/// Returns the decrypted data.
/// # Example
/// ```rust
/// use devolutions_crypto::ciphertext::{ encrypt, CiphertextVersion};
///
/// let data = b"somesecretdata";
/// let key = b"somesecretkey";
///
/// let encrypted_data = encrypt(data, key, CiphertextVersion::Latest).unwrap();
/// let decrypted_data = encrypted_data.decrypt(key).unwrap();
///
/// assert_eq!(data.to_vec(), decrypted_data);
///```
pub fn decrypt(&self, key: &[u8]) -> Result<Vec<u8>> {
match &self.payload {
CiphertextPayload::V1(x) => x.decrypt(key, &self.header),
CiphertextPayload::V2Symmetric(x) => x.decrypt(key, &self.header),
_ => Err(Error::InvalidDataType),
}
}
/// Decrypt the data blob using a `PrivateKey`.
/// # Arguments
/// * `private_key` - Key to use. Must be the one in the same keypair as the `PublicKey` used for encryption.
/// # Returns
/// Returns the decrypted data.
/// # Example
/// ```rust
/// use devolutions_crypto::ciphertext::{ encrypt_asymmetric, CiphertextVersion };
/// use devolutions_crypto::key::{ generate_keypair, KeyVersion };
///
/// let data = b"somesecretdata";
/// let keypair = generate_keypair(KeyVersion::Latest);
///
/// let encrypted_data = encrypt_asymmetric(data, &keypair.public_key, CiphertextVersion::Latest).unwrap();
/// let decrypted_data = encrypted_data.decrypt_asymmetric(&keypair.private_key).unwrap();
///
/// assert_eq!(decrypted_data, data);
///```
pub fn decrypt_asymmetric(&self, private_key: &PrivateKey) -> Result<Vec<u8>> {
match &self.payload {
CiphertextPayload::V2Asymmetric(x) => x.decrypt(private_key, &self.header),
CiphertextPayload::V1(_) => Err(Error::UnknownVersion),
_ => Err(Error::InvalidDataType),
}
}
}
impl From<Ciphertext> for Vec<u8> {
/// Serialize the structure into a `Vec<u8>`, for storage, transmission or use in another language.
fn from(data: Ciphertext) -> Self {
let mut header: Self = data.header.into();
let mut payload: Self = data.payload.into();
header.append(&mut payload);
header
}
}
impl TryFrom<&[u8]> for Ciphertext {
type Error = Error;
/// Parses the data. Can return an Error of the data is invalid or unrecognized.
fn try_from(data: &[u8]) -> Result<Self> {
if data.len() < Header::len() {
return Err(Error::InvalidLength);
};
let header = Header::try_from(&data[0..Header::len()])?;
let payload = match header.version {
CiphertextVersion::V1 => {
CiphertextPayload::V1(CiphertextV1::try_from(&data[Header::len()..])?)
}
CiphertextVersion::V2 => match header.data_subtype {
CiphertextSubtype::Symmetric | CiphertextSubtype::None => {
CiphertextPayload::V2Symmetric(CiphertextV2Symmetric::try_from(
&data[Header::len()..],
)?)
}
CiphertextSubtype::Asymmetric => CiphertextPayload::V2Asymmetric(
CiphertextV2Asymmetric::try_from(&data[Header::len()..])?,
),
},
_ => return Err(Error::UnknownVersion),
};
Ok(Self { header, payload })
}
}
impl From<CiphertextPayload> for Vec<u8> {
fn from(data: CiphertextPayload) -> Self {
match data {
CiphertextPayload::V1(x) => x.into(),
CiphertextPayload::V2Symmetric(x) => x.into(),
CiphertextPayload::V2Asymmetric(x) => x.into(),
}
}
}
#[test]
fn encrypt_decrypt_test() {
let key = "0123456789abcdefghijkl".as_bytes();
let data = "This is a very complex string of character that we need to encrypt".as_bytes();
let encrypted = encrypt(data, key, CiphertextVersion::Latest).unwrap();
let encrypted: Vec<u8> = encrypted.into();
let encrypted = Ciphertext::try_from(encrypted.as_slice()).unwrap();
let decrypted = encrypted.decrypt(key).unwrap();
assert_eq!(decrypted, data);
}
#[test]
fn encrypt_decrypt_v1_test() {
let key = "0123456789abcdefghijkl".as_bytes();
let data = "This is a very complex string of character that we need to encrypt".as_bytes();
let encrypted = encrypt(data, key, CiphertextVersion::V1).unwrap();
assert_eq!(encrypted.header.version, CiphertextVersion::V1);
let encrypted: Vec<u8> = encrypted.into();
let encrypted = Ciphertext::try_from(encrypted.as_slice()).unwrap();
let decrypted = encrypted.decrypt(key).unwrap();
assert_eq!(decrypted, data);
}
#[test]
fn encrypt_decrypt_v2_test() {
let key = "0123456789abcdefghijkl".as_bytes();
let data = "This is a very complex string of character that we need to encrypt".as_bytes();
let encrypted = encrypt(data, key, CiphertextVersion::V2).unwrap();
assert_eq!(encrypted.header.version, CiphertextVersion::V2);
let encrypted: Vec<u8> = encrypted.into();
let encrypted = Ciphertext::try_from(encrypted.as_slice()).unwrap();
let decrypted = encrypted.decrypt(key).unwrap();
assert_eq!(decrypted, data);
}
#[test]
fn asymmetric_test() {
use super::key::{generate_keypair, KeyVersion};
let test_plaintext = b"this is a test data";
let keypair = generate_keypair(KeyVersion::Latest);
let encrypted_data =
encrypt_asymmetric(test_plaintext, &keypair.public_key, CiphertextVersion::V2).unwrap();
let encrypted_data_vec: Vec<u8> = encrypted_data.into();
assert_ne!(encrypted_data_vec.len(), 0);
let encrypted_data = Ciphertext::try_from(encrypted_data_vec.as_slice()).unwrap();
let decrypted_data = encrypted_data
.decrypt_asymmetric(&keypair.private_key)
.unwrap();
assert_eq!(decrypted_data, test_plaintext);
}
#[test]
fn asymmetric_test_v2() {
use super::key::{generate_keypair, KeyVersion};
let test_plaintext = b"this is a test data";
let keypair = generate_keypair(KeyVersion::V1);
let encrypted_data =
encrypt_asymmetric(test_plaintext, &keypair.public_key, CiphertextVersion::V2).unwrap();
assert_eq!(encrypted_data.header.version, CiphertextVersion::V2);
let encrypted_data_vec: Vec<u8> = encrypted_data.into();
assert_ne!(encrypted_data_vec.len(), 0);
let encrypted_data = Ciphertext::try_from(encrypted_data_vec.as_slice()).unwrap();
let decrypted_data = encrypted_data
.decrypt_asymmetric(&keypair.private_key)
.unwrap();
assert_eq!(decrypted_data, test_plaintext);
}