@@ -9,11 +9,19 @@ signature module InputSig<LocationSig Location> {
99 class LocatableElement {
1010 Location getLocation ( ) ;
1111 }
12+
13+ class UnknownLocation instanceof Location ;
1214}
1315
1416module CryptographyBase< LocationSig Location, InputSig< Location > Input> {
1517 final class LocatableElement = Input:: LocatableElement ;
1618
19+ final class UnknownLocation = Input:: UnknownLocation ;
20+
21+ final class UnknownPropertyValue extends string {
22+ UnknownPropertyValue ( ) { this = "<unknown>" }
23+ }
24+
1725 abstract class NodeBase instanceof LocatableElement {
1826 /**
1927 * 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> {
2533 */
2634 Location getLocation ( ) { result = super .getLocation ( ) }
2735
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+
2841 /**
2942 * Returns the child of this node with the given edge name.
3043 *
3144 * This predicate is used by derived classes to construct the graph of cryptographic operations.
3245 */
33- NodeBase getChild ( string edgeName ) { edgeName = "origin" and result = this . getOrigin ( ) }
46+ NodeBase getChild ( string edgeName ) { none ( ) }
3447
3548 /**
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.
3752 */
38- NodeBase getOrigin ( ) { none ( ) }
53+ predicate properties ( string key , string value , Location location ) {
54+ key = "origin" and location = this .getOrigin ( value ) .getLocation ( )
55+ }
3956
4057 /**
4158 * Returns the parent of this node.
@@ -86,7 +103,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
86103 abstract class HashOperation extends Operation {
87104 abstract override HashAlgorithm getAlgorithm ( ) ;
88105
89- override string getOperationName ( ) { result = "hash " }
106+ override string getOperationName ( ) { result = "HASH " }
90107 }
91108
92109 // 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> {
105122 MD5 ( ) or
106123 SHA1 ( ) or
107124 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 ( )
121127
122128 /**
123129 * A hashing algorithm that transforms variable-length input into a fixed-size hash value.
124130 */
125131 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 ) }
127147
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 ( ) ;
129152 }
130153
131154 /**
132155 * An operation that derives one or more keys from an input value.
133156 */
134157 abstract class KeyDerivationOperation extends Operation {
135- override string getOperationName ( ) { result = "key derivation " }
158+ override string getOperationName ( ) { result = "KEY_DERIVATION " }
136159 }
137160
138161 /**
@@ -143,7 +166,7 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
143166 }
144167
145168 /**
146- * HKDF Extract+Expand key derivation function.
169+ * HKDF key derivation function
147170 */
148171 abstract class HKDF extends KeyDerivationAlgorithm {
149172 final override string getAlgorithmName ( ) { result = "HKDF" }
@@ -157,6 +180,9 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
157180 }
158181 }
159182
183+ /**
184+ * PKCS #12 key derivation function
185+ */
160186 abstract class PKCS12KDF extends KeyDerivationAlgorithm {
161187 final override string getAlgorithmName ( ) { result = "PKCS12KDF" }
162188
@@ -168,4 +194,31 @@ module CryptographyBase<LocationSig Location, InputSig<Location> Input> {
168194 edgeName = "digest" and result = this .getHashAlgorithm ( )
169195 }
170196 }
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+ }
171224}
0 commit comments