@@ -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 /**
0 commit comments