Skip to content

Commit 7836234

Browse files
committed
WIP: hash types example and documentation
1 parent 1a7d8cb commit 7836234

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

cpp/ql/lib/experimental/Quantum/Base.qll

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
6464
override NodeBase getChild(string edgeName) {
6565
result = super.getChild(edgeName)
6666
or
67-
edgeName = "algorithm" and
67+
edgeName = "uses" and
6868
if exists(this.getAlgorithm()) then result = this.getAlgorithm() else result = this
6969
}
7070
}
@@ -89,13 +89,43 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
8989
override string getOperationName() { result = "hash" }
9090
}
9191

92+
// Rule: no newtype representing a type of algorithm should be modelled with multiple interfaces
93+
//
94+
// Example: HKDF and PKCS12KDF are both key derivation algorithms.
95+
// However, PKCS12KDF also has a property: the iteration count.
96+
//
97+
// If we have HKDF and PKCS12KDF under TKeyDerivationType,
98+
// someone modelling a library might try to make a generic identification of both of those algorithms.
99+
//
100+
// They will therefore not use the specialized type for PKCS12KDF,
101+
// meaning "from PKCS12KDF algo select algo" will have no results.
102+
//
103+
newtype THashType =
104+
// We're saying by this that all of these have an identical interface / properties / edges
105+
MD5() or
106+
SHA1() or
107+
SHA256() or
108+
SHA512()
109+
110+
class HashAlgorithmType extends THashType {
111+
string toString() { hashTypeToNameMapping(this, result) }
112+
}
113+
114+
predicate hashTypeToNameMapping(THashType type, string name) {
115+
type instanceof SHA1 and name = "SHA-1"
116+
or
117+
type instanceof SHA256 and name = "SHA-256"
118+
or
119+
type instanceof SHA512 and name = "SHA-512"
120+
}
121+
92122
/**
93123
* A hashing algorithm that transforms variable-length input into a fixed-size hash value.
94124
*/
95-
abstract class HashAlgorithm extends Algorithm { }
125+
abstract class HashAlgorithm extends Algorithm {
126+
abstract HashAlgorithmType getHashType();
96127

97-
abstract class SHA1 extends HashAlgorithm {
98-
override string getAlgorithmName() { result = "SHA1" }
128+
override string getAlgorithmName() { hashTypeToNameMapping(this.getHashType(), result) }
99129
}
100130

101131
/**

cpp/ql/lib/experimental/Quantum/OpenSSL.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module OpenSSLModel {
66

77
abstract class KeyDerivationOperation extends Crypto::KeyDerivationOperation { }
88

9-
class SHA1Algo extends Crypto::SHA1 instanceof MacroAccess {
9+
class SHA1Algo extends Crypto::HashAlgorithm instanceof MacroAccess {
1010
SHA1Algo() { this.getMacro().getName() = "SN_sha1" }
11+
12+
override Crypto::HashAlgorithmType getHashType() { result instanceof Crypto::SHA1 }
1113
}
1214

1315
module AlgorithmToEVPKeyDeriveConfig implements DataFlow::ConfigSig {

0 commit comments

Comments
 (0)