Skip to content

Commit 9a35feb

Browse files
committed
Rust: Query framework and basic tests.
1 parent 2f2c9f8 commit 9a35feb

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Hard-coded cryptographic value
3+
* @description Using hardcoded keys, passwords, salts or initialization
4+
* vectors is not secure.
5+
* @kind problem
6+
* @problem.severity warning
7+
* @security-severity TODO
8+
* @precision high
9+
* @id rust/hardcoded-crytographic-value
10+
* @tags security
11+
* external/cwe/cwe-259
12+
* external/cwe/cwe-321
13+
* external/cwe/cwe-798
14+
* external/cwe/cwe-1204
15+
*/
16+
17+
import rust
18+
19+
from Locatable e
20+
where none()
21+
select e, ""

rust/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: queries/security/CWE-798/HardcodedCryptographicValue.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
qltest_cargo_check: true
2+
qltest_dependencies:
3+
- cipher = { version = "0.4.4" }
4+
- rabbit = { version = "0.4.1" }
5+
- aes = { version = "0.8.4" }
6+
- cfb-mode = { version = "0.8.2" }
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
use cipher::{consts::*, StreamCipher, AsyncStreamCipher, KeyInit, KeyIvInit, BlockEncrypt};
3+
use rabbit::{Rabbit, RabbitKeyOnly};
4+
use aes::Aes256;
5+
6+
// --- tests ---
7+
8+
fn test_stream_cipher_rabbit(
9+
key: &[u8;16], iv: &[u8;16], plaintext: &str
10+
) {
11+
let mut data = plaintext.as_bytes().to_vec();
12+
13+
// rabbit
14+
15+
let mut rabbit_cipher1 = RabbitKeyOnly::new(rabbit::Key::from_slice(key));
16+
rabbit_cipher1.apply_keystream(&mut data);
17+
18+
let const1: &[u8;16] = &[0u8;16]; // $ MISSING: Alert[rust/hardcoded-crytographic-value]
19+
let mut rabbit_cipher2 = RabbitKeyOnly::new(rabbit::Key::from_slice(const1));
20+
rabbit_cipher2.apply_keystream(&mut data);
21+
22+
let mut rabbit_cipher3 = Rabbit::new(rabbit::Key::from_slice(key), rabbit::Iv::from_slice(iv));
23+
rabbit_cipher3.apply_keystream(&mut data);
24+
25+
let const2: &[u8;16] = &[0u8;16]; // $ MISSING: Alert[rust/hardcoded-crytographic-value]
26+
let mut rabbit_cipher4 = Rabbit::new(rabbit::Key::from_slice(const2), rabbit::Iv::from_slice(iv));
27+
rabbit_cipher4.apply_keystream(&mut data);
28+
29+
let const3: &[u8;16] = &[0u8;16]; // $ MISSING: Alert[rust/hardcoded-crytographic-value]
30+
let mut rabbit_cipher5 = Rabbit::new(rabbit::Key::from_slice(key), rabbit::Iv::from_slice(const3));
31+
rabbit_cipher5.apply_keystream(&mut data);
32+
33+
let const4: &[u8;16] = &[0u8;16]; // (unused, so good)
34+
}
35+
36+
fn test_block_cipher_aes(
37+
key: &[u8], iv: &[u8], key256: &[u8;32],
38+
block128: &mut [u8;16], input: &[u8], output: &mut [u8]
39+
) {
40+
// aes
41+
42+
let aes_cipher1 = Aes256::new(key256.into());
43+
aes_cipher1.encrypt_block(block128.into());
44+
45+
let const1 = &[0u8;32]; // $ MISSING: Alert[rust/hardcoded-crytographic-value]
46+
let aes_cipher2 = Aes256::new(const1.into());
47+
aes_cipher2.encrypt_block(block128.into());
48+
49+
let aes_cipher3 = Aes256::new_from_slice(key256).unwrap();
50+
aes_cipher3.encrypt_block(block128.into());
51+
52+
let const2 = &[0u8;32]; // $ MISSING: Alert[rust/hardcoded-crytographic-value]
53+
let aes_cipher4 = Aes256::new_from_slice(const2).unwrap();
54+
aes_cipher4.encrypt_block(block128.into());
55+
56+
let aes_cipher5 = cfb_mode::Encryptor::<aes::Aes256>::new(key.into(), iv.into());
57+
_ = aes_cipher5.encrypt_b2b(input, output).unwrap();
58+
59+
let const3 = &[0u8;32]; // $ MISSING: Alert[rust/hardcoded-crytographic-value]
60+
let aes_cipher6 = cfb_mode::Encryptor::<aes::Aes256>::new(const3.into(), iv.into());
61+
_ = aes_cipher6.encrypt_b2b(input, output).unwrap();
62+
63+
let const4 = &[0u8; 16]; // $ MISSING: Alert[rust/hardcoded-crytographic-value]
64+
let aes_cipher7 = cfb_mode::Encryptor::<aes::Aes256>::new(key.into(), const4.into());
65+
_ = aes_cipher7.encrypt_b2b(input, output).unwrap();
66+
}

0 commit comments

Comments
 (0)