Skip to content

Commit 95be12e

Browse files
committed
Rust: Add qhelp and examples.
1 parent b4a6063 commit 95be12e

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>
8+
Hardcoded passwords, keys, initialization vectors and salts should not be used for cryptographic operations.
9+
</p>
10+
<ul>
11+
<li>
12+
Attackers can easily recover hardcoded values if they have access to the source code or compiled executable.
13+
</li>
14+
<li>
15+
Some hardcoded values may be easily guessable.
16+
</li>
17+
<li>
18+
Hardcoded values may leave cryptographic operations vulnerable to dictionary attacks, rainbow tables, and other forms of cryptanalysis.
19+
</li>
20+
</ul>
21+
22+
</overview>
23+
<recommendation>
24+
25+
<p>
26+
Use randomly generated key material, initialization vectors and salts. Use strong passwords that are not hardcoded in source code.
27+
</p>
28+
29+
</recommendation>
30+
<example>
31+
32+
<p>
33+
The following example shows instantiating a cipher with hardcoded key material, making the encrypted data vulnerable to recovery.
34+
</p>
35+
36+
<sample src="HardcodedCryptographicValueBad.rs" />
37+
38+
<p>
39+
In the fixed code below, the key material is randomly generated and not hardcoded, which protects the encrypted data against recovery. A real application would also need a strategy for secure key management after the key has been generated.
40+
</p>
41+
42+
<sample src="HardcodedCryptographicValueGood.rs" />
43+
44+
</example>
45+
<references>
46+
47+
<li>
48+
OWASP: <a href="https://www.owasp.org/index.php/Use_of_hard-coded_password">Use of hard-coded password</a>.
49+
</li>
50+
<li>
51+
OWASP: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Key_Management_Cheat_Sheet.html">Key Management Cheat Sheet</a>.
52+
</li>
53+
<li>
54+
O'Reilly: <a href="https://www.oreilly.com/library/view/secure-programming-cookbook/0596003943/ch04s09.html">Using Salts, Nonces, and Initialization Vectors</a>.
55+
</li>
56+
57+
</references>
58+
</qhelp>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let key: [u8;32] = [0;32]; // BAD: Using hardcoded keys for encryption
2+
let cipher = Aes256Gcm::new(&key.into());
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let key = Aes256Gcm::generate_key(aes_gcm::aead::OsRng); // GOOD: Using randomly generated keys for encryption
2+
let cipher = Aes256Gcm::new(&key);

0 commit comments

Comments
 (0)