@@ -6,79 +6,20 @@ import go
6
6
7
7
/**
8
8
* Names of cryptographic algorithms, separated into strong and weak variants.
9
- *
10
9
* The names are normalized: upper-case, no spaces, dashes or underscores.
11
- *
12
10
* The names are inspired by the names used in real world crypto libraries.
13
- *
14
11
* The classification into strong and weak are based on Wikipedia, OWASP and google (2017).
15
12
*/
16
13
private module AlgorithmNames {
17
- predicate isStrongHashingAlgorithm ( string name ) {
18
- name = "DSA" or
19
- name = "ED25519" or
20
- name = "ES256" or
21
- name = "ECDSA256" or
22
- name = "ES384" or
23
- name = "ECDSA384" or
24
- name = "ES512" or
25
- name = "ECDSA512" or
26
- name = "SHA2" or
27
- name = "SHA224" or
28
- name = "SHA256" or
29
- name = "SHA384" or
30
- name = "SHA512" or
31
- name = "SHA3"
32
- }
33
-
34
14
predicate isWeakHashingAlgorithm ( string name ) {
35
- name = "HAVEL128" or
36
- name = "MD2" or
37
- name = "MD4" or
38
15
name = "MD5" or
39
- name = "PANAMA" or
40
- name = "RIPEMD" or
41
- name = "RIPEMD128" or
42
- name = "RIPEMD256" or
43
- name = "RIPEMD320" or
44
- name = "SHA0" or
45
16
name = "SHA1"
46
17
}
47
18
48
- predicate isStrongEncryptionAlgorithm ( string name ) {
49
- name = "AES" or
50
- name = "AES128" or
51
- name = "AES192" or
52
- name = "AES256" or
53
- name = "AES512" or
54
- name = "RSA" or
55
- name = "RABBIT" or
56
- name = "BLOWFISH"
57
- }
58
-
59
19
predicate isWeakEncryptionAlgorithm ( string name ) {
60
20
name = "DES" or
61
- name = "3DES" or
62
- name = "TRIPLEDES" or
63
- name = "TDEA" or
64
- name = "TRIPLEDEA" or
65
- name = "ARC2" or
66
- name = "RC2" or
67
- name = "ARC4" or
68
- name = "RC4" or
69
- name = "ARCFOUR" or
70
- name = "ARC5" or
71
- name = "RC5"
72
- }
73
-
74
- predicate isStrongPasswordHashingAlgorithm ( string name ) {
75
- name = "ARGON2" or
76
- name = "PBKDF2" or
77
- name = "BCRYPT" or
78
- name = "SCRYPT"
21
+ name = "RC4"
79
22
}
80
-
81
- predicate isWeakPasswordHashingAlgorithm ( string name ) { none ( ) }
82
23
}
83
24
84
25
private import AlgorithmNames
@@ -87,20 +28,9 @@ private import AlgorithmNames
87
28
* A cryptographic algorithm.
88
29
*/
89
30
private newtype TCryptographicAlgorithm =
90
- MkHashingAlgorithm ( string name , boolean isWeak ) {
91
- isStrongHashingAlgorithm ( name ) and isWeak = false
92
- or
93
- isWeakHashingAlgorithm ( name ) and isWeak = true
94
- } or
31
+ MkHashingAlgorithm ( string name , boolean isWeak ) { isWeakHashingAlgorithm ( name ) and isWeak = true } or
95
32
MkEncryptionAlgorithm ( string name , boolean isWeak ) {
96
- isStrongEncryptionAlgorithm ( name ) and isWeak = false
97
- or
98
33
isWeakEncryptionAlgorithm ( name ) and isWeak = true
99
- } or
100
- MkPasswordHashingAlgorithm ( string name , boolean isWeak ) {
101
- isStrongPasswordHashingAlgorithm ( name ) and isWeak = false
102
- or
103
- isWeakPasswordHashingAlgorithm ( name ) and isWeak = true
104
34
}
105
35
106
36
/**
@@ -116,13 +46,11 @@ abstract class CryptographicAlgorithm extends TCryptographicAlgorithm {
116
46
abstract string getName ( ) ;
117
47
118
48
/**
119
- * Holds if the name of this algorithm matches `name` modulo case,
120
- * white space, dashes and underscores.
49
+ * Holds if the name of this algorithm matches `name` modulo case, white space, dashes and underscores.
121
50
*/
122
51
bindingset [ name]
123
52
predicate matchesName ( string name ) {
124
53
exists ( name .regexpReplaceAll ( "[-_]" , "" ) .regexpFind ( "(?i)\\Q" + getName ( ) + "\\E" , _, _) )
125
- // name.toUpperCase().regexpReplaceAll("[-_ ]", "").regexpMatch(".*" + getName() + ".*")
126
54
}
127
55
128
56
/**
@@ -132,7 +60,7 @@ abstract class CryptographicAlgorithm extends TCryptographicAlgorithm {
132
60
}
133
61
134
62
/**
135
- * A hashing algorithm such as `MD5` or `SHA512`.
63
+ * A hashing algorithm
136
64
*/
137
65
class HashingAlgorithm extends MkHashingAlgorithm , CryptographicAlgorithm {
138
66
string name ;
@@ -146,7 +74,7 @@ class HashingAlgorithm extends MkHashingAlgorithm, CryptographicAlgorithm {
146
74
}
147
75
148
76
/**
149
- * An encryption algorithm such as `DES` or `AES512`.
77
+ * An encryption algorithm
150
78
*/
151
79
class EncryptionAlgorithm extends MkEncryptionAlgorithm , CryptographicAlgorithm {
152
80
string name ;
@@ -159,41 +87,20 @@ class EncryptionAlgorithm extends MkEncryptionAlgorithm, CryptographicAlgorithm
159
87
override predicate isWeak ( ) { isWeak = true }
160
88
}
161
89
162
- /**
163
- * A password hashing algorithm such as `PBKDF2` or `SCRYPT`.
164
- */
165
- class PasswordHashingAlgorithm extends MkPasswordHashingAlgorithm , CryptographicAlgorithm {
166
- string name ;
167
- boolean isWeak ;
168
-
169
- PasswordHashingAlgorithm ( ) { this = MkPasswordHashingAlgorithm ( name , isWeak ) }
170
-
171
- override string getName ( ) { result = name }
172
-
173
- override predicate isWeak ( ) { isWeak = true }
174
- }
175
-
176
90
/**
177
91
* An application of a cryptographic algorithm.
178
92
*/
179
93
abstract class CryptographicOperation extends Expr {
180
- /**
181
- * Gets the input the algorithm is used on, e.g. the plain text input to be encrypted.
182
- */
183
- abstract Expr getInput ( ) ;
184
-
185
94
/**
186
95
* Gets the applied algorithm.
187
96
*/
188
97
abstract CryptographicAlgorithm getAlgorithm ( ) ;
189
98
}
190
99
191
100
/**
192
- * Below are the cryptographic functions that have been implemented so far for this library.
193
101
* Class that checks for use of Md5 package.
194
102
*/
195
103
class Md5 extends CryptographicOperation {
196
- Expr input ;
197
104
CryptographicAlgorithm algorithm ;
198
105
SelectorExpr sel ;
199
106
CallExpr call ;
@@ -203,10 +110,33 @@ class Md5 extends CryptographicOperation {
203
110
algorithm .matchesName ( sel .getBase ( ) .toString ( ) ) and
204
111
algorithm .matchesName ( "MD5" ) and
205
112
sel .getSelector ( ) .toString ( ) = call .getCalleeName ( ) .toString ( ) and
206
- call .getArgument ( 0 ) = input
113
+ (
114
+ call .getCalleeName ( ) .toString ( ) = "New" or
115
+ call .getCalleeName ( ) .toString ( ) = "Sum"
116
+ )
207
117
}
208
118
209
- override Expr getInput ( ) { result = input }
119
+ override CryptographicAlgorithm getAlgorithm ( ) { result = algorithm }
120
+ }
121
+
122
+ /**
123
+ * Class that checks for use of SHA1 package.
124
+ */
125
+ class Sha1 extends CryptographicOperation {
126
+ CryptographicAlgorithm algorithm ;
127
+ SelectorExpr sel ;
128
+ CallExpr call ;
129
+
130
+ Sha1 ( ) {
131
+ this = call and
132
+ algorithm .matchesName ( sel .getBase ( ) .toString ( ) ) and
133
+ algorithm .matchesName ( "SHA1" ) and
134
+ sel .getSelector ( ) .toString ( ) = call .getCalleeName ( ) .toString ( ) and
135
+ (
136
+ call .getCalleeName ( ) .toString ( ) = "New" or
137
+ call .getCalleeName ( ) .toString ( ) = "Sum"
138
+ )
139
+ }
210
140
211
141
override CryptographicAlgorithm getAlgorithm ( ) { result = algorithm }
212
142
}
@@ -215,7 +145,6 @@ class Md5 extends CryptographicOperation {
215
145
* Class that checks for use of Des package.
216
146
*/
217
147
class Des extends CryptographicOperation {
218
- Expr input ;
219
148
CryptographicAlgorithm algorithm ;
220
149
SelectorExpr sel ;
221
150
CallExpr call ;
@@ -225,10 +154,30 @@ class Des extends CryptographicOperation {
225
154
algorithm .matchesName ( sel .getBase ( ) .toString ( ) ) and
226
155
algorithm .matchesName ( "DES" ) and
227
156
sel .getSelector ( ) .toString ( ) = call .getCalleeName ( ) .toString ( ) and
228
- call .getArgument ( 0 ) = input
157
+ (
158
+ call .getCalleeName ( ) .toString ( ) = "NewCipher" or
159
+ call .getCalleeName ( ) .toString ( ) = "NewTripleDESCipher"
160
+ )
229
161
}
230
162
231
- override Expr getInput ( ) { result = input }
163
+ override CryptographicAlgorithm getAlgorithm ( ) { result = algorithm }
164
+ }
165
+
166
+ /**
167
+ * Class that checks for use of RC4 package.
168
+ */
169
+ class Rc4 extends CryptographicOperation {
170
+ CryptographicAlgorithm algorithm ;
171
+ SelectorExpr sel ;
172
+ CallExpr call ;
173
+
174
+ Rc4 ( ) {
175
+ this = call and
176
+ algorithm .matchesName ( sel .getBase ( ) .toString ( ) ) and
177
+ algorithm .matchesName ( "RC4" ) and
178
+ sel .getSelector ( ) .toString ( ) = call .getCalleeName ( ) .toString ( ) and
179
+ call .getCalleeName ( ) .toString ( ) = "NewCipher"
180
+ }
232
181
233
182
override CryptographicAlgorithm getAlgorithm ( ) { result = algorithm }
234
183
}
0 commit comments