Skip to content

Commit 8b9e5b4

Browse files
authored
Merge pull request #19623 from trailofbits/fegge/quantum-signatures
Quantum: Added signature input nodes to signature verify operation nodes
2 parents 51ef76a + d0739b2 commit 8b9e5b4

File tree

1 file changed

+69
-5
lines changed
  • shared/quantum/codeql/quantum/experimental

1 file changed

+69
-5
lines changed

shared/quantum/codeql/quantum/experimental/Model.qll

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
424424
final override ConsumerInputDataFlowNode getInputNode() { result = inputNode }
425425
}
426426

427+
final private class SignatureArtifactConsumer extends ArtifactConsumerAndInstance {
428+
ConsumerInputDataFlowNode inputNode;
429+
430+
SignatureArtifactConsumer() {
431+
exists(SignatureOperationInstance op | inputNode = op.getSignatureConsumer()) and
432+
this = Input::dfn_to_element(inputNode)
433+
}
434+
435+
final override ConsumerInputDataFlowNode getInputNode() { result = inputNode }
436+
}
437+
427438
/**
428439
* An artifact that is produced by an operation, representing a concrete artifact instance rather than a synthetic consumer artifact.
429440
*/
@@ -458,6 +469,8 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
458469
}
459470

460471
override DataFlowNode getOutputNode() { result = creator.getOutputArtifact() }
472+
473+
KeyOperationInstance getCreator() { result = creator }
461474
}
462475

463476
/**
@@ -782,6 +795,17 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
782795
abstract ArtifactOutputDataFlowNode getOutputArtifact();
783796
}
784797

798+
/**
799+
* A key operation instance representing a signature being generated or verified.
800+
*/
801+
abstract class SignatureOperationInstance extends KeyOperationInstance {
802+
/**
803+
* Gets the consumer of the signature that is being verified in case of a
804+
* verification operation.
805+
*/
806+
abstract ConsumerInputDataFlowNode getSignatureConsumer();
807+
}
808+
785809
/**
786810
* A key-based algorithm instance used in cryptographic operations such as encryption, decryption,
787811
* signing, verification, and key wrapping.
@@ -1264,6 +1288,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
12641288
TNonceInput(NonceArtifactConsumer e) or
12651289
TMessageInput(MessageArtifactConsumer e) or
12661290
TSaltInput(SaltArtifactConsumer e) or
1291+
TSignatureInput(SignatureArtifactConsumer e) or
12671292
TRandomNumberGeneration(RandomNumberGenerationInstance e) { e.flowsTo(_) } or
12681293
// Key Creation Operation union type (e.g., key generation, key load)
12691294
TKeyCreationOperation(KeyCreationOperationInstance e) or
@@ -1325,14 +1350,14 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
13251350
/**
13261351
* Returns the child of this node with the given edge name.
13271352
*
1328-
* This predicate is overriden by derived classes to construct the graph of cryptographic operations.
1353+
* This predicate is overridden by derived classes to construct the graph of cryptographic operations.
13291354
*/
13301355
NodeBase getChild(string edgeName) { none() }
13311356

13321357
/**
13331358
* Defines properties of this node by name and either a value or location or both.
13341359
*
1335-
* This predicate is overriden by derived classes to construct the graph of cryptographic operations.
1360+
* This predicate is overridden by derived classes to construct the graph of cryptographic operations.
13361361
*/
13371362
predicate properties(string key, string value, Location location) { none() }
13381363

@@ -1505,6 +1530,20 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
15051530
override LocatableElement asElement() { result = instance }
15061531
}
15071532

1533+
/**
1534+
* A signature input. This may represent a signature, or a signature component
1535+
* such as the scalar values r and s in ECDSA.
1536+
*/
1537+
final class SignatureArtifactNode extends ArtifactNode, TSignatureInput {
1538+
SignatureArtifactConsumer instance;
1539+
1540+
SignatureArtifactNode() { this = TSignatureInput(instance) }
1541+
1542+
final override string getInternalType() { result = "SignatureInput" }
1543+
1544+
override LocatableElement asElement() { result = instance }
1545+
}
1546+
15081547
/**
15091548
* A salt input.
15101549
*/
@@ -1528,13 +1567,22 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
15281567

15291568
KeyOperationOutputNode() { this = TKeyOperationOutput(instance) }
15301569

1531-
final override string getInternalType() { result = "KeyOperationOutput" }
1570+
override string getInternalType() { result = "KeyOperationOutput" }
15321571

15331572
override LocatableElement asElement() { result = instance }
15341573

15351574
override string getSourceNodeRelationship() { none() }
15361575
}
15371576

1577+
class SignOperationOutputNode extends KeyOperationOutputNode {
1578+
SignOperationOutputNode() {
1579+
this.asElement().(KeyOperationOutputArtifactInstance).getCreator().getKeyOperationSubtype() =
1580+
TSignMode()
1581+
}
1582+
1583+
override string getInternalType() { result = "SignatureOutput" }
1584+
}
1585+
15381586
/**
15391587
* A source of random number generation.
15401588
*/
@@ -2107,6 +2155,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
21072155
}
21082156

21092157
class SignatureOperationNode extends KeyOperationNode {
2158+
override SignatureOperationInstance instance;
21102159
string nodeName;
21112160

21122161
SignatureOperationNode() {
@@ -2116,6 +2165,21 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
21162165
}
21172166

21182167
override string getInternalType() { result = nodeName }
2168+
2169+
SignatureArtifactNode getASignatureArtifact() {
2170+
result.asElement() = instance.getSignatureConsumer().getConsumer()
2171+
}
2172+
2173+
override NodeBase getChild(string key) {
2174+
result = super.getChild(key)
2175+
or
2176+
// [KNOWN_OR_UNKNOWN] - only if we know the type is verify
2177+
this.getKeyOperationSubtype() = TVerifyMode() and
2178+
key = "Signature" and
2179+
if exists(this.getASignatureArtifact())
2180+
then result = this.getASignatureArtifact()
2181+
else result = this
2182+
}
21192183
}
21202184

21212185
/**
@@ -2563,15 +2627,15 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
25632627
or
25642628
curveName = "CURVE25519" and keySize = 255 and curveFamily = CURVE25519()
25652629
or
2630+
curveName = "CURVE448" and keySize = 448 and curveFamily = CURVE448()
2631+
or
25662632
// TODO: separate these into key agreement logic or sign/verify (ECDSA / ECDH)
25672633
// or
25682634
// curveName = "X25519" and keySize = 255 and curveFamily = CURVE25519()
25692635
// or
25702636
// curveName = "ED25519" and keySize = 255 and curveFamily = CURVE25519()
25712637
// or
25722638
// curveName = "ED448" and keySize = 448 and curveFamily = CURVE448()
2573-
// curveName = "CURVE448" and keySize = 448 and curveFamily = CURVE448()
2574-
// or
25752639
// or
25762640
// curveName = "X448" and keySize = 448 and curveFamily = CURVE448()
25772641
curveName = "SM2" and keySize in [256, 512] and curveFamily = SM2()

0 commit comments

Comments
 (0)