Skip to content

Commit 9970e58

Browse files
committed
quantum-c#: add HashAlgorithms
1 parent d690a34 commit 9970e58

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ class SigningNamedCurvePropertyAccess extends PropertyAccess {
6060
string getCurveName() { result = curveName }
6161
}
6262

63+
class HashAlgorithmNameType extends CryptographyType {
64+
HashAlgorithmNameType() { this.hasName("HashAlgorithmName") }
65+
}
66+
67+
class HashAlgorithmName extends PropertyAccess {
68+
string algorithmName;
69+
70+
HashAlgorithmName() {
71+
this.getType() instanceof HashAlgorithmNameType and
72+
this.getProperty().getName() = algorithmName
73+
}
74+
75+
string getAlgorithmName() { result = algorithmName }
76+
}
77+
6378
/**
6479
* Private predicate mapping NIST names to SEC names and leaving all others the same.
6580
*/
@@ -76,12 +91,12 @@ private predicate eccurveNameMapping(string nist, string secp) {
7691
}
7792

7893
// OPERATION INSTANCES
79-
private class ECDsaClass extends Type {
80-
ECDsaClass() { this.hasFullyQualifiedName("System.Security.Cryptography", "ECDsa") }
94+
private class ECDsaClass extends CryptographyType {
95+
ECDsaClass() { this.hasName("ECDsa") }
8196
}
8297

83-
private class RSAClass extends Type {
84-
RSAClass() { this.hasFullyQualifiedName("System.Security.Cryptography", "RSA") }
98+
private class RSAClass extends CryptographyType {
99+
RSAClass() { this.hasName("RSA") }
85100
}
86101

87102
class ByteArrayType extends Type {
@@ -92,7 +107,7 @@ class ReadOnlyByteSpanType extends Type {
92107
ReadOnlyByteSpanType() { this.getName() = "ReadOnlySpan<Byte>" }
93108
}
94109

95-
abstract class DotNetSigner extends MethodCall {
110+
class DotNetSigner extends MethodCall {
96111
DotNetSigner() { this.getTarget().getName().matches(["Verify%", "Sign%"]) }
97112

98113
Expr getMessageArg() {
@@ -117,6 +132,11 @@ abstract class DotNetSigner extends MethodCall {
117132
result = this
118133
}
119134

135+
Expr getHashAlgorithmArg() {
136+
// Get the hash algorithm argument if it has the correct type.
137+
result = this.getAnArgument() and result.getType() instanceof HashAlgorithmNameType
138+
}
139+
120140
predicate isSigner() { this.getTarget().getName().matches("Sign%") }
121141

122142
predicate isVerifier() { this.getTarget().getName().matches("Verify%") }

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ class ECDsaORRSASigningOperationInstance extends Crypto::SignatureOperationInsta
99
CryptographyCreateCall creator;
1010

1111
ECDsaORRSASigningOperationInstance() {
12-
this instanceof DotNetSigner and
1312
CryptographyCreateToUseFlow::flow(DataFlow::exprNode(creator), DataFlow::exprNode(this))
1413
}
1514

16-
// TODO FIXME
1715
override Crypto::AlgorithmValueConsumer getAnAlgorithmValueConsumer() {
1816
result = creator.getAlgorithmArg()
17+
or
18+
// FIXME: currently not working
19+
result = super.getHashAlgorithmArg()
1920
}
2021

2122
override Crypto::KeyOperationSubtype getKeyOperationSubtype() {

0 commit comments

Comments
 (0)