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
|
Description: replace crypto
This patch is from upstream 0.5
Author: Ed Neville <ed-debian@s5h.net>
diff --git a/src/crypto/aes256.rs b/src/crypto/aes256.rs
index 6e6c541..eebfad6 100644
--- a/src/crypto/aes256.rs
+++ b/src/crypto/aes256.rs
@@ -1,4 +1,4 @@
-// Copyright (c) 2016-2017 Martijn Rijkeboer <mrr@sru-systems.com>
+// Copyright (c) 2016-2017,2025 Martijn Rijkeboer <mrr@sru-systems.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
@@ -6,63 +6,23 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use crate::rust_crypto::aes::{self, KeySize};
-use crate::rust_crypto::blockmodes::PkcsPadding;
-use crate::rust_crypto::buffer::{
- BufferResult, ReadBuffer, RefReadBuffer, RefWriteBuffer, WriteBuffer,
-};
use crate::types::{MasterIV, MasterKey, Result};
+use aes::Aes256;
+use cbc::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyIvInit};
+
+type Aes256CbcDec = cbc::Decryptor<Aes256>;
+type Aes256CbcEnc = cbc::Encryptor<Aes256>;
/// Decrypt the input using the key and initialization vector.
pub fn decrypt(key: &MasterKey, iv: &MasterIV, input: &[u8]) -> Result<Vec<u8>> {
- let mut cipher = aes::cbc_decryptor(KeySize::KeySize256, &key.unsecure(), &iv.0, PkcsPadding);
- let mut output = Vec::new();
- let mut read_buffer = RefReadBuffer::new(input);
- let mut buffer = [0; 4096];
- let mut write_buffer = RefWriteBuffer::new(&mut buffer);
-
- loop {
- let result = cipher.decrypt(&mut read_buffer, &mut write_buffer, true)?;
- output.extend(
- write_buffer
- .take_read_buffer()
- .take_remaining()
- .iter()
- .map(|&i| i),
- );
- match result {
- BufferResult::BufferUnderflow => break,
- BufferResult::BufferOverflow => {}
- }
- }
-
- Ok(output)
+ let cipher = Aes256CbcDec::new(&key.unsecure().into(), &iv.0.into());
+ Ok(cipher.decrypt_padded_vec_mut::<Pkcs7>(input)?)
}
/// Encrypt the input using the key and initialization vector.
pub fn encrypt(key: &MasterKey, iv: &MasterIV, input: &[u8]) -> Result<Vec<u8>> {
- let mut cipher = aes::cbc_encryptor(KeySize::KeySize256, &key.unsecure(), &iv.0, PkcsPadding);
- let mut output = Vec::new();
- let mut read_buffer = RefReadBuffer::new(input);
- let mut buffer = [0; 4096];
- let mut write_buffer = RefWriteBuffer::new(&mut buffer);
-
- loop {
- let result = cipher.encrypt(&mut read_buffer, &mut write_buffer, true)?;
- output.extend(
- write_buffer
- .take_read_buffer()
- .take_remaining()
- .iter()
- .map(|&i| i),
- );
- match result {
- BufferResult::BufferUnderflow => break,
- BufferResult::BufferOverflow => {}
- }
- }
-
- Ok(output)
+ let cipher = Aes256CbcEnc::new(&key.unsecure().into(), &iv.0.into());
+ Ok(cipher.encrypt_padded_vec_mut::<Pkcs7>(input))
}
#[cfg(test)]
diff --git a/src/crypto/salsa20.rs b/src/crypto/salsa20.rs
index ab61351..01dc5ac 100644
--- a/src/crypto/salsa20.rs
+++ b/src/crypto/salsa20.rs
@@ -1,4 +1,4 @@
-// Copyright (c) 2016-2017 Martijn Rijkeboer <mrr@sru-systems.com>
+// Copyright (c) 2016-2017,2025 Martijn Rijkeboer <mrr@sru-systems.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
@@ -6,12 +6,12 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use crate::rust_crypto::symmetriccipher::SynchronousStreamCipher;
use crate::types::StreamKey;
+use salsa20::cipher::{KeyIvInit, StreamCipher};
const SALSA20_NOUNCE: [u8; 8] = [0xe8, 0x30, 0x09, 0x4b, 0x97, 0x20, 0x5d, 0x2a];
-pub use crate::rust_crypto::salsa20::Salsa20;
+pub use salsa20::Salsa20;
/// Decrypt the input using the Salsa20 stream cipher.
pub fn decrypt(cipher: &mut Salsa20, input: &Vec<u8>) -> Vec<u8> {
@@ -25,13 +25,13 @@ pub fn encrypt(cipher: &mut Salsa20, input: &Vec<u8>) -> Vec<u8> {
/// Create a new Salsa20 stream cipher using the specified key.
pub fn new_cipher(key: &StreamKey) -> Salsa20 {
- Salsa20::new(&key.unpack(), &SALSA20_NOUNCE)
+ Salsa20::new(&key.unpack().into(), &SALSA20_NOUNCE.into())
}
fn process(cipher: &mut Salsa20, input: &Vec<u8>) -> Vec<u8> {
- let mut output = vec![0; input.len()];
- cipher.process(input, &mut output);
- output
+ let mut buffer = input.clone();
+ cipher.apply_keystream(&mut buffer);
+ buffer
}
#[cfg(test)]
diff --git a/src/crypto/sha256.rs b/src/crypto/sha256.rs
index fdd230c..e9080ff 100644
--- a/src/crypto/sha256.rs
+++ b/src/crypto/sha256.rs
@@ -1,4 +1,4 @@
-// Copyright (c) 2016-2017 Martijn Rijkeboer <mrr@sru-systems.com>
+// Copyright (c) 2016-2017,2025 Martijn Rijkeboer <mrr@sru-systems.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
@@ -6,19 +6,16 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use crate::rust_crypto::digest::Digest;
-use crate::rust_crypto::sha2::Sha256;
+use sha2::{Digest, Sha256};
/// Hash the input using the SHA256 hashing algorithm.
pub fn hash(inputs: &[&[u8]]) -> [u8; 32] {
let mut hasher = Sha256::new();
for input in inputs {
- hasher.input(input);
+ hasher.update(input);
}
- let mut result = [0u8; 32];
- hasher.result(&mut result);
- result
+ hasher.finalize().into()
}
#[cfg(test)]
diff --git a/src/lib.rs b/src/lib.rs
index eefea66..f833c69 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -127,7 +127,6 @@
//!
//! - KeePass 1 databases.
-extern crate crypto as rust_crypto;
extern crate xml as rust_xml;
pub use crate::types::Association;
diff --git a/src/types/error.rs b/src/types/error.rs
index c252c5e..cb85914 100644
--- a/src/types/error.rs
+++ b/src/types/error.rs
@@ -1,4 +1,4 @@
-// Copyright (c) 2016-2017 Martijn Rijkeboer <mrr@sru-systems.com>
+// Copyright (c) 2016-2017,2025 Martijn Rijkeboer <mrr@sru-systems.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
@@ -6,7 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-use crate::rust_crypto::symmetriccipher::SymmetricCipherError;
+use cbc::cipher::block_padding::UnpadError;
use std::error;
use std::fmt;
use std::io;
@@ -17,7 +17,7 @@ use xml::writer as xmlwriter;
#[derive(Debug)]
pub enum Error {
/// Error during the encryption or decryption of the database.
- CryptoError(SymmetricCipherError),
+ CryptoError(UnpadError),
/// The hash of a data block is invalid.
InvalidBlockHash,
@@ -83,16 +83,7 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
- Error::CryptoError(err) => match err {
- SymmetricCipherError::InvalidLength => {
- write!(f, "Crypto error: invalid length.")
- }
-
- SymmetricCipherError::InvalidPadding => {
- write!(f, "Crypto error: invalid padding.")
- }
- },
-
+ Error::CryptoError(_) => write!(f, "Crypto error: invalid padding."),
Error::InvalidBlockHash => write!(f, "Invalid block hash"),
Error::InvalidBlockId(val) => write!(f, "Invalid block id: {}", val),
Error::InvalidDbSignature(val) => write!(f, "Invalid database signature: {:?}", val),
@@ -151,8 +142,8 @@ impl From<xmlwriter::Error> for Error {
}
}
-impl From<SymmetricCipherError> for Error {
- fn from(err: SymmetricCipherError) -> Error {
+impl From<UnpadError> for Error {
+ fn from(err: UnpadError) -> Error {
Error::CryptoError(err)
}
}
diff --git a/src/types/transformed_key.rs b/src/types/transformed_key.rs
index 6d92ff6..2d15b2c 100644
--- a/src/types/transformed_key.rs
+++ b/src/types/transformed_key.rs
@@ -6,17 +6,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
-#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
-use crate::rust_crypto::aesni;
-
use crate::crypto::sha256;
-use crate::rust_crypto::aes;
-use crate::rust_crypto::aessafe;
-use crate::rust_crypto::symmetriccipher::BlockEncryptor;
-use crate::rust_crypto::util;
use crate::types::composite_key::CompositeKey;
use crate::types::transform_rounds::TransformRounds;
use crate::types::transform_seed::TransformSeed;
+
+use aes::Aes256;
+use cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit};
use secstr::SecStr;
/// Key used for generating the master key.
@@ -33,22 +29,16 @@ impl TransformedKey {
seed: &TransformSeed,
rounds: &TransformRounds,
) -> TransformedKey {
- let mut tmp_key = key.unsecure().clone();
- let mut output = [0u8; 32];
- if util::supports_aesni() {
- let cipher = aesni::AesNiEncryptor::new(aes::KeySize::KeySize256, &seed.0);
- for _ in 0..rounds.0 {
- cipher.encrypt_block(&tmp_key[0..16], &mut output[0..16]);
- cipher.encrypt_block(&tmp_key[16..32], &mut output[16..32]);
- tmp_key = output;
- }
- } else {
- let cipher = aessafe::AesSafe256Encryptor::new(&seed.0);
- for _ in 0..rounds.0 {
- cipher.encrypt_block(&tmp_key[0..16], &mut output[0..16]);
- cipher.encrypt_block(&tmp_key[16..32], &mut output[16..32]);
- tmp_key = output;
- }
+ let mut tmp_key: [u8; 32] = key.unsecure().clone().into();
+ let cipher = Aes256::new(&seed.0.into());
+ for _ in 0..rounds.0 {
+ let mut blocks = [
+ GenericArray::clone_from_slice(&tmp_key[0..16]).clone(),
+ GenericArray::clone_from_slice(&tmp_key[16..32]).clone(),
+ ];
+ cipher.encrypt_blocks(&mut blocks);
+ tmp_key[0..16].copy_from_slice(&blocks[0]);
+ tmp_key[16..32].copy_from_slice(&blocks[1]);
}
TransformedKey::secure(sha256::hash(&[&tmp_key]))
|