@@ -9,11 +9,19 @@ signature module InputSig<LocationSig Location> {
9
9
class LocatableElement {
10
10
Location getLocation ( ) ;
11
11
}
12
+
13
+ class UnknownLocation instanceof Location ;
12
14
}
13
15
14
16
module CryptographyBase< LocationSig Location, InputSig< Location > Input> {
15
17
final class LocatableElement = Input:: LocatableElement ;
16
18
19
+ final class UnknownLocation = Input:: UnknownLocation ;
20
+
21
+ final class UnknownPropertyValue extends string {
22
+ UnknownPropertyValue ( ) { this = "<unknown>" }
23
+ }
24
+
17
25
abstract class NodeBase instanceof LocatableElement {
18
26
/**
19
27
* Returns a string representation of this node, usually the name of the operation/algorithm/property.
@@ -25,17 +33,26 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
25
33
*/
26
34
Location getLocation ( ) { result = super .getLocation ( ) }
27
35
36
+ /**
37
+ * Gets the origin of this node, e.g., a string literal in source describing it.
38
+ */
39
+ LocatableElement getOrigin ( string value ) { none ( ) }
40
+
28
41
/**
29
42
* Returns the child of this node with the given edge name.
30
43
*
31
44
* This predicate is used by derived classes to construct the graph of cryptographic operations.
32
45
*/
33
- NodeBase getChild ( string edgeName ) { edgeName = "origin" and result = this . getOrigin ( ) }
46
+ NodeBase getChild ( string edgeName ) { none ( ) }
34
47
35
48
/**
36
- * Gets the origin of this node, e.g., a string literal in source describing it.
49
+ * Defines properties of this node by name and either a value or location or both.
50
+ *
51
+ * This predicate is used by derived classes to construct the graph of cryptographic operations.
37
52
*/
38
- NodeBase getOrigin ( ) { none ( ) }
53
+ predicate properties ( string key , string value , Location location ) {
54
+ key = "origin" and location = this .getOrigin ( value ) .getLocation ( )
55
+ }
39
56
40
57
/**
41
58
* Returns the parent of this node.
@@ -86,7 +103,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
86
103
abstract class HashOperation extends Operation {
87
104
abstract override HashAlgorithm getAlgorithm ( ) ;
88
105
89
- override string getOperationName ( ) { result = "hash " }
106
+ override string getOperationName ( ) { result = "HASH " }
90
107
}
91
108
92
109
// Rule: no newtype representing a type of algorithm should be modelled with multiple interfaces
@@ -105,34 +122,40 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
105
122
MD5 ( ) or
106
123
SHA1 ( ) or
107
124
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
- }
125
+ SHA512 ( ) or
126
+ OtherHashType ( )
121
127
122
128
/**
123
129
* A hashing algorithm that transforms variable-length input into a fixed-size hash value.
124
130
*/
125
131
abstract class HashAlgorithm extends Algorithm {
126
- abstract HashAlgorithmType getHashType ( ) ;
132
+ final predicate hashTypeToNameMapping ( THashType type , string name ) {
133
+ type instanceof MD5 and name = "MD5"
134
+ or
135
+ type instanceof SHA1 and name = "SHA-1"
136
+ or
137
+ type instanceof SHA256 and name = "SHA-256"
138
+ or
139
+ type instanceof SHA512 and name = "SHA-512"
140
+ or
141
+ type instanceof OtherHashType and name = this .getRawAlgorithmName ( )
142
+ }
143
+
144
+ abstract THashType getHashType ( ) ;
145
+
146
+ override string getAlgorithmName ( ) { this .hashTypeToNameMapping ( this .getHashType ( ) , result ) }
127
147
128
- override string getAlgorithmName ( ) { hashTypeToNameMapping ( this .getHashType ( ) , result ) }
148
+ /**
149
+ * Gets the raw name of this hash algorithm from source.
150
+ */
151
+ abstract string getRawAlgorithmName ( ) ;
129
152
}
130
153
131
154
/**
132
155
* An operation that derives one or more keys from an input value.
133
156
*/
134
157
abstract class KeyDerivationOperation extends Operation {
135
- override string getOperationName ( ) { result = "key derivation " }
158
+ override string getOperationName ( ) { result = "KEY_DERIVATION " }
136
159
}
137
160
138
161
/**
@@ -143,7 +166,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
143
166
}
144
167
145
168
/**
146
- * HKDF Extract+Expand key derivation function.
169
+ * HKDF key derivation function
147
170
*/
148
171
abstract class HKDF extends KeyDerivationAlgorithm {
149
172
final override string getAlgorithmName ( ) { result = "HKDF" }
@@ -157,6 +180,9 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
157
180
}
158
181
}
159
182
183
+ /**
184
+ * PKCS #12 key derivation function
185
+ */
160
186
abstract class PKCS12KDF extends KeyDerivationAlgorithm {
161
187
final override string getAlgorithmName ( ) { result = "PKCS12KDF" }
162
188
@@ -168,4 +194,31 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
168
194
edgeName = "digest" and result = this .getHashAlgorithm ( )
169
195
}
170
196
}
197
+
198
+ /**
199
+ * Elliptic curve algorithm
200
+ */
201
+ abstract class EllipticCurve extends Algorithm {
202
+ abstract string getVersion ( Location location ) ;
203
+
204
+ abstract string getKeySize ( Location location ) ;
205
+
206
+ override predicate properties ( string key , string value , Location location ) {
207
+ super .properties ( key , value , location )
208
+ or
209
+ key = "version" and
210
+ if exists ( this .getVersion ( location ) )
211
+ then value = this .getVersion ( location )
212
+ else (
213
+ value instanceof UnknownPropertyValue and location instanceof UnknownLocation
214
+ )
215
+ or
216
+ key = "key_size" and
217
+ if exists ( this .getKeySize ( location ) )
218
+ then value = this .getKeySize ( location )
219
+ else (
220
+ value instanceof UnknownPropertyValue and location instanceof UnknownLocation
221
+ )
222
+ }
223
+ }
171
224
}
0 commit comments