Skip to content

Commit 6c4e0a9

Browse files
committed
Rust: A few more test cases.
1 parent bdb2f3d commit 6c4e0a9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

rust/ql/test/query-tests/security/CWE-327/options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ qltest_dependencies:
77
- des = { version = "0.8.1" }
88
- rc2 = { version = "0.8.1" }
99
- rc5 = { version = "0.0.1" }
10+
- cbc = { version = "0.1.2" }

rust/ql/test/query-tests/security/CWE-327/test_cipher.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,29 @@ fn test_block_cipher(
115115
rc5_cipher2.encrypt_block(data.into());
116116
rc5_cipher2.decrypt_block(data.into());
117117
}
118+
119+
type MyDesEncryptor = cbc::Encryptor<des::Des>;
120+
121+
fn test_cbc(
122+
key: &[u8], key128: &[u8;16], iv: &[u8], iv128: &[u8;16],
123+
input: &[u8], data: &mut [u8]
124+
) {
125+
let data_len = data.len();
126+
127+
// aes
128+
let aes_cipher1 = cbc::Encryptor::<aes::Aes128>::new(key128.into(), iv128.into());
129+
_ = aes_cipher1.encrypt_padded_mut::<aes::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
130+
131+
// des (broken)
132+
let des_cipher1 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
133+
_ = des_cipher1.encrypt_padded_mut::<des::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
134+
135+
let des_cipher2 = MyDesEncryptor::new(key.into(), iv.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
136+
_ = des_cipher2.encrypt_padded_mut::<des::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
137+
138+
let des_cipher3 = cbc::Encryptor::<des::Des>::new_from_slices(&key, &iv).unwrap(); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
139+
_ = des_cipher3.encrypt_padded_mut::<des::cipher::block_padding::Pkcs7>(data, data_len).unwrap();
140+
141+
let des_cipher4 = cbc::Encryptor::<des::Des>::new(key.into(), iv.into()); // $ MISSING: Alert[rust/weak-cryptographic-algorithm]
142+
_ = des_cipher4.encrypt_padded_b2b_mut::<des::cipher::block_padding::Pkcs7>(input, data).unwrap();
143+
}

0 commit comments

Comments
 (0)