Skip to content

Commit 1762959

Browse files
committed
quantum-c#: Add HashAlgorithmInstance
1 parent 9970e58 commit 1762959

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,20 @@ class SigningNamedCurveAlgorithmInstance extends Crypto::EllipticCurveInstance i
2424
Crypto::ellipticCurveNameToKeySizeAndFamilyMapping(this.getRawEllipticCurveName(), result, _)
2525
}
2626
}
27+
28+
class HashAlgorithmInstance extends Crypto::HashAlgorithmInstance instanceof HashAlgorithmName {
29+
HashAlgorithmConsumer consumer;
30+
31+
HashAlgorithmInstance() {
32+
HashAlgorithmNameToUse::flow(DataFlow::exprNode(this), consumer.getInputNode())
33+
}
34+
35+
// Q: super.getHashFamily does not work because it is ambigous. But super.(HashAlgorithmName) does not work either.
36+
override Crypto::THashType getHashFamily() { result = this.(HashAlgorithmName).getHashFamily() }
37+
38+
override string getRawHashAlgorithmName() { result = super.getAlgorithmName() }
39+
40+
override int getFixedDigestLength() { result = this.(HashAlgorithmName).getFixedDigestLength() }
41+
42+
Crypto::AlgorithmValueConsumer getConsumer() { result = consumer }
43+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ class ECDsaAlgorithmValueConsumer extends Crypto::AlgorithmValueConsumer {
1414
exists(SigningNamedCurveAlgorithmInstance l | l.getConsumer() = this and result = l)
1515
}
1616
}
17+
18+
class HashAlgorithmConsumer extends Crypto::AlgorithmValueConsumer {
19+
HashAlgorithmUser call;
20+
21+
HashAlgorithmConsumer() { this = call.getHashAlgorithmUser() }
22+
23+
override Crypto::ConsumerInputDataFlowNode getInputNode() { result.asExpr() = this }
24+
25+
override Crypto::AlgorithmInstance getAKnownAlgorithmSource() {
26+
exists(HashAlgorithmInstance l | l.getConsumer() = this and result = l)
27+
}
28+
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
private import csharp
2+
private import experimental.quantum.Language
23

34
// This class models Create calls for the ECDsa and RSA classes in .NET.
45
class CryptographyCreateCall extends MethodCall {
@@ -14,6 +15,15 @@ class CryptographyCreateCall extends MethodCall {
1415
or
1516
result = this.(ECDsaCreateCallWithECCurve).getArgument(0)
1617
}
18+
19+
Expr getKeyConsumer() {
20+
this.hasNoArguments() and result = this
21+
or
22+
result = this.(ECDsaCreateCallWithParameters).getArgument(0)
23+
or
24+
result = this.(ECDsaCreateCallWithECCurve)
25+
}
26+
1727
}
1828

1929
class ECDsaCreateCall extends CryptographyCreateCall {
@@ -73,6 +83,42 @@ class HashAlgorithmName extends PropertyAccess {
7383
}
7484

7585
string getAlgorithmName() { result = algorithmName }
86+
87+
Crypto::THashType getHashFamily() { hashAlgorithmToFamily(this.getAlgorithmName(), result, _) }
88+
89+
int getFixedDigestLength() { hashAlgorithmToFamily(this.getAlgorithmName(), _, result) }
90+
}
91+
92+
private predicate hashAlgorithmToFamily(
93+
string hashName, Crypto::THashType hashFamily, int digestLength
94+
) {
95+
hashName = "MD5" and hashFamily = Crypto::MD5() and digestLength = 128
96+
or
97+
hashName = "SHA1" and hashFamily = Crypto::SHA1() and digestLength = 160
98+
or
99+
hashName = "SHA256" and hashFamily = Crypto::SHA2() and digestLength = 256
100+
or
101+
hashName = "SHA384" and hashFamily = Crypto::SHA2() and digestLength = 384
102+
or
103+
hashName = "SHA512" and hashFamily = Crypto::SHA2() and digestLength = 512
104+
or
105+
hashName = "SHA3_256" and hashFamily = Crypto::SHA3() and digestLength = 256
106+
or
107+
hashName = "SHA3_384" and hashFamily = Crypto::SHA3() and digestLength = 384
108+
or
109+
hashName = "SHA3_512" and hashFamily = Crypto::SHA3() and digestLength = 512
110+
// Q: is there an idiomatic way to add a default type here?
111+
}
112+
113+
class HashAlgorithmUser extends MethodCall {
114+
Expr arg;
115+
116+
HashAlgorithmUser() {
117+
arg = this.getAnArgument() and
118+
arg.getType() instanceof HashAlgorithmNameType
119+
}
120+
121+
Expr getHashAlgorithmUser() { result = arg }
76122
}
77123

78124
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ private module CreateToUseFlowConfig implements DataFlow::ConfigSig {
3030
}
3131

3232
module CryptographyCreateToUseFlow = DataFlow::Global<CreateToUseFlowConfig>;
33+
34+
module HashAlgorithmNameToUseConfig implements DataFlow::ConfigSig {
35+
predicate isSource(DataFlow::Node src) { src.asExpr() instanceof HashAlgorithmName }
36+
37+
predicate isSink(DataFlow::Node sink) {
38+
exists(HashAlgorithmConsumer consumer | sink = consumer.getInputNode())
39+
}
40+
}
41+
42+
module HashAlgorithmNameToUse = DataFlow::Global<HashAlgorithmNameToUseConfig>;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class ECDsaORRSASigningOperationInstance extends Crypto::SignatureOperationInsta
1515
override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() {
1616
result = creator.getAlgorithmArg()
1717
or
18-
// FIXME: currently not working
1918
result = super.getHashAlgorithmArg()
2019
}
2120

@@ -28,9 +27,8 @@ class ECDsaORRSASigningOperationInstance extends Crypto::SignatureOperationInsta
2827
else result = Crypto::TUnknownKeyOperationMode()
2928
}
3029

31-
// TODO FIXME
3230
override Crypto::ConsumerInputDataFlowNode getKeyConsumer() {
33-
result.asExpr() = creator.getAlgorithmArg()
31+
result.asExpr() = creator.getKeyConsumer()
3432
}
3533

3634
override Crypto::ConsumerInputDataFlowNode getNonceConsumer() { none() }

0 commit comments

Comments
 (0)