diff options
author | Shun Sakai <sorairolake@protonmail.ch> | 2023-07-09 17:35:03 +0900 |
---|---|---|
committer | Shun Sakai <sorairolake@protonmail.ch> | 2023-07-09 17:35:03 +0900 |
commit | e2cb5bafb95fea51328238acac907c054ae726a5 (patch) | |
tree | cb797e8443c0afa7493bff26335373f008226f5b | |
parent | 0557953f980bd424401c60502b4aac3525b26083 (diff) |
openpgp: Add Camellia support to RustCrypto backend.
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | openpgp/Cargo.toml | 3 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/rust/aead.rs | 57 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/rust/symmetric.rs | 117 |
4 files changed, 167 insertions, 21 deletions
@@ -271,6 +271,16 @@ dependencies = [ ] [[package]] +name = "camellia" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3264e2574e9ef2b53ce6f536dea83a69ac0bc600b762d1523ff83fe07230ce30" +dependencies = [ + "byteorder", + "cipher", +] + +[[package]] name = "capnp" version = "0.14.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2387,6 +2397,7 @@ dependencies = [ "botan", "buffered-reader", "bzip2", + "camellia", "cast5", "cfb-mode", "chrono", diff --git a/openpgp/Cargo.toml b/openpgp/Cargo.toml index 1a9e6585..aed5d290 100644 --- a/openpgp/Cargo.toml +++ b/openpgp/Cargo.toml @@ -61,6 +61,7 @@ aes = { version = "0.8", optional = true } aes-gcm = { version = "0.10", optional = true, features = ["std"] } block-padding = { version = "0.3", optional = true } blowfish = { version = "0.9", optional = true } +camellia = { version = "0.1", optional = true } cast5 = { version = "0.11", optional = true } cipher = { version = "0.4", optional = true, features = ["std"] } cfb-mode = { version = "0.8", optional = true } @@ -126,7 +127,7 @@ default = ["compression", "crypto-nettle"] # TODO(#333): Allow for/implement more backends crypto-nettle = ["nettle"] crypto-rust = [ - "aes", "block-padding", "blowfish", "cast5", "cfb-mode", "cipher", "des", + "aes", "block-padding", "blowfish", "camellia", "cast5", "cfb-mode", "cipher", "des", "digest", "eax", "ecb", "ed25519", "ed25519-dalek", "generic-array", "idea", "md-5", "num-bigint-dig", "rand", "rand07", "ripemd", "rsa", "sha-1", "sha2", "twofish", "typenum", "x25519-dalek-ng", "p256", diff --git a/openpgp/src/crypto/backend/rust/aead.rs b/openpgp/src/crypto/backend/rust/aead.rs index 248b5cb7..9bd4a317 100644 --- a/openpgp/src/crypto/backend/rust/aead.rs +++ b/openpgp/src/crypto/backend/rust/aead.rs @@ -204,14 +204,65 @@ impl AEADAlgorithm { Ok(Box::new(ctx)) }, }, + SymmetricAlgorithm::Camellia128 => match op { + CipherOp::Encrypt => { + let mut ctx = + Eax::<camellia::Camellia128, Encrypt>::with_key_and_nonce( + GenericArray::try_from_slice(key)?, + GenericArray::try_from_slice(nonce)?); + ctx.update_assoc(aad); + Ok(Box::new(ctx)) + }, + CipherOp::Decrypt => { + let mut ctx = + Eax::<camellia::Camellia128, Decrypt>::with_key_and_nonce( + GenericArray::try_from_slice(key)?, + GenericArray::try_from_slice(nonce)?); + ctx.update_assoc(aad); + Ok(Box::new(ctx)) + }, + }, + SymmetricAlgorithm::Camellia192 => match op { + CipherOp::Encrypt => { + let mut ctx = + Eax::<camellia::Camellia192, Encrypt>::with_key_and_nonce( + GenericArray::try_from_slice(key)?, + GenericArray::try_from_slice(nonce)?); + ctx.update_assoc(aad); + Ok(Box::new(ctx)) + }, + CipherOp::Decrypt => { + let mut ctx = + Eax::<camellia::Camellia192, Decrypt>::with_key_and_nonce( + GenericArray::try_from_slice(key)?, + GenericArray::try_from_slice(nonce)?); + ctx.update_assoc(aad); + Ok(Box::new(ctx)) + }, + }, + SymmetricAlgorithm::Camellia256 => match op { + CipherOp::Encrypt => { + let mut ctx = + Eax::<camellia::Camellia256, Encrypt>::with_key_and_nonce( + GenericArray::try_from_slice(key)?, + GenericArray::try_from_slice(nonce)?); + ctx.update_assoc(aad); + Ok(Box::new(ctx)) + }, + CipherOp::Decrypt => { + let mut ctx = + Eax::<camellia::Camellia256, Decrypt>::with_key_and_nonce( + GenericArray::try_from_slice(key)?, + GenericArray::try_from_slice(nonce)?); + ctx.update_assoc(aad); + Ok(Box::new(ctx)) + }, + }, | SymmetricAlgorithm::IDEA | SymmetricAlgorithm::TripleDES | SymmetricAlgorithm::CAST5 | SymmetricAlgorithm::Blowfish | SymmetricAlgorithm::Twofish - | SymmetricAlgorithm::Camellia128 - | SymmetricAlgorithm::Camellia192 - | SymmetricAlgorithm::Camellia256 | SymmetricAlgorithm::Private(_) | SymmetricAlgorithm::Unknown(_) | SymmetricAlgorithm::Unencrypted => diff --git a/openpgp/src/crypto/backend/rust/symmetric.rs b/openpgp/src/crypto/backend/rust/symmetric.rs index 30bf7274..b04f0fc9 100644 --- a/openpgp/src/crypto/backend/rust/symmetric.rs +++ b/openpgp/src/crypto/backend/rust/symmetric.rs @@ -21,9 +21,9 @@ enum CfbEncrypt { Aes192(cfb_mode::Encryptor<aes::Aes192>), Aes256(cfb_mode::Encryptor<aes::Aes256>), Twofish(cfb_mode::Encryptor<twofish::Twofish>), - //Camellia128 - //Camellia192 - //Camellia256 + Camellia128(cfb_mode::Encryptor<camellia::Camellia128>), + Camellia192(cfb_mode::Encryptor<camellia::Camellia192>), + Camellia256(cfb_mode::Encryptor<camellia::Camellia256>), } enum CfbDecrypt { @@ -35,9 +35,9 @@ enum CfbDecrypt { Aes192(cfb_mode::Decryptor<aes::Aes192>), Aes256(cfb_mode::Decryptor<aes::Aes256>), Twofish(cfb_mode::Decryptor<twofish::Twofish>), - //Camellia128 - //Camellia192 - //Camellia256 + Camellia128(cfb_mode::Decryptor<camellia::Camellia128>), + Camellia192(cfb_mode::Decryptor<camellia::Camellia192>), + Camellia256(cfb_mode::Decryptor<camellia::Camellia256>), } enum EcbEncrypt { @@ -49,9 +49,9 @@ enum EcbEncrypt { Aes192(ecb::Encryptor<aes::Aes192>), Aes256(ecb::Encryptor<aes::Aes256>), Twofish(ecb::Encryptor<twofish::Twofish>), - //Camellia128 - //Camellia192 - //Camellia256 + Camellia128(ecb::Encryptor<camellia::Camellia128>), + Camellia192(ecb::Encryptor<camellia::Camellia192>), + Camellia256(ecb::Encryptor<camellia::Camellia256>), } enum EcbDecrypt { @@ -63,9 +63,9 @@ enum EcbDecrypt { Aes192(ecb::Decryptor<aes::Aes192>), Aes256(ecb::Decryptor<aes::Aes256>), Twofish(ecb::Decryptor<twofish::Twofish>), - //Camellia128 - //Camellia192 - //Camellia256 + Camellia128(ecb::Decryptor<camellia::Camellia128>), + Camellia192(ecb::Decryptor<camellia::Camellia192>), + Camellia256(ecb::Decryptor<camellia::Camellia256>), } macro_rules! impl_block_size { @@ -88,6 +88,12 @@ macro_rules! impl_block_size { <aes::Aes256 as cipher::BlockSizeUser>::block_size(), $mode::Twofish(_) => <twofish::Twofish as cipher::BlockSizeUser>::block_size(), + $mode::Camellia128(_) => + <camellia::Camellia128 as cipher::BlockSizeUser>::block_size(), + $mode::Camellia192(_) => + <camellia::Camellia192 as cipher::BlockSizeUser>::block_size(), + $mode::Camellia256(_) => + <camellia::Camellia256 as cipher::BlockSizeUser>::block_size(), } } } @@ -143,6 +149,18 @@ macro_rules! impl_enc_mode { let blocks = to_blocks(&mut buf); m.encrypt_blocks_mut(blocks) } + $mode::Camellia128(m) => { + let blocks = to_blocks(&mut buf); + m.encrypt_blocks_mut(blocks) + } + $mode::Camellia192(m) => { + let blocks = to_blocks(&mut buf); + m.encrypt_blocks_mut(blocks) + } + $mode::Camellia256(m) => { + let blocks = to_blocks(&mut buf); + m.encrypt_blocks_mut(blocks) + } } dst.copy_from_slice(&buf[..dst.len()]); } else { @@ -180,6 +198,18 @@ macro_rules! impl_enc_mode { let blocks = to_blocks(dst); m.encrypt_blocks_mut(blocks) } + $mode::Camellia128(m) => { + let blocks = to_blocks(dst); + m.encrypt_blocks_mut(blocks) + } + $mode::Camellia192(m) => { + let blocks = to_blocks(dst); + m.encrypt_blocks_mut(blocks) + } + $mode::Camellia256(m) => { + let blocks = to_blocks(dst); + m.encrypt_blocks_mut(blocks) + } } } Ok(()) @@ -256,6 +286,18 @@ macro_rules! impl_dec_mode { let blocks = to_blocks(&mut buf); m.decrypt_blocks_mut(blocks) } + $mode::Camellia128(m) => { + let blocks = to_blocks(&mut buf); + m.decrypt_blocks_mut(blocks) + } + $mode::Camellia192(m) => { + let blocks = to_blocks(&mut buf); + m.decrypt_blocks_mut(blocks) + } + $mode::Camellia256(m) => { + let blocks = to_blocks(&mut buf); + m.decrypt_blocks_mut(blocks) + } } dst.copy_from_slice(&buf[..dst.len()]); } else { @@ -293,6 +335,18 @@ macro_rules! impl_dec_mode { let blocks = to_blocks(dst); m.decrypt_blocks_mut(blocks) } + $mode::Camellia128(m) => { + let blocks = to_blocks(dst); + m.decrypt_blocks_mut(blocks) + } + $mode::Camellia192(m) => { + let blocks = to_blocks(dst); + m.decrypt_blocks_mut(blocks) + } + $mode::Camellia256(m) => { + let blocks = to_blocks(dst); + m.decrypt_blocks_mut(blocks) + } } } Ok(()) @@ -382,8 +436,25 @@ macro_rules! make_mode { Ok(Box::new($enum::Twofish( $mode::$mode2::<twofish::Twofish>::new(key $(, $iv)?)))) }, - Camellia128 | Camellia192 | Camellia256 - | Private(_) | Unknown(_) | Unencrypted => + Camellia128 => { + let key = GA::try_from_slice(&key)?; + $( let $iv = &GA::try_from_slice(&$iv)?; )? + Ok(Box::new($enum::Camellia128( + $mode::$mode2::<camellia::Camellia128>::new(key $(, $iv)?)))) + }, + Camellia192 => { + let key = GA::try_from_slice(&key)?; + $( let $iv = &GA::try_from_slice(&$iv)?; )? + Ok(Box::new($enum::Camellia192( + $mode::$mode2::<camellia::Camellia192>::new(key $(, $iv)?)))) + }, + Camellia256 => { + let key = GA::try_from_slice(&key)?; + $( let $iv = &GA::try_from_slice(&$iv)?; )? + Ok(Box::new($enum::Camellia256( + $mode::$mode2::<camellia::Camellia256>::new(key $(, $iv)?)))) + }, + Private(_) | Unknown(_) | Unencrypted => { Err(Error::UnsupportedSymmetricAlgorithm(self).into()) } @@ -405,9 +476,9 @@ impl SymmetricAlgorithm { AES192 => true, AES256 => true, Twofish => true, - Camellia128 => false, - Camellia192 => false, - Camellia256 => false, + Camellia128 => true, + Camellia192 => true, + Camellia256 => true, Private(_) => false, Unknown(_) => false, Unencrypted => false, @@ -444,6 +515,12 @@ mod tests { <aes::Aes256 as cipher::KeySizeUser>::key_size()); assert_eq!(SymmetricAlgorithm::Twofish.key_size()?, <twofish::Twofish as cipher::KeySizeUser>::key_size()); + assert_eq!(SymmetricAlgorithm::Camellia128.key_size()?, + <camellia::Camellia128 as cipher::KeySizeUser>::key_size()); + assert_eq!(SymmetricAlgorithm::Camellia192.key_size()?, + <camellia::Camellia192 as cipher::KeySizeUser>::key_size()); + assert_eq!(SymmetricAlgorithm::Camellia256.key_size()?, + <camellia::Camellia256 as cipher::KeySizeUser>::key_size()); Ok(()) } @@ -467,6 +544,12 @@ mod tests { <aes::Aes256 as cipher::BlockSizeUser>::block_size()); assert_eq!(SymmetricAlgorithm::Twofish.block_size()?, <twofish::Twofish as cipher::BlockSizeUser>::block_size()); + assert_eq!(SymmetricAlgorithm::Camellia128.block_size()?, + <camellia::Camellia128 as cipher::BlockSizeUser>::block_size()); + assert_eq!(SymmetricAlgorithm::Camellia192.block_size()?, + <camellia::Camellia192 as cipher::BlockSizeUser>::block_size()); + assert_eq!(SymmetricAlgorithm::Camellia256.block_size()?, + <camellia::Camellia256 as cipher::BlockSizeUser>::block_size()); Ok(()) } } |