Skip to content

Commit 5fc267a

Browse files
committed
Added support for ChaCha20Poly1305
1 parent e643cc4 commit 5fc267a

File tree

5 files changed

+46
-27
lines changed

5 files changed

+46
-27
lines changed

csharp/ql/lib/experimental/quantum/dotnet/AlgorithmInstances.qll

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ abstract class SigningAlgorithmInstance extends Crypto::KeyOperationAlgorithmIns
2727

2828
override Crypto::ConsumerInputDataFlowNode getKeySizeConsumer() { none() }
2929

30-
3130
override int getKeySizeFixed() { none() }
3231
}
3332

@@ -160,28 +159,37 @@ class CipherModeLiteralInstance extends Crypto::ModeOfOperationAlgorithmInstance
160159
}
161160

162161
/**
163-
* A call to either `Encrypt` or `Decrypt` on an `AesGcm` or `AesCcm` instance.
164-
* The algorithm is defined implicitly by this AST node.
162+
* A call to either `Encrypt` or `Decrypt` on an `AesGcm`, `AesCcm`, or
163+
* `ChaCha20Poly1305` instance. The algorithm is defined implicitly by this AST
164+
* node.
165165
*/
166-
class AesModeAlgorithmInstance extends Crypto::KeyOperationAlgorithmInstance,
167-
Crypto::ModeOfOperationAlgorithmInstance instanceof AesModeUse
166+
class AeadAlgorithmInstance extends Crypto::KeyOperationAlgorithmInstance,
167+
Crypto::ModeOfOperationAlgorithmInstance instanceof AeadUse
168168
{
169-
override string getRawAlgorithmName() { result = "Aes" }
169+
override string getRawAlgorithmName() {
170+
super.getQualifier().getType().hasName("Aes%") and result = "Aes"
171+
or
172+
super.getQualifier().getType().hasName("ChaCha20%") and result = "ChaCha20"
173+
}
170174

171175
override string getRawModeAlgorithmName() {
172-
this.getRawAlgorithmName() = "AesGcm" and result = "Gcm"
176+
super.getQualifier().getType().getName() = "AesGcm" and result = "Gcm"
173177
or
174-
this.getRawAlgorithmName() = "AesCcm" and result = "Ccm"
178+
super.getQualifier().getType().getName() = "AesCcm" and result = "Ccm"
175179
}
176180

177181
override Crypto::KeyOpAlg::Algorithm getAlgorithmType() {
182+
this.getRawAlgorithmName() = "Aes" and
178183
result = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::AES())
184+
or
185+
this.getRawAlgorithmName() = "ChaCha20" and
186+
result = Crypto::KeyOpAlg::TSymmetricCipher(Crypto::KeyOpAlg::CHACHA20())
179187
}
180188

181189
override Crypto::TBlockCipherModeOfOperationType getModeType() {
182-
this.getRawAlgorithmName() = "AesGcm" and result = Crypto::GCM()
190+
this.getRawModeAlgorithmName() = "Gcm" and result = Crypto::GCM()
183191
or
184-
this.getRawAlgorithmName() = "AesCcm" and result = Crypto::CCM()
192+
this.getRawModeAlgorithmName() = "Ccm" and result = Crypto::CCM()
185193
}
186194

187195
override int getKeySizeFixed() { none() }

csharp/ql/lib/experimental/quantum/dotnet/AlgorithmValueConsumers.qll

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ class SymmetricAlgorithmConsumer extends Crypto::AlgorithmValueConsumer instance
7171
}
7272

