@@ -14,7 +14,7 @@ class EllipticCurveStringLiteralInstance extends Crypto::EllipticCurveInstance i
14
14
override string getRawEllipticCurveName ( ) { result = super .getValue ( ) }
15
15
16
16
EllipticCurveAlgorithmValueConsumer getConsumer ( ) {
17
- result = EllipticCurveStringLiteralToConsumer :: getConsumerFromLiteral ( this , _, _)
17
+ result = EllipticCurveStringLiteralToConsumerFlow :: getConsumerFromLiteral ( this , _, _)
18
18
}
19
19
20
20
override Crypto:: TEllipticCurveType getEllipticCurveType ( ) {
@@ -69,25 +69,19 @@ abstract class SignatureAlgorithmInstance extends Crypto::KeyOperationAlgorithmI
69
69
}
70
70
71
71
override string getRawAlgorithmName ( ) {
72
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
72
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
73
73
}
74
74
75
- /**
76
- * Used for data flow from elliptic curve string literals to the algorithm
77
- * instance.
78
- */
75
+ // Used for data flow from elliptic curve string literals to the algorithm
79
76
DataFlow:: Node getParametersInput ( ) { none ( ) }
80
77
81
- /**
82
- * Used for data flow from elliptic curve string literals to the algorithm
83
- * instance.
84
- */
78
+ // Used for data flow from elliptic curve string literals to the algorithm
85
79
DataFlow:: Node getEllipticCurveInput ( ) { none ( ) }
86
80
}
87
81
88
82
/**
89
- * Represents an elliptic curve signature algorithm where both the signature
90
- * algorithm and elliptic curve are implicitly defined by the underlying type.
83
+ * An elliptic curve signature algorithm where both the signature algorithm and
84
+ * elliptic curve are implicitly defined by the underlying type.
91
85
*/
92
86
abstract class KnownEllipticCurveSignatureAlgorithmInstance extends KnownEllipticCurveInstance ,
93
87
SignatureAlgorithmInstance
@@ -107,7 +101,7 @@ class DsaSignatureAlgorithmInstance extends SignatureAlgorithmInstance instanceo
107
101
}
108
102
109
103
override string getRawAlgorithmName ( ) {
110
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
104
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
111
105
}
112
106
}
113
107
@@ -122,7 +116,7 @@ class Ed25519SignatureAlgorithmInstance extends KnownEllipticCurveSignatureAlgor
122
116
}
123
117
124
118
override string getRawAlgorithmName ( ) {
125
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
119
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
126
120
}
127
121
128
122
override string getRawEllipticCurveName ( ) { result = "Curve25519" }
@@ -139,7 +133,7 @@ class Ed448SignatureAlgorithmInstance extends KnownEllipticCurveSignatureAlgorit
139
133
}
140
134
141
135
override string getRawAlgorithmName ( ) {
142
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
136
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
143
137
}
144
138
145
139
override string getRawEllipticCurveName ( ) { result = "Curve448" }
@@ -163,7 +157,7 @@ class EcdsaSignatureAlgorithmInstance extends SignatureAlgorithmInstance instanc
163
157
}
164
158
165
159
override string getRawAlgorithmName ( ) {
166
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
160
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
167
161
}
168
162
169
163
override Crypto:: KeyOpAlg:: Algorithm getAlgorithmType ( ) {
@@ -184,7 +178,7 @@ class StatefulSignatureAlgorithmInstance extends SignatureAlgorithmInstance inst
184
178
}
185
179
186
180
override string getRawAlgorithmName ( ) {
187
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
181
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
188
182
}
189
183
190
184
override Crypto:: KeyOpAlg:: Algorithm getAlgorithmType ( ) {
@@ -218,19 +212,15 @@ abstract class KeyGenerationAlgorithmInstance extends Crypto::KeyOperationAlgori
218
212
}
219
213
220
214
override string getRawAlgorithmName ( ) {
221
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
215
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
222
216
}
223
217
224
- /**
225
- * Used for data flow from elliptic curve string literals to the algorithm
226
- * instance.
227
- */
218
+ // Used for data flow from elliptic curve string literals to the algorithm
219
+ // instance.
228
220
DataFlow:: Node getParametersInput ( ) { none ( ) }
229
221
230
- /**
231
- * Used for data flow from elliptic curve string literals to the algorithm
232
- * instance.
233
- */
222
+ // Used for data flow from elliptic curve string literals to the algorithm
223
+ // instance.
234
224
DataFlow:: Node getEllipticCurveInput ( ) { none ( ) }
235
225
}
236
226
@@ -320,7 +310,7 @@ class StatefulSignatureKeyGenerationAlgorithmInstance extends KeyGenerationAlgor
320
310
}
321
311
322
312
override string getRawAlgorithmName ( ) {
323
- typeNameToRawAlgorithmName ( super .getConstructedType ( ) .getName ( ) , result )
313
+ typeNameToRawAlgorithmNameMapping ( super .getConstructedType ( ) .getName ( ) , result )
324
314
}
325
315
326
316
override Crypto:: KeyOpAlg:: Algorithm getAlgorithmType ( ) {
@@ -332,11 +322,103 @@ class StatefulSignatureKeyGenerationAlgorithmInstance extends KeyGenerationAlgor
332
322
}
333
323
}
334
324
325
+ /**
326
+ * A block cipher used in a mode of operation. The algorithm is implicitly
327
+ * defined by the type.
328
+ */
329
+ class BlockCipherAlgorithmInstance extends Crypto:: KeyOperationAlgorithmInstance instanceof ClassInstanceExpr
330
+ {
331
+ // We track the block cipher mode here to ensure that going from the block
332
+ // cipher instance to the block cipher mode instance and back always yields
333
+ // the same instance.
334
+ //
335
+ // Since the block cipher algorithm instance is always resolved using data
336
+ // flow from the block cipher mode, we don't loose any information by
337
+ // requiring that this flow exists.
338
+ BlockCipherModeAlgorithmInstance mode ;
339
+
340
+ BlockCipherAlgorithmInstance ( ) {
341
+ super .getConstructedType ( ) instanceof Modes:: BlockCipher and
342
+ mode = BlockCipherToBlockCipherModeFlow:: getBlockCipherModeFromBlockCipher ( this , _, _)
343
+ }
344
+
345
+ override Crypto:: KeyOpAlg:: Algorithm getAlgorithmType ( ) {
346
+ if blockCipherNameToAlgorithmMapping ( this .getRawAlgorithmName ( ) , _)
347
+ then blockCipherNameToAlgorithmMapping ( this .getRawAlgorithmName ( ) , result )
348
+ else result = Crypto:: KeyOpAlg:: TSymmetricCipher ( Crypto:: KeyOpAlg:: OtherSymmetricCipherType ( ) )
349
+ }
350
+
351
+ // TODO: Implement this.
352
+ override int getKeySizeFixed ( ) { none ( ) }
353
+
354
+ override string getRawAlgorithmName ( ) {
355
+ typeNameToRawAlgorithmNameMapping ( super .getType ( ) .getName ( ) , result )
356
+ }
357
+
358
+ override Crypto:: ModeOfOperationAlgorithmInstance getModeOfOperationAlgorithm ( ) { result = mode }
359
+
360
+ override Crypto:: PaddingAlgorithmInstance getPaddingAlgorithm ( ) {
361
+ result = BlockCipherToPaddingModeFlow:: getPaddingModeFromBlockCipher ( this )
362
+ }
363
+
364
+ override Crypto:: ConsumerInputDataFlowNode getKeySizeConsumer ( ) { none ( ) }
365
+
366
+ // Gets a consumer of this block cipher algorithm instance.
367
+ Crypto:: AlgorithmValueConsumer getConsumer ( ) { result = mode .getBlockCipherArg ( ) }
368
+ }
369
+
370
+ /**
371
+ * A block cipher mode instance.
372
+ */
373
+ class BlockCipherModeAlgorithmInstance extends Crypto:: ModeOfOperationAlgorithmInstance ,
374
+ BlockCipherModeAlgorithmValueConsumer instanceof ClassInstanceExpr
375
+ {
376
+ BlockCipherModeAlgorithmInstance ( ) {
377
+ super .getConstructedType ( ) instanceof Modes:: UnpaddedBlockCipherMode
378
+ }
379
+
380
+ override string getRawModeAlgorithmName ( ) {
381
+ result = super .getConstructedType ( ) .getName ( ) .splitAt ( "BlockCipher" , 0 )
382
+ }
383
+
384
+ override Crypto:: TBlockCipherModeOfOperationType getModeType ( ) {
385
+ if modeNameToModeTypeMapping ( this .getRawModeAlgorithmName ( ) , _)
386
+ then modeNameToModeTypeMapping ( this .getRawModeAlgorithmName ( ) , result )
387
+ else result = Crypto:: OtherMode ( )
388
+ }
389
+
390
+ Expr getBlockCipherArg ( ) {
391
+ exists ( Expr arg |
392
+ arg = super .getAnArgument ( ) and
393
+ arg .getType ( ) instanceof Modes:: BlockCipher and
394
+ result = arg
395
+ )
396
+ }
397
+
398
+ Crypto:: AlgorithmValueConsumer getConsumer ( ) { result = this }
399
+ }
400
+
401
+ /**
402
+ * A padding mode instance implicitly determined by the constructor.
403
+ */
404
+ class PaddingAlgorithmInstance extends Crypto:: PaddingAlgorithmInstance instanceof ClassInstanceExpr
405
+ {
406
+ PaddingAlgorithmInstance ( ) { super .getConstructedType ( ) instanceof Modes:: PaddingMode }
407
+
408
+ override Crypto:: TPaddingType getPaddingType ( ) {
409
+ paddingNameToTypeMapping ( this .getRawPaddingAlgorithmName ( ) , result )
410
+ }
411
+
412
+ override string getRawPaddingAlgorithmName ( ) {
413
+ result = super .getConstructedType ( ) .getName ( ) .splitAt ( "Padding" , 0 )
414
+ }
415
+ }
416
+
335
417
/**
336
418
* Private predicates mapping type names to raw names, key sizes and algorithms.
337
419
*/
338
420
bindingset [ typeName]
339
- private predicate typeNameToRawAlgorithmName ( string typeName , string algorithmName ) {
421
+ private predicate typeNameToRawAlgorithmNameMapping ( string typeName , string algorithmName ) {
340
422
// Ed25519, Ed25519ph, and Ed25519ctx key generators and signers
341
423
typeName .matches ( "Ed25519%" ) and
342
424
algorithmName = "Ed25519"
@@ -356,6 +438,66 @@ private predicate typeNameToRawAlgorithmName(string typeName, string algorithmNa
356
438
// HSS
357
439
typeName .matches ( "HSS%" ) and
358
440
algorithmName = "HSS"
441
+ or
442
+ typeName .matches ( "AES%" ) and
443
+ algorithmName = "AES"
444
+ or
445
+ typeName .matches ( "Aria%" ) and
446
+ algorithmName = "Aria"
447
+ or
448
+ typeName .matches ( "Blowfish%" ) and
449
+ algorithmName = "Blowfish"
450
+ or
451
+ typeName .matches ( "DES%" ) and
452
+ algorithmName = "DES"
453
+ or
454
+ typeName .matches ( "TripleDES%" ) and
455
+ algorithmName = "TripleDES"
456
+ }
457
+
458
+ private predicate modeNameToModeTypeMapping (
459
+ string modeName , Crypto:: TBlockCipherModeOfOperationType modeType
460
+ ) {
461
+ modeName = "CBC" and
462
+ modeType = Crypto:: CBC ( )
463
+ or
464
+ modeName = "CCM" and
465
+ modeType = Crypto:: CCM ( )
466
+ or
467
+ modeName = "CFB" and
468
+ modeType = Crypto:: CFB ( )
469
+ or
470
+ modeName = "CTR" and
471
+ modeType = Crypto:: CTR ( )
472
+ or
473
+ modeName = "ECB" and
474
+ modeType = Crypto:: ECB ( )
475
+ or
476
+ modeName = "GCM" and
477
+ modeType = Crypto:: GCM ( )
478
+ or
479
+ modeName = "OCB" and
480
+ modeType = Crypto:: OCB ( )
481
+ or
482
+ modeName = "OFB" and
483
+ modeType = Crypto:: OFB ( )
484
+ or
485
+ modeName = "XTS" and
486
+ modeType = Crypto:: XTS ( )
487
+ }
488
+
489
+ private predicate paddingNameToTypeMapping ( string paddingName , Crypto:: TPaddingType paddingType ) {
490
+ paddingName = "NoPadding" and
491
+ paddingType = Crypto:: NoPadding ( )
492
+ or
493
+ paddingName = "PKCS7" and
494
+ paddingType = Crypto:: PKCS7 ( )
495
+ or
496
+ paddingName = "ISO10126" and
497
+ paddingType = Crypto:: OtherPadding ( )
498
+ or
499
+ paddingName = "ZeroByte" and
500
+ paddingType = Crypto:: OtherPadding ( )
359
501
}
360
502
361
503
private predicate signatureNameToKeySizeAndAlgorithmMapping (
@@ -381,3 +523,22 @@ private predicate generatorNameToKeySizeAndAlgorithmMapping(
381
523
keySize = 448 and
382
524
algorithm = Crypto:: KeyOpAlg:: TSignature ( Crypto:: KeyOpAlg:: Ed448 ( ) )
383
525
}
526
+
527
+ private predicate blockCipherNameToAlgorithmMapping (
528
+ string name , Crypto:: KeyOpAlg:: Algorithm algorithm
529
+ ) {
530
+ name = "AES" and
531
+ algorithm = Crypto:: KeyOpAlg:: TSymmetricCipher ( Crypto:: KeyOpAlg:: AES ( ) )
532
+ or
533
+ name = "Aria" and
534
+ algorithm = Crypto:: KeyOpAlg:: TSymmetricCipher ( Crypto:: KeyOpAlg:: ARIA ( ) )
535
+ or
536
+ name = "Blowfish" and
537
+ algorithm = Crypto:: KeyOpAlg:: TSymmetricCipher ( Crypto:: KeyOpAlg:: BLOWFISH ( ) )
538
+ or
539
+ name = "DES" and
540
+ algorithm = Crypto:: KeyOpAlg:: TSymmetricCipher ( Crypto:: KeyOpAlg:: DES ( ) )
541
+ or
542
+ name = "TripleDES" and
543
+ algorithm = Crypto:: KeyOpAlg:: TSymmetricCipher ( Crypto:: KeyOpAlg:: TripleDES ( ) )
544
+ }
0 commit comments