|
| 1 | +/** |
| 2 | + * A language-independent library for reasoning about cryptography. |
| 3 | + */ |
| 4 | + |
| 5 | +import codeql.util.Location |
| 6 | +import codeql.util.Option |
| 7 | + |
| 8 | +signature module InputSig<LocationSig Location> { |
| 9 | + class KnownUnknownLocation extends Location; |
| 10 | + |
| 11 | + class LocatableElement { |
| 12 | + Location getLocation(); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +module CryptographyBase<LocationSig Location, InputSig<Location> Input> { |
| 17 | + final class LocatableElement = Input::LocatableElement; |
| 18 | + |
| 19 | + newtype TNode = |
| 20 | + TNodeUnknown() or |
| 21 | + TNodeAsset() or |
| 22 | + TNodeValue() // currently unused |
| 23 | + |
| 24 | + class KnownNode = TNodeAsset or TNodeValue; |
| 25 | + |
| 26 | + abstract class NodeBase extends TNode { |
| 27 | + /** |
| 28 | + * Returns a string representation of this node, usually the name of the operation/algorithm/property. |
| 29 | + */ |
| 30 | + abstract string toString(); |
| 31 | + |
| 32 | + /** |
| 33 | + * Returns the location of this node in the code. |
| 34 | + */ |
| 35 | + abstract Location getLocation(); |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the child of this node with the given edge name. |
| 39 | + * |
| 40 | + * This predicate is used by derived classes to construct the graph of cryptographic operations. |
| 41 | + */ |
| 42 | + NodeBase getChild(string edgeName) { none() } |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns the parent of this node. |
| 46 | + */ |
| 47 | + final NodeBase getAParent() { result.getChild(_) = this } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * A node representing an unknown value. |
| 52 | + * |
| 53 | + * If a property should have a value but that value is unknown, `UnknownNode` to represent that value. |
| 54 | + */ |
| 55 | + final class UnknownNode extends NodeBase, TNodeUnknown { |
| 56 | + override string toString() { result = "unknown" } |
| 57 | + |
| 58 | + override Location getLocation() { result instanceof Input::KnownUnknownLocation } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * A node with a known location in the code. |
| 63 | + */ |
| 64 | + abstract class LocatableNode extends NodeBase, TNodeAsset { |
| 65 | + abstract LocatableElement toElement(); |
| 66 | + |
| 67 | + override Location getLocation() { result = this.toElement().getLocation() } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * A node representing a known asset, i.e., an algorithm, operation, or property. |
| 72 | + */ |
| 73 | + class Asset = LocatableNode; |
| 74 | + |
| 75 | + /** |
| 76 | + * A cryptographic operation, such as hashing or encryption. |
| 77 | + */ |
| 78 | + abstract class Operation extends Asset { |
| 79 | + /** |
| 80 | + * Gets the algorithm associated with this operation. |
| 81 | + */ |
| 82 | + private NodeBase getAlgorithmOrUnknown() { |
| 83 | + if exists(this.getAlgorithm()) |
| 84 | + then result = this.getAlgorithm() |
| 85 | + else result instanceof UnknownNode |
| 86 | + } |
| 87 | + |
| 88 | + abstract Algorithm getAlgorithm(); |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets the name of this operation, e.g., "hash" or "encrypt". |
| 92 | + */ |
| 93 | + abstract string getOperationName(); |
| 94 | + |
| 95 | + final override string toString() { result = this.getOperationName() } |
| 96 | + |
| 97 | + override NodeBase getChild(string edgeName) { |
| 98 | + edgeName = "algorithm" and |
| 99 | + this.getAlgorithmOrUnknown() = result |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + abstract class Algorithm extends Asset { |
| 104 | + /** |
| 105 | + * Gets the name of this algorithm, e.g., "AES" or "SHA". |
| 106 | + */ |
| 107 | + abstract string getAlgorithmName(); |
| 108 | + |
| 109 | + final override string toString() { result = this.getAlgorithmName() } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * A hashing operation that processes data to generate a hash value. |
| 114 | + * This operation takes an input message of arbitrary content and length and produces a fixed-size |
| 115 | + * hash value as the output using a specified hashing algorithm. |
| 116 | + */ |
| 117 | + abstract class HashOperation extends Operation { |
| 118 | + abstract override HashAlgorithm getAlgorithm(); |
| 119 | + |
| 120 | + override string getOperationName() { result = "hash" } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * A hashing algorithm that transforms variable-length input into a fixed-size hash value. |
| 125 | + */ |
| 126 | + abstract class HashAlgorithm extends Algorithm { } |
| 127 | + |
| 128 | + /** |
| 129 | + * An operation that derives one or more keys from an input value. |
| 130 | + */ |
| 131 | + abstract class KeyDerivationOperation extends Operation { |
| 132 | + override string getOperationName() { result = "key derivation" } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * An algorithm that derives one or more keys from an input value. |
| 137 | + */ |
| 138 | + abstract class KeyDerivationAlgorithm extends Algorithm { |
| 139 | + abstract override string getAlgorithmName(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * HKDF Extract+Expand key derivation function. |
| 144 | + */ |
| 145 | + abstract class HKDFAlgorithm extends KeyDerivationAlgorithm { |
| 146 | + final override string getAlgorithmName() { result = "HKDF" } |
| 147 | + |
| 148 | + private NodeBase getHashAlgorithmOrUnknown() { |
| 149 | + if exists(this.getHashAlgorithm()) |
| 150 | + then result = this.getHashAlgorithm() |
| 151 | + else result instanceof UnknownNode |
| 152 | + } |
| 153 | + |
| 154 | + abstract HashAlgorithm getHashAlgorithm(); |
| 155 | + |
| 156 | + /** |
| 157 | + * digest:HashAlgorithm |
| 158 | + */ |
| 159 | + override NodeBase getChild(string edgeName) { |
| 160 | + result = super.getChild(edgeName) |
| 161 | + or |
| 162 | + edgeName = "digest" and result = this.getHashAlgorithmOrUnknown() |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments