Skip to content

Commit 72ba77d

Browse files
Add rule that checks for using the insecure ECB block mode for encryption
1 parent 5e189b8 commit 72ba77d

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

swift/ql/src/queries/Security/ECB-Encryption/ECBEncryption.qhelp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"qhelp.dtd">
44
<qhelp>
55
<overview>
6-
<p>ECB should not be used as a mode for encryption as it has dangerous weaknesses. Data is encrypted the same way every time, which means that the same plaintext input will always produce the same ciphertext. This behavior makes messages encrypted with ECB
6+
<p>ECB should not be used as a mode for encryption as it has dangerous weaknesses. Data is encrypted the same way every time, which means that the same plaintext input will always produce the same ciphertext. This behavior makes messages encrypted with ECB
77
more vulnerable to replay attacks.</p>
88
</overview>
99

swift/ql/test/query-tests/Security/ECB-Encryption/ECBEncryption.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ subpaths
2727
| test.swift:55:37:55:53 | call to getECBBlockMode() | test.swift:34:9:34:13 | call to init() : | test.swift:55:37:55:53 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:34:9:34:13 | call to init() : | call to init() |
2828
| test.swift:65:42:65:42 | ecb | test.swift:45:12:45:16 | call to init() : | test.swift:65:42:65:42 | ecb | The initialization of the cipher 'ecb' uses the insecure ECB block mode from $@. | test.swift:45:12:45:16 | call to init() : | call to init() |
2929
| test.swift:66:42:66:46 | call to init() | test.swift:66:42:66:46 | call to init() | test.swift:66:42:66:46 | call to init() | The initialization of the cipher 'call to init()' uses the insecure ECB block mode from $@. | test.swift:66:42:66:46 | call to init() | call to init() |
30-
| test.swift:67:42:67:58 | call to getECBBlockMode() | test.swift:34:9:34:13 | call to init() : | test.swift:67:42:67:58 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:34:9:34:13 | call to init() : | call to init() |
30+
| test.swift:67:42:67:58 | call to getECBBlockMode() | test.swift:34:9:34:13 | call to init() : | test.swift:67:42:67:58 | call to getECBBlockMode() | The initialization of the cipher 'call to getECBBlockMode()' uses the insecure ECB block mode from $@. | test.swift:34:9:34:13 | call to init() : | call to init() |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
// --- stubs ---
3+
4+
// These stubs roughly follows the same structure as classes from CryptoSwift
5+
class AES
6+
{
7+
init(key: Array<UInt8>, blockMode: BlockMode, padding: Padding) { }
8+
init(key: Array<UInt8>, blockMode: BlockMode) { }
9+
}
10+
11+
protocol BlockMode { }
12+
13+
struct ECB: BlockMode {
14+
init() { }
15+
}
16+
17+
struct CBC: BlockMode {
18+
init() { }
19+
}
20+
21+
protocol PaddingProtocol { }
22+
23+
enum Padding: PaddingProtocol {
24+
case noPadding, zeroPadding, pkcs7, pkcs5, eme_pkcs1v15, emsa_pkcs1v15, iso78164, iso10126
25+
}
26+
27+
28+
// --- tests ---
29+
30+
func test1() {
31+
let key: Array<UInt8> = [0x2a, 0x3a, 0x80, 0x05, 0xaf, 0x46, 0x58, 0x2d, 0x66, 0x52, 0x10, 0xae, 0x86, 0xd3, 0x8e, 0x8f]
32+
let ecb = ECB()
33+
let cbc = CBC()
34+
let padding = Padding.noPadding
35+
36+
let b1 = AES(key: key, blockMode: ecb, padding: padding) // BAD
37+
let b2 = AES(key: key, blockMode: ecb) // BAD
38+
let b3 = AES(key: key, blockMode: ECB(), padding: padding) // BAD
39+
let b4 = AES(key: key, blockMode: ECB()) // BAD
40+
41+
let g1 = AES(key: key, blockMode: cbc, padding: padding) // GOOD
42+
let g2 = AES(key: key, blockMode: cbc) // GOOD
43+
let g3 = AES(key: key, blockMode: CBC(), padding: padding) // GOOD
44+
let g4 = AES(key: key, blockMode: CBC()) // GOOD
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
// --- stubs ---
3+
4+
// These stubs roughly follows the same structure as classes from CryptoSwift
5+
class Blowfish
6+
{
7+
init(key: Array<UInt8>, blockMode: BlockMode, padding: Padding) { }
8+
init(key: Array<UInt8>, blockMode: BlockMode) { }
9+
}
10+
11+
protocol BlockMode { }
12+
13+
struct ECB: BlockMode {
14+
init() { }
15+
}
16+
17+
struct CBC: BlockMode {
18+
init() { }
19+
}
20+
21+
protocol PaddingProtocol { }
22+
23+
enum Padding: PaddingProtocol {
24+
case noPadding, zeroPadding, pkcs7, pkcs5, eme_pkcs1v15, emsa_pkcs1v15, iso78164, iso10126
25+
}
26+
27+
28+
// --- tests ---
29+
30+
func test1() {
31+
let key: Array<UInt8> = [0x2a, 0x3a, 0x80, 0x05, 0xaf, 0x46, 0x58, 0x2d, 0x66, 0x52, 0x10, 0xae, 0x86, 0xd3, 0x8e, 0x8f]
32+
let ecb = ECB()
33+
let cbc = CBC()
34+
let padding = Padding.noPadding
35+
36+
let b1 = Blowfish(key: key, blockMode: ecb, padding: padding) // BAD
37+
let b2 = Blowfish(key: key, blockMode: ECB(), padding: padding) // BAD
38+
39+
let g1 = Blowfish(key: key, blockMode: cbc, padding: padding) // GOOD
40+
let g2 = Blowfish(key: key, blockMode: CBC(), padding: padding) // GOOD
41+
}

0 commit comments

Comments
 (0)