7373
/**
74-
* A call to either `Encrypt` or `Decrypt` on an `AesGcm` or `AesCcm` instance.
75-
* The algorithm is defined implicitly by this AST node.
74+
* A call to either `Encrypt` or `Decrypt` on an `AesGcm`, `AesCcm` or
75+
* `ChaCha20Poly1305` instance. The algorithm is defined implicitly by this AST
76+
* node.
7677
*/
77-
class AesModeAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer instanceof AesModeUse {
78+
class AeadAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer instanceof AeadUse {
7879
override Crypto::ConsumerInputDataFlowNode getInputNode() { none() }
7980

80-
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() { result = this }
81+
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {
82+
// See `AeadAlgorithmInstance` for the algorithm instance.
83+
result = this
84+
}
8185
}

csharp/ql/lib/experimental/quantum/dotnet/Cryptography.qll

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,25 @@ private class RSASigner extends SignerUse {
311311
RSASigner() { this.getQualifier().getType() instanceof RSAClass }
312312
}
313313

314-
class AesMode extends Class {
315-
AesMode() { this.hasFullyQualifiedName("System.Security.Cryptography", ["AesGcm", "AesCcm"]) }
314+
/**
315+
* An AEAD class, such as `AesGcm`, `AesCcm`, or `ChaCha20Poly1305`.
316+
*/
317+
class Aead extends Class {
318+
Aead() {
319+
this.hasFullyQualifiedName("System.Security.Cryptography",
320+
["AesGcm", "AesCcm", "ChaCha20Poly1305"])
321+
}
316322
}
317323

318-
class AesModeCreation extends ObjectCreation {
319-
AesModeCreation() { this.getObjectType() instanceof AesMode }
324+
class AeadCreation extends ObjectCreation {
325+
AeadCreation() { this.getObjectType() instanceof Aead }
320326

321327
Expr getKeyArg() { result = this.getArgument(0) }
322328
}
323329

324-
class AesModeUse extends MethodCall {
325-
AesModeUse() {
326-
this.getQualifier().getType() instanceof AesMode and
330+
class AeadUse extends MethodCall {
331+
AeadUse() {
332+
this.getQualifier().getType() instanceof Aead and
327333
this.getTarget().hasName(["Encrypt", "Decrypt"])
328334
}
329335

csharp/ql/lib/experimental/quantum/dotnet/FlowAnalysis.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module HashCreateToUseFlow = CreationToUseFlow<HashAlgorithmCreateCall, HashUse>
8484

8585
module CryptoStreamFlow = CreationToUseFlow<CryptoStreamCreation, CryptoStreamUse>;
8686

87-
module AesModeFlow = CreationToUseFlow<AesModeCreation, AesModeUse>;
87+
module AeadFlow = CreationToUseFlow<AeadCreation, AeadUse>;
8888

8989
module SymmetricAlgorithmFlow =
9090
CreationToUseFlow<SymmetricAlgorithmCreation, SymmetricAlgorithmUse>;

csharp/ql/lib/experimental/quantum/dotnet/OperationInstances.qll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,21 @@ class CryptoStreamOperationInstance extends Crypto::KeyOperationInstance instanc
144144
}
145145

146146
/**
147-
* A call to either `Encrypt` or `Decrypt` on an `AesGcm` or `AesCcm` instance.
147+
* A call to either `Encrypt` or `Decrypt` on an `AesGcm`, `AesCcm`, or
148+
* `ChaCha20Poly1305` instance.
148149
*/
149-
class AesModeOperationInstance extends Crypto::KeyOperationInstance instanceof AesModeUse {
150+
class AeadOperationInstance extends Crypto::KeyOperationInstance instanceof AeadUse {
150151
override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() {
151-
// See `AesModeAlgorithmInstance` for the algorithm value consumer.
152+
// See `AeadModeAlgorithmValueConsumer` for the algorithm value consumer.
152153
result = this
153154
}
154155

155156
override Crypto::KeyOperationSubtype getKeyOperationSubtype() {
156-
result = this.(AesModeUse).getKeyOperationSubtype()
157+
result = this.(AeadUse).getKeyOperationSubtype()
157158
}
158159

159160
override Crypto::ConsumerInputDataFlowNode getKeyConsumer() {
160-
result.asExpr() = AesModeFlow::getCreationFromUse(this, _, _).getKeyArg()
161+
result.asExpr() = AeadFlow::getCreationFromUse(this, _, _).getKeyArg()
161162
}
162163

163164
override Crypto::ConsumerInputDataFlowNode getNonceConsumer() {

0 commit comments

Comments
 (0)