forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithmInstances.qll
More file actions
358 lines (310 loc) · 11.1 KB
/
AlgorithmInstances.qll
File metadata and controls
358 lines (310 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
private import java
private import experimental.quantum.Language
private import AlgorithmValueConsumers
private import OperationInstances
private import FlowAnalysis
/**
* A string literal that represents an elliptic curve name.
*/
class EllipticCurveStringLiteralInstance extends Crypto::EllipticCurveInstance instanceof StringLiteral
{
EllipticCurveStringLiteralInstance() {
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(super.getValue().toUpperCase(), _, _)
}
override string getRawEllipticCurveName() { result = super.getValue() }
Crypto::AlgorithmValueConsumer getConsumer() {
result = EllipticCurveStringLiteralToConsumerFlow::getConsumerFromLiteral(this, _, _)
}
override Crypto::TEllipticCurveType getEllipticCurveType() {
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.getRawEllipticCurveName().toUpperCase(),
_, result)
}
override int getKeySize() {
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.getRawEllipticCurveName().toUpperCase(),
result, _)
}
}
/**
* A signature algorithm instance where the algorithm is implicitly defined by
* the constructed type.
*/
class ImplicitSignatureClassInstanceExpr extends Crypto::KeyOperationAlgorithmInstance,
ImplicitAlgorithmValueConsumer instanceof ClassInstanceExpr
{
ImplicitSignatureClassInstanceExpr() { super.getConstructedType() instanceof Signers::Signer }
override Crypto::ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm() { none() }
override Crypto::PaddingAlgorithmInstance getPaddingAlgorithm() { none() }
override Crypto::ConsumerInputDataFlowNode getKeySizeConsumer() { none() }
override Crypto::KeyOpAlg::Algorithm getAlgorithmType() {
signatureNameToAlgorithmMapping(this.getRawAlgorithmName(), result)
}
override int getKeySizeFixed() {
signatureNameToKeySizeMapping(this.getRawAlgorithmName(), result)
}
override string getRawAlgorithmName() {
typeNameToRawAlgorithmNameMapping(super.getConstructedType().getName(), result)
}
// Used for data flow from elliptic curve string literals to the algorithm
// instance.
DataFlow::Node getParametersInput() { none() }
// Used for data flow from elliptic curve string literals to the algorithm
// instance.
DataFlow::Node getEllipticCurveInput() { none() }
}
/**
* A key generation algorithm instance where algorithm is a key operation (e.g.
* a signature algorithm) implicitly defined by the constructed type.
*/
class ImplicitKeyGenerationClassInstanceExpr extends Crypto::KeyOperationAlgorithmInstance,
ImplicitAlgorithmValueConsumer instanceof ClassInstanceExpr
{
ImplicitKeyGenerationClassInstanceExpr() {
super.getConstructedType() instanceof Generators::KeyGenerator and
super.getConstructedType().getName().matches(["Ed25519%", "Ed448%", "LMS%", "HSS%"])
}
override string getRawAlgorithmName() {
typeNameToRawAlgorithmNameMapping(super.getConstructedType().getName(), result)
}
override Crypto::ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm() { none() }
override Crypto::PaddingAlgorithmInstance getPaddingAlgorithm() { none() }
override Crypto::ConsumerInputDataFlowNode getKeySizeConsumer() { none() }
override Crypto::KeyOpAlg::Algorithm getAlgorithmType() {
generatorNameToAlgorithmMapping(this.getRawAlgorithmName(), result)
}
override int getKeySizeFixed() {
generatorNameToKeySizeMapping(this.getRawAlgorithmName(), result)
}
// Used for data flow from elliptic curve string literals to the algorithm
// instance.
DataFlow::Node getParametersInput() { none() }
// Used for data flow from elliptic curve string literals to the algorithm
// instance.
DataFlow::Node getEllipticCurveInput() { none() }
}
/**
* A block cipher used in a mode of operation. The algorithm is implicitly
* defined by the type.
*/
class BlockCipherAlgorithmInstance extends Crypto::KeyOperationAlgorithmInstance instanceof ClassInstanceExpr
{
// We track the block cipher mode here to ensure that going from the block
// cipher instance to the block cipher mode instance and back always yields
// the same instance.
//
// Since the block cipher algorithm instance is always resolved using data
// flow from the block cipher mode, we don't loose any information by
// requiring that this flow exists.
BlockCipherModeAlgorithmInstance mode;
BlockCipherAlgorithmInstance() {
super.getConstructedType() instanceof Modes::BlockCipher and
mode = BlockCipherToBlockCipherModeFlow::getBlockCipherModeFromBlockCipher(this, _, _)
}
override Crypto::KeyOpAlg::Algorithm getAlgorithmType() {
if blockCipherNameToAlgorithmMapping(this.getRawAlgorithmName(), _)
then blockCipherNameToAlgorithmMapping(this.getRawAlgorithmName(), result)
else result = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::OtherSymmetricCipherType())
}
// TODO: Implement this.
override int getKeySizeFixed() { none() }
override string getRawAlgorithmName() {
typeNameToRawAlgorithmNameMapping(super.getType().getName(), result)
}
override Crypto::ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm() { result = mode }
override Crypto::PaddingAlgorithmInstance getPaddingAlgorithm() {
result = BlockCipherToPaddingModeFlow::getPaddingModeFromBlockCipher(this)
}
override Crypto::ConsumerInputDataFlowNode getKeySizeConsumer() { none() }
// Gets a consumer of this block cipher algorithm instance.
Crypto::AlgorithmValueConsumer getConsumer() { result = mode.getBlockCipherArg() }
}
/**
* A block cipher mode instance.
*/
class BlockCipherModeAlgorithmInstance extends Crypto::ModeOfOperationAlgorithmInstance,
ImplicitAlgorithmValueConsumer instanceof ClassInstanceExpr
{
BlockCipherModeAlgorithmInstance() {
super.getConstructedType() instanceof Modes::UnpaddedBlockCipherMode
}
override string getRawModeAlgorithmName() {
result = super.getConstructedType().getName().splitAt("BlockCipher", 0)
}
override Crypto::TBlockCipherModeOfOperationType getModeType() {
if modeNameToModeTypeMapping(this.getRawModeAlgorithmName(), _)
then modeNameToModeTypeMapping(this.getRawModeAlgorithmName(), result)
else result = Crypto::OtherMode()
}
Expr getBlockCipherArg() {
exists(Expr arg |
arg = super.getAnArgument() and
arg.getType() instanceof Modes::BlockCipher and
result = arg
)
}
Crypto::AlgorithmValueConsumer getConsumer() { result = this }
}
/**
* A padding mode instance implicitly determined by the constructor.
*/
class PaddingAlgorithmInstance extends Crypto::PaddingAlgorithmInstance instanceof ClassInstanceExpr
{
PaddingAlgorithmInstance() { super.getConstructedType() instanceof Modes::PaddingMode }
override Crypto::TPaddingType getPaddingType() {
paddingNameToTypeMapping(this.getRawPaddingAlgorithmName(), result)
}
override string getRawPaddingAlgorithmName() {
result = super.getConstructedType().getName().splitAt("Padding", 0)
}
}
/**
* Private predicates mapping type names to raw names, key sizes and algorithms.
*/
bindingset[typeName]
private predicate typeNameToRawAlgorithmNameMapping(string typeName, string algorithmName) {
// Ed25519, Ed25519ph, and Ed25519ctx key generators and signers
typeName.matches("Ed25519%") and
algorithmName = "Ed25519"
or
// Ed448 and Ed448ph key generators and signers
typeName.matches("Ed448%") and
algorithmName = "Ed448"
or
// ECDSA
typeName.matches("ECDSA%") and
algorithmName = "ECDSA"
or
// DSA
typeName.matches("DSA%") and
algorithmName = "DSA"
or
// LMS
typeName.matches("LMS%") and
algorithmName = "LMS"
or
// HSS
typeName.matches("HSS%") and
algorithmName = "HSS"
or
typeName.matches("AES%") and
algorithmName = "AES"
or
typeName.matches("Aria%") and
algorithmName = "Aria"
or
typeName.matches("Blowfish%") and
algorithmName = "Blowfish"
or
typeName.matches("DES%") and
algorithmName = "DES"
or
typeName.matches("TripleDES%") and
algorithmName = "TripleDES"
}
private predicate modeNameToModeTypeMapping(
string modeName, Crypto::TBlockCipherModeOfOperationType modeType
) {
modeName = "CBC" and
modeType = Crypto::CBC()
or
modeName = "CCM" and
modeType = Crypto::CCM()
or
modeName = "CFB" and
modeType = Crypto::CFB()
or
modeName = "CTR" and
modeType = Crypto::CTR()
or
modeName = "ECB" and
modeType = Crypto::ECB()
or
modeName = "GCM" and
modeType = Crypto::GCM()
or
modeName = "OCB" and
modeType = Crypto::OCB()
or
modeName = "OFB" and
modeType = Crypto::OFB()
or
modeName = "XTS" and
modeType = Crypto::XTS()
}
private predicate paddingNameToTypeMapping(string paddingName, Crypto::TPaddingType paddingType) {
paddingName = "NoPadding" and
paddingType = Crypto::NoPadding()
or
paddingName = "PKCS7" and
paddingType = Crypto::PKCS7()
or
paddingName = "ISO10126" and
paddingType = Crypto::OtherPadding()
or
paddingName = "ZeroByte" and
paddingType = Crypto::OtherPadding()
}
private predicate signatureNameToAlgorithmMapping(
string signatureName, Crypto::KeyOpAlg::Algorithm algorithmType
) {
signatureName = "Ed25519" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::Ed25519())
or
signatureName = "Ed448" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::Ed448())
or
signatureName = "ECDSA" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::ECDSA())
or
signatureName = "LMS" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::LMS())
or
signatureName = "HSS" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::HSS())
}
private predicate signatureNameToKeySizeMapping(string signatureName, int keySize) {
signatureName = "Ed25519" and
keySize = 256
or
signatureName = "Ed448" and
keySize = 448
}
private predicate generatorNameToAlgorithmMapping(
string generatorName, Crypto::KeyOpAlg::Algorithm algorithmType
) {
generatorName = "Ed25519" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::Ed25519())
or
generatorName = "Ed448" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::Ed448())
or
generatorName = "LMS" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::LMS())
or
generatorName = "HSS" and
algorithmType = Crypto::KeyOpAlg::TSignature(Crypto::KeyOpAlg::HSS())
}
private predicate generatorNameToKeySizeMapping(string generatorName, int keySize) {
generatorName = "Ed25519" and
keySize = 256
or
generatorName = "Ed448" and
keySize = 448
}
private predicate blockCipherNameToAlgorithmMapping(
string cipherName, Crypto::KeyOpAlg::Algorithm algorithmType
) {
cipherName = "AES" and
algorithmType = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::AES())
or
cipherName = "Aria" and
algorithmType = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::ARIA())
or
cipherName = "Blowfish" and
algorithmType = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::BLOWFISH())
or
cipherName = "DES" and
algorithmType = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::DES())
or
cipherName = "TripleDES" and
algorithmType = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::TripleDES())
